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>
32 #include "prefs_common.h"
33 #include "ssl_certificate.h"
35 #include "alertpanel.h"
38 static gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert);
39 static gboolean sslcertwindow_ask_expired_cert(SSLCertificate *cert);
40 static gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert);
42 static GtkWidget *cert_presenter(SSLCertificate *cert)
44 GtkWidget *vbox = NULL;
45 GtkWidget *hbox = NULL;
46 GtkWidget *frame_owner = NULL;
47 GtkWidget *frame_signer = NULL;
48 GtkWidget *frame_status = NULL;
49 GtkTable *owner_table = NULL;
50 GtkTable *signer_table = NULL;
51 GtkTable *status_table = NULL;
52 GtkWidget *label = NULL;
55 char *issuer_commonname, *issuer_location, *issuer_organization;
56 char *subject_commonname, *subject_location, *subject_organization;
57 char *fingerprint, *sig_status, *exp_date;
59 unsigned char md[EVP_MAX_MD_SIZE];
65 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
66 NID_commonName, buf, 100) >= 0)
67 issuer_commonname = g_strdup(buf);
69 issuer_commonname = g_strdup(_("<not in certificate>"));
70 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
71 NID_localityName, buf, 100) >= 0) {
72 issuer_location = g_strdup(buf);
73 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
74 NID_countryName, buf, 100) >= 0)
75 issuer_location = g_strconcat(issuer_location,", ",buf, NULL);
76 } else if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
77 NID_countryName, buf, 100) >= 0)
78 issuer_location = g_strdup(buf);
80 issuer_location = g_strdup(_("<not in certificate>"));
82 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
83 NID_organizationName, buf, 100) >= 0)
84 issuer_organization = g_strdup(buf);
86 issuer_organization = g_strdup(_("<not in certificate>"));
89 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
90 NID_commonName, buf, 100) >= 0)
91 subject_commonname = g_strdup(buf);
93 subject_commonname = g_strdup(_("<not in certificate>"));
94 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
95 NID_localityName, buf, 100) >= 0) {
96 subject_location = g_strdup(buf);
97 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
98 NID_countryName, buf, 100) >= 0)
99 subject_location = g_strconcat(subject_location,", ",buf, NULL);
100 } else if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
101 NID_countryName, buf, 100) >= 0)
102 subject_location = g_strdup(buf);
104 subject_location = g_strdup(_("<not in certificate>"));
106 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
107 NID_organizationName, buf, 100) >= 0)
108 subject_organization = g_strdup(buf);
110 subject_organization = g_strdup(_("<not in certificate>"));
112 if ((validity = X509_get_notAfter(cert->x509_cert)) != NULL) {
113 exp_time_t = asn1toTime(validity);
115 exp_time_t = (time_t)0;
118 memset(buf, 0, sizeof(buf));
119 strftime(buf, sizeof(buf)-1, prefs_common.date_format, localtime_r(&exp_time_t, <));
120 exp_date = buf? g_strdup(buf):g_strdup("?");
123 X509_digest(cert->x509_cert, EVP_md5(), md, &n);
124 fingerprint = readable_fingerprint(md, (int)n);
127 sig_status = ssl_certificate_check_signer(cert->x509_cert);
129 if (sig_status==NULL)
130 sig_status = g_strdup(_("correct"));
132 vbox = gtk_vbox_new(FALSE, 5);
133 hbox = gtk_hbox_new(FALSE, 5);
135 frame_owner = gtk_frame_new(_("Owner"));
136 frame_signer = gtk_frame_new(_("Signer"));
137 frame_status = gtk_frame_new(_("Status"));
139 owner_table = GTK_TABLE(gtk_table_new(3, 2, FALSE));
140 signer_table = GTK_TABLE(gtk_table_new(3, 2, FALSE));
141 status_table = GTK_TABLE(gtk_table_new(3, 2, FALSE));
143 label = gtk_label_new(_("Name: "));
144 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
145 gtk_table_attach(owner_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
146 label = gtk_label_new(subject_commonname);
147 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
148 gtk_table_attach(owner_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
150 label = gtk_label_new(_("Organization: "));
151 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
152 gtk_table_attach(owner_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
153 label = gtk_label_new(subject_organization);
154 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
155 gtk_table_attach(owner_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
157 label = gtk_label_new(_("Location: "));
158 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
159 gtk_table_attach(owner_table, label, 0, 1, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
160 label = gtk_label_new(subject_location);
161 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
162 gtk_table_attach(owner_table, label, 1, 2, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
164 label = gtk_label_new(_("Name: "));
165 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
166 gtk_table_attach(signer_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
167 label = gtk_label_new(issuer_commonname);
168 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
169 gtk_table_attach(signer_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
171 label = gtk_label_new(_("Organization: "));
172 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
173 gtk_table_attach(signer_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
174 label = gtk_label_new(issuer_organization);
175 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
176 gtk_table_attach(signer_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
178 label = gtk_label_new(_("Location: "));
179 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
180 gtk_table_attach(signer_table, label, 0, 1, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
181 label = gtk_label_new(issuer_location);
182 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
183 gtk_table_attach(signer_table, label, 1, 2, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
185 label = gtk_label_new(_("Fingerprint: "));
186 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
187 gtk_table_attach(status_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
188 label = gtk_label_new(fingerprint);
189 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
190 gtk_table_attach(status_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
191 label = gtk_label_new(_("Signature status: "));
192 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
193 gtk_table_attach(status_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
194 label = gtk_label_new(sig_status);
195 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
196 gtk_table_attach(status_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
197 label = gtk_label_new(_("Expires on: "));
198 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
199 gtk_table_attach(status_table, label, 0, 1, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
200 label = gtk_label_new(exp_date);
201 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
202 gtk_table_attach(status_table, label, 1, 2, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
204 gtk_container_add(GTK_CONTAINER(frame_owner), GTK_WIDGET(owner_table));
205 gtk_container_add(GTK_CONTAINER(frame_signer), GTK_WIDGET(signer_table));
206 gtk_container_add(GTK_CONTAINER(frame_status), GTK_WIDGET(status_table));
208 gtk_box_pack_end(GTK_BOX(hbox), frame_signer, TRUE, TRUE, 0);
209 gtk_box_pack_end(GTK_BOX(hbox), frame_owner, TRUE, TRUE, 0);
210 gtk_box_pack_end(GTK_BOX(vbox), frame_status, TRUE, TRUE, 0);
211 gtk_box_pack_end(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
213 gtk_widget_show_all(vbox);
215 g_free(issuer_commonname);
216 g_free(issuer_location);
217 g_free(issuer_organization);
218 g_free(subject_commonname);
219 g_free(subject_location);
220 g_free(subject_organization);
227 static gboolean sslcert_ask_hook(gpointer source, gpointer data)
229 SSLCertHookData *hookdata = (SSLCertHookData *)source;
231 if (prefs_common.skip_ssl_cert_check)
234 if (hookdata == NULL) {
237 if (hookdata->old_cert == NULL) {
238 if (hookdata->expired)
239 hookdata->accept = sslcertwindow_ask_expired_cert(hookdata->cert);
241 hookdata->accept = sslcertwindow_ask_new_cert(hookdata->cert);
243 hookdata->accept = sslcertwindow_ask_changed_cert(hookdata->old_cert, hookdata->cert);
248 void sslcertwindow_register_hook(void)
250 hooks_register_hook(SSLCERT_ASK_HOOKLIST, sslcert_ask_hook, NULL);
253 void sslcertwindow_show_cert(SSLCertificate *cert)
255 GtkWidget *cert_widget = cert_presenter(cert);
258 buf = g_strdup_printf(_("SSL certificate for %s"), cert->host);
259 alertpanel_full(buf, NULL, GTK_STOCK_CLOSE, NULL, NULL,
260 FALSE, cert_widget, ALERT_NOTICE, G_ALERTDEFAULT);
264 static gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert)
266 gchar *buf, *sig_status;
271 GtkWidget *cert_widget;
273 vbox = gtk_vbox_new(FALSE, 5);
274 buf = g_strdup_printf(_("Certificate for %s is unknown.\nDo you want to accept it?"), cert->host);
275 label = gtk_label_new(buf);
276 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
277 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
280 sig_status = ssl_certificate_check_signer(cert->x509_cert);
282 if (sig_status==NULL)
283 sig_status = g_strdup(_("correct"));
285 buf = g_strdup_printf(_("Signature status: %s"), sig_status);
286 label = gtk_label_new(buf);
287 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
288 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
292 button = gtk_expander_new_with_mnemonic(_("_View certificate"));
293 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
294 cert_widget = cert_presenter(cert);
295 gtk_container_add(GTK_CONTAINER(button), cert_widget);
297 val = alertpanel_full(_("Unknown SSL Certificate"), NULL,
298 _("_Cancel connection"), _("_Accept and save"), NULL,
299 FALSE, vbox, ALERT_QUESTION, G_ALERTDEFAULT);
301 return (val == G_ALERTALTERNATE);
304 static gboolean sslcertwindow_ask_expired_cert(SSLCertificate *cert)
306 gchar *buf, *sig_status;
311 GtkWidget *cert_widget;
313 vbox = gtk_vbox_new(FALSE, 5);
314 buf = g_strdup_printf(_("Certificate for %s is expired.\nDo you want to continue?"), cert->host);
315 label = gtk_label_new(buf);
316 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
317 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
320 sig_status = ssl_certificate_check_signer(cert->x509_cert);
322 if (sig_status==NULL)
323 sig_status = g_strdup(_("correct"));
325 buf = g_strdup_printf(_("Signature status: %s"), sig_status);
326 label = gtk_label_new(buf);
327 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
328 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
332 button = gtk_expander_new_with_mnemonic(_("_View certificate"));
333 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
334 cert_widget = cert_presenter(cert);
335 gtk_container_add(GTK_CONTAINER(button), cert_widget);
337 val = alertpanel_full(_("Expired SSL Certificate"), NULL,
338 _("_Cancel connection"), _("_Accept"), NULL,
339 FALSE, vbox, ALERT_QUESTION, G_ALERTDEFAULT);
341 return (val == G_ALERTALTERNATE);
344 static gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert)
346 GtkWidget *old_cert_widget = cert_presenter(old_cert);
347 GtkWidget *new_cert_widget = cert_presenter(new_cert);
349 gchar *buf, *sig_status;
355 vbox = gtk_vbox_new(FALSE, 5);
356 label = gtk_label_new(_("New certificate:"));
357 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
358 gtk_box_pack_end(GTK_BOX(vbox), new_cert_widget, TRUE, TRUE, 0);
359 gtk_box_pack_end(GTK_BOX(vbox), label, TRUE, TRUE, 0);
360 gtk_box_pack_end(GTK_BOX(vbox), gtk_hseparator_new(), TRUE, TRUE, 0);
361 label = gtk_label_new(_("Known certificate:"));
362 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
363 gtk_box_pack_end(GTK_BOX(vbox), old_cert_widget, TRUE, TRUE, 0);
364 gtk_box_pack_end(GTK_BOX(vbox), label, TRUE, TRUE, 0);
365 gtk_widget_show_all(vbox);
367 vbox2 = gtk_vbox_new(FALSE, 5);
368 buf = g_strdup_printf(_("Certificate for %s has changed. Do you want to accept it?"), new_cert->host);
369 label = gtk_label_new(buf);
370 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
371 gtk_box_pack_start(GTK_BOX(vbox2), label, TRUE, TRUE, 0);
374 sig_status = ssl_certificate_check_signer(new_cert->x509_cert);
376 if (sig_status==NULL)
377 sig_status = g_strdup(_("correct"));
379 buf = g_strdup_printf(_("Signature status: %s"), sig_status);
380 label = gtk_label_new(buf);
381 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
382 gtk_box_pack_start(GTK_BOX(vbox2), label, TRUE, TRUE, 0);
386 button = gtk_expander_new_with_mnemonic(_("_View certificates"));
387 gtk_box_pack_start(GTK_BOX(vbox2), button, FALSE, FALSE, 0);
388 gtk_container_add(GTK_CONTAINER(button), vbox);
390 val = alertpanel_full(_("Changed SSL Certificate"), NULL,
391 _("_Cancel connection"), _("_Accept and save"), NULL,
392 FALSE, vbox2, ALERT_WARNING, G_ALERTDEFAULT);
394 return (val == G_ALERTALTERNATE);