Beispiel #1
0
        /// <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 IRutokenSlot GetUsableSlot(IRutokenPkcs11Library pkcs11)
        {
            // Get list of available slots with token present
            List <IRutokenSlot> slots = pkcs11.GetRutokenSlotList(SlotsType.WithTokenPresent);

            Assert.IsNotNull(slots);
            Assert.IsTrue(slots.Count > 0);

            // First slot with token present is OK...
            IRutokenSlot matchingSlot = slots[0];

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

                foreach (IRutokenSlot slot in slots)
                {
                    ITokenInfo tokenInfo = null;

                    try
                    {
                        tokenInfo = slot.GetTokenInfo();
                    }
                    catch (Pkcs11Exception ex)
                    {
                        if (ex.RV != CKR.CKR_TOKEN_NOT_RECOGNIZED && ex.RV != CKR.CKR_TOKEN_NOT_PRESENT)
                        {
                            throw;
                        }
                    }

                    if (tokenInfo == null)
                    {
                        continue;
                    }

                    if (!string.IsNullOrEmpty(Settings.TokenSerial))
                    {
                        if (0 != string.Compare(Settings.TokenSerial, tokenInfo.SerialNumber, StringComparison.Ordinal))
                        {
                            continue;
                        }
                    }

                    if (!string.IsNullOrEmpty(Settings.TokenLabel))
                    {
                        if (0 != string.Compare(Settings.TokenLabel, tokenInfo.Label, StringComparison.Ordinal))
                        {
                            continue;
                        }
                    }

                    matchingSlot = slot;
                    break;
                }
            }

            Assert.IsTrue(matchingSlot != null, "Token matching criteria specified in Settings class is not present");
            return(matchingSlot);
        }
Beispiel #2
0
 public TokenSlot(IRutokenPkcs11Library pkcs11)
 {
     _pkcs11 = pkcs11;
 }