2012-10-17 [colin] 3.8.1cvs100
[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 x509_crt_copy(gnutls_x509_crt src)
92 {
93     int ret;
94     size_t size;
95     gnutls_datum tmp;
96     gnutls_x509_crt 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 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 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 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 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 gnutls_d2i_X509_fp(FILE *fp, int format)
211 {
212         gnutls_x509_crt cert = NULL;
213         gnutls_datum 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 gnutls_d2i_key_fp(FILE *fp, int format)
241 {
242         gnutls_x509_privkey key = NULL;
243         gnutls_datum 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 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 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 cert)
473 {
474         gnutls_x509_crt *ca_list;
475         unsigned int max = 512;
476         unsigned int flags = 0;
477         gnutls_datum 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 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 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 gnutls_x509_crt ssl_certificate_get_x509_from_pem_file(const gchar *file)
651 {
652         gnutls_x509_crt x509 = NULL;
653         if (!file)
654                 return NULL;
655         
656         if (is_file_exist(file)) {
657                 FILE *fp = g_fopen(file, "r");
658                 if (fp) {
659                         x509 = gnutls_d2i_X509_fp(fp, 1);
660                         fclose(fp);
661                         return x509;
662                 } else {
663                         log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
664                                   file, strerror(errno));
665                 }
666         } else {
667                 log_error(LOG_PROTOCOL, _("Certificate file %s missing (%s)\n"),
668                           file, strerror(errno));
669         }
670         return NULL;
671 }
672
673 gnutls_x509_privkey ssl_certificate_get_pkey_from_pem_file(const gchar *file)
674 {
675         gnutls_x509_privkey key = NULL;
676         if (!file)
677                 return NULL;
678         
679         if (is_file_exist(file)) {
680                 FILE *fp = g_fopen(file, "r");
681                 if (fp) {
682                         key = gnutls_d2i_key_fp(fp, 1);
683                         fclose(fp);
684                         return key;
685                 } else {
686                         log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
687                         file, strerror(errno));
688                 }
689         } else {
690                 log_error(LOG_PROTOCOL, _("Key file %s missing (%s)\n"), file,
691                           strerror(errno));
692         }
693         return NULL;
694 }
695
696 /* From GnuTLS lib/gnutls_x509.c */
697 static int
698 parse_pkcs12 (gnutls_pkcs12_t p12,
699               const char *password,
700               gnutls_x509_privkey * key,
701               gnutls_x509_crt_t * cert)
702 {
703   gnutls_pkcs12_bag_t bag = NULL;
704   int index = 0;
705   int ret;
706
707   for (;;)
708     {
709       int elements_in_bag;
710       int i;
711
712       ret = gnutls_pkcs12_bag_init (&bag);
713       if (ret < 0)
714         {
715           bag = NULL;
716           goto done;
717         }
718
719       ret = gnutls_pkcs12_get_bag (p12, index, bag);
720       if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
721         break;
722       if (ret < 0)
723         {
724           goto done;
725         }
726
727       ret = gnutls_pkcs12_bag_get_type (bag, 0);
728       if (ret < 0)
729         {
730           goto done;
731         }
732
733       if (ret == GNUTLS_BAG_ENCRYPTED)
734         {
735           ret = gnutls_pkcs12_bag_decrypt (bag, password);
736           if (ret < 0)
737             {
738               goto done;
739             }
740         }
741
742       elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
743       if (elements_in_bag < 0)
744         {
745           goto done;
746         }
747
748       for (i = 0; i < elements_in_bag; i++)
749         {
750           int type;
751           gnutls_datum data;
752
753           type = gnutls_pkcs12_bag_get_type (bag, i);
754           if (type < 0)
755             {
756               goto done;
757             }
758
759           ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
760           if (ret < 0)
761             {
762               goto done;
763             }
764
765           switch (type)
766             {
767             case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
768             case GNUTLS_BAG_PKCS8_KEY:
769               ret = gnutls_x509_privkey_init (key);
770               if (ret < 0)
771                 {
772                   goto done;
773                 }
774
775               ret = gnutls_x509_privkey_import_pkcs8
776                 (*key, &data, GNUTLS_X509_FMT_DER, password,
777                  type == GNUTLS_BAG_PKCS8_KEY ? GNUTLS_PKCS_PLAIN : 0);
778               if (ret < 0)
779                 {
780                   goto done;
781                 }
782               break;
783
784             case GNUTLS_BAG_CERTIFICATE:
785               ret = gnutls_x509_crt_init (cert);
786               if (ret < 0)
787                 {
788                   goto done;
789                 }
790
791               ret =
792                 gnutls_x509_crt_import (*cert, &data, GNUTLS_X509_FMT_DER);
793               if (ret < 0)
794                 {
795                   goto done;
796                 }
797               break;
798
799             case GNUTLS_BAG_ENCRYPTED:
800               /* XXX Bother to recurse one level down?  Unlikely to
801                  use the same password anyway. */
802             case GNUTLS_BAG_EMPTY:
803             default:
804               break;
805             }
806         }
807
808       index++;
809       gnutls_pkcs12_bag_deinit (bag);
810     }
811
812   ret = 0;
813
814 done:
815   if (bag)
816     gnutls_pkcs12_bag_deinit (bag);
817
818   return ret;
819 }
820 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
821                         gnutls_x509_crt *x509, gnutls_x509_privkey *pkey)
822 {
823         gnutls_pkcs12_t p12 = NULL;
824
825         int r;
826
827         *x509 = NULL;
828         *pkey = NULL;
829         if (!file)
830                 return;
831
832         if (is_file_exist(file)) {
833                 FILE *fp = g_fopen(file, "r");
834                 if (fp) {
835                         p12 = gnutls_d2i_PKCS12_fp(fp, 0);
836                         fclose(fp);
837                         if (!p12) {
838                                 log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
839                         }
840                 } else {
841                         log_error(LOG_PROTOCOL, _("Cannot open P12 certificate file %s (%s)\n"),
842                                   file, strerror(errno));
843                 }
844         } else {
845                 log_error(LOG_PROTOCOL, _("P12 Certificate file %s missing (%s)\n"), file,
846                           strerror(errno));
847         }
848         if (p12 != NULL) {
849                 if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
850                         debug_print("got p12\n");
851                 } else {
852                         log_error(LOG_PROTOCOL, "%s\n", gnutls_strerror(r));
853                 }
854                 gnutls_pkcs12_deinit(p12);
855         }
856 }
857
858 gboolean ssl_certificate_check_subject_cn(SSLCertificate *cert)
859 {
860         return gnutls_x509_crt_check_hostname(cert->x509_cert, cert->host) != 0;
861 }
862
863 gchar *ssl_certificate_get_subject_cn(SSLCertificate *cert)
864 {
865         gchar subject_cn[BUFFSIZE];
866         size_t n = BUFFSIZE;
867
868         if(gnutls_x509_crt_get_dn_by_oid(cert->x509_cert, 
869                 GNUTLS_OID_X520_COMMON_NAME, 0, 0, subject_cn, &n))
870                 strncpy(subject_cn, _("<not in certificate>"), BUFFSIZE);
871
872         return g_strdup(subject_cn);
873 }
874
875 #endif /* USE_GNUTLS */