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"
26 #include "qapi/error.h"
27 #include "authz/base.h"
32 * Apple has deprecated sasl.h functions in OS X 10.11. Therefore,
33 * files that use SASL API need to disable -Wdeprecated-declarations.
36 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
39 /* Max amount of data we send/recv for SASL steps to prevent DOS */
40 #define SASL_DATA_MAX_LEN (1024 * 1024)
43 bool vnc_sasl_server_init(Error
**errp
)
45 int saslErr
= sasl_server_init(NULL
, "qemu");
47 if (saslErr
!= SASL_OK
) {
48 error_setg(errp
, "Failed to initialize SASL auth: %s",
49 sasl_errstring(saslErr
, NULL
, NULL
));
55 void vnc_sasl_client_cleanup(VncState
*vs
)
58 vs
->sasl
.runSSF
= false;
59 vs
->sasl
.wantSSF
= false;
60 vs
->sasl
.waitWriteSSF
= 0;
61 vs
->sasl
.encodedLength
= vs
->sasl
.encodedOffset
= 0;
62 vs
->sasl
.encoded
= NULL
;
63 g_free(vs
->sasl
.username
);
64 g_free(vs
->sasl
.mechlist
);
65 vs
->sasl
.username
= vs
->sasl
.mechlist
= NULL
;
66 sasl_dispose(&vs
->sasl
.conn
);
72 size_t vnc_client_write_sasl(VncState
*vs
)
76 VNC_DEBUG("Write SASL: Pending output %p size %zd offset %zd "
77 "Encoded: %p size %d offset %d\n",
78 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
79 vs
->sasl
.encoded
, vs
->sasl
.encodedLength
, vs
->sasl
.encodedOffset
);
81 if (!vs
->sasl
.encoded
) {
83 err
= sasl_encode(vs
->sasl
.conn
,
84 (char *)vs
->output
.buffer
,
86 (const char **)&vs
->sasl
.encoded
,
87 &vs
->sasl
.encodedLength
);
89 return vnc_client_io_error(vs
, -1, NULL
);
91 vs
->sasl
.encodedRawLength
= vs
->output
.offset
;
92 vs
->sasl
.encodedOffset
= 0;
95 ret
= vnc_client_write_buf(vs
,
96 vs
->sasl
.encoded
+ vs
->sasl
.encodedOffset
,
97 vs
->sasl
.encodedLength
- vs
->sasl
.encodedOffset
);
101 vs
->sasl
.encodedOffset
+= ret
;
102 if (vs
->sasl
.encodedOffset
== vs
->sasl
.encodedLength
) {
103 bool throttled
= vs
->force_update_offset
!= 0;
105 if (vs
->sasl
.encodedRawLength
>= vs
->force_update_offset
) {
106 vs
->force_update_offset
= 0;
108 vs
->force_update_offset
-= vs
->sasl
.encodedRawLength
;
110 if (throttled
&& vs
->force_update_offset
== 0) {
111 trace_vnc_client_unthrottle_forced(vs
, vs
->ioc
);
113 offset
= vs
->output
.offset
;
114 buffer_advance(&vs
->output
, vs
->sasl
.encodedRawLength
);
115 if (offset
>= vs
->throttle_output_offset
&&
116 vs
->output
.offset
< vs
->throttle_output_offset
) {
117 trace_vnc_client_unthrottle_incremental(vs
, vs
->ioc
,
120 vs
->sasl
.encoded
= NULL
;
121 vs
->sasl
.encodedOffset
= vs
->sasl
.encodedLength
= 0;
124 /* Can't merge this block with one above, because
125 * someone might have written more unencrypted
126 * data in vs->output while we were processing
127 * SASL encoded output
129 if (vs
->output
.offset
== 0) {
131 g_source_remove(vs
->ioc_tag
);
133 vs
->ioc_tag
= qio_channel_add_watch(
134 vs
->ioc
, G_IO_IN
| G_IO_HUP
| G_IO_ERR
,
135 vnc_client_io
, vs
, NULL
);
142 size_t vnc_client_read_sasl(VncState
*vs
)
145 uint8_t encoded
[4096];
147 unsigned int decodedLen
;
150 ret
= vnc_client_read_buf(vs
, encoded
, sizeof(encoded
));
154 err
= sasl_decode(vs
->sasl
.conn
,
155 (char *)encoded
, ret
,
156 &decoded
, &decodedLen
);
159 return vnc_client_io_error(vs
, -1, NULL
);
160 VNC_DEBUG("Read SASL Encoded %p size %ld Decoded %p size %d\n",
161 encoded
, ret
, decoded
, decodedLen
);
162 buffer_reserve(&vs
->input
, decodedLen
);
163 buffer_append(&vs
->input
, decoded
, decodedLen
);
168 static int vnc_auth_sasl_check_access(VncState
*vs
)
175 rv
= sasl_getprop(vs
->sasl
.conn
, SASL_USERNAME
, &val
);
177 trace_vnc_auth_fail(vs
, vs
->auth
, "Cannot fetch SASL username",
178 sasl_errstring(rv
, NULL
, NULL
));
182 trace_vnc_auth_fail(vs
, vs
->auth
, "No SASL username set", "");
186 vs
->sasl
.username
= g_strdup((const char*)val
);
187 trace_vnc_auth_sasl_username(vs
, vs
->sasl
.username
);
189 if (vs
->vd
->sasl
.authzid
== NULL
) {
190 trace_vnc_auth_sasl_acl(vs
, 1);
194 allow
= qauthz_is_allowed_by_id(vs
->vd
->sasl
.authzid
,
195 vs
->sasl
.username
, &err
);
197 trace_vnc_auth_fail(vs
, vs
->auth
, "Error from authz",
198 error_get_pretty(err
));
203 trace_vnc_auth_sasl_acl(vs
, allow
);
204 return allow
? 0 : -1;
207 static int vnc_auth_sasl_check_ssf(VncState
*vs
)
212 if (!vs
->sasl
.wantSSF
)
215 err
= sasl_getprop(vs
->sasl
.conn
, SASL_SSF
, &val
);
219 ssf
= *(const int *)val
;
221 trace_vnc_auth_sasl_ssf(vs
, ssf
);
224 return 0; /* 56 is good for Kerberos */
226 /* Only setup for read initially, because we're about to send an RPC
227 * reply which must be in plain text. When the next incoming RPC
228 * arrives, we'll switch on writes too
230 * cf qemudClientReadSASL in qemud.c
234 /* We have a SSF that's good enough */
243 * u32 clientin-length
244 * u8-array clientin-string
248 * u32 serverout-length
249 * u8-array serverout-strin
253 static int protocol_client_auth_sasl_step_len(VncState
*vs
, uint8_t *data
, size_t len
);
255 static int protocol_client_auth_sasl_step(VncState
*vs
, uint8_t *data
, size_t len
)
257 uint32_t datalen
= len
;
258 const char *serverout
;
259 unsigned int serveroutlen
;
261 char *clientdata
= NULL
;
263 /* NB, distinction of NULL vs "" is *critical* in SASL */
265 clientdata
= (char*)data
;
266 clientdata
[datalen
-1] = '\0'; /* Wire includes '\0', but make sure */
267 datalen
--; /* Don't count NULL byte when passing to _start() */
270 err
= sasl_server_step(vs
->sasl
.conn
,
275 trace_vnc_auth_sasl_step(vs
, data
, len
, serverout
, serveroutlen
, err
);
276 if (err
!= SASL_OK
&&
277 err
!= SASL_CONTINUE
) {
278 trace_vnc_auth_fail(vs
, vs
->auth
, "Cannot step SASL auth",
279 sasl_errdetail(vs
->sasl
.conn
));
280 sasl_dispose(&vs
->sasl
.conn
);
281 vs
->sasl
.conn
= NULL
;
285 if (serveroutlen
> SASL_DATA_MAX_LEN
) {
286 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL data too long", "");
287 sasl_dispose(&vs
->sasl
.conn
);
288 vs
->sasl
.conn
= NULL
;
293 vnc_write_u32(vs
, serveroutlen
+ 1);
294 vnc_write(vs
, serverout
, serveroutlen
+ 1);
296 vnc_write_u32(vs
, 0);
299 /* Whether auth is complete */
300 vnc_write_u8(vs
, err
== SASL_CONTINUE
? 0 : 1);
302 if (err
== SASL_CONTINUE
) {
303 /* Wait for step length */
304 vnc_read_when(vs
, protocol_client_auth_sasl_step_len
, 4);
306 if (!vnc_auth_sasl_check_ssf(vs
)) {
307 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL SSF too weak", "");
311 /* Check the username access control list */
312 if (vnc_auth_sasl_check_access(vs
) < 0) {
316 trace_vnc_auth_pass(vs
, vs
->auth
);
317 vnc_write_u32(vs
, 0); /* Accept auth */
319 * Delay writing in SSF encoded mode until pending output
323 vs
->sasl
.waitWriteSSF
= vs
->output
.offset
;
324 start_client_init(vs
);
330 vnc_write_u32(vs
, 1); /* Reject auth */
331 vnc_write_u32(vs
, sizeof("Authentication failed"));
332 vnc_write(vs
, "Authentication failed", sizeof("Authentication failed"));
334 vnc_client_error(vs
);
338 vnc_client_error(vs
);
342 static int protocol_client_auth_sasl_step_len(VncState
*vs
, uint8_t *data
, size_t len
)
344 uint32_t steplen
= read_u32(data
, 0);
346 if (steplen
> SASL_DATA_MAX_LEN
) {
347 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL step len too large", "");
348 vnc_client_error(vs
);
353 return protocol_client_auth_sasl_step(vs
, NULL
, 0);
355 vnc_read_when(vs
, protocol_client_auth_sasl_step
, steplen
);
364 * u32 clientin-length
365 * u8-array clientin-string
369 * u32 serverout-length
370 * u8-array serverout-strin
374 #define SASL_DATA_MAX_LEN (1024 * 1024)
376 static int protocol_client_auth_sasl_start(VncState
*vs
, uint8_t *data
, size_t len
)
378 uint32_t datalen
= len
;
379 const char *serverout
;
380 unsigned int serveroutlen
;
382 char *clientdata
= NULL
;
384 /* NB, distinction of NULL vs "" is *critical* in SASL */
386 clientdata
= (char*)data
;
387 clientdata
[datalen
-1] = '\0'; /* Should be on wire, but make sure */
388 datalen
--; /* Don't count NULL byte when passing to _start() */
391 err
= sasl_server_start(vs
->sasl
.conn
,
397 trace_vnc_auth_sasl_start(vs
, data
, len
, serverout
, serveroutlen
, err
);
398 if (err
!= SASL_OK
&&
399 err
!= SASL_CONTINUE
) {
400 trace_vnc_auth_fail(vs
, vs
->auth
, "Cannot start SASL auth",
401 sasl_errdetail(vs
->sasl
.conn
));
402 sasl_dispose(&vs
->sasl
.conn
);
403 vs
->sasl
.conn
= NULL
;
406 if (serveroutlen
> SASL_DATA_MAX_LEN
) {
407 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL data too long", "");
408 sasl_dispose(&vs
->sasl
.conn
);
409 vs
->sasl
.conn
= NULL
;
414 vnc_write_u32(vs
, serveroutlen
+ 1);
415 vnc_write(vs
, serverout
, serveroutlen
+ 1);
417 vnc_write_u32(vs
, 0);
420 /* Whether auth is complete */
421 vnc_write_u8(vs
, err
== SASL_CONTINUE
? 0 : 1);
423 if (err
== SASL_CONTINUE
) {
424 /* Wait for step length */
425 vnc_read_when(vs
, protocol_client_auth_sasl_step_len
, 4);
427 if (!vnc_auth_sasl_check_ssf(vs
)) {
428 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL SSF too weak", "");
432 /* Check the username access control list */
433 if (vnc_auth_sasl_check_access(vs
) < 0) {
437 trace_vnc_auth_pass(vs
, vs
->auth
);
438 vnc_write_u32(vs
, 0); /* Accept auth */
439 start_client_init(vs
);
445 vnc_write_u32(vs
, 1); /* Reject auth */
446 vnc_write_u32(vs
, sizeof("Authentication failed"));
447 vnc_write(vs
, "Authentication failed", sizeof("Authentication failed"));
449 vnc_client_error(vs
);
453 vnc_client_error(vs
);
457 static int protocol_client_auth_sasl_start_len(VncState
*vs
, uint8_t *data
, size_t len
)
459 uint32_t startlen
= read_u32(data
, 0);
461 if (startlen
> SASL_DATA_MAX_LEN
) {
462 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL start len too large", "");
463 vnc_client_error(vs
);
468 return protocol_client_auth_sasl_start(vs
, NULL
, 0);
470 vnc_read_when(vs
, protocol_client_auth_sasl_start
, startlen
);
474 static int protocol_client_auth_sasl_mechname(VncState
*vs
, uint8_t *data
, size_t len
)
476 char *mechname
= g_strndup((const char *) data
, len
);
477 trace_vnc_auth_sasl_mech_choose(vs
, mechname
);
479 if (strncmp(vs
->sasl
.mechlist
, mechname
, len
) == 0) {
480 if (vs
->sasl
.mechlist
[len
] != '\0' &&
481 vs
->sasl
.mechlist
[len
] != ',') {
485 char *offset
= strstr(vs
->sasl
.mechlist
, mechname
);
489 if (offset
[-1] != ',' ||
490 (offset
[len
] != '\0'&&
491 offset
[len
] != ',')) {
496 g_free(vs
->sasl
.mechlist
);
497 vs
->sasl
.mechlist
= mechname
;
499 vnc_read_when(vs
, protocol_client_auth_sasl_start_len
, 4);
503 trace_vnc_auth_fail(vs
, vs
->auth
, "Unsupported mechname", mechname
);
504 vnc_client_error(vs
);
509 static int protocol_client_auth_sasl_mechname_len(VncState
*vs
, uint8_t *data
, size_t len
)
511 uint32_t mechlen
= read_u32(data
, 0);
514 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL mechname too long", "");
515 vnc_client_error(vs
);
519 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL mechname too short", "");
520 vnc_client_error(vs
);
523 vnc_read_when(vs
, protocol_client_auth_sasl_mechname
,mechlen
);
528 vnc_socket_ip_addr_string(QIOChannelSocket
*ioc
,
536 addr
= qio_channel_socket_get_local_address(ioc
, errp
);
538 addr
= qio_channel_socket_get_remote_address(ioc
, errp
);
544 if (addr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
545 error_setg(errp
, "Not an inet socket type");
546 qapi_free_SocketAddress(addr
);
549 ret
= g_strdup_printf("%s;%s", addr
->u
.inet
.host
, addr
->u
.inet
.port
);
550 qapi_free_SocketAddress(addr
);
554 void start_auth_sasl(VncState
*vs
)
556 const char *mechlist
= NULL
;
557 sasl_security_properties_t secprops
;
559 Error
*local_err
= NULL
;
560 char *localAddr
, *remoteAddr
;
563 /* Get local & remote client addresses in form IPADDR;PORT */
564 localAddr
= vnc_socket_ip_addr_string(vs
->sioc
, true, &local_err
);
566 trace_vnc_auth_fail(vs
, vs
->auth
, "Cannot format local IP",
567 error_get_pretty(local_err
));
571 remoteAddr
= vnc_socket_ip_addr_string(vs
->sioc
, false, &local_err
);
573 trace_vnc_auth_fail(vs
, vs
->auth
, "Cannot format remote IP",
574 error_get_pretty(local_err
));
579 err
= sasl_server_new("vnc",
580 NULL
, /* FQDN - just delegates to gethostname */
581 NULL
, /* User realm */
584 NULL
, /* Callbacks, not needed */
589 localAddr
= remoteAddr
= NULL
;
591 if (err
!= SASL_OK
) {
592 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL context setup failed",
593 sasl_errstring(err
, NULL
, NULL
));
594 vs
->sasl
.conn
= NULL
;
598 /* Inform SASL that we've got an external SSF layer from TLS/x509 */
599 if (vs
->auth
== VNC_AUTH_VENCRYPT
&&
600 vs
->subauth
== VNC_AUTH_VENCRYPT_X509SASL
) {
604 keysize
= qcrypto_tls_session_get_key_size(vs
->tls
,
607 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot TLS get cipher size",
608 error_get_pretty(local_err
));
609 sasl_dispose(&vs
->sasl
.conn
);
610 vs
->sasl
.conn
= NULL
;
613 ssf
= keysize
* CHAR_BIT
; /* tls key size is bytes, sasl wants bits */
615 err
= sasl_setprop(vs
->sasl
.conn
, SASL_SSF_EXTERNAL
, &ssf
);
616 if (err
!= SASL_OK
) {
617 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot set SASL external SSF",
618 sasl_errstring(err
, NULL
, NULL
));
619 sasl_dispose(&vs
->sasl
.conn
);
620 vs
->sasl
.conn
= NULL
;
624 vs
->sasl
.wantSSF
= 1;
627 memset (&secprops
, 0, sizeof secprops
);
628 /* Inform SASL that we've got an external SSF layer from TLS.
630 * Disable SSF, if using TLS+x509+SASL only. TLS without x509
631 * is not sufficiently strong
633 if (vs
->vd
->is_unix
||
634 (vs
->auth
== VNC_AUTH_VENCRYPT
&&
635 vs
->subauth
== VNC_AUTH_VENCRYPT_X509SASL
)) {
636 /* If we've got TLS or UNIX domain sock, we don't care about SSF */
637 secprops
.min_ssf
= 0;
638 secprops
.max_ssf
= 0;
639 secprops
.maxbufsize
= 8192;
640 secprops
.security_flags
= 0;
642 /* Plain TCP, better get an SSF layer */
643 secprops
.min_ssf
= 56; /* Good enough to require kerberos */
644 secprops
.max_ssf
= 100000; /* Arbitrary big number */
645 secprops
.maxbufsize
= 8192;
646 /* Forbid any anonymous or trivially crackable auth */
647 secprops
.security_flags
=
648 SASL_SEC_NOANONYMOUS
| SASL_SEC_NOPLAINTEXT
;
651 err
= sasl_setprop(vs
->sasl
.conn
, SASL_SEC_PROPS
, &secprops
);
652 if (err
!= SASL_OK
) {
653 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot set SASL security props",
654 sasl_errstring(err
, NULL
, NULL
));
655 sasl_dispose(&vs
->sasl
.conn
);
656 vs
->sasl
.conn
= NULL
;
660 err
= sasl_listmech(vs
->sasl
.conn
,
661 NULL
, /* Don't need to set user */
668 if (err
!= SASL_OK
) {
669 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot list SASL mechanisms",
670 sasl_errdetail(vs
->sasl
.conn
));
671 sasl_dispose(&vs
->sasl
.conn
);
672 vs
->sasl
.conn
= NULL
;
675 trace_vnc_auth_sasl_mech_list(vs
, mechlist
);
677 vs
->sasl
.mechlist
= g_strdup(mechlist
);
678 mechlistlen
= strlen(mechlist
);
679 vnc_write_u32(vs
, mechlistlen
);
680 vnc_write(vs
, mechlist
, mechlistlen
);
683 vnc_read_when(vs
, protocol_client_auth_sasl_mechname_len
, 4);
688 error_free(local_err
);
689 vnc_client_error(vs
);