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