crypto: fix building complaint
[qemu.git] / ui / vnc-auth-vencrypt.c
blob11c8c9a819fce621eeabe6b020c8e4d90b3677f9
1 /*
2 * QEMU VNC display driver: VeNCrypt authentication setup
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
6 * Copyright (C) 2009 Red Hat, Inc
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
27 #include "qemu/osdep.h"
28 #include "vnc.h"
29 #include "qapi/error.h"
30 #include "qemu/main-loop.h"
32 static void start_auth_vencrypt_subauth(VncState *vs)
34 switch (vs->subauth) {
35 case VNC_AUTH_VENCRYPT_TLSNONE:
36 case VNC_AUTH_VENCRYPT_X509NONE:
37 VNC_DEBUG("Accept TLS auth none\n");
38 vnc_write_u32(vs, 0); /* Accept auth completion */
39 start_client_init(vs);
40 break;
42 case VNC_AUTH_VENCRYPT_TLSVNC:
43 case VNC_AUTH_VENCRYPT_X509VNC:
44 VNC_DEBUG("Start TLS auth VNC\n");
45 start_auth_vnc(vs);
46 break;
48 #ifdef CONFIG_VNC_SASL
49 case VNC_AUTH_VENCRYPT_TLSSASL:
50 case VNC_AUTH_VENCRYPT_X509SASL:
51 VNC_DEBUG("Start TLS auth SASL\n");
52 start_auth_sasl(vs);
53 break;
54 #endif /* CONFIG_VNC_SASL */
56 default: /* Should not be possible, but just in case */
57 VNC_DEBUG("Reject subauth %d server bug\n", vs->auth);
58 vnc_write_u8(vs, 1);
59 if (vs->minor >= 8) {
60 static const char err[] = "Unsupported authentication type";
61 vnc_write_u32(vs, sizeof(err));
62 vnc_write(vs, err, sizeof(err));
64 vnc_client_error(vs);
68 static void vnc_tls_handshake_done(Object *source,
69 Error *err,
70 gpointer user_data)
72 VncState *vs = user_data;
74 if (err) {
75 VNC_DEBUG("Handshake failed %s\n",
76 error_get_pretty(err));
77 vnc_client_error(vs);
78 } else {
79 vs->ioc_tag = qio_channel_add_watch(
80 vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
81 start_auth_vencrypt_subauth(vs);
86 static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
88 int auth = read_u32(data, 0);
90 if (auth != vs->subauth) {
91 VNC_DEBUG("Rejecting auth %d\n", auth);
92 vnc_write_u8(vs, 0); /* Reject auth */
93 vnc_flush(vs);
94 vnc_client_error(vs);
95 } else {
96 Error *err = NULL;
97 QIOChannelTLS *tls;
98 VNC_DEBUG("Accepting auth %d, setting up TLS for handshake\n", auth);
99 vnc_write_u8(vs, 1); /* Accept auth */
100 vnc_flush(vs);
102 if (vs->ioc_tag) {
103 g_source_remove(vs->ioc_tag);
104 vs->ioc_tag = 0;
107 tls = qio_channel_tls_new_server(
108 vs->ioc,
109 vs->vd->tlscreds,
110 vs->vd->tlsaclname,
111 &err);
112 if (!tls) {
113 VNC_DEBUG("Failed to setup TLS %s\n", error_get_pretty(err));
114 error_free(err);
115 vnc_client_error(vs);
116 return 0;
119 VNC_DEBUG("Start TLS VeNCrypt handshake process\n");
120 object_unref(OBJECT(vs->ioc));
121 vs->ioc = QIO_CHANNEL(tls);
122 vs->tls = qio_channel_tls_get_session(tls);
124 qio_channel_tls_handshake(tls,
125 vnc_tls_handshake_done,
127 NULL);
129 return 0;
132 static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len)
134 if (data[0] != 0 ||
135 data[1] != 2) {
136 VNC_DEBUG("Unsupported VeNCrypt protocol %d.%d\n", (int)data[0], (int)data[1]);
137 vnc_write_u8(vs, 1); /* Reject version */
138 vnc_flush(vs);
139 vnc_client_error(vs);
140 } else {
141 VNC_DEBUG("Sending allowed auth %d\n", vs->subauth);
142 vnc_write_u8(vs, 0); /* Accept version */
143 vnc_write_u8(vs, 1); /* Number of sub-auths */
144 vnc_write_u32(vs, vs->subauth); /* The supported auth */
145 vnc_flush(vs);
146 vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
148 return 0;
152 void start_auth_vencrypt(VncState *vs)
154 /* Send VeNCrypt version 0.2 */
155 vnc_write_u8(vs, 0);
156 vnc_write_u8(vs, 2);
158 vnc_read_when(vs, protocol_client_vencrypt_init, 2);