From cd3f780e5eb4ed04d5e890aeeaf2d13fed2fba8c Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sat, 28 Apr 2007 19:56:22 +0000 Subject: Make cryptoki-log work with win32 --- Makefile.win32 | 35 ++++++++ module/cryptoki-log.c | 221 ++++++++++++++++++++++++++++++++++++------------ pkcs11/cryptoki-unix.h | 55 ++++++++++++ pkcs11/cryptoki-win32.h | 66 +++++++++++++++ pkcs11/cryptoki.h | 55 ------------ 5 files changed, 323 insertions(+), 109 deletions(-) create mode 100644 Makefile.win32 create mode 100644 pkcs11/cryptoki-unix.h create mode 100644 pkcs11/cryptoki-win32.h delete mode 100644 pkcs11/cryptoki.h diff --git a/Makefile.win32 b/Makefile.win32 new file mode 100644 index 0000000..f6932e6 --- /dev/null +++ b/Makefile.win32 @@ -0,0 +1,35 @@ +!IF "$(OS)" == "Windows_NT" +NULL= +!ELSE +NULL=nul +!ENDIF + +CPP=cl.exe +MTL=midl.exe +RSC=rc.exe + +OUTDIR=.\win32 + +ALL : "$(OUTDIR)\cklog.dll" + +CLEAN : + -@erase "$(OUTDIR)\*.obj" + -@erase "$(OUTDIR)\vc60.*" + -@erase "$(OUTDIR)\cklog.*" + +INCLUDES = /I pkcs11 + +"$(OUTDIR)" : + if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)" + +CPP_PROJ=/nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /Fp"$(OUTDIR)\cklog.pch" /YX /Fo"$(OUTDIR)\\" /Fd"$(OUTDIR)\\" /FD /GZ /c +MTL_PROJ=/nologo /D "_DEBUG" /mktyplib203 /win32 + +LINK32=link.exe +LINK32_FLAGS=kernel32.lib /nologo /dll /incremental:yes /pdb:"$(OUTDIR)\cklog.pdb" /debug /machine:I386 /out:"$(OUTDIR)\cklog.dll" /implib:"$(OUTDIR)\cklog.lib" /pdbtype:sept + +$(OUTDIR)\cryptoki-log.obj : .\module\cryptoki-log.c "$(OUTDIR)" + $(CPP) $(INCLUDES) $(CPP_PROJ) .\module\cryptoki-log.c + +"$(OUTDIR)\cklog.dll" : "$(OUTDIR)" "$(OUTDIR)\cryptoki-log.obj" + $(LINK32) $(LINK32_FLAGS) "$(OUTDIR)\cryptoki-log.obj" 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 #include #include #include -#include -#include #include -#include #include -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 + +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 +#include +#include -/* 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) diff --git a/pkcs11/cryptoki-unix.h b/pkcs11/cryptoki-unix.h new file mode 100644 index 0000000..47c2a6e --- /dev/null +++ b/pkcs11/cryptoki-unix.h @@ -0,0 +1,55 @@ +/* cryptoki.h include file for PKCS #11. */ +/* $Revision: 1.4 $ */ + +/* License to copy and use this software is granted provided that it is + * identified as "RSA Security Inc. PKCS #11 Cryptographic Token Interface + * (Cryptoki)" in all material mentioning or referencing this software. + + * License is also granted to make and use derivative works provided that + * such works are identified as "derived from the RSA Security Inc. PKCS #11 + * Cryptographic Token Interface (Cryptoki)" in all material mentioning or + * referencing the derived work. + + * RSA Security Inc. makes no representations concerning either the + * merchantability of this software or the suitability of this software for + * any particular purpose. It is provided "as is" without express or implied + * warranty of any kind. + */ + +/* This is a sample file containing the top level include directives + * for building Win32 Cryptoki libraries and applications. + */ + +#ifndef ___CRYPTOKI_H_INC___ +#define ___CRYPTOKI_H_INC___ + +#ifndef CK_PTR +#define CK_PTR * +#endif + +#ifndef CK_DEFINE_FUNCTION +#define CK_DEFINE_FUNCTION(returnType, name) returnType name +#endif + +#ifndef CK_DECLARE_FUNCTION +#define CK_DECLARE_FUNCTION(returnType, name) returnType name +#endif + +#ifndef CK_DECLARE_FUNCTION_POINTER +#define CK_DECLARE_FUNCTION_POINTER(returnType, name) returnType (* name) +#endif + +#ifndef CK_CALLBACK_FUNCTION +#define CK_CALLBACK_FUNCTION(returnType, name) returnType (* name) +#endif + +#ifndef NULL_PTR +#include /* For NULL */ +#define NULL_PTR NULL +#endif + +#define CK_NEED_ARG_LIST 1 + +#include "pkcs11.h" + +#endif /* ___CRYPTOKI_H_INC___ */ diff --git a/pkcs11/cryptoki-win32.h b/pkcs11/cryptoki-win32.h new file mode 100644 index 0000000..f457b3e --- /dev/null +++ b/pkcs11/cryptoki-win32.h @@ -0,0 +1,66 @@ +/* cryptoki.h include file for PKCS #11. */ +/* $Revision: 1.4 $ */ + +/* License to copy and use this software is granted provided that it is + * identified as "RSA Security Inc. PKCS #11 Cryptographic Token Interface + * (Cryptoki)" in all material mentioning or referencing this software. + + * License is also granted to make and use derivative works provided that + * such works are identified as "derived from the RSA Security Inc. PKCS #11 + * Cryptographic Token Interface (Cryptoki)" in all material mentioning or + * referencing the derived work. + + * RSA Security Inc. makes no representations concerning either the + * merchantability of this software or the suitability of this software for + * any particular purpose. It is provided "as is" without express or implied + * warranty of any kind. + */ + +/* This is a sample file containing the top level include directives + * for building Win32 Cryptoki libraries and applications. + */ + +#ifndef ___CRYPTOKI_H_INC___ +#define ___CRYPTOKI_H_INC___ + +#pragma pack(push, cryptoki, 1) + +/* Specifies that the function is a DLL entry point. */ +#define CK_IMPORT_SPEC __declspec(dllimport) + +/* Define CRYPTOKI_EXPORTS during the build of cryptoki libraries. Do + * not define it in applications. + */ +#ifdef CRYPTOKI_EXPORTS +/* Specified that the function is an exported DLL entry point. */ +#define CK_EXPORT_SPEC __declspec(dllexport) +#else +#define CK_EXPORT_SPEC CK_IMPORT_SPEC +#endif + +/* Ensures the calling convention for Win32 builds */ +#define CK_CALL_SPEC __cdecl + +#define CK_PTR * + +#define CK_DEFINE_FUNCTION(returnType, name) \ + returnType CK_EXPORT_SPEC CK_CALL_SPEC name + +#define CK_DECLARE_FUNCTION(returnType, name) \ + returnType CK_EXPORT_SPEC CK_CALL_SPEC name + +#define CK_DECLARE_FUNCTION_POINTER(returnType, name) \ + returnType CK_IMPORT_SPEC (CK_CALL_SPEC CK_PTR name) + +#define CK_CALLBACK_FUNCTION(returnType, name) \ + returnType (CK_CALL_SPEC CK_PTR name) + +#ifndef NULL_PTR +#define NULL_PTR 0 +#endif + +#include "pkcs11.h" + +#pragma pack(pop, cryptoki) + +#endif /* ___CRYPTOKI_H_INC___ */ diff --git a/pkcs11/cryptoki.h b/pkcs11/cryptoki.h deleted file mode 100644 index 47c2a6e..0000000 --- a/pkcs11/cryptoki.h +++ /dev/null @@ -1,55 +0,0 @@ -/* cryptoki.h include file for PKCS #11. */ -/* $Revision: 1.4 $ */ - -/* License to copy and use this software is granted provided that it is - * identified as "RSA Security Inc. PKCS #11 Cryptographic Token Interface - * (Cryptoki)" in all material mentioning or referencing this software. - - * License is also granted to make and use derivative works provided that - * such works are identified as "derived from the RSA Security Inc. PKCS #11 - * Cryptographic Token Interface (Cryptoki)" in all material mentioning or - * referencing the derived work. - - * RSA Security Inc. makes no representations concerning either the - * merchantability of this software or the suitability of this software for - * any particular purpose. It is provided "as is" without express or implied - * warranty of any kind. - */ - -/* This is a sample file containing the top level include directives - * for building Win32 Cryptoki libraries and applications. - */ - -#ifndef ___CRYPTOKI_H_INC___ -#define ___CRYPTOKI_H_INC___ - -#ifndef CK_PTR -#define CK_PTR * -#endif - -#ifndef CK_DEFINE_FUNCTION -#define CK_DEFINE_FUNCTION(returnType, name) returnType name -#endif - -#ifndef CK_DECLARE_FUNCTION -#define CK_DECLARE_FUNCTION(returnType, name) returnType name -#endif - -#ifndef CK_DECLARE_FUNCTION_POINTER -#define CK_DECLARE_FUNCTION_POINTER(returnType, name) returnType (* name) -#endif - -#ifndef CK_CALLBACK_FUNCTION -#define CK_CALLBACK_FUNCTION(returnType, name) returnType (* name) -#endif - -#ifndef NULL_PTR -#include /* For NULL */ -#define NULL_PTR NULL -#endif - -#define CK_NEED_ARG_LIST 1 - -#include "pkcs11.h" - -#endif /* ___CRYPTOKI_H_INC___ */ -- cgit v1.2.3