1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2010 Red Hat, Inc
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see
17 * <http://www.gnu.org/licenses/>.
26 #include <gnutls/gnutls.h>
28 #include "gtlsbackend-gnutls.h"
29 #include "gtlscertificate-gnutls.h"
30 #include "gtlsclientconnection-gnutls.h"
31 #include "gtlsfiledatabase-gnutls.h"
32 #include "gtlsserverconnection-gnutls.h"
36 struct _GTlsBackendGnutlsPrivate
39 GTlsDatabase *default_database;
42 static void g_tls_backend_gnutls_interface_init (GTlsBackendInterface *iface);
44 G_DEFINE_DYNAMIC_TYPE_EXTENDED (GTlsBackendGnutls, g_tls_backend_gnutls, G_TYPE_OBJECT, 0,
45 G_IMPLEMENT_INTERFACE_DYNAMIC (G_TYPE_TLS_BACKEND,
46 g_tls_backend_gnutls_interface_init);)
48 #ifdef GTLS_GNUTLS_DEBUG
50 gtls_log_func (int level, const char *msg)
52 #if ENABLE(TIZEN_DLOG)
53 TIZEN_LOGI("GTLS: %s", msg);
55 g_print ("GTLS: %s", msg);
61 gtls_gnutls_init (gpointer data)
63 gnutls_global_init ();
65 #ifdef GTLS_GNUTLS_DEBUG
66 gnutls_global_set_log_function (gtls_log_func);
67 gnutls_global_set_log_level (9);
70 /* Leak the module to keep it from being unloaded. */
71 g_type_plugin_use (g_type_get_plugin (G_TYPE_TLS_BACKEND_GNUTLS));
75 static GOnce gnutls_inited = G_ONCE_INIT;
78 g_tls_backend_gnutls_init (GTlsBackendGnutls *backend)
80 /* Once we call gtls_gnutls_init(), we can't allow the module to be
81 * unloaded (since if gnutls gets unloaded but gcrypt doesn't, then
82 * gcrypt will have dangling pointers to gnutls's mutex functions).
83 * So we initialize it from here rather than at class init time so
84 * that it doesn't happen unless the app is actually using TLS (as
85 * opposed to just calling g_io_modules_scan_all_in_directory()).
87 g_once (&gnutls_inited, gtls_gnutls_init, NULL);
89 backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend, G_TYPE_TLS_BACKEND_GNUTLS, GTlsBackendGnutlsPrivate);
90 g_mutex_init (&backend->priv->mutex);
94 g_tls_backend_gnutls_finalize (GObject *object)
96 GTlsBackendGnutls *backend = G_TLS_BACKEND_GNUTLS (object);
98 if (backend->priv->default_database)
99 g_object_unref (backend->priv->default_database);
100 g_mutex_clear (&backend->priv->mutex);
102 G_OBJECT_CLASS (g_tls_backend_gnutls_parent_class)->finalize (object);
106 g_tls_backend_gnutls_real_create_database (GTlsBackendGnutls *self,
109 const gchar *anchor_file = NULL;
110 #ifdef GTLS_SYSTEM_CA_FILE
111 anchor_file = GTLS_SYSTEM_CA_FILE;
113 return g_tls_file_database_new (anchor_file, error);
117 g_tls_backend_gnutls_class_init (GTlsBackendGnutlsClass *backend_class)
119 GObjectClass *gobject_class = G_OBJECT_CLASS (backend_class);
120 gobject_class->finalize = g_tls_backend_gnutls_finalize;
121 backend_class->create_database = g_tls_backend_gnutls_real_create_database;
122 g_type_class_add_private (backend_class, sizeof (GTlsBackendGnutlsPrivate));
126 g_tls_backend_gnutls_class_finalize (GTlsBackendGnutlsClass *backend_class)
131 g_tls_backend_gnutls_get_default_database (GTlsBackend *backend)
133 GTlsBackendGnutls *self = G_TLS_BACKEND_GNUTLS (backend);
134 GTlsDatabase *result;
135 GError *error = NULL;
137 g_mutex_lock (&self->priv->mutex);
139 if (self->priv->default_database)
141 result = g_object_ref (self->priv->default_database);
145 g_assert (G_TLS_BACKEND_GNUTLS_GET_CLASS (self)->create_database);
146 result = G_TLS_BACKEND_GNUTLS_GET_CLASS (self)->create_database (self, &error);
149 g_warning ("couldn't load TLS file database: %s",
151 g_clear_error (&error);
156 self->priv->default_database = g_object_ref (result);
160 g_mutex_unlock (&self->priv->mutex);
166 g_tls_backend_gnutls_interface_init (GTlsBackendInterface *iface)
168 iface->get_certificate_type = g_tls_certificate_gnutls_get_type;
169 iface->get_client_connection_type = g_tls_client_connection_gnutls_get_type;
170 iface->get_server_connection_type = g_tls_server_connection_gnutls_get_type;
171 iface->get_file_database_type = g_tls_file_database_gnutls_get_type;
172 iface->get_default_database = g_tls_backend_gnutls_get_default_database;
175 /* Session cache support; all the details are sort of arbitrary. Note
176 * that having session_cache_cleanup() be a little bit slow isn't the
177 * end of the world, since it will still be faster than the network
178 * is. (NSS uses a linked list for its cache...)
181 G_LOCK_DEFINE_STATIC (session_cache_lock);
182 GHashTable *client_session_cache, *server_session_cache;
184 #define SESSION_CACHE_MAX_SIZE 50
185 #define SESSION_CACHE_MAX_AGE (60 * 60) /* one hour */
189 GBytes *session_data;
191 } GTlsBackendGnutlsCacheData;
194 session_cache_cleanup (GHashTable *cache)
198 GTlsBackendGnutlsCacheData *cache_data;
199 time_t expired = time (NULL) - SESSION_CACHE_MAX_AGE;
201 g_hash_table_iter_init (&iter, cache);
202 while (g_hash_table_iter_next (&iter, &key, &value))
205 if (cache_data->last_used < expired)
206 g_hash_table_iter_remove (&iter);
211 cache_data_free (gpointer data)
213 GTlsBackendGnutlsCacheData *cache_data = data;
215 g_bytes_unref (cache_data->session_id);
216 g_bytes_unref (cache_data->session_data);
217 g_slice_free (GTlsBackendGnutlsCacheData, cache_data);
221 get_session_cache (unsigned int type,
224 GHashTable **cache_p;
226 cache_p = (type == GNUTLS_CLIENT) ? &client_session_cache : &server_session_cache;
227 if (!*cache_p && create)
229 *cache_p = g_hash_table_new_full (g_bytes_hash, g_bytes_equal,
230 NULL, cache_data_free);
236 g_tls_backend_gnutls_store_session (unsigned int type,
238 GBytes *session_data)
240 GTlsBackendGnutlsCacheData *cache_data;
243 G_LOCK (session_cache_lock);
245 cache = get_session_cache (type, TRUE);
246 cache_data = g_hash_table_lookup (cache, session_id);
249 if (!g_bytes_equal (cache_data->session_data, session_data))
251 g_bytes_unref (cache_data->session_data);
252 cache_data->session_data = g_bytes_ref (session_data);
257 if (g_hash_table_size (cache) >= SESSION_CACHE_MAX_SIZE)
258 session_cache_cleanup (cache);
260 cache_data = g_slice_new (GTlsBackendGnutlsCacheData);
261 cache_data->session_id = g_bytes_ref (session_id);
262 cache_data->session_data = g_bytes_ref (session_data);
264 g_hash_table_insert (cache, cache_data->session_id, cache_data);
266 cache_data->last_used = time (NULL);
268 G_UNLOCK (session_cache_lock);
272 g_tls_backend_gnutls_remove_session (unsigned int type,
277 G_LOCK (session_cache_lock);
279 cache = get_session_cache (type, FALSE);
281 g_hash_table_remove (cache, session_id);
283 G_UNLOCK (session_cache_lock);
287 g_tls_backend_gnutls_lookup_session (unsigned int type,
290 GTlsBackendGnutlsCacheData *cache_data;
291 GBytes *session_data = NULL;
294 G_LOCK (session_cache_lock);
296 cache = get_session_cache (type, FALSE);
299 cache_data = g_hash_table_lookup (cache, session_id);
302 cache_data->last_used = time (NULL);
303 session_data = g_bytes_ref (cache_data->session_data);
307 G_UNLOCK (session_cache_lock);
313 g_tls_backend_gnutls_register (GIOModule *module)
315 g_tls_backend_gnutls_register_type (G_TYPE_MODULE (module));
316 g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
317 g_tls_backend_gnutls_get_type(),