summaryrefslogtreecommitdiff
path: root/src/sslmanager.c
diff options
context:
space:
mode:
authorSimeon Simeonov2018-02-26 11:23:00 +0100
committerSimeon Simeonov2018-02-26 11:23:00 +0100
commit0b3cbf57875fd692e4ba0b336fefa4bee1ed00dc (patch)
treeaf916a30553c78ce9d4f2d8658656a175c5b6921 /src/sslmanager.c
Initial commit for sylpheed 3.7.0
Diffstat (limited to 'src/sslmanager.c')
-rw-r--r--src/sslmanager.c225
1 files changed, 225 insertions, 0 deletions
diff --git a/src/sslmanager.c b/src/sslmanager.c
new file mode 100644
index 0000000..a633710
--- /dev/null
+++ b/src/sslmanager.c
@@ -0,0 +1,225 @@
1/*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2014 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_SSL
25
26#include "defs.h"
27
28#include <glib.h>
29#include <glib/gi18n.h>
30#include <gtk/gtkdialog.h>
31#include <gtk/gtkhbox.h>
32#include <gtk/gtkvbox.h>
33#include <gtk/gtkstock.h>
34#include <gtk/gtkimage.h>
35#include <gtk/gtklabel.h>
36
37#include "ssl.h"
38#include "sslmanager.h"
39#include "manage_window.h"
40#include "prefs_common.h"
41
42gint ssl_manager_verify_cert(SockInfo *sockinfo, const gchar *hostname,
43 X509 *server_cert, glong verify_result)
44{
45 static PangoFontDescription *font_desc;
46 GtkWidget *dialog;
47 GtkWidget *hbox;
48 GtkWidget *image;
49 GtkWidget *vbox;
50 GtkWidget *label;
51 const gchar *title;
52 GString *message;
53 gchar *subject, *issuer;
54 guchar keyid[EVP_MAX_MD_SIZE];
55 gchar keyidstr[EVP_MAX_MD_SIZE * 3 + 1] = "";
56 guint keyidlen = 0;
57 gchar *sha1_keyidstr, *md5_keyidstr;
58 BIO *bio;
59 gchar not_before[64] = "", not_after[64] = "";
60 gint i;
61 gint result;
62 gboolean disable_always = FALSE;
63
64 if (verify_result == X509_V_OK)
65 return 0;
66
67 gdk_threads_enter();
68
69 title = _("SSL certificate verify failed");
70
71 subject = X509_NAME_oneline(X509_get_subject_name(server_cert),
72 NULL, 0);
73 issuer = X509_NAME_oneline(X509_get_issuer_name(server_cert), NULL, 0);
74
75 bio = BIO_new(BIO_s_mem());
76 ASN1_TIME_print(bio, X509_get_notBefore(server_cert));
77 BIO_gets(bio, not_before, sizeof(not_before));
78 BIO_reset(bio);
79 ASN1_TIME_print(bio, X509_get_notAfter(server_cert));
80 BIO_gets(bio, not_after, sizeof(not_after));
81 BIO_free(bio);
82
83 if (X509_digest(server_cert, EVP_sha1(), keyid, &keyidlen)) {
84 for (i = 0; i < keyidlen; i++)
85 g_snprintf(keyidstr + i * 3, 4, "%02x:", keyid[i]);
86 keyidstr[keyidlen * 3 - 1] = '\0';
87 sha1_keyidstr = g_ascii_strup(keyidstr, -1);
88 } else {
89 sha1_keyidstr = g_strdup("(cannot calculate digest)");
90 }
91 if (X509_digest(server_cert, EVP_md5(), keyid, &keyidlen)) {
92 for (i = 0; i < keyidlen; i++)
93 g_snprintf(keyidstr + i * 3, 4, "%02x:", keyid[i]);
94 keyidstr[keyidlen * 3 - 1] = '\0';
95 md5_keyidstr = g_ascii_strup(keyidstr, -1);
96 } else {
97 md5_keyidstr = g_strdup("(cannot calculate digest)");
98 }
99
100 message = g_string_new("");
101 g_string_append_printf(message, _("The SSL certificate of %s cannot be verified by the following reason:"), hostname);
102 if (verify_result == X509_V_ERR_APPLICATION_VERIFICATION) {
103 g_string_append_printf(message, "\n certificate hostname does not match\n\n");
104 } else {
105 g_string_append_printf(message, "\n %s\n\n", X509_verify_cert_error_string(verify_result));
106 }
107 g_string_append_printf(message, _("Subject: %s\n"), subject ? subject : "(unknown)");
108 g_string_append_printf(message, _("Issuer: %s\n"), issuer ? issuer : "(unknown)");
109 g_string_append_printf(message, _("Issued date: %s\n"), not_before);
110 g_string_append_printf(message, _("Expire date: %s\n"), not_after);
111 g_string_append(message, "\n");
112 g_string_append_printf(message, _("SHA1 fingerprint: %s\n"), sha1_keyidstr);
113 g_string_append_printf(message, _("MD5 fingerprint: %s\n"), md5_keyidstr);
114 g_string_append(message, "\n");
115 g_string_append(message, _("Do you accept this certificate?"));
116 g_free(md5_keyidstr);
117 g_free(sha1_keyidstr);
118 if (issuer)
119 OPENSSL_free(issuer);
120 if (subject)
121 OPENSSL_free(subject);
122
123 dialog = gtk_dialog_new();
124 gtk_window_set_title(GTK_WINDOW(dialog), title);
125 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
126 gtk_window_set_position(GTK_WINDOW(dialog),
127 GTK_WIN_POS_CENTER_ON_PARENT);
128 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
129 manage_window_set_transient(GTK_WINDOW(dialog));
130 gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
131 gtk_widget_realize(dialog);
132
133 hbox = gtk_hbox_new(FALSE, 12);
134 gtk_container_set_border_width(GTK_CONTAINER(hbox), 12);
135 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
136 hbox, FALSE, FALSE, 0);
137
138 image = gtk_image_new_from_stock
139 (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG);
140
141 gtk_misc_set_alignment(GTK_MISC(image), 0.5, 0.0);
142 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
143
144 vbox = gtk_vbox_new(FALSE, 12);
145 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
146
147 label = gtk_label_new(title);
148 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
149 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
150 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
151
152 if (!font_desc) {
153 gint size;
154
155 size = pango_font_description_get_size
156 (label->style->font_desc);
157 font_desc = pango_font_description_new();
158 pango_font_description_set_weight
159 (font_desc, PANGO_WEIGHT_BOLD);
160 pango_font_description_set_size
161 (font_desc, size * PANGO_SCALE_LARGE);
162 }
163 if (font_desc)
164 gtk_widget_modify_font(label, font_desc);
165
166 label = gtk_label_new(message->str);
167 g_string_free(message, TRUE);
168 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
169 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
170 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
171 gtk_label_set_selectable(GTK_LABEL(label), TRUE);
172 GTK_WIDGET_UNSET_FLAGS(label, GTK_CAN_FOCUS);
173#ifdef G_OS_WIN32
174 {
175 GtkStyle *style;
176 style = gtk_widget_get_style(dialog);
177 gtk_widget_modify_base(label, GTK_STATE_ACTIVE,
178 &style->base[GTK_STATE_SELECTED]);
179 gtk_widget_modify_text(label, GTK_STATE_ACTIVE,
180 &style->text[GTK_STATE_SELECTED]);
181 }
182#endif
183
184 /* prohibit acception of expired certificates */
185 if (verify_result == X509_V_ERR_CERT_HAS_EXPIRED)
186 disable_always = TRUE;
187
188 if (prefs_common.comply_gnome_hig)
189 gtk_dialog_add_buttons(GTK_DIALOG(dialog),
190 _("_Reject"), GTK_RESPONSE_REJECT,
191 _("_Temporarily accept"), GTK_RESPONSE_OK,
192 _("Always _accept"), GTK_RESPONSE_ACCEPT,
193 NULL);
194 else
195 gtk_dialog_add_buttons(GTK_DIALOG(dialog),
196 _("Always _accept"), GTK_RESPONSE_ACCEPT,
197 _("_Temporarily accept"), GTK_RESPONSE_OK,
198 _("_Reject"), GTK_RESPONSE_REJECT,
199 NULL);
200 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
201 if (disable_always)
202 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog),
203 GTK_RESPONSE_ACCEPT, FALSE);
204
205 gtk_widget_show_all(dialog);
206
207 result = gtk_dialog_run(GTK_DIALOG(dialog));
208 gtk_widget_destroy(dialog);
209
210 gdk_threads_leave();
211
212 switch (result) {
213 case GTK_RESPONSE_ACCEPT:
214 return 0;
215 case GTK_RESPONSE_OK:
216 return 1;
217 case GTK_RESPONSE_REJECT:
218 default:
219 break;
220 }
221
222 return -1;
223}
224
225#endif /* USE_SSL */