2 * empathy-tls-verifier.c - Source for EmpathyTLSVerifier
3 * Copyright (C) 2010 Collabora Ltd.
4 * @author Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
5 * @author Stef Walter <stefw@collabora.co.uk>
7 * This library 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 library 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
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <gnutls/gnutls.h>
25 #include <gnutls/x509.h>
27 #include <telepathy-glib/util.h>
29 #include "empathy-tls-verifier.h"
33 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
34 #include "empathy-debug.h"
35 #include "empathy-utils.h"
37 G_DEFINE_TYPE (EmpathyTLSVerifier, empathy_tls_verifier,
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTLSVerifier);
43 PROP_TLS_CERTIFICATE = 1,
50 EmpathyTLSCertificate *certificate;
53 GSimpleAsyncResult *verify_result;
57 } EmpathyTLSVerifierPriv;
60 verification_output_to_reason (gint res,
62 EmpTLSCertificateRejectReason *reason)
64 gboolean retval = TRUE;
66 g_assert (reason != NULL);
68 if (res != GNUTLS_E_SUCCESS)
72 /* the certificate is not structurally valid */
75 case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
76 *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED;
78 case GNUTLS_E_CONSTRAINT_ERROR:
79 *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_LIMIT_EXCEEDED;
82 *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
89 /* the certificate is structurally valid, check for other errors. */
90 if (verify_output & GNUTLS_CERT_INVALID)
94 if (verify_output & GNUTLS_CERT_SIGNER_NOT_FOUND)
95 *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED;
96 else if (verify_output & GNUTLS_CERT_SIGNER_NOT_CA)
97 *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED;
98 else if (verify_output & GNUTLS_CERT_INSECURE_ALGORITHM)
99 *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_INSECURE;
100 else if (verify_output & GNUTLS_CERT_NOT_ACTIVATED)
101 *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_NOT_ACTIVATED;
102 else if (verify_output & GNUTLS_CERT_EXPIRED)
103 *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_EXPIRED;
105 *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
115 build_certificate_list_for_gnutls (GcrCertificateChain *chain,
116 gnutls_x509_crt_t **list,
118 gnutls_x509_crt_t **anchors,
121 GcrCertificate *cert;
123 gnutls_x509_crt_t *retval;
124 gnutls_x509_crt_t gcert;
125 gnutls_datum_t datum;
131 g_assert (n_anchors);
133 *list = *anchors = NULL;
134 *n_list = *n_anchors = 0;
136 length = gcr_certificate_chain_get_length (chain);
137 retval = g_malloc0 (sizeof (gnutls_x509_crt_t) * length);
139 /* Convert the main body of the chain to gnutls */
140 for (idx = 0; idx < length; ++idx)
142 cert = gcr_certificate_chain_get_certificate (chain, idx);
143 datum.data = (gpointer)gcr_certificate_get_der_data (cert, &n_data);
146 gnutls_x509_crt_init (&gcert);
147 if (gnutls_x509_crt_import (gcert, &datum, GNUTLS_X509_FMT_DER) < 0)
148 g_return_if_reached ();
156 /* See if we have an anchor */
157 if (gcr_certificate_chain_get_status (chain) ==
158 GCR_CERTIFICATE_CHAIN_ANCHORED)
160 cert = gcr_certificate_chain_get_anchor (chain);
161 g_return_if_fail (cert);
163 datum.data = (gpointer)gcr_certificate_get_der_data (cert, &n_data);
166 gnutls_x509_crt_init (&gcert);
167 if (gnutls_x509_crt_import (gcert, &datum, GNUTLS_X509_FMT_DER) < 0)
168 g_return_if_reached ();
170 retval = g_malloc0 (sizeof (gnutls_x509_crt_t) * 1);
178 free_certificate_list_for_gnutls (gnutls_x509_crt_t *list,
183 for (idx = 0; idx < n_list; idx++)
184 gnutls_x509_crt_deinit (list[idx]);
189 complete_verification (EmpathyTLSVerifier *self)
191 EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
193 DEBUG ("Verification successful, completing...");
195 g_simple_async_result_complete_in_idle (priv->verify_result);
197 tp_clear_object (&priv->verify_result);
201 abort_verification (EmpathyTLSVerifier *self,
202 EmpTLSCertificateRejectReason reason)
204 EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
206 DEBUG ("Verification error %u, aborting...", reason);
208 g_simple_async_result_set_error (priv->verify_result,
209 G_IO_ERROR, reason, "TLS verification failed with reason %u",
211 g_simple_async_result_complete_in_idle (priv->verify_result);
213 tp_clear_object (&priv->verify_result);
217 debug_certificate (GcrCertificate *cert)
219 gchar *subject = gcr_certificate_get_subject_dn (cert);
220 DEBUG ("Certificate: %s", subject);
225 debug_certificate_chain (GcrCertificateChain *chain)
227 GEnumClass *enum_class;
228 GEnumValue *enum_value;
230 GcrCertificate *cert;
232 enum_class = G_ENUM_CLASS
233 (g_type_class_peek (GCR_TYPE_CERTIFICATE_CHAIN_STATUS));
234 enum_value = g_enum_get_value (enum_class,
235 gcr_certificate_chain_get_status (chain));
236 length = gcr_certificate_chain_get_length (chain);
237 DEBUG ("Certificate chain: length %u status %s",
238 length, enum_value ? enum_value->value_nick : "XXX");
240 for (idx = 0; idx < length; ++idx)
242 cert = gcr_certificate_chain_get_certificate (chain, idx);
243 debug_certificate (cert);
248 perform_verification (EmpathyTLSVerifier *self,
249 GcrCertificateChain *chain)
251 gboolean ret = FALSE;
252 EmpTLSCertificateRejectReason reason =
253 EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
254 gnutls_x509_crt_t *list, *anchors;
255 guint n_list, n_anchors;
258 EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
260 DEBUG ("Performing verification");
261 debug_certificate_chain (chain);
264 * If the first certificate is an pinned certificate then we completely
265 * ignore the rest of the verification process.
267 if (gcr_certificate_chain_get_status (chain) == GCR_CERTIFICATE_CHAIN_PINNED)
269 DEBUG ("Found pinned certificate for %s", priv->hostname);
270 complete_verification (self);
274 build_certificate_list_for_gnutls (chain, &list, &n_list,
275 &anchors, &n_anchors);
276 if (list == NULL || n_list == 0) {
277 g_warn_if_reached ();
278 abort_verification (self, EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN);
283 res = gnutls_x509_crt_list_verify (list, n_list, anchors, n_anchors,
284 NULL, 0, 0, &verify_output);
285 ret = verification_output_to_reason (res, verify_output, &reason);
287 DEBUG ("Certificate verification gave result %d with reason %u", ret,
291 abort_verification (self, reason);
295 /* now check if the certificate matches the hostname. */
296 if (gnutls_x509_crt_check_hostname (list[0], priv->hostname) == 0)
298 gchar *certified_hostname;
300 certified_hostname = empathy_get_x509_certificate_hostname (list[0]);
301 tp_asv_set_string (priv->details,
302 "expected-hostname", priv->hostname);
303 tp_asv_set_string (priv->details,
304 "certificate-hostname", certified_hostname);
306 DEBUG ("Hostname mismatch: got %s but expected %s",
307 certified_hostname, priv->hostname);
309 g_free (certified_hostname);
310 abort_verification (self,
311 EMP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH);
315 DEBUG ("Hostname matched");
316 complete_verification (self);
319 free_certificate_list_for_gnutls (list, n_list);
320 free_certificate_list_for_gnutls (anchors, n_anchors);
324 perform_verification_cb (GObject *object,
328 GError *error = NULL;
330 GcrCertificateChain *chain = GCR_CERTIFICATE_CHAIN (object);
331 EmpathyTLSVerifier *self = EMPATHY_TLS_VERIFIER (user_data);
333 /* Even if building the chain fails, try verifying what we have */
334 if (!gcr_certificate_chain_build_finish (chain, res, &error))
336 DEBUG ("Building of certificate chain failed: %s", error->message);
337 g_clear_error (&error);
340 perform_verification (self, chain);
342 /* Matches ref when staring chain build */
343 g_object_unref (self);
347 empathy_tls_verifier_get_property (GObject *object,
352 EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
356 case PROP_TLS_CERTIFICATE:
357 g_value_set_object (value, priv->certificate);
360 g_value_set_string (value, priv->hostname);
363 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
369 empathy_tls_verifier_set_property (GObject *object,
374 EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
378 case PROP_TLS_CERTIFICATE:
379 priv->certificate = g_value_dup_object (value);
382 priv->hostname = g_value_dup_string (value);
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
391 empathy_tls_verifier_dispose (GObject *object)
393 EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
395 if (priv->dispose_run)
398 priv->dispose_run = TRUE;
400 tp_clear_object (&priv->certificate);
402 G_OBJECT_CLASS (empathy_tls_verifier_parent_class)->dispose (object);
406 empathy_tls_verifier_finalize (GObject *object)
408 EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
410 DEBUG ("%p", object);
412 tp_clear_boxed (G_TYPE_HASH_TABLE, &priv->details);
413 g_free (priv->hostname);
415 G_OBJECT_CLASS (empathy_tls_verifier_parent_class)->finalize (object);
419 empathy_tls_verifier_init (EmpathyTLSVerifier *self)
421 EmpathyTLSVerifierPriv *priv;
423 priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
424 EMPATHY_TYPE_TLS_VERIFIER, EmpathyTLSVerifierPriv);
425 priv->details = tp_asv_new (NULL, NULL);
429 empathy_tls_verifier_class_init (EmpathyTLSVerifierClass *klass)
432 GObjectClass *oclass = G_OBJECT_CLASS (klass);
434 g_type_class_add_private (klass, sizeof (EmpathyTLSVerifierPriv));
436 oclass->set_property = empathy_tls_verifier_set_property;
437 oclass->get_property = empathy_tls_verifier_get_property;
438 oclass->finalize = empathy_tls_verifier_finalize;
439 oclass->dispose = empathy_tls_verifier_dispose;
441 pspec = g_param_spec_object ("certificate", "The EmpathyTLSCertificate",
442 "The EmpathyTLSCertificate to be verified.",
443 EMPATHY_TYPE_TLS_CERTIFICATE,
444 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
445 g_object_class_install_property (oclass, PROP_TLS_CERTIFICATE, pspec);
447 pspec = g_param_spec_string ("hostname", "The hostname",
448 "The hostname which should be certified by the certificate.",
450 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
451 g_object_class_install_property (oclass, PROP_HOSTNAME, pspec);
455 empathy_tls_verifier_new (EmpathyTLSCertificate *certificate,
456 const gchar *hostname)
458 g_assert (EMPATHY_IS_TLS_CERTIFICATE (certificate));
459 g_assert (hostname != NULL);
461 return g_object_new (EMPATHY_TYPE_TLS_VERIFIER,
462 "certificate", certificate,
463 "hostname", hostname,
468 empathy_tls_verifier_verify_async (EmpathyTLSVerifier *self,
469 GAsyncReadyCallback callback,
472 GcrCertificateChain *chain;
473 GcrCertificate *cert;
474 GPtrArray *cert_data = NULL;
477 EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
479 DEBUG ("Starting verification");
481 g_return_if_fail (priv->verify_result == NULL);
483 g_object_get (priv->certificate, "cert-data", &cert_data, NULL);
484 g_return_if_fail (cert_data);
486 priv->verify_result = g_simple_async_result_new (G_OBJECT (self),
487 callback, user_data, NULL);
489 /* Create a certificate chain */
490 chain = gcr_certificate_chain_new ();
491 for (idx = 0; idx < cert_data->len; ++idx) {
492 data = g_ptr_array_index (cert_data, idx);
493 cert = gcr_simple_certificate_new (data->data, data->len);
494 gcr_certificate_chain_add (chain, cert);
495 g_object_unref (cert);
498 gcr_certificate_chain_build_async (chain, GCR_PURPOSE_CLIENT_AUTH, priv->hostname, 0,
499 NULL, perform_verification_cb, g_object_ref (self));
501 g_object_unref (chain);
502 g_boxed_free (TP_ARRAY_TYPE_UCHAR_ARRAY_LIST, cert_data);
506 empathy_tls_verifier_verify_finish (EmpathyTLSVerifier *self,
508 EmpTLSCertificateRejectReason *reason,
509 GHashTable **details,
512 EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
514 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
518 *reason = (*error)->code;
522 *details = tp_asv_new (NULL, NULL);
523 tp_g_hash_table_update (*details, priv->details,
524 (GBoxedCopyFunc) g_strdup,
525 (GBoxedCopyFunc) tp_g_value_slice_dup);
532 *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
538 empathy_tls_verifier_store_exception (EmpathyTLSVerifier *self)
541 GcrCertificate *cert;
542 GPtrArray *cert_data = NULL;
543 GError *error = NULL;
544 EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
546 g_object_get (priv->certificate, "cert-data", &cert_data, NULL);
547 g_return_if_fail (cert_data);
551 DEBUG ("No certificate to pin.");
555 /* The first certificate in the chain is for the host */
556 data = g_ptr_array_index (cert_data, 0);
557 cert = gcr_simple_certificate_new ((gpointer)data->data, data->len);
559 DEBUG ("Storing pinned certificate:");
560 debug_certificate (cert);
562 if (!gcr_trust_add_pinned_certificate (cert, GCR_PURPOSE_CLIENT_AUTH,
563 priv->hostname, NULL, &error))
564 DEBUG ("Can't store the pinned certificate: %s", error->message);
566 g_object_unref (cert);
567 g_boxed_free (TP_ARRAY_TYPE_UCHAR_ARRAY_LIST, cert_data);