blob: 7a1a41ef37063a8085b60f553b780929355e121b [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
Adam Tkac21b61a52010-07-21 09:19:00 +000030#include <rfb/SSecurityTLS.h>
Adam Tkacdfe19cf2010-04-23 14:14:11 +000031#include <rfb/SConnection.h>
32#include <rfb/LogWriter.h>
33#include <rfb/Exception.h>
34#include <rdr/TLSInStream.h>
35#include <rdr/TLSOutStream.h>
36
Adam Tkacf39671d2010-07-21 09:10:54 +000037#define DH_BITS 1024 /* XXX This should be configurable! */
Adam Tkacdfe19cf2010-04-23 14:14:11 +000038
39using namespace rfb;
40
Adam Tkac21b61a52010-07-21 09:19:00 +000041StringParameter SSecurityTLS::X509_CertFile
Adam Tkacf39671d2010-07-21 09:10:54 +000042("x509cert", "specifies path to the x509 certificate in PEM format", "", ConfServer);
43
Adam Tkac21b61a52010-07-21 09:19:00 +000044StringParameter SSecurityTLS::X509_KeyFile
Adam Tkacf39671d2010-07-21 09:10:54 +000045("x509key", "specifies path to the key of the x509 certificate in PEM format", "", ConfServer);
46
Adam Tkacdfe19cf2010-04-23 14:14:11 +000047static LogWriter vlog("TLS");
Adam Tkac348269d2011-02-18 10:54:11 +000048static LogWriter vlog_raw("RawTLS");
Adam Tkacdfe19cf2010-04-23 14:14:11 +000049
Adam Tkacdfe19cf2010-04-23 14:14:11 +000050static void debug_log(int level, const char* str)
51{
Pierre Ossmanad8609a2012-04-26 09:04:14 +000052 vlog.debug("[%d]: %s", level, str);
Adam Tkacdfe19cf2010-04-23 14:14:11 +000053}
Adam Tkacdfe19cf2010-04-23 14:14:11 +000054
Adam Tkac21b61a52010-07-21 09:19:00 +000055void SSecurityTLS::initGlobal()
Adam Tkacdfe19cf2010-04-23 14:14:11 +000056{
57 static bool globalInitDone = false;
58
59 if (!globalInitDone) {
60 if (gnutls_global_init() != GNUTLS_E_SUCCESS)
61 throw AuthFailureException("gnutls_global_init failed");
62
Adam Tkac348269d2011-02-18 10:54:11 +000063 /* 100 means debug log */
64 if (vlog_raw.getLevel() >= 100) {
65 gnutls_global_set_log_level(10);
66 gnutls_global_set_log_function(debug_log);
67 }
Adam Tkacdfe19cf2010-04-23 14:14:11 +000068
69 globalInitDone = true;
70 }
71}
72
Adam Tkac21b61a52010-07-21 09:19:00 +000073SSecurityTLS::SSecurityTLS(bool _anon) : session(0), dh_params(0),
Adam Tkacf39671d2010-07-21 09:10:54 +000074 anon_cred(0), cert_cred(0),
75 anon(_anon), fis(0), fos(0)
Adam Tkacdfe19cf2010-04-23 14:14:11 +000076{
Adam Tkacf39671d2010-07-21 09:10:54 +000077 certfile = X509_CertFile.getData();
78 keyfile = X509_KeyFile.getData();
Adam Tkacdfe19cf2010-04-23 14:14:11 +000079}
80
Adam Tkac21b61a52010-07-21 09:19:00 +000081void SSecurityTLS::shutdown()
Adam Tkacdfe19cf2010-04-23 14:14:11 +000082{
Adam Tkacf39671d2010-07-21 09:10:54 +000083 if (session) {
84 if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS) {
85 /* FIXME: Treat as non-fatal error */
86 vlog.error("TLS session wasn't terminated gracefully");
87 }
88 }
89
90 if (dh_params) {
91 gnutls_dh_params_deinit(dh_params);
92 dh_params = 0;
93 }
94
95 if (anon_cred) {
96 gnutls_anon_free_server_credentials(anon_cred);
97 anon_cred = 0;
98 }
99
100 if (cert_cred) {
101 gnutls_certificate_free_credentials(cert_cred);
102 cert_cred = 0;
103 }
104
105 if (session) {
106 gnutls_deinit(session);
107 session = 0;
108
109 gnutls_global_deinit();
110 }
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000111}
112
113
Adam Tkac21b61a52010-07-21 09:19:00 +0000114SSecurityTLS::~SSecurityTLS()
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000115{
Adam Tkacf39671d2010-07-21 09:10:54 +0000116 shutdown();
117
118 if (fis)
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000119 delete fis;
Adam Tkacf39671d2010-07-21 09:10:54 +0000120 if (fos)
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000121 delete fos;
Adam Tkacf39671d2010-07-21 09:10:54 +0000122
123 delete[] keyfile;
124 delete[] certfile;
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000125}
126
Adam Tkac21b61a52010-07-21 09:19:00 +0000127bool SSecurityTLS::processMsg(SConnection *sc)
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000128{
129 rdr::InStream* is = sc->getInStream();
130 rdr::OutStream* os = sc->getOutStream();
131
132 vlog.debug("Process security message (session %p)", session);
133
134 if (!session) {
135 initGlobal();
136
137 if (gnutls_init(&session, GNUTLS_SERVER) != GNUTLS_E_SUCCESS)
138 throw AuthFailureException("gnutls_init failed");
139
140 if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
141 throw AuthFailureException("gnutls_set_default_priority failed");
142
143 try {
144 setParams(session);
145 }
146 catch(...) {
147 os->writeU8(0);
148 throw;
149 }
150
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000151 os->writeU8(1);
152 os->flush();
153 }
154
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000155 rdr::TLSInStream *tlsis = new rdr::TLSInStream(is, session);
156 rdr::TLSOutStream *tlsos = new rdr::TLSOutStream(os, session);
157
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000158 int err;
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000159 err = gnutls_handshake(session);
160 if (err != GNUTLS_E_SUCCESS) {
161 delete tlsis;
162 delete tlsos;
163
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000164 if (!gnutls_error_is_fatal(err)) {
165 vlog.debug("Deferring completion of TLS handshake: %s", gnutls_strerror(err));
166 return false;
167 }
168 vlog.error("TLS Handshake failed: %s", gnutls_strerror (err));
Adam Tkacf39671d2010-07-21 09:10:54 +0000169 shutdown();
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000170 throw AuthFailureException("TLS Handshake failed");
171 }
172
173 vlog.debug("Handshake completed");
174
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000175 sc->setStreams(fis = tlsis, fos = tlsos);
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000176
177 return true;
178}
179
Adam Tkac21b61a52010-07-21 09:19:00 +0000180void SSecurityTLS::setParams(gnutls_session session)
Adam Tkacf39671d2010-07-21 09:10:54 +0000181{
182 static const int kx_anon_priority[] = { GNUTLS_KX_ANON_DH, 0 };
183 static const int kx_priority[] = { GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA,
184 GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0 };
185
Adam Tkac6948ead2010-08-11 15:58:59 +0000186 if (gnutls_kx_set_priority(session, anon ? kx_anon_priority : kx_priority)
187 != GNUTLS_E_SUCCESS)
188 throw AuthFailureException("gnutls_kx_set_priority failed");
Adam Tkacf39671d2010-07-21 09:10:54 +0000189
190 if (gnutls_dh_params_init(&dh_params) != GNUTLS_E_SUCCESS)
191 throw AuthFailureException("gnutls_dh_params_init failed");
192
193 if (gnutls_dh_params_generate2(dh_params, DH_BITS) != GNUTLS_E_SUCCESS)
194 throw AuthFailureException("gnutls_dh_params_generate2 failed");
195
196 if (anon) {
197 if (gnutls_anon_allocate_server_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
198 throw AuthFailureException("gnutls_anon_allocate_server_credentials failed");
199
200 gnutls_anon_set_server_dh_params(anon_cred, dh_params);
201
202 if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred)
203 != GNUTLS_E_SUCCESS)
204 throw AuthFailureException("gnutls_credentials_set failed");
205
206 vlog.debug("Anonymous session has been set");
207
208 } else {
209 if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
210 throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
211
212 gnutls_certificate_set_dh_params(cert_cred, dh_params);
213
214 if (gnutls_certificate_set_x509_key_file(cert_cred, certfile, keyfile,
215 GNUTLS_X509_FMT_PEM) != GNUTLS_E_SUCCESS)
216 throw AuthFailureException("load of key failed");
217
218 if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred)
219 != GNUTLS_E_SUCCESS)
220 throw AuthFailureException("gnutls_credentials_set failed");
221
222 vlog.debug("X509 session has been set");
223
224 }
225
226}