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 | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 45 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 46 | #include <gnutls/x509.h> |
| 47 | |
Adam Tkac | 68481c1 | 2011-02-09 14:15:09 +0000 | [diff] [blame] | 48 | /* |
| 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 |
DRC | b7ab54f | 2011-02-09 03:27:26 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
Adam Tkac | b486423 | 2011-02-09 15:38:37 +0000 | [diff] [blame] | 58 | /* Ancient GNUTLS... */ |
| 59 | #if !defined(GNUTLS_VERSION_NUMBER) && !defined(LIBGNUTLS_VERSION_NUMBER) |
| 60 | #define WITHOUT_X509_TIMES |
| 61 | #endif |
| 62 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 63 | using namespace rfb; |
| 64 | |
Pierre Ossman | 3d2a84b | 2014-09-17 16:45:35 +0200 | [diff] [blame] | 65 | StringParameter CSecurityTLS::X509CA("X509CA", "X509 CA certificate", "", ConfViewer); |
| 66 | StringParameter CSecurityTLS::X509CRL("X509CRL", "X509 CRL file", "", ConfViewer); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 67 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 68 | static LogWriter vlog("TLS"); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 69 | |
Pierre Ossman | ad2b3c4 | 2018-09-21 15:31:11 +0200 | [diff] [blame] | 70 | CSecurityTLS::CSecurityTLS(CConnection* cc, bool _anon) |
| 71 | : CSecurity(cc), session(NULL), anon_cred(NULL), cert_cred(NULL), |
Pierre Ossman | 06c1199 | 2018-09-21 15:34:47 +0200 | [diff] [blame] | 72 | anon(_anon), tlsis(NULL), tlsos(NULL), rawis(NULL), rawos(NULL) |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 73 | { |
Pierre Ossman | 3d2a84b | 2014-09-17 16:45:35 +0200 | [diff] [blame] | 74 | cafile = X509CA.getData(); |
| 75 | crlfile = X509CRL.getData(); |
Pierre Ossman | 8aa4bc5 | 2016-08-23 17:02:58 +0200 | [diff] [blame] | 76 | |
| 77 | if (gnutls_global_init() != GNUTLS_E_SUCCESS) |
| 78 | throw AuthFailureException("gnutls_global_init failed"); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 81 | void CSecurityTLS::setDefaults() |
| 82 | { |
| 83 | char* homeDir = NULL; |
| 84 | |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 85 | if (getvnchomedir(&homeDir) == -1) { |
| 86 | vlog.error("Could not obtain VNC home directory path"); |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 90 | int len = strlen(homeDir) + 1; |
Adam Tkac | 437b0c2 | 2011-02-07 10:46:16 +0000 | [diff] [blame] | 91 | 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 Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 95 | delete [] homeDir; |
| 96 | |
Adam Tkac | f16a421 | 2011-02-07 10:47:07 +0000 | [diff] [blame] | 97 | if (!fileexists(caDefault.buf)) |
Jan Grulich | 8105be9 | 2018-09-26 12:50:33 +0200 | [diff] [blame^] | 98 | X509CA.setDefaultStr(caDefault.buf); |
Adam Tkac | f16a421 | 2011-02-07 10:47:07 +0000 | [diff] [blame] | 99 | if (!fileexists(crlDefault.buf)) |
Jan Grulich | 8105be9 | 2018-09-26 12:50:33 +0200 | [diff] [blame^] | 100 | X509CRL.setDefaultStr(crlDefault.buf); |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Adam Tkac | 44cdb13 | 2011-02-09 14:09:10 +0000 | [diff] [blame] | 103 | void CSecurityTLS::shutdown(bool needbye) |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 104 | { |
Adam Tkac | 44cdb13 | 2011-02-09 14:09:10 +0000 | [diff] [blame] | 105 | if (session && needbye) |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 106 | if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS) |
Adam Tkac | 44cdb13 | 2011-02-09 14:09:10 +0000 | [diff] [blame] | 107 | vlog.error("gnutls_bye failed"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 108 | |
| 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 Ossman | 06c1199 | 2018-09-21 15:34:47 +0200 | [diff] [blame] | 119 | if (rawis && rawos) { |
| 120 | cc->setStreams(rawis, rawos); |
| 121 | rawis = NULL; |
| 122 | rawos = NULL; |
| 123 | } |
| 124 | |
Pierre Ossman | 1b74634 | 2018-09-21 15:33:30 +0200 | [diff] [blame] | 125 | if (tlsis) { |
| 126 | delete tlsis; |
| 127 | tlsis = NULL; |
| 128 | } |
| 129 | if (tlsos) { |
| 130 | delete tlsos; |
| 131 | tlsos = NULL; |
| 132 | } |
| 133 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 134 | if (session) { |
| 135 | gnutls_deinit(session); |
| 136 | session = 0; |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 137 | } |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 141 | CSecurityTLS::~CSecurityTLS() |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 142 | { |
Adam Tkac | 44cdb13 | 2011-02-09 14:09:10 +0000 | [diff] [blame] | 143 | shutdown(true); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 144 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 145 | delete[] cafile; |
| 146 | delete[] crlfile; |
Pierre Ossman | 8aa4bc5 | 2016-08-23 17:02:58 +0200 | [diff] [blame] | 147 | |
| 148 | gnutls_global_deinit(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Pierre Ossman | ad2b3c4 | 2018-09-21 15:31:11 +0200 | [diff] [blame] | 151 | bool CSecurityTLS::processMsg() |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 152 | { |
| 153 | rdr::InStream* is = cc->getInStream(); |
| 154 | rdr::OutStream* os = cc->getOutStream(); |
| 155 | client = cc; |
| 156 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 157 | if (!session) { |
| 158 | if (!is->checkNoWait(1)) |
| 159 | return false; |
| 160 | |
Adam Tkac | ce6c8b0 | 2011-05-10 08:54:57 +0000 | [diff] [blame] | 161 | 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 Ossman | 1922550 | 2017-10-12 15:05:07 +0200 | [diff] [blame] | 167 | reason.buf = strDup("protocol error"); |
Adam Tkac | ce6c8b0 | 2011-05-10 08:54:57 +0000 | [diff] [blame] | 168 | throw AuthFailureException(reason.buf); |
| 169 | } |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 170 | |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 171 | 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 Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 176 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 177 | setParam(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 178 | |
Pierre Ossman | 1b74634 | 2018-09-21 15:33:30 +0200 | [diff] [blame] | 179 | // 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 Ossman | 06c1199 | 2018-09-21 15:34:47 +0200 | [diff] [blame] | 183 | |
| 184 | rawis = is; |
| 185 | rawos = os; |
Pierre Ossman | 1b74634 | 2018-09-21 15:33:30 +0200 | [diff] [blame] | 186 | } |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 187 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 188 | int err; |
| 189 | err = gnutls_handshake(session); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 190 | if (err != GNUTLS_E_SUCCESS) { |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 191 | if (!gnutls_error_is_fatal(err)) |
| 192 | return false; |
| 193 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 194 | vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err)); |
Adam Tkac | 44cdb13 | 2011-02-09 14:09:10 +0000 | [diff] [blame] | 195 | shutdown(false); |
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 | |
Pierre Ossman | 1b74634 | 2018-09-21 15:33:30 +0200 | [diff] [blame] | 201 | cc->setStreams(tlsis, tlsos); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 206 | void CSecurityTLS::setParam() |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 207 | { |
Pierre Ossman | 27eb55e | 2015-01-29 13:31:06 +0100 | [diff] [blame] | 208 | static const char kx_anon_priority[] = ":+ANON-ECDH:+ANON-DH"; |
Pierre Ossman | 88c24ed | 2015-01-29 13:12:22 +0100 | [diff] [blame] | 209 | |
| 210 | int ret; |
Pierre Ossman | 27eb55e | 2015-01-29 13:31:06 +0100 | [diff] [blame] | 211 | char *prio; |
Pierre Ossman | 88c24ed | 2015-01-29 13:12:22 +0100 | [diff] [blame] | 212 | const char *err; |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 213 | |
Pierre Ossman | 27eb55e | 2015-01-29 13:31:06 +0100 | [diff] [blame] | 214 | prio = (char*)malloc(strlen(Security::GnuTLSPriority) + |
| 215 | strlen(kx_anon_priority) + 1); |
| 216 | if (prio == NULL) |
| 217 | throw AuthFailureException("Not enough memory for GnuTLS priority string"); |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 218 | |
Pierre Ossman | 27eb55e | 2015-01-29 13:31:06 +0100 | [diff] [blame] | 219 | strcpy(prio, Security::GnuTLSPriority); |
| 220 | if (anon) |
| 221 | strcat(prio, kx_anon_priority); |
| 222 | |
| 223 | ret = gnutls_priority_set_direct(session, prio, &err); |
| 224 | |
| 225 | free(prio); |
| 226 | |
| 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 | } |
| 232 | |
| 233 | if (anon) { |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 234 | if (gnutls_anon_allocate_client_credentials(&anon_cred) != GNUTLS_E_SUCCESS) |
| 235 | throw AuthFailureException("gnutls_anon_allocate_client_credentials failed"); |
| 236 | |
| 237 | if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_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("Anonymous session has been set"); |
| 241 | } else { |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 242 | if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS) |
| 243 | throw AuthFailureException("gnutls_certificate_allocate_credentials failed"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 244 | |
Pierre Ossman | c04f756 | 2018-08-16 13:28:37 +0200 | [diff] [blame] | 245 | if (gnutls_certificate_set_x509_system_trust(cert_cred) != GNUTLS_E_SUCCESS) |
| 246 | vlog.error("Could not load system certificate trust store"); |
| 247 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 248 | if (*cafile && gnutls_certificate_set_x509_trust_file(cert_cred,cafile,GNUTLS_X509_FMT_PEM) < 0) |
| 249 | throw AuthFailureException("load of CA cert failed"); |
| 250 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 251 | /* Load previously saved certs */ |
| 252 | char *homeDir = NULL; |
| 253 | int err; |
| 254 | if (getvnchomedir(&homeDir) == -1) |
| 255 | vlog.error("Could not obtain VNC home directory path"); |
| 256 | else { |
| 257 | CharArray caSave(strlen(homeDir) + 19 + 1); |
| 258 | sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir); |
| 259 | delete [] homeDir; |
| 260 | |
| 261 | err = gnutls_certificate_set_x509_trust_file(cert_cred, caSave.buf, |
| 262 | GNUTLS_X509_FMT_PEM); |
| 263 | if (err < 0) |
| 264 | vlog.debug("Failed to load saved server certificates from %s", caSave.buf); |
| 265 | } |
| 266 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 267 | if (*crlfile && gnutls_certificate_set_x509_crl_file(cert_cred,crlfile,GNUTLS_X509_FMT_PEM) < 0) |
| 268 | throw AuthFailureException("load of CRL failed"); |
| 269 | |
Adam Tkac | 6948ead | 2010-08-11 15:58:59 +0000 | [diff] [blame] | 270 | if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred) != GNUTLS_E_SUCCESS) |
| 271 | throw AuthFailureException("gnutls_credentials_set failed"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 272 | |
Pierre Ossman | 894f2c5 | 2017-09-08 15:28:39 +0200 | [diff] [blame] | 273 | if (gnutls_server_name_set(session, GNUTLS_NAME_DNS, |
| 274 | client->getServerName(), |
| 275 | strlen(client->getServerName())) != GNUTLS_E_SUCCESS) |
| 276 | vlog.error("Failed to configure the server name for TLS handshake"); |
| 277 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 278 | vlog.debug("X509 session has been set"); |
| 279 | } |
| 280 | } |
| 281 | |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 282 | void CSecurityTLS::checkSession() |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 283 | { |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 284 | const unsigned allowed_errors = GNUTLS_CERT_INVALID | |
| 285 | GNUTLS_CERT_SIGNER_NOT_FOUND | |
| 286 | GNUTLS_CERT_SIGNER_NOT_CA; |
| 287 | unsigned int status; |
Pierre Ossman | 88c24ed | 2015-01-29 13:12:22 +0100 | [diff] [blame] | 288 | const gnutls_datum_t *cert_list; |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 289 | unsigned int cert_list_size = 0; |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 290 | int err; |
Pierre Ossman | 88c24ed | 2015-01-29 13:12:22 +0100 | [diff] [blame] | 291 | gnutls_datum_t info; |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 292 | |
| 293 | if (anon) |
| 294 | return; |
| 295 | |
| 296 | if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) |
| 297 | throw AuthFailureException("unsupported certificate type"); |
| 298 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 299 | err = gnutls_certificate_verify_peers2(session, &status); |
| 300 | if (err != 0) { |
| 301 | vlog.error("server certificate verification failed: %s", gnutls_strerror(err)); |
| 302 | throw AuthFailureException("server certificate verification failed"); |
| 303 | } |
| 304 | |
| 305 | if (status & GNUTLS_CERT_REVOKED) |
| 306 | throw AuthFailureException("server certificate has been revoked"); |
| 307 | |
Adam Tkac | 68481c1 | 2011-02-09 14:15:09 +0000 | [diff] [blame] | 308 | #ifndef WITHOUT_X509_TIMES |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 309 | if (status & GNUTLS_CERT_NOT_ACTIVATED) |
| 310 | throw AuthFailureException("server certificate has not been activated"); |
| 311 | |
| 312 | if (status & GNUTLS_CERT_EXPIRED) { |
| 313 | vlog.debug("server certificate has expired"); |
| 314 | if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate has expired", |
| 315 | "The certificate of the server has expired, " |
| 316 | "do you want to continue?")) |
| 317 | throw AuthFailureException("server certificate has expired"); |
| 318 | } |
Adam Tkac | 68481c1 | 2011-02-09 14:15:09 +0000 | [diff] [blame] | 319 | #endif |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 320 | /* Process other errors later */ |
| 321 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 322 | cert_list = gnutls_certificate_get_peers(session, &cert_list_size); |
| 323 | if (!cert_list_size) |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 324 | throw AuthFailureException("empty certificate chain"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 325 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 326 | /* Process only server's certificate, not issuer's certificate */ |
Pierre Ossman | 88c24ed | 2015-01-29 13:12:22 +0100 | [diff] [blame] | 327 | gnutls_x509_crt_t crt; |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 328 | gnutls_x509_crt_init(&crt); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 329 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 330 | if (gnutls_x509_crt_import(crt, &cert_list[0], GNUTLS_X509_FMT_DER) < 0) |
| 331 | throw AuthFailureException("decoding of certificate failed"); |
| 332 | |
| 333 | if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) { |
| 334 | char buf[255]; |
| 335 | vlog.debug("hostname mismatch"); |
| 336 | snprintf(buf, sizeof(buf), "Hostname (%s) does not match any certificate, " |
| 337 | "do you want to continue?", client->getServerName()); |
| 338 | buf[sizeof(buf) - 1] = '\0'; |
| 339 | if (!msg->showMsgBox(UserMsgBox::M_YESNO, "hostname mismatch", buf)) |
| 340 | throw AuthFailureException("hostname mismatch"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 343 | if (status == 0) { |
| 344 | /* Everything is fine (hostname + verification) */ |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 345 | gnutls_x509_crt_deinit(crt); |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 346 | return; |
| 347 | } |
| 348 | |
| 349 | if (status & GNUTLS_CERT_INVALID) |
| 350 | vlog.debug("server certificate invalid"); |
| 351 | if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) |
| 352 | vlog.debug("server cert signer not found"); |
| 353 | if (status & GNUTLS_CERT_SIGNER_NOT_CA) |
| 354 | vlog.debug("server cert signer not CA"); |
| 355 | |
Pierre Ossman | e43e5e3 | 2017-09-01 11:15:31 +0200 | [diff] [blame] | 356 | if (status & GNUTLS_CERT_INSECURE_ALGORITHM) |
| 357 | throw AuthFailureException("The server certificate uses an insecure algorithm"); |
| 358 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 359 | if ((status & (~allowed_errors)) != 0) { |
| 360 | /* No other errors are allowed */ |
| 361 | vlog.debug("GNUTLS status of certificate verification: %u", status); |
| 362 | throw AuthFailureException("Invalid status of server certificate verification"); |
| 363 | } |
| 364 | |
| 365 | vlog.debug("Saved server certificates don't match"); |
| 366 | |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 367 | if (gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_ONELINE, &info)) { |
Adam Tkac | 5d4c6ac | 2011-01-19 14:06:48 +0000 | [diff] [blame] | 368 | /* |
| 369 | * GNUTLS doesn't correctly export gnutls_free symbol which is |
| 370 | * a function pointer. Linking with Visual Studio 2008 Express will |
| 371 | * fail when you call gnutls_free(). |
| 372 | */ |
| 373 | #if WIN32 |
| 374 | free(info.data); |
| 375 | #else |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 376 | gnutls_free(info.data); |
Adam Tkac | 5d4c6ac | 2011-01-19 14:06:48 +0000 | [diff] [blame] | 377 | #endif |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 378 | throw AuthFailureException("Could not find certificate to display"); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 379 | } |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 380 | |
Adam Tkac | 68481c1 | 2011-02-09 14:15:09 +0000 | [diff] [blame] | 381 | size_t out_size = 0; |
Adam Tkac | e32573a | 2011-02-09 14:13:41 +0000 | [diff] [blame] | 382 | char *out_buf = NULL; |
| 383 | char *certinfo = NULL; |
| 384 | int len = 0; |
| 385 | |
| 386 | vlog.debug("certificate issuer unknown"); |
| 387 | |
| 388 | len = snprintf(NULL, 0, "This certificate has been signed by an unknown " |
| 389 | "authority:\n\n%s\n\nDo you want to save it and " |
| 390 | "continue?\n ", info.data); |
| 391 | if (len < 0) |
| 392 | AuthFailureException("certificate decoding error"); |
| 393 | |
| 394 | vlog.debug("%s", info.data); |
| 395 | |
| 396 | certinfo = new char[len]; |
| 397 | if (certinfo == NULL) |
| 398 | throw AuthFailureException("Out of memory"); |
| 399 | |
| 400 | snprintf(certinfo, len, "This certificate has been signed by an unknown " |
| 401 | "authority:\n\n%s\n\nDo you want to save it and " |
| 402 | "continue? ", info.data); |
| 403 | |
| 404 | for (int i = 0; i < len - 1; i++) |
| 405 | if (certinfo[i] == ',' && certinfo[i + 1] == ' ') |
| 406 | certinfo[i] = '\n'; |
| 407 | |
| 408 | if (!msg->showMsgBox(UserMsgBox::M_YESNO, "certificate issuer unknown", |
| 409 | certinfo)) { |
| 410 | delete [] certinfo; |
| 411 | throw AuthFailureException("certificate issuer unknown"); |
| 412 | } |
| 413 | |
| 414 | delete [] certinfo; |
| 415 | |
| 416 | if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, NULL, &out_size) |
| 417 | == GNUTLS_E_SHORT_MEMORY_BUFFER) |
| 418 | AuthFailureException("Out of memory"); |
| 419 | |
| 420 | // Save cert |
| 421 | out_buf = new char[out_size]; |
| 422 | if (out_buf == NULL) |
| 423 | AuthFailureException("Out of memory"); |
| 424 | |
| 425 | if (gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, out_buf, &out_size) < 0) |
| 426 | AuthFailureException("certificate issuer unknown, and certificate " |
| 427 | "export failed"); |
| 428 | |
| 429 | char *homeDir = NULL; |
| 430 | if (getvnchomedir(&homeDir) == -1) |
| 431 | vlog.error("Could not obtain VNC home directory path"); |
| 432 | else { |
| 433 | FILE *f; |
| 434 | CharArray caSave(strlen(homeDir) + 1 + 19); |
| 435 | sprintf(caSave.buf, "%sx509_savedcerts.pem", homeDir); |
| 436 | delete [] homeDir; |
| 437 | f = fopen(caSave.buf, "a+"); |
| 438 | if (!f) |
| 439 | msg->showMsgBox(UserMsgBox::M_OK, "certificate save failed", |
| 440 | "Could not save the certificate"); |
| 441 | else { |
| 442 | fprintf(f, "%s\n", out_buf); |
| 443 | fclose(f); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | delete [] out_buf; |
| 448 | |
| 449 | gnutls_x509_crt_deinit(crt); |
| 450 | /* |
| 451 | * GNUTLS doesn't correctly export gnutls_free symbol which is |
| 452 | * a function pointer. Linking with Visual Studio 2008 Express will |
| 453 | * fail when you call gnutls_free(). |
| 454 | */ |
| 455 | #if WIN32 |
| 456 | free(info.data); |
| 457 | #else |
| 458 | gnutls_free(info.data); |
| 459 | #endif |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame] | 460 | } |
| 461 | |