Exemple #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));
        }
        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));
        }
Exemple #5
0
 internal SigningKey(SafeCryptKeyHandle keyHandle, X509Certificate2 certificate)
 {
     this.KeyHandle             = keyHandle;
     this.Certificate           = certificate;
     this.CertificateThumbprint = SigningKey.StringToByteArray(this.Certificate.Thumbprint);
 }
 internal RsaSigningKey(SafeCryptKeyHandle keyHandle, X509Certificate2 certificate) : base(keyHandle, certificate)
 {
 }
 internal static extern CngNative.ErrorCode NCryptSignHashPkcs1(SafeCryptKeyHandle hKey, [In] ref CngNative.BCRYPT_PKCS1_PADDING_INFO pPaddingInfo, [MarshalAs(UnmanagedType.LPArray)][In] byte[] pbHashValue, int cbHashValue, [MarshalAs(UnmanagedType.LPArray)] byte[] pbSignature, int cbSignature, out int pcbResult, CngNative.AsymmetricPaddingMode dwFlags);
 internal static extern CngNative.ErrorCode NCryptSignHash(SafeCryptKeyHandle hKey, IntPtr pPaddingInfo, [MarshalAs(UnmanagedType.LPArray)] byte[] pbHashValue, int cbHashValue, [MarshalAs(UnmanagedType.LPArray)] byte[] pbSignature, int cbSignature, out int pcbResult, int dwFlags);
 internal static extern CngNative.ErrorCode NCryptGetProperty(SafeCryptKeyHandle hObject, string pszProperty, [MarshalAs(UnmanagedType.LPArray)][Out] byte[] pbOutput, int cbOutput, out int pcbResult, int dwFlags);
 internal static extern bool CryptAcquireCertificatePrivateKey([In] IntPtr pCertContext, [In] uint dwFlags, [In] IntPtr pvReserved, [In][Out] ref SafeCryptKeyHandle phKeyHandle, [In][Out] ref uint pdwKeySpec, [In][Out] ref bool pfCallerFreeProv);