blob: 8f3e8904a19c37b1447d814d3230b4adbed1299b [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)
50#error here
51#define GNUTLS_CERT_NOT_ACTIVATED 512
52#define GNUTLS_CERT_EXPIRED 1024
53#endif
54
55#if !defined(GNUTLS_VERSION_NUMBER) || (GNUTLS_VERSION_NUMBER < 0x020301)
56#define GNUTLS_CRT_PRINT_ONELINE 1
57#endif
58
Adam Tkacb10489b2010-04-23 14:16:04 +000059#define TLS_DEBUG
60
61using namespace rfb;
62
Adam Tkac3c5be392010-07-21 09:27:34 +000063StringParameter CSecurityTLS::x509ca("x509ca", "X509 CA certificate", "", ConfViewer);
64StringParameter CSecurityTLS::x509crl("x509crl", "X509 CRL file", "", ConfViewer);
Adam Tkac0e61c342010-07-21 09:23:25 +000065
Adam Tkacb10489b2010-04-23 14:16:04 +000066static LogWriter vlog("TLS");
67
68#ifdef TLS_DEBUG
69static void debug_log(int level, const char* str)
70{
71 vlog.debug(str);
72}
73#endif
74
Adam Tkac3c5be392010-07-21 09:27:34 +000075void CSecurityTLS::initGlobal()
Adam Tkacb10489b2010-04-23 14:16:04 +000076{
77 static bool globalInitDone = false;
78
79 if (!globalInitDone) {
80 gnutls_global_init();
81
82#ifdef TLS_DEBUG
83 gnutls_global_set_log_level(10);
84 gnutls_global_set_log_function(debug_log);
85#endif
86
87 globalInitDone = true;
88 }
89}
90
Adam Tkac3c5be392010-07-21 09:27:34 +000091CSecurityTLS::CSecurityTLS(bool _anon) : session(0), anon_cred(0),
Adam Tkac0e61c342010-07-21 09:23:25 +000092 anon(_anon), fis(0), fos(0)
Adam Tkacb10489b2010-04-23 14:16:04 +000093{
Adam Tkac0e61c342010-07-21 09:23:25 +000094 cafile = x509ca.getData();
95 crlfile = x509crl.getData();
Adam Tkacb10489b2010-04-23 14:16:04 +000096}
97
Adam Tkac27b2f772010-11-18 13:33:57 +000098void CSecurityTLS::setDefaults()
99{
100 char* homeDir = NULL;
101
Adam Tkacaf081722011-02-07 10:45:15 +0000102 if (getvnchomedir(&homeDir) == -1) {
103 vlog.error("Could not obtain VNC home directory path");
Adam Tkac27b2f772010-11-18 13:33:57 +0000104 return;
105 }
106
Adam Tkacaf081722011-02-07 10:45:15 +0000107 int len = strlen(homeDir) + 1;
Adam Tkac437b0c22011-02-07 10:46:16 +0000108 CharArray caDefault(len + 11);
109 CharArray crlDefault(len + 12);
110 sprintf(caDefault.buf, "%sx509_ca.pem", homeDir);
111 sprintf(crlDefault.buf, "%s509_crl.pem", homeDir);
Adam Tkac27b2f772010-11-18 13:33:57 +0000112 delete [] homeDir;
113
Adam Tkacf16a4212011-02-07 10:47:07 +0000114 if (!fileexists(caDefault.buf))
115 x509ca.setDefaultStr(strdup(caDefault.buf));
116 if (!fileexists(crlDefault.buf))
117 x509crl.setDefaultStr(strdup(crlDefault.buf));
Adam Tkac27b2f772010-11-18 13:33:57 +0000118}
119
Adam Tkac3c5be392010-07-21 09:27:34 +0000120void CSecurityTLS::shutdown()
Adam Tkacb10489b2010-04-23 14:16:04 +0000121{
Adam Tkac0e61c342010-07-21 09:23:25 +0000122 if (session)
Adam Tkac6948ead2010-08-11 15:58:59 +0000123 if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS)
124 throw Exception("gnutls_bye failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000125
126 if (anon_cred) {
127 gnutls_anon_free_client_credentials(anon_cred);
128 anon_cred = 0;
129 }
130
131 if (cert_cred) {
132 gnutls_certificate_free_credentials(cert_cred);
133 cert_cred = 0;
134 }
135
136 if (session) {
137 gnutls_deinit(session);
138 session = 0;
139
140 gnutls_global_deinit();
141 }
Adam Tkacb10489b2010-04-23 14:16:04 +0000142}
143
144
Adam Tkac3c5be392010-07-21 09:27:34 +0000145CSecurityTLS::~CSecurityTLS()
Adam Tkacb10489b2010-04-23 14:16:04 +0000146{
Adam Tkac0e61c342010-07-21 09:23:25 +0000147 shutdown();
148
Adam Tkacb10489b2010-04-23 14:16:04 +0000149 if (fis)
150 delete fis;
151 if (fos)
152 delete fos;
Adam Tkac0e61c342010-07-21 09:23:25 +0000153
154 delete[] cafile;
155 delete[] crlfile;
Adam Tkacb10489b2010-04-23 14:16:04 +0000156}
157
Adam Tkac3c5be392010-07-21 09:27:34 +0000158bool CSecurityTLS::processMsg(CConnection* cc)
Adam Tkacb10489b2010-04-23 14:16:04 +0000159{
160 rdr::InStream* is = cc->getInStream();
161 rdr::OutStream* os = cc->getOutStream();
162 client = cc;
163
164 initGlobal();
165
166 if (!session) {
167 if (!is->checkNoWait(1))
168 return false;
169
170 if (is->readU8() == 0)
171 return true;
172
Adam Tkac6948ead2010-08-11 15:58:59 +0000173 if (gnutls_init(&session, GNUTLS_CLIENT) != GNUTLS_E_SUCCESS)
174 throw AuthFailureException("gnutls_init failed");
175
176 if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
177 throw AuthFailureException("gnutls_set_default_priority failed");
Adam Tkacb10489b2010-04-23 14:16:04 +0000178
Adam Tkac0e61c342010-07-21 09:23:25 +0000179 setParam();
Adam Tkacb10489b2010-04-23 14:16:04 +0000180
181 gnutls_transport_set_pull_function(session, rdr::gnutls_InStream_pull);
182 gnutls_transport_set_push_function(session, rdr::gnutls_OutStream_push);
183 gnutls_transport_set_ptr2(session,
184 (gnutls_transport_ptr) is,
185 (gnutls_transport_ptr) os);
186 }
187
188 int err;
189 err = gnutls_handshake(session);
190 if (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(err))
191 return false;
192
193 if (err != GNUTLS_E_SUCCESS) {
194 vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err));
Adam Tkac0e61c342010-07-21 09:23:25 +0000195 shutdown();
Adam Tkacb10489b2010-04-23 14:16:04 +0000196 throw AuthFailureException("TLS Handshake failed");
197 }
Adam Tkac0e61c342010-07-21 09:23:25 +0000198
199 checkSession();
Adam Tkacb10489b2010-04-23 14:16:04 +0000200
201 cc->setStreams(fis = new rdr::TLSInStream(is, session),
202 fos = new rdr::TLSOutStream(os, session));
203
204 return true;
205}
206
Adam Tkac3c5be392010-07-21 09:27:34 +0000207void CSecurityTLS::setParam()
Adam Tkac0e61c342010-07-21 09:23:25 +0000208{
209 static const int kx_anon_priority[] = { GNUTLS_KX_ANON_DH, 0 };
210 static const int kx_priority[] = { GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA,
211 GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0 };
212
213 if (anon) {
Adam Tkac6948ead2010-08-11 15:58:59 +0000214 if (gnutls_kx_set_priority(session, kx_anon_priority) != GNUTLS_E_SUCCESS)
215 throw AuthFailureException("gnutls_kx_set_priority failed");
216
217 if (gnutls_anon_allocate_client_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
218 throw AuthFailureException("gnutls_anon_allocate_client_credentials failed");
219
220 if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred) != GNUTLS_E_SUCCESS)
221 throw AuthFailureException("gnutls_credentials_set failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000222
223 vlog.debug("Anonymous session has been set");
224 } else {
Adam Tkac6948ead2010-08-11 15:58:59 +0000225 if (gnutls_kx_set_priority(session, kx_priority) != GNUTLS_E_SUCCESS)
226 throw AuthFailureException("gnutls_kx_set_priority failed");
227
228 if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
229 throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000230
231 if (*cafile && gnutls_certificate_set_x509_trust_file(cert_cred,cafile,GNUTLS_X509_FMT_PEM) < 0)
232 throw AuthFailureException("load of CA cert failed");
233
234 if (*crlfile && gnutls_certificate_set_x509_crl_file(cert_cred,crlfile,GNUTLS_X509_FMT_PEM) < 0)
235 throw AuthFailureException("load of CRL failed");
236
Adam Tkac6948ead2010-08-11 15:58:59 +0000237 if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred) != GNUTLS_E_SUCCESS)
238 throw AuthFailureException("gnutls_credentials_set failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000239
240 vlog.debug("X509 session has been set");
241 }
242}
243
Adam Tkac3c5be392010-07-21 09:27:34 +0000244void CSecurityTLS::checkSession()
Adam Tkac0e61c342010-07-21 09:23:25 +0000245{
246 int status;
247 const gnutls_datum *cert_list;
248 unsigned int cert_list_size = 0;
249 unsigned int i;
DRCff1e1ff2011-02-08 23:43:55 +0000250 gnutls_datum info;
Adam Tkac0e61c342010-07-21 09:23:25 +0000251
252 if (anon)
253 return;
254
255 if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509)
256 throw AuthFailureException("unsupported certificate type");
257
258 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
259 if (!cert_list_size)
260 throw AuthFailureException("unsupported certificate type");
261
262 status = gnutls_certificate_verify_peers(session);
263 if (status == GNUTLS_E_NO_CERTIFICATE_FOUND)
264 throw AuthFailureException("no certificate sent");
265
266 if (status < 0) {
267 vlog.error("X509 verify failed: %s\n", gnutls_strerror (status));
268 throw AuthFailureException("certificate verification failed");
269 }
270
Adam Tkac27b2f772010-11-18 13:33:57 +0000271 if (status & GNUTLS_CERT_REVOKED) {
272 throw AuthFailureException("certificate has been revoked");
273 }
Adam Tkac0e61c342010-07-21 09:23:25 +0000274
Adam Tkac27b2f772010-11-18 13:33:57 +0000275 if (status & GNUTLS_CERT_NOT_ACTIVATED) {
276 throw AuthFailureException("certificate has not been activated");
277 }
Adam Tkac0e61c342010-07-21 09:23:25 +0000278
279 for (i = 0; i < cert_list_size; i++) {
280 gnutls_x509_crt crt;
281 gnutls_x509_crt_init(&crt);
282
283 if (gnutls_x509_crt_import(crt, &cert_list[i],GNUTLS_X509_FMT_DER) < 0)
Adam Tkac27b2f772010-11-18 13:33:57 +0000284 throw AuthFailureException("decoding of certificate failed");
285
DRCff1e1ff2011-02-08 23:43:55 +0000286 #if defined(GNUTLS_VERSION_NUMBER) && (GNUTLS_VERSION_NUMBER >= 0x010706)
Adam Tkac27b2f772010-11-18 13:33:57 +0000287 if (gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_ONELINE, &info)) {
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000288 /*
289 * GNUTLS doesn't correctly export gnutls_free symbol which is
290 * a function pointer. Linking with Visual Studio 2008 Express will
291 * fail when you call gnutls_free().
292 */
293#if WIN32
294 free(info.data);
295#else
Adam Tkac27b2f772010-11-18 13:33:57 +0000296 gnutls_free(info.data);
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000297#endif
Adam Tkac27b2f772010-11-18 13:33:57 +0000298 throw AuthFailureException("Could not find certificate to display");
299 }
DRCff1e1ff2011-02-08 23:43:55 +0000300 #endif
Adam Tkac0e61c342010-07-21 09:23:25 +0000301
302 if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) {
Adam Tkac27b2f772010-11-18 13:33:57 +0000303 char buf[255];
304 sprintf(buf, "Hostname (%s) does not match any certificate, do you want to continue?", client->getServerName());
305 vlog.debug("hostname mismatch");
306 if(!msg->showMsgBox(UserMsgBox::M_YESNO, "hostname mismatch", buf))
307 throw AuthFailureException("hostname mismatch");
Adam Tkac0e61c342010-07-21 09:23:25 +0000308 }
Adam Tkac27b2f772010-11-18 13:33:57 +0000309
310 if (status & GNUTLS_CERT_EXPIRED) {
311 vlog.debug("certificate has expired");
312 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certficate has expired", "The certificate of the server has expired, do you want to continue?"))
313 throw AuthFailureException("certificate has expired");
314 }
315
316 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) {
317 size_t out_size;
318 char *homeDir = NULL;
319 char *out_buf = NULL;
320 char *certinfo = NULL;
321 int len = 0;
322
323 vlog.debug("certificate issuer unknown");
324
325 len = snprintf(NULL, 0, "This certificate has been signed by an unknown authority:\n\n%s\n\nDo you want to save it and continue?\n ", info.data);
326 if (len < 0)
327 AuthFailureException("certificate decoding error");
328
329 vlog.debug("%s", info.data);
330
331 certinfo = new char[len];
332 if (certinfo == NULL)
333 throw AuthFailureException("Out of memory");
334
335 snprintf(certinfo, len, "This certificate has been signed by an unknown authority:\n\n%s\n\nDo you want to save it and continue? ", info.data);
336
337 for (int i = 0; i < len - 1; i++)
338 if (certinfo[i] == ',' && certinfo[i + 1] == ' ')
339 certinfo[i] = '\n';
340
341 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate issuer unknown",
342 certinfo)) {
343 delete [] certinfo;
344 throw AuthFailureException("certificate issuer unknown");
345 }
346 delete [] certinfo;
347
348 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, NULL, &out_size)
349 == GNUTLS_E_SHORT_MEMORY_BUFFER)
350 AuthFailureException("Out of memory");
351
352 // Save cert
353 out_buf = new char[out_size];
354 if (out_buf == NULL)
355 AuthFailureException("Out of memory");
356
357 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, out_buf, &out_size)
358 < 0)
359 AuthFailureException("certificate issuer unknown, and certificate "
360 "export failed");
Adam Tkacaf081722011-02-07 10:45:15 +0000361
362 if (getvnchomedir(&homeDir) == -1)
363 vlog.error("Could not obtain VNC home directory path");
Adam Tkac27b2f772010-11-18 13:33:57 +0000364 else {
365 FILE *f;
Adam Tkac437b0c22011-02-07 10:46:16 +0000366 CharArray caSave(strlen(homeDir) + 1 + 19);
367 sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir);
Adam Tkac27b2f772010-11-18 13:33:57 +0000368 delete [] homeDir;
369 f = fopen(caSave.buf, "a+");
370 if (!f)
371 msg->showMsgBox(UserMsgBox::M_OK, "certificate save failed",
372 "Could not save the certificate");
373 else {
374 fprintf(f, "%s\n", out_buf);
375 fclose(f);
376 }
377 }
378 delete [] out_buf;
379 } else if (status & GNUTLS_CERT_INVALID)
380 throw AuthFailureException("certificate not trusted");
381
Adam Tkac0e61c342010-07-21 09:23:25 +0000382 gnutls_x509_crt_deinit(crt);
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000383 /*
384 * GNUTLS doesn't correctly export gnutls_free symbol which is
385 * a function pointer. Linking with Visual Studio 2008 Express will
386 * fail when you call gnutls_free().
387 */
388#if WIN32
389 free(info.data);
390#else
Adam Tkac27b2f772010-11-18 13:33:57 +0000391 gnutls_free(info.data);
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000392#endif
Adam Tkac0e61c342010-07-21 09:23:25 +0000393 }
394}
395