gnome-keyring r1522 - in trunk: . daemon egg egg/tests gcr pkcs11/gck



Author: nnielsen
Date: Sat Feb  7 23:54:04 2009
New Revision: 1522
URL: http://svn.gnome.org/viewvc/gnome-keyring?rev=1522&view=rev

Log:
Centralize the way that libgcrypt is initialized from our code.

So the different components don't step on each other's toes.

Added:
   trunk/egg/egg-libgcrypt.c
   trunk/egg/egg-libgcrypt.h
Modified:
   trunk/ChangeLog
   trunk/daemon/gkr-daemon.c
   trunk/egg/Makefile.am
   trunk/egg/tests/unit-test-symkey.c
   trunk/gcr/gcr-library.c
   trunk/pkcs11/gck/gck-crypto.c

Modified: trunk/daemon/gkr-daemon.c
==============================================================================
--- trunk/daemon/gkr-daemon.c	(original)
+++ trunk/daemon/gkr-daemon.c	Sat Feb  7 23:54:04 2009
@@ -27,9 +27,11 @@
 #include "common/gkr-async.h"
 #include "common/gkr-cleanup.h"
 #include "common/gkr-daemon-util.h"
+#include "common/gkr-unix-signal.h"
+
+#include "egg/egg-libgcrypt.h"
 #include "egg/egg-secure-memory.h"
 #include "egg/egg-unix-credentials.h"
-#include "common/gkr-unix-signal.h"
 
 #include "keyrings/gkr-keyring-login.h"
 
@@ -686,6 +688,8 @@
 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 #endif
 
+	egg_libgcrypt_initialize ();
+	
 	/* Send all warning or error messages to syslog */
 	prepare_logging ();
 	

Modified: trunk/egg/Makefile.am
==============================================================================
--- trunk/egg/Makefile.am	(original)
+++ trunk/egg/Makefile.am	Sat Feb  7 23:54:04 2009
@@ -21,6 +21,7 @@
 	egg-asn1.c egg-asn1.h \
 	egg-buffer.c egg-buffer.h \
 	egg-hex.c egg-hex.h \
+	egg-libgcrypt.c egg-libgcrypt.h \
 	egg-openssl.c egg-openssl.h \
 	egg-unix-credentials.c egg-unix-credentials.h \
 	egg-secure-memory.c egg-secure-memory.h \

Added: trunk/egg/egg-libgcrypt.c
==============================================================================
--- (empty file)
+++ trunk/egg/egg-libgcrypt.c	Sat Feb  7 23:54:04 2009
@@ -0,0 +1,118 @@
+/* 
+ * gnome-keyring
+ * 
+ * Copyright (C) 2008 Stefan Walter
+ * 
+ * This program is free software; you can redistribute it and/or modify 
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *  
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *  
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.  
+ */
+
+#include "config.h"
+
+#include "egg-libgcrypt.h"
+#include "egg-secure-memory.h"
+
+#include <glib.h>
+
+#include <gcrypt.h>
+
+static void
+log_handler (gpointer unused, int unknown, const gchar *msg, va_list va)
+{
+	/* TODO: Figure out additional arguments */
+	g_logv ("gcrypt", G_LOG_LEVEL_MESSAGE, msg, va);
+}
+
+static int 
+no_mem_handler (gpointer unused, size_t sz, unsigned int unknown)
+{
+	/* TODO: Figure out additional arguments */
+	g_error ("couldn't allocate %lu bytes of memory", 
+	         (unsigned long int)sz);
+	return 0;
+}
+
+static void
+fatal_handler (gpointer unused, int unknown, const gchar *msg)
+{
+	/* TODO: Figure out additional arguments */
+	g_log ("gcrypt", G_LOG_LEVEL_ERROR, "%s", msg);
+}
+
+static int
+glib_thread_mutex_init (void **lock)
+{
+	*lock = g_mutex_new ();
+	return 0;
+}
+
+static int 
+glib_thread_mutex_destroy (void **lock)
+{
+	g_mutex_free (*lock);
+	return 0;
+}
+
+static int 
+glib_thread_mutex_lock (void **lock)
+{
+	g_mutex_lock (*lock);
+	return 0;
+}
+
+static int 
+glib_thread_mutex_unlock (void **lock)
+{
+	g_mutex_unlock (*lock);
+	return 0;
+}
+
+static struct gcry_thread_cbs glib_thread_cbs = {
+	GCRY_THREAD_OPTION_USER, NULL,
+	glib_thread_mutex_init, glib_thread_mutex_destroy,
+	glib_thread_mutex_lock, glib_thread_mutex_unlock,
+	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 
+};
+
+void
+egg_libgcrypt_initialize (void)
+{
+	static volatile gsize gcrypt_initialized = 0;
+	unsigned seed;
+
+	if (g_once_init_enter (&gcrypt_initialized)) {
+		
+		/* Only initialize libgcrypt if it hasn't already been initialized */
+		if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
+			if (g_thread_supported())
+				gcry_control (GCRYCTL_SET_THREAD_CBS, &glib_thread_cbs);
+			gcry_check_version (LIBGCRYPT_VERSION);
+			gcry_set_log_handler (log_handler, NULL);
+			gcry_set_outofcore_handler (no_mem_handler, NULL);
+			gcry_set_fatalerror_handler (fatal_handler, NULL);
+			gcry_set_allocation_handler ((gcry_handler_alloc_t)g_malloc, 
+			                             (gcry_handler_alloc_t)egg_secure_alloc, 
+			                             egg_secure_check, 
+			                             (gcry_handler_realloc_t)egg_secure_realloc, 
+			                             egg_secure_free);
+			gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
+		}
+		
+		gcry_create_nonce (&seed, sizeof (seed));
+		srand (seed);
+
+		g_once_init_leave (&gcrypt_initialized, 1);
+	}
+}

Added: trunk/egg/egg-libgcrypt.h
==============================================================================
--- (empty file)
+++ trunk/egg/egg-libgcrypt.h	Sat Feb  7 23:54:04 2009
@@ -0,0 +1,30 @@
+/* 
+ * gnome-keyring
+ * 
+ * Copyright (C) 2008 Stefan Walter
+ * 
+ * This program is free software; you can redistribute it and/or modify 
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *  
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *  
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.  
+ */
+
+#include "config.h"
+
+#ifndef EGG_LIBGCRYPT_H_
+#define EGG_LIBGCRYPT_H_
+
+/* Initializes libgcrypt for use in a glib program */
+void egg_libgcrypt_initialize (void);
+
+#endif /* EGG_LIBGCRYPT_H_ */

Modified: trunk/egg/tests/unit-test-symkey.c
==============================================================================
--- trunk/egg/tests/unit-test-symkey.c	(original)
+++ trunk/egg/tests/unit-test-symkey.c	Sat Feb  7 23:54:04 2009
@@ -27,6 +27,7 @@
 
 #include "run-auto-test.h"
 
+#include "egg-libgcrypt.h"
 #include "egg-secure-memory.h"
 #include "egg-symkey.h"
 
@@ -34,12 +35,7 @@
 
 DEFINE_SETUP(crypto_setup)
 {
-	gcry_check_version (LIBGCRYPT_VERSION);
-	gcry_set_allocation_handler ((gcry_handler_alloc_t)g_malloc, 
-	                             (gcry_handler_alloc_t)egg_secure_alloc, 
-	                             egg_secure_check, 
-	                             (gcry_handler_realloc_t)egg_secure_realloc, 
-	                             egg_secure_free);
+	egg_libgcrypt_initialize ();
 }
 
 DEFINE_TEARDOWN(crypto_setup)

Modified: trunk/gcr/gcr-library.c
==============================================================================
--- trunk/gcr/gcr-library.c	(original)
+++ trunk/gcr/gcr-library.c	Sat Feb  7 23:54:04 2009
@@ -25,6 +25,7 @@
 #include "gcr-types.h"
 #include "gcr-internal.h"
 
+#include "egg/egg-libgcrypt.h"
 #include "egg/egg-secure-memory.h"
 
 #include <gcrypt.h>
@@ -106,97 +107,18 @@
 	return g_realloc (p, sz);
 }
 
-/* ------------------------------------------------------------------------------
- * GCRYPT HOOKS
- */
-
-static void
-log_handler (gpointer unused, int unknown, const gchar *msg, va_list va)
-{
-	/* TODO: Figure out additional arguments */
-	g_logv ("gcrypt", G_LOG_LEVEL_MESSAGE, msg, va);
-}
-
-static int 
-no_mem_handler (gpointer unused, size_t sz, unsigned int unknown)
-{
-	/* TODO: Figure out additional arguments */
-	g_error ("couldn't allocate %lu bytes of memory", 
-	         (unsigned long int)sz);
-	return 0;
-}
-
-static void
-fatal_handler (gpointer unused, int unknown, const gchar *msg)
-{
-	/* TODO: Figure out additional arguments */
-	g_log ("gcrypt", G_LOG_LEVEL_ERROR, "%s", msg);
-}
-
-static int
-glib_thread_mutex_init (void **lock)
-{
-	*lock = g_mutex_new ();
-	return 0;
-}
-
-static int 
-glib_thread_mutex_destroy (void **lock)
-{
-	g_mutex_free (*lock);
-	return 0;
-}
-
-static int 
-glib_thread_mutex_lock (void **lock)
-{
-	g_mutex_lock (*lock);
-	return 0;
-}
-
-static int 
-glib_thread_mutex_unlock (void **lock)
-{
-	g_mutex_unlock (*lock);
-	return 0;
-}
-
-static struct gcry_thread_cbs glib_thread_cbs = {
-	GCRY_THREAD_OPTION_USER, NULL,
-	glib_thread_mutex_init, glib_thread_mutex_destroy,
-	glib_thread_mutex_lock, glib_thread_mutex_unlock,
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 
-};
-
 void
 _gcr_initialize (void)
 {
 	static volatile gsize gcr_initialized = 0;
 	GP11Module *module;
 	GError *error = NULL;
-	unsigned seed;
+	
+	/* Initialize the libgcrypt library if needed */
+	egg_libgcrypt_initialize ();
 
 	if (g_once_init_enter (&gcr_initialized)) {
 		
-		/* Only initialize libgcrypt if it hasn't already been initialized */
-		if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
-			if (g_thread_supported())
-				gcry_control (GCRYCTL_SET_THREAD_CBS, &glib_thread_cbs);
-			gcry_check_version (LIBGCRYPT_VERSION);
-			gcry_set_log_handler (log_handler, NULL);
-			gcry_set_outofcore_handler (no_mem_handler, NULL);
-			gcry_set_fatalerror_handler (fatal_handler, NULL);
-			gcry_set_allocation_handler ((gcry_handler_alloc_t)g_malloc, 
-			                             (gcry_handler_alloc_t)egg_secure_alloc, 
-			                             egg_secure_check, 
-			                             (gcry_handler_realloc_t)egg_secure_realloc, 
-			                             egg_secure_free);
-			gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
-		}
-		
-		gcry_create_nonce (&seed, sizeof (seed));
-		srand (seed);
-
 		/* TODO: This needs reworking for multiple modules */
 		module = gp11_module_initialize (PKCS11_MODULE_PATH, NULL, &error);
 		if (module) 

Modified: trunk/pkcs11/gck/gck-crypto.c
==============================================================================
--- trunk/pkcs11/gck/gck-crypto.c	(original)
+++ trunk/pkcs11/gck/gck-crypto.c	Sat Feb  7 23:54:04 2009
@@ -23,6 +23,7 @@
 
 #include "gck-crypto.h"
 
+#include "egg/egg-libgcrypt.h"
 #include "egg/egg-secure-memory.h"
 
 /* ----------------------------------------------------------------------------
@@ -981,90 +982,9 @@
  * INITIALIZATION
  */
 
-static void
-log_handler (gpointer unused, int unknown, const gchar *msg, va_list va)
-{
-	/* TODO: Figure out additional arguments */
-	g_logv ("gcrypt", G_LOG_LEVEL_MESSAGE, msg, va);
-}
-
-static int 
-no_mem_handler (gpointer unused, size_t sz, unsigned int unknown)
-{
-	/* TODO: Figure out additional arguments */
-	g_error ("couldn't allocate %lu bytes of memory", 
-	         (unsigned long int)sz);
-	return 0;
-}
-
-static void
-fatal_handler (gpointer unused, int unknown, const gchar *msg)
-{
-	/* TODO: Figure out additional arguments */
-	g_log ("gcrypt", G_LOG_LEVEL_ERROR, "%s", msg);
-}
-
-static int
-glib_thread_mutex_init (void **lock)
-{
-	*lock = g_mutex_new ();
-	return 0;
-}
-
-static int 
-glib_thread_mutex_destroy (void **lock)
-{
-	g_mutex_free (*lock);
-	return 0;
-}
-
-static int 
-glib_thread_mutex_lock (void **lock)
-{
-	g_mutex_lock (*lock);
-	return 0;
-}
-
-static int 
-glib_thread_mutex_unlock (void **lock)
-{
-	g_mutex_unlock (*lock);
-	return 0;
-}
-
-static struct gcry_thread_cbs glib_thread_cbs = {
-	GCRY_THREAD_OPTION_USER, NULL,
-	glib_thread_mutex_init, glib_thread_mutex_destroy,
-	glib_thread_mutex_lock, glib_thread_mutex_unlock,
-	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 
-};
 
 void
 gck_crypto_initialize (void)
 {
-	static gsize gcrypt_initialized = FALSE;
-	unsigned seed;
-
-	if (g_once_init_enter (&gcrypt_initialized)) {
-		
-		/* Only initialize libgcrypt if it hasn't already been initialized */
-		if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
-			gcry_control (GCRYCTL_SET_THREAD_CBS, &glib_thread_cbs);
-			gcry_check_version (LIBGCRYPT_VERSION);
-			gcry_set_log_handler (log_handler, NULL);
-			gcry_set_outofcore_handler (no_mem_handler, NULL);
-			gcry_set_fatalerror_handler (fatal_handler, NULL);
-			gcry_set_allocation_handler ((gcry_handler_alloc_t)g_malloc, 
-			                             (gcry_handler_alloc_t)egg_secure_alloc, 
-			                             egg_secure_check, 
-			                             (gcry_handler_realloc_t)egg_secure_realloc, 
-			                             egg_secure_free);
-			gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
-		}
-		
-		gcry_create_nonce (&seed, sizeof (seed));
-		srand (seed);
-		
-		g_once_init_leave (&gcrypt_initialized, 1);
-	}
+	egg_libgcrypt_initialize ();
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]