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 "safe_fclose.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 if (fp != NULL && prefs_common_unsafe_ssl_certs())
58 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
59 "certs", G_DIR_SEPARATOR_S,
60 host, ".", port, ".", fp, ".cert", NULL);
62 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
63 "certs", G_DIR_SEPARATOR_S,
64 host, ".", port, ".cert", NULL);
67 static gchar *get_certificate_chain_path(const gchar *host, const gchar *port, const gchar *fp)
69 gchar *tmp = get_certificate_path(host, port, fp);
70 gchar *result = g_strconcat(tmp, ".chain", NULL);
77 char * readable_fingerprint(unsigned char *src, int len)
88 tmp2 = g_strdup_printf("%s:%02X", ret, src[i]);
90 tmp2 = g_strdup_printf("%02X", src[i]);
100 static gnutls_x509_crt_t x509_crt_copy(gnutls_x509_crt_t src)
105 gnutls_x509_crt_t dest;
108 if (gnutls_x509_crt_init(&dest) != 0) {
109 g_warning("couldn't gnutls_x509_crt_init");
113 if (gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, NULL, &size)
114 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
115 g_warning("couldn't gnutls_x509_crt_export to get size");
116 gnutls_x509_crt_deinit(dest);
120 tmp.data = malloc(size);
121 memset(tmp.data, 0, size);
122 ret = gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, tmp.data, &size);
125 ret = gnutls_x509_crt_import(dest, &tmp, GNUTLS_X509_FMT_DER);
127 g_warning("couldn't gnutls_x509_crt_import for real (%d %s)", ret, gnutls_strerror(ret));
128 gnutls_x509_crt_deinit(dest);
132 g_warning("couldn't gnutls_x509_crt_export for real (%d %s)", ret, gnutls_strerror(ret));
133 gnutls_x509_crt_deinit(dest);
142 static SSLCertificate *ssl_certificate_new(gnutls_x509_crt_t x509_cert, const gchar *host, gushort port)
144 SSLCertificate *cert = g_new0(SSLCertificate, 1);
146 unsigned char md[128];
148 if (host == NULL || x509_cert == NULL) {
149 ssl_certificate_destroy(cert);
152 cert->x509_cert = x509_crt_copy(x509_cert);
153 cert->status = (guint)-1;
154 cert->host = g_strdup(host);
159 gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_MD5, md, &n);
160 cert->fingerprint = readable_fingerprint(md, (int)n);
164 static void gnutls_export_X509_fp(FILE *fp, gnutls_x509_crt_t x509_cert, gnutls_x509_crt_fmt_t format)
166 char output[10*1024];
167 size_t cert_size = 10*1024;
170 if ((r = gnutls_x509_crt_export(x509_cert, format, output, &cert_size)) < 0) {
171 g_warning("couldn't export cert %s (%zd)", gnutls_strerror(r), cert_size);
175 debug_print("writing %zd bytes\n",cert_size);
176 if (fwrite(&output, 1, cert_size, fp) < cert_size) {
177 g_warning("failed to write cert: %d %s", errno, g_strerror(errno));
181 size_t gnutls_i2d_X509(gnutls_x509_crt_t x509_cert, unsigned char **output)
183 size_t cert_size = 10*1024;
189 *output = malloc(cert_size);
191 if ((r = gnutls_x509_crt_export(x509_cert, GNUTLS_X509_FMT_DER, *output, &cert_size)) < 0) {
192 g_warning("couldn't export cert %s (%zd)", gnutls_strerror(r), cert_size);
200 size_t gnutls_i2d_PrivateKey(gnutls_x509_privkey_t pkey, unsigned char **output)
202 size_t key_size = 10*1024;
208 *output = malloc(key_size);
210 if ((r = gnutls_x509_privkey_export(pkey, GNUTLS_X509_FMT_DER, *output, &key_size)) < 0) {
211 g_warning("couldn't export key %s (%zd)", gnutls_strerror(r), key_size);
219 static int gnutls_import_X509_list_fp(FILE *fp, gnutls_x509_crt_fmt_t format,
220 gnutls_x509_crt_t **cert_list, gint *num_certs)
222 gnutls_x509_crt_t *crt_list;
223 unsigned int max = 512;
224 unsigned int flags = 0;
235 if (fstat(fileno(fp), &s) < 0) {
240 crt_list=(gnutls_x509_crt_t*)malloc(max*sizeof(gnutls_x509_crt_t));
241 tmp.data = malloc(s.st_size);
242 memset(tmp.data, 0, s.st_size);
243 tmp.size = s.st_size;
244 if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
251 if ((r = gnutls_x509_crt_list_import(crt_list, &max,
252 &tmp, format, flags)) < 0) {
253 debug_print("cert import failed: %s\n", gnutls_strerror(r));
259 debug_print("got %d certs in crt_list! %p\n", max, &crt_list);
261 *cert_list = crt_list;
267 /* return one certificate, read from file */
268 static gnutls_x509_crt_t gnutls_import_X509_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
270 gnutls_x509_crt_t *certs = NULL;
271 gnutls_x509_crt_t cert = NULL;
274 if ((r = gnutls_import_X509_list_fp(fp, format, &certs, &ncerts)) < 0) {
281 for (i = 1; i < ncerts; i++)
282 gnutls_x509_crt_deinit(certs[i]);
290 static gnutls_x509_privkey_t gnutls_import_key_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
292 gnutls_x509_privkey_t key = NULL;
296 if (fstat(fileno(fp), &s) < 0) {
300 tmp.data = malloc(s.st_size);
301 memset(tmp.data, 0, s.st_size);
302 tmp.size = s.st_size;
303 if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
309 gnutls_x509_privkey_init(&key);
310 if ((r = gnutls_x509_privkey_import(key, &tmp, format)) < 0) {
311 debug_print("key import failed: %s\n", gnutls_strerror(r));
312 gnutls_x509_privkey_deinit(key);
316 debug_print("got key! %p\n", key);
320 static gnutls_pkcs12_t gnutls_import_PKCS12_fp(FILE *fp, gnutls_x509_crt_fmt_t format)
322 gnutls_pkcs12_t p12 = NULL;
326 if (fstat(fileno(fp), &s) < 0) {
327 log_error(LOG_PROTOCOL, _("Cannot stat P12 certificate file (%s)\n"),
331 tmp.data = malloc(s.st_size);
332 memset(tmp.data, 0, s.st_size);
333 tmp.size = s.st_size;
334 if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
335 log_error(LOG_PROTOCOL, _("Cannot read P12 certificate file (%s)\n"),
341 gnutls_pkcs12_init(&p12);
343 if ((r = gnutls_pkcs12_import(p12, &tmp, format, 0)) < 0) {
344 log_error(LOG_PROTOCOL, _("Cannot import P12 certificate file (%s)\n"),
346 gnutls_pkcs12_deinit(p12);
350 debug_print("got p12! %p\n", p12);
354 static void ssl_certificate_save (SSLCertificate *cert)
359 file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
360 "certs", G_DIR_SEPARATOR_S, NULL);
362 if (!is_dir_exist(file))
366 port = g_strdup_printf("%d", cert->port);
367 file = get_certificate_path(cert->host, port, cert->fingerprint);
370 fp = g_fopen(file, "wb");
373 debug_print("Can't save certificate !\n");
377 gnutls_export_X509_fp(fp, cert->x509_cert, GNUTLS_X509_FMT_DER);
384 void ssl_certificate_destroy(SSLCertificate *cert)
390 gnutls_x509_crt_deinit(cert->x509_cert);
392 g_free(cert->fingerprint);
397 void ssl_certificate_delete_from_disk(SSLCertificate *cert)
401 buf = g_strdup_printf("%d", cert->port);
402 file = get_certificate_path(cert->host, buf, cert->fingerprint);
405 file = get_certificate_chain_path(cert->host, buf, cert->fingerprint);
411 SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gchar *fingerprint)
415 SSLCertificate *cert = NULL;
416 gnutls_x509_crt_t tmp_x509;
418 gboolean must_rename = FALSE;
420 buf = g_strdup_printf("%d", port);
422 if (fingerprint != NULL) {
423 file = get_certificate_path(host, buf, fingerprint);
424 fp = g_fopen(file, "rb");
427 /* see if we have the old one */
428 debug_print("didn't get %s\n", file);
430 file = get_certificate_path(host, buf, NULL);
431 fp = g_fopen(file, "rb");
434 debug_print("got %s\n", file);
435 must_rename = (fingerprint != NULL);
438 debug_print("got %s first try\n", file);
446 if ((tmp_x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_DER)) != NULL) {
447 cert = ssl_certificate_new(tmp_x509, host, port);
448 debug_print("got cert %p\n", cert);
449 gnutls_x509_crt_deinit(tmp_x509);
456 gchar *old = get_certificate_path(host, buf, NULL);
457 gchar *new = get_certificate_path(host, buf, fingerprint);
458 if (strcmp(old, new))
459 move_file(old, new, TRUE);
468 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
472 size_t cert_size_a = 0, cert_size_b = 0;
475 if (cert_a == NULL || cert_b == NULL)
478 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_a))
479 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
480 g_warning("couldn't gnutls_x509_crt_export to get size a %s", gnutls_strerror(r));
484 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_b))
485 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
486 g_warning("couldn't gnutls_x509_crt_export to get size b %s", gnutls_strerror(r));
490 output_a = g_malloc(cert_size_a);
491 output_b = g_malloc(cert_size_b);
492 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
493 g_warning("couldn't gnutls_x509_crt_export a %s", gnutls_strerror(r));
498 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
499 g_warning("couldn't gnutls_x509_crt_export b %s", gnutls_strerror(r));
504 if (cert_size_a != cert_size_b) {
505 g_warning("size differ %zd %zd", cert_size_a, cert_size_b);
510 if (memcmp(output_a, output_b, cert_size_a)) {
511 g_warning("contents differ");
522 static guint check_cert(SSLCertificate *cert)
524 gnutls_x509_crt_t *ca_list = NULL;
525 gnutls_x509_crt_t *chain = NULL;
526 unsigned int max_ca = 512, max_certs;
527 unsigned int flags = 0;
530 gchar *chain_file = NULL, *buf = NULL;
533 if (claws_ssl_get_cert_file())
534 fp = g_fopen(claws_ssl_get_cert_file(), "r");
541 if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &ca_list, &max_ca)) < 0) {
542 debug_print("CA import failed: %s\n", gnutls_strerror(r));
549 buf = g_strdup_printf("%d", cert->port);
550 chain_file = get_certificate_chain_path(cert->host, buf, cert->fingerprint);
552 if (is_file_exist(chain_file)) {
553 unsigned char md[128];
557 fp = g_fopen(chain_file, "r");
559 debug_print("fopen %s failed: %s\n", chain_file, g_strerror(errno));
563 if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &chain, &max_certs)) < 0) {
564 debug_print("chain import failed: %s\n", gnutls_strerror(r));
573 gnutls_x509_crt_get_fingerprint(chain[0], GNUTLS_DIG_MD5, md, &n);
574 fingerprint = readable_fingerprint(md, n);
575 if (!fingerprint || strcmp(fingerprint, cert->fingerprint)) {
576 debug_print("saved chain fingerprint does not match current : %s / %s\n",
577 cert->fingerprint, fingerprint);
583 r = gnutls_x509_crt_list_verify (chain,
587 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
590 debug_print("chain check failed: %s\n", gnutls_strerror(r));
592 for (i = 0; i < max_certs; i++)
593 gnutls_x509_crt_deinit(chain[i]);
597 r = gnutls_x509_crt_verify(cert->x509_cert, ca_list, max_ca, flags, &status);
599 debug_print("cert check failed: %s\n", gnutls_strerror(r));
602 for (i = 0; i < max_ca; i++)
603 gnutls_x509_crt_deinit(ca_list[i]);
613 static gboolean ssl_certificate_is_valid(SSLCertificate *cert, guint status)
615 gchar *str_status = ssl_certificate_check_signer(cert, status);
617 if (str_status != NULL) {
621 return ssl_certificate_check_subject_cn(cert);
624 char *ssl_certificate_check_signer (SSLCertificate *cert, guint status)
626 gnutls_x509_crt_t x509_cert = cert ? cert->x509_cert : NULL;
629 return g_strdup(_("Internal error"));
631 if (status == (guint)-1) {
632 status = check_cert(cert);
634 return g_strdup(_("Uncheckable"));
636 if (status & GNUTLS_CERT_INVALID) {
637 if (gnutls_x509_crt_check_issuer(x509_cert, x509_cert))
638 return g_strdup(_("Self-signed certificate"));
640 if (status & GNUTLS_CERT_REVOKED)
641 return g_strdup(_("Revoked certificate"));
642 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
643 return g_strdup(_("No certificate issuer found"));
644 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
645 return g_strdup(_("Certificate issuer is not a CA"));
651 static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const gchar *host, gushort port)
657 for (i = 0; i < len; i++) {
659 unsigned char md[128];
660 gnutls_x509_crt_t cert = certs[i];
664 gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, md, &n);
665 gchar *fingerprint = readable_fingerprint(md, n);
666 gchar *buf = g_strdup_printf("%d", port);
668 file = get_certificate_chain_path(host, buf, fingerprint);
673 fp = g_fopen(file, "wb");
676 debug_print("Can't save certificate !\n");
682 gnutls_export_X509_fp(fp, cert, GNUTLS_X509_FMT_PEM);
689 gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status,
690 const gchar *host, gushort port,
691 gboolean accept_if_valid)
693 SSLCertificate *current_cert = NULL;
694 SSLCertificate *known_cert;
695 SSLCertHookData cert_hook_data;
698 unsigned char md[128];
699 gboolean valid = FALSE;
701 current_cert = ssl_certificate_new(x509_cert, host, port);
703 if (current_cert == NULL) {
704 debug_print("Buggy certificate !\n");
708 current_cert->status = status;
711 gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
712 fingerprint = readable_fingerprint(md, n);
714 known_cert = ssl_certificate_find(host, port, fingerprint);
719 valid = ssl_certificate_is_valid(current_cert, status);
721 valid = FALSE; /* Force check */
723 if (known_cert == NULL) {
725 ssl_certificate_save(current_cert);
726 ssl_certificate_destroy(current_cert);
730 cert_hook_data.cert = current_cert;
731 cert_hook_data.old_cert = NULL;
732 cert_hook_data.expired = FALSE;
733 cert_hook_data.accept = FALSE;
735 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
737 if (!cert_hook_data.accept) {
738 ssl_certificate_destroy(current_cert);
741 ssl_certificate_save(current_cert);
742 ssl_certificate_destroy(current_cert);
745 } else if (!ssl_certificate_compare (current_cert, known_cert)) {
747 ssl_certificate_save(current_cert);
748 ssl_certificate_destroy(current_cert);
749 ssl_certificate_destroy(known_cert);
753 cert_hook_data.cert = current_cert;
754 cert_hook_data.old_cert = known_cert;
755 cert_hook_data.expired = FALSE;
756 cert_hook_data.accept = FALSE;
758 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
760 if (!cert_hook_data.accept) {
761 ssl_certificate_destroy(current_cert);
762 ssl_certificate_destroy(known_cert);
765 ssl_certificate_save(current_cert);
766 ssl_certificate_destroy(current_cert);
767 ssl_certificate_destroy(known_cert);
770 } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
771 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
773 if (warned_expired == NULL)
774 warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
776 if (g_hash_table_lookup(warned_expired, tmp)) {
778 ssl_certificate_destroy(current_cert);
779 ssl_certificate_destroy(known_cert);
783 cert_hook_data.cert = current_cert;
784 cert_hook_data.old_cert = NULL;
785 cert_hook_data.expired = TRUE;
786 cert_hook_data.accept = FALSE;
788 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
790 if (!cert_hook_data.accept) {
792 ssl_certificate_destroy(current_cert);
793 ssl_certificate_destroy(known_cert);
796 g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
797 ssl_certificate_destroy(current_cert);
798 ssl_certificate_destroy(known_cert);
803 ssl_certificate_destroy(current_cert);
804 ssl_certificate_destroy(known_cert);
808 gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len,
809 const gchar *host, gushort port,
810 gboolean accept_if_valid)
813 gnutls_x509_crt_t *cas = NULL;
814 gboolean result = FALSE;
818 if (claws_ssl_get_cert_file()) {
819 FILE *fp = g_fopen(claws_ssl_get_cert_file(), "rb");
823 r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &cas, &ncas);
828 g_warning("Can't read SSL_CERT_FILE '%s': %s",
829 claws_ssl_get_cert_file(),
832 debug_print("Can't find SSL ca-certificates file\n");
836 gnutls_x509_crt_list_verify (certs,
840 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
843 result = ssl_certificate_check(certs[0], status, host, port,
846 if (result == TRUE) {
847 ssl_certificate_save_chain(certs, chain_len, host, port);
850 for (i = 0; i < ncas; i++)
851 gnutls_x509_crt_deinit(cas[i]);
857 gnutls_x509_crt_t ssl_certificate_get_x509_from_pem_file(const gchar *file)
859 gnutls_x509_crt_t x509 = NULL;
863 if (is_file_exist(file)) {
864 FILE *fp = g_fopen(file, "r");
866 x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_PEM);
870 log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
871 file, g_strerror(errno));
874 log_error(LOG_PROTOCOL, _("Certificate file %s missing (%s)\n"),
875 file, g_strerror(errno));
880 gnutls_x509_privkey_t ssl_certificate_get_pkey_from_pem_file(const gchar *file)
882 gnutls_x509_privkey_t key = NULL;
886 if (is_file_exist(file)) {
887 FILE *fp = g_fopen(file, "r");
889 key = gnutls_import_key_fp(fp, GNUTLS_X509_FMT_PEM);
893 log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
894 file, g_strerror(errno));
897 log_error(LOG_PROTOCOL, _("Key file %s missing (%s)\n"), file,
903 /* From GnuTLS lib/gnutls_x509.c */
905 parse_pkcs12 (gnutls_pkcs12_t p12,
906 const char *password,
907 gnutls_x509_privkey_t * key,
908 gnutls_x509_crt_t * cert)
910 gnutls_pkcs12_bag_t bag = NULL;
919 ret = gnutls_pkcs12_bag_init (&bag);
926 ret = gnutls_pkcs12_get_bag (p12, index, bag);
927 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
934 ret = gnutls_pkcs12_bag_get_type (bag, 0);
940 if (ret == GNUTLS_BAG_ENCRYPTED)
942 ret = gnutls_pkcs12_bag_decrypt (bag, password);
949 elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
950 if (elements_in_bag < 0)
955 for (i = 0; i < elements_in_bag; i++)
960 type = gnutls_pkcs12_bag_get_type (bag, i);
966 ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
974 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
975 case GNUTLS_BAG_PKCS8_KEY:
976 ret = gnutls_x509_privkey_init (key);
982 ret = gnutls_x509_privkey_import_pkcs8
983 (*key, &data, GNUTLS_X509_FMT_DER, password,
984 type == GNUTLS_BAG_PKCS8_KEY ? GNUTLS_PKCS_PLAIN : 0);
991 case GNUTLS_BAG_CERTIFICATE:
992 ret = gnutls_x509_crt_init (cert);
999 gnutls_x509_crt_import (*cert, &data, GNUTLS_X509_FMT_DER);
1006 case GNUTLS_BAG_ENCRYPTED:
1007 /* XXX Bother to recurse one level down? Unlikely to
1008 use the same password anyway. */
1009 case GNUTLS_BAG_EMPTY:
1016 gnutls_pkcs12_bag_deinit (bag);
1023 gnutls_pkcs12_bag_deinit (bag);
1027 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
1028 gnutls_x509_crt_t *x509, gnutls_x509_privkey_t *pkey)
1030 gnutls_pkcs12_t p12 = NULL;
1039 if (is_file_exist(file)) {
1040 FILE *fp = g_fopen(file, "r");
1042 p12 = gnutls_import_PKCS12_fp(fp, GNUTLS_X509_FMT_DER);
1045 log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
1048 log_error(LOG_PROTOCOL, _("Cannot open P12 certificate file %s (%s)\n"),
1049 file, g_strerror(errno));
1052 log_error(LOG_PROTOCOL, _("P12 Certificate file %s missing (%s)\n"), file,
1056 if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
1057 debug_print("got p12\n");
1059 log_error(LOG_PROTOCOL, "%s\n", gnutls_strerror(r));
1061 gnutls_pkcs12_deinit(p12);
1065 gboolean ssl_certificate_check_subject_cn(SSLCertificate *cert)
1067 return gnutls_x509_crt_check_hostname(cert->x509_cert, cert->host) != 0;
1070 gchar *ssl_certificate_get_subject_cn(SSLCertificate *cert)
1072 gchar subject_cn[BUFFSIZE];
1073 size_t n = BUFFSIZE;
1075 if(gnutls_x509_crt_get_dn_by_oid(cert->x509_cert,
1076 GNUTLS_OID_X520_COMMON_NAME, 0, 0, subject_cn, &n))
1077 return g_strdup(_("<not in certificate>"));
1079 return g_strdup(subject_cn);
1082 #endif /* USE_GNUTLS */