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