Rename i2d/d2i internal functions to clearer import/export.
[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 #ifdef USE_GNUTLS
156 static void gnutls_export_X509_fp(FILE *fp, gnutls_x509_crt_t x509_cert, gnutls_x509_crt_fmt_t format)
157 {
158         char output[10*1024];
159         size_t cert_size = 10*1024;
160         int r;
161         
162         if ((r = gnutls_x509_crt_export(x509_cert, format, output, &cert_size)) < 0) {
163                 g_warning("couldn't export cert %s (%zd)\n", gnutls_strerror(r), cert_size);
164                 return;
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\n");
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)?GNUTLS_X509_FMT_DER:GNUTLS_X509_FMT_PEM)) < 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)?GNUTLS_X509_FMT_DER:GNUTLS_X509_FMT_PEM,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 #endif
346
347 static void ssl_certificate_save (SSLCertificate *cert)
348 {
349         gchar *file, *port;
350         FILE *fp;
351
352         file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
353                           "certs", G_DIR_SEPARATOR_S, NULL);
354         
355         if (!is_dir_exist(file))
356                 make_dir_hier(file);
357         g_free(file);
358
359         port = g_strdup_printf("%d", cert->port);
360         file = get_certificate_path(cert->host, port, cert->fingerprint);
361
362         g_free(port);
363         fp = g_fopen(file, "wb");
364         if (fp == NULL) {
365                 g_free(file);
366                 debug_print("Can't save certificate !\n");
367                 return;
368         }
369
370         gnutls_export_X509_fp(fp, cert->x509_cert, GNUTLS_X509_FMT_DER);
371
372         g_free(file);
373         fclose(fp);
374
375 }
376
377 void ssl_certificate_destroy(SSLCertificate *cert) 
378 {
379         if (cert == NULL)
380                 return;
381
382         if (cert->x509_cert)
383                 gnutls_x509_crt_deinit(cert->x509_cert);
384         g_free(cert->host);
385         g_free(cert->fingerprint);
386         g_free(cert);
387         cert = NULL;
388 }
389
390 void ssl_certificate_delete_from_disk(SSLCertificate *cert)
391 {
392         gchar *buf;
393         gchar *file;
394         buf = g_strdup_printf("%d", cert->port);
395         file = get_certificate_path(cert->host, buf, cert->fingerprint);
396         claws_unlink (file);
397         g_free(file);
398         g_free(buf);
399 }
400
401 SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gchar *fingerprint)
402 {
403         gchar *file = NULL;
404         gchar *buf;
405         SSLCertificate *cert = NULL;
406         gnutls_x509_crt_t tmp_x509;
407         FILE *fp = NULL;
408         gboolean must_rename = FALSE;
409
410         buf = g_strdup_printf("%d", port);
411         
412         if (fingerprint != NULL) {
413                 file = get_certificate_path(host, buf, fingerprint);
414                 fp = g_fopen(file, "rb");
415         }
416         if (fp == NULL) {
417                 /* see if we have the old one */
418                 debug_print("didn't get %s\n", file);
419                 g_free(file);
420                 file = get_certificate_path(host, buf, NULL);
421                 fp = g_fopen(file, "rb");
422
423                 if (fp) {
424                         debug_print("got %s\n", file);
425                         must_rename = (fingerprint != NULL);
426                 }
427         } else {
428                 debug_print("got %s first try\n", file);
429         }
430         if (fp == NULL) {
431                 g_free(file);
432                 g_free(buf);
433                 return NULL;
434         }
435         
436         if ((tmp_x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_DER)) != NULL) {
437                 cert = ssl_certificate_new(tmp_x509, host, port);
438                 debug_print("got cert %p\n", cert);
439                 gnutls_x509_crt_deinit(tmp_x509);
440         }
441         fclose(fp);
442         g_free(file);
443         
444         if (must_rename) {
445                 gchar *old = get_certificate_path(host, buf, NULL);
446                 gchar *new = get_certificate_path(host, buf, fingerprint);
447                 if (strcmp(old, new))
448                         move_file(old, new, TRUE);
449                 g_free(old);
450                 g_free(new);
451         }
452         g_free(buf);
453
454         return cert;
455 }
456
457 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
458 {
459         char *output_a;
460         char *output_b;
461         size_t cert_size_a = 0, cert_size_b = 0;
462         int r;
463
464         if (cert_a == NULL || cert_b == NULL)
465                 return FALSE;
466
467         if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_a)) 
468             != GNUTLS_E_SHORT_MEMORY_BUFFER) {
469                 g_warning("couldn't gnutls_x509_crt_export to get size a %s\n", gnutls_strerror(r));
470                 return FALSE;
471         }
472
473         if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_b))
474             != GNUTLS_E_SHORT_MEMORY_BUFFER) {
475                 g_warning("couldn't gnutls_x509_crt_export to get size b %s\n", gnutls_strerror(r));
476                 return FALSE;
477         }
478
479         output_a = g_malloc(cert_size_a);
480         output_b = g_malloc(cert_size_b);
481         if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
482                 g_warning("couldn't gnutls_x509_crt_export a %s\n", gnutls_strerror(r));
483                 g_free(output_a);
484                 g_free(output_b);
485                 return FALSE;
486         }
487         if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
488                 g_warning("couldn't gnutls_x509_crt_export b %s\n", gnutls_strerror(r));
489                 g_free(output_a);
490                 g_free(output_b);
491                 return FALSE;
492         }
493         if (cert_size_a != cert_size_b) {
494                 g_warning("size differ %zd %zd\n", cert_size_a, cert_size_b);
495                 g_free(output_a);
496                 g_free(output_b);
497                 return FALSE;
498         }
499         if (memcmp(output_a, output_b, cert_size_a)) {
500                 g_warning("contents differ\n");
501                 g_free(output_a);
502                 g_free(output_b);
503                 return FALSE;
504         }
505         g_free(output_a);
506         g_free(output_b);
507         
508         return TRUE;
509 }
510
511 static guint check_cert(gnutls_x509_crt_t cert)
512 {
513         gnutls_x509_crt_t *ca_list;
514         unsigned int max = 512;
515         unsigned int flags = 0;
516         int r, i;
517         unsigned int status;
518         FILE *fp;
519
520         if (claws_ssl_get_cert_file())
521                 fp = g_fopen(claws_ssl_get_cert_file(), "r");
522         else
523                 return (guint)-1;
524
525         if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &ca_list, &max)) < 0) {
526                 debug_print("cert import failed: %s\n", gnutls_strerror(r));
527                 fclose(fp);
528                 return (guint)-1;
529         }
530
531         r = gnutls_x509_crt_verify(cert, ca_list, max, flags, &status);
532         fclose(fp);
533
534         for (i = 0; i < max; i++)
535                 gnutls_x509_crt_deinit(ca_list[i]);
536         free(ca_list);
537
538         if (r < 0)
539                 return (guint)-1;
540         else
541                 return status;
542
543 }
544
545 char *ssl_certificate_check_signer (gnutls_x509_crt_t cert, guint status) 
546 {
547         if (status == (guint)-1) {
548                 status = check_cert(cert);
549                 if (status == -1)
550                         return g_strdup(_("Uncheckable"));
551         }
552         if (status & GNUTLS_CERT_INVALID) {
553                 if (gnutls_x509_crt_check_issuer(cert, cert))
554                         return g_strdup(_("Self-signed certificate"));
555         }
556         if (status & GNUTLS_CERT_REVOKED)
557                 return g_strdup(_("Revoked certificate"));
558         if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
559                 return g_strdup(_("No certificate issuer found"));
560         if (status & GNUTLS_CERT_SIGNER_NOT_CA)
561                 return g_strdup(_("Certificate issuer is not a CA"));
562
563
564         return NULL;
565 }
566
567 gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, const gchar *host, gushort port)
568 {
569         SSLCertificate *current_cert = NULL;
570         SSLCertificate *known_cert;
571         SSLCertHookData cert_hook_data;
572         gchar *fingerprint;
573         size_t n;
574         unsigned char md[128];  
575
576         current_cert = ssl_certificate_new(x509_cert, host, port);
577         
578         if (current_cert == NULL) {
579                 debug_print("Buggy certificate !\n");
580                 return FALSE;
581         }
582
583         current_cert->status = status;
584         /* fingerprint */
585         n = 128;
586         gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
587         fingerprint = readable_fingerprint(md, n);
588
589         known_cert = ssl_certificate_find(host, port, fingerprint);
590
591         g_free(fingerprint);
592
593         if (known_cert == NULL) {
594                 cert_hook_data.cert = current_cert;
595                 cert_hook_data.old_cert = NULL;
596                 cert_hook_data.expired = FALSE;
597                 cert_hook_data.accept = FALSE;
598                 
599                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
600                 
601                 if (!cert_hook_data.accept) {
602                         ssl_certificate_destroy(current_cert);
603                         return FALSE;
604                 } else {
605                         ssl_certificate_save(current_cert);
606                         ssl_certificate_destroy(current_cert);
607                         return TRUE;
608                 }
609         } else if (!ssl_certificate_compare (current_cert, known_cert)) {
610                 cert_hook_data.cert = current_cert;
611                 cert_hook_data.old_cert = known_cert;
612                 cert_hook_data.expired = FALSE;
613                 cert_hook_data.accept = FALSE;
614                 
615                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
616
617                 if (!cert_hook_data.accept) {
618                         ssl_certificate_destroy(current_cert);
619                         ssl_certificate_destroy(known_cert);
620                         return FALSE;
621                 } else {
622                         ssl_certificate_save(current_cert);
623                         ssl_certificate_destroy(current_cert);
624                         ssl_certificate_destroy(known_cert);
625                         return TRUE;
626                 }
627         } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
628                 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
629                 
630                 if (warned_expired == NULL)
631                         warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
632                 
633                 if (g_hash_table_lookup(warned_expired, tmp)) {
634                         g_free(tmp);
635                         ssl_certificate_destroy(current_cert);
636                         ssl_certificate_destroy(known_cert);
637                         return TRUE;
638                 }
639                         
640                 cert_hook_data.cert = current_cert;
641                 cert_hook_data.old_cert = NULL;
642                 cert_hook_data.expired = TRUE;
643                 cert_hook_data.accept = FALSE;
644                 
645                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
646
647                 if (!cert_hook_data.accept) {
648                         g_free(tmp);
649                         ssl_certificate_destroy(current_cert);
650                         ssl_certificate_destroy(known_cert);
651                         return FALSE;
652                 } else {
653                         g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
654                         ssl_certificate_destroy(current_cert);
655                         ssl_certificate_destroy(known_cert);
656                         return TRUE;
657                 }
658         }
659
660         ssl_certificate_destroy(current_cert);
661         ssl_certificate_destroy(known_cert);
662         return TRUE;
663 }
664
665 gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len, const gchar *host, gushort port)
666 {
667         int ncas = 0, ncrls = 0;
668         gnutls_x509_crt_t *cas = NULL;
669         gnutls_x509_crl_t *crls = NULL;
670         gboolean result = FALSE;
671         int i;
672         gint status;
673
674         if (claws_ssl_get_cert_file()) {
675                 FILE *fp = g_fopen(claws_ssl_get_cert_file(), "rb");
676                 int r = -errno;
677
678                 if (fp) {
679                         r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &cas, &ncas);
680                         fclose(fp);
681                 }
682
683                 if (r < 0)
684                         g_warning("Can't read SSL_CERT_FILE %s: %s\n",
685                                 claws_ssl_get_cert_file(), 
686                                 gnutls_strerror(r));
687         } else {
688                 debug_print("Can't find SSL ca-certificates file\n");
689         }
690
691
692         gnutls_x509_crt_list_verify (certs,
693                              chain_len,
694                              cas, ncas,
695                              NULL, 0,
696                              GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
697                              &status);
698
699         result = ssl_certificate_check(certs[0], status, host, port);
700
701         for (i = 0; i < ncas; i++)
702                 gnutls_x509_crt_deinit(cas[i]);
703         free(cas);
704
705         return result;
706 }
707
708 gnutls_x509_crt_t ssl_certificate_get_x509_from_pem_file(const gchar *file)
709 {
710         gnutls_x509_crt_t x509 = NULL;
711         if (!file)
712                 return NULL;
713         
714         if (is_file_exist(file)) {
715                 FILE *fp = g_fopen(file, "r");
716                 if (fp) {
717                         x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_PEM);
718                         fclose(fp);
719                         return x509;
720                 } else {
721                         log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
722                                   file, strerror(errno));
723                 }
724         } else {
725                 log_error(LOG_PROTOCOL, _("Certificate file %s missing (%s)\n"),
726                           file, strerror(errno));
727         }
728         return NULL;
729 }
730
731 gnutls_x509_privkey_t ssl_certificate_get_pkey_from_pem_file(const gchar *file)
732 {
733         gnutls_x509_privkey_t key = NULL;
734         if (!file)
735                 return NULL;
736         
737         if (is_file_exist(file)) {
738                 FILE *fp = g_fopen(file, "r");
739                 if (fp) {
740                         key = gnutls_import_key_fp(fp, GNUTLS_X509_FMT_PEM);
741                         fclose(fp);
742                         return key;
743                 } else {
744                         log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
745                         file, strerror(errno));
746                 }
747         } else {
748                 log_error(LOG_PROTOCOL, _("Key file %s missing (%s)\n"), file,
749                           strerror(errno));
750         }
751         return NULL;
752 }
753
754 /* From GnuTLS lib/gnutls_x509.c */
755 static int
756 parse_pkcs12 (gnutls_pkcs12_t p12,
757               const char *password,
758               gnutls_x509_privkey_t * key,
759               gnutls_x509_crt_t * cert)
760 {
761   gnutls_pkcs12_bag_t bag = NULL;
762   int index = 0;
763   int ret;
764
765   for (;;)
766     {
767       int elements_in_bag;
768       int i;
769
770       ret = gnutls_pkcs12_bag_init (&bag);
771       if (ret < 0)
772         {
773           bag = NULL;
774           goto done;
775         }
776
777       ret = gnutls_pkcs12_get_bag (p12, index, bag);
778       if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
779         break;
780       if (ret < 0)
781         {
782           goto done;
783         }
784
785       ret = gnutls_pkcs12_bag_get_type (bag, 0);
786       if (ret < 0)
787         {
788           goto done;
789         }
790
791       if (ret == GNUTLS_BAG_ENCRYPTED)
792         {
793           ret = gnutls_pkcs12_bag_decrypt (bag, password);
794           if (ret < 0)
795             {
796               goto done;
797             }
798         }
799
800       elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
801       if (elements_in_bag < 0)
802         {
803           goto done;
804         }
805
806       for (i = 0; i < elements_in_bag; i++)
807         {
808           int type;
809           gnutls_datum_t data;
810
811           type = gnutls_pkcs12_bag_get_type (bag, i);
812           if (type < 0)
813             {
814               goto done;
815             }
816
817           ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
818           if (ret < 0)
819             {
820               goto done;
821             }
822
823           switch (type)
824             {
825             case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
826             case GNUTLS_BAG_PKCS8_KEY:
827               ret = gnutls_x509_privkey_init (key);
828               if (ret < 0)
829                 {
830                   goto done;
831                 }
832
833               ret = gnutls_x509_privkey_import_pkcs8
834                 (*key, &data, GNUTLS_X509_FMT_DER, password,
835                  type == GNUTLS_BAG_PKCS8_KEY ? GNUTLS_PKCS_PLAIN : 0);
836               if (ret < 0)
837                 {
838                   goto done;
839                 }
840               break;
841
842             case GNUTLS_BAG_CERTIFICATE:
843               ret = gnutls_x509_crt_init (cert);
844               if (ret < 0)
845                 {
846                   goto done;
847                 }
848
849               ret =
850                 gnutls_x509_crt_import (*cert, &data, GNUTLS_X509_FMT_DER);
851               if (ret < 0)
852                 {
853                   goto done;
854                 }
855               break;
856
857             case GNUTLS_BAG_ENCRYPTED:
858               /* XXX Bother to recurse one level down?  Unlikely to
859                  use the same password anyway. */
860             case GNUTLS_BAG_EMPTY:
861             default:
862               break;
863             }
864         }
865
866       index++;
867       gnutls_pkcs12_bag_deinit (bag);
868     }
869
870   ret = 0;
871
872 done:
873   if (bag)
874     gnutls_pkcs12_bag_deinit (bag);
875
876   return ret;
877 }
878 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
879                         gnutls_x509_crt_t *x509, gnutls_x509_privkey_t *pkey)
880 {
881         gnutls_pkcs12_t p12 = NULL;
882
883         int r;
884
885         *x509 = NULL;
886         *pkey = NULL;
887         if (!file)
888                 return;
889
890         if (is_file_exist(file)) {
891                 FILE *fp = g_fopen(file, "r");
892                 if (fp) {
893                         p12 = gnutls_import_PKCS12_fp(fp, GNUTLS_X509_FMT_DER);
894                         fclose(fp);
895                         if (!p12) {
896                                 log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
897                         }
898                 } else {
899                         log_error(LOG_PROTOCOL, _("Cannot open P12 certificate file %s (%s)\n"),
900                                   file, strerror(errno));
901                 }
902         } else {
903                 log_error(LOG_PROTOCOL, _("P12 Certificate file %s missing (%s)\n"), file,
904                           strerror(errno));
905         }
906         if (p12 != NULL) {
907                 if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
908                         debug_print("got p12\n");
909                 } else {
910                         log_error(LOG_PROTOCOL, "%s\n", gnutls_strerror(r));
911                 }
912                 gnutls_pkcs12_deinit(p12);
913         }
914 }
915
916 gboolean ssl_certificate_check_subject_cn(SSLCertificate *cert)
917 {
918         return gnutls_x509_crt_check_hostname(cert->x509_cert, cert->host) != 0;
919 }
920
921 gchar *ssl_certificate_get_subject_cn(SSLCertificate *cert)
922 {
923         gchar subject_cn[BUFFSIZE];
924         size_t n = BUFFSIZE;
925
926         if(gnutls_x509_crt_get_dn_by_oid(cert->x509_cert, 
927                 GNUTLS_OID_X520_COMMON_NAME, 0, 0, subject_cn, &n))
928                 strncpy(subject_cn, _("<not in certificate>"), BUFFSIZE);
929
930         return g_strdup(subject_cn);
931 }
932
933 #endif /* USE_GNUTLS */