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,
48 EmpathyTLSCertificate *certificate;
49 EmpTLSCertificateRejectReason reason;
52 } EmpathyTLSDialogPriv;
55 empathy_tls_dialog_get_property (GObject *object,
60 EmpathyTLSDialogPriv *priv = GET_PRIV (object);
64 case PROP_TLS_CERTIFICATE:
65 g_value_set_object (value, priv->certificate);
68 g_value_set_uint (value, priv->reason);
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
77 empathy_tls_dialog_set_property (GObject *object,
82 EmpathyTLSDialogPriv *priv = GET_PRIV (object);
86 case PROP_TLS_CERTIFICATE:
87 priv->certificate = g_value_dup_object (value);
90 priv->reason = g_value_get_uint (value);
93 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
99 empathy_tls_dialog_dispose (GObject *object)
101 EmpathyTLSDialogPriv *priv = GET_PRIV (object);
103 if (priv->dispose_run)
106 priv->dispose_run = TRUE;
108 tp_clear_object (&priv->certificate);
110 G_OBJECT_CLASS (empathy_tls_dialog_parent_class)->dispose (object);
114 reason_to_string (EmpTLSCertificateRejectReason reason)
117 const gchar *reason_str;
119 str = g_string_new (NULL);
121 g_string_append (str, _("The identity provided by the chat server cannot be "
126 case EMP_TLS_CERTIFICATE_REJECT_REASON_UNTRUSTED:
127 reason_str = _("The certrificate is not signed by a Certification "
130 case EMP_TLS_CERTIFICATE_REJECT_REASON_EXPIRED:
131 reason_str = _("The certificate is expired");
133 case EMP_TLS_CERTIFICATE_REJECT_REASON_NOT_ACTIVATED:
134 reason_str = _("The certificate hasn't yet been activated");
136 case EMP_TLS_CERTIFICATE_REJECT_REASON_FINGERPRINT_MISMATCH:
137 reason_str = _("The certificate does not have the expected fingerprint");
139 case EMP_TLS_CERTIFICATE_REJECT_REASON_HOSTNAME_MISMATCH:
140 reason_str = _("The hostname verified by the certificate doesn't match "
143 case EMP_TLS_CERTIFICATE_REJECT_REASON_SELF_SIGNED:
144 reason_str = _("The certificate is self-signed");
146 case EMP_TLS_CERTIFICATE_REJECT_REASON_REVOKED:
147 reason_str = _("The certificate has been revoked by the issuing "
148 "Certification Authority");
150 case EMP_TLS_CERTIFICATE_REJECT_REASON_INSECURE:
151 reason_str = _("The certificate is cryptographically weak");
153 case EMP_TLS_CERTIFICATE_REJECT_REASON_LIMIT_EXCEEDED:
154 reason_str = _("The certificate length exceeds verifiable limits");
156 case EMP_TLS_CERTIFICATE_REJECT_REASON_UNKNOWN:
158 reason_str = _("The certificate is malformed");
162 g_string_append (str, reason_str);
164 return g_string_free (str, FALSE);
168 build_gcr_widget (EmpathyTLSDialog *self)
170 GcrCertificateBasicsWidget *widget;
171 GcrCertificate *certificate;
172 GPtrArray *cert_chain = NULL;
174 EmpathyTLSDialogPriv *priv = GET_PRIV (self);
176 g_object_get (priv->certificate,
177 "cert-data", &cert_chain,
179 first_cert = g_ptr_array_index (cert_chain, 0);
181 certificate = gcr_simple_certificate_new ((const guchar *) first_cert->data,
183 widget = gcr_certificate_basics_widget_new (certificate);
185 g_object_unref (certificate);
186 g_ptr_array_unref (cert_chain);
188 return GTK_WIDGET (widget);
192 empathy_tls_dialog_constructed (GObject *object)
194 GtkWidget *content_area, *expander, *details;
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);
201 gtk_dialog_add_buttons (dialog,
202 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
203 _("Continue"), GTK_RESPONSE_YES,
206 text = reason_to_string (priv->reason);
208 g_object_set (message_dialog,
209 "text", _("This connection is untrusted, would you like to "
211 "secondary-text", text,
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);
225 details = build_gcr_widget (self);
226 gtk_container_add (GTK_CONTAINER (expander), details);
227 gtk_widget_show (details);
231 empathy_tls_dialog_init (EmpathyTLSDialog *self)
233 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
234 EMPATHY_TYPE_TLS_DIALOG, EmpathyTLSDialogPriv);
238 empathy_tls_dialog_class_init (EmpathyTLSDialogClass *klass)
241 GObjectClass *oclass = G_OBJECT_CLASS (klass);
243 g_type_class_add_private (klass, sizeof (EmpathyTLSDialogPriv));
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;
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);
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);
265 empathy_tls_dialog_new (EmpathyTLSCertificate *certificate,
266 EmpTLSCertificateRejectReason reason)
268 g_assert (EMPATHY_IS_TLS_CERTIFICATE (certificate));
270 return g_object_new (EMPATHY_TYPE_TLS_DIALOG,
271 "message-type", GTK_MESSAGE_WARNING,
272 "certificate", certificate,