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