diff options
Diffstat (limited to 'module')
-rw-r--r-- | module/cryptoki-log.c | 221 |
1 files changed, 167 insertions, 54 deletions
diff --git a/module/cryptoki-log.c b/module/cryptoki-log.c index d978bc0..84e4b7a 100644 --- a/module/cryptoki-log.c +++ b/module/cryptoki-log.c @@ -36,40 +36,117 @@ * */ +#ifndef _WIN32 #include "config.h" +#endif -#include "cryptoki.h" +#define CRYPTOKI_EXPORTS +#ifdef _WIN32 +#include "cryptoki-win32.h" +#else +#include "cryptoki-unix.h" +#endif #include <sys/types.h> #include <stdlib.h> #include <stdio.h> #include <string.h> -#include <pthread.h> -#include <unistd.h> #include <errno.h> -#include <dlfcn.h> #include <stdarg.h> -static void *module_handle = NULL; -static CK_FUNCTION_LIST_PTR module_list = NULL; -/* TODO: module fini should call finalize_common */ +#ifdef _WIN32 + +#define WIN32_LEAN_AND_MEAN +#include <windows.h> + +static HINSTANCE module_handle = NULL; +static HANDLE global_mutex = NULL; + +#define LOCK_OUTPUT \ + WaitForSingleObject (global_mutex, INFINITE); + +#define UNLOCK_OUTPUT \ + ReleaseMutex (global_mutex); + +/* Stupid Windows, things it's the only thing going */ +#undef CreateMutex +#else + +#include <dlfcn.h> +#include <pthread.h> +#include <unistd.h> -/* protects all global variables, and session list */ +static void *module_handle = NULL; static pthread_mutex_t global_mutex = PTHREAD_MUTEX_INITIALIZER; -static int use_locking = 1; + +#define LOCK_OUTPUT \ + pthread_mutex_lock (&global_mutex); + +#define UNLOCK_OUTPUT \ + pthread_mutex_unlock (&global_mutex); + +#endif + +static CK_FUNCTION_LIST_PTR module_list = NULL; static FILE* output_file = NULL; +#ifdef _WIN32 + +static char win32_line_buf[4096] = { 0, }; + +static void +cklog_win32(const char* msg, va_list va) +{ + size_t l; + char* p; + char ch; + + l = strlen(win32_line_buf); + + /* Line is just too long? */ + if(l >= 2048) + l = 0; + + /* Print into the buffer */ + _vsnprintf(win32_line_buf + l, sizeof(win32_line_buf) - l, msg, va); + win32_line_buf[sizeof(win32_line_buf) - 1] = 0; + + /* Now send out all lines from the buffer */ + while((p = strchr(win32_line_buf, '\n')) != NULL) { + ch = p[1]; + p[1] = 0; + OutputDebugStringA(win32_line_buf); + p[1] = ch; + memmove(win32_line_buf, p + 1, sizeof(win32_line_buf) - ((p + 1) - win32_line_buf)); + } +} + +#endif + static void cklog (const char* msg, ...) { va_list va; va_start (va, msg); - vfprintf (output_file ? output_file : stderr, msg, va); - va_end (va); - if (output_file) + + if(output_file && output_file != stderr) + { + vfprintf (output_file, msg, va); fflush (output_file); + } + else + { +#ifdef _WIN32 + cklog_win32 (msg, va); +#else + vfprintf (stderr, msg, va); + fflush (stderr); +#endif + } + + va_end (va); } @@ -479,12 +556,6 @@ log_user_type (const char *pref, const char *name, CK_USER_TYPE val, CK_RV statu cklog ("\n"); } -#define LOCK_OUTPUT \ - if (use_locking) pthread_mutex_lock (&global_mutex); - -#define UNLOCK_OUTPUT \ - if (use_locking) pthread_mutex_unlock (&global_mutex); - #define BEGIN_CALL(name) \ { \ CK_##name _func; const char* _name; CK_RV _ret = CKR_OK; \ @@ -511,98 +582,98 @@ log_user_type (const char *pref, const char *name, CK_USER_TYPE val, CK_RV statu return _ret; \ } -#define IN " IN: " -#define OUT " OUT: " +#define LIN " IN: " +#define LOUT " OUT: " #define IN_ATTRIBUTE_ARRAY(a, n) \ - log_attribute_array (IN, #a, a, n, CKR_OK); + log_attribute_array (LIN, #a, a, n, CKR_OK); #define IN_BOOL(a) \ - log_bool (IN, #a, a, CKR_OK); + log_bool (LIN, #a, a, CKR_OK); #define IN_BYTE_ARRAY(a, n) \ - log_byte_array (IN, #a, a, &n, CKR_OK); + log_byte_array (LIN, #a, a, &n, CKR_OK); #define IN_HANDLE(a) \ - log_ulong (IN, #a, a, "H", CKR_OK); + log_ulong (LIN, #a, a, "H", CKR_OK); #define IN_INIT_ARGS(a) \ - log_init_args (IN, #a, a, CKR_OK); + log_init_args (LIN, #a, a, CKR_OK); #define IN_POINTER(a) \ - log_pointer (IN, #a, a, CKR_OK); + log_pointer (LIN, #a, a, CKR_OK); #define IN_MECHANISM(a) \ - log_mechanism (IN, #a, a, CKR_OK); + log_mechanism (LIN, #a, a, CKR_OK); #define IN_MECHANISM_TYPE(a) \ - log_mechanism_type (IN, #a, a, CKR_OK); + log_mechanism_type (LIN, #a, a, CKR_OK); #define IN_SESSION(a) \ - log_ulong (IN, #a, a, "S", CKR_OK); + log_ulong (LIN, #a, a, "S", CKR_OK); #define IN_SLOT_ID(a) \ - log_ulong (IN, #a, a, "SL", CKR_OK); + log_ulong (LIN, #a, a, "SL", CKR_OK); #define IN_STRING(a) \ - log_string (IN, #a, a, CKR_OK); + log_string (LIN, #a, a, CKR_OK); #define IN_ULONG(a) \ - log_ulong (IN, #a, a, NULL, CKR_OK); + log_ulong (LIN, #a, a, NULL, CKR_OK); #define IN_ULONG_PTR(a) \ - log_ulong_pointer (IN, #a, a, NULL, CKR_OK); + log_ulong_pointer (LIN, #a, a, NULL, CKR_OK); #define IN_USER_TYPE(a) \ - log_user_type (IN, #a, a, CKR_OK); + log_user_type (LIN, #a, a, CKR_OK); #define OUT_ATTRIBUTE_ARRAY(a, n) \ - log_attribute_array (OUT, #a, a, n, _ret); + log_attribute_array (LOUT, #a, a, n, _ret); #define OUT_BYTE_ARRAY(a, n) \ - log_byte_array(OUT, #a, a, n, _ret); + log_byte_array(LOUT, #a, a, n, _ret); #define OUT_HANDLE(a) \ - log_ulong_pointer (OUT, #a, a, "H", _ret); + log_ulong_pointer (LOUT, #a, a, "H", _ret); #define OUT_HANDLE_ARRAY(a, n) \ - log_ulong_array (OUT, #a, a, n, "H", _ret); + log_ulong_array (LOUT, #a, a, n, "H", _ret); #define OUT_INFO(a) \ - log_info (OUT, #a, a, _ret); + log_info (LOUT, #a, a, _ret); #define OUT_MECHANISM_INFO(a) \ - log_mechanism_info (OUT, #a, a, _ret); + log_mechanism_info (LOUT, #a, a, _ret); #define OUT_MECHANISM_TYPE_ARRAY(a, n) \ - log_mechanism_type_array (OUT, #a, a, n, _ret); + log_mechanism_type_array (LOUT, #a, a, n, _ret); #define OUT_POINTER(a) \ - log_pointer (OUT, #a, a, _ret); + log_pointer (LOUT, #a, a, _ret); #define OUT_SESSION(a) \ - log_ulong_pointer (OUT, #a, a, "S", _ret); + log_ulong_pointer (LOUT, #a, a, "S", _ret); #define OUT_SESSION_INFO(a) \ - log_session_info (OUT, #a, a, _ret); + log_session_info (LOUT, #a, a, _ret); #define OUT_SLOT_ID_ARRAY(a, n) \ - log_ulong_array (OUT, #a, a, n, "SL", _ret); + log_ulong_array (LOUT, #a, a, n, "SL", _ret); #define OUT_SLOT_ID(a) \ - log_ulong_pointer (OUT, #a, a, "SL", _ret); + log_ulong_pointer (LOUT, #a, a, "SL", _ret); #define OUT_SLOT_INFO(a) \ - log_slot_info (OUT, #a, a, _ret); + log_slot_info (LOUT, #a, a, _ret); #define OUT_TOKEN_INFO(a) \ - log_token_info (OUT, #a, a, _ret); + log_token_info (LOUT, #a, a, _ret); #define OUT_ULONG(a) \ - log_ulong_pointer (OUT, #a, a, NULL, _ret); + log_ulong_pointer (LOUT, #a, a, NULL, _ret); #define OUT_ULONG_ARRAY(a, n) \ - log_ulong_array (OUT, #a, a, n, NULL, _ret); + log_ulong_array (LOUT, #a, a, n, NULL, _ret); @@ -634,7 +705,10 @@ CL_C_Initialize (CK_VOID_PTR pInitArgs) cklog ("couldn't open log file: %s", output); } } + +#ifndef _WIN32 setlinebuf (output_file); +#endif /* Load the other module */ module = getenv("CRYPTOKI_LOG_MODULE"); @@ -643,6 +717,22 @@ CL_C_Initialize (CK_VOID_PTR pInitArgs) return CKR_DEVICE_ERROR; } +#ifdef _WIN32 + + module_handle = LoadLibraryA (module); + if (!module_handle) { + cklog ("couldn't load module: %s: %d\n", module, GetLastError ()); + return CKR_DEVICE_ERROR; + } + + func = (CK_C_GetFunctionList)GetProcAddress (module_handle, "C_GetFunctionList"); + if (!func) { + cklog ("couldn't find C_GetFunctionList function in module: %s: %d\n", module, GetLastError ()); + return CKR_DEVICE_ERROR; + } + +#else + module_handle = dlopen (module, RTLD_NOW | RTLD_LOCAL); if (!module_handle) { cklog ("couldn't load module: %s: %s\n", module, dlerror ()); @@ -655,6 +745,8 @@ CL_C_Initialize (CK_VOID_PTR pInitArgs) return CKR_DEVICE_ERROR; } +#endif + ret = (func) (&module_list); if (ret != CKR_OK) { cklog ("C_GetFunctionList returned an error in module: %s: %d\n", module, ret); @@ -668,13 +760,28 @@ CL_C_Initialize (CK_VOID_PTR pInitArgs) if (pInitArgs != NULL) { args = pInitArgs; - if (!(args->flags & CKF_OS_LOCKING_OK)) - use_locking = 0; + if (!(args->flags & CKF_OS_LOCKING_OK)) { + cklog ("can't use OS locking, can't log properly"); + return CKR_CANT_LOCK; + } } +#ifdef _WIN32 + if(!global_mutex) + { + global_mutex = CreateMutexA(NULL, FALSE, NULL); + if (!global_mutex) + return CKR_CANT_LOCK; + } + + cklog ("INITIALIZE cryptoki-log debugging (%s / %s)\n", + module, output ? output : "stderr"); +#else + cklog ("INITIALIZE cryptoki-log debugging (%s / %s)\n", + module, output ? output : "ods"); +#endif + - cklog ("INITIALIZE cryptoki-log debugging (%s / %s locks / %s)\n", - module, use_locking ? "with" : "no", output ? output : "stderr"); BEGIN_CALL (C_Initialize) IN_INIT_ARGS (pInitArgs) @@ -1540,7 +1647,13 @@ static struct CK_FUNCTION_LIST functionList = { CL_C_WaitForSlotEvent }; +/* #ifdef MSVC +__declspec(dllexport) CK_RV +#if 0 CK_RV +#endif +*/ +__declspec(dllexport) CK_RV C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) { if (!list) |