Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
62808c7
feat add windows support
agrandville Jun 29, 2021
8b52bc6
fix cmake minimal version
agrandville Jun 29, 2021
37b6ada
fix build set version
agrandville Jun 29, 2021
0bfde54
fix disable appveyor remote desktop
agrandville Jun 29, 2021
6306bc6
fix static cast size_t to CK_ULONG
agrandville Jun 29, 2021
7dc0df0
feat add Win64 compilation support
agrandville Jun 29, 2021
e6db92d
fix appveyor error 'version already exists'
agrandville Jun 29, 2021
9a83334
fix windows CMake generator name
agrandville Jun 29, 2021
223a83b
fix downgrade to Visual Studio 14 2015
agrandville Jun 29, 2021
e0f7c14
doc add windows compilation notes
agrandville Jun 29, 2021
ed359d6
feat publish github release
agrandville Jun 30, 2021
1e143db
fix change artifact zip path
agrandville Jun 30, 2021
762c472
fix change artifact zip path
agrandville Jun 30, 2021
c229b27
Update .appveyor.yml
agrandville Jun 30, 2021
8ad94de
Update CMakeLists.txt
agrandville Jun 30, 2021
ae4eb73
Update .appveyor.yml
agrandville Jun 30, 2021
292cb09
Update .appveyor.yml
agrandville Jun 30, 2021
172a6dc
Update .appveyor.yml
agrandville Jun 30, 2021
83838a4
feat release appVeyor builds
agrandville Jun 30, 2021
c75abcb
feat disable appveyor RDP on finish
agrandville Jun 30, 2021
8322301
fix PR regression
agrandville Jun 30, 2021
92fa357
fix merge win32/cryptoki.h with pkcs11-env.h
agrandville Jun 30, 2021
38dd4a7
fix remove getopt source
agrandville Jul 1, 2021
6644bdc
Import pristine copy of NetBSD getopt code
agrandville Jul 1, 2021
524e6aa
fix windows portability
agrandville Jul 1, 2021
2decd75
fix update getopt build
agrandville Jul 1, 2021
f5a1536
fix remove unnecessary include
agrandville Jul 1, 2021
71f3f71
fix remove unnecessary GTest libraries
agrandville Jul 1, 2021
0c8badc
fix remove appveyor RDP on finish
agrandville Jul 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: 0.0.0-{build}
configuration: Release
platform:
- x86
#- x64
cache:
- C:/Tools/vcpkg/installed/
environment:
APPVEYOR_SAVE_CACHE_ON_ERROR: true
matrix:
- GENERATOR: "Visual Studio 14 2015"
TARGET: x86-windows
- GENERATOR: "Visual Studio 14 2015 Win64"
TARGET: x64-windows
install:
# Update vcpkg
- cd c:\tools\vcpkg
- cmd: git fetch
- cmd: git checkout 2021.05.12
- cmd: bootstrap-vcpkg.bat
- cmd: vcpkg install gtest:x86-windows
- cmd: vcpkg install gtest:x64-windows
- cmd: vcpkg update
- cmd: vcpkg upgrade --no-dry-run
build_script:
- cmd: if exist "C:\projects\pkcs11test\build" rd /s /q C:\projects\pkcs11test\build
- cmd: mkdir C:\projects\pkcs11test\build
- cmd: cd C:\projects\pkcs11test\build
- cmd: vcpkg integrate install
- cmd: cmake .. -G "%GENERATOR%" -DVCPKG_TARGET_TRIPLET=%TARGET% -DCMAKE_TOOLCHAIN_FILE=C:/Tools/vcpkg/scripts/buildsystems/vcpkg.cmake
- cmd: cmake --build . --config RelWithDebInfo
- cmd: ctest -C RelWithDebInfo --progress --verbose
- cmd: cpack
artifacts:
- path: "build/*.zip"
name: artifacts-zip

deploy:
release: pkcs11test-v$(APPVEYOR_BUILD_VERSION)
description: 'Windows binaries auto-built by AppVeyor'
provider: GitHub
auth_token:
secure: KX9JW8h86LzNA1TDJy2adlFIV+E455sZqXTc/HQDPLSbHRTwEv5zpcdRA/n9xJGp
artifact: artifacts-zip
draft: true
prerelease: false
on:
branch: dev
# APPVEYOR_REPO_TAG: true # deploy on tag push only
70 changes: 70 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(p11test)

set(VERSION "0.0.0")
set(VERSION_MAJOR 0)
set(VERSION_MINOR 0)
set(VERSION_PATCH 0)

set(SOURCES cipher.cc
describe.cc
digest.cc
dual.cc
globals.cc
hmac.cc
init.cc
key.cc
keypair.cc
login.cc
object.cc
pkcs11-describe.cc
pkcs11test.cc
rng.cc
session.cc
sign.cc
slot.cc
tookan.cc
)

if(MSVC)
list(APPEND INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/third_party/netbsd)

list(APPEND SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/third_party/netbsd/getopt_long.c)

endif()

list(APPEND INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/third_party/pkcs11)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSTRICT_P11")
find_package(GTest CONFIG REQUIRED COMPONENTS CXX_LIB)
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE GTest::gtest)
target_include_directories(${PROJECT_NAME} PUBLIC ${INCLUDE_DIRS} )

if(${VCPKG_TARGET_TRIPLET} MATCHES ".*windows-static$")
message(FATAL_ERROR "GTest library doesn't support static linking for now")
# switch to MultiThreaded runtime library (MT) vs MultiThreaded DLL (MD)
set_property(TARGET ${PROJECT_NAME} PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()


install(FILES ${CMAKE_CURRENT_BINARY_DIR}/RelWithDebInfo/p11test.exe DESTINATION bin)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/RelWithDebInfo/p11test.pdb DESTINATION bin OPTIONAL)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/RelWithDebInfo/gtest.dll DESTINATION bin)

# Packaging
set(CPACK_PACKAGE_NAME ${PACKAGE_NAME})
set(CPACK_PACKAGE_VENDOR "plcs11test")
set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH})
set(CPACK_GENERATOR "ZIP")
set(CPACK_SOURCE_GENERATOR "ZIP")
set(CPACK_SOURCE_IGNORE_FILES "build/*;\.git/*")

include(CPack)
38 changes: 38 additions & 0 deletions NOTES.WIN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Building pkcs11test for Windows

This document describes process of building both 32-bit and 64-bit versions of pkcs11test on 64-bit Windows machine.

## Required software

- [Visual Studio](https://visualstudio.microsoft.com/vs/community/) (Community)
- [C/C++ dependency manager from Microsoft](https://vcpkg.io/)
- [CMake](https://cmake.org/)

## Prepare working directories

set P11TEST_HOME=C:\Projects\pkcs11test
set VCPKG_HOME=C:\Projects\vcpkg

## Build GTest

cd %VCPKG_HOME%
bootstrap-vcpkg.bat
vcpkg install gtest:x86-windows
vcpkg install gtest:x64-windows
vcpkg integrate install

## Build pkcs11test
git clone https://github.com/google/pkcs11test.git %P11TEST_HOME%
## x86
mkdir %P11TEST_HOME%\tmp
cd %P11TEST_HOME%\tmp
cmake .. -A Win32 -DCMAKE_TOOLCHAIN_FILE=%VCPKG_HOME%/scripts/buildsystems/vcpkg.cmake
cmake --build . --config RelWithDebInfo
cmake -DCMAKE_INSTALL_PREFIX=%P11TEST_HOME%\out32 -DCMAKE_INSTALL_CONFIG_NAME=RelWithDebInfo -P cmake_install.cmake

## x64
mkdir %P11TEST_HOME%\tmp
cd %P11TEST_HOME%\tmp
cmake .. -A x64 -DVCPKG_TARGET_TRIPLET=x64-windows -DCMAKE_TOOLCHAIN_FILE=%VCPKG_HOME%/scripts/buildsystems/vcpkg.cmake
cmake --build . --config RelWithDebInfo
cmake -DCMAKE_INSTALL_PREFIX=%P11TEST_HOME%\out64 -DCMAKE_INSTALL_CONFIG_NAME=RelWithDebInfo -P cmake_install.cmake
4 changes: 2 additions & 2 deletions cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ TEST_F(ReadOnlySessionTest, CreateSecretKeyAttributes) {
{CKA_DECRYPT, (CK_VOID_PTR)&g_ck_true, sizeof(CK_BBOOL)},
{CKA_CLASS, &key_class, sizeof(key_class)},
{CKA_KEY_TYPE, (CK_VOID_PTR)&key_type, sizeof(key_type)},
{CKA_VALUE, (CK_VOID_PTR)key.data(), key.size()},
{CKA_VALUE, (CK_VOID_PTR)key.data(), static_cast<CK_ULONG>(key.size())},
};
CK_OBJECT_HANDLE key_object;
ASSERT_CKR_OK(g_fns->C_CreateObject(session_, attrs.data(), attrs.size(), &key_object));
Expand Down Expand Up @@ -626,7 +626,7 @@ TEST_F(ReadOnlySessionTest, SecretKeyTestVectors) {
{CKA_DECRYPT, (CK_VOID_PTR)&g_ck_true, sizeof(CK_BBOOL)},
{CKA_CLASS, &key_class, sizeof(key_class)},
{CKA_KEY_TYPE, (CK_VOID_PTR)&key_type, sizeof(key_type)},
{CKA_VALUE, (CK_VOID_PTR)key.data(), key.size()},
{CKA_VALUE, (CK_VOID_PTR)key.data(), static_cast<CK_ULONG>(key.size())},
};
CK_OBJECT_HANDLE key_object;
ASSERT_CKR_OK(g_fns->C_CreateObject(session_, attrs.data(), attrs.size(), &key_object));
Expand Down
6 changes: 6 additions & 0 deletions digest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
#include <cstdlib>
#include "pkcs11test.h"


#ifdef _WIN32
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif


#include <map>
#include <sstream>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion hmac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ TEST_F(ReadOnlySessionTest, HmacTestVectors) {
{CKA_VERIFY, (CK_VOID_PTR)&g_ck_true, sizeof(CK_BBOOL)},
{CKA_CLASS, &key_class, sizeof(key_class)},
{CKA_KEY_TYPE, (CK_VOID_PTR)&key_type, sizeof(key_type)},
{CKA_VALUE, (CK_VOID_PTR)key.data(), key.size()},
{CKA_VALUE, (CK_VOID_PTR)key.data(), static_cast<CK_ULONG>(key.size())},
};
CK_OBJECT_HANDLE key_object;
ASSERT_CKR_OK(g_fns->C_CreateObject(session_, attrs.data(), attrs.size(), &key_object));
Expand Down
20 changes: 10 additions & 10 deletions keypair.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ TEST_F(ReadOnlySessionTest, CreateKeyPairObjects) {
{CKA_PRIVATE, (CK_VOID_PTR)&g_ck_false, sizeof(CK_BBOOL)},
{CKA_CLASS, &public_key_class, sizeof(public_key_class)},
{CKA_KEY_TYPE, (CK_VOID_PTR)&key_type, sizeof(key_type)},
{CKA_PUBLIC_EXPONENT, (CK_VOID_PTR)public_exponent.data(), public_exponent.size()},
{CKA_MODULUS, (CK_VOID_PTR)public_modulus.data(), public_modulus.size()},
{CKA_PUBLIC_EXPONENT, (CK_VOID_PTR)public_exponent.data(), static_cast<CK_ULONG>(public_exponent.size())},
{CKA_MODULUS, (CK_VOID_PTR)public_modulus.data(), static_cast<CK_ULONG>(public_modulus.size())},
};
EXPECT_CKR_OK(g_fns->C_CreateObject(session_,
public_attrs.data(),
Expand All @@ -254,34 +254,34 @@ TEST_F(ReadOnlySessionTest, CreateKeyPairObjects) {
{CKA_PRIVATE, (CK_VOID_PTR)&g_ck_false, sizeof(CK_BBOOL)},
{CKA_CLASS, &private_key_class, sizeof(private_key_class)},
{CKA_KEY_TYPE, (CK_VOID_PTR)&key_type, sizeof(key_type)},
{CKA_PUBLIC_EXPONENT, (CK_VOID_PTR)public_exponent.data(), public_exponent.size()},
{CKA_PRIVATE_EXPONENT, (CK_BYTE_PTR)private_exponent.data(), private_exponent.size()},
{CKA_MODULUS, (CK_VOID_PTR)public_modulus.data(), public_modulus.size()},
{CKA_PUBLIC_EXPONENT, (CK_VOID_PTR)public_exponent.data(), static_cast<CK_ULONG>(public_exponent.size())},
{CKA_PRIVATE_EXPONENT, (CK_BYTE_PTR)private_exponent.data(), static_cast<CK_ULONG>(private_exponent.size())},
{CKA_MODULUS, (CK_VOID_PTR)public_modulus.data(), static_cast<CK_ULONG>(public_modulus.size())},
};
string prime1data;
if (!keydata.prime1.empty()) {
prime1data = hex_decode(keydata.prime1);
private_attrs.push_back({CKA_PRIME_1, (CK_BYTE_PTR)prime1data.data(), prime1data.size()});
private_attrs.push_back({CKA_PRIME_1, (CK_BYTE_PTR)prime1data.data(), static_cast<CK_ULONG>(prime1data.size())});
}
string prime2data;
if (!keydata.prime2.empty()) {
prime2data = hex_decode(keydata.prime2);
private_attrs.push_back({CKA_PRIME_2, (CK_BYTE_PTR)prime2data.data(), prime2data.size()});
private_attrs.push_back({CKA_PRIME_2, (CK_BYTE_PTR)prime2data.data(), static_cast<CK_ULONG>(prime2data.size())});
}
string exponent1data;
if (!keydata.exponent1.empty()) {
exponent1data = hex_decode(keydata.exponent1);
private_attrs.push_back({CKA_EXPONENT_1, (CK_BYTE_PTR)exponent1data.data(), exponent1data.size()});
private_attrs.push_back({CKA_EXPONENT_1, (CK_BYTE_PTR)exponent1data.data(),static_cast<CK_ULONG>(exponent1data.size())});
}
string exponent2data;
if (!keydata.exponent2.empty()) {
exponent2data = hex_decode(keydata.exponent2);
private_attrs.push_back({CKA_EXPONENT_2, (CK_BYTE_PTR)exponent2data.data(), exponent2data.size()});
private_attrs.push_back({CKA_EXPONENT_2, (CK_BYTE_PTR)exponent2data.data(), static_cast<CK_ULONG>(exponent2data.size())});
}
string coefficientdata;
if (!keydata.coefficient.empty()) {
coefficientdata = hex_decode(keydata.coefficient);
private_attrs.push_back({CKA_COEFFICIENT, (CK_BYTE_PTR)coefficientdata.data(), coefficientdata.size()});
private_attrs.push_back({CKA_COEFFICIENT, (CK_BYTE_PTR)coefficientdata.data(), static_cast<CK_ULONG>(coefficientdata.size())});
}
EXPECT_CKR_OK(g_fns->C_CreateObject(session_,
private_attrs.data(),
Expand Down
40 changes: 40 additions & 0 deletions pkcs11-env.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,52 @@
# pragma pack(push, 1)
#endif



#ifdef _WIN32

/* Specifies that the function is a DLL entry point. */
#define CK_IMPORT_SPEC __declspec(dllimport)

#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)



#else //_WIN32

/* UNIX version */
/* The following definitions need to be provided to the preprocessor before the PKCS#11 header file can be included */
#define CK_PTR *
#define CK_DEFINE_FUNCTION(returnType, name) returnType name
#define CK_DECLARE_FUNCTION(returnType, name) returnType name
#define CK_DECLARE_FUNCTION_POINTER(returnType, name) returnType (*name)
#define CK_CALLBACK_FUNCTION(returnType, name) returnType (* name)


#endif //_WIN32

#ifndef NULL_PTR
#define NULL_PTR 0
#endif
Expand Down
29 changes: 25 additions & 4 deletions pkcs11test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@
// limitations under the License.

// C headers
#include <getopt.h>
#ifdef _WIN32
constexpr auto FILEPATH_NATIVE = '\\';
#include <windows.h>
#include <stdlib.h>
#else
constexpr auto FILEPATH_NATIVE = '/';
#include <dlfcn.h>
#include <unistd.h>
#endif

// C++ headers
#include <cctype>
Expand Down Expand Up @@ -111,8 +119,8 @@ CK_C_GetFunctionList load_pkcs11_library(const char* libpath, const char* libnam
string fullname;
if (libpath != nullptr) {
fullname = libpath;
if (fullname.at(fullname.size() - 1) != '/') {
fullname += '/';
if (fullname.at(fullname.size() - 1) != FILEPATH_NATIVE) {
fullname += FILEPATH_NATIVE;
}
}
fullname += libname;
Expand All @@ -121,13 +129,24 @@ CK_C_GetFunctionList load_pkcs11_library(const char* libpath, const char* libnam
exit(1);
}

void* lib = dlopen(fullname.c_str(), RTLD_NOW);
void* lib;
#if defined WIN32
lib = LoadLibraryA(fullname.c_str());
#else
lib = dlopen(fullname.c_str(), RTLD_NOW);
#endif
if (lib == nullptr) {
cerr << "Failed to dlopen(" << fullname << ")" << endl;
exit(1);
}

void* fn = dlsym(lib, "C_GetFunctionList");
void* fn;
#if defined WIN32
fn = GetProcAddress((HMODULE)lib, "C_GetFunctionList");
#else
fn = dlsym(lib, "C_GetFunctionList");
#endif

if (fn == nullptr) {
cerr<< "Failed to dlsym(\"C_GetFunctionList\")" << endl;
exit(1);
Expand Down Expand Up @@ -196,6 +215,7 @@ int main(int argc, char* argv[]) {
// Load the module.
CK_C_GetFunctionList get_fn_list = load_pkcs11_library(module_path, module_name);


// Retrieve the set of function pointers (C_GetFunctionList is the only function it's OK to call before C_Initialize).
if (get_fn_list(&g_fns) != CKR_OK) {
cerr << "Failed to retrieve list of functions" << endl;
Expand All @@ -212,6 +232,7 @@ int main(int argc, char* argv[]) {
if (use_slot_index || !explicit_slotid) {
CK_SLOT_ID slots[32];
CK_ULONG slot_count = 32;

rv = g_fns->C_GetSlotList(CK_TRUE, slots, &slot_count);
if (rv == CKR_OK) {
if (slot_count == 0) {
Expand Down
Loading