Example #1
0
        /// <summary>
        /// Static class constructor
        /// </summary>
        static Settings()
        {
            // Setup arguments passed to the C_Initialize function
            if (UseOsLocking)
            {
                InitArgs40 = new LLA40.CK_C_INITIALIZE_ARGS
                {
                    Flags = CKF.CKF_OS_LOCKING_OK
                };
                InitArgs41 = new LLA41.CK_C_INITIALIZE_ARGS
                {
                    Flags = CKF.CKF_OS_LOCKING_OK
                };
                InitArgs80 = new LLA80.CK_C_INITIALIZE_ARGS
                {
                    Flags = CKF.CKF_OS_LOCKING_OK
                };
                InitArgs81 = new LLA81.CK_C_INITIALIZE_ARGS
                {
                    Flags = CKF.CKF_OS_LOCKING_OK
                };
            }

            // Convert strings to byte arrays
            SecurityOfficerPinArray = ConvertUtils.Utf8StringToBytes(SecurityOfficerPin);
            NormalUserPinArray      = ConvertUtils.Utf8StringToBytes(NormalUserPin);
            NewUserPinArray         = ConvertUtils.Utf8StringToBytes(NewUserPin);
            WrongUserPinArray       = ConvertUtils.Utf8StringToBytes(WrongUserPin);
            LocalPinArray           = ConvertUtils.Utf8StringToBytes(LocalPin);
            TokenLongLabelArray     = ConvertUtils.Utf8StringToBytes(TokenLongLabel);
            TokenStdLabelArray      = ConvertUtils.Utf8StringToBytes(TokenStdLabel);
        }
Example #2
0
        /// <summary>
        /// Static class constructor
        /// </summary>
        static Settings()
        {
            // Uncomment following three lines to enable managed logging via System.Diagnostics.Trace class
            // SimplePkcs11InteropLoggerFactory simpleLoggerFactory = new SimplePkcs11InteropLoggerFactory();
            // simpleLoggerFactory.EnableDiagnosticsTraceOutput();
            // Pkcs11InteropLoggerFactory.SetLoggerFactory(simpleLoggerFactory);

            // Uncomment following three lines to enable unmanaged logging via PKCS11-LOGGER library
            // System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LIBRARY_PATH", Pkcs11LibraryPath);
            // System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LOG_FILE_PATH", @"c:\pkcs11-logger.txt");
            // Pkcs11LibraryPath = @"c:\pkcs11-logger-x86.dll";

            // Setup arguments passed to the C_Initialize function
            if (AppType == AppType.MultiThreaded)
            {
                InitArgs40       = new LLA40.CK_C_INITIALIZE_ARGS();
                InitArgs40.Flags = CKF.CKF_OS_LOCKING_OK;

                InitArgs41       = new LLA41.CK_C_INITIALIZE_ARGS();
                InitArgs41.Flags = CKF.CKF_OS_LOCKING_OK;

                InitArgs80       = new LLA80.CK_C_INITIALIZE_ARGS();
                InitArgs80.Flags = CKF.CKF_OS_LOCKING_OK;

                InitArgs81       = new LLA81.CK_C_INITIALIZE_ARGS();
                InitArgs81.Flags = CKF.CKF_OS_LOCKING_OK;
            }

            // Convert strings to byte arrays
            SecurityOfficerPinArray = ConvertUtils.Utf8StringToBytes(SecurityOfficerPin);
            NormalUserPinArray      = ConvertUtils.Utf8StringToBytes(NormalUserPin);
            ApplicationNameArray    = ConvertUtils.Utf8StringToBytes(ApplicationName);

            // Build PKCS#11 URI that identifies private key usable in signature creation tests
            Pkcs11UriBuilder pkcs11UriBuilder = new Pkcs11UriBuilder();

            pkcs11UriBuilder.ModulePath = Pkcs11LibraryPath;
            pkcs11UriBuilder.Serial     = TokenSerial;
            pkcs11UriBuilder.Token      = TokenLabel;
            pkcs11UriBuilder.PinValue   = NormalUserPin;
            pkcs11UriBuilder.Type       = CKO.CKO_PRIVATE_KEY;
            pkcs11UriBuilder.Object     = ApplicationName;

            PrivateKeyUri = pkcs11UriBuilder.ToString();
        }
Example #3
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 LowLevelAPI81.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;
            }
        }
Example #4
0
        /// <summary>
        /// Initializes the Cryptoki library
        /// </summary>
        /// <param name="initArgs">CK_C_INITIALIZE_ARGS structure containing information on how the library should deal with multi-threaded access or null if an application will not be accessing Cryptoki through multiple threads simultaneously</param>
        /// <returns>CKR_ARGUMENTS_BAD, CKR_CANT_LOCK, CKR_CRYPTOKI_ALREADY_INITIALIZED, CKR_FUNCTION_FAILED, CKR_GENERAL_ERROR, CKR_HOST_MEMORY, CKR_NEED_TO_CREATE_THREADS, CKR_OK</returns>
        public CKR C_Initialize(CK_C_INITIALIZE_ARGS initArgs)
        {
            if (this._disposed)
                throw new ObjectDisposedException(this.GetType().FullName);

            ulong rv = _delegates.C_Initialize(initArgs);
            return (CKR)Convert.ToUInt32(rv);
        }
Example #5
0
 internal static extern ulong C_Initialize(CK_C_INITIALIZE_ARGS initArgs);
Example #6
0
        /// <summary>
        /// Initializes the Cryptoki library
        /// </summary>
        /// <param name="initArgs">CK_C_INITIALIZE_ARGS structure containing information on how the library should deal with multi-threaded access or null if an application will not be accessing Cryptoki through multiple threads simultaneously</param>
        /// <returns>CKR_ARGUMENTS_BAD, CKR_CANT_LOCK, CKR_CRYPTOKI_ALREADY_INITIALIZED, CKR_FUNCTION_FAILED, CKR_GENERAL_ERROR, CKR_HOST_MEMORY, CKR_NEED_TO_CREATE_THREADS, CKR_OK</returns>
        public CKR C_Initialize(CK_C_INITIALIZE_ARGS initArgs)
        {
            if (this._disposed)
                throw new ObjectDisposedException(this.GetType().FullName);

            return _delegates.C_Initialize(initArgs);
        }
        public void _04_MultiThreadedInitializeTest()
        {
            if (Platform.UnmanagedLongSize != 8 || Platform.StructPackingSize != 1)
                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());
            }
        }
Example #8
0
 internal static extern CKR C_Initialize(CK_C_INITIALIZE_ARGS initArgs);
Example #9
0
        /// <summary>
        /// Static class constructor
        /// </summary>
        static Settings()
        {
            // Uncomment following three lines to enable logging of PKCS#11 calls with PKCS11-LOGGER library
            // System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LIBRARY_PATH", Pkcs11LibraryPath);
            // System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LOG_FILE_PATH", @"c:\pkcs11-logger.txt");
            // Pkcs11LibraryPath = @"c:\pkcs11-logger-x86.dll";

            // Setup arguments passed to the C_Initialize function
            if (UseOsLocking)
            {
                InitArgs40 = new LLA40.CK_C_INITIALIZE_ARGS();
                InitArgs40.Flags = CKF.CKF_OS_LOCKING_OK;

                InitArgs41 = new LLA41.CK_C_INITIALIZE_ARGS();
                InitArgs41.Flags = CKF.CKF_OS_LOCKING_OK;

                InitArgs80 = new LLA80.CK_C_INITIALIZE_ARGS();
                InitArgs80.Flags = CKF.CKF_OS_LOCKING_OK;

                InitArgs81 = new LLA81.CK_C_INITIALIZE_ARGS();
                InitArgs81.Flags = CKF.CKF_OS_LOCKING_OK;
            }

            // Convert strings to byte arrays
            SecurityOfficerPinArray = ConvertUtils.Utf8StringToBytes(SecurityOfficerPin);
            NormalUserPinArray = ConvertUtils.Utf8StringToBytes(NormalUserPin);
            ApplicationNameArray = ConvertUtils.Utf8StringToBytes(ApplicationName);

            // Build PKCS#11 URI that identifies private key usable in signature creation tests
            Pkcs11UriBuilder pkcs11UriBuilder = new Pkcs11UriBuilder();
            pkcs11UriBuilder.ModulePath = Pkcs11LibraryPath;
            pkcs11UriBuilder.Serial = TokenSerial;
            pkcs11UriBuilder.Token = TokenLabel;
            pkcs11UriBuilder.PinValue = NormalUserPin;
            pkcs11UriBuilder.Type = CKO.CKO_PRIVATE_KEY;
            pkcs11UriBuilder.Object = ApplicationName;
            
            PrivateKeyUri = pkcs11UriBuilder.ToString();
        }