1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 * gtlsserverconnection-openssl.c
5 * Copyright (C) 2015 NICE s.r.l.
7 * This file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * In addition, when the library is used with OpenSSL, a special
21 * exception applies. Refer to the LICENSE_EXCEPTION file for details.
23 * Authors: Ignacio Casal Quinteiro
28 #include "gtlsserverconnection-openssl.h"
29 #include "gtlscertificate-openssl.h"
31 #include "openssl-include.h"
32 #include <glib/gi18n-lib.h>
34 struct _GTlsServerConnectionOpenssl
36 GTlsConnectionOpenssl parent_instance;
38 GTlsAuthenticationMode authentication_mode;
47 PROP_AUTHENTICATION_MODE
50 static void g_tls_server_connection_openssl_initable_interface_init (GInitableIface *iface);
52 static void g_tls_server_connection_openssl_server_connection_interface_init (GTlsServerConnectionInterface *iface);
54 static GInitableIface *g_tls_server_connection_openssl_parent_initable_iface;
56 G_DEFINE_TYPE_WITH_CODE (GTlsServerConnectionOpenssl, g_tls_server_connection_openssl, G_TYPE_TLS_CONNECTION_OPENSSL,
57 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
58 g_tls_server_connection_openssl_initable_interface_init)
59 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION,
60 g_tls_server_connection_openssl_server_connection_interface_init)
61 G_IMPLEMENT_INTERFACE (G_TYPE_DTLS_SERVER_CONNECTION,
65 g_tls_server_connection_openssl_finalize (GObject *object)
67 GTlsServerConnectionOpenssl *openssl = G_TLS_SERVER_CONNECTION_OPENSSL (object);
69 SSL_free (openssl->ssl);
70 SSL_CTX_free (openssl->ssl_ctx);
71 SSL_SESSION_free (openssl->session);
73 G_OBJECT_CLASS (g_tls_server_connection_openssl_parent_class)->finalize (object);
77 g_tls_server_connection_openssl_get_property (GObject *object,
82 GTlsServerConnectionOpenssl *openssl = G_TLS_SERVER_CONNECTION_OPENSSL (object);
86 case PROP_AUTHENTICATION_MODE:
87 g_value_set_enum (value, openssl->authentication_mode);
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
96 g_tls_server_connection_openssl_set_property (GObject *object,
101 GTlsServerConnectionOpenssl *openssl = G_TLS_SERVER_CONNECTION_OPENSSL (object);
105 case PROP_AUTHENTICATION_MODE:
106 openssl->authentication_mode = g_value_get_enum (value);
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
115 verify_callback (int preverify_ok,
122 g_tls_server_connection_openssl_prepare_handshake (GTlsConnectionBase *tls,
123 gchar **advertised_protocols)
125 GTlsServerConnectionOpenssl *openssl = G_TLS_SERVER_CONNECTION_OPENSSL (tls);
126 GTlsConnectionBaseClass *base_class = G_TLS_CONNECTION_BASE_CLASS (g_tls_server_connection_openssl_parent_class);
129 switch (openssl->authentication_mode)
131 case G_TLS_AUTHENTICATION_REQUIRED:
132 req_mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
134 case G_TLS_AUTHENTICATION_REQUESTED:
135 req_mode = SSL_VERIFY_PEER;
137 case G_TLS_AUTHENTICATION_NONE:
139 req_mode = SSL_VERIFY_NONE;
143 SSL_set_verify (openssl->ssl, req_mode, verify_callback);
144 /* FIXME: is this ok? */
145 SSL_set_verify_depth (openssl->ssl, 0);
147 if (base_class->prepare_handshake)
148 base_class->prepare_handshake (tls, advertised_protocols);
152 g_tls_server_connection_openssl_get_ssl (GTlsConnectionOpenssl *connection)
154 return G_TLS_SERVER_CONNECTION_OPENSSL (connection)->ssl;
158 ssl_set_certificate (SSL *ssl,
159 GTlsCertificate *cert,
164 GTlsCertificate *issuer;
165 char error_buffer[256];
167 key = g_tls_certificate_openssl_get_key (G_TLS_CERTIFICATE_OPENSSL (cert));
171 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
172 _("Certificate has no private key"));
176 /* Note, order is important. If a certificate has been set previously,
177 * OpenSSL requires that the new certificate is set _before_ the new
178 * private key is set. */
179 x = g_tls_certificate_openssl_get_cert (G_TLS_CERTIFICATE_OPENSSL (cert));
180 if (SSL_use_certificate (ssl, x) <= 0)
182 ERR_error_string_n (ERR_get_error (), error_buffer, sizeof (error_buffer));
183 g_set_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
184 _("There is a problem with the certificate: %s"),
189 if (SSL_use_PrivateKey (ssl, key) <= 0)
191 ERR_error_string_n (ERR_get_error (), error_buffer, sizeof (error_buffer));
192 g_set_error (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
193 _("There is a problem with the certificate private key: %s"),
198 if (SSL_clear_chain_certs (ssl) == 0)
200 ERR_error_string_n (ERR_get_error (), error_buffer, sizeof (error_buffer));
201 g_warning ("There was a problem clearing the chain certificates: %s",
205 /* Add all the issuers to create the full certificate chain */
206 for (issuer = g_tls_certificate_get_issuer (G_TLS_CERTIFICATE (cert));
208 issuer = g_tls_certificate_get_issuer (issuer))
212 issuer_x = g_tls_certificate_openssl_get_cert (G_TLS_CERTIFICATE_OPENSSL (issuer));
214 /* Be careful here and duplicate the certificate since the ssl object
215 * will take the ownership
217 if (SSL_add1_chain_cert (ssl, issuer_x) == 0)
219 ERR_error_string_n (ERR_get_error (), error_buffer, sizeof (error_buffer));
220 g_warning ("There was a problem adding the chain certificate: %s",
229 on_certificate_changed (GObject *object,
234 GTlsCertificate *cert;
236 ssl = g_tls_server_connection_openssl_get_ssl (G_TLS_CONNECTION_OPENSSL (object));
237 cert = g_tls_connection_get_certificate (G_TLS_CONNECTION (object));
240 ssl_set_certificate (ssl, cert, NULL);
244 g_tls_server_connection_openssl_class_init (GTlsServerConnectionOpensslClass *klass)
246 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
247 GTlsConnectionBaseClass *base_class = G_TLS_CONNECTION_BASE_CLASS (klass);
248 GTlsConnectionOpensslClass *connection_class = G_TLS_CONNECTION_OPENSSL_CLASS (klass);
250 gobject_class->finalize = g_tls_server_connection_openssl_finalize;
251 gobject_class->get_property = g_tls_server_connection_openssl_get_property;
252 gobject_class->set_property = g_tls_server_connection_openssl_set_property;
254 base_class->prepare_handshake = g_tls_server_connection_openssl_prepare_handshake;
256 connection_class->get_ssl = g_tls_server_connection_openssl_get_ssl;
258 g_object_class_override_property (gobject_class, PROP_AUTHENTICATION_MODE, "authentication-mode");
262 g_tls_server_connection_openssl_init (GTlsServerConnectionOpenssl *openssl)
267 g_tls_server_connection_openssl_server_connection_interface_init (GTlsServerConnectionInterface *iface)
271 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined (LIBRESSL_VERSION_NUMBER)
273 ssl_info_callback (const SSL *ssl,
277 if ((type & SSL_CB_HANDSHAKE_DONE) != 0)
279 /* Disable renegotiation (CVE-2009-3555) */
280 ssl->s3->flags |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
286 set_cipher_list (GTlsServerConnectionOpenssl *server,
289 const gchar *cipher_list;
291 cipher_list = g_getenv ("G_TLS_OPENSSL_CIPHER_LIST");
294 if (!SSL_CTX_set_cipher_list (server->ssl_ctx, cipher_list))
296 char error_buffer[256];
297 ERR_error_string_n (ERR_get_error (), error_buffer, sizeof (error_buffer));
298 g_set_error (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
299 _("Could not set TLS cipher list: %s"),
309 set_max_protocol (GTlsServerConnectionOpenssl *server,
312 #ifdef SSL_CTX_set_max_proto_version
315 proto = g_getenv ("G_TLS_OPENSSL_MAX_PROTO");
318 gint64 version = g_ascii_strtoll (proto, NULL, 0);
320 if (version > 0 && version < G_MAXINT)
322 if (!SSL_CTX_set_max_proto_version (server->ssl_ctx, (int)version))
324 char error_buffer[256];
325 ERR_error_string_n (ERR_get_error (), error_buffer, sizeof (error_buffer));
326 g_set_error (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
327 _("Could not set MAX protocol to %d: %s"),
328 (int)version, error_buffer);
338 #ifdef SSL_CTX_set1_sigalgs_list
340 set_signature_algorithm_list (GTlsServerConnectionOpenssl *server)
342 const gchar *signature_algorithm_list;
344 signature_algorithm_list = g_getenv ("G_TLS_OPENSSL_SIGNATURE_ALGORITHM_LIST");
345 if (!signature_algorithm_list)
348 SSL_CTX_set1_sigalgs_list (server->ssl_ctx, signature_algorithm_list);
352 #ifdef SSL_CTX_set1_curves_list
354 set_curve_list (GTlsServerConnectionOpenssl *server)
356 const gchar *curve_list;
358 curve_list = g_getenv ("G_TLS_OPENSSL_CURVE_LIST");
362 SSL_CTX_set1_curves_list (server->ssl_ctx, curve_list);
367 g_tls_server_connection_openssl_initable_init (GInitable *initable,
368 GCancellable *cancellable,
371 GTlsServerConnectionOpenssl *server = G_TLS_SERVER_CONNECTION_OPENSSL (initable);
372 GTlsCertificate *cert;
374 char error_buffer[256];
376 server->session = SSL_SESSION_new ();
378 server->ssl_ctx = SSL_CTX_new (g_tls_connection_base_is_dtls (G_TLS_CONNECTION_BASE (server))
379 #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined (LIBRESSL_VERSION_NUMBER)
380 ? DTLS_server_method ()
381 : TLS_server_method ());
383 ? DTLSv1_server_method ()
384 : SSLv23_server_method ());
386 if (!server->ssl_ctx)
388 ERR_error_string_n (ERR_get_error (), error_buffer, sizeof (error_buffer));
389 g_set_error (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
390 _("Could not create TLS context: %s"),
395 if (!set_cipher_list (server, error))
398 if (!set_max_protocol (server, error))
401 /* Only TLS 1.2 or higher */
402 options = SSL_OP_NO_TICKET |
403 SSL_OP_NO_COMPRESSION |
404 SSL_OP_CIPHER_SERVER_PREFERENCE |
405 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
406 SSL_OP_SINGLE_ECDH_USE |
407 #ifdef SSL_OP_NO_TLSv1_1
414 #ifdef SSL_OP_NO_RENEGOTIATION
415 options |= SSL_OP_NO_RENEGOTIATION;
418 SSL_CTX_set_options (server->ssl_ctx, options);
420 SSL_CTX_add_session (server->ssl_ctx, server->session);
422 #ifdef SSL_CTX_set1_sigalgs_list
423 set_signature_algorithm_list (server);
426 #ifdef SSL_CTX_set1_curves_list
427 set_curve_list (server);
430 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined (LIBRESSL_VERSION_NUMBER)
431 # ifdef SSL_CTX_set_ecdh_auto
432 SSL_CTX_set_ecdh_auto (server->ssl_ctx, 1);
437 ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);
440 SSL_CTX_set_tmp_ecdh (server->ssl_ctx, ecdh);
446 SSL_CTX_set_info_callback (server->ssl_ctx, ssl_info_callback);
449 cert = g_tls_connection_get_certificate (G_TLS_CONNECTION (initable));
451 server->ssl = SSL_new (server->ssl_ctx);
454 ERR_error_string_n (ERR_get_error (), error_buffer, sizeof (error_buffer));
455 g_set_error (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
456 _("Could not create TLS connection: %s"),
461 if (cert && !ssl_set_certificate (server->ssl, cert, error))
464 SSL_set_accept_state (server->ssl);
466 if (!g_tls_server_connection_openssl_parent_initable_iface->
467 init (initable, cancellable, error))
470 g_signal_connect (server, "notify::certificate", G_CALLBACK (on_certificate_changed), NULL);
476 g_tls_server_connection_openssl_initable_interface_init (GInitableIface *iface)
478 g_tls_server_connection_openssl_parent_initable_iface = g_type_interface_peek_parent (iface);
480 iface->init = g_tls_server_connection_openssl_initable_init;