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