blob: 2fec9bc157fb45382dd2836eecc40f659239ef38 [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/SSecurityTLSBase.h>
29#include <rfb/SConnection.h>
30#include <rfb/LogWriter.h>
31#include <rfb/Exception.h>
32#include <rdr/TLSInStream.h>
33#include <rdr/TLSOutStream.h>
34
35#define TLS_DEBUG
36
37using namespace rfb;
38
39static LogWriter vlog("TLS");
40
41#ifdef TLS_DEBUG
42static void debug_log(int level, const char* str)
43{
44 vlog.debug(str);
45}
46#endif
47
48void SSecurityTLSBase::initGlobal()
49{
50 static bool globalInitDone = false;
51
52 if (!globalInitDone) {
53 if (gnutls_global_init() != GNUTLS_E_SUCCESS)
54 throw AuthFailureException("gnutls_global_init failed");
55
56#ifdef TLS_DEBUG
57 gnutls_global_set_log_level(10);
58 gnutls_global_set_log_function(debug_log);
59#endif
60
61 globalInitDone = true;
62 }
63}
64
65SSecurityTLSBase::SSecurityTLSBase() : session(0)
66{
67 fis=0;
68 fos=0;
69}
70
71void SSecurityTLSBase::shutdown()
72{
73 if(session)
74 ;//gnutls_bye(session, GNUTLS_SHUT_RDWR);
75}
76
77
78SSecurityTLSBase::~SSecurityTLSBase()
79{
80 if (session) {
81 //gnutls_bye(session, GNUTLS_SHUT_RDWR);
82 gnutls_deinit(session);
83 }
84 if(fis)
85 delete fis;
86 if(fos)
87 delete fos;
88 /* FIXME: should be doing gnutls_global_deinit() at some point */
89}
90
91bool SSecurityTLSBase::processMsg(SConnection *sc)
92{
93 rdr::InStream* is = sc->getInStream();
94 rdr::OutStream* os = sc->getOutStream();
95
96 vlog.debug("Process security message (session %p)", session);
97
98 if (!session) {
99 initGlobal();
100
101 if (gnutls_init(&session, GNUTLS_SERVER) != GNUTLS_E_SUCCESS)
102 throw AuthFailureException("gnutls_init failed");
103
104 if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
105 throw AuthFailureException("gnutls_set_default_priority failed");
106
107 try {
108 setParams(session);
109 }
110 catch(...) {
111 os->writeU8(0);
112 throw;
113 }
114
115 gnutls_transport_set_pull_function(session,rdr::gnutls_InStream_pull);
116 gnutls_transport_set_push_function(session,rdr::gnutls_OutStream_push);
117 gnutls_transport_set_ptr2(session,
118 (gnutls_transport_ptr)is,
119 (gnutls_transport_ptr)os);
120 os->writeU8(1);
121 os->flush();
122 }
123
124 int err;
125 if ((err = gnutls_handshake(session)) != GNUTLS_E_SUCCESS) {
126 if (!gnutls_error_is_fatal(err)) {
127 vlog.debug("Deferring completion of TLS handshake: %s", gnutls_strerror(err));
128 return false;
129 }
130 vlog.error("TLS Handshake failed: %s", gnutls_strerror (err));
131 gnutls_bye(session, GNUTLS_SHUT_RDWR);
132 freeResources();
133 gnutls_deinit(session);
134 session = 0;
135 throw AuthFailureException("TLS Handshake failed");
136 }
137
138 vlog.debug("Handshake completed");
139
140 sc->setStreams(fis=new rdr::TLSInStream(is,session),
141 fos=new rdr::TLSOutStream(os,session));
142
143 return true;
144}
145
146#endif /* HAVE_GNUTLS */