Adam Tkac | f5f6a00 | 2010-07-21 09:09:19 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2005 Martin Koegler |
| 3 | * Copyright (C) 2010 TigerVNC Team |
| 4 | * |
| 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 18 | * USA. |
| 19 | */ |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
| 25 | #ifndef HAVE_GNUTLS |
| 26 | #error "This source should not be compiled without HAVE_GNUTLS defined" |
| 27 | #endif |
| 28 | |
| 29 | #include <rfb/CSecurityX509.h> |
| 30 | #include <rfb/CConnection.h> |
| 31 | #include <rfb/Exception.h> |
| 32 | #include <rfb/LogWriter.h> |
| 33 | |
| 34 | #include <gnutls/x509.h> |
| 35 | |
| 36 | using namespace rfb; |
| 37 | |
| 38 | StringParameter CSecurityX509::x509ca("x509ca", "X509 CA certificate", "", ConfViewer); |
| 39 | StringParameter CSecurityX509::x509crl("x509crl", "X509 CRL file", "", ConfViewer); |
| 40 | |
| 41 | static LogWriter vlog("CSecurityX509"); |
| 42 | |
| 43 | CSecurityX509::CSecurityX509() : cert_cred(0) |
| 44 | { |
| 45 | cafile = x509ca.getData(); |
| 46 | crlfile = x509crl.getData(); |
| 47 | } |
| 48 | |
| 49 | CSecurityX509::~CSecurityX509() |
| 50 | { |
| 51 | shutdown(); |
| 52 | if (cert_cred) |
| 53 | gnutls_certificate_free_credentials(cert_cred); |
| 54 | delete[] cafile; |
| 55 | delete[] crlfile; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | void CSecurityX509::freeResources() |
| 60 | { |
| 61 | if (cert_cred) |
| 62 | gnutls_certificate_free_credentials(cert_cred); |
| 63 | cert_cred = 0; |
| 64 | } |
| 65 | |
| 66 | void CSecurityX509::setParam(gnutls_session session) |
| 67 | { |
| 68 | int kx_priority[] = { GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA, GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0 }; |
| 69 | gnutls_kx_set_priority(session, kx_priority); |
| 70 | |
| 71 | gnutls_certificate_allocate_credentials(&cert_cred); |
| 72 | |
| 73 | if (*cafile && gnutls_certificate_set_x509_trust_file(cert_cred,cafile,GNUTLS_X509_FMT_PEM) < 0) |
| 74 | throw AuthFailureException("load of CA cert failed"); |
| 75 | |
| 76 | if (*crlfile && gnutls_certificate_set_x509_crl_file(cert_cred,crlfile,GNUTLS_X509_FMT_PEM) < 0) |
| 77 | throw AuthFailureException("load of CRL failed"); |
| 78 | |
| 79 | gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred); |
| 80 | } |
| 81 | |
| 82 | void CSecurityX509::checkSession(gnutls_session session) |
| 83 | { |
| 84 | int status; |
| 85 | const gnutls_datum *cert_list; |
| 86 | unsigned int cert_list_size = 0; |
| 87 | unsigned int i; |
| 88 | |
| 89 | if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) |
| 90 | throw AuthFailureException("unsupported certificate type"); |
| 91 | |
| 92 | cert_list = gnutls_certificate_get_peers(session, &cert_list_size); |
| 93 | if (!cert_list_size) |
| 94 | throw AuthFailureException("unsupported certificate type"); |
| 95 | |
| 96 | status = gnutls_certificate_verify_peers(session); |
| 97 | if (status == GNUTLS_E_NO_CERTIFICATE_FOUND) |
| 98 | throw AuthFailureException("no certificate sent"); |
| 99 | |
| 100 | if (status < 0) { |
| 101 | vlog.error("X509 verify failed: %s\n", gnutls_strerror (status)); |
| 102 | throw AuthFailureException("certificate verification failed"); |
| 103 | } |
| 104 | |
| 105 | if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) |
| 106 | throw AuthFailureException("certificate issuer unknown"); |
| 107 | |
| 108 | if (status & GNUTLS_CERT_INVALID) |
| 109 | throw AuthFailureException("certificate not trusted"); |
| 110 | |
| 111 | for (i = 0; i < cert_list_size; i++) { |
| 112 | gnutls_x509_crt crt; |
| 113 | gnutls_x509_crt_init(&crt); |
| 114 | |
| 115 | if (gnutls_x509_crt_import(crt, &cert_list[i],GNUTLS_X509_FMT_DER) < 0) |
| 116 | throw AuthFailureException("Decoding of certificate failed"); |
| 117 | |
| 118 | if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) { |
| 119 | #if 0 |
| 120 | /* FIXME: This must be changed to OK/Cancel checkbox */ |
| 121 | throw AuthFailureException("Hostname mismatch"); /* Non-fatal for now... */ |
| 122 | #endif |
| 123 | } |
| 124 | gnutls_x509_crt_deinit(crt); |
| 125 | } |
| 126 | } |
| 127 | |