summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am10
-rw-r--r--src/module.c44
-rw-r--r--src/msg.c147
-rw-r--r--src/p11-tests.c43
-rw-r--r--src/p11-tests.h26
5 files changed, 268 insertions, 2 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 7d13b5e..d8cbb2d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,3 +1,9 @@
+AM_CFLAGS = -I$(top_srcdir)
+
bin_PROGRAMS = p11-tests
-p11_tests_SOURCES = p11_tests.c
-AM_CFLAGS = -g -O0
+
+p11_tests_LDADD = -ldl
+p11_tests_SOURCES = \
+ module.c \
+ msg.c \
+ p11-tests.c
diff --git a/src/module.c b/src/module.c
new file mode 100644
index 0000000..9815c21
--- /dev/null
+++ b/src/module.c
@@ -0,0 +1,44 @@
+
+#include "config.h"
+
+#include "p11-tests.h"
+
+#include <dlfcn.h>
+
+static void *module = NULL;
+CK_FUNCTION_LIST_PTR p11_funcs = NULL;
+
+void
+p11t_module_load(const char *filename)
+{
+ CK_C_GetFunctionList get_function_list;
+ CK_RV rv;
+
+ module = dlopen(filename, RTLD_NOW);
+ if(!module)
+ p11t_msg_fatal("couldn't open library: %s: %s", filename, dlerror());
+
+ /* Lookup the appropriate function in library */
+ get_function_list = (CK_C_GetFunctionList)dlsym (module, "C_GetFunctionList");
+ if(!get_function_list)
+ p11t_msg_fatal("couldn't find C_GetFunctionList function in library: %s: %s", filename, dlerror());
+
+ /* Get the function list */
+ rv = (get_function_list)(&p11_funcs);
+ if(rv != CKR_OK)
+ p11t_msg_fatal("couldn't get function list: %s", p11t_msg_rv(rv));
+
+ /* Make sure we have a compatible version */
+ if(p11_funcs->version.major != CRYPTOKI_VERSION_MAJOR)
+ p11t_msg_fatal("incompatible version of pkcs11 module: %d.%d",
+ (int)p11_funcs->version.major,
+ (int)p11_funcs->version.minor);
+}
+
+void
+p11t_module_unload(void)
+{
+ if(module)
+ dlclose(module);
+ module = NULL;
+}
diff --git a/src/msg.c b/src/msg.c
new file mode 100644
index 0000000..600f217
--- /dev/null
+++ b/src/msg.c
@@ -0,0 +1,147 @@
+
+#include "config.h"
+
+#include "p11-tests.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static const char *the_prefix = NULL;
+
+const char*
+p11t_msg_rv(CK_RV rv)
+{
+ switch(rv) {
+ #define CT(x) case x: return #x;
+ CT(CKR_OK)
+ CT(CKR_CANCEL)
+ CT(CKR_HOST_MEMORY)
+ CT(CKR_SLOT_ID_INVALID)
+ CT(CKR_GENERAL_ERROR)
+ CT(CKR_FUNCTION_FAILED)
+ CT(CKR_ARGUMENTS_BAD)
+ CT(CKR_NO_EVENT)
+ CT(CKR_NEED_TO_CREATE_THREADS)
+ CT(CKR_CANT_LOCK)
+ CT(CKR_ATTRIBUTE_READ_ONLY)
+ CT(CKR_ATTRIBUTE_SENSITIVE)
+ CT(CKR_ATTRIBUTE_TYPE_INVALID)
+ CT(CKR_ATTRIBUTE_VALUE_INVALID)
+ CT(CKR_DATA_INVALID)
+ CT(CKR_DATA_LEN_RANGE)
+ CT(CKR_DEVICE_ERROR)
+ CT(CKR_DEVICE_MEMORY)
+ CT(CKR_DEVICE_REMOVED)
+ CT(CKR_ENCRYPTED_DATA_INVALID)
+ CT(CKR_ENCRYPTED_DATA_LEN_RANGE)
+ CT(CKR_FUNCTION_CANCELED)
+ CT(CKR_FUNCTION_NOT_PARALLEL)
+ CT(CKR_FUNCTION_NOT_SUPPORTED)
+ CT(CKR_KEY_HANDLE_INVALID)
+ CT(CKR_KEY_SIZE_RANGE)
+ CT(CKR_KEY_TYPE_INCONSISTENT)
+ CT(CKR_KEY_NOT_NEEDED)
+ CT(CKR_KEY_CHANGED)
+ CT(CKR_KEY_NEEDED)
+ CT(CKR_KEY_INDIGESTIBLE)
+ CT(CKR_KEY_FUNCTION_NOT_PERMITTED)
+ CT(CKR_KEY_NOT_WRAPPABLE)
+ CT(CKR_KEY_UNEXTRACTABLE)
+ CT(CKR_MECHANISM_INVALID)
+ CT(CKR_MECHANISM_PARAM_INVALID)
+ CT(CKR_OBJECT_HANDLE_INVALID)
+ CT(CKR_OPERATION_ACTIVE)
+ CT(CKR_OPERATION_NOT_INITIALIZED)
+ CT(CKR_PIN_INCORRECT)
+ CT(CKR_PIN_INVALID)
+ CT(CKR_PIN_LEN_RANGE)
+ CT(CKR_PIN_EXPIRED)
+ CT(CKR_PIN_LOCKED)
+ CT(CKR_SESSION_CLOSED)
+ CT(CKR_SESSION_COUNT)
+ CT(CKR_SESSION_HANDLE_INVALID)
+ CT(CKR_SESSION_PARALLEL_NOT_SUPPORTED)
+ CT(CKR_SESSION_READ_ONLY)
+ CT(CKR_SESSION_EXISTS)
+ CT(CKR_SESSION_READ_ONLY_EXISTS)
+ CT(CKR_SESSION_READ_WRITE_SO_EXISTS)
+ CT(CKR_SIGNATURE_INVALID)
+ CT(CKR_SIGNATURE_LEN_RANGE)
+ CT(CKR_TEMPLATE_INCOMPLETE)
+ CT(CKR_TEMPLATE_INCONSISTENT)
+ CT(CKR_TOKEN_NOT_PRESENT)
+ CT(CKR_TOKEN_NOT_RECOGNIZED)
+ CT(CKR_TOKEN_WRITE_PROTECTED)
+ CT(CKR_UNWRAPPING_KEY_HANDLE_INVALID)
+ CT(CKR_UNWRAPPING_KEY_SIZE_RANGE)
+ CT(CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT)
+ CT(CKR_USER_ALREADY_LOGGED_IN)
+ CT(CKR_USER_NOT_LOGGED_IN)
+ CT(CKR_USER_PIN_NOT_INITIALIZED)
+ CT(CKR_USER_TYPE_INVALID)
+ CT(CKR_USER_ANOTHER_ALREADY_LOGGED_IN)
+ CT(CKR_USER_TOO_MANY_TYPES)
+ CT(CKR_WRAPPED_KEY_INVALID)
+ CT(CKR_WRAPPED_KEY_LEN_RANGE)
+ CT(CKR_WRAPPING_KEY_HANDLE_INVALID)
+ CT(CKR_WRAPPING_KEY_SIZE_RANGE)
+ CT(CKR_WRAPPING_KEY_TYPE_INCONSISTENT)
+ CT(CKR_RANDOM_SEED_NOT_SUPPORTED)
+ CT(CKR_RANDOM_NO_RNG)
+ CT(CKR_DOMAIN_PARAMS_INVALID)
+ CT(CKR_BUFFER_TOO_SMALL)
+ CT(CKR_SAVED_STATE_INVALID)
+ CT(CKR_INFORMATION_SENSITIVE)
+ CT(CKR_STATE_UNSAVEABLE)
+ CT(CKR_CRYPTOKI_NOT_INITIALIZED)
+ CT(CKR_CRYPTOKI_ALREADY_INITIALIZED)
+ CT(CKR_MUTEX_BAD)
+ CT(CKR_MUTEX_NOT_LOCKED)
+ CT(CKR_FUNCTION_REJECTED)
+ CT(CKR_VENDOR_DEFINED)
+ #undef CT
+ default:
+ p11t_msg_print("unknown or invalid CK_RV value: 0x%08X", rv);
+ return "";
+ }
+}
+
+void
+p11t_msg_va(const char *message, va_list va)
+{
+ int len;
+
+ if(the_prefix)
+ fprintf(stderr, "%s: ", the_prefix);
+ vfprintf(stderr, message, va);
+ len = strlen(message);
+ if(len && message[len - 1] != '\n')
+ fputc('\n', stderr);
+ fflush(stderr);
+}
+
+void
+p11t_msg_print(const char *message, ...)
+{
+ va_list va;
+ va_start(va, message);
+ p11t_msg_va(message, va);
+ va_end(va);
+}
+
+void
+p11t_msg_fatal(const char *message, ...)
+{
+ va_list va;
+ va_start(va, message);
+ p11t_msg_va(message, va);
+ va_end(va);
+ exit(1);
+}
+
+void
+p11t_msg_prefix(const char *prefix)
+{
+ the_prefix = prefix;
+}
diff --git a/src/p11-tests.c b/src/p11-tests.c
index e69de29..80185cf 100644
--- a/src/p11-tests.c
+++ b/src/p11-tests.c
@@ -0,0 +1,43 @@
+
+
+#include "p11-tests.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static void
+usage()
+{
+ fprintf(stderr, "usage: p11-tests module [init_string]\n");
+ exit(2);
+}
+
+int
+main(int argc, char* argv[])
+{
+ int ch;
+
+ while((ch = getopt(argc, argv, "")) != -1)
+ {
+ switch(ch)
+ {
+ case '?':
+ default:
+ usage();
+ break;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if(argc < 1 || argc > 2)
+ usage();
+
+ p11t_module_load(argv[0]);
+
+ p11t_module_unload();
+
+ return 0;
+}
diff --git a/src/p11-tests.h b/src/p11-tests.h
new file mode 100644
index 0000000..83c69af
--- /dev/null
+++ b/src/p11-tests.h
@@ -0,0 +1,26 @@
+#ifndef P11TESTST_H_
+#define P11TESTST_H_
+
+#include "pkcs11/pkcs11.h"
+
+#include <stdarg.h>
+
+/* -------------------------------------------------------------------
+ * message.c
+ */
+
+const char* p11t_msg_rv(CK_RV rv);
+
+void p11t_msg_va(const char *message, va_list va);
+void p11t_msg_print(const char *message, ...);
+void p11t_msg_fatal(const char *message, ...);
+void p11t_msg_prefix(const char *prefix);
+
+/* -------------------------------------------------------------------
+ * module.c
+ */
+
+void p11t_module_load(const char *filename);
+void p11t_module_unload(void);
+
+#endif /* P11TESTST_H_ */