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"
30 /* Max amount of data we send/recv for SASL steps to prevent DOS */
31 #define SASL_DATA_MAX_LEN (1024 * 1024)
34 void vnc_sasl_client_cleanup(VncState
*vs
)
37 vs
->sasl
.runSSF
= false;
38 vs
->sasl
.wantSSF
= false;
39 vs
->sasl
.waitWriteSSF
= 0;
40 vs
->sasl
.encodedLength
= vs
->sasl
.encodedOffset
= 0;
41 vs
->sasl
.encoded
= NULL
;
42 g_free(vs
->sasl
.username
);
43 g_free(vs
->sasl
.mechlist
);
44 vs
->sasl
.username
= vs
->sasl
.mechlist
= NULL
;
45 sasl_dispose(&vs
->sasl
.conn
);
51 size_t vnc_client_write_sasl(VncState
*vs
)
55 VNC_DEBUG("Write SASL: Pending output %p size %zd offset %zd "
56 "Encoded: %p size %d offset %d\n",
57 vs
->output
.buffer
, vs
->output
.capacity
, vs
->output
.offset
,
58 vs
->sasl
.encoded
, vs
->sasl
.encodedLength
, vs
->sasl
.encodedOffset
);
60 if (!vs
->sasl
.encoded
) {
62 err
= sasl_encode(vs
->sasl
.conn
,
63 (char *)vs
->output
.buffer
,
65 (const char **)&vs
->sasl
.encoded
,
66 &vs
->sasl
.encodedLength
);
68 return vnc_client_io_error(vs
, -1, NULL
);
70 vs
->sasl
.encodedRawLength
= vs
->output
.offset
;
71 vs
->sasl
.encodedOffset
= 0;
74 ret
= vnc_client_write_buf(vs
,
75 vs
->sasl
.encoded
+ vs
->sasl
.encodedOffset
,
76 vs
->sasl
.encodedLength
- vs
->sasl
.encodedOffset
);
80 vs
->sasl
.encodedOffset
+= ret
;
81 if (vs
->sasl
.encodedOffset
== vs
->sasl
.encodedLength
) {
82 if (vs
->sasl
.encodedRawLength
>= vs
->force_update_offset
) {
83 vs
->force_update_offset
= 0;
85 vs
->force_update_offset
-= vs
->sasl
.encodedRawLength
;
87 buffer_advance(&vs
->output
, vs
->sasl
.encodedRawLength
);
88 vs
->sasl
.encoded
= NULL
;
89 vs
->sasl
.encodedOffset
= vs
->sasl
.encodedLength
= 0;
92 /* Can't merge this block with one above, because
93 * someone might have written more unencrypted
94 * data in vs->output while we were processing
97 if (vs
->output
.offset
== 0) {
99 g_source_remove(vs
->ioc_tag
);
101 vs
->ioc_tag
= qio_channel_add_watch(
102 vs
->ioc
, G_IO_IN
, vnc_client_io
, vs
, NULL
);
109 size_t vnc_client_read_sasl(VncState
*vs
)
112 uint8_t encoded
[4096];
114 unsigned int decodedLen
;
117 ret
= vnc_client_read_buf(vs
, encoded
, sizeof(encoded
));
121 err
= sasl_decode(vs
->sasl
.conn
,
122 (char *)encoded
, ret
,
123 &decoded
, &decodedLen
);
126 return vnc_client_io_error(vs
, -1, NULL
);
127 VNC_DEBUG("Read SASL Encoded %p size %ld Decoded %p size %d\n",
128 encoded
, ret
, decoded
, decodedLen
);
129 buffer_reserve(&vs
->input
, decodedLen
);
130 buffer_append(&vs
->input
, decoded
, decodedLen
);
135 static int vnc_auth_sasl_check_access(VncState
*vs
)
141 err
= sasl_getprop(vs
->sasl
.conn
, SASL_USERNAME
, &val
);
142 if (err
!= SASL_OK
) {
143 trace_vnc_auth_fail(vs
, vs
->auth
, "Cannot fetch SASL username",
144 sasl_errstring(err
, NULL
, NULL
));
148 trace_vnc_auth_fail(vs
, vs
->auth
, "No SASL username set", "");
152 vs
->sasl
.username
= g_strdup((const char*)val
);
153 trace_vnc_auth_sasl_username(vs
, vs
->sasl
.username
);
155 if (vs
->vd
->sasl
.acl
== NULL
) {
156 trace_vnc_auth_sasl_acl(vs
, 1);
160 allow
= qemu_acl_party_is_allowed(vs
->vd
->sasl
.acl
, vs
->sasl
.username
);
162 trace_vnc_auth_sasl_acl(vs
, allow
);
163 return allow
? 0 : -1;
166 static int vnc_auth_sasl_check_ssf(VncState
*vs
)
171 if (!vs
->sasl
.wantSSF
)
174 err
= sasl_getprop(vs
->sasl
.conn
, SASL_SSF
, &val
);
178 ssf
= *(const int *)val
;
180 trace_vnc_auth_sasl_ssf(vs
, ssf
);
183 return 0; /* 56 is good for Kerberos */
185 /* Only setup for read initially, because we're about to send an RPC
186 * reply which must be in plain text. When the next incoming RPC
187 * arrives, we'll switch on writes too
189 * cf qemudClientReadSASL in qemud.c
193 /* We have a SSF that's good enough */
202 * u32 clientin-length
203 * u8-array clientin-string
207 * u32 serverout-length
208 * u8-array serverout-strin
212 static int protocol_client_auth_sasl_step_len(VncState
*vs
, uint8_t *data
, size_t len
);
214 static int protocol_client_auth_sasl_step(VncState
*vs
, uint8_t *data
, size_t len
)
216 uint32_t datalen
= len
;
217 const char *serverout
;
218 unsigned int serveroutlen
;
220 char *clientdata
= NULL
;
222 /* NB, distinction of NULL vs "" is *critical* in SASL */
224 clientdata
= (char*)data
;
225 clientdata
[datalen
-1] = '\0'; /* Wire includes '\0', but make sure */
226 datalen
--; /* Don't count NULL byte when passing to _start() */
229 err
= sasl_server_step(vs
->sasl
.conn
,
234 trace_vnc_auth_sasl_step(vs
, data
, len
, serverout
, serveroutlen
, err
);
235 if (err
!= SASL_OK
&&
236 err
!= SASL_CONTINUE
) {
237 trace_vnc_auth_fail(vs
, vs
->auth
, "Cannot step SASL auth",
238 sasl_errdetail(vs
->sasl
.conn
));
239 sasl_dispose(&vs
->sasl
.conn
);
240 vs
->sasl
.conn
= NULL
;
244 if (serveroutlen
> SASL_DATA_MAX_LEN
) {
245 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL data too long", "");
246 sasl_dispose(&vs
->sasl
.conn
);
247 vs
->sasl
.conn
= NULL
;
252 vnc_write_u32(vs
, serveroutlen
+ 1);
253 vnc_write(vs
, serverout
, serveroutlen
+ 1);
255 vnc_write_u32(vs
, 0);
258 /* Whether auth is complete */
259 vnc_write_u8(vs
, err
== SASL_CONTINUE
? 0 : 1);
261 if (err
== SASL_CONTINUE
) {
262 /* Wait for step length */
263 vnc_read_when(vs
, protocol_client_auth_sasl_step_len
, 4);
265 if (!vnc_auth_sasl_check_ssf(vs
)) {
266 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL SSF too weak", "");
270 /* Check username whitelist ACL */
271 if (vnc_auth_sasl_check_access(vs
) < 0) {
275 trace_vnc_auth_pass(vs
, vs
->auth
);
276 vnc_write_u32(vs
, 0); /* Accept auth */
278 * Delay writing in SSF encoded mode until pending output
282 vs
->sasl
.waitWriteSSF
= vs
->output
.offset
;
283 start_client_init(vs
);
289 vnc_write_u32(vs
, 1); /* Reject auth */
290 vnc_write_u32(vs
, sizeof("Authentication failed"));
291 vnc_write(vs
, "Authentication failed", sizeof("Authentication failed"));
293 vnc_client_error(vs
);
297 vnc_client_error(vs
);
301 static int protocol_client_auth_sasl_step_len(VncState
*vs
, uint8_t *data
, size_t len
)
303 uint32_t steplen
= read_u32(data
, 0);
305 if (steplen
> SASL_DATA_MAX_LEN
) {
306 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL step len too large", "");
307 vnc_client_error(vs
);
312 return protocol_client_auth_sasl_step(vs
, NULL
, 0);
314 vnc_read_when(vs
, protocol_client_auth_sasl_step
, steplen
);
323 * u32 clientin-length
324 * u8-array clientin-string
328 * u32 serverout-length
329 * u8-array serverout-strin
333 #define SASL_DATA_MAX_LEN (1024 * 1024)
335 static int protocol_client_auth_sasl_start(VncState
*vs
, uint8_t *data
, size_t len
)
337 uint32_t datalen
= len
;
338 const char *serverout
;
339 unsigned int serveroutlen
;
341 char *clientdata
= NULL
;
343 /* NB, distinction of NULL vs "" is *critical* in SASL */
345 clientdata
= (char*)data
;
346 clientdata
[datalen
-1] = '\0'; /* Should be on wire, but make sure */
347 datalen
--; /* Don't count NULL byte when passing to _start() */
350 err
= sasl_server_start(vs
->sasl
.conn
,
356 trace_vnc_auth_sasl_start(vs
, data
, len
, serverout
, serveroutlen
, err
);
357 if (err
!= SASL_OK
&&
358 err
!= SASL_CONTINUE
) {
359 trace_vnc_auth_fail(vs
, vs
->auth
, "Cannot start SASL auth",
360 sasl_errdetail(vs
->sasl
.conn
));
361 sasl_dispose(&vs
->sasl
.conn
);
362 vs
->sasl
.conn
= NULL
;
365 if (serveroutlen
> SASL_DATA_MAX_LEN
) {
366 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL data too long", "");
367 sasl_dispose(&vs
->sasl
.conn
);
368 vs
->sasl
.conn
= NULL
;
373 vnc_write_u32(vs
, serveroutlen
+ 1);
374 vnc_write(vs
, serverout
, serveroutlen
+ 1);
376 vnc_write_u32(vs
, 0);
379 /* Whether auth is complete */
380 vnc_write_u8(vs
, err
== SASL_CONTINUE
? 0 : 1);
382 if (err
== SASL_CONTINUE
) {
383 /* Wait for step length */
384 vnc_read_when(vs
, protocol_client_auth_sasl_step_len
, 4);
386 if (!vnc_auth_sasl_check_ssf(vs
)) {
387 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL SSF too weak", "");
391 /* Check username whitelist ACL */
392 if (vnc_auth_sasl_check_access(vs
) < 0) {
396 trace_vnc_auth_pass(vs
, vs
->auth
);
397 vnc_write_u32(vs
, 0); /* Accept auth */
398 start_client_init(vs
);
404 vnc_write_u32(vs
, 1); /* Reject auth */
405 vnc_write_u32(vs
, sizeof("Authentication failed"));
406 vnc_write(vs
, "Authentication failed", sizeof("Authentication failed"));
408 vnc_client_error(vs
);
412 vnc_client_error(vs
);
416 static int protocol_client_auth_sasl_start_len(VncState
*vs
, uint8_t *data
, size_t len
)
418 uint32_t startlen
= read_u32(data
, 0);
420 if (startlen
> SASL_DATA_MAX_LEN
) {
421 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL start len too large", "");
422 vnc_client_error(vs
);
427 return protocol_client_auth_sasl_start(vs
, NULL
, 0);
429 vnc_read_when(vs
, protocol_client_auth_sasl_start
, startlen
);
433 static int protocol_client_auth_sasl_mechname(VncState
*vs
, uint8_t *data
, size_t len
)
435 char *mechname
= g_strndup((const char *) data
, len
);
436 trace_vnc_auth_sasl_mech_choose(vs
, mechname
);
438 if (strncmp(vs
->sasl
.mechlist
, mechname
, len
) == 0) {
439 if (vs
->sasl
.mechlist
[len
] != '\0' &&
440 vs
->sasl
.mechlist
[len
] != ',') {
444 char *offset
= strstr(vs
->sasl
.mechlist
, mechname
);
448 if (offset
[-1] != ',' ||
449 (offset
[len
] != '\0'&&
450 offset
[len
] != ',')) {
455 g_free(vs
->sasl
.mechlist
);
456 vs
->sasl
.mechlist
= mechname
;
458 vnc_read_when(vs
, protocol_client_auth_sasl_start_len
, 4);
462 trace_vnc_auth_fail(vs
, vs
->auth
, "Unsupported mechname", mechname
);
463 vnc_client_error(vs
);
468 static int protocol_client_auth_sasl_mechname_len(VncState
*vs
, uint8_t *data
, size_t len
)
470 uint32_t mechlen
= read_u32(data
, 0);
473 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL mechname too long", "");
474 vnc_client_error(vs
);
478 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL mechname too short", "");
479 vnc_client_error(vs
);
482 vnc_read_when(vs
, protocol_client_auth_sasl_mechname
,mechlen
);
487 vnc_socket_ip_addr_string(QIOChannelSocket
*ioc
,
495 addr
= qio_channel_socket_get_local_address(ioc
, errp
);
497 addr
= qio_channel_socket_get_remote_address(ioc
, errp
);
503 if (addr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
504 error_setg(errp
, "Not an inet socket type");
507 ret
= g_strdup_printf("%s;%s", addr
->u
.inet
.host
, addr
->u
.inet
.port
);
508 qapi_free_SocketAddress(addr
);
512 void start_auth_sasl(VncState
*vs
)
514 const char *mechlist
= NULL
;
515 sasl_security_properties_t secprops
;
517 Error
*local_err
= NULL
;
518 char *localAddr
, *remoteAddr
;
521 /* Get local & remote client addresses in form IPADDR;PORT */
522 localAddr
= vnc_socket_ip_addr_string(vs
->sioc
, true, &local_err
);
524 trace_vnc_auth_fail(vs
, vs
->auth
, "Cannot format local IP",
525 error_get_pretty(local_err
));
529 remoteAddr
= vnc_socket_ip_addr_string(vs
->sioc
, false, &local_err
);
531 trace_vnc_auth_fail(vs
, vs
->auth
, "Cannot format remote IP",
532 error_get_pretty(local_err
));
537 err
= sasl_server_new("vnc",
538 NULL
, /* FQDN - just delegates to gethostname */
539 NULL
, /* User realm */
542 NULL
, /* Callbacks, not needed */
547 localAddr
= remoteAddr
= NULL
;
549 if (err
!= SASL_OK
) {
550 trace_vnc_auth_fail(vs
, vs
->auth
, "SASL context setup failed",
551 sasl_errstring(err
, NULL
, NULL
));
552 vs
->sasl
.conn
= NULL
;
556 /* Inform SASL that we've got an external SSF layer from TLS/x509 */
557 if (vs
->auth
== VNC_AUTH_VENCRYPT
&&
558 vs
->subauth
== VNC_AUTH_VENCRYPT_X509SASL
) {
559 Error
*local_err
= NULL
;
563 keysize
= qcrypto_tls_session_get_key_size(vs
->tls
,
566 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot TLS get cipher size",
567 error_get_pretty(local_err
));
568 error_free(local_err
);
569 sasl_dispose(&vs
->sasl
.conn
);
570 vs
->sasl
.conn
= NULL
;
573 ssf
= keysize
* CHAR_BIT
; /* tls key size is bytes, sasl wants bits */
575 err
= sasl_setprop(vs
->sasl
.conn
, SASL_SSF_EXTERNAL
, &ssf
);
576 if (err
!= SASL_OK
) {
577 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot set SASL external SSF",
578 sasl_errstring(err
, NULL
, NULL
));
579 sasl_dispose(&vs
->sasl
.conn
);
580 vs
->sasl
.conn
= NULL
;
584 vs
->sasl
.wantSSF
= 1;
587 memset (&secprops
, 0, sizeof secprops
);
588 /* Inform SASL that we've got an external SSF layer from TLS.
590 * Disable SSF, if using TLS+x509+SASL only. TLS without x509
591 * is not sufficiently strong
593 if (vs
->vd
->is_unix
||
594 (vs
->auth
== VNC_AUTH_VENCRYPT
&&
595 vs
->subauth
== VNC_AUTH_VENCRYPT_X509SASL
)) {
596 /* If we've got TLS or UNIX domain sock, we don't care about SSF */
597 secprops
.min_ssf
= 0;
598 secprops
.max_ssf
= 0;
599 secprops
.maxbufsize
= 8192;
600 secprops
.security_flags
= 0;
602 /* Plain TCP, better get an SSF layer */
603 secprops
.min_ssf
= 56; /* Good enough to require kerberos */
604 secprops
.max_ssf
= 100000; /* Arbitrary big number */
605 secprops
.maxbufsize
= 8192;
606 /* Forbid any anonymous or trivially crackable auth */
607 secprops
.security_flags
=
608 SASL_SEC_NOANONYMOUS
| SASL_SEC_NOPLAINTEXT
;
611 err
= sasl_setprop(vs
->sasl
.conn
, SASL_SEC_PROPS
, &secprops
);
612 if (err
!= SASL_OK
) {
613 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot set SASL security props",
614 sasl_errstring(err
, NULL
, NULL
));
615 sasl_dispose(&vs
->sasl
.conn
);
616 vs
->sasl
.conn
= NULL
;
620 err
= sasl_listmech(vs
->sasl
.conn
,
621 NULL
, /* Don't need to set user */
628 if (err
!= SASL_OK
) {
629 trace_vnc_auth_fail(vs
, vs
->auth
, "cannot list SASL mechanisms",
630 sasl_errdetail(vs
->sasl
.conn
));
631 sasl_dispose(&vs
->sasl
.conn
);
632 vs
->sasl
.conn
= NULL
;
635 trace_vnc_auth_sasl_mech_list(vs
, mechlist
);
637 vs
->sasl
.mechlist
= g_strdup(mechlist
);
638 mechlistlen
= strlen(mechlist
);
639 vnc_write_u32(vs
, mechlistlen
);
640 vnc_write(vs
, mechlist
, mechlistlen
);
643 vnc_read_when(vs
, protocol_client_auth_sasl_mechname_len
, 4);
648 error_free(local_err
);
649 vnc_client_error(vs
);