2 * empathy-tls-dialog.c - Source for EmpathyTLSDialog
3 * Copyright (C) 2010 Collabora Ltd.
4 * @author Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
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.
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.
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
23 #include "empathy-tls-dialog.h"
25 #include <glib/gi18n-lib.h>
27 #include <telepathy-glib/util.h>
29 #include "gcr-simple-certificate.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
32 #include <libempathy/empathy-debug.h>
33 #include <libempathy/empathy-utils.h>
35 G_DEFINE_TYPE (EmpathyTLSDialog, empathy_tls_dialog,
36 GTK_TYPE_MESSAGE_DIALOG)
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTLSDialog);
41 PROP_TLS_CERTIFICATE = 1,
49 EmpathyTLSCertificate *certificate;
50 EmpTLSCertificateRejectReason reason;
55 } EmpathyTLSDialogPriv;
58 empathy_tls_dialog_get_property (GObject *object,
63 EmpathyTLSDialogPriv *priv = GET_PRIV (object);
67 case PROP_TLS_CERTIFICATE:
68 g_value_set_object (value, priv->certificate);
71 g_value_set_uint (value, priv->reason);
74 g_value_set_boolean (value, priv->remember);
77 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
83 empathy_tls_dialog_set_property (GObject *object,
88 EmpathyTLSDialogPriv *priv = GET_PRIV (object);
92 case PROP_TLS_CERTIFICATE:
93 priv->certificate = g_value_dup_object (value);
96 priv->reason = g_value_get_uint (value);
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
105 empathy_tls_dialog_dispose (GObject *object)
107 EmpathyTLSDialogPriv *priv = GET_PRIV (object);
109 if (priv->dispose_run)
112 priv->dispose_run = TRUE;
114 tp_clear_object (&priv->certificate);
116 G_OBJECT_CLASS (empathy_tls_dialog_parent_class)->dispose (object);
120 reason_to_string (EmpTLSCertificateRejectReason reason)
123 const gchar *reason_str;
125 str = g_string_new (NULL);
127 g_string_append (str, _("The identity provided by the chat server cannot be "
132 case EMP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED:
133 reason_str = _("The certrificate is not signed by a Certification "
136 case EMP_TLS_CERTIFICATE_REJECT_REASON_EXPIRED:
137 reason_str = _("The certificate is expired");
139 case EMP_TLS_CERTIFICATE_REJECT_REASON_NOT_ACTIVATED:
140 reason_str = _("The certificate hasn't yet been activated");
142 case EMP_TLS_CERTIFICATE_REJECT_REASON_FINGERPRINT_MISMATCH:
143 reason_str = _("The certificate does not have the expected fingerprint");
145 case EMP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH:
146 reason_str = _("The hostname verified by the certificate doesn't match "
149 case EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED:
150 reason_str = _("The certificate is self-signed");
152 case EMP_TLS_CERTIFICATE_REJECT_REASON_REVOKED:
153 reason_str = _("The certificate has been revoked by the issuing "
154 "Certification Authority");
156 case EMP_TLS_CERTIFICATE_REJECT_REASON_INSECURE:
157 reason_str = _("The certificate is cryptographically weak");
159 case EMP_TLS_CERTIFICATE_REJECT_REASON_LIMIT_EXCEEDED:
160 reason_str = _("The certificate length exceeds verifiable limits");
162 case EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN:
164 reason_str = _("The certificate is malformed");
168 g_string_append (str, reason_str);
170 return g_string_free (str, FALSE);
174 build_gcr_widget (EmpathyTLSDialog *self)
176 GcrCertificateBasicsWidget *widget;
177 GcrCertificate *certificate;
178 GPtrArray *cert_chain = NULL;
180 EmpathyTLSDialogPriv *priv = GET_PRIV (self);
182 g_object_get (priv->certificate,
183 "cert-data", &cert_chain,
185 first_cert = g_ptr_array_index (cert_chain, 0);
187 certificate = gcr_simple_certificate_new ((const guchar *) first_cert->data,
189 widget = gcr_certificate_basics_widget_new (certificate);
191 g_object_unref (certificate);
192 g_ptr_array_unref (cert_chain);
194 return GTK_WIDGET (widget);
198 checkbox_toggled_cb (GtkToggleButton *checkbox,
201 EmpathyTLSDialog *self = user_data;
202 EmpathyTLSDialogPriv *priv = GET_PRIV (self);
204 priv->remember = gtk_toggle_button_get_active (checkbox);
205 g_object_notify (G_OBJECT (self), "remember");
209 empathy_tls_dialog_constructed (GObject *object)
211 GtkWidget *content_area, *expander, *details, *checkbox;
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);
218 gtk_dialog_add_buttons (dialog,
219 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
220 _("Continue"), GTK_RESPONSE_YES,
223 text = reason_to_string (priv->reason);
225 g_object_set (message_dialog,
226 "text", _("This connection is untrusted, would you like to "
228 "secondary-text", text,
233 content_area = gtk_dialog_get_content_area (dialog);
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.
240 if (priv->reason == EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED)
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);
247 g_signal_connect (checkbox, "toggled",
248 G_CALLBACK (checkbox_toggled_cb), self);
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);
259 details = build_gcr_widget (self);
260 gtk_container_add (GTK_CONTAINER (expander), details);
261 gtk_widget_show (details);
265 empathy_tls_dialog_init (EmpathyTLSDialog *self)
267 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
268 EMPATHY_TYPE_TLS_DIALOG, EmpathyTLSDialogPriv);
272 empathy_tls_dialog_class_init (EmpathyTLSDialogClass *klass)
275 GObjectClass *oclass = G_OBJECT_CLASS (klass);
277 g_type_class_add_private (klass, sizeof (EmpathyTLSDialogPriv));
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;
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);
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);
297 pspec = g_param_spec_boolean ("remember", "Whether to remember the decision",
298 "Whether we should remember the decision for this certificate.",
300 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
301 g_object_class_install_property (oclass, PROP_REMEMBER, pspec);
305 empathy_tls_dialog_new (EmpathyTLSCertificate *certificate,
306 EmpTLSCertificateRejectReason reason)
308 g_assert (EMPATHY_IS_TLS_CERTIFICATE (certificate));
310 return g_object_new (EMPATHY_TYPE_TLS_DIALOG,
311 "message-type", GTK_MESSAGE_WARNING,
312 "certificate", certificate,