]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tls-verifier.c
libempathy: Match changes in libgcr terminology and debug output.
[empathy.git] / libempathy / empathy-tls-verifier.c
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>
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <config.h>
23
24 #include <gnutls/gnutls.h>
25 #include <gnutls/x509.h>
26
27 #include <telepathy-glib/util.h>
28
29 #include "empathy-tls-verifier.h"
30
31 #include <gcr/gcr.h>
32
33 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
34 #include "empathy-debug.h"
35 #include "empathy-utils.h"
36
37 G_DEFINE_TYPE (EmpathyTLSVerifier, empathy_tls_verifier,
38     G_TYPE_OBJECT)
39
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTLSVerifier);
41
42 enum {
43   PROP_TLS_CERTIFICATE = 1,
44   PROP_HOSTNAME,
45
46   LAST_PROPERTY,
47 };
48
49 typedef struct {
50   EmpathyTLSCertificate *certificate;
51   gchar *hostname;
52
53   GSimpleAsyncResult *verify_result;
54   GHashTable *details;
55
56   gboolean dispose_run;
57 } EmpathyTLSVerifierPriv;
58
59 static gboolean
60 verification_output_to_reason (gint res,
61     guint verify_output,
62     EmpTLSCertificateRejectReason *reason)
63 {
64   gboolean retval = TRUE;
65
66   g_assert (reason != NULL);
67
68   if (res != GNUTLS_E_SUCCESS)
69     {
70       retval = FALSE;
71
72       /* the certificate is not structurally valid */
73       switch (res)
74         {
75         case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
76           *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED;
77           break;
78         case GNUTLS_E_CONSTRAINT_ERROR:
79           *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_LIMIT_EXCEEDED;
80           break;
81         default:
82           *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
83           break;
84         }
85
86       goto out;
87     }
88
89   /* the certificate is structurally valid, check for other errors. */
90   if (verify_output & GNUTLS_CERT_INVALID)
91     {
92       retval = FALSE;
93
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;
104       else
105         *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
106
107       goto out;
108     }
109
110  out:
111   return retval;
112 }
113
114 static void
115 build_certificate_list_for_gnutls (GcrCertificateChain *chain,
116         gnutls_x509_crt_t **list, guint *n_list,
117         gnutls_x509_crt_t **anchors, guint *n_anchors)
118 {
119   GcrCertificate *cert;
120   guint idx, length;
121   gnutls_x509_crt_t *retval;
122   gnutls_x509_crt_t gcert;
123   gnutls_datum_t datum;
124   gsize n_data;
125
126   g_assert (list);
127   g_assert (n_list);
128   g_assert (anchors);
129   g_assert (n_anchors);
130
131   *list = *anchors = NULL;
132   *n_list = *n_anchors = 0;
133
134   length = gcr_certificate_chain_get_length (chain);
135   retval = g_malloc0 (sizeof (gnutls_x509_crt_t) * length);
136
137   /* Convert the main body of the chain to gnutls */
138   for (idx = 0; idx < length; ++idx)
139     {
140       cert = gcr_certificate_chain_get_certificate (chain, idx);
141       datum.data = (gpointer)gcr_certificate_get_der_data (cert, &n_data);
142       datum.size = n_data;
143
144       gnutls_x509_crt_init (&gcert);
145       if (gnutls_x509_crt_import (gcert, &datum, GNUTLS_X509_FMT_DER) < 0)
146         g_return_if_reached ();
147
148       retval[idx] = gcert;
149     }
150
151   *list = retval;
152   *n_list = length;
153
154   /* See if we have an anchor */
155   if (gcr_certificate_chain_get_status (chain) ==
156           GCR_CERTIFICATE_CHAIN_ANCHORED)
157     {
158       cert = gcr_certificate_chain_get_anchor (chain);
159       g_return_if_fail (cert);
160
161       datum.data = (gpointer)gcr_certificate_get_der_data (cert, &n_data);
162       datum.size = n_data;
163
164       gnutls_x509_crt_init (&gcert);
165       if (gnutls_x509_crt_import (gcert, &datum, GNUTLS_X509_FMT_DER) < 0)
166         g_return_if_reached ();
167
168       retval = g_malloc0 (sizeof (gnutls_x509_crt_t) * 1);
169       retval[0] = gcert;
170       *anchors = retval;
171       *n_anchors = 1;
172     }
173 }
174
175 static void
176 free_certificate_list_for_gnutls (gnutls_x509_crt_t *list, guint n_list)
177 {
178   guint idx;
179
180   for (idx = 0; idx < n_list; idx++)
181     gnutls_x509_crt_deinit (list[idx]);
182   g_free (list);
183 }
184
185 static void
186 complete_verification (EmpathyTLSVerifier *self)
187 {
188   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
189
190   DEBUG ("Verification successful, completing...");
191
192   g_simple_async_result_complete_in_idle (priv->verify_result);
193
194   tp_clear_object (&priv->verify_result);
195 }
196
197 static void
198 abort_verification (EmpathyTLSVerifier *self,
199     EmpTLSCertificateRejectReason reason)
200 {
201   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
202
203   DEBUG ("Verification error %u, aborting...", reason);
204
205   g_simple_async_result_set_error (priv->verify_result,
206       G_IO_ERROR, reason, "TLS verification failed with reason %u",
207       reason);
208   g_simple_async_result_complete_in_idle (priv->verify_result);
209
210   tp_clear_object (&priv->verify_result);
211 }
212
213 static void
214 debug_certificate_chain (GcrCertificateChain *chain)
215 {
216     GEnumClass *enum_class;
217     GEnumValue *enum_value;
218     gint idx, length;
219     GcrCertificate *cert;
220     gchar *subject;
221
222     enum_class = G_ENUM_CLASS
223             (g_type_class_peek (GCR_TYPE_CERTIFICATE_CHAIN_STATUS));
224     enum_value = g_enum_get_value (enum_class,
225             gcr_certificate_chain_get_status (chain));
226     length = gcr_certificate_chain_get_length (chain);
227     DEBUG ("Certificate chain: length %u status %s",
228             length, enum_value ? enum_value->value_nick : "XXX");
229
230     for (idx = 0; idx < length; ++idx)
231       {
232         cert = gcr_certificate_chain_get_certificate (chain, idx);
233         subject = gcr_certificate_get_subject_dn (cert);
234         DEBUG ("  Certificate: %s", subject);
235         g_free (subject);
236       }
237 }
238
239 static void
240 perform_verification (EmpathyTLSVerifier *self, GcrCertificateChain *chain)
241 {
242   gboolean ret = FALSE;
243   EmpTLSCertificateRejectReason reason =
244     EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
245   gnutls_x509_crt_t *list, *anchors;
246   guint n_list, n_anchors;
247   guint verify_output;
248   gint res;
249   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
250
251   DEBUG ("Performing verification");
252   debug_certificate_chain (chain);
253
254   /*
255    * If the first certificate is an pinned certificate then we completely
256    * ignore the rest of the verification process.
257    */
258   if (gcr_certificate_chain_get_status (chain) == GCR_CERTIFICATE_CHAIN_PINNED)
259     {
260       DEBUG ("Found pinned certificate for %s", priv->hostname);
261       complete_verification (self);
262       goto out;
263   }
264
265   build_certificate_list_for_gnutls (chain, &list, &n_list,
266           &anchors, &n_anchors);
267   if (list == NULL || n_list == 0) {
268       g_warn_if_reached ();
269       abort_verification (self, EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN);
270       goto out;
271   }
272
273   verify_output = 0;
274   res = gnutls_x509_crt_list_verify (list, n_list, anchors, n_anchors,
275            NULL, 0, 0, &verify_output);
276   ret = verification_output_to_reason (res, verify_output, &reason);
277
278   DEBUG ("Certificate verification gave result %d with reason %u", ret,
279           reason);
280
281   if (!ret) {
282       abort_verification (self, reason);
283       goto out;
284   }
285
286   /* now check if the certificate matches the hostname. */
287   if (gnutls_x509_crt_check_hostname (list[0], priv->hostname) == 0)
288     {
289       gchar *certified_hostname;
290
291       certified_hostname = empathy_get_x509_certificate_hostname (list[0]);
292       tp_asv_set_string (priv->details,
293           "expected-hostname", priv->hostname);
294       tp_asv_set_string (priv->details,
295           "certificate-hostname", certified_hostname);
296
297       DEBUG ("Hostname mismatch: got %s but expected %s",
298           certified_hostname, priv->hostname);
299
300       g_free (certified_hostname);
301       abort_verification (self,
302               EMP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH);
303       goto out;
304     }
305
306   DEBUG ("Hostname matched");
307   complete_verification (self);
308
309  out:
310   free_certificate_list_for_gnutls (list, n_list);
311   free_certificate_list_for_gnutls (anchors, n_anchors);
312 }
313
314 static void
315 perform_verification_cb (GObject *object, GAsyncResult *res, gpointer user_data)
316 {
317   GError *error = NULL;
318
319   GcrCertificateChain *chain = GCR_CERTIFICATE_CHAIN (object);
320   EmpathyTLSVerifier *self = EMPATHY_TLS_VERIFIER (user_data);
321
322   /* Even if building the chain fails, try verifying what we have */
323   if (!gcr_certificate_chain_build_finish (chain, res, &error))
324     {
325       DEBUG ("Building of certificate chain failed: %s", error->message);
326       g_clear_error (&error);
327     }
328
329   perform_verification (self, chain);
330
331   /* Matches ref when staring chain build */
332   g_object_unref (self);
333 }
334
335 static void
336 empathy_tls_verifier_get_property (GObject *object,
337     guint property_id,
338     GValue *value,
339     GParamSpec *pspec)
340 {
341   EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
342
343   switch (property_id)
344     {
345     case PROP_TLS_CERTIFICATE:
346       g_value_set_object (value, priv->certificate);
347       break;
348     case PROP_HOSTNAME:
349       g_value_set_string (value, priv->hostname);
350       break;
351     default:
352       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
353       break;
354     }
355 }
356
357 static void
358 empathy_tls_verifier_set_property (GObject *object,
359     guint property_id,
360     const GValue *value,
361     GParamSpec *pspec)
362 {
363   EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
364
365   switch (property_id)
366     {
367     case PROP_TLS_CERTIFICATE:
368       priv->certificate = g_value_dup_object (value);
369       break;
370     case PROP_HOSTNAME:
371       priv->hostname = g_value_dup_string (value);
372       break;
373     default:
374       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
375       break;
376     }
377 }
378
379 static void
380 empathy_tls_verifier_dispose (GObject *object)
381 {
382   EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
383
384   if (priv->dispose_run)
385     return;
386
387   priv->dispose_run = TRUE;
388
389   tp_clear_object (&priv->certificate);
390
391   G_OBJECT_CLASS (empathy_tls_verifier_parent_class)->dispose (object);
392 }
393
394 static void
395 empathy_tls_verifier_finalize (GObject *object)
396 {
397   EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
398
399   DEBUG ("%p", object);
400
401   tp_clear_boxed (G_TYPE_HASH_TABLE, &priv->details);
402   g_free (priv->hostname);
403
404   G_OBJECT_CLASS (empathy_tls_verifier_parent_class)->finalize (object);
405 }
406
407 static void
408 empathy_tls_verifier_init (EmpathyTLSVerifier *self)
409 {
410   EmpathyTLSVerifierPriv *priv;
411
412   priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
413       EMPATHY_TYPE_TLS_VERIFIER, EmpathyTLSVerifierPriv);
414   priv->details = tp_asv_new (NULL, NULL);
415 }
416
417 static void
418 empathy_tls_verifier_class_init (EmpathyTLSVerifierClass *klass)
419 {
420   GParamSpec *pspec;
421   GObjectClass *oclass = G_OBJECT_CLASS (klass);
422
423   g_type_class_add_private (klass, sizeof (EmpathyTLSVerifierPriv));
424
425   oclass->set_property = empathy_tls_verifier_set_property;
426   oclass->get_property = empathy_tls_verifier_get_property;
427   oclass->finalize = empathy_tls_verifier_finalize;
428   oclass->dispose = empathy_tls_verifier_dispose;
429
430   pspec = g_param_spec_object ("certificate", "The EmpathyTLSCertificate",
431       "The EmpathyTLSCertificate to be verified.",
432       EMPATHY_TYPE_TLS_CERTIFICATE,
433       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
434   g_object_class_install_property (oclass, PROP_TLS_CERTIFICATE, pspec);
435
436   pspec = g_param_spec_string ("hostname", "The hostname",
437       "The hostname which should be certified by the certificate.",
438       NULL,
439       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
440   g_object_class_install_property (oclass, PROP_HOSTNAME, pspec);
441 }
442
443 EmpathyTLSVerifier *
444 empathy_tls_verifier_new (EmpathyTLSCertificate *certificate,
445     const gchar *hostname)
446 {
447   g_assert (EMPATHY_IS_TLS_CERTIFICATE (certificate));
448   g_assert (hostname != NULL);
449
450   return g_object_new (EMPATHY_TYPE_TLS_VERIFIER,
451       "certificate", certificate,
452       "hostname", hostname,
453       NULL);
454 }
455
456 void
457 empathy_tls_verifier_verify_async (EmpathyTLSVerifier *self,
458     GAsyncReadyCallback callback,
459     gpointer user_data)
460 {
461   GcrCertificateChain *chain;
462   GcrCertificate *cert;
463   GPtrArray *certs = NULL;
464   GArray *cert_data;
465   guint idx;
466   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
467
468   DEBUG ("Starting verification");
469
470   g_return_if_fail (priv->verify_result == NULL);
471
472   g_object_get (priv->certificate, "cert-data", &certs, NULL);
473   g_return_if_fail (certs);
474
475   priv->verify_result = g_simple_async_result_new (G_OBJECT (self),
476       callback, user_data, NULL);
477
478   /* Create a certificate chain */
479   chain = gcr_certificate_chain_new ();
480   for (idx = 0; idx < certs->len; ++idx) {
481     cert_data = g_ptr_array_index (certs, idx);
482     cert = gcr_simple_certificate_new_static (cert_data->data, cert_data->len);
483     gcr_certificate_chain_add (chain, cert);
484     g_object_unref (cert);
485   }
486
487   gcr_certificate_chain_build_async (chain, GCR_PURPOSE_CLIENT_AUTH, priv->hostname, 0,
488           NULL, perform_verification_cb, g_object_ref (self));
489
490   g_object_unref (chain);
491   g_ptr_array_unref (certs);
492 }
493
494 gboolean
495 empathy_tls_verifier_verify_finish (EmpathyTLSVerifier *self,
496     GAsyncResult *res,
497     EmpTLSCertificateRejectReason *reason,
498     GHashTable **details,
499     GError **error)
500 {
501   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
502
503   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
504           error))
505     {
506       if (reason != NULL)
507         *reason = (*error)->code;
508
509       if (details != NULL)
510         {
511           *details = tp_asv_new (NULL, NULL);
512           tp_g_hash_table_update (*details, priv->details,
513               (GBoxedCopyFunc) g_strdup,
514               (GBoxedCopyFunc) tp_g_value_slice_dup);
515         }
516
517       return FALSE;
518     }
519
520   if (reason != NULL)
521     *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
522
523   return TRUE;
524 }
525
526 void
527 empathy_tls_verifier_store_exception (EmpathyTLSVerifier *self)
528 {
529   GArray *last_cert;
530   GcrCertificate *cert;
531   GPtrArray *certs;
532   GError *error = NULL;
533   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
534
535   g_object_get (priv->certificate, "cert-data", &certs, NULL);
536   last_cert = g_ptr_array_index (certs, certs->len - 1);
537   cert = gcr_simple_certificate_new_static ((gpointer)last_cert->data,
538           last_cert->len);
539
540   if (!gcr_trust_add_pinned_certificate (cert, GCR_PURPOSE_CLIENT_AUTH,
541           priv->hostname, NULL, &error))
542       DEBUG ("Can't store the certificate exeption: %s", error->message);
543
544   g_object_unref (cert);
545 }