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,
50 EmpathyTLSCertificate *certificate;
51 EmpTLSCertificateRejectReason reason;
57 } EmpathyTLSDialogPriv;
60 empathy_tls_dialog_get_property (GObject *object,
65 EmpathyTLSDialogPriv *priv = GET_PRIV (object);
69 case PROP_TLS_CERTIFICATE:
70 g_value_set_object (value, priv->certificate);
73 g_value_set_uint (value, priv->reason);
76 g_value_set_boolean (value, priv->remember);
79 g_value_set_boxed (value, priv->details);
82 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
88 empathy_tls_dialog_set_property (GObject *object,
93 EmpathyTLSDialogPriv *priv = GET_PRIV (object);
97 case PROP_TLS_CERTIFICATE:
98 priv->certificate = g_value_dup_object (value);
101 priv->reason = g_value_get_uint (value);
104 priv->details = g_value_dup_boxed (value);
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
113 empathy_tls_dialog_dispose (GObject *object)
115 EmpathyTLSDialogPriv *priv = GET_PRIV (object);
117 if (priv->dispose_run)
120 priv->dispose_run = TRUE;
122 tp_clear_object (&priv->certificate);
124 G_OBJECT_CLASS (empathy_tls_dialog_parent_class)->dispose (object);
128 empathy_tls_dialog_finalize (GObject *object)
130 EmpathyTLSDialogPriv *priv = GET_PRIV (object);
132 tp_clear_boxed (G_TYPE_HASH_TABLE, &priv->details);
134 G_OBJECT_CLASS (empathy_tls_dialog_parent_class)->finalize (object);
138 reason_to_string (EmpathyTLSDialog *self)
141 const gchar *reason_str;
142 EmpTLSCertificateRejectReason reason;
144 EmpathyTLSDialogPriv *priv = GET_PRIV (self);
146 str = g_string_new (NULL);
147 reason = priv->reason;
148 details = priv->details;
150 g_string_append (str, _("The identity provided by the chat server cannot be "
155 case EMP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED:
156 reason_str = _("The certificate is not signed by a Certification "
159 case EMP_TLS_CERTIFICATE_REJECT_REASON_EXPIRED:
160 reason_str = _("The certificate has expired");
162 case EMP_TLS_CERTIFICATE_REJECT_REASON_NOT_ACTIVATED:
163 reason_str = _("The certificate hasn't yet been activated");
165 case EMP_TLS_CERTIFICATE_REJECT_REASON_FINGERPRINT_MISMATCH:
166 reason_str = _("The certificate does not have the expected fingerprint");
168 case EMP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH:
169 reason_str = _("The hostname verified by the certificate doesn't match "
172 case EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED:
173 reason_str = _("The certificate is self-signed");
175 case EMP_TLS_CERTIFICATE_REJECT_REASON_REVOKED:
176 reason_str = _("The certificate has been revoked by the issuing "
177 "Certification Authority");
179 case EMP_TLS_CERTIFICATE_REJECT_REASON_INSECURE:
180 reason_str = _("The certificate is cryptographically weak");
182 case EMP_TLS_CERTIFICATE_REJECT_REASON_LIMIT_EXCEEDED:
183 reason_str = _("The certificate length exceeds verifiable limits");
185 case EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN:
187 reason_str = _("The certificate is malformed");
191 g_string_append_printf (str, "%s.", reason_str);
193 /* add more information in case of HOSTNAME_MISMATCH */
194 if (reason == EMP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH)
196 const gchar *expected_hostname, *certificate_hostname;
198 expected_hostname = tp_asv_get_string (details, "expected-hostname");
199 certificate_hostname = tp_asv_get_string (details,
200 "certificate-hostname");
202 if (expected_hostname != NULL && certificate_hostname != NULL)
204 g_string_append (str, "\n");
205 g_string_append_printf (str, _("Expected hostname: %s"),
207 g_string_append (str, "\n");
208 g_string_append_printf (str, _("Certificate hostname: %s"),
209 certificate_hostname);
213 return g_string_free (str, FALSE);
217 build_gcr_widget (EmpathyTLSDialog *self)
219 GcrCertificateBasicsWidget *widget;
220 GcrCertificate *certificate;
221 GPtrArray *cert_chain = NULL;
223 EmpathyTLSDialogPriv *priv = GET_PRIV (self);
225 g_object_get (priv->certificate,
226 "cert-data", &cert_chain,
228 first_cert = g_ptr_array_index (cert_chain, 0);
230 certificate = gcr_simple_certificate_new ((const guchar *) first_cert->data,
232 widget = gcr_certificate_basics_widget_new (certificate);
234 g_object_unref (certificate);
235 g_ptr_array_unref (cert_chain);
237 return GTK_WIDGET (widget);
241 checkbox_toggled_cb (GtkToggleButton *checkbox,
244 EmpathyTLSDialog *self = user_data;
245 EmpathyTLSDialogPriv *priv = GET_PRIV (self);
247 priv->remember = gtk_toggle_button_get_active (checkbox);
248 g_object_notify (G_OBJECT (self), "remember");
252 empathy_tls_dialog_constructed (GObject *object)
254 GtkWidget *content_area, *expander, *details, *checkbox;
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);
261 gtk_dialog_add_buttons (dialog,
262 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
263 _("Continue"), GTK_RESPONSE_YES,
266 text = reason_to_string (self);
268 g_object_set (message_dialog,
269 "text", _("This connection is untrusted. Would you like to "
271 "secondary-text", text,
276 content_area = gtk_dialog_get_content_area (dialog);
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.
283 if (priv->reason == EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED)
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);
290 g_signal_connect (checkbox, "toggled",
291 G_CALLBACK (checkbox_toggled_cb), self);
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);
302 details = build_gcr_widget (self);
303 gtk_container_add (GTK_CONTAINER (expander), details);
304 gtk_widget_show (details);
308 empathy_tls_dialog_init (EmpathyTLSDialog *self)
310 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
311 EMPATHY_TYPE_TLS_DIALOG, EmpathyTLSDialogPriv);
315 empathy_tls_dialog_class_init (EmpathyTLSDialogClass *klass)
318 GObjectClass *oclass = G_OBJECT_CLASS (klass);
320 g_type_class_add_private (klass, sizeof (EmpathyTLSDialogPriv));
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;
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);
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);
341 pspec = g_param_spec_boolean ("remember", "Whether to remember the decision",
342 "Whether we should remember the decision for this certificate.",
344 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
345 g_object_class_install_property (oclass, PROP_REMEMBER, pspec);
347 pspec = g_param_spec_boxed ("details", "Rejection details",
348 "Additional details about the rejection of this certificate.",
350 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
351 g_object_class_install_property (oclass, PROP_DETAILS, pspec);
355 empathy_tls_dialog_new (EmpathyTLSCertificate *certificate,
356 EmpTLSCertificateRejectReason reason,
359 g_assert (EMPATHY_IS_TLS_CERTIFICATE (certificate));
361 return g_object_new (EMPATHY_TYPE_TLS_DIALOG,
362 "message-type", GTK_MESSAGE_WARNING,
363 "certificate", certificate,