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 gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert);
37 gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert);
39 GtkWidget *cert_presenter(SSLCertificate *cert)
41 GtkWidget *vbox = NULL;
42 GtkWidget *hbox = NULL;
43 GtkWidget *frame_owner = NULL;
44 GtkWidget *frame_signer = NULL;
45 GtkWidget *frame_status = NULL;
46 GtkTable *owner_table = NULL;
47 GtkTable *signer_table = NULL;
48 GtkTable *status_table = NULL;
49 GtkWidget *label = NULL;
51 char *issuer_commonname, *issuer_location, *issuer_organization;
52 char *subject_commonname, *subject_location, *subject_organization;
53 char *fingerprint, *sig_status;
55 unsigned char md[EVP_MAX_MD_SIZE];
58 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
59 NID_commonName, buf, 100) >= 0)
60 issuer_commonname = g_strdup(buf);
62 issuer_commonname = g_strdup(_("<not in certificate>"));
63 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
64 NID_localityName, buf, 100) >= 0) {
65 issuer_location = g_strdup(buf);
66 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
67 NID_countryName, buf, 100) >= 0)
68 issuer_location = g_strconcat(issuer_location,", ",buf, NULL);
69 } else if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
70 NID_countryName, buf, 100) >= 0)
71 issuer_location = g_strdup(buf);
73 issuer_location = g_strdup(_("<not in certificate>"));
75 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert),
76 NID_organizationName, buf, 100) >= 0)
77 issuer_organization = g_strdup(buf);
79 issuer_organization = g_strdup(_("<not in certificate>"));
82 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
83 NID_commonName, buf, 100) >= 0)
84 subject_commonname = g_strdup(buf);
86 subject_commonname = g_strdup(_("<not in certificate>"));
87 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
88 NID_localityName, buf, 100) >= 0) {
89 subject_location = g_strdup(buf);
90 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
91 NID_countryName, buf, 100) >= 0)
92 subject_location = g_strconcat(subject_location,", ",buf, NULL);
93 } else if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
94 NID_countryName, buf, 100) >= 0)
95 subject_location = g_strdup(buf);
97 subject_location = g_strdup(_("<not in certificate>"));
99 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert),
100 NID_organizationName, buf, 100) >= 0)
101 subject_organization = g_strdup(buf);
103 subject_organization = g_strdup(_("<not in certificate>"));
106 X509_digest(cert->x509_cert, EVP_md5(), md, &n);
107 fingerprint = readable_fingerprint(md, (int)n);
110 sig_status = ssl_certificate_check_signer(cert->x509_cert);
112 if (sig_status==NULL)
113 sig_status = g_strdup(_("correct"));
115 vbox = gtk_vbox_new(FALSE, 5);
116 hbox = gtk_hbox_new(FALSE, 5);
118 frame_owner = gtk_frame_new(_("Owner"));
119 frame_signer = gtk_frame_new(_("Signer"));
120 frame_status = gtk_frame_new(_("Status"));
122 owner_table = GTK_TABLE(gtk_table_new(3, 2, FALSE));
123 signer_table = GTK_TABLE(gtk_table_new(3, 2, FALSE));
124 status_table = GTK_TABLE(gtk_table_new(2, 2, FALSE));
126 label = gtk_label_new(_("Name: "));
127 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
128 gtk_table_attach(owner_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
129 label = gtk_label_new(subject_commonname);
130 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
131 gtk_table_attach(owner_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
133 label = gtk_label_new(_("Organization: "));
134 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
135 gtk_table_attach(owner_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
136 label = gtk_label_new(subject_organization);
137 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
138 gtk_table_attach(owner_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
140 label = gtk_label_new(_("Location: "));
141 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
142 gtk_table_attach(owner_table, label, 0, 1, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
143 label = gtk_label_new(subject_location);
144 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
145 gtk_table_attach(owner_table, label, 1, 2, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
147 label = gtk_label_new(_("Name: "));
148 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
149 gtk_table_attach(signer_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
150 label = gtk_label_new(issuer_commonname);
151 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
152 gtk_table_attach(signer_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
154 label = gtk_label_new(_("Organization: "));
155 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
156 gtk_table_attach(signer_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
157 label = gtk_label_new(issuer_organization);
158 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
159 gtk_table_attach(signer_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
161 label = gtk_label_new(_("Location: "));
162 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
163 gtk_table_attach(signer_table, label, 0, 1, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
164 label = gtk_label_new(issuer_location);
165 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
166 gtk_table_attach(signer_table, label, 1, 2, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
168 label = gtk_label_new(_("Fingerprint: "));
169 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
170 gtk_table_attach(status_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
171 label = gtk_label_new(fingerprint);
172 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
173 gtk_table_attach(status_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
174 label = gtk_label_new(_("Signature status: "));
175 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
176 gtk_table_attach(status_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
177 label = gtk_label_new(sig_status);
178 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
179 gtk_table_attach(status_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
181 gtk_container_add(GTK_CONTAINER(frame_owner), GTK_WIDGET(owner_table));
182 gtk_container_add(GTK_CONTAINER(frame_signer), GTK_WIDGET(signer_table));
183 gtk_container_add(GTK_CONTAINER(frame_status), GTK_WIDGET(status_table));
185 gtk_box_pack_end(GTK_BOX(hbox), frame_signer, TRUE, TRUE, 0);
186 gtk_box_pack_end(GTK_BOX(hbox), frame_owner, TRUE, TRUE, 0);
187 gtk_box_pack_end(GTK_BOX(vbox), frame_status, TRUE, TRUE, 0);
188 gtk_box_pack_end(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
190 gtk_widget_show_all(vbox);
192 g_free(issuer_commonname);
193 g_free(issuer_location);
194 g_free(issuer_organization);
195 g_free(subject_commonname);
196 g_free(subject_location);
197 g_free(subject_organization);
204 static gboolean sslcert_ask_hook(gpointer source, gpointer data)
206 SSLCertHookData *hookdata = (SSLCertHookData *)source;
207 if (hookdata == NULL) {
210 if (hookdata->old_cert == NULL)
211 hookdata->accept = sslcertwindow_ask_new_cert(hookdata->cert);
213 hookdata->accept = sslcertwindow_ask_changed_cert(hookdata->old_cert, hookdata->cert);
218 void sslcertwindow_register_hook(void)
220 hooks_register_hook(SSLCERT_ASK_HOOKLIST, sslcert_ask_hook, NULL);
223 void sslcertwindow_show_cert(SSLCertificate *cert)
225 GtkWidget *cert_widget = cert_presenter(cert);
228 buf = g_strdup_printf(_("SSL certificate for %s"), cert->host);
229 alertpanel_full(buf, NULL, GTK_STOCK_CLOSE, NULL, NULL,
230 FALSE, cert_widget, ALERT_NOTICE, G_ALERTDEFAULT);
234 gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert)
236 gchar *buf, *sig_status;
241 GtkWidget *cert_widget;
243 vbox = gtk_vbox_new(FALSE, 5);
244 buf = g_strdup_printf(_("Certificate for %s is unknown.\nDo you want to accept it?"), cert->host);
245 label = gtk_label_new(buf);
246 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
247 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
250 sig_status = ssl_certificate_check_signer(cert->x509_cert);
252 if (sig_status==NULL)
253 sig_status = g_strdup(_("correct"));
255 buf = g_strdup_printf(_("Signature status: %s"), sig_status);
256 label = gtk_label_new(buf);
257 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
258 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
262 button = gtk_expander_new_with_mnemonic(_("_View certificate"));
263 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
264 cert_widget = cert_presenter(cert);
265 gtk_container_add(GTK_CONTAINER(button), cert_widget);
267 val = alertpanel_full(_("Unknown SSL Certificate"), NULL,
268 _("Accept and save"), _("Cancel connection"), NULL,
269 FALSE, vbox, ALERT_QUESTION, G_ALERTDEFAULT);
271 return (val == G_ALERTDEFAULT);
274 gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert)
276 GtkWidget *old_cert_widget = cert_presenter(old_cert);
277 GtkWidget *new_cert_widget = cert_presenter(new_cert);
279 gchar *buf, *sig_status;
285 vbox = gtk_vbox_new(FALSE, 5);
286 label = gtk_label_new(_("New certificate:"));
287 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
288 gtk_box_pack_end(GTK_BOX(vbox), new_cert_widget, TRUE, TRUE, 0);
289 gtk_box_pack_end(GTK_BOX(vbox), label, TRUE, TRUE, 0);
290 gtk_box_pack_end(GTK_BOX(vbox), gtk_hseparator_new(), TRUE, TRUE, 0);
291 label = gtk_label_new(_("Known certificate:"));
292 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
293 gtk_box_pack_end(GTK_BOX(vbox), old_cert_widget, TRUE, TRUE, 0);
294 gtk_box_pack_end(GTK_BOX(vbox), label, TRUE, TRUE, 0);
295 gtk_widget_show_all(vbox);
297 vbox2 = gtk_vbox_new(FALSE, 5);
298 buf = g_strdup_printf(_("Certificate for %s has changed. Do you want to accept it?"), new_cert->host);
299 label = gtk_label_new(buf);
300 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
301 gtk_box_pack_start(GTK_BOX(vbox2), label, TRUE, TRUE, 0);
304 sig_status = ssl_certificate_check_signer(new_cert->x509_cert);
306 if (sig_status==NULL)
307 sig_status = g_strdup(_("correct"));
309 buf = g_strdup_printf(_("Signature status: %s"), sig_status);
310 label = gtk_label_new(buf);
311 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
312 gtk_box_pack_start(GTK_BOX(vbox2), label, TRUE, TRUE, 0);
316 button = gtk_expander_new_with_mnemonic(_("_View certificates"));
317 gtk_box_pack_start(GTK_BOX(vbox2), button, FALSE, FALSE, 0);
318 gtk_container_add(GTK_CONTAINER(button), vbox);
320 val = alertpanel_full(_("Changed SSL Certificate"), NULL,
321 _("Accept and save"), _("Cancel connection"), NULL,
322 FALSE, vbox2, ALERT_WARNING, G_ALERTDEFAULT);
324 return (val == G_ALERTDEFAULT);