Exemplo n.º 1
0
        /// <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>
        /// Ejects token from slot.
        /// This method should be used only for testing purposes with PKCS11-MOCK module.
        /// </summary>
        /// <param name="pkcs11">Instance of the extended class</param>
        /// <param name="slotId">The ID of the token's slot</param>
        /// <returns>CKR_CRYPTOKI_NOT_INITIALIZED, CKR_SLOT_ID_INVALID, CKR_OK</returns>
        public static CKR C_EjectToken(this LLA81.Pkcs11 pkcs11, ulong slotId)
        {
            if (pkcs11.Disposed)
            {
                throw new ObjectDisposedException(pkcs11.GetType().FullName);
            }

            Delegates.C_EjectToken8x cEjectToken = null;

            if (pkcs11.LibraryHandle != IntPtr.Zero)
            {
                IntPtr cEjectTokenPtr = UnmanagedLibrary.GetFunctionPointer(pkcs11.LibraryHandle, "C_EjectToken");
                cEjectToken = UnmanagedLibrary.GetDelegateForFunctionPointer <Delegates.C_EjectToken8x>(cEjectTokenPtr);
            }
            else
            {
                cEjectToken = NativeMethods.C_EjectToken8x;
            }

            ulong rv = cEjectToken(slotId);

            return((CKR)Convert.ToUInt32(rv));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Obtains a list of unmanaged struct sizes.
        /// This method should be used only for testing purposes with PKCS11-MOCK module.
        /// </summary>
        /// <param name="pkcs11">Instance of the extended class</param>
        /// <param name="sizeList">
        /// If set to null then the number of sizes is returned in "count" parameter, without actually returning a list of sizes.
        /// If not set to null then "count" parameter must contain the lenght of sizeList array and size list is returned in "sizeList" parameter.
        /// </param>
        /// <param name="count">Location that receives the number of sizes</param>
        /// <returns>CKR_ARGUMENTS_BAD, CKR_BUFFER_TOO_SMALL, CKR_OK</returns>
        public static CKR C_GetUnmanagedStructSizeList(this LLA81.Pkcs11 pkcs11, ulong[] sizeList, ref ulong count)
        {
            if (pkcs11.Disposed)
            {
                throw new ObjectDisposedException(pkcs11.GetType().FullName);
            }

            Delegates.C_GetUnmanagedStructSizeListDelegate8x cGetUnmanagedStructSizeList = null;

            if (pkcs11.LibraryHandle != IntPtr.Zero)
            {
                IntPtr cGetUnmanagedStructSizeListPtr = UnmanagedLibrary.GetFunctionPointer(pkcs11.LibraryHandle, "C_GetUnmanagedStructSizeList");
                cGetUnmanagedStructSizeList = UnmanagedLibrary.GetDelegateForFunctionPointer <Delegates.C_GetUnmanagedStructSizeListDelegate8x>(cGetUnmanagedStructSizeListPtr);
            }
            else
            {
                cGetUnmanagedStructSizeList = NativeMethods.C_GetUnmanagedStructSizeList8x;
            }

            ulong rv = cGetUnmanagedStructSizeList(sizeList, ref count);

            return((CKR)Convert.ToUInt32(rv));
        }
        /// <summary>
        /// Logs a user into a token interactively.
        /// This method should be used only for testing purposes with PKCS11-MOCK module.
        /// </summary>
        /// <param name="pkcs11">Instance of the extended class</param>
        /// <param name="session">The session's handle</param>
        /// <returns>CKR_CRYPTOKI_NOT_INITIALIZED, CKR_SESSION_HANDLE_INVALID, CKR_USER_ALREADY_LOGGED_IN, CKR_USER_ANOTHER_ALREADY_LOGGED_IN, CKR_OK</returns>
        public static CKR C_InteractiveLogin(this LLA41.Pkcs11 pkcs11, uint session)
        {
            if (pkcs11.Disposed)
            {
                throw new ObjectDisposedException(pkcs11.GetType().FullName);
            }

            Delegates.C_InteractiveLogin4x cInteractiveLogin = null;

            if (pkcs11.LibraryHandle != IntPtr.Zero)
            {
                IntPtr cInteractiveLoginPtr = UnmanagedLibrary.GetFunctionPointer(pkcs11.LibraryHandle, "C_InteractiveLogin");
                cInteractiveLogin = UnmanagedLibrary.GetDelegateForFunctionPointer <Delegates.C_InteractiveLogin4x>(cInteractiveLoginPtr);
            }
            else
            {
                cInteractiveLogin = NativeMethods.C_InteractiveLogin4x;
            }

            uint rv = cInteractiveLogin(session);

            return((CKR)rv);
        }
Exemplo n.º 5
0
 /// <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);
 }
Exemplo n.º 6
0
        private void InitializeWithHandle(IntPtr libraryHandle)
        {
            RG_GetVersion =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_GetVersionDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_GetVersion"));

            RG_InitializeLib =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_InitializeLibDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_InitializeLib"));

            RG_Uninitialize =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_UninitializeDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_Uninitialize"));

            RG_CloseResource =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_CloseResourceDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_CloseResource"));

            RG_FindEndPoints =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_FindEndPointsDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_FindEndPoints"));

            RG_GetFoundEndPointInfo =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_GetFoundEndPointInfoDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_GetFoundEndPointInfo"));


            RG_FindDevices =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_FindDevicesDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_FindDevices"));

            RG_GetFoundDeviceInfo =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_GetFoundDeviceInfoDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_GetFoundDeviceInfo"));

            RG_InitDevice =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_InitDeviceDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_InitDevice"));

            RG_GetInfo =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_GetInfoDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_GetInfo"));

            RG_GetInfoExt =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_GetInfoExtDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_GetInfoExt"));

            RG_GetStatus =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_GetStatusDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_GetStatus"));

            RG_SetCardsMask =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_SetCardsMaskDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_SetCardsMask"));

            RG_ClearProfiles =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_ClearProfilesDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_ClearProfiles"));

            RG_WriteProfile =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_WriteProfileDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_WriteProfile"));

            RG_WriteCodogramm =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_WriteCodogrammDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_WriteCodogramm"));

            RG_StartCodogramm =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_StartCodogrammDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_StartCodogramm"));

            RG_SetControlOutputState =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_SetControlOutputStateDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_SetControlOutputState"));

            RG_ReadBlockDirect =
                UnmanagedLibrary.GetDelegateForFunctionPointer <RG_ReadBlockDirectDelegate>(
                    UnmanagedLibrary.GetFunctionPointer(libraryHandle, "RG_ReadBlockDirect"));
        }