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
21 #include "empathy-server-tls-handler.h"
23 #include <telepathy-glib/interfaces.h>
24 #include <telepathy-glib/util.h>
26 #define DEBUG_FLAG EMPATHY_DEBUG_TLS
27 #include "empathy-debug.h"
28 #include "empathy-tls-certificate.h"
29 #include "empathy-utils.h"
31 #include "extensions/extensions.h"
33 static void async_initable_iface_init (GAsyncInitableIface *iface);
39 PROP_REFERENCE_IDENTITIES,
46 EmpathyTLSCertificate *certificate;
48 gchar **reference_identities;
50 GSimpleAsyncResult *async_init_res;
51 } EmpathyServerTLSHandlerPriv;
53 G_DEFINE_TYPE_WITH_CODE (EmpathyServerTLSHandler, empathy_server_tls_handler,
55 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init));
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyServerTLSHandler);
60 tls_certificate_prepared_cb (GObject *source,
64 EmpathyTLSCertificate *certificate = EMPATHY_TLS_CERTIFICATE (source);
65 EmpathyServerTLSHandler *self = user_data;
67 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (self);
69 empathy_tls_certificate_prepare_finish (certificate, result, &error);
73 g_simple_async_result_set_from_error (priv->async_init_res, error);
77 g_simple_async_result_complete_in_idle (priv->async_init_res);
78 tp_clear_object (&priv->async_init_res);
82 tls_handler_init_finish (GAsyncInitable *initable,
86 gboolean retval = TRUE;
88 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
96 tls_handler_init_async (GAsyncInitable *initable,
98 GCancellable *cancellable,
99 GAsyncReadyCallback callback,
102 GHashTable *properties;
103 const gchar *cert_object_path;
104 const gchar *hostname;
105 const gchar * const *identities;
106 const gchar *bus_name;
108 GError *error = NULL;
110 * Used when channel doesn't implement ReferenceIdentities. A GStrv
111 * with [0] the hostname, and [1] a NULL terminator.
113 gchar *default_identities[2];
114 EmpathyServerTLSHandler *self = EMPATHY_SERVER_TLS_HANDLER (initable);
115 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (self);
117 g_assert (priv->channel != NULL);
119 priv->async_init_res = g_simple_async_result_new (G_OBJECT (self),
120 callback, user_data, empathy_server_tls_handler_new_async);
121 properties = tp_channel_borrow_immutable_properties (priv->channel);
123 hostname = tp_asv_get_string (properties,
124 TP_PROP_CHANNEL_TYPE_SERVER_TLS_CONNECTION_HOSTNAME);
125 priv->hostname = g_strdup (hostname);
127 DEBUG ("Received hostname: %s", hostname);
129 identities = tp_asv_get_strv (properties,
130 TP_PROP_CHANNEL_TYPE_SERVER_TLS_CONNECTION_REFERENCE_IDENTITIES);
133 * If the channel doesn't implement the ReferenceIdentities parameter
134 * then fallback to the hostname.
136 if (identities == NULL)
138 default_identities[0] = (gchar *) hostname;
139 default_identities[1] = NULL;
140 identities = (const gchar **) default_identities;
145 gchar *output = g_strjoinv (", ", (gchar **) identities);
146 DEBUG ("Received reference identities: %s", output);
148 #endif /* ENABLE_DEBUG */
151 priv->reference_identities = g_strdupv ((gchar **) identities);
153 cert_object_path = tp_asv_get_object_path (properties,
154 EMP_IFACE_CHANNEL_TYPE_SERVER_TLS_CONNECTION ".ServerCertificate");
155 bus_name = tp_proxy_get_bus_name (TP_PROXY (priv->channel));
156 dbus = tp_proxy_get_dbus_daemon (TP_PROXY (priv->channel));
158 DEBUG ("Creating an EmpathyTLSCertificate for path %s, bus name %s",
159 cert_object_path, bus_name);
161 priv->certificate = empathy_tls_certificate_new (dbus, bus_name,
162 cert_object_path, &error);
166 DEBUG ("Unable to create the EmpathyTLSCertificate: error %s",
169 g_simple_async_result_set_from_error (priv->async_init_res, error);
170 g_simple_async_result_complete_in_idle (priv->async_init_res);
172 g_error_free (error);
173 tp_clear_object (&priv->async_init_res);
178 empathy_tls_certificate_prepare_async (priv->certificate,
179 tls_certificate_prepared_cb, self);
183 async_initable_iface_init (GAsyncInitableIface *iface)
185 iface->init_async = tls_handler_init_async;
186 iface->init_finish = tls_handler_init_finish;
190 empathy_server_tls_handler_finalize (GObject *object)
192 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (object);
194 DEBUG ("%p", object);
196 tp_clear_object (&priv->channel);
197 tp_clear_object (&priv->certificate);
198 g_strfreev (priv->reference_identities);
199 g_free (priv->hostname);
201 G_OBJECT_CLASS (empathy_server_tls_handler_parent_class)->finalize (object);
205 empathy_server_tls_handler_get_property (GObject *object,
210 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (object);
215 g_value_set_object (value, priv->channel);
217 case PROP_TLS_CERTIFICATE:
218 g_value_set_object (value, priv->certificate);
221 g_value_set_string (value, priv->hostname);
223 case PROP_REFERENCE_IDENTITIES:
224 g_value_set_boxed (value, priv->reference_identities);
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
233 empathy_server_tls_handler_set_property (GObject *object,
238 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (object);
243 priv->channel = g_value_dup_object (value);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
252 empathy_server_tls_handler_class_init (EmpathyServerTLSHandlerClass *klass)
254 GObjectClass *oclass = G_OBJECT_CLASS (klass);
257 oclass->get_property = empathy_server_tls_handler_get_property;
258 oclass->set_property = empathy_server_tls_handler_set_property;
259 oclass->finalize = empathy_server_tls_handler_finalize;
261 g_type_class_add_private (klass, sizeof (EmpathyServerTLSHandlerPriv));
263 pspec = g_param_spec_object ("channel", "The TpChannel",
264 "The TpChannel this handler is supposed to handle.",
266 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
267 g_object_class_install_property (oclass, PROP_CHANNEL, pspec);
269 pspec = g_param_spec_object ("certificate", "The EmpathyTLSCertificate",
270 "The EmpathyTLSCertificate carried by the channel.",
271 EMPATHY_TYPE_TLS_CERTIFICATE,
272 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
273 g_object_class_install_property (oclass, PROP_TLS_CERTIFICATE, pspec);
275 pspec = g_param_spec_string ("hostname", "The hostname",
276 "The hostname the user is expecting to connect to.",
278 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
279 g_object_class_install_property (oclass, PROP_HOSTNAME, pspec);
281 pspec = g_param_spec_boxed ("reference-identities", "Reference Identities",
282 "The server certificate should certify one of these identities",
283 G_TYPE_STRV, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
284 g_object_class_install_property (oclass, PROP_REFERENCE_IDENTITIES, pspec);
289 empathy_server_tls_handler_init (EmpathyServerTLSHandler *self)
291 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
292 EMPATHY_TYPE_SERVER_TLS_HANDLER, EmpathyServerTLSHandlerPriv);
296 empathy_server_tls_handler_new_async (TpChannel *channel,
297 GAsyncReadyCallback callback,
300 g_assert (TP_IS_CHANNEL (channel));
301 g_assert (channel != NULL);
303 g_async_initable_new_async (EMPATHY_TYPE_SERVER_TLS_HANDLER,
304 G_PRIORITY_DEFAULT, NULL, callback, user_data,
305 "channel", channel, NULL);
308 EmpathyServerTLSHandler *
309 empathy_server_tls_handler_new_finish (GAsyncResult *result,
312 GObject *object, *source_object;
314 source_object = g_async_result_get_source_object (result);
316 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
318 g_object_unref (source_object);
321 return EMPATHY_SERVER_TLS_HANDLER (object);
326 EmpathyTLSCertificate *
327 empathy_server_tls_handler_get_certificate (EmpathyServerTLSHandler *self)
329 EmpathyServerTLSHandlerPriv *priv = GET_PRIV (self);
331 g_assert (priv->certificate != NULL);
333 return priv->certificate;