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