blob: 7d5846906fb38d2400ca2eb383076fac96813924 [file] [log] [blame]
DRCff1e1ff2011-02-08 23:43:55 +00001/*
Adam Tkacb10489b2010-04-23 14:16:04 +00002 * Copyright (C) 2004 Red Hat Inc.
3 * Copyright (C) 2005 Martin Koegler
4 * Copyright (C) 2010 TigerVNC Team
Adam Tkac27b2f772010-11-18 13:33:57 +00005 * Copyright (C) 2010 m-privacy GmbH
Adam Tkacb10489b2010-04-23 14:16:04 +00006 *
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this software; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
Adam Tkac43958232010-07-21 09:06:59 +000027#ifndef HAVE_GNUTLS
28#error "This header should not be compiled without HAVE_GNUTLS defined"
29#endif
Adam Tkacb10489b2010-04-23 14:16:04 +000030
Adam Tkac27b2f772010-11-18 13:33:57 +000031#include <stdlib.h>
Adam Tkacc4674db2011-01-19 14:11:16 +000032#ifndef WIN32
Adam Tkac27b2f772010-11-18 13:33:57 +000033#include <unistd.h>
Adam Tkacc4674db2011-01-19 14:11:16 +000034#endif
Adam Tkac27b2f772010-11-18 13:33:57 +000035
Adam Tkac3c5be392010-07-21 09:27:34 +000036#include <rfb/CSecurityTLS.h>
Adam Tkac0e61c342010-07-21 09:23:25 +000037#include <rfb/SSecurityVeNCrypt.h>
Adam Tkacb10489b2010-04-23 14:16:04 +000038#include <rfb/CConnection.h>
39#include <rfb/LogWriter.h>
40#include <rfb/Exception.h>
Adam Tkac27b2f772010-11-18 13:33:57 +000041#include <rfb/UserMsgBox.h>
Adam Tkacb10489b2010-04-23 14:16:04 +000042#include <rdr/TLSInStream.h>
43#include <rdr/TLSOutStream.h>
Adam Tkac27b2f772010-11-18 13:33:57 +000044#include <os/os.h>
Adam Tkac179d2b12011-01-19 14:15:14 +000045#include <os/print.h>
Adam Tkacb10489b2010-04-23 14:16:04 +000046
Adam Tkac0e61c342010-07-21 09:23:25 +000047#include <gnutls/x509.h>
48
DRCb7ab54f2011-02-09 03:27:26 +000049#if !defined(GNUTLS_VERSION_NUMBER) || (GNUTLS_VERSION_NUMBER < 0x020708)
DRCb7ab54f2011-02-09 03:27:26 +000050#define GNUTLS_CERT_NOT_ACTIVATED 512
51#define GNUTLS_CERT_EXPIRED 1024
52#endif
53
54#if !defined(GNUTLS_VERSION_NUMBER) || (GNUTLS_VERSION_NUMBER < 0x020301)
55#define GNUTLS_CRT_PRINT_ONELINE 1
56#endif
57
Adam Tkacb10489b2010-04-23 14:16:04 +000058#define TLS_DEBUG
59
60using namespace rfb;
61
Adam Tkac3c5be392010-07-21 09:27:34 +000062StringParameter CSecurityTLS::x509ca("x509ca", "X509 CA certificate", "", ConfViewer);
63StringParameter CSecurityTLS::x509crl("x509crl", "X509 CRL file", "", ConfViewer);
Adam Tkac0e61c342010-07-21 09:23:25 +000064
Adam Tkacb10489b2010-04-23 14:16:04 +000065static LogWriter vlog("TLS");
66
67#ifdef TLS_DEBUG
Adam Tkace32573a2011-02-09 14:13:41 +000068static LogWriter vlog_raw("Raw TLS");
69
Adam Tkacb10489b2010-04-23 14:16:04 +000070static void debug_log(int level, const char* str)
71{
Adam Tkace32573a2011-02-09 14:13:41 +000072 vlog_raw.debug(str);
Adam Tkacb10489b2010-04-23 14:16:04 +000073}
74#endif
75
Adam Tkac3c5be392010-07-21 09:27:34 +000076void CSecurityTLS::initGlobal()
Adam Tkacb10489b2010-04-23 14:16:04 +000077{
78 static bool globalInitDone = false;
79
80 if (!globalInitDone) {
81 gnutls_global_init();
82
83#ifdef TLS_DEBUG
84 gnutls_global_set_log_level(10);
85 gnutls_global_set_log_function(debug_log);
86#endif
87
88 globalInitDone = true;
89 }
90}
91
Adam Tkac3c5be392010-07-21 09:27:34 +000092CSecurityTLS::CSecurityTLS(bool _anon) : session(0), anon_cred(0),
Adam Tkac0e61c342010-07-21 09:23:25 +000093 anon(_anon), fis(0), fos(0)
Adam Tkacb10489b2010-04-23 14:16:04 +000094{
Adam Tkac0e61c342010-07-21 09:23:25 +000095 cafile = x509ca.getData();
96 crlfile = x509crl.getData();
Adam Tkacb10489b2010-04-23 14:16:04 +000097}
98
Adam Tkac27b2f772010-11-18 13:33:57 +000099void CSecurityTLS::setDefaults()
100{
101 char* homeDir = NULL;
102
Adam Tkacaf081722011-02-07 10:45:15 +0000103 if (getvnchomedir(&homeDir) == -1) {
104 vlog.error("Could not obtain VNC home directory path");
Adam Tkac27b2f772010-11-18 13:33:57 +0000105 return;
106 }
107
Adam Tkacaf081722011-02-07 10:45:15 +0000108 int len = strlen(homeDir) + 1;
Adam Tkac437b0c22011-02-07 10:46:16 +0000109 CharArray caDefault(len + 11);
110 CharArray crlDefault(len + 12);
111 sprintf(caDefault.buf, "%sx509_ca.pem", homeDir);
112 sprintf(crlDefault.buf, "%s509_crl.pem", homeDir);
Adam Tkac27b2f772010-11-18 13:33:57 +0000113 delete [] homeDir;
114
Adam Tkacf16a4212011-02-07 10:47:07 +0000115 if (!fileexists(caDefault.buf))
116 x509ca.setDefaultStr(strdup(caDefault.buf));
117 if (!fileexists(crlDefault.buf))
118 x509crl.setDefaultStr(strdup(crlDefault.buf));
Adam Tkac27b2f772010-11-18 13:33:57 +0000119}
120
Adam Tkac44cdb132011-02-09 14:09:10 +0000121void CSecurityTLS::shutdown(bool needbye)
Adam Tkacb10489b2010-04-23 14:16:04 +0000122{
Adam Tkac44cdb132011-02-09 14:09:10 +0000123 if (session && needbye)
Adam Tkac6948ead2010-08-11 15:58:59 +0000124 if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS)
Adam Tkac44cdb132011-02-09 14:09:10 +0000125 vlog.error("gnutls_bye failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000126
127 if (anon_cred) {
128 gnutls_anon_free_client_credentials(anon_cred);
129 anon_cred = 0;
130 }
131
132 if (cert_cred) {
133 gnutls_certificate_free_credentials(cert_cred);
134 cert_cred = 0;
135 }
136
137 if (session) {
138 gnutls_deinit(session);
139 session = 0;
140
141 gnutls_global_deinit();
142 }
Adam Tkacb10489b2010-04-23 14:16:04 +0000143}
144
145
Adam Tkac3c5be392010-07-21 09:27:34 +0000146CSecurityTLS::~CSecurityTLS()
Adam Tkacb10489b2010-04-23 14:16:04 +0000147{
Adam Tkac44cdb132011-02-09 14:09:10 +0000148 shutdown(true);
Adam Tkac0e61c342010-07-21 09:23:25 +0000149
Adam Tkacb10489b2010-04-23 14:16:04 +0000150 if (fis)
151 delete fis;
152 if (fos)
153 delete fos;
Adam Tkac0e61c342010-07-21 09:23:25 +0000154
155 delete[] cafile;
156 delete[] crlfile;
Adam Tkacb10489b2010-04-23 14:16:04 +0000157}
158
Adam Tkac3c5be392010-07-21 09:27:34 +0000159bool CSecurityTLS::processMsg(CConnection* cc)
Adam Tkacb10489b2010-04-23 14:16:04 +0000160{
161 rdr::InStream* is = cc->getInStream();
162 rdr::OutStream* os = cc->getOutStream();
163 client = cc;
164
165 initGlobal();
166
167 if (!session) {
168 if (!is->checkNoWait(1))
169 return false;
170
171 if (is->readU8() == 0)
172 return true;
173
Adam Tkac6948ead2010-08-11 15:58:59 +0000174 if (gnutls_init(&session, GNUTLS_CLIENT) != GNUTLS_E_SUCCESS)
175 throw AuthFailureException("gnutls_init failed");
176
177 if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
178 throw AuthFailureException("gnutls_set_default_priority failed");
Adam Tkacb10489b2010-04-23 14:16:04 +0000179
Adam Tkac0e61c342010-07-21 09:23:25 +0000180 setParam();
Adam Tkacb10489b2010-04-23 14:16:04 +0000181
182 gnutls_transport_set_pull_function(session, rdr::gnutls_InStream_pull);
183 gnutls_transport_set_push_function(session, rdr::gnutls_OutStream_push);
184 gnutls_transport_set_ptr2(session,
185 (gnutls_transport_ptr) is,
186 (gnutls_transport_ptr) os);
187 }
188
189 int err;
190 err = gnutls_handshake(session);
191 if (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(err))
192 return false;
193
194 if (err != GNUTLS_E_SUCCESS) {
195 vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err));
Adam Tkac44cdb132011-02-09 14:09:10 +0000196 shutdown(false);
Adam Tkacb10489b2010-04-23 14:16:04 +0000197 throw AuthFailureException("TLS Handshake failed");
198 }
Adam Tkac0e61c342010-07-21 09:23:25 +0000199
200 checkSession();
Adam Tkacb10489b2010-04-23 14:16:04 +0000201
202 cc->setStreams(fis = new rdr::TLSInStream(is, session),
203 fos = new rdr::TLSOutStream(os, session));
204
205 return true;
206}
207
Adam Tkac3c5be392010-07-21 09:27:34 +0000208void CSecurityTLS::setParam()
Adam Tkac0e61c342010-07-21 09:23:25 +0000209{
210 static const int kx_anon_priority[] = { GNUTLS_KX_ANON_DH, 0 };
211 static const int kx_priority[] = { GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA,
212 GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0 };
213
214 if (anon) {
Adam Tkac6948ead2010-08-11 15:58:59 +0000215 if (gnutls_kx_set_priority(session, kx_anon_priority) != GNUTLS_E_SUCCESS)
216 throw AuthFailureException("gnutls_kx_set_priority failed");
217
218 if (gnutls_anon_allocate_client_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
219 throw AuthFailureException("gnutls_anon_allocate_client_credentials failed");
220
221 if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred) != GNUTLS_E_SUCCESS)
222 throw AuthFailureException("gnutls_credentials_set failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000223
224 vlog.debug("Anonymous session has been set");
225 } else {
Adam Tkac6948ead2010-08-11 15:58:59 +0000226 if (gnutls_kx_set_priority(session, kx_priority) != GNUTLS_E_SUCCESS)
227 throw AuthFailureException("gnutls_kx_set_priority failed");
228
229 if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
230 throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000231
232 if (*cafile && gnutls_certificate_set_x509_trust_file(cert_cred,cafile,GNUTLS_X509_FMT_PEM) < 0)
233 throw AuthFailureException("load of CA cert failed");
234
Adam Tkace32573a2011-02-09 14:13:41 +0000235 /* Load previously saved certs */
236 char *homeDir = NULL;
237 int err;
238 if (getvnchomedir(&homeDir) == -1)
239 vlog.error("Could not obtain VNC home directory path");
240 else {
241 CharArray caSave(strlen(homeDir) + 19 + 1);
242 sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir);
243 delete [] homeDir;
244
245 err = gnutls_certificate_set_x509_trust_file(cert_cred, caSave.buf,
246 GNUTLS_X509_FMT_PEM);
247 if (err < 0)
248 vlog.debug("Failed to load saved server certificates from %s", caSave.buf);
249 }
250
Adam Tkac0e61c342010-07-21 09:23:25 +0000251 if (*crlfile && gnutls_certificate_set_x509_crl_file(cert_cred,crlfile,GNUTLS_X509_FMT_PEM) < 0)
252 throw AuthFailureException("load of CRL failed");
253
Adam Tkac6948ead2010-08-11 15:58:59 +0000254 if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred) != GNUTLS_E_SUCCESS)
255 throw AuthFailureException("gnutls_credentials_set failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000256
257 vlog.debug("X509 session has been set");
258 }
259}
260
Adam Tkac3c5be392010-07-21 09:27:34 +0000261void CSecurityTLS::checkSession()
Adam Tkac0e61c342010-07-21 09:23:25 +0000262{
Adam Tkace32573a2011-02-09 14:13:41 +0000263 const unsigned allowed_errors = GNUTLS_CERT_INVALID |
264 GNUTLS_CERT_SIGNER_NOT_FOUND |
265 GNUTLS_CERT_SIGNER_NOT_CA;
266 unsigned int status;
Adam Tkac0e61c342010-07-21 09:23:25 +0000267 const gnutls_datum *cert_list;
268 unsigned int cert_list_size = 0;
Adam Tkace32573a2011-02-09 14:13:41 +0000269 int err;
DRCff1e1ff2011-02-08 23:43:55 +0000270 gnutls_datum info;
Adam Tkac0e61c342010-07-21 09:23:25 +0000271
272 if (anon)
273 return;
274
275 if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509)
276 throw AuthFailureException("unsupported certificate type");
277
Adam Tkace32573a2011-02-09 14:13:41 +0000278 err = gnutls_certificate_verify_peers2(session, &status);
279 if (err != 0) {
280 vlog.error("server certificate verification failed: %s", gnutls_strerror(err));
281 throw AuthFailureException("server certificate verification failed");
282 }
283
284 if (status & GNUTLS_CERT_REVOKED)
285 throw AuthFailureException("server certificate has been revoked");
286
287 if (status & GNUTLS_CERT_NOT_ACTIVATED)
288 throw AuthFailureException("server certificate has not been activated");
289
290 if (status & GNUTLS_CERT_EXPIRED) {
291 vlog.debug("server certificate has expired");
292 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate has expired",
293 "The certificate of the server has expired, "
294 "do you want to continue?"))
295 throw AuthFailureException("server certificate has expired");
296 }
297 /* Process other errors later */
298
Adam Tkac0e61c342010-07-21 09:23:25 +0000299 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
300 if (!cert_list_size)
Adam Tkace32573a2011-02-09 14:13:41 +0000301 throw AuthFailureException("empty certificate chain");
Adam Tkac0e61c342010-07-21 09:23:25 +0000302
Adam Tkace32573a2011-02-09 14:13:41 +0000303 /* Process only server's certificate, not issuer's certificate */
304 gnutls_x509_crt crt;
305 gnutls_x509_crt_init(&crt);
Adam Tkac0e61c342010-07-21 09:23:25 +0000306
Adam Tkace32573a2011-02-09 14:13:41 +0000307 if (gnutls_x509_crt_import(crt, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
308 throw AuthFailureException("decoding of certificate failed");
309
310 if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) {
311 char buf[255];
312 vlog.debug("hostname mismatch");
313 snprintf(buf, sizeof(buf), "Hostname (%s) does not match any certificate, "
314 "do you want to continue?", client->getServerName());
315 buf[sizeof(buf) - 1] = '\0';
316 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "hostname mismatch", buf))
317 throw AuthFailureException("hostname mismatch");
Adam Tkac0e61c342010-07-21 09:23:25 +0000318 }
319
Adam Tkace32573a2011-02-09 14:13:41 +0000320 if (status == 0) {
321 /* Everything is fine (hostname + verification) */
Adam Tkac0e61c342010-07-21 09:23:25 +0000322 gnutls_x509_crt_deinit(crt);
Adam Tkace32573a2011-02-09 14:13:41 +0000323 return;
324 }
325
326 if (status & GNUTLS_CERT_INVALID)
327 vlog.debug("server certificate invalid");
328 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
329 vlog.debug("server cert signer not found");
330 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
331 vlog.debug("server cert signer not CA");
332
333 if ((status & (~allowed_errors)) != 0) {
334 /* No other errors are allowed */
335 vlog.debug("GNUTLS status of certificate verification: %u", status);
336 throw AuthFailureException("Invalid status of server certificate verification");
337 }
338
339 vlog.debug("Saved server certificates don't match");
340
341 #if defined(GNUTLS_VERSION_NUMBER) && (GNUTLS_VERSION_NUMBER >= 0x010706)
342 if (gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_ONELINE, &info)) {
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000343 /*
344 * GNUTLS doesn't correctly export gnutls_free symbol which is
345 * a function pointer. Linking with Visual Studio 2008 Express will
346 * fail when you call gnutls_free().
347 */
348#if WIN32
349 free(info.data);
350#else
Adam Tkac27b2f772010-11-18 13:33:57 +0000351 gnutls_free(info.data);
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000352#endif
Adam Tkace32573a2011-02-09 14:13:41 +0000353 throw AuthFailureException("Could not find certificate to display");
Adam Tkac0e61c342010-07-21 09:23:25 +0000354 }
Adam Tkace32573a2011-02-09 14:13:41 +0000355 #endif
356
357 size_t out_size;
358 char *out_buf = NULL;
359 char *certinfo = NULL;
360 int len = 0;
361
362 vlog.debug("certificate issuer unknown");
363
364 len = snprintf(NULL, 0, "This certificate has been signed by an unknown "
365 "authority:\n\n%s\n\nDo you want to save it and "
366 "continue?\n ", info.data);
367 if (len < 0)
368 AuthFailureException("certificate decoding error");
369
370 vlog.debug("%s", info.data);
371
372 certinfo = new char[len];
373 if (certinfo == NULL)
374 throw AuthFailureException("Out of memory");
375
376 snprintf(certinfo, len, "This certificate has been signed by an unknown "
377 "authority:\n\n%s\n\nDo you want to save it and "
378 "continue? ", info.data);
379
380 for (int i = 0; i < len - 1; i++)
381 if (certinfo[i] == ',' && certinfo[i + 1] == ' ')
382 certinfo[i] = '\n';
383
384 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate issuer unknown",
385 certinfo)) {
386 delete [] certinfo;
387 throw AuthFailureException("certificate issuer unknown");
388 }
389
390 delete [] certinfo;
391
392 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, NULL, &out_size)
393 == GNUTLS_E_SHORT_MEMORY_BUFFER)
394 AuthFailureException("Out of memory");
395
396 // Save cert
397 out_buf = new char[out_size];
398 if (out_buf == NULL)
399 AuthFailureException("Out of memory");
400
401 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, out_buf, &out_size) < 0)
402 AuthFailureException("certificate issuer unknown, and certificate "
403 "export failed");
404
405 char *homeDir = NULL;
406 if (getvnchomedir(&homeDir) == -1)
407 vlog.error("Could not obtain VNC home directory path");
408 else {
409 FILE *f;
410 CharArray caSave(strlen(homeDir) + 1 + 19);
411 sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir);
412 delete [] homeDir;
413 f = fopen(caSave.buf, "a+");
414 if (!f)
415 msg->showMsgBox(UserMsgBox::M_OK, "certificate save failed",
416 "Could not save the certificate");
417 else {
418 fprintf(f, "%s\n", out_buf);
419 fclose(f);
420 }
421 }
422
423 delete [] out_buf;
424
425 gnutls_x509_crt_deinit(crt);
426 /*
427 * GNUTLS doesn't correctly export gnutls_free symbol which is
428 * a function pointer. Linking with Visual Studio 2008 Express will
429 * fail when you call gnutls_free().
430 */
431#if WIN32
432 free(info.data);
433#else
434 gnutls_free(info.data);
435#endif
Adam Tkac0e61c342010-07-21 09:23:25 +0000436}
437