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/>.
27 #include <openssl/ssl.h>
29 #include <glib/gi18n.h>
31 #include "ssl_certificate.h"
37 static GHashTable *warned_expired = NULL;
39 gboolean prefs_common_unsafe_ssl_certs(void);
41 static gchar *get_certificate_path(const gchar *host, const gchar *port, const gchar *fp)
43 if (fp != NULL && prefs_common_unsafe_ssl_certs())
44 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
45 "certs", G_DIR_SEPARATOR_S,
46 host, ".", port, ".", fp, ".cert", NULL);
48 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
49 "certs", G_DIR_SEPARATOR_S,
50 host, ".", port, ".cert", NULL);
53 static SSLCertificate *ssl_certificate_new_lookup(X509 *x509_cert, gchar *host, gushort port, gboolean lookup);
56 time_t asn1toTime(ASN1_TIME *asn1Time)
61 if (asn1Time == NULL || asn1Time->length < 13)
64 memset(&tm, 0, sizeof(tm));
66 #define N2(n) ((asn1Time->data[n]-'0')*10 + asn1Time->data[(n)+1]-'0')
68 #define CPY(f,n) (tm.f=N2(n))
73 tm.tm_year += 100; /* Sux */
84 if (asn1Time->data[12] != 'Z')
86 if (asn1Time->length < 17)
89 offset=N2(13)*3600+N2(15)*60;
91 if (asn1Time->data[12] == '-')
98 return mktime(&tm)-offset;
101 static char * get_fqdn(char *host)
105 if (host == NULL || strlen(host) == 0)
108 hp = my_gethostbyname(host);
110 return g_strdup(host); /*caller should free*/
112 return g_strdup(hp->h_name);
115 char * readable_fingerprint(unsigned char *src, int len)
126 tmp2 = g_strdup_printf("%s:%02X", ret, src[i]);
128 tmp2 = g_strdup_printf("%02X", src[i]);
130 ret = g_strdup(tmp2);
137 static SSLCertificate *ssl_certificate_new_lookup(X509 *x509_cert, gchar *host, gushort port, gboolean lookup)
139 SSLCertificate *cert = g_new0(SSLCertificate, 1);
141 unsigned char md[EVP_MAX_MD_SIZE];
143 if (host == NULL || x509_cert == NULL) {
144 ssl_certificate_destroy(cert);
147 cert->x509_cert = X509_dup(x509_cert);
149 cert->host = get_fqdn(host);
151 cert->host = g_strdup(host);
155 X509_digest(cert->x509_cert, EVP_md5(), md, &n);
156 cert->fingerprint = readable_fingerprint(md, (int)n);
161 static void ssl_certificate_save (SSLCertificate *cert)
166 file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
167 "certs", G_DIR_SEPARATOR_S, NULL);
169 if (!is_dir_exist(file))
173 port = g_strdup_printf("%d", cert->port);
174 file = get_certificate_path(cert->host, port, cert->fingerprint);
177 fp = g_fopen(file, "wb");
180 debug_print("Can't save certificate !\n");
183 i2d_X509_fp(fp, cert->x509_cert);
189 char* ssl_certificate_to_string(SSLCertificate *cert)
192 char *issuer_commonname, *issuer_location, *issuer_organization;
193 char *subject_commonname, *subject_location, *subject_organization;
194 char *fingerprint, *sig_status;
196 unsigned char md[EVP_MAX_MD_SIZE];
199 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
200 NID_commonName, buf, 100) >= 0)
201 issuer_commonname = g_strdup(buf);
203 issuer_commonname = g_strdup(_("<not in certificate>"));
204 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
205 NID_localityName, buf, 100) >= 0) {
206 issuer_location = g_strdup(buf);
207 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
208 NID_countryName, buf, 100) >= 0)
209 issuer_location = g_strconcat(issuer_location,", ",buf, NULL);
210 } else if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
211 NID_countryName, buf, 100) >= 0)
212 issuer_location = g_strdup(buf);
214 issuer_location = g_strdup(_("<not in certificate>"));
216 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
217 NID_organizationName, buf, 100) >= 0)
218 issuer_organization = g_strdup(buf);
220 issuer_organization = g_strdup(_("<not in certificate>"));
223 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
224 NID_commonName, buf, 100) >= 0)
225 subject_commonname = g_strdup(buf);
227 subject_commonname = g_strdup(_("<not in certificate>"));
228 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
229 NID_localityName, buf, 100) >= 0) {
230 subject_location = g_strdup(buf);
231 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
232 NID_countryName, buf, 100) >= 0)
233 subject_location = g_strconcat(subject_location,", ",buf, NULL);
234 } else if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
235 NID_countryName, buf, 100) >= 0)
236 subject_location = g_strdup(buf);
238 subject_location = g_strdup(_("<not in certificate>"));
240 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
241 NID_organizationName, buf, 100) >= 0)
242 subject_organization = g_strdup(buf);
244 subject_organization = g_strdup(_("<not in certificate>"));
247 X509_digest(cert->x509_cert, EVP_md5(), md, &n);
248 fingerprint = readable_fingerprint(md, (int)n);
251 sig_status = ssl_certificate_check_signer(cert->x509_cert);
253 ret = g_strdup_printf(_(" Owner: %s (%s) in %s\n Signed by: %s (%s) in %s\n Fingerprint: %s\n Signature status: %s"),
254 subject_commonname, subject_organization, subject_location,
255 issuer_commonname, issuer_organization, issuer_location,
257 (sig_status==NULL ? "correct":sig_status));
259 g_free(issuer_commonname);
260 g_free(issuer_location);
261 g_free(issuer_organization);
262 g_free(subject_commonname);
263 g_free(subject_location);
264 g_free(subject_organization);
270 void ssl_certificate_destroy(SSLCertificate *cert)
276 X509_free(cert->x509_cert);
278 g_free(cert->fingerprint);
283 void ssl_certificate_delete_from_disk(SSLCertificate *cert)
287 buf = g_strdup_printf("%d", cert->port);
288 file = get_certificate_path(cert->host, buf, cert->fingerprint);
294 SSLCertificate *ssl_certificate_find (gchar *host, gushort port, const gchar *fingerprint)
296 return ssl_certificate_find_lookup (host, port, fingerprint, TRUE);
299 SSLCertificate *ssl_certificate_find_lookup (gchar *host, gushort port, const gchar *fingerprint, gboolean lookup)
304 SSLCertificate *cert = NULL;
307 gboolean must_rename = FALSE;
310 fqdn_host = get_fqdn(host);
312 fqdn_host = g_strdup(host);
314 buf = g_strdup_printf("%d", port);
316 if (fingerprint != NULL) {
317 file = get_certificate_path(fqdn_host, buf, fingerprint);
318 fp = g_fopen(file, "rb");
321 /* see if we have the old one */
323 file = get_certificate_path(fqdn_host, buf, NULL);
324 fp = g_fopen(file, "rb");
327 must_rename = (fingerprint != NULL);
336 if ((tmp_x509 = d2i_X509_fp(fp, 0)) != NULL) {
337 cert = ssl_certificate_new_lookup(tmp_x509, fqdn_host, port, lookup);
344 gchar *old = get_certificate_path(fqdn_host, buf, NULL);
345 gchar *new = get_certificate_path(fqdn_host, buf, fingerprint);
346 if (strcmp(old, new))
347 move_file(old, new, TRUE);
357 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
359 if (cert_a == NULL || cert_b == NULL)
361 else if (!X509_cmp(cert_a->x509_cert, cert_b->x509_cert))
367 char *ssl_certificate_check_signer (X509 *cert)
369 X509_STORE_CTX store_ctx;
371 char *err_msg = NULL;
373 store = X509_STORE_new();
375 g_print("Can't create X509_STORE\n");
378 if (!X509_STORE_set_default_paths(store)) {
379 X509_STORE_free (store);
380 return g_strdup(_("Couldn't load X509 default paths"));
383 X509_STORE_CTX_init (&store_ctx, store, cert, NULL);
385 if(!X509_verify_cert (&store_ctx)) {
386 err_msg = g_strdup(X509_verify_cert_error_string(
387 X509_STORE_CTX_get_error(&store_ctx)));
388 debug_print("Can't check signer: %s\n", err_msg);
389 X509_STORE_CTX_cleanup (&store_ctx);
390 X509_STORE_free (store);
394 X509_STORE_CTX_cleanup (&store_ctx);
395 X509_STORE_free (store);
399 gboolean ssl_certificate_check (X509 *x509_cert, gchar *fqdn, gchar *host, gushort port)
401 SSLCertificate *current_cert = NULL;
402 SSLCertificate *known_cert;
403 SSLCertHookData cert_hook_data;
404 gchar *fqdn_host = NULL;
407 unsigned char md[EVP_MAX_MD_SIZE];
410 fqdn_host = g_strdup(fqdn);
412 fqdn_host = get_fqdn(host);
414 g_warning("no host!\n");
418 current_cert = ssl_certificate_new_lookup(x509_cert, fqdn_host, port, FALSE);
420 if (current_cert == NULL) {
421 debug_print("Buggy certificate !\n");
427 X509_digest(x509_cert, EVP_md5(), md, &n);
428 fingerprint = readable_fingerprint(md, (int)n);
430 known_cert = ssl_certificate_find_lookup (fqdn_host, port, fingerprint, FALSE);
435 if (known_cert == NULL) {
436 cert_hook_data.cert = current_cert;
437 cert_hook_data.old_cert = NULL;
438 cert_hook_data.expired = FALSE;
439 cert_hook_data.accept = FALSE;
441 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
443 if (!cert_hook_data.accept) {
444 ssl_certificate_destroy(current_cert);
447 ssl_certificate_save(current_cert);
448 ssl_certificate_destroy(current_cert);
451 } else if (!ssl_certificate_compare (current_cert, known_cert)) {
452 cert_hook_data.cert = current_cert;
453 cert_hook_data.old_cert = known_cert;
454 cert_hook_data.expired = FALSE;
455 cert_hook_data.accept = FALSE;
457 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
459 if (!cert_hook_data.accept) {
460 ssl_certificate_destroy(current_cert);
461 ssl_certificate_destroy(known_cert);
464 ssl_certificate_save(current_cert);
465 ssl_certificate_destroy(current_cert);
466 ssl_certificate_destroy(known_cert);
469 } else if (asn1toTime(X509_get_notAfter(current_cert->x509_cert)) < time(NULL)) {
470 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
472 if (warned_expired == NULL)
473 warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
475 if (g_hash_table_lookup(warned_expired, tmp)) {
477 ssl_certificate_destroy(current_cert);
478 ssl_certificate_destroy(known_cert);
482 cert_hook_data.cert = current_cert;
483 cert_hook_data.old_cert = NULL;
484 cert_hook_data.expired = TRUE;
485 cert_hook_data.accept = FALSE;
487 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
489 if (!cert_hook_data.accept) {
491 ssl_certificate_destroy(current_cert);
492 ssl_certificate_destroy(known_cert);
495 g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
496 ssl_certificate_destroy(current_cert);
497 ssl_certificate_destroy(known_cert);
502 ssl_certificate_destroy(current_cert);
503 ssl_certificate_destroy(known_cert);
507 #endif /* USE_OPENSSL */