]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-tls-dialog.c
Merge remote-tracking branch 'glassrose/add-All-service-selection-in-debug-window'
[empathy.git] / libempathy-gtk / empathy-tls-dialog.c
1 /*
2  * empathy-tls-dialog.c - Source for EmpathyTLSDialog
3  * Copyright (C) 2010 Collabora Ltd.
4  * @author Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include <config.h>
22
23 #include "empathy-tls-dialog.h"
24
25 #include <glib/gi18n-lib.h>
26 #include <gcr/gcr.h>
27 #include <telepathy-glib/util.h>
28
29 #include <gcr/gcr.h>
30
31 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
32 #include <libempathy/empathy-debug.h>
33 #include <libempathy/empathy-utils.h>
34
35 G_DEFINE_TYPE (EmpathyTLSDialog, empathy_tls_dialog,
36     GTK_TYPE_MESSAGE_DIALOG)
37
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTLSDialog);
39
40 enum {
41   PROP_TLS_CERTIFICATE = 1,
42   PROP_REASON,
43   PROP_REMEMBER,
44   PROP_DETAILS,
45
46   LAST_PROPERTY,
47 };
48
49 typedef struct {
50   EmpathyTLSCertificate *certificate;
51   EmpTLSCertificateRejectReason reason;
52   GHashTable *details;
53
54   gboolean remember;
55
56   gboolean dispose_run;
57 } EmpathyTLSDialogPriv;
58
59 static void
60 empathy_tls_dialog_get_property (GObject *object,
61     guint property_id,
62     GValue *value,
63     GParamSpec *pspec)
64 {
65   EmpathyTLSDialogPriv *priv = GET_PRIV (object);
66
67   switch (property_id)
68     {
69     case PROP_TLS_CERTIFICATE:
70       g_value_set_object (value, priv->certificate);
71       break;
72     case PROP_REASON:
73       g_value_set_uint (value, priv->reason);
74       break;
75     case PROP_REMEMBER:
76       g_value_set_boolean (value, priv->remember);
77       break;
78     case PROP_DETAILS:
79       g_value_set_boxed (value, priv->details);
80       break;
81     default:
82       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
83       break;
84     }
85 }
86
87 static void
88 empathy_tls_dialog_set_property (GObject *object,
89     guint property_id,
90     const GValue *value,
91     GParamSpec *pspec)
92 {
93   EmpathyTLSDialogPriv *priv = GET_PRIV (object);
94
95   switch (property_id)
96     {
97     case PROP_TLS_CERTIFICATE:
98       priv->certificate = g_value_dup_object (value);
99       break;
100     case PROP_REASON:
101       priv->reason = g_value_get_uint (value);
102       break;
103     case PROP_DETAILS:
104       priv->details = g_value_dup_boxed (value);
105       break;
106     default:
107       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
108       break;
109     }
110 }
111
112 static void
113 empathy_tls_dialog_dispose (GObject *object)
114 {
115   EmpathyTLSDialogPriv *priv = GET_PRIV (object);
116
117   if (priv->dispose_run)
118     return;
119
120   priv->dispose_run = TRUE;
121
122   tp_clear_object (&priv->certificate);
123
124   G_OBJECT_CLASS (empathy_tls_dialog_parent_class)->dispose (object);
125 }
126
127 static void
128 empathy_tls_dialog_finalize (GObject *object)
129 {
130   EmpathyTLSDialogPriv *priv = GET_PRIV (object);
131
132   tp_clear_boxed (G_TYPE_HASH_TABLE, &priv->details);
133
134   G_OBJECT_CLASS (empathy_tls_dialog_parent_class)->finalize (object);
135 }
136
137 static gchar *
138 reason_to_string (EmpathyTLSDialog *self)
139 {
140   GString *str;
141   const gchar *reason_str;
142   EmpTLSCertificateRejectReason reason;
143   GHashTable *details;
144   EmpathyTLSDialogPriv *priv = GET_PRIV (self);
145
146   str = g_string_new (NULL);
147   reason = priv->reason;
148   details = priv->details;
149
150   g_string_append (str, _("The identity provided by the chat server cannot be "
151           "verified."));
152   g_string_append (str, "\n\n");
153
154   switch (reason)
155     {
156     case EMP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED:
157       reason_str = _("The certificate is not signed by a Certification "
158           "Authority.");
159       break;
160     case EMP_TLS_CERTIFICATE_REJECT_REASON_EXPIRED:
161       reason_str = _("The certificate has expired.");
162       break;
163     case EMP_TLS_CERTIFICATE_REJECT_REASON_NOT_ACTIVATED:
164       reason_str = _("The certificate hasn't yet been activated.");
165       break;
166     case EMP_TLS_CERTIFICATE_REJECT_REASON_FINGERPRINT_MISMATCH:
167       reason_str = _("The certificate does not have the expected fingerprint.");
168       break;
169     case EMP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH:
170       reason_str = _("The hostname verified by the certificate doesn't match "
171           "the server name.");
172       break;
173     case EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED:
174       reason_str = _("The certificate is self-signed.");
175       break;
176     case EMP_TLS_CERTIFICATE_REJECT_REASON_REVOKED:
177       reason_str = _("The certificate has been revoked by the issuing "
178           "Certification Authority.");
179       break;
180     case EMP_TLS_CERTIFICATE_REJECT_REASON_INSECURE:
181       reason_str = _("The certificate is cryptographically weak.");
182       break;
183     case EMP_TLS_CERTIFICATE_REJECT_REASON_LIMIT_EXCEEDED:
184       reason_str = _("The certificate length exceeds verifiable limits.");
185       break;
186     case EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN:
187     default:
188       reason_str = _("The certificate is malformed.");
189       break;
190     }
191
192   g_string_append (str, reason_str);
193
194   /* add more information in case of HOSTNAME_MISMATCH */
195   if (reason == EMP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH)
196     {
197       const gchar *expected_hostname, *certificate_hostname;
198
199       expected_hostname = tp_asv_get_string (details, "expected-hostname");
200       certificate_hostname = tp_asv_get_string (details,
201           "certificate-hostname");
202
203       if (expected_hostname != NULL && certificate_hostname != NULL)
204         {
205           g_string_append (str, "\n\n");
206           g_string_append_printf (str, _("Expected hostname: %s"),
207               expected_hostname);
208           g_string_append (str, "\n");
209           g_string_append_printf (str, _("Certificate hostname: %s"),
210               certificate_hostname);
211         }
212     }
213
214   return g_string_free (str, FALSE);
215 }
216
217 static GtkWidget *
218 build_gcr_widget (EmpathyTLSDialog *self)
219 {
220   GcrCertificateWidget *widget;
221   GcrCertificate *certificate;
222   GPtrArray *cert_chain = NULL;
223   GArray *first_cert;
224   int height;
225   EmpathyTLSDialogPriv *priv = GET_PRIV (self);
226
227   g_object_get (priv->certificate,
228       "cert-data", &cert_chain,
229       NULL);
230   first_cert = g_ptr_array_index (cert_chain, 0);
231
232   certificate = gcr_simple_certificate_new ((const guchar *) first_cert->data,
233       first_cert->len);
234   widget = gcr_certificate_widget_new (certificate);
235
236   /* FIXME: make this widget bigger by default -- GTK+ should really handle
237    * this sort of thing for us */
238   gtk_widget_get_preferred_height (GTK_WIDGET (widget), NULL, &height);
239   /* force the widget to at least 150 pixels high */
240   gtk_widget_set_size_request (GTK_WIDGET (widget), -1, MAX (height, 150));
241
242   g_object_unref (certificate);
243   g_ptr_array_unref (cert_chain);
244
245   return GTK_WIDGET (widget);
246 }
247
248 static void
249 checkbox_toggled_cb (GtkToggleButton *checkbox,
250     gpointer user_data)
251 {
252   EmpathyTLSDialog *self = user_data;
253   EmpathyTLSDialogPriv *priv = GET_PRIV (self);
254
255   priv->remember = gtk_toggle_button_get_active (checkbox);
256   g_object_notify (G_OBJECT (self), "remember");
257 }
258
259 static void
260 certificate_invalidated_cb (EmpathyTLSCertificate *certificate,
261     guint domain,
262     gint code,
263     gchar *message,
264     EmpathyTLSDialog *self)
265 {
266   gtk_widget_destroy (GTK_WIDGET (self));
267 }
268
269 static void
270 empathy_tls_dialog_constructed (GObject *object)
271 {
272   GtkWidget *content_area, *expander, *details, *checkbox;
273   gchar *text;
274   EmpathyTLSDialog *self = EMPATHY_TLS_DIALOG (object);
275   GtkMessageDialog *message_dialog = GTK_MESSAGE_DIALOG (self);
276   GtkDialog *dialog = GTK_DIALOG (self);
277   EmpathyTLSDialogPriv *priv = GET_PRIV (self);
278
279   gtk_dialog_add_buttons (dialog,
280       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
281       _("C_ontinue"), GTK_RESPONSE_YES,
282       NULL);
283
284   text = reason_to_string (self);
285
286   g_object_set (message_dialog,
287       "title", _("Untrusted connection"),
288       "text", _("This connection is untrusted. Would you like to "
289           "continue anyway?"),
290       "secondary-text", text,
291       NULL);
292
293   g_free (text);
294
295   content_area = gtk_dialog_get_content_area (dialog);
296
297   checkbox = gtk_check_button_new_with_label (
298       _("Remember this choice for future connections"));
299   gtk_box_pack_end (GTK_BOX (content_area), checkbox, FALSE, FALSE, 0);
300   gtk_widget_show (checkbox);
301   g_signal_connect (checkbox, "toggled", G_CALLBACK (checkbox_toggled_cb),
302       self);
303
304   text = g_strdup_printf ("<b>%s</b>", _("Certificate Details"));
305   expander = gtk_expander_new (text);
306   gtk_expander_set_use_markup (GTK_EXPANDER (expander), TRUE);
307   gtk_box_pack_end (GTK_BOX (content_area), expander, TRUE, TRUE, 0);
308   gtk_widget_show (expander);
309
310   g_free (text);
311
312   details = build_gcr_widget (self);
313   gtk_container_add (GTK_CONTAINER (expander), details);
314   gtk_widget_show (details);
315
316   gtk_window_set_keep_above (GTK_WINDOW (self), TRUE);
317
318   tp_g_signal_connect_object (priv->certificate, "invalidated",
319       G_CALLBACK (certificate_invalidated_cb), self, 0);
320 }
321
322 static void
323 empathy_tls_dialog_init (EmpathyTLSDialog *self)
324 {
325   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
326       EMPATHY_TYPE_TLS_DIALOG, EmpathyTLSDialogPriv);
327 }
328
329 static void
330 empathy_tls_dialog_class_init (EmpathyTLSDialogClass *klass)
331 {
332   GParamSpec *pspec;
333   GObjectClass *oclass = G_OBJECT_CLASS (klass);
334
335   g_type_class_add_private (klass, sizeof (EmpathyTLSDialogPriv));
336
337   oclass->set_property = empathy_tls_dialog_set_property;
338   oclass->get_property = empathy_tls_dialog_get_property;
339   oclass->dispose = empathy_tls_dialog_dispose;
340   oclass->finalize = empathy_tls_dialog_finalize;
341   oclass->constructed = empathy_tls_dialog_constructed;
342
343   pspec = g_param_spec_object ("certificate", "The EmpathyTLSCertificate",
344       "The EmpathyTLSCertificate to be displayed.",
345       EMPATHY_TYPE_TLS_CERTIFICATE,
346       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
347   g_object_class_install_property (oclass, PROP_TLS_CERTIFICATE, pspec);
348
349   pspec = g_param_spec_uint ("reason", "The reason",
350       "The reason why the certificate is being asked for confirmation.",
351       0, NUM_EMP_TLS_CERTIFICATE_REJECT_REASONS - 1,
352       EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN,
353       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
354   g_object_class_install_property (oclass, PROP_REASON, pspec);
355
356   pspec = g_param_spec_boolean ("remember", "Whether to remember the decision",
357       "Whether we should remember the decision for this certificate.",
358       FALSE,
359       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
360   g_object_class_install_property (oclass, PROP_REMEMBER, pspec);
361
362   pspec = g_param_spec_boxed ("details", "Rejection details",
363       "Additional details about the rejection of this certificate.",
364       G_TYPE_HASH_TABLE,
365       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
366   g_object_class_install_property (oclass, PROP_DETAILS, pspec);
367 }
368
369 GtkWidget *
370 empathy_tls_dialog_new (EmpathyTLSCertificate *certificate,
371     EmpTLSCertificateRejectReason reason,
372     GHashTable *details)
373 {
374   g_assert (EMPATHY_IS_TLS_CERTIFICATE (certificate));
375
376   return g_object_new (EMPATHY_TYPE_TLS_DIALOG,
377       "message-type", GTK_MESSAGE_WARNING,
378       "certificate", certificate,
379       "reason", reason,
380       "details", details,
381       NULL);
382 }