2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <openssl/ssl.h>
28 #include "ssl_certificate.h"
29 #include "alertpanel.h"
32 #include "prefs_common.h"
35 static void ssl_certificate_destroy(SSLCertificate *cert);
36 static char *ssl_certificate_check_signer (X509 *cert);
38 static char * get_fqdn(char *host)
41 hp = my_gethostbyname(host);
43 return g_strdup(host); /*caller should free*/
45 return g_strdup(hp->h_name);
48 static char * readable_fingerprint(unsigned char *src, int len)
59 tmp2 = g_strdup_printf("%s:%02X", ret, src[i]);
61 tmp2 = g_strdup_printf("%02X", src[i]);
70 SSLCertificate *ssl_certificate_new(X509 *x509_cert, gchar *host, gushort port)
72 SSLCertificate *cert = g_new0(SSLCertificate, 1);
74 if (host == NULL || x509_cert == NULL) {
75 ssl_certificate_destroy(cert);
78 cert->x509_cert = X509_dup(x509_cert);
79 cert->host = get_fqdn(host);
84 static void ssl_certificate_save (SSLCertificate *cert)
89 file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
90 "certs", G_DIR_SEPARATOR_S, NULL);
92 if (!is_dir_exist(file))
96 port = g_strdup_printf("%d", cert->port);
97 file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
98 "certs", G_DIR_SEPARATOR_S,
99 cert->host, ".", port, ".cert", NULL);
102 fp = fopen(file, "w");
105 alertpanel_error(_("Can't save certificate !"));
108 i2d_X509_fp(fp, cert->x509_cert);
114 char* ssl_certificate_to_string(SSLCertificate *cert)
117 char *issuer_commonname, *issuer_location, *issuer_organization;
118 char *subject_commonname, *subject_location, *subject_organization;
119 char *fingerprint, *sig_status;
121 unsigned char md[EVP_MAX_MD_SIZE];
124 X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
125 NID_commonName, buf, 100);
126 issuer_commonname = g_strdup(buf);
127 X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
128 NID_localityName, buf, 100);
129 issuer_location = g_strdup(buf);
130 X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
131 NID_countryName, buf, 100);
132 issuer_location = g_strconcat(issuer_location,", ",buf, NULL);
133 X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
134 NID_organizationName, buf, 100);
135 issuer_organization = g_strdup(buf);
138 X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
139 NID_commonName, buf, 100);
140 subject_commonname = g_strdup(buf);
141 X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
142 NID_localityName, buf, 100);
143 subject_location = g_strdup(buf);
144 X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
145 NID_countryName, buf, 100);
146 subject_location = g_strconcat(subject_location,", ",buf, NULL);
147 X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
148 NID_organizationName, buf, 100);
149 subject_organization = g_strdup(buf);
152 X509_digest(cert->x509_cert, EVP_md5(), md, &n);
153 fingerprint = readable_fingerprint(md, (int)n);
156 sig_status = ssl_certificate_check_signer(cert->x509_cert);
158 ret = g_strdup_printf(_(" Owner: %s (%s) in %s\n Signed by: %s (%s) in %s\n Fingerprint: %s\n Signature status: %s"),
159 subject_commonname, subject_organization, subject_location,
160 issuer_commonname, issuer_organization, issuer_location,
162 (sig_status==NULL ? "correct":sig_status));
164 if (issuer_commonname)
165 g_free(issuer_commonname);
167 g_free(issuer_location);
168 if (issuer_organization)
169 g_free(issuer_organization);
170 if (subject_commonname)
171 g_free(subject_commonname);
172 if (subject_location)
173 g_free(subject_location);
174 if (subject_organization)
175 g_free(subject_organization);
183 void ssl_certificate_destroy(SSLCertificate *cert)
185 g_return_if_fail(cert != NULL);
187 X509_free(cert->x509_cert);
194 SSLCertificate *ssl_certificate_find (gchar *host, gushort port)
199 SSLCertificate *cert = NULL;
203 fqdn_host = get_fqdn(host);
204 buf = g_strdup_printf("%d", port);
205 file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
206 "certs", G_DIR_SEPARATOR_S,
207 fqdn_host, ".", buf, ".cert", NULL);
210 fp = fopen(file, "r");
218 if ((tmp_x509 = d2i_X509_fp(fp, 0)) != NULL) {
219 cert = ssl_certificate_new(tmp_x509, fqdn_host, port);
229 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
231 if (cert_a == NULL || cert_b == NULL)
233 else if (!X509_cmp(cert_a->x509_cert, cert_b->x509_cert))
239 static char *ssl_certificate_check_signer (X509 *cert)
241 X509_STORE_CTX store_ctx;
244 char *cert_file = NULL;
245 char *err_msg = NULL;
247 store = X509_STORE_new();
249 printf("Can't create X509_STORE\n");
252 if (X509_STORE_set_default_paths(store))
254 if (X509_STORE_load_locations(store, cert_file, NULL))
258 X509_STORE_free (store);
259 return g_strdup(_("Can't load X509 default paths"));
262 X509_STORE_CTX_init (&store_ctx, store, cert, NULL);
263 ok = X509_verify_cert (&store_ctx);
266 err_msg = g_strdup(X509_verify_cert_error_string(
267 X509_STORE_CTX_get_error(&store_ctx)));
268 debug_print("Can't check signer: %s\n", err_msg);
269 X509_STORE_CTX_cleanup (&store_ctx);
270 X509_STORE_free (store);
274 X509_STORE_CTX_cleanup (&store_ctx);
275 X509_STORE_free (store);
279 gboolean ssl_certificate_check (X509 *x509_cert, gchar *host, gushort port)
281 SSLCertificate *current_cert = ssl_certificate_new(x509_cert, host, port);
282 SSLCertificate *known_cert;
284 if (current_cert == NULL) {
285 debug_print("Buggy certificate !\n");
289 known_cert = ssl_certificate_find (host, port);
291 if (known_cert == NULL) {
293 gchar *err_msg, *cur_cert_str;
295 cur_cert_str = ssl_certificate_to_string(current_cert);
297 err_msg = g_strdup_printf(_("%s presented an unknown SSL certificate:\n%s"),
300 g_free (cur_cert_str);
302 if (prefs_common.no_recv_err_panel) {
303 log_error(_("%s\n\nMail won't be retrieved on this account until you save the certificate.\n(Uncheck the \"%s\" preference).\n"),
305 _("Don't popup error dialog on receive error"));
310 val = alertpanel(_("Warning"),
312 _("Accept and save"), _("Cancel connection"), NULL);
316 case G_ALERTALTERNATE:
317 ssl_certificate_destroy(current_cert);
320 ssl_certificate_save(current_cert);
321 ssl_certificate_destroy(current_cert);
325 else if (!ssl_certificate_compare (current_cert, known_cert)) {
327 gchar *err_msg, *known_cert_str, *cur_cert_str, *sig_status;
329 sig_status = ssl_certificate_check_signer(x509_cert);
331 known_cert_str = ssl_certificate_to_string(known_cert);
332 cur_cert_str = ssl_certificate_to_string(current_cert);
333 err_msg = g_strdup_printf(_("%s's SSL certificate changed !\nWe have saved this one:\n%s\n\nIt is now:\n%s\n\nThis could mean the server answering is not the known one."),
337 g_free (cur_cert_str);
338 g_free (known_cert_str);
342 if (prefs_common.no_recv_err_panel) {
343 log_error(_("%s\n\nMail won't be retrieved on this account until you save the certificate.\n(Uncheck the \"%s\" preference).\n"),
345 _("Don't popup error dialog on receive error"));
350 val = alertpanel(_("Warning"),
352 _("Accept and save"), _("Cancel connection"), NULL);
356 case G_ALERTALTERNATE:
357 ssl_certificate_destroy(current_cert);
358 ssl_certificate_destroy(known_cert);
361 ssl_certificate_save(current_cert);
362 ssl_certificate_destroy(current_cert);
363 ssl_certificate_destroy(known_cert);
368 ssl_certificate_destroy(current_cert);
369 ssl_certificate_destroy(known_cert);