summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/check.c2
-rw-r--r--src/config.c2
-rw-r--r--src/key.c2
-rw-r--r--src/module.c219
-rw-r--r--src/msg.c48
-rw-r--r--src/object.c2
-rw-r--r--src/p11-tests.c2
-rw-r--r--src/p11-tests.h10
-rw-r--r--src/rsa.c30
-rw-r--r--src/session.c4
-rw-r--r--src/slot.c11
-rw-r--r--src/test-data.c6
12 files changed, 253 insertions, 85 deletions
diff --git a/src/check.c b/src/check.c
index 334a9e7..d76f0e9 100644
--- a/src/check.c
+++ b/src/check.c
@@ -1,6 +1,4 @@
-#include "config.h"
-
#include "p11-tests.h"
#include <ctype.h>
diff --git a/src/config.c b/src/config.c
index 9ca3bb0..48b4151 100644
--- a/src/config.c
+++ b/src/config.c
@@ -1,6 +1,4 @@
-#include "config.h"
-
#include "p11-tests.h"
#include <ctype.h>
diff --git a/src/key.c b/src/key.c
index f8b79ae..495cff4 100644
--- a/src/key.c
+++ b/src/key.c
@@ -1,6 +1,4 @@
-#include "config.h"
-
#include "p11-tests.h"
CK_OBJECT_HANDLE
diff --git a/src/module.c b/src/module.c
index e47ba38..cda07a2 100644
--- a/src/module.c
+++ b/src/module.c
@@ -1,15 +1,27 @@
-#include "config.h"
+#ifdef _WIN32
-#include "p11-tests.h"
+#define WIN32_LEAN_AND_MEAN
+#define _WIN32_WINNT 0x400
+#include <windows.h>
+
+static HMODULE module = NULL;
+
+#else /* !_WIN32 */
#include <dlfcn.h>
-#include <errno.h>
#include <pthread.h>
+
+static void *module = NULL;
+
+#endif /* !_WIN32 */
+
+#include "p11-tests.h"
+
+#include <errno.h>
#include <stdlib.h>
#include <string.h>
-static void *module = NULL;
CK_FUNCTION_LIST_PTR p11t_module_funcs = NULL;
static const char *init_string = NULL;
@@ -27,127 +39,221 @@ p11t_module_config(const char *name, const char *value)
static CK_RV
create_mutex(void **mutex)
{
- pthread_mutex_t *mut;
-
if(!mutex)
{
p11t_msg_print("CreateMutex: null mutex passed");
return CKR_ARGUMENTS_BAD;
}
- mut = malloc(sizeof(pthread_mutex_t));
- assert(mut);
+#ifdef _WIN32
+
+ *mutex = CreateMutex(NULL, FALSE, NULL);
+ assert(*mutex != NULL);
+
+#else /* !_WIN32 */
+
+ {
+ pthread_mutex_t *mut;
+ mut = malloc(sizeof(pthread_mutex_t));
+ assert(mut);
+
+ if(pthread_mutex_init(mut, NULL) != 0)
+ assert(0);
+ *mutex = mut;
+ }
+
+#endif /* !_WIN32 */
- if(pthread_mutex_init(mut, NULL) != 0)
- assert(0);
- *mutex = mut;
return CKR_OK;
}
static CK_RV
destroy_mutex (void *mutex)
{
- int res;
-
if(!mutex)
{
p11t_msg_print("DestroyMutex: null mutex");
return CKR_MUTEX_BAD;
}
- res = pthread_mutex_destroy(mutex);
- if(res != 0)
+#ifdef _WIN32
+
+ if(!CloseHandle((HANDLE)mutex))
{
- if(res == EBUSY)
- {
- p11t_msg_print("DestroyMutex: mutex is locked");
- return CKR_GENERAL_ERROR;
- }
- else if(res == EINVAL)
+ DWORD error = GetLastError();
+ if(error == ERROR_INVALID_HANDLE)
{
p11t_msg_print("DestroyMutex: mutex is invalid");
return CKR_MUTEX_BAD;
}
else
{
- p11t_msg_print("DestroyMutex: failed: %d", res);
+ p11t_msg_print("DestroyMutex: failed: %d", GetLastError());
return CKR_GENERAL_ERROR;
+ }
+ }
+
+#else /* !_WIN32 */
+
+ {
+ int res = pthread_mutex_destroy(mutex);
+ if(res != 0)
+ {
+ if(res == EBUSY)
+ {
+ p11t_msg_print("DestroyMutex: mutex is locked");
+ return CKR_GENERAL_ERROR;
+ }
+ else if(res == EINVAL)
+ {
+ p11t_msg_print("DestroyMutex: mutex is invalid");
+ return CKR_MUTEX_BAD;
+ }
+ else
+ {
+ p11t_msg_print("DestroyMutex: failed: %d", res);
+ return CKR_GENERAL_ERROR;
+
+ }
}
}
+#endif /* !_WIN32 */
+
return CKR_OK;
}
static CK_RV
lock_mutex (void *mutex)
{
- int res;
- if (!mutex)
+ if(!mutex)
{
p11t_msg_print("LockMutex: null mutex");
return CKR_MUTEX_BAD;
}
- res = pthread_mutex_lock(mutex);
- if(res != 0)
+#ifdef _WIN32
+
{
- if(res == EDEADLK)
+ DWORD result = WaitForSingleObject((HANDLE)mutex, INFINITE);
+ if(result == WAIT_ABANDONED)
{
- p11t_msg_print("LockMutex: would deadlock");
+ p11t_msg_print("LockMutex: thread exited without releasing mutex");
return CKR_CANT_LOCK;
}
- else if(res == EINVAL)
+ else if(result == WAIT_FAILED)
{
- p11t_msg_print("LockMutex: invalid mutex used");
- return CKR_MUTEX_BAD;
+ DWORD error = GetLastError();
+ if(error == ERROR_INVALID_HANDLE)
+ {
+ p11t_msg_print("LockMutex: invalid handle");
+ return CKR_MUTEX_BAD;
+ }
+ else
+ {
+ p11t_msg_print("LockMutex: failed: %d", error);
+ return CKR_GENERAL_ERROR;
+ }
}
- else
+ else if(result != WAIT_OBJECT_0)
{
- p11t_msg_print("LockMutex: failed: %d", res);
- return CKR_GENERAL_ERROR;
+ p11t_msg_print("LockMutex: couldn't lock");
+ return CKR_CANT_LOCK;
+ }
+ }
+#else /* !_WIN32 */
+
+ int res = pthread_mutex_lock(mutex);
+ if(res != 0)
+ {
+ if(res == EDEADLK)
+ {
+ p11t_msg_print("LockMutex: would deadlock");
+ return CKR_CANT_LOCK;
+ }
+ else if(res == EINVAL)
+ {
+ p11t_msg_print("LockMutex: invalid mutex used");
+ return CKR_MUTEX_BAD;
+ }
+ else
+ {
+ p11t_msg_print("LockMutex: failed: %d", res);
+ return CKR_GENERAL_ERROR;
+
+ }
}
}
+#endif /* !_WIN32 */
+
return CKR_OK;
}
static CK_RV
unlock_mutex (void *mutex)
{
- int res;
- if (!mutex)
+ if(!mutex)
{
p11t_msg_print("UnlockMutex: null mutex");
return CKR_MUTEX_BAD;
}
- res = pthread_mutex_lock(mutex);
- if(res != 0)
+#ifdef _WIN32
+
+ if(!ReleaseMutex((HANDLE)mutex))
{
- if(res == EPERM)
+ DWORD error = GetLastError();
+ if(error == ERROR_NOT_OWNER)
{
p11t_msg_print("UnlockMutex: mutex not locked");
return CKR_MUTEX_NOT_LOCKED;
}
- else if(res == EINVAL)
+ else if(error == ERROR_INVALID_HANDLE)
{
p11t_msg_print("UnlockMutex: invalid mutex used");
return CKR_MUTEX_BAD;
}
else
{
- p11t_msg_print("UnlockMutex: failed: %d", res);
+ p11t_msg_print("UnlockMutex: failed: %d", error);
return CKR_GENERAL_ERROR;
-
}
}
- else
+
+#else /* !_WIN32 */
+
{
- free(mutex);
+ int res = pthread_mutex_lock(mutex);
+ if(res != 0)
+ {
+ if(res == EPERM)
+ {
+ p11t_msg_print("UnlockMutex: mutex not locked");
+ return CKR_MUTEX_NOT_LOCKED;
+ }
+ else if(res == EINVAL)
+ {
+ p11t_msg_print("UnlockMutex: invalid mutex used");
+ return CKR_MUTEX_BAD;
+ }
+ else
+ {
+ p11t_msg_print("UnlockMutex: failed: %d", res);
+ return CKR_GENERAL_ERROR;
+
+ }
+ }
+ else
+ {
+ free(mutex);
+ }
}
+#endif /* !_WIN32 */
+
return CKR_OK;
}
@@ -158,6 +264,20 @@ p11t_module_load(const char *filename)
CK_C_GetFunctionList get_function_list;
CK_RV rv;
+#ifdef _WIN32
+
+ module = LoadLibrary(filename);
+ if(!module)
+ p11t_msg_fatal("couldn't load library: %s: %s", filename, p11t_msg_lasterr());
+
+ /* Lookup the appropriate function in the library */
+ get_function_list = (CK_C_GetFunctionList)GetProcAddress(module, "C_GetFunctionList");
+ if(!get_function_list)
+ p11t_msg_fatal("C_GetFunctionList: couldn't find function in library: %s: %s",
+ filename, p11t_msg_lasterr());
+
+#else /* !_WIN32 */
+
module = dlopen(filename, RTLD_NOW);
if(!module)
p11t_msg_fatal("couldn't open library: %s: %s", filename, dlerror());
@@ -165,7 +285,10 @@ p11t_module_load(const char *filename)
/* Lookup the appropriate function in library */
get_function_list = (CK_C_GetFunctionList)dlsym (module, "C_GetFunctionList");
if(!get_function_list)
- p11t_msg_fatal("C_GetFunctionList: couldn't find function in library: %s: %s", filename, dlerror());
+ p11t_msg_fatal("C_GetFunctionList: couldn't find function in library: %s: %s",
+ filename, dlerror());
+
+#endif /* !_WIN32 */
/* Get the function list */
rv = (get_function_list)(&p11t_module_funcs);
@@ -345,6 +468,12 @@ void
p11t_module_unload(void)
{
if(module)
+ {
+#ifdef _WIN32
+ FreeLibrary(module);
+#else
dlclose(module);
+#endif
+ }
module = NULL;
}
diff --git a/src/msg.c b/src/msg.c
index ef218bf..dc7d2a4 100644
--- a/src/msg.c
+++ b/src/msg.c
@@ -1,12 +1,18 @@
-#include "config.h"
-
#include "p11-tests.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define _WIN32_WINNT 0x400
+#include <windows.h>
+#else
+#include <errno.h>
+#endif
+
static const char *the_prefix = NULL;
const char*
@@ -107,10 +113,46 @@ p11t_msg_rv(CK_RV rv)
}
}
+#ifdef _WIN32
+
+static char last_error[1024];
+
+const char*
+p11t_msg_lasterr(void)
+{
+ LPVOID lpMsgBuf;
+ DWORD error = GetLastError();
+ DWORD dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, error,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+ (LPTSTR) &lpMsgBuf, 0, NULL);
+ if(dwRet && lpMsgBuf)
+ {
+ strncpy(last_error, lpMsgBuf, 1024);
+ LocalFree(lpMsgBuf);
+ }
+ else
+ {
+ _snprintf(last_error, 1024, "unknown error: 0x%08x", (int)error);
+ }
+
+ return last_error;
+}
+
+#else /* _WIN32 */
+
+const char*
+p11t_msg_lasterr(void)
+{
+ return strerror(errno);
+}
+
+#endif /* _WIN32 */
+
void
p11t_msg_va(const char *message, va_list va)
{
- int len;
+ size_t len;
if(the_prefix)
fprintf(stdout, "%s: ", the_prefix);
diff --git a/src/object.c b/src/object.c
index fe4e801..bce527c 100644
--- a/src/object.c
+++ b/src/object.c
@@ -1,6 +1,4 @@
-#include "config.h"
-
#include "p11-tests.h"
#include <stdlib.h>
diff --git a/src/p11-tests.c b/src/p11-tests.c
index 58d547a..58f0413 100644
--- a/src/p11-tests.c
+++ b/src/p11-tests.c
@@ -1,10 +1,10 @@
#include "p11-tests.h"
+#include "compat.h"
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
int p11t_test_unexpected = 1;
diff --git a/src/p11-tests.h b/src/p11-tests.h
index c4a2e25..9be69da 100644
--- a/src/p11-tests.h
+++ b/src/p11-tests.h
@@ -1,6 +1,14 @@
#ifndef P11TESTST_H_
#define P11TESTST_H_
+#ifndef _WIN32
+#include "config.h"
+#endif
+
+#ifdef _MSC_VER
+#pragma warning(disable : 4996)
+#endif
+
#include "pkcs11/pkcs11.h"
#include <assert.h>
@@ -17,6 +25,7 @@ extern int p11t_test_unexpected;
*/
const char* p11t_msg_rv(CK_RV rv);
+const char* p11t_msg_lasterr(void);
void p11t_msg_va(const char *message, va_list va);
void p11t_msg_print(const char *message, ...);
@@ -139,6 +148,7 @@ void p11t_slot_for_each_mech(CK_MECHANISM_TYPE mech_type, P11tSlotMechCallback c
* test-data.c
*/
+#define P11T_BLOCK 1024
extern const CK_BYTE p11t_test_data[];
extern const CK_ULONG p11t_test_data_size;
extern const CK_ULONG p11t_test_data_bits;
diff --git a/src/rsa.c b/src/rsa.c
index 2a343d6..a960d63 100644
--- a/src/rsa.c
+++ b/src/rsa.c
@@ -1,6 +1,4 @@
-#include "config.h"
-
#include "p11-tests.h"
#include <stdio.h>
@@ -17,8 +15,8 @@ static void
test_rsa_decrypt(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key,
CK_MECHANISM_TYPE mech_type, RSA* rsa)
{
- CK_BYTE encrypted[p11t_test_data_size];
- CK_BYTE decrypted[p11t_test_data_size];
+ CK_BYTE encrypted[P11T_BLOCK];
+ CK_BYTE decrypted[P11T_BLOCK];
CK_MECHANISM mech;
const CK_BYTE* data;
CK_ULONG n_data, n_decrypted;
@@ -34,7 +32,7 @@ test_rsa_decrypt(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key,
assert(size);
assert(size < sizeof(encrypted));
assert(size < sizeof(decrypted));
- assert(size < n_data);
+ assert(size < (int)n_data);
switch(mech_type)
{
@@ -113,7 +111,7 @@ hash_for_rsa_pkcs_sign(int algo, int wrap, const CK_BYTE* data,
if(!wrap)
{
assert(*n_output >= n_hash);
- *n_output = n_hash;
+ *n_output = (CK_ULONG)n_hash;
memcpy(output, hash, n_hash);
return;
}
@@ -127,10 +125,10 @@ hash_for_rsa_pkcs_sign(int algo, int wrap, const CK_BYTE* data,
sig.algor->parameter = &parameter;
sig.digest = &digest;
sig.digest->data = hash;
- sig.digest->length = n_hash;
+ sig.digest->length = (int)n_hash;
val = i2d_X509_SIG(&sig, &output);
- assert(*n_output >= val);
+ assert(*n_output >= (CK_ULONG)val);
*n_output = val;
}
@@ -138,8 +136,8 @@ static void
test_rsa_pkcs_sign_hash(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key,
RSA* rsa, int algo)
{
- CK_BYTE hash[p11t_test_data_size];
- CK_BYTE sig[p11t_test_data_size];
+ CK_BYTE hash[P11T_BLOCK];
+ CK_BYTE sig[P11T_BLOCK];
CK_MECHANISM mech;
CK_ULONG n_hash, n_sig;
CK_RV rv;
@@ -174,8 +172,8 @@ static void
test_rsa_x509_sign(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key, RSA* rsa)
{
const CK_BYTE* data;
- CK_BYTE sig[p11t_test_data_size];
- CK_BYTE check[p11t_test_data_size];
+ CK_BYTE sig[P11T_BLOCK];
+ CK_BYTE check[P11T_BLOCK];
CK_MECHANISM mech;
CK_ULONG n_data, n_sig;
CK_RV rv;
@@ -202,7 +200,7 @@ test_rsa_x509_sign(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key, RSA* rsa)
if(res < 0)
p11t_check_fail("C_Sign: rsa x509 signature was invalid");
- assert(res > n_data);
+ assert(res > (int)n_data);
if(memcmp(check + (res - n_data), data, n_data) != 0)
p11t_check_fail("C_Sign: rsa x509 signature did not verify");
}
@@ -267,8 +265,8 @@ static void
test_rsa_encrypt(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key,
CK_MECHANISM_TYPE mech_type, RSA* rsa)
{
- CK_BYTE encrypted[p11t_test_data_size];
- CK_BYTE check[p11t_test_data_size];
+ CK_BYTE encrypted[P11T_BLOCK];
+ CK_BYTE check[P11T_BLOCK];
CK_OBJECT_HANDLE privkey;
CK_MECHANISM mech;
const CK_BYTE* data;
@@ -285,7 +283,7 @@ test_rsa_encrypt(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key,
assert(size);
assert(size < sizeof(encrypted));
assert(size < sizeof(check));
- assert(size < n_data);
+ assert(size < (int)n_data);
switch(mech_type)
{
diff --git a/src/session.c b/src/session.c
index 3db9b37..356fd8e 100644
--- a/src/session.c
+++ b/src/session.c
@@ -1,6 +1,4 @@
-#include "config.h"
-
#include "p11-tests.h"
#include <string.h>
@@ -79,7 +77,7 @@ calculate_pin(CK_SLOT_ID slot, CK_USER_TYPE user, CK_ULONG_PTR n_pin)
pin = NULL;
if(pin)
- *n_pin = strlen(pin);
+ *n_pin = (CK_ULONG)strlen(pin);
return (CK_UTF8CHAR_PTR)pin;
}
diff --git a/src/slot.c b/src/slot.c
index c9139d6..e3ff99f 100644
--- a/src/slot.c
+++ b/src/slot.c
@@ -1,6 +1,4 @@
-#include "config.h"
-
#include "p11-tests.h"
#include <stdlib.h>
@@ -43,7 +41,7 @@ test_slot_global(void)
/** - Normal call */
rv = (p11t_module_funcs->C_GetInfo)(&slot_global);
- if(p11t_check_returns("C_GetInfo", rv, CKR_OK))
+ if(!p11t_check_returns("C_GetInfo", rv, CKR_OK))
{
memset(&slot_global, 0, sizeof(CK_INFO));
return;
@@ -307,6 +305,9 @@ test_slot_mechanisms(void)
rv = (p11t_module_funcs->C_GetMechanismList)(slot_id, NULL, &mech_count);
p11t_check_returns("C_GetMechanismList: without buffer", rv, CKR_OK);
+ mech_list = NULL;
+ mech_info = NULL;
+
if(mech_count > 0)
{
mech_list = calloc(mech_count + 5, sizeof(CK_MECHANISM_TYPE));
@@ -377,9 +378,9 @@ test_slot_mechanisms(void)
p11t_check_nflag("CK_MECHANISM_INFO.flags", mech_info[i].flags, CKF_EXTENSION);
}
- slot_mech_info[i] = mech_info;
}
+ slot_mech_info[i] = mech_info;
slot_mech_type[i] = mech_list;
slot_mech_count[i] = mech_count;
}
@@ -424,7 +425,7 @@ get_slot_index(CK_SLOT_ID slot)
CK_SLOT_ID
p11t_slot_get_id(int index)
{
- if(index >= p11t_slot_count)
+ if(index >= (int)p11t_slot_count)
return CK_INVALID;
return slot_ids[index];
}
diff --git a/src/test-data.c b/src/test-data.c
index 6d94e74..4a05230 100644
--- a/src/test-data.c
+++ b/src/test-data.c
@@ -1,6 +1,4 @@
-#include "config.h"
-
#include "p11-tests.h"
const CK_BYTE p11t_test_data[] =
@@ -65,6 +63,6 @@ const CK_BYTE p11t_test_data[] =
"0123456789ABCD E" \
"0123456789ABCD F";
-const CK_ULONG p11t_test_data_size = 1024;
-const CK_ULONG p11t_test_data_bits = 1024 * 8;
+const CK_ULONG p11t_test_data_size = P11T_BLOCK;
+const CK_ULONG p11t_test_data_bits = P11T_BLOCK * 8;