]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-tls-dialog.c
Update simplified Chinese translation.
[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-simple-certificate.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.\n"));
152
153   switch (reason)
154     {
155     case EMP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED:
156       reason_str = _("The certificate is not signed by a Certification "
157           "Authority");
158       break;
159     case EMP_TLS_CERTIFICATE_REJECT_REASON_EXPIRED:
160       reason_str = _("The certificate has expired");
161       break;
162     case EMP_TLS_CERTIFICATE_REJECT_REASON_NOT_ACTIVATED:
163       reason_str = _("The certificate hasn't yet been activated");
164       break;
165     case EMP_TLS_CERTIFICATE_REJECT_REASON_FINGERPRINT_MISMATCH:
166       reason_str = _("The certificate does not have the expected fingerprint");
167       break;
168     case EMP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH:
169       reason_str = _("The hostname verified by the certificate doesn't match "
170           "the server name");
171       break;
172     case EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED:
173       reason_str = _("The certificate is self-signed");
174       break;
175     case EMP_TLS_CERTIFICATE_REJECT_REASON_REVOKED:
176       reason_str = _("The certificate has been revoked by the issuing "
177           "Certification Authority");
178       break;
179     case EMP_TLS_CERTIFICATE_REJECT_REASON_INSECURE:
180       reason_str = _("The certificate is cryptographically weak");
181       break;
182     case EMP_TLS_CERTIFICATE_REJECT_REASON_LIMIT_EXCEEDED:
183       reason_str = _("The certificate length exceeds verifiable limits");
184       break;
185     case EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN:
186     default:
187       reason_str = _("The certificate is malformed");
188       break;
189     }
190
191   g_string_append_printf (str, "%s.", reason_str);
192
193   /* add more information in case of HOSTNAME_MISMATCH */
194   if (reason == EMP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH)
195     {
196       const gchar *expected_hostname, *certificate_hostname;
197
198       expected_hostname = tp_asv_get_string (details, "expected-hostname");
199       certificate_hostname = tp_asv_get_string (details,
200           "certificate-hostname");
201
202       if (expected_hostname != NULL && certificate_hostname != NULL)
203         {
204           g_string_append (str, "\n");
205           g_string_append_printf (str, _("Expected hostname: %s"),
206               expected_hostname);
207           g_string_append (str, "\n");
208           g_string_append_printf (str, _("Certificate hostname: %s"),
209               certificate_hostname);
210         }
211     }
212
213   return g_string_free (str, FALSE);
214 }
215
216 static GtkWidget *
217 build_gcr_widget (EmpathyTLSDialog *self)
218 {
219   GcrCertificateBasicsWidget *widget;
220   GcrCertificate *certificate;
221   GPtrArray *cert_chain = NULL;
222   GArray *first_cert;
223   EmpathyTLSDialogPriv *priv = GET_PRIV (self);
224
225   g_object_get (priv->certificate,
226       "cert-data", &cert_chain,
227       NULL);
228   first_cert = g_ptr_array_index (cert_chain, 0);
229
230   certificate = gcr_simple_certificate_new ((const guchar *) first_cert->data,
231       first_cert->len);
232   widget = gcr_certificate_basics_widget_new (certificate);
233
234   g_object_unref (certificate);
235   g_ptr_array_unref (cert_chain);
236
237   return GTK_WIDGET (widget);
238 }
239
240 static void
241 checkbox_toggled_cb (GtkToggleButton *checkbox,
242     gpointer user_data)
243 {
244   EmpathyTLSDialog *self = user_data;
245   EmpathyTLSDialogPriv *priv = GET_PRIV (self);
246
247   priv->remember = gtk_toggle_button_get_active (checkbox);
248   g_object_notify (G_OBJECT (self), "remember");
249 }
250
251 static void
252 empathy_tls_dialog_constructed (GObject *object)
253 {
254   GtkWidget *content_area, *expander, *details, *checkbox;
255   gchar *text;
256   EmpathyTLSDialog *self = EMPATHY_TLS_DIALOG (object);
257   GtkMessageDialog *message_dialog = GTK_MESSAGE_DIALOG (self);
258   GtkDialog *dialog = GTK_DIALOG (self);
259   EmpathyTLSDialogPriv *priv = GET_PRIV (self);
260
261   gtk_dialog_add_buttons (dialog,
262       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
263       _("Continue"), GTK_RESPONSE_YES,
264       NULL);
265
266   text = reason_to_string (self);
267
268   g_object_set (message_dialog,
269       "text", _("This connection is untrusted. Would you like to "
270           "continue anyway?"),
271       "secondary-text", text,
272       NULL);
273
274   g_free (text);
275
276   content_area = gtk_dialog_get_content_area (dialog);
277
278   /* FIXME: right now we do this only if the error is SelfSigned, as we can
279    * easily store the new CA cert in $XDG_CONFIG_DIR/telepathy/certs in that
280    * case. For the other errors, we probably need a smarter/more powerful
281    * certificate storage.
282    */
283   if (priv->reason == EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED)
284     {
285       checkbox = gtk_check_button_new_with_label (
286           _("Remember this choice for future connections"));
287       gtk_box_pack_end (GTK_BOX (content_area), checkbox, FALSE, FALSE, 0);
288       gtk_widget_show (checkbox);
289
290       g_signal_connect (checkbox, "toggled",
291           G_CALLBACK (checkbox_toggled_cb), self);
292     }
293
294   text = g_strdup_printf ("<b>%s</b>", _("Certificate Details"));
295   expander = gtk_expander_new (text);
296   gtk_expander_set_use_markup (GTK_EXPANDER (expander), TRUE);
297   gtk_box_pack_end (GTK_BOX (content_area), expander, TRUE, TRUE, 0);
298   gtk_widget_show (expander);
299
300   g_free (text);
301
302   details = build_gcr_widget (self);
303   gtk_container_add (GTK_CONTAINER (expander), details);
304   gtk_widget_show (details);
305 }
306
307 static void
308 empathy_tls_dialog_init (EmpathyTLSDialog *self)
309 {
310   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
311       EMPATHY_TYPE_TLS_DIALOG, EmpathyTLSDialogPriv);
312 }
313
314 static void
315 empathy_tls_dialog_class_init (EmpathyTLSDialogClass *klass)
316 {
317   GParamSpec *pspec;
318   GObjectClass *oclass = G_OBJECT_CLASS (klass);
319
320   g_type_class_add_private (klass, sizeof (EmpathyTLSDialogPriv));
321
322   oclass->set_property = empathy_tls_dialog_set_property;
323   oclass->get_property = empathy_tls_dialog_get_property;
324   oclass->dispose = empathy_tls_dialog_dispose;
325   oclass->finalize = empathy_tls_dialog_finalize;
326   oclass->constructed = empathy_tls_dialog_constructed;
327
328   pspec = g_param_spec_object ("certificate", "The EmpathyTLSCertificate",
329       "The EmpathyTLSCertificate to be displayed.",
330       EMPATHY_TYPE_TLS_CERTIFICATE,
331       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
332   g_object_class_install_property (oclass, PROP_TLS_CERTIFICATE, pspec);
333
334   pspec = g_param_spec_uint ("reason", "The reason",
335       "The reason why the certificate is being asked for confirmation.",
336       0, NUM_EMP_TLS_CERTIFICATE_REJECT_REASONS - 1,
337       EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN,
338       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
339   g_object_class_install_property (oclass, PROP_REASON, pspec);
340
341   pspec = g_param_spec_boolean ("remember", "Whether to remember the decision",
342       "Whether we should remember the decision for this certificate.",
343       FALSE,
344       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
345   g_object_class_install_property (oclass, PROP_REMEMBER, pspec);
346
347   pspec = g_param_spec_boxed ("details", "Rejection details",
348       "Additional details about the rejection of this certificate.",
349       G_TYPE_HASH_TABLE,
350       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
351   g_object_class_install_property (oclass, PROP_DETAILS, pspec);
352 }
353
354 GtkWidget *
355 empathy_tls_dialog_new (EmpathyTLSCertificate *certificate,
356     EmpTLSCertificateRejectReason reason,
357     GHashTable *details)
358 {
359   g_assert (EMPATHY_IS_TLS_CERTIFICATE (certificate));
360
361   return g_object_new (EMPATHY_TYPE_TLS_DIALOG,
362       "message-type", GTK_MESSAGE_WARNING,
363       "certificate", certificate,
364       "reason", reason,
365       "details", details,
366       NULL);
367 }