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 <sys/types.h>
38 #include <glib/gi18n.h>
40 #include "ssl_certificate.h"
47 static GHashTable *warned_expired = NULL;
49 gboolean prefs_common_unsafe_ssl_certs(void);
51 static gchar *get_certificate_path(const gchar *host, const gchar *port, const gchar *fp)
53 if (fp != NULL && prefs_common_unsafe_ssl_certs())
54 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
55 "certs", G_DIR_SEPARATOR_S,
56 host, ".", port, ".", fp, ".cert", NULL);
58 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
59 "certs", G_DIR_SEPARATOR_S,
60 host, ".", port, ".cert", NULL);
64 static SSLCertificate *ssl_certificate_new_lookup(X509 *x509_cert, gchar *host, gushort port, gboolean lookup);
66 static SSLCertificate *ssl_certificate_new_lookup(gnutls_x509_crt x509_cert, gchar *host, gushort port, gboolean lookup);
70 time_t asn1toTime(ASN1_TIME *asn1Time)
75 if (asn1Time == NULL || asn1Time->length < 13)
78 memset(&tm, 0, sizeof(tm));
80 #define N2(n) ((asn1Time->data[n]-'0')*10 + asn1Time->data[(n)+1]-'0')
82 #define CPY(f,n) (tm.f=N2(n))
87 tm.tm_year += 100; /* Sux */
98 if (asn1Time->data[12] != 'Z')
100 if (asn1Time->length < 17)
103 offset=N2(13)*3600+N2(15)*60;
105 if (asn1Time->data[12] == '-')
112 return mktime(&tm)-offset;
116 static char * get_fqdn(char *host)
120 if (host == NULL || strlen(host) == 0)
123 hp = my_gethostbyname(host);
125 return g_strdup(host); /*caller should free*/
127 return g_strdup(hp->h_name);
130 char * readable_fingerprint(unsigned char *src, int len)
141 tmp2 = g_strdup_printf("%s:%02X", ret, src[i]);
143 tmp2 = g_strdup_printf("%02X", src[i]);
145 ret = g_strdup(tmp2);
153 static gnutls_x509_crt x509_crt_copy(gnutls_x509_crt src)
158 gnutls_x509_crt dest;
160 if (gnutls_x509_crt_init(&dest) != 0) {
161 g_warning("couldn't gnutls_x509_crt_init\n");
165 if (gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, NULL, &size)
166 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
167 g_warning("couldn't gnutls_x509_crt_export to get size\n");
168 gnutls_x509_crt_deinit(dest);
172 tmp.data = malloc(size);
173 memset(tmp.data, 0, size);
174 ret = gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, tmp.data, &size);
177 ret = gnutls_x509_crt_import(dest, &tmp, GNUTLS_X509_FMT_DER);
179 g_warning("couldn't gnutls_x509_crt_import for real (%d %s)\n", ret, gnutls_strerror(ret));
180 gnutls_x509_crt_deinit(dest);
184 g_warning("couldn't gnutls_x509_crt_export for real (%d %s)\n", ret, gnutls_strerror(ret));
185 gnutls_x509_crt_deinit(dest);
195 static SSLCertificate *ssl_certificate_new_lookup(X509 *x509_cert, gchar *host, gushort port, gboolean lookup)
197 static SSLCertificate *ssl_certificate_new_lookup(gnutls_x509_crt x509_cert, gchar *host, gushort port, gboolean lookup)
200 SSLCertificate *cert = g_new0(SSLCertificate, 1);
202 unsigned char md[128];
204 if (host == NULL || x509_cert == NULL) {
205 ssl_certificate_destroy(cert);
209 cert->x509_cert = X509_dup(x509_cert);
211 cert->x509_cert = x509_crt_copy(x509_cert);
212 cert->status = (guint)-1;
215 cert->host = get_fqdn(host);
217 cert->host = g_strdup(host);
222 X509_digest(cert->x509_cert, EVP_md5(), md, &n);
223 cert->fingerprint = readable_fingerprint(md, (int)n);
225 gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_MD5, md, &n);
226 cert->fingerprint = readable_fingerprint(md, (int)n);
232 static void i2d_X509_fp(FILE *fp, gnutls_x509_crt x509_cert)
234 char output[10*1024];
235 size_t cert_size = 10*1024;
238 if ((r = gnutls_x509_crt_export(x509_cert, GNUTLS_X509_FMT_DER, output, &cert_size)) < 0) {
239 g_warning("couldn't export cert %s (%d)\n", gnutls_strerror(r), cert_size);
242 debug_print("writing %zd bytes\n",cert_size);
243 if (fwrite(&output, 1, cert_size, fp) < cert_size) {
244 g_warning("failed to write cert\n");
247 static gnutls_x509_crt d2i_X509_fp(FILE *fp, int unused)
249 gnutls_x509_crt cert = NULL;
253 if (fstat(fileno(fp), &s) < 0) {
257 tmp.data = malloc(s.st_size);
258 memset(tmp.data, 0, s.st_size);
259 tmp.size = s.st_size;
260 if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
265 gnutls_x509_crt_init(&cert);
266 if ((r = gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER)) < 0) {
267 g_warning("import failed: %s\n", gnutls_strerror(r));
268 gnutls_x509_crt_deinit(cert);
271 debug_print("got cert! %p\n", cert);
276 static void ssl_certificate_save (SSLCertificate *cert)
281 file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
282 "certs", G_DIR_SEPARATOR_S, NULL);
284 if (!is_dir_exist(file))
288 port = g_strdup_printf("%d", cert->port);
289 file = get_certificate_path(cert->host, port, cert->fingerprint);
292 fp = g_fopen(file, "wb");
295 debug_print("Can't save certificate !\n");
298 i2d_X509_fp(fp, cert->x509_cert);
304 void ssl_certificate_destroy(SSLCertificate *cert)
311 X509_free(cert->x509_cert);
313 gnutls_x509_crt_deinit(cert->x509_cert);
316 g_free(cert->fingerprint);
321 void ssl_certificate_delete_from_disk(SSLCertificate *cert)
325 buf = g_strdup_printf("%d", cert->port);
326 file = get_certificate_path(cert->host, buf, cert->fingerprint);
332 SSLCertificate *ssl_certificate_find (gchar *host, gushort port, const gchar *fingerprint)
334 return ssl_certificate_find_lookup (host, port, fingerprint, TRUE);
337 SSLCertificate *ssl_certificate_find_lookup (gchar *host, gushort port, const gchar *fingerprint, gboolean lookup)
342 SSLCertificate *cert = NULL;
346 gnutls_x509_crt tmp_x509;
349 gboolean must_rename = FALSE;
352 fqdn_host = get_fqdn(host);
354 fqdn_host = g_strdup(host);
356 buf = g_strdup_printf("%d", port);
358 if (fingerprint != NULL) {
359 file = get_certificate_path(fqdn_host, buf, fingerprint);
360 fp = g_fopen(file, "rb");
363 /* see if we have the old one */
364 debug_print("didn't get %s\n", file);
366 file = get_certificate_path(fqdn_host, buf, NULL);
367 fp = g_fopen(file, "rb");
370 debug_print("got %s\n", file);
371 must_rename = (fingerprint != NULL);
374 debug_print("got %s first try\n", file);
383 if ((tmp_x509 = d2i_X509_fp(fp, 0)) != NULL) {
384 cert = ssl_certificate_new_lookup(tmp_x509, fqdn_host, port, lookup);
385 debug_print("got cert %p\n", cert);
389 gnutls_x509_crt_deinit(tmp_x509);
396 gchar *old = get_certificate_path(fqdn_host, buf, NULL);
397 gchar *new = get_certificate_path(fqdn_host, buf, fingerprint);
398 if (strcmp(old, new))
399 move_file(old, new, TRUE);
409 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
412 if (cert_a == NULL || cert_b == NULL)
414 else if (!X509_cmp(cert_a->x509_cert, cert_b->x509_cert))
421 size_t cert_size_a = 0, cert_size_b = 0;
424 if (cert_a == NULL || cert_b == NULL)
427 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_a))
428 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
429 g_warning("couldn't gnutls_x509_crt_export to get size a %s\n", gnutls_strerror(r));
433 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_b))
434 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
435 g_warning("couldn't gnutls_x509_crt_export to get size b %s\n", gnutls_strerror(r));
439 output_a = malloc(cert_size_a);
440 output_b = malloc(cert_size_b);
441 if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
442 g_warning("couldn't gnutls_x509_crt_export a %s\n", gnutls_strerror(r));
445 if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
446 g_warning("couldn't gnutls_x509_crt_export b %s\n", gnutls_strerror(r));
449 if (cert_size_a != cert_size_b) {
450 g_warning("size differ %d %d\n", cert_size_a, cert_size_b);
453 if (memcmp(output_a, output_b, cert_size_a)) {
454 g_warning("contents differ\n");
463 char *ssl_certificate_check_signer (X509 *cert)
465 X509_STORE_CTX store_ctx;
467 char *err_msg = NULL;
469 store = X509_STORE_new();
471 g_print("Can't create X509_STORE\n");
474 if (!X509_STORE_set_default_paths(store)) {
475 X509_STORE_free (store);
476 return g_strdup(_("Couldn't load X509 default paths"));
479 X509_STORE_CTX_init (&store_ctx, store, cert, NULL);
481 if(!X509_verify_cert (&store_ctx)) {
482 err_msg = g_strdup(X509_verify_cert_error_string(
483 X509_STORE_CTX_get_error(&store_ctx)));
484 debug_print("Can't check signer: %s\n", err_msg);
485 X509_STORE_CTX_cleanup (&store_ctx);
486 X509_STORE_free (store);
490 X509_STORE_CTX_cleanup (&store_ctx);
491 X509_STORE_free (store);
495 char *ssl_certificate_check_signer (gnutls_x509_crt cert, guint status)
497 if (status == (guint)-1)
498 return g_strdup(_("Uncheckable"));
500 if (status & GNUTLS_CERT_INVALID) {
501 if (gnutls_x509_crt_check_issuer(cert, cert))
502 return g_strdup(_("Self-signed certificate"));
504 if (status & GNUTLS_CERT_REVOKED)
505 return g_strdup(_("Revoked certificate"));
506 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
507 return g_strdup(_("No certificate issuer found"));
508 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
509 return g_strdup(_("Certificate issuer is not a CA"));
517 gboolean ssl_certificate_check (X509 *x509_cert, gchar *fqdn, gchar *host, gushort port)
519 gboolean ssl_certificate_check (gnutls_x509_crt x509_cert, guint status, gchar *fqdn, gchar *host, gushort port)
522 SSLCertificate *current_cert = NULL;
523 SSLCertificate *known_cert;
524 SSLCertHookData cert_hook_data;
525 gchar *fqdn_host = NULL;
528 unsigned char md[128];
531 fqdn_host = g_strdup(fqdn);
533 fqdn_host = get_fqdn(host);
535 g_warning("no host!\n");
539 current_cert = ssl_certificate_new_lookup(x509_cert, fqdn_host, port, FALSE);
541 if (current_cert == NULL) {
542 debug_print("Buggy certificate !\n");
548 current_cert->status = status;
552 X509_digest(x509_cert, EVP_md5(), md, &n);
553 fingerprint = readable_fingerprint(md, (int)n);
556 gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
557 fingerprint = readable_fingerprint(md, (int)n);
560 known_cert = ssl_certificate_find_lookup (fqdn_host, port, fingerprint, FALSE);
565 if (known_cert == NULL) {
566 cert_hook_data.cert = current_cert;
567 cert_hook_data.old_cert = NULL;
568 cert_hook_data.expired = FALSE;
569 cert_hook_data.accept = FALSE;
571 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
573 if (!cert_hook_data.accept) {
574 ssl_certificate_destroy(current_cert);
577 ssl_certificate_save(current_cert);
578 ssl_certificate_destroy(current_cert);
581 } else if (!ssl_certificate_compare (current_cert, known_cert)) {
582 cert_hook_data.cert = current_cert;
583 cert_hook_data.old_cert = known_cert;
584 cert_hook_data.expired = FALSE;
585 cert_hook_data.accept = FALSE;
587 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
589 if (!cert_hook_data.accept) {
590 ssl_certificate_destroy(current_cert);
591 ssl_certificate_destroy(known_cert);
594 ssl_certificate_save(current_cert);
595 ssl_certificate_destroy(current_cert);
596 ssl_certificate_destroy(known_cert);
600 } else if (asn1toTime(X509_get_notAfter(current_cert->x509_cert)) < time(NULL)) {
602 } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
604 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
606 if (warned_expired == NULL)
607 warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
609 if (g_hash_table_lookup(warned_expired, tmp)) {
611 ssl_certificate_destroy(current_cert);
612 ssl_certificate_destroy(known_cert);
616 cert_hook_data.cert = current_cert;
617 cert_hook_data.old_cert = NULL;
618 cert_hook_data.expired = TRUE;
619 cert_hook_data.accept = FALSE;
621 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
623 if (!cert_hook_data.accept) {
625 ssl_certificate_destroy(current_cert);
626 ssl_certificate_destroy(known_cert);
629 g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
630 ssl_certificate_destroy(current_cert);
631 ssl_certificate_destroy(known_cert);
636 ssl_certificate_destroy(current_cert);
637 ssl_certificate_destroy(known_cert);
641 #endif /* USE_OPENSSL */