Add new gcr library for crypto UI and related tasks. Implement GckParser
[platform/upstream/gcr.git] / gcr / gcr-internal.c
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 "gcr-internal.h"
25
26 #include "egg/egg-secure-memory.h"
27
28 #include <gcrypt.h>
29
30 static void
31 log_handler (gpointer unused, int unknown, const gchar *msg, va_list va)
32 {
33         /* TODO: Figure out additional arguments */
34         g_logv ("gcrypt", G_LOG_LEVEL_MESSAGE, msg, va);
35 }
36
37 static int
38 no_mem_handler (gpointer unused, size_t sz, unsigned int unknown)
39 {
40         /* TODO: Figure out additional arguments */
41         g_error ("couldn't allocate %lu bytes of memory",
42                  (unsigned long int)sz);
43         return 0;
44 }
45
46 static void
47 fatal_handler (gpointer unused, int unknown, const gchar *msg)
48 {
49         /* TODO: Figure out additional arguments */
50         g_log ("gcrypt", G_LOG_LEVEL_ERROR, "%s", msg);
51 }
52
53 static int
54 glib_thread_mutex_init (void **lock)
55 {
56         *lock = g_mutex_new ();
57         return 0;
58 }
59
60 static int
61 glib_thread_mutex_destroy (void **lock)
62 {
63         g_mutex_free (*lock);
64         return 0;
65 }
66
67 static int
68 glib_thread_mutex_lock (void **lock)
69 {
70         g_mutex_lock (*lock);
71         return 0;
72 }
73
74 static int
75 glib_thread_mutex_unlock (void **lock)
76 {
77         g_mutex_unlock (*lock);
78         return 0;
79 }
80
81 static struct gcry_thread_cbs glib_thread_cbs = {
82         GCRY_THREAD_OPTION_USER, NULL,
83         glib_thread_mutex_init, glib_thread_mutex_destroy,
84         glib_thread_mutex_lock, glib_thread_mutex_unlock,
85         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
86 };
87
88 void
89 _gcr_initialize (void)
90 {
91         static gsize gcrypt_initialized = FALSE;
92         unsigned seed;
93
94         if (g_once_init_enter (&gcrypt_initialized)) {
95
96                 /* Only initialize libgcrypt if it hasn't already been initialized */
97                 if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
98                         gcry_control (GCRYCTL_SET_THREAD_CBS, &glib_thread_cbs);
99                         gcry_check_version (LIBGCRYPT_VERSION);
100                         gcry_set_log_handler (log_handler, NULL);
101                         gcry_set_outofcore_handler (no_mem_handler, NULL);
102                         gcry_set_fatalerror_handler (fatal_handler, NULL);
103                         gcry_set_allocation_handler ((gcry_handler_alloc_t)g_malloc,
104                                                      (gcry_handler_alloc_t)egg_secure_alloc,
105                                                      egg_secure_check,
106                                                      (gcry_handler_realloc_t)egg_secure_realloc,
107                                                      egg_secure_free);
108                         gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
109                 }
110
111                 gcry_create_nonce (&seed, sizeof (seed));
112                 srand (seed);
113
114                 g_once_init_leave (&gcrypt_initialized, 1);
115         }
116 }