Branch data Line data Source code
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., 51 Franklin Street, Fifth Floor, Boston,
19 : : * MA 02110-1301 USA
20 : : *
21 : : * Author: Stef Walter <stefw@thewalter.net>
22 : : */
23 : :
24 : : #include "config.h"
25 : :
26 : : #include "egg-libgcrypt.h"
27 : : #include "egg-secure-memory.h"
28 : :
29 : : #include <glib.h>
30 : :
31 : : #include <gcrypt.h>
32 : :
33 : : #include <errno.h>
34 : :
35 : 1902637 : EGG_SECURE_DECLARE (libgcrypt);
36 : :
37 : : static void
38 : 0 : log_handler (gpointer unused, int unknown, const gchar *msg, va_list va)
39 : : {
40 : : /* TODO: Figure out additional arguments */
41 : 0 : g_logv ("gcrypt", G_LOG_LEVEL_MESSAGE, msg, va);
42 : 0 : }
43 : :
44 : : static int
45 : 0 : no_mem_handler (gpointer unused, size_t sz, unsigned int unknown)
46 : : {
47 : : /* TODO: Figure out additional arguments */
48 : 0 : g_error ("couldn't allocate %lu bytes of memory",
49 : : (unsigned long int)sz);
50 : : return 0;
51 : : }
52 : :
53 : : static void
54 : 0 : fatal_handler (gpointer unused, int unknown, const gchar *msg)
55 : : {
56 : : /* TODO: Figure out additional arguments */
57 : 0 : g_log ("gcrypt", G_LOG_LEVEL_ERROR, "%s", msg);
58 : 0 : }
59 : :
60 : : #if GCRYPT_VERSION_NUMBER < 0x010600
61 : : GCRY_THREAD_OPTION_PTHREAD_IMPL;
62 : : #endif
63 : :
64 : : void
65 : 69 : egg_libgcrypt_initialize (void)
66 : : {
67 : : static size_t gcrypt_initialized = 0;
68 : : unsigned seed;
69 : :
70 [ + + + - : 69 : if (g_once_init_enter (&gcrypt_initialized)) {
+ + ]
71 : :
72 : : /* Only initialize libgcrypt if it hasn't already been initialized */
73 [ + - ]: 23 : if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
74 : : #if GCRYPT_VERSION_NUMBER < 0x010600
75 : : gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
76 : : #endif
77 : 23 : gcry_check_version (LIBGCRYPT_VERSION);
78 : 23 : gcry_set_log_handler (log_handler, NULL);
79 : 23 : gcry_set_outofcore_handler (no_mem_handler, NULL);
80 : 23 : gcry_set_fatalerror_handler (fatal_handler, NULL);
81 : 23 : gcry_set_allocation_handler ((gcry_handler_alloc_t)g_malloc,
82 : : (gcry_handler_alloc_t)egg_secure_alloc,
83 : : egg_secure_check,
84 : : (gcry_handler_realloc_t)egg_secure_realloc,
85 : : egg_secure_free);
86 : 23 : gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
87 : : }
88 : :
89 : 23 : gcry_create_nonce (&seed, sizeof (seed));
90 : 23 : srand (seed);
91 : :
92 : 23 : g_once_init_leave (&gcrypt_initialized, 1);
93 : : }
94 : 69 : }
|