Ejemplo n.º 1
0
        public static SigningKey CreateFromCertificate(X509Certificate2 certificate)
        {
            SafeCryptKeyHandle cryptKeyHandleFromCertificatePrivateKey = CngNative.GetCryptKeyHandleFromCertificatePrivateKey(certificate);
            string             text = CngNative.NCryptGetPropertyString(cryptKeyHandleFromCertificatePrivateKey, "Algorithm Group");

            if (text.Equals("RSA", StringComparison.OrdinalIgnoreCase))
            {
                return(new RsaSigningKey(cryptKeyHandleFromCertificatePrivateKey, certificate));
            }
            return(new DsaSigningKey(cryptKeyHandleFromCertificatePrivateKey, certificate));
        }
        internal static int NCryptGetPropertyInt32(SafeCryptKeyHandle hObject, string propertyName)
        {
            byte[] array = new byte[4];
            int    num;

            CngNative.ErrorCode errorCode = CngNative.NCryptGetProperty(hObject, propertyName, array, array.Length, out num, 0);
            if (errorCode != CngNative.ErrorCode.Success)
            {
                throw new Win32Exception((int)errorCode);
            }
            return(BitConverter.ToInt32(array, 0));
        }
        public override byte[] Sign(byte[] data, SignatureHashAlgorithm hashAlgorithm)
        {
            HashAlgorithm hashAlgorithm2 = hashAlgorithm.CreateAlgorithm();

            byte[] array = hashAlgorithm2.ComputeHash(data);
            int    num;

            CngNative.ErrorCode status = CngNative.NCryptSignHash(base.KeyHandle, IntPtr.Zero, array, array.Length, null, 0, out num, 0);
            CngNative.VerifyStatus(status);
            byte[] array2 = new byte[num];
            status = CngNative.NCryptSignHash(base.KeyHandle, IntPtr.Zero, array, array.Length, array2, array2.Length, out num, 0);
            CngNative.VerifyStatus(status);
            return(array2);
        }
        public override byte[] Sign(byte[] data, SignatureHashAlgorithm hashAlgorithm)
        {
            HashAlgorithm hashAlgorithm2 = hashAlgorithm.CreateAlgorithm();

            byte[] array = hashAlgorithm2.ComputeHash(data);
            CngNative.BCRYPT_PKCS1_PADDING_INFO bCRYPT_PKCS1_PADDING_INFO = default(CngNative.BCRYPT_PKCS1_PADDING_INFO);
            bCRYPT_PKCS1_PADDING_INFO.pszAlgId = hashAlgorithm.Name;
            int num;

            CngNative.ErrorCode status = CngNative.NCryptSignHashPkcs1(base.KeyHandle, ref bCRYPT_PKCS1_PADDING_INFO, array, array.Length, null, 0, out num, CngNative.AsymmetricPaddingMode.Pkcs1);
            CngNative.VerifyStatus(status);
            byte[] array2 = new byte[num];
            status = CngNative.NCryptSignHashPkcs1(base.KeyHandle, ref bCRYPT_PKCS1_PADDING_INFO, array, array.Length, array2, array2.Length, out num, CngNative.AsymmetricPaddingMode.Pkcs1);
            CngNative.VerifyStatus(status);
            return(array2);
        }
        internal static SafeCryptKeyHandle GetCryptKeyHandleFromCertificatePrivateKey(X509Certificate2 cert)
        {
            SafeCryptKeyHandle result = new SafeCryptKeyHandle();
            uint num  = 0u;
            bool flag = false;

            if (!CngNative.CryptAcquireCertificatePrivateKey(cert.Handle, 262208u, IntPtr.Zero, ref result, ref num, ref flag))
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                throw new Win32Exception(lastWin32Error);
            }
            if (!flag || num != 4294967295u)
            {
                throw new InvalidOperationException();
            }
            return(result);
        }
        internal static string NCryptGetPropertyString(SafeCryptKeyHandle hObject, string propertyName)
        {
            int num = 0;

            CngNative.ErrorCode errorCode = CngNative.NCryptGetProperty(hObject, propertyName, null, 0, out num, 0);
            if (errorCode != CngNative.ErrorCode.Success)
            {
                throw new Win32Exception((int)errorCode);
            }
            byte[] array = new byte[num];
            errorCode = CngNative.NCryptGetProperty(hObject, propertyName, array, array.Length, out num, 0);
            if (errorCode != CngNative.ErrorCode.Success)
            {
                throw new Win32Exception((int)errorCode);
            }
            string arg_4E_0 = Encoding.Unicode.GetString(array, 0, num);

            char[] trimChars = new char[1];
            return(arg_4E_0.Trim(trimChars));
        }
Ejemplo n.º 7
0
 protected override bool ReleaseHandle()
 {
     return(CngNative.NCryptFreeObject(this.handle) == CngNative.ErrorCode.Success);
 }