blob: 000331c068d127a5f3a1e08edc4e37d09cbe6fca [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
Pierre Ossmanad2b3c42018-09-21 15:31:11 +020070CSecurityTLS::CSecurityTLS(CConnection* cc, bool _anon)
71 : CSecurity(cc), session(NULL), anon_cred(NULL), cert_cred(NULL),
Pierre Ossman06c11992018-09-21 15:34:47 +020072 anon(_anon), tlsis(NULL), tlsos(NULL), rawis(NULL), rawos(NULL)
Adam Tkacb10489b2010-04-23 14:16:04 +000073{
Pierre Ossman3d2a84b2014-09-17 16:45:35 +020074 cafile = X509CA.getData();
75 crlfile = X509CRL.getData();
Pierre Ossman8aa4bc52016-08-23 17:02:58 +020076
77 if (gnutls_global_init() != GNUTLS_E_SUCCESS)
78 throw AuthFailureException("gnutls_global_init failed");
Adam Tkacb10489b2010-04-23 14:16:04 +000079}
80
Adam Tkac27b2f772010-11-18 13:33:57 +000081void CSecurityTLS::setDefaults()
82{
83 char* homeDir = NULL;
84
Adam Tkacaf081722011-02-07 10:45:15 +000085 if (getvnchomedir(&homeDir) == -1) {
86 vlog.error("Could not obtain VNC home directory path");
Adam Tkac27b2f772010-11-18 13:33:57 +000087 return;
88 }
89
Adam Tkacaf081722011-02-07 10:45:15 +000090 int len = strlen(homeDir) + 1;
Adam Tkac437b0c22011-02-07 10:46:16 +000091 CharArray caDefault(len + 11);
92 CharArray crlDefault(len + 12);
93 sprintf(caDefault.buf, "%sx509_ca.pem", homeDir);
94 sprintf(crlDefault.buf, "%s509_crl.pem", homeDir);
Adam Tkac27b2f772010-11-18 13:33:57 +000095 delete [] homeDir;
96
Adam Tkacf16a4212011-02-07 10:47:07 +000097 if (!fileexists(caDefault.buf))
Pierre Ossman3d2a84b2014-09-17 16:45:35 +020098 X509CA.setDefaultStr(strdup(caDefault.buf));
Adam Tkacf16a4212011-02-07 10:47:07 +000099 if (!fileexists(crlDefault.buf))
Pierre Ossman3d2a84b2014-09-17 16:45:35 +0200100 X509CRL.setDefaultStr(strdup(crlDefault.buf));
Adam Tkac27b2f772010-11-18 13:33:57 +0000101}
102
Adam Tkac44cdb132011-02-09 14:09:10 +0000103void CSecurityTLS::shutdown(bool needbye)
Adam Tkacb10489b2010-04-23 14:16:04 +0000104{
Adam Tkac44cdb132011-02-09 14:09:10 +0000105 if (session && needbye)
Adam Tkac6948ead2010-08-11 15:58:59 +0000106 if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS)
Adam Tkac44cdb132011-02-09 14:09:10 +0000107 vlog.error("gnutls_bye failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000108
109 if (anon_cred) {
110 gnutls_anon_free_client_credentials(anon_cred);
111 anon_cred = 0;
112 }
113
114 if (cert_cred) {
115 gnutls_certificate_free_credentials(cert_cred);
116 cert_cred = 0;
117 }
118
Pierre Ossman06c11992018-09-21 15:34:47 +0200119 if (rawis && rawos) {
120 cc->setStreams(rawis, rawos);
121 rawis = NULL;
122 rawos = NULL;
123 }
124
Pierre Ossman1b746342018-09-21 15:33:30 +0200125 if (tlsis) {
126 delete tlsis;
127 tlsis = NULL;
128 }
129 if (tlsos) {
130 delete tlsos;
131 tlsos = NULL;
132 }
133
Adam Tkac0e61c342010-07-21 09:23:25 +0000134 if (session) {
135 gnutls_deinit(session);
136 session = 0;
Adam Tkac0e61c342010-07-21 09:23:25 +0000137 }
Adam Tkacb10489b2010-04-23 14:16:04 +0000138}
139
140
Adam Tkac3c5be392010-07-21 09:27:34 +0000141CSecurityTLS::~CSecurityTLS()
Adam Tkacb10489b2010-04-23 14:16:04 +0000142{
Adam Tkac44cdb132011-02-09 14:09:10 +0000143 shutdown(true);
Adam Tkac0e61c342010-07-21 09:23:25 +0000144
Adam Tkac0e61c342010-07-21 09:23:25 +0000145 delete[] cafile;
146 delete[] crlfile;
Pierre Ossman8aa4bc52016-08-23 17:02:58 +0200147
148 gnutls_global_deinit();
Adam Tkacb10489b2010-04-23 14:16:04 +0000149}
150
Pierre Ossmanad2b3c42018-09-21 15:31:11 +0200151bool CSecurityTLS::processMsg()
Adam Tkacb10489b2010-04-23 14:16:04 +0000152{
153 rdr::InStream* is = cc->getInStream();
154 rdr::OutStream* os = cc->getOutStream();
155 client = cc;
156
Adam Tkacb10489b2010-04-23 14:16:04 +0000157 if (!session) {
158 if (!is->checkNoWait(1))
159 return false;
160
Adam Tkacce6c8b02011-05-10 08:54:57 +0000161 if (is->readU8() == 0) {
162 rdr::U32 result = is->readU32();
163 CharArray reason;
164 if (result == secResultFailed || result == secResultTooMany)
165 reason.buf = is->readString();
166 else
Pierre Ossman19225502017-10-12 15:05:07 +0200167 reason.buf = strDup("protocol error");
Adam Tkacce6c8b02011-05-10 08:54:57 +0000168 throw AuthFailureException(reason.buf);
169 }
Adam Tkacb10489b2010-04-23 14:16:04 +0000170
Adam Tkac6948ead2010-08-11 15:58:59 +0000171 if (gnutls_init(&session, GNUTLS_CLIENT) != GNUTLS_E_SUCCESS)
172 throw AuthFailureException("gnutls_init failed");
173
174 if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
175 throw AuthFailureException("gnutls_set_default_priority failed");
Adam Tkacb10489b2010-04-23 14:16:04 +0000176
Adam Tkac0e61c342010-07-21 09:23:25 +0000177 setParam();
Adam Tkacb10489b2010-04-23 14:16:04 +0000178
Pierre Ossman1b746342018-09-21 15:33:30 +0200179 // Create these early as they set up the push/pull functions
180 // for GnuTLS
181 tlsis = new rdr::TLSInStream(is, session);
182 tlsos = new rdr::TLSOutStream(os, session);
Pierre Ossman06c11992018-09-21 15:34:47 +0200183
184 rawis = is;
185 rawos = os;
Pierre Ossman1b746342018-09-21 15:33:30 +0200186 }
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000187
Adam Tkacb10489b2010-04-23 14:16:04 +0000188 int err;
189 err = gnutls_handshake(session);
Adam Tkacb10489b2010-04-23 14:16:04 +0000190 if (err != GNUTLS_E_SUCCESS) {
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000191 if (!gnutls_error_is_fatal(err))
192 return false;
193
Adam Tkacb10489b2010-04-23 14:16:04 +0000194 vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err));
Adam Tkac44cdb132011-02-09 14:09:10 +0000195 shutdown(false);
Adam Tkacb10489b2010-04-23 14:16:04 +0000196 throw AuthFailureException("TLS Handshake failed");
197 }
Adam Tkac0e61c342010-07-21 09:23:25 +0000198
Pierre Ossman83eee752018-10-09 16:54:38 +0200199 vlog.debug("TLS handshake completed with %s",
200 gnutls_session_get_desc(session));
201
Adam Tkac0e61c342010-07-21 09:23:25 +0000202 checkSession();
Adam Tkacb10489b2010-04-23 14:16:04 +0000203
Pierre Ossman1b746342018-09-21 15:33:30 +0200204 cc->setStreams(tlsis, tlsos);
Adam Tkacb10489b2010-04-23 14:16:04 +0000205
206 return true;
207}
208
Adam Tkac3c5be392010-07-21 09:27:34 +0000209void CSecurityTLS::setParam()
Adam Tkac0e61c342010-07-21 09:23:25 +0000210{
Pierre Ossman27eb55e2015-01-29 13:31:06 +0100211 static const char kx_anon_priority[] = ":+ANON-ECDH:+ANON-DH";
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100212
213 int ret;
Pierre Ossman27eb55e2015-01-29 13:31:06 +0100214 char *prio;
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100215 const char *err;
Adam Tkac0e61c342010-07-21 09:23:25 +0000216
Pierre Ossman27eb55e2015-01-29 13:31:06 +0100217 prio = (char*)malloc(strlen(Security::GnuTLSPriority) +
218 strlen(kx_anon_priority) + 1);
219 if (prio == NULL)
220 throw AuthFailureException("Not enough memory for GnuTLS priority string");
Adam Tkac6948ead2010-08-11 15:58:59 +0000221
Pierre Ossman27eb55e2015-01-29 13:31:06 +0100222 strcpy(prio, Security::GnuTLSPriority);
223 if (anon)
224 strcat(prio, kx_anon_priority);
225
226 ret = gnutls_priority_set_direct(session, prio, &err);
227
228 free(prio);
229
230 if (ret != GNUTLS_E_SUCCESS) {
231 if (ret == GNUTLS_E_INVALID_REQUEST)
232 vlog.error("GnuTLS priority syntax error at: %s", err);
233 throw AuthFailureException("gnutls_set_priority_direct failed");
234 }
235
236 if (anon) {
Adam Tkac6948ead2010-08-11 15:58:59 +0000237 if (gnutls_anon_allocate_client_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
238 throw AuthFailureException("gnutls_anon_allocate_client_credentials failed");
239
240 if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred) != GNUTLS_E_SUCCESS)
241 throw AuthFailureException("gnutls_credentials_set failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000242
243 vlog.debug("Anonymous session has been set");
244 } else {
Adam Tkac6948ead2010-08-11 15:58:59 +0000245 if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
246 throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000247
Pierre Ossmanc04f7562018-08-16 13:28:37 +0200248 if (gnutls_certificate_set_x509_system_trust(cert_cred) != GNUTLS_E_SUCCESS)
249 vlog.error("Could not load system certificate trust store");
250
Adam Tkac0e61c342010-07-21 09:23:25 +0000251 if (*cafile && gnutls_certificate_set_x509_trust_file(cert_cred,cafile,GNUTLS_X509_FMT_PEM) < 0)
252 throw AuthFailureException("load of CA cert failed");
253
Adam Tkace32573a2011-02-09 14:13:41 +0000254 /* Load previously saved certs */
255 char *homeDir = NULL;
256 int err;
257 if (getvnchomedir(&homeDir) == -1)
258 vlog.error("Could not obtain VNC home directory path");
259 else {
260 CharArray caSave(strlen(homeDir) + 19 + 1);
261 sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir);
262 delete [] homeDir;
263
264 err = gnutls_certificate_set_x509_trust_file(cert_cred, caSave.buf,
265 GNUTLS_X509_FMT_PEM);
266 if (err < 0)
267 vlog.debug("Failed to load saved server certificates from %s", caSave.buf);
268 }
269
Adam Tkac0e61c342010-07-21 09:23:25 +0000270 if (*crlfile && gnutls_certificate_set_x509_crl_file(cert_cred,crlfile,GNUTLS_X509_FMT_PEM) < 0)
271 throw AuthFailureException("load of CRL failed");
272
Adam Tkac6948ead2010-08-11 15:58:59 +0000273 if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred) != GNUTLS_E_SUCCESS)
274 throw AuthFailureException("gnutls_credentials_set failed");
Adam Tkac0e61c342010-07-21 09:23:25 +0000275
Pierre Ossman894f2c52017-09-08 15:28:39 +0200276 if (gnutls_server_name_set(session, GNUTLS_NAME_DNS,
277 client->getServerName(),
278 strlen(client->getServerName())) != GNUTLS_E_SUCCESS)
279 vlog.error("Failed to configure the server name for TLS handshake");
280
Adam Tkac0e61c342010-07-21 09:23:25 +0000281 vlog.debug("X509 session has been set");
282 }
283}
284
Adam Tkac3c5be392010-07-21 09:27:34 +0000285void CSecurityTLS::checkSession()
Adam Tkac0e61c342010-07-21 09:23:25 +0000286{
Adam Tkace32573a2011-02-09 14:13:41 +0000287 const unsigned allowed_errors = GNUTLS_CERT_INVALID |
288 GNUTLS_CERT_SIGNER_NOT_FOUND |
289 GNUTLS_CERT_SIGNER_NOT_CA;
290 unsigned int status;
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100291 const gnutls_datum_t *cert_list;
Adam Tkac0e61c342010-07-21 09:23:25 +0000292 unsigned int cert_list_size = 0;
Adam Tkace32573a2011-02-09 14:13:41 +0000293 int err;
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100294 gnutls_datum_t info;
Adam Tkac0e61c342010-07-21 09:23:25 +0000295
296 if (anon)
297 return;
298
299 if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509)
300 throw AuthFailureException("unsupported certificate type");
301
Adam Tkace32573a2011-02-09 14:13:41 +0000302 err = gnutls_certificate_verify_peers2(session, &status);
303 if (err != 0) {
304 vlog.error("server certificate verification failed: %s", gnutls_strerror(err));
305 throw AuthFailureException("server certificate verification failed");
306 }
307
308 if (status & GNUTLS_CERT_REVOKED)
309 throw AuthFailureException("server certificate has been revoked");
310
Adam Tkac68481c12011-02-09 14:15:09 +0000311#ifndef WITHOUT_X509_TIMES
Adam Tkace32573a2011-02-09 14:13:41 +0000312 if (status & GNUTLS_CERT_NOT_ACTIVATED)
313 throw AuthFailureException("server certificate has not been activated");
314
315 if (status & GNUTLS_CERT_EXPIRED) {
316 vlog.debug("server certificate has expired");
317 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate has expired",
318 "The certificate of the server has expired, "
319 "do you want to continue?"))
320 throw AuthFailureException("server certificate has expired");
321 }
Adam Tkac68481c12011-02-09 14:15:09 +0000322#endif
Adam Tkace32573a2011-02-09 14:13:41 +0000323 /* Process other errors later */
324
Adam Tkac0e61c342010-07-21 09:23:25 +0000325 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
326 if (!cert_list_size)
Adam Tkace32573a2011-02-09 14:13:41 +0000327 throw AuthFailureException("empty certificate chain");
Adam Tkac0e61c342010-07-21 09:23:25 +0000328
Adam Tkace32573a2011-02-09 14:13:41 +0000329 /* Process only server's certificate, not issuer's certificate */
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100330 gnutls_x509_crt_t crt;
Adam Tkace32573a2011-02-09 14:13:41 +0000331 gnutls_x509_crt_init(&crt);
Adam Tkac0e61c342010-07-21 09:23:25 +0000332
Adam Tkace32573a2011-02-09 14:13:41 +0000333 if (gnutls_x509_crt_import(crt, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
334 throw AuthFailureException("decoding of certificate failed");
335
336 if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) {
337 char buf[255];
338 vlog.debug("hostname mismatch");
339 snprintf(buf, sizeof(buf), "Hostname (%s) does not match any certificate, "
340 "do you want to continue?", client->getServerName());
341 buf[sizeof(buf) - 1] = '\0';
342 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "hostname mismatch", buf))
343 throw AuthFailureException("hostname mismatch");
Adam Tkac0e61c342010-07-21 09:23:25 +0000344 }
345
Adam Tkace32573a2011-02-09 14:13:41 +0000346 if (status == 0) {
347 /* Everything is fine (hostname + verification) */
Adam Tkac0e61c342010-07-21 09:23:25 +0000348 gnutls_x509_crt_deinit(crt);
Adam Tkace32573a2011-02-09 14:13:41 +0000349 return;
350 }
351
352 if (status & GNUTLS_CERT_INVALID)
353 vlog.debug("server certificate invalid");
354 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
355 vlog.debug("server cert signer not found");
356 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
357 vlog.debug("server cert signer not CA");
358
Pierre Ossmane43e5e32017-09-01 11:15:31 +0200359 if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
360 throw AuthFailureException("The server certificate uses an insecure algorithm");
361
Adam Tkace32573a2011-02-09 14:13:41 +0000362 if ((status & (~allowed_errors)) != 0) {
363 /* No other errors are allowed */
364 vlog.debug("GNUTLS status of certificate verification: %u", status);
365 throw AuthFailureException("Invalid status of server certificate verification");
366 }
367
368 vlog.debug("Saved server certificates don't match");
369
Adam Tkace32573a2011-02-09 14:13:41 +0000370 if (gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_ONELINE, &info)) {
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000371 /*
372 * GNUTLS doesn't correctly export gnutls_free symbol which is
373 * a function pointer. Linking with Visual Studio 2008 Express will
374 * fail when you call gnutls_free().
375 */
376#if WIN32
377 free(info.data);
378#else
Adam Tkac27b2f772010-11-18 13:33:57 +0000379 gnutls_free(info.data);
Adam Tkac5d4c6ac2011-01-19 14:06:48 +0000380#endif
Adam Tkace32573a2011-02-09 14:13:41 +0000381 throw AuthFailureException("Could not find certificate to display");
Adam Tkac0e61c342010-07-21 09:23:25 +0000382 }
Adam Tkace32573a2011-02-09 14:13:41 +0000383
Adam Tkac68481c12011-02-09 14:15:09 +0000384 size_t out_size = 0;
Adam Tkace32573a2011-02-09 14:13:41 +0000385 char *out_buf = NULL;
386 char *certinfo = NULL;
387 int len = 0;
388
389 vlog.debug("certificate issuer unknown");
390
391 len = snprintf(NULL, 0, "This certificate has been signed by an unknown "
392 "authority:\n\n%s\n\nDo you want to save it and "
393 "continue?\n ", info.data);
394 if (len < 0)
395 AuthFailureException("certificate decoding error");
396
397 vlog.debug("%s", info.data);
398
399 certinfo = new char[len];
400 if (certinfo == NULL)
401 throw AuthFailureException("Out of memory");
402
403 snprintf(certinfo, len, "This certificate has been signed by an unknown "
404 "authority:\n\n%s\n\nDo you want to save it and "
405 "continue? ", info.data);
406
407 for (int i = 0; i < len - 1; i++)
408 if (certinfo[i] == ',' && certinfo[i + 1] == ' ')
409 certinfo[i] = '\n';
410
411 if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate issuer unknown",
412 certinfo)) {
413 delete [] certinfo;
414 throw AuthFailureException("certificate issuer unknown");
415 }
416
417 delete [] certinfo;
418
419 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, NULL, &out_size)
420 == GNUTLS_E_SHORT_MEMORY_BUFFER)
421 AuthFailureException("Out of memory");
422
423 // Save cert
424 out_buf = new char[out_size];
425 if (out_buf == NULL)
426 AuthFailureException("Out of memory");
427
428 if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, out_buf, &out_size) < 0)
429 AuthFailureException("certificate issuer unknown, and certificate "
430 "export failed");
431
432 char *homeDir = NULL;
433 if (getvnchomedir(&homeDir) == -1)
434 vlog.error("Could not obtain VNC home directory path");
435 else {
436 FILE *f;
437 CharArray caSave(strlen(homeDir) + 1 + 19);
438 sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir);
439 delete [] homeDir;
440 f = fopen(caSave.buf, "a+");
441 if (!f)
442 msg->showMsgBox(UserMsgBox::M_OK, "certificate save failed",
443 "Could not save the certificate");
444 else {
445 fprintf(f, "%s\n", out_buf);
446 fclose(f);
447 }
448 }
449
450 delete [] out_buf;
451
452 gnutls_x509_crt_deinit(crt);
453 /*
454 * GNUTLS doesn't correctly export gnutls_free symbol which is
455 * a function pointer. Linking with Visual Studio 2008 Express will
456 * fail when you call gnutls_free().
457 */
458#if WIN32
459 free(info.data);
460#else
461 gnutls_free(info.data);
462#endif
Adam Tkac0e61c342010-07-21 09:23:25 +0000463}
464