]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-tls-dialog.c
b61d5caccde56388d6f117c6d717f4d3a03b4bc3
[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
45   LAST_PROPERTY,
46 };
47
48 typedef struct {
49   EmpathyTLSCertificate *certificate;
50   EmpTLSCertificateRejectReason reason;
51
52   gboolean remember;
53
54   gboolean dispose_run;
55 } EmpathyTLSDialogPriv;
56
57 static void
58 empathy_tls_dialog_get_property (GObject *object,
59     guint property_id,
60     GValue *value,
61     GParamSpec *pspec)
62 {
63   EmpathyTLSDialogPriv *priv = GET_PRIV (object);
64
65   switch (property_id)
66     {
67     case PROP_TLS_CERTIFICATE:
68       g_value_set_object (value, priv->certificate);
69       break;
70     case PROP_REASON:
71       g_value_set_uint (value, priv->reason);
72       break;
73     case PROP_REMEMBER:
74       g_value_set_boolean (value, priv->remember);
75       break;
76     default:
77       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
78       break;
79     }
80 }
81
82 static void
83 empathy_tls_dialog_set_property (GObject *object,
84     guint property_id,
85     const GValue *value,
86     GParamSpec *pspec)
87 {
88   EmpathyTLSDialogPriv *priv = GET_PRIV (object);
89
90   switch (property_id)
91     {
92     case PROP_TLS_CERTIFICATE:
93       priv->certificate = g_value_dup_object (value);
94       break;
95     case PROP_REASON:
96       priv->reason = g_value_get_uint (value);
97       break;
98     default:
99       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
100       break;
101     }
102 }
103
104 static void
105 empathy_tls_dialog_dispose (GObject *object)
106 {
107   EmpathyTLSDialogPriv *priv = GET_PRIV (object);
108
109   if (priv->dispose_run)
110     return;
111
112   priv->dispose_run = TRUE;
113
114   tp_clear_object (&priv->certificate);
115
116   G_OBJECT_CLASS (empathy_tls_dialog_parent_class)->dispose (object);
117 }
118
119 static gchar *
120 reason_to_string (EmpTLSCertificateRejectReason reason)
121 {
122   GString *str;
123   const gchar *reason_str;
124
125   str = g_string_new (NULL);
126
127   g_string_append (str, _("The identity provided by the chat server cannot be "
128           "verified.\n"));
129
130   switch (reason)
131     {
132     case EMP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED:
133       reason_str = _("The certrificate is not signed by a Certification "
134           "Authority");
135       break;
136     case EMP_TLS_CERTIFICATE_REJECT_REASON_EXPIRED:
137       reason_str = _("The certificate is expired");
138       break;
139     case EMP_TLS_CERTIFICATE_REJECT_REASON_NOT_ACTIVATED:
140       reason_str = _("The certificate hasn't yet been activated");
141       break;
142     case EMP_TLS_CERTIFICATE_REJECT_REASON_FINGERPRINT_MISMATCH:
143       reason_str = _("The certificate does not have the expected fingerprint");
144       break;
145     case EMP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH:
146       reason_str = _("The hostname verified by the certificate doesn't match "
147           "the server name");
148       break;
149     case EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED:
150       reason_str = _("The certificate is self-signed");
151       break;
152     case EMP_TLS_CERTIFICATE_REJECT_REASON_REVOKED:
153       reason_str = _("The certificate has been revoked by the issuing "
154           "Certification Authority");
155       break;
156     case EMP_TLS_CERTIFICATE_REJECT_REASON_INSECURE:
157       reason_str = _("The certificate is cryptographically weak");
158       break;
159     case EMP_TLS_CERTIFICATE_REJECT_REASON_LIMIT_EXCEEDED:
160       reason_str = _("The certificate length exceeds verifiable limits");
161       break;
162     case EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN:
163     default:
164       reason_str = _("The certificate is malformed");
165       break;
166     }
167
168   g_string_append (str, reason_str);
169
170   return g_string_free (str, FALSE);
171 }
172
173 static GtkWidget *
174 build_gcr_widget (EmpathyTLSDialog *self)
175 {
176   GcrCertificateBasicsWidget *widget;
177   GcrCertificate *certificate;
178   GPtrArray *cert_chain = NULL;
179   GArray *first_cert;
180   EmpathyTLSDialogPriv *priv = GET_PRIV (self);
181
182   g_object_get (priv->certificate,
183       "cert-data", &cert_chain,
184       NULL);
185   first_cert = g_ptr_array_index (cert_chain, 0);
186
187   certificate = gcr_simple_certificate_new ((const guchar *) first_cert->data,
188       first_cert->len);
189   widget = gcr_certificate_basics_widget_new (certificate);
190
191   g_object_unref (certificate);
192   g_ptr_array_unref (cert_chain);
193
194   return GTK_WIDGET (widget);
195 }
196
197 static void
198 checkbox_toggled_cb (GtkToggleButton *checkbox,
199     gpointer user_data)
200 {
201   EmpathyTLSDialog *self = user_data;
202   EmpathyTLSDialogPriv *priv = GET_PRIV (self);
203
204   priv->remember = gtk_toggle_button_get_active (checkbox);
205   g_object_notify (G_OBJECT (self), "remember");
206 }
207
208 static void
209 empathy_tls_dialog_constructed (GObject *object)
210 {
211   GtkWidget *content_area, *expander, *details, *checkbox;
212   gchar *text;
213   EmpathyTLSDialog *self = EMPATHY_TLS_DIALOG (object);
214   GtkMessageDialog *message_dialog = GTK_MESSAGE_DIALOG (self);
215   GtkDialog *dialog = GTK_DIALOG (self);
216   EmpathyTLSDialogPriv *priv = GET_PRIV (self);
217
218   gtk_dialog_add_buttons (dialog,
219       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
220       _("Continue"), GTK_RESPONSE_YES,
221       NULL);
222
223   text = reason_to_string (priv->reason);
224
225   g_object_set (message_dialog,
226       "text", _("This connection is untrusted, would you like to "
227           "continue anyway?"),
228       "secondary-text", text,
229       NULL);
230
231   g_free (text);
232
233   content_area = gtk_dialog_get_content_area (dialog);
234
235   /* FIXME: right now we do this only if the error is SelfSigned, as we can
236    * easily store the new CA cert in $XDG_CONFIG_DIR/telepathy/certs in that
237    * case. For the other errors, we probably need a smarter/more powerful
238    * certificate storage.
239    */
240   if (priv->reason == EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED)
241     {
242       checkbox = gtk_check_button_new_with_label (
243           _("Remember this choice for future connections"));
244       gtk_box_pack_end (GTK_BOX (content_area), checkbox, FALSE, FALSE, 0);
245       gtk_widget_show (checkbox);
246
247       g_signal_connect (checkbox, "toggled",
248           G_CALLBACK (checkbox_toggled_cb), self);
249     }
250
251   text = g_strdup_printf ("<b>%s</b>", _("Certificate Details"));
252   expander = gtk_expander_new (text);
253   gtk_expander_set_use_markup (GTK_EXPANDER (expander), TRUE);
254   gtk_box_pack_end (GTK_BOX (content_area), expander, TRUE, TRUE, 0);
255   gtk_widget_show (expander);
256
257   g_free (text);
258
259   details = build_gcr_widget (self);
260   gtk_container_add (GTK_CONTAINER (expander), details);
261   gtk_widget_show (details);
262 }
263
264 static void
265 empathy_tls_dialog_init (EmpathyTLSDialog *self)
266 {
267   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
268       EMPATHY_TYPE_TLS_DIALOG, EmpathyTLSDialogPriv);
269 }
270
271 static void
272 empathy_tls_dialog_class_init (EmpathyTLSDialogClass *klass)
273 {
274   GParamSpec *pspec;
275   GObjectClass *oclass = G_OBJECT_CLASS (klass);
276
277   g_type_class_add_private (klass, sizeof (EmpathyTLSDialogPriv));
278
279   oclass->set_property = empathy_tls_dialog_set_property;
280   oclass->get_property = empathy_tls_dialog_get_property;
281   oclass->dispose = empathy_tls_dialog_dispose;
282   oclass->constructed = empathy_tls_dialog_constructed;
283
284   pspec = g_param_spec_object ("certificate", "The EmpathyTLSCertificate",
285       "The EmpathyTLSCertificate to be displayed.",
286       EMPATHY_TYPE_TLS_CERTIFICATE,
287       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
288   g_object_class_install_property (oclass, PROP_TLS_CERTIFICATE, pspec);
289
290   pspec = g_param_spec_uint ("reason", "The reason",
291       "The reason why the certificate is being asked for confirmation.",
292       0, NUM_EMP_TLS_CERTIFICATE_REJECT_REASONS - 1,
293       EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN,
294       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
295   g_object_class_install_property (oclass, PROP_REASON, pspec);
296
297   pspec = g_param_spec_boolean ("remember", "Whether to remember the decision",
298       "Whether we should remember the decision for this certificate.",
299       FALSE,
300       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
301   g_object_class_install_property (oclass, PROP_REMEMBER, pspec);
302 }
303
304 GtkWidget *
305 empathy_tls_dialog_new (EmpathyTLSCertificate *certificate,
306     EmpTLSCertificateRejectReason reason)
307 {
308   g_assert (EMPATHY_IS_TLS_CERTIFICATE (certificate));
309
310   return g_object_new (EMPATHY_TYPE_TLS_DIALOG,
311       "message-type", GTK_MESSAGE_WARNING,
312       "certificate", certificate,
313       "reason", reason,
314       NULL);
315 }