public void _04_MultiThreadedInitializeTest() { if (Platform.UnmanagedLongSize != 4 || Platform.StructPackingSize != 0) { Assert.Inconclusive("Test cannot be executed on this platform"); } CKR rv = CKR.CKR_OK; using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath)) { // If an application will be accessing PKCS#11 library from multiple threads // simultaneously, it has to provide initArgs parameter to C_Initialize method. // The easiest way is to set CKF_OS_LOCKING_OK flag, which will indicate that // PKCS#11 library can use the native operation system threading model for locking. CK_C_INITIALIZE_ARGS initArgs = new CK_C_INITIALIZE_ARGS(); initArgs.Flags = CKF.CKF_OS_LOCKING_OK; rv = pkcs11.C_Initialize(initArgs); if ((rv != CKR.CKR_OK) && (rv != CKR.CKR_CRYPTOKI_ALREADY_INITIALIZED)) { Assert.Fail(rv.ToString()); } // Do something interesting rv = pkcs11.C_Finalize(IntPtr.Zero); if (rv != CKR.CKR_OK) { Assert.Fail(rv.ToString()); } } }
public void _04_MultiThreadedInitializeTest() { Helpers.CheckPlatform(); CKR rv = CKR.CKR_OK; using (Pkcs11Library pkcs11Library = new Pkcs11Library(Settings.Pkcs11LibraryPath)) { // If an application will be accessing PKCS#11 library from multiple threads // simultaneously, it has to provide initArgs parameter to C_Initialize method. // The easiest way is to set CKF_OS_LOCKING_OK flag, which will indicate that // PKCS#11 library can use the native operation system threading model for locking. CK_C_INITIALIZE_ARGS initArgs = new CK_C_INITIALIZE_ARGS(); initArgs.Flags = CKF.CKF_OS_LOCKING_OK; rv = pkcs11Library.C_Initialize(initArgs); if ((rv != CKR.CKR_OK) && (rv != CKR.CKR_CRYPTOKI_ALREADY_INITIALIZED)) { Assert.Fail(rv.ToString()); } // Do something interesting rv = pkcs11Library.C_Finalize(IntPtr.Zero); if (rv != CKR.CKR_OK) { Assert.Fail(rv.ToString()); } } }
/// <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; } }
/// <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; } }
/// <summary> /// Initializes PCKS#11 library /// </summary> /// <param name="appType">Type of application that will be using PKCS#11 library</param> protected void Initialize(AppType appType) { _logger.Debug("Pkcs11({0})::Initialize", _libraryPath); 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); } }