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 <glib/gi18n.h>
31 #include "ssl_certificate.h"
33 #include "alertpanel.h"
36 static void toggle_cert_cb(GtkWidget *widget,
38 gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert);
39 gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert);
41 GtkWidget *cert_presenter(SSLCertificate *cert)
43 GtkWidget *vbox = NULL;
44 GtkWidget *hbox = NULL;
45 GtkWidget *frame_owner = NULL;
46 GtkWidget *frame_signer = NULL;
47 GtkWidget *frame_status = NULL;
48 GtkTable *owner_table = NULL;
49 GtkTable *signer_table = NULL;
50 GtkTable *status_table = NULL;
51 GtkWidget *label = NULL;
53 char *issuer_commonname, *issuer_location, *issuer_organization;
54 char *subject_commonname, *subject_location, *subject_organization;
55 char *fingerprint, *sig_status;
57 unsigned char md[EVP_MAX_MD_SIZE];
60 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
61 NID_commonName, buf, 100) >= 0)
62 issuer_commonname = g_strdup(buf);
64 issuer_commonname = g_strdup(_("<not in certificate>"));
65 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
66 NID_localityName, buf, 100) >= 0) {
67 issuer_location = g_strdup(buf);
68 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
69 NID_countryName, buf, 100) >= 0)
70 issuer_location = g_strconcat(issuer_location,", ",buf, NULL);
71 } else if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
72 NID_countryName, buf, 100) >= 0)
73 issuer_location = g_strdup(buf);
75 issuer_location = g_strdup(_("<not in certificate>"));
77 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
78 NID_organizationName, buf, 100) >= 0)
79 issuer_organization = g_strdup(buf);
81 issuer_organization = g_strdup(_("<not in certificate>"));
84 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
85 NID_commonName, buf, 100) >= 0)
86 subject_commonname = g_strdup(buf);
88 subject_commonname = g_strdup(_("<not in certificate>"));
89 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
90 NID_localityName, buf, 100) >= 0) {
91 subject_location = g_strdup(buf);
92 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
93 NID_countryName, buf, 100) >= 0)
94 subject_location = g_strconcat(subject_location,", ",buf, NULL);
95 } else if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
96 NID_countryName, buf, 100) >= 0)
97 subject_location = g_strdup(buf);
99 subject_location = g_strdup(_("<not in certificate>"));
101 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
102 NID_organizationName, buf, 100) >= 0)
103 subject_organization = g_strdup(buf);
105 subject_organization = g_strdup(_("<not in certificate>"));
108 X509_digest(cert->x509_cert, EVP_md5(), md, &n);
109 fingerprint = readable_fingerprint(md, (int)n);
112 sig_status = ssl_certificate_check_signer(cert->x509_cert);
114 if (sig_status==NULL)
115 sig_status = g_strdup(_("correct"));
117 vbox = gtk_vbox_new(FALSE, 5);
118 hbox = gtk_hbox_new(FALSE, 5);
120 frame_owner = gtk_frame_new(_("Owner"));
121 frame_signer = gtk_frame_new(_("Signer"));
122 frame_status = gtk_frame_new(_("Status"));
124 owner_table = GTK_TABLE(gtk_table_new(3, 2, FALSE));
125 signer_table = GTK_TABLE(gtk_table_new(3, 2, FALSE));
126 status_table = GTK_TABLE(gtk_table_new(2, 2, FALSE));
128 label = gtk_label_new(_("Name: "));
129 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
130 gtk_table_attach(owner_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
131 label = gtk_label_new(subject_commonname);
132 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
133 gtk_table_attach(owner_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
135 label = gtk_label_new(_("Organization: "));
136 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
137 gtk_table_attach(owner_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
138 label = gtk_label_new(subject_organization);
139 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
140 gtk_table_attach(owner_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
142 label = gtk_label_new(_("Location: "));
143 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
144 gtk_table_attach(owner_table, label, 0, 1, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
145 label = gtk_label_new(subject_location);
146 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
147 gtk_table_attach(owner_table, label, 1, 2, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
149 label = gtk_label_new(_("Name: "));
150 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
151 gtk_table_attach(signer_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
152 label = gtk_label_new(issuer_commonname);
153 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
154 gtk_table_attach(signer_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
156 label = gtk_label_new(_("Organization: "));
157 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
158 gtk_table_attach(signer_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
159 label = gtk_label_new(issuer_organization);
160 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
161 gtk_table_attach(signer_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
163 label = gtk_label_new(_("Location: "));
164 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
165 gtk_table_attach(signer_table, label, 0, 1, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
166 label = gtk_label_new(issuer_location);
167 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
168 gtk_table_attach(signer_table, label, 1, 2, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
170 label = gtk_label_new(_("Fingerprint: "));
171 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
172 gtk_table_attach(status_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
173 label = gtk_label_new(fingerprint);
174 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
175 gtk_table_attach(status_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
176 label = gtk_label_new(_("Signature status: "));
177 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
178 gtk_table_attach(status_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
179 label = gtk_label_new(sig_status);
180 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
181 gtk_table_attach(status_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
183 gtk_container_add(GTK_CONTAINER(frame_owner), GTK_WIDGET(owner_table));
184 gtk_container_add(GTK_CONTAINER(frame_signer), GTK_WIDGET(signer_table));
185 gtk_container_add(GTK_CONTAINER(frame_status), GTK_WIDGET(status_table));
187 gtk_box_pack_end(GTK_BOX(hbox), frame_signer, TRUE, TRUE, 0);
188 gtk_box_pack_end(GTK_BOX(hbox), frame_owner, TRUE, TRUE, 0);
189 gtk_box_pack_end(GTK_BOX(vbox), frame_status, TRUE, TRUE, 0);
190 gtk_box_pack_end(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
192 gtk_widget_show_all(vbox);
194 g_free(issuer_commonname);
195 g_free(issuer_location);
196 g_free(issuer_organization);
197 g_free(subject_commonname);
198 g_free(subject_location);
199 g_free(subject_organization);
206 static gboolean sslcert_ask_hook(gpointer source, gpointer data)
208 SSLCertHookData *hookdata = (SSLCertHookData *)source;
209 if (hookdata == NULL) {
212 if (hookdata->old_cert == NULL)
213 hookdata->accept = sslcertwindow_ask_new_cert(hookdata->cert);
215 hookdata->accept = sslcertwindow_ask_changed_cert(hookdata->old_cert, hookdata->cert);
220 void sslcertwindow_register_hook(void)
222 hooks_register_hook(SSLCERT_ASK_HOOKLIST, sslcert_ask_hook, NULL);
225 void sslcertwindow_show_cert(SSLCertificate *cert)
227 GtkWidget *cert_widget = cert_presenter(cert);
230 buf = g_strdup_printf(_("SSL certificate for %s"), cert->host);
231 alertpanel_with_type(buf, NULL, _("OK"), NULL, NULL, cert_widget, ALERT_NOTICE);
235 static void toggle_cert_cb(GtkWidget *widget,
238 GtkWidget *cert_widget = GTK_WIDGET(data);
239 GtkWidget *box = widget->parent;
240 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
241 if(cert_widget->parent == NULL) {
242 gtk_box_pack_start(GTK_BOX(box), cert_widget, TRUE, TRUE, 0);
243 gtk_widget_show(cert_widget);
245 gtk_widget_show(cert_widget);
247 gtk_widget_hide(cert_widget);
250 gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert)
252 gchar *buf, *sig_status;
257 GtkWidget *cert_widget;
259 vbox = gtk_vbox_new(FALSE, 5);
260 buf = g_strdup_printf(_("Certificate for %s is unknown. Do you want to accept it?"), cert->host);
261 label = gtk_label_new(buf);
262 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
263 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
266 sig_status = ssl_certificate_check_signer(cert->x509_cert);
268 if (sig_status==NULL)
269 sig_status = g_strdup(_("correct"));
271 buf = g_strdup_printf(_("Signature status: %s"), sig_status);
272 label = gtk_label_new(buf);
273 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
274 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
278 button = gtk_toggle_button_new_with_label(_("View certificate"));
279 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
280 cert_widget = cert_presenter(cert);
281 g_signal_connect(G_OBJECT(button), "toggled",
282 G_CALLBACK(toggle_cert_cb), cert_widget);
284 val = alertpanel_with_type(_("Unknown SSL Certificate"), NULL, _("Accept and save"), _("Cancel connection"), NULL, vbox, ALERT_QUESTION);
286 return (val == G_ALERTDEFAULT);
289 gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert)
291 GtkWidget *old_cert_widget = cert_presenter(old_cert);
292 GtkWidget *new_cert_widget = cert_presenter(new_cert);
294 gchar *buf, *sig_status;
300 vbox = gtk_vbox_new(FALSE, 5);
301 label = gtk_label_new(_("New certificate:"));
302 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
303 gtk_box_pack_end(GTK_BOX(vbox), new_cert_widget, TRUE, TRUE, 0);
304 gtk_box_pack_end(GTK_BOX(vbox), label, TRUE, TRUE, 0);
305 gtk_box_pack_end(GTK_BOX(vbox), gtk_hseparator_new(), TRUE, TRUE, 0);
306 label = gtk_label_new(_("Known certificate:"));
307 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
308 gtk_box_pack_end(GTK_BOX(vbox), old_cert_widget, TRUE, TRUE, 0);
309 gtk_box_pack_end(GTK_BOX(vbox), label, TRUE, TRUE, 0);
310 gtk_widget_show_all(vbox);
312 vbox2 = gtk_vbox_new(FALSE, 5);
313 buf = g_strdup_printf(_("Certificate for %s has changed. Do you want to accept it?"), new_cert->host);
314 label = gtk_label_new(buf);
315 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
316 gtk_box_pack_start(GTK_BOX(vbox2), label, TRUE, TRUE, 0);
319 sig_status = ssl_certificate_check_signer(new_cert->x509_cert);
321 if (sig_status==NULL)
322 sig_status = g_strdup(_("correct"));
324 buf = g_strdup_printf(_("Signature status: %s"), sig_status);
325 label = gtk_label_new(buf);
326 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
327 gtk_box_pack_start(GTK_BOX(vbox2), label, TRUE, TRUE, 0);
331 button = gtk_toggle_button_new_with_label(_("View certificates"));
332 gtk_box_pack_start(GTK_BOX(vbox2), button, FALSE, FALSE, 0);
333 g_signal_connect(G_OBJECT(button), "toggled",
334 G_CALLBACK(toggle_cert_cb), vbox);
336 val = alertpanel_with_type(_("Changed SSL Certificate"), NULL, _("Accept and save"), _("Cancel connection"), NULL, vbox2, ALERT_WARNING);
338 return (val == G_ALERTDEFAULT);