blob: 0755f3686d942d75788b2bb81190c9868fe59c06 [file] [log] [blame]
Adam Tkacb10489b2010-04-23 14:16:04 +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/CSecurityTLSBase.h>
29#include <rfb/CConnection.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 CSecurityTLSBase::initGlobal()
49{
50 static bool globalInitDone = false;
51
52 if (!globalInitDone) {
53 gnutls_global_init();
54
55#ifdef TLS_DEBUG
56 gnutls_global_set_log_level(10);
57 gnutls_global_set_log_function(debug_log);
58#endif
59
60 globalInitDone = true;
61 }
62}
63
64CSecurityTLSBase::CSecurityTLSBase() : session(0)
65{
66 fis = 0;
67 fos = 0;
68}
69
70void CSecurityTLSBase::shutdown()
71{
72 if(session)
73 ;//gnutls_bye(session, GNUTLS_SHUT_RDWR);
74}
75
76
77CSecurityTLSBase::~CSecurityTLSBase()
78{
79 if (session) {
80 //gnutls_bye(session, GNUTLS_SHUT_RDWR);
81 gnutls_deinit (session);
82 session = 0;
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 CSecurityTLSBase::processMsg(CConnection* cc)
92{
93 rdr::InStream* is = cc->getInStream();
94 rdr::OutStream* os = cc->getOutStream();
95 client = cc;
96
97 initGlobal();
98
99 if (!session) {
100 if (!is->checkNoWait(1))
101 return false;
102
103 if (is->readU8() == 0)
104 return true;
105
106 gnutls_init(&session, GNUTLS_CLIENT);
107 gnutls_set_default_priority(session);
108
109 setParam(session);
110
111 gnutls_transport_set_pull_function(session, rdr::gnutls_InStream_pull);
112 gnutls_transport_set_push_function(session, rdr::gnutls_OutStream_push);
113 gnutls_transport_set_ptr2(session,
114 (gnutls_transport_ptr) is,
115 (gnutls_transport_ptr) os);
116 }
117
118 int err;
119 err = gnutls_handshake(session);
120 if (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(err))
121 return false;
122
123 if (err != GNUTLS_E_SUCCESS) {
124 vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err));
125 gnutls_bye(session, GNUTLS_SHUT_RDWR);
126 freeResources();
127 gnutls_deinit(session);
128 session = 0;
129 throw AuthFailureException("TLS Handshake failed");
130 }
131 checkSession(session);
132
133 cc->setStreams(fis = new rdr::TLSInStream(is, session),
134 fos = new rdr::TLSOutStream(os, session));
135
136 return true;
137}
138
139#endif /* HAVE_GNUTLS */