2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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/>.
25 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
27 #include <openssl/ssl.h>
29 #include <gnutls/gnutls.h>
30 #include <gnutls/x509.h>
31 #include <gnutls/pkcs12.h>
36 #include <sys/types.h>
39 #include <glib/gi18n.h>
41 # include <winsock2.h>
43 # include <sys/socket.h>
44 # include <netinet/in.h>
46 #endif /* G_OS_WIN32 */
47 #include "ssl_certificate.h"
54 static GHashTable *warned_expired = NULL;
56 gboolean prefs_common_unsafe_ssl_certs(void);
58 static gchar *get_certificate_path(const gchar *host, const gchar *port, const gchar *fp)
60 if (fp != NULL && prefs_common_unsafe_ssl_certs())
61 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
62 "certs", G_DIR_SEPARATOR_S,
63 host, ".", port, ".", fp, ".cert", NULL);
65 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
66 "certs", G_DIR_SEPARATOR_S,
67 host, ".", port, ".cert", NULL);
71 static SSLCertificate *ssl_certificate_new_lookup(X509 *x509_cert, gchar *host, gushort port, gboolean lookup);
73 static SSLCertificate *ssl_certificate_new_lookup(gnutls_x509_crt x509_cert, gchar *host, gushort port, gboolean lookup);
77 time_t asn1toTime(ASN1_TIME *asn1Time)
82 if (asn1Time == NULL || asn1Time->length < 13)
85 memset(&tm, 0, sizeof(tm));
87 #define N2(n) ((asn1Time->data[n]-'0')*10 + asn1Time->data[(n)+1]-'0')
89 #define CPY(f,n) (tm.f=N2(n))
94 tm.tm_year += 100; /* Sux */
105 if (asn1Time->data[12] != 'Z')
107 if (asn1Time->length < 17)
110 offset=N2(13)*3600+N2(15)*60;
112 if (asn1Time->data[12] == '-')
119 return mktime(&tm)-offset;
123 static char * get_fqdn(char *host)
127 struct addrinfo hints, *res;
132 if (host == NULL || strlen(host) == 0)
135 memset(&hints, 0, sizeof(hints));
136 hints.ai_flags = AI_CANONNAME;
137 hints.ai_family = AF_UNSPEC;
138 hints.ai_socktype = SOCK_STREAM;
139 hints.ai_protocol = IPPROTO_TCP;
141 gai_err = getaddrinfo(host, NULL, &hints, &res);
143 g_warning("getaddrinfo for %s failed: %s\n",
144 host, gai_strerror(gai_err));
145 return g_strdup(host);
148 if (res->ai_canonname && strlen(res->ai_canonname)) {
149 gchar *fqdn = g_strdup(res->ai_canonname);
154 return g_strdup(host);
157 return g_strdup(host);
160 hp = my_gethostbyname(host);
162 return g_strdup(host); /*caller should free*/
164 return g_strdup(hp->h_name);
168 char * readable_fingerprint(unsigned char *src, int len)
179 tmp2 = g_strdup_printf("%s:%02X", ret, src[i]);
181 tmp2 = g_strdup_printf("%02X", src[i]);
183 ret = g_strdup(tmp2);
191 static gnutls_x509_crt x509_crt_copy(gnutls_x509_crt src)
196 gnutls_x509_crt dest;
198 if (gnutls_x509_crt_init(&dest) != 0) {
199 g_warning("couldn't gnutls_x509_crt_init\n");
203 if (gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, NULL, &size)
204 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
205 g_warning("couldn't gnutls_x509_crt_export to get size\n");
206 gnutls_x509_crt_deinit(dest);
210 tmp.data = malloc(size);
211 memset(tmp.data, 0, size);
212 ret = gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, tmp.data, &size);
215 ret = gnutls_x509_crt_import(dest, &tmp, GNUTLS_X509_FMT_DER);
217 g_warning("couldn't gnutls_x509_crt_import for real (%d %s)\n", ret, gnutls_strerror(ret));
218 gnutls_x509_crt_deinit(dest);
222 g_warning("couldn't gnutls_x509_crt_export for real (%d %s)\n", ret, gnutls_strerror(ret));
223 gnutls_x509_crt_deinit(dest);
233 static SSLCertificate *ssl_certificate_new_lookup(X509 *x509_cert, gchar *host, gushort port, gboolean lookup)
235 static SSLCertificate *ssl_certificate_new_lookup(gnutls_x509_crt x509_cert, gchar *host, gushort port, gboolean lookup)
238 SSLCertificate *cert = g_new0(SSLCertificate, 1);
244 unsigned char md[128];
246 if (host == NULL || x509_cert == NULL) {
247 ssl_certificate_destroy(cert);
251 cert->x509_cert = X509_dup(x509_cert);
253 cert->x509_cert = x509_crt_copy(x509_cert);
254 cert->status = (guint)-1;
257 cert->host = get_fqdn(host);
259 cert->host = g_strdup(host);
264 X509_digest(cert->x509_cert, EVP_md5(), md, &n);
265 cert->fingerprint = readable_fingerprint(md, (int)n);
268 gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_MD5, md, &n);
269 cert->fingerprint = readable_fingerprint(md, (int)n);
275 static void gnutls_i2d_X509_fp(FILE *fp, gnutls_x509_crt x509_cert)
277 char output[10*1024];
278 size_t cert_size = 10*1024;
281 if ((r = gnutls_x509_crt_export(x509_cert, GNUTLS_X509_FMT_DER, output, &cert_size)) < 0) {
282 g_warning("couldn't export cert %s (%zd)\n", gnutls_strerror(r), cert_size);
285 debug_print("writing %zd bytes\n",cert_size);
286 if (fwrite(&output, 1, cert_size, fp) < cert_size) {
287 g_warning("failed to write cert\n");
291 size_t gnutls_i2d_X509(gnutls_x509_crt x509_cert, unsigned char **output)
293 size_t cert_size = 10*1024;
299 *output = malloc(cert_size);
301 if ((r = gnutls_x509_crt_export(x509_cert, GNUTLS_X509_FMT_DER, *output, &cert_size)) < 0) {
302 g_warning("couldn't export cert %s (%zd)\n", gnutls_strerror(r), cert_size);
310 size_t gnutls_i2d_PrivateKey(gnutls_x509_privkey pkey, unsigned char **output)
312 size_t key_size = 10*1024;
318 *output = malloc(key_size);
320 if ((r = gnutls_x509_privkey_export(pkey, GNUTLS_X509_FMT_DER, *output, &key_size)) < 0) {
321 g_warning("couldn't export key %s (%zd)\n", gnutls_strerror(r), key_size);
329 static gnutls_x509_crt gnutls_d2i_X509_fp(FILE *fp, int format)
331 gnutls_x509_crt cert = NULL;
335 if (fstat(fileno(fp), &s) < 0) {
339 tmp.data = malloc(s.st_size);
340 memset(tmp.data, 0, s.st_size);
341 tmp.size = s.st_size;
342 if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
348 gnutls_x509_crt_init(&cert);
349 if ((r = gnutls_x509_crt_import(cert, &tmp, (format == 0)?GNUTLS_X509_FMT_DER:GNUTLS_X509_FMT_PEM)) < 0) {
350 debug_print("cert import failed: %s\n", gnutls_strerror(r));
351 gnutls_x509_crt_deinit(cert);
355 debug_print("got cert! %p\n", cert);
359 static gnutls_x509_privkey gnutls_d2i_key_fp(FILE *fp, int format)
361 gnutls_x509_privkey key = NULL;
365 if (fstat(fileno(fp), &s) < 0) {
369 tmp.data = malloc(s.st_size);
370 memset(tmp.data, 0, s.st_size);
371 tmp.size = s.st_size;
372 if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
378 gnutls_x509_privkey_init(&key);
379 if ((r = gnutls_x509_privkey_import(key, &tmp, (format == 0)?GNUTLS_X509_FMT_DER:GNUTLS_X509_FMT_PEM)) < 0) {
380 debug_print("key import failed: %s\n", gnutls_strerror(r));
381 gnutls_x509_privkey_deinit(key);
385 debug_print("got key! %p\n", key);
389 static gnutls_pkcs12_t gnutls_d2i_PKCS12_fp(FILE *fp, int format)
391 gnutls_pkcs12_t p12 = NULL;
395 if (fstat(fileno(fp), &s) < 0) {
399 tmp.data = malloc(s.st_size);
400 memset(tmp.data, 0, s.st_size);
401 tmp.size = s.st_size;
402 if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
408 gnutls_pkcs12_init(&p12);
410 if ((r = gnutls_pkcs12_import(p12, &tmp, (format == 0)?GNUTLS_X509_FMT_DER:GNUTLS_X509_FMT_PEM,0)) < 0) {
411 g_warning("p12 import failed: %s\n", gnutls_strerror(r));
412 gnutls_pkcs12_deinit(p12);
416 debug_print("got p12! %p\n", p12);
422 static void ssl_certificate_save (SSLCertificate *cert)
427 file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
428 "certs", G_DIR_SEPARATOR_S, NULL);
430 if (!is_dir_exist(file))
434 port = g_strdup_printf("%d", cert->port);
435 file = get_certificate_path(cert->host, port, cert->fingerprint);
438 fp = g_fopen(file, "wb");
441 debug_print("Can't save certificate !\n");
445 gnutls_i2d_X509_fp(fp, cert->x509_cert);
447 i2d_X509_fp(fp, cert->x509_cert);
454 void ssl_certificate_destroy(SSLCertificate *cert)
461 X509_free(cert->x509_cert);
463 gnutls_x509_crt_deinit(cert->x509_cert);
466 g_free(cert->fingerprint);
471 void ssl_certificate_delete_from_disk(SSLCertificate *cert)
475 buf = g_strdup_printf("%d", cert->port);
476 file = get_certificate_path(cert->host, buf, cert->fingerprint);
482 SSLCertificate *ssl_certificate_find (gchar *host, gushort port, const gchar *fingerprint)
484 return ssl_certificate_find_lookup (host, port, fingerprint, TRUE);
487 SSLCertificate *ssl_certificate_find_lookup (gchar *host, gushort port, const gchar *fingerprint, gboolean lookup)
492 SSLCertificate *cert = NULL;
496 gnutls_x509_crt tmp_x509;
499 gboolean must_rename = FALSE;
502 fqdn_host = get_fqdn(host);
504 fqdn_host = g_strdup(host);
506 buf = g_strdup_printf("%d", port);
508 if (fingerprint != NULL) {
509 file = get_certificate_path(fqdn_host, buf, fingerprint);
510 fp = g_fopen(file, "rb");
513 /* see if we have the old one */
514 debug_print("didn't get %s\n", file);
516 file = get_certificate_path(fqdn_host, buf, NULL);
517 fp = g_fopen(file, "rb");
520 debug_print("got %s\n", file);
521 must_rename = (fingerprint != NULL);
524 debug_print("got %s first try\n", file);
534 if ((tmp_x509 = d2i_X509_fp(fp, 0)) != NULL) {
536 if ((tmp_x509 = gnutls_d2i_X509_fp(fp, 0)) != NULL) {
538 cert = ssl_certificate_new_lookup(tmp_x509, fqdn_host, port, lookup);
539 debug_print("got cert %p\n", cert);
543 gnutls_x509_crt_deinit(tmp_x509);
550 gchar *old = get_certificate_path(fqdn_host, buf, NULL);
551 gchar *new = get_certificate_path(fqdn_host, buf, fingerprint);
552 if (strcmp(old, new))
553 move_file(old, new, TRUE);
563 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
566 if (cert_a == NULL || cert_b == NULL)
568 else if (!X509_cmp(cert_a->x509_cert, cert_b->x509_cert))
575 size_t cert_size_a = 0, cert_size_b = 0;
578 if (cert_a == NULL || cert_b == NULL)
581 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_a))
582 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
583 g_warning("couldn't gnutls_x509_crt_export to get size a %s\n", gnutls_strerror(r));
587 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_b))
588 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
589 g_warning("couldn't gnutls_x509_crt_export to get size b %s\n", gnutls_strerror(r));
593 output_a = malloc(cert_size_a);
594 output_b = malloc(cert_size_b);
595 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
596 g_warning("couldn't gnutls_x509_crt_export a %s\n", gnutls_strerror(r));
601 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
602 g_warning("couldn't gnutls_x509_crt_export b %s\n", gnutls_strerror(r));
607 if (cert_size_a != cert_size_b) {
608 g_warning("size differ %zd %zd\n", cert_size_a, cert_size_b);
613 if (memcmp(output_a, output_b, cert_size_a)) {
614 g_warning("contents differ\n");
627 char *ssl_certificate_check_signer (X509 *cert)
629 X509_STORE_CTX store_ctx;
630 X509_STORE *store = SSL_CTX_get_cert_store(ssl_get_ctx());
631 char *err_msg = NULL;
634 g_print("Can't create X509_STORE\n");
638 X509_STORE_CTX_init (&store_ctx, store, cert, NULL);
640 if(!X509_verify_cert (&store_ctx)) {
641 err_msg = g_strdup(X509_verify_cert_error_string(
642 X509_STORE_CTX_get_error(&store_ctx)));
643 debug_print("Can't check signer: %s\n", err_msg);
644 X509_STORE_CTX_cleanup (&store_ctx);
648 X509_STORE_CTX_cleanup (&store_ctx);
652 static guint check_cert(gnutls_x509_crt cert)
654 gnutls_x509_crt *ca_list;
655 unsigned int max = 512;
656 unsigned int flags = 0;
663 if (claws_ssl_get_cert_file())
664 fp = fopen(claws_ssl_get_cert_file(), "r");
668 if (fstat(fileno(fp), &s) < 0) {
674 ca_list=(gnutls_x509_crt_t*)malloc(max*sizeof(gnutls_x509_crt_t));
675 tmp.data = malloc(s.st_size);
676 memset(tmp.data, 0, s.st_size);
677 tmp.size = s.st_size;
678 if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
686 if ((r = gnutls_x509_crt_list_import(ca_list, &max,
687 &tmp, GNUTLS_X509_FMT_PEM, flags)) < 0) {
688 debug_print("cert import failed: %s\n", gnutls_strerror(r));
695 debug_print("got %d certs in ca_list! %p\n", max, &ca_list);
696 r = gnutls_x509_crt_verify(cert, ca_list, max, flags, &status);
699 for (i = 0; i < max; i++)
700 gnutls_x509_crt_deinit(ca_list[i]);
710 char *ssl_certificate_check_signer (gnutls_x509_crt cert, guint status)
712 if (status == (guint)-1) {
713 status = check_cert(cert);
715 return g_strdup(_("Uncheckable"));
717 if (status & GNUTLS_CERT_INVALID) {
718 if (gnutls_x509_crt_check_issuer(cert, cert))
719 return g_strdup(_("Self-signed certificate"));
721 if (status & GNUTLS_CERT_REVOKED)
722 return g_strdup(_("Revoked certificate"));
723 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
724 return g_strdup(_("No certificate issuer found"));
725 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
726 return g_strdup(_("Certificate issuer is not a CA"));
734 gboolean ssl_certificate_check (X509 *x509_cert, gchar *fqdn, gchar *host, gushort port)
736 gboolean ssl_certificate_check (gnutls_x509_crt x509_cert, guint status, gchar *fqdn, gchar *host, gushort port)
739 SSLCertificate *current_cert = NULL;
740 SSLCertificate *known_cert;
741 SSLCertHookData cert_hook_data;
742 gchar *fqdn_host = NULL;
749 unsigned char md[128];
752 fqdn_host = g_strdup(fqdn);
754 fqdn_host = get_fqdn(host);
756 g_warning("no host!\n");
760 current_cert = ssl_certificate_new_lookup(x509_cert, fqdn_host, port, FALSE);
762 if (current_cert == NULL) {
763 debug_print("Buggy certificate !\n");
769 current_cert->status = status;
773 X509_digest(x509_cert, EVP_md5(), md, &n);
774 fingerprint = readable_fingerprint(md, (int)n);
777 gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
778 fingerprint = readable_fingerprint(md, n);
781 known_cert = ssl_certificate_find_lookup (fqdn_host, port, fingerprint, FALSE);
786 if (known_cert == NULL) {
787 cert_hook_data.cert = current_cert;
788 cert_hook_data.old_cert = NULL;
789 cert_hook_data.expired = FALSE;
790 cert_hook_data.accept = FALSE;
792 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
794 if (!cert_hook_data.accept) {
795 ssl_certificate_destroy(current_cert);
798 ssl_certificate_save(current_cert);
799 ssl_certificate_destroy(current_cert);
802 } else if (!ssl_certificate_compare (current_cert, known_cert)) {
803 cert_hook_data.cert = current_cert;
804 cert_hook_data.old_cert = known_cert;
805 cert_hook_data.expired = FALSE;
806 cert_hook_data.accept = FALSE;
808 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
810 if (!cert_hook_data.accept) {
811 ssl_certificate_destroy(current_cert);
812 ssl_certificate_destroy(known_cert);
815 ssl_certificate_save(current_cert);
816 ssl_certificate_destroy(current_cert);
817 ssl_certificate_destroy(known_cert);
821 } else if (asn1toTime(X509_get_notAfter(current_cert->x509_cert)) < time(NULL)) {
823 } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
825 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
827 if (warned_expired == NULL)
828 warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
830 if (g_hash_table_lookup(warned_expired, tmp)) {
832 ssl_certificate_destroy(current_cert);
833 ssl_certificate_destroy(known_cert);
837 cert_hook_data.cert = current_cert;
838 cert_hook_data.old_cert = NULL;
839 cert_hook_data.expired = TRUE;
840 cert_hook_data.accept = FALSE;
842 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
844 if (!cert_hook_data.accept) {
846 ssl_certificate_destroy(current_cert);
847 ssl_certificate_destroy(known_cert);
850 g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
851 ssl_certificate_destroy(current_cert);
852 ssl_certificate_destroy(known_cert);
857 ssl_certificate_destroy(current_cert);
858 ssl_certificate_destroy(known_cert);
863 X509 *ssl_certificate_get_x509_from_pem_file(const gchar *file)
868 if (is_file_exist(file)) {
869 FILE *fp = g_fopen(file, "r");
871 x509 = PEM_read_X509(fp, NULL, NULL, NULL);
876 log_error(LOG_PROTOCOL, "Can not open certificate file %s\n", file);
881 static int ssl_pkey_password_cb(char *buf, int max_len, int flag, void *pwd)
886 EVP_PKEY *ssl_certificate_get_pkey_from_pem_file(const gchar *file)
888 EVP_PKEY *pkey = NULL;
891 if (is_file_exist(file)) {
892 FILE *fp = g_fopen(file, "r");
894 pkey = PEM_read_PrivateKey(fp, NULL, ssl_pkey_password_cb, NULL);
899 log_error(LOG_PROTOCOL, "Can not open private key file %s\n", file);
904 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
905 X509 **x509, EVP_PKEY **pkey)
914 if (is_file_exist(file)) {
915 FILE *fp = g_fopen(file, "r");
917 p12 = d2i_PKCS12_fp(fp, NULL);
921 log_error(LOG_PROTOCOL, "Can not open certificate file %s\n", file);
924 if (PKCS12_parse(p12, password, pkey, x509, NULL) == 1) {
925 /* we got the correct password */
928 hooks_invoke(SSL_CERT_GET_PASSWORD, &tmp);
929 if (PKCS12_parse(p12, tmp, pkey, x509, NULL) == 1) {
930 debug_print("got p12\n");
932 log_error(LOG_PROTOCOL, "%s\n", ERR_error_string(ERR_get_error(),NULL));
941 gnutls_x509_crt ssl_certificate_get_x509_from_pem_file(const gchar *file)
943 gnutls_x509_crt x509 = NULL;
947 if (is_file_exist(file)) {
948 FILE *fp = g_fopen(file, "r");
950 x509 = gnutls_d2i_X509_fp(fp, 1);
955 log_error(LOG_PROTOCOL, "Can not open certificate file %s\n", file);
960 gnutls_x509_privkey ssl_certificate_get_pkey_from_pem_file(const gchar *file)
962 gnutls_x509_privkey key = NULL;
966 if (is_file_exist(file)) {
967 FILE *fp = g_fopen(file, "r");
969 key = gnutls_d2i_key_fp(fp, 1);
974 log_error(LOG_PROTOCOL, "Can not open key file %s\n", file);
979 /* From GnuTLS lib/gnutls_x509.c */
981 parse_pkcs12 (gnutls_pkcs12_t p12,
982 const char *password,
983 gnutls_x509_privkey * key,
984 gnutls_x509_crt_t * cert)
986 gnutls_pkcs12_bag bag = NULL;
995 ret = gnutls_pkcs12_bag_init (&bag);
1002 ret = gnutls_pkcs12_get_bag (p12, index, bag);
1003 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
1010 ret = gnutls_pkcs12_bag_get_type (bag, 0);
1016 if (ret == GNUTLS_BAG_ENCRYPTED)
1018 ret = gnutls_pkcs12_bag_decrypt (bag, password);
1025 elements_in_bag = gnutls_pkcs12_bag_get_count (bag);
1026 if (elements_in_bag < 0)
1031 for (i = 0; i < elements_in_bag; i++)
1036 type = gnutls_pkcs12_bag_get_type (bag, i);
1042 ret = gnutls_pkcs12_bag_get_data (bag, i, &data);
1050 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
1051 case GNUTLS_BAG_PKCS8_KEY:
1052 ret = gnutls_x509_privkey_init (key);
1058 ret = gnutls_x509_privkey_import_pkcs8
1059 (*key, &data, GNUTLS_X509_FMT_DER, password,
1060 type == GNUTLS_BAG_PKCS8_KEY ? GNUTLS_PKCS_PLAIN : 0);
1067 case GNUTLS_BAG_CERTIFICATE:
1068 ret = gnutls_x509_crt_init (cert);
1075 gnutls_x509_crt_import (*cert, &data, GNUTLS_X509_FMT_DER);
1082 case GNUTLS_BAG_ENCRYPTED:
1083 /* XXX Bother to recurse one level down? Unlikely to
1084 use the same password anyway. */
1085 case GNUTLS_BAG_EMPTY:
1092 gnutls_pkcs12_bag_deinit (bag);
1099 gnutls_pkcs12_bag_deinit (bag);
1103 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
1104 gnutls_x509_crt *x509, gnutls_x509_privkey *pkey)
1106 gnutls_pkcs12_t p12 = NULL;
1115 if (is_file_exist(file)) {
1116 FILE *fp = g_fopen(file, "r");
1118 p12 = gnutls_d2i_PKCS12_fp(fp, 0);
1122 log_error(LOG_PROTOCOL, "Can not open certificate file %s\n", file);
1125 if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
1126 debug_print("got p12\n");
1128 log_error(LOG_PROTOCOL, "%s\n", gnutls_strerror(r));
1130 gnutls_pkcs12_deinit(p12);
1134 #endif /* USE_OPENSSL */