2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2015 Colin Leroy <colin@colino.net>
4 * and the Claws Mail team
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.
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.
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/>.
23 #include "claws-features.h"
27 #include <gnutls/gnutls.h>
28 #include <gnutls/x509.h>
29 #include <gnutls/pkcs12.h>
33 #include <sys/types.h>
36 #include <glib/gi18n.h>
39 # include <winsock2.h>
41 # include <sys/socket.h>
42 # include <netinet/in.h>
44 #endif /* G_OS_WIN32 */
45 #include "ssl_certificate.h"
52 static GHashTable *warned_expired = NULL;
54 gboolean prefs_common_unsafe_ssl_certs(void);
56 static gchar *get_certificate_path(const gchar *host, const gchar *port, const gchar *fp)
58 if (fp != NULL && prefs_common_unsafe_ssl_certs())
59 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
60 "certs", G_DIR_SEPARATOR_S,
61 host, ".", port, ".", fp, ".cert", NULL);
63 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
64 "certs", G_DIR_SEPARATOR_S,
65 host, ".", port, ".cert", NULL);
68 static gchar *get_certificate_chain_path(const gchar *host, const gchar *port, const gchar *fp)
70 gchar *tmp = get_certificate_path(host, port, fp);
71 gchar *result = g_strconcat(tmp, ".chain", NULL);
78 char * readable_fingerprint(unsigned char *src, int len)
89 tmp2 = g_strdup_printf("%s:%02X", ret, src[i]);
91 tmp2 = g_strdup_printf("%02X", src[i]);
101 static gnutls_x509_crt_t x509_crt_copy(gnutls_x509_crt_t src)
106 gnutls_x509_crt_t dest;
109 if (gnutls_x509_crt_init(&dest) != 0) {
110 g_warning("couldn't gnutls_x509_crt_init\n");
114 if (gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, NULL, &size)
115 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
116 g_warning("couldn't gnutls_x509_crt_export to get size\n");
117 gnutls_x509_crt_deinit(dest);
121 tmp.data = malloc(size);
122 memset(tmp.data, 0, size);
123 ret = gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, tmp.data, &size);
126 ret = gnutls_x509_crt_import(dest, &tmp, GNUTLS_X509_FMT_DER);
128 g_warning("couldn't gnutls_x509_crt_import for real (%d %s)\n", ret, gnutls_strerror(ret));
129 gnutls_x509_crt_deinit(dest);
133 g_warning("couldn't gnutls_x509_crt_export for real (%d %s)\n", ret, gnutls_strerror(ret));
134 gnutls_x509_crt_deinit(dest);
143 static SSLCertificate *ssl_certificate_new(gnutls_x509_crt_t x509_cert, const gchar *host, gushort port)
145 SSLCertificate *cert = g_new0(SSLCertificate, 1);
147 unsigned char md[128];
149 if (host == NULL || x509_cert == NULL) {
150 ssl_certificate_destroy(cert);
153 cert->x509_cert = x509_crt_copy(x509_cert);
154 cert->status = (guint)-1;
155 cert->host = g_strdup(host);
160 gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_MD5, md, &n);
161 cert->fingerprint = readable_fingerprint(md, (int)n);
165 static void gnutls_export_X509_fp(FILE *fp, gnutls_x509_crt_t x509_cert, gnutls_x509_crt_fmt_t format)
167 char output[10*1024];
168 size_t cert_size = 10*1024;
171 if ((r = gnutls_x509_crt_export(x509_cert, format, output, &cert_size)) < 0) {
172 g_warning("couldn't export cert %s (%zd)\n", gnutls_strerror(r), cert_size);
176 debug_print("writing %zd bytes\n",cert_size);
177 if (fwrite(&output, 1, cert_size, fp) < cert_size) {
178 g_warning("failed to write cert: %d %s\n", errno, g_strerror(errno));
182 size_t gnutls_i2d_X509(gnutls_x509_crt_t x509_cert, unsigned char **output)
184 size_t cert_size = 10*1024;
190 *output = malloc(cert_size);
192 if ((r = gnutls_x509_crt_export(x509_cert, GNUTLS_X509_FMT_DER, *output, &cert_size)) < 0) {
193 g_warning("couldn't export cert %s (%zd)\n", gnutls_strerror(r), cert_size);
201 size_t gnutls_i2d_PrivateKey(gnutls_x509_privkey_t pkey, unsigned char **output)
203 size_t key_size = 10*1024;
209 *output = malloc(key_size);
211 if ((r = gnutls_x509_privkey_export(pkey, GNUTLS_X509_FMT_DER, *output, &key_size)) < 0) {
212 g_warning("couldn't export key %s (%zd)\n", gnutls_strerror(r), key_size);
220 static int gnutls_import_X509_list_fp(FILE *fp, gnutls_x509_crt_fmt_t format,
221 gnutls_x509_crt_t **cert_list, gint *num_certs)
223 gnutls_x509_crt_t *crt_list;
224 unsigned int max = 512;
225 unsigned int flags = 0;
236 if (fstat(fileno(fp), &s) < 0) {
241 crt_list=(gnutls_x509_crt_t*)malloc(max*sizeof(gnutls_x509_crt_t));
242 tmp.data = malloc(s.st_size);
243 memset(tmp.data, 0, s.st_size);
244 tmp.size = s.st_size;
245 if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
252 if ((r = gnutls_x509_crt_list_import(crt_list, &max,
253 &tmp, format, flags)) < 0) {
254 debug_print("cert import failed: %s\n", gnutls_strerror(r));
260 debug_print("got %d certs in crt_list! %p\n", max, &crt_list);
262 *cert_list = crt_list;
268 /* return one certificate, read from file */
269 static gnutls_x509_crt_t gnutls_import_X509_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
271 gnutls_x509_crt_t *certs = NULL;
272 gnutls_x509_crt_t cert = NULL;
275 if ((r = gnutls_import_X509_list_fp(fp, format, &certs, &ncerts)) < 0) {
282 for (i = 1; i < ncerts; i++)
283 gnutls_x509_crt_deinit(certs[i]);
291 static gnutls_x509_privkey_t gnutls_import_key_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
293 gnutls_x509_privkey_t key = NULL;
297 if (fstat(fileno(fp), &s) < 0) {
301 tmp.data = malloc(s.st_size);
302 memset(tmp.data, 0, s.st_size);
303 tmp.size = s.st_size;
304 if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
310 gnutls_x509_privkey_init(&key);
311 if ((r = gnutls_x509_privkey_import(key, &tmp, format)) < 0) {
312 debug_print("key import failed: %s\n", gnutls_strerror(r));
313 gnutls_x509_privkey_deinit(key);
317 debug_print("got key! %p\n", key);
321 static gnutls_pkcs12_t gnutls_import_PKCS12_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
323 gnutls_pkcs12_t p12 = NULL;
327 if (fstat(fileno(fp), &s) < 0) {
328 log_error(LOG_PROTOCOL, _("Cannot stat P12 certificate file (%s)\n"),
332 tmp.data = malloc(s.st_size);
333 memset(tmp.data, 0, s.st_size);
334 tmp.size = s.st_size;
335 if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
336 log_error(LOG_PROTOCOL, _("Cannot read P12 certificate file (%s)\n"),
342 gnutls_pkcs12_init(&p12);
344 if ((r = gnutls_pkcs12_import(p12, &tmp, format, 0)) < 0) {
345 log_error(LOG_PROTOCOL, _("Cannot import P12 certificate file (%s)\n"),
347 gnutls_pkcs12_deinit(p12);
351 debug_print("got p12! %p\n", p12);
355 static void ssl_certificate_save (SSLCertificate *cert)
360 file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
361 "certs", G_DIR_SEPARATOR_S, NULL);
363 if (!is_dir_exist(file))
367 port = g_strdup_printf("%d", cert->port);
368 file = get_certificate_path(cert->host, port, cert->fingerprint);
371 fp = g_fopen(file, "wb");
374 debug_print("Can't save certificate !\n");
378 gnutls_export_X509_fp(fp, cert->x509_cert, GNUTLS_X509_FMT_DER);
385 void ssl_certificate_destroy(SSLCertificate *cert)
391 gnutls_x509_crt_deinit(cert->x509_cert);
393 g_free(cert->fingerprint);
398 void ssl_certificate_delete_from_disk(SSLCertificate *cert)
402 buf = g_strdup_printf("%d", cert->port);
403 file = get_certificate_path(cert->host, buf, cert->fingerprint);
406 file = get_certificate_chain_path(cert->host, buf, cert->fingerprint);
412 SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gchar *fingerprint)
416 SSLCertificate *cert = NULL;
417 gnutls_x509_crt_t tmp_x509;
419 gboolean must_rename = FALSE;
421 buf = g_strdup_printf("%d", port);
423 if (fingerprint != NULL) {
424 file = get_certificate_path(host, buf, fingerprint);
425 fp = g_fopen(file, "rb");
428 /* see if we have the old one */
429 debug_print("didn't get %s\n", file);
431 file = get_certificate_path(host, buf, NULL);
432 fp = g_fopen(file, "rb");
435 debug_print("got %s\n", file);
436 must_rename = (fingerprint != NULL);
439 debug_print("got %s first try\n", file);
447 if ((tmp_x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_DER)) != NULL) {
448 cert = ssl_certificate_new(tmp_x509, host, port);
449 debug_print("got cert %p\n", cert);
450 gnutls_x509_crt_deinit(tmp_x509);
457 gchar *old = get_certificate_path(host, buf, NULL);
458 gchar *new = get_certificate_path(host, buf, fingerprint);
459 if (strcmp(old, new))
460 move_file(old, new, TRUE);
469 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
473 size_t cert_size_a = 0, cert_size_b = 0;
476 if (cert_a == NULL || cert_b == NULL)
479 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_a))
480 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
481 g_warning("couldn't gnutls_x509_crt_export to get size a %s\n", gnutls_strerror(r));
485 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_b))
486 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
487 g_warning("couldn't gnutls_x509_crt_export to get size b %s\n", gnutls_strerror(r));
491 output_a = g_malloc(cert_size_a);
492 output_b = g_malloc(cert_size_b);
493 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
494 g_warning("couldn't gnutls_x509_crt_export a %s\n", gnutls_strerror(r));
499 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
500 g_warning("couldn't gnutls_x509_crt_export b %s\n", gnutls_strerror(r));
505 if (cert_size_a != cert_size_b) {
506 g_warning("size differ %zd %zd\n", cert_size_a, cert_size_b);
511 if (memcmp(output_a, output_b, cert_size_a)) {
512 g_warning("contents differ\n");
523 static guint check_cert(SSLCertificate *cert)
525 gnutls_x509_crt_t *ca_list = NULL;
526 gnutls_x509_crt_t *chain = NULL;
527 unsigned int max_ca = 512, max_certs;
528 unsigned int flags = 0;
531 gchar *chain_file = NULL, *buf = NULL;
534 if (claws_ssl_get_cert_file())
535 fp = g_fopen(claws_ssl_get_cert_file(), "r");
542 if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &ca_list, &max_ca)) < 0) {
543 debug_print("CA import failed: %s\n", gnutls_strerror(r));
550 buf = g_strdup_printf("%d", cert->port);
551 chain_file = get_certificate_chain_path(cert->host, buf, cert->fingerprint);
553 if (is_file_exist(chain_file)) {
554 unsigned char md[128];
558 fp = g_fopen(chain_file, "r");
560 debug_print("fopen %s failed: %s\n", chain_file, g_strerror(errno));
564 if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &chain, &max_certs)) < 0) {
565 debug_print("chain import failed: %s\n", gnutls_strerror(r));
574 gnutls_x509_crt_get_fingerprint(chain[0], GNUTLS_DIG_MD5, md, &n);
575 fingerprint = readable_fingerprint(md, n);
576 if (!fingerprint || strcmp(fingerprint, cert->fingerprint)) {
577 debug_print("Saved chain fingerprint does not match current : %s / %s",
578 cert->fingerprint, fingerprint);
584 r = gnutls_x509_crt_list_verify (chain,
588 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
591 debug_print("chain check failed: %s\n", gnutls_strerror(r));
593 for (i = 0; i < max_certs; i++)
594 gnutls_x509_crt_deinit(chain[i]);
598 r = gnutls_x509_crt_verify(cert->x509_cert, ca_list, max_ca, flags, &status);
600 debug_print("cert check failed: %s\n", gnutls_strerror(r));
603 for (i = 0; i < max_ca; i++)
604 gnutls_x509_crt_deinit(ca_list[i]);
614 static gboolean ssl_certificate_is_valid(SSLCertificate *cert, guint status)
616 gchar *str_status = ssl_certificate_check_signer(cert, status);
618 if (str_status != NULL) {
622 return ssl_certificate_check_subject_cn(cert);
625 char *ssl_certificate_check_signer (SSLCertificate *cert, guint status)
627 gnutls_x509_crt_t x509_cert = cert ? cert->x509_cert : NULL;
630 return g_strdup(_("Internal error"));
632 if (status == (guint)-1) {
633 status = check_cert(cert);
635 return g_strdup(_("Uncheckable"));
637 if (status & GNUTLS_CERT_INVALID) {
638 if (gnutls_x509_crt_check_issuer(x509_cert, x509_cert))
639 return g_strdup(_("Self-signed certificate"));
641 if (status & GNUTLS_CERT_REVOKED)
642 return g_strdup(_("Revoked certificate"));
643 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
644 return g_strdup(_("No certificate issuer found"));
645 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
646 return g_strdup(_("Certificate issuer is not a CA"));
652 static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const gchar *host, gushort port)
658 for (i = 0; i < len; i++) {
660 unsigned char md[128];
661 gnutls_x509_crt_t cert = certs[i];
666 gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, md, &n);
667 fingerprint = readable_fingerprint(md, n);
668 gchar *buf = g_strdup_printf("%d", port);
670 file = get_certificate_chain_path(host, buf, fingerprint);
674 fp = g_fopen(file, "wb");
677 debug_print("Can't save certificate !\n");
683 gnutls_export_X509_fp(fp, cert, GNUTLS_X509_FMT_PEM);
690 gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status,
691 const gchar *host, gushort port,
692 gboolean accept_if_valid)
694 SSLCertificate *current_cert = NULL;
695 SSLCertificate *known_cert;
696 SSLCertHookData cert_hook_data;
699 unsigned char md[128];
700 gboolean valid = FALSE;
702 current_cert = ssl_certificate_new(x509_cert, host, port);
704 if (current_cert == NULL) {
705 debug_print("Buggy certificate !\n");
709 current_cert->status = status;
712 gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
713 fingerprint = readable_fingerprint(md, n);
715 known_cert = ssl_certificate_find(host, port, fingerprint);
720 valid = ssl_certificate_is_valid(current_cert, status);
722 valid = FALSE; /* Force check */
724 if (known_cert == NULL) {
726 ssl_certificate_save(current_cert);
727 ssl_certificate_destroy(current_cert);
731 cert_hook_data.cert = current_cert;
732 cert_hook_data.old_cert = NULL;
733 cert_hook_data.expired = FALSE;
734 cert_hook_data.accept = FALSE;
736 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
738 if (!cert_hook_data.accept) {
739 ssl_certificate_destroy(current_cert);
742 ssl_certificate_save(current_cert);
743 ssl_certificate_destroy(current_cert);
746 } else if (!ssl_certificate_compare (current_cert, known_cert)) {
748 ssl_certificate_save(current_cert);
749 ssl_certificate_destroy(current_cert);
750 ssl_certificate_destroy(known_cert);
754 cert_hook_data.cert = current_cert;
755 cert_hook_data.old_cert = known_cert;
756 cert_hook_data.expired = FALSE;
757 cert_hook_data.accept = FALSE;
759 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
761 if (!cert_hook_data.accept) {
762 ssl_certificate_destroy(current_cert);
763 ssl_certificate_destroy(known_cert);
766 ssl_certificate_save(current_cert);
767 ssl_certificate_destroy(current_cert);
768 ssl_certificate_destroy(known_cert);
771 } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
772 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
774 if (warned_expired == NULL)
775 warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
777 if (g_hash_table_lookup(warned_expired, tmp)) {
779 ssl_certificate_destroy(current_cert);
780 ssl_certificate_destroy(known_cert);
784 cert_hook_data.cert = current_cert;
785 cert_hook_data.old_cert = NULL;
786 cert_hook_data.expired = TRUE;
787 cert_hook_data.accept = FALSE;
789 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
791 if (!cert_hook_data.accept) {
793 ssl_certificate_destroy(current_cert);
794 ssl_certificate_destroy(known_cert);
797 g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
798 ssl_certificate_destroy(current_cert);
799 ssl_certificate_destroy(known_cert);
804 ssl_certificate_destroy(current_cert);
805 ssl_certificate_destroy(known_cert);
809 gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len,
810 const gchar *host, gushort port,
811 gboolean accept_if_valid)
814 gnutls_x509_crt_t *cas = NULL;
815 gboolean result = FALSE;
819 if (claws_ssl_get_cert_file()) {
820 FILE *fp = g_fopen(claws_ssl_get_cert_file(), "rb");
824 r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &cas, &ncas);
829 g_warning("Can't read SSL_CERT_FILE %s: %s\n",
830 claws_ssl_get_cert_file(),
833 debug_print("Can't find SSL ca-certificates file\n");
837 gnutls_x509_crt_list_verify (certs,
841 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
844 result = ssl_certificate_check(certs[0], status, host, port,
847 if (result == TRUE) {
848 ssl_certificate_save_chain(certs, chain_len, host, port);
851 for (i = 0; i < ncas; i++)
852 gnutls_x509_crt_deinit(cas[i]);
858 gnutls_x509_crt_t ssl_certificate_get_x509_from_pem_file(const gchar *file)
860 gnutls_x509_crt_t x509 = NULL;
864 if (is_file_exist(file)) {
865 FILE *fp = g_fopen(file, "r");
867 x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_PEM);
871 log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
872 file, g_strerror(errno));
875 log_error(LOG_PROTOCOL, _("Certificate file %s missing (%s)\n"),
876 file, g_strerror(errno));
881 gnutls_x509_privkey_t ssl_certificate_get_pkey_from_pem_file(const gchar *file)
883 gnutls_x509_privkey_t key = NULL;
887 if (is_file_exist(file)) {
888 FILE *fp = g_fopen(file, "r");
890 key = gnutls_import_key_fp(fp, GNUTLS_X509_FMT_PEM);
894 log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
895 file, g_strerror(errno));
898 log_error(LOG_PROTOCOL, _("Key file %s missing (%s)\n"), file,
904 /* From GnuTLS lib/gnutls_x509.c */
906 parse_pkcs12 (gnutls_pkcs12_t p12,
907 const char *password,
908 gnutls_x509_privkey_t * key,
909 gnutls_x509_crt_t * cert)
911 gnutls_pkcs12_bag_t bag = NULL;
920 ret = gnutls_pkcs12_bag_init (&bag);
927 ret = gnutls_pkcs12_get_bag (p12, index, bag);
928 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
935 ret = gnutls_pkcs12_bag_get_type (bag, 0);
941 if (ret == GNUTLS_BAG_ENCRYPTED)
943 ret = gnutls_pkcs12_bag_decrypt (bag, password);
950 elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
951 if (elements_in_bag < 0)
956 for (i = 0; i < elements_in_bag; i++)
961 type = gnutls_pkcs12_bag_get_type (bag, i);
967 ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
975 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
976 case GNUTLS_BAG_PKCS8_KEY:
977 ret = gnutls_x509_privkey_init (key);
983 ret = gnutls_x509_privkey_import_pkcs8
984 (*key, &data, GNUTLS_X509_FMT_DER, password,
985 type == GNUTLS_BAG_PKCS8_KEY ? GNUTLS_PKCS_PLAIN : 0);
992 case GNUTLS_BAG_CERTIFICATE:
993 ret = gnutls_x509_crt_init (cert);
1000 gnutls_x509_crt_import (*cert, &data, GNUTLS_X509_FMT_DER);
1007 case GNUTLS_BAG_ENCRYPTED:
1008 /* XXX Bother to recurse one level down? Unlikely to
1009 use the same password anyway. */
1010 case GNUTLS_BAG_EMPTY:
1017 gnutls_pkcs12_bag_deinit (bag);
1024 gnutls_pkcs12_bag_deinit (bag);
1028 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
1029 gnutls_x509_crt_t *x509, gnutls_x509_privkey_t *pkey)
1031 gnutls_pkcs12_t p12 = NULL;
1040 if (is_file_exist(file)) {
1041 FILE *fp = g_fopen(file, "r");
1043 p12 = gnutls_import_PKCS12_fp(fp, GNUTLS_X509_FMT_DER);
1046 log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
1049 log_error(LOG_PROTOCOL, _("Cannot open P12 certificate file %s (%s)\n"),
1050 file, g_strerror(errno));
1053 log_error(LOG_PROTOCOL, _("P12 Certificate file %s missing (%s)\n"), file,
1057 if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
1058 debug_print("got p12\n");
1060 log_error(LOG_PROTOCOL, "%s\n", gnutls_strerror(r));
1062 gnutls_pkcs12_deinit(p12);
1066 gboolean ssl_certificate_check_subject_cn(SSLCertificate *cert)
1068 return gnutls_x509_crt_check_hostname(cert->x509_cert, cert->host) != 0;
1071 gchar *ssl_certificate_get_subject_cn(SSLCertificate *cert)
1073 gchar subject_cn[BUFFSIZE];
1074 size_t n = BUFFSIZE;
1076 if(gnutls_x509_crt_get_dn_by_oid(cert->x509_cert,
1077 GNUTLS_OID_X520_COMMON_NAME, 0, 0, subject_cn, &n))
1078 return g_strdup(_("<not in certificate>"));
1080 return g_strdup(subject_cn);
1083 #endif /* USE_GNUTLS */