blob: 6b6b017c7d6aae0a719ad7278e8a6794df9ed0bf [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 Tkacb4864232011-02-09 15:38:37 +000060/* Ancient GNUTLS... */
61#if !defined(GNUTLS_VERSION_NUMBER) && !defined(LIBGNUTLS_VERSION_NUMBER)
62#define WITHOUT_X509_TIMES
63#endif
64
Adam Tkacb10489b2010-04-23 14:16:04 +000065using namespace rfb;
66
Adam Tkac3c5be392010-07-21 09:27:34 +000067StringParameter CSecurityTLS::x509ca("x509ca", "X509 CA certificate", "", ConfViewer);
68StringParameter CSecurityTLS::x509crl("x509crl", "X509 CRL file", "", ConfViewer);
Adam Tkac0e61c342010-07-21 09:23:25 +000069
Adam Tkacb10489b2010-04-23 14:16:04 +000070static LogWriter vlog("TLS");
Adam Tkac348269d2011-02-18 10:54:11 +000071static LogWriter vlog_raw("RawTLS");
Adam Tkace32573a2011-02-09 14:13:41 +000072
Adam Tkacb10489b2010-04-23 14:16:04 +000073static void debug_log(int level, const char* str)
74{
Pierre Ossmanad8609a2012-04-26 09:04:14 +000075 vlog_raw.debug("[%d]: %s", level, str);
Adam Tkacb10489b2010-04-23 14:16:04 +000076}
Adam Tkacb10489b2010-04-23 14:16:04 +000077
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
Adam Tkac348269d2011-02-18 10:54:11 +000085 /* 100 means debug log */
86 if (vlog_raw.getLevel() >= 100) {
87 gnutls_global_set_log_level(10);
88 gnutls_global_set_log_function(debug_log);
89 }
Adam Tkacb10489b2010-04-23 14:16:04 +000090
91 globalInitDone = true;
92 }
93}
94
Adam Tkac3c5be392010-07-21 09:27:34 +000095CSecurityTLS::CSecurityTLS(bool _anon) : session(0), anon_cred(0),
Adam Tkac0e61c342010-07-21 09:23:25 +000096 anon(_anon), fis(0), fos(0)
Adam Tkacb10489b2010-04-23 14:16:04 +000097{
Adam Tkac0e61c342010-07-21 09:23:25 +000098 cafile = x509ca.getData();
99 crlfile = x509crl.getData();
Adam Tkacb10489b2010-04-23 14:16:04 +0000100}
101
Adam Tkac27b2f772010-11-18 13:33:57 +0000102void CSecurityTLS::setDefaults()
103{
104 char* homeDir = NULL;
105
Adam Tkacaf081722011-02-07 10:45:15 +0000106 if (getvnchomedir(&homeDir) == -1) {
107 vlog.error("Could not obtain VNC home directory path");
Adam Tkac27b2f772010-11-18 13:33:57 +0000108 return;
109 }
110
Adam Tkacaf081722011-02-07 10:45:15 +0000111 int len = strlen(homeDir) + 1;
Adam Tkac437b0c22011-02-07 10:46:16 +0000112 CharArray caDefault(len + 11);
113 CharArray crlDefault(len + 12);
114 sprintf(caDefault.buf, "%sx509_ca.pem", homeDir);
115 sprintf(crlDefault.buf, "%s509_crl.pem", homeDir);
Adam Tkac27b2f772010-11-18 13:33:57 +0000116 delete [] homeDir;
117
Adam Tkacf16a4212011-02-07 10:47:07 +0000118 if (!fileexists(caDefault.buf))
119 x509ca.setDefaultStr(strdup(caDefault.buf));
120 if (!fileexists(crlDefault.buf))
121 x509crl.setDefaultStr(strdup(crlDefault.buf));
Adam Tkac27b2f772010-11-18 13:33:57 +0000122}
123
Adam Tkac44cdb132011-02-09 14:09:10 +0000124void CSecurityTLS::shutdown(bool needbye)
Adam Tkacb10489b2010-04-23 14:16:04 +0000125{
Adam Tkac44cdb132011-02-09 14:09:10 +0000126 if (session && needbye)
Adam Tkac6948ead2010-08-11 15:58:59 +0000127 if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS)
Adam Tkac44cdb132011-02-09 14:09:10 +0000128 vlog.error("gnutls_bye failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000129
130 if (anon_cred) {
131 gnutls_anon_free_client_credentials(anon_cred);
132 anon_cred = 0;
133 }
134
135 if (cert_cred) {
136 gnutls_certificate_free_credentials(cert_cred);
137 cert_cred = 0;
138 }
139
140 if (session) {
141 gnutls_deinit(session);
142 session = 0;
143
144 gnutls_global_deinit();
145 }
Adam Tkacb10489b2010-04-23 14:16:04 +0000146}
147
148
Adam Tkac3c5be392010-07-21 09:27:34 +0000149CSecurityTLS::~CSecurityTLS()
Adam Tkacb10489b2010-04-23 14:16:04 +0000150{
Adam Tkac44cdb132011-02-09 14:09:10 +0000151 shutdown(true);
Adam Tkac0e61c342010-07-21 09:23:25 +0000152
Adam Tkacb10489b2010-04-23 14:16:04 +0000153 if (fis)
154 delete fis;
155 if (fos)
156 delete fos;
Adam Tkac0e61c342010-07-21 09:23:25 +0000157
158 delete[] cafile;
159 delete[] crlfile;
Adam Tkacb10489b2010-04-23 14:16:04 +0000160}
161
Adam Tkac3c5be392010-07-21 09:27:34 +0000162bool CSecurityTLS::processMsg(CConnection* cc)
Adam Tkacb10489b2010-04-23 14:16:04 +0000163{
164 rdr::InStream* is = cc->getInStream();
165 rdr::OutStream* os = cc->getOutStream();
166 client = cc;
167
168 initGlobal();
169
170 if (!session) {
171 if (!is->checkNoWait(1))
172 return false;
173
Adam Tkacce6c8b02011-05-10 08:54:57 +0000174 if (is->readU8() == 0) {
175 rdr::U32 result = is->readU32();
176 CharArray reason;
177 if (result == secResultFailed || result == secResultTooMany)
178 reason.buf = is->readString();
179 else
180 reason.buf = strDup("Authentication failure (protocol error)");
181 throw AuthFailureException(reason.buf);
182 }
Adam Tkacb10489b2010-04-23 14:16:04 +0000183
Adam Tkac6948ead2010-08-11 15:58:59 +0000184 if (gnutls_init(&session, GNUTLS_CLIENT) != GNUTLS_E_SUCCESS)
185 throw AuthFailureException("gnutls_init failed");
186
187 if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
188 throw AuthFailureException("gnutls_set_default_priority failed");
Adam Tkacb10489b2010-04-23 14:16:04 +0000189
Adam Tkac0e61c342010-07-21 09:23:25 +0000190 setParam();
Adam Tkacb10489b2010-04-23 14:16:04 +0000191
192 gnutls_transport_set_pull_function(session, rdr::gnutls_InStream_pull);
193 gnutls_transport_set_push_function(session, rdr::gnutls_OutStream_push);
194 gnutls_transport_set_ptr2(session,
195 (gnutls_transport_ptr) is,
196 (gnutls_transport_ptr) os);
197 }
198
199 int err;
200 err = gnutls_handshake(session);
201 if (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(err))
202 return false;
203
204 if (err != GNUTLS_E_SUCCESS) {
205 vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err));
Adam Tkac44cdb132011-02-09 14:09:10 +0000206 shutdown(false);
Adam Tkacb10489b2010-04-23 14:16:04 +0000207 throw AuthFailureException("TLS Handshake failed");
208 }
Adam Tkac0e61c342010-07-21 09:23:25 +0000209
210 checkSession();
Adam Tkacb10489b2010-04-23 14:16:04 +0000211
212 cc->setStreams(fis = new rdr::TLSInStream(is, session),
213 fos = new rdr::TLSOutStream(os, session));
214
215 return true;
216}
217
Adam Tkac3c5be392010-07-21 09:27:34 +0000218void CSecurityTLS::setParam()
Adam Tkac0e61c342010-07-21 09:23:25 +0000219{
220 static const int kx_anon_priority[] = { GNUTLS_KX_ANON_DH, 0 };
221 static const int kx_priority[] = { GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA,
222 GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0 };
223
224 if (anon) {
Adam Tkac6948ead2010-08-11 15:58:59 +0000225 if (gnutls_kx_set_priority(session, kx_anon_priority) != GNUTLS_E_SUCCESS)
226 throw AuthFailureException("gnutls_kx_set_priority failed");
227
228 if (gnutls_anon_allocate_client_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
229 throw AuthFailureException("gnutls_anon_allocate_client_credentials failed");
230
231 if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred) != GNUTLS_E_SUCCESS)
232 throw AuthFailureException("gnutls_credentials_set failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000233
234 vlog.debug("Anonymous session has been set");
235 } else {
Adam Tkac6948ead2010-08-11 15:58:59 +0000236 if (gnutls_kx_set_priority(session, kx_priority) != GNUTLS_E_SUCCESS)
237 throw AuthFailureException("gnutls_kx_set_priority failed");
238
239 if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
240 throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000241
242 if (*cafile && gnutls_certificate_set_x509_trust_file(cert_cred,cafile,GNUTLS_X509_FMT_PEM) < 0)
243 throw AuthFailureException("load of CA cert failed");
244
Adam Tkace32573a2011-02-09 14:13:41 +0000245 /* Load previously saved certs */
246 char *homeDir = NULL;
247 int err;
248 if (getvnchomedir(&homeDir) == -1)
249 vlog.error("Could not obtain VNC home directory path");
250 else {
251 CharArray caSave(strlen(homeDir) + 19 + 1);
252 sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir);
253 delete [] homeDir;
254
255 err = gnutls_certificate_set_x509_trust_file(cert_cred, caSave.buf,
256 GNUTLS_X509_FMT_PEM);
257 if (err < 0)
258 vlog.debug("Failed to load saved server certificates from %s", caSave.buf);
259 }
260
Adam Tkac0e61c342010-07-21 09:23:25 +0000261 if (*crlfile && gnutls_certificate_set_x509_crl_file(cert_cred,crlfile,GNUTLS_X509_FMT_PEM) < 0)
262 throw AuthFailureException("load of CRL failed");
263
Adam Tkac6948ead2010-08-11 15:58:59 +0000264 if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred) != GNUTLS_E_SUCCESS)
265 throw AuthFailureException("gnutls_credentials_set failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000266
267 vlog.debug("X509 session has been set");
268 }
269}
270
Adam Tkac3c5be392010-07-21 09:27:34 +0000271void CSecurityTLS::checkSession()
Adam Tkac0e61c342010-07-21 09:23:25 +0000272{
Adam Tkace32573a2011-02-09 14:13:41 +0000273 const unsigned allowed_errors = GNUTLS_CERT_INVALID |
274 GNUTLS_CERT_SIGNER_NOT_FOUND |
275 GNUTLS_CERT_SIGNER_NOT_CA;
276 unsigned int status;
Adam Tkac0e61c342010-07-21 09:23:25 +0000277 const gnutls_datum *cert_list;
278 unsigned int cert_list_size = 0;
Adam Tkace32573a2011-02-09 14:13:41 +0000279 int err;
DRCff1e1ff2011-02-08 23:43:55 +0000280 gnutls_datum info;
Adam Tkac0e61c342010-07-21 09:23:25 +0000281
282 if (anon)
283 return;
284
285 if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509)
286 throw AuthFailureException("unsupported certificate type");
287
Adam Tkace32573a2011-02-09 14:13:41 +0000288 err = gnutls_certificate_verify_peers2(session, &status);
289 if (err != 0) {
290 vlog.error("server certificate verification failed: %s", gnutls_strerror(err));
291 throw AuthFailureException("server certificate verification failed");
292 }
293
294 if (status & GNUTLS_CERT_REVOKED)
295 throw AuthFailureException("server certificate has been revoked");
296
Adam Tkac68481c12011-02-09 14:15:09 +0000297#ifndef WITHOUT_X509_TIMES
Adam Tkace32573a2011-02-09 14:13:41 +0000298 if (status & GNUTLS_CERT_NOT_ACTIVATED)
299 throw AuthFailureException("server certificate has not been activated");
300
301 if (status & GNUTLS_CERT_EXPIRED) {
302 vlog.debug("server certificate has expired");
303 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate has expired",
304 "The certificate of the server has expired, "
305 "do you want to continue?"))
306 throw AuthFailureException("server certificate has expired");
307 }
Adam Tkac68481c12011-02-09 14:15:09 +0000308#endif
Adam Tkace32573a2011-02-09 14:13:41 +0000309 /* Process other errors later */
310
Adam Tkac0e61c342010-07-21 09:23:25 +0000311 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
312 if (!cert_list_size)
Adam Tkace32573a2011-02-09 14:13:41 +0000313 throw AuthFailureException("empty certificate chain");
Adam Tkac0e61c342010-07-21 09:23:25 +0000314
Adam Tkace32573a2011-02-09 14:13:41 +0000315 /* Process only server's certificate, not issuer's certificate */
316 gnutls_x509_crt crt;
317 gnutls_x509_crt_init(&crt);
Adam Tkac0e61c342010-07-21 09:23:25 +0000318
Adam Tkace32573a2011-02-09 14:13:41 +0000319 if (gnutls_x509_crt_import(crt, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
320 throw AuthFailureException("decoding of certificate failed");
321
322 if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) {
323 char buf[255];
324 vlog.debug("hostname mismatch");
325 snprintf(buf, sizeof(buf), "Hostname (%s) does not match any certificate, "
326 "do you want to continue?", client->getServerName());
327 buf[sizeof(buf) - 1] = '\0';
328 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "hostname mismatch", buf))
329 throw AuthFailureException("hostname mismatch");
Adam Tkac0e61c342010-07-21 09:23:25 +0000330 }
331
Adam Tkace32573a2011-02-09 14:13:41 +0000332 if (status == 0) {
333 /* Everything is fine (hostname + verification) */
Adam Tkac0e61c342010-07-21 09:23:25 +0000334 gnutls_x509_crt_deinit(crt);
Adam Tkace32573a2011-02-09 14:13:41 +0000335 return;
336 }
337
338 if (status & GNUTLS_CERT_INVALID)
339 vlog.debug("server certificate invalid");
340 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
341 vlog.debug("server cert signer not found");
342 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
343 vlog.debug("server cert signer not CA");
344
345 if ((status & (~allowed_errors)) != 0) {
346 /* No other errors are allowed */
347 vlog.debug("GNUTLS status of certificate verification: %u", status);
348 throw AuthFailureException("Invalid status of server certificate verification");
349 }
350
351 vlog.debug("Saved server certificates don't match");
352
Adam Tkace32573a2011-02-09 14:13:41 +0000353 if (gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_ONELINE, &info)) {
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000354 /*
355 * GNUTLS doesn't correctly export gnutls_free symbol which is
356 * a function pointer. Linking with Visual Studio 2008 Express will
357 * fail when you call gnutls_free().
358 */
359#if WIN32
360 free(info.data);
361#else
Adam Tkac27b2f772010-11-18 13:33:57 +0000362 gnutls_free(info.data);
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000363#endif
Adam Tkace32573a2011-02-09 14:13:41 +0000364 throw AuthFailureException("Could not find certificate to display");
Adam Tkac0e61c342010-07-21 09:23:25 +0000365 }
Adam Tkace32573a2011-02-09 14:13:41 +0000366
Adam Tkac68481c12011-02-09 14:15:09 +0000367 size_t out_size = 0;
Adam Tkace32573a2011-02-09 14:13:41 +0000368 char *out_buf = NULL;
369 char *certinfo = NULL;
370 int len = 0;
371
372 vlog.debug("certificate issuer unknown");
373
374 len = snprintf(NULL, 0, "This certificate has been signed by an unknown "
375 "authority:\n\n%s\n\nDo you want to save it and "
376 "continue?\n ", info.data);
377 if (len < 0)
378 AuthFailureException("certificate decoding error");
379
380 vlog.debug("%s", info.data);
381
382 certinfo = new char[len];
383 if (certinfo == NULL)
384 throw AuthFailureException("Out of memory");
385
386 snprintf(certinfo, len, "This certificate has been signed by an unknown "
387 "authority:\n\n%s\n\nDo you want to save it and "
388 "continue? ", info.data);
389
390 for (int i = 0; i < len - 1; i++)
391 if (certinfo[i] == ',' && certinfo[i + 1] == ' ')
392 certinfo[i] = '\n';
393
394 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate issuer unknown",
395 certinfo)) {
396 delete [] certinfo;
397 throw AuthFailureException("certificate issuer unknown");
398 }
399
400 delete [] certinfo;
401
402 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, NULL, &out_size)
403 == GNUTLS_E_SHORT_MEMORY_BUFFER)
404 AuthFailureException("Out of memory");
405
406 // Save cert
407 out_buf = new char[out_size];
408 if (out_buf == NULL)
409 AuthFailureException("Out of memory");
410
411 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, out_buf, &out_size) < 0)
412 AuthFailureException("certificate issuer unknown, and certificate "
413 "export failed");
414
415 char *homeDir = NULL;
416 if (getvnchomedir(&homeDir) == -1)
417 vlog.error("Could not obtain VNC home directory path");
418 else {
419 FILE *f;
420 CharArray caSave(strlen(homeDir) + 1 + 19);
421 sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir);
422 delete [] homeDir;
423 f = fopen(caSave.buf, "a+");
424 if (!f)
425 msg->showMsgBox(UserMsgBox::M_OK, "certificate save failed",
426 "Could not save the certificate");
427 else {
428 fprintf(f, "%s\n", out_buf);
429 fclose(f);
430 }
431 }
432
433 delete [] out_buf;
434
435 gnutls_x509_crt_deinit(crt);
436 /*
437 * GNUTLS doesn't correctly export gnutls_free symbol which is
438 * a function pointer. Linking with Visual Studio 2008 Express will
439 * fail when you call gnutls_free().
440 */
441#if WIN32
442 free(info.data);
443#else
444 gnutls_free(info.data);
445#endif
Adam Tkac0e61c342010-07-21 09:23:25 +0000446}
447