62ddd9beeb8cd122463f97b2f9aacf919276e040
[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 gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert);
37 gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert);
38
39 GtkWidget *cert_presenter(SSLCertificate *cert)
40 {
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;
50         char buf[100];
51         char *issuer_commonname, *issuer_location, *issuer_organization;
52         char *subject_commonname, *subject_location, *subject_organization;
53         char *fingerprint, *sig_status;
54         unsigned int n;
55         unsigned char md[EVP_MAX_MD_SIZE];      
56         
57         /* issuer */    
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);
61         else
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);
72         else
73                 issuer_location = g_strdup(_("<not in certificate>"));
74
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);
78         else 
79                 issuer_organization = g_strdup(_("<not in certificate>"));
80          
81         /* subject */   
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);
85         else
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);
96         else
97                 subject_location = g_strdup(_("<not in certificate>"));
98
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);
102         else 
103                 subject_organization = g_strdup(_("<not in certificate>"));
104          
105         /* fingerprint */
106         X509_digest(cert->x509_cert, EVP_md5(), md, &n);
107         fingerprint = readable_fingerprint(md, (int)n);
108
109         /* signature */
110         sig_status = ssl_certificate_check_signer(cert->x509_cert);
111
112         if (sig_status==NULL)
113                 sig_status = g_strdup(_("correct"));
114
115         vbox = gtk_vbox_new(FALSE, 5);
116         hbox = gtk_hbox_new(FALSE, 5);
117         
118         frame_owner  = gtk_frame_new(_("Owner"));
119         frame_signer = gtk_frame_new(_("Signer"));
120         frame_status = gtk_frame_new(_("Status"));
121         
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));
125         
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);
132         
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);
139         
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);
146
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);
153         
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);
160         
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);
167
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);
180         
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));
184         
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);
189         
190         gtk_widget_show_all(vbox);
191         
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);
198         g_free(fingerprint);
199         g_free(sig_status);
200         
201         return vbox;
202 }
203
204 static gboolean sslcert_ask_hook(gpointer source, gpointer data)
205 {
206         SSLCertHookData *hookdata = (SSLCertHookData *)source;
207         if (hookdata == NULL) {
208                 return FALSE;
209         }
210         if (hookdata->old_cert == NULL)
211                 hookdata->accept = sslcertwindow_ask_new_cert(hookdata->cert);
212         else
213                 hookdata->accept = sslcertwindow_ask_changed_cert(hookdata->old_cert, hookdata->cert);
214
215         return TRUE;
216 }
217
218 void sslcertwindow_register_hook(void)
219 {
220         hooks_register_hook(SSLCERT_ASK_HOOKLIST, sslcert_ask_hook, NULL);
221 }
222
223 void sslcertwindow_show_cert(SSLCertificate *cert)
224 {
225         GtkWidget *cert_widget = cert_presenter(cert);
226         gchar *buf;
227         
228         buf = g_strdup_printf(_("SSL certificate for %s"), cert->host);
229         alertpanel_with_type(buf, NULL, _("OK"), NULL, NULL, cert_widget, ALERT_NOTICE);
230         g_free(buf);
231 }
232
233 gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert)
234 {
235         gchar *buf, *sig_status;
236         AlertValue val;
237         GtkWidget *vbox;
238         GtkWidget *label;
239         GtkWidget *button;
240         GtkWidget *cert_widget;
241         
242         vbox = gtk_vbox_new(FALSE, 5);
243         buf = g_strdup_printf(_("Certificate for %s is unknown. Do you want to accept it?"), cert->host);
244         label = gtk_label_new(buf);
245         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
246         gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
247         g_free(buf);
248         
249         sig_status = ssl_certificate_check_signer(cert->x509_cert);
250
251         if (sig_status==NULL)
252                 sig_status = g_strdup(_("correct"));
253
254         buf = g_strdup_printf(_("Signature status: %s"), sig_status);
255         label = gtk_label_new(buf);
256         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
257         gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
258         g_free(buf);
259         g_free(sig_status);
260         
261         button = gtk_expander_new_with_mnemonic(_("_View certificate"));
262         gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
263         cert_widget = cert_presenter(cert);
264         gtk_container_add(GTK_CONTAINER(button), cert_widget);
265         val = alertpanel_with_type(_("Unknown SSL Certificate"), NULL, _("Accept and save"), _("Cancel connection"), NULL, vbox, ALERT_QUESTION);
266         
267         return (val == G_ALERTDEFAULT);
268 }
269
270 gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert)
271 {
272         GtkWidget *old_cert_widget = cert_presenter(old_cert);
273         GtkWidget *new_cert_widget = cert_presenter(new_cert);
274         GtkWidget *vbox;
275         gchar *buf, *sig_status;
276         GtkWidget *vbox2;
277         GtkWidget *label;
278         GtkWidget *button;
279         AlertValue val;
280         
281         vbox = gtk_vbox_new(FALSE, 5);
282         label = gtk_label_new(_("New certificate:"));
283         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
284         gtk_box_pack_end(GTK_BOX(vbox), new_cert_widget, TRUE, TRUE, 0);
285         gtk_box_pack_end(GTK_BOX(vbox), label, TRUE, TRUE, 0);
286         gtk_box_pack_end(GTK_BOX(vbox), gtk_hseparator_new(), TRUE, TRUE, 0);
287         label = gtk_label_new(_("Known certificate:"));
288         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
289         gtk_box_pack_end(GTK_BOX(vbox), old_cert_widget, TRUE, TRUE, 0);
290         gtk_box_pack_end(GTK_BOX(vbox), label, TRUE, TRUE, 0);
291         gtk_widget_show_all(vbox);
292         
293         vbox2 = gtk_vbox_new(FALSE, 5);
294         buf = g_strdup_printf(_("Certificate for %s has changed. Do you want to accept it?"), new_cert->host);
295         label = gtk_label_new(buf);
296         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
297         gtk_box_pack_start(GTK_BOX(vbox2), label, TRUE, TRUE, 0);
298         g_free(buf);
299         
300         sig_status = ssl_certificate_check_signer(new_cert->x509_cert);
301
302         if (sig_status==NULL)
303                 sig_status = g_strdup(_("correct"));
304
305         buf = g_strdup_printf(_("Signature status: %s"), sig_status);
306         label = gtk_label_new(buf);
307         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
308         gtk_box_pack_start(GTK_BOX(vbox2), label, TRUE, TRUE, 0);
309         g_free(buf);
310         g_free(sig_status);
311         
312         button = gtk_expander_new_with_mnemonic(_("_View certificates"));
313         gtk_box_pack_start(GTK_BOX(vbox2), button, FALSE, FALSE, 0);
314         gtk_container_add(GTK_CONTAINER(button), vbox);
315
316         val = alertpanel_with_type(_("Changed SSL Certificate"), NULL, _("Accept and save"), _("Cancel connection"), NULL, vbox2, ALERT_WARNING);
317         
318         return (val == G_ALERTDEFAULT);
319 }
320 #endif