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 output_a
= g_malloc(cert_size_a
);
498 output_b
= g_malloc(cert_size_b
);
499 if ((r
= gnutls_x509_crt_export(cert_a
->x509_cert
, GNUTLS_X509_FMT_DER
, output_a
, &cert_size_a
)) < 0) {
500 g_warning("couldn't gnutls_x509_crt_export a %s", gnutls_strerror(r
));
505 if ((r
= gnutls_x509_crt_export(cert_b
->x509_cert
, GNUTLS_X509_FMT_DER
, output_b
, &cert_size_b
)) < 0) {
506 g_warning("couldn't gnutls_x509_crt_export b %s", gnutls_strerror(r
));
511 if (cert_size_a
!= cert_size_b
) {
512 g_warning("size differ %"G_GSIZE_FORMAT
" %"G_GSIZE_FORMAT
, cert_size_a
, cert_size_b
);
517 if (memcmp(output_a
, output_b
, cert_size_a
)) {
518 g_warning("contents differ");
529 static guint
check_cert(SSLCertificate
*cert
)
531 gnutls_x509_crt_t
*ca_list
= NULL
;
532 gnutls_x509_crt_t
*chain
= NULL
;
533 unsigned int max_ca
= 512, max_certs
;
534 unsigned int flags
= 0;
537 gchar
*chain_file
= NULL
, *buf
= NULL
;
540 if (claws_ssl_get_cert_file())
541 fp
= claws_fopen(claws_ssl_get_cert_file(), "r");
548 if ((r
= gnutls_import_X509_list_fp(fp
, GNUTLS_X509_FMT_PEM
, &ca_list
, &max_ca
)) < 0) {
549 debug_print("CA import failed: %s\n", gnutls_strerror(r
));
556 buf
= g_strdup_printf("%d", cert
->port
);
557 chain_file
= get_certificate_chain_path(cert
->host
, buf
, cert
->fingerprint
);
559 if (is_file_exist(chain_file
)) {
560 unsigned char md
[128];
564 fp
= claws_fopen(chain_file
, "r");
566 debug_print("claws_fopen %s failed: %s\n", chain_file
, g_strerror(errno
));
570 if ((r
= gnutls_import_X509_list_fp(fp
, GNUTLS_X509_FMT_PEM
, &chain
, &max_certs
)) < 0) {
571 debug_print("chain import failed: %s\n", gnutls_strerror(r
));
580 gnutls_x509_crt_get_fingerprint(chain
[0], GNUTLS_DIG_MD5
, md
, &n
);
581 fingerprint
= readable_fingerprint(md
, n
);
582 if (!fingerprint
|| strcmp(fingerprint
, cert
->fingerprint
)) {
583 debug_print("saved chain fingerprint does not match current : %s / %s\n",
584 cert
->fingerprint
, fingerprint
);
590 r
= gnutls_x509_crt_list_verify (chain
,
594 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT
,
597 debug_print("chain check failed: %s\n", gnutls_strerror(r
));
599 for (i
= 0; i
< max_certs
; i
++)
600 gnutls_x509_crt_deinit(chain
[i
]);
604 r
= gnutls_x509_crt_verify(cert
->x509_cert
, ca_list
, max_ca
, flags
, &status
);
606 debug_print("cert check failed: %s\n", gnutls_strerror(r
));
609 for (i
= 0; i
< max_ca
; i
++)
610 gnutls_x509_crt_deinit(ca_list
[i
]);
620 static gboolean
ssl_certificate_is_valid(SSLCertificate
*cert
, guint status
)
622 gchar
*str_status
= ssl_certificate_check_signer(cert
, status
);
624 if (str_status
!= NULL
) {
628 return ssl_certificate_check_subject_cn(cert
);
631 char *ssl_certificate_check_signer (SSLCertificate
*cert
, guint status
)
633 gnutls_x509_crt_t x509_cert
= cert
? cert
->x509_cert
: NULL
;
636 return g_strdup(_("Internal error"));
638 if (status
== (guint
)-1) {
639 status
= check_cert(cert
);
641 return g_strdup(_("Uncheckable"));
643 if (status
& GNUTLS_CERT_INVALID
) {
644 if (gnutls_x509_crt_check_issuer(x509_cert
, x509_cert
))
645 return g_strdup(_("Self-signed certificate"));
647 if (status
& GNUTLS_CERT_REVOKED
)
648 return g_strdup(_("Revoked certificate"));
649 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
650 return g_strdup(_("No certificate issuer found"));
651 if (status
& GNUTLS_CERT_SIGNER_NOT_CA
)
652 return g_strdup(_("Certificate issuer is not a CA"));
658 static void ssl_certificate_save_chain(gnutls_x509_crt_t
*certs
, gint len
, const gchar
*host
, gushort port
)
664 for (i
= 0; i
< len
; i
++) {
666 unsigned char md
[128];
667 gnutls_x509_crt_t cert
= certs
[i
];
671 gnutls_x509_crt_get_fingerprint(cert
, GNUTLS_DIG_MD5
, md
, &n
);
672 gchar
*fingerprint
= readable_fingerprint(md
, n
);
673 gchar
*buf
= g_strdup_printf("%d", port
);
675 file
= get_certificate_chain_path(host
, buf
, fingerprint
);
680 fp
= claws_fopen(file
, "wb");
683 debug_print("Can't save certificate !\n");
689 gnutls_export_X509_fp(fp
, cert
, GNUTLS_X509_FMT_PEM
);
693 claws_safe_fclose(fp
);
696 gboolean
ssl_certificate_check (gnutls_x509_crt_t x509_cert
, guint status
,
697 const gchar
*host
, gushort port
,
698 gboolean accept_if_valid
)
700 SSLCertificate
*current_cert
= NULL
;
701 SSLCertificate
*known_cert
;
702 SSLCertHookData cert_hook_data
;
705 unsigned char md
[128];
706 gboolean valid
= FALSE
;
708 current_cert
= ssl_certificate_new(x509_cert
, host
, port
);
710 if (current_cert
== NULL
) {
711 debug_print("Buggy certificate !\n");
715 current_cert
->status
= status
;
718 gnutls_x509_crt_get_fingerprint(x509_cert
, GNUTLS_DIG_MD5
, md
, &n
);
719 fingerprint
= readable_fingerprint(md
, n
);
721 known_cert
= ssl_certificate_find(host
, port
, fingerprint
);
726 valid
= ssl_certificate_is_valid(current_cert
, status
);
728 valid
= FALSE
; /* Force check */
730 if (known_cert
== NULL
) {
732 ssl_certificate_save(current_cert
);
733 ssl_certificate_destroy(current_cert
);
737 cert_hook_data
.cert
= current_cert
;
738 cert_hook_data
.old_cert
= NULL
;
739 cert_hook_data
.expired
= FALSE
;
740 cert_hook_data
.accept
= FALSE
;
742 hooks_invoke(SSLCERT_ASK_HOOKLIST
, &cert_hook_data
);
744 if (!cert_hook_data
.accept
) {
745 ssl_certificate_destroy(current_cert
);
748 ssl_certificate_save(current_cert
);
749 ssl_certificate_destroy(current_cert
);
752 } else if (!ssl_certificate_compare (current_cert
, known_cert
)) {
754 ssl_certificate_save(current_cert
);
755 ssl_certificate_destroy(current_cert
);
756 ssl_certificate_destroy(known_cert
);
760 cert_hook_data
.cert
= current_cert
;
761 cert_hook_data
.old_cert
= known_cert
;
762 cert_hook_data
.expired
= FALSE
;
763 cert_hook_data
.accept
= FALSE
;
765 hooks_invoke(SSLCERT_ASK_HOOKLIST
, &cert_hook_data
);
767 if (!cert_hook_data
.accept
) {
768 ssl_certificate_destroy(current_cert
);
769 ssl_certificate_destroy(known_cert
);
772 ssl_certificate_save(current_cert
);
773 ssl_certificate_destroy(current_cert
);
774 ssl_certificate_destroy(known_cert
);
777 } else if (gnutls_x509_crt_get_expiration_time(current_cert
->x509_cert
) < time(NULL
)) {
778 gchar
*tmp
= g_strdup_printf("%s:%d", current_cert
->host
, current_cert
->port
);
780 if (warned_expired
== NULL
)
781 warned_expired
= g_hash_table_new(g_str_hash
, g_str_equal
);
783 if (g_hash_table_lookup(warned_expired
, tmp
)) {
785 ssl_certificate_destroy(current_cert
);
786 ssl_certificate_destroy(known_cert
);
790 cert_hook_data
.cert
= current_cert
;
791 cert_hook_data
.old_cert
= NULL
;
792 cert_hook_data
.expired
= TRUE
;
793 cert_hook_data
.accept
= FALSE
;
795 hooks_invoke(SSLCERT_ASK_HOOKLIST
, &cert_hook_data
);
797 if (!cert_hook_data
.accept
) {
799 ssl_certificate_destroy(current_cert
);
800 ssl_certificate_destroy(known_cert
);
803 g_hash_table_insert(warned_expired
, tmp
, GINT_TO_POINTER(1));
804 ssl_certificate_destroy(current_cert
);
805 ssl_certificate_destroy(known_cert
);
810 ssl_certificate_destroy(current_cert
);
811 ssl_certificate_destroy(known_cert
);
815 gboolean
ssl_certificate_check_chain(gnutls_x509_crt_t
*certs
, gint chain_len
,
816 const gchar
*host
, gushort port
,
817 gboolean accept_if_valid
)
820 gnutls_x509_crt_t
*cas
= NULL
;
821 gboolean result
= FALSE
;
825 if (claws_ssl_get_cert_file()) {
826 FILE *fp
= claws_fopen(claws_ssl_get_cert_file(), "rb");
830 r
= gnutls_import_X509_list_fp(fp
, GNUTLS_X509_FMT_PEM
, &cas
, &ncas
);
835 g_warning("can't read SSL_CERT_FILE '%s': %s",
836 claws_ssl_get_cert_file(),
839 debug_print("Can't find SSL ca-certificates file\n");
843 gnutls_x509_crt_list_verify (certs
,
847 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT
,
850 result
= ssl_certificate_check(certs
[0], status
, host
, port
,
853 if (result
== TRUE
) {
854 ssl_certificate_save_chain(certs
, chain_len
, host
, port
);
857 for (i
= 0; i
< ncas
; i
++)
858 gnutls_x509_crt_deinit(cas
[i
]);
864 gnutls_x509_crt_t
ssl_certificate_get_x509_from_pem_file(const gchar
*file
)
866 gnutls_x509_crt_t x509
= NULL
;
870 if (is_file_exist(file
)) {
871 FILE *fp
= claws_fopen(file
, "r");
873 x509
= gnutls_import_X509_fp(fp
, GNUTLS_X509_FMT_PEM
);
877 log_error(LOG_PROTOCOL
, _("Cannot open certificate file %s: %s\n"),
878 file
, g_strerror(errno
));
881 log_error(LOG_PROTOCOL
, _("Certificate file %s missing (%s)\n"),
882 file
, g_strerror(errno
));
887 gnutls_x509_privkey_t
ssl_certificate_get_pkey_from_pem_file(const gchar
*file
)
889 gnutls_x509_privkey_t key
= NULL
;
893 if (is_file_exist(file
)) {
894 FILE *fp
= claws_fopen(file
, "r");
896 key
= gnutls_import_key_fp(fp
, GNUTLS_X509_FMT_PEM
);
900 log_error(LOG_PROTOCOL
, _("Cannot open key file %s (%s)\n"),
901 file
, g_strerror(errno
));
904 log_error(LOG_PROTOCOL
, _("Key file %s missing (%s)\n"), file
,
910 /* From GnuTLS lib/gnutls_x509.c */
912 parse_pkcs12 (gnutls_pkcs12_t p12
,
913 const char *password
,
914 gnutls_x509_privkey_t
* key
,
915 gnutls_x509_crt_t
* cert
)
917 gnutls_pkcs12_bag_t bag
= NULL
;
926 ret
= gnutls_pkcs12_bag_init (&bag
);
933 ret
= gnutls_pkcs12_get_bag (p12
, index
, bag
);
934 if (ret
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
941 ret
= gnutls_pkcs12_bag_get_type (bag
, 0);
947 if (ret
== GNUTLS_BAG_ENCRYPTED
)
949 ret
= gnutls_pkcs12_bag_decrypt (bag
, password
);
956 elements_in_bag
= gnutls_pkcs12_bag_get_count (bag
);
957 if (elements_in_bag
< 0)
962 for (i
= 0; i
< elements_in_bag
; i
++)
967 type
= gnutls_pkcs12_bag_get_type (bag
, i
);
973 ret
= gnutls_pkcs12_bag_get_data (bag
, i
, &data
);
981 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY
:
982 case GNUTLS_BAG_PKCS8_KEY
:
983 ret
= gnutls_x509_privkey_init (key
);
989 ret
= gnutls_x509_privkey_import_pkcs8
990 (*key
, &data
, GNUTLS_X509_FMT_DER
, password
,
991 type
== GNUTLS_BAG_PKCS8_KEY
? GNUTLS_PKCS_PLAIN
: 0);
998 case GNUTLS_BAG_CERTIFICATE
:
999 ret
= gnutls_x509_crt_init (cert
);
1006 gnutls_x509_crt_import (*cert
, &data
, GNUTLS_X509_FMT_DER
);
1013 case GNUTLS_BAG_ENCRYPTED
:
1014 /* XXX Bother to recurse one level down? Unlikely to
1015 use the same password anyway. */
1016 case GNUTLS_BAG_EMPTY
:
1023 gnutls_pkcs12_bag_deinit (bag
);
1030 gnutls_pkcs12_bag_deinit (bag
);
1034 void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar
*file
, const gchar
*password
,
1035 gnutls_x509_crt_t
*x509
, gnutls_x509_privkey_t
*pkey
)
1037 gnutls_pkcs12_t p12
= NULL
;
1046 if (is_file_exist(file
)) {
1047 FILE *fp
= claws_fopen(file
, "r");
1049 p12
= gnutls_import_PKCS12_fp(fp
, GNUTLS_X509_FMT_DER
);
1052 log_error(LOG_PROTOCOL
, _("Failed to read P12 certificate file %s\n"), file
);
1055 log_error(LOG_PROTOCOL
, _("Cannot open P12 certificate file %s (%s)\n"),
1056 file
, g_strerror(errno
));
1059 log_error(LOG_PROTOCOL
, _("P12 Certificate file %s missing (%s)\n"), file
,
1063 if ((r
= parse_pkcs12(p12
, password
, pkey
, x509
)) == 0) {
1064 debug_print("got p12\n");
1066 log_error(LOG_PROTOCOL
, "%s\n", gnutls_strerror(r
));
1068 gnutls_pkcs12_deinit(p12
);
1072 gboolean
ssl_certificate_check_subject_cn(SSLCertificate
*cert
)
1074 return gnutls_x509_crt_check_hostname(cert
->x509_cert
, cert
->host
) != 0;
1077 gchar
*ssl_certificate_get_subject_cn(SSLCertificate
*cert
)
1079 gchar subject_cn
[BUFFSIZE
];
1080 size_t n
= BUFFSIZE
;
1082 if(gnutls_x509_crt_get_dn_by_oid(cert
->x509_cert
,
1083 GNUTLS_OID_X520_COMMON_NAME
, 0, 0, subject_cn
, &n
))
1084 return g_strdup(_("<not in certificate>"));
1086 return g_strdup(subject_cn
);
1089 #endif /* USE_GNUTLS */