Skip to content

Instantly share code, notes, and snippets.

@markscottwright
Created October 31, 2017 13:46
Show Gist options
  • Save markscottwright/6ca24eacddc89eb119b4613500789b28 to your computer and use it in GitHub Desktop.
Save markscottwright/6ca24eacddc89eb119b4613500789b28 to your computer and use it in GitHub Desktop.
PKCS11 skeleton
/*
* This is a skeleton for a pkcs11 application that takes care of loading the named pkcs11 library and getting
* the list of pkcs11 functions.
*/
#include <iostream>
#include <iomanip>
#include <windows.h>
// get this from https://www.cryptsoft.com/pkcs11doc/STANDARD/include/v220
// also get pkcs11t.h, pkcs11f.h, pkcs11.h
#include "cryptoki.h"
using namespace std;
typedef CK_RV (*C_GetFunctionList_t)(CK_FUNCTION_LIST_PTR_PTR);
#define IS_OK if (rv != CKR_OK) {cout << __LINE__ << ":rv = " << std::hex << rv << endl; return 1;}
int main() {
HMODULE lib = LoadLibrary("pkcs11-dll-name");
C_GetFunctionList_t C_GetFunctionList = (C_GetFunctionList_t) GetProcAddress(lib, "C_GetFunctionList");
CK_FUNCTION_LIST_PTR pkcs11;
CK_RV rv = C_GetFunctionList(&pkcs11); IS_OK;
rv = pkcs11->C_Initialize(NULL); IS_OK;
CK_ULONG num_slots = 100;
CK_SLOT_ID slots[100];
rv = pkcs11->C_GetSlotList(CK_TRUE, slots, &num_slots); IS_OK;
for (CK_ULONG i = 0; i < num_slots; ++i) {
cout << "slot: " << slots[i] << endl;
}
rv = pkcs11->C_Finalize(NULL); IS_OK;
FreeLibrary(lib);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment