summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStef Walter <stef@memberwebs.com>2008-12-06 00:22:35 +0000
committerStef Walter <stef@memberwebs.com>2008-12-06 00:22:35 +0000
commitd3fa2085bf9169eab3430ff903f75b7487c7ee82 (patch)
treecf0f05047b689dc5e48a2dcec955c268b95eaee8
parent746579baf9176eb13dc19e98627e27e900c9859d (diff)
Build on Win32 and create VCE project.
-rw-r--r--compat.c231
-rw-r--r--compat.h19
-rw-r--r--p11-tests.sln20
-rw-r--r--p11-tests.vcproj254
-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
16 files changed, 777 insertions, 85 deletions
diff --git a/compat.c b/compat.c
new file mode 100644
index 0000000..13794b4
--- /dev/null
+++ b/compat.c
@@ -0,0 +1,231 @@
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. 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.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * nor may "Apache" appear in their name, without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR
+ * ITS 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ * Portions of this software are based upon public domain software
+ * originally written at the National Center for Supercomputing Applications,
+ * University of Illinois, Urbana-Champaign.
+ */
+
+#ifdef _WIN32
+
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <stdlib.h>
+
+#define OPTERRCOLON (1)
+#define OPTERRNF (2)
+#define OPTERRARG (3)
+
+char *optarg;
+int optreset = 0;
+int optind = 1;
+int opterr = 1;
+int optopt;
+
+static int
+optiserr(int argc, char * const *argv, int oint, const char *optstr,
+ int optchr, int err)
+{
+ if(opterr)
+ {
+ fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
+ switch(err)
+ {
+ case OPTERRCOLON:
+ fprintf(stderr, ": in flags\n");
+ break;
+ case OPTERRNF:
+ fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
+ break;
+ case OPTERRARG:
+ fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
+ break;
+ default:
+ fprintf(stderr, "unknown\n");
+ break;
+ }
+ }
+ optopt = argv[oint][optchr];
+ return('?');
+}
+
+
+
+int
+getopt(int argc, char* const *argv, const char *optstr)
+{
+ static int optchr = 0;
+ static int dash = 0; /* have already seen the - */
+
+ char *cp;
+
+ if (optreset)
+ optreset = optchr = dash = 0;
+ if(optind >= argc)
+ return(EOF);
+ if(!dash && (argv[optind][0] != '-'))
+ return(EOF);
+ if(!dash && (argv[optind][0] == '-') && !argv[optind][1])
+ {
+ /*
+ * use to specify stdin. Need to let pgm process this and
+ * the following args
+ */
+ return(EOF);
+ }
+ if((argv[optind][0] == '-') && (argv[optind][1] == '-'))
+ {
+ /* -- indicates end of args */
+ optind++;
+ return(EOF);
+ }
+ if(!dash)
+ {
+ assert((argv[optind][0] == '-') && argv[optind][1]);
+ dash = 1;
+ optchr = 1;
+ }
+
+ /* Check if the guy tries to do a -: kind of flag */
+ assert(dash);
+ if(argv[optind][optchr] == ':')
+ {
+ dash = 0;
+ optind++;
+ return(optiserr(argc, argv, optind-1, optstr, optchr, OPTERRCOLON));
+ }
+ if(!(cp = strchr(optstr, argv[optind][optchr])))
+ {
+ int errind = optind;
+ int errchr = optchr;
+
+ if(!argv[optind][optchr+1])
+ {
+ dash = 0;
+ optind++;
+ }
+ else
+ optchr++;
+ return(optiserr(argc, argv, errind, optstr, errchr, OPTERRNF));
+ }
+ if(cp[1] == ':')
+ {
+ dash = 0;
+ optind++;
+ if(optind == argc)
+ return(optiserr(argc, argv, optind-1, optstr, optchr, OPTERRARG));
+ optarg = argv[optind++];
+ return(*cp);
+ }
+ else
+ {
+ if(!argv[optind][optchr+1])
+ {
+ dash = 0;
+ optind++;
+ }
+ else
+ optchr++;
+ return(*cp);
+ }
+ assert(0);
+ return(0);
+}
+
+#ifdef TESTGETOPT
+int
+ main (int argc, char **argv)
+ {
+ int c;
+ extern char *optarg;
+ extern int optind;
+ int aflg = 0;
+ int bflg = 0;
+ int errflg = 0;
+ char *ofile = NULL;
+
+ while ((c = getopt(argc, argv, "abo:")) != EOF)
+ switch (c) {
+ case 'a':
+ if (bflg)
+ errflg++;
+ else
+ aflg++;
+ break;
+ case 'b':
+ if (aflg)
+ errflg++;
+ else
+ bflg++;
+ break;
+ case 'o':
+ ofile = optarg;
+ (void)printf("ofile = %s\n", ofile);
+ break;
+ case '?':
+ errflg++;
+ }
+ if (errflg) {
+ (void)fprintf(stderr,
+ "usage: cmd [-a|-b] [-o <filename>] files...\n");
+ exit (2);
+ }
+ for ( ; optind < argc; optind++)
+ (void)printf("%s\n", argv[optind]);
+ return 0;
+ }
+
+#endif /* TESTGETOPT */
+
+#endif /* WIN32 */
diff --git a/compat.h b/compat.h
new file mode 100644
index 0000000..02ad831
--- /dev/null
+++ b/compat.h
@@ -0,0 +1,19 @@
+
+#ifndef _COMPAT_H_
+#define _COMPAT_H_
+
+#ifdef _WIN32
+
+int getopt(int argc, char* const *argv, const char *optstr);
+extern char *optarg;
+extern int optreset;
+extern int optind;
+extern int opterr;
+
+#else
+
+#include <unistd.h>
+
+#endif
+
+#endif /* _COMPAT_H_ */
diff --git a/p11-tests.sln b/p11-tests.sln
new file mode 100644
index 0000000..286b709
--- /dev/null
+++ b/p11-tests.sln
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 9.00
+# Visual C++ Express 2005
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "p11-tests", "p11-tests.vcproj", "{9F45CE89-9B52-442A-82ED-929391ECF072}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Win32 = Debug|Win32
+ Release|Win32 = Release|Win32
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {9F45CE89-9B52-442A-82ED-929391ECF072}.Debug|Win32.ActiveCfg = Debug|Win32
+ {9F45CE89-9B52-442A-82ED-929391ECF072}.Debug|Win32.Build.0 = Debug|Win32
+ {9F45CE89-9B52-442A-82ED-929391ECF072}.Release|Win32.ActiveCfg = Release|Win32
+ {9F45CE89-9B52-442A-82ED-929391ECF072}.Release|Win32.Build.0 = Release|Win32
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
diff --git a/p11-tests.vcproj b/p11-tests.vcproj
new file mode 100644
index 0000000..de29a8e
--- /dev/null
+++ b/p11-tests.vcproj
@@ -0,0 +1,254 @@
+<?xml version="1.0" encoding="Windows-1252"?>
+<VisualStudioProject
+ ProjectType="Visual C++"
+ Version="8.00"
+ Name="p11-tests"
+ ProjectGUID="{9F45CE89-9B52-442A-82ED-929391ECF072}"
+ RootNamespace="p11-tests"
+ Keyword="Win32Proj"
+ >
+ <Platforms>
+ <Platform
+ Name="Win32"
+ />
+ </Platforms>
+ <ToolFiles>
+ </ToolFiles>
+ <Configurations>
+ <Configuration
+ Name="Debug|Win32"
+ OutputDirectory="Debug"
+ IntermediateDirectory="Debug"
+ ConfigurationType="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="$(ProjectDir)"
+ PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="3"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ AdditionalDependencies="libeay32MTd.lib"
+ LinkIncremental="2"
+ GenerateDebugInformation="true"
+ SubSystem="1"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Release|Win32"
+ OutputDirectory="Release"
+ IntermediateDirectory="Release"
+ ConfigurationType="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ AdditionalIncludeDirectories="$(ProjectDir)"
+ PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;"
+ RuntimeLibrary="2"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="3"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ AdditionalDependencies="libeay32MT.lib"
+ LinkIncremental="2"
+ GenerateDebugInformation="true"
+ SubSystem="1"
+ OptimizeReferences="2"
+ EnableCOMDATFolding="2"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ </Configurations>
+ <References>
+ </References>
+ <Files>
+ <Filter
+ Name="Header Files"
+ Filter="h;hpp;hxx;hm;inl;inc;xsd"
+ UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
+ >
+ <File
+ RelativePath=".\src\p11-tests.h"
+ >
+ </File>
+ <File
+ RelativePath=".\pkcs11\pkcs11.h"
+ >
+ </File>
+ </Filter>
+ <Filter
+ Name="Resource Files"
+ Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx"
+ UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
+ >
+ </Filter>
+ <Filter
+ Name="Source Files"
+ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+ UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
+ >
+ <File
+ RelativePath=".\src\check.c"
+ >
+ </File>
+ <File
+ RelativePath=".\compat.c"
+ >
+ </File>
+ <File
+ RelativePath=".\compat.h"
+ >
+ </File>
+ <File
+ RelativePath=".\src\config.c"
+ >
+ </File>
+ <File
+ RelativePath=".\src\key.c"
+ >
+ </File>
+ <File
+ RelativePath=".\src\module.c"
+ >
+ </File>
+ <File
+ RelativePath=".\src\msg.c"
+ >
+ </File>
+ <File
+ RelativePath=".\src\object.c"
+ >
+ </File>
+ <File
+ RelativePath=".\src\p11-tests.c"
+ >
+ </File>
+ <File
+ RelativePath=".\src\rsa.c"
+ >
+ </File>
+ <File
+ RelativePath=".\src\session.c"
+ >
+ </File>
+ <File
+ RelativePath=".\src\slot.c"
+ >
+ </File>
+ <File
+ RelativePath=".\src\test-data.c"
+ >
+ </File>
+ </Filter>
+ </Files>
+ <Globals>
+ </Globals>
+</VisualStudioProject>
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;