Esempio n. 1
0
        /// <summary>
        /// Loads and initializes PCKS#11 library
        /// </summary>
        /// <param name="libraryPath">Library name or path</param>
        /// <param name="useOsLocking">Flag indicating whether PKCS#11 library can use the native operation system threading model for locking. Should be set to true in all multithreaded applications.</param>
        /// <param name="useGetFunctionList">Flag indicating whether cryptoki function pointers should be acquired via C_GetFunctionList (true) or via platform native function (false)</param>
        public Pkcs11(string libraryPath, bool useOsLocking, bool useGetFunctionList)
        {
            _p11 = new LowLevelAPI40.Pkcs11(libraryPath, useGetFunctionList);

            try
            {
                CK_C_INITIALIZE_ARGS initArgs = null;
                if (useOsLocking)
                {
                    initArgs       = new CK_C_INITIALIZE_ARGS();
                    initArgs.Flags = CKF.CKF_OS_LOCKING_OK;
                }

                CKR rv = _p11.C_Initialize(initArgs);
                if ((rv != CKR.CKR_OK) && (rv != CKR.CKR_CRYPTOKI_ALREADY_INITIALIZED))
                {
                    throw new Pkcs11Exception("C_Initialize", rv);
                }
            }
            catch
            {
                _p11.Dispose();
                _p11 = null;
                throw;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Loads and initializes PCKS#11 library
        /// </summary>
        /// <param name="libraryPath">Library name or path</param>
        /// <param name="appType">Type of application that will be using PKCS#11 library</param>
        public Pkcs11(string libraryPath, AppType appType)
        {
            _p11 = new LowLevelAPI40.Pkcs11(libraryPath);

            try
            {
                CK_C_INITIALIZE_ARGS initArgs = null;
                if (appType == AppType.MultiThreaded)
                {
                    initArgs       = new CK_C_INITIALIZE_ARGS();
                    initArgs.Flags = CKF.CKF_OS_LOCKING_OK;
                }

                CKR rv = _p11.C_Initialize(initArgs);
                if ((rv != CKR.CKR_OK) && (rv != CKR.CKR_CRYPTOKI_ALREADY_INITIALIZED))
                {
                    throw new Pkcs11Exception("C_Initialize", rv);
                }
            }
            catch
            {
                _p11.Dispose();
                _p11 = null;
                throw;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes new instance of Slot class
        /// </summary>
        /// <param name="pkcs11">Low level PKCS#11 wrapper</param>
        /// <param name="slotId">PKCS#11 handle of slot</param>
        internal Slot(LowLevelAPI40.Pkcs11 pkcs11, uint slotId)
        {
            if (pkcs11 == null)
                throw new ArgumentNullException("pkcs11");

            _p11 = pkcs11;
            _slotId = slotId;
        }
Esempio n. 4
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(Pkcs11Factories factories, LowLevelPkcs11 pkcs11, ulong sessionId)
        {
            LowLevelAPI40.Pkcs11 p11 = pkcs11 as LowLevelAPI40.Pkcs11;
            if (p11 == null)
            {
                throw new ArgumentException("Incorrect type of low level PKCS#11 wrapper");
            }

            return(new Session(factories, p11, sessionId));
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes new instance of Slot class
        /// </summary>
        /// <param name="pkcs11">Low level PKCS#11 wrapper</param>
        /// <param name="slotId">PKCS#11 handle of slot</param>
        internal Slot(LowLevelAPI40.Pkcs11 pkcs11, uint slotId)
        {
            if (pkcs11 == null)
            {
                throw new ArgumentNullException("pkcs11");
            }

            _p11    = pkcs11;
            _slotId = slotId;
        }
Esempio n. 6
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)
        {
            LowLevelAPI40.Pkcs11 p11 = pkcs11 as LowLevelAPI40.Pkcs11;
            if (p11 == null)
            {
                throw new ArgumentException("Incorrect type of low level PKCS#11 wrapper");
            }

            return(new Slot(factories, p11, slotId));
        }
Esempio n. 7
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>
        protected internal Slot(Pkcs11InteropFactories factories, LowLevelAPI40.Pkcs11 pkcs11, ulong slotId)
        {
            _logger.Debug("Slot({0})::ctor", slotId);

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

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

            _factories = factories;
            _p11       = pkcs11;
            _slotId    = ConvertUtils.UInt32FromUInt64(slotId);
        }
Esempio n. 8
0
        /// <summary>
        /// Disposes object
        /// </summary>
        /// <param name="disposing">Flag indicating whether managed resources should be disposed</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    // Dispose managed objects
                    if (_p11 != null)
                    {
                        _p11.C_Finalize(IntPtr.Zero);
                        _p11.Dispose();
                        _p11 = null;
                    }
                }

                // Dispose unmanaged objects
                _disposed = true;
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Disposes object
        /// </summary>
        /// <param name="disposing">Flag indicating whether managed resources should be disposed</param>
        protected virtual void Dispose(bool disposing)
        {
            _logger.Debug("Pkcs11({0})::Dispose2", _libraryPath);

            if (!this._disposed)
            {
                if (disposing)
                {
                    // Dispose managed objects
                    if (_p11 != null)
                    {
                        _p11.C_Finalize(IntPtr.Zero);

                        _logger.Info("Unloading PKCS#11 library {0}", _libraryPath);
                        _p11.Dispose();
                        _p11 = null;
                    }
                }

                // Dispose unmanaged objects
                _disposed = true;
            }
        }
Esempio n. 10
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 LowLevelAPI40.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. 11
0
        /// <summary>
        /// Loads and initializes PCKS#11 library
        /// </summary>
        /// <param name="libraryPath">Library name or path</param>
        /// <param name="useOsLocking">Flag indicating whether PKCS#11 library can use the native operation system threading model for locking. Should be set to true in all multithreaded applications.</param>
        public Pkcs11(string libraryPath, bool useOsLocking)
        {
            _p11 = new LowLevelAPI40.Pkcs11(libraryPath);

            try
            {
                CK_C_INITIALIZE_ARGS initArgs = null;
                if (useOsLocking)
                {
                    initArgs = new CK_C_INITIALIZE_ARGS();
                    initArgs.Flags = CKF.CKF_OS_LOCKING_OK;
                }

                CKR rv = _p11.C_Initialize(initArgs);
                if ((rv != CKR.CKR_OK) && (rv != CKR.CKR_CRYPTOKI_ALREADY_INITIALIZED))
                    throw new Pkcs11Exception("C_Initialize", rv);
            }
            catch
            {
                _p11.Dispose();
                _p11 = null;
                throw;
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Disposes object
        /// </summary>
        /// <param name="disposing">Flag indicating whether managed resources should be disposed</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    // Dispose managed objects
                    if (_p11 != null)
                    {
                        _p11.C_Finalize(IntPtr.Zero);
                        _p11.Dispose();
                        _p11 = null;
                    }
                }

                // Dispose unmanaged objects
                _disposed = true;
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Initializes new instance of Session class
        /// </summary>
        /// <param name="pkcs11">Low level PKCS#11 wrapper</param>
        /// <param name="sessionId">PKCS#11 handle of session</param>
        internal Session(LowLevelAPI40.Pkcs11 pkcs11, uint sessionId)
        {
            if (pkcs11 == null)
                throw new ArgumentNullException("pkcs11");

            if (sessionId == CK.CK_INVALID_HANDLE)
                throw new ArgumentException("Invalid handle specified", "sessionId");

            _p11 = pkcs11;
            _sessionId = sessionId;
        }