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 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright 2010 Collabora, Ltd
*
* 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 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/>.
*
* Author: Stef Walter <stefw@collabora.co.uk>
*/
#include "config.h"
#include "gtlsfiledatabase-gnutls.h"
#include <gio/gio.h>
#include <glib/gi18n-lib.h>
#include <gnutls/x509.h>
static void g_tls_file_database_gnutls_file_database_interface_init (GTlsFileDatabaseInterface *iface);
static void g_tls_file_database_gnutls_initable_interface_init (GInitableIface *iface);
G_DEFINE_TYPE_WITH_CODE (GTlsFileDatabaseGnutls, g_tls_file_database_gnutls, G_TYPE_TLS_DATABASE_GNUTLS,
G_IMPLEMENT_INTERFACE (G_TYPE_TLS_FILE_DATABASE,
g_tls_file_database_gnutls_file_database_interface_init);
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
g_tls_file_database_gnutls_initable_interface_init);
);
enum
{
PROP_0,
PROP_ANCHORS,
};
struct _GTlsFileDatabaseGnutlsPrivate
{
/* read-only after construct */
gchar *anchor_filename;
/* protected by mutex */
GMutex mutex;
/*
* These are hash tables of GBytes -> GPtrArray<GBytes>. The values of
* the ptr array are full DER encoded certificate values. The keys are byte
* arrays containing either subject DNs, issuer DNs, or full DER encoded certs
*/
GHashTable *subjects;
GHashTable *issuers;
/*
* This is a table of GBytes -> GBytes. The values and keys are
* DER encoded certificate values.
*/
GHashTable *complete;
/*
* This is a table of gchar * -> GPtrArray<GBytes>. The values of
* the ptr array are full DER encoded certificate values. The keys are the
* string handles. This array is populated on demand.
*/
GHashTable *handles;
};
static GHashTable *
bytes_multi_table_new (void)
{
return g_hash_table_new_full (g_bytes_hash, g_bytes_equal,
(GDestroyNotify)g_bytes_unref,
(GDestroyNotify)g_ptr_array_unref);
}
static void
bytes_multi_table_insert (GHashTable *table,
GBytes *key,
GBytes *value)
{
GPtrArray *multi;
multi = g_hash_table_lookup (table, key);
if (multi == NULL)
{
multi = g_ptr_array_new_with_free_func ((GDestroyNotify)g_bytes_unref);
g_hash_table_insert (table, g_bytes_ref (key), multi);
}
g_ptr_array_add (multi, g_bytes_ref (value));
}
static GBytes *
bytes_multi_table_lookup_ref_one (GHashTable *table,
GBytes *key)
{
GPtrArray *multi;
multi = g_hash_table_lookup (table, key);
if (multi == NULL)
return NULL;
g_assert (multi->len > 0);
return g_bytes_ref (multi->pdata[0]);
}
static GList *
bytes_multi_table_lookup_ref_all (GHashTable *table,
GBytes *key)
{
GPtrArray *multi;
GList *list = NULL;
gint i;
multi = g_hash_table_lookup (table, key);
if (multi == NULL)
return NULL;
for (i = 0; i < multi->len; i++)
list = g_list_prepend (list, g_bytes_ref (multi->pdata[i]));
return g_list_reverse (list);
}
static gchar *
create_handle_for_certificate (const gchar *filename,
GBytes *der)
{
gchar *bookmark;
gchar *uri_part;
gchar *uri;
/*
* Here we create a URI that looks like:
* file:///etc/ssl/certs/ca-certificates.crt#11b2641821252596420e468c275771f5e51022c121a17bd7a89a2f37b6336c8f
*/
uri_part = g_filename_to_uri (filename, NULL, NULL);
if (!uri_part)
return NULL;
bookmark = g_compute_checksum_for_bytes (G_CHECKSUM_SHA256, der);
uri = g_strconcat (uri_part, "#", bookmark, NULL);
g_free (bookmark);
g_free (uri_part);
return uri;
}
static GHashTable *
create_handles_array_unlocked (const gchar *filename,
GHashTable *complete)
{
GHashTable *handles;
GHashTableIter iter;
GBytes *der;
gchar *handle;
handles = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
(GDestroyNotify)g_bytes_unref);
g_hash_table_iter_init (&iter, complete);
while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&der))
{
handle = create_handle_for_certificate (filename, der);
if (handle != NULL)
g_hash_table_insert (handles, handle, g_bytes_ref (der));
}
return handles;
}
static gboolean
load_anchor_file (const gchar *filename,
GHashTable *subjects,
GHashTable *issuers,
GHashTable *complete,
GError **error)
{
GList *list, *l;
gnutls_x509_crt_t cert;
gnutls_datum_t dn;
GBytes *der;
GBytes *subject;
GBytes *issuer;
gint gerr;
GError *my_error = NULL;
list = g_tls_certificate_list_new_from_file (filename, &my_error);
if (my_error)
{
g_propagate_error (error, my_error);
return FALSE;
}
for (l = list; l; l = l->next)
{
cert = g_tls_certificate_gnutls_get_cert (l->data);
gerr = gnutls_x509_crt_get_raw_dn (cert, &dn);
if (gerr < 0)
{
g_warning ("failed to get subject of anchor certificate: %s",
gnutls_strerror (gerr));
continue;
}
subject = g_bytes_new_with_free_func (dn.data, dn.size, gnutls_free, dn.data);
gerr = gnutls_x509_crt_get_raw_issuer_dn (cert, &dn);
if (gerr < 0)
{
g_warning ("failed to get subject of anchor certificate: %s",
gnutls_strerror (gerr));
continue;
}
issuer = g_bytes_new_with_free_func (dn.data, dn.size, gnutls_free, dn.data);
der = g_tls_certificate_gnutls_get_bytes (l->data);
g_return_val_if_fail (der != NULL, FALSE);
/* Three different ways of looking up same certificate */
bytes_multi_table_insert (subjects, subject, der);
bytes_multi_table_insert (issuers, issuer, der);
g_hash_table_insert (complete, g_bytes_ref (der),
g_bytes_ref (der));
g_bytes_unref (der);
g_bytes_unref (subject);
g_bytes_unref (issuer);
g_object_unref (l->data);
}
g_list_free (list);
return TRUE;
}
static void
g_tls_file_database_gnutls_finalize (GObject *object)
{
GTlsFileDatabaseGnutls *self = G_TLS_FILE_DATABASE_GNUTLS (object);
if (self->priv->subjects)
g_hash_table_destroy (self->priv->subjects);
self->priv->subjects = NULL;
if (self->priv->issuers)
g_hash_table_destroy (self->priv->issuers);
self->priv->issuers = NULL;
if (self->priv->complete)
g_hash_table_destroy (self->priv->complete);
self->priv->complete = NULL;
if (self->priv->handles)
g_hash_table_destroy (self->priv->handles);
self->priv->handles = NULL;
g_free (self->priv->anchor_filename);
self->priv->anchor_filename = NULL;
g_mutex_clear (&self->priv->mutex);
G_OBJECT_CLASS (g_tls_file_database_gnutls_parent_class)->finalize (object);
}
static void
g_tls_file_database_gnutls_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GTlsFileDatabaseGnutls *self = G_TLS_FILE_DATABASE_GNUTLS (object);
switch (prop_id)
{
case PROP_ANCHORS:
g_value_set_string (value, self->priv->anchor_filename);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
g_tls_file_database_gnutls_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GTlsFileDatabaseGnutls *self = G_TLS_FILE_DATABASE_GNUTLS (object);
gchar *anchor_path;
switch (prop_id)
{
case PROP_ANCHORS:
anchor_path = g_value_dup_string (value);
if (anchor_path && !g_path_is_absolute (anchor_path))
{
g_warning ("The anchor file name for used with a GTlsFileDatabase "
"must be an absolute path, and not relative: %s", anchor_path);
}
else
{
self->priv->anchor_filename = anchor_path;
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
g_tls_file_database_gnutls_init (GTlsFileDatabaseGnutls *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
G_TYPE_TLS_FILE_DATABASE_GNUTLS,
GTlsFileDatabaseGnutlsPrivate);
g_mutex_init (&self->priv->mutex);
}
static gchar*
g_tls_file_database_gnutls_create_certificate_handle (GTlsDatabase *database,
GTlsCertificate *certificate)
{
GTlsFileDatabaseGnutls *self = G_TLS_FILE_DATABASE_GNUTLS (database);
GBytes *der;
gboolean contains;
gchar *handle = NULL;
der = g_tls_certificate_gnutls_get_bytes (G_TLS_CERTIFICATE_GNUTLS (certificate));
g_return_val_if_fail (der != NULL, FALSE);
g_mutex_lock (&self->priv->mutex);
/* At the same time look up whether this certificate is in list */
contains = g_hash_table_lookup (self->priv->complete, der) ? TRUE : FALSE;
g_mutex_unlock (&self->priv->mutex);
/* Certificate is in the database */
if (contains)
handle = create_handle_for_certificate (self->priv->anchor_filename, der);
g_bytes_unref (der);
return handle;
}
static GTlsCertificate*
g_tls_file_database_gnutls_lookup_certificate_for_handle (GTlsDatabase *database,
const gchar *handle,
GTlsInteraction *interaction,
GTlsDatabaseLookupFlags flags,
GCancellable *cancellable,
GError **error)
{
GTlsFileDatabaseGnutls *self = G_TLS_FILE_DATABASE_GNUTLS (database);
GTlsCertificate *cert;
GBytes *der;
gnutls_datum_t datum;
gsize length;
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
if (!handle)
return NULL;
g_mutex_lock (&self->priv->mutex);
/* Create the handles table if not already done */
if (!self->priv->handles)
self->priv->handles = create_handles_array_unlocked (self->priv->anchor_filename,
self->priv->complete);
der = g_hash_table_lookup (self->priv->handles, handle);
if (der != NULL)
g_bytes_ref (der);
g_mutex_unlock (&self->priv->mutex);
if (der == NULL)
return NULL;
datum.data = (unsigned char *)g_bytes_get_data (der, &length);
datum.size = length;
if (g_cancellable_set_error_if_cancelled (cancellable, error))
cert = NULL;
else
cert = g_tls_certificate_gnutls_new (&datum, NULL);
g_bytes_unref (der);
return cert;
}
static gboolean
g_tls_file_database_gnutls_lookup_assertion (GTlsDatabaseGnutls *database,
GTlsCertificateGnutls *certificate,
GTlsDatabaseGnutlsAssertion assertion,
const gchar *purpose,
GSocketConnectable *identity,
GCancellable *cancellable,
GError **error)
{
GTlsFileDatabaseGnutls *self = G_TLS_FILE_DATABASE_GNUTLS (database);
GBytes *der = NULL;
gboolean contains;
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return FALSE;
/* We only have anchored certificate assertions here */
if (assertion != G_TLS_DATABASE_GNUTLS_ANCHORED_CERTIFICATE)
return FALSE;
/*
* TODO: We should be parsing any Extended Key Usage attributes and
* comparing them to the purpose.
*/
der = g_tls_certificate_gnutls_get_bytes (certificate);
g_mutex_lock (&self->priv->mutex);
contains = g_hash_table_lookup (self->priv->complete, der) ? TRUE : FALSE;
g_mutex_unlock (&self->priv->mutex);
g_bytes_unref (der);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return FALSE;
/* All certificates in our file are anchored certificates */
return contains;
}
static GTlsCertificate*
g_tls_file_database_gnutls_lookup_certificate_issuer (GTlsDatabase *database,
GTlsCertificate *certificate,
GTlsInteraction *interaction,
GTlsDatabaseLookupFlags flags,
GCancellable *cancellable,
GError **error)
{
GTlsFileDatabaseGnutls *self = G_TLS_FILE_DATABASE_GNUTLS (database);
gnutls_datum_t dn = { NULL, 0 };
GBytes *subject, *der;
gnutls_datum_t datum;
GTlsCertificate *issuer = NULL;
gnutls_x509_crt_t cert;
gsize length;
int gerr;
g_return_val_if_fail (G_IS_TLS_CERTIFICATE_GNUTLS (certificate), NULL);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
if (flags & G_TLS_DATABASE_LOOKUP_KEYPAIR)
return NULL;
/* Dig out the issuer of this certificate */
cert = g_tls_certificate_gnutls_get_cert (G_TLS_CERTIFICATE_GNUTLS (certificate));
gerr = gnutls_x509_crt_get_raw_issuer_dn (cert, &dn);
if (gerr < 0)
{
g_warning ("failed to get issuer of certificate: %s", gnutls_strerror (gerr));
return NULL;
}
subject = g_bytes_new_with_free_func (dn.data, dn.size, gnutls_free, dn.data);
/* Find the full DER value of the certificate */
g_mutex_lock (&self->priv->mutex);
der = bytes_multi_table_lookup_ref_one (self->priv->subjects, subject);
g_mutex_unlock (&self->priv->mutex);
g_bytes_unref (subject);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
{
issuer = NULL;
}
else if (der != NULL)
{
datum.data = (unsigned char *)g_bytes_get_data (der, &length);
datum.size = length;
issuer = g_tls_certificate_gnutls_new (&datum, NULL);
}
if (der != NULL)
g_bytes_unref (der);
return issuer;
}
static GList*
g_tls_file_database_gnutls_lookup_certificates_issued_by (GTlsDatabase *database,
GByteArray *issuer_raw_dn,
GTlsInteraction *interaction,
GTlsDatabaseLookupFlags flags,
GCancellable *cancellable,
GError **error)
{
GTlsFileDatabaseGnutls *self = G_TLS_FILE_DATABASE_GNUTLS (database);
GBytes *issuer;
gnutls_datum_t datum;
GList *issued = NULL;
GList *ders;
gsize length;
GList *l;
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return NULL;
/* We don't have any private keys here */
if (flags & G_TLS_DATABASE_LOOKUP_KEYPAIR)
return NULL;
issuer = g_bytes_new_static (issuer_raw_dn->data, issuer_raw_dn->len);
/* Find the full DER value of the certificate */
g_mutex_lock (&self->priv->mutex);
ders = bytes_multi_table_lookup_ref_all (self->priv->issuers, issuer);
g_mutex_unlock (&self->priv->mutex);
g_bytes_unref (issuer);
for (l = ders; l != NULL; l = g_list_next (l))
{
if (g_cancellable_set_error_if_cancelled (cancellable, error))
{
g_list_free_full (issued, g_object_unref);
issued = NULL;
break;
}
datum.data = (unsigned char *)g_bytes_get_data (l->data, &length);
datum.size = length;
issued = g_list_prepend (issued, g_tls_certificate_gnutls_new (&datum, NULL));
}
g_list_free_full (ders, (GDestroyNotify)g_bytes_unref);
return issued;
}
static void
g_tls_file_database_gnutls_class_init (GTlsFileDatabaseGnutlsClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GTlsDatabaseClass *database_class = G_TLS_DATABASE_CLASS (klass);
GTlsDatabaseGnutlsClass *gnutls_class = G_TLS_DATABASE_GNUTLS_CLASS (klass);
g_type_class_add_private (klass, sizeof (GTlsFileDatabaseGnutlsPrivate));
gobject_class->get_property = g_tls_file_database_gnutls_get_property;
gobject_class->set_property = g_tls_file_database_gnutls_set_property;
gobject_class->finalize = g_tls_file_database_gnutls_finalize;
database_class->create_certificate_handle = g_tls_file_database_gnutls_create_certificate_handle;
database_class->lookup_certificate_for_handle = g_tls_file_database_gnutls_lookup_certificate_for_handle;
database_class->lookup_certificate_issuer = g_tls_file_database_gnutls_lookup_certificate_issuer;
database_class->lookup_certificates_issued_by = g_tls_file_database_gnutls_lookup_certificates_issued_by;
gnutls_class->lookup_assertion = g_tls_file_database_gnutls_lookup_assertion;
g_object_class_override_property (gobject_class, PROP_ANCHORS, "anchors");
}
static void
g_tls_file_database_gnutls_file_database_interface_init (GTlsFileDatabaseInterface *iface)
{
}
static gboolean
g_tls_file_database_gnutls_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
GTlsFileDatabaseGnutls *self = G_TLS_FILE_DATABASE_GNUTLS (initable);
GHashTable *subjects, *issuers, *complete;
gboolean result;
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return FALSE;
subjects = bytes_multi_table_new ();
issuers = bytes_multi_table_new ();
complete = g_hash_table_new_full (g_bytes_hash, g_bytes_equal,
(GDestroyNotify)g_bytes_unref,
(GDestroyNotify)g_bytes_unref);
if (self->priv->anchor_filename)
result = load_anchor_file (self->priv->anchor_filename, subjects, issuers,
complete, error);
else
result = TRUE;
if (g_cancellable_set_error_if_cancelled (cancellable, error))
result = FALSE;
if (result)
{
g_mutex_lock (&self->priv->mutex);
if (!self->priv->subjects)
{
self->priv->subjects = subjects;
subjects = NULL;
}
if (!self->priv->issuers)
{
self->priv->issuers = issuers;
issuers = NULL;
}
if (!self->priv->complete)
{
self->priv->complete = complete;
complete = NULL;
}
g_mutex_unlock (&self->priv->mutex);
}
if (subjects != NULL)
g_hash_table_unref (subjects);
if (issuers != NULL)
g_hash_table_unref (issuers);
if (complete != NULL)
g_hash_table_unref (complete);
return result;
}
static void
g_tls_file_database_gnutls_initable_interface_init (GInitableIface *iface)
{
iface->init = g_tls_file_database_gnutls_initable_init;
}
|