blob: 289015ad488a8d71bade7e823fb8e0b8aeafc41b [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
Adam Tkac43958232010-07-21 09:06:59 +000026#ifndef HAVE_GNUTLS
27#error "This header should not be compiled without HAVE_GNUTLS defined"
28#endif
Adam Tkacb10489b2010-04-23 14:16:04 +000029
30#include <rfb/CSecurityTLSBase.h>
31#include <rfb/CConnection.h>
32#include <rfb/LogWriter.h>
33#include <rfb/Exception.h>
34#include <rdr/TLSInStream.h>
35#include <rdr/TLSOutStream.h>
36
37#define TLS_DEBUG
38
39using namespace rfb;
40
41static LogWriter vlog("TLS");
42
43#ifdef TLS_DEBUG
44static void debug_log(int level, const char* str)
45{
46 vlog.debug(str);
47}
48#endif
49
50void CSecurityTLSBase::initGlobal()
51{
52 static bool globalInitDone = false;
53
54 if (!globalInitDone) {
55 gnutls_global_init();
56
57#ifdef TLS_DEBUG
58 gnutls_global_set_log_level(10);
59 gnutls_global_set_log_function(debug_log);
60#endif
61
62 globalInitDone = true;
63 }
64}
65
66CSecurityTLSBase::CSecurityTLSBase() : session(0)
67{
68 fis = 0;
69 fos = 0;
70}
71
72void CSecurityTLSBase::shutdown()
73{
74 if(session)
75 ;//gnutls_bye(session, GNUTLS_SHUT_RDWR);
76}
77
78
79CSecurityTLSBase::~CSecurityTLSBase()
80{
81 if (session) {
82 //gnutls_bye(session, GNUTLS_SHUT_RDWR);
83 gnutls_deinit (session);
84 session = 0;
85 }
86 if (fis)
87 delete fis;
88 if (fos)
89 delete fos;
90 /* FIXME: should be doing gnutls_global_deinit() at some point */
91}
92
93bool CSecurityTLSBase::processMsg(CConnection* cc)
94{
95 rdr::InStream* is = cc->getInStream();
96 rdr::OutStream* os = cc->getOutStream();
97 client = cc;
98
99 initGlobal();
100
101 if (!session) {
102 if (!is->checkNoWait(1))
103 return false;
104
105 if (is->readU8() == 0)
106 return true;
107
108 gnutls_init(&session, GNUTLS_CLIENT);
109 gnutls_set_default_priority(session);
110
111 setParam(session);
112
113 gnutls_transport_set_pull_function(session, rdr::gnutls_InStream_pull);
114 gnutls_transport_set_push_function(session, rdr::gnutls_OutStream_push);
115 gnutls_transport_set_ptr2(session,
116 (gnutls_transport_ptr) is,
117 (gnutls_transport_ptr) os);
118 }
119
120 int err;
121 err = gnutls_handshake(session);
122 if (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(err))
123 return false;
124
125 if (err != GNUTLS_E_SUCCESS) {
126 vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err));
127 gnutls_bye(session, GNUTLS_SHUT_RDWR);
128 freeResources();
129 gnutls_deinit(session);
130 session = 0;
131 throw AuthFailureException("TLS Handshake failed");
132 }
133 checkSession(session);
134
135 cc->setStreams(fis = new rdr::TLSInStream(is, session),
136 fos = new rdr::TLSOutStream(os, session));
137
138 return true;
139}
140