// TestPK11.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "cryptoki.h"
#include "stdio.h"
#include "windows.h"
#include <time.h>
void ShowError(CK_RV rv);
void ShowMsg(char *str);
void Convert(char *str, BYTE* pb, int len);
void GenerateKeyPair(CK_FUNCTION_LIST_PTR pFunctionList, CK_SESSION_HANDLE hSession);
int main(int argc, char* argv[])
{
char str[5000];
clock_t start, finish;
double duration;
int t, x;
HANDLE hFile;
DWORD len;
for(int i=0;i<300;i++)
{
printf("*****************:%d\n",i);
// Pointer to the Function List of the PKCS#11 DLL
CK_FUNCTION_LIST_PTR pFunctionList = NULL_PTR;
// Pointer to the Function C_GetFunctionList of the PKCS#11 DLL
CK_C_GetFunctionList pC_GetFunctionList = NULL_PTR;
// Load the PKCS#11 DLL from the specified path
HINSTANCE hInstDLL = LoadLibrary("c:\\winnt\\system32\\gfaicpkcs11.dll");
//HINSTANCE hInstDLL = LoadLibrary("AETPKSS1.dll");
ShowMsg("LoadLibrary AETPKSS1.dll\n");
if ( (pC_GetFunctionList = (CK_C_GetFunctionList) GetProcAddress(hInstDLL, "C_GetFunctionList")) == NULL_PTR)
{
return 0;
}
// Return Value
CK_RV rv;
// Call C_GetFunctionList() from the loaded DLL and get the Pointer to the Function List
rv = (pC_GetFunctionList)(&pFunctionList);
if (rv != CKR_OK) ShowError(rv);
ShowMsg("GetFunctionList\n");
// Init the PKCS#11 Library
rv = pFunctionList->C_Initialize(NULL_PTR);
if (rv != CKR_OK) ShowError(rv);
ShowMsg("Initialize PKCS11 Library\n");
rv = pFunctionList->C_Finalize(NULL_PTR);
if (rv != CKR_OK) ShowError(rv);
ShowMsg("Library Finalized\n");
}
// // Get the number of Slots configured in the system which contain a token
// CK_ULONG Slots=0;
// rv = pFunctionList->C_GetSlotList(TRUE, NULL_PTR, &Slots);
// if (rv != CKR_OK) ShowError(rv);
//
// // Get the Slot ID's which contain a token
// CK_SLOT_ID_PTR ulSlotID = new CK_SLOT_ID[Slots];
// rv = pFunctionList->C_GetSlotList(TRUE, ulSlotID, &Slots);
// if (rv != CKR_OK) ShowError(rv);
///*
// // Get Info
// CK_INFO stInfo;
// rv = pFunctionList->C_GetInfo(&stInfo);
// if (rv != CKR_OK) ShowError(rv);
//
// // Get Slot Info from the 1st slot
// CK_SLOT_INFO stSlotInfo;
// rv = pFunctionList->C_GetSlotInfo(ulSlotID[0], &stSlotInfo);
// if (rv != CKR_OK) ShowError(rv);
//
// ShowMsg("SlotInfo:");
//// ShowMsg(stSlotInfo.slotDescription);
// ShowMsg("\n");
//
// // Get Token Info from the token in the first slot
// CK_TOKEN_INFO stTokenInfo;
// rv = pFunctionList->C_GetTokenInfo(ulSlotID[0], &stTokenInfo);
// if (rv != CKR_OK) ShowError(rv);
//*/
// // Open a R/W Session with token in the first slot
// CK_SESSION_HANDLE hSession1; // Session Handle
// CK_FLAGS flags = CKF_SERIAL_SESSION + CKF_RW_SESSION; // R/W SESSION Flags
// rv = pFunctionList->C_OpenSession(ulSlotID[0], flags, NULL_PTR, NULL_PTR, &hSession1);
// if (rv != CKR_OK) ShowError(rv);
// ShowMsg("Open Session...\n");
//
///* // Get Session Info
// CK_SESSION_INFO stSessionInfo;
// rv = pFunctionList->C_GetSessionInfo(hSession1, &stSessionInfo);
// if (rv != CKR_OK) ShowError(rv);
//*/
// // Login the USER
// ShowMsg("Login the user... ");
// start = clock();
// CK_CHAR pPin[] = {"1111"}; // PIN
// int iPinLen = sizeof(pPin)-1;
// rv = pFunctionList->C_Login(hSession1, CKU_USER, pPin, iPinLen);
// if (rv != CKR_OK) ShowError(rv);
// finish = clock();
// duration = (double)(finish - start);
// sprintf(str, "Verify PIN Successful. Time:%fms\n", duration);
// ShowMsg(str);
//
// //GenerateKeyPair(pFunctionList, hSession1);
//
//
// // Search for Certificate
// ShowMsg("Searching Certificate...\n");
// CK_OBJECT_HANDLE hCertificate[10]; // Key Handels
// CK_ULONG ulObjectCount;
// CK_OBJECT_CLASS CertificateClass = CKO_CERTIFICATE; // CKA_CLASS = Private Key
// CK_ATTRIBUTE searchTemplate1[] =
// {
// {CKA_CLASS, &CertificateClass, sizeof(CertificateClass)},
// };
//
// CK_ATTRIBUTE template1[] = {
// {CKA_LABEL, NULL, 0},
// {CKA_SUBJECT, NULL, 0},
// {CKA_ID, NULL, 0},
// {CKA_VALUE, NULL, 0}
// };
//
// // Initialize Object search operation
// rv = pFunctionList->C_FindObjectsInit(hSession1,
// searchTemplate1,
// 1);
// if (rv != CKR_OK) ShowError(rv);
//
// int z = 0;
// while(1)
// {
// start = clock();
// rv = pFunctionList->C_FindObjects(hSession1,
// &hCertificate[z],
// 1,
// &ulObjectCount);
// finish = clock();
// duration = (double)(finish - start);
//
// if (rv != CKR_OK) ShowError(rv);
//
// if (ulObjectCount == 0) break;
//
// z++;
// sprintf(str,"Find Certificate %d Time:%fms\n", z, duration);
// ShowMsg(str);
//
// }
//
// for(x = 0; x < z; x++)
// {
// start = clock();
// rv = pFunctionList->C_GetAttributeValue(hSession1,
// hCertificate[x],
// template1,
// 4);
//
// for(t = 0; t < 4; t++)
// template1[t].pValue = new BYTE[template1[t].ulValueLen];
//
// rv = pFunctionList->C_GetAttributeValue(hSession1,
// hCertificate[x],
// template1,
// 4);
//
// finish = clock();
// duration = (double)(finish - start);
//
// sprintf(str, "Read Certificate %d Time:%fms\n",x+1, duration);
// ShowMsg(str);
//
// sprintf(str, "C%d.cer", x+1);
// hFile = CreateFile(str,
// GENERIC_WRITE,
// 0,
// NULL,
// CREATE_ALWAYS,
// 0,
// NULL);
//
// WriteFile(hFile, template1[3].pValue, template1[3].ulValueLen, &len, NULL);
// CloseHandle(hFile);
//
// sprintf(str, "Certificate %d was saved in C%d.cer\n", x+1, x+1);
// ShowMsg(str);
//
// for(t = 0; t < 4; t++)
// {
// delete template1[t].pValue;
// template1[t].pValue = NULL;
// }
// }
//
// // terminate the search operation
// rv = pFunctionList->C_FindObjectsFinal(hSession1);
// if (rv != CKR_OK) ShowError(rv);
//
//
// // Search for a private signing Key
// ShowMsg("Searching Private Signing Key...\n");
// CK_OBJECT_HANDLE hPrivateKey[10]; // Key Handels
// CK_OBJECT_CLASS PrivateKeyClass = CKO_PRIVATE_KEY; // CKA_CLASS = Private Key
// CK_BBOOL bSign = TRUE; // CKA_SIGN = TRUE
// CK_ATTRIBUTE searchTemplate2[] =
// {
// {CKA_CLASS, &PrivateKeyClass, sizeof(PrivateKeyClass)},
// {CKA_SIGN, &bSign, sizeof(bSign)}
// };
//
// // Initialize Object search operation
// rv = pFunctionList->C_FindObjectsInit(hSession1,
// searchTemplate2,
// 1);
// if (rv != CKR_OK) ShowError(rv);
//
// z = 0;
// while(1)
// {
// // search all private signing keys
// start = clock();
// rv = pFunctionList->C_FindObjects(hSession1,
// &hPrivateKey[z],
// 1,
// &ulObjectCount);
// finish = clock();
// duration = (double)(finish - start);
//
// if (rv != CKR_OK) ShowError(rv);
//
// if (ulObjectCount == 0) break;
//
// z++;
//
// sprintf(str,"Find Priave Signing Key %d Time:%fms\n", z, duration);
// ShowMsg(str);
// }
//
// // terminate the search operation
// rv = pFunctionList->C_FindObjectsFinal(hSession1);
// if (rv != CKR_OK) ShowError(rv);
//
//
// CK_ATTRIBUTE template2[] = {
// {CKA_CLASS, NULL, 0},
// {CKA_TOKEN, NULL, 0},
// {CKA_LABEL, NULL, 0},
// {CKA_SUBJECT, NULL, 0},
//