Net.Pkcs11Interop.LowLevelAPI40.Pkcs11.C_GetMechanismList C# (CSharp) Method

C_GetMechanismList() public method

Obtains a list of mechanism types supported by a token
public C_GetMechanismList ( uint slotId, CKM mechanismList, uint &count ) : CKR
slotId uint The ID of the token's slot
mechanismList CKM /// If set to null then the number of mechanisms is returned in "count" parameter, without actually returning a list of mechanisms. /// If not set to null then "count" parameter must contain the lenght of mechanismList array and mechanism list is returned in "mechanismList" parameter. ///
count uint Location that receives the number of mechanisms
return CKR
        public CKR C_GetMechanismList(uint slotId, CKM[] mechanismList, ref uint count)
        {
            if (this._disposed)
                throw new ObjectDisposedException(this.GetType().FullName);

            uint[] uintList = null;
            if (mechanismList != null)
                uintList = new uint[mechanismList.Length];

            uint rv = _delegates.C_GetMechanismList(slotId, uintList, ref count);

            if (mechanismList != null)
            {
                for (int i = 0; i < mechanismList.Length; i++)
                    mechanismList[i] = (CKM)uintList[i];
            }

            return (CKR)rv;
        }

Usage Example

        public void _01_BasicMechanismListAndInfoTest()
        {
            if (Platform.UnmanagedLongSize != 4 || Platform.StructPackingSize != 0)
                Assert.Inconclusive("Test cannot be executed on this platform");

            CKR rv = CKR.CKR_OK;
            
            using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath))
            {
                rv = pkcs11.C_Initialize(Settings.InitArgs40);
                if ((rv != CKR.CKR_OK) && (rv != CKR.CKR_CRYPTOKI_ALREADY_INITIALIZED))
                    Assert.Fail(rv.ToString());
                
                // Find first slot with token present
                uint slotId = Helpers.GetUsableSlot(pkcs11);
                
                // Get number of supported mechanisms in first call
                uint mechanismCount = 0;
                rv = pkcs11.C_GetMechanismList(slotId, null, ref mechanismCount);
                if (rv != CKR.CKR_OK)
                    Assert.Fail(rv.ToString());
                
                Assert.IsTrue(mechanismCount > 0);
                
                // Allocate array for supported mechanisms
                CKM[] mechanismList = new CKM[mechanismCount];
                
                // Get supported mechanisms in second call
                rv = pkcs11.C_GetMechanismList(slotId, mechanismList, ref mechanismCount);
                if (rv != CKR.CKR_OK)
                    Assert.Fail(rv.ToString());
                
                // Analyze first supported mechanism
                CK_MECHANISM_INFO mechanismInfo = new CK_MECHANISM_INFO();
                rv = pkcs11.C_GetMechanismInfo(slotId, mechanismList[0], ref mechanismInfo);
                if (rv != CKR.CKR_OK)
                    Assert.Fail(rv.ToString());
                
                // Do something interesting with mechanism info
                
                rv = pkcs11.C_Finalize(IntPtr.Zero);
                if (rv != CKR.CKR_OK)
                    Assert.Fail(rv.ToString());
            }
        }