e828a654350aae012f1ef52de4452e40e6e380ba
[claws.git] / src / gtk / sslcertwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #if USE_OPENSSL
25
26 #include <openssl/ssl.h>
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30
31 #include "ssl_certificate.h"
32 #include "utils.h"
33 #include "alertpanel.h"
34 #include "hooks.h"
35
36 static void toggle_cert_cb(GtkWidget    *widget,
37                          gpointer        data);
38 gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert);
39 gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert);
40
41 GtkWidget *cert_presenter(SSLCertificate *cert)
42 {
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;
52         char buf[100];
53         char *issuer_commonname, *issuer_location, *issuer_organization;
54         char *subject_commonname, *subject_location, *subject_organization;
55         char *fingerprint, *sig_status;
56         unsigned int n;
57         unsigned char md[EVP_MAX_MD_SIZE];      
58         
59         /* issuer */    
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);
63         else
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);
74         else
75                 issuer_location = g_strdup(_("<not in certificate>"));
76
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);
80         else 
81                 issuer_organization = g_strdup(_("<not in certificate>"));
82          
83         /* subject */   
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);
87         else
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);
98         else
99                 subject_location = g_strdup(_("<not in certificate>"));
100
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);
104         else 
105                 subject_organization = g_strdup(_("<not in certificate>"));
106          
107         /* fingerprint */
108         X509_digest(cert->x509_cert, EVP_md5(), md, &n);
109         fingerprint = readable_fingerprint(md, (int)n);
110
111         /* signature */
112         sig_status = ssl_certificate_check_signer(cert->x509_cert);
113
114         if (sig_status==NULL)
115                 sig_status = g_strdup(_("correct"));
116
117         vbox = gtk_vbox_new(FALSE, 5);
118         hbox = gtk_hbox_new(FALSE, 5);
119         
120         frame_owner  = gtk_frame_new(_("Owner"));
121         frame_signer = gtk_frame_new(_("Signer"));
122         frame_status = gtk_frame_new(_("Status"));
123         
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));
127         
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);
134         
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);
141         
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);
148
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);
155         
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);
162         
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);
169
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);
182         
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));
186         
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);
191         
192         gtk_widget_show_all(vbox);
193         
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);
200         g_free(fingerprint);
201         g_free(sig_status);
202         
203         return vbox;
204 }
205
206 static gboolean sslcert_ask_hook(gpointer source, gpointer data)
207 {
208         SSLCertHookData *hookdata = (SSLCertHookData *)source;
209         if (hookdata == NULL) {
210                 return FALSE;
211         }
212         if (hookdata->old_cert == NULL)
213                 hookdata->accept = sslcertwindow_ask_new_cert(hookdata->cert);
214         else
215                 hookdata->accept = sslcertwindow_ask_changed_cert(hookdata->old_cert, hookdata->cert);
216
217         return TRUE;
218 }
219
220 void sslcertwindow_register_hook(void)
221 {
222         hooks_register_hook(SSLCERT_ASK_HOOKLIST, sslcert_ask_hook, NULL);
223 }
224
225 void sslcertwindow_show_cert(SSLCertificate *cert)
226 {
227         GtkWidget *cert_widget = cert_presenter(cert);
228         gchar *buf;
229         
230         buf = g_strdup_printf(_("SSL certificate for %s"), cert->host);
231         alertpanel_with_type(buf, NULL, _("OK"), NULL, NULL, cert_widget, ALERT_NOTICE);
232         g_free(buf);
233 }
234
235 static void toggle_cert_cb(GtkWidget    *widget,
236                          gpointer        data)
237 {
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);
244                 } else
245                         gtk_widget_show(cert_widget);
246         } else
247                 gtk_widget_hide(cert_widget);
248 }
249
250 gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert)
251 {
252         gchar *buf, *sig_status;
253         AlertValue val;
254         GtkWidget *vbox;
255         GtkWidget *label;
256         GtkWidget *button;
257         GtkWidget *cert_widget;
258         
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);
264         g_free(buf);
265         
266         sig_status = ssl_certificate_check_signer(cert->x509_cert);
267
268         if (sig_status==NULL)
269                 sig_status = g_strdup(_("correct"));
270
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);
275         g_free(buf);
276         g_free(sig_status);
277         
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);
283
284         val = alertpanel_with_type(_("Unknown SSL Certificate"), NULL, _("Accept and save"), _("Cancel connection"), NULL, vbox, ALERT_QUESTION);
285         
286         return (val == G_ALERTDEFAULT);
287 }
288
289 gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert)
290 {
291         GtkWidget *old_cert_widget = cert_presenter(old_cert);
292         GtkWidget *new_cert_widget = cert_presenter(new_cert);
293         GtkWidget *vbox;
294         gchar *buf, *sig_status;
295         GtkWidget *vbox2;
296         GtkWidget *label;
297         GtkWidget *button;
298         AlertValue val;
299         
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);
311         
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);
317         g_free(buf);
318         
319         sig_status = ssl_certificate_check_signer(new_cert->x509_cert);
320
321         if (sig_status==NULL)
322                 sig_status = g_strdup(_("correct"));
323
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);
328         g_free(buf);
329         g_free(sig_status);
330         
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);
335
336         val = alertpanel_with_type(_("Changed SSL Certificate"), NULL, _("Accept and save"), _("Cancel connection"), NULL, vbox2, ALERT_WARNING);
337         
338         return (val == G_ALERTDEFAULT);
339 }
340 #endif