Exemplo n.º 1
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 LowLevelAPI41.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;
            }
        }
Exemplo n.º 2
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 LowLevelAPI41.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;
            }
        }
Exemplo n.º 3
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;
            }
        }
Exemplo n.º 4
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;
            }
        }
Exemplo n.º 5
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 LowLevelAPI41.Pkcs11(_libraryPath, (initType == InitType.WithFunctionList));
                Initialize(appType);
            }
            catch
            {
                if (_p11 != null)
                {
                    _logger.Info("Unloading PKCS#11 library {0}", _libraryPath);
                    _p11.Dispose();
                    _p11 = null;
                }

                throw;
            }
        }
Exemplo n.º 6
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 LowLevelAPI41.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;
            }
        }