Ejemplo n.º 1
0
        /// <summary>
        /// Obtains a list of slots in the system
        /// </summary>
        /// <param name="slotsType">Type of slots to be obtained</param>
        /// <returns>List of available slots</returns>
        public List <ISlot> GetSlotList(SlotsType slotsType)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            _logger.Debug("Pkcs11Library({0})::GetSlotList", _libraryPath);

            NativeULong slotCount = 0;
            CKR         rv        = _pkcs11Library.C_GetSlotList((slotsType == SlotsType.WithTokenPresent), null, ref slotCount);

            if (rv != CKR.CKR_OK)
            {
                throw new Pkcs11Exception("C_GetSlotList", rv);
            }

            if (slotCount == 0)
            {
                return(new List <ISlot>());
            }
            else
            {
                NativeULong[] slotList = new NativeULong[slotCount];
                rv = _pkcs11Library.C_GetSlotList((slotsType == SlotsType.WithTokenPresent), slotList, ref slotCount);
                if (rv != CKR.CKR_OK)
                {
                    throw new Pkcs11Exception("C_GetSlotList", rv);
                }

                if (slotList.Length != ConvertUtils.UInt32ToInt32(slotCount))
                {
                    Array.Resize(ref slotList, ConvertUtils.UInt32ToInt32(slotCount));
                }

                List <ISlot> list = new List <ISlot>();
                foreach (NativeULong slot in slotList)
                {
                    list.Add(_factories.SlotFactory.Create(_factories, _pkcs11Library, slot));
                }

                return(list);
            }
        }