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
);
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;
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
);
207 size_t gnutls_i2d_PrivateKey(gnutls_x509_privkey_t pkey
, unsigned char **output
)
209 size_t key_size
= 10*1024;
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
);
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;
242 if (fstat(fileno(fp
), &s
) < 0) {
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");
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
));
266 debug_print("got %d certs in crt_list! %p\n", max
, &crt_list
);
268 *cert_list
= crt_list
;
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
;
281 if ((r
= gnutls_import_X509_list_fp(fp
, format
, &certs
, &ncerts
)) < 0) {
288 for (i
= 1; i
< ncerts
; i
++)
289 gnutls_x509_crt_deinit(certs
[i
]);
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
;
303 if (fstat(fileno(fp
), &s
) < 0) {
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");
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
);
323 debug_print("got key! %p\n", key
);
327 static gnutls_pkcs12_t
gnutls_import_PKCS12_fp(FILE *fp
, gnutls_x509_crt_fmt_t format
)
329 gnutls_pkcs12_t p12
= NULL
;
333 if (fstat(fileno(fp
), &s
) < 0) {
334 log_error(LOG_PROTOCOL
, _("Cannot stat P12 certificate file (%s)\n"),
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"),
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"),
353 gnutls_pkcs12_deinit(p12
);
357 debug_print("got p12! %p\n", p12
);
361 static void ssl_certificate_save (SSLCertificate
*cert
)
366 file
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
367 "certs", G_DIR_SEPARATOR_S
, NULL
);
369 if (!is_dir_exist(file
))
373 port
= g_strdup_printf("%d", cert
->port
);
374 file
= get_certificate_path(cert
->host
, port
, cert
->fingerprint
);
377 fp
= claws_fopen(file
, "wb");
380 debug_print("Can't save certificate !\n");
384 gnutls_export_X509_fp(fp
, cert
->x509_cert
, GNUTLS_X509_FMT_DER
);
387 claws_safe_fclose(fp
);
391 void ssl_certificate_destroy(SSLCertificate
*cert
)
397 gnutls_x509_crt_deinit(cert
->x509_cert
);
399 g_free(cert
->fingerprint
);
404 void ssl_certificate_delete_from_disk(SSLCertificate
*cert
)
408 buf
= g_strdup_printf("%d", cert
->port
);
409 file
= get_certificate_path(cert
->host
, buf
, cert
->fingerprint
);
412 file
= get_certificate_chain_path(cert
->host
, buf
, cert
->fingerprint
);
418 SSLCertificate
*ssl_certificate_find (const gchar
*host
, gushort port
, const gchar
*fingerprint
)
422 SSLCertificate
*cert
= NULL
;
423 gnutls_x509_crt_t tmp_x509
;
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");
434 /* see if we have the old one */
435 debug_print("didn't get %s\n", file
);
437 file
= get_certificate_path(host
, buf
, NULL
);
438 fp
= claws_fopen(file
, "rb");
441 debug_print("got %s\n", file
);
442 must_rename
= (fingerprint
!= NULL
);
445 debug_print("got %s first try\n", file
);
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
);
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
);
475 static gboolean
ssl_certificate_compare (SSLCertificate
*cert_a
, SSLCertificate
*cert_b
)
479 size_t cert_size_a
= 0, cert_size_b
= 0;
482 if (cert_a
== NULL
|| cert_b
== NULL
)
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
));
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
));
497 if (cert_size_a
!= cert_size_b
) {
498 debug_print("sizes differ: %"G_GSIZE_FORMAT
" != %"G_GSIZE_FORMAT
"\n", cert_size_a
, cert_size_b
);
502 output_a
= g_malloc(cert_size_a
);
503 output_b
= g_malloc(cert_size_b
);
504 if ((r
= gnutls_x509_crt_export(cert_a
->x509_cert
, GNUTLS_X509_FMT_DER
, output_a
, &cert_size_a
)) < 0) {
505 g_warning("couldn't gnutls_x509_crt_export a %s", gnutls_strerror(r
));
510 if ((r
= gnutls_x509_crt_export(cert_b
->x509_cert
, GNUTLS_X509_FMT_DER
, output_b
, &cert_size_b
)) < 0) {
511 g_warning("couldn't gnutls_x509_crt_export b %s", gnutls_strerror(r
));
516 if (memcmp(output_a
, output_b
, cert_size_a
)) {
517 debug_print("contents differ\n");
528 static guint
check_cert(SSLCertificate
*cert
)
530 gnutls_x509_crt_t
*ca_list
= NULL
;
531 gnutls_x509_crt_t
*chain
= NULL
;
532 unsigned int max_ca
= 512, max_certs
;
533 unsigned int flags
= 0;
536 gchar
*chain_file
= NULL
, *buf
= NULL
;
539 if (claws_ssl_get_cert_file())
540 fp
= claws_fopen(claws_ssl_get_cert_file(), "r");
547 if ((r
= gnutls_import_X509_list_fp(fp
, GNUTLS_X509_FMT_PEM
, &ca_list
, &max_ca
)) < 0) {
548 debug_print("CA import failed: %s\n", gnutls_strerror(r
));
555 buf
= g_strdup_printf("%d", cert
->port
);
556 chain_file
= get_certificate_chain_path(cert
->host
, buf
, cert
->fingerprint
);
558 if (is_file_exist(chain_file
)) {
559 unsigned char md
[128];
563 fp
= claws_fopen(chain_file
, "r");
565 debug_print("claws_fopen %s failed: %s\n", chain_file
, g_strerror(errno
));
569 if ((r
= gnutls_import_X509_list_fp(fp
, GNUTLS_X509_FMT_PEM
, &chain
, &max_certs
)) < 0) {
570 debug_print("chain import failed: %s\n", gnutls_strerror(r
));
579 gnutls_x509_crt_get_fingerprint(chain
[0], GNUTLS_DIG_MD5
, md
, &n
);
580 fingerprint
= readable_fingerprint(md
, n
);
581 if (!fingerprint
|| strcmp(fingerprint
, cert
->fingerprint
)) {
582 debug_print("saved chain fingerprint does not match current : %s / %s\n",
583 cert
->fingerprint
, fingerprint
);
589 r
= gnutls_x509_crt_list_verify (chain
,
593 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT
,
596 debug_print("chain check failed: %s\n", gnutls_strerror(r
));
598 for (i
= 0; i
< max_certs
; i
++)
599 gnutls_x509_crt_deinit(chain
[i
]);
603 r
= gnutls_x509_crt_verify(cert
->x509_cert
, ca_list
, max_ca
, flags
, &status
);
605 debug_print("cert check failed: %s\n", gnutls_strerror(r
));
608 for (i
= 0; i
< max_ca
; i
++)
609 gnutls_x509_crt_deinit(ca_list
[i
]);
619 static gboolean
ssl_certificate_is_valid(SSLCertificate
*cert
, guint status
)
621 gchar
*str_status
= ssl_certificate_check_signer(cert
, status
);
623 if (str_status
!= NULL
) {
627 return ssl_certificate_check_subject_cn(cert
);
630 char *ssl_certificate_check_signer (SSLCertificate
*cert
, guint status
)
632 gnutls_x509_crt_t x509_cert
= cert
? cert
->x509_cert
: NULL
;
635 return g_strdup(_("Internal error"));
637 if (status
== (guint
)-1) {
638 status
= check_cert(cert
);
640 return g_strdup(_("Uncheckable"));
642 if (status
& GNUTLS_CERT_INVALID
) {
643 if (gnutls_x509_crt_check_issuer(x509_cert
, x509_cert
))
644 return g_strdup(_("Self-signed certificate"));
646 if (status
& GNUTLS_CERT_REVOKED
)
647 return g_strdup(_("Revoked certificate"));
648 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
649 return g_strdup(_("No certificate issuer found"));
650 if (status
& GNUTLS_CERT_SIGNER_NOT_CA
)
651 return g_strdup(_("Certificate issuer is not a CA"));
657 static void ssl_certificate_save_chain(gnutls_x509_crt_t
*certs
, gint len
, const gchar
*host
, gushort port
)
663 for (i
= 0; i
< len
; i
++) {
665 unsigned char md
[128];
666 gnutls_x509_crt_t cert
= certs
[i
];
670 gnutls_x509_crt_get_fingerprint(cert
, GNUTLS_DIG_MD5
, md
, &n
);
671 gchar
*fingerprint
= readable_fingerprint(md
, n
);
672 gchar
*buf
= g_strdup_printf("%d", port
);
674 file
= get_certificate_chain_path(host
, buf
, fingerprint
);
679 fp
= claws_fopen(file
, "wb");
682 debug_print("Can't save certificate !\n");
688 gnutls_export_X509_fp(fp
, cert
, GNUTLS_X509_FMT_PEM
);
692 claws_safe_fclose(fp
);
695 gboolean
ssl_certificate_check (gnutls_x509_crt_t x509_cert
, guint status
,
696 const gchar
*host
, gushort port
,
697 gboolean accept_if_valid
)
699 SSLCertificate
*current_cert
= NULL
;
700 SSLCertificate
*known_cert
;
701 SSLCertHookData cert_hook_data
;
704 unsigned char md
[128];
705 gboolean valid
= FALSE
;
707 current_cert
= ssl_certificate_new(x509_cert
, host
, port
);
709 if (current_cert
== NULL
) {
710 debug_print("Buggy certificate !\n");
714 current_cert
->status
= status
;
717 gnutls_x509_crt_get_fingerprint(x509_cert
, GNUTLS_DIG_MD5
, md
, &n
);
718 fingerprint
= readable_fingerprint(md
, n
);
720 known_cert
= ssl_certificate_find(host
, port
, fingerprint
);
725 valid
= ssl_certificate_is_valid(current_cert
, status
);
727 valid
= FALSE
; /* Force check */
729 if (known_cert
== NULL
) {
731 ssl_certificate_save(current_cert
);
732 ssl_certificate_destroy(current_cert
);
736 cert_hook_data
.cert
= current_cert
;
737 cert_hook_data
.old_cert
= NULL
;
738 cert_hook_data
.expired
= FALSE
;
739 cert_hook_data
.accept
= FALSE
;
741 hooks_invoke(SSLCERT_ASK_HOOKLIST
, &cert_hook_data
);
743 if (!cert_hook_data
.accept
) {
744 ssl_certificate_destroy(current_cert
);
747 ssl_certificate_save(current_cert
);
748 ssl_certificate_destroy(current_cert
);
751 } else if (!ssl_certificate_compare (current_cert
, known_cert
)) {
753 ssl_certificate_save(current_cert
);
754 ssl_certificate_destroy(current_cert
);
755 ssl_certificate_destroy(known_cert
);
759 cert_hook_data
.cert
= current_cert
;
760 cert_hook_data
.old_cert
= known_cert
;
761 cert_hook_data
.expired
= FALSE
;
762 cert_hook_data
.accept
= FALSE
;
764 hooks_invoke(SSLCERT_ASK_HOOKLIST
, &cert_hook_data
);
766 if (!cert_hook_data
.accept
) {
767 ssl_certificate_destroy(current_cert
);
768 ssl_certificate_destroy(known_cert
);
771 ssl_certificate_save(current_cert
);
772 ssl_certificate_destroy(current_cert
);
773 ssl_certificate_destroy(known_cert
);
776 } else if (gnutls_x509_crt_get_expiration_time(current_cert
->x509_cert
) < time(NULL
)) {
777 gchar
*tmp
= g_strdup_printf("%s:%d", current_cert
->host
, current_cert
->port
);
779 if (warned_expired
== NULL
)
780 warned_expired
= g_hash_table_new(g_str_hash
, g_str_equal
);
782 if (g_hash_table_lookup(warned_expired
, tmp
)) {
784 ssl_certificate_destroy(current_cert
);
785 ssl_certificate_destroy(known_cert
);
789 cert_hook_data
.cert
= current_cert
;
790 cert_hook_data
.old_cert
= NULL
;
791 cert_hook_data
.expired
= TRUE
;
792 cert_hook_data
.accept
= FALSE
;
794 hooks_invoke(SSLCERT_ASK_HOOKLIST
, &cert_hook_data
);
796 if (!cert_hook_data
.accept
) {
798 ssl_certificate_destroy(current_cert
);
799 ssl_certificate_destroy(known_cert
);
802 g_hash_table_insert(warned_expired
, tmp
, GINT_TO_POINTER(1));
803 ssl_certificate_destroy(current_cert
);
804 ssl_certificate_destroy(known_cert
);
809 ssl_certificate_destroy(current_cert
);
810 ssl_certificate_destroy(known_cert
);
814 gboolean
ssl_certificate_check_chain(gnutls_x509_crt_t
*certs
, gint chain_len
,
815 const gchar
*host
, gushort port
,
816 gboolean accept_if_valid
)
819 gnutls_x509_crt_t
*cas
= NULL
;
820 gboolean result
= FALSE
;
824 if (claws_ssl_get_cert_file()) {
825 FILE *fp
= claws_fopen(claws_ssl_get_cert_file(), "rb");
829 r
= gnutls_import_X509_list_fp(fp
, GNUTLS_X509_FMT_PEM
, &cas
, &ncas
);
834 g_warning("can't read SSL_CERT_FILE '%s': %s",
835 claws_ssl_get_cert_file(),
838 debug_print("Can't find SSL ca-certificates file\n");
842 gnutls_x509_crt_list_verify (certs
,
846 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT
,
849 result
= ssl_certificate_check(certs
[0], status
, host
, port
,
852 if (result
== TRUE
) {
853 ssl_certificate_save_chain(certs
, chain_len
, host
, port
);
856 for (i
= 0; i
< ncas
; i
++)
857 gnutls_x509_crt_deinit(cas
[i
]);
863 gnutls_x509_crt_t
ssl_certificate_get_x509_from_pem_file(const gchar
*file
)
865 gnutls_x509_crt_t x509
= NULL
;
869 if (is_file_exist(file
)) {
870 FILE *fp
= claws_fopen(file
, "r");
872 x509
= gnutls_import_X509_fp(fp
, GNUTLS_X509_FMT_PEM
);
876 log_error(LOG_PROTOCOL
, _("Cannot open certificate file %s: %s\n"),
877 file
, g_strerror(errno
));
880 log_error(LOG_PROTOCOL
, _("Certificate file %s missing (%s)\n"),
881 file
, g_strerror(errno
));
886 gnutls_x509_privkey_t
ssl_certificate_get_pkey_from_pem_file(const gchar
*file
)
888 gnutls_x509_privkey_t key
= NULL
;
892 if (is_file_exist(file
)) {
893 FILE *fp
= claws_fopen(file
, "r");
895 key
= gnutls_import_key_fp(fp
, GNUTLS_X509_FMT_PEM
);
899 log_error(LOG_PROTOCOL
, _("Cannot open key file %s (%s)\n"),
900 file
, g_strerror(errno
));
903 log_error(LOG_PROTOCOL
, _("Key file %s missing (%s)\n"), file
,
909 /* From GnuTLS lib/gnutls_x509.c */
911 parse_pkcs12 (gnutls_pkcs12_t p12
,
912 const char *password
,
913 gnutls_x509_privkey_t
* key
,
914 gnutls_x509_crt_t
* cert
)
916 gnutls_pkcs12_bag_t bag
= NULL
;
925 ret
= gnutls_pkcs12_bag_init (&bag
);
932 ret
= gnutls_pkcs12_get_bag (p12
, index
, bag
);
933 if (ret
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
940 ret
= gnutls_pkcs12_bag_get_type (bag
, 0);
946 if (ret
== GNUTLS_BAG_ENCRYPTED
)
948 ret
= gnutls_pkcs12_bag_decrypt (bag
, password
);
955 elements_in_bag
= gnutls_pkcs12_bag_get_count (bag
);
956 if (elements_in_bag
< 0)
961 for (i
= 0; i
< elements_in_bag
; i
++)
966 type
= gnutls_pkcs12_bag_get_type (bag
, i
);
972 ret
= gnutls_pkcs12_bag_get_data (bag
, i
, &data
);
980 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY
:
981 case GNUTLS_BAG_PKCS8_KEY
:
982 ret
= gnutls_x509_privkey_init (key
);
988 ret
= gnutls_x509_privkey_import_pkcs8
989 (*key
, &data
, GNUTLS_X509_FMT_DER
, password
,
990 type
== GNUTLS_BAG_PKCS8_KEY
? GNUTLS_PKCS_PLAIN
: 0);
997 case GNUTLS_BAG_CERTIFICATE
:
998 ret
= gnutls_x509_crt_init (cert
);
1005 gnutls_x509_crt_import (*cert
, &data
, GNUTLS_X509_FMT_DER
);
1012 case GNUTLS_BAG_ENCRYPTED
:
1013 /* XXX Bother to recurse one level down? Unlikely to
1014 use the same password anyway. */
1015 case GNUTLS_BAG_EMPTY
:
1022 gnutls_pkcs12_bag_deinit (bag
);
1029 gnutls_pkcs12_bag_deinit (bag
);
1033 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar
*file
, const gchar
*password
,
1034 gnutls_x509_crt_t
*x509
, gnutls_x509_privkey_t
*pkey
)
1036 gnutls_pkcs12_t p12
= NULL
;
1045 if (is_file_exist(file
)) {
1046 FILE *fp
= claws_fopen(file
, "r");
1048 p12
= gnutls_import_PKCS12_fp(fp
, GNUTLS_X509_FMT_DER
);
1051 log_error(LOG_PROTOCOL
, _("Failed to read P12 certificate file %s\n"), file
);
1054 log_error(LOG_PROTOCOL
, _("Cannot open P12 certificate file %s (%s)\n"),
1055 file
, g_strerror(errno
));
1058 log_error(LOG_PROTOCOL
, _("P12 Certificate file %s missing (%s)\n"), file
,
1062 if ((r
= parse_pkcs12(p12
, password
, pkey
, x509
)) == 0) {
1063 debug_print("got p12\n");
1065 log_error(LOG_PROTOCOL
, "%s\n", gnutls_strerror(r
));
1067 gnutls_pkcs12_deinit(p12
);
1071 gboolean
ssl_certificate_check_subject_cn(SSLCertificate
*cert
)
1073 return gnutls_x509_crt_check_hostname(cert
->x509_cert
, cert
->host
) != 0;
1076 gchar
*ssl_certificate_get_subject_cn(SSLCertificate
*cert
)
1078 gchar subject_cn
[BUFFSIZE
];
1079 size_t n
= BUFFSIZE
;
1081 if(gnutls_x509_crt_get_dn_by_oid(cert
->x509_cert
,
1082 GNUTLS_OID_X520_COMMON_NAME
, 0, 0, subject_cn
, &n
))
1083 return g_strdup(_("<not in certificate>"));
1085 return g_strdup(subject_cn
);
1088 #endif /* USE_GNUTLS */