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/>.
19 * In addition, when the library is used with OpenSSL, a special
20 * exception applies. Refer to the LICENSE_EXCEPTION file for details.
29 #include <gnutls/gnutls.h>
31 #include "gtlsbackend-gnutls.h"
32 #include "gtlscertificate-gnutls.h"
33 #include "gtlsclientconnection-gnutls.h"
34 #include "gtlsfiledatabase-gnutls.h"
35 #include "gtlsserverconnection-gnutls.h"
37 struct _GTlsBackendGnutlsPrivate
40 GTlsDatabase *default_database;
43 static void g_tls_backend_gnutls_interface_init (GTlsBackendInterface *iface);
45 G_DEFINE_DYNAMIC_TYPE_EXTENDED (GTlsBackendGnutls, g_tls_backend_gnutls, G_TYPE_OBJECT, 0,
46 G_IMPLEMENT_INTERFACE_DYNAMIC (G_TYPE_TLS_BACKEND,
47 g_tls_backend_gnutls_interface_init);)
49 #ifdef GTLS_GNUTLS_DEBUG
51 gtls_log_func (int level, const char *msg)
53 g_print ("GTLS: %s", msg);
58 gtls_gnutls_init (gpointer data)
60 gnutls_global_init ();
62 #ifdef GTLS_GNUTLS_DEBUG
63 gnutls_global_set_log_function (gtls_log_func);
64 gnutls_global_set_log_level (9);
67 /* Leak the module to keep it from being unloaded. */
68 g_type_plugin_use (g_type_get_plugin (G_TYPE_TLS_BACKEND_GNUTLS));
72 static GOnce gnutls_inited = G_ONCE_INIT;
75 g_tls_backend_gnutls_init (GTlsBackendGnutls *backend)
77 /* Once we call gtls_gnutls_init(), we can't allow the module to be
78 * unloaded (since if gnutls gets unloaded but gcrypt doesn't, then
79 * gcrypt will have dangling pointers to gnutls's mutex functions).
80 * So we initialize it from here rather than at class init time so
81 * that it doesn't happen unless the app is actually using TLS (as
82 * opposed to just calling g_io_modules_scan_all_in_directory()).
84 g_once (&gnutls_inited, gtls_gnutls_init, NULL);
86 backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend, G_TYPE_TLS_BACKEND_GNUTLS, GTlsBackendGnutlsPrivate);
87 g_mutex_init (&backend->priv->mutex);
91 g_tls_backend_gnutls_finalize (GObject *object)
93 GTlsBackendGnutls *backend = G_TLS_BACKEND_GNUTLS (object);
95 if (backend->priv->default_database)
96 g_object_unref (backend->priv->default_database);
97 g_mutex_clear (&backend->priv->mutex);
99 G_OBJECT_CLASS (g_tls_backend_gnutls_parent_class)->finalize (object);
103 g_tls_backend_gnutls_real_create_database (GTlsBackendGnutls *self,
106 const gchar *anchor_file = NULL;
107 #ifdef GTLS_SYSTEM_CA_FILE
108 anchor_file = GTLS_SYSTEM_CA_FILE;
110 return g_tls_file_database_new (anchor_file, error);
114 g_tls_backend_gnutls_class_init (GTlsBackendGnutlsClass *backend_class)
116 GObjectClass *gobject_class = G_OBJECT_CLASS (backend_class);
117 gobject_class->finalize = g_tls_backend_gnutls_finalize;
118 backend_class->create_database = g_tls_backend_gnutls_real_create_database;
119 g_type_class_add_private (backend_class, sizeof (GTlsBackendGnutlsPrivate));
123 g_tls_backend_gnutls_class_finalize (GTlsBackendGnutlsClass *backend_class)
128 g_tls_backend_gnutls_get_default_database (GTlsBackend *backend)
130 GTlsBackendGnutls *self = G_TLS_BACKEND_GNUTLS (backend);
131 GTlsDatabase *result;
132 GError *error = NULL;
134 g_mutex_lock (&self->priv->mutex);
136 if (self->priv->default_database)
138 result = g_object_ref (self->priv->default_database);
142 g_assert (G_TLS_BACKEND_GNUTLS_GET_CLASS (self)->create_database);
143 result = G_TLS_BACKEND_GNUTLS_GET_CLASS (self)->create_database (self, &error);
146 g_warning ("couldn't load TLS file database: %s",
148 g_clear_error (&error);
153 self->priv->default_database = g_object_ref (result);
157 g_mutex_unlock (&self->priv->mutex);
163 g_tls_backend_gnutls_interface_init (GTlsBackendInterface *iface)
165 iface->get_certificate_type = g_tls_certificate_gnutls_get_type;
166 iface->get_client_connection_type = g_tls_client_connection_gnutls_get_type;
167 iface->get_server_connection_type = g_tls_server_connection_gnutls_get_type;
168 iface->get_file_database_type = g_tls_file_database_gnutls_get_type;
169 iface->get_default_database = g_tls_backend_gnutls_get_default_database;
172 /* Session cache support; all the details are sort of arbitrary. Note
173 * that having session_cache_cleanup() be a little bit slow isn't the
174 * end of the world, since it will still be faster than the network
175 * is. (NSS uses a linked list for its cache...)
178 G_LOCK_DEFINE_STATIC (session_cache_lock);
179 GHashTable *client_session_cache, *server_session_cache;
181 #define SESSION_CACHE_MAX_SIZE 50
182 #define SESSION_CACHE_MAX_AGE (60 * 60) /* one hour */
186 GBytes *session_data;
188 } GTlsBackendGnutlsCacheData;
191 session_cache_cleanup (GHashTable *cache)
195 GTlsBackendGnutlsCacheData *cache_data;
196 time_t expired = time (NULL) - SESSION_CACHE_MAX_AGE;
198 g_hash_table_iter_init (&iter, cache);
199 while (g_hash_table_iter_next (&iter, &key, &value))
202 if (cache_data->last_used < expired)
203 g_hash_table_iter_remove (&iter);
208 cache_data_free (gpointer data)
210 GTlsBackendGnutlsCacheData *cache_data = data;
212 g_bytes_unref (cache_data->session_id);
213 g_bytes_unref (cache_data->session_data);
214 g_slice_free (GTlsBackendGnutlsCacheData, cache_data);
218 get_session_cache (unsigned int type,
221 GHashTable **cache_p;
223 cache_p = (type == GNUTLS_CLIENT) ? &client_session_cache : &server_session_cache;
224 if (!*cache_p && create)
226 *cache_p = g_hash_table_new_full (g_bytes_hash, g_bytes_equal,
227 NULL, cache_data_free);
233 g_tls_backend_gnutls_store_session (unsigned int type,
235 GBytes *session_data)
237 GTlsBackendGnutlsCacheData *cache_data;
240 G_LOCK (session_cache_lock);
242 cache = get_session_cache (type, TRUE);
243 cache_data = g_hash_table_lookup (cache, session_id);
246 if (!g_bytes_equal (cache_data->session_data, session_data))
248 g_bytes_unref (cache_data->session_data);
249 cache_data->session_data = g_bytes_ref (session_data);
254 if (g_hash_table_size (cache) >= SESSION_CACHE_MAX_SIZE)
255 session_cache_cleanup (cache);
257 cache_data = g_slice_new (GTlsBackendGnutlsCacheData);
258 cache_data->session_id = g_bytes_ref (session_id);
259 cache_data->session_data = g_bytes_ref (session_data);
261 g_hash_table_insert (cache, cache_data->session_id, cache_data);
263 cache_data->last_used = time (NULL);
265 G_UNLOCK (session_cache_lock);
269 g_tls_backend_gnutls_remove_session (unsigned int type,
274 G_LOCK (session_cache_lock);
276 cache = get_session_cache (type, FALSE);
278 g_hash_table_remove (cache, session_id);
280 G_UNLOCK (session_cache_lock);
284 g_tls_backend_gnutls_lookup_session (unsigned int type,
287 GTlsBackendGnutlsCacheData *cache_data;
288 GBytes *session_data = NULL;
291 G_LOCK (session_cache_lock);
293 cache = get_session_cache (type, FALSE);
296 cache_data = g_hash_table_lookup (cache, session_id);
299 cache_data->last_used = time (NULL);
300 session_data = g_bytes_ref (cache_data->session_data);
304 G_UNLOCK (session_cache_lock);
310 g_tls_backend_gnutls_register (GIOModule *module)
312 g_tls_backend_gnutls_register_type (G_TYPE_MODULE (module));
313 g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
314 g_tls_backend_gnutls_get_type(),