diff options
Diffstat (limited to 'module/p11-log.c')
-rw-r--r-- | module/p11-log.c | 1679 |
1 files changed, 1679 insertions, 0 deletions
diff --git a/module/p11-log.c b/module/p11-log.c new file mode 100644 index 0000000..5061546 --- /dev/null +++ b/module/p11-log.c @@ -0,0 +1,1679 @@ +/* + * Copyright (c) 2007, Stefan Walter + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * + * CONTRIBUTORS + * Stef Walter <stef@memberwebs.com> + * + */ + +#ifndef _WIN32 +#include "config.h" +#endif + +#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 <errno.h> +#include <stdarg.h> + + +#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> + +static void *module_handle = NULL; +static pthread_mutex_t global_mutex = PTHREAD_MUTEX_INITIALIZER; + +#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; + +#define LINE_BUF_LEN 4096 +#define PREFIX "PKCS11: " +#define PREFIX_LEN (sizeof(PREFIX) - 1) + +static char the_line_buf_full[LINE_BUF_LEN + PREFIX_LEN] = { 0, }; +static char* the_line_buf = the_line_buf_full + PREFIX_LEN; + +static void +cklog_line(const char* line) +{ + if (output_file && output_file != stderr) { + fputs (line, output_file); + } else { +#ifdef _WIN32 + OutputDebugStringA(line); +#else + fputs (line, stderr); +#endif + } + + if (output_file) + fflush (output_file); +} + +static void +cklog(const char* msg, ...) +{ + va_list va; + size_t l; + char* p; + char ch; + + va_start (va, msg); + + /* Fill in the prefix if necessary */ + if(!the_line_buf_full[0]) + memcpy(the_line_buf_full, PREFIX, PREFIX_LEN); + + /* Length of data already present */ + l = strlen(the_line_buf); + + /* Line is just too long? */ + if(l >= 2048) + l = 0; + + /* Print into the buffer */ +#ifdef _WIN32 + _vsnprintf(the_line_buf + l, LINE_BUF_LEN - l, msg, va); +#else + vsnprintf(the_line_buf + l, LINE_BUF_LEN - l, msg, va); +#endif + the_line_buf[LINE_BUF_LEN - 1] = 0; + + va_end (va); + + /* Now send out all lines from the buffer */ + while((p = strchr(the_line_buf, '\n')) != NULL) { + ch = p[1]; + p[1] = 0; + cklog_line(the_line_buf_full); + p[1] = ch; + memmove(the_line_buf, p + 1, LINE_BUF_LEN - ((p + 1) - the_line_buf)); + } +} + +#define LOG_FLAG(flags, had, flag) \ + if ((flags & flag) == flag) { \ + cklog ("%s%s", (had) ? "|" : " = ", #flag); \ + had++; \ + } + +/* The constant logging */ +#include "p11-constants.c" + +static void +log_some_bytes (CK_BYTE_PTR arr, CK_ULONG num) +{ + CK_ULONG i; + char buf[128]; + char *p, *e; + CK_BYTE ch; + + if(!arr) { + cklog ("NULL"); + return; + } else if (num == (CK_ULONG)-1) { + cklog ("????"); + return; + } + + buf[0] = '\"'; + p = buf + 1; + e = buf + (sizeof (buf) - 8); + + for(i = 0; i < num && p < e; ++i, ++p) { + ch = arr[i]; + if (ch == '\t') { + p[0] = '\\'; p[1] = 't'; + ++p; + } else if (ch == '\n') { + p[0] = '\\'; p[1] = 'n'; + ++p; + } else if (ch == '\r') { + p[0] = '\\'; p[1] = 'r'; + ++p; + } else if (ch >= 32 && ch < 127) { + *p = ch; + } else { + p[0] = '\\'; + p[1] = 'x'; + sprintf(p + 2, "%02X", ch); + p += 3; + } + } + + *p = 0; + if (p >= e) + strcpy (e, "..."); + strcat (p, "\""); + cklog ("%s", buf); +} + +static void +log_pointer (const char *pref, const char *name, CK_VOID_PTR val, CK_RV status) +{ + if (status != CKR_OK) + return; + if (val == NULL) + cklog ("%s%s = NULL\n", pref, name); + else + cklog ("%s%s = 0x%08X\n", pref, name, (CK_ULONG)val); +} + +static void +log_attribute_array (const char *pref, const char *name, CK_ATTRIBUTE_PTR arr, + CK_ULONG_PTR num, CK_RV status) +{ + CK_ULONG i; + if (status == CKR_BUFFER_TOO_SMALL) { + arr = NULL; + status = CKR_OK; + } + if (status != CKR_OK) + return; + + cklog ("%s%s = ", pref, name); + if (num == NULL) { + cklog ("(?) NONE\n"); + } else if (arr == NULL) { + cklog ("(%d) NONE\n", *num); + } else { + cklog ("(%d) [", *num); + for(i = 0; i < *num; ++i) { + if (i > 0) + cklog (","); + cklog (" { type: "); + log_CKA (arr[i].type); + cklog (", pValue: "); + if (arr[i].ulValueLen == (CK_ULONG)-1) { + cklog ("(-1) INVALID"); + } else if (arr[i].pValue == NULL) { + cklog ("(%d) NULL", arr[i].ulValueLen); + + /* A pretty good guess that 4 == CK_ULONG */ + } else if (arr[i].ulValueLen == 4) { + cklog ("(%d) 0x%08X", arr[i].ulValueLen, *((CK_ULONG_PTR)(arr[i].pValue))); + + /* A pretty good guess that 1 = CK_BBOOL */ + } else if (arr[i].ulValueLen == 1) { + cklog ("(%d) 0x%02X", arr[i].ulValueLen, (CK_ULONG)(*((CK_BBOOL*)(arr[i].pValue)))); + + /* Any other length */ + } else { + cklog ("(%d) ", arr[i].ulValueLen); + log_some_bytes (arr[i].pValue, arr[i].ulValueLen); + } + cklog (" }"); + } + cklog (" ]\n"); + } +} + +static void +log_bool (const char *pref, const char *name, CK_BBOOL val, CK_RV status) +{ + if (status == CKR_OK) + cklog ("%s%s = %s\n", pref, name, val ? "CK_TRUE" : "CK_FALSE"); +} + +static void +log_byte_array (const char *pref, const char *name, CK_BYTE_PTR arr, + CK_ULONG_PTR num, CK_RV status) +{ + if (status == CKR_BUFFER_TOO_SMALL) { + arr = NULL; + status = CKR_OK; + } + + if (status != CKR_OK) + return; + cklog ("%s%s = ", pref, name); + if (num == NULL) { + cklog ("(?) NOTHING\n"); + } else if (arr == NULL) { + cklog ("(%d) NOTHING\n", *num); + } else { + cklog ("(%d) ", *num); + log_some_bytes (arr, *num); + cklog ("\n"); + } +} + +static void +log_info (const char *pref, const char *name, CK_INFO_PTR info, CK_RV status) +{ + if (status != CKR_OK) + return; + if (info == NULL) { + log_pointer (pref, name, info, status); + } else { + cklog ("%s%s = { cryptokiVersion: %d.%d, manufacturerID: \"%0.32s\", flags: %X, " + "libraryDescription: \"%0.32s\", libraryVersion: %d.%d }\n", + pref, name, info->cryptokiVersion.major, info->cryptokiVersion.minor, + info->manufacturerID, info->flags, info->libraryDescription, + info->libraryVersion.major, info->libraryVersion.minor); + } +} + +static void +log_init_args (const char *pref, const char *name, CK_VOID_PTR init_args, CK_RV status) +{ + int had = 0; + if (status != CKR_OK) + return; + if (init_args == NULL) + log_pointer (pref, name, init_args, status); + else { + CK_C_INITIALIZE_ARGS *args = (CK_C_INITIALIZE_ARGS*)init_args; + cklog ("%s%s = { CreateMutex: 0x%08X, DestroyMutex: 0x%08X, LockMutex: 0x%08X" + "UnlockMutex: 0x%08X, flags: %X", + pref, name, (CK_ULONG)(args->CreateMutex), (CK_ULONG)(args->DestroyMutex), + (CK_ULONG)(args->LockMutex), (CK_ULONG)(args->UnlockMutex), args->flags); + LOG_FLAG (args->flags, had, CKF_OS_LOCKING_OK) + cklog (", pReserved: 0x%08X }\n", (CK_ULONG)args->pReserved); + } +} + +static void +log_mechanism_info (const char *pref, const char *name, CK_MECHANISM_INFO_PTR info, CK_RV status) +{ + int had = 0; + if (status != CKR_OK) + return; + if (info == NULL) { + log_pointer (pref, name, info, status); + } else { + cklog ("%s%s = { ulMinKeySize: %d, ulMaxKeySize: %d, flags: %X ", + pref, name, info->ulMinKeySize, info->ulMaxKeySize, info->flags); + LOG_FLAG (info->flags, had, CKF_HW); + LOG_FLAG (info->flags, had, CKF_ENCRYPT); + LOG_FLAG (info->flags, had, CKF_DECRYPT); + LOG_FLAG (info->flags, had, CKF_DIGEST); + LOG_FLAG (info->flags, had, CKF_SIGN); + LOG_FLAG (info->flags, had, CKF_SIGN_RECOVER); + LOG_FLAG (info->flags, had, CKF_VERIFY); + LOG_FLAG (info->flags, had, CKF_VERIFY_RECOVER); + LOG_FLAG (info->flags, had, CKF_GENERATE); + LOG_FLAG (info->flags, had, CKF_GENERATE_KEY_PAIR); + LOG_FLAG (info->flags, had, CKF_WRAP); + LOG_FLAG (info->flags, had, CKF_UNWRAP); + LOG_FLAG (info->flags, had, CKF_DERIVE); + LOG_FLAG (info->flags, had, CKF_EXTENSION); + cklog (" }\n"); + } +} + +static void +log_mechanism (const char *pref, const char *name, CK_MECHANISM_PTR mech, CK_RV status) +{ + if (status != CKR_OK) + return; + cklog ("%s%s = { mechanism: ", pref, name); + log_CKM (mech->mechanism); + cklog (", pParameter: (%d) ", mech->ulParameterLen); + log_some_bytes (mech->pParameter, mech->ulParameterLen); + cklog (" }\n"); +} + +static void +log_mechanism_type (const char *pref, const char *name, CK_MECHANISM_TYPE val, CK_RV status) +{ + if (status != CKR_OK) + return; + cklog ("%s%s = ", pref, name); + log_CKM (val); + cklog ("\n"); +} + +static void +log_mechanism_type_array (const char *pref, const char *name, CK_MECHANISM_TYPE_PTR arr, + CK_ULONG_PTR num, CK_RV status) +{ + CK_ULONG i; + + if (status == CKR_BUFFER_TOO_SMALL) { + arr = NULL; + status = CKR_OK; + } + if (status != CKR_OK) + return; + + cklog ("%s%s = ", pref, name); + if (num == NULL) { + cklog ("(?) NO-VALUES\n"); + } else if (arr == NULL) { + cklog ("(%d) NO-VALUES\n", *num); + } else { + cklog ("(%d) [ ", *num); + for(i = 0; i < *num; ++i) { + if (i > 0) + cklog (", "); + log_CKM(arr[i]); + } + cklog (" ]\n"); + } +} + +static void +log_session_info (const char *pref, const char *name, CK_SESSION_INFO_PTR info, CK_RV status) +{ + int had = 0; + if (status != CKR_OK) + return; + if (info == NULL) { + log_pointer (pref, name, info, status); + } else { + cklog ("%s%s = { slotID: SL%d, state: ", pref, name, info->slotID); + log_CKS (info->state); + cklog (", flags: %d", info->flags); + LOG_FLAG (info->flags, had, CKF_SERIAL_SESSION); + LOG_FLAG (info->flags, had, CKF_RW_SESSION); + cklog (", ulDeviceError: %d }\n", info->ulDeviceError); + } +} + +static void +log_slot_info (const char *pref, const char *name, CK_SLOT_INFO_PTR info, CK_RV status) +{ + int had = 0; + if (status != CKR_OK) + return; + if (info == NULL) { + log_pointer (pref, name, info, status); + } else { + cklog ("%s%s = { slotDescription: \"%0.32s\", manufacturerID: \"%0.32s\", flags: %d", + pref, name, info->slotDescription, info->manufacturerID, info->flags); + LOG_FLAG (info->flags, had, CKF_TOKEN_PRESENT); + LOG_FLAG (info->flags, had, CKF_REMOVABLE_DEVICE); + LOG_FLAG (info->flags, had, CKF_HW_SLOT); + cklog (", hardwareVersion: %d.%d, firmwareVersion: %d.%d }\n", + info->hardwareVersion.major, info->hardwareVersion.minor, + info->firmwareVersion.major, info->firmwareVersion.minor); + } +} + +static void +log_string (const char *pref, const char *name, CK_UTF8CHAR_PTR str, const CK_RV status) +{ + if (status != CKR_OK) + return; + if (str == NULL) + log_pointer (pref, name, str, status); + else + cklog ("%s%s = \"%s\"\n", pref, name, str); +} + +static void +log_token_info (const char *pref, const char *name, CK_TOKEN_INFO_PTR info, CK_RV status) +{ + int had = 0; + if (status != CKR_OK) + return; + if (info == NULL) { + log_pointer (pref, name, info, status); + } else { + cklog ("%s%s = { label: \"%0.32s\", manufacturerID: \"%0.32s\", model: \"%0.32s\", " + "serialNumber: \"%0.16s\", flags: %d", pref, name, info->label, + info->manufacturerID, info->model, info->serialNumber, info->flags); + LOG_FLAG (info->flags, had, CKF_RNG); + LOG_FLAG (info->flags, had, CKF_WRITE_PROTECTED); + LOG_FLAG (info->flags, had, CKF_LOGIN_REQUIRED); + LOG_FLAG (info->flags, had, CKF_USER_PIN_INITIALIZED); + LOG_FLAG (info->flags, had, CKF_RESTORE_KEY_NOT_NEEDED); + LOG_FLAG (info->flags, had, CKF_CLOCK_ON_TOKEN); + LOG_FLAG (info->flags, had, CKF_PROTECTED_AUTHENTICATION_PATH); + LOG_FLAG (info->flags, had, CKF_DUAL_CRYPTO_OPERATIONS); + LOG_FLAG (info->flags, had, CKF_TOKEN_INITIALIZED); + LOG_FLAG (info->flags, had, CKF_SECONDARY_AUTHENTICATION); + LOG_FLAG (info->flags, had, CKF_USER_PIN_COUNT_LOW); + LOG_FLAG (info->flags, had, CKF_USER_PIN_FINAL_TRY); + LOG_FLAG (info->flags, had, CKF_USER_PIN_LOCKED); + LOG_FLAG (info->flags, had, CKF_USER_PIN_TO_BE_CHANGED); + LOG_FLAG (info->flags, had, CKF_SO_PIN_COUNT_LOW); + LOG_FLAG (info->flags, had, CKF_SO_PIN_FINAL_TRY); + LOG_FLAG (info->flags, had, CKF_SO_PIN_LOCKED); + LOG_FLAG (info->flags, had, CKF_SO_PIN_TO_BE_CHANGED); + if (!had) + cklog ("%d", info->flags); + cklog (", ulMaxSessionCount: %d, ulSessionCount: %d, ulMaxRwSessionCount: %d, " + "ulRwSessionCount: %d, ulMaxPinLen: %d, ulMinPinLen: %d, ulTotalPublicMemory: %d, " + "ulFreePublicMemory: %d, ulTotalPrivateMemory: %d, ulFreePrivateMemory: %d, " + "hardwareVersion: %d.%d, firmwareVersion: %d.%d, utcTime: %0.16s }\n", + info->ulMaxSessionCount, info->ulSessionCount, info->ulMaxRwSessionCount, + info->ulRwSessionCount, info->ulMaxPinLen, info->ulMinPinLen, info->ulTotalPublicMemory, + info->ulFreePublicMemory, info->ulTotalPrivateMemory, info->ulFreePrivateMemory, + info->hardwareVersion.major, info->hardwareVersion.minor, info->firmwareVersion.major, + info->firmwareVersion.minor, (info->flags & CKF_CLOCK_ON_TOKEN) ? (const char*)info->utcTime : ""); + } +} + +static void +log_ulong (const char *pref, const char *name, CK_ULONG val, const char* npref, CK_RV status) +{ + if (!npref) + npref = ""; + if (status == CKR_OK) + cklog ("%s%s = %s%d\n", pref, name, npref, val); +} + +static void +log_ulong_array (const char *pref, const char *name, CK_ULONG_PTR arr, + CK_ULONG_PTR num, const char *npref, CK_RV status) +{ + CK_ULONG i; + + if (status == CKR_BUFFER_TOO_SMALL) { + arr = NULL; + status = CKR_OK; + } + + if (status != CKR_OK) + return; + if (npref == NULL) + npref = ""; + cklog ("%s%s = ", pref, name); + if (num == NULL) { + cklog ("(?) NO-VALUES\n"); + } else if (arr == NULL) { + cklog ("(%d) NO-VALUES\n", *num); + } else { + cklog ("(%d) [", *num); + for(i = 0; i < *num; ++i) + cklog ("%s %s%d", i == 0 ? "" : ",", npref, arr[i]); + cklog (" ]\n"); + } +} + +static void +log_ulong_pointer (const char *pref, const char *name, CK_ULONG_PTR val, + const char *npref, CK_RV status) +{ + if (status != CKR_OK) + return; + if (npref == NULL) + npref = ""; + if (val == NULL) + cklog ("%s%s = NULL\n", pref, name); + else + cklog ("%s%s = 0x%08X = %s%d\n", pref, name, (CK_ULONG)val, npref, *val); +} + +static void +log_user_type (const char *pref, const char *name, CK_USER_TYPE val, CK_RV status) +{ + if (status != CKR_OK) + return; + cklog ("%s%s = ", pref, name); + log_CKU (val); + cklog ("\n"); +} + +#define BEGIN_CALL(name) \ + { \ + CK_##name _func; const char* _name; CK_RV _ret = CKR_OK; \ + if (!module_list) return CKR_CRYPTOKI_NOT_INITIALIZED; \ + _func = module_list->name; \ + _name = #name; \ + if (!_func) { \ + cklog ("module has no function for: %s\n", _name); \ + return CKR_DEVICE_ERROR; \ + } \ + LOCK_OUTPUT \ + cklog ("%s\n", _name); + +#define PROCESS_CALL(args) \ + UNLOCK_OUTPUT \ + _ret = (_func) args; \ + LOCK_OUTPUT + +#define DONE_CALL \ + cklog ("%s = ", _name); \ + log_CKR (_ret); \ + cklog ("\n"); \ + UNLOCK_OUTPUT \ + return _ret; \ + } + +#define LIN " IN: " +#define LOUT " OUT: " + +#define IN_ATTRIBUTE_ARRAY(a, n) \ + log_attribute_array (LIN, #a, a, n, CKR_OK); + +#define IN_BOOL(a) \ + log_bool (LIN, #a, a, CKR_OK); + +#define IN_BYTE_ARRAY(a, n) \ + log_byte_array (LIN, #a, a, &n, CKR_OK); + +#define IN_HANDLE(a) \ + log_ulong (LIN, #a, a, "H", CKR_OK); + +#define IN_INIT_ARGS(a) \ + log_init_args (LIN, #a, a, CKR_OK); + +#define IN_POINTER(a) \ + log_pointer (LIN, #a, a, CKR_OK); + +#define IN_MECHANISM(a) \ + log_mechanism (LIN, #a, a, CKR_OK); + +#define IN_MECHANISM_TYPE(a) \ + log_mechanism_type (LIN, #a, a, CKR_OK); + +#define IN_SESSION(a) \ + log_ulong (LIN, #a, a, "S", CKR_OK); + +#define IN_SLOT_ID(a) \ + log_ulong (LIN, #a, a, "SL", CKR_OK); + +#define IN_STRING(a) \ + log_string (LIN, #a, a, CKR_OK); + +#define IN_ULONG(a) \ + log_ulong (LIN, #a, a, NULL, CKR_OK); + +#define IN_ULONG_PTR(a) \ + log_ulong_pointer (LIN, #a, a, NULL, CKR_OK); + +#define IN_USER_TYPE(a) \ + log_user_type (LIN, #a, a, CKR_OK); + +#define OUT_ATTRIBUTE_ARRAY(a, n) \ + log_attribute_array (LOUT, #a, a, n, _ret); + +#define OUT_BYTE_ARRAY(a, n) \ + log_byte_array(LOUT, #a, a, n, _ret); + +#define OUT_HANDLE(a) \ + log_ulong_pointer (LOUT, #a, a, "H", _ret); + +#define OUT_HANDLE_ARRAY(a, n) \ + log_ulong_array (LOUT, #a, a, n, "H", _ret); + +#define OUT_INFO(a) \ + log_info (LOUT, #a, a, _ret); + +#define OUT_MECHANISM_INFO(a) \ + log_mechanism_info (LOUT, #a, a, _ret); + +#define OUT_MECHANISM_TYPE_ARRAY(a, n) \ + log_mechanism_type_array (LOUT, #a, a, n, _ret); + +#define OUT_POINTER(a) \ + log_pointer (LOUT, #a, a, _ret); + +#define OUT_SESSION(a) \ + log_ulong_pointer (LOUT, #a, a, "S", _ret); + +#define OUT_SESSION_INFO(a) \ + log_session_info (LOUT, #a, a, _ret); + +#define OUT_SLOT_ID_ARRAY(a, n) \ + log_ulong_array (LOUT, #a, a, n, "SL", _ret); + +#define OUT_SLOT_ID(a) \ + log_ulong_pointer (LOUT, #a, a, "SL", _ret); + +#define OUT_SLOT_INFO(a) \ + log_slot_info (LOUT, #a, a, _ret); + +#define OUT_TOKEN_INFO(a) \ + log_token_info (LOUT, #a, a, _ret); + +#define OUT_ULONG(a) \ + log_ulong_pointer (LOUT, #a, a, NULL, _ret); + +#define OUT_ULONG_ARRAY(a, n) \ + log_ulong_array (LOUT, #a, a, n, NULL, _ret); + + + +/* ---------------------------------------------------------------- */ + +static CK_RV +CL_C_Initialize (CK_VOID_PTR pInitArgs) +{ + CK_C_INITIALIZE_ARGS_PTR args; + CK_C_GetFunctionList func; + const char *module, *output; + CK_RV ret; + + module_handle = NULL; + module_list = NULL; + + /* Always need a relevant output file */ + if (output_file && output_file != stderr) + fclose (output_file); + output_file = stderr; + + output = getenv("P11_LOG_FILE"); + if (output && !output[0]) + output = NULL; + if (output) { + output_file = fopen (output, "ab"); + if (!output_file) { + output_file = stderr; + cklog ("couldn't open log file: %s", output); + } + } + +#ifndef _WIN32 + setlinebuf (output_file); +#endif + + /* Load the other module */ + module = getenv("P11_LOG_MODULE"); + if (!module || !module[0]) { + cklog ("P11_LOG_MODULE not set\n"); + 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 ()); + return CKR_DEVICE_ERROR; + } + + func = (CK_C_GetFunctionList)dlsym (module_handle, "C_GetFunctionList"); + if (!func) { + cklog ("couldn't find C_GetFunctionList function in module: %s: %s\n", module, dlerror ()); + 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); + return CKR_DEVICE_ERROR; + } + + if (!module_list) { + cklog ("C_GetFunctionList returned a null function list in module: %s\n", module); + return CKR_DEVICE_ERROR; + } + + if (pInitArgs != NULL) { + args = pInitArgs; + 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 p11-log debugging (%s / %s)\n", + module, output ? output : "ods"); +#else + cklog ("INITIALIZE p11-log debugging (%s / %s)\n", + module, output ? output : "stderr"); +#endif + + + + BEGIN_CALL (C_Initialize) + IN_INIT_ARGS (pInitArgs) + PROCESS_CALL ((pInitArgs)) + DONE_CALL +} + +static CK_RV +CL_C_Finalize (CK_VOID_PTR pReserved) +{ + BEGIN_CALL (C_Finalize) + IN_POINTER (pReserved) + PROCESS_CALL ((pReserved)) + DONE_CALL + + cklog ("FINALIZE p11-log debugging\n"); +} + +static CK_RV +CL_C_GetInfo (CK_INFO_PTR pInfo) +{ + BEGIN_CALL (C_GetInfo) + PROCESS_CALL ((pInfo)) + OUT_INFO (pInfo) + DONE_CALL +} + +static CK_RV +CL_C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) +{ + /* This would be a strange call to receive */ + return C_GetFunctionList (list); +} + +static CK_RV +CL_C_GetSlotList (CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) +{ + BEGIN_CALL (C_GetSlotList) + IN_BOOL (tokenPresent) + IN_ULONG_PTR (pulCount) + PROCESS_CALL ((tokenPresent, pSlotList, pulCount)) + OUT_SLOT_ID_ARRAY (pSlotList, pulCount) + DONE_CALL +} + +static CK_RV +CL_C_GetSlotInfo (CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) +{ + BEGIN_CALL (C_GetSlotInfo) + IN_SLOT_ID (slotID) + PROCESS_CALL ((slotID, pInfo)) + OUT_SLOT_INFO (pInfo) + DONE_CALL +} + +static CK_RV +CL_C_GetTokenInfo (CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo) +{ + BEGIN_CALL (C_GetTokenInfo) + IN_SLOT_ID (slotID) + PROCESS_CALL ((slotID, pInfo)) + OUT_TOKEN_INFO (pInfo) + DONE_CALL +} + +static CK_RV +CL_C_GetMechanismList (CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList, + CK_ULONG_PTR pulCount) +{ + BEGIN_CALL (C_GetMechanismList) + IN_SLOT_ID (slotID) + IN_ULONG_PTR (pulCount) + PROCESS_CALL ((slotID, pMechanismList, pulCount)) + OUT_MECHANISM_TYPE_ARRAY (pMechanismList, pulCount) + DONE_CALL +} + +static CK_RV +CL_C_GetMechanismInfo (CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, + CK_MECHANISM_INFO_PTR pInfo) +{ + BEGIN_CALL (C_GetMechanismInfo) + IN_SLOT_ID (slotID) + IN_MECHANISM_TYPE (type) + PROCESS_CALL ((slotID, type, pInfo)) + OUT_MECHANISM_INFO (pInfo) + DONE_CALL +} + +static CK_RV +CL_C_InitToken (CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen, + CK_UTF8CHAR_PTR pLabel) +{ + BEGIN_CALL (C_InitToken) + IN_SLOT_ID (slotID) + IN_BYTE_ARRAY (pPin, ulPinLen) + IN_STRING (pLabel) + PROCESS_CALL ((slotID, pPin, ulPinLen, pLabel)) + DONE_CALL +} + +static CK_RV +CL_C_WaitForSlotEvent (CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved) +{ + int had = 0; + BEGIN_CALL (C_WaitForSlotEvent) + cklog (" IN: flags = %d", flags); + LOG_FLAG (flags, had, CKF_DONT_BLOCK); + cklog ("\n"); + PROCESS_CALL ((flags, pSlot, pReserved)) + OUT_SLOT_ID (pSlot) + OUT_POINTER (pReserved) + DONE_CALL +} + +static CK_RV +CL_C_OpenSession (CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication, + CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession) +{ + int had = 0; + BEGIN_CALL (C_OpenSession) + IN_SLOT_ID (slotID) + cklog (" IN: flags = %d", flags); + LOG_FLAG (flags, had, CKF_SERIAL_SESSION); + LOG_FLAG (flags, had, CKF_RW_SESSION); + cklog ("\n"); + IN_POINTER (pApplication); + IN_POINTER (Notify); + PROCESS_CALL ((slotID, flags, pApplication, Notify, phSession)); + OUT_SESSION (phSession) + DONE_CALL +} + +static CK_RV +CL_C_CloseSession (CK_SESSION_HANDLE hSession) +{ + BEGIN_CALL (C_CloseSession) + IN_SESSION (hSession) + PROCESS_CALL ((hSession)) + DONE_CALL +} + +static CK_RV +CL_C_CloseAllSessions (CK_SLOT_ID slotID) +{ + BEGIN_CALL (C_CloseAllSessions) + IN_SLOT_ID (slotID) + PROCESS_CALL ((slotID)) + DONE_CALL +} + +static CK_RV +CL_C_GetFunctionStatus (CK_SESSION_HANDLE hSession) +{ + BEGIN_CALL (C_GetFunctionStatus) + IN_SESSION (hSession) + PROCESS_CALL ((hSession)) + DONE_CALL +} + +CK_RV +CL_C_CancelFunction (CK_SESSION_HANDLE hSession) +{ + BEGIN_CALL (C_CancelFunction) + IN_SESSION (hSession) + PROCESS_CALL ((hSession)) + DONE_CALL +}
CK_RV +CL_C_GetSessionInfo (CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo) +{ + BEGIN_CALL (C_GetSessionInfo) + IN_SESSION (hSession) + PROCESS_CALL ((hSession, pInfo)) + OUT_SESSION_INFO (pInfo) + DONE_CALL +} + +static CK_RV +CL_C_InitPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin, + CK_ULONG ulPinLen) +{ + BEGIN_CALL (C_InitPIN) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pPin, ulPinLen) + PROCESS_CALL ((hSession, pPin, ulPinLen)) + DONE_CALL +} + +static CK_RV +CL_C_SetPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin, + CK_ULONG ulOldLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewLen) +{ + BEGIN_CALL (C_SetPIN) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pOldPin, ulOldLen) + IN_BYTE_ARRAY (pNewPin, ulNewLen); + PROCESS_CALL ((hSession, pOldPin, ulOldLen, pNewPin, ulNewLen)) + DONE_CALL +} + +static CK_RV +CL_C_GetOperationState (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState, + CK_ULONG_PTR pulOperationStateLen) +{ + BEGIN_CALL (C_GetOperationState) + IN_SESSION (hSession) + IN_ULONG_PTR (pulOperationStateLen) + PROCESS_CALL ((hSession, pOperationState, pulOperationStateLen)) + OUT_BYTE_ARRAY (pOperationState, pulOperationStateLen) + DONE_CALL +} + +static CK_RV +CL_C_SetOperationState (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState, + CK_ULONG ulOperationStateLen, CK_OBJECT_HANDLE hEncryptionKey, + CK_OBJECT_HANDLE hAuthenticationKey) +{ + BEGIN_CALL (C_SetOperationState) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pOperationState, ulOperationStateLen) + IN_HANDLE (hEncryptionKey) + IN_HANDLE (hAuthenticationKey) + PROCESS_CALL ((hSession, pOperationState, ulOperationStateLen, hEncryptionKey, hAuthenticationKey)) + DONE_CALL +} + +static CK_RV +CL_C_Login (CK_SESSION_HANDLE hSession, CK_USER_TYPE userType, + CK_UTF8CHAR_PTR pPin, CK_ULONG pPinLen) +{ + BEGIN_CALL (C_Login) + IN_SESSION (hSession) + IN_USER_TYPE (userType) + IN_BYTE_ARRAY (pPin, pPinLen); + PROCESS_CALL ((hSession, userType, pPin, pPinLen)) + DONE_CALL +} + +static CK_RV +CL_C_Logout (CK_SESSION_HANDLE hSession) +{ + BEGIN_CALL (C_Logout) + IN_SESSION (hSession) + PROCESS_CALL ((hSession)) + DONE_CALL +} + +static CK_RV +CL_C_CreateObject (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, + CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phObject) +{ + BEGIN_CALL (C_CreateObject) + IN_SESSION (hSession) + IN_ATTRIBUTE_ARRAY (pTemplate, &ulCount) + PROCESS_CALL ((hSession, pTemplate, ulCount, phObject)) + OUT_HANDLE (phObject) + DONE_CALL +} + +static CK_RV +CL_C_CopyObject (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, + CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, + CK_OBJECT_HANDLE_PTR phNewObject) +{ + BEGIN_CALL (C_CopyObject) + IN_SESSION (hSession) + IN_HANDLE (hObject) + IN_ATTRIBUTE_ARRAY (pTemplate, &ulCount) + PROCESS_CALL ((hSession, hObject, pTemplate, ulCount, phNewObject)) + OUT_HANDLE (phNewObject) + DONE_CALL +} + + +static CK_RV +CL_C_DestroyObject (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject) +{ + BEGIN_CALL (C_DestroyObject); + IN_SESSION (hSession) + IN_HANDLE (hObject) + PROCESS_CALL ((hSession, hObject)) + DONE_CALL +} + +static CK_RV +CL_C_GetObjectSize (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, + CK_ULONG_PTR pulSize) +{ + BEGIN_CALL (C_GetObjectSize); + IN_SESSION (hSession) + IN_HANDLE (hObject) + PROCESS_CALL ((hSession, hObject, pulSize)) + OUT_ULONG (pulSize) + DONE_CALL +} + +static CK_RV +CL_C_GetAttributeValue (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, + CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount) +{ + BEGIN_CALL (C_GetAttributeValue) + IN_SESSION (hSession) + IN_HANDLE (hObject) + IN_ATTRIBUTE_ARRAY (pTemplate, &ulCount) + PROCESS_CALL ((hSession, hObject, pTemplate, ulCount)) + OUT_ATTRIBUTE_ARRAY (pTemplate, &ulCount) + DONE_CALL +} + +static CK_RV +CL_C_SetAttributeValue (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, + CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount) +{ + BEGIN_CALL (C_SetAttributeValue) + IN_SESSION (hSession) + IN_HANDLE (hObject) + IN_ATTRIBUTE_ARRAY (pTemplate, &ulCount) + PROCESS_CALL ((hSession, hObject, pTemplate, ulCount)) + DONE_CALL +} + +static CK_RV +CL_C_FindObjectsInit (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, + CK_ULONG ulCount) +{ + BEGIN_CALL (C_FindObjectsInit) + IN_SESSION (hSession) + IN_ATTRIBUTE_ARRAY (pTemplate, &ulCount) + PROCESS_CALL ((hSession, pTemplate, ulCount)) + DONE_CALL +} + +static CK_RV +CL_C_FindObjects (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject, + CK_ULONG ulMaxObjectCount, CK_ULONG_PTR pulObjectCount) +{ + BEGIN_CALL (C_FindObjects) + IN_SESSION (hSession) + IN_ULONG (ulMaxObjectCount) + PROCESS_CALL ((hSession, phObject, ulMaxObjectCount, pulObjectCount)) + OUT_HANDLE_ARRAY (phObject, pulObjectCount) + DONE_CALL +} + +static CK_RV +CL_C_FindObjectsFinal (CK_SESSION_HANDLE hSession) +{ + BEGIN_CALL (C_FindObjectsFinal) + IN_SESSION (hSession) + PROCESS_CALL ((hSession)) + DONE_CALL +} + +static CK_RV +CL_C_EncryptInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, + CK_OBJECT_HANDLE hKey) +{ + BEGIN_CALL (C_EncryptInit) + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + IN_HANDLE (hKey) + PROCESS_CALL ((hSession, pMechanism, hKey)) + DONE_CALL +} + +static CK_RV +CL_C_Encrypt (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, + CK_BYTE_PTR pEncryptedData, CK_ULONG_PTR pulEncryptedDataLen) +{ + BEGIN_CALL (C_Encrypt) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pData, ulDataLen) + PROCESS_CALL ((hSession, pData, ulDataLen, pEncryptedData, pulEncryptedDataLen)) + OUT_BYTE_ARRAY (pEncryptedData, pulEncryptedDataLen) + DONE_CALL +} + +static CK_RV +CL_C_EncryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, + CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart, + CK_ULONG_PTR pulEncryptedPartLen) +{ + BEGIN_CALL (C_EncryptUpdate) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pPart, ulPartLen) + PROCESS_CALL ((hSession, pPart, ulPartLen, pEncryptedPart, pulEncryptedPartLen)) + OUT_BYTE_ARRAY (pEncryptedPart, pulEncryptedPartLen) + DONE_CALL +} + +static CK_RV +CL_C_EncryptFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pLastEncryptedPart, + CK_ULONG_PTR pulLastEncryptedPartLen) +{ + BEGIN_CALL (C_EncryptFinal) + IN_SESSION (hSession) + PROCESS_CALL ((hSession, pLastEncryptedPart, pulLastEncryptedPartLen)) + OUT_BYTE_ARRAY (pLastEncryptedPart, pulLastEncryptedPartLen) + DONE_CALL +} + +static CK_RV +CL_C_DecryptInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, + CK_OBJECT_HANDLE hKey) +{ + BEGIN_CALL (C_DecryptInit) + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + IN_HANDLE (hKey) + PROCESS_CALL ((hSession, pMechanism, hKey)) + DONE_CALL +} + +static CK_RV +CL_C_Decrypt (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedData, + CK_ULONG pulEncryptedDataLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen) +{ + BEGIN_CALL (C_Decrypt) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pEncryptedData, pulEncryptedDataLen) + PROCESS_CALL ((hSession, pEncryptedData, pulEncryptedDataLen, pData, pulDataLen)) + OUT_BYTE_ARRAY (pData, pulDataLen) + DONE_CALL +} + +static CK_RV +CL_C_DecryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart, + CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen) +{ + BEGIN_CALL (C_DecryptUpdate) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pEncryptedPart, ulEncryptedPartLen) + PROCESS_CALL ((hSession, pEncryptedPart, ulEncryptedPartLen, pPart, pulPartLen)) + OUT_BYTE_ARRAY (pPart, pulPartLen) + DONE_CALL +} + +static CK_RV +CL_C_DecryptFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pLastPart, + CK_ULONG_PTR pulLastPartLen) +{ + BEGIN_CALL (C_DecryptFinal) + IN_SESSION (hSession) + PROCESS_CALL ((hSession, pLastPart, pulLastPartLen)) + OUT_BYTE_ARRAY (pLastPart, pulLastPartLen) + DONE_CALL +} + +static CK_RV +CL_C_DigestInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism) +{ + BEGIN_CALL (C_DigestInit) + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + PROCESS_CALL ((hSession, pMechanism)) + DONE_CALL +} + +static CK_RV +CL_C_Digest (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, + CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen) +{ + BEGIN_CALL (C_Digest) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pData, ulDataLen) + PROCESS_CALL ((hSession, pData, ulDataLen, pDigest, pulDigestLen)) + OUT_BYTE_ARRAY (pDigest, pulDigestLen) + DONE_CALL +} + +static CK_RV +CL_C_DigestUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen) +{ + BEGIN_CALL (C_DigestUpdate) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pPart, ulPartLen) + PROCESS_CALL ((hSession, pPart, ulPartLen)) + DONE_CALL +} + +static CK_RV +CL_C_DigestKey (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey) +{ + BEGIN_CALL (C_DigestKey) + IN_SESSION (hSession) + IN_HANDLE (hKey) + PROCESS_CALL ((hSession, hKey)) + DONE_CALL +} + +static CK_RV +CL_C_DigestFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest, + CK_ULONG_PTR pulDigestLen) +{ + BEGIN_CALL (C_DigestFinal) + IN_SESSION (hSession) + PROCESS_CALL ((hSession, pDigest, pulDigestLen)) + OUT_BYTE_ARRAY (pDigest, pulDigestLen) + DONE_CALL +} + +static CK_RV +CL_C_SignInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, + CK_OBJECT_HANDLE hKey) +{ + BEGIN_CALL (C_SignInit) + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + IN_HANDLE (hKey) + PROCESS_CALL ((hSession, pMechanism, hKey)) + DONE_CALL +} + +static CK_RV +CL_C_Sign (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, + CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen) +{ + BEGIN_CALL (C_Sign) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pData, ulDataLen) + PROCESS_CALL ((hSession, pData, ulDataLen, pSignature, pulSignatureLen)) + OUT_BYTE_ARRAY (pSignature, pulSignatureLen) + DONE_CALL +} + +static CK_RV +CL_C_SignUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen) +{ + BEGIN_CALL (C_SignUpdate) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pPart, ulPartLen) + PROCESS_CALL ((hSession, pPart, ulPartLen)) + DONE_CALL +} + +static CK_RV +CL_C_SignFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature, + CK_ULONG_PTR pulSignatureLen) +{ + BEGIN_CALL (C_SignFinal) + IN_SESSION (hSession) + PROCESS_CALL ((hSession, pSignature, pulSignatureLen)) + OUT_BYTE_ARRAY (pSignature, pulSignatureLen) + DONE_CALL +} + +static CK_RV +CL_C_SignRecoverInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, + CK_OBJECT_HANDLE hKey) +{ + BEGIN_CALL (C_SignRecoverInit) + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + IN_HANDLE (hKey) + PROCESS_CALL ((hSession, pMechanism, hKey)) + DONE_CALL +} + +static CK_RV +CL_C_SignRecover (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, + CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen) +{ + BEGIN_CALL (C_SignRecover) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pData, ulDataLen) + PROCESS_CALL ((hSession, pData, ulDataLen, pSignature, pulSignatureLen)) + OUT_BYTE_ARRAY (pSignature, pulSignatureLen) + DONE_CALL +} + +static CK_RV +CL_C_VerifyInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, + CK_OBJECT_HANDLE hKey) +{ + BEGIN_CALL (C_VerifyInit); + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + IN_HANDLE (hKey) + PROCESS_CALL ((hSession, pMechanism, hKey)) + DONE_CALL +} + +static CK_RV +CL_C_Verify (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, + CK_BYTE_PTR pSignature, CK_ULONG pulSignatureLen) +{ + BEGIN_CALL (C_Verify) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pData, ulDataLen) + IN_BYTE_ARRAY (pSignature, pulSignatureLen) + PROCESS_CALL ((hSession, pData, ulDataLen, pSignature, pulSignatureLen)) + DONE_CALL +} + +static CK_RV +CL_C_VerifyUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen) +{ + BEGIN_CALL (C_VerifyUpdate) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pPart, ulPartLen) + PROCESS_CALL ((hSession, pPart, ulPartLen)) + DONE_CALL +} + +static CK_RV +CL_C_VerifyFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature, + CK_ULONG pulSignatureLen) +{ + BEGIN_CALL (C_VerifyFinal) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pSignature, pulSignatureLen); + PROCESS_CALL ((hSession, pSignature, pulSignatureLen)) + DONE_CALL +} + +static CK_RV +CL_C_VerifyRecoverInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, + CK_OBJECT_HANDLE hKey) +{ + BEGIN_CALL (C_VerifyRecoverInit) + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + IN_HANDLE (hKey) + PROCESS_CALL ((hSession, pMechanism, hKey)) + DONE_CALL +} + +static CK_RV +CL_C_VerifyRecover (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature, + CK_ULONG pulSignatureLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen) +{ + BEGIN_CALL (C_VerifyRecover) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pSignature, pulSignatureLen) + PROCESS_CALL ((hSession, pSignature, pulSignatureLen, pData, pulDataLen)) + OUT_BYTE_ARRAY (pData, pulDataLen) + DONE_CALL +} + +static CK_RV +CL_C_DigestEncryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, + CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart, + CK_ULONG_PTR ulEncryptedPartLen) +{ + BEGIN_CALL (C_DigestEncryptUpdate); + IN_SESSION (hSession) + IN_BYTE_ARRAY (pPart, ulPartLen) + PROCESS_CALL ((hSession, pPart, ulPartLen, pEncryptedPart, ulEncryptedPartLen)) + OUT_BYTE_ARRAY (pEncryptedPart, ulEncryptedPartLen) + DONE_CALL +} + +static CK_RV +CL_C_DecryptDigestUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart, + CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, + CK_ULONG_PTR pulPartLen) +{ + BEGIN_CALL (C_DecryptDigestUpdate) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pEncryptedPart, ulEncryptedPartLen) + PROCESS_CALL ((hSession, pEncryptedPart, ulEncryptedPartLen, pPart, pulPartLen)) + OUT_BYTE_ARRAY (pPart, pulPartLen) + DONE_CALL +} + +static CK_RV +CL_C_SignEncryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, + CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart, + CK_ULONG_PTR ulEncryptedPartLen) +{ + BEGIN_CALL (C_SignEncryptUpdate) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pPart, ulPartLen) + PROCESS_CALL ((hSession, pPart, ulPartLen, pEncryptedPart, ulEncryptedPartLen)) + OUT_BYTE_ARRAY (pEncryptedPart, ulEncryptedPartLen) + DONE_CALL +} + +static CK_RV +CL_C_DecryptVerifyUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart, + CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, + CK_ULONG_PTR pulPartLen) +{ + BEGIN_CALL (C_DecryptVerifyUpdate) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pEncryptedPart, ulEncryptedPartLen) + PROCESS_CALL ((hSession, pEncryptedPart, ulEncryptedPartLen, pPart, pulPartLen)) + OUT_BYTE_ARRAY (pPart, pulPartLen) + DONE_CALL +} + +static CK_RV +CL_C_GenerateKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, + CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, + CK_OBJECT_HANDLE_PTR phKey) +{ + BEGIN_CALL (C_GenerateKey) + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + IN_ATTRIBUTE_ARRAY (pTemplate, &ulCount) + PROCESS_CALL ((hSession, pMechanism, pTemplate, ulCount, phKey)) + OUT_HANDLE (phKey) + DONE_CALL +} + +static CK_RV +CL_C_GenerateKeyPair (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, + CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicKeyAttributeCount, + CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateKeyAttributeCount, + CK_OBJECT_HANDLE_PTR phPublicKey, CK_OBJECT_HANDLE_PTR phPrivateKey) +{ + BEGIN_CALL (C_GenerateKeyPair) + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + IN_ATTRIBUTE_ARRAY (pPublicKeyTemplate, &ulPublicKeyAttributeCount) + IN_ATTRIBUTE_ARRAY (pPrivateKeyTemplate, &ulPrivateKeyAttributeCount) + PROCESS_CALL ((hSession, pMechanism, pPublicKeyTemplate, ulPublicKeyAttributeCount, + pPrivateKeyTemplate, ulPrivateKeyAttributeCount, phPublicKey, phPrivateKey)) + OUT_HANDLE (phPublicKey) + OUT_HANDLE (phPrivateKey) + DONE_CALL +} + +static CK_RV +CL_C_WrapKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, + CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey, + CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen) +{ + BEGIN_CALL (C_WrapKey) + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + IN_HANDLE (hWrappingKey) + IN_HANDLE (hKey) + PROCESS_CALL ((hSession, pMechanism, hWrappingKey, hKey, pWrappedKey, pulWrappedKeyLen)) + OUT_BYTE_ARRAY (pWrappedKey, pulWrappedKeyLen) + DONE_CALL +} + +static CK_RV +CL_C_UnwrapKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, + CK_OBJECT_HANDLE pUnwrappingKey, CK_BYTE_PTR pWrappedKey, + CK_ULONG pulWrappedKeyLen, CK_ATTRIBUTE_PTR pTemplate, + CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey) +{ + BEGIN_CALL (C_UnwrapKey) + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + IN_HANDLE (pUnwrappingKey) + IN_BYTE_ARRAY (pWrappedKey, pulWrappedKeyLen) + IN_ATTRIBUTE_ARRAY (pTemplate, &ulCount) + PROCESS_CALL ((hSession, pMechanism, pUnwrappingKey, pWrappedKey, + pulWrappedKeyLen, pTemplate, ulCount, phKey)) + OUT_HANDLE (phKey) + DONE_CALL +} + +static CK_RV +CL_C_DeriveKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, + CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, + CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey) +{ + BEGIN_CALL (C_DeriveKey) + IN_SESSION (hSession) + IN_MECHANISM (pMechanism) + IN_HANDLE (hBaseKey) + IN_ATTRIBUTE_ARRAY (pTemplate, &ulCount) + PROCESS_CALL ((hSession, pMechanism, hBaseKey, pTemplate, ulCount, phKey)) + OUT_HANDLE (phKey) + DONE_CALL +} + +static CK_RV +CL_C_SeedRandom (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen) +{ + BEGIN_CALL (C_SeedRandom) + IN_SESSION (hSession) + IN_BYTE_ARRAY (pSeed, ulSeedLen); + PROCESS_CALL ((hSession, pSeed, ulSeedLen)) + DONE_CALL +} + +static CK_RV +CL_C_GenerateRandom (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData, + CK_ULONG ulRandomLen) +{ + BEGIN_CALL (C_GenerateRandom) + IN_SESSION (hSession) + IN_ULONG (ulRandomLen) + PROCESS_CALL ((hSession, pRandomData, ulRandomLen)) + OUT_BYTE_ARRAY (pRandomData, &ulRandomLen) + DONE_CALL +} + +static struct CK_FUNCTION_LIST functionList = { + { 2, 11 }, /* version */ + CL_C_Initialize, + CL_C_Finalize, + CL_C_GetInfo, + CL_C_GetFunctionList, + CL_C_GetSlotList, + CL_C_GetSlotInfo, + CL_C_GetTokenInfo, + CL_C_GetMechanismList, + CL_C_GetMechanismInfo, + CL_C_InitToken, + CL_C_InitPIN, + CL_C_SetPIN, + CL_C_OpenSession, + CL_C_CloseSession, + CL_C_CloseAllSessions, + CL_C_GetSessionInfo, + CL_C_GetOperationState, + CL_C_SetOperationState, + CL_C_Login, + CL_C_Logout, + CL_C_CreateObject, + CL_C_CopyObject, + CL_C_DestroyObject, + CL_C_GetObjectSize, + CL_C_GetAttributeValue, + CL_C_SetAttributeValue, + CL_C_FindObjectsInit, + CL_C_FindObjects, + CL_C_FindObjectsFinal, + CL_C_EncryptInit, + CL_C_Encrypt, + CL_C_EncryptUpdate, + CL_C_EncryptFinal, + CL_C_DecryptInit, + CL_C_Decrypt, + CL_C_DecryptUpdate, + CL_C_DecryptFinal, + CL_C_DigestInit, + CL_C_Digest, + CL_C_DigestUpdate, + CL_C_DigestKey, + CL_C_DigestFinal, + CL_C_SignInit, + CL_C_Sign, + CL_C_SignUpdate, + CL_C_SignFinal, + CL_C_SignRecoverInit, + CL_C_SignRecover, + CL_C_VerifyInit, + CL_C_Verify, + CL_C_VerifyUpdate, + CL_C_VerifyFinal, + CL_C_VerifyRecoverInit, + CL_C_VerifyRecover, + CL_C_DigestEncryptUpdate, + CL_C_DecryptDigestUpdate, + CL_C_SignEncryptUpdate, + CL_C_DecryptVerifyUpdate, + CL_C_GenerateKey, + CL_C_GenerateKeyPair, + CL_C_WrapKey, + CL_C_UnwrapKey, + CL_C_DeriveKey, + CL_C_SeedRandom, + CL_C_GenerateRandom, + CL_C_GetFunctionStatus, + CL_C_CancelFunction, + CL_C_WaitForSlotEvent +}; + +#ifdef _WIN32 +__declspec(dllexport) CK_RV +#else +CK_RV +#endif +C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) +{ + if (!list) + return CKR_ARGUMENTS_BAD; + + *list = &functionList; + return CKR_OK; +} |