1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Red Hat, Inc
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include <gnutls/gnutls.h>
24 #include <gnutls/x509.h>
27 #include "gtlscertificate-gnutls.h"
28 #include <glib/gi18n-lib.h>
30 static void g_tls_certificate_gnutls_initable_iface_init (GInitableIface *iface);
32 G_DEFINE_TYPE_WITH_CODE (GTlsCertificateGnutls, g_tls_certificate_gnutls, G_TYPE_TLS_CERTIFICATE,
33 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
34 g_tls_certificate_gnutls_initable_iface_init);)
47 struct _GTlsCertificateGnutlsPrivate
49 gnutls_x509_crt_t cert;
50 gnutls_x509_privkey_t key;
52 GTlsCertificateGnutls *issuer;
54 GError *construct_error;
61 g_tls_certificate_gnutls_finalize (GObject *object)
63 GTlsCertificateGnutls *gnutls = G_TLS_CERTIFICATE_GNUTLS (object);
65 gnutls_x509_crt_deinit (gnutls->priv->cert);
66 gnutls_x509_privkey_deinit (gnutls->priv->key);
68 if (gnutls->priv->issuer)
69 g_object_unref (gnutls->priv->issuer);
71 g_clear_error (&gnutls->priv->construct_error);
73 G_OBJECT_CLASS (g_tls_certificate_gnutls_parent_class)->finalize (object);
77 g_tls_certificate_gnutls_get_property (GObject *object,
82 GTlsCertificateGnutls *gnutls = G_TLS_CERTIFICATE_GNUTLS (object);
83 GByteArray *certificate;
84 char *certificate_pem;
90 case PROP_CERTIFICATE:
92 status = gnutls_x509_crt_export (gnutls->priv->cert,
95 if (status != GNUTLS_E_SHORT_MEMORY_BUFFER)
99 certificate = g_byte_array_sized_new (size);
100 certificate->len = size;
101 status = gnutls_x509_crt_export (gnutls->priv->cert,
103 certificate->data, &size);
106 g_byte_array_free (certificate, TRUE);
110 g_value_take_boxed (value, certificate);
113 case PROP_CERTIFICATE_PEM:
115 status = gnutls_x509_crt_export (gnutls->priv->cert,
118 if (status != GNUTLS_E_SHORT_MEMORY_BUFFER)
119 certificate_pem = NULL;
122 certificate_pem = g_malloc (size);
123 status = gnutls_x509_crt_export (gnutls->priv->cert,
125 certificate_pem, &size);
128 g_free (certificate_pem);
129 certificate_pem = NULL;
132 g_value_take_string (value, certificate_pem);
136 g_value_set_object (value, gnutls->priv->issuer);
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145 g_tls_certificate_gnutls_set_property (GObject *object,
150 GTlsCertificateGnutls *gnutls = G_TLS_CERTIFICATE_GNUTLS (object);
158 case PROP_CERTIFICATE:
159 bytes = g_value_get_boxed (value);
162 g_return_if_fail (gnutls->priv->have_cert == FALSE);
163 data.data = bytes->data;
164 data.size = bytes->len;
165 status = gnutls_x509_crt_import (gnutls->priv->cert, &data,
166 GNUTLS_X509_FMT_DER);
168 gnutls->priv->have_cert = TRUE;
169 else if (!gnutls->priv->construct_error)
171 gnutls->priv->construct_error =
172 g_error_new (G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
173 _("Could not parse DER certificate: %s"),
174 gnutls_strerror (status));
179 case PROP_CERTIFICATE_PEM:
180 string = g_value_get_string (value);
183 g_return_if_fail (gnutls->priv->have_cert == FALSE);
184 data.data = (void *)string;
185 data.size = strlen (string);
186 status = gnutls_x509_crt_import (gnutls->priv->cert, &data,
187 GNUTLS_X509_FMT_PEM);
189 gnutls->priv->have_cert = TRUE;
190 else if (!gnutls->priv->construct_error)
192 gnutls->priv->construct_error =
193 g_error_new (G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
194 _("Could not parse PEM certificate: %s"),
195 gnutls_strerror (status));
199 case PROP_PRIVATE_KEY:
200 bytes = g_value_get_boxed (value);
203 g_return_if_fail (gnutls->priv->have_key == FALSE);
204 data.data = bytes->data;
205 data.size = bytes->len;
206 status = gnutls_x509_privkey_import (gnutls->priv->key, &data,
207 GNUTLS_X509_FMT_DER);
209 gnutls->priv->have_key = TRUE;
210 else if (!gnutls->priv->construct_error)
212 gnutls->priv->construct_error =
213 g_error_new (G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
214 _("Could not parse DER private key: %s"),
215 gnutls_strerror (status));
219 case PROP_PRIVATE_KEY_PEM:
220 string = g_value_get_string (value);
223 g_return_if_fail (gnutls->priv->have_key == FALSE);
224 data.data = (void *)string;
225 data.size = strlen (string);
226 status = gnutls_x509_privkey_import (gnutls->priv->key, &data,
227 GNUTLS_X509_FMT_PEM);
229 gnutls->priv->have_key = TRUE;
230 else if (!gnutls->priv->construct_error)
232 gnutls->priv->construct_error =
233 g_error_new (G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
234 _("Could not parse PEM private key: %s"),
235 gnutls_strerror (status));
240 gnutls->priv->issuer = g_value_dup_object (value);
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249 g_tls_certificate_gnutls_init (GTlsCertificateGnutls *gnutls)
251 gnutls->priv = G_TYPE_INSTANCE_GET_PRIVATE (gnutls,
252 G_TYPE_TLS_CERTIFICATE_GNUTLS,
253 GTlsCertificateGnutlsPrivate);
255 gnutls_x509_crt_init (&gnutls->priv->cert);
256 gnutls_x509_privkey_init (&gnutls->priv->key);
260 g_tls_certificate_gnutls_initable_init (GInitable *initable,
261 GCancellable *cancellable,
264 GTlsCertificateGnutls *gnutls = G_TLS_CERTIFICATE_GNUTLS (initable);
266 if (gnutls->priv->construct_error)
268 g_propagate_error (error, gnutls->priv->construct_error);
269 gnutls->priv->construct_error = NULL;
272 else if (!gnutls->priv->have_cert)
274 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
275 _("No certificate data provided"));
282 static GTlsCertificateFlags
283 g_tls_certificate_gnutls_verify (GTlsCertificate *cert,
284 GSocketConnectable *identity,
285 GTlsCertificate *trusted_ca)
287 GTlsCertificateGnutls *cert_gnutls;
289 gnutls_x509_crt_t *chain;
290 GTlsCertificateFlags gtls_flags;
293 cert_gnutls = G_TLS_CERTIFICATE_GNUTLS (cert);
294 for (num_certs = 0; cert_gnutls; cert_gnutls = cert_gnutls->priv->issuer)
296 chain = g_new (gnutls_x509_crt_t, num_certs);
297 cert_gnutls = G_TLS_CERTIFICATE_GNUTLS (cert);
298 for (i = 0; cert_gnutls; cert_gnutls = cert_gnutls->priv->issuer, i++)
299 chain[i] = cert_gnutls->priv->cert;
303 gnutls_x509_crt_t ca;
307 ca = G_TLS_CERTIFICATE_GNUTLS (trusted_ca)->priv->cert;
308 status = gnutls_x509_crt_list_verify (chain, num_certs,
311 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
316 return G_TLS_CERTIFICATE_GENERIC_ERROR;
319 gtls_flags = g_tls_certificate_gnutls_convert_flags (gnutls_flags);
324 /* We have to check these ourselves since gnutls_x509_crt_list_verify
325 * won't bother if it gets an UNKNOWN_CA.
328 for (i = 0; i < num_certs; i++)
330 t = gnutls_x509_crt_get_activation_time (chain[i]);
331 if (t == (time_t) -1 || t > now)
332 gtls_flags |= G_TLS_CERTIFICATE_NOT_ACTIVATED;
334 t = gnutls_x509_crt_get_expiration_time (chain[i]);
335 if (t == (time_t) -1 || t < now)
336 gtls_flags |= G_TLS_CERTIFICATE_EXPIRED;
342 gtls_flags |= g_tls_certificate_gnutls_verify_identity (G_TLS_CERTIFICATE_GNUTLS (cert), identity);
348 g_tls_certificate_gnutls_class_init (GTlsCertificateGnutlsClass *klass)
350 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
351 GTlsCertificateClass *certificate_class = G_TLS_CERTIFICATE_CLASS (klass);
353 g_type_class_add_private (klass, sizeof (GTlsCertificateGnutlsPrivate));
355 gobject_class->get_property = g_tls_certificate_gnutls_get_property;
356 gobject_class->set_property = g_tls_certificate_gnutls_set_property;
357 gobject_class->finalize = g_tls_certificate_gnutls_finalize;
359 certificate_class->verify = g_tls_certificate_gnutls_verify;
361 g_object_class_override_property (gobject_class, PROP_CERTIFICATE, "certificate");
362 g_object_class_override_property (gobject_class, PROP_CERTIFICATE_PEM, "certificate-pem");
363 g_object_class_override_property (gobject_class, PROP_PRIVATE_KEY, "private-key");
364 g_object_class_override_property (gobject_class, PROP_PRIVATE_KEY_PEM, "private-key-pem");
365 g_object_class_override_property (gobject_class, PROP_ISSUER, "issuer");
369 g_tls_certificate_gnutls_initable_iface_init (GInitableIface *iface)
371 iface->init = g_tls_certificate_gnutls_initable_init;
375 g_tls_certificate_gnutls_new (const gnutls_datum *datum,
376 GTlsCertificate *issuer)
378 GTlsCertificateGnutls *gnutls;
380 gnutls = g_object_new (G_TYPE_TLS_CERTIFICATE_GNUTLS,
383 if (gnutls_x509_crt_import (gnutls->priv->cert, datum,
384 GNUTLS_X509_FMT_DER) == 0)
385 gnutls->priv->have_cert = TRUE;
387 return G_TLS_CERTIFICATE (gnutls);
390 const gnutls_x509_crt_t
391 g_tls_certificate_gnutls_get_cert (GTlsCertificateGnutls *gnutls)
393 return gnutls->priv->cert;
396 const gnutls_x509_privkey_t
397 g_tls_certificate_gnutls_get_key (GTlsCertificateGnutls *gnutls)
399 return gnutls->priv->key;
403 g_tls_certificate_gnutls_copy_cert (GTlsCertificateGnutls *gnutls)
405 gnutls_x509_crt_t cert;
410 gnutls_x509_crt_export (gnutls->priv->cert, GNUTLS_X509_FMT_DER,
412 data.data = g_malloc (size);
414 gnutls_x509_crt_export (gnutls->priv->cert, GNUTLS_X509_FMT_DER,
417 gnutls_x509_crt_init (&cert);
418 gnutls_x509_crt_import (cert, &data, GNUTLS_X509_FMT_DER);
424 gnutls_x509_privkey_t
425 g_tls_certificate_gnutls_copy_key (GTlsCertificateGnutls *gnutls)
427 gnutls_x509_privkey_t key;
429 gnutls_x509_privkey_init (&key);
430 gnutls_x509_privkey_cpy (key, gnutls->priv->key);
434 static const struct {
436 GTlsCertificateFlags gtls_flag;
438 { GNUTLS_CERT_SIGNER_NOT_FOUND | GNUTLS_CERT_SIGNER_NOT_CA, G_TLS_CERTIFICATE_UNKNOWN_CA },
439 { GNUTLS_CERT_NOT_ACTIVATED, G_TLS_CERTIFICATE_NOT_ACTIVATED },
440 { GNUTLS_CERT_EXPIRED, G_TLS_CERTIFICATE_EXPIRED },
441 { GNUTLS_CERT_REVOKED, G_TLS_CERTIFICATE_REVOKED },
442 { GNUTLS_CERT_INSECURE_ALGORITHM, G_TLS_CERTIFICATE_INSECURE }
444 static const int flags_map_size = G_N_ELEMENTS (flags_map);
447 g_tls_certificate_gnutls_convert_flags (guint gnutls_flags)
450 GTlsCertificateFlags gtls_flags;
452 /* Convert GNUTLS status to GTlsCertificateFlags. GNUTLS sets
453 * GNUTLS_CERT_INVALID if it sets any other flag, so we want to
454 * strip that out unless it's the only flag set. Then we convert
455 * specific flags we recognize, and if there are any flags left over
456 * at the end, we add G_TLS_CERTIFICATE_GENERIC_ERROR.
460 if (gnutls_flags != GNUTLS_CERT_INVALID)
461 gnutls_flags = gnutls_flags & ~GNUTLS_CERT_INVALID;
462 for (i = 0; i < flags_map_size && gnutls_flags != 0; i++)
464 if (gnutls_flags & flags_map[i].gnutls_flag)
466 gnutls_flags &= ~flags_map[i].gnutls_flag;
467 gtls_flags |= flags_map[i].gtls_flag;
471 gtls_flags |= G_TLS_CERTIFICATE_GENERIC_ERROR;
477 g_tls_certificate_gnutls_verify_identity (GTlsCertificateGnutls *gnutls,
478 GSocketConnectable *identity)
480 const char *hostname;
482 if (G_IS_NETWORK_ADDRESS (identity))
483 hostname = g_network_address_get_hostname (G_NETWORK_ADDRESS (identity));
484 else if (G_IS_NETWORK_SERVICE (identity))
485 hostname = g_network_service_get_domain (G_NETWORK_SERVICE (identity));
491 if (gnutls_x509_crt_check_hostname (gnutls->priv->cert, hostname))
495 /* FIXME: check sRVName and uniformResourceIdentifier
496 * subjectAltNames, if appropriate for @identity.
499 return G_TLS_CERTIFICATE_BAD_IDENTITY;
503 g_tls_certificate_gnutls_set_issuer (GTlsCertificateGnutls *gnutls,
504 GTlsCertificateGnutls *issuer)
506 g_return_if_fail (G_IS_TLS_CERTIFICATE_GNUTLS (gnutls));
507 g_return_if_fail (!issuer || G_IS_TLS_CERTIFICATE_GNUTLS (issuer));
510 g_object_ref (issuer);
511 if (gnutls->priv->issuer)
512 g_object_unref (gnutls->priv->issuer);
513 gnutls->priv->issuer = issuer;
514 g_object_notify (G_OBJECT (gnutls), "issuer");