/// <summary> /// Get delegates with C_GetFunctionList function from the statically linked PKCS#11 library /// </summary> private void InitializeWithGetFunctionList() { IntPtr functionList = IntPtr.Zero; CKR rv = (CKR)Convert.ToUInt32(NativeMethods.C_GetFunctionList(out functionList)); if ((rv != CKR.CKR_OK) || (functionList == IntPtr.Zero)) { throw new Pkcs11Exception("C_GetFunctionList", rv); } CK_FUNCTION_LIST ckFunctionList = (CK_FUNCTION_LIST)UnmanagedMemory.Read(functionList, typeof(CK_FUNCTION_LIST)); Initialize(ckFunctionList); }
/// <summary> /// Get delegates with C_GetFunctionList function from the dynamically loaded shared PKCS#11 library /// </summary> /// <param name="libraryHandle">Handle to the PKCS#11 library</param> private void InitializeWithGetFunctionList(IntPtr libraryHandle) { IntPtr cGetFunctionListPtr = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetFunctionList"); C_GetFunctionListDelegate cGetFunctionList = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetFunctionListDelegate>(cGetFunctionListPtr); IntPtr functionList = IntPtr.Zero; CKR rv = (CKR)Convert.ToUInt32(cGetFunctionList(out functionList)); if ((rv != CKR.CKR_OK) || (functionList == IntPtr.Zero)) { throw new Pkcs11Exception("C_GetFunctionList", rv); } CK_FUNCTION_LIST ckFunctionList = (CK_FUNCTION_LIST)UnmanagedMemory.Read(functionList, typeof(CK_FUNCTION_LIST)); Initialize(ckFunctionList); }
/// <summary> /// Get delegates from unmanaged function pointers /// </summary> /// <param name="ckFunctionList">Structure which contains cryptoki function pointers</param> private void Initialize(CK_FUNCTION_LIST ckFunctionList) { C_Initialize = UnmanagedLibrary.GetDelegateForFunctionPointer <C_InitializeDelegate>(ckFunctionList.C_Initialize); C_Finalize = UnmanagedLibrary.GetDelegateForFunctionPointer <C_FinalizeDelegate>(ckFunctionList.C_Finalize); C_GetInfo = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetInfoDelegate>(ckFunctionList.C_GetInfo); C_GetFunctionList = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetFunctionListDelegate>(ckFunctionList.C_GetFunctionList); C_GetSlotList = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetSlotListDelegate>(ckFunctionList.C_GetSlotList); C_GetSlotInfo = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetSlotInfoDelegate>(ckFunctionList.C_GetSlotInfo); C_GetTokenInfo = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetTokenInfoDelegate>(ckFunctionList.C_GetTokenInfo); C_GetMechanismList = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetMechanismListDelegate>(ckFunctionList.C_GetMechanismList); C_GetMechanismInfo = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetMechanismInfoDelegate>(ckFunctionList.C_GetMechanismInfo); C_InitToken = UnmanagedLibrary.GetDelegateForFunctionPointer <C_InitTokenDelegate>(ckFunctionList.C_InitToken); C_InitPIN = UnmanagedLibrary.GetDelegateForFunctionPointer <C_InitPINDelegate>(ckFunctionList.C_InitPIN); C_SetPIN = UnmanagedLibrary.GetDelegateForFunctionPointer <C_SetPINDelegate>(ckFunctionList.C_SetPIN); C_OpenSession = UnmanagedLibrary.GetDelegateForFunctionPointer <C_OpenSessionDelegate>(ckFunctionList.C_OpenSession); C_CloseSession = UnmanagedLibrary.GetDelegateForFunctionPointer <C_CloseSessionDelegate>(ckFunctionList.C_CloseSession); C_CloseAllSessions = UnmanagedLibrary.GetDelegateForFunctionPointer <C_CloseAllSessionsDelegate>(ckFunctionList.C_CloseAllSessions); C_GetSessionInfo = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetSessionInfoDelegate>(ckFunctionList.C_GetSessionInfo); C_GetOperationState = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetOperationStateDelegate>(ckFunctionList.C_GetOperationState); C_SetOperationState = UnmanagedLibrary.GetDelegateForFunctionPointer <C_SetOperationStateDelegate>(ckFunctionList.C_SetOperationState); C_Login = UnmanagedLibrary.GetDelegateForFunctionPointer <C_LoginDelegate>(ckFunctionList.C_Login); C_Logout = UnmanagedLibrary.GetDelegateForFunctionPointer <C_LogoutDelegate>(ckFunctionList.C_Logout); C_CreateObject = UnmanagedLibrary.GetDelegateForFunctionPointer <C_CreateObjectDelegate>(ckFunctionList.C_CreateObject); C_CopyObject = UnmanagedLibrary.GetDelegateForFunctionPointer <C_CopyObjectDelegate>(ckFunctionList.C_CopyObject); C_DestroyObject = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DestroyObjectDelegate>(ckFunctionList.C_DestroyObject); C_GetObjectSize = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetObjectSizeDelegate>(ckFunctionList.C_GetObjectSize); C_GetAttributeValue = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetAttributeValueDelegate>(ckFunctionList.C_GetAttributeValue); C_SetAttributeValue = UnmanagedLibrary.GetDelegateForFunctionPointer <C_SetAttributeValueDelegate>(ckFunctionList.C_SetAttributeValue); C_FindObjectsInit = UnmanagedLibrary.GetDelegateForFunctionPointer <C_FindObjectsInitDelegate>(ckFunctionList.C_FindObjectsInit); C_FindObjects = UnmanagedLibrary.GetDelegateForFunctionPointer <C_FindObjectsDelegate>(ckFunctionList.C_FindObjects); C_FindObjectsFinal = UnmanagedLibrary.GetDelegateForFunctionPointer <C_FindObjectsFinalDelegate>(ckFunctionList.C_FindObjectsFinal); C_EncryptInit = UnmanagedLibrary.GetDelegateForFunctionPointer <C_EncryptInitDelegate>(ckFunctionList.C_EncryptInit); C_Encrypt = UnmanagedLibrary.GetDelegateForFunctionPointer <C_EncryptDelegate>(ckFunctionList.C_Encrypt); C_EncryptUpdate = UnmanagedLibrary.GetDelegateForFunctionPointer <C_EncryptUpdateDelegate>(ckFunctionList.C_EncryptUpdate); C_EncryptFinal = UnmanagedLibrary.GetDelegateForFunctionPointer <C_EncryptFinalDelegate>(ckFunctionList.C_EncryptFinal); C_DecryptInit = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DecryptInitDelegate>(ckFunctionList.C_DecryptInit); C_Decrypt = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DecryptDelegate>(ckFunctionList.C_Decrypt); C_DecryptUpdate = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DecryptUpdateDelegate>(ckFunctionList.C_DecryptUpdate); C_DecryptFinal = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DecryptFinalDelegate>(ckFunctionList.C_DecryptFinal); C_DigestInit = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DigestInitDelegate>(ckFunctionList.C_DigestInit); C_Digest = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DigestDelegate>(ckFunctionList.C_Digest); C_DigestUpdate = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DigestUpdateDelegate>(ckFunctionList.C_DigestUpdate); C_DigestKey = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DigestKeyDelegate>(ckFunctionList.C_DigestKey); C_DigestFinal = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DigestFinalDelegate>(ckFunctionList.C_DigestFinal); C_SignInit = UnmanagedLibrary.GetDelegateForFunctionPointer <C_SignInitDelegate>(ckFunctionList.C_SignInit); C_Sign = UnmanagedLibrary.GetDelegateForFunctionPointer <C_SignDelegate>(ckFunctionList.C_Sign); C_SignUpdate = UnmanagedLibrary.GetDelegateForFunctionPointer <C_SignUpdateDelegate>(ckFunctionList.C_SignUpdate); C_SignFinal = UnmanagedLibrary.GetDelegateForFunctionPointer <C_SignFinalDelegate>(ckFunctionList.C_SignFinal); C_SignRecoverInit = UnmanagedLibrary.GetDelegateForFunctionPointer <C_SignRecoverInitDelegate>(ckFunctionList.C_SignRecoverInit); C_SignRecover = UnmanagedLibrary.GetDelegateForFunctionPointer <C_SignRecoverDelegate>(ckFunctionList.C_SignRecover); C_VerifyInit = UnmanagedLibrary.GetDelegateForFunctionPointer <C_VerifyInitDelegate>(ckFunctionList.C_VerifyInit); C_Verify = UnmanagedLibrary.GetDelegateForFunctionPointer <C_VerifyDelegate>(ckFunctionList.C_Verify); C_VerifyUpdate = UnmanagedLibrary.GetDelegateForFunctionPointer <C_VerifyUpdateDelegate>(ckFunctionList.C_VerifyUpdate); C_VerifyFinal = UnmanagedLibrary.GetDelegateForFunctionPointer <C_VerifyFinalDelegate>(ckFunctionList.C_VerifyFinal); C_VerifyRecoverInit = UnmanagedLibrary.GetDelegateForFunctionPointer <C_VerifyRecoverInitDelegate>(ckFunctionList.C_VerifyRecoverInit); C_VerifyRecover = UnmanagedLibrary.GetDelegateForFunctionPointer <C_VerifyRecoverDelegate>(ckFunctionList.C_VerifyRecover); C_DigestEncryptUpdate = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DigestEncryptUpdateDelegate>(ckFunctionList.C_DigestEncryptUpdate); C_DecryptDigestUpdate = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DecryptDigestUpdateDelegate>(ckFunctionList.C_DecryptDigestUpdate); C_SignEncryptUpdate = UnmanagedLibrary.GetDelegateForFunctionPointer <C_SignEncryptUpdateDelegate>(ckFunctionList.C_SignEncryptUpdate); C_DecryptVerifyUpdate = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DecryptVerifyUpdateDelegate>(ckFunctionList.C_DecryptVerifyUpdate); C_GenerateKey = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GenerateKeyDelegate>(ckFunctionList.C_GenerateKey); C_GenerateKeyPair = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GenerateKeyPairDelegate>(ckFunctionList.C_GenerateKeyPair); C_WrapKey = UnmanagedLibrary.GetDelegateForFunctionPointer <C_WrapKeyDelegate>(ckFunctionList.C_WrapKey); C_UnwrapKey = UnmanagedLibrary.GetDelegateForFunctionPointer <C_UnwrapKeyDelegate>(ckFunctionList.C_UnwrapKey); C_DeriveKey = UnmanagedLibrary.GetDelegateForFunctionPointer <C_DeriveKeyDelegate>(ckFunctionList.C_DeriveKey); C_SeedRandom = UnmanagedLibrary.GetDelegateForFunctionPointer <C_SeedRandomDelegate>(ckFunctionList.C_SeedRandom); C_GenerateRandom = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GenerateRandomDelegate>(ckFunctionList.C_GenerateRandom); C_GetFunctionStatus = UnmanagedLibrary.GetDelegateForFunctionPointer <C_GetFunctionStatusDelegate>(ckFunctionList.C_GetFunctionStatus); C_CancelFunction = UnmanagedLibrary.GetDelegateForFunctionPointer <C_CancelFunctionDelegate>(ckFunctionList.C_CancelFunction); C_WaitForSlotEvent = UnmanagedLibrary.GetDelegateForFunctionPointer <C_WaitForSlotEventDelegate>(ckFunctionList.C_WaitForSlotEvent); }
/// <summary> /// Get delegates without C_GetFunctionList function from the dynamically loaded shared PKCS#11 library /// </summary> /// <param name="libraryHandle">Handle to the PKCS#11 library</param> private void InitializeWithoutGetFunctionList(IntPtr libraryHandle) { CK_FUNCTION_LIST ckFunctionList = new CK_FUNCTION_LIST(); ckFunctionList.C_Initialize = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_Initialize"); ckFunctionList.C_Finalize = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_Finalize"); ckFunctionList.C_GetInfo = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetInfo"); ckFunctionList.C_GetFunctionList = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetFunctionList"); ckFunctionList.C_GetSlotList = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetSlotList"); ckFunctionList.C_GetSlotInfo = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetSlotInfo"); ckFunctionList.C_GetTokenInfo = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetTokenInfo"); ckFunctionList.C_GetMechanismList = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetMechanismList"); ckFunctionList.C_GetMechanismInfo = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetMechanismInfo"); ckFunctionList.C_InitToken = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_InitToken"); ckFunctionList.C_InitPIN = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_InitPIN"); ckFunctionList.C_SetPIN = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_SetPIN"); ckFunctionList.C_OpenSession = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_OpenSession"); ckFunctionList.C_CloseSession = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_CloseSession"); ckFunctionList.C_CloseAllSessions = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_CloseAllSessions"); ckFunctionList.C_GetSessionInfo = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetSessionInfo"); ckFunctionList.C_GetOperationState = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetOperationState"); ckFunctionList.C_SetOperationState = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_SetOperationState"); ckFunctionList.C_Login = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_Login"); ckFunctionList.C_Logout = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_Logout"); ckFunctionList.C_CreateObject = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_CreateObject"); ckFunctionList.C_CopyObject = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_CopyObject"); ckFunctionList.C_DestroyObject = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DestroyObject"); ckFunctionList.C_GetObjectSize = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetObjectSize"); ckFunctionList.C_GetAttributeValue = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetAttributeValue"); ckFunctionList.C_SetAttributeValue = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_SetAttributeValue"); ckFunctionList.C_FindObjectsInit = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_FindObjectsInit"); ckFunctionList.C_FindObjects = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_FindObjects"); ckFunctionList.C_FindObjectsFinal = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_FindObjectsFinal"); ckFunctionList.C_EncryptInit = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_EncryptInit"); ckFunctionList.C_Encrypt = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_Encrypt"); ckFunctionList.C_EncryptUpdate = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_EncryptUpdate"); ckFunctionList.C_EncryptFinal = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_EncryptFinal"); ckFunctionList.C_DecryptInit = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DecryptInit"); ckFunctionList.C_Decrypt = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_Decrypt"); ckFunctionList.C_DecryptUpdate = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DecryptUpdate"); ckFunctionList.C_DecryptFinal = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DecryptFinal"); ckFunctionList.C_DigestInit = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DigestInit"); ckFunctionList.C_Digest = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_Digest"); ckFunctionList.C_DigestUpdate = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DigestUpdate"); ckFunctionList.C_DigestKey = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DigestKey"); ckFunctionList.C_DigestFinal = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DigestFinal"); ckFunctionList.C_SignInit = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_SignInit"); ckFunctionList.C_Sign = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_Sign"); ckFunctionList.C_SignUpdate = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_SignUpdate"); ckFunctionList.C_SignFinal = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_SignFinal"); ckFunctionList.C_SignRecoverInit = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_SignRecoverInit"); ckFunctionList.C_SignRecover = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_SignRecover"); ckFunctionList.C_VerifyInit = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_VerifyInit"); ckFunctionList.C_Verify = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_Verify"); ckFunctionList.C_VerifyUpdate = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_VerifyUpdate"); ckFunctionList.C_VerifyFinal = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_VerifyFinal"); ckFunctionList.C_VerifyRecoverInit = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_VerifyRecoverInit"); ckFunctionList.C_VerifyRecover = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_VerifyRecover"); ckFunctionList.C_DigestEncryptUpdate = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DigestEncryptUpdate"); ckFunctionList.C_DecryptDigestUpdate = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DecryptDigestUpdate"); ckFunctionList.C_SignEncryptUpdate = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_SignEncryptUpdate"); ckFunctionList.C_DecryptVerifyUpdate = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DecryptVerifyUpdate"); ckFunctionList.C_GenerateKey = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GenerateKey"); ckFunctionList.C_GenerateKeyPair = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GenerateKeyPair"); ckFunctionList.C_WrapKey = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_WrapKey"); ckFunctionList.C_UnwrapKey = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_UnwrapKey"); ckFunctionList.C_DeriveKey = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_DeriveKey"); ckFunctionList.C_SeedRandom = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_SeedRandom"); ckFunctionList.C_GenerateRandom = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GenerateRandom"); ckFunctionList.C_GetFunctionStatus = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_GetFunctionStatus"); ckFunctionList.C_CancelFunction = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_CancelFunction"); ckFunctionList.C_WaitForSlotEvent = UnmanagedLibrary.GetFunctionPointer(libraryHandle, "C_WaitForSlotEvent"); Initialize(ckFunctionList); }