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 | 68481c1 | 2011-02-09 14:15:09 +0000 | [diff] [blame] | 46 | #include <os/tls.h> |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 47 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 48 | #include <gnutls/x509.h> |
| 49 | |
Adam Tkac | 68481c1 | 2011-02-09 14:15:09 +0000 | [diff] [blame] | 50 | /* |
| 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 |
DRC | b7ab54f | 2011-02-09 03:27:26 +0000 | [diff] [blame] | 58 | #endif |
| 59 | |
Adam Tkac | b486423 | 2011-02-09 15:38:37 +0000 | [diff] [blame] | 60 | /* Ancient GNUTLS... */ |
| 61 | #if !defined(GNUTLS_VERSION_NUMBER) && !defined(LIBGNUTLS_VERSION_NUMBER) |
| 62 | #define WITHOUT_X509_TIMES |
| 63 | #endif |
| 64 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 65 | using namespace rfb; |
| 66 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 67 | StringParameter CSecurityTLS::x509ca("x509ca", "X509 CA certificate", "", ConfViewer); |
| 68 | StringParameter CSecurityTLS::x509crl("x509crl", "X509 CRL file", "", ConfViewer); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 69 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 70 | static LogWriter vlog("TLS"); |
Adam Tkac | 348269d | 2011-02-18 10:54:11 +0000 | [diff] [blame] | 71 | static LogWriter vlog_raw("RawTLS"); |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 72 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 73 | static void debug_log(int level, const char* str) |
| 74 | { |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 75 | vlog_raw.debug(str); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 76 | } |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 77 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 78 | void CSecurityTLS::initGlobal() |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 79 | { |
| 80 | static bool globalInitDone = false; |
| 81 | |
| 82 | if (!globalInitDone) { |
| 83 | gnutls_global_init(); |
| 84 | |
Adam Tkac | 348269d | 2011-02-18 10:54:11 +0000 | [diff] [blame] | 85 | /* 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 Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 90 | |
| 91 | globalInitDone = true; |
| 92 | } |
| 93 | } |
| 94 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 95 | CSecurityTLS::CSecurityTLS(bool _anon) : session(0), anon_cred(0), |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 96 | anon(_anon), fis(0), fos(0) |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 97 | { |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 98 | cafile = x509ca.getData(); |
| 99 | crlfile = x509crl.getData(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 102 | void CSecurityTLS::setDefaults() |
| 103 | { |
| 104 | char* homeDir = NULL; |
| 105 | |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 106 | if (getvnchomedir(&homeDir) == -1) { |
| 107 | vlog.error("Could not obtain VNC home directory path"); |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 111 | int len = strlen(homeDir) + 1; |
Adam Tkac | 437b0c2 | 2011-02-07 10:46:16 +0000 | [diff] [blame] | 112 | 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 Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 116 | delete [] homeDir; |
| 117 | |
Adam Tkac | f16a421 | 2011-02-07 10:47:07 +0000 | [diff] [blame] | 118 | if (!fileexists(caDefault.buf)) |
| 119 | x509ca.setDefaultStr(strdup(caDefault.buf)); |
| 120 | if (!fileexists(crlDefault.buf)) |
| 121 | x509crl.setDefaultStr(strdup(crlDefault.buf)); |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Adam Tkac | 44cdb13 | 2011-02-09 14:09:10 +0000 | [diff] [blame] | 124 | void CSecurityTLS::shutdown(bool needbye) |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 125 | { |
Adam Tkac | 44cdb13 | 2011-02-09 14:09:10 +0000 | [diff] [blame] | 126 | if (session && needbye) |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 127 | if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS) |
Adam Tkac | 44cdb13 | 2011-02-09 14:09:10 +0000 | [diff] [blame] | 128 | vlog.error("gnutls_bye failed"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 129 | |
| 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 Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 149 | CSecurityTLS::~CSecurityTLS() |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 150 | { |
Adam Tkac | 44cdb13 | 2011-02-09 14:09:10 +0000 | [diff] [blame] | 151 | shutdown(true); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 152 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 153 | if (fis) |
| 154 | delete fis; |
| 155 | if (fos) |
| 156 | delete fos; |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 157 | |
| 158 | delete[] cafile; |
| 159 | delete[] crlfile; |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 162 | bool CSecurityTLS::processMsg(CConnection* cc) |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 163 | { |
| 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 | |
| 174 | if (is->readU8() == 0) |
| 175 | return true; |
| 176 | |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 177 | if (gnutls_init(&session, GNUTLS_CLIENT) != GNUTLS_E_SUCCESS) |
| 178 | throw AuthFailureException("gnutls_init failed"); |
| 179 | |
| 180 | if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS) |
| 181 | throw AuthFailureException("gnutls_set_default_priority failed"); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 182 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 183 | setParam(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 184 | |
| 185 | gnutls_transport_set_pull_function(session, rdr::gnutls_InStream_pull); |
| 186 | gnutls_transport_set_push_function(session, rdr::gnutls_OutStream_push); |
| 187 | gnutls_transport_set_ptr2(session, |
| 188 | (gnutls_transport_ptr) is, |
| 189 | (gnutls_transport_ptr) os); |
| 190 | } |
| 191 | |
| 192 | int err; |
| 193 | err = gnutls_handshake(session); |
| 194 | if (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(err)) |
| 195 | return false; |
| 196 | |
| 197 | if (err != GNUTLS_E_SUCCESS) { |
| 198 | vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err)); |
Adam Tkac | 44cdb13 | 2011-02-09 14:09:10 +0000 | [diff] [blame] | 199 | shutdown(false); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 200 | throw AuthFailureException("TLS Handshake failed"); |
| 201 | } |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 202 | |
| 203 | checkSession(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 204 | |
| 205 | cc->setStreams(fis = new rdr::TLSInStream(is, session), |
| 206 | fos = new rdr::TLSOutStream(os, session)); |
| 207 | |
| 208 | return true; |
| 209 | } |
| 210 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 211 | void CSecurityTLS::setParam() |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 212 | { |
| 213 | static const int kx_anon_priority[] = { GNUTLS_KX_ANON_DH, 0 }; |
| 214 | static const int kx_priority[] = { GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA, |
| 215 | GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0 }; |
| 216 | |
| 217 | if (anon) { |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 218 | if (gnutls_kx_set_priority(session, kx_anon_priority) != GNUTLS_E_SUCCESS) |
| 219 | throw AuthFailureException("gnutls_kx_set_priority failed"); |
| 220 | |
| 221 | if (gnutls_anon_allocate_client_credentials(&anon_cred) != GNUTLS_E_SUCCESS) |
| 222 | throw AuthFailureException("gnutls_anon_allocate_client_credentials failed"); |
| 223 | |
| 224 | if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred) != GNUTLS_E_SUCCESS) |
| 225 | throw AuthFailureException("gnutls_credentials_set failed"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 226 | |
| 227 | vlog.debug("Anonymous session has been set"); |
| 228 | } else { |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 229 | if (gnutls_kx_set_priority(session, kx_priority) != GNUTLS_E_SUCCESS) |
| 230 | throw AuthFailureException("gnutls_kx_set_priority failed"); |
| 231 | |
| 232 | if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS) |
| 233 | throw AuthFailureException("gnutls_certificate_allocate_credentials failed"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 234 | |
| 235 | if (*cafile && gnutls_certificate_set_x509_trust_file(cert_cred,cafile,GNUTLS_X509_FMT_PEM) < 0) |
| 236 | throw AuthFailureException("load of CA cert failed"); |
| 237 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 238 | /* Load previously saved certs */ |
| 239 | char *homeDir = NULL; |
| 240 | int err; |
| 241 | if (getvnchomedir(&homeDir) == -1) |
| 242 | vlog.error("Could not obtain VNC home directory path"); |
| 243 | else { |
| 244 | CharArray caSave(strlen(homeDir) + 19 + 1); |
| 245 | sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir); |
| 246 | delete [] homeDir; |
| 247 | |
| 248 | err = gnutls_certificate_set_x509_trust_file(cert_cred, caSave.buf, |
| 249 | GNUTLS_X509_FMT_PEM); |
| 250 | if (err < 0) |
| 251 | vlog.debug("Failed to load saved server certificates from %s", caSave.buf); |
| 252 | } |
| 253 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 254 | if (*crlfile && gnutls_certificate_set_x509_crl_file(cert_cred,crlfile,GNUTLS_X509_FMT_PEM) < 0) |
| 255 | throw AuthFailureException("load of CRL failed"); |
| 256 | |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 257 | if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred) != GNUTLS_E_SUCCESS) |
| 258 | throw AuthFailureException("gnutls_credentials_set failed"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 259 | |
| 260 | vlog.debug("X509 session has been set"); |
| 261 | } |
| 262 | } |
| 263 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 264 | void CSecurityTLS::checkSession() |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 265 | { |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 266 | const unsigned allowed_errors = GNUTLS_CERT_INVALID | |
| 267 | GNUTLS_CERT_SIGNER_NOT_FOUND | |
| 268 | GNUTLS_CERT_SIGNER_NOT_CA; |
| 269 | unsigned int status; |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 270 | const gnutls_datum *cert_list; |
| 271 | unsigned int cert_list_size = 0; |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 272 | int err; |
DRC | ff1e1ff | 2011-02-08 23:43:55 +0000 | [diff] [blame] | 273 | gnutls_datum info; |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 274 | |
| 275 | if (anon) |
| 276 | return; |
| 277 | |
| 278 | if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) |
| 279 | throw AuthFailureException("unsupported certificate type"); |
| 280 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 281 | err = gnutls_certificate_verify_peers2(session, &status); |
| 282 | if (err != 0) { |
| 283 | vlog.error("server certificate verification failed: %s", gnutls_strerror(err)); |
| 284 | throw AuthFailureException("server certificate verification failed"); |
| 285 | } |
| 286 | |
| 287 | if (status & GNUTLS_CERT_REVOKED) |
| 288 | throw AuthFailureException("server certificate has been revoked"); |
| 289 | |
Adam Tkac | 68481c1 | 2011-02-09 14:15:09 +0000 | [diff] [blame] | 290 | #ifndef WITHOUT_X509_TIMES |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 291 | if (status & GNUTLS_CERT_NOT_ACTIVATED) |
| 292 | throw AuthFailureException("server certificate has not been activated"); |
| 293 | |
| 294 | if (status & GNUTLS_CERT_EXPIRED) { |
| 295 | vlog.debug("server certificate has expired"); |
| 296 | if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate has expired", |
| 297 | "The certificate of the server has expired, " |
| 298 | "do you want to continue?")) |
| 299 | throw AuthFailureException("server certificate has expired"); |
| 300 | } |
Adam Tkac | 68481c1 | 2011-02-09 14:15:09 +0000 | [diff] [blame] | 301 | #endif |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 302 | /* Process other errors later */ |
| 303 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 304 | cert_list = gnutls_certificate_get_peers(session, &cert_list_size); |
| 305 | if (!cert_list_size) |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 306 | throw AuthFailureException("empty certificate chain"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 307 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 308 | /* Process only server's certificate, not issuer's certificate */ |
| 309 | gnutls_x509_crt crt; |
| 310 | gnutls_x509_crt_init(&crt); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 311 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 312 | if (gnutls_x509_crt_import(crt, &cert_list[0], GNUTLS_X509_FMT_DER) < 0) |
| 313 | throw AuthFailureException("decoding of certificate failed"); |
| 314 | |
| 315 | if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) { |
| 316 | char buf[255]; |
| 317 | vlog.debug("hostname mismatch"); |
| 318 | snprintf(buf, sizeof(buf), "Hostname (%s) does not match any certificate, " |
| 319 | "do you want to continue?", client->getServerName()); |
| 320 | buf[sizeof(buf) - 1] = '\0'; |
| 321 | if (!msg->showMsgBox(UserMsgBox::M_YESNO, "hostname mismatch", buf)) |
| 322 | throw AuthFailureException("hostname mismatch"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 325 | if (status == 0) { |
| 326 | /* Everything is fine (hostname + verification) */ |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 327 | gnutls_x509_crt_deinit(crt); |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 328 | return; |
| 329 | } |
| 330 | |
| 331 | if (status & GNUTLS_CERT_INVALID) |
| 332 | vlog.debug("server certificate invalid"); |
| 333 | if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) |
| 334 | vlog.debug("server cert signer not found"); |
| 335 | if (status & GNUTLS_CERT_SIGNER_NOT_CA) |
| 336 | vlog.debug("server cert signer not CA"); |
| 337 | |
| 338 | if ((status & (~allowed_errors)) != 0) { |
| 339 | /* No other errors are allowed */ |
| 340 | vlog.debug("GNUTLS status of certificate verification: %u", status); |
| 341 | throw AuthFailureException("Invalid status of server certificate verification"); |
| 342 | } |
| 343 | |
| 344 | vlog.debug("Saved server certificates don't match"); |
| 345 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 346 | if (gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_ONELINE, &info)) { |
Adam Tkac | 5d4c6ac | 2011-01-19 14:06:48 +0000 | [diff] [blame] | 347 | /* |
| 348 | * GNUTLS doesn't correctly export gnutls_free symbol which is |
| 349 | * a function pointer. Linking with Visual Studio 2008 Express will |
| 350 | * fail when you call gnutls_free(). |
| 351 | */ |
| 352 | #if WIN32 |
| 353 | free(info.data); |
| 354 | #else |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 355 | gnutls_free(info.data); |
Adam Tkac | 5d4c6ac | 2011-01-19 14:06:48 +0000 | [diff] [blame] | 356 | #endif |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 357 | throw AuthFailureException("Could not find certificate to display"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 358 | } |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 359 | |
Adam Tkac | 68481c1 | 2011-02-09 14:15:09 +0000 | [diff] [blame] | 360 | size_t out_size = 0; |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 361 | char *out_buf = NULL; |
| 362 | char *certinfo = NULL; |
| 363 | int len = 0; |
| 364 | |
| 365 | vlog.debug("certificate issuer unknown"); |
| 366 | |
| 367 | len = snprintf(NULL, 0, "This certificate has been signed by an unknown " |
| 368 | "authority:\n\n%s\n\nDo you want to save it and " |
| 369 | "continue?\n ", info.data); |
| 370 | if (len < 0) |
| 371 | AuthFailureException("certificate decoding error"); |
| 372 | |
| 373 | vlog.debug("%s", info.data); |
| 374 | |
| 375 | certinfo = new char[len]; |
| 376 | if (certinfo == NULL) |
| 377 | throw AuthFailureException("Out of memory"); |
| 378 | |
| 379 | snprintf(certinfo, len, "This certificate has been signed by an unknown " |
| 380 | "authority:\n\n%s\n\nDo you want to save it and " |
| 381 | "continue? ", info.data); |
| 382 | |
| 383 | for (int i = 0; i < len - 1; i++) |
| 384 | if (certinfo[i] == ',' && certinfo[i + 1] == ' ') |
| 385 | certinfo[i] = '\n'; |
| 386 | |
| 387 | if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate issuer unknown", |
| 388 | certinfo)) { |
| 389 | delete [] certinfo; |
| 390 | throw AuthFailureException("certificate issuer unknown"); |
| 391 | } |
| 392 | |
| 393 | delete [] certinfo; |
| 394 | |
| 395 | if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, NULL, &out_size) |
| 396 | == GNUTLS_E_SHORT_MEMORY_BUFFER) |
| 397 | AuthFailureException("Out of memory"); |
| 398 | |
| 399 | // Save cert |
| 400 | out_buf = new char[out_size]; |
| 401 | if (out_buf == NULL) |
| 402 | AuthFailureException("Out of memory"); |
| 403 | |
| 404 | if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, out_buf, &out_size) < 0) |
| 405 | AuthFailureException("certificate issuer unknown, and certificate " |
| 406 | "export failed"); |
| 407 | |
| 408 | char *homeDir = NULL; |
| 409 | if (getvnchomedir(&homeDir) == -1) |
| 410 | vlog.error("Could not obtain VNC home directory path"); |
| 411 | else { |
| 412 | FILE *f; |
| 413 | CharArray caSave(strlen(homeDir) + 1 + 19); |
| 414 | sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir); |
| 415 | delete [] homeDir; |
| 416 | f = fopen(caSave.buf, "a+"); |
| 417 | if (!f) |
| 418 | msg->showMsgBox(UserMsgBox::M_OK, "certificate save failed", |
| 419 | "Could not save the certificate"); |
| 420 | else { |
| 421 | fprintf(f, "%s\n", out_buf); |
| 422 | fclose(f); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | delete [] out_buf; |
| 427 | |
| 428 | gnutls_x509_crt_deinit(crt); |
| 429 | /* |
| 430 | * GNUTLS doesn't correctly export gnutls_free symbol which is |
| 431 | * a function pointer. Linking with Visual Studio 2008 Express will |
| 432 | * fail when you call gnutls_free(). |
| 433 | */ |
| 434 | #if WIN32 |
| 435 | free(info.data); |
| 436 | #else |
| 437 | gnutls_free(info.data); |
| 438 | #endif |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 439 | } |
| 440 | |