blob: bf77b9b2352c5d6f40fdd17b7e205537953529b5 [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 Ossman1b746342018-09-21 15:33:30 +020054 cert_cred(NULL), anon(_anon), tlsis(NULL), tlsos(NULL)
Adam Tkacdfe19cf2010-04-23 14:14:11 +000055{
Adam Tkacf39671d2010-07-21 09:10:54 +000056 certfile = X509_CertFile.getData();
57 keyfile = X509_KeyFile.getData();
Pierre Ossman8aa4bc52016-08-23 17:02:58 +020058
59 if (gnutls_global_init() != GNUTLS_E_SUCCESS)
60 throw AuthFailureException("gnutls_global_init failed");
Adam Tkacdfe19cf2010-04-23 14:14:11 +000061}
62
Adam Tkac21b61a52010-07-21 09:19:00 +000063void SSecurityTLS::shutdown()
Adam Tkacdfe19cf2010-04-23 14:14:11 +000064{
Adam Tkacf39671d2010-07-21 09:10:54 +000065 if (session) {
66 if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS) {
67 /* FIXME: Treat as non-fatal error */
68 vlog.error("TLS session wasn't terminated gracefully");
69 }
70 }
71
72 if (dh_params) {
73 gnutls_dh_params_deinit(dh_params);
74 dh_params = 0;
75 }
76
77 if (anon_cred) {
78 gnutls_anon_free_server_credentials(anon_cred);
79 anon_cred = 0;
80 }
81
82 if (cert_cred) {
83 gnutls_certificate_free_credentials(cert_cred);
84 cert_cred = 0;
85 }
86
Pierre Ossman1b746342018-09-21 15:33:30 +020087 if (tlsis) {
88 delete tlsis;
89 tlsis = NULL;
90 }
91 if (tlsos) {
92 delete tlsos;
93 tlsos = NULL;
94 }
95
Adam Tkacf39671d2010-07-21 09:10:54 +000096 if (session) {
97 gnutls_deinit(session);
98 session = 0;
Adam Tkacf39671d2010-07-21 09:10:54 +000099 }
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000100}
101
102
Adam Tkac21b61a52010-07-21 09:19:00 +0000103SSecurityTLS::~SSecurityTLS()
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000104{
Adam Tkacf39671d2010-07-21 09:10:54 +0000105 shutdown();
106
Adam Tkacf39671d2010-07-21 09:10:54 +0000107 delete[] keyfile;
108 delete[] certfile;
Pierre Ossman8aa4bc52016-08-23 17:02:58 +0200109
110 gnutls_global_deinit();
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000111}
112
Pierre Ossmanad2b3c42018-09-21 15:31:11 +0200113bool SSecurityTLS::processMsg()
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000114{
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000115 vlog.debug("Process security message (session %p)", session);
116
117 if (!session) {
Pierre Ossman1b746342018-09-21 15:33:30 +0200118 rdr::InStream* is = sc->getInStream();
119 rdr::OutStream* os = sc->getOutStream();
120
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000121 if (gnutls_init(&session, GNUTLS_SERVER) != GNUTLS_E_SUCCESS)
122 throw AuthFailureException("gnutls_init failed");
123
124 if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
125 throw AuthFailureException("gnutls_set_default_priority failed");
126
127 try {
128 setParams(session);
129 }
130 catch(...) {
131 os->writeU8(0);
132 throw;
133 }
134
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000135 os->writeU8(1);
136 os->flush();
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000137
Pierre Ossman1b746342018-09-21 15:33:30 +0200138 // Create these early as they set up the push/pull functions
139 // for GnuTLS
140 tlsis = new rdr::TLSInStream(is, session);
141 tlsos = new rdr::TLSOutStream(os, session);
142 }
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000143
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000144 int err;
Pierre Ossmanfe48cd42012-07-03 14:43:38 +0000145 err = gnutls_handshake(session);
146 if (err != GNUTLS_E_SUCCESS) {
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000147 if (!gnutls_error_is_fatal(err)) {
148 vlog.debug("Deferring completion of TLS handshake: %s", gnutls_strerror(err));
149 return false;
150 }
151 vlog.error("TLS Handshake failed: %s", gnutls_strerror (err));
Adam Tkacf39671d2010-07-21 09:10:54 +0000152 shutdown();
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000153 throw AuthFailureException("TLS Handshake failed");
154 }
155
156 vlog.debug("Handshake completed");
157
Pierre Ossman1b746342018-09-21 15:33:30 +0200158 sc->setStreams(tlsis, tlsos);
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000159
160 return true;
161}
162
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100163void SSecurityTLS::setParams(gnutls_session_t session)
Adam Tkacf39671d2010-07-21 09:10:54 +0000164{
Pierre Ossman27eb55e2015-01-29 13:31:06 +0100165 static const char kx_anon_priority[] = ":+ANON-ECDH:+ANON-DH";
Adam Tkacf39671d2010-07-21 09:10:54 +0000166
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100167 int ret;
Pierre Ossman27eb55e2015-01-29 13:31:06 +0100168 char *prio;
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100169 const char *err;
170
Pierre Ossman27eb55e2015-01-29 13:31:06 +0100171 prio = (char*)malloc(strlen(Security::GnuTLSPriority) +
172 strlen(kx_anon_priority) + 1);
173 if (prio == NULL)
174 throw AuthFailureException("Not enough memory for GnuTLS priority string");
175
176 strcpy(prio, Security::GnuTLSPriority);
177 if (anon)
178 strcat(prio, kx_anon_priority);
179
180 ret = gnutls_priority_set_direct(session, prio, &err);
181
182 free(prio);
183
Pierre Ossman88c24ed2015-01-29 13:12:22 +0100184 if (ret != GNUTLS_E_SUCCESS) {
185 if (ret == GNUTLS_E_INVALID_REQUEST)
186 vlog.error("GnuTLS priority syntax error at: %s", err);
187 throw AuthFailureException("gnutls_set_priority_direct failed");
188 }
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
Brian P. Hinzcab73382017-11-14 20:57:07 -0500214 switch (gnutls_certificate_set_x509_key_file(cert_cred, certfile, keyfile, GNUTLS_X509_FMT_PEM)) {
215 case GNUTLS_E_SUCCESS:
216 break;
217 case GNUTLS_E_CERTIFICATE_KEY_MISMATCH:
218 throw AuthFailureException("Private key does not match certificate");
219 case GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE:
220 throw AuthFailureException("Unsupported certificate type");
221 default:
222 throw AuthFailureException("Error loading X509 certificate or key");
223 }
Adam Tkacf39671d2010-07-21 09:10:54 +0000224
225 if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred)
226 != GNUTLS_E_SUCCESS)
227 throw AuthFailureException("gnutls_credentials_set failed");
228
229 vlog.debug("X509 session has been set");
230
231 }
232
233}