Sync Spanish manual
[claws.git] / src / common / ssl_certificate.c
blobf9dbd3b3ca89b9c1e4d10e6881eb6e91ac15fe77
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 Colin Leroy and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #ifdef USE_GNUTLS
25 #include <gnutls/gnutls.h>
26 #include <gnutls/x509.h>
27 #include <gnutls/pkcs12.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <stdio.h>
33 #include <glib.h>
34 #include <glib/gi18n.h>
35 #include <errno.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 #include "file-utils.h"
51 static GHashTable *warned_expired = NULL;
53 gboolean prefs_common_unsafe_ssl_certs(void);
55 static gchar *get_certificate_path(const gchar *host, const gchar *port, const gchar *fp)
57 gchar *ret;
58 gchar *filename;
60 if (fp != NULL && prefs_common_unsafe_ssl_certs()) {
61 filename = g_strconcat(host, ".", port, ".", fp, ".cert", NULL);
62 } else {
63 filename = g_strconcat(host, ".", port, ".cert", NULL);
65 subst_for_filename(filename);
67 ret = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
68 "certs", G_DIR_SEPARATOR_S,
69 filename, NULL);
71 g_free(filename);
72 return ret;
75 static gchar *get_certificate_chain_path(const gchar *host, const gchar *port, const gchar *fp)
77 gchar *tmp = get_certificate_path(host, port, fp);
78 gchar *result = g_strconcat(tmp, ".chain", NULL);
80 g_free(tmp);
82 return result;
85 char * readable_fingerprint(unsigned char *src, int len)
87 int i=0;
88 char * ret;
90 if (src == NULL)
91 return NULL;
92 ret = g_strdup("");
93 while (i < len) {
94 char *tmp2;
95 if(i>0)
96 tmp2 = g_strdup_printf("%s:%02X", ret, src[i]);
97 else
98 tmp2 = g_strdup_printf("%02X", src[i]);
99 g_free(ret);
100 ret = g_strdup(tmp2);
101 g_free(tmp2);
102 i++;
104 return ret;
107 #if USE_GNUTLS
108 static gnutls_x509_crt_t x509_crt_copy(gnutls_x509_crt_t src)
110 int ret;
111 size_t size;
112 gnutls_datum_t tmp;
113 gnutls_x509_crt_t dest;
114 size = 0;
116 if (gnutls_x509_crt_init(&dest) != 0) {
117 g_warning("couldn't gnutls_x509_crt_init");
118 return NULL;
121 if (gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, NULL, &size)
122 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
123 g_warning("couldn't gnutls_x509_crt_export to get size");
124 gnutls_x509_crt_deinit(dest);
125 return NULL;
128 tmp.data = malloc(size);
129 memset(tmp.data, 0, size);
130 ret = gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, tmp.data, &size);
131 if (ret == 0) {
132 tmp.size = size;
133 ret = gnutls_x509_crt_import(dest, &tmp, GNUTLS_X509_FMT_DER);
134 if (ret) {
135 g_warning("couldn't gnutls_x509_crt_import for real (%d %s)", ret, gnutls_strerror(ret));
136 gnutls_x509_crt_deinit(dest);
137 dest = NULL;
139 } else {
140 g_warning("couldn't gnutls_x509_crt_export for real (%d %s)", ret, gnutls_strerror(ret));
141 gnutls_x509_crt_deinit(dest);
142 dest = NULL;
145 free(tmp.data);
146 return dest;
148 #endif
150 static SSLCertificate *ssl_certificate_new(gnutls_x509_crt_t x509_cert, const gchar *host, gushort port)
152 SSLCertificate *cert = g_new0(SSLCertificate, 1);
153 size_t n;
154 unsigned char md[128];
156 if (host == NULL || x509_cert == NULL) {
157 ssl_certificate_destroy(cert);
158 return NULL;
160 cert->x509_cert = x509_crt_copy(x509_cert);
161 cert->status = (guint)-1;
162 cert->host = g_strdup(host);
163 cert->port = port;
165 /* fingerprint */
166 n = sizeof(md);
167 gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_MD5, md, &n);
168 cert->fingerprint = readable_fingerprint(md, (int)n);
169 return cert;
172 static void gnutls_export_X509_fp(FILE *fp, gnutls_x509_crt_t x509_cert, gnutls_x509_crt_fmt_t format)
174 char output[10*1024];
175 size_t cert_size = 10*1024;
176 int r;
178 if ((r = gnutls_x509_crt_export(x509_cert, format, output, &cert_size)) < 0) {
179 g_warning("couldn't export cert %s (%"G_GSIZE_FORMAT")", gnutls_strerror(r), cert_size);
180 return;
182 debug_print("writing %" G_GSIZE_FORMAT " bytes\n",cert_size);
183 if (claws_fwrite(&output, 1, cert_size, fp) < cert_size) {
184 g_warning("failed to write cert: %d %s", errno, g_strerror(errno));
188 size_t gnutls_i2d_X509(gnutls_x509_crt_t x509_cert, unsigned char **output)
190 size_t cert_size = 10*1024;
191 int r;
193 if (output == NULL)
194 return 0;
196 *output = malloc(cert_size);
198 if ((r = gnutls_x509_crt_export(x509_cert, GNUTLS_X509_FMT_DER, *output, &cert_size)) < 0) {
199 g_warning("couldn't export cert %s (%"G_GSIZE_FORMAT")", gnutls_strerror(r), cert_size);
200 free(*output);
201 *output = NULL;
202 return 0;
204 return cert_size;
207 size_t gnutls_i2d_PrivateKey(gnutls_x509_privkey_t pkey, unsigned char **output)
209 size_t key_size = 10*1024;
210 int r;
212 if (output == NULL)
213 return 0;
215 *output = malloc(key_size);
217 if ((r = gnutls_x509_privkey_export(pkey, GNUTLS_X509_FMT_DER, *output, &key_size)) < 0) {
218 g_warning("couldn't export key %s (%"G_GSIZE_FORMAT")", gnutls_strerror(r), key_size);
219 free(*output);
220 *output = NULL;
221 return 0;
223 return key_size;
226 static int gnutls_import_X509_list_fp(FILE *fp, gnutls_x509_crt_fmt_t format,
227 gnutls_x509_crt_t **cert_list, gint *num_certs)
229 gnutls_x509_crt_t *crt_list;
230 unsigned int max = 512;
231 unsigned int flags = 0;
232 gnutls_datum_t tmp;
233 struct stat s;
234 int r;
236 *cert_list = NULL;
237 *num_certs = 0;
239 if (fp == NULL)
240 return -ENOENT;
242 if (fstat(fileno(fp), &s) < 0) {
243 perror("fstat");
244 return -errno;
247 crt_list=(gnutls_x509_crt_t*)malloc(max*sizeof(gnutls_x509_crt_t));
248 tmp.data = malloc(s.st_size);
249 memset(tmp.data, 0, s.st_size);
250 tmp.size = s.st_size;
251 if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
252 perror("claws_fread");
253 free(tmp.data);
254 free(crt_list);
255 return -EIO;
258 if ((r = gnutls_x509_crt_list_import(crt_list, &max,
259 &tmp, format, flags)) < 0) {
260 debug_print("cert import failed: %s\n", gnutls_strerror(r));
261 free(tmp.data);
262 free(crt_list);
263 return r;
265 free(tmp.data);
266 debug_print("got %d certs in crt_list! %p\n", max, &crt_list);
268 *cert_list = crt_list;
269 *num_certs = max;
271 return r;
274 /* return one certificate, read from file */
275 static gnutls_x509_crt_t gnutls_import_X509_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
277 gnutls_x509_crt_t *certs = NULL;
278 gnutls_x509_crt_t cert = NULL;
279 int i, ncerts, r;
281 if ((r = gnutls_import_X509_list_fp(fp, format, &certs, &ncerts)) < 0) {
282 return NULL;
285 if (ncerts == 0)
286 return NULL;
288 for (i = 1; i < ncerts; i++)
289 gnutls_x509_crt_deinit(certs[i]);
291 cert = certs[0];
292 free(certs);
294 return cert;
297 static gnutls_x509_privkey_t gnutls_import_key_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
299 gnutls_x509_privkey_t key = NULL;
300 gnutls_datum_t tmp;
301 struct stat s;
302 int r;
303 if (fstat(fileno(fp), &s) < 0) {
304 perror("fstat");
305 return NULL;
307 tmp.data = malloc(s.st_size);
308 memset(tmp.data, 0, s.st_size);
309 tmp.size = s.st_size;
310 if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
311 perror("claws_fread");
312 free(tmp.data);
313 return NULL;
316 gnutls_x509_privkey_init(&key);
317 if ((r = gnutls_x509_privkey_import(key, &tmp, format)) < 0) {
318 debug_print("key import failed: %s\n", gnutls_strerror(r));
319 gnutls_x509_privkey_deinit(key);
320 key = NULL;
322 free(tmp.data);
323 debug_print("got key! %p\n", key);
324 return key;
327 static gnutls_pkcs12_t gnutls_import_PKCS12_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
329 gnutls_pkcs12_t p12 = NULL;
330 gnutls_datum_t tmp;
331 struct stat s;
332 int r;
333 if (fstat(fileno(fp), &s) < 0) {
334 log_error(LOG_PROTOCOL, _("Cannot stat P12 certificate file (%s)\n"),
335 g_strerror(errno));
336 return NULL;
338 tmp.data = malloc(s.st_size);
339 memset(tmp.data, 0, s.st_size);
340 tmp.size = s.st_size;
341 if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
342 log_error(LOG_PROTOCOL, _("Cannot read P12 certificate file (%s)\n"),
343 g_strerror(errno));
344 free(tmp.data);
345 return NULL;
348 gnutls_pkcs12_init(&p12);
350 if ((r = gnutls_pkcs12_import(p12, &tmp, format, 0)) < 0) {
351 log_error(LOG_PROTOCOL, _("Cannot import P12 certificate file (%s)\n"),
352 gnutls_strerror(r));
353 gnutls_pkcs12_deinit(p12);
354 p12 = NULL;
356 free(tmp.data);
357 debug_print("got p12! %p\n", p12);
358 return p12;
361 static void ssl_certificate_save (SSLCertificate *cert)
363 gchar *file, *port;
364 FILE *fp;
366 file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
367 "certs", G_DIR_SEPARATOR_S, NULL);
369 if (!is_dir_exist(file))
370 make_dir_hier(file);
371 g_free(file);
373 port = g_strdup_printf("%d", cert->port);
374 file = get_certificate_path(cert->host, port, cert->fingerprint);
376 g_free(port);
377 fp = claws_fopen(file, "wb");
378 if (fp == NULL) {
379 g_free(file);
380 debug_print("Can't save certificate !\n");
381 return;
384 gnutls_export_X509_fp(fp, cert->x509_cert, GNUTLS_X509_FMT_DER);
386 g_free(file);
387 claws_safe_fclose(fp);
391 void ssl_certificate_destroy(SSLCertificate *cert)
393 if (cert == NULL)
394 return;
396 if (cert->x509_cert)
397 gnutls_x509_crt_deinit(cert->x509_cert);
398 g_free(cert->host);
399 g_free(cert->fingerprint);
400 g_free(cert);
401 cert = NULL;
404 void ssl_certificate_delete_from_disk(SSLCertificate *cert)
406 gchar *buf;
407 gchar *file;
408 buf = g_strdup_printf("%d", cert->port);
409 file = get_certificate_path(cert->host, buf, cert->fingerprint);
410 claws_unlink (file);
411 g_free(file);
412 file = get_certificate_chain_path(cert->host, buf, cert->fingerprint);
413 claws_unlink (file);
414 g_free(file);
415 g_free(buf);
418 SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gchar *fingerprint)
420 gchar *file = NULL;
421 gchar *buf;
422 SSLCertificate *cert = NULL;
423 gnutls_x509_crt_t tmp_x509;
424 FILE *fp = NULL;
425 gboolean must_rename = FALSE;
427 buf = g_strdup_printf("%d", port);
429 if (fingerprint != NULL) {
430 file = get_certificate_path(host, buf, fingerprint);
431 fp = claws_fopen(file, "rb");
433 if (fp == NULL) {
434 /* see if we have the old one */
435 debug_print("didn't get %s\n", file);
436 g_free(file);
437 file = get_certificate_path(host, buf, NULL);
438 fp = claws_fopen(file, "rb");
440 if (fp) {
441 debug_print("got %s\n", file);
442 must_rename = (fingerprint != NULL);
444 } else {
445 debug_print("got %s first try\n", file);
447 if (fp == NULL) {
448 g_free(file);
449 g_free(buf);
450 return NULL;
453 if ((tmp_x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_DER)) != NULL) {
454 cert = ssl_certificate_new(tmp_x509, host, port);
455 debug_print("got cert %p\n", cert);
456 gnutls_x509_crt_deinit(tmp_x509);
459 claws_fclose(fp);
460 g_free(file);
462 if (must_rename) {
463 gchar *old = get_certificate_path(host, buf, NULL);
464 gchar *new = get_certificate_path(host, buf, fingerprint);
465 if (strcmp(old, new))
466 move_file(old, new, TRUE);
467 g_free(old);
468 g_free(new);
470 g_free(buf);
472 return cert;
475 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
477 char *output_a;
478 char *output_b;
479 size_t cert_size_a = 0, cert_size_b = 0;
480 int r;
482 if (cert_a == NULL || cert_b == NULL)
483 return FALSE;
485 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_a))
486 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
487 g_warning("couldn't gnutls_x509_crt_export to get size a %s", gnutls_strerror(r));
488 return FALSE;
491 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_b))
492 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
493 g_warning("couldn't gnutls_x509_crt_export to get size b %s", gnutls_strerror(r));
494 return FALSE;
497 if (cert_size_a != cert_size_b) {
498 debug_print("sizes differ: %"G_GSIZE_FORMAT" != %"G_GSIZE_FORMAT"\n", cert_size_a, cert_size_b);
499 return FALSE;
502 output_a = g_malloc(cert_size_a);
503 output_b = g_malloc(cert_size_b);
504 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
505 g_warning("couldn't gnutls_x509_crt_export a %s", gnutls_strerror(r));
506 g_free(output_a);
507 g_free(output_b);
508 return FALSE;
510 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
511 g_warning("couldn't gnutls_x509_crt_export b %s", gnutls_strerror(r));
512 g_free(output_a);
513 g_free(output_b);
514 return FALSE;
516 if (memcmp(output_a, output_b, cert_size_a)) {
517 debug_print("contents differ\n");
518 g_free(output_a);
519 g_free(output_b);
520 return FALSE;
522 g_free(output_a);
523 g_free(output_b);
525 return TRUE;
528 static guint check_cert(SSLCertificate *cert)
530 gnutls_x509_crt_t *ca_list = NULL;
531 gnutls_x509_crt_t *chain = NULL;
532 unsigned int max_ca = 512, max_certs;
533 unsigned int flags = 0;
534 int r, i;
535 unsigned int status;
536 gchar *chain_file = NULL, *buf = NULL;
537 FILE *fp;
539 if (claws_ssl_get_cert_file())
540 fp = claws_fopen(claws_ssl_get_cert_file(), "r");
541 else
542 return (guint)-1;
544 if (fp == NULL)
545 return (guint)-1;
547 if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &ca_list, &max_ca)) < 0) {
548 debug_print("CA import failed: %s\n", gnutls_strerror(r));
549 claws_fclose(fp);
550 return (guint)-1;
552 claws_fclose(fp);
553 fp = NULL;
555 buf = g_strdup_printf("%d", cert->port);
556 chain_file = get_certificate_chain_path(cert->host, buf, cert->fingerprint);
557 g_free(buf);
558 if (is_file_exist(chain_file)) {
559 unsigned char md[128];
560 size_t n = 128;
561 char *fingerprint;
563 fp = claws_fopen(chain_file, "r");
564 if (fp == NULL) {
565 debug_print("claws_fopen %s failed: %s\n", chain_file, g_strerror(errno));
566 g_free(chain_file);
567 return (guint)-1;
569 if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &chain, &max_certs)) < 0) {
570 debug_print("chain import failed: %s\n", gnutls_strerror(r));
571 claws_fclose(fp);
572 g_free(chain_file);
573 return (guint)-1;
575 g_free(chain_file);
576 claws_fclose(fp);
577 fp = NULL;
579 gnutls_x509_crt_get_fingerprint(chain[0], GNUTLS_DIG_MD5, md, &n);
580 fingerprint = readable_fingerprint(md, n);
581 if (!fingerprint || strcmp(fingerprint, cert->fingerprint)) {
582 debug_print("saved chain fingerprint does not match current : %s / %s\n",
583 cert->fingerprint, fingerprint);
585 return (guint)-1;
587 g_free(fingerprint);
589 r = gnutls_x509_crt_list_verify (chain,
590 max_certs,
591 ca_list, max_ca,
592 NULL, 0,
593 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
594 &status);
595 if (r < 0)
596 debug_print("chain check failed: %s\n", gnutls_strerror(r));
598 for (i = 0; i < max_certs; i++)
599 gnutls_x509_crt_deinit(chain[i]);
600 free(chain);
602 } else {
603 r = gnutls_x509_crt_verify(cert->x509_cert, ca_list, max_ca, flags, &status);
604 if (r < 0)
605 debug_print("cert check failed: %s\n", gnutls_strerror(r));
608 for (i = 0; i < max_ca; i++)
609 gnutls_x509_crt_deinit(ca_list[i]);
610 free(ca_list);
612 if (r < 0)
613 return (guint)-1;
614 else
615 return status;
619 static gboolean ssl_certificate_is_valid(SSLCertificate *cert, guint status)
621 gchar *str_status = ssl_certificate_check_signer(cert, status);
623 if (str_status != NULL) {
624 g_free(str_status);
625 return FALSE;
627 return ssl_certificate_check_subject_cn(cert);
630 char *ssl_certificate_check_signer (SSLCertificate *cert, guint status)
632 gnutls_x509_crt_t x509_cert = cert ? cert->x509_cert : NULL;
634 if (!cert)
635 return g_strdup(_("Internal error"));
637 if (status == (guint)-1) {
638 status = check_cert(cert);
639 if (status == -1)
640 return g_strdup(_("Uncheckable"));
642 if (status & GNUTLS_CERT_INVALID) {
643 if (gnutls_x509_crt_check_issuer(x509_cert, x509_cert))
644 return g_strdup(_("Self-signed certificate"));
646 if (status & GNUTLS_CERT_REVOKED)
647 return g_strdup(_("Revoked certificate"));
648 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
649 return g_strdup(_("No certificate issuer found"));
650 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
651 return g_strdup(_("Certificate issuer is not a CA"));
654 return NULL;
657 static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const gchar *host, gushort port)
659 gint i;
660 gchar *file = NULL;
661 FILE *fp = NULL;
663 for (i = 0; i < len; i++) {
664 size_t n;
665 unsigned char md[128];
666 gnutls_x509_crt_t cert = certs[i];
668 if (i == 0) {
669 n = sizeof(md);
670 gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, md, &n);
671 gchar *fingerprint = readable_fingerprint(md, n);
672 gchar *buf = g_strdup_printf("%d", port);
674 file = get_certificate_chain_path(host, buf, fingerprint);
675 g_free(fingerprint);
677 g_free(buf);
679 fp = claws_fopen(file, "wb");
680 if (fp == NULL) {
681 g_free(file);
682 debug_print("Can't save certificate !\n");
683 return;
685 g_free(file);
688 gnutls_export_X509_fp(fp, cert, GNUTLS_X509_FMT_PEM);
691 if (fp)
692 claws_safe_fclose(fp);
695 gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status,
696 const gchar *host, gushort port,
697 gboolean accept_if_valid)
699 SSLCertificate *current_cert = NULL;
700 SSLCertificate *known_cert;
701 SSLCertHookData cert_hook_data;
702 gchar *fingerprint;
703 size_t n;
704 unsigned char md[128];
705 gboolean valid = FALSE;
707 current_cert = ssl_certificate_new(x509_cert, host, port);
709 if (current_cert == NULL) {
710 debug_print("Buggy certificate !\n");
711 return FALSE;
714 current_cert->status = status;
715 /* fingerprint */
716 n = sizeof(md);
717 gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
718 fingerprint = readable_fingerprint(md, n);
720 known_cert = ssl_certificate_find(host, port, fingerprint);
722 g_free(fingerprint);
724 if (accept_if_valid)
725 valid = ssl_certificate_is_valid(current_cert, status);
726 else
727 valid = FALSE; /* Force check */
729 if (known_cert == NULL) {
730 if (valid) {
731 ssl_certificate_save(current_cert);
732 ssl_certificate_destroy(current_cert);
733 return TRUE;
736 cert_hook_data.cert = current_cert;
737 cert_hook_data.old_cert = NULL;
738 cert_hook_data.expired = FALSE;
739 cert_hook_data.accept = FALSE;
741 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
743 if (!cert_hook_data.accept) {
744 ssl_certificate_destroy(current_cert);
745 return FALSE;
746 } else {
747 ssl_certificate_save(current_cert);
748 ssl_certificate_destroy(current_cert);
749 return TRUE;
751 } else if (!ssl_certificate_compare (current_cert, known_cert)) {
752 if (valid) {
753 ssl_certificate_save(current_cert);
754 ssl_certificate_destroy(current_cert);
755 ssl_certificate_destroy(known_cert);
756 return TRUE;
759 cert_hook_data.cert = current_cert;
760 cert_hook_data.old_cert = known_cert;
761 cert_hook_data.expired = FALSE;
762 cert_hook_data.accept = FALSE;
764 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
766 if (!cert_hook_data.accept) {
767 ssl_certificate_destroy(current_cert);
768 ssl_certificate_destroy(known_cert);
769 return FALSE;
770 } else {
771 ssl_certificate_save(current_cert);
772 ssl_certificate_destroy(current_cert);
773 ssl_certificate_destroy(known_cert);
774 return TRUE;
776 } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
777 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
779 if (warned_expired == NULL)
780 warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
782 if (g_hash_table_lookup(warned_expired, tmp)) {
783 g_free(tmp);
784 ssl_certificate_destroy(current_cert);
785 ssl_certificate_destroy(known_cert);
786 return TRUE;
789 cert_hook_data.cert = current_cert;
790 cert_hook_data.old_cert = NULL;
791 cert_hook_data.expired = TRUE;
792 cert_hook_data.accept = FALSE;
794 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
796 if (!cert_hook_data.accept) {
797 g_free(tmp);
798 ssl_certificate_destroy(current_cert);
799 ssl_certificate_destroy(known_cert);
800 return FALSE;
801 } else {
802 g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
803 ssl_certificate_destroy(current_cert);
804 ssl_certificate_destroy(known_cert);
805 return TRUE;
809 ssl_certificate_destroy(current_cert);
810 ssl_certificate_destroy(known_cert);
811 return TRUE;
814 gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len,
815 const gchar *host, gushort port,
816 gboolean accept_if_valid)
818 int ncas = 0;
819 gnutls_x509_crt_t *cas = NULL;
820 gboolean result = FALSE;
821 int i;
822 gint status;
824 if (claws_ssl_get_cert_file()) {
825 FILE *fp = claws_fopen(claws_ssl_get_cert_file(), "rb");
826 int r = -errno;
828 if (fp) {
829 r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &cas, &ncas);
830 claws_fclose(fp);
833 if (r < 0)
834 g_warning("can't read SSL_CERT_FILE '%s': %s",
835 claws_ssl_get_cert_file(),
836 gnutls_strerror(r));
837 } else {
838 debug_print("Can't find SSL ca-certificates file\n");
842 gnutls_x509_crt_list_verify (certs,
843 chain_len,
844 cas, ncas,
845 NULL, 0,
846 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
847 &status);
849 result = ssl_certificate_check(certs[0], status, host, port,
850 accept_if_valid);
852 if (result == TRUE) {
853 ssl_certificate_save_chain(certs, chain_len, host, port);
856 for (i = 0; i < ncas; i++)
857 gnutls_x509_crt_deinit(cas[i]);
858 free(cas);
860 return result;
863 gnutls_x509_crt_t ssl_certificate_get_x509_from_pem_file(const gchar *file)
865 gnutls_x509_crt_t x509 = NULL;
866 if (!file)
867 return NULL;
869 if (is_file_exist(file)) {
870 FILE *fp = claws_fopen(file, "r");
871 if (fp) {
872 x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_PEM);
873 claws_fclose(fp);
874 return x509;
875 } else {
876 log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
877 file, g_strerror(errno));
879 } else {
880 log_error(LOG_PROTOCOL, _("Certificate file %s missing (%s)\n"),
881 file, g_strerror(errno));
883 return NULL;
886 gnutls_x509_privkey_t ssl_certificate_get_pkey_from_pem_file(const gchar *file)
888 gnutls_x509_privkey_t key = NULL;
889 if (!file)
890 return NULL;
892 if (is_file_exist(file)) {
893 FILE *fp = claws_fopen(file, "r");
894 if (fp) {
895 key = gnutls_import_key_fp(fp, GNUTLS_X509_FMT_PEM);
896 claws_fclose(fp);
897 return key;
898 } else {
899 log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
900 file, g_strerror(errno));
902 } else {
903 log_error(LOG_PROTOCOL, _("Key file %s missing (%s)\n"), file,
904 g_strerror(errno));
906 return NULL;
909 /* From GnuTLS lib/gnutls_x509.c */
910 static int
911 parse_pkcs12 (gnutls_pkcs12_t p12,
912 const char *password,
913 gnutls_x509_privkey_t * key,
914 gnutls_x509_crt_t * cert)
916 gnutls_pkcs12_bag_t bag = NULL;
917 int index = 0;
918 int ret;
920 for (;;)
922 int elements_in_bag;
923 int i;
925 ret = gnutls_pkcs12_bag_init (&bag);
926 if (ret < 0)
928 bag = NULL;
929 goto done;
932 ret = gnutls_pkcs12_get_bag (p12, index, bag);
933 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
934 break;
935 if (ret < 0)
937 goto done;
940 ret = gnutls_pkcs12_bag_get_type (bag, 0);
941 if (ret < 0)
943 goto done;
946 if (ret == GNUTLS_BAG_ENCRYPTED)
948 ret = gnutls_pkcs12_bag_decrypt (bag, password);
949 if (ret < 0)
951 goto done;
955 elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
956 if (elements_in_bag < 0)
958 goto done;
961 for (i = 0; i < elements_in_bag; i++)
963 int type;
964 gnutls_datum_t data;
966 type = gnutls_pkcs12_bag_get_type (bag, i);
967 if (type < 0)
969 goto done;
972 ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
973 if (ret < 0)
975 goto done;
978 switch (type)
980 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
981 case GNUTLS_BAG_PKCS8_KEY:
982 ret = gnutls_x509_privkey_init (key);
983 if (ret < 0)
985 goto done;
988 ret = gnutls_x509_privkey_import_pkcs8
989 (*key, &data, GNUTLS_X509_FMT_DER, password,
990 type == GNUTLS_BAG_PKCS8_KEY ? GNUTLS_PKCS_PLAIN : 0);
991 if (ret < 0)
993 goto done;
995 break;
997 case GNUTLS_BAG_CERTIFICATE:
998 ret = gnutls_x509_crt_init (cert);
999 if (ret < 0)
1001 goto done;
1004 ret =
1005 gnutls_x509_crt_import (*cert, &data, GNUTLS_X509_FMT_DER);
1006 if (ret < 0)
1008 goto done;
1010 break;
1012 case GNUTLS_BAG_ENCRYPTED:
1013 /* XXX Bother to recurse one level down? Unlikely to
1014 use the same password anyway. */
1015 case GNUTLS_BAG_EMPTY:
1016 default:
1017 break;
1021 index++;
1022 gnutls_pkcs12_bag_deinit (bag);
1025 ret = 0;
1027 done:
1028 if (bag)
1029 gnutls_pkcs12_bag_deinit (bag);
1031 return ret;
1033 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
1034 gnutls_x509_crt_t *x509, gnutls_x509_privkey_t *pkey)
1036 gnutls_pkcs12_t p12 = NULL;
1038 int r;
1040 *x509 = NULL;
1041 *pkey = NULL;
1042 if (!file)
1043 return;
1045 if (is_file_exist(file)) {
1046 FILE *fp = claws_fopen(file, "r");
1047 if (fp) {
1048 p12 = gnutls_import_PKCS12_fp(fp, GNUTLS_X509_FMT_DER);
1049 claws_fclose(fp);
1050 if (!p12) {
1051 log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
1053 } else {
1054 log_error(LOG_PROTOCOL, _("Cannot open P12 certificate file %s (%s)\n"),
1055 file, g_strerror(errno));
1057 } else {
1058 log_error(LOG_PROTOCOL, _("P12 Certificate file %s missing (%s)\n"), file,
1059 g_strerror(errno));
1061 if (p12 != NULL) {
1062 if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
1063 debug_print("got p12\n");
1064 } else {
1065 log_error(LOG_PROTOCOL, "%s\n", gnutls_strerror(r));
1067 gnutls_pkcs12_deinit(p12);
1071 gboolean ssl_certificate_check_subject_cn(SSLCertificate *cert)
1073 return gnutls_x509_crt_check_hostname(cert->x509_cert, cert->host) != 0;
1076 gchar *ssl_certificate_get_subject_cn(SSLCertificate *cert)
1078 gchar subject_cn[BUFFSIZE];
1079 size_t n = BUFFSIZE;
1081 if(gnutls_x509_crt_get_dn_by_oid(cert->x509_cert,
1082 GNUTLS_OID_X520_COMMON_NAME, 0, 0, subject_cn, &n))
1083 return g_strdup(_("<not in certificate>"));
1085 return g_strdup(subject_cn);
1088 #endif /* USE_GNUTLS */