DRC | ff1e1ff | 2011-02-08 23:43:55 +0000 | [diff] [blame] | 1 | /* |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 2 | * Copyright (C) 2004 Red Hat Inc. |
| 3 | * Copyright (C) 2005 Martin Koegler |
| 4 | * Copyright (C) 2010 TigerVNC Team |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 5 | * Copyright (C) 2010 m-privacy GmbH |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 6 | * |
| 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 Tkac | 4395823 | 2010-07-21 09:06:59 +0000 | [diff] [blame] | 27 | #ifndef HAVE_GNUTLS |
| 28 | #error "This header should not be compiled without HAVE_GNUTLS defined" |
| 29 | #endif |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 30 | |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
Adam Tkac | c4674db | 2011-01-19 14:11:16 +0000 | [diff] [blame] | 32 | #ifndef WIN32 |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
Adam Tkac | c4674db | 2011-01-19 14:11:16 +0000 | [diff] [blame] | 34 | #endif |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 35 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 36 | #include <rfb/CSecurityTLS.h> |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 37 | #include <rfb/SSecurityVeNCrypt.h> |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 38 | #include <rfb/CConnection.h> |
| 39 | #include <rfb/LogWriter.h> |
| 40 | #include <rfb/Exception.h> |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 41 | #include <rfb/UserMsgBox.h> |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 42 | #include <rdr/TLSInStream.h> |
| 43 | #include <rdr/TLSOutStream.h> |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 44 | #include <os/os.h> |
Adam Tkac | 179d2b1 | 2011-01-19 14:15:14 +0000 | [diff] [blame] | 45 | #include <os/print.h> |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 46 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 47 | #include <gnutls/x509.h> |
| 48 | |
DRC | b7ab54f | 2011-02-09 03:27:26 +0000 | [diff] [blame^] | 49 | #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 Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 59 | #define TLS_DEBUG |
| 60 | |
| 61 | using namespace rfb; |
| 62 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 63 | StringParameter CSecurityTLS::x509ca("x509ca", "X509 CA certificate", "", ConfViewer); |
| 64 | StringParameter CSecurityTLS::x509crl("x509crl", "X509 CRL file", "", ConfViewer); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 65 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 66 | static LogWriter vlog("TLS"); |
| 67 | |
| 68 | #ifdef TLS_DEBUG |
| 69 | static void debug_log(int level, const char* str) |
| 70 | { |
| 71 | vlog.debug(str); |
| 72 | } |
| 73 | #endif |
| 74 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 75 | void CSecurityTLS::initGlobal() |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 76 | { |
| 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 Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 91 | CSecurityTLS::CSecurityTLS(bool _anon) : session(0), anon_cred(0), |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 92 | anon(_anon), fis(0), fos(0) |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 93 | { |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 94 | cafile = x509ca.getData(); |
| 95 | crlfile = x509crl.getData(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 98 | void CSecurityTLS::setDefaults() |
| 99 | { |
| 100 | char* homeDir = NULL; |
| 101 | |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 102 | if (getvnchomedir(&homeDir) == -1) { |
| 103 | vlog.error("Could not obtain VNC home directory path"); |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 107 | int len = strlen(homeDir) + 1; |
Adam Tkac | 437b0c2 | 2011-02-07 10:46:16 +0000 | [diff] [blame] | 108 | 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 Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 112 | delete [] homeDir; |
| 113 | |
Adam Tkac | f16a421 | 2011-02-07 10:47:07 +0000 | [diff] [blame] | 114 | if (!fileexists(caDefault.buf)) |
| 115 | x509ca.setDefaultStr(strdup(caDefault.buf)); |
| 116 | if (!fileexists(crlDefault.buf)) |
| 117 | x509crl.setDefaultStr(strdup(crlDefault.buf)); |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 120 | void CSecurityTLS::shutdown() |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 121 | { |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 122 | if (session) |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 123 | if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS) |
| 124 | throw Exception("gnutls_bye failed"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 125 | |
| 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 Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 145 | CSecurityTLS::~CSecurityTLS() |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 146 | { |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 147 | shutdown(); |
| 148 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 149 | if (fis) |
| 150 | delete fis; |
| 151 | if (fos) |
| 152 | delete fos; |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 153 | |
| 154 | delete[] cafile; |
| 155 | delete[] crlfile; |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 158 | bool CSecurityTLS::processMsg(CConnection* cc) |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 159 | { |
| 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 Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 173 | 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 Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 178 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 179 | setParam(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 180 | |
| 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 Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 195 | shutdown(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 196 | throw AuthFailureException("TLS Handshake failed"); |
| 197 | } |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 198 | |
| 199 | checkSession(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 200 | |
| 201 | cc->setStreams(fis = new rdr::TLSInStream(is, session), |
| 202 | fos = new rdr::TLSOutStream(os, session)); |
| 203 | |
| 204 | return true; |
| 205 | } |
| 206 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 207 | void CSecurityTLS::setParam() |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 208 | { |
| 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 Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 214 | 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 Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 222 | |
| 223 | vlog.debug("Anonymous session has been set"); |
| 224 | } else { |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 225 | 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 Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 230 | |
| 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 Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 237 | if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred) != GNUTLS_E_SUCCESS) |
| 238 | throw AuthFailureException("gnutls_credentials_set failed"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 239 | |
| 240 | vlog.debug("X509 session has been set"); |
| 241 | } |
| 242 | } |
| 243 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 244 | void CSecurityTLS::checkSession() |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 245 | { |
| 246 | int status; |
| 247 | const gnutls_datum *cert_list; |
| 248 | unsigned int cert_list_size = 0; |
| 249 | unsigned int i; |
DRC | ff1e1ff | 2011-02-08 23:43:55 +0000 | [diff] [blame] | 250 | gnutls_datum info; |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 251 | |
| 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 Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 271 | if (status & GNUTLS_CERT_REVOKED) { |
| 272 | throw AuthFailureException("certificate has been revoked"); |
| 273 | } |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 274 | |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 275 | if (status & GNUTLS_CERT_NOT_ACTIVATED) { |
| 276 | throw AuthFailureException("certificate has not been activated"); |
| 277 | } |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 278 | |
| 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 Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 284 | throw AuthFailureException("decoding of certificate failed"); |
| 285 | |
DRC | ff1e1ff | 2011-02-08 23:43:55 +0000 | [diff] [blame] | 286 | #if defined(GNUTLS_VERSION_NUMBER) && (GNUTLS_VERSION_NUMBER >= 0x010706) |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 287 | if (gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_ONELINE, &info)) { |
Adam Tkac | 5d4c6ac | 2011-01-19 14:06:48 +0000 | [diff] [blame] | 288 | /* |
| 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 Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 296 | gnutls_free(info.data); |
Adam Tkac | 5d4c6ac | 2011-01-19 14:06:48 +0000 | [diff] [blame] | 297 | #endif |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 298 | throw AuthFailureException("Could not find certificate to display"); |
| 299 | } |
DRC | ff1e1ff | 2011-02-08 23:43:55 +0000 | [diff] [blame] | 300 | #endif |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 301 | |
| 302 | if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) { |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 303 | 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 Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 308 | } |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 309 | |
| 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 Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 361 | |
| 362 | if (getvnchomedir(&homeDir) == -1) |
| 363 | vlog.error("Could not obtain VNC home directory path"); |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 364 | else { |
| 365 | FILE *f; |
Adam Tkac | 437b0c2 | 2011-02-07 10:46:16 +0000 | [diff] [blame] | 366 | CharArray caSave(strlen(homeDir) + 1 + 19); |
| 367 | sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir); |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 368 | 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 Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 382 | gnutls_x509_crt_deinit(crt); |
Adam Tkac | 5d4c6ac | 2011-01-19 14:06:48 +0000 | [diff] [blame] | 383 | /* |
| 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 Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 391 | gnutls_free(info.data); |
Adam Tkac | 5d4c6ac | 2011-01-19 14:06:48 +0000 | [diff] [blame] | 392 | #endif |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |