public Pkcs11Signature(string libraryPath, ulong slotId)
        {
            Pkcs11InteropFactories factories = new Pkcs11InteropFactories();

            pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, libraryPath, AppType.MultiThreaded);
            slot          = pkcs11Library.GetSlotList(SlotsType.WithOrWithoutTokenPresent).Find(slot => slot.SlotId == slotId);
        }
Esempio n. 2
0
        public Pkcs11Library(string libraryPath, string loggerPath = null)
        {
            _path = libraryPath;

            Pkcs11InteropFactories factories = Pkcs11Admin.Instance.Factories;

            try
            {
                _pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, loggerPath ?? libraryPath, AppType.MultiThreaded);
            }
            catch (Pkcs11Exception ex)
            {
                if (ex.RV == CKR.CKR_CANT_LOCK)
                {
                    _pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, loggerPath ?? libraryPath, AppType.SingleThreaded);
                }
                else
                {
                    throw;
                }
            }

            Info  = GetPkcs11LibraryInfo();
            Slots = GetPkcs11Slots();
        }
Esempio n. 3
0
        /// <summary>
        /// Creates slot with specified handle
        /// </summary>
        /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
        /// <param name="pkcs11">Low level PKCS#11 wrapper</param>
        /// <param name="slotId">PKCS#11 handle of slot</param>
        public ISlot CreateSlot(Pkcs11InteropFactories factories, LowLevelPkcs11 pkcs11, ulong slotId)
        {
            LowLevelAPI81.Pkcs11 p11 = pkcs11 as LowLevelAPI81.Pkcs11;
            if (p11 == null)
            {
                throw new ArgumentException("Incorrect type of low level PKCS#11 wrapper");
            }

            return(new Slot(factories, p11, slotId));
        }
Esempio n. 4
0
        /// <summary>
        /// Creates slot with specified handle
        /// </summary>
        /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
        /// <param name="pkcs11Library">Low level PKCS#11 wrapper</param>
        /// <param name="slotId">PKCS#11 handle of slot</param>
        public ISlot Create(Pkcs11InteropFactories factories, LowLevelPkcs11Library pkcs11Library, ulong slotId)
        {
            LowLevelAPI80.MockPkcs11Library p11 = pkcs11Library as LowLevelAPI80.MockPkcs11Library;
            if (p11 == null)
            {
                throw new ArgumentException("Incorrect type of low level PKCS#11 wrapper");
            }

            return(new MockSlot(factories, p11, slotId));
        }
        /// <summary>
        /// Initializes session with specified handle
        /// </summary>
        /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
        /// <param name="pkcs11">Low level PKCS#11 wrapper</param>
        /// <param name="sessionId">PKCS#11 handle of session</param>
        public ISession CreateSession(Pkcs11InteropFactories factories, LowLevelPkcs11 pkcs11, ulong sessionId)
        {
            LowLevelAPI80.MockPkcs11 p11 = pkcs11 as LowLevelAPI80.MockPkcs11;
            if (p11 == null)
            {
                throw new ArgumentException("Incorrect type of low level PKCS#11 wrapper");
            }

            return(new MockSession(factories, p11, sessionId));
        }
        /// <summary>
        /// Initializes session with specified handle
        /// </summary>
        /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
        /// <param name="pkcs11Library">Low level PKCS#11 wrapper</param>
        /// <param name="sessionId">PKCS#11 handle of session</param>
        public ISession Create(Pkcs11InteropFactories factories, LowLevelPkcs11Library pkcs11Library, ulong sessionId)
        {
            LowLevelAPI40.Pkcs11Library p11 = pkcs11Library as LowLevelAPI40.Pkcs11Library;
            if (p11 == null)
            {
                throw new ArgumentException("Incorrect type of low level PKCS#11 wrapper");
            }

            return(new Session(factories, p11, sessionId));
        }
Esempio n. 7
0
        /// <summary>
        /// Initializes new instance of Pkcs11 class
        /// </summary>
        /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
        /// <param name="libraryPath">Library name or path</param>
        protected Pkcs11(Pkcs11InteropFactories factories, string libraryPath)
        {
            _logger.Debug("Pkcs11({0})::ctor1", libraryPath);

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

            _factories   = factories;
            _libraryPath = libraryPath;
        }
        public void TestAccess()
        {
            // Specify the path to unmanaged PKCS#11 library provided by the cryptographic device vendor
            string pkcs11LibraryPath = @"d:\Program Files\SoftHSM2\lib\softhsm2-x64.dll";

            // Create factories used by Pkcs11Interop library
            Pkcs11InteropFactories factories = new Pkcs11InteropFactories();

            // Load unmanaged PKCS#11 library
            using (IPkcs11Library pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, pkcs11LibraryPath, AppType.MultiThreaded))
            {
                // Show general information about loaded library
                ILibraryInfo libraryInfo = pkcs11Library.GetInfo();

                Console.WriteLine("Library");
                Console.WriteLine("  Manufacturer:       " + libraryInfo.ManufacturerId);
                Console.WriteLine("  Description:        " + libraryInfo.LibraryDescription);
                Console.WriteLine("  Version:            " + libraryInfo.LibraryVersion);

                // Get list of all available slots
                foreach (ISlot slot in pkcs11Library.GetSlotList(SlotsType.WithOrWithoutTokenPresent))
                {
                    // Show basic information about slot
                    ISlotInfo slotInfo = slot.GetSlotInfo();

                    Console.WriteLine();
                    Console.WriteLine("Slot");
                    Console.WriteLine("  Manufacturer:       " + slotInfo.ManufacturerId);
                    Console.WriteLine("  Description:        " + slotInfo.SlotDescription);
                    Console.WriteLine("  Token present:      " + slotInfo.SlotFlags.TokenPresent);

                    if (slotInfo.SlotFlags.TokenPresent)
                    {
                        // Show basic information about token present in the slot
                        ITokenInfo tokenInfo = slot.GetTokenInfo();

                        Console.WriteLine("Token");
                        Console.WriteLine("  Manufacturer:       " + tokenInfo.ManufacturerId);
                        Console.WriteLine("  Model:              " + tokenInfo.Model);
                        Console.WriteLine("  Serial number:      " + tokenInfo.SerialNumber);
                        Console.WriteLine("  Label:              " + tokenInfo.Label);

                        // Show list of mechanisms (algorithms) supported by the token
                        Console.WriteLine("Supported mechanisms: ");
                        foreach (CKM mechanism in slot.GetMechanismList())
                        {
                            Console.WriteLine("  " + mechanism);
                        }
                    }
                }
            }
        }
Esempio n. 9
0
        public PkcsInteropSigner()
        {
            Pkcs11InteropFactories factories = new Pkcs11InteropFactories();

            pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, pkcs11LibraryPath, AppType.MultiThreaded);

            var slots = pkcs11Library.GetSlotList(SlotsType.WithTokenPresent);

            if (slots.Count == 0)
            {
                throw new InstanceCreationException("Por favor, verifique que el lector está conectado y que la cédula fue insertada correctamente.");
            }
            slot = slots[0];
        }
Esempio n. 10
0
        public ActionResult GetAllCerts()
        {
            Pkcs11InteropFactories factories = new Pkcs11InteropFactories();

            using (IPkcs11Library pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, DllLibPath, AppType.MultiThreaded))
            {
                ISlot slot = pkcs11Library.GetSlotList(SlotsType.WithTokenPresent).FirstOrDefault();

                if (slot is null)
                {
                    return(Ok("No slots found"));
                }



                ITokenInfo tokenInfo = slot.GetTokenInfo();

                ISlotInfo slotInfo = slot.GetSlotInfo();

                using (var session = slot.OpenSession(SessionType.ReadWrite))
                {
                    session.Login(CKU.CKU_USER, Encoding.UTF8.GetBytes(TokenPin));


                    var certificateSearchAttributes = new List <IObjectAttribute>()
                    {
                        session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_CERTIFICATE),
                        session.Factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, true),
                        session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CERTIFICATE_TYPE, CKC.CKC_X_509)
                    };

                    IObjectHandle certificate = session.FindAllObjects(certificateSearchAttributes).FirstOrDefault();

                    var certificateValue = session.GetAttributeValue(certificate, new List <CKA>
                    {
                        CKA.CKA_VALUE
                    });


                    var xcert = new X509Certificate2(certificateValue[0].GetValueAsByteArray());

                    return(Ok(

                               new
                    {
                        xcert.Thumbprint,
                        xcert.Subject,
                        xcert.IssuerName,
                        hasKeyNull = xcert.PrivateKey is null
                    }));
Esempio n. 11
0
        public Pkcs11Scp11Context(Session session) : base(session)
        {
            var factories = new Pkcs11InteropFactories();

            using (var lib = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, "/usr/local/lib/libykcs11.dylib", AppType.SingleThreaded))
            {
                foreach (var slot in lib.GetSlotList(SlotsType.WithTokenPresent))
                {
                    using (var s = slot.OpenSession(SessionType.ReadWrite))
                    {
                        s.Login(CKU.CKU_USER, "123456");

                        var keys = s.FindAllObjects(new List <IObjectAttribute> {
                            factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY),
                            factories.ObjectAttributeFactory.Create(CKA.CKA_KEY_TYPE, CKK.CKK_EC),
                            factories.ObjectAttributeFactory.Create(CKA.CKA_MODULUS_BITS, 256)
                        });

                        if (keys.Count > 0)
                        {
                            var bytes = s.GetAttributeValue(keys[0], new List <CKA> {
                                CKA.CKA_EC_POINT
                            })[0].GetValueAsByteArray();
                            var       octets = (Asn1OctetString)Asn1Object.FromByteArray(bytes);
                            X9ECPoint point  = new X9ECPoint(domain.Curve, octets);
                            pk_oce = new ECPublicKeyParameters(point.Point, domain);

                            var mech = factories.MechanismFactory.Create(CKM.CKM_ECDH1_DERIVE,
                                                                         factories.MechanismParamsFactory.CreateCkEcdh1DeriveParams((ulong)CKD.CKD_NULL, null, pk_sd.Q.GetEncoded()));

                            var obj = s.DeriveKey(mech, keys[0], new List <IObjectAttribute> {
                                factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY),
                                factories.ObjectAttributeFactory.Create(CKA.CKA_KEY_TYPE, CKK.CKK_GENERIC_SECRET),
                                factories.ObjectAttributeFactory.Create(CKA.CKA_EXTRACTABLE, true),
                                factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, false)
                            });

                            var v = s.GetAttributeValue(obj, new List <CKA> {
                                CKA.CKA_VALUE
                            });
                            shsss = v[0].GetValueAsByteArray();

                            s.DestroyObject(obj);

                            break;
                        }
                    }
                }
            }
        }
        public CardDeviceController(string pkcs11Libpath, IPinProvider pinProvider, string zepLabel = "SIG_ZEP")
        {
            if (pkcs11Libpath == null)
            {
                throw new ArgumentNullException(nameof(pkcs11Libpath));
            }
            if (pinProvider == null)
            {
                throw new ArgumentNullException(nameof(pinProvider));
            }

            this.pinProvider = pinProvider;
            Pkcs11InteropFactories factories = new Pkcs11InteropFactories();

            this.pkcs11 = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, pkcs11Libpath, AppType.SingleThreaded);

            try
            {
                List <ISlot> slots = this.pkcs11.GetSlotList(SlotsType.WithTokenPresent);
                this.slot = slots.SingleOrDefault(t => string.IsNullOrEmpty(zepLabel) || string.Equals(t.GetTokenInfo().Label, zepLabel, StringComparison.Ordinal));
                if (this.slot == null)
                {
                    this.pkcs11.Dispose();
                    throw new ArgumentException($"PKCS#11 lib '{pkcs11Libpath}' can not contains slot with label '{zepLabel}'.");
                }

                this.loginSession = this.slot.OpenSession(SessionType.ReadOnly);

                if (!this.SessionIsAuthenticated(this.loginSession))
                {
                    SecureString pin = pinProvider.GetBokPin();
                    try
                    {
                        this.loginSession.Login(CKU.CKU_USER, pin);
                    }
                    finally
                    {
                        pin?.Dispose();
                    }
                }
            }
            catch (Exception)
            {
                this.loginSession?.Dispose();
                this.pkcs11.Dispose();
                throw;
            }
        }
Esempio n. 13
0
        public void Initialize(string libraryFilePath)
        {
            if (Factories == null)
            {
                Factories = new Pkcs11InteropFactories();
            }

            if (PKCS11Library != null)
            {
                PKCS11Library.Dispose();
            }

            _libraryFilePath = libraryFilePath;
            PKCS11Library    = Factories.Pkcs11LibraryFactory.LoadPkcs11Library(Factories, LibraryFilePath, AppType.MultiThreaded);

            Initialized?.Invoke(this, new EventArgs());
        }
Esempio n. 14
0
        /// <summary>
        /// Initializes new instance of Slot class
        /// </summary>
        /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
        /// <param name="pkcs11Library">Low level PKCS#11 wrapper</param>
        /// <param name="slotId">PKCS#11 handle of slot</param>
        protected internal Slot(Pkcs11InteropFactories factories, LowLevelAPI80.Pkcs11Library pkcs11Library, ulong slotId)
        {
            _logger.Debug("Slot({0})::ctor", slotId);

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

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

            _factories     = factories;
            _pkcs11Library = pkcs11Library;
            _slotId        = ConvertUtils.UInt64FromUInt64(slotId);
        }
Esempio n. 15
0
        public void TestAccessKeyAndCertificate()
        {
            // Specify the path to unmanaged PKCS#11 library provided by the cryptographic device vendor
            string pkcs11LibraryPath = @"d:\Program Files\SoftHSM2\lib\softhsm2-x64.dll";

            Pkcs11InteropFactories factories = new Pkcs11InteropFactories();

            using (IPkcs11Library pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, pkcs11LibraryPath, AppType.MultiThreaded))
            {
                ISlot slot = pkcs11Library.GetSlotList(SlotsType.WithTokenPresent).Find(slot => slot.SlotId == 171137967);
                Assert.IsNotNull(slot, "Slot with ID 171137967 does not exist or does not have token.");

                using (ISession session = slot.OpenSession(SessionType.ReadWrite))
                {
                    // Login as normal user
                    session.Login(CKU.CKU_USER, "5678");

                    List <IObjectAttribute> attributes             = new List <IObjectAttribute>();
                    ObjectAttributeFactory  objectAttributeFactory = new ObjectAttributeFactory();
                    attributes.Add(objectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY));
                    List <IObjectHandle> keys = session.FindAllObjects(attributes);
                    Assert.AreEqual(1, keys.Count, "Unexpected number of private keys: {0}", keys.Count);


                    attributes.Clear();
                    attributes.Add(objectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_CERTIFICATE));
                    attributes.Add(objectAttributeFactory.Create(CKA.CKA_CERTIFICATE_TYPE, CKC.CKC_X_509));
                    List <IObjectHandle> certificates = session.FindAllObjects(attributes);
                    Assert.AreEqual(1, certificates.Count, "Unexpected number of certificates: {0}", certificates.Count);

                    List <CKA> certificateAttributeKeys = new List <CKA>();
                    certificateAttributeKeys.Add(CKA.CKA_VALUE);
                    certificateAttributeKeys.Add(CKA.CKA_LABEL);
                    List <IObjectAttribute> certificateAttributes = session.GetAttributeValue(certificates[0], certificateAttributeKeys);
                    Assert.AreEqual(2, certificateAttributes.Count, "Unexpected number of certificate attributes: {0}", certificateAttributes.Count);
                    string certificateLabel = certificateAttributes[1].GetValueAsString();
                    Console.WriteLine("Certificate label: {0}", certificateLabel);
                    byte[]           certificateBytes = certificateAttributes[0].GetValueAsByteArray();
                    X509Certificate2 certificate      = new X509Certificate2(certificateBytes);
                    Console.WriteLine("Subject: {0}", certificate.Subject.ToString());
                }
            }
        }
        public RutokenPkcs11Library(Pkcs11InteropFactories factories, string libraryPath, AppType appType)
            : base(factories, libraryPath)
        {
            _logger.Debug("RutokenPkcs11Library({0})::ctor1", _libraryPath);

            try
            {
                _logger.Info("Loading PKCS#11 library {0}", _libraryPath);
                _pkcs11Library = new LLA.RutokenPkcs11Library(libraryPath);
                Initialize(appType);
            }
            catch
            {
                if (_pkcs11Library != null)
                {
                    _logger.Info("Unloading PKCS#11 library {0}", _libraryPath);
                    _pkcs11Library.Dispose();
                    _pkcs11Library = null;
                }
                throw;
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Loads and initializes PCKS#11 library
        /// </summary>
        /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
        /// <param name="libraryPath">Library name or path</param>
        /// <param name="appType">Type of application that will be using PKCS#11 library</param>
        public MockPkcs11(Pkcs11InteropFactories factories, string libraryPath, AppType appType)
            : base(factories, libraryPath)
        {
            _logger.Debug("MockPkcs11({0})::ctor1", _libraryPath);

            try
            {
                _logger.Info("Loading PKCS#11 library {0}", _libraryPath);
                _p11 = new LowLevelAPI41.MockPkcs11(libraryPath);
                Initialize(appType);
            }
            catch
            {
                if (_p11 != null)
                {
                    _logger.Info("Unloading PKCS#11 library {0}", _libraryPath);
                    _p11.Dispose();
                    _p11 = null;
                }
                throw;
            }
        }
Esempio n. 18
0
        public ActionResult GetAllTokenDetails()
        {
            Pkcs11InteropFactories factories = new Pkcs11InteropFactories();
            List <ITokenInfo>      tokens    = new List <ITokenInfo>();
            List <ISlotInfo>       slots     = new List <ISlotInfo>();

            using (IPkcs11Library pkcs11Library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, DllLibPath, AppType.MultiThreaded))
            {
                var slotList = pkcs11Library.GetSlotList(SlotsType.WithTokenPresent).ToList();
                slotList.ForEach(item =>
                {
                    tokens.Add(item.GetTokenInfo());
                    slots.Add(item.GetSlotInfo());
                });

                return(Ok(new
                {
                    tokens,
                    slots
                }));
            }
        }
        /// <summary>
        /// Loads and initializes PCKS#11 library
        /// </summary>
        /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
        /// <param name="libraryPath">Library name or path</param>
        /// <param name="appType">Type of application that will be using PKCS#11 library</param>
        /// <param name="initType">Source of PKCS#11 function pointers</param>
        public MockPkcs11Library(Pkcs11InteropFactories factories, string libraryPath, AppType appType, InitType initType)
            : base(factories, libraryPath)
        {
            _logger.Debug("MockPkcs11Library({0})::ctor2", _libraryPath);

            try
            {
                _logger.Info("Loading PKCS#11 library {0}", _libraryPath);
                _pkcs11Library = new LowLevelAPI40.MockPkcs11Library(libraryPath, (initType == InitType.WithFunctionList));
                Initialize(appType);
            }
            catch
            {
                if (_pkcs11Library != null)
                {
                    _logger.Info("Unloading PKCS#11 library {0}", _libraryPath);
                    _pkcs11Library.Dispose();
                    _pkcs11Library = null;
                }
                throw;
            }
        }
Esempio n. 20
0
        /// <summary></summary>
        private void CheckForOpenSc()
        {
            string sProgramFiles;
            object ModuleValue;

            using (RegistryKey Pkcs11Key = Registry.LocalMachine.OpenSubKey(csOpenScSubkey, false))
            {
                if (Pkcs11Key != null)
                {
                    ModuleValue = Pkcs11Key.GetValue("Module");
                    if ((ModuleValue != null) && (File.Exists((string)ModuleValue)))
                    {
                        _sPkcs11Library = (string)ModuleValue;
                    }
                }
            }

            if (string.IsNullOrEmpty(_sPkcs11Library))
            {
                sProgramFiles = Environment.GetEnvironmentVariable("ProgramFiles");
                if (File.Exists(sProgramFiles + csOpenScSubpath))
                {
                    _sPkcs11Library = sProgramFiles + csOpenScSubpath;
                }
            }

            if (string.IsNullOrEmpty(_sPkcs11Library))
            {
                _Pkcs11Factories = null;
                _Pkcs11Library   = null;
            }
            else
            {
                _Pkcs11Factories   = new Pkcs11InteropFactories();
                _Pkcs11Library     = _Pkcs11Factories.Pkcs11LibraryFactory.LoadPkcs11Library(_Pkcs11Factories, _sPkcs11Library, AppType.SingleThreaded);
                _Pkcs11LibraryInfo = _Pkcs11Library.GetInfo();
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Loads and initializes PCKS#11 library
        /// </summary>
        /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
        /// <param name="libraryPath">Library name or path</param>
        /// <param name="appType">Type of application that will be using PKCS#11 library</param>
        public Pkcs11Library(Pkcs11InteropFactories factories, string libraryPath, AppType appType)
            : this(factories, libraryPath)
        {
            _logger.Debug("Pkcs11Library({0})::ctor2", _libraryPath);

            try
            {
                _logger.Info("Loading PKCS#11 library {0}", _libraryPath);
                _pkcs11Library = new LowLevelAPI41.Pkcs11Library(_libraryPath);
                Initialize(appType);
            }
            catch
            {
                if (_pkcs11Library != null)
                {
                    _logger.Info("Unloading PKCS#11 library {0}", _libraryPath);
                    _pkcs11Library.Dispose();
                    _pkcs11Library = null;
                }

                throw;
            }
        }
Esempio n. 22
0
        /// <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;
            }
        }
Esempio n. 23
0
        /// <summary>
        /// Loads and initializes PCKS#11 library
        /// </summary>
        /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
        /// <param name="libraryPath">Library name or path</param>
        /// <param name="appType">Type of application that will be using PKCS#11 library</param>
        /// <param name="initType">Source of PKCS#11 function pointers</param>
        public Pkcs11(Pkcs11InteropFactories factories, string libraryPath, AppType appType, InitType initType)
            : this(factories, libraryPath)
        {
            _logger.Debug("Pkcs11({0})::ctor3", _libraryPath);

            try
            {
                _logger.Info("Loading PKCS#11 library {0}", _libraryPath);
                _p11 = new LowLevelAPI80.Pkcs11(_libraryPath, (initType == InitType.WithFunctionList));
                Initialize(appType);
            }
            catch
            {
                if (_p11 != null)
                {
                    _logger.Info("Unloading PKCS#11 library {0}", _libraryPath);
                    _p11.Dispose();
                    _p11 = null;
                }

                throw;
            }
        }
Esempio n. 24
0
 /// <summary>
 /// Initializes new instance of Slot class
 /// </summary>
 /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
 /// <param name="pkcs11">Low level PKCS#11 wrapper</param>
 /// <param name="slotId">PKCS#11 handle of slot</param>
 internal MockSlot(Pkcs11InteropFactories factories, LowLevelAPI80.MockPkcs11 pkcs11, ulong slotId)
     : base(factories, pkcs11, slotId)
 {
     _logger.Debug("MockSlot({0})::ctor", _slotId);
 }
 public IRutokenSlot CreateRutoken(Pkcs11InteropFactories factories, LowLevelPkcs11Library pkcs11Library, ulong slotId)
 {
     return((IRutokenSlot)Create(factories, pkcs11Library, slotId));
 }
 public ISlot Create(Pkcs11InteropFactories factories, LowLevelPkcs11Library pkcs11Library, ulong slotId)
 {
     return(_factory.Create(factories, pkcs11Library, slotId));
 }
Esempio n. 27
0
 /// <summary>
 /// Initializes session with specified handle
 /// </summary>
 /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
 /// <param name="pkcs11">Low level PKCS#11 wrapper</param>
 /// <param name="sessionId">PKCS#11 handle of session</param>
 public ISession CreateSession(Pkcs11InteropFactories factories, LowLevelPkcs11 pkcs11, ulong sessionId)
 {
     return(_factory.CreateSession(factories, pkcs11, sessionId));
 }
 /// <summary>
 /// Initializes session with specified handle
 /// </summary>
 /// <param name="factories">Factories to be used by Developer and Pkcs11Interop library</param>
 /// <param name="pkcs11Library">Low level PKCS#11 wrapper</param>
 /// <param name="sessionId">PKCS#11 handle of session</param>
 public ISession Create(Pkcs11InteropFactories factories, LowLevelPkcs11Library pkcs11Library, ulong sessionId)
 {
     return(_factory.Create(factories, pkcs11Library, sessionId));
 }
 public IRutokenPkcs11Library LoadRutokenPkcs11Library(Pkcs11InteropFactories factories, string libraryPath, AppType appType, InitType initType)
 {
     return((IRutokenPkcs11Library)LoadPkcs11Library(factories, libraryPath, appType, initType));
 }
 public IPkcs11Library LoadPkcs11Library(Pkcs11InteropFactories factories, string libraryPath, AppType appType, InitType initType)
 {
     return(_factory.LoadPkcs11Library(factories, libraryPath, appType, initType));
 }