blob: 49532f52913577911fdb05bd78509c5891778709 [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
Pierre Ossman27eb55e2015-01-29 13:31:06 +010030#include <stdlib.h>
31
Adam Tkac21b61a52010-07-21 09:19:00 +000032#include <rfb/SSecurityTLS.h>
Adam Tkacdfe19cf2010-04-23 14:14:11 +000033#include <rfb/SConnection.h>
34#include <rfb/LogWriter.h>
35#include <rfb/Exception.h>
36#include <rdr/TLSInStream.h>
37#include <rdr/TLSOutStream.h>
Brian P. Hinz4b9b8972017-11-15 05:08:16 -050038#include <gnutls/x509.h>
Adam Tkacdfe19cf2010-04-23 14:14:11 +000039
Adam Tkacf39671d2010-07-21 09:10:54 +000040#define DH_BITS 1024 /* XXX This should be configurable! */
Adam Tkacdfe19cf2010-04-23 14:14:11 +000041
42using namespace rfb;
43
Adam Tkac21b61a52010-07-21 09:19:00 +000044StringParameter SSecurityTLS::X509_CertFile
Pierre Ossman3d2a84b2014-09-17 16:45:35 +020045("X509Cert", "Path to the X509 certificate in PEM format", "", ConfServer);
Adam Tkacf39671d2010-07-21 09:10:54 +000046
Adam Tkac21b61a52010-07-21 09:19:00 +000047StringParameter SSecurityTLS::X509_KeyFile
Pierre Ossman3d2a84b2014-09-17 16:45:35 +020048("X509Key", "Path to the key of the X509 certificate in PEM format", "", ConfServer);
Adam Tkacf39671d2010-07-21 09:10:54 +000049
Adam Tkacdfe19cf2010-04-23 14:14:11 +000050static LogWriter vlog("TLS");
Adam Tkacdfe19cf2010-04-23 14:14:11 +000051
Pierre Ossmanad2b3c42018-09-21 15:31:11 +020052SSecurityTLS::SSecurityTLS(SConnection* sc, bool _anon)
53 : SSecurity(sc), session(NULL), dh_params(NULL), anon_cred(NULL),
Pierre Ossman06c11992018-09-21 15:34:47 +020054 cert_cred(NULL), anon(_anon), tlsis(NULL), tlsos(NULL),
55 rawis(NULL), rawos(NULL)
Adam Tkacdfe19cf2010-04-23 14:14:11 +000056{
Adam Tkacf39671d2010-07-21 09:10:54 +000057 certfile = X509_CertFile.getData();
58 keyfile = X509_KeyFile.getData();
Pierre Ossman8aa4bc52016-08-23 17:02:58 +020059
60 if (gnutls_global_init() != GNUTLS_E_SUCCESS)
61 throw AuthFailureException("gnutls_global_init failed");
Adam Tkacdfe19cf2010-04-23 14:14:11 +000062}
63
Adam Tkac21b61a52010-07-21 09:19:00 +000064void SSecurityTLS::shutdown()
Adam Tkacdfe19cf2010-04-23 14:14:11 +000065{
Adam Tkacf39671d2010-07-21 09:10:54 +000066 if (session) {
67 if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS) {
68 /* FIXME: Treat as non-fatal error */
69 vlog.error("TLS session wasn't terminated gracefully");
70 }
71 }
72
73 if (dh_params) {
74 gnutls_dh_params_deinit(dh_params);
75 dh_params = 0;
76 }
77
78 if (anon_cred) {
79 gnutls_anon_free_server_credentials(anon_cred);
80 anon_cred = 0;
81 }
82
83 if (cert_cred) {
84 gnutls_certificate_free_credentials(cert_cred);
85 cert_cred = 0;
86 }
87
Pierre Ossman06c11992018-09-21 15:34:47 +020088 if (rawis && rawos) {
89 sc->setStreams(rawis, rawos);
90 rawis = NULL;
91 rawos = NULL;
92 }
93
Pierre Ossman1b746342018-09-21 15:33:30 +020094 if (tlsis) {
95 delete tlsis;
96 tlsis = NULL;
97 }
98 if (tlsos) {
99 delete tlsos;
100 tlsos = NULL;
101 }
102
Adam Tkacf39671d2010-07-21 09:10:54 +0000103 if (session) {
104 gnutls_deinit(session);
105 session = 0;
Adam Tkacf39671d2010-07-21 09:10:54 +0000106 }
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000107}
108
109
Adam Tkac21b61a52010-07-21 09:19:00 +0000110SSecurityTLS::~SSecurityTLS()
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000111{
Adam Tkacf39671d2010-07-21 09:10:54 +0000112 shutdown();
113
Adam Tkacf39671d2010-07-21 09:10:54 +0000114 delete[] keyfile;
115 delete[] certfile;
Pierre Ossman8aa4bc52016-08-23 17:02:58 +0200116
117 gnutls_global_deinit();
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000118}
119
Pierre Ossmanad2b3c42018-09-21 15:31:11 +0200120bool SSecurityTLS::processMsg()
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000121{
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000122 vlog.debug("Process security message (session %p)", session);
123
124 if (!session) {
Pierre Ossman1b746342018-09-21 15:33:30 +0200125 rdr::InStream* is = sc->getInStream();
126 rdr::OutStream* os = sc->getOutStream();
127
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000128 if (gnutls_init(&session, GNUTLS_SERVER) != GNUTLS_E_SUCCESS)
129 throw AuthFailureException("gnutls_init failed");
130
131 if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
132 throw AuthFailureException("gnutls_set_default_priority failed");
133
134 try {
135 setParams(session);
136 }
137 catch(...) {
138 os->writeU8(0);
139 throw;
140 }
141
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000142 os->writeU8(1);
143 os->flush();
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000144
Pierre Ossman1b746342018-09-21 15:33:30 +0200145 // Create these early as they set up the push/pull functions
146 // for GnuTLS
147 tlsis = new rdr::TLSInStream(is, session);
148 tlsos = new rdr::TLSOutStream(os, session);
Pierre Ossman06c11992018-09-21 15:34:47 +0200149
150 rawis = is;
151 rawos = os;
Pierre Ossman1b746342018-09-21 15:33:30 +0200152 }
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000153
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000154 int err;
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000155 err = gnutls_handshake(session);
156 if (err != GNUTLS_E_SUCCESS) {
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000157 if (!gnutls_error_is_fatal(err)) {
158 vlog.debug("Deferring completion of TLS handshake: %s", gnutls_strerror(err));
159 return false;
160 }
161 vlog.error("TLS Handshake failed: %s", gnutls_strerror (err));
Adam Tkacf39671d2010-07-21 09:10:54 +0000162 shutdown();
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000163 throw AuthFailureException("TLS Handshake failed");
164 }
165
166 vlog.debug("Handshake completed");
167
Pierre Ossman1b746342018-09-21 15:33:30 +0200168 sc->setStreams(tlsis, tlsos);
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000169
170 return true;
171}
172
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100173void SSecurityTLS::setParams(gnutls_session_t session)
Adam Tkacf39671d2010-07-21 09:10:54 +0000174{
Pierre Ossman27eb55e2015-01-29 13:31:06 +0100175 static const char kx_anon_priority[] = ":+ANON-ECDH:+ANON-DH";
Adam Tkacf39671d2010-07-21 09:10:54 +0000176
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100177 int ret;
Pierre Ossman27eb55e2015-01-29 13:31:06 +0100178 char *prio;
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100179 const char *err;
180
Pierre Ossman27eb55e2015-01-29 13:31:06 +0100181 prio = (char*)malloc(strlen(Security::GnuTLSPriority) +
182 strlen(kx_anon_priority) + 1);
183 if (prio == NULL)
184 throw AuthFailureException("Not enough memory for GnuTLS priority string");
185
186 strcpy(prio, Security::GnuTLSPriority);
187 if (anon)
188 strcat(prio, kx_anon_priority);
189
190 ret = gnutls_priority_set_direct(session, prio, &err);
191
192 free(prio);
193
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100194 if (ret != GNUTLS_E_SUCCESS) {
195 if (ret == GNUTLS_E_INVALID_REQUEST)
196 vlog.error("GnuTLS priority syntax error at: %s", err);
197 throw AuthFailureException("gnutls_set_priority_direct failed");
198 }
Adam Tkacf39671d2010-07-21 09:10:54 +0000199
200 if (gnutls_dh_params_init(&dh_params) != GNUTLS_E_SUCCESS)
201 throw AuthFailureException("gnutls_dh_params_init failed");
202
203 if (gnutls_dh_params_generate2(dh_params, DH_BITS) != GNUTLS_E_SUCCESS)
204 throw AuthFailureException("gnutls_dh_params_generate2 failed");
205
206 if (anon) {
207 if (gnutls_anon_allocate_server_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
208 throw AuthFailureException("gnutls_anon_allocate_server_credentials failed");
209
210 gnutls_anon_set_server_dh_params(anon_cred, dh_params);
211
212 if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred)
213 != GNUTLS_E_SUCCESS)
214 throw AuthFailureException("gnutls_credentials_set failed");
215
216 vlog.debug("Anonymous session has been set");
217
218 } else {
219 if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
220 throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
221
222 gnutls_certificate_set_dh_params(cert_cred, dh_params);
223
Brian P. Hinzcab73382017-11-14 20:57:07 -0500224 switch (gnutls_certificate_set_x509_key_file(cert_cred, certfile, keyfile, GNUTLS_X509_FMT_PEM)) {
225 case GNUTLS_E_SUCCESS:
226 break;
227 case GNUTLS_E_CERTIFICATE_KEY_MISMATCH:
228 throw AuthFailureException("Private key does not match certificate");
229 case GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE:
230 throw AuthFailureException("Unsupported certificate type");
231 default:
232 throw AuthFailureException("Error loading X509 certificate or key");
233 }
Adam Tkacf39671d2010-07-21 09:10:54 +0000234
235 if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred)
236 != GNUTLS_E_SUCCESS)
237 throw AuthFailureException("gnutls_credentials_set failed");
238
239 vlog.debug("X509 session has been set");
240
241 }
242
243}