blob: 52fc9cb87d3065bab6c38ba21882dd689e1b4ee9 [file] [log] [blame]
Adam Tkacdfe19cf2010-04-23 14:14:11 +00001/*
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 Tkacdf799702010-04-28 15:45:53 +000026#ifndef HAVE_GNUTLS
27#error "This source should not be compiled without HAVE_GNUTLS defined"
28#endif
Adam Tkacdfe19cf2010-04-23 14:14:11 +000029
30#include <rfb/SSecurityTLS.h>
31#include <rfb/Exception.h>
32
33#define DH_BITS 1024
34
35#undef TLS_DEBUG
36
37using namespace rfb;
38
39SSecurityTLS::SSecurityTLS() : dh_params(0), anon_cred(0)
40{
41}
42
43SSecurityTLS::~SSecurityTLS()
44{
45 shutdown();
46 if (dh_params)
47 gnutls_dh_params_deinit(dh_params);
48 if (anon_cred)
49 gnutls_anon_free_server_credentials(anon_cred);
50}
51
52void SSecurityTLS::freeResources()
53{
54 if (dh_params)
55 gnutls_dh_params_deinit(dh_params);
56 dh_params = 0;
57 if (anon_cred)
58 gnutls_anon_free_server_credentials(anon_cred);
59 anon_cred = 0;
60}
61
62void SSecurityTLS::setParams(gnutls_session session)
63{
64 static const int kx_priority[] = {GNUTLS_KX_ANON_DH, 0};
65 gnutls_kx_set_priority(session, kx_priority);
66
67 if (gnutls_anon_allocate_server_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
68 throw AuthFailureException("gnutls_anon_allocate_server_credentials failed");
69
70 if (gnutls_dh_params_init(&dh_params) != GNUTLS_E_SUCCESS)
71 throw AuthFailureException("gnutls_dh_params_init failed");
72
73 if (gnutls_dh_params_generate2(dh_params, DH_BITS) != GNUTLS_E_SUCCESS)
74 throw AuthFailureException("gnutls_dh_params_generate2 failed");
75
76 gnutls_anon_set_server_dh_params(anon_cred, dh_params);
77
78 if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred)
79 != GNUTLS_E_SUCCESS)
80 throw AuthFailureException("gnutls_credentials_set failed");
81
82}
83