root / vnc-tls.c @ a1f6684d
History | View | Annotate | Download (14 kB)
1 | 274b6fcc | aliguori | /*
|
---|---|---|---|
2 | 274b6fcc | aliguori | * QEMU VNC display driver: TLS helpers
|
3 | 274b6fcc | aliguori | *
|
4 | 274b6fcc | aliguori | * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
|
5 | 274b6fcc | aliguori | * Copyright (C) 2006 Fabrice Bellard
|
6 | 274b6fcc | aliguori | * Copyright (C) 2009 Red Hat, Inc
|
7 | 274b6fcc | aliguori | *
|
8 | 274b6fcc | aliguori | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 | 274b6fcc | aliguori | * of this software and associated documentation files (the "Software"), to deal
|
10 | 274b6fcc | aliguori | * in the Software without restriction, including without limitation the rights
|
11 | 274b6fcc | aliguori | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12 | 274b6fcc | aliguori | * copies of the Software, and to permit persons to whom the Software is
|
13 | 274b6fcc | aliguori | * furnished to do so, subject to the following conditions:
|
14 | 274b6fcc | aliguori | *
|
15 | 274b6fcc | aliguori | * The above copyright notice and this permission notice shall be included in
|
16 | 274b6fcc | aliguori | * all copies or substantial portions of the Software.
|
17 | 274b6fcc | aliguori | *
|
18 | 274b6fcc | aliguori | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19 | 274b6fcc | aliguori | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20 | 274b6fcc | aliguori | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
21 | 274b6fcc | aliguori | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22 | 274b6fcc | aliguori | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23 | 274b6fcc | aliguori | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24 | 274b6fcc | aliguori | * THE SOFTWARE.
|
25 | 274b6fcc | aliguori | */
|
26 | 274b6fcc | aliguori | |
27 | 274b6fcc | aliguori | #include "vnc.h" |
28 | 274b6fcc | aliguori | #include "qemu_socket.h" |
29 | 274b6fcc | aliguori | |
30 | 274b6fcc | aliguori | #if defined(_VNC_DEBUG) && _VNC_DEBUG >= 2 |
31 | 274b6fcc | aliguori | /* Very verbose, so only enabled for _VNC_DEBUG >= 2 */
|
32 | 274b6fcc | aliguori | static void vnc_debug_gnutls_log(int level, const char* str) { |
33 | 274b6fcc | aliguori | VNC_DEBUG("%d %s", level, str);
|
34 | 274b6fcc | aliguori | } |
35 | 274b6fcc | aliguori | #endif /* defined(_VNC_DEBUG) && _VNC_DEBUG >= 2 */ |
36 | 274b6fcc | aliguori | |
37 | 274b6fcc | aliguori | |
38 | 274b6fcc | aliguori | #define DH_BITS 1024 |
39 | 274b6fcc | aliguori | static gnutls_dh_params_t dh_params;
|
40 | 274b6fcc | aliguori | |
41 | 274b6fcc | aliguori | static int vnc_tls_initialize(void) |
42 | 274b6fcc | aliguori | { |
43 | 274b6fcc | aliguori | static int tlsinitialized = 0; |
44 | 274b6fcc | aliguori | |
45 | 274b6fcc | aliguori | if (tlsinitialized)
|
46 | 274b6fcc | aliguori | return 1; |
47 | 274b6fcc | aliguori | |
48 | 274b6fcc | aliguori | if (gnutls_global_init () < 0) |
49 | 274b6fcc | aliguori | return 0; |
50 | 274b6fcc | aliguori | |
51 | 274b6fcc | aliguori | /* XXX ought to re-generate diffie-hellmen params periodically */
|
52 | 274b6fcc | aliguori | if (gnutls_dh_params_init (&dh_params) < 0) |
53 | 274b6fcc | aliguori | return 0; |
54 | 274b6fcc | aliguori | if (gnutls_dh_params_generate2 (dh_params, DH_BITS) < 0) |
55 | 274b6fcc | aliguori | return 0; |
56 | 274b6fcc | aliguori | |
57 | 274b6fcc | aliguori | #if defined(_VNC_DEBUG) && _VNC_DEBUG >= 2 |
58 | 274b6fcc | aliguori | gnutls_global_set_log_level(10);
|
59 | 274b6fcc | aliguori | gnutls_global_set_log_function(vnc_debug_gnutls_log); |
60 | 274b6fcc | aliguori | #endif
|
61 | 274b6fcc | aliguori | |
62 | 274b6fcc | aliguori | tlsinitialized = 1;
|
63 | 274b6fcc | aliguori | |
64 | 274b6fcc | aliguori | return 1; |
65 | 274b6fcc | aliguori | } |
66 | 274b6fcc | aliguori | |
67 | 274b6fcc | aliguori | static ssize_t vnc_tls_push(gnutls_transport_ptr_t transport,
|
68 | 274b6fcc | aliguori | const void *data, |
69 | 274b6fcc | aliguori | size_t len) { |
70 | 274b6fcc | aliguori | struct VncState *vs = (struct VncState *)transport; |
71 | 274b6fcc | aliguori | int ret;
|
72 | 274b6fcc | aliguori | |
73 | 274b6fcc | aliguori | retry:
|
74 | 274b6fcc | aliguori | ret = send(vs->csock, data, len, 0);
|
75 | 274b6fcc | aliguori | if (ret < 0) { |
76 | 274b6fcc | aliguori | if (errno == EINTR)
|
77 | 274b6fcc | aliguori | goto retry;
|
78 | 274b6fcc | aliguori | return -1; |
79 | 274b6fcc | aliguori | } |
80 | 274b6fcc | aliguori | return ret;
|
81 | 274b6fcc | aliguori | } |
82 | 274b6fcc | aliguori | |
83 | 274b6fcc | aliguori | |
84 | 274b6fcc | aliguori | static ssize_t vnc_tls_pull(gnutls_transport_ptr_t transport,
|
85 | 274b6fcc | aliguori | void *data,
|
86 | 274b6fcc | aliguori | size_t len) { |
87 | 274b6fcc | aliguori | struct VncState *vs = (struct VncState *)transport; |
88 | 274b6fcc | aliguori | int ret;
|
89 | 274b6fcc | aliguori | |
90 | 274b6fcc | aliguori | retry:
|
91 | 274b6fcc | aliguori | ret = recv(vs->csock, data, len, 0);
|
92 | 274b6fcc | aliguori | if (ret < 0) { |
93 | 274b6fcc | aliguori | if (errno == EINTR)
|
94 | 274b6fcc | aliguori | goto retry;
|
95 | 274b6fcc | aliguori | return -1; |
96 | 274b6fcc | aliguori | } |
97 | 274b6fcc | aliguori | return ret;
|
98 | 274b6fcc | aliguori | } |
99 | 274b6fcc | aliguori | |
100 | 274b6fcc | aliguori | |
101 | 274b6fcc | aliguori | static gnutls_anon_server_credentials vnc_tls_initialize_anon_cred(void) |
102 | 274b6fcc | aliguori | { |
103 | 274b6fcc | aliguori | gnutls_anon_server_credentials anon_cred; |
104 | 274b6fcc | aliguori | int ret;
|
105 | 274b6fcc | aliguori | |
106 | 274b6fcc | aliguori | if ((ret = gnutls_anon_allocate_server_credentials(&anon_cred)) < 0) { |
107 | 274b6fcc | aliguori | VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret));
|
108 | 274b6fcc | aliguori | return NULL; |
109 | 274b6fcc | aliguori | } |
110 | 274b6fcc | aliguori | |
111 | 274b6fcc | aliguori | gnutls_anon_set_server_dh_params(anon_cred, dh_params); |
112 | 274b6fcc | aliguori | |
113 | 274b6fcc | aliguori | return anon_cred;
|
114 | 274b6fcc | aliguori | } |
115 | 274b6fcc | aliguori | |
116 | 274b6fcc | aliguori | |
117 | 274b6fcc | aliguori | static gnutls_certificate_credentials_t vnc_tls_initialize_x509_cred(VncDisplay *vd)
|
118 | 274b6fcc | aliguori | { |
119 | 274b6fcc | aliguori | gnutls_certificate_credentials_t x509_cred; |
120 | 274b6fcc | aliguori | int ret;
|
121 | 274b6fcc | aliguori | |
122 | 274b6fcc | aliguori | if (!vd->tls.x509cacert) {
|
123 | 274b6fcc | aliguori | VNC_DEBUG("No CA x509 certificate specified\n");
|
124 | 274b6fcc | aliguori | return NULL; |
125 | 274b6fcc | aliguori | } |
126 | 274b6fcc | aliguori | if (!vd->tls.x509cert) {
|
127 | 274b6fcc | aliguori | VNC_DEBUG("No server x509 certificate specified\n");
|
128 | 274b6fcc | aliguori | return NULL; |
129 | 274b6fcc | aliguori | } |
130 | 274b6fcc | aliguori | if (!vd->tls.x509key) {
|
131 | 274b6fcc | aliguori | VNC_DEBUG("No server private key specified\n");
|
132 | 274b6fcc | aliguori | return NULL; |
133 | 274b6fcc | aliguori | } |
134 | 274b6fcc | aliguori | |
135 | 274b6fcc | aliguori | if ((ret = gnutls_certificate_allocate_credentials(&x509_cred)) < 0) { |
136 | 274b6fcc | aliguori | VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret));
|
137 | 274b6fcc | aliguori | return NULL; |
138 | 274b6fcc | aliguori | } |
139 | 274b6fcc | aliguori | if ((ret = gnutls_certificate_set_x509_trust_file(x509_cred,
|
140 | 274b6fcc | aliguori | vd->tls.x509cacert, |
141 | 274b6fcc | aliguori | GNUTLS_X509_FMT_PEM)) < 0) {
|
142 | 274b6fcc | aliguori | VNC_DEBUG("Cannot load CA certificate %s\n", gnutls_strerror(ret));
|
143 | 274b6fcc | aliguori | gnutls_certificate_free_credentials(x509_cred); |
144 | 274b6fcc | aliguori | return NULL; |
145 | 274b6fcc | aliguori | } |
146 | 274b6fcc | aliguori | |
147 | 274b6fcc | aliguori | if ((ret = gnutls_certificate_set_x509_key_file (x509_cred,
|
148 | 274b6fcc | aliguori | vd->tls.x509cert, |
149 | 274b6fcc | aliguori | vd->tls.x509key, |
150 | 274b6fcc | aliguori | GNUTLS_X509_FMT_PEM)) < 0) {
|
151 | 274b6fcc | aliguori | VNC_DEBUG("Cannot load certificate & key %s\n", gnutls_strerror(ret));
|
152 | 274b6fcc | aliguori | gnutls_certificate_free_credentials(x509_cred); |
153 | 274b6fcc | aliguori | return NULL; |
154 | 274b6fcc | aliguori | } |
155 | 274b6fcc | aliguori | |
156 | 274b6fcc | aliguori | if (vd->tls.x509cacrl) {
|
157 | 274b6fcc | aliguori | if ((ret = gnutls_certificate_set_x509_crl_file(x509_cred,
|
158 | 274b6fcc | aliguori | vd->tls.x509cacrl, |
159 | 274b6fcc | aliguori | GNUTLS_X509_FMT_PEM)) < 0) {
|
160 | 274b6fcc | aliguori | VNC_DEBUG("Cannot load CRL %s\n", gnutls_strerror(ret));
|
161 | 274b6fcc | aliguori | gnutls_certificate_free_credentials(x509_cred); |
162 | 274b6fcc | aliguori | return NULL; |
163 | 274b6fcc | aliguori | } |
164 | 274b6fcc | aliguori | } |
165 | 274b6fcc | aliguori | |
166 | 274b6fcc | aliguori | gnutls_certificate_set_dh_params (x509_cred, dh_params); |
167 | 274b6fcc | aliguori | |
168 | 274b6fcc | aliguori | return x509_cred;
|
169 | 274b6fcc | aliguori | } |
170 | 274b6fcc | aliguori | |
171 | 274b6fcc | aliguori | |
172 | 274b6fcc | aliguori | int vnc_tls_validate_certificate(struct VncState *vs) |
173 | 274b6fcc | aliguori | { |
174 | 274b6fcc | aliguori | int ret;
|
175 | 274b6fcc | aliguori | unsigned int status; |
176 | 274b6fcc | aliguori | const gnutls_datum_t *certs;
|
177 | 274b6fcc | aliguori | unsigned int nCerts, i; |
178 | 274b6fcc | aliguori | time_t now; |
179 | 274b6fcc | aliguori | |
180 | 274b6fcc | aliguori | VNC_DEBUG("Validating client certificate\n");
|
181 | 274b6fcc | aliguori | if ((ret = gnutls_certificate_verify_peers2 (vs->tls.session, &status)) < 0) { |
182 | 274b6fcc | aliguori | VNC_DEBUG("Verify failed %s\n", gnutls_strerror(ret));
|
183 | 274b6fcc | aliguori | return -1; |
184 | 274b6fcc | aliguori | } |
185 | 274b6fcc | aliguori | |
186 | 274b6fcc | aliguori | if ((now = time(NULL)) == ((time_t)-1)) { |
187 | 274b6fcc | aliguori | return -1; |
188 | 274b6fcc | aliguori | } |
189 | 274b6fcc | aliguori | |
190 | 274b6fcc | aliguori | if (status != 0) { |
191 | 274b6fcc | aliguori | if (status & GNUTLS_CERT_INVALID)
|
192 | 274b6fcc | aliguori | VNC_DEBUG("The certificate is not trusted.\n");
|
193 | 274b6fcc | aliguori | |
194 | 274b6fcc | aliguori | if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
|
195 | 274b6fcc | aliguori | VNC_DEBUG("The certificate hasn't got a known issuer.\n");
|
196 | 274b6fcc | aliguori | |
197 | 274b6fcc | aliguori | if (status & GNUTLS_CERT_REVOKED)
|
198 | 274b6fcc | aliguori | VNC_DEBUG("The certificate has been revoked.\n");
|
199 | 274b6fcc | aliguori | |
200 | 274b6fcc | aliguori | if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
|
201 | 274b6fcc | aliguori | VNC_DEBUG("The certificate uses an insecure algorithm\n");
|
202 | 274b6fcc | aliguori | |
203 | 274b6fcc | aliguori | return -1; |
204 | 274b6fcc | aliguori | } else {
|
205 | 274b6fcc | aliguori | VNC_DEBUG("Certificate is valid!\n");
|
206 | 274b6fcc | aliguori | } |
207 | 274b6fcc | aliguori | |
208 | 274b6fcc | aliguori | /* Only support x509 for now */
|
209 | 274b6fcc | aliguori | if (gnutls_certificate_type_get(vs->tls.session) != GNUTLS_CRT_X509)
|
210 | 274b6fcc | aliguori | return -1; |
211 | 274b6fcc | aliguori | |
212 | 274b6fcc | aliguori | if (!(certs = gnutls_certificate_get_peers(vs->tls.session, &nCerts)))
|
213 | 274b6fcc | aliguori | return -1; |
214 | 274b6fcc | aliguori | |
215 | 274b6fcc | aliguori | for (i = 0 ; i < nCerts ; i++) { |
216 | 274b6fcc | aliguori | gnutls_x509_crt_t cert; |
217 | 274b6fcc | aliguori | VNC_DEBUG ("Checking certificate chain %d\n", i);
|
218 | 274b6fcc | aliguori | if (gnutls_x509_crt_init (&cert) < 0) |
219 | 274b6fcc | aliguori | return -1; |
220 | 274b6fcc | aliguori | |
221 | 274b6fcc | aliguori | if (gnutls_x509_crt_import(cert, &certs[i], GNUTLS_X509_FMT_DER) < 0) { |
222 | 274b6fcc | aliguori | gnutls_x509_crt_deinit (cert); |
223 | 274b6fcc | aliguori | return -1; |
224 | 274b6fcc | aliguori | } |
225 | 274b6fcc | aliguori | |
226 | 274b6fcc | aliguori | if (gnutls_x509_crt_get_expiration_time (cert) < now) {
|
227 | 274b6fcc | aliguori | VNC_DEBUG("The certificate has expired\n");
|
228 | 274b6fcc | aliguori | gnutls_x509_crt_deinit (cert); |
229 | 274b6fcc | aliguori | return -1; |
230 | 274b6fcc | aliguori | } |
231 | 274b6fcc | aliguori | |
232 | 274b6fcc | aliguori | if (gnutls_x509_crt_get_activation_time (cert) > now) {
|
233 | 274b6fcc | aliguori | VNC_DEBUG("The certificate is not yet activated\n");
|
234 | 274b6fcc | aliguori | gnutls_x509_crt_deinit (cert); |
235 | 274b6fcc | aliguori | return -1; |
236 | 274b6fcc | aliguori | } |
237 | 274b6fcc | aliguori | |
238 | 274b6fcc | aliguori | if (gnutls_x509_crt_get_activation_time (cert) > now) {
|
239 | 274b6fcc | aliguori | VNC_DEBUG("The certificate is not yet activated\n");
|
240 | 274b6fcc | aliguori | gnutls_x509_crt_deinit (cert); |
241 | 274b6fcc | aliguori | return -1; |
242 | 274b6fcc | aliguori | } |
243 | 274b6fcc | aliguori | |
244 | 274b6fcc | aliguori | if (i == 0) { |
245 | 274b6fcc | aliguori | size_t dnameSize = 1024;
|
246 | 274b6fcc | aliguori | vs->tls.dname = qemu_malloc(dnameSize); |
247 | 274b6fcc | aliguori | requery:
|
248 | 274b6fcc | aliguori | if ((ret = gnutls_x509_crt_get_dn (cert, vs->tls.dname, &dnameSize)) != 0) { |
249 | 274b6fcc | aliguori | if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
|
250 | 274b6fcc | aliguori | vs->tls.dname = qemu_realloc(vs->tls.dname, dnameSize); |
251 | 274b6fcc | aliguori | goto requery;
|
252 | 274b6fcc | aliguori | } |
253 | 274b6fcc | aliguori | gnutls_x509_crt_deinit (cert); |
254 | 274b6fcc | aliguori | VNC_DEBUG("Cannot get client distinguished name: %s",
|
255 | 274b6fcc | aliguori | gnutls_strerror (ret)); |
256 | 274b6fcc | aliguori | return -1; |
257 | 274b6fcc | aliguori | } |
258 | 274b6fcc | aliguori | |
259 | 274b6fcc | aliguori | if (vs->vd->tls.x509verify) {
|
260 | 274b6fcc | aliguori | int allow;
|
261 | 274b6fcc | aliguori | if (!vs->vd->tls.acl) {
|
262 | 274b6fcc | aliguori | VNC_DEBUG("no ACL activated, allowing access");
|
263 | 274b6fcc | aliguori | gnutls_x509_crt_deinit (cert); |
264 | 274b6fcc | aliguori | continue;
|
265 | 274b6fcc | aliguori | } |
266 | 274b6fcc | aliguori | |
267 | 274b6fcc | aliguori | allow = qemu_acl_party_is_allowed(vs->vd->tls.acl, |
268 | 274b6fcc | aliguori | vs->tls.dname); |
269 | 274b6fcc | aliguori | |
270 | 274b6fcc | aliguori | VNC_DEBUG("TLS x509 ACL check for %s is %s\n",
|
271 | 274b6fcc | aliguori | vs->tls.dname, allow ? "allowed" : "denied"); |
272 | 274b6fcc | aliguori | if (!allow) {
|
273 | 274b6fcc | aliguori | gnutls_x509_crt_deinit (cert); |
274 | 274b6fcc | aliguori | return -1; |
275 | 274b6fcc | aliguori | } |
276 | 274b6fcc | aliguori | } |
277 | 274b6fcc | aliguori | } |
278 | 274b6fcc | aliguori | |
279 | 274b6fcc | aliguori | gnutls_x509_crt_deinit (cert); |
280 | 274b6fcc | aliguori | } |
281 | 274b6fcc | aliguori | |
282 | 274b6fcc | aliguori | return 0; |
283 | 274b6fcc | aliguori | } |
284 | 274b6fcc | aliguori | |
285 | 274b6fcc | aliguori | |
286 | 274b6fcc | aliguori | int vnc_tls_client_setup(struct VncState *vs, |
287 | 274b6fcc | aliguori | int needX509Creds) {
|
288 | 274b6fcc | aliguori | static const int cert_type_priority[] = { GNUTLS_CRT_X509, 0 }; |
289 | 274b6fcc | aliguori | static const int protocol_priority[]= { GNUTLS_TLS1_1, GNUTLS_TLS1_0, GNUTLS_SSL3, 0 }; |
290 | 274b6fcc | aliguori | static const int kx_anon[] = {GNUTLS_KX_ANON_DH, 0}; |
291 | 274b6fcc | aliguori | static const int kx_x509[] = {GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA, GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0}; |
292 | 274b6fcc | aliguori | |
293 | 274b6fcc | aliguori | VNC_DEBUG("Do TLS setup\n");
|
294 | 274b6fcc | aliguori | if (vnc_tls_initialize() < 0) { |
295 | 274b6fcc | aliguori | VNC_DEBUG("Failed to init TLS\n");
|
296 | 274b6fcc | aliguori | vnc_client_error(vs); |
297 | 274b6fcc | aliguori | return -1; |
298 | 274b6fcc | aliguori | } |
299 | 274b6fcc | aliguori | if (vs->tls.session == NULL) { |
300 | 274b6fcc | aliguori | if (gnutls_init(&vs->tls.session, GNUTLS_SERVER) < 0) { |
301 | 274b6fcc | aliguori | vnc_client_error(vs); |
302 | 274b6fcc | aliguori | return -1; |
303 | 274b6fcc | aliguori | } |
304 | 274b6fcc | aliguori | |
305 | 274b6fcc | aliguori | if (gnutls_set_default_priority(vs->tls.session) < 0) { |
306 | 274b6fcc | aliguori | gnutls_deinit(vs->tls.session); |
307 | 274b6fcc | aliguori | vs->tls.session = NULL;
|
308 | 274b6fcc | aliguori | vnc_client_error(vs); |
309 | 274b6fcc | aliguori | return -1; |
310 | 274b6fcc | aliguori | } |
311 | 274b6fcc | aliguori | |
312 | 274b6fcc | aliguori | if (gnutls_kx_set_priority(vs->tls.session, needX509Creds ? kx_x509 : kx_anon) < 0) { |
313 | 274b6fcc | aliguori | gnutls_deinit(vs->tls.session); |
314 | 274b6fcc | aliguori | vs->tls.session = NULL;
|
315 | 274b6fcc | aliguori | vnc_client_error(vs); |
316 | 274b6fcc | aliguori | return -1; |
317 | 274b6fcc | aliguori | } |
318 | 274b6fcc | aliguori | |
319 | 274b6fcc | aliguori | if (gnutls_certificate_type_set_priority(vs->tls.session, cert_type_priority) < 0) { |
320 | 274b6fcc | aliguori | gnutls_deinit(vs->tls.session); |
321 | 274b6fcc | aliguori | vs->tls.session = NULL;
|
322 | 274b6fcc | aliguori | vnc_client_error(vs); |
323 | 274b6fcc | aliguori | return -1; |
324 | 274b6fcc | aliguori | } |
325 | 274b6fcc | aliguori | |
326 | 274b6fcc | aliguori | if (gnutls_protocol_set_priority(vs->tls.session, protocol_priority) < 0) { |
327 | 274b6fcc | aliguori | gnutls_deinit(vs->tls.session); |
328 | 274b6fcc | aliguori | vs->tls.session = NULL;
|
329 | 274b6fcc | aliguori | vnc_client_error(vs); |
330 | 274b6fcc | aliguori | return -1; |
331 | 274b6fcc | aliguori | } |
332 | 274b6fcc | aliguori | |
333 | 274b6fcc | aliguori | if (needX509Creds) {
|
334 | 274b6fcc | aliguori | gnutls_certificate_server_credentials x509_cred = vnc_tls_initialize_x509_cred(vs->vd); |
335 | 274b6fcc | aliguori | if (!x509_cred) {
|
336 | 274b6fcc | aliguori | gnutls_deinit(vs->tls.session); |
337 | 274b6fcc | aliguori | vs->tls.session = NULL;
|
338 | 274b6fcc | aliguori | vnc_client_error(vs); |
339 | 274b6fcc | aliguori | return -1; |
340 | 274b6fcc | aliguori | } |
341 | 274b6fcc | aliguori | if (gnutls_credentials_set(vs->tls.session, GNUTLS_CRD_CERTIFICATE, x509_cred) < 0) { |
342 | 274b6fcc | aliguori | gnutls_deinit(vs->tls.session); |
343 | 274b6fcc | aliguori | vs->tls.session = NULL;
|
344 | 274b6fcc | aliguori | gnutls_certificate_free_credentials(x509_cred); |
345 | 274b6fcc | aliguori | vnc_client_error(vs); |
346 | 274b6fcc | aliguori | return -1; |
347 | 274b6fcc | aliguori | } |
348 | 274b6fcc | aliguori | if (vs->vd->tls.x509verify) {
|
349 | 274b6fcc | aliguori | VNC_DEBUG("Requesting a client certificate\n");
|
350 | 274b6fcc | aliguori | gnutls_certificate_server_set_request (vs->tls.session, GNUTLS_CERT_REQUEST); |
351 | 274b6fcc | aliguori | } |
352 | 274b6fcc | aliguori | |
353 | 274b6fcc | aliguori | } else {
|
354 | 274b6fcc | aliguori | gnutls_anon_server_credentials anon_cred = vnc_tls_initialize_anon_cred(); |
355 | 274b6fcc | aliguori | if (!anon_cred) {
|
356 | 274b6fcc | aliguori | gnutls_deinit(vs->tls.session); |
357 | 274b6fcc | aliguori | vs->tls.session = NULL;
|
358 | 274b6fcc | aliguori | vnc_client_error(vs); |
359 | 274b6fcc | aliguori | return -1; |
360 | 274b6fcc | aliguori | } |
361 | 274b6fcc | aliguori | if (gnutls_credentials_set(vs->tls.session, GNUTLS_CRD_ANON, anon_cred) < 0) { |
362 | 274b6fcc | aliguori | gnutls_deinit(vs->tls.session); |
363 | 274b6fcc | aliguori | vs->tls.session = NULL;
|
364 | 274b6fcc | aliguori | gnutls_anon_free_server_credentials(anon_cred); |
365 | 274b6fcc | aliguori | vnc_client_error(vs); |
366 | 274b6fcc | aliguori | return -1; |
367 | 274b6fcc | aliguori | } |
368 | 274b6fcc | aliguori | } |
369 | 274b6fcc | aliguori | |
370 | 274b6fcc | aliguori | gnutls_transport_set_ptr(vs->tls.session, (gnutls_transport_ptr_t)vs); |
371 | 274b6fcc | aliguori | gnutls_transport_set_push_function(vs->tls.session, vnc_tls_push); |
372 | 274b6fcc | aliguori | gnutls_transport_set_pull_function(vs->tls.session, vnc_tls_pull); |
373 | 274b6fcc | aliguori | } |
374 | 274b6fcc | aliguori | return 0; |
375 | 274b6fcc | aliguori | } |
376 | 274b6fcc | aliguori | |
377 | 274b6fcc | aliguori | |
378 | 274b6fcc | aliguori | void vnc_tls_client_cleanup(struct VncState *vs) |
379 | 274b6fcc | aliguori | { |
380 | 274b6fcc | aliguori | if (vs->tls.session) {
|
381 | 274b6fcc | aliguori | gnutls_deinit(vs->tls.session); |
382 | 274b6fcc | aliguori | vs->tls.session = NULL;
|
383 | 274b6fcc | aliguori | } |
384 | 274b6fcc | aliguori | vs->tls.wiremode = VNC_WIREMODE_CLEAR; |
385 | 274b6fcc | aliguori | free(vs->tls.dname); |
386 | 274b6fcc | aliguori | } |
387 | 274b6fcc | aliguori | |
388 | 274b6fcc | aliguori | |
389 | 274b6fcc | aliguori | |
390 | 274b6fcc | aliguori | static int vnc_set_x509_credential(VncDisplay *vd, |
391 | 274b6fcc | aliguori | const char *certdir, |
392 | 274b6fcc | aliguori | const char *filename, |
393 | 274b6fcc | aliguori | char **cred,
|
394 | 274b6fcc | aliguori | int ignoreMissing)
|
395 | 274b6fcc | aliguori | { |
396 | 274b6fcc | aliguori | struct stat sb;
|
397 | 274b6fcc | aliguori | |
398 | 274b6fcc | aliguori | if (*cred) {
|
399 | 274b6fcc | aliguori | qemu_free(*cred); |
400 | 274b6fcc | aliguori | *cred = NULL;
|
401 | 274b6fcc | aliguori | } |
402 | 274b6fcc | aliguori | |
403 | 274b6fcc | aliguori | *cred = qemu_malloc(strlen(certdir) + strlen(filename) + 2);
|
404 | 274b6fcc | aliguori | |
405 | 274b6fcc | aliguori | strcpy(*cred, certdir); |
406 | 274b6fcc | aliguori | strcat(*cred, "/");
|
407 | 274b6fcc | aliguori | strcat(*cred, filename); |
408 | 274b6fcc | aliguori | |
409 | 274b6fcc | aliguori | VNC_DEBUG("Check %s\n", *cred);
|
410 | 274b6fcc | aliguori | if (stat(*cred, &sb) < 0) { |
411 | 274b6fcc | aliguori | qemu_free(*cred); |
412 | 274b6fcc | aliguori | *cred = NULL;
|
413 | 274b6fcc | aliguori | if (ignoreMissing && errno == ENOENT)
|
414 | 274b6fcc | aliguori | return 0; |
415 | 274b6fcc | aliguori | return -1; |
416 | 274b6fcc | aliguori | } |
417 | 274b6fcc | aliguori | |
418 | 274b6fcc | aliguori | return 0; |
419 | 274b6fcc | aliguori | } |
420 | 274b6fcc | aliguori | |
421 | 274b6fcc | aliguori | |
422 | 274b6fcc | aliguori | #define X509_CA_CERT_FILE "ca-cert.pem" |
423 | 274b6fcc | aliguori | #define X509_CA_CRL_FILE "ca-crl.pem" |
424 | 274b6fcc | aliguori | #define X509_SERVER_KEY_FILE "server-key.pem" |
425 | 274b6fcc | aliguori | #define X509_SERVER_CERT_FILE "server-cert.pem" |
426 | 274b6fcc | aliguori | |
427 | 274b6fcc | aliguori | |
428 | 274b6fcc | aliguori | int vnc_tls_set_x509_creds_dir(VncDisplay *vd,
|
429 | 274b6fcc | aliguori | const char *certdir) |
430 | 274b6fcc | aliguori | { |
431 | 274b6fcc | aliguori | if (vnc_set_x509_credential(vd, certdir, X509_CA_CERT_FILE, &vd->tls.x509cacert, 0) < 0) |
432 | 274b6fcc | aliguori | goto cleanup;
|
433 | 274b6fcc | aliguori | if (vnc_set_x509_credential(vd, certdir, X509_CA_CRL_FILE, &vd->tls.x509cacrl, 1) < 0) |
434 | 274b6fcc | aliguori | goto cleanup;
|
435 | 274b6fcc | aliguori | if (vnc_set_x509_credential(vd, certdir, X509_SERVER_CERT_FILE, &vd->tls.x509cert, 0) < 0) |
436 | 274b6fcc | aliguori | goto cleanup;
|
437 | 274b6fcc | aliguori | if (vnc_set_x509_credential(vd, certdir, X509_SERVER_KEY_FILE, &vd->tls.x509key, 0) < 0) |
438 | 274b6fcc | aliguori | goto cleanup;
|
439 | 274b6fcc | aliguori | |
440 | 274b6fcc | aliguori | return 0; |
441 | 274b6fcc | aliguori | |
442 | 274b6fcc | aliguori | cleanup:
|
443 | 274b6fcc | aliguori | qemu_free(vd->tls.x509cacert); |
444 | 274b6fcc | aliguori | qemu_free(vd->tls.x509cacrl); |
445 | 274b6fcc | aliguori | qemu_free(vd->tls.x509cert); |
446 | 274b6fcc | aliguori | qemu_free(vd->tls.x509key); |
447 | 274b6fcc | aliguori | vd->tls.x509cacert = vd->tls.x509cacrl = vd->tls.x509cert = vd->tls.x509key = NULL;
|
448 | 274b6fcc | aliguori | return -1; |
449 | 274b6fcc | aliguori | } |