Beispiel #1
0
        public static SafeProvHandleImpl AcquireProvider(CspParameters providerParameters)
        {
            var providerHandle = SafeProvHandleImpl.InvalidHandle;


            var dwFlags = Constants.CRYPT_VERIFYCONTEXT;

            if ((providerParameters.Flags & CspProviderFlags.UseMachineKeyStore) != CspProviderFlags.NoFlags)
            {
                dwFlags |= Constants.CRYPT_MACHINE_KEYSET;
            }

            if (!CryptoApi.CryptAcquireContext(ref providerHandle, providerParameters.KeyContainerName, providerParameters.ProviderName, (uint)providerParameters.ProviderType, dwFlags))
            {
                throw CreateWin32Error();
            }

            return(providerHandle);
        }
Beispiel #2
0
        public static int GetKeyParameterInt32(SafeKeyHandleImpl keyHandle, uint keyParamId)
        {
            const int doubleWordSize = 4;

            uint dwDataLength = doubleWordSize;
            var  dwDataBytes  = new byte[doubleWordSize];

            if (!CryptoApi.CryptGetKeyParam(keyHandle, keyParamId, dwDataBytes, ref dwDataLength, 0))
            {
                throw CreateWin32Error();
            }

            if (dwDataLength != doubleWordSize)
            {
                throw ExceptionUtility.CryptographicException(Constants.NTE_BAD_DATA);
            }

            return(BitConverter.ToInt32(dwDataBytes, 0));
        }
Beispiel #3
0
        /// <summary>
        /// Вернет список криптопровайдеров
        /// </summary>
        /// <returns></returns>
        public static Dictionary <string, int> GetProviders()
        {
            Dictionary <string, int> installedCSPs = new Dictionary <string, int>();
            uint          cbName;
            uint          dwType;
            uint          dwIndex;
            StringBuilder pszName;

            dwIndex = 0;
            dwType  = 1;
            cbName  = 0;
            while (CryptoApi.CryptEnumProviders(dwIndex, IntPtr.Zero, 0, ref dwType, null, ref cbName))
            {
                pszName = new StringBuilder((int)cbName);

                if (CryptoApi.CryptEnumProviders(dwIndex++, IntPtr.Zero, 0, ref dwType, pszName, ref cbName))
                {
                    installedCSPs.Add(pszName.ToString(), (int)dwType);
                }
            }
            return(installedCSPs);
        }
Beispiel #4
0
        private static SafeHashHandleImpl SetupHashAlgorithm(SafeProvHandleImpl providerHandle, byte[] hashValue)
        {
            var hashHandle = CreateHash_3411_94(providerHandle);

            uint hashLength = 0;

            if (!CryptoApi.CryptGetHashParam(hashHandle, Constants.HP_HASHVAL, null, ref hashLength, 0))
            {
                throw CreateWin32Error();
            }

            if (hashValue.Length != hashLength)
            {
                throw ExceptionUtility.CryptographicException(Constants.NTE_BAD_HASH);
            }

            if (!CryptoApi.CryptSetHashParam(hashHandle, Constants.HP_HASHVAL, hashValue, 0))
            {
                throw CreateWin32Error();
            }

            return(hashHandle);
        }
Beispiel #5
0
        public static byte[] SignValue(SafeProvHandleImpl hProv, int keyNumber, byte[] hashValue)
        {
            using (var hashHandle = SetupHashAlgorithm(hProv, hashValue))
            {
                uint signatureLength = 0;

                // Вычисление размера подписи
                if (!CryptoApi.CryptSignHash(hashHandle, (uint)keyNumber, null, 0, null, ref signatureLength))
                {
                    throw CreateWin32Error();
                }

                var signatureValue = new byte[signatureLength];

                // Вычисление значения подписи
                if (!CryptoApi.CryptSignHash(hashHandle, (uint)keyNumber, null, 0, signatureValue, ref signatureLength))
                {
                    throw CreateWin32Error();
                }

                return(signatureValue);
            }
        }
Beispiel #6
0
        public static void SetProviderParameter(SafeProvHandleImpl providerHandle, int keyNumber, uint keyParamId, IntPtr keyParamValue)
        {
            if ((keyParamId == Constants.PP_KEYEXCHANGE_PIN) || (keyParamId == Constants.PP_SIGNATURE_PIN))
            {
                if (keyNumber == Constants.AT_KEYEXCHANGE)
                {
                    keyParamId = Constants.PP_KEYEXCHANGE_PIN;
                }
                else if (keyNumber == Constants.AT_SIGNATURE)
                {
                    keyParamId = Constants.PP_SIGNATURE_PIN;
                }
                else
                {
                    throw ExceptionUtility.NotSupported("KeyAlgorithmNotSupported");
                }
            }

            if (!CryptoApi.CryptSetProvParam(providerHandle, keyParamId, keyParamValue, 0))
            {
                throw CreateWin32Error();
            }
        }
Beispiel #7
0
 protected override bool ReleaseHandle()
 {
     CryptoApi.CryptDestroyHash(handle);
     return(true);
 }