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