correctly set modified flag after auto-save
[claws.git] / src / common / ssl_certificate.c
blobc82a3584b8d838a82581e7d5d9b54d68afe56746
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 output_a = g_malloc(cert_size_a);
498 output_b = g_malloc(cert_size_b);
499 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
500 g_warning("couldn't gnutls_x509_crt_export a %s", gnutls_strerror(r));
501 g_free(output_a);
502 g_free(output_b);
503 return FALSE;
505 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
506 g_warning("couldn't gnutls_x509_crt_export b %s", gnutls_strerror(r));
507 g_free(output_a);
508 g_free(output_b);
509 return FALSE;
511 if (cert_size_a != cert_size_b) {
512 g_warning("size differ %"G_GSIZE_FORMAT" %"G_GSIZE_FORMAT, cert_size_a, cert_size_b);
513 g_free(output_a);
514 g_free(output_b);
515 return FALSE;
517 if (memcmp(output_a, output_b, cert_size_a)) {
518 g_warning("contents differ");
519 g_free(output_a);
520 g_free(output_b);
521 return FALSE;
523 g_free(output_a);
524 g_free(output_b);
526 return TRUE;
529 static guint check_cert(SSLCertificate *cert)
531 gnutls_x509_crt_t *ca_list = NULL;
532 gnutls_x509_crt_t *chain = NULL;
533 unsigned int max_ca = 512, max_certs;
534 unsigned int flags = 0;
535 int r, i;
536 unsigned int status;
537 gchar *chain_file = NULL, *buf = NULL;
538 FILE *fp;
540 if (claws_ssl_get_cert_file())
541 fp = claws_fopen(claws_ssl_get_cert_file(), "r");
542 else
543 return (guint)-1;
545 if (fp == NULL)
546 return (guint)-1;
548 if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &ca_list, &max_ca)) < 0) {
549 debug_print("CA import failed: %s\n", gnutls_strerror(r));
550 claws_fclose(fp);
551 return (guint)-1;
553 claws_fclose(fp);
554 fp = NULL;
556 buf = g_strdup_printf("%d", cert->port);
557 chain_file = get_certificate_chain_path(cert->host, buf, cert->fingerprint);
558 g_free(buf);
559 if (is_file_exist(chain_file)) {
560 unsigned char md[128];
561 size_t n = 128;
562 char *fingerprint;
564 fp = claws_fopen(chain_file, "r");
565 if (fp == NULL) {
566 debug_print("claws_fopen %s failed: %s\n", chain_file, g_strerror(errno));
567 g_free(chain_file);
568 return (guint)-1;
570 if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &chain, &max_certs)) < 0) {
571 debug_print("chain import failed: %s\n", gnutls_strerror(r));
572 claws_fclose(fp);
573 g_free(chain_file);
574 return (guint)-1;
576 g_free(chain_file);
577 claws_fclose(fp);
578 fp = NULL;
580 gnutls_x509_crt_get_fingerprint(chain[0], GNUTLS_DIG_MD5, md, &n);
581 fingerprint = readable_fingerprint(md, n);
582 if (!fingerprint || strcmp(fingerprint, cert->fingerprint)) {
583 debug_print("saved chain fingerprint does not match current : %s / %s\n",
584 cert->fingerprint, fingerprint);
586 return (guint)-1;
588 g_free(fingerprint);
590 r = gnutls_x509_crt_list_verify (chain,
591 max_certs,
592 ca_list, max_ca,
593 NULL, 0,
594 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
595 &status);
596 if (r < 0)
597 debug_print("chain check failed: %s\n", gnutls_strerror(r));
599 for (i = 0; i < max_certs; i++)
600 gnutls_x509_crt_deinit(chain[i]);
601 free(chain);
603 } else {
604 r = gnutls_x509_crt_verify(cert->x509_cert, ca_list, max_ca, flags, &status);
605 if (r < 0)
606 debug_print("cert check failed: %s\n", gnutls_strerror(r));
609 for (i = 0; i < max_ca; i++)
610 gnutls_x509_crt_deinit(ca_list[i]);
611 free(ca_list);
613 if (r < 0)
614 return (guint)-1;
615 else
616 return status;
620 static gboolean ssl_certificate_is_valid(SSLCertificate *cert, guint status)
622 gchar *str_status = ssl_certificate_check_signer(cert, status);
624 if (str_status != NULL) {
625 g_free(str_status);
626 return FALSE;
628 return ssl_certificate_check_subject_cn(cert);
631 char *ssl_certificate_check_signer (SSLCertificate *cert, guint status)
633 gnutls_x509_crt_t x509_cert = cert ? cert->x509_cert : NULL;
635 if (!cert)
636 return g_strdup(_("Internal error"));
638 if (status == (guint)-1) {
639 status = check_cert(cert);
640 if (status == -1)
641 return g_strdup(_("Uncheckable"));
643 if (status & GNUTLS_CERT_INVALID) {
644 if (gnutls_x509_crt_check_issuer(x509_cert, x509_cert))
645 return g_strdup(_("Self-signed certificate"));
647 if (status & GNUTLS_CERT_REVOKED)
648 return g_strdup(_("Revoked certificate"));
649 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
650 return g_strdup(_("No certificate issuer found"));
651 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
652 return g_strdup(_("Certificate issuer is not a CA"));
655 return NULL;
658 static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const gchar *host, gushort port)
660 gint i;
661 gchar *file = NULL;
662 FILE *fp = NULL;
664 for (i = 0; i < len; i++) {
665 size_t n;
666 unsigned char md[128];
667 gnutls_x509_crt_t cert = certs[i];
669 if (i == 0) {
670 n = sizeof(md);
671 gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, md, &n);
672 gchar *fingerprint = readable_fingerprint(md, n);
673 gchar *buf = g_strdup_printf("%d", port);
675 file = get_certificate_chain_path(host, buf, fingerprint);
676 g_free(fingerprint);
678 g_free(buf);
680 fp = claws_fopen(file, "wb");
681 if (fp == NULL) {
682 g_free(file);
683 debug_print("Can't save certificate !\n");
684 return;
686 g_free(file);
689 gnutls_export_X509_fp(fp, cert, GNUTLS_X509_FMT_PEM);
692 if (fp)
693 claws_safe_fclose(fp);
696 gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status,
697 const gchar *host, gushort port,
698 gboolean accept_if_valid)
700 SSLCertificate *current_cert = NULL;
701 SSLCertificate *known_cert;
702 SSLCertHookData cert_hook_data;
703 gchar *fingerprint;
704 size_t n;
705 unsigned char md[128];
706 gboolean valid = FALSE;
708 current_cert = ssl_certificate_new(x509_cert, host, port);
710 if (current_cert == NULL) {
711 debug_print("Buggy certificate !\n");
712 return FALSE;
715 current_cert->status = status;
716 /* fingerprint */
717 n = sizeof(md);
718 gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
719 fingerprint = readable_fingerprint(md, n);
721 known_cert = ssl_certificate_find(host, port, fingerprint);
723 g_free(fingerprint);
725 if (accept_if_valid)
726 valid = ssl_certificate_is_valid(current_cert, status);
727 else
728 valid = FALSE; /* Force check */
730 if (known_cert == NULL) {
731 if (valid) {
732 ssl_certificate_save(current_cert);
733 ssl_certificate_destroy(current_cert);
734 return TRUE;
737 cert_hook_data.cert = current_cert;
738 cert_hook_data.old_cert = NULL;
739 cert_hook_data.expired = FALSE;
740 cert_hook_data.accept = FALSE;
742 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
744 if (!cert_hook_data.accept) {
745 ssl_certificate_destroy(current_cert);
746 return FALSE;
747 } else {
748 ssl_certificate_save(current_cert);
749 ssl_certificate_destroy(current_cert);
750 return TRUE;
752 } else if (!ssl_certificate_compare (current_cert, known_cert)) {
753 if (valid) {
754 ssl_certificate_save(current_cert);
755 ssl_certificate_destroy(current_cert);
756 ssl_certificate_destroy(known_cert);
757 return TRUE;
760 cert_hook_data.cert = current_cert;
761 cert_hook_data.old_cert = known_cert;
762 cert_hook_data.expired = FALSE;
763 cert_hook_data.accept = FALSE;
765 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
767 if (!cert_hook_data.accept) {
768 ssl_certificate_destroy(current_cert);
769 ssl_certificate_destroy(known_cert);
770 return FALSE;
771 } else {
772 ssl_certificate_save(current_cert);
773 ssl_certificate_destroy(current_cert);
774 ssl_certificate_destroy(known_cert);
775 return TRUE;
777 } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
778 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
780 if (warned_expired == NULL)
781 warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
783 if (g_hash_table_lookup(warned_expired, tmp)) {
784 g_free(tmp);
785 ssl_certificate_destroy(current_cert);
786 ssl_certificate_destroy(known_cert);
787 return TRUE;
790 cert_hook_data.cert = current_cert;
791 cert_hook_data.old_cert = NULL;
792 cert_hook_data.expired = TRUE;
793 cert_hook_data.accept = FALSE;
795 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
797 if (!cert_hook_data.accept) {
798 g_free(tmp);
799 ssl_certificate_destroy(current_cert);
800 ssl_certificate_destroy(known_cert);
801 return FALSE;
802 } else {
803 g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
804 ssl_certificate_destroy(current_cert);
805 ssl_certificate_destroy(known_cert);
806 return TRUE;
810 ssl_certificate_destroy(current_cert);
811 ssl_certificate_destroy(known_cert);
812 return TRUE;
815 gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len,
816 const gchar *host, gushort port,
817 gboolean accept_if_valid)
819 int ncas = 0;
820 gnutls_x509_crt_t *cas = NULL;
821 gboolean result = FALSE;
822 int i;
823 gint status;
825 if (claws_ssl_get_cert_file()) {
826 FILE *fp = claws_fopen(claws_ssl_get_cert_file(), "rb");
827 int r = -errno;
829 if (fp) {
830 r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &cas, &ncas);
831 claws_fclose(fp);
834 if (r < 0)
835 g_warning("can't read SSL_CERT_FILE '%s': %s",
836 claws_ssl_get_cert_file(),
837 gnutls_strerror(r));
838 } else {
839 debug_print("Can't find SSL ca-certificates file\n");
843 gnutls_x509_crt_list_verify (certs,
844 chain_len,
845 cas, ncas,
846 NULL, 0,
847 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
848 &status);
850 result = ssl_certificate_check(certs[0], status, host, port,
851 accept_if_valid);
853 if (result == TRUE) {
854 ssl_certificate_save_chain(certs, chain_len, host, port);
857 for (i = 0; i < ncas; i++)
858 gnutls_x509_crt_deinit(cas[i]);
859 free(cas);
861 return result;
864 gnutls_x509_crt_t ssl_certificate_get_x509_from_pem_file(const gchar *file)
866 gnutls_x509_crt_t x509 = NULL;
867 if (!file)
868 return NULL;
870 if (is_file_exist(file)) {
871 FILE *fp = claws_fopen(file, "r");
872 if (fp) {
873 x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_PEM);
874 claws_fclose(fp);
875 return x509;
876 } else {
877 log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
878 file, g_strerror(errno));
880 } else {
881 log_error(LOG_PROTOCOL, _("Certificate file %s missing (%s)\n"),
882 file, g_strerror(errno));
884 return NULL;
887 gnutls_x509_privkey_t ssl_certificate_get_pkey_from_pem_file(const gchar *file)
889 gnutls_x509_privkey_t key = NULL;
890 if (!file)
891 return NULL;
893 if (is_file_exist(file)) {
894 FILE *fp = claws_fopen(file, "r");
895 if (fp) {
896 key = gnutls_import_key_fp(fp, GNUTLS_X509_FMT_PEM);
897 claws_fclose(fp);
898 return key;
899 } else {
900 log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
901 file, g_strerror(errno));
903 } else {
904 log_error(LOG_PROTOCOL, _("Key file %s missing (%s)\n"), file,
905 g_strerror(errno));
907 return NULL;
910 /* From GnuTLS lib/gnutls_x509.c */
911 static int
912 parse_pkcs12 (gnutls_pkcs12_t p12,
913 const char *password,
914 gnutls_x509_privkey_t * key,
915 gnutls_x509_crt_t * cert)
917 gnutls_pkcs12_bag_t bag = NULL;
918 int index = 0;
919 int ret;
921 for (;;)
923 int elements_in_bag;
924 int i;
926 ret = gnutls_pkcs12_bag_init (&bag);
927 if (ret < 0)
929 bag = NULL;
930 goto done;
933 ret = gnutls_pkcs12_get_bag (p12, index, bag);
934 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
935 break;
936 if (ret < 0)
938 goto done;
941 ret = gnutls_pkcs12_bag_get_type (bag, 0);
942 if (ret < 0)
944 goto done;
947 if (ret == GNUTLS_BAG_ENCRYPTED)
949 ret = gnutls_pkcs12_bag_decrypt (bag, password);
950 if (ret < 0)
952 goto done;
956 elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
957 if (elements_in_bag < 0)
959 goto done;
962 for (i = 0; i < elements_in_bag; i++)
964 int type;
965 gnutls_datum_t data;
967 type = gnutls_pkcs12_bag_get_type (bag, i);
968 if (type < 0)
970 goto done;
973 ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
974 if (ret < 0)
976 goto done;
979 switch (type)
981 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
982 case GNUTLS_BAG_PKCS8_KEY:
983 ret = gnutls_x509_privkey_init (key);
984 if (ret < 0)
986 goto done;
989 ret = gnutls_x509_privkey_import_pkcs8
990 (*key, &data, GNUTLS_X509_FMT_DER, password,
991 type == GNUTLS_BAG_PKCS8_KEY ? GNUTLS_PKCS_PLAIN : 0);
992 if (ret < 0)
994 goto done;
996 break;
998 case GNUTLS_BAG_CERTIFICATE:
999 ret = gnutls_x509_crt_init (cert);
1000 if (ret < 0)
1002 goto done;
1005 ret =
1006 gnutls_x509_crt_import (*cert, &data, GNUTLS_X509_FMT_DER);
1007 if (ret < 0)
1009 goto done;
1011 break;
1013 case GNUTLS_BAG_ENCRYPTED:
1014 /* XXX Bother to recurse one level down? Unlikely to
1015 use the same password anyway. */
1016 case GNUTLS_BAG_EMPTY:
1017 default:
1018 break;
1022 index++;
1023 gnutls_pkcs12_bag_deinit (bag);
1026 ret = 0;
1028 done:
1029 if (bag)
1030 gnutls_pkcs12_bag_deinit (bag);
1032 return ret;
1034 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
1035 gnutls_x509_crt_t *x509, gnutls_x509_privkey_t *pkey)
1037 gnutls_pkcs12_t p12 = NULL;
1039 int r;
1041 *x509 = NULL;
1042 *pkey = NULL;
1043 if (!file)
1044 return;
1046 if (is_file_exist(file)) {
1047 FILE *fp = claws_fopen(file, "r");
1048 if (fp) {
1049 p12 = gnutls_import_PKCS12_fp(fp, GNUTLS_X509_FMT_DER);
1050 claws_fclose(fp);
1051 if (!p12) {
1052 log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
1054 } else {
1055 log_error(LOG_PROTOCOL, _("Cannot open P12 certificate file %s (%s)\n"),
1056 file, g_strerror(errno));
1058 } else {
1059 log_error(LOG_PROTOCOL, _("P12 Certificate file %s missing (%s)\n"), file,
1060 g_strerror(errno));
1062 if (p12 != NULL) {
1063 if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
1064 debug_print("got p12\n");
1065 } else {
1066 log_error(LOG_PROTOCOL, "%s\n", gnutls_strerror(r));
1068 gnutls_pkcs12_deinit(p12);
1072 gboolean ssl_certificate_check_subject_cn(SSLCertificate *cert)
1074 return gnutls_x509_crt_check_hostname(cert->x509_cert, cert->host) != 0;
1077 gchar *ssl_certificate_get_subject_cn(SSLCertificate *cert)
1079 gchar subject_cn[BUFFSIZE];
1080 size_t n = BUFFSIZE;
1082 if(gnutls_x509_crt_get_dn_by_oid(cert->x509_cert,
1083 GNUTLS_OID_X520_COMMON_NAME, 0, 0, subject_cn, &n))
1084 return g_strdup(_("<not in certificate>"));
1086 return g_strdup(subject_cn);
1089 #endif /* USE_GNUTLS */