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