0.8.6claws75
[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 <gtk/gtk.h>
29 #include "../common/intl.h"
30 #include "../ssl_certificate.h"
31 #include "../common/utils.h"
32 #include "../alertpanel.h"
33
34 GtkWidget *cert_presenter(SSLCertificate *cert)
35 {
36         GtkWidget *vbox = NULL;
37         GtkWidget *hbox = NULL;
38         GtkWidget *frame_owner = NULL;
39         GtkWidget *frame_signer = NULL;
40         GtkWidget *frame_status = NULL;
41         GtkTable *owner_table = NULL;
42         GtkTable *signer_table = NULL;
43         GtkTable *status_table = NULL;
44         GtkWidget *label = NULL;
45         char *ret, buf[100];
46         char *issuer_commonname, *issuer_location, *issuer_organization;
47         char *subject_commonname, *subject_location, *subject_organization;
48         char *fingerprint, *sig_status;
49         unsigned int n;
50         unsigned char md[EVP_MAX_MD_SIZE];      
51         
52         /* issuer */    
53         if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert), 
54                                        NID_commonName, buf, 100) >= 0)
55                 issuer_commonname = g_strdup(buf);
56         else
57                 issuer_commonname = g_strdup(_("<not in certificate>"));
58         if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert), 
59                                        NID_localityName, buf, 100) >= 0) {
60                 issuer_location = g_strdup(buf);
61                 if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert), 
62                                        NID_countryName, buf, 100) >= 0)
63                         issuer_location = g_strconcat(issuer_location,", ",buf, NULL);
64         } else if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert), 
65                                        NID_countryName, buf, 100) >= 0)
66                 issuer_location = g_strdup(buf);
67         else
68                 issuer_location = g_strdup(_("<not in certificate>"));
69
70         if (X509_NAME_get_text_by_NID(X509_get_issuer_name(cert->x509_cert), 
71                                        NID_organizationName, buf, 100) >= 0)
72                 issuer_organization = g_strdup(buf);
73         else 
74                 issuer_organization = g_strdup(_("<not in certificate>"));
75          
76         /* subject */   
77         if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert), 
78                                        NID_commonName, buf, 100) >= 0)
79                 subject_commonname = g_strdup(buf);
80         else
81                 subject_commonname = g_strdup(_("<not in certificate>"));
82         if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert), 
83                                        NID_localityName, buf, 100) >= 0) {
84                 subject_location = g_strdup(buf);
85                 if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert), 
86                                        NID_countryName, buf, 100) >= 0)
87                         subject_location = g_strconcat(subject_location,", ",buf, NULL);
88         } else if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert), 
89                                        NID_countryName, buf, 100) >= 0)
90                 subject_location = g_strdup(buf);
91         else
92                 subject_location = g_strdup(_("<not in certificate>"));
93
94         if (X509_NAME_get_text_by_NID(X509_get_subject_name(cert->x509_cert), 
95                                        NID_organizationName, buf, 100) >= 0)
96                 subject_organization = g_strdup(buf);
97         else 
98                 subject_organization = g_strdup(_("<not in certificate>"));
99          
100         /* fingerprint */
101         X509_digest(cert->x509_cert, EVP_md5(), md, &n);
102         fingerprint = readable_fingerprint(md, (int)n);
103
104         /* signature */
105         sig_status = ssl_certificate_check_signer(cert->x509_cert);
106
107         if (sig_status==NULL)
108                 sig_status = g_strdup(_("correct"));
109
110         vbox = gtk_vbox_new(FALSE, 5);
111         hbox = gtk_hbox_new(FALSE, 5);
112         
113         frame_owner  = gtk_frame_new(_("Owner"));
114         frame_signer = gtk_frame_new(_("Signer"));
115         frame_status = gtk_frame_new(_("Status"));
116         
117         owner_table = GTK_TABLE(gtk_table_new(3, 2, FALSE));
118         signer_table = GTK_TABLE(gtk_table_new(3, 2, FALSE));
119         status_table = GTK_TABLE(gtk_table_new(2, 2, FALSE));
120         
121         label = gtk_label_new(_("Name: "));
122         gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
123         gtk_table_attach(owner_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
124         label = gtk_label_new(subject_commonname);
125         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
126         gtk_table_attach(owner_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
127         
128         label = gtk_label_new(_("Organization: "));
129         gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
130         gtk_table_attach(owner_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
131         label = gtk_label_new(subject_organization);
132         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
133         gtk_table_attach(owner_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
134         
135         label = gtk_label_new(_("Location: "));
136         gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
137         gtk_table_attach(owner_table, label, 0, 1, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
138         label = gtk_label_new(subject_location);
139         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
140         gtk_table_attach(owner_table, label, 1, 2, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
141
142         label = gtk_label_new(_("Name: "));
143         gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
144         gtk_table_attach(signer_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
145         label = gtk_label_new(issuer_commonname);
146         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
147         gtk_table_attach(signer_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
148         
149         label = gtk_label_new(_("Organization: "));
150         gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
151         gtk_table_attach(signer_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
152         label = gtk_label_new(issuer_organization);
153         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
154         gtk_table_attach(signer_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
155         
156         label = gtk_label_new(_("Location: "));
157         gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
158         gtk_table_attach(signer_table, label, 0, 1, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
159         label = gtk_label_new(issuer_location);
160         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
161         gtk_table_attach(signer_table, label, 1, 2, 2, 3, GTK_EXPAND|GTK_FILL, 0, 0, 0);
162
163         label = gtk_label_new(_("Fingerprint: "));
164         gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
165         gtk_table_attach(status_table, label, 0, 1, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
166         label = gtk_label_new(fingerprint);
167         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
168         gtk_table_attach(status_table, label, 1, 2, 0, 1, GTK_EXPAND|GTK_FILL, 0, 0, 0);
169         label = gtk_label_new(_("Signature status: "));
170         gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
171         gtk_table_attach(status_table, label, 0, 1, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
172         label = gtk_label_new(sig_status);
173         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
174         gtk_table_attach(status_table, label, 1, 2, 1, 2, GTK_EXPAND|GTK_FILL, 0, 0, 0);
175         
176         gtk_container_add(GTK_CONTAINER(frame_owner), GTK_WIDGET(owner_table));
177         gtk_container_add(GTK_CONTAINER(frame_signer), GTK_WIDGET(signer_table));
178         gtk_container_add(GTK_CONTAINER(frame_status), GTK_WIDGET(status_table));
179         
180         gtk_box_pack_end(GTK_BOX(hbox), frame_signer, TRUE, TRUE, 0);
181         gtk_box_pack_end(GTK_BOX(hbox), frame_owner, TRUE, TRUE, 0);
182         gtk_box_pack_end(GTK_BOX(vbox), frame_status, TRUE, TRUE, 0);
183         gtk_box_pack_end(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
184         
185         gtk_widget_show_all(vbox);
186         
187         g_free(issuer_commonname);
188         g_free(issuer_location);
189         g_free(issuer_organization);
190         g_free(subject_commonname);
191         g_free(subject_location);
192         g_free(subject_organization);
193         g_free(fingerprint);
194         g_free(sig_status);
195         
196         return vbox;
197 }
198
199 void sslcertwindow_show_cert(SSLCertificate *cert)
200 {
201         GtkWidget *cert_widget = cert_presenter(cert);
202         gchar *buf;
203         
204         buf = g_strdup_printf(_("SSL certificate for %s"), cert->host);
205         alertpanel_with_widget(buf, NULL, _("OK"), NULL, NULL, cert_widget);
206         g_free(buf);
207 }
208
209 gboolean sslcertwindow_ask_new_cert(SSLCertificate *cert)
210 {
211         GtkWidget *cert_widget = cert_presenter(cert);
212         gchar *buf;
213         AlertValue val;
214         buf = g_strdup_printf(_("Do you want to accept SSL certificate for %s?"), cert->host);
215         val = alertpanel_with_widget(_("Unknown SSL Certificate"), buf, _("Accept and save"), _("Cancel connection"), NULL, cert_widget);
216         g_free(buf);
217         return (val == G_ALERTDEFAULT);
218 }
219
220 gboolean sslcertwindow_ask_changed_cert(SSLCertificate *old_cert, SSLCertificate *new_cert)
221 {
222         GtkWidget *old_cert_widget = cert_presenter(old_cert);
223         GtkWidget *new_cert_widget = cert_presenter(new_cert);
224         GtkWidget *vbox;
225         GtkWidget *label;
226         gchar *buf;
227         AlertValue val;
228         
229         vbox = gtk_vbox_new(FALSE, 5);
230         label = gtk_label_new(_("New certificate:"));
231         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
232         gtk_box_pack_end(GTK_BOX(vbox), new_cert_widget, TRUE, TRUE, 0);
233         gtk_box_pack_end(GTK_BOX(vbox), label, TRUE, TRUE, 0);
234         gtk_box_pack_end(GTK_BOX(vbox), gtk_hseparator_new(), TRUE, TRUE, 0);
235         label = gtk_label_new(_("Known certificate:"));
236         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
237         gtk_box_pack_end(GTK_BOX(vbox), old_cert_widget, TRUE, TRUE, 0);
238         gtk_box_pack_end(GTK_BOX(vbox), label, TRUE, TRUE, 0);
239         gtk_widget_show_all(vbox);
240         
241         buf = g_strdup_printf(_("Do you want to accept new SSL certificate for %s?"), new_cert->host);
242         val = alertpanel_with_widget(_("Changed SSL Certificate"), buf, _("Accept and save"), _("Cancel connection"), NULL, vbox);
243         g_free(buf);
244         return (val == G_ALERTDEFAULT);
245 }
246 #endif