Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 Red Hat Inc. |
| 3 | * Copyright (C) 2005 Martin Koegler |
| 4 | * Copyright (C) 2010 TigerVNC Team |
| 5 | * |
| 6 | * This is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This software is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this software; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 19 | * USA. |
| 20 | */ |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include <config.h> |
| 24 | #endif |
| 25 | |
Adam Tkac | 4395823 | 2010-07-21 09:06:59 +0000 | [diff] [blame] | 26 | #ifndef HAVE_GNUTLS |
| 27 | #error "This header should not be compiled without HAVE_GNUTLS defined" |
| 28 | #endif |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 29 | |
| 30 | #include <rfb/CSecurityTLSBase.h> |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 31 | #include <rfb/SSecurityVeNCrypt.h> |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 32 | #include <rfb/CConnection.h> |
| 33 | #include <rfb/LogWriter.h> |
| 34 | #include <rfb/Exception.h> |
| 35 | #include <rdr/TLSInStream.h> |
| 36 | #include <rdr/TLSOutStream.h> |
| 37 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 38 | #include <gnutls/x509.h> |
| 39 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 40 | #define TLS_DEBUG |
| 41 | |
| 42 | using namespace rfb; |
| 43 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 44 | StringParameter CSecurityTLSBase::x509ca("x509ca", "X509 CA certificate", "", ConfViewer); |
| 45 | StringParameter CSecurityTLSBase::x509crl("x509crl", "X509 CRL file", "", ConfViewer); |
| 46 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 47 | static LogWriter vlog("TLS"); |
| 48 | |
| 49 | #ifdef TLS_DEBUG |
| 50 | static void debug_log(int level, const char* str) |
| 51 | { |
| 52 | vlog.debug(str); |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | void CSecurityTLSBase::initGlobal() |
| 57 | { |
| 58 | static bool globalInitDone = false; |
| 59 | |
| 60 | if (!globalInitDone) { |
| 61 | gnutls_global_init(); |
| 62 | |
| 63 | #ifdef TLS_DEBUG |
| 64 | gnutls_global_set_log_level(10); |
| 65 | gnutls_global_set_log_function(debug_log); |
| 66 | #endif |
| 67 | |
| 68 | globalInitDone = true; |
| 69 | } |
| 70 | } |
| 71 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 72 | CSecurityTLSBase::CSecurityTLSBase(bool _anon) : session(0), anon_cred(0), |
| 73 | anon(_anon), fis(0), fos(0) |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 74 | { |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 75 | cafile = x509ca.getData(); |
| 76 | crlfile = x509crl.getData(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void CSecurityTLSBase::shutdown() |
| 80 | { |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 81 | if (session) |
| 82 | gnutls_bye(session, GNUTLS_SHUT_RDWR); |
| 83 | |
| 84 | if (anon_cred) { |
| 85 | gnutls_anon_free_client_credentials(anon_cred); |
| 86 | anon_cred = 0; |
| 87 | } |
| 88 | |
| 89 | if (cert_cred) { |
| 90 | gnutls_certificate_free_credentials(cert_cred); |
| 91 | cert_cred = 0; |
| 92 | } |
| 93 | |
| 94 | if (session) { |
| 95 | gnutls_deinit(session); |
| 96 | session = 0; |
| 97 | |
| 98 | gnutls_global_deinit(); |
| 99 | } |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
| 103 | CSecurityTLSBase::~CSecurityTLSBase() |
| 104 | { |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 105 | shutdown(); |
| 106 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 107 | if (fis) |
| 108 | delete fis; |
| 109 | if (fos) |
| 110 | delete fos; |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 111 | |
| 112 | delete[] cafile; |
| 113 | delete[] crlfile; |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool CSecurityTLSBase::processMsg(CConnection* cc) |
| 117 | { |
| 118 | rdr::InStream* is = cc->getInStream(); |
| 119 | rdr::OutStream* os = cc->getOutStream(); |
| 120 | client = cc; |
| 121 | |
| 122 | initGlobal(); |
| 123 | |
| 124 | if (!session) { |
| 125 | if (!is->checkNoWait(1)) |
| 126 | return false; |
| 127 | |
| 128 | if (is->readU8() == 0) |
| 129 | return true; |
| 130 | |
| 131 | gnutls_init(&session, GNUTLS_CLIENT); |
| 132 | gnutls_set_default_priority(session); |
| 133 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 134 | setParam(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 135 | |
| 136 | gnutls_transport_set_pull_function(session, rdr::gnutls_InStream_pull); |
| 137 | gnutls_transport_set_push_function(session, rdr::gnutls_OutStream_push); |
| 138 | gnutls_transport_set_ptr2(session, |
| 139 | (gnutls_transport_ptr) is, |
| 140 | (gnutls_transport_ptr) os); |
| 141 | } |
| 142 | |
| 143 | int err; |
| 144 | err = gnutls_handshake(session); |
| 145 | if (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(err)) |
| 146 | return false; |
| 147 | |
| 148 | if (err != GNUTLS_E_SUCCESS) { |
| 149 | vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err)); |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 150 | shutdown(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 151 | throw AuthFailureException("TLS Handshake failed"); |
| 152 | } |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 153 | |
| 154 | checkSession(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 155 | |
| 156 | cc->setStreams(fis = new rdr::TLSInStream(is, session), |
| 157 | fos = new rdr::TLSOutStream(os, session)); |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
Adam Tkac | 0e61c34 | 2010-07-21 09:23:25 +0000 | [diff] [blame^] | 162 | void CSecurityTLSBase::setParam() |
| 163 | { |
| 164 | static const int kx_anon_priority[] = { GNUTLS_KX_ANON_DH, 0 }; |
| 165 | static const int kx_priority[] = { GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA, |
| 166 | GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0 }; |
| 167 | |
| 168 | if (anon) { |
| 169 | gnutls_kx_set_priority(session, kx_anon_priority); |
| 170 | gnutls_anon_allocate_client_credentials(&anon_cred); |
| 171 | gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred); |
| 172 | |
| 173 | vlog.debug("Anonymous session has been set"); |
| 174 | } else { |
| 175 | gnutls_kx_set_priority(session, kx_priority); |
| 176 | gnutls_certificate_allocate_credentials(&cert_cred); |
| 177 | |
| 178 | if (*cafile && gnutls_certificate_set_x509_trust_file(cert_cred,cafile,GNUTLS_X509_FMT_PEM) < 0) |
| 179 | throw AuthFailureException("load of CA cert failed"); |
| 180 | |
| 181 | if (*crlfile && gnutls_certificate_set_x509_crl_file(cert_cred,crlfile,GNUTLS_X509_FMT_PEM) < 0) |
| 182 | throw AuthFailureException("load of CRL failed"); |
| 183 | |
| 184 | gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred); |
| 185 | |
| 186 | vlog.debug("X509 session has been set"); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void CSecurityTLSBase::checkSession() |
| 191 | { |
| 192 | int status; |
| 193 | const gnutls_datum *cert_list; |
| 194 | unsigned int cert_list_size = 0; |
| 195 | unsigned int i; |
| 196 | |
| 197 | if (anon) |
| 198 | return; |
| 199 | |
| 200 | if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) |
| 201 | throw AuthFailureException("unsupported certificate type"); |
| 202 | |
| 203 | cert_list = gnutls_certificate_get_peers(session, &cert_list_size); |
| 204 | if (!cert_list_size) |
| 205 | throw AuthFailureException("unsupported certificate type"); |
| 206 | |
| 207 | status = gnutls_certificate_verify_peers(session); |
| 208 | if (status == GNUTLS_E_NO_CERTIFICATE_FOUND) |
| 209 | throw AuthFailureException("no certificate sent"); |
| 210 | |
| 211 | if (status < 0) { |
| 212 | vlog.error("X509 verify failed: %s\n", gnutls_strerror (status)); |
| 213 | throw AuthFailureException("certificate verification failed"); |
| 214 | } |
| 215 | |
| 216 | if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) |
| 217 | throw AuthFailureException("certificate issuer unknown"); |
| 218 | |
| 219 | if (status & GNUTLS_CERT_INVALID) |
| 220 | throw AuthFailureException("certificate not trusted"); |
| 221 | |
| 222 | for (i = 0; i < cert_list_size; i++) { |
| 223 | gnutls_x509_crt crt; |
| 224 | gnutls_x509_crt_init(&crt); |
| 225 | |
| 226 | if (gnutls_x509_crt_import(crt, &cert_list[i],GNUTLS_X509_FMT_DER) < 0) |
| 227 | throw AuthFailureException("Decoding of certificate failed"); |
| 228 | |
| 229 | if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) { |
| 230 | #if 0 |
| 231 | throw AuthFailureException("Hostname mismatch"); /* Non-fatal for now... */ |
| 232 | #endif |
| 233 | } |
| 234 | gnutls_x509_crt_deinit(crt); |
| 235 | } |
| 236 | } |
| 237 | |