2 * QEMU VNC display driver: SASL auth protocol
4 * Copyright (C) 2009 Red Hat, Inc
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 /* Max amount of data we send/recv for SASL steps to prevent DOS */
28 #define SASL_DATA_MAX_LEN (1024 * 1024)
31 void vnc_sasl_client_cleanup(VncState
*vs
)
34 vs
->sasl
.runSSF
= vs
->sasl
.waitWriteSSF
= vs
->sasl
.wantSSF
= 0;
35 vs
->sasl
.encodedLength
= vs
->sasl
.encodedOffset
= 0;
36 vs
->sasl
.encoded
= NULL
;
37 free(vs
->sasl
.username
);
38 free(vs
->sasl
.mechlist
);
39 vs
->sasl
.username
= vs
->sasl
.mechlist
= NULL
;
40 sasl_dispose(&vs
->sasl
.conn
);
46 long vnc_client_write_sasl(VncState
*vs
)
50 VNC_DEBUG("Write SASL: Pending output %p size %d offset %d Encoded: %p size %d offset %d\n",
51 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
52 vs
->sasl
.encoded
, vs
->sasl
.encodedLength
, vs
->sasl
.encodedOffset
);
54 if (!vs
->sasl
.encoded
) {
56 err
= sasl_encode(vs
->sasl
.conn
,
57 (char *)vs
->output
.buffer
,
59 (const char **)&vs
->sasl
.encoded
,
60 &vs
->sasl
.encodedLength
);
62 return vnc_client_io_error(vs
, -1, EIO
);
64 vs
->sasl
.encodedOffset
= 0;
67 ret
= vnc_client_write_buf(vs
,
68 vs
->sasl
.encoded
+ vs
->sasl
.encodedOffset
,
69 vs
->sasl
.encodedLength
- vs
->sasl
.encodedOffset
);
73 vs
->sasl
.encodedOffset
+= ret
;
74 if (vs
->sasl
.encodedOffset
== vs
->sasl
.encodedLength
) {
75 vs
->output
.offset
= 0;
76 vs
->sasl
.encoded
= NULL
;
77 vs
->sasl
.encodedOffset
= vs
->sasl
.encodedLength
= 0;
80 /* Can't merge this block with one above, because
81 * someone might have written more unencrypted
82 * data in vs->output while we were processing
85 if (vs
->output
.offset
== 0) {
86 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
93 long vnc_client_read_sasl(VncState
*vs
)
96 uint8_t encoded
[4096];
98 unsigned int decodedLen
;
101 ret
= vnc_client_read_buf(vs
, encoded
, sizeof(encoded
));
105 err
= sasl_decode(vs
->sasl
.conn
,
106 (char *)encoded
, ret
,
107 &decoded
, &decodedLen
);
110 return vnc_client_io_error(vs
, -1, -EIO
);
111 VNC_DEBUG("Read SASL Encoded %p size %ld Decoded %p size %d\n",
112 encoded
, ret
, decoded
, decodedLen
);
113 buffer_reserve(&vs
->input
, decodedLen
);
114 buffer_append(&vs
->input
, decoded
, decodedLen
);
119 static int vnc_auth_sasl_check_access(VncState
*vs
)
125 err
= sasl_getprop(vs
->sasl
.conn
, SASL_USERNAME
, &val
);
126 if (err
!= SASL_OK
) {
127 VNC_DEBUG("cannot query SASL username on connection %d (%s), denying access\n",
128 err
, sasl_errstring(err
, NULL
, NULL
));
132 VNC_DEBUG("no client username was found, denying access\n");
135 VNC_DEBUG("SASL client username %s\n", (const char *)val
);
137 vs
->sasl
.username
= qemu_strdup((const char*)val
);
139 if (vs
->vd
->sasl
.acl
== NULL
) {
140 VNC_DEBUG("no ACL activated, allowing access\n");
144 allow
= qemu_acl_party_is_allowed(vs
->vd
->sasl
.acl
, vs
->sasl
.username
);
146 VNC_DEBUG("SASL client %s %s by ACL\n", vs
->sasl
.username
,
147 allow
? "allowed" : "denied");
148 return allow
? 0 : -1;
151 static int vnc_auth_sasl_check_ssf(VncState
*vs
)
156 if (!vs
->sasl
.wantSSF
)
159 err
= sasl_getprop(vs
->sasl
.conn
, SASL_SSF
, &val
);
163 ssf
= *(const int *)val
;
164 VNC_DEBUG("negotiated an SSF of %d\n", ssf
);
166 return 0; /* 56 is good for Kerberos */
168 /* Only setup for read initially, because we're about to send an RPC
169 * reply which must be in plain text. When the next incoming RPC
170 * arrives, we'll switch on writes too
172 * cf qemudClientReadSASL in qemud.c
176 /* We have a SSF that's good enough */
185 * u32 clientin-length
186 * u8-array clientin-string
190 * u32 serverout-length
191 * u8-array serverout-strin
195 static int protocol_client_auth_sasl_step_len(VncState
*vs
, uint8_t *data
, size_t len
);
197 static int protocol_client_auth_sasl_step(VncState
*vs
, uint8_t *data
, size_t len
)
199 uint32_t datalen
= len
;
200 const char *serverout
;
201 unsigned int serveroutlen
;
203 char *clientdata
= NULL
;
205 /* NB, distinction of NULL vs "" is *critical* in SASL */
207 clientdata
= (char*)data
;
208 clientdata
[datalen
-1] = '\0'; /* Wire includes '\0', but make sure */
209 datalen
--; /* Don't count NULL byte when passing to _start() */
212 VNC_DEBUG("Step using SASL Data %p (%d bytes)\n",
213 clientdata
, datalen
);
214 err
= sasl_server_step(vs
->sasl
.conn
,
219 if (err
!= SASL_OK
&&
220 err
!= SASL_CONTINUE
) {
221 VNC_DEBUG("sasl step failed %d (%s)\n",
222 err
, sasl_errdetail(vs
->sasl
.conn
));
223 sasl_dispose(&vs
->sasl
.conn
);
224 vs
->sasl
.conn
= NULL
;
228 if (serveroutlen
> SASL_DATA_MAX_LEN
) {
229 VNC_DEBUG("sasl step reply data too long %d\n",
231 sasl_dispose(&vs
->sasl
.conn
);
232 vs
->sasl
.conn
= NULL
;
236 VNC_DEBUG("SASL return data %d bytes, nil; %d\n",
237 serveroutlen
, serverout
? 0 : 1);
240 vnc_write_u32(vs
, serveroutlen
+ 1);
241 vnc_write(vs
, serverout
, serveroutlen
+ 1);
243 vnc_write_u32(vs
, 0);
246 /* Whether auth is complete */
247 vnc_write_u8(vs
, err
== SASL_CONTINUE
? 0 : 1);
249 if (err
== SASL_CONTINUE
) {
250 VNC_DEBUG("%s", "Authentication must continue\n");
251 /* Wait for step length */
252 vnc_read_when(vs
, protocol_client_auth_sasl_step_len
, 4);
254 if (!vnc_auth_sasl_check_ssf(vs
)) {
255 VNC_DEBUG("Authentication rejected for weak SSF %d\n", vs
->csock
);
259 /* Check username whitelist ACL */
260 if (vnc_auth_sasl_check_access(vs
) < 0) {
261 VNC_DEBUG("Authentication rejected for ACL %d\n", vs
->csock
);
265 VNC_DEBUG("Authentication successful %d\n", vs
->csock
);
266 vnc_write_u32(vs
, 0); /* Accept auth */
268 * Delay writing in SSF encoded mode until pending output
272 vs
->sasl
.waitWriteSSF
= vs
->output
.offset
;
273 start_client_init(vs
);
279 vnc_write_u32(vs
, 1); /* Reject auth */
280 vnc_write_u32(vs
, sizeof("Authentication failed"));
281 vnc_write(vs
, "Authentication failed", sizeof("Authentication failed"));
283 vnc_client_error(vs
);
287 vnc_client_error(vs
);
291 static int protocol_client_auth_sasl_step_len(VncState
*vs
, uint8_t *data
, size_t len
)
293 uint32_t steplen
= read_u32(data
, 0);
294 VNC_DEBUG("Got client step len %d\n", steplen
);
295 if (steplen
> SASL_DATA_MAX_LEN
) {
296 VNC_DEBUG("Too much SASL data %d\n", steplen
);
297 vnc_client_error(vs
);
302 return protocol_client_auth_sasl_step(vs
, NULL
, 0);
304 vnc_read_when(vs
, protocol_client_auth_sasl_step
, steplen
);
313 * u32 clientin-length
314 * u8-array clientin-string
318 * u32 serverout-length
319 * u8-array serverout-strin
323 #define SASL_DATA_MAX_LEN (1024 * 1024)
325 static int protocol_client_auth_sasl_start(VncState
*vs
, uint8_t *data
, size_t len
)
327 uint32_t datalen
= len
;
328 const char *serverout
;
329 unsigned int serveroutlen
;
331 char *clientdata
= NULL
;
333 /* NB, distinction of NULL vs "" is *critical* in SASL */
335 clientdata
= (char*)data
;
336 clientdata
[datalen
-1] = '\0'; /* Should be on wire, but make sure */
337 datalen
--; /* Don't count NULL byte when passing to _start() */
340 VNC_DEBUG("Start SASL auth with mechanism %s. Data %p (%d bytes)\n",
341 vs
->sasl
.mechlist
, clientdata
, datalen
);
342 err
= sasl_server_start(vs
->sasl
.conn
,
348 if (err
!= SASL_OK
&&
349 err
!= SASL_CONTINUE
) {
350 VNC_DEBUG("sasl start failed %d (%s)\n",
351 err
, sasl_errdetail(vs
->sasl
.conn
));
352 sasl_dispose(&vs
->sasl
.conn
);
353 vs
->sasl
.conn
= NULL
;
356 if (serveroutlen
> SASL_DATA_MAX_LEN
) {
357 VNC_DEBUG("sasl start reply data too long %d\n",
359 sasl_dispose(&vs
->sasl
.conn
);
360 vs
->sasl
.conn
= NULL
;
364 VNC_DEBUG("SASL return data %d bytes, nil; %d\n",
365 serveroutlen
, serverout
? 0 : 1);
368 vnc_write_u32(vs
, serveroutlen
+ 1);
369 vnc_write(vs
, serverout
, serveroutlen
+ 1);
371 vnc_write_u32(vs
, 0);
374 /* Whether auth is complete */
375 vnc_write_u8(vs
, err
== SASL_CONTINUE
? 0 : 1);
377 if (err
== SASL_CONTINUE
) {
378 VNC_DEBUG("%s", "Authentication must continue\n");
379 /* Wait for step length */
380 vnc_read_when(vs
, protocol_client_auth_sasl_step_len
, 4);
382 if (!vnc_auth_sasl_check_ssf(vs
)) {
383 VNC_DEBUG("Authentication rejected for weak SSF %d\n", vs
->csock
);
387 /* Check username whitelist ACL */
388 if (vnc_auth_sasl_check_access(vs
) < 0) {
389 VNC_DEBUG("Authentication rejected for ACL %d\n", vs
->csock
);
393 VNC_DEBUG("Authentication successful %d\n", vs
->csock
);
394 vnc_write_u32(vs
, 0); /* Accept auth */
395 start_client_init(vs
);
401 vnc_write_u32(vs
, 1); /* Reject auth */
402 vnc_write_u32(vs
, sizeof("Authentication failed"));
403 vnc_write(vs
, "Authentication failed", sizeof("Authentication failed"));
405 vnc_client_error(vs
);
409 vnc_client_error(vs
);
413 static int protocol_client_auth_sasl_start_len(VncState
*vs
, uint8_t *data
, size_t len
)
415 uint32_t startlen
= read_u32(data
, 0);
416 VNC_DEBUG("Got client start len %d\n", startlen
);
417 if (startlen
> SASL_DATA_MAX_LEN
) {
418 VNC_DEBUG("Too much SASL data %d\n", startlen
);
419 vnc_client_error(vs
);
424 return protocol_client_auth_sasl_start(vs
, NULL
, 0);
426 vnc_read_when(vs
, protocol_client_auth_sasl_start
, startlen
);
430 static int protocol_client_auth_sasl_mechname(VncState
*vs
, uint8_t *data
, size_t len
)
432 char *mechname
= malloc(len
+ 1);
434 VNC_DEBUG("Out of memory reading mechname\n");
435 vnc_client_error(vs
);
437 strncpy(mechname
, (char*)data
, len
);
438 mechname
[len
] = '\0';
439 VNC_DEBUG("Got client mechname '%s' check against '%s'\n",
440 mechname
, vs
->sasl
.mechlist
);
442 if (strncmp(vs
->sasl
.mechlist
, mechname
, len
) == 0) {
443 if (vs
->sasl
.mechlist
[len
] != '\0' &&
444 vs
->sasl
.mechlist
[len
] != ',') {
445 VNC_DEBUG("One %d", vs
->sasl
.mechlist
[len
]);
446 vnc_client_error(vs
);
450 char *offset
= strstr(vs
->sasl
.mechlist
, mechname
);
451 VNC_DEBUG("Two %p\n", offset
);
453 vnc_client_error(vs
);
456 VNC_DEBUG("Two '%s'\n", offset
);
457 if (offset
[-1] != ',' ||
458 (offset
[len
] != '\0'&&
459 offset
[len
] != ',')) {
460 vnc_client_error(vs
);
465 free(vs
->sasl
.mechlist
);
466 vs
->sasl
.mechlist
= mechname
;
468 VNC_DEBUG("Validated mechname '%s'\n", mechname
);
469 vnc_read_when(vs
, protocol_client_auth_sasl_start_len
, 4);
473 static int protocol_client_auth_sasl_mechname_len(VncState
*vs
, uint8_t *data
, size_t len
)
475 uint32_t mechlen
= read_u32(data
, 0);
476 VNC_DEBUG("Got client mechname len %d\n", mechlen
);
478 VNC_DEBUG("Too long SASL mechname data %d\n", mechlen
);
479 vnc_client_error(vs
);
483 VNC_DEBUG("Too short SASL mechname %d\n", mechlen
);
484 vnc_client_error(vs
);
487 vnc_read_when(vs
, protocol_client_auth_sasl_mechname
,mechlen
);
491 #define USES_X509_AUTH(vs) \
492 ((vs)->subauth == VNC_AUTH_VENCRYPT_X509NONE || \
493 (vs)->subauth == VNC_AUTH_VENCRYPT_X509VNC || \
494 (vs)->subauth == VNC_AUTH_VENCRYPT_X509PLAIN || \
495 (vs)->subauth == VNC_AUTH_VENCRYPT_X509SASL)
498 void start_auth_sasl(VncState
*vs
)
500 const char *mechlist
= NULL
;
501 sasl_security_properties_t secprops
;
503 char *localAddr
, *remoteAddr
;
506 VNC_DEBUG("Initialize SASL auth %d\n", vs
->csock
);
508 /* Get local & remote client addresses in form IPADDR;PORT */
509 if (!(localAddr
= vnc_socket_local_addr("%s;%s", vs
->csock
)))
512 if (!(remoteAddr
= vnc_socket_remote_addr("%s;%s", vs
->csock
))) {
517 err
= sasl_server_new("vnc",
518 NULL
, /* FQDN - just delegates to gethostname */
519 NULL
, /* User realm */
522 NULL
, /* Callbacks, not needed */
527 localAddr
= remoteAddr
= NULL
;
529 if (err
!= SASL_OK
) {
530 VNC_DEBUG("sasl context setup failed %d (%s)",
531 err
, sasl_errstring(err
, NULL
, NULL
));
532 vs
->sasl
.conn
= NULL
;
536 #ifdef CONFIG_VNC_TLS
537 /* Inform SASL that we've got an external SSF layer from TLS/x509 */
538 if (vs
->vd
->auth
== VNC_AUTH_VENCRYPT
&&
539 vs
->vd
->subauth
== VNC_AUTH_VENCRYPT_X509SASL
) {
540 gnutls_cipher_algorithm_t cipher
;
543 cipher
= gnutls_cipher_get(vs
->tls
.session
);
544 if (!(ssf
= (sasl_ssf_t
)gnutls_cipher_get_key_size(cipher
))) {
545 VNC_DEBUG("%s", "cannot TLS get cipher size\n");
546 sasl_dispose(&vs
->sasl
.conn
);
547 vs
->sasl
.conn
= NULL
;
550 ssf
*= 8; /* tls key size is bytes, sasl wants bits */
552 err
= sasl_setprop(vs
->sasl
.conn
, SASL_SSF_EXTERNAL
, &ssf
);
553 if (err
!= SASL_OK
) {
554 VNC_DEBUG("cannot set SASL external SSF %d (%s)\n",
555 err
, sasl_errstring(err
, NULL
, NULL
));
556 sasl_dispose(&vs
->sasl
.conn
);
557 vs
->sasl
.conn
= NULL
;
561 #endif /* CONFIG_VNC_TLS */
562 vs
->sasl
.wantSSF
= 1;
564 memset (&secprops
, 0, sizeof secprops
);
565 /* Inform SASL that we've got an external SSF layer from TLS */
566 if (strncmp(vs
->vd
->display
, "unix:", 5) == 0
567 #ifdef CONFIG_VNC_TLS
568 /* Disable SSF, if using TLS+x509+SASL only. TLS without x509
569 is not sufficiently strong */
570 || (vs
->vd
->auth
== VNC_AUTH_VENCRYPT
&&
571 vs
->vd
->subauth
== VNC_AUTH_VENCRYPT_X509SASL
)
572 #endif /* CONFIG_VNC_TLS */
574 /* If we've got TLS or UNIX domain sock, we don't care about SSF */
575 secprops
.min_ssf
= 0;
576 secprops
.max_ssf
= 0;
577 secprops
.maxbufsize
= 8192;
578 secprops
.security_flags
= 0;
580 /* Plain TCP, better get an SSF layer */
581 secprops
.min_ssf
= 56; /* Good enough to require kerberos */
582 secprops
.max_ssf
= 100000; /* Arbitrary big number */
583 secprops
.maxbufsize
= 8192;
584 /* Forbid any anonymous or trivially crackable auth */
585 secprops
.security_flags
=
586 SASL_SEC_NOANONYMOUS
| SASL_SEC_NOPLAINTEXT
;
589 err
= sasl_setprop(vs
->sasl
.conn
, SASL_SEC_PROPS
, &secprops
);
590 if (err
!= SASL_OK
) {
591 VNC_DEBUG("cannot set SASL security props %d (%s)\n",
592 err
, sasl_errstring(err
, NULL
, NULL
));
593 sasl_dispose(&vs
->sasl
.conn
);
594 vs
->sasl
.conn
= NULL
;
598 err
= sasl_listmech(vs
->sasl
.conn
,
599 NULL
, /* Don't need to set user */
606 if (err
!= SASL_OK
) {
607 VNC_DEBUG("cannot list SASL mechanisms %d (%s)\n",
608 err
, sasl_errdetail(vs
->sasl
.conn
));
609 sasl_dispose(&vs
->sasl
.conn
);
610 vs
->sasl
.conn
= NULL
;
613 VNC_DEBUG("Available mechanisms for client: '%s'\n", mechlist
);
615 if (!(vs
->sasl
.mechlist
= strdup(mechlist
))) {
616 VNC_DEBUG("Out of memory");
617 sasl_dispose(&vs
->sasl
.conn
);
618 vs
->sasl
.conn
= NULL
;
621 mechlistlen
= strlen(mechlist
);
622 vnc_write_u32(vs
, mechlistlen
);
623 vnc_write(vs
, mechlist
, mechlistlen
);
626 VNC_DEBUG("Wait for client mechname length\n");
627 vnc_read_when(vs
, protocol_client_auth_sasl_mechname_len
, 4);
632 vnc_client_error(vs
);