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