Net.Pkcs11Interop.LowLevelAPI81.Pkcs11.C_GetSlotList C# (CSharp) Method

C_GetSlotList() public method

Obtains a list of slots in the system
public C_GetSlotList ( bool tokenPresent, ulong slotList, ulong &count ) : CKR
tokenPresent bool Indicates whether the list obtained includes only those slots with a token present (true) or all slots (false)
slotList ulong /// If set to null then the number of slots is returned in "count" parameter, without actually returning a list of slots. /// If not set to null then "count" parameter must contain the lenght of slotList array and slot list is returned in "slotList" parameter. ///
count ulong Location that receives the number of slots
return CKR
        public CKR C_GetSlotList(bool tokenPresent, ulong[] slotList, ref ulong count)
        {
            if (this._disposed)
                throw new ObjectDisposedException(this.GetType().FullName);

            ulong rv = _delegates.C_GetSlotList(tokenPresent, slotList, ref count);
            return (CKR)Convert.ToUInt32(rv);
        }

Usage Example

コード例 #1
0
ファイル: Helpers.cs プロジェクト: Gianluigi/Pkcs11Interop
        /// <summary>
        /// Finds slot containing the token that matches criteria specified in Settings class
        /// </summary>
        /// <param name='pkcs11'>Initialized PKCS11 wrapper</param>
        /// <returns>Slot containing the token that matches criteria</returns>
        public static ulong GetUsableSlot(Pkcs11 pkcs11)
        {
            CKR rv = CKR.CKR_OK;

            // Get list of available slots with token present
            ulong slotCount = 0;
            rv = pkcs11.C_GetSlotList(true, null, ref slotCount);
            if (rv != CKR.CKR_OK)
                Assert.Fail(rv.ToString());

            Assert.IsTrue(slotCount > 0);

            ulong[] slotList = new ulong[slotCount];

            rv = pkcs11.C_GetSlotList(true, slotList, ref slotCount);
            if (rv != CKR.CKR_OK)
                Assert.Fail(rv.ToString());

            // Return first slot with token present when both TokenSerial and TokenLabel are null...
            if (Settings.TokenSerial == null && Settings.TokenLabel == null)
                return slotList[0];

            // First slot with token present is OK...
            ulong? matchingSlot = slotList[0];

            // ...unless there are matching criteria specified in Settings class
            if (Settings.TokenSerial != null || Settings.TokenLabel != null)
            {
                matchingSlot = null;

                foreach (uint slot in slotList)
                {
                    CK_TOKEN_INFO tokenInfo = new CK_TOKEN_INFO();
                    rv = pkcs11.C_GetTokenInfo(slot, ref tokenInfo);
                    if (rv != CKR.CKR_OK)
                    {
                        if (rv == CKR.CKR_TOKEN_NOT_RECOGNIZED || rv == CKR.CKR_TOKEN_NOT_PRESENT)
                            continue;
                        else
                            Assert.Fail(rv.ToString());
                    }

                    if (!string.IsNullOrEmpty(Settings.TokenSerial))
                        if (0 != string.Compare(Settings.TokenSerial, ConvertUtils.BytesToUtf8String(tokenInfo.SerialNumber, true), StringComparison.Ordinal))
                            continue;

                    if (!string.IsNullOrEmpty(Settings.TokenLabel))
                        if (0 != string.Compare(Settings.TokenLabel, ConvertUtils.BytesToUtf8String(tokenInfo.Label, true), StringComparison.Ordinal))
                            continue;

                    matchingSlot = slot;
                    break;
                }
            }

            Assert.IsTrue(matchingSlot != null, "Token matching criteria specified in Settings class is not present");
            return matchingSlot.Value;
        }
All Usage Examples Of Net.Pkcs11Interop.LowLevelAPI81.Pkcs11::C_GetSlotList