account-settings: early return if SASL earlier
[empathy-mirror.git] / libempathy / empathy-tls-verifier.c
bloba9a4f3818f63267767243da8a0b48701d3fa53e9
1 /*
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
22 #include <config.h>
24 #include <gnutls/gnutls.h>
25 #include <gnutls/x509.h>
27 #include "empathy-tls-verifier.h"
29 #include <gcr/gcr.h>
31 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
32 #include "empathy-debug.h"
33 #include "empathy-utils.h"
35 G_DEFINE_TYPE (EmpathyTLSVerifier, empathy_tls_verifier,
36 G_TYPE_OBJECT)
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTLSVerifier);
40 enum {
41 PROP_TLS_CERTIFICATE = 1,
42 PROP_HOSTNAME,
43 PROP_REFERENCE_IDENTITIES,
45 LAST_PROPERTY,
48 typedef struct {
49 TpTLSCertificate *certificate;
50 gchar *hostname;
51 gchar **reference_identities;
53 GSimpleAsyncResult *verify_result;
54 GHashTable *details;
56 gboolean dispose_run;
57 } EmpathyTLSVerifierPriv;
59 static gboolean
60 verification_output_to_reason (gint res,
61 guint verify_output,
62 TpTLSCertificateRejectReason *reason)
64 gboolean retval = TRUE;
66 g_assert (reason != NULL);
68 if (res != GNUTLS_E_SUCCESS)
70 retval = FALSE;
72 /* the certificate is not structurally valid */
73 switch (res)
75 case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
76 *reason = TP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED;
77 break;
78 case GNUTLS_E_CONSTRAINT_ERROR:
79 *reason = TP_TLS_CERTIFICATE_REJECT_REASON_LIMIT_EXCEEDED;
80 break;
81 default:
82 *reason = TP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
83 break;
86 goto out;
89 /* the certificate is structurally valid, check for other errors. */
90 if (verify_output & GNUTLS_CERT_INVALID)
92 retval = FALSE;
94 if (verify_output & GNUTLS_CERT_SIGNER_NOT_FOUND)
95 *reason = TP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED;
96 else if (verify_output & GNUTLS_CERT_SIGNER_NOT_CA)
97 *reason = TP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED;
98 else if (verify_output & GNUTLS_CERT_INSECURE_ALGORITHM)
99 *reason = TP_TLS_CERTIFICATE_REJECT_REASON_INSECURE;
100 else if (verify_output & GNUTLS_CERT_NOT_ACTIVATED)
101 *reason = TP_TLS_CERTIFICATE_REJECT_REASON_NOT_ACTIVATED;
102 else if (verify_output & GNUTLS_CERT_EXPIRED)
103 *reason = TP_TLS_CERTIFICATE_REJECT_REASON_EXPIRED;
104 else
105 *reason = TP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
107 goto out;
110 out:
111 return retval;
114 static void
115 build_certificate_list_for_gnutls (GcrCertificateChain *chain,
116 gnutls_x509_crt_t **list,
117 guint *n_list,
118 gnutls_x509_crt_t **anchors,
119 guint *n_anchors)
121 GcrCertificate *cert;
122 guint idx, length;
123 gnutls_x509_crt_t *retval;
124 gnutls_x509_crt_t gcert;
125 gnutls_datum_t datum;
126 gsize n_data;
128 g_assert (list);
129 g_assert (n_list);
130 g_assert (anchors);
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);
144 datum.size = 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 ();
150 retval[idx] = gcert;
153 *list = retval;
154 *n_list = length;
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);
164 datum.size = 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);
171 retval[0] = gcert;
172 *anchors = retval;
173 *n_anchors = 1;
177 static void
178 free_certificate_list_for_gnutls (gnutls_x509_crt_t *list,
179 guint n_list)
181 guint idx;
183 for (idx = 0; idx < n_list; idx++)
184 gnutls_x509_crt_deinit (list[idx]);
185 g_free (list);
188 static void
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);
200 static void
201 abort_verification (EmpathyTLSVerifier *self,
202 TpTLSCertificateRejectReason 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",
210 reason);
211 g_simple_async_result_complete_in_idle (priv->verify_result);
213 tp_clear_object (&priv->verify_result);
216 static void
217 debug_certificate (GcrCertificate *cert)
219 gchar *subject = gcr_certificate_get_subject_dn (cert);
220 DEBUG ("Certificate: %s", subject);
221 g_free (subject);
224 static void
225 debug_certificate_chain (GcrCertificateChain *chain)
227 GEnumClass *enum_class;
228 GEnumValue *enum_value;
229 gint idx, length;
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);
247 static void
248 perform_verification (EmpathyTLSVerifier *self,
249 GcrCertificateChain *chain)
251 gboolean ret = FALSE;
252 TpTLSCertificateRejectReason reason =
253 TP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
254 gnutls_x509_crt_t *list, *anchors;
255 guint n_list, n_anchors;
256 guint verify_output;
257 gint res;
258 gint i;
259 gboolean matched = FALSE;
260 EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
262 DEBUG ("Performing verification");
263 debug_certificate_chain (chain);
265 list = anchors = NULL;
266 n_list = n_anchors = 0;
269 * If the first certificate is an pinned certificate then we completely
270 * ignore the rest of the verification process.
272 if (gcr_certificate_chain_get_status (chain) == GCR_CERTIFICATE_CHAIN_PINNED)
274 DEBUG ("Found pinned certificate for %s", priv->hostname);
275 complete_verification (self);
276 goto out;
279 build_certificate_list_for_gnutls (chain, &list, &n_list,
280 &anchors, &n_anchors);
281 if (list == NULL || n_list == 0) {
282 g_warn_if_reached ();
283 abort_verification (self, TP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN);
284 goto out;
287 verify_output = 0;
288 res = gnutls_x509_crt_list_verify (list, n_list, anchors, n_anchors,
289 NULL, 0, 0, &verify_output);
290 ret = verification_output_to_reason (res, verify_output, &reason);
292 DEBUG ("Certificate verification gave result %d with reason %u", ret,
293 reason);
295 if (!ret) {
296 abort_verification (self, reason);
297 goto out;
300 /* now check if the certificate matches one of the reference identities. */
301 if (priv->reference_identities != NULL)
303 for (i = 0, matched = FALSE; priv->reference_identities[i] != NULL; ++i)
305 if (gnutls_x509_crt_check_hostname (list[0],
306 priv->reference_identities[i]) == 1)
308 matched = TRUE;
309 break;
314 if (!matched)
316 gchar *certified_hostname;
318 certified_hostname = empathy_get_x509_certificate_hostname (list[0]);
319 tp_asv_set_string (priv->details,
320 "expected-hostname", priv->hostname);
321 tp_asv_set_string (priv->details,
322 "certificate-hostname", certified_hostname);
324 DEBUG ("Hostname mismatch: got %s but expected %s",
325 certified_hostname, priv->hostname);
327 g_free (certified_hostname);
328 abort_verification (self,
329 TP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH);
330 goto out;
333 DEBUG ("Hostname matched");
334 complete_verification (self);
336 out:
337 free_certificate_list_for_gnutls (list, n_list);
338 free_certificate_list_for_gnutls (anchors, n_anchors);
341 static void
342 perform_verification_cb (GObject *object,
343 GAsyncResult *res,
344 gpointer user_data)
346 GError *error = NULL;
348 GcrCertificateChain *chain = GCR_CERTIFICATE_CHAIN (object);
349 EmpathyTLSVerifier *self = EMPATHY_TLS_VERIFIER (user_data);
351 /* Even if building the chain fails, try verifying what we have */
352 if (!gcr_certificate_chain_build_finish (chain, res, &error))
354 DEBUG ("Building of certificate chain failed: %s", error->message);
355 g_clear_error (&error);
358 perform_verification (self, chain);
360 /* Matches ref when staring chain build */
361 g_object_unref (self);
364 static void
365 empathy_tls_verifier_get_property (GObject *object,
366 guint property_id,
367 GValue *value,
368 GParamSpec *pspec)
370 EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
372 switch (property_id)
374 case PROP_TLS_CERTIFICATE:
375 g_value_set_object (value, priv->certificate);
376 break;
377 case PROP_HOSTNAME:
378 g_value_set_string (value, priv->hostname);
379 break;
380 case PROP_REFERENCE_IDENTITIES:
381 g_value_set_boxed (value, priv->reference_identities);
382 break;
383 default:
384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
385 break;
389 static void
390 empathy_tls_verifier_set_property (GObject *object,
391 guint property_id,
392 const GValue *value,
393 GParamSpec *pspec)
395 EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
397 switch (property_id)
399 case PROP_TLS_CERTIFICATE:
400 priv->certificate = g_value_dup_object (value);
401 break;
402 case PROP_HOSTNAME:
403 priv->hostname = g_value_dup_string (value);
404 break;
405 case PROP_REFERENCE_IDENTITIES:
406 priv->reference_identities = g_value_dup_boxed (value);
407 break;
408 default:
409 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
410 break;
414 static void
415 empathy_tls_verifier_dispose (GObject *object)
417 EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
419 if (priv->dispose_run)
420 return;
422 priv->dispose_run = TRUE;
424 tp_clear_object (&priv->certificate);
426 G_OBJECT_CLASS (empathy_tls_verifier_parent_class)->dispose (object);
429 static void
430 empathy_tls_verifier_finalize (GObject *object)
432 EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
434 DEBUG ("%p", object);
436 tp_clear_boxed (G_TYPE_HASH_TABLE, &priv->details);
437 g_free (priv->hostname);
438 g_strfreev (priv->reference_identities);
440 G_OBJECT_CLASS (empathy_tls_verifier_parent_class)->finalize (object);
443 static void
444 empathy_tls_verifier_init (EmpathyTLSVerifier *self)
446 EmpathyTLSVerifierPriv *priv;
448 priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
449 EMPATHY_TYPE_TLS_VERIFIER, EmpathyTLSVerifierPriv);
450 priv->details = tp_asv_new (NULL, NULL);
453 static void
454 empathy_tls_verifier_class_init (EmpathyTLSVerifierClass *klass)
456 GParamSpec *pspec;
457 GObjectClass *oclass = G_OBJECT_CLASS (klass);
459 g_type_class_add_private (klass, sizeof (EmpathyTLSVerifierPriv));
461 oclass->set_property = empathy_tls_verifier_set_property;
462 oclass->get_property = empathy_tls_verifier_get_property;
463 oclass->finalize = empathy_tls_verifier_finalize;
464 oclass->dispose = empathy_tls_verifier_dispose;
466 pspec = g_param_spec_object ("certificate", "The TpTLSCertificate",
467 "The TpTLSCertificate to be verified.",
468 TP_TYPE_TLS_CERTIFICATE,
469 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
470 g_object_class_install_property (oclass, PROP_TLS_CERTIFICATE, pspec);
472 pspec = g_param_spec_string ("hostname", "The hostname",
473 "The hostname which is certified by the certificate.",
474 NULL,
475 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
476 g_object_class_install_property (oclass, PROP_HOSTNAME, pspec);
478 pspec = g_param_spec_boxed ("reference-identities",
479 "The reference identities",
480 "The certificate should certify one of these identities.",
481 G_TYPE_STRV,
482 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
483 g_object_class_install_property (oclass, PROP_REFERENCE_IDENTITIES, pspec);
486 EmpathyTLSVerifier *
487 empathy_tls_verifier_new (TpTLSCertificate *certificate,
488 const gchar *hostname,
489 const gchar **reference_identities)
491 g_assert (TP_IS_TLS_CERTIFICATE (certificate));
492 g_assert (hostname != NULL);
493 g_assert (reference_identities != NULL);
495 return g_object_new (EMPATHY_TYPE_TLS_VERIFIER,
496 "certificate", certificate,
497 "hostname", hostname,
498 "reference-identities", reference_identities,
499 NULL);
502 void
503 empathy_tls_verifier_verify_async (EmpathyTLSVerifier *self,
504 GAsyncReadyCallback callback,
505 gpointer user_data)
507 GcrCertificateChain *chain;
508 GcrCertificate *cert;
509 GPtrArray *cert_data;
510 GArray *data;
511 guint idx;
512 EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
514 DEBUG ("Starting verification");
516 g_return_if_fail (priv->verify_result == NULL);
518 cert_data = tp_tls_certificate_get_cert_data (priv->certificate);
519 g_return_if_fail (cert_data);
521 priv->verify_result = g_simple_async_result_new (G_OBJECT (self),
522 callback, user_data, NULL);
524 /* Create a certificate chain */
525 chain = gcr_certificate_chain_new ();
526 for (idx = 0; idx < cert_data->len; ++idx) {
527 data = g_ptr_array_index (cert_data, idx);
528 cert = gcr_simple_certificate_new ((guchar *) data->data, data->len);
529 gcr_certificate_chain_add (chain, cert);
530 g_object_unref (cert);
533 gcr_certificate_chain_build_async (chain, GCR_PURPOSE_SERVER_AUTH, priv->hostname, 0,
534 NULL, perform_verification_cb, g_object_ref (self));
536 g_object_unref (chain);
539 gboolean
540 empathy_tls_verifier_verify_finish (EmpathyTLSVerifier *self,
541 GAsyncResult *res,
542 TpTLSCertificateRejectReason *reason,
543 GHashTable **details,
544 GError **error)
546 EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
548 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
549 error))
551 if (reason != NULL)
552 *reason = (*error)->code;
554 if (details != NULL)
556 *details = tp_asv_new (NULL, NULL);
557 tp_g_hash_table_update (*details, priv->details,
558 (GBoxedCopyFunc) g_strdup,
559 (GBoxedCopyFunc) tp_g_value_slice_dup);
562 return FALSE;
565 if (reason != NULL)
566 *reason = TP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
568 return TRUE;
571 void
572 empathy_tls_verifier_store_exception (EmpathyTLSVerifier *self)
574 GArray *data;
575 GcrCertificate *cert;
576 GPtrArray *cert_data;
577 GError *error = NULL;
578 EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
580 cert_data = tp_tls_certificate_get_cert_data (priv->certificate);
581 g_return_if_fail (cert_data);
583 if (!cert_data->len)
585 DEBUG ("No certificate to pin.");
586 return;
589 /* The first certificate in the chain is for the host */
590 data = g_ptr_array_index (cert_data, 0);
591 cert = gcr_simple_certificate_new ((gpointer)data->data, data->len);
593 DEBUG ("Storing pinned certificate:");
594 debug_certificate (cert);
596 if (!gcr_trust_add_pinned_certificate (cert, GCR_PURPOSE_SERVER_AUTH,
597 priv->hostname, NULL, &error))
598 DEBUG ("Can't store the pinned certificate: %s", error->message);
600 g_object_unref (cert);