Example #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));
        }
Example #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);
        }
Example #5
0
        /// <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);
        }
        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"));
        }