Net.Pkcs11Interop.HighLevelAPI80.Pkcs11.GetSlotList C# (CSharp) Method

GetSlotList() public method

Obtains a list of slots in the system
public GetSlotList ( bool tokenPresent ) : List
tokenPresent bool Flag indicating whether the list obtained includes only those slots with a token present (true), or all slots (false)
return List
        public List<Slot> GetSlotList(bool tokenPresent)
        {
            if (this._disposed)
                throw new ObjectDisposedException(this.GetType().FullName);

            ulong slotCount = 0;
            CKR rv = _p11.C_GetSlotList(tokenPresent, null, ref slotCount);
            if (rv != CKR.CKR_OK)
                throw new Pkcs11Exception("C_GetSlotList", rv);

            if (slotCount == 0)
            {
                return new List<Slot>();
            }
            else
            {
                ulong[] slotList = new ulong[slotCount];
                rv = _p11.C_GetSlotList(tokenPresent, slotList, ref slotCount);
                if (rv != CKR.CKR_OK)
                    throw new Pkcs11Exception("C_GetSlotList", rv);

                if (slotList.Length != Convert.ToInt32(slotCount))
                    Array.Resize(ref slotList, Convert.ToInt32(slotCount));

                List<Slot> list = new List<Slot>();
                foreach (ulong slot in slotList)
                    list.Add(new Slot(_p11, slot));

                return list;
            }
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Obtains a list of all PKCS#11 URI matching slots
        /// </summary>
        /// <param name="pkcs11Uri">PKCS#11 URI</param>
        /// <param name="pkcs11">High level PKCS#11 wrapper</param>
        /// <param name="tokenPresent">Flag indicating whether the list obtained includes only those slots with a token present (true), or all slots (false)</param>
        /// <returns>List of slots matching PKCS#11 URI</returns>
        public static List <Slot> GetMatchingSlotList(Pkcs11Uri pkcs11Uri, Pkcs11 pkcs11, bool tokenPresent)
        {
            if (pkcs11Uri == null)
            {
                throw new ArgumentNullException("pkcs11Uri");
            }

            if (pkcs11 == null)
            {
                throw new ArgumentNullException("pkcs11");
            }

            List <Slot> matchingSlots = new List <Slot>();

            LibraryInfo libraryInfo = pkcs11.GetInfo();

            if (!Matches(pkcs11Uri, libraryInfo))
            {
                return(matchingSlots);
            }

            List <Slot> slots = pkcs11.GetSlotList(false);

            if ((slots == null) || (slots.Count == 0))
            {
                return(slots);
            }

            foreach (Slot slot in slots)
            {
                SlotInfo slotInfo = slot.GetSlotInfo();
                if (Matches(pkcs11Uri, slotInfo))
                {
                    if (slotInfo.SlotFlags.TokenPresent)
                    {
                        TokenInfo tokenInfo = slot.GetTokenInfo();
                        if (Matches(pkcs11Uri, tokenInfo))
                        {
                            matchingSlots.Add(slot);
                        }
                    }
                    else
                    {
                        if (!tokenPresent && Pkcs11UriSharedUtils.Matches(pkcs11Uri, null, null, null, null))
                        {
                            matchingSlots.Add(slot);
                        }
                    }
                }
            }

            return(matchingSlots);
        }
All Usage Examples Of Net.Pkcs11Interop.HighLevelAPI80.Pkcs11::GetSlotList