Implement certificate chain retrieval and passing. CAs are not loaded
[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_i2d_X509_fp(FILE *fp, gnutls_x509_crt_t x509_cert)
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, GNUTLS_X509_FMT_DER, 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 gnutls_x509_crt_t gnutls_d2i_X509_fp(FILE *fp, int format)
211 {
212         gnutls_x509_crt_t cert = NULL;
213         gnutls_datum_t tmp;
214         struct stat s;
215         int r;
216         if (fstat(fileno(fp), &s) < 0) {
217                 perror("fstat");
218                 return NULL;
219         }
220         tmp.data = malloc(s.st_size);
221         memset(tmp.data, 0, s.st_size);
222         tmp.size = s.st_size;
223         if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
224                 perror("fread");
225                 free(tmp.data);
226                 return NULL;
227         }
228
229         gnutls_x509_crt_init(&cert);
230         if ((r = gnutls_x509_crt_import(cert, &tmp, (format == 0)?GNUTLS_X509_FMT_DER:GNUTLS_X509_FMT_PEM)) < 0) {
231                 debug_print("cert import failed: %s\n", gnutls_strerror(r));
232                 gnutls_x509_crt_deinit(cert);
233                 cert = NULL;
234         }
235         free(tmp.data);
236         debug_print("got cert! %p\n", cert);
237         return cert;
238 }
239
240 static gnutls_x509_privkey_t gnutls_d2i_key_fp(FILE *fp, int format)
241 {
242         gnutls_x509_privkey_t key = NULL;
243         gnutls_datum_t tmp;
244         struct stat s;
245         int r;
246         if (fstat(fileno(fp), &s) < 0) {
247                 perror("fstat");
248                 return NULL;
249         }
250         tmp.data = malloc(s.st_size);
251         memset(tmp.data, 0, s.st_size);
252         tmp.size = s.st_size;
253         if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
254                 perror("fread");
255                 free(tmp.data);
256                 return NULL;
257         }
258
259         gnutls_x509_privkey_init(&key);
260         if ((r = gnutls_x509_privkey_import(key, &tmp, (format == 0)?GNUTLS_X509_FMT_DER:GNUTLS_X509_FMT_PEM)) < 0) {
261                 debug_print("key import failed: %s\n", gnutls_strerror(r));
262                 gnutls_x509_privkey_deinit(key);
263                 key = NULL;
264         }
265         free(tmp.data);
266         debug_print("got key! %p\n", key);
267         return key;
268 }
269
270 static gnutls_pkcs12_t gnutls_d2i_PKCS12_fp(FILE *fp, int format)
271 {
272         gnutls_pkcs12_t p12 = NULL;
273         gnutls_datum_t tmp;
274         struct stat s;
275         int r;
276         if (fstat(fileno(fp), &s) < 0) {
277                 log_error(LOG_PROTOCOL, _("Cannot stat P12 certificate file (%s)\n"),
278                                   strerror(errno));
279                 return NULL;
280         }
281         tmp.data = malloc(s.st_size);
282         memset(tmp.data, 0, s.st_size);
283         tmp.size = s.st_size;
284         if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
285                 log_error(LOG_PROTOCOL, _("Cannot read P12 certificate file (%s)\n"),
286                                   strerror(errno));
287                 free(tmp.data);
288                 return NULL;
289         }
290
291         gnutls_pkcs12_init(&p12);
292
293         if ((r = gnutls_pkcs12_import(p12, &tmp, (format == 0)?GNUTLS_X509_FMT_DER:GNUTLS_X509_FMT_PEM,0)) < 0) {
294                 log_error(LOG_PROTOCOL, _("Cannot import P12 certificate file (%s)\n"),
295                                   gnutls_strerror(r));
296                 gnutls_pkcs12_deinit(p12);
297                 p12 = NULL;
298         }
299         free(tmp.data);
300         debug_print("got p12! %p\n", p12);
301         return p12;
302 }
303
304 #endif
305
306 static void ssl_certificate_save (SSLCertificate *cert)
307 {
308         gchar *file, *port;
309         FILE *fp;
310
311         file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
312                           "certs", G_DIR_SEPARATOR_S, NULL);
313         
314         if (!is_dir_exist(file))
315                 make_dir_hier(file);
316         g_free(file);
317
318         port = g_strdup_printf("%d", cert->port);
319         file = get_certificate_path(cert->host, port, cert->fingerprint);
320
321         g_free(port);
322         fp = g_fopen(file, "wb");
323         if (fp == NULL) {
324                 g_free(file);
325                 debug_print("Can't save certificate !\n");
326                 return;
327         }
328 #ifdef USE_GNUTLS
329         gnutls_i2d_X509_fp(fp, cert->x509_cert);
330 #else
331         i2d_X509_fp(fp, cert->x509_cert);
332 #endif
333         g_free(file);
334         fclose(fp);
335
336 }
337
338 void ssl_certificate_destroy(SSLCertificate *cert) 
339 {
340         if (cert == NULL)
341                 return;
342
343         if (cert->x509_cert)
344                 gnutls_x509_crt_deinit(cert->x509_cert);
345         g_free(cert->host);
346         g_free(cert->fingerprint);
347         g_free(cert);
348         cert = NULL;
349 }
350
351 void ssl_certificate_delete_from_disk(SSLCertificate *cert)
352 {
353         gchar *buf;
354         gchar *file;
355         buf = g_strdup_printf("%d", cert->port);
356         file = get_certificate_path(cert->host, buf, cert->fingerprint);
357         claws_unlink (file);
358         g_free(file);
359         g_free(buf);
360 }
361
362 SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gchar *fingerprint)
363 {
364         gchar *file = NULL;
365         gchar *buf;
366         SSLCertificate *cert = NULL;
367         gnutls_x509_crt_t tmp_x509;
368         FILE *fp = NULL;
369         gboolean must_rename = FALSE;
370
371         buf = g_strdup_printf("%d", port);
372         
373         if (fingerprint != NULL) {
374                 file = get_certificate_path(host, buf, fingerprint);
375                 fp = g_fopen(file, "rb");
376         }
377         if (fp == NULL) {
378                 /* see if we have the old one */
379                 debug_print("didn't get %s\n", file);
380                 g_free(file);
381                 file = get_certificate_path(host, buf, NULL);
382                 fp = g_fopen(file, "rb");
383
384                 if (fp) {
385                         debug_print("got %s\n", file);
386                         must_rename = (fingerprint != NULL);
387                 }
388         } else {
389                 debug_print("got %s first try\n", file);
390         }
391         if (fp == NULL) {
392                 g_free(file);
393                 g_free(buf);
394                 return NULL;
395         }
396         
397         if ((tmp_x509 = gnutls_d2i_X509_fp(fp, 0)) != NULL) {
398                 cert = ssl_certificate_new(tmp_x509, host, port);
399                 debug_print("got cert %p\n", cert);
400                 gnutls_x509_crt_deinit(tmp_x509);
401         }
402         fclose(fp);
403         g_free(file);
404         
405         if (must_rename) {
406                 gchar *old = get_certificate_path(host, buf, NULL);
407                 gchar *new = get_certificate_path(host, buf, fingerprint);
408                 if (strcmp(old, new))
409                         move_file(old, new, TRUE);
410                 g_free(old);
411                 g_free(new);
412         }
413         g_free(buf);
414
415         return cert;
416 }
417
418 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
419 {
420         char *output_a;
421         char *output_b;
422         size_t cert_size_a = 0, cert_size_b = 0;
423         int r;
424
425         if (cert_a == NULL || cert_b == NULL)
426                 return FALSE;
427
428         if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_a)) 
429             != GNUTLS_E_SHORT_MEMORY_BUFFER) {
430                 g_warning("couldn't gnutls_x509_crt_export to get size a %s\n", gnutls_strerror(r));
431                 return FALSE;
432         }
433
434         if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_b))
435             != GNUTLS_E_SHORT_MEMORY_BUFFER) {
436                 g_warning("couldn't gnutls_x509_crt_export to get size b %s\n", gnutls_strerror(r));
437                 return FALSE;
438         }
439
440         output_a = g_malloc(cert_size_a);
441         output_b = g_malloc(cert_size_b);
442         if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
443                 g_warning("couldn't gnutls_x509_crt_export a %s\n", gnutls_strerror(r));
444                 g_free(output_a);
445                 g_free(output_b);
446                 return FALSE;
447         }
448         if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
449                 g_warning("couldn't gnutls_x509_crt_export b %s\n", gnutls_strerror(r));
450                 g_free(output_a);
451                 g_free(output_b);
452                 return FALSE;
453         }
454         if (cert_size_a != cert_size_b) {
455                 g_warning("size differ %zd %zd\n", cert_size_a, cert_size_b);
456                 g_free(output_a);
457                 g_free(output_b);
458                 return FALSE;
459         }
460         if (memcmp(output_a, output_b, cert_size_a)) {
461                 g_warning("contents differ\n");
462                 g_free(output_a);
463                 g_free(output_b);
464                 return FALSE;
465         }
466         g_free(output_a);
467         g_free(output_b);
468         
469         return TRUE;
470 }
471
472 static guint check_cert(gnutls_x509_crt_t cert)
473 {
474         gnutls_x509_crt_t *ca_list;
475         unsigned int max = 512;
476         unsigned int flags = 0;
477         gnutls_datum_t tmp;
478         struct stat s;
479         int r, i;
480         unsigned int status;
481         FILE *fp;
482
483         if (claws_ssl_get_cert_file())
484                 fp = g_fopen(claws_ssl_get_cert_file(), "r");
485         else
486                 return (guint)-1;
487
488         if (fstat(fileno(fp), &s) < 0) {
489                 perror("fstat");
490                 fclose(fp);
491                 return (guint)-1;
492         }
493
494         ca_list=(gnutls_x509_crt_t*)malloc(max*sizeof(gnutls_x509_crt_t));
495         tmp.data = malloc(s.st_size);
496         memset(tmp.data, 0, s.st_size);
497         tmp.size = s.st_size;
498         if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
499                 perror("fread");
500                 free(tmp.data);
501                 free(ca_list);
502                 fclose(fp);
503                 return (guint)-1;
504         }
505
506         if ((r = gnutls_x509_crt_list_import(ca_list, &max, 
507                         &tmp, GNUTLS_X509_FMT_PEM, flags)) < 0) {
508                 debug_print("cert import failed: %s\n", gnutls_strerror(r));
509                 free(tmp.data);
510                 free(ca_list);
511                 fclose(fp);
512                 return (guint)-1;
513         }
514         free(tmp.data);
515         debug_print("got %d certs in ca_list! %p\n", max, &ca_list);
516         r = gnutls_x509_crt_verify(cert, ca_list, max, flags, &status);
517         fclose(fp);
518
519         for (i = 0; i < max; i++)
520                 gnutls_x509_crt_deinit(ca_list[i]);
521         free(ca_list);
522
523         if (r < 0)
524                 return (guint)-1;
525         else
526                 return status;
527
528 }
529
530 char *ssl_certificate_check_signer (gnutls_x509_crt_t cert, guint status) 
531 {
532         if (status == (guint)-1) {
533                 status = check_cert(cert);
534                 if (status == -1)
535                         return g_strdup(_("Uncheckable"));
536         }
537         if (status & GNUTLS_CERT_INVALID) {
538                 if (gnutls_x509_crt_check_issuer(cert, cert))
539                         return g_strdup(_("Self-signed certificate"));
540         }
541         if (status & GNUTLS_CERT_REVOKED)
542                 return g_strdup(_("Revoked certificate"));
543         if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
544                 return g_strdup(_("No certificate issuer found"));
545         if (status & GNUTLS_CERT_SIGNER_NOT_CA)
546                 return g_strdup(_("Certificate issuer is not a CA"));
547
548
549         return NULL;
550 }
551
552 gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, const gchar *host, gushort port)
553 {
554         SSLCertificate *current_cert = NULL;
555         SSLCertificate *known_cert;
556         SSLCertHookData cert_hook_data;
557         gchar *fingerprint;
558         size_t n;
559         unsigned char md[128];  
560
561         current_cert = ssl_certificate_new(x509_cert, host, port);
562         
563         if (current_cert == NULL) {
564                 debug_print("Buggy certificate !\n");
565                 return FALSE;
566         }
567
568         current_cert->status = status;
569         /* fingerprint */
570         n = 128;
571         gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
572         fingerprint = readable_fingerprint(md, n);
573
574         known_cert = ssl_certificate_find(host, port, fingerprint);
575
576         g_free(fingerprint);
577
578         if (known_cert == NULL) {
579                 cert_hook_data.cert = current_cert;
580                 cert_hook_data.old_cert = NULL;
581                 cert_hook_data.expired = FALSE;
582                 cert_hook_data.accept = FALSE;
583                 
584                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
585                 
586                 if (!cert_hook_data.accept) {
587                         ssl_certificate_destroy(current_cert);
588                         return FALSE;
589                 } else {
590                         ssl_certificate_save(current_cert);
591                         ssl_certificate_destroy(current_cert);
592                         return TRUE;
593                 }
594         } else if (!ssl_certificate_compare (current_cert, known_cert)) {
595                 cert_hook_data.cert = current_cert;
596                 cert_hook_data.old_cert = known_cert;
597                 cert_hook_data.expired = FALSE;
598                 cert_hook_data.accept = FALSE;
599                 
600                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
601
602                 if (!cert_hook_data.accept) {
603                         ssl_certificate_destroy(current_cert);
604                         ssl_certificate_destroy(known_cert);
605                         return FALSE;
606                 } else {
607                         ssl_certificate_save(current_cert);
608                         ssl_certificate_destroy(current_cert);
609                         ssl_certificate_destroy(known_cert);
610                         return TRUE;
611                 }
612         } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
613                 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
614                 
615                 if (warned_expired == NULL)
616                         warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
617                 
618                 if (g_hash_table_lookup(warned_expired, tmp)) {
619                         g_free(tmp);
620                         ssl_certificate_destroy(current_cert);
621                         ssl_certificate_destroy(known_cert);
622                         return TRUE;
623                 }
624                         
625                 cert_hook_data.cert = current_cert;
626                 cert_hook_data.old_cert = NULL;
627                 cert_hook_data.expired = TRUE;
628                 cert_hook_data.accept = FALSE;
629                 
630                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
631
632                 if (!cert_hook_data.accept) {
633                         g_free(tmp);
634                         ssl_certificate_destroy(current_cert);
635                         ssl_certificate_destroy(known_cert);
636                         return FALSE;
637                 } else {
638                         g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
639                         ssl_certificate_destroy(current_cert);
640                         ssl_certificate_destroy(known_cert);
641                         return TRUE;
642                 }
643         }
644
645         ssl_certificate_destroy(current_cert);
646         ssl_certificate_destroy(known_cert);
647         return TRUE;
648 }
649
650 gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len, const gchar *host, gushort port)
651 {
652         gboolean result = FALSE;
653         gint status;
654
655         gnutls_x509_crt_list_verify (certs,
656                              chain_len,
657                              NULL, 0,
658                              NULL, 0,
659                              GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
660                              &status);
661
662         result = ssl_certificate_check(certs[0], status, host, port);
663
664         return result;
665 }
666
667 gnutls_x509_crt_t ssl_certificate_get_x509_from_pem_file(const gchar *file)
668 {
669         gnutls_x509_crt_t x509 = NULL;
670         if (!file)
671                 return NULL;
672         
673         if (is_file_exist(file)) {
674                 FILE *fp = g_fopen(file, "r");
675                 if (fp) {
676                         x509 = gnutls_d2i_X509_fp(fp, 1);
677                         fclose(fp);
678                         return x509;
679                 } else {
680                         log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
681                                   file, strerror(errno));
682                 }
683         } else {
684                 log_error(LOG_PROTOCOL, _("Certificate file %s missing (%s)\n"),
685                           file, strerror(errno));
686         }
687         return NULL;
688 }
689
690 gnutls_x509_privkey_t ssl_certificate_get_pkey_from_pem_file(const gchar *file)
691 {
692         gnutls_x509_privkey_t key = NULL;
693         if (!file)
694                 return NULL;
695         
696         if (is_file_exist(file)) {
697                 FILE *fp = g_fopen(file, "r");
698                 if (fp) {
699                         key = gnutls_d2i_key_fp(fp, 1);
700                         fclose(fp);
701                         return key;
702                 } else {
703                         log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
704                         file, strerror(errno));
705                 }
706         } else {
707                 log_error(LOG_PROTOCOL, _("Key file %s missing (%s)\n"), file,
708                           strerror(errno));
709         }
710         return NULL;
711 }
712
713 /* From GnuTLS lib/gnutls_x509.c */
714 static int
715 parse_pkcs12 (gnutls_pkcs12_t p12,
716               const char *password,
717               gnutls_x509_privkey_t * key,
718               gnutls_x509_crt_t * cert)
719 {
720   gnutls_pkcs12_bag_t bag = NULL;
721   int index = 0;
722   int ret;
723
724   for (;;)
725     {
726       int elements_in_bag;
727       int i;
728
729       ret = gnutls_pkcs12_bag_init (&bag);
730       if (ret < 0)
731         {
732           bag = NULL;
733           goto done;
734         }
735
736       ret = gnutls_pkcs12_get_bag (p12, index, bag);
737       if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
738         break;
739       if (ret < 0)
740         {
741           goto done;
742         }
743
744       ret = gnutls_pkcs12_bag_get_type (bag, 0);
745       if (ret < 0)
746         {
747           goto done;
748         }
749
750       if (ret == GNUTLS_BAG_ENCRYPTED)
751         {
752           ret = gnutls_pkcs12_bag_decrypt (bag, password);
753           if (ret < 0)
754             {
755               goto done;
756             }
757         }
758
759       elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
760       if (elements_in_bag < 0)
761         {
762           goto done;
763         }
764
765       for (i = 0; i < elements_in_bag; i++)
766         {
767           int type;
768           gnutls_datum_t data;
769
770           type = gnutls_pkcs12_bag_get_type (bag, i);
771           if (type < 0)
772             {
773               goto done;
774             }
775
776           ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
777           if (ret < 0)
778             {
779               goto done;
780             }
781
782           switch (type)
783             {
784             case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
785             case GNUTLS_BAG_PKCS8_KEY:
786               ret = gnutls_x509_privkey_init (key);
787               if (ret < 0)
788                 {
789                   goto done;
790                 }
791
792               ret = gnutls_x509_privkey_import_pkcs8
793                 (*key, &data, GNUTLS_X509_FMT_DER, password,
794                  type == GNUTLS_BAG_PKCS8_KEY ? GNUTLS_PKCS_PLAIN : 0);
795               if (ret < 0)
796                 {
797                   goto done;
798                 }
799               break;
800
801             case GNUTLS_BAG_CERTIFICATE:
802               ret = gnutls_x509_crt_init (cert);
803               if (ret < 0)
804                 {
805                   goto done;
806                 }
807
808               ret =
809                 gnutls_x509_crt_import (*cert, &data, GNUTLS_X509_FMT_DER);
810               if (ret < 0)
811                 {
812                   goto done;
813                 }
814               break;
815
816             case GNUTLS_BAG_ENCRYPTED:
817               /* XXX Bother to recurse one level down?  Unlikely to
818                  use the same password anyway. */
819             case GNUTLS_BAG_EMPTY:
820             default:
821               break;
822             }
823         }
824
825       index++;
826       gnutls_pkcs12_bag_deinit (bag);
827     }
828
829   ret = 0;
830
831 done:
832   if (bag)
833     gnutls_pkcs12_bag_deinit (bag);
834
835   return ret;
836 }
837 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
838                         gnutls_x509_crt_t *x509, gnutls_x509_privkey_t *pkey)
839 {
840         gnutls_pkcs12_t p12 = NULL;
841
842         int r;
843
844         *x509 = NULL;
845         *pkey = NULL;
846         if (!file)
847                 return;
848
849         if (is_file_exist(file)) {
850                 FILE *fp = g_fopen(file, "r");
851                 if (fp) {
852                         p12 = gnutls_d2i_PKCS12_fp(fp, 0);
853                         fclose(fp);
854                         if (!p12) {
855                                 log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
856                         }
857                 } else {
858                         log_error(LOG_PROTOCOL, _("Cannot open P12 certificate file %s (%s)\n"),
859                                   file, strerror(errno));
860                 }
861         } else {
862                 log_error(LOG_PROTOCOL, _("P12 Certificate file %s missing (%s)\n"), file,
863                           strerror(errno));
864         }
865         if (p12 != NULL) {
866                 if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
867                         debug_print("got p12\n");
868                 } else {
869                         log_error(LOG_PROTOCOL, "%s\n", gnutls_strerror(r));
870                 }
871                 gnutls_pkcs12_deinit(p12);
872         }
873 }
874
875 gboolean ssl_certificate_check_subject_cn(SSLCertificate *cert)
876 {
877         return gnutls_x509_crt_check_hostname(cert->x509_cert, cert->host) != 0;
878 }
879
880 gchar *ssl_certificate_get_subject_cn(SSLCertificate *cert)
881 {
882         gchar subject_cn[BUFFSIZE];
883         size_t n = BUFFSIZE;
884
885         if(gnutls_x509_crt_get_dn_by_oid(cert->x509_cert, 
886                 GNUTLS_OID_X520_COMMON_NAME, 0, 0, subject_cn, &n))
887                 strncpy(subject_cn, _("<not in certificate>"), BUFFSIZE);
888
889         return g_strdup(subject_cn);
890 }
891
892 #endif /* USE_GNUTLS */