1381aa18585eca0e97f050fccd9457680a6d60a0
[claws.git] / src / common / ssl_certificate.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> 
4  * and the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #ifdef USE_GNUTLS
27 #include <gnutls/gnutls.h>
28 #include <gnutls/x509.h>
29 #include <gnutls/pkcs12.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <glib.h>
36 #include <glib/gi18n.h>
37 #include <errno.h>
38 #ifdef G_OS_WIN32
39 #  include <winsock2.h>
40 #else
41 #  include <sys/socket.h>
42 #  include <netinet/in.h>
43 #  include <netdb.h>
44 #endif /* G_OS_WIN32 */
45 #include "ssl_certificate.h"
46 #include "utils.h"
47 #include "log.h"
48 #include "socket.h"
49 #include "hooks.h"
50 #include "defs.h"
51
52 static GHashTable *warned_expired = NULL;
53
54 gboolean prefs_common_unsafe_ssl_certs(void);
55
56 static gchar *get_certificate_path(const gchar *host, const gchar *port, const gchar *fp)
57 {
58         if (fp != NULL && prefs_common_unsafe_ssl_certs())
59                 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
60                           "certs", G_DIR_SEPARATOR_S,
61                           host, ".", port, ".", fp, ".cert", NULL);
62         else 
63                 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
64                           "certs", G_DIR_SEPARATOR_S,
65                           host, ".", port, ".cert", NULL);
66 }
67
68 char * readable_fingerprint(unsigned char *src, int len) 
69 {
70         int i=0;
71         char * ret;
72         
73         if (src == NULL)
74                 return NULL;
75         ret = g_strdup("");
76         while (i < len) {
77                 char *tmp2;
78                 if(i>0)
79                         tmp2 = g_strdup_printf("%s:%02X", ret, src[i]);
80                 else
81                         tmp2 = g_strdup_printf("%02X", src[i]);
82                 g_free(ret);
83                 ret = g_strdup(tmp2);
84                 g_free(tmp2);
85                 i++;
86         }
87         return ret;
88 }
89
90 #if USE_GNUTLS
91 static gnutls_x509_crt_t x509_crt_copy(gnutls_x509_crt_t src)
92 {
93     int ret;
94     size_t size;
95     gnutls_datum_t tmp;
96     gnutls_x509_crt_t dest;
97     size = 0;
98     
99     if (gnutls_x509_crt_init(&dest) != 0) {
100         g_warning("couldn't gnutls_x509_crt_init\n");
101         return NULL;
102     }
103
104     if (gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, NULL, &size) 
105         != GNUTLS_E_SHORT_MEMORY_BUFFER) {
106         g_warning("couldn't gnutls_x509_crt_export to get size\n");
107         gnutls_x509_crt_deinit(dest);
108         return NULL;
109     }
110
111     tmp.data = malloc(size);
112     memset(tmp.data, 0, size);
113     ret = gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, tmp.data, &size);
114     if (ret == 0) {
115         tmp.size = size;
116         ret = gnutls_x509_crt_import(dest, &tmp, GNUTLS_X509_FMT_DER);
117         if (ret) {
118                 g_warning("couldn't gnutls_x509_crt_import for real (%d %s)\n", ret, gnutls_strerror(ret));
119                 gnutls_x509_crt_deinit(dest);
120                 dest = NULL;
121         }
122     } else {
123         g_warning("couldn't gnutls_x509_crt_export for real (%d %s)\n", ret, gnutls_strerror(ret));
124         gnutls_x509_crt_deinit(dest);
125         dest = NULL;
126     }
127
128     free(tmp.data);
129     return dest;
130 }
131 #endif
132
133 static SSLCertificate *ssl_certificate_new(gnutls_x509_crt_t x509_cert, const gchar *host, gushort port)
134 {
135         SSLCertificate *cert = g_new0(SSLCertificate, 1);
136         size_t n;
137         unsigned char md[128];  
138
139         if (host == NULL || x509_cert == NULL) {
140                 ssl_certificate_destroy(cert);
141                 return NULL;
142         }
143         cert->x509_cert = x509_crt_copy(x509_cert);
144         cert->status = (guint)-1;
145         cert->host = g_strdup(host);
146         cert->port = port;
147         
148         /* fingerprint */
149         n = 128;
150         gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_MD5, md, &n);
151         cert->fingerprint = readable_fingerprint(md, (int)n);
152         return cert;
153 }
154
155 static void gnutls_export_X509_fp(FILE *fp, gnutls_x509_crt_t x509_cert, gnutls_x509_crt_fmt_t format)
156 {
157         char output[10*1024];
158         size_t cert_size = 10*1024;
159         int r;
160         
161         if ((r = gnutls_x509_crt_export(x509_cert, format, output, &cert_size)) < 0) {
162                 g_warning("couldn't export cert %s (%zd)\n", gnutls_strerror(r), cert_size);
163                 return;
164         }
165
166         debug_print("writing %zd bytes\n",cert_size);
167         if (fwrite(&output, 1, cert_size, fp) < cert_size) {
168                 g_warning("failed to write cert: %d %s\n", errno, strerror(errno));
169         }
170 }
171
172 size_t gnutls_i2d_X509(gnutls_x509_crt_t x509_cert, unsigned char **output)
173 {
174         size_t cert_size = 10*1024;
175         int r;
176         
177         if (output == NULL)
178                 return 0;
179         
180         *output = malloc(cert_size);
181
182         if ((r = gnutls_x509_crt_export(x509_cert, GNUTLS_X509_FMT_DER, *output, &cert_size)) < 0) {
183                 g_warning("couldn't export cert %s (%zd)\n", gnutls_strerror(r), cert_size);
184                 free(*output);
185                 *output = NULL;
186                 return 0;
187         }
188         return cert_size;
189 }
190
191 size_t gnutls_i2d_PrivateKey(gnutls_x509_privkey_t pkey, unsigned char **output)
192 {
193         size_t key_size = 10*1024;
194         int r;
195         
196         if (output == NULL)
197                 return 0;
198         
199         *output = malloc(key_size);
200
201         if ((r = gnutls_x509_privkey_export(pkey, GNUTLS_X509_FMT_DER, *output, &key_size)) < 0) {
202                 g_warning("couldn't export key %s (%zd)\n", gnutls_strerror(r), key_size);
203                 free(*output);
204                 *output = NULL;
205                 return 0;
206         }
207         return key_size;
208 }
209
210 static int gnutls_import_X509_list_fp(FILE *fp, gnutls_x509_crt_fmt_t format,
211                                    gnutls_x509_crt_t **cert_list, gint *num_certs)
212 {
213         gnutls_x509_crt_t *crt_list;
214         unsigned int max = 512;
215         unsigned int flags = 0;
216         gnutls_datum_t tmp;
217         struct stat s;
218         int r;
219
220         *cert_list = NULL;
221         *num_certs = 0;
222
223         if (fp == NULL)
224                 return -ENOENT;
225
226         if (fstat(fileno(fp), &s) < 0) {
227                 perror("fstat");
228                 return -errno;
229         }
230
231         crt_list=(gnutls_x509_crt_t*)malloc(max*sizeof(gnutls_x509_crt_t));
232         tmp.data = malloc(s.st_size);
233         memset(tmp.data, 0, s.st_size);
234         tmp.size = s.st_size;
235         if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
236                 perror("fread");
237                 free(tmp.data);
238                 free(crt_list);
239                 return -EIO;
240         }
241
242         if ((r = gnutls_x509_crt_list_import(crt_list, &max, 
243                         &tmp, format, flags)) < 0) {
244                 debug_print("cert import failed: %s\n", gnutls_strerror(r));
245                 free(tmp.data);
246                 free(crt_list);
247                 return r;
248         }
249         free(tmp.data);
250         debug_print("got %d certs in crt_list! %p\n", max, &crt_list);
251
252         *cert_list = crt_list;
253         *num_certs = max;
254
255         return r;
256 }
257
258 /* return one certificate, read from file */
259 static gnutls_x509_crt_t gnutls_import_X509_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
260 {
261         gnutls_x509_crt_t *certs = NULL;
262         gnutls_x509_crt_t cert = NULL;
263         int i, ncerts, r;
264
265         if ((r = gnutls_import_X509_list_fp(fp, format, &certs, &ncerts)) < 0) {
266                 return NULL;
267         }
268
269         if (ncerts == 0)
270                 return NULL;
271
272         for (i = 1; i < ncerts; i++)
273                 gnutls_x509_crt_deinit(certs[i]);
274
275         cert = certs[0];
276         free(certs);
277
278         return cert;
279 }
280
281 static gnutls_x509_privkey_t gnutls_import_key_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
282 {
283         gnutls_x509_privkey_t key = NULL;
284         gnutls_datum_t tmp;
285         struct stat s;
286         int r;
287         if (fstat(fileno(fp), &s) < 0) {
288                 perror("fstat");
289                 return NULL;
290         }
291         tmp.data = malloc(s.st_size);
292         memset(tmp.data, 0, s.st_size);
293         tmp.size = s.st_size;
294         if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
295                 perror("fread");
296                 free(tmp.data);
297                 return NULL;
298         }
299
300         gnutls_x509_privkey_init(&key);
301         if ((r = gnutls_x509_privkey_import(key, &tmp, format)) < 0) {
302                 debug_print("key import failed: %s\n", gnutls_strerror(r));
303                 gnutls_x509_privkey_deinit(key);
304                 key = NULL;
305         }
306         free(tmp.data);
307         debug_print("got key! %p\n", key);
308         return key;
309 }
310
311 static gnutls_pkcs12_t gnutls_import_PKCS12_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
312 {
313         gnutls_pkcs12_t p12 = NULL;
314         gnutls_datum_t tmp;
315         struct stat s;
316         int r;
317         if (fstat(fileno(fp), &s) < 0) {
318                 log_error(LOG_PROTOCOL, _("Cannot stat P12 certificate file (%s)\n"),
319                                   strerror(errno));
320                 return NULL;
321         }
322         tmp.data = malloc(s.st_size);
323         memset(tmp.data, 0, s.st_size);
324         tmp.size = s.st_size;
325         if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
326                 log_error(LOG_PROTOCOL, _("Cannot read P12 certificate file (%s)\n"),
327                                   strerror(errno));
328                 free(tmp.data);
329                 return NULL;
330         }
331
332         gnutls_pkcs12_init(&p12);
333
334         if ((r = gnutls_pkcs12_import(p12, &tmp, format, 0)) < 0) {
335                 log_error(LOG_PROTOCOL, _("Cannot import P12 certificate file (%s)\n"),
336                                   gnutls_strerror(r));
337                 gnutls_pkcs12_deinit(p12);
338                 p12 = NULL;
339         }
340         free(tmp.data);
341         debug_print("got p12! %p\n", p12);
342         return p12;
343 }
344
345 static void ssl_certificate_save (SSLCertificate *cert)
346 {
347         gchar *file, *port;
348         FILE *fp;
349
350         file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
351                           "certs", G_DIR_SEPARATOR_S, NULL);
352         
353         if (!is_dir_exist(file))
354                 make_dir_hier(file);
355         g_free(file);
356
357         port = g_strdup_printf("%d", cert->port);
358         file = get_certificate_path(cert->host, port, cert->fingerprint);
359
360         g_free(port);
361         fp = g_fopen(file, "wb");
362         if (fp == NULL) {
363                 g_free(file);
364                 debug_print("Can't save certificate !\n");
365                 return;
366         }
367
368         gnutls_export_X509_fp(fp, cert->x509_cert, GNUTLS_X509_FMT_DER);
369
370         g_free(file);
371         fclose(fp);
372
373 }
374
375 void ssl_certificate_destroy(SSLCertificate *cert) 
376 {
377         if (cert == NULL)
378                 return;
379
380         if (cert->x509_cert)
381                 gnutls_x509_crt_deinit(cert->x509_cert);
382         g_free(cert->host);
383         g_free(cert->fingerprint);
384         g_free(cert);
385         cert = NULL;
386 }
387
388 void ssl_certificate_delete_from_disk(SSLCertificate *cert)
389 {
390         gchar *buf;
391         gchar *file;
392         buf = g_strdup_printf("%d", cert->port);
393         file = get_certificate_path(cert->host, buf, cert->fingerprint);
394         claws_unlink (file);
395         g_free(file);
396         g_free(buf);
397 }
398
399 SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gchar *fingerprint)
400 {
401         gchar *file = NULL;
402         gchar *buf;
403         SSLCertificate *cert = NULL;
404         gnutls_x509_crt_t tmp_x509;
405         FILE *fp = NULL;
406         gboolean must_rename = FALSE;
407
408         buf = g_strdup_printf("%d", port);
409         
410         if (fingerprint != NULL) {
411                 file = get_certificate_path(host, buf, fingerprint);
412                 fp = g_fopen(file, "rb");
413         }
414         if (fp == NULL) {
415                 /* see if we have the old one */
416                 debug_print("didn't get %s\n", file);
417                 g_free(file);
418                 file = get_certificate_path(host, buf, NULL);
419                 fp = g_fopen(file, "rb");
420
421                 if (fp) {
422                         debug_print("got %s\n", file);
423                         must_rename = (fingerprint != NULL);
424                 }
425         } else {
426                 debug_print("got %s first try\n", file);
427         }
428         if (fp == NULL) {
429                 g_free(file);
430                 g_free(buf);
431                 return NULL;
432         }
433         
434         if ((tmp_x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_DER)) != NULL) {
435                 cert = ssl_certificate_new(tmp_x509, host, port);
436                 debug_print("got cert %p\n", cert);
437                 gnutls_x509_crt_deinit(tmp_x509);
438         }
439
440         fclose(fp);
441         g_free(file);
442         
443         if (must_rename) {
444                 gchar *old = get_certificate_path(host, buf, NULL);
445                 gchar *new = get_certificate_path(host, buf, fingerprint);
446                 if (strcmp(old, new))
447                         move_file(old, new, TRUE);
448                 g_free(old);
449                 g_free(new);
450         }
451         g_free(buf);
452
453         return cert;
454 }
455
456 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
457 {
458         char *output_a;
459         char *output_b;
460         size_t cert_size_a = 0, cert_size_b = 0;
461         int r;
462
463         if (cert_a == NULL || cert_b == NULL)
464                 return FALSE;
465
466         if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_a)) 
467             != GNUTLS_E_SHORT_MEMORY_BUFFER) {
468                 g_warning("couldn't gnutls_x509_crt_export to get size a %s\n", gnutls_strerror(r));
469                 return FALSE;
470         }
471
472         if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_b))
473             != GNUTLS_E_SHORT_MEMORY_BUFFER) {
474                 g_warning("couldn't gnutls_x509_crt_export to get size b %s\n", gnutls_strerror(r));
475                 return FALSE;
476         }
477
478         output_a = g_malloc(cert_size_a);
479         output_b = g_malloc(cert_size_b);
480         if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
481                 g_warning("couldn't gnutls_x509_crt_export a %s\n", gnutls_strerror(r));
482                 g_free(output_a);
483                 g_free(output_b);
484                 return FALSE;
485         }
486         if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
487                 g_warning("couldn't gnutls_x509_crt_export b %s\n", gnutls_strerror(r));
488                 g_free(output_a);
489                 g_free(output_b);
490                 return FALSE;
491         }
492         if (cert_size_a != cert_size_b) {
493                 g_warning("size differ %zd %zd\n", cert_size_a, cert_size_b);
494                 g_free(output_a);
495                 g_free(output_b);
496                 return FALSE;
497         }
498         if (memcmp(output_a, output_b, cert_size_a)) {
499                 g_warning("contents differ\n");
500                 g_free(output_a);
501                 g_free(output_b);
502                 return FALSE;
503         }
504         g_free(output_a);
505         g_free(output_b);
506         
507         return TRUE;
508 }
509
510 static guint check_cert(gnutls_x509_crt_t cert)
511 {
512         gnutls_x509_crt_t *ca_list;
513         unsigned int max = 512;
514         unsigned int flags = 0;
515         int r, i;
516         unsigned int status;
517         FILE *fp;
518
519         if (claws_ssl_get_cert_file())
520                 fp = g_fopen(claws_ssl_get_cert_file(), "r");
521         else
522                 return (guint)-1;
523
524         if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &ca_list, &max)) < 0) {
525                 debug_print("cert import failed: %s\n", gnutls_strerror(r));
526                 fclose(fp);
527                 return (guint)-1;
528         }
529
530         r = gnutls_x509_crt_verify(cert, ca_list, max, flags, &status);
531         fclose(fp);
532
533         for (i = 0; i < max; i++)
534                 gnutls_x509_crt_deinit(ca_list[i]);
535         free(ca_list);
536
537         if (r < 0)
538                 return (guint)-1;
539         else
540                 return status;
541
542 }
543
544 char *ssl_certificate_check_signer (gnutls_x509_crt_t cert, guint status) 
545 {
546         if (status == (guint)-1) {
547                 status = check_cert(cert);
548                 if (status == -1)
549                         return g_strdup(_("Uncheckable"));
550         }
551         if (status & GNUTLS_CERT_INVALID) {
552                 if (gnutls_x509_crt_check_issuer(cert, cert))
553                         return g_strdup(_("Self-signed certificate"));
554         }
555         if (status & GNUTLS_CERT_REVOKED)
556                 return g_strdup(_("Revoked certificate"));
557         if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
558                 return g_strdup(_("No certificate issuer found"));
559         if (status & GNUTLS_CERT_SIGNER_NOT_CA)
560                 return g_strdup(_("Certificate issuer is not a CA"));
561
562
563         return NULL;
564 }
565
566 gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, const gchar *host, gushort port)
567 {
568         SSLCertificate *current_cert = NULL;
569         SSLCertificate *known_cert;
570         SSLCertHookData cert_hook_data;
571         gchar *fingerprint;
572         size_t n;
573         unsigned char md[128];  
574
575         current_cert = ssl_certificate_new(x509_cert, host, port);
576         
577         if (current_cert == NULL) {
578                 debug_print("Buggy certificate !\n");
579                 return FALSE;
580         }
581
582         current_cert->status = status;
583         /* fingerprint */
584         n = 128;
585         gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
586         fingerprint = readable_fingerprint(md, n);
587
588         known_cert = ssl_certificate_find(host, port, fingerprint);
589
590         g_free(fingerprint);
591
592         if (known_cert == NULL) {
593                 cert_hook_data.cert = current_cert;
594                 cert_hook_data.old_cert = NULL;
595                 cert_hook_data.expired = FALSE;
596                 cert_hook_data.accept = FALSE;
597                 
598                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
599                 
600                 if (!cert_hook_data.accept) {
601                         ssl_certificate_destroy(current_cert);
602                         return FALSE;
603                 } else {
604                         ssl_certificate_save(current_cert);
605                         ssl_certificate_destroy(current_cert);
606                         return TRUE;
607                 }
608         } else if (!ssl_certificate_compare (current_cert, known_cert)) {
609                 cert_hook_data.cert = current_cert;
610                 cert_hook_data.old_cert = known_cert;
611                 cert_hook_data.expired = FALSE;
612                 cert_hook_data.accept = FALSE;
613                 
614                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
615
616                 if (!cert_hook_data.accept) {
617                         ssl_certificate_destroy(current_cert);
618                         ssl_certificate_destroy(known_cert);
619                         return FALSE;
620                 } else {
621                         ssl_certificate_save(current_cert);
622                         ssl_certificate_destroy(current_cert);
623                         ssl_certificate_destroy(known_cert);
624                         return TRUE;
625                 }
626         } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
627                 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
628                 
629                 if (warned_expired == NULL)
630                         warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
631                 
632                 if (g_hash_table_lookup(warned_expired, tmp)) {
633                         g_free(tmp);
634                         ssl_certificate_destroy(current_cert);
635                         ssl_certificate_destroy(known_cert);
636                         return TRUE;
637                 }
638                         
639                 cert_hook_data.cert = current_cert;
640                 cert_hook_data.old_cert = NULL;
641                 cert_hook_data.expired = TRUE;
642                 cert_hook_data.accept = FALSE;
643                 
644                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
645
646                 if (!cert_hook_data.accept) {
647                         g_free(tmp);
648                         ssl_certificate_destroy(current_cert);
649                         ssl_certificate_destroy(known_cert);
650                         return FALSE;
651                 } else {
652                         g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
653                         ssl_certificate_destroy(current_cert);
654                         ssl_certificate_destroy(known_cert);
655                         return TRUE;
656                 }
657         }
658
659         ssl_certificate_destroy(current_cert);
660         ssl_certificate_destroy(known_cert);
661         return TRUE;
662 }
663
664 gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len, const gchar *host, gushort port)
665 {
666         int ncas = 0, ncrls = 0;
667         gnutls_x509_crt_t *cas = NULL;
668         gnutls_x509_crl_t *crls = NULL;
669         gboolean result = FALSE;
670         int i;
671         gint status;
672
673         if (claws_ssl_get_cert_file()) {
674                 FILE *fp = g_fopen(claws_ssl_get_cert_file(), "rb");
675                 int r = -errno;
676
677                 if (fp) {
678                         r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &cas, &ncas);
679                         fclose(fp);
680                 }
681
682                 if (r < 0)
683                         g_warning("Can't read SSL_CERT_FILE %s: %s\n",
684                                 claws_ssl_get_cert_file(), 
685                                 gnutls_strerror(r));
686         } else {
687                 debug_print("Can't find SSL ca-certificates file\n");
688         }
689
690
691         gnutls_x509_crt_list_verify (certs,
692                              chain_len,
693                              cas, ncas,
694                              NULL, 0,
695                              GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
696                              &status);
697
698         result = ssl_certificate_check(certs[0], status, host, port);
699
700         for (i = 0; i < ncas; i++)
701                 gnutls_x509_crt_deinit(cas[i]);
702         free(cas);
703
704         return result;
705 }
706
707 gnutls_x509_crt_t ssl_certificate_get_x509_from_pem_file(const gchar *file)
708 {
709         gnutls_x509_crt_t x509 = NULL;
710         if (!file)
711                 return NULL;
712         
713         if (is_file_exist(file)) {
714                 FILE *fp = g_fopen(file, "r");
715                 if (fp) {
716                         x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_PEM);
717                         fclose(fp);
718                         return x509;
719                 } else {
720                         log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
721                                   file, strerror(errno));
722                 }
723         } else {
724                 log_error(LOG_PROTOCOL, _("Certificate file %s missing (%s)\n"),
725                           file, strerror(errno));
726         }
727         return NULL;
728 }
729
730 gnutls_x509_privkey_t ssl_certificate_get_pkey_from_pem_file(const gchar *file)
731 {
732         gnutls_x509_privkey_t key = NULL;
733         if (!file)
734                 return NULL;
735         
736         if (is_file_exist(file)) {
737                 FILE *fp = g_fopen(file, "r");
738                 if (fp) {
739                         key = gnutls_import_key_fp(fp, GNUTLS_X509_FMT_PEM);
740                         fclose(fp);
741                         return key;
742                 } else {
743                         log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
744                         file, strerror(errno));
745                 }
746         } else {
747                 log_error(LOG_PROTOCOL, _("Key file %s missing (%s)\n"), file,
748                           strerror(errno));
749         }
750         return NULL;
751 }
752
753 /* From GnuTLS lib/gnutls_x509.c */
754 static int
755 parse_pkcs12 (gnutls_pkcs12_t p12,
756               const char *password,
757               gnutls_x509_privkey_t * key,
758               gnutls_x509_crt_t * cert)
759 {
760   gnutls_pkcs12_bag_t bag = NULL;
761   int index = 0;
762   int ret;
763
764   for (;;)
765     {
766       int elements_in_bag;
767       int i;
768
769       ret = gnutls_pkcs12_bag_init (&bag);
770       if (ret < 0)
771         {
772           bag = NULL;
773           goto done;
774         }
775
776       ret = gnutls_pkcs12_get_bag (p12, index, bag);
777       if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
778         break;
779       if (ret < 0)
780         {
781           goto done;
782         }
783
784       ret = gnutls_pkcs12_bag_get_type (bag, 0);
785       if (ret < 0)
786         {
787           goto done;
788         }
789
790       if (ret == GNUTLS_BAG_ENCRYPTED)
791         {
792           ret = gnutls_pkcs12_bag_decrypt (bag, password);
793           if (ret < 0)
794             {
795               goto done;
796             }
797         }
798
799       elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
800       if (elements_in_bag < 0)
801         {
802           goto done;
803         }
804
805       for (i = 0; i < elements_in_bag; i++)
806         {
807           int type;
808           gnutls_datum_t data;
809
810           type = gnutls_pkcs12_bag_get_type (bag, i);
811           if (type < 0)
812             {
813               goto done;
814             }
815
816           ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
817           if (ret < 0)
818             {
819               goto done;
820             }
821
822           switch (type)
823             {
824             case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
825             case GNUTLS_BAG_PKCS8_KEY:
826               ret = gnutls_x509_privkey_init (key);
827               if (ret < 0)
828                 {
829                   goto done;
830                 }
831
832               ret = gnutls_x509_privkey_import_pkcs8
833                 (*key, &data, GNUTLS_X509_FMT_DER, password,
834                  type == GNUTLS_BAG_PKCS8_KEY ? GNUTLS_PKCS_PLAIN : 0);
835               if (ret < 0)
836                 {
837                   goto done;
838                 }
839               break;
840
841             case GNUTLS_BAG_CERTIFICATE:
842               ret = gnutls_x509_crt_init (cert);
843               if (ret < 0)
844                 {
845                   goto done;
846                 }
847
848               ret =
849                 gnutls_x509_crt_import (*cert, &data, GNUTLS_X509_FMT_DER);
850               if (ret < 0)
851                 {
852                   goto done;
853                 }
854               break;
855
856             case GNUTLS_BAG_ENCRYPTED:
857               /* XXX Bother to recurse one level down?  Unlikely to
858                  use the same password anyway. */
859             case GNUTLS_BAG_EMPTY:
860             default:
861               break;
862             }
863         }
864
865       index++;
866       gnutls_pkcs12_bag_deinit (bag);
867     }
868
869   ret = 0;
870
871 done:
872   if (bag)
873     gnutls_pkcs12_bag_deinit (bag);
874
875   return ret;
876 }
877 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
878                         gnutls_x509_crt_t *x509, gnutls_x509_privkey_t *pkey)
879 {
880         gnutls_pkcs12_t p12 = NULL;
881
882         int r;
883
884         *x509 = NULL;
885         *pkey = NULL;
886         if (!file)
887                 return;
888
889         if (is_file_exist(file)) {
890                 FILE *fp = g_fopen(file, "r");
891                 if (fp) {
892                         p12 = gnutls_import_PKCS12_fp(fp, GNUTLS_X509_FMT_DER);
893                         fclose(fp);
894                         if (!p12) {
895                                 log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
896                         }
897                 } else {
898                         log_error(LOG_PROTOCOL, _("Cannot open P12 certificate file %s (%s)\n"),
899                                   file, strerror(errno));
900                 }
901         } else {
902                 log_error(LOG_PROTOCOL, _("P12 Certificate file %s missing (%s)\n"), file,
903                           strerror(errno));
904         }
905         if (p12 != NULL) {
906                 if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
907                         debug_print("got p12\n");
908                 } else {
909                         log_error(LOG_PROTOCOL, "%s\n", gnutls_strerror(r));
910                 }
911                 gnutls_pkcs12_deinit(p12);
912         }
913 }
914
915 gboolean ssl_certificate_check_subject_cn(SSLCertificate *cert)
916 {
917         return gnutls_x509_crt_check_hostname(cert->x509_cert, cert->host) != 0;
918 }
919
920 gchar *ssl_certificate_get_subject_cn(SSLCertificate *cert)
921 {
922         gchar subject_cn[BUFFSIZE];
923         size_t n = BUFFSIZE;
924
925         if(gnutls_x509_crt_get_dn_by_oid(cert->x509_cert, 
926                 GNUTLS_OID_X520_COMMON_NAME, 0, 0, subject_cn, &n))
927                 strncpy(subject_cn, _("<not in certificate>"), BUFFSIZE);
928
929         return g_strdup(subject_cn);
930 }
931
932 #endif /* USE_GNUTLS */