~ubuntu-branches/ubuntu/oneiric/libgnome-keyring/oneiric-201109181305

1 by Sebastien Bacher
Import upstream version 2.29.4git20100216
1
/*
2
 * gnome-keyring
3
 *
4
 * Copyright (C) 2008 Stefan Walter
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but
12
 * 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.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19
 * 02111-1307, USA.
20
 */
21
22
#include "config.h"
23
24
#include "egg-libgcrypt.h"
25
#include "egg-secure-memory.h"
26
27
#include <glib.h>
28
29
#include <gcrypt.h>
30
31
static void
32
log_handler (gpointer unused, int unknown, const gchar *msg, va_list va)
33
{
34
	/* TODO: Figure out additional arguments */
35
	g_logv ("gcrypt", G_LOG_LEVEL_MESSAGE, msg, va);
36
}
37
38
static int
39
no_mem_handler (gpointer unused, size_t sz, unsigned int unknown)
40
{
41
	/* TODO: Figure out additional arguments */
42
	g_error ("couldn't allocate %lu bytes of memory",
43
	         (unsigned long int)sz);
44
	return 0;
45
}
46
47
static void
48
fatal_handler (gpointer unused, int unknown, const gchar *msg)
49
{
50
	/* TODO: Figure out additional arguments */
51
	g_log ("gcrypt", G_LOG_LEVEL_ERROR, "%s", msg);
52
}
53
54
static int
55
glib_thread_mutex_init (void **lock)
56
{
57
	*lock = g_mutex_new ();
58
	return 0;
59
}
60
61
static int
62
glib_thread_mutex_destroy (void **lock)
63
{
64
	g_mutex_free (*lock);
65
	return 0;
66
}
67
68
static int
69
glib_thread_mutex_lock (void **lock)
70
{
71
	g_mutex_lock (*lock);
72
	return 0;
73
}
74
75
static int
76
glib_thread_mutex_unlock (void **lock)
77
{
78
	g_mutex_unlock (*lock);
79
	return 0;
80
}
81
82
static struct gcry_thread_cbs glib_thread_cbs = {
83
	GCRY_THREAD_OPTION_USER, NULL,
84
	glib_thread_mutex_init, glib_thread_mutex_destroy,
85
	glib_thread_mutex_lock, glib_thread_mutex_unlock,
86
	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
87
};
88
89
void
90
egg_libgcrypt_initialize (void)
91
{
92
	static volatile gsize gcrypt_initialized = 0;
93
	unsigned seed;
94
95
	if (g_once_init_enter (&gcrypt_initialized)) {
96
97
		/* Only initialize libgcrypt if it hasn't already been initialized */
98
		if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
99
			if (g_thread_supported())
100
				gcry_control (GCRYCTL_SET_THREAD_CBS, &glib_thread_cbs);
101
			gcry_check_version (LIBGCRYPT_VERSION);
102
			gcry_set_log_handler (log_handler, NULL);
103
			gcry_set_outofcore_handler (no_mem_handler, NULL);
104
			gcry_set_fatalerror_handler (fatal_handler, NULL);
105
			gcry_set_allocation_handler ((gcry_handler_alloc_t)g_malloc,
106
			                             (gcry_handler_alloc_t)egg_secure_alloc,
107
			                             egg_secure_check,
108
			                             (gcry_handler_realloc_t)egg_secure_realloc,
109
			                             egg_secure_free);
110
			gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
111
		}
112
113
		gcry_create_nonce (&seed, sizeof (seed));
114
		srand (seed);
115
116
		g_once_init_leave (&gcrypt_initialized, 1);
117
	}
118
}