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
25 #include "qemu/osdep.h"
28 /* Max amount of data we send/recv for SASL steps to prevent DOS */
29 #define SASL_DATA_MAX_LEN (1024 * 1024)
32 void vnc_sasl_client_cleanup(VncState
*vs
)
35 vs
->sasl
.runSSF
= false;
36 vs
->sasl
.wantSSF
= false;
37 vs
->sasl
.waitWriteSSF
= 0;
38 vs
->sasl
.encodedLength
= vs
->sasl
.encodedOffset
= 0;
39 vs
->sasl
.encoded
= NULL
;
40 g_free(vs
->sasl
.username
);
41 g_free(vs
->sasl
.mechlist
);
42 vs
->sasl
.username
= vs
->sasl
.mechlist
= NULL
;
43 sasl_dispose(&vs
->sasl
.conn
);
49 long vnc_client_write_sasl(VncState
*vs
)
53 VNC_DEBUG("Write SASL: Pending output %p size %zd offset %zd "
54 "Encoded: %p size %d offset %d\n",
55 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
56 vs
->sasl
.encoded
, vs
->sasl
.encodedLength
, vs
->sasl
.encodedOffset
);
58 if (!vs
->sasl
.encoded
) {
60 err
= sasl_encode(vs
->sasl
.conn
,
61 (char *)vs
->output
.buffer
,
63 (const char **)&vs
->sasl
.encoded
,
64 &vs
->sasl
.encodedLength
);
66 return vnc_client_io_error(vs
, -1, NULL
);
68 vs
->sasl
.encodedOffset
= 0;
71 ret
= vnc_client_write_buf(vs
,
72 vs
->sasl
.encoded
+ vs
->sasl
.encodedOffset
,
73 vs
->sasl
.encodedLength
- vs
->sasl
.encodedOffset
);
77 vs
->sasl
.encodedOffset
+= ret
;
78 if (vs
->sasl
.encodedOffset
== vs
->sasl
.encodedLength
) {
79 vs
->output
.offset
= 0;
80 vs
->sasl
.encoded
= NULL
;
81 vs
->sasl
.encodedOffset
= vs
->sasl
.encodedLength
= 0;
84 /* Can't merge this block with one above, because
85 * someone might have written more unencrypted
86 * data in vs->output while we were processing
89 if (vs
->output
.offset
== 0) {
91 g_source_remove(vs
->ioc_tag
);
93 vs
->ioc_tag
= qio_channel_add_watch(
94 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
101 long vnc_client_read_sasl(VncState
*vs
)
104 uint8_t encoded
[4096];
106 unsigned int decodedLen
;
109 ret
= vnc_client_read_buf(vs
, encoded
, sizeof(encoded
));
113 err
= sasl_decode(vs
->sasl
.conn
,
114 (char *)encoded
, ret
,
115 &decoded
, &decodedLen
);
118 return vnc_client_io_error(vs
, -1, NULL
);
119 VNC_DEBUG("Read SASL Encoded %p size %ld Decoded %p size %d\n",
120 encoded
, ret
, decoded
, decodedLen
);
121 buffer_reserve(&vs
->input
, decodedLen
);
122 buffer_append(&vs
->input
, decoded
, decodedLen
);
127 static int vnc_auth_sasl_check_access(VncState
*vs
)
133 err
= sasl_getprop(vs
->sasl
.conn
, SASL_USERNAME
, &val
);
134 if (err
!= SASL_OK
) {
135 VNC_DEBUG("cannot query SASL username on connection %d (%s), denying access\n",
136 err
, sasl_errstring(err
, NULL
, NULL
));
140 VNC_DEBUG("no client username was found, denying access\n");
143 VNC_DEBUG("SASL client username %s\n", (const char *)val
);
145 vs
->sasl
.username
= g_strdup((const char*)val
);
147 if (vs
->vd
->sasl
.acl
== NULL
) {
148 VNC_DEBUG("no ACL activated, allowing access\n");
152 allow
= qemu_acl_party_is_allowed(vs
->vd
->sasl
.acl
, vs
->sasl
.username
);
154 VNC_DEBUG("SASL client %s %s by ACL\n", vs
->sasl
.username
,
155 allow
? "allowed" : "denied");
156 return allow
? 0 : -1;
159 static int vnc_auth_sasl_check_ssf(VncState
*vs
)
164 if (!vs
->sasl
.wantSSF
)
167 err
= sasl_getprop(vs
->sasl
.conn
, SASL_SSF
, &val
);
171 ssf
= *(const int *)val
;
172 VNC_DEBUG("negotiated an SSF of %d\n", ssf
);
174 return 0; /* 56 is good for Kerberos */
176 /* Only setup for read initially, because we're about to send an RPC
177 * reply which must be in plain text. When the next incoming RPC
178 * arrives, we'll switch on writes too
180 * cf qemudClientReadSASL in qemud.c
184 /* We have a SSF that's good enough */
193 * u32 clientin-length
194 * u8-array clientin-string
198 * u32 serverout-length
199 * u8-array serverout-strin
203 static int protocol_client_auth_sasl_step_len(VncState
*vs
, uint8_t *data
, size_t len
);
205 static int protocol_client_auth_sasl_step(VncState
*vs
, uint8_t *data
, size_t len
)
207 uint32_t datalen
= len
;
208 const char *serverout
;
209 unsigned int serveroutlen
;
211 char *clientdata
= NULL
;
213 /* NB, distinction of NULL vs "" is *critical* in SASL */
215 clientdata
= (char*)data
;
216 clientdata
[datalen
-1] = '\0'; /* Wire includes '\0', but make sure */
217 datalen
--; /* Don't count NULL byte when passing to _start() */
220 VNC_DEBUG("Step using SASL Data %p (%d bytes)\n",
221 clientdata
, datalen
);
222 err
= sasl_server_step(vs
->sasl
.conn
,
227 if (err
!= SASL_OK
&&
228 err
!= SASL_CONTINUE
) {
229 VNC_DEBUG("sasl step failed %d (%s)\n",
230 err
, sasl_errdetail(vs
->sasl
.conn
));
231 sasl_dispose(&vs
->sasl
.conn
);
232 vs
->sasl
.conn
= NULL
;
236 if (serveroutlen
> SASL_DATA_MAX_LEN
) {
237 VNC_DEBUG("sasl step reply data too long %d\n",
239 sasl_dispose(&vs
->sasl
.conn
);
240 vs
->sasl
.conn
= NULL
;
244 VNC_DEBUG("SASL return data %d bytes, nil; %d\n",
245 serveroutlen
, serverout
? 0 : 1);
248 vnc_write_u32(vs
, serveroutlen
+ 1);
249 vnc_write(vs
, serverout
, serveroutlen
+ 1);
251 vnc_write_u32(vs
, 0);
254 /* Whether auth is complete */
255 vnc_write_u8(vs
, err
== SASL_CONTINUE
? 0 : 1);
257 if (err
== SASL_CONTINUE
) {
258 VNC_DEBUG("%s", "Authentication must continue\n");
259 /* Wait for step length */
260 vnc_read_when(vs
, protocol_client_auth_sasl_step_len
, 4);
262 if (!vnc_auth_sasl_check_ssf(vs
)) {
263 VNC_DEBUG("Authentication rejected for weak SSF %p\n", vs
->ioc
);
267 /* Check username whitelist ACL */
268 if (vnc_auth_sasl_check_access(vs
) < 0) {
269 VNC_DEBUG("Authentication rejected for ACL %p\n", vs
->ioc
);
273 VNC_DEBUG("Authentication successful %p\n", vs
->ioc
);
274 vnc_write_u32(vs
, 0); /* Accept auth */
276 * Delay writing in SSF encoded mode until pending output
280 vs
->sasl
.waitWriteSSF
= vs
->output
.offset
;
281 start_client_init(vs
);
287 vnc_write_u32(vs
, 1); /* Reject auth */
288 vnc_write_u32(vs
, sizeof("Authentication failed"));
289 vnc_write(vs
, "Authentication failed", sizeof("Authentication failed"));
291 vnc_client_error(vs
);
295 vnc_client_error(vs
);
299 static int protocol_client_auth_sasl_step_len(VncState
*vs
, uint8_t *data
, size_t len
)
301 uint32_t steplen
= read_u32(data
, 0);
302 VNC_DEBUG("Got client step len %d\n", steplen
);
303 if (steplen
> SASL_DATA_MAX_LEN
) {
304 VNC_DEBUG("Too much SASL data %d\n", steplen
);
305 vnc_client_error(vs
);
310 return protocol_client_auth_sasl_step(vs
, NULL
, 0);
312 vnc_read_when(vs
, protocol_client_auth_sasl_step
, steplen
);
321 * u32 clientin-length
322 * u8-array clientin-string
326 * u32 serverout-length
327 * u8-array serverout-strin
331 #define SASL_DATA_MAX_LEN (1024 * 1024)
333 static int protocol_client_auth_sasl_start(VncState
*vs
, uint8_t *data
, size_t len
)
335 uint32_t datalen
= len
;
336 const char *serverout
;
337 unsigned int serveroutlen
;
339 char *clientdata
= NULL
;
341 /* NB, distinction of NULL vs "" is *critical* in SASL */
343 clientdata
= (char*)data
;
344 clientdata
[datalen
-1] = '\0'; /* Should be on wire, but make sure */
345 datalen
--; /* Don't count NULL byte when passing to _start() */
348 VNC_DEBUG("Start SASL auth with mechanism %s. Data %p (%d bytes)\n",
349 vs
->sasl
.mechlist
, clientdata
, datalen
);
350 err
= sasl_server_start(vs
->sasl
.conn
,
356 if (err
!= SASL_OK
&&
357 err
!= SASL_CONTINUE
) {
358 VNC_DEBUG("sasl start failed %d (%s)\n",
359 err
, sasl_errdetail(vs
->sasl
.conn
));
360 sasl_dispose(&vs
->sasl
.conn
);
361 vs
->sasl
.conn
= NULL
;
364 if (serveroutlen
> SASL_DATA_MAX_LEN
) {
365 VNC_DEBUG("sasl start reply data too long %d\n",
367 sasl_dispose(&vs
->sasl
.conn
);
368 vs
->sasl
.conn
= NULL
;
372 VNC_DEBUG("SASL return data %d bytes, nil; %d\n",
373 serveroutlen
, serverout
? 0 : 1);
376 vnc_write_u32(vs
, serveroutlen
+ 1);
377 vnc_write(vs
, serverout
, serveroutlen
+ 1);
379 vnc_write_u32(vs
, 0);
382 /* Whether auth is complete */
383 vnc_write_u8(vs
, err
== SASL_CONTINUE
? 0 : 1);
385 if (err
== SASL_CONTINUE
) {
386 VNC_DEBUG("%s", "Authentication must continue\n");
387 /* Wait for step length */
388 vnc_read_when(vs
, protocol_client_auth_sasl_step_len
, 4);
390 if (!vnc_auth_sasl_check_ssf(vs
)) {
391 VNC_DEBUG("Authentication rejected for weak SSF %p\n", vs
->ioc
);
395 /* Check username whitelist ACL */
396 if (vnc_auth_sasl_check_access(vs
) < 0) {
397 VNC_DEBUG("Authentication rejected for ACL %p\n", vs
->ioc
);
401 VNC_DEBUG("Authentication successful %p\n", vs
->ioc
);
402 vnc_write_u32(vs
, 0); /* Accept auth */
403 start_client_init(vs
);
409 vnc_write_u32(vs
, 1); /* Reject auth */
410 vnc_write_u32(vs
, sizeof("Authentication failed"));
411 vnc_write(vs
, "Authentication failed", sizeof("Authentication failed"));
413 vnc_client_error(vs
);
417 vnc_client_error(vs
);
421 static int protocol_client_auth_sasl_start_len(VncState
*vs
, uint8_t *data
, size_t len
)
423 uint32_t startlen
= read_u32(data
, 0);
424 VNC_DEBUG("Got client start len %d\n", startlen
);
425 if (startlen
> SASL_DATA_MAX_LEN
) {
426 VNC_DEBUG("Too much SASL data %d\n", startlen
);
427 vnc_client_error(vs
);
432 return protocol_client_auth_sasl_start(vs
, NULL
, 0);
434 vnc_read_when(vs
, protocol_client_auth_sasl_start
, startlen
);
438 static int protocol_client_auth_sasl_mechname(VncState
*vs
, uint8_t *data
, size_t len
)
440 char *mechname
= g_strndup((const char *) data
, len
);
441 VNC_DEBUG("Got client mechname '%s' check against '%s'\n",
442 mechname
, vs
->sasl
.mechlist
);
444 if (strncmp(vs
->sasl
.mechlist
, mechname
, len
) == 0) {
445 if (vs
->sasl
.mechlist
[len
] != '\0' &&
446 vs
->sasl
.mechlist
[len
] != ',') {
447 VNC_DEBUG("One %d", vs
->sasl
.mechlist
[len
]);
451 char *offset
= strstr(vs
->sasl
.mechlist
, mechname
);
452 VNC_DEBUG("Two %p\n", offset
);
456 VNC_DEBUG("Two '%s'\n", offset
);
457 if (offset
[-1] != ',' ||
458 (offset
[len
] != '\0'&&
459 offset
[len
] != ',')) {
464 g_free(vs
->sasl
.mechlist
);
465 vs
->sasl
.mechlist
= mechname
;
467 VNC_DEBUG("Validated mechname '%s'\n", mechname
);
468 vnc_read_when(vs
, protocol_client_auth_sasl_start_len
, 4);
472 vnc_client_error(vs
);
477 static int protocol_client_auth_sasl_mechname_len(VncState
*vs
, uint8_t *data
, size_t len
)
479 uint32_t mechlen
= read_u32(data
, 0);
480 VNC_DEBUG("Got client mechname len %d\n", mechlen
);
482 VNC_DEBUG("Too long SASL mechname data %d\n", mechlen
);
483 vnc_client_error(vs
);
487 VNC_DEBUG("Too short SASL mechname %d\n", mechlen
);
488 vnc_client_error(vs
);
491 vnc_read_when(vs
, protocol_client_auth_sasl_mechname
,mechlen
);
496 vnc_socket_ip_addr_string(QIOChannelSocket
*ioc
,
504 addr
= qio_channel_socket_get_local_address(ioc
, errp
);
506 addr
= qio_channel_socket_get_remote_address(ioc
, errp
);
512 if (addr
->type
!= SOCKET_ADDRESS_KIND_INET
) {
513 error_setg(errp
, "Not an inet socket type");
516 ret
= g_strdup_printf("%s;%s", addr
->u
.inet
->host
, addr
->u
.inet
->port
);
517 qapi_free_SocketAddress(addr
);
521 void start_auth_sasl(VncState
*vs
)
523 const char *mechlist
= NULL
;
524 sasl_security_properties_t secprops
;
526 char *localAddr
, *remoteAddr
;
529 VNC_DEBUG("Initialize SASL auth %p\n", vs
->ioc
);
531 /* Get local & remote client addresses in form IPADDR;PORT */
532 localAddr
= vnc_socket_ip_addr_string(vs
->sioc
, true, NULL
);
537 remoteAddr
= vnc_socket_ip_addr_string(vs
->sioc
, false, NULL
);
543 err
= sasl_server_new("vnc",
544 NULL
, /* FQDN - just delegates to gethostname */
545 NULL
, /* User realm */
548 NULL
, /* Callbacks, not needed */
553 localAddr
= remoteAddr
= NULL
;
555 if (err
!= SASL_OK
) {
556 VNC_DEBUG("sasl context setup failed %d (%s)",
557 err
, sasl_errstring(err
, NULL
, NULL
));
558 vs
->sasl
.conn
= NULL
;
562 /* Inform SASL that we've got an external SSF layer from TLS/x509 */
563 if (vs
->auth
== VNC_AUTH_VENCRYPT
&&
564 vs
->subauth
== VNC_AUTH_VENCRYPT_X509SASL
) {
565 Error
*local_err
= NULL
;
569 keysize
= qcrypto_tls_session_get_key_size(vs
->tls
,
572 VNC_DEBUG("cannot TLS get cipher size: %s\n",
573 error_get_pretty(local_err
));
574 error_free(local_err
);
575 sasl_dispose(&vs
->sasl
.conn
);
576 vs
->sasl
.conn
= NULL
;
579 ssf
= keysize
* CHAR_BIT
; /* tls key size is bytes, sasl wants bits */
581 err
= sasl_setprop(vs
->sasl
.conn
, SASL_SSF_EXTERNAL
, &ssf
);
582 if (err
!= SASL_OK
) {
583 VNC_DEBUG("cannot set SASL external SSF %d (%s)\n",
584 err
, sasl_errstring(err
, NULL
, NULL
));
585 sasl_dispose(&vs
->sasl
.conn
);
586 vs
->sasl
.conn
= NULL
;
590 vs
->sasl
.wantSSF
= 1;
593 memset (&secprops
, 0, sizeof secprops
);
594 /* Inform SASL that we've got an external SSF layer from TLS.
596 * Disable SSF, if using TLS+x509+SASL only. TLS without x509
597 * is not sufficiently strong
599 if (vs
->vd
->is_unix
||
600 (vs
->auth
== VNC_AUTH_VENCRYPT
&&
601 vs
->subauth
== VNC_AUTH_VENCRYPT_X509SASL
)) {
602 /* If we've got TLS or UNIX domain sock, we don't care about SSF */
603 secprops
.min_ssf
= 0;
604 secprops
.max_ssf
= 0;
605 secprops
.maxbufsize
= 8192;
606 secprops
.security_flags
= 0;
608 /* Plain TCP, better get an SSF layer */
609 secprops
.min_ssf
= 56; /* Good enough to require kerberos */
610 secprops
.max_ssf
= 100000; /* Arbitrary big number */
611 secprops
.maxbufsize
= 8192;
612 /* Forbid any anonymous or trivially crackable auth */
613 secprops
.security_flags
=
614 SASL_SEC_NOANONYMOUS
| SASL_SEC_NOPLAINTEXT
;
617 err
= sasl_setprop(vs
->sasl
.conn
, SASL_SEC_PROPS
, &secprops
);
618 if (err
!= SASL_OK
) {
619 VNC_DEBUG("cannot set SASL security props %d (%s)\n",
620 err
, sasl_errstring(err
, NULL
, NULL
));
621 sasl_dispose(&vs
->sasl
.conn
);
622 vs
->sasl
.conn
= NULL
;
626 err
= sasl_listmech(vs
->sasl
.conn
,
627 NULL
, /* Don't need to set user */
634 if (err
!= SASL_OK
) {
635 VNC_DEBUG("cannot list SASL mechanisms %d (%s)\n",
636 err
, sasl_errdetail(vs
->sasl
.conn
));
637 sasl_dispose(&vs
->sasl
.conn
);
638 vs
->sasl
.conn
= NULL
;
641 VNC_DEBUG("Available mechanisms for client: '%s'\n", mechlist
);
643 vs
->sasl
.mechlist
= g_strdup(mechlist
);
644 mechlistlen
= strlen(mechlist
);
645 vnc_write_u32(vs
, mechlistlen
);
646 vnc_write(vs
, mechlist
, mechlistlen
);
649 VNC_DEBUG("Wait for client mechname length\n");
650 vnc_read_when(vs
, protocol_client_auth_sasl_mechname_len
, 4);
655 vnc_client_error(vs
);