Adam Tkac | 5bf73fb | 2010-07-21 09:08:24 +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/SSecurityX509.h> |
| 30 | #include <rfb/Exception.h> |
| 31 | |
| 32 | #define DH_BITS 1024 |
| 33 | |
| 34 | using namespace rfb; |
| 35 | |
| 36 | StringParameter SSecurityX509::X509_CertFile |
| 37 | ("x509cert", "specifies path to the x509 certificate in PEM format", "", ConfServer); |
| 38 | |
| 39 | StringParameter SSecurityX509::X509_KeyFile |
| 40 | ("x509key", "specifies path to the key of the x509 certificate in PEM format", "", ConfServer); |
| 41 | |
| 42 | SSecurityX509::SSecurityX509() : dh_params(0), cert_cred(0) |
| 43 | { |
| 44 | certfile = X509_CertFile.getData(); |
| 45 | keyfile = X509_KeyFile.getData(); |
| 46 | } |
| 47 | |
| 48 | SSecurityX509::~SSecurityX509() |
| 49 | { |
| 50 | shutdown(); |
| 51 | if (dh_params) |
| 52 | gnutls_dh_params_deinit(dh_params); |
| 53 | if (cert_cred) |
| 54 | gnutls_certificate_free_credentials(cert_cred); |
| 55 | delete[] keyfile; |
| 56 | delete[] certfile; |
| 57 | } |
| 58 | |
| 59 | void SSecurityX509::freeResources() |
| 60 | { |
| 61 | if (dh_params) |
| 62 | gnutls_dh_params_deinit(dh_params); |
| 63 | dh_params=0; |
| 64 | if (cert_cred) |
| 65 | gnutls_certificate_free_credentials(cert_cred); |
| 66 | cert_cred=0; |
| 67 | } |
| 68 | |
| 69 | void SSecurityX509::setParams(gnutls_session session) |
| 70 | { |
| 71 | static const int kx_priority[] = {GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA, GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0}; |
| 72 | gnutls_kx_set_priority(session, kx_priority); |
| 73 | |
| 74 | if (gnutls_certificate_allocate_credentials(&cert_cred) < 0) |
| 75 | goto error; |
| 76 | if (gnutls_dh_params_init(&dh_params) < 0) |
| 77 | goto error; |
| 78 | if (gnutls_dh_params_generate2(dh_params, DH_BITS) < 0) |
| 79 | goto error; |
| 80 | gnutls_certificate_set_dh_params(cert_cred, dh_params); |
| 81 | if (gnutls_certificate_set_x509_key_file(cert_cred, certfile, keyfile,GNUTLS_X509_FMT_PEM) < 0) |
| 82 | throw AuthFailureException("load of key failed"); |
| 83 | if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred) < 0) |
| 84 | goto error; |
| 85 | return; |
| 86 | |
| 87 | error: |
| 88 | throw AuthFailureException("setParams failed"); |
| 89 | } |
| 90 | |