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/>.
21 #include "claws-features.h"
25 #include <gnutls/gnutls.h>
26 #include <gnutls/x509.h>
27 #include <gnutls/pkcs12.h>
31 #include <sys/types.h>
34 #include <glib/gi18n.h>
37 # include <winsock2.h>
39 # include <sys/socket.h>
40 # include <netinet/in.h>
42 #endif /* G_OS_WIN32 */
43 #include "ssl_certificate.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)
60 if (fp != NULL && prefs_common_unsafe_ssl_certs()) {
61 filename = g_strconcat(host, ".", port, ".", fp, ".cert", NULL);
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,
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);
85 char * readable_fingerprint(unsigned char *src, int len)
96 tmp2 = g_strdup_printf("%s:%02X", ret, src[i]);
98 tmp2 = g_strdup_printf("%02X", src[i]);
100 ret = g_strdup(tmp2);
108 static gnutls_x509_crt_t x509_crt_copy(gnutls_x509_crt_t src)
113 gnutls_x509_crt_t dest;
116 if (gnutls_x509_crt_init(&dest) != 0) {
117 g_warning("couldn't gnutls_x509_crt_init");
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);
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);
133 ret = gnutls_x509_crt_import(dest, &tmp, GNUTLS_X509_FMT_DER);
135 g_warning("couldn't gnutls_x509_crt_import for real (%d %s)", ret, gnutls_strerror(ret));
136 gnutls_x509_crt_deinit(dest);
140 g_warning("couldn't gnutls_x509_crt_export for real (%d %s)", ret, gnutls_strerror(ret));
141 gnutls_x509_crt_deinit(dest);
150 static SSLCertificate *ssl_certificate_new(gnutls_x509_crt_t x509_cert, const gchar *host, gushort port)
152 SSLCertificate *cert = g_new0(SSLCertificate, 1);
154 unsigned char md[128];
156 if (host == NULL || x509_cert == NULL) {
157 ssl_certificate_destroy(cert);
160 cert->x509_cert = x509_crt_copy(x509_cert);
161 cert->status = (guint)-1;
162 cert->host = g_strdup(host);
167 gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_MD5, md, &n);
168 cert->fingerprint = readable_fingerprint(md, (int)n);
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;
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);
183 debug_print("writing %Iu bytes\n",cert_size);
185 debug_print("writing %zd bytes\n",cert_size);
187 if (claws_fwrite(&output, 1, cert_size, fp) < cert_size) {
188 g_warning("failed to write cert: %d %s", errno, g_strerror(errno));
192 size_t gnutls_i2d_X509(gnutls_x509_crt_t x509_cert, unsigned char **output)
194 size_t cert_size = 10*1024;
200 *output = malloc(cert_size);
202 if ((r = gnutls_x509_crt_export(x509_cert, GNUTLS_X509_FMT_DER, *output, &cert_size)) < 0) {
203 g_warning("couldn't export cert %s (%"G_GSIZE_FORMAT")", gnutls_strerror(r), cert_size);
211 size_t gnutls_i2d_PrivateKey(gnutls_x509_privkey_t pkey, unsigned char **output)
213 size_t key_size = 10*1024;
219 *output = malloc(key_size);
221 if ((r = gnutls_x509_privkey_export(pkey, GNUTLS_X509_FMT_DER, *output, &key_size)) < 0) {
222 g_warning("couldn't export key %s (%"G_GSIZE_FORMAT")", gnutls_strerror(r), key_size);
230 static int gnutls_import_X509_list_fp(FILE *fp, gnutls_x509_crt_fmt_t format,
231 gnutls_x509_crt_t **cert_list, gint *num_certs)
233 gnutls_x509_crt_t *crt_list;
234 unsigned int max = 512;
235 unsigned int flags = 0;
246 if (fstat(fileno(fp), &s) < 0) {
251 crt_list=(gnutls_x509_crt_t*)malloc(max*sizeof(gnutls_x509_crt_t));
252 tmp.data = malloc(s.st_size);
253 memset(tmp.data, 0, s.st_size);
254 tmp.size = s.st_size;
255 if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
256 perror("claws_fread");
262 if ((r = gnutls_x509_crt_list_import(crt_list, &max,
263 &tmp, format, flags)) < 0) {
264 debug_print("cert import failed: %s\n", gnutls_strerror(r));
270 debug_print("got %d certs in crt_list! %p\n", max, &crt_list);
272 *cert_list = crt_list;
278 /* return one certificate, read from file */
279 static gnutls_x509_crt_t gnutls_import_X509_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
281 gnutls_x509_crt_t *certs = NULL;
282 gnutls_x509_crt_t cert = NULL;
285 if ((r = gnutls_import_X509_list_fp(fp, format, &certs, &ncerts)) < 0) {
292 for (i = 1; i < ncerts; i++)
293 gnutls_x509_crt_deinit(certs[i]);
301 static gnutls_x509_privkey_t gnutls_import_key_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
303 gnutls_x509_privkey_t key = NULL;
307 if (fstat(fileno(fp), &s) < 0) {
311 tmp.data = malloc(s.st_size);
312 memset(tmp.data, 0, s.st_size);
313 tmp.size = s.st_size;
314 if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
315 perror("claws_fread");
320 gnutls_x509_privkey_init(&key);
321 if ((r = gnutls_x509_privkey_import(key, &tmp, format)) < 0) {
322 debug_print("key import failed: %s\n", gnutls_strerror(r));
323 gnutls_x509_privkey_deinit(key);
327 debug_print("got key! %p\n", key);
331 static gnutls_pkcs12_t gnutls_import_PKCS12_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
333 gnutls_pkcs12_t p12 = NULL;
337 if (fstat(fileno(fp), &s) < 0) {
338 log_error(LOG_PROTOCOL, _("Cannot stat P12 certificate file (%s)\n"),
342 tmp.data = malloc(s.st_size);
343 memset(tmp.data, 0, s.st_size);
344 tmp.size = s.st_size;
345 if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
346 log_error(LOG_PROTOCOL, _("Cannot read P12 certificate file (%s)\n"),
352 gnutls_pkcs12_init(&p12);
354 if ((r = gnutls_pkcs12_import(p12, &tmp, format, 0)) < 0) {
355 log_error(LOG_PROTOCOL, _("Cannot import P12 certificate file (%s)\n"),
357 gnutls_pkcs12_deinit(p12);
361 debug_print("got p12! %p\n", p12);
365 static void ssl_certificate_save (SSLCertificate *cert)
370 file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
371 "certs", G_DIR_SEPARATOR_S, NULL);
373 if (!is_dir_exist(file))
377 port = g_strdup_printf("%d", cert->port);
378 file = get_certificate_path(cert->host, port, cert->fingerprint);
381 fp = claws_fopen(file, "wb");
384 debug_print("Can't save certificate !\n");
388 gnutls_export_X509_fp(fp, cert->x509_cert, GNUTLS_X509_FMT_DER);
391 claws_safe_fclose(fp);
395 void ssl_certificate_destroy(SSLCertificate *cert)
401 gnutls_x509_crt_deinit(cert->x509_cert);
403 g_free(cert->fingerprint);
408 void ssl_certificate_delete_from_disk(SSLCertificate *cert)
412 buf = g_strdup_printf("%d", cert->port);
413 file = get_certificate_path(cert->host, buf, cert->fingerprint);
416 file = get_certificate_chain_path(cert->host, buf, cert->fingerprint);
422 SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gchar *fingerprint)
426 SSLCertificate *cert = NULL;
427 gnutls_x509_crt_t tmp_x509;
429 gboolean must_rename = FALSE;
431 buf = g_strdup_printf("%d", port);
433 if (fingerprint != NULL) {
434 file = get_certificate_path(host, buf, fingerprint);
435 fp = claws_fopen(file, "rb");
438 /* see if we have the old one */
439 debug_print("didn't get %s\n", file);
441 file = get_certificate_path(host, buf, NULL);
442 fp = claws_fopen(file, "rb");
445 debug_print("got %s\n", file);
446 must_rename = (fingerprint != NULL);
449 debug_print("got %s first try\n", file);
457 if ((tmp_x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_DER)) != NULL) {
458 cert = ssl_certificate_new(tmp_x509, host, port);
459 debug_print("got cert %p\n", cert);
460 gnutls_x509_crt_deinit(tmp_x509);
467 gchar *old = get_certificate_path(host, buf, NULL);
468 gchar *new = get_certificate_path(host, buf, fingerprint);
469 if (strcmp(old, new))
470 move_file(old, new, TRUE);
479 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
483 size_t cert_size_a = 0, cert_size_b = 0;
486 if (cert_a == NULL || cert_b == NULL)
489 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_a))
490 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
491 g_warning("couldn't gnutls_x509_crt_export to get size a %s", gnutls_strerror(r));
495 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_b))
496 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
497 g_warning("couldn't gnutls_x509_crt_export to get size b %s", gnutls_strerror(r));
501 output_a = g_malloc(cert_size_a);
502 output_b = g_malloc(cert_size_b);
503 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
504 g_warning("couldn't gnutls_x509_crt_export a %s", gnutls_strerror(r));
509 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
510 g_warning("couldn't gnutls_x509_crt_export b %s", gnutls_strerror(r));
515 if (cert_size_a != cert_size_b) {
516 g_warning("size differ %"G_GSIZE_FORMAT" %"G_GSIZE_FORMAT, cert_size_a, cert_size_b);
521 if (memcmp(output_a, output_b, cert_size_a)) {
522 g_warning("contents differ");
533 static guint check_cert(SSLCertificate *cert)
535 gnutls_x509_crt_t *ca_list = NULL;
536 gnutls_x509_crt_t *chain = NULL;
537 unsigned int max_ca = 512, max_certs;
538 unsigned int flags = 0;
541 gchar *chain_file = NULL, *buf = NULL;
544 if (claws_ssl_get_cert_file())
545 fp = claws_fopen(claws_ssl_get_cert_file(), "r");
552 if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &ca_list, &max_ca)) < 0) {
553 debug_print("CA import failed: %s\n", gnutls_strerror(r));
560 buf = g_strdup_printf("%d", cert->port);
561 chain_file = get_certificate_chain_path(cert->host, buf, cert->fingerprint);
563 if (is_file_exist(chain_file)) {
564 unsigned char md[128];
568 fp = claws_fopen(chain_file, "r");
570 debug_print("claws_fopen %s failed: %s\n", chain_file, g_strerror(errno));
574 if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &chain, &max_certs)) < 0) {
575 debug_print("chain import failed: %s\n", gnutls_strerror(r));
584 gnutls_x509_crt_get_fingerprint(chain[0], GNUTLS_DIG_MD5, md, &n);
585 fingerprint = readable_fingerprint(md, n);
586 if (!fingerprint || strcmp(fingerprint, cert->fingerprint)) {
587 debug_print("saved chain fingerprint does not match current : %s / %s\n",
588 cert->fingerprint, fingerprint);
594 r = gnutls_x509_crt_list_verify (chain,
598 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
601 debug_print("chain check failed: %s\n", gnutls_strerror(r));
603 for (i = 0; i < max_certs; i++)
604 gnutls_x509_crt_deinit(chain[i]);
608 r = gnutls_x509_crt_verify(cert->x509_cert, ca_list, max_ca, flags, &status);
610 debug_print("cert check failed: %s\n", gnutls_strerror(r));
613 for (i = 0; i < max_ca; i++)
614 gnutls_x509_crt_deinit(ca_list[i]);
624 static gboolean ssl_certificate_is_valid(SSLCertificate *cert, guint status)
626 gchar *str_status = ssl_certificate_check_signer(cert, status);
628 if (str_status != NULL) {
632 return ssl_certificate_check_subject_cn(cert);
635 char *ssl_certificate_check_signer (SSLCertificate *cert, guint status)
637 gnutls_x509_crt_t x509_cert = cert ? cert->x509_cert : NULL;
640 return g_strdup(_("Internal error"));
642 if (status == (guint)-1) {
643 status = check_cert(cert);
645 return g_strdup(_("Uncheckable"));
647 if (status & GNUTLS_CERT_INVALID) {
648 if (gnutls_x509_crt_check_issuer(x509_cert, x509_cert))
649 return g_strdup(_("Self-signed certificate"));
651 if (status & GNUTLS_CERT_REVOKED)
652 return g_strdup(_("Revoked certificate"));
653 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
654 return g_strdup(_("No certificate issuer found"));
655 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
656 return g_strdup(_("Certificate issuer is not a CA"));
662 static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const gchar *host, gushort port)
668 for (i = 0; i < len; i++) {
670 unsigned char md[128];
671 gnutls_x509_crt_t cert = certs[i];
675 gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, md, &n);
676 gchar *fingerprint = readable_fingerprint(md, n);
677 gchar *buf = g_strdup_printf("%d", port);
679 file = get_certificate_chain_path(host, buf, fingerprint);
684 fp = claws_fopen(file, "wb");
687 debug_print("Can't save certificate !\n");
693 gnutls_export_X509_fp(fp, cert, GNUTLS_X509_FMT_PEM);
697 claws_safe_fclose(fp);
700 gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status,
701 const gchar *host, gushort port,
702 gboolean accept_if_valid)
704 SSLCertificate *current_cert = NULL;
705 SSLCertificate *known_cert;
706 SSLCertHookData cert_hook_data;
709 unsigned char md[128];
710 gboolean valid = FALSE;
712 current_cert = ssl_certificate_new(x509_cert, host, port);
714 if (current_cert == NULL) {
715 debug_print("Buggy certificate !\n");
719 current_cert->status = status;
722 gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
723 fingerprint = readable_fingerprint(md, n);
725 known_cert = ssl_certificate_find(host, port, fingerprint);
730 valid = ssl_certificate_is_valid(current_cert, status);
732 valid = FALSE; /* Force check */
734 if (known_cert == NULL) {
736 ssl_certificate_save(current_cert);
737 ssl_certificate_destroy(current_cert);
741 cert_hook_data.cert = current_cert;
742 cert_hook_data.old_cert = NULL;
743 cert_hook_data.expired = FALSE;
744 cert_hook_data.accept = FALSE;
746 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
748 if (!cert_hook_data.accept) {
749 ssl_certificate_destroy(current_cert);
752 ssl_certificate_save(current_cert);
753 ssl_certificate_destroy(current_cert);
756 } else if (!ssl_certificate_compare (current_cert, known_cert)) {
758 ssl_certificate_save(current_cert);
759 ssl_certificate_destroy(current_cert);
760 ssl_certificate_destroy(known_cert);
764 cert_hook_data.cert = current_cert;
765 cert_hook_data.old_cert = known_cert;
766 cert_hook_data.expired = FALSE;
767 cert_hook_data.accept = FALSE;
769 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
771 if (!cert_hook_data.accept) {
772 ssl_certificate_destroy(current_cert);
773 ssl_certificate_destroy(known_cert);
776 ssl_certificate_save(current_cert);
777 ssl_certificate_destroy(current_cert);
778 ssl_certificate_destroy(known_cert);
781 } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
782 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
784 if (warned_expired == NULL)
785 warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
787 if (g_hash_table_lookup(warned_expired, tmp)) {
789 ssl_certificate_destroy(current_cert);
790 ssl_certificate_destroy(known_cert);
794 cert_hook_data.cert = current_cert;
795 cert_hook_data.old_cert = NULL;
796 cert_hook_data.expired = TRUE;
797 cert_hook_data.accept = FALSE;
799 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
801 if (!cert_hook_data.accept) {
803 ssl_certificate_destroy(current_cert);
804 ssl_certificate_destroy(known_cert);
807 g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
808 ssl_certificate_destroy(current_cert);
809 ssl_certificate_destroy(known_cert);
814 ssl_certificate_destroy(current_cert);
815 ssl_certificate_destroy(known_cert);
819 gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len,
820 const gchar *host, gushort port,
821 gboolean accept_if_valid)
824 gnutls_x509_crt_t *cas = NULL;
825 gboolean result = FALSE;
829 if (claws_ssl_get_cert_file()) {
830 FILE *fp = claws_fopen(claws_ssl_get_cert_file(), "rb");
834 r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &cas, &ncas);
839 g_warning("Can't read SSL_CERT_FILE '%s': %s",
840 claws_ssl_get_cert_file(),
843 debug_print("Can't find SSL ca-certificates file\n");
847 gnutls_x509_crt_list_verify (certs,
851 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
854 result = ssl_certificate_check(certs[0], status, host, port,
857 if (result == TRUE) {
858 ssl_certificate_save_chain(certs, chain_len, host, port);
861 for (i = 0; i < ncas; i++)
862 gnutls_x509_crt_deinit(cas[i]);
868 gnutls_x509_crt_t ssl_certificate_get_x509_from_pem_file(const gchar *file)
870 gnutls_x509_crt_t x509 = NULL;
874 if (is_file_exist(file)) {
875 FILE *fp = claws_fopen(file, "r");
877 x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_PEM);
881 log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
882 file, g_strerror(errno));
885 log_error(LOG_PROTOCOL, _("Certificate file %s missing (%s)\n"),
886 file, g_strerror(errno));
891 gnutls_x509_privkey_t ssl_certificate_get_pkey_from_pem_file(const gchar *file)
893 gnutls_x509_privkey_t key = NULL;
897 if (is_file_exist(file)) {
898 FILE *fp = claws_fopen(file, "r");
900 key = gnutls_import_key_fp(fp, GNUTLS_X509_FMT_PEM);
904 log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
905 file, g_strerror(errno));
908 log_error(LOG_PROTOCOL, _("Key file %s missing (%s)\n"), file,
914 /* From GnuTLS lib/gnutls_x509.c */
916 parse_pkcs12 (gnutls_pkcs12_t p12,
917 const char *password,
918 gnutls_x509_privkey_t * key,
919 gnutls_x509_crt_t * cert)
921 gnutls_pkcs12_bag_t bag = NULL;
930 ret = gnutls_pkcs12_bag_init (&bag);
937 ret = gnutls_pkcs12_get_bag (p12, index, bag);
938 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
945 ret = gnutls_pkcs12_bag_get_type (bag, 0);
951 if (ret == GNUTLS_BAG_ENCRYPTED)
953 ret = gnutls_pkcs12_bag_decrypt (bag, password);
960 elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
961 if (elements_in_bag < 0)
966 for (i = 0; i < elements_in_bag; i++)
971 type = gnutls_pkcs12_bag_get_type (bag, i);
977 ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
985 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
986 case GNUTLS_BAG_PKCS8_KEY:
987 ret = gnutls_x509_privkey_init (key);
993 ret = gnutls_x509_privkey_import_pkcs8
994 (*key, &data, GNUTLS_X509_FMT_DER, password,
995 type == GNUTLS_BAG_PKCS8_KEY ? GNUTLS_PKCS_PLAIN : 0);
1002 case GNUTLS_BAG_CERTIFICATE:
1003 ret = gnutls_x509_crt_init (cert);
1010 gnutls_x509_crt_import (*cert, &data, GNUTLS_X509_FMT_DER);
1017 case GNUTLS_BAG_ENCRYPTED:
1018 /* XXX Bother to recurse one level down? Unlikely to
1019 use the same password anyway. */
1020 case GNUTLS_BAG_EMPTY:
1027 gnutls_pkcs12_bag_deinit (bag);
1034 gnutls_pkcs12_bag_deinit (bag);
1038 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
1039 gnutls_x509_crt_t *x509, gnutls_x509_privkey_t *pkey)
1041 gnutls_pkcs12_t p12 = NULL;
1050 if (is_file_exist(file)) {
1051 FILE *fp = claws_fopen(file, "r");
1053 p12 = gnutls_import_PKCS12_fp(fp, GNUTLS_X509_FMT_DER);
1056 log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
1059 log_error(LOG_PROTOCOL, _("Cannot open P12 certificate file %s (%s)\n"),
1060 file, g_strerror(errno));
1063 log_error(LOG_PROTOCOL, _("P12 Certificate file %s missing (%s)\n"), file,
1067 if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
1068 debug_print("got p12\n");
1070 log_error(LOG_PROTOCOL, "%s\n", gnutls_strerror(r));
1072 gnutls_pkcs12_deinit(p12);
1076 gboolean ssl_certificate_check_subject_cn(SSLCertificate *cert)
1078 return gnutls_x509_crt_check_hostname(cert->x509_cert, cert->host) != 0;
1081 gchar *ssl_certificate_get_subject_cn(SSLCertificate *cert)
1083 gchar subject_cn[BUFFSIZE];
1084 size_t n = BUFFSIZE;
1086 if(gnutls_x509_crt_get_dn_by_oid(cert->x509_cert,
1087 GNUTLS_OID_X520_COMMON_NAME, 0, 0, subject_cn, &n))
1088 return g_strdup(_("<not in certificate>"));
1090 return g_strdup(subject_cn);
1093 #endif /* USE_GNUTLS */