1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "qemu/osdep.h"
28#include "vnc.h"
29#include "qapi/error.h"
30#include "qemu/main-loop.h"
31#include "trace.h"
32
33static void start_auth_vencrypt_subauth(VncState *vs)
34{
35 switch (vs->subauth) {
36 case VNC_AUTH_VENCRYPT_TLSNONE:
37 case VNC_AUTH_VENCRYPT_X509NONE:
38 vnc_write_u32(vs, 0);
39 start_client_init(vs);
40 break;
41
42 case VNC_AUTH_VENCRYPT_TLSVNC:
43 case VNC_AUTH_VENCRYPT_X509VNC:
44 start_auth_vnc(vs);
45 break;
46
47#ifdef CONFIG_VNC_SASL
48 case VNC_AUTH_VENCRYPT_TLSSASL:
49 case VNC_AUTH_VENCRYPT_X509SASL:
50 start_auth_sasl(vs);
51 break;
52#endif
53
54 default:
55 trace_vnc_auth_fail(vs, vs->auth, "Unhandled VeNCrypt subauth", "");
56 vnc_write_u8(vs, 1);
57 if (vs->minor >= 8) {
58 static const char err[] = "Unsupported authentication type";
59 vnc_write_u32(vs, sizeof(err));
60 vnc_write(vs, err, sizeof(err));
61 }
62 vnc_client_error(vs);
63 }
64}
65
66static void vnc_tls_handshake_done(QIOTask *task,
67 gpointer user_data)
68{
69 VncState *vs = user_data;
70 Error *err = NULL;
71
72 if (qio_task_propagate_error(task, &err)) {
73 trace_vnc_auth_fail(vs, vs->auth, "TLS handshake failed",
74 error_get_pretty(err));
75 vnc_client_error(vs);
76 error_free(err);
77 } else {
78 if (vs->ioc_tag) {
79 g_source_remove(vs->ioc_tag);
80 }
81 vs->ioc_tag = qio_channel_add_watch(
82 vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
83 start_auth_vencrypt_subauth(vs);
84 }
85}
86
87
88static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
89{
90 int auth = read_u32(data, 0);
91
92 trace_vnc_auth_vencrypt_subauth(vs, auth);
93 if (auth != vs->subauth) {
94 trace_vnc_auth_fail(vs, vs->auth, "Unsupported sub-auth version", "");
95 vnc_write_u8(vs, 0);
96 vnc_flush(vs);
97 vnc_client_error(vs);
98 } else {
99 Error *err = NULL;
100 QIOChannelTLS *tls;
101 vnc_write_u8(vs, 1);
102 vnc_flush(vs);
103
104 if (vs->ioc_tag) {
105 g_source_remove(vs->ioc_tag);
106 vs->ioc_tag = 0;
107 }
108
109 tls = qio_channel_tls_new_server(
110 vs->ioc,
111 vs->vd->tlscreds,
112 vs->vd->tlsauthzid,
113 &err);
114 if (!tls) {
115 trace_vnc_auth_fail(vs, vs->auth, "TLS setup failed",
116 error_get_pretty(err));
117 error_free(err);
118 vnc_client_error(vs);
119 return 0;
120 }
121
122 qio_channel_set_name(QIO_CHANNEL(tls), "vnc-server-tls");
123 object_unref(OBJECT(vs->ioc));
124 vs->ioc = QIO_CHANNEL(tls);
125 trace_vnc_client_io_wrap(vs, vs->ioc, "tls");
126 vs->tls = qio_channel_tls_get_session(tls);
127
128 qio_channel_tls_handshake(tls,
129 vnc_tls_handshake_done,
130 vs,
131 NULL,
132 NULL);
133 }
134 return 0;
135}
136
137static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len)
138{
139 trace_vnc_auth_vencrypt_version(vs, (int)data[0], (int)data[1]);
140 if (data[0] != 0 ||
141 data[1] != 2) {
142 trace_vnc_auth_fail(vs, vs->auth, "Unsupported version", "");
143 vnc_write_u8(vs, 1);
144 vnc_flush(vs);
145 vnc_client_error(vs);
146 } else {
147 vnc_write_u8(vs, 0);
148 vnc_write_u8(vs, 1);
149 vnc_write_u32(vs, vs->subauth);
150 vnc_flush(vs);
151 vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
152 }
153 return 0;
154}
155
156
157void start_auth_vencrypt(VncState *vs)
158{
159
160 vnc_write_u8(vs, 0);
161 vnc_write_u8(vs, 2);
162
163 vnc_read_when(vs, protocol_client_vencrypt_init, 2);
164}
165
166