blob: 9b29213eebbeccec310e9219c047c8a5cb3560c8 [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 Tkacb10489b2010-04-23 14:16:04 +000045
Adam Tkac0e61c342010-07-21 09:23:25 +000046#include <gnutls/x509.h>
47
Adam Tkac68481c12011-02-09 14:15:09 +000048/*
49 * GNUTLS 2.6.5 and older didn't have some variables defined so don't use them.
50 * GNUTLS 1.X.X defined LIBGNUTLS_VERSION_NUMBER so treat it as "old" gnutls as
51 * well
52 */
53#if (defined(GNUTLS_VERSION_NUMBER) && GNUTLS_VERSION_NUMBER < 0x020606) || \
54 defined(LIBGNUTLS_VERSION_NUMBER)
55#define WITHOUT_X509_TIMES
DRCb7ab54f2011-02-09 03:27:26 +000056#endif
57
Adam Tkacb4864232011-02-09 15:38:37 +000058/* Ancient GNUTLS... */
59#if !defined(GNUTLS_VERSION_NUMBER) && !defined(LIBGNUTLS_VERSION_NUMBER)
60#define WITHOUT_X509_TIMES
61#endif
62
Adam Tkacb10489b2010-04-23 14:16:04 +000063using namespace rfb;
64
Pierre Ossman3d2a84b2014-09-17 16:45:35 +020065StringParameter CSecurityTLS::X509CA("X509CA", "X509 CA certificate", "", ConfViewer);
66StringParameter CSecurityTLS::X509CRL("X509CRL", "X509 CRL file", "", ConfViewer);
Adam Tkac0e61c342010-07-21 09:23:25 +000067
Adam Tkacb10489b2010-04-23 14:16:04 +000068static LogWriter vlog("TLS");
Adam Tkacb10489b2010-04-23 14:16:04 +000069
Adam Tkac3c5be392010-07-21 09:27:34 +000070void CSecurityTLS::initGlobal()
Adam Tkacb10489b2010-04-23 14:16:04 +000071{
72 static bool globalInitDone = false;
73
74 if (!globalInitDone) {
75 gnutls_global_init();
Adam Tkacb10489b2010-04-23 14:16:04 +000076 globalInitDone = true;
77 }
78}
79
Adam Tkac3c5be392010-07-21 09:27:34 +000080CSecurityTLS::CSecurityTLS(bool _anon) : session(0), anon_cred(0),
Adam Tkac0e61c342010-07-21 09:23:25 +000081 anon(_anon), fis(0), fos(0)
Adam Tkacb10489b2010-04-23 14:16:04 +000082{
Pierre Ossman3d2a84b2014-09-17 16:45:35 +020083 cafile = X509CA.getData();
84 crlfile = X509CRL.getData();
Adam Tkacb10489b2010-04-23 14:16:04 +000085}
86
Adam Tkac27b2f772010-11-18 13:33:57 +000087void CSecurityTLS::setDefaults()
88{
89 char* homeDir = NULL;
90
Adam Tkacaf081722011-02-07 10:45:15 +000091 if (getvnchomedir(&homeDir) == -1) {
92 vlog.error("Could not obtain VNC home directory path");
Adam Tkac27b2f772010-11-18 13:33:57 +000093 return;
94 }
95
Adam Tkacaf081722011-02-07 10:45:15 +000096 int len = strlen(homeDir) + 1;
Adam Tkac437b0c22011-02-07 10:46:16 +000097 CharArray caDefault(len + 11);
98 CharArray crlDefault(len + 12);
99 sprintf(caDefault.buf, "%sx509_ca.pem", homeDir);
100 sprintf(crlDefault.buf, "%s509_crl.pem", homeDir);
Adam Tkac27b2f772010-11-18 13:33:57 +0000101 delete [] homeDir;
102
Adam Tkacf16a4212011-02-07 10:47:07 +0000103 if (!fileexists(caDefault.buf))
Pierre Ossman3d2a84b2014-09-17 16:45:35 +0200104 X509CA.setDefaultStr(strdup(caDefault.buf));
Adam Tkacf16a4212011-02-07 10:47:07 +0000105 if (!fileexists(crlDefault.buf))
Pierre Ossman3d2a84b2014-09-17 16:45:35 +0200106 X509CRL.setDefaultStr(strdup(crlDefault.buf));
Adam Tkac27b2f772010-11-18 13:33:57 +0000107}
108
Adam Tkac44cdb132011-02-09 14:09:10 +0000109void CSecurityTLS::shutdown(bool needbye)
Adam Tkacb10489b2010-04-23 14:16:04 +0000110{
Adam Tkac44cdb132011-02-09 14:09:10 +0000111 if (session && needbye)
Adam Tkac6948ead2010-08-11 15:58:59 +0000112 if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS)
Adam Tkac44cdb132011-02-09 14:09:10 +0000113 vlog.error("gnutls_bye failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000114
115 if (anon_cred) {
116 gnutls_anon_free_client_credentials(anon_cred);
117 anon_cred = 0;
118 }
119
120 if (cert_cred) {
121 gnutls_certificate_free_credentials(cert_cred);
122 cert_cred = 0;
123 }
124
125 if (session) {
126 gnutls_deinit(session);
127 session = 0;
128
129 gnutls_global_deinit();
130 }
Adam Tkacb10489b2010-04-23 14:16:04 +0000131}
132
133
Adam Tkac3c5be392010-07-21 09:27:34 +0000134CSecurityTLS::~CSecurityTLS()
Adam Tkacb10489b2010-04-23 14:16:04 +0000135{
Adam Tkac44cdb132011-02-09 14:09:10 +0000136 shutdown(true);
Adam Tkac0e61c342010-07-21 09:23:25 +0000137
Adam Tkacb10489b2010-04-23 14:16:04 +0000138 if (fis)
139 delete fis;
140 if (fos)
141 delete fos;
Adam Tkac0e61c342010-07-21 09:23:25 +0000142
143 delete[] cafile;
144 delete[] crlfile;
Adam Tkacb10489b2010-04-23 14:16:04 +0000145}
146
Adam Tkac3c5be392010-07-21 09:27:34 +0000147bool CSecurityTLS::processMsg(CConnection* cc)
Adam Tkacb10489b2010-04-23 14:16:04 +0000148{
149 rdr::InStream* is = cc->getInStream();
150 rdr::OutStream* os = cc->getOutStream();
151 client = cc;
152
153 initGlobal();
154
155 if (!session) {
156 if (!is->checkNoWait(1))
157 return false;
158
Adam Tkacce6c8b02011-05-10 08:54:57 +0000159 if (is->readU8() == 0) {
160 rdr::U32 result = is->readU32();
161 CharArray reason;
162 if (result == secResultFailed || result == secResultTooMany)
163 reason.buf = is->readString();
164 else
165 reason.buf = strDup("Authentication failure (protocol error)");
166 throw AuthFailureException(reason.buf);
167 }
Adam Tkacb10489b2010-04-23 14:16:04 +0000168
Adam Tkac6948ead2010-08-11 15:58:59 +0000169 if (gnutls_init(&session, GNUTLS_CLIENT) != GNUTLS_E_SUCCESS)
170 throw AuthFailureException("gnutls_init failed");
171
172 if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
173 throw AuthFailureException("gnutls_set_default_priority failed");
Adam Tkacb10489b2010-04-23 14:16:04 +0000174
Adam Tkac0e61c342010-07-21 09:23:25 +0000175 setParam();
Adam Tkacb10489b2010-04-23 14:16:04 +0000176 }
177
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000178 rdr::TLSInStream *tlsis = new rdr::TLSInStream(is, session);
179 rdr::TLSOutStream *tlsos = new rdr::TLSOutStream(os, session);
180
Adam Tkacb10489b2010-04-23 14:16:04 +0000181 int err;
182 err = gnutls_handshake(session);
Adam Tkacb10489b2010-04-23 14:16:04 +0000183 if (err != GNUTLS_E_SUCCESS) {
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000184 delete tlsis;
185 delete tlsos;
186
187 if (!gnutls_error_is_fatal(err))
188 return false;
189
Adam Tkacb10489b2010-04-23 14:16:04 +0000190 vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err));
Adam Tkac44cdb132011-02-09 14:09:10 +0000191 shutdown(false);
Adam Tkacb10489b2010-04-23 14:16:04 +0000192 throw AuthFailureException("TLS Handshake failed");
193 }
Adam Tkac0e61c342010-07-21 09:23:25 +0000194
195 checkSession();
Adam Tkacb10489b2010-04-23 14:16:04 +0000196
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000197 cc->setStreams(fis = tlsis, fos = tlsos);
Adam Tkacb10489b2010-04-23 14:16:04 +0000198
199 return true;
200}
201
Adam Tkac3c5be392010-07-21 09:27:34 +0000202void CSecurityTLS::setParam()
Adam Tkac0e61c342010-07-21 09:23:25 +0000203{
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100204 static const char kx_anon_priority[] = "NORMAL:+ANON-ECDH:+ANON-DH";
205 static const char kx_priority[] = "NORMAL";
206
207 int ret;
208 const char *err;
Adam Tkac0e61c342010-07-21 09:23:25 +0000209
210 if (anon) {
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100211 ret = gnutls_priority_set_direct(session, kx_anon_priority, &err);
212 if (ret != GNUTLS_E_SUCCESS) {
213 if (ret == GNUTLS_E_INVALID_REQUEST)
214 vlog.error("GnuTLS priority syntax error at: %s", err);
215 throw AuthFailureException("gnutls_set_priority_direct failed");
216 }
Adam Tkac6948ead2010-08-11 15:58:59 +0000217
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 {
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100226 ret = gnutls_priority_set_direct(session, kx_priority, &err);
227 if (ret != GNUTLS_E_SUCCESS) {
228 if (ret == GNUTLS_E_INVALID_REQUEST)
229 vlog.error("GnuTLS priority syntax error at: %s", err);
230 throw AuthFailureException("gnutls_set_priority_direct failed");
231 }
Adam Tkac6948ead2010-08-11 15:58:59 +0000232
233 if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
234 throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000235
236 if (*cafile && gnutls_certificate_set_x509_trust_file(cert_cred,cafile,GNUTLS_X509_FMT_PEM) < 0)
237 throw AuthFailureException("load of CA cert failed");
238
Adam Tkace32573a2011-02-09 14:13:41 +0000239 /* Load previously saved certs */
240 char *homeDir = NULL;
241 int err;
242 if (getvnchomedir(&homeDir) == -1)
243 vlog.error("Could not obtain VNC home directory path");
244 else {
245 CharArray caSave(strlen(homeDir) + 19 + 1);
246 sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir);
247 delete [] homeDir;
248
249 err = gnutls_certificate_set_x509_trust_file(cert_cred, caSave.buf,
250 GNUTLS_X509_FMT_PEM);
251 if (err < 0)
252 vlog.debug("Failed to load saved server certificates from %s", caSave.buf);
253 }
254
Adam Tkac0e61c342010-07-21 09:23:25 +0000255 if (*crlfile && gnutls_certificate_set_x509_crl_file(cert_cred,crlfile,GNUTLS_X509_FMT_PEM) < 0)
256 throw AuthFailureException("load of CRL failed");
257
Adam Tkac6948ead2010-08-11 15:58:59 +0000258 if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred) != GNUTLS_E_SUCCESS)
259 throw AuthFailureException("gnutls_credentials_set failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000260
261 vlog.debug("X509 session has been set");
262 }
263}
264
Adam Tkac3c5be392010-07-21 09:27:34 +0000265void CSecurityTLS::checkSession()
Adam Tkac0e61c342010-07-21 09:23:25 +0000266{
Adam Tkace32573a2011-02-09 14:13:41 +0000267 const unsigned allowed_errors = GNUTLS_CERT_INVALID |
268 GNUTLS_CERT_SIGNER_NOT_FOUND |
269 GNUTLS_CERT_SIGNER_NOT_CA;
270 unsigned int status;
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100271 const gnutls_datum_t *cert_list;
Adam Tkac0e61c342010-07-21 09:23:25 +0000272 unsigned int cert_list_size = 0;
Adam Tkace32573a2011-02-09 14:13:41 +0000273 int err;
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100274 gnutls_datum_t info;
Adam Tkac0e61c342010-07-21 09:23:25 +0000275
276 if (anon)
277 return;
278
279 if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509)
280 throw AuthFailureException("unsupported certificate type");
281
Adam Tkace32573a2011-02-09 14:13:41 +0000282 err = gnutls_certificate_verify_peers2(session, &status);
283 if (err != 0) {
284 vlog.error("server certificate verification failed: %s", gnutls_strerror(err));
285 throw AuthFailureException("server certificate verification failed");
286 }
287
288 if (status & GNUTLS_CERT_REVOKED)
289 throw AuthFailureException("server certificate has been revoked");
290
Adam Tkac68481c12011-02-09 14:15:09 +0000291#ifndef WITHOUT_X509_TIMES
Adam Tkace32573a2011-02-09 14:13:41 +0000292 if (status & GNUTLS_CERT_NOT_ACTIVATED)
293 throw AuthFailureException("server certificate has not been activated");
294
295 if (status & GNUTLS_CERT_EXPIRED) {
296 vlog.debug("server certificate has expired");
297 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate has expired",
298 "The certificate of the server has expired, "
299 "do you want to continue?"))
300 throw AuthFailureException("server certificate has expired");
301 }
Adam Tkac68481c12011-02-09 14:15:09 +0000302#endif
Adam Tkace32573a2011-02-09 14:13:41 +0000303 /* Process other errors later */
304
Adam Tkac0e61c342010-07-21 09:23:25 +0000305 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
306 if (!cert_list_size)
Adam Tkace32573a2011-02-09 14:13:41 +0000307 throw AuthFailureException("empty certificate chain");
Adam Tkac0e61c342010-07-21 09:23:25 +0000308
Adam Tkace32573a2011-02-09 14:13:41 +0000309 /* Process only server's certificate, not issuer's certificate */
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100310 gnutls_x509_crt_t crt;
Adam Tkace32573a2011-02-09 14:13:41 +0000311 gnutls_x509_crt_init(&crt);
Adam Tkac0e61c342010-07-21 09:23:25 +0000312
Adam Tkace32573a2011-02-09 14:13:41 +0000313 if (gnutls_x509_crt_import(crt, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
314 throw AuthFailureException("decoding of certificate failed");
315
316 if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) {
317 char buf[255];
318 vlog.debug("hostname mismatch");
319 snprintf(buf, sizeof(buf), "Hostname (%s) does not match any certificate, "
320 "do you want to continue?", client->getServerName());
321 buf[sizeof(buf) - 1] = '\0';
322 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "hostname mismatch", buf))
323 throw AuthFailureException("hostname mismatch");
Adam Tkac0e61c342010-07-21 09:23:25 +0000324 }
325
Adam Tkace32573a2011-02-09 14:13:41 +0000326 if (status == 0) {
327 /* Everything is fine (hostname + verification) */
Adam Tkac0e61c342010-07-21 09:23:25 +0000328 gnutls_x509_crt_deinit(crt);
Adam Tkace32573a2011-02-09 14:13:41 +0000329 return;
330 }
331
332 if (status & GNUTLS_CERT_INVALID)
333 vlog.debug("server certificate invalid");
334 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
335 vlog.debug("server cert signer not found");
336 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
337 vlog.debug("server cert signer not CA");
338
339 if ((status & (~allowed_errors)) != 0) {
340 /* No other errors are allowed */
341 vlog.debug("GNUTLS status of certificate verification: %u", status);
342 throw AuthFailureException("Invalid status of server certificate verification");
343 }
344
345 vlog.debug("Saved server certificates don't match");
346
Adam Tkace32573a2011-02-09 14:13:41 +0000347 if (gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_ONELINE, &info)) {
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000348 /*
349 * GNUTLS doesn't correctly export gnutls_free symbol which is
350 * a function pointer. Linking with Visual Studio 2008 Express will
351 * fail when you call gnutls_free().
352 */
353#if WIN32
354 free(info.data);
355#else
Adam Tkac27b2f772010-11-18 13:33:57 +0000356 gnutls_free(info.data);
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000357#endif
Adam Tkace32573a2011-02-09 14:13:41 +0000358 throw AuthFailureException("Could not find certificate to display");
Adam Tkac0e61c342010-07-21 09:23:25 +0000359 }
Adam Tkace32573a2011-02-09 14:13:41 +0000360
Adam Tkac68481c12011-02-09 14:15:09 +0000361 size_t out_size = 0;
Adam Tkace32573a2011-02-09 14:13:41 +0000362 char *out_buf = NULL;
363 char *certinfo = NULL;
364 int len = 0;
365
366 vlog.debug("certificate issuer unknown");
367
368 len = snprintf(NULL, 0, "This certificate has been signed by an unknown "
369 "authority:\n\n%s\n\nDo you want to save it and "
370 "continue?\n ", info.data);
371 if (len < 0)
372 AuthFailureException("certificate decoding error");
373
374 vlog.debug("%s", info.data);
375
376 certinfo = new char[len];
377 if (certinfo == NULL)
378 throw AuthFailureException("Out of memory");
379
380 snprintf(certinfo, len, "This certificate has been signed by an unknown "
381 "authority:\n\n%s\n\nDo you want to save it and "
382 "continue? ", info.data);
383
384 for (int i = 0; i < len - 1; i++)
385 if (certinfo[i] == ',' && certinfo[i + 1] == ' ')
386 certinfo[i] = '\n';
387
388 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate issuer unknown",
389 certinfo)) {
390 delete [] certinfo;
391 throw AuthFailureException("certificate issuer unknown");
392 }
393
394 delete [] certinfo;
395
396 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, NULL, &out_size)
397 == GNUTLS_E_SHORT_MEMORY_BUFFER)
398 AuthFailureException("Out of memory");
399
400 // Save cert
401 out_buf = new char[out_size];
402 if (out_buf == NULL)
403 AuthFailureException("Out of memory");
404
405 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, out_buf, &out_size) < 0)
406 AuthFailureException("certificate issuer unknown, and certificate "
407 "export failed");
408
409 char *homeDir = NULL;
410 if (getvnchomedir(&homeDir) == -1)
411 vlog.error("Could not obtain VNC home directory path");
412 else {
413 FILE *f;
414 CharArray caSave(strlen(homeDir) + 1 + 19);
415 sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir);
416 delete [] homeDir;
417 f = fopen(caSave.buf, "a+");
418 if (!f)
419 msg->showMsgBox(UserMsgBox::M_OK, "certificate save failed",
420 "Could not save the certificate");
421 else {
422 fprintf(f, "%s\n", out_buf);
423 fclose(f);
424 }
425 }
426
427 delete [] out_buf;
428
429 gnutls_x509_crt_deinit(crt);
430 /*
431 * GNUTLS doesn't correctly export gnutls_free symbol which is
432 * a function pointer. Linking with Visual Studio 2008 Express will
433 * fail when you call gnutls_free().
434 */
435#if WIN32
436 free(info.data);
437#else
438 gnutls_free(info.data);
439#endif
Adam Tkac0e61c342010-07-21 09:23:25 +0000440}
441