gnome-keyring r1200 - in trunk: . gp11 gp11/tests pkcs11 pkcs11/tests



Author: nnielsen
Date: Thu Jul 24 17:30:50 2008
New Revision: 1200
URL: http://svn.gnome.org/viewvc/gnome-keyring?rev=1200&view=rev

Log:
	* gp11/gp11.h:
	* gp11/gp11-module.c:
	* gp11/tests/unit-test-gp11-module.c:
	* gp11/tests/unit-test-gp11-object.c:
	* gp11/tests/unit-test-gp11-session.c:
	* gp11/tests/unit-test-gp11-slot.c:
	* pkcs11/gkr-pkcs11-module.c:
	* pkcs11/Makefile.am:
	* pkcs11/tests/Makefile.am:
	* pkcs11/tests/unit-test-pkcs11-initialize.c:
	* configure.in: Add ability to gnome-keyring module to pass in the
	socket using the reserved argument. This is similar to how NSS
	libsoftkn3 likes to be initialized. 


Added:
   trunk/pkcs11/tests/   (props changed)
   trunk/pkcs11/tests/Makefile.am
   trunk/pkcs11/tests/unit-test-pkcs11-initialize.c
Modified:
   trunk/ChangeLog
   trunk/configure.in
   trunk/gp11/gp11-module.c
   trunk/gp11/gp11.h
   trunk/gp11/tests/unit-test-gp11-module.c
   trunk/gp11/tests/unit-test-gp11-object.c
   trunk/gp11/tests/unit-test-gp11-session.c
   trunk/gp11/tests/unit-test-gp11-slot.c
   trunk/pkcs11/Makefile.am
   trunk/pkcs11/gkr-pkcs11-module.c

Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in	(original)
+++ trunk/configure.in	Thu Jul 24 17:30:50 2008
@@ -510,6 +510,7 @@
 pam/Makefile
 pam/tests/Makefile
 pkcs11/Makefile
+pkcs11/tests/Makefile
 po/Makefile.in
 reference/Makefile
 tests/Makefile

Modified: trunk/gp11/gp11-module.c
==============================================================================
--- trunk/gp11/gp11-module.c	(original)
+++ trunk/gp11/gp11-module.c	Thu Jul 24 17:30:50 2008
@@ -176,7 +176,7 @@
 }
 
 GP11Module*
-gp11_module_initialize (const gchar *path, GError **err)
+gp11_module_initialize (const gchar *path, gpointer reserved, GError **err)
 {
 	CK_C_INITIALIZE_ARGS init_args;
 	CK_C_GetFunctionList get_function_list;
@@ -232,7 +232,7 @@
 	init_args.DestroyMutex = destroy_mutex;
 	init_args.LockMutex = lock_mutex;
 	init_args.UnlockMutex = unlock_mutex;
-	init_args.pReserved = NULL;
+	init_args.pReserved = reserved;
 	
 	/* Now initialize the module */
 	rv = (mod->funcs->C_Initialize) (&init_args);

Modified: trunk/gp11/gp11.h
==============================================================================
--- trunk/gp11/gp11.h	(original)
+++ trunk/gp11/gp11.h	Thu Jul 24 17:30:50 2008
@@ -215,6 +215,7 @@
 GType               gp11_module_get_type                    (void) G_GNUC_CONST;
 
 GP11Module*         gp11_module_initialize                  (const gchar *path, 
+                                                             gpointer reserved,
                                                              GError **err);
 
 GP11ModuleInfo*     gp11_module_get_info                    (GP11Module *module);

Modified: trunk/gp11/tests/unit-test-gp11-module.c
==============================================================================
--- trunk/gp11/tests/unit-test-gp11-module.c	(original)
+++ trunk/gp11/tests/unit-test-gp11-module.c	Thu Jul 24 17:30:50 2008
@@ -12,7 +12,7 @@
 	GError *err = NULL;
 
 	/* Successful load */
-	module = gp11_module_initialize (".libs/libgp11-test-module.so", &err);
+	module = gp11_module_initialize (".libs/libgp11-test-module.so", NULL, &err);
 	SUCCESS_RES (module, err);
 }
 
@@ -27,11 +27,11 @@
 	GError *err = NULL;
 	
 	/* Shouldn't be able to load modules */
-	invalid = gp11_module_initialize ("blah-blah-non-existant", &err);
+	invalid = gp11_module_initialize ("blah-blah-non-existant", NULL, &err);
 	FAIL_RES (invalid, err);
 
 	/* Shouldn't be able to load any file successfully */ 
-	invalid = gp11_module_initialize ("/usr/lib/libm.so", &err);
+	invalid = gp11_module_initialize ("/usr/lib/libm.so", NULL, &err);
 	FAIL_RES (invalid, err);
 
 }

Modified: trunk/gp11/tests/unit-test-gp11-object.c
==============================================================================
--- trunk/gp11/tests/unit-test-gp11-object.c	(original)
+++ trunk/gp11/tests/unit-test-gp11-object.c	Thu Jul 24 17:30:50 2008
@@ -19,7 +19,7 @@
 	GList *slots;
 	
 	/* Successful load */
-	module = gp11_module_initialize (".libs/libgp11-test-module.so", &err);
+	module = gp11_module_initialize (".libs/libgp11-test-module.so", NULL, &err);
 	SUCCESS_RES (module, err);
 	
 	slots = gp11_module_get_slots (module, TRUE);

Modified: trunk/gp11/tests/unit-test-gp11-session.c
==============================================================================
--- trunk/gp11/tests/unit-test-gp11-session.c	(original)
+++ trunk/gp11/tests/unit-test-gp11-session.c	Thu Jul 24 17:30:50 2008
@@ -18,7 +18,7 @@
 	GList *slots;
 	
 	/* Successful load */
-	module = gp11_module_initialize (".libs/libgp11-test-module.so", &err);
+	module = gp11_module_initialize (".libs/libgp11-test-module.so", NULL, &err);
 	SUCCESS_RES (module, err);
 	
 	slots = gp11_module_get_slots (module, TRUE);

Modified: trunk/gp11/tests/unit-test-gp11-slot.c
==============================================================================
--- trunk/gp11/tests/unit-test-gp11-slot.c	(original)
+++ trunk/gp11/tests/unit-test-gp11-slot.c	Thu Jul 24 17:30:50 2008
@@ -14,7 +14,7 @@
 	GList *slots;
 	
 	/* Successful load */
-	module = gp11_module_initialize (".libs/libgp11-test-module.so", &err);
+	module = gp11_module_initialize (".libs/libgp11-test-module.so", NULL, &err);
 	SUCCESS_RES (module, err);
 	
 	slots = gp11_module_get_slots (module, TRUE);

Modified: trunk/pkcs11/Makefile.am
==============================================================================
--- trunk/pkcs11/Makefile.am	(original)
+++ trunk/pkcs11/Makefile.am	Thu Jul 24 17:30:50 2008
@@ -30,3 +30,11 @@
 	$(top_builddir)/common/libgkr-module-common.la 
 
 
+if WITH_TESTS
+TESTS_DIR = tests
+else
+TESTS_DIR = 
+endif
+
+SUBDIRS = . $(TESTS_DIR)
+

Modified: trunk/pkcs11/gkr-pkcs11-module.c
==============================================================================
--- trunk/pkcs11/gkr-pkcs11-module.c	(original)
+++ trunk/pkcs11/gkr-pkcs11-module.c	Thu Jul 24 17:30:50 2008
@@ -37,6 +37,7 @@
 #include <sys/un.h>
 
 #include <stdlib.h>
+#include <ctype.h>
 #include <stdint.h>
 #include <pthread.h>
 #include <unistd.h>
@@ -173,6 +174,100 @@
 }
 
 /* -----------------------------------------------------------------------------
+ * MODULE ARGUMENTS
+ */
+
+static void
+parse_argument (char *arg)
+{
+	char *value;
+	
+	value = arg + strcspn (arg, ":=");
+	if (!*value)
+		value = NULL;
+	else 
+		*(value++) = 0;
+
+	/* Setup the socket path from the arguments */
+	if (strcmp (arg, "socket") == 0) {
+		strncpy (socket_path, value, sizeof (socket_path));
+		socket_path[sizeof (socket_path) - 1] = 0;		
+	} else {
+		WARN(("unrecognized argument: %s", arg));
+	}
+}
+
+static void
+parse_arguments (const char *string) 
+{
+	char quote = '\0';
+	char *src, *dup, *at, *arg;
+	
+	if (!string)
+		return;
+	
+	src = dup = strdup (string);
+	if (!dup) {
+		WARN (("couldn't allocate memory for argument string"));
+		return;
+	}
+
+	arg = at = src;
+	for (src = dup; *src; src++) {
+		
+		/* Matching quote */
+		if (quote == *src) {
+			quote = '\0';
+			
+		/* Inside of quotes */
+		} else if (quote != '\0') {
+			if (*src == '\\') {
+				*at++ = *src++;
+				if (!*src) {
+					WARN (("couldn't parse argument string: %s", string));
+					goto done;
+				}
+				if (*src != quote) 
+					*at++ = '\\';
+			}
+			*at++ = *src;
+			
+		/* Space, not inside of quotes */
+		} else if (isspace(*src)) {
+			*at = 0;
+			parse_argument (arg);
+			arg = at;
+			
+		/* Other character outside of quotes */
+		} else {
+			switch (*src) {
+			case '\'':
+			case '"':
+				quote = *src;
+				break;
+			case '\\':
+				*at++ = *src++;
+				if (!*src) {
+					WARN (("couldn't parse argument string: %s", string));
+					goto done;
+				}
+				/* fall through */
+			default:
+				*at++ = *src;
+				break;
+			}
+		}
+	}
+
+	
+	if (at != arg) 
+		parse_argument (arg);
+	
+done:
+	free (dup);
+}
+
+/* -----------------------------------------------------------------------------
  * CALL SESSION
  */
 
@@ -1208,7 +1303,6 @@
 	CK_RV ret = CKR_OK;
 	pid_t initialize_pid;
 	char *path;
-	int l;
 	
 	DBG (("C_Initialize: enter"));
 
@@ -1226,7 +1320,6 @@
 
 		/* pReserved must be NULL */
 		args = init_args;
-		PREREQ (!args->pReserved, CKR_ARGUMENTS_BAD);
 
 		/* ALL supplied function pointers need to have the value either NULL or non-NULL. */
 		supplied_ok = (args->CreateMutex == NULL && args->DestroyMutex == NULL &&
@@ -1237,11 +1330,18 @@
 
 		/*
 		 * When the CKF_OS_LOCKING_OK flag isn't set and mutex function pointers are supplied
-		 * by an application, return an error.  DBus must be able to use its own locks.
+		 * by an application, return an error.  We must be able to use its own locks.
 		 */
 		if (!(args->flags & CKF_OS_LOCKING_OK) && (args->CreateMutex != NULL)) {
 			PREREQ (FALSE, CKR_CANT_LOCK);
 		}
+		
+		/* 
+		 * We support setting the socket path and other arguments from from the 
+		 * pReserved pointer, similar to how NSS PKCS#11 components are initialized. 
+		 */ 
+		if (args->pReserved) 
+			parse_arguments ((const char*)args->pReserved);			
 	}
 
 	/* Main initialization */
@@ -1267,16 +1367,16 @@
 			crypto_pid = initialize_pid;
 			
 			/* Lookup the socket path, append '.pkcs11' */
-			socket_path[0] = 0;
-			path = getenv ("GNOME_KEYRING_SOCKET");
-			if (path && path[0]) {
-				l = sizeof (socket_path) - 1;
-				strncpy (socket_path, path, l);
-				strncat (socket_path, ".pkcs11", l);
-				socket_path[l] = 0;
-				
-				DBG (("gnome-keyring pkcs11 socket is: %s", socket_path));
+			if (socket_path[0] == 0) {
+				socket_path[0] = 0;
+				path = getenv ("GNOME_KEYRING_SOCKET");
+				if (path && path[0]) {
+					snprintf (socket_path, sizeof (socket_path), "%s.pkcs11", path);
+					socket_path[sizeof (socket_path) - 1] = 0;
+				}
 			}
+			
+			DBG (("gnome-keyring pkcs11 socket is: %s", socket_path));
 		}
 
 	pthread_mutex_unlock (&global_mutex);

Added: trunk/pkcs11/tests/Makefile.am
==============================================================================
--- (empty file)
+++ trunk/pkcs11/tests/Makefile.am	Thu Jul 24 17:30:50 2008
@@ -0,0 +1,13 @@
+UNIT_AUTO = \
+	unit-test-pkcs11-initialize.c
+
+UNIT_FLAGS = \
+	-DEXTERNAL_TEST
+	
+UNIT_PROMPT = 
+
+UNIT_LIBS = \
+	$(GIO_LIBS) \
+	$(top_builddir)/gp11/libgp11.la
+	
+include $(top_srcdir)/tests/gtest.make

Added: trunk/pkcs11/tests/unit-test-pkcs11-initialize.c
==============================================================================
--- (empty file)
+++ trunk/pkcs11/tests/unit-test-pkcs11-initialize.c	Thu Jul 24 17:30:50 2008
@@ -0,0 +1,20 @@
+
+#include <glib.h>
+#include <string.h>
+
+#include "run-auto-test.h"
+
+#include "gp11/gp11.h"
+
+DEFINE_TEST(module_arguments)
+{
+	GP11Module *module;
+	GError *err = NULL;
+
+	/* Test that extra arguments allow successful initialize */
+	module = gp11_module_initialize ("../.libs/gnome-keyring-pkcs11.so", "socket='/tmp/blah' invalid=yes", &err);
+	g_assert (module);
+	g_assert (!err);
+	
+	g_object_unref (module);
+}



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