/// <summary>
        /// Constructs internal context for Pkcs11Token class
        /// </summary>
        /// <param name="slotContext">Internal context for Pkcs11Slot class</param>
        /// <returns>Internal context for Pkcs11Token class</returns>
        private Pkcs11TokenContext GetTokenContext(Pkcs11SlotContext slotContext)
        {
            var     tokenInfo     = new Pkcs11TokenInfo(slotContext.Slot.GetTokenInfo());
            Session masterSession = (!tokenInfo.Initialized) ? null : slotContext.Slot.OpenSession(SessionType.ReadOnly);

            return(new Pkcs11TokenContext(tokenInfo, masterSession, slotContext));
        }
        /// <summary>
        /// Requests PIN code for PKCS#11 token
        /// </summary>
        /// <param name="tokenContext">Internal context for Pkcs11Token class</param>
        /// <returns>PIN code</returns>
        public static byte[] GetTokenPin(Pkcs11TokenContext tokenContext)
        {
            IPinProvider pinProvider = tokenContext.SlotContext.StoreContext.PinProvider;

            Pkcs11X509StoreInfo storeInfo = tokenContext.SlotContext.StoreContext.StoreInfo;
            Pkcs11SlotInfo      slotInfo  = tokenContext.SlotContext.SlotInfo;
            Pkcs11TokenInfo     tokenInfo = tokenContext.TokenInfo;

            GetPinResult getPinResult = pinProvider.GetTokenPin(storeInfo, slotInfo, tokenInfo);

            if (getPinResult == null)
            {
                throw new Exception("Invalid response from IPinProvider");
            }

            if (getPinResult.Cancel)
            {
                throw new LoginCancelledException("Login with token pin was cancelled");
            }

            return(getPinResult.Pin);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates new instance of Pkcs11TokenContext class
 /// </summary>
 /// <param name="tokenInfo">Detailed information about PKCS#11 token (cryptographic device)</param>
 /// <param name="authenticatedSession">High level PKCS#11 session that holds authenticated state of the token</param>
 /// <param name="slotContext">Internal context for Pkcs11Slot class</param>
 internal Pkcs11TokenContext(Pkcs11TokenInfo tokenInfo, ISession authenticatedSession, Pkcs11SlotContext slotContext)
 {
     _tokenInfo            = tokenInfo ?? throw new ArgumentNullException(nameof(tokenInfo));
     _authenticatedSession = authenticatedSession;
     _slotContext          = slotContext ?? throw new ArgumentNullException(nameof(slotContext));
 }