]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tls-verifier.c
75943bfbdeb1b83c9d883019235d1ce6e6b8ebdd
[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  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include <config.h>
22
23 #include <gnutls/gnutls.h>
24 #include <gnutls/x509.h>
25
26 #include <telepathy-glib/util.h>
27
28 #include "empathy-tls-verifier.h"
29
30 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
31 #include "empathy-debug.h"
32 #include "empathy-utils.h"
33
34 G_DEFINE_TYPE (EmpathyTLSVerifier, empathy_tls_verifier,
35     G_TYPE_OBJECT)
36
37 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTLSVerifier);
38
39 enum {
40   PROP_TLS_CERTIFICATE = 1,
41   PROP_HOSTNAME,
42
43   LAST_PROPERTY,
44 };
45
46 static const gchar* system_ca_paths[] = {
47   "/etc/ssl/certs/ca-certificates.crt",
48   NULL,
49 };
50
51 typedef struct {
52   GPtrArray *cert_chain;
53
54   GPtrArray *trusted_ca_list;
55   GPtrArray *trusted_crl_list;
56
57   EmpathyTLSCertificate *certificate;
58   gchar *hostname;
59
60   GSimpleAsyncResult *verify_result;
61
62   gboolean dispose_run;
63 } EmpathyTLSVerifierPriv;
64
65 static gnutls_x509_crt_t *
66 ptr_array_to_x509_crt_list (GPtrArray *chain)
67 {
68   gnutls_x509_crt_t *retval;
69   gint idx;
70
71   retval = g_malloc0 (sizeof (gnutls_x509_crt_t) * chain->len);
72
73   for (idx = 0; idx < (gint) chain->len; idx++)
74     retval[idx] = g_ptr_array_index (chain, idx);
75
76   return retval;
77 }
78
79 static gboolean
80 verification_output_to_reason (gint res,
81     guint verify_output,
82     EmpTLSCertificateRejectReason *reason)
83 {
84   gboolean retval = TRUE;
85
86   if (res != GNUTLS_E_SUCCESS)
87     {
88       retval = FALSE;
89
90       /* the certificate is not structurally valid */
91       switch (res)
92         {
93         case GNUTLS_E_INSUFFICIENT_CREDENTIALS:
94           *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED;
95           break;
96         case GNUTLS_E_CONSTRAINT_ERROR:
97           *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_LIMIT_EXCEEDED;
98           break;
99         default:
100           *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
101           break;
102         }
103
104       goto out;
105     }
106
107   /* the certificate is structurally valid, check for other errors. */
108   if (verify_output & GNUTLS_CERT_INVALID)
109     {
110       retval = FALSE;
111
112       if (verify_output & GNUTLS_CERT_SIGNER_NOT_FOUND)
113         *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED;
114       else if (verify_output & GNUTLS_CERT_SIGNER_NOT_CA)
115         *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED;
116       else if (verify_output & GNUTLS_CERT_INSECURE_ALGORITHM)
117         *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_INSECURE;
118       else if (verify_output & GNUTLS_CERT_NOT_ACTIVATED)
119         *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_NOT_ACTIVATED;
120       else if (verify_output & GNUTLS_CERT_EXPIRED)
121         *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_EXPIRED;
122       else
123         *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
124
125       goto out;
126     }
127
128  out:
129   return retval;
130 }
131
132 static gboolean
133 verify_last_certificate (EmpathyTLSVerifier *self,
134     gnutls_x509_crt_t cert,
135     EmpTLSCertificateRejectReason *reason)
136 {
137   guint verify_output;
138   gint res;
139   gnutls_x509_crt_t *trusted_ca_list;
140   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
141
142   trusted_ca_list = ptr_array_to_x509_crt_list (priv->trusted_ca_list);
143   res = gnutls_x509_crt_verify (cert, trusted_ca_list,
144       priv->trusted_ca_list->len, 0, &verify_output);
145
146   g_free (trusted_ca_list);
147
148   return verification_output_to_reason (res, verify_output, reason);
149 }
150
151 static gboolean
152 verify_certificate (EmpathyTLSVerifier *self,
153     gnutls_x509_crt_t cert,
154     gnutls_x509_crt_t issuer,
155     EmpTLSCertificateRejectReason *reason)
156 {
157   guint verify_output;
158   gint res;
159
160   res = gnutls_x509_crt_verify (cert, &issuer, 1, 0, &verify_output);
161
162   return verification_output_to_reason (res, verify_output, reason);
163 }
164
165 static void
166 complete_verification (EmpathyTLSVerifier *self)
167 {
168   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
169
170   DEBUG ("Verification successful, completing...");
171
172   g_simple_async_result_complete_in_idle (priv->verify_result);
173
174   tp_clear_object (&priv->verify_result);  
175 }
176
177 static void
178 abort_verification (EmpathyTLSVerifier *self,
179     EmpTLSCertificateRejectReason reason)
180 {
181   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
182
183   DEBUG ("Verification error %u, aborting...", reason);
184
185   g_simple_async_result_set_error (priv->verify_result,
186       G_IO_ERROR, reason, "TLS verification failed with reason %u",
187       reason);
188   g_simple_async_result_complete_in_idle (priv->verify_result);
189
190   tp_clear_object (&priv->verify_result);
191 }
192
193 static void
194 real_start_verification (EmpathyTLSVerifier *self)
195 {
196   gnutls_x509_crt_t last_cert;
197   gint idx;
198   gboolean res = FALSE;
199   gint num_certs;
200   EmpTLSCertificateRejectReason reason =
201     EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
202   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
203
204   num_certs = priv->cert_chain->len;
205
206   DEBUG ("Starting verification");
207
208   if (priv->trusted_ca_list->len > 0)
209     {
210       /* if the last certificate is self-signed, ignore it, as we want to check
211        * the chain against our trusted CA list first.
212        */
213       last_cert = g_ptr_array_index (priv->cert_chain, num_certs - 1);
214
215       if (gnutls_x509_crt_check_issuer (last_cert, last_cert) > 0)
216         num_certs--;
217     }
218
219   for (idx = 1; idx < num_certs; idx++)
220     {
221       res = verify_certificate (self,
222           g_ptr_array_index (priv->cert_chain, idx -1),
223           g_ptr_array_index (priv->cert_chain, idx),
224           &reason);
225
226       DEBUG ("Certificate verification %d gave result %d with reason %u", idx,
227           res, reason);
228
229       if (!res)
230         {
231           abort_verification (self, reason);
232           return;
233         }
234     }
235
236   if (priv->trusted_ca_list->len > 0)
237     {
238       res = verify_last_certificate (self,
239           g_ptr_array_index (priv->cert_chain, num_certs - 1),
240           &reason);
241     }
242
243   if (!res)
244     {
245       abort_verification (self, reason);
246       return;
247     }
248
249   complete_verification (self);
250 }
251
252 static gboolean
253 start_verification (gpointer user_data)
254 {
255   EmpathyTLSVerifier *self = user_data;
256
257   real_start_verification (self);
258
259   return FALSE;
260 }
261
262 static void
263 build_gnutls_cert_list (EmpathyTLSVerifier *self)
264 {
265   guint num_certs;
266   guint idx;
267   GPtrArray *certificate_data = NULL;
268   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
269
270   g_object_get (priv->certificate,
271       "cert-data", &certificate_data,
272       NULL);
273   num_certs = certificate_data->len;
274
275   priv->cert_chain = g_ptr_array_sized_new (num_certs);
276
277   for (idx = 0; idx < num_certs; idx++)
278     {
279       gnutls_x509_crt_t cert;
280       GArray *one_cert;
281       gnutls_datum_t datum = { NULL, 0 };
282
283       one_cert = g_ptr_array_index (certificate_data, idx);
284       datum.data = (guchar *) one_cert->data;
285       datum.size = one_cert->len;
286
287       gnutls_x509_crt_init (&cert);
288       gnutls_x509_crt_import (cert, &datum, GNUTLS_X509_FMT_DER);
289
290       g_ptr_array_add (priv->cert_chain, cert);
291     }
292 }
293
294 static gint
295 get_number_and_type_of_certificates (gnutls_datum_t *datum,
296     gnutls_x509_crt_fmt_t *format)
297 {
298   gnutls_x509_crt_t fake;
299   gint retval = 1;
300   gint res;
301
302   res = gnutls_x509_crt_list_import (&fake, (guint *) &retval, datum,
303       GNUTLS_X509_FMT_PEM, 0);
304
305   if (res == GNUTLS_E_SHORT_MEMORY_BUFFER || res > 0)
306     {
307       *format = GNUTLS_X509_FMT_PEM;
308       return retval;
309     }
310
311   /* try DER */
312   res = gnutls_x509_crt_list_import (&fake, (guint *) &retval, datum,
313       GNUTLS_X509_FMT_DER, 0);
314
315   if (res > 0)
316     {
317       *format = GNUTLS_X509_FMT_DER;
318       return retval;
319     }
320
321   return res;
322 }
323
324 static gboolean
325 build_gnutls_ca_and_crl_lists (GIOSchedulerJob *job,
326     GCancellable *cancellable,
327     gpointer user_data)
328 {
329   gint idx;
330   EmpathyTLSVerifier *self = user_data;
331   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
332
333   priv->trusted_ca_list = g_ptr_array_new ();
334
335   for (idx = 0; idx < (gint) G_N_ELEMENTS (system_ca_paths) - 1; idx++)
336     {
337       const gchar *path;
338       gchar *contents = NULL;
339       gsize length = 0;
340       gint res, n_certs;
341       gnutls_x509_crt_t *cert_list;
342       gnutls_datum_t datum = { NULL, 0 };
343       gnutls_x509_crt_fmt_t format = 0;
344       GError *error = NULL;
345
346       path = system_ca_paths[idx];
347       g_file_get_contents (path, &contents, &length, &error);
348
349       if (error != NULL)
350         {
351           DEBUG ("Unable to read system CAs from path %s", path);
352           g_error_free (error);
353           continue;
354         }
355
356       datum.data = (guchar *) contents;
357       datum.size = length;
358       n_certs = get_number_and_type_of_certificates (&datum, &format);
359
360       if (n_certs < 0)
361         {
362           DEBUG ("Unable to parse the system CAs from path %s: GnuTLS "
363               "returned error %d", path, n_certs);
364
365           g_free (contents);
366           continue;
367         }
368
369       cert_list = g_malloc0 (sizeof (gnutls_x509_crt_t) * n_certs);
370       res = gnutls_x509_crt_list_import (cert_list, (guint *) &n_certs, &datum,
371           format, 0);
372
373       if (res < 0)
374         {
375           DEBUG ("Unable to import system CAs from path %s; "
376               "GnuTLS returned error %d", path, res);
377
378           g_free (contents);
379           continue;
380         }
381
382       DEBUG ("Successfully imported %d system CA certificates from path %s",
383           n_certs, path);
384
385       /* append the newly created cert structutes into the global GPtrArray */
386       for (idx = 0; idx < n_certs; idx++)
387         g_ptr_array_add (priv->trusted_ca_list, cert_list[idx]);
388
389       g_free (contents);
390     }
391
392   /* TODO: do the CRL too */
393
394   g_io_scheduler_job_send_to_mainloop_async (job,
395       start_verification, self, NULL);
396
397   return FALSE;
398 }
399
400 static void
401 empathy_tls_verifier_get_property (GObject *object,
402     guint property_id,
403     GValue *value,
404     GParamSpec *pspec)
405 {
406   EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
407
408   switch (property_id)
409     {
410     case PROP_TLS_CERTIFICATE:
411       g_value_set_object (value, priv->certificate);
412       break;
413     case PROP_HOSTNAME:
414       g_value_set_string (value, priv->hostname);
415       break;
416     default:
417       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
418       break;
419     }
420 }
421
422 static void
423 empathy_tls_verifier_set_property (GObject *object,
424     guint property_id,
425     const GValue *value,
426     GParamSpec *pspec)
427 {
428   EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
429
430   switch (property_id)
431     {
432     case PROP_TLS_CERTIFICATE:
433       priv->certificate = g_value_dup_object (value);
434       break;
435     case PROP_HOSTNAME:
436       priv->hostname = g_value_dup_string (value);
437       break;
438     default:
439       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
440       break;
441     }
442 }
443
444 static void
445 empathy_tls_verifier_dispose (GObject *object)
446 {
447   EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
448
449   if (priv->dispose_run)
450     return;
451
452   priv->dispose_run = TRUE;
453
454   tp_clear_object (&priv->certificate);
455
456   G_OBJECT_CLASS (empathy_tls_verifier_parent_class)->dispose (object);
457 }
458
459 static void
460 empathy_tls_verifier_finalize (GObject *object)
461 {
462   EmpathyTLSVerifierPriv *priv = GET_PRIV (object);
463
464   DEBUG ("%p", object);
465   
466   g_free (priv->hostname);
467
468   G_OBJECT_CLASS (empathy_tls_verifier_parent_class)->finalize (object);
469 }
470
471 static void
472 empathy_tls_verifier_constructed (GObject *object)
473 {
474   EmpathyTLSVerifier *self = EMPATHY_TLS_VERIFIER (object);
475
476   build_gnutls_cert_list (self);
477   
478   if (G_OBJECT_CLASS (empathy_tls_verifier_parent_class)->constructed != NULL)
479     G_OBJECT_CLASS (empathy_tls_verifier_parent_class)->constructed (object);
480 }
481
482 static void
483 empathy_tls_verifier_init (EmpathyTLSVerifier *self)
484 {
485   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
486       EMPATHY_TYPE_TLS_VERIFIER, EmpathyTLSVerifierPriv);
487 }
488
489 static void
490 empathy_tls_verifier_class_init (EmpathyTLSVerifierClass *klass)
491 {
492   GParamSpec *pspec;
493   GObjectClass *oclass = G_OBJECT_CLASS (klass);
494
495   g_type_class_add_private (klass, sizeof (EmpathyTLSVerifierPriv));
496
497   oclass->set_property = empathy_tls_verifier_set_property;
498   oclass->get_property = empathy_tls_verifier_get_property;
499   oclass->finalize = empathy_tls_verifier_finalize;
500   oclass->dispose = empathy_tls_verifier_dispose;
501   oclass->constructed = empathy_tls_verifier_constructed;
502
503   pspec = g_param_spec_object ("certificate", "The EmpathyTLSCertificate",
504       "The EmpathyTLSCertificate to be verified.",
505       EMPATHY_TYPE_TLS_CERTIFICATE,
506       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
507   g_object_class_install_property (oclass, PROP_TLS_CERTIFICATE, pspec);
508
509   pspec = g_param_spec_string ("hostname", "The hostname",
510       "The hostname which should be certified by the certificate.",
511       NULL,
512       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
513   g_object_class_install_property (oclass, PROP_HOSTNAME, pspec);
514 }
515
516 EmpathyTLSVerifier *
517 empathy_tls_verifier_new (EmpathyTLSCertificate *certificate,
518     const gchar *hostname)
519 {
520   g_assert (EMPATHY_IS_TLS_CERTIFICATE (certificate));
521   g_assert (hostname != NULL);
522
523   return g_object_new (EMPATHY_TYPE_TLS_VERIFIER,
524       "certificate", certificate,
525       "hostname", hostname,
526       NULL);
527 }
528
529 void
530 empathy_tls_verifier_verify_async (EmpathyTLSVerifier *self,
531     GAsyncReadyCallback callback,
532     gpointer user_data)
533 {
534   EmpathyTLSVerifierPriv *priv = GET_PRIV (self);
535
536   priv->verify_result = g_simple_async_result_new (G_OBJECT (self),
537       callback, user_data, NULL);
538
539   g_io_scheduler_push_job (build_gnutls_ca_and_crl_lists,
540       self, NULL, G_PRIORITY_DEFAULT, NULL);
541 }
542
543 gboolean
544 empathy_tls_verifier_verify_finish (EmpathyTLSVerifier *self,
545     GAsyncResult *res,
546     EmpTLSCertificateRejectReason *reason,
547     GError **error)
548 {
549   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
550           error))
551     {
552       *reason = (*error)->code;
553       return FALSE;
554     }
555
556   *reason = EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN;
557   return TRUE;
558 }