2 * empathy-server-tls-handler.c - Source for EmpathyServerTLSHandler
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
22 #include "empathy-server-tls-handler.h"
24 #include <telepathy-glib/telepathy-glib-dbus.h>
26 #include "empathy-utils.h"
27 #include "extensions.h"
29 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
30 #include "empathy-debug.h"
32 static void async_initable_iface_init (GAsyncInitableIface *iface);
38 PROP_REFERENCE_IDENTITIES,
45 TpTLSCertificate *certificate;
47 gchar **reference_identities;
49 GSimpleAsyncResult *async_init_res;
50 } EmpathyServerTLSHandlerPriv;
52 G_DEFINE_TYPE_WITH_CODE (EmpathyServerTLSHandler, empathy_server_tls_handler,
54 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init));
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyServerTLSHandler);
59 tls_certificate_prepared_cb (GObject *source,
63 TpTLSCertificate *certificate = TP_TLS_CERTIFICATE (source);
64 EmpathyServerTLSHandler *self = user_data;
66 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (self);
68 if (!tp_proxy_prepare_finish (certificate, result, &error))
70 g_simple_async_result_set_from_error (priv->async_init_res, error);
74 g_simple_async_result_complete_in_idle (priv->async_init_res);
75 tp_clear_object (&priv->async_init_res);
79 tls_handler_init_finish (GAsyncInitable *initable,
83 gboolean retval = TRUE;
85 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
93 tls_handler_init_async (GAsyncInitable *initable,
95 GCancellable *cancellable,
96 GAsyncReadyCallback callback,
100 const gchar *cert_object_path;
101 const gchar *bus_name;
102 GError *error = NULL;
103 GQuark features[] = { TP_TLS_CERTIFICATE_FEATURE_CORE, 0 };
105 * Used when channel doesn't implement ReferenceIdentities. A GStrv
106 * with [0] the hostname, and [1] a NULL terminator.
108 gchar *default_identities[2];
109 EmpathyServerTLSHandler *self = EMPATHY_SERVER_TLS_HANDLER (initable);
110 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (self);
112 g_assert (priv->channel != NULL);
114 priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
115 callback, user_data, empathy_server_tls_handler_new_async);
116 properties = tp_channel_dup_immutable_properties (priv->channel);
118 g_variant_lookup (properties,
119 TP_PROP_CHANNEL_TYPE_SERVER_TLS_CONNECTION_HOSTNAME,
120 "s", &priv->hostname);
122 DEBUG ("Received hostname: %s", priv->hostname);
124 g_variant_lookup (properties,
125 TP_PROP_CHANNEL_TYPE_SERVER_TLS_CONNECTION_REFERENCE_IDENTITIES,
126 "^as", &priv->reference_identities);
129 * If the channel doesn't implement the ReferenceIdentities parameter
130 * then fallback to the hostname.
132 if (priv->reference_identities == NULL)
134 default_identities[0] = (gchar *) priv->hostname;
135 default_identities[1] = NULL;
136 priv->reference_identities = g_strdupv (default_identities);
141 gchar *output = g_strjoinv (", ", (gchar **) priv->reference_identities);
142 DEBUG ("Received reference identities: %s", output);
144 #endif /* ENABLE_DEBUG */
147 g_variant_lookup (properties,
148 EMP_IFACE_CHANNEL_TYPE_SERVER_TLS_CONNECTION ".ServerCertificate",
149 "&o", &cert_object_path);
150 bus_name = tp_proxy_get_bus_name (TP_PROXY (priv->channel));
152 DEBUG ("Creating an TpTLSCertificate for path %s, bus name %s",
153 cert_object_path, bus_name);
155 priv->certificate = tp_tls_certificate_new (TP_PROXY (priv->channel),
156 cert_object_path, &error);
158 g_variant_unref (properties);
162 DEBUG ("Unable to create the TpTLSCertificate: error %s",
165 g_simple_async_result_set_from_error (priv->async_init_res, error);
166 g_simple_async_result_complete_in_idle (priv->async_init_res);
168 g_error_free (error);
169 tp_clear_object (&priv->async_init_res);
174 tp_proxy_prepare_async (priv->certificate, features,
175 tls_certificate_prepared_cb, self);
179 async_initable_iface_init (GAsyncInitableIface *iface)
181 iface->init_async = tls_handler_init_async;
182 iface->init_finish = tls_handler_init_finish;
186 empathy_server_tls_handler_finalize (GObject *object)
188 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (object);
190 DEBUG ("%p", object);
192 tp_clear_object (&priv->channel);
193 tp_clear_object (&priv->certificate);
194 g_strfreev (priv->reference_identities);
195 g_free (priv->hostname);
197 G_OBJECT_CLASS (empathy_server_tls_handler_parent_class)->finalize (object);
201 empathy_server_tls_handler_get_property (GObject *object,
206 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (object);
211 g_value_set_object (value, priv->channel);
213 case PROP_TLS_CERTIFICATE:
214 g_value_set_object (value, priv->certificate);
217 g_value_set_string (value, priv->hostname);
219 case PROP_REFERENCE_IDENTITIES:
220 g_value_set_boxed (value, priv->reference_identities);
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
229 empathy_server_tls_handler_set_property (GObject *object,
234 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (object);
239 priv->channel = g_value_dup_object (value);
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
248 empathy_server_tls_handler_class_init (EmpathyServerTLSHandlerClass *klass)
250 GObjectClass *oclass = G_OBJECT_CLASS (klass);
253 oclass->get_property = empathy_server_tls_handler_get_property;
254 oclass->set_property = empathy_server_tls_handler_set_property;
255 oclass->finalize = empathy_server_tls_handler_finalize;
257 g_type_class_add_private (klass, sizeof (EmpathyServerTLSHandlerPriv));
259 pspec = g_param_spec_object ("channel", "The TpChannel",
260 "The TpChannel this handler is supposed to handle.",
262 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
263 g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
265 pspec = g_param_spec_object ("certificate", "The TpTLSCertificate",
266 "The TpTLSCertificate carried by the channel.",
267 TP_TYPE_TLS_CERTIFICATE,
268 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
269 g_object_class_install_property (oclass, PROP_TLS_CERTIFICATE, pspec);
271 pspec = g_param_spec_string ("hostname", "The hostname",
272 "The hostname the user is expecting to connect to.",
274 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
275 g_object_class_install_property (oclass, PROP_HOSTNAME, pspec);
277 pspec = g_param_spec_boxed ("reference-identities", "Reference Identities",
278 "The server certificate should certify one of these identities",
279 G_TYPE_STRV, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
280 g_object_class_install_property (oclass, PROP_REFERENCE_IDENTITIES, pspec);
285 empathy_server_tls_handler_init (EmpathyServerTLSHandler *self)
287 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
288 EMPATHY_TYPE_SERVER_TLS_HANDLER, EmpathyServerTLSHandlerPriv);
292 empathy_server_tls_handler_new_async (TpChannel *channel,
293 GAsyncReadyCallback callback,
296 g_assert (TP_IS_CHANNEL (channel));
297 g_assert (channel != NULL);
299 g_async_initable_new_async (EMPATHY_TYPE_SERVER_TLS_HANDLER,
300 G_PRIORITY_DEFAULT, NULL, callback, user_data,
301 "channel", channel, NULL);
304 EmpathyServerTLSHandler *
305 empathy_server_tls_handler_new_finish (GAsyncResult *result,
308 GObject *object, *source_object;
310 source_object = g_async_result_get_source_object (result);
312 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
314 g_object_unref (source_object);
317 return EMPATHY_SERVER_TLS_HANDLER (object);
323 empathy_server_tls_handler_get_certificate (EmpathyServerTLSHandler *self)
325 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (self);
327 g_assert (priv->certificate != NULL);
329 return priv->certificate;