6ba8f21ddf54a92ed6fb4a4e268618cdddafbcb9
[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_full(buf, NULL, GTK_STOCK_CLOSE, NULL, NULL,
230                         FALSE, cert_widget, ALERT_NOTICE, G_ALERTDEFAULT);
231         g_free(buf);
232 }
233
234 gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert)
235 {
236         gchar *buf, *sig_status;
237         AlertValue val;
238         GtkWidget *vbox;
239         GtkWidget *label;
240         GtkWidget *button;
241         GtkWidget *cert_widget;
242         
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);
248         g_free(buf);
249         
250         sig_status = ssl_certificate_check_signer(cert->x509_cert);
251
252         if (sig_status==NULL)
253                 sig_status = g_strdup(_("correct"));
254
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);
259         g_free(buf);
260         g_free(sig_status);
261         
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);
266
267         val = alertpanel_full(_("Unknown SSL Certificate"), NULL,
268                               _("Accept and save"), _("Cancel connection"), NULL,
269                               FALSE, vbox, ALERT_QUESTION, G_ALERTDEFAULT);
270         
271         return (val == G_ALERTDEFAULT);
272 }
273
274 gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert)
275 {
276         GtkWidget *old_cert_widget = cert_presenter(old_cert);
277         GtkWidget *new_cert_widget = cert_presenter(new_cert);
278         GtkWidget *vbox;
279         gchar *buf, *sig_status;
280         GtkWidget *vbox2;
281         GtkWidget *label;
282         GtkWidget *button;
283         AlertValue val;
284         
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);
296         
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);
302         g_free(buf);
303         
304         sig_status = ssl_certificate_check_signer(new_cert->x509_cert);
305
306         if (sig_status==NULL)
307                 sig_status = g_strdup(_("correct"));
308
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);
313         g_free(buf);
314         g_free(sig_status);
315         
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);
319
320         val = alertpanel_full(_("Changed SSL Certificate"), NULL,
321                               _("Accept and save"), _("Cancel connection"), NULL,
322                               FALSE, vbox2, ALERT_WARNING, G_ALERTDEFAULT);
323         
324         return (val == G_ALERTDEFAULT);
325 }
326 #endif