blob: f81c6340a4b73b3a12be7022770acf4b6cf3724f [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 Tkac68481c12011-02-09 14:15:09 +000046#include <os/tls.h>
Adam Tkacb10489b2010-04-23 14:16:04 +000047
Adam Tkac0e61c342010-07-21 09:23:25 +000048#include <gnutls/x509.h>
49
Adam Tkac68481c12011-02-09 14:15:09 +000050/*
51 * GNUTLS 2.6.5 and older didn't have some variables defined so don't use them.
52 * GNUTLS 1.X.X defined LIBGNUTLS_VERSION_NUMBER so treat it as "old" gnutls as
53 * well
54 */
55#if (defined(GNUTLS_VERSION_NUMBER) && GNUTLS_VERSION_NUMBER < 0x020606) || \
56 defined(LIBGNUTLS_VERSION_NUMBER)
57#define WITHOUT_X509_TIMES
DRCb7ab54f2011-02-09 03:27:26 +000058#endif
59
Adam Tkacb10489b2010-04-23 14:16:04 +000060#define TLS_DEBUG
61
62using namespace rfb;
63
Adam Tkac3c5be392010-07-21 09:27:34 +000064StringParameter CSecurityTLS::x509ca("x509ca", "X509 CA certificate", "", ConfViewer);
65StringParameter CSecurityTLS::x509crl("x509crl", "X509 CRL file", "", ConfViewer);
Adam Tkac0e61c342010-07-21 09:23:25 +000066
Adam Tkacb10489b2010-04-23 14:16:04 +000067static LogWriter vlog("TLS");
68
69#ifdef TLS_DEBUG
Adam Tkace32573a2011-02-09 14:13:41 +000070static LogWriter vlog_raw("Raw TLS");
71
Adam Tkacb10489b2010-04-23 14:16:04 +000072static void debug_log(int level, const char* str)
73{
Adam Tkace32573a2011-02-09 14:13:41 +000074 vlog_raw.debug(str);
Adam Tkacb10489b2010-04-23 14:16:04 +000075}
76#endif
77
Adam Tkac3c5be392010-07-21 09:27:34 +000078void CSecurityTLS::initGlobal()
Adam Tkacb10489b2010-04-23 14:16:04 +000079{
80 static bool globalInitDone = false;
81
82 if (!globalInitDone) {
83 gnutls_global_init();
84
85#ifdef TLS_DEBUG
86 gnutls_global_set_log_level(10);
87 gnutls_global_set_log_function(debug_log);
88#endif
89
90 globalInitDone = true;
91 }
92}
93
Adam Tkac3c5be392010-07-21 09:27:34 +000094CSecurityTLS::CSecurityTLS(bool _anon) : session(0), anon_cred(0),
Adam Tkac0e61c342010-07-21 09:23:25 +000095 anon(_anon), fis(0), fos(0)
Adam Tkacb10489b2010-04-23 14:16:04 +000096{
Adam Tkac0e61c342010-07-21 09:23:25 +000097 cafile = x509ca.getData();
98 crlfile = x509crl.getData();
Adam Tkacb10489b2010-04-23 14:16:04 +000099}
100
Adam Tkac27b2f772010-11-18 13:33:57 +0000101void CSecurityTLS::setDefaults()
102{
103 char* homeDir = NULL;
104
Adam Tkacaf081722011-02-07 10:45:15 +0000105 if (getvnchomedir(&homeDir) == -1) {
106 vlog.error("Could not obtain VNC home directory path");
Adam Tkac27b2f772010-11-18 13:33:57 +0000107 return;
108 }
109
Adam Tkacaf081722011-02-07 10:45:15 +0000110 int len = strlen(homeDir) + 1;
Adam Tkac437b0c22011-02-07 10:46:16 +0000111 CharArray caDefault(len + 11);
112 CharArray crlDefault(len + 12);
113 sprintf(caDefault.buf, "%sx509_ca.pem", homeDir);
114 sprintf(crlDefault.buf, "%s509_crl.pem", homeDir);
Adam Tkac27b2f772010-11-18 13:33:57 +0000115 delete [] homeDir;
116
Adam Tkacf16a4212011-02-07 10:47:07 +0000117 if (!fileexists(caDefault.buf))
118 x509ca.setDefaultStr(strdup(caDefault.buf));
119 if (!fileexists(crlDefault.buf))
120 x509crl.setDefaultStr(strdup(crlDefault.buf));
Adam Tkac27b2f772010-11-18 13:33:57 +0000121}
122
Adam Tkac44cdb132011-02-09 14:09:10 +0000123void CSecurityTLS::shutdown(bool needbye)
Adam Tkacb10489b2010-04-23 14:16:04 +0000124{
Adam Tkac44cdb132011-02-09 14:09:10 +0000125 if (session && needbye)
Adam Tkac6948ead2010-08-11 15:58:59 +0000126 if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS)
Adam Tkac44cdb132011-02-09 14:09:10 +0000127 vlog.error("gnutls_bye failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000128
129 if (anon_cred) {
130 gnutls_anon_free_client_credentials(anon_cred);
131 anon_cred = 0;
132 }
133
134 if (cert_cred) {
135 gnutls_certificate_free_credentials(cert_cred);
136 cert_cred = 0;
137 }
138
139 if (session) {
140 gnutls_deinit(session);
141 session = 0;
142
143 gnutls_global_deinit();
144 }
Adam Tkacb10489b2010-04-23 14:16:04 +0000145}
146
147
Adam Tkac3c5be392010-07-21 09:27:34 +0000148CSecurityTLS::~CSecurityTLS()
Adam Tkacb10489b2010-04-23 14:16:04 +0000149{
Adam Tkac44cdb132011-02-09 14:09:10 +0000150 shutdown(true);
Adam Tkac0e61c342010-07-21 09:23:25 +0000151
Adam Tkacb10489b2010-04-23 14:16:04 +0000152 if (fis)
153 delete fis;
154 if (fos)
155 delete fos;
Adam Tkac0e61c342010-07-21 09:23:25 +0000156
157 delete[] cafile;
158 delete[] crlfile;
Adam Tkacb10489b2010-04-23 14:16:04 +0000159}
160
Adam Tkac3c5be392010-07-21 09:27:34 +0000161bool CSecurityTLS::processMsg(CConnection* cc)
Adam Tkacb10489b2010-04-23 14:16:04 +0000162{
163 rdr::InStream* is = cc->getInStream();
164 rdr::OutStream* os = cc->getOutStream();
165 client = cc;
166
167 initGlobal();
168
169 if (!session) {
170 if (!is->checkNoWait(1))
171 return false;
172
173 if (is->readU8() == 0)
174 return true;
175
Adam Tkac6948ead2010-08-11 15:58:59 +0000176 if (gnutls_init(&session, GNUTLS_CLIENT) != GNUTLS_E_SUCCESS)
177 throw AuthFailureException("gnutls_init failed");
178
179 if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
180 throw AuthFailureException("gnutls_set_default_priority failed");
Adam Tkacb10489b2010-04-23 14:16:04 +0000181
Adam Tkac0e61c342010-07-21 09:23:25 +0000182 setParam();
Adam Tkacb10489b2010-04-23 14:16:04 +0000183
184 gnutls_transport_set_pull_function(session, rdr::gnutls_InStream_pull);
185 gnutls_transport_set_push_function(session, rdr::gnutls_OutStream_push);
186 gnutls_transport_set_ptr2(session,
187 (gnutls_transport_ptr) is,
188 (gnutls_transport_ptr) os);
189 }
190
191 int err;
192 err = gnutls_handshake(session);
193 if (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(err))
194 return false;
195
196 if (err != GNUTLS_E_SUCCESS) {
197 vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err));
Adam Tkac44cdb132011-02-09 14:09:10 +0000198 shutdown(false);
Adam Tkacb10489b2010-04-23 14:16:04 +0000199 throw AuthFailureException("TLS Handshake failed");
200 }
Adam Tkac0e61c342010-07-21 09:23:25 +0000201
202 checkSession();
Adam Tkacb10489b2010-04-23 14:16:04 +0000203
204 cc->setStreams(fis = new rdr::TLSInStream(is, session),
205 fos = new rdr::TLSOutStream(os, session));
206
207 return true;
208}
209
Adam Tkac3c5be392010-07-21 09:27:34 +0000210void CSecurityTLS::setParam()
Adam Tkac0e61c342010-07-21 09:23:25 +0000211{
212 static const int kx_anon_priority[] = { GNUTLS_KX_ANON_DH, 0 };
213 static const int kx_priority[] = { GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA,
214 GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0 };
215
216 if (anon) {
Adam Tkac6948ead2010-08-11 15:58:59 +0000217 if (gnutls_kx_set_priority(session, kx_anon_priority) != GNUTLS_E_SUCCESS)
218 throw AuthFailureException("gnutls_kx_set_priority failed");
219
220 if (gnutls_anon_allocate_client_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
221 throw AuthFailureException("gnutls_anon_allocate_client_credentials failed");
222
223 if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred) != GNUTLS_E_SUCCESS)
224 throw AuthFailureException("gnutls_credentials_set failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000225
226 vlog.debug("Anonymous session has been set");
227 } else {
Adam Tkac6948ead2010-08-11 15:58:59 +0000228 if (gnutls_kx_set_priority(session, kx_priority) != GNUTLS_E_SUCCESS)
229 throw AuthFailureException("gnutls_kx_set_priority failed");
230
231 if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
232 throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000233
234 if (*cafile && gnutls_certificate_set_x509_trust_file(cert_cred,cafile,GNUTLS_X509_FMT_PEM) < 0)
235 throw AuthFailureException("load of CA cert failed");
236
Adam Tkace32573a2011-02-09 14:13:41 +0000237 /* Load previously saved certs */
238 char *homeDir = NULL;
239 int err;
240 if (getvnchomedir(&homeDir) == -1)
241 vlog.error("Could not obtain VNC home directory path");
242 else {
243 CharArray caSave(strlen(homeDir) + 19 + 1);
244 sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir);
245 delete [] homeDir;
246
247 err = gnutls_certificate_set_x509_trust_file(cert_cred, caSave.buf,
248 GNUTLS_X509_FMT_PEM);
249 if (err < 0)
250 vlog.debug("Failed to load saved server certificates from %s", caSave.buf);
251 }
252
Adam Tkac0e61c342010-07-21 09:23:25 +0000253 if (*crlfile && gnutls_certificate_set_x509_crl_file(cert_cred,crlfile,GNUTLS_X509_FMT_PEM) < 0)
254 throw AuthFailureException("load of CRL failed");
255
Adam Tkac6948ead2010-08-11 15:58:59 +0000256 if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred) != GNUTLS_E_SUCCESS)
257 throw AuthFailureException("gnutls_credentials_set failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000258
259 vlog.debug("X509 session has been set");
260 }
261}
262
Adam Tkac3c5be392010-07-21 09:27:34 +0000263void CSecurityTLS::checkSession()
Adam Tkac0e61c342010-07-21 09:23:25 +0000264{
Adam Tkace32573a2011-02-09 14:13:41 +0000265 const unsigned allowed_errors = GNUTLS_CERT_INVALID |
266 GNUTLS_CERT_SIGNER_NOT_FOUND |
267 GNUTLS_CERT_SIGNER_NOT_CA;
268 unsigned int status;
Adam Tkac0e61c342010-07-21 09:23:25 +0000269 const gnutls_datum *cert_list;
270 unsigned int cert_list_size = 0;
Adam Tkace32573a2011-02-09 14:13:41 +0000271 int err;
DRCff1e1ff2011-02-08 23:43:55 +0000272 gnutls_datum info;
Adam Tkac0e61c342010-07-21 09:23:25 +0000273
274 if (anon)
275 return;
276
277 if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509)
278 throw AuthFailureException("unsupported certificate type");
279
Adam Tkace32573a2011-02-09 14:13:41 +0000280 err = gnutls_certificate_verify_peers2(session, &status);
281 if (err != 0) {
282 vlog.error("server certificate verification failed: %s", gnutls_strerror(err));
283 throw AuthFailureException("server certificate verification failed");
284 }
285
286 if (status & GNUTLS_CERT_REVOKED)
287 throw AuthFailureException("server certificate has been revoked");
288
Adam Tkac68481c12011-02-09 14:15:09 +0000289#ifndef WITHOUT_X509_TIMES
Adam Tkace32573a2011-02-09 14:13:41 +0000290 if (status & GNUTLS_CERT_NOT_ACTIVATED)
291 throw AuthFailureException("server certificate has not been activated");
292
293 if (status & GNUTLS_CERT_EXPIRED) {
294 vlog.debug("server certificate has expired");
295 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate has expired",
296 "The certificate of the server has expired, "
297 "do you want to continue?"))
298 throw AuthFailureException("server certificate has expired");
299 }
Adam Tkac68481c12011-02-09 14:15:09 +0000300#endif
Adam Tkace32573a2011-02-09 14:13:41 +0000301 /* Process other errors later */
302
Adam Tkac0e61c342010-07-21 09:23:25 +0000303 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
304 if (!cert_list_size)
Adam Tkace32573a2011-02-09 14:13:41 +0000305 throw AuthFailureException("empty certificate chain");
Adam Tkac0e61c342010-07-21 09:23:25 +0000306
Adam Tkace32573a2011-02-09 14:13:41 +0000307 /* Process only server's certificate, not issuer's certificate */
308 gnutls_x509_crt crt;
309 gnutls_x509_crt_init(&crt);
Adam Tkac0e61c342010-07-21 09:23:25 +0000310
Adam Tkace32573a2011-02-09 14:13:41 +0000311 if (gnutls_x509_crt_import(crt, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
312 throw AuthFailureException("decoding of certificate failed");
313
314 if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) {
315 char buf[255];
316 vlog.debug("hostname mismatch");
317 snprintf(buf, sizeof(buf), "Hostname (%s) does not match any certificate, "
318 "do you want to continue?", client->getServerName());
319 buf[sizeof(buf) - 1] = '\0';
320 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "hostname mismatch", buf))
321 throw AuthFailureException("hostname mismatch");
Adam Tkac0e61c342010-07-21 09:23:25 +0000322 }
323
Adam Tkace32573a2011-02-09 14:13:41 +0000324 if (status == 0) {
325 /* Everything is fine (hostname + verification) */
Adam Tkac0e61c342010-07-21 09:23:25 +0000326 gnutls_x509_crt_deinit(crt);
Adam Tkace32573a2011-02-09 14:13:41 +0000327 return;
328 }
329
330 if (status & GNUTLS_CERT_INVALID)
331 vlog.debug("server certificate invalid");
332 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
333 vlog.debug("server cert signer not found");
334 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
335 vlog.debug("server cert signer not CA");
336
337 if ((status & (~allowed_errors)) != 0) {
338 /* No other errors are allowed */
339 vlog.debug("GNUTLS status of certificate verification: %u", status);
340 throw AuthFailureException("Invalid status of server certificate verification");
341 }
342
343 vlog.debug("Saved server certificates don't match");
344
Adam Tkace32573a2011-02-09 14:13:41 +0000345 if (gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_ONELINE, &info)) {
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000346 /*
347 * GNUTLS doesn't correctly export gnutls_free symbol which is
348 * a function pointer. Linking with Visual Studio 2008 Express will
349 * fail when you call gnutls_free().
350 */
351#if WIN32
352 free(info.data);
353#else
Adam Tkac27b2f772010-11-18 13:33:57 +0000354 gnutls_free(info.data);
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000355#endif
Adam Tkace32573a2011-02-09 14:13:41 +0000356 throw AuthFailureException("Could not find certificate to display");
Adam Tkac0e61c342010-07-21 09:23:25 +0000357 }
Adam Tkace32573a2011-02-09 14:13:41 +0000358
Adam Tkac68481c12011-02-09 14:15:09 +0000359 size_t out_size = 0;
Adam Tkace32573a2011-02-09 14:13:41 +0000360 char *out_buf = NULL;
361 char *certinfo = NULL;
362 int len = 0;
363
364 vlog.debug("certificate issuer unknown");
365
366 len = snprintf(NULL, 0, "This certificate has been signed by an unknown "
367 "authority:\n\n%s\n\nDo you want to save it and "
368 "continue?\n ", info.data);
369 if (len < 0)
370 AuthFailureException("certificate decoding error");
371
372 vlog.debug("%s", info.data);
373
374 certinfo = new char[len];
375 if (certinfo == NULL)
376 throw AuthFailureException("Out of memory");
377
378 snprintf(certinfo, len, "This certificate has been signed by an unknown "
379 "authority:\n\n%s\n\nDo you want to save it and "
380 "continue? ", info.data);
381
382 for (int i = 0; i < len - 1; i++)
383 if (certinfo[i] == ',' && certinfo[i + 1] == ' ')
384 certinfo[i] = '\n';
385
386 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate issuer unknown",
387 certinfo)) {
388 delete [] certinfo;
389 throw AuthFailureException("certificate issuer unknown");
390 }
391
392 delete [] certinfo;
393
394 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, NULL, &out_size)
395 == GNUTLS_E_SHORT_MEMORY_BUFFER)
396 AuthFailureException("Out of memory");
397
398 // Save cert
399 out_buf = new char[out_size];
400 if (out_buf == NULL)
401 AuthFailureException("Out of memory");
402
403 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, out_buf, &out_size) < 0)
404 AuthFailureException("certificate issuer unknown, and certificate "
405 "export failed");
406
407 char *homeDir = NULL;
408 if (getvnchomedir(&homeDir) == -1)
409 vlog.error("Could not obtain VNC home directory path");
410 else {
411 FILE *f;
412 CharArray caSave(strlen(homeDir) + 1 + 19);
413 sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir);
414 delete [] homeDir;
415 f = fopen(caSave.buf, "a+");
416 if (!f)
417 msg->showMsgBox(UserMsgBox::M_OK, "certificate save failed",
418 "Could not save the certificate");
419 else {
420 fprintf(f, "%s\n", out_buf);
421 fclose(f);
422 }
423 }
424
425 delete [] out_buf;
426
427 gnutls_x509_crt_deinit(crt);
428 /*
429 * GNUTLS doesn't correctly export gnutls_free symbol which is
430 * a function pointer. Linking with Visual Studio 2008 Express will
431 * fail when you call gnutls_free().
432 */
433#if WIN32
434 free(info.data);
435#else
436 gnutls_free(info.data);
437#endif
Adam Tkac0e61c342010-07-21 09:23:25 +0000438}
439