1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
|
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* GIO - GLib Input, Output and Streaming Library
*
* Copyright 2010 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* In addition, when the library is used with OpenSSL, a special
* exception applies. Refer to the LICENSE_EXCEPTION file for details.
*/
#include "config.h"
#include "glib.h"
#include <errno.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <string.h>
#include "gtlsconnection-base.h"
#include "gtlsconnection-gnutls.h"
#include "gtlssessioncache.h"
#include "gtlsclientconnection-gnutls.h"
#include "gtlsbackend-gnutls.h"
#include "gtlscertificate-gnutls.h"
#include <glib/gi18n-lib.h>
enum
{
PROP_0,
PROP_VALIDATION_FLAGS,
PROP_SERVER_IDENTITY,
PROP_USE_SSL3,
PROP_ACCEPTED_CAS,
PROP_SESSION_RESUMPTION_ENABLED,
PROP_SESSION_REUSED
};
struct _GTlsClientConnectionGnutls
{
GTlsConnectionGnutls parent_instance;
GTlsCertificateFlags validation_flags;
GSocketConnectable *server_identity;
gboolean use_ssl3;
gboolean session_reused;
/* session_data is either the session ticket that was used to resume this
* connection, or the most recent session ticket received from the server.
* Because session ticket reuse is generally undesirable, it should only be
* accessed if session_data_override is set.
*/
gchar *session_id;
GBytes *session_data;
gboolean session_data_override;
GPtrArray *accepted_cas;
gboolean accepted_cas_changed;
gnutls_pcert_st *pcert;
unsigned int pcert_length;
gnutls_privkey_t pkey;
};
static void g_tls_client_connection_gnutls_initable_interface_init (GInitableIface *iface);
static void g_tls_client_connection_gnutls_client_connection_interface_init (GTlsClientConnectionInterface *iface);
static void g_tls_client_connection_gnutls_dtls_client_connection_interface_init (GDtlsClientConnectionInterface *iface);
static int g_tls_client_connection_gnutls_handshake_thread_retrieve_function (GTlsConnectionGnutls *conn,
gnutls_session_t session,
const gnutls_datum_t *req_ca_rdn,
int nreqs,
const gnutls_pk_algorithm_t *pk_algos,
int pk_algos_length,
gnutls_pcert_st **pcert,
unsigned int *pcert_length,
gnutls_privkey_t *pkey);
static GInitableIface *g_tls_client_connection_gnutls_parent_initable_iface;
G_DEFINE_FINAL_TYPE_WITH_CODE (GTlsClientConnectionGnutls, g_tls_client_connection_gnutls, G_TYPE_TLS_CONNECTION_GNUTLS,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
g_tls_client_connection_gnutls_initable_interface_init)
G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION,
g_tls_client_connection_gnutls_client_connection_interface_init);
G_IMPLEMENT_INTERFACE (G_TYPE_DTLS_CLIENT_CONNECTION,
g_tls_client_connection_gnutls_dtls_client_connection_interface_init));
static void
clear_gnutls_certificate_copy (gnutls_pcert_st **pcert,
guint *pcert_length,
gnutls_privkey_t *pkey)
{
g_tls_certificate_gnutls_copy_free (*pcert, *pcert_length, *pkey);
*pcert = NULL;
*pcert_length = 0;
*pkey = NULL;
}
static void
g_tls_client_connection_gnutls_init (GTlsClientConnectionGnutls *gnutls)
{
}
static const gchar *
get_server_identity (GTlsClientConnectionGnutls *gnutls)
{
if (G_IS_NETWORK_ADDRESS (gnutls->server_identity))
return g_network_address_get_hostname (G_NETWORK_ADDRESS (gnutls->server_identity));
else if (G_IS_NETWORK_SERVICE (gnutls->server_identity))
return g_network_service_get_domain (G_NETWORK_SERVICE (gnutls->server_identity));
else
return NULL;
}
static int session_inc_ref (gpointer data)
{
g_bytes_ref (data);
return 1;
}
static int
handshake_thread_session_ticket_received_cb (gnutls_session_t session,
guint htype,
guint when,
guint incoming,
const gnutls_datum_t *msg)
{
GTlsConnectionBase *tls = gnutls_transport_get_ptr (session);
GTlsClientConnectionGnutls *gnutls = G_TLS_CLIENT_CONNECTION_GNUTLS (tls);
gnutls_datum_t session_datum;
if (gnutls_session_get_data2 (session, &session_datum) == GNUTLS_E_SUCCESS)
{
g_clear_pointer (&gnutls->session_data, g_bytes_unref);
gnutls->session_data = g_bytes_new_with_free_func (session_datum.data,
session_datum.size,
(GDestroyNotify)gnutls_free,
session_datum.data);
if (g_tls_connection_base_get_session_resumption (tls) && gnutls->session_id)
{
g_tls_store_session_data (gnutls->session_id,
(gpointer)gnutls->session_data,
(SessionDup)g_bytes_ref,
(SessionAcquire)session_inc_ref,
(SessionRelease)g_bytes_unref,
glib_protocol_version_from_gnutls (gnutls_protocol_get_version (session)));
}
}
return 0;
}
static void
g_tls_client_connection_gnutls_finalize (GObject *object)
{
GTlsClientConnectionGnutls *gnutls = G_TLS_CLIENT_CONNECTION_GNUTLS (object);
g_clear_object (&gnutls->server_identity);
g_clear_pointer (&gnutls->accepted_cas, g_ptr_array_unref);
g_clear_pointer (&gnutls->session_data, g_bytes_unref);
clear_gnutls_certificate_copy (&gnutls->pcert, &gnutls->pcert_length, &gnutls->pkey);
G_OBJECT_CLASS (g_tls_client_connection_gnutls_parent_class)->finalize (object);
}
static gboolean
g_tls_client_connection_gnutls_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (initable);
gnutls_session_t session;
const gchar *hostname;
if (!g_tls_client_connection_gnutls_parent_initable_iface->init (initable, cancellable, error))
return FALSE;
session = g_tls_connection_gnutls_get_session (gnutls);
hostname = get_server_identity (G_TLS_CLIENT_CONNECTION_GNUTLS (gnutls));
if (hostname && !g_hostname_is_ip_address (hostname))
{
gchar *normalized_hostname = g_strdup (hostname);
if (hostname[strlen (hostname) - 1] == '.')
normalized_hostname[strlen (hostname) - 1] = '\0';
gnutls_server_name_set (session, GNUTLS_NAME_DNS,
normalized_hostname, strlen (normalized_hostname));
g_free (normalized_hostname);
}
gnutls_handshake_set_hook_function (session, GNUTLS_HANDSHAKE_NEW_SESSION_TICKET,
GNUTLS_HOOK_POST, handshake_thread_session_ticket_received_cb);
return TRUE;
}
static void
g_tls_client_connection_gnutls_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GTlsConnectionBase *tls = G_TLS_CONNECTION_BASE (object);
GTlsClientConnectionGnutls *gnutls = G_TLS_CLIENT_CONNECTION_GNUTLS (object);
GList *accepted_cas;
gint i;
switch (prop_id)
{
case PROP_VALIDATION_FLAGS:
g_value_set_flags (value, gnutls->validation_flags);
break;
case PROP_SERVER_IDENTITY:
g_value_set_object (value, gnutls->server_identity);
break;
case PROP_USE_SSL3:
g_value_set_boolean (value, gnutls->use_ssl3);
break;
case PROP_ACCEPTED_CAS:
accepted_cas = NULL;
if (gnutls->accepted_cas)
{
for (i = 0; i < gnutls->accepted_cas->len; ++i)
{
accepted_cas = g_list_prepend (accepted_cas, g_byte_array_ref (
gnutls->accepted_cas->pdata[i]));
}
accepted_cas = g_list_reverse (accepted_cas);
}
g_value_set_pointer (value, accepted_cas);
break;
case PROP_SESSION_REUSED:
g_value_set_boolean (value, gnutls->session_reused);
break;
case PROP_SESSION_RESUMPTION_ENABLED:
g_value_set_boolean (value, g_tls_connection_base_get_session_resumption (tls));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
g_tls_client_connection_gnutls_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GTlsConnectionBase *tls = G_TLS_CONNECTION_BASE (object);
GTlsClientConnectionGnutls *gnutls = G_TLS_CLIENT_CONNECTION_GNUTLS (object);
const char *hostname;
switch (prop_id)
{
case PROP_VALIDATION_FLAGS:
gnutls->validation_flags = g_value_get_flags (value);
break;
case PROP_SERVER_IDENTITY:
if (gnutls->server_identity)
g_object_unref (gnutls->server_identity);
gnutls->server_identity = g_value_dup_object (value);
hostname = get_server_identity (gnutls);
if (hostname && !g_hostname_is_ip_address (hostname))
{
gnutls_session_t session = g_tls_connection_gnutls_get_session (G_TLS_CONNECTION_GNUTLS (gnutls));
/* This will only be triggered if the identity is set after
* initialization */
if (session)
{
gchar *normalized_hostname = g_strdup (hostname);
if (hostname[strlen (hostname) - 1] == '.')
normalized_hostname[strlen (hostname) - 1] = '\0';
gnutls_server_name_set (session, GNUTLS_NAME_DNS,
normalized_hostname, strlen (normalized_hostname));
g_free (normalized_hostname);
}
}
break;
case PROP_USE_SSL3:
gnutls->use_ssl3 = g_value_get_boolean (value);
break;
case PROP_SESSION_RESUMPTION_ENABLED:
g_tls_connection_base_set_session_resumption (tls, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static int
g_tls_client_connection_gnutls_handshake_thread_retrieve_function (GTlsConnectionGnutls *conn,
gnutls_session_t session,
const gnutls_datum_t *req_ca_rdn,
int nreqs,
const gnutls_pk_algorithm_t *pk_algos,
int pk_algos_length,
gnutls_pcert_st **pcert,
unsigned int *pcert_length,
gnutls_privkey_t *pkey)
{
GTlsConnectionBase *tls = G_TLS_CONNECTION_BASE (conn);
GTlsClientConnectionGnutls *gnutls = G_TLS_CLIENT_CONNECTION_GNUTLS (conn);
GPtrArray *accepted_cas;
gboolean had_accepted_cas;
GByteArray *dn;
int i;
/* FIXME: Here we are supposed to ensure that the certificate supports one of
* the algorithms given in pk_algos.
*/
had_accepted_cas = gnutls->accepted_cas != NULL;
accepted_cas = g_ptr_array_new_with_free_func ((GDestroyNotify)g_byte_array_unref);
for (i = 0; i < nreqs; i++)
{
dn = g_byte_array_new ();
g_byte_array_append (dn, req_ca_rdn[i].data, req_ca_rdn[i].size);
g_ptr_array_add (accepted_cas, dn);
}
if (gnutls->accepted_cas)
g_ptr_array_unref (gnutls->accepted_cas);
gnutls->accepted_cas = accepted_cas;
gnutls->accepted_cas_changed = gnutls->accepted_cas || had_accepted_cas;
clear_gnutls_certificate_copy (&gnutls->pcert, &gnutls->pcert_length, &gnutls->pkey);
g_tls_connection_gnutls_handshake_thread_get_certificate (conn, pcert, pcert_length, pkey);
if (*pcert_length == 0)
{
clear_gnutls_certificate_copy (pcert, pcert_length, pkey);
if (g_tls_connection_base_handshake_thread_request_certificate (tls))
g_tls_connection_gnutls_handshake_thread_get_certificate (conn, pcert, pcert_length, pkey);
if (*pcert_length == 0)
{
clear_gnutls_certificate_copy (pcert, pcert_length, pkey);
/* If there is still no client certificate, this connection will
* probably fail, but we must not give up yet. The certificate might
* be optional, e.g. if the server is using
* G_TLS_AUTHENTICATION_REQUESTED, not G_TLS_AUTHENTICATION_REQUIRED.
*/
g_tls_connection_base_handshake_thread_set_missing_requested_client_certificate (tls);
return 0;
}
}
if (!*pkey)
{
clear_gnutls_certificate_copy (pcert, pcert_length, pkey);
/* No private key. GnuTLS expects it to be non-null if pcert_length is
* nonzero, so we have to abort now.
*/
g_tls_connection_base_handshake_thread_set_missing_requested_client_certificate (tls);
return -1;
}
/* We'll assume ownership. The return values are unowned. */
gnutls->pcert = *pcert;
gnutls->pcert_length = *pcert_length;
gnutls->pkey = *pkey;
return 0;
}
static void
g_tls_client_connection_gnutls_prepare_handshake (GTlsConnectionBase *tls,
gchar **advertised_protocols)
{
GTlsClientConnectionGnutls *gnutls = G_TLS_CLIENT_CONNECTION_GNUTLS (tls);
gnutls->session_id = g_tls_connection_base_get_session_id (G_TLS_CONNECTION_BASE (gnutls));
if (gnutls->session_data_override)
{
g_assert (gnutls->session_data);
gnutls_session_set_data (g_tls_connection_gnutls_get_session (G_TLS_CONNECTION_GNUTLS (tls)),
g_bytes_get_data (gnutls->session_data, NULL),
g_bytes_get_size (gnutls->session_data));
}
else if (gnutls->session_id)
{
GBytes *session_data;
session_data = (GBytes *)g_tls_lookup_session_data (gnutls->session_id);
if (session_data)
{
gnutls_session_set_data (g_tls_connection_gnutls_get_session (G_TLS_CONNECTION_GNUTLS (tls)),
g_bytes_get_data (session_data, NULL),
g_bytes_get_size (session_data));
g_clear_pointer (&gnutls->session_data, g_bytes_unref);
gnutls->session_data = g_steal_pointer (&session_data);
gnutls->session_reused = TRUE;
}
}
G_TLS_CONNECTION_BASE_CLASS (g_tls_client_connection_gnutls_parent_class)->
prepare_handshake (tls, advertised_protocols);
}
static void
g_tls_client_connection_gnutls_complete_handshake (GTlsConnectionBase *tls,
gboolean handshake_succeeded,
gchar **negotiated_protocol,
GTlsProtocolVersion *protocol_version,
gchar **ciphersuite_name,
GError **error)
{
GTlsClientConnectionGnutls *gnutls = G_TLS_CLIENT_CONNECTION_GNUTLS (tls);
gnutls_session_t session;
gnutls_protocol_t version;
G_TLS_CONNECTION_BASE_CLASS (g_tls_client_connection_gnutls_parent_class)->complete_handshake (tls,
handshake_succeeded,
negotiated_protocol,
protocol_version,
ciphersuite_name,
error);
/* It may have changed during the handshake, but we have to wait until here
* because we can't emit notifies on the handshake thread.
*/
if (gnutls->accepted_cas_changed)
g_object_notify (G_OBJECT (gnutls), "accepted-cas");
if (handshake_succeeded)
{
/* If we're not using TLS 1.3, store the session ticket here. We
* don't normally perform session resumption in TLS 1.2, but we still
* support it if the application calls copy_session_state() (which
* doesn't exist for DTLS, so do this for TLS only).
*
* Note to distant future: remove this when dropping TLS 1.2 support.
*/
session = g_tls_connection_gnutls_get_session (G_TLS_CONNECTION_GNUTLS (tls));
version = gnutls_protocol_get_version (session);
if (version <= GNUTLS_TLS1_2 && !g_tls_connection_base_is_dtls (tls))
{
gnutls_datum_t session_datum;
if (gnutls_session_get_data2 (g_tls_connection_gnutls_get_session (G_TLS_CONNECTION_GNUTLS (tls)),
&session_datum) == 0)
{
g_clear_pointer (&gnutls->session_data, g_bytes_unref);
gnutls->session_data = g_bytes_new_with_free_func (session_datum.data,
session_datum.size,
(GDestroyNotify)gnutls_free,
session_datum.data);
}
}
}
}
static void
g_tls_client_connection_gnutls_copy_session_state (GTlsClientConnection *conn,
GTlsClientConnection *source)
{
GTlsClientConnectionGnutls *gnutls = G_TLS_CLIENT_CONNECTION_GNUTLS (conn);
GTlsClientConnectionGnutls *gnutls_source = G_TLS_CLIENT_CONNECTION_GNUTLS (source);
/* Precondition: source has handshaked, conn has not. */
g_return_if_fail (!gnutls->session_id);
g_return_if_fail (gnutls_source->session_id);
g_return_if_fail (!gnutls->session_data);
/* Prefer to use a new session ticket, if possible. */
gnutls->session_data = g_tls_lookup_session_data (gnutls_source->session_id);
if (!gnutls->session_data && gnutls_source->session_data)
{
/* If it's not possible, we'll try to reuse the old ticket, even though
* this is a privacy risk. Applications should not use this function
* unless they need us to try as hard as possible to resume a session,
* even at the cost of privacy.
*/
gnutls->session_data = g_bytes_ref (gnutls_source->session_data);
}
gnutls->session_data_override = !!gnutls->session_data;
gnutls->session_reused = gnutls->session_data_override;
}
static void
g_tls_client_connection_gnutls_class_init (GTlsClientConnectionGnutlsClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GTlsConnectionBaseClass *base_class = G_TLS_CONNECTION_BASE_CLASS (klass);
GTlsConnectionGnutlsClass *gnutls_class = G_TLS_CONNECTION_GNUTLS_CLASS (klass);
gobject_class->get_property = g_tls_client_connection_gnutls_get_property;
gobject_class->set_property = g_tls_client_connection_gnutls_set_property;
gobject_class->finalize = g_tls_client_connection_gnutls_finalize;
base_class->prepare_handshake = g_tls_client_connection_gnutls_prepare_handshake;
base_class->complete_handshake = g_tls_client_connection_gnutls_complete_handshake;
gnutls_class->handshake_thread_retrieve_function = g_tls_client_connection_gnutls_handshake_thread_retrieve_function;
g_object_class_override_property (gobject_class, PROP_VALIDATION_FLAGS, "validation-flags");
g_object_class_override_property (gobject_class, PROP_SERVER_IDENTITY, "server-identity");
g_object_class_override_property (gobject_class, PROP_USE_SSL3, "use-ssl3");
g_object_class_override_property (gobject_class, PROP_ACCEPTED_CAS, "accepted-cas");
g_object_class_override_property (gobject_class, PROP_SESSION_REUSED, "session-reused");
g_object_class_override_property (gobject_class, PROP_SESSION_RESUMPTION_ENABLED, "session-resumption-enabled");
}
static void
g_tls_client_connection_gnutls_client_connection_interface_init (GTlsClientConnectionInterface *iface)
{
iface->copy_session_state = g_tls_client_connection_gnutls_copy_session_state;
}
static void
g_tls_client_connection_gnutls_initable_interface_init (GInitableIface *iface)
{
g_tls_client_connection_gnutls_parent_initable_iface = g_type_interface_peek_parent (iface);
iface->init = g_tls_client_connection_gnutls_initable_init;
}
static void
g_tls_client_connection_gnutls_dtls_client_connection_interface_init (GDtlsClientConnectionInterface *iface)
{
/* Nothing here. */
}
|