/// <summary> /// Constructs internal context for Pkcs11X509Store class /// </summary> /// <param name="libraryPath">Name of or path to PKCS#11 library</param> /// <param name="pinProvider">Provider of PIN codes for PKCS#11 tokens and keys</param> /// <returns>Internal context for Pkcs11X509Store class</returns> private Pkcs11X509StoreContext GetStoreContext(string libraryPath, IPinProvider pinProvider) { Pkcs11 pkcs11 = null; try { pkcs11 = new Pkcs11(libraryPath, AppType.MultiThreaded); var storeInfo = new Pkcs11X509StoreInfo(libraryPath, pkcs11.GetInfo()); return(new Pkcs11X509StoreContext(pkcs11, storeInfo, pinProvider)); } catch { if (pkcs11 != null) { pkcs11.Dispose(); pkcs11 = null; } throw; } }
/// <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); }
/// <summary> /// Constructs internal context for Pkcs11X509Store class /// </summary> /// <param name="libraryPath">Name of or path to PKCS#11 library</param> /// <param name="pinProvider">Provider of PIN codes for PKCS#11 tokens and keys</param> /// <returns>Internal context for Pkcs11X509Store class</returns> private Pkcs11X509StoreContext GetStoreContext(string libraryPath, IPinProvider pinProvider) { Pkcs11InteropFactories factories = new Pkcs11InteropFactories(); IPkcs11Library pkcs11Library = null; try { pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, libraryPath, AppType.MultiThreaded); var storeInfo = new Pkcs11X509StoreInfo(libraryPath, pkcs11Library.GetInfo()); return(new Pkcs11X509StoreContext(pkcs11Library, storeInfo, pinProvider)); } catch { if (pkcs11Library != null) { pkcs11Library.Dispose(); pkcs11Library = null; } throw; } }
/// <summary> /// Creates new instance of Pkcs11X509StoreContext class /// </summary> /// <param name="pkcs11">High level PKCS#11 wrapper</param> /// <param name="storeInfo">Detailed information about PKCS#11 based X.509 store</param> /// <param name="pinProvider">Provider of PIN codes for PKCS#11 tokens and keys</param> internal Pkcs11X509StoreContext(Pkcs11 pkcs11, Pkcs11X509StoreInfo storeInfo, IPinProvider pinProvider) { _pkcs11 = pkcs11 ?? throw new ArgumentNullException(nameof(pkcs11)); _storeInfo = storeInfo ?? throw new ArgumentNullException(nameof(storeInfo)); _pinProvider = pinProvider ?? throw new ArgumentNullException(nameof(pinProvider)); }
/// <summary> /// Creates new instance of Pkcs11X509StoreContext class /// </summary> /// <param name="pkcs11Library">High level PKCS#11 wrapper</param> /// <param name="storeInfo">Detailed information about PKCS#11 based X.509 store</param> /// <param name="pinProvider">Provider of PIN codes for PKCS#11 tokens and keys</param> internal Pkcs11X509StoreContext(IPkcs11Library pkcs11Library, Pkcs11X509StoreInfo storeInfo, IPinProvider pinProvider) { _pkcs11Library = pkcs11Library ?? throw new ArgumentNullException(nameof(pkcs11Library)); _storeInfo = storeInfo ?? throw new ArgumentNullException(nameof(storeInfo)); _pinProvider = pinProvider ?? throw new ArgumentNullException(nameof(pinProvider)); }