public static DSA GetDSAPrivateKey(this X509Certificate2 certificate) { if (certificate == null) { throw new ArgumentNullException("certificate"); } if (!certificate.HasPrivateKey || !IsDSA(certificate)) { return(null); } CngKeyHandleOpenOptions openOptions; using (SafeCertContextHandle certificateContext = X509Native.GetCertificateContext(certificate)) using (SafeNCryptKeyHandle privateKeyHandle = X509Native.TryAcquireCngPrivateKey(certificateContext, out openOptions)) { if (privateKeyHandle == null) { // fall back to CAPI if we cannot acquire the key using CNG. DSACryptoServiceProvider dsaCsp = (DSACryptoServiceProvider)certificate.PrivateKey; CspParameters cspParameters = CopyCspParameters(dsaCsp); DSACryptoServiceProvider clone = new DSACryptoServiceProvider(cspParameters); return(clone); } CngKey key = CngKey.Open(privateKeyHandle, openOptions); return(new DSACng(key)); } }
public static RSA GetRSAPrivateKey(this X509Certificate2 certificate) { if (certificate == null) { throw new ArgumentNullException("certificate"); } if (!certificate.HasPrivateKey || !IsRSA(certificate)) { return(null); } using (SafeCertContextHandle certificateContext = X509Native.GetCertificateContext(certificate)) using (SafeNCryptKeyHandle privateKeyHandle = X509Native.TryAcquireCngPrivateKey(certificateContext)) { if (privateKeyHandle == null) { if (LocalAppContextSwitches.DontReliablyClonePrivateKey) { return((RSA)certificate.PrivateKey); } // fall back to CAPI if we cannot acquire the key using CNG. RSACryptoServiceProvider rsaCsp = (RSACryptoServiceProvider)certificate.PrivateKey; CspParameters cspParameters = DSACertificateExtensions.CopyCspParameters(rsaCsp); RSACryptoServiceProvider clone = new RSACryptoServiceProvider(cspParameters); return(clone); } CngKey key = CngKey.Open(privateKeyHandle, CngKeyHandleOpenOptions.None); return(new RSACng(key)); } }
public static RSA GetRSAPrivateKey(this X509Certificate2 certificate) { if (certificate == null) { throw new ArgumentNullException("certificate"); } if (!certificate.HasPrivateKey || !IsRSA(certificate)) { return(null); } using (SafeCertContextHandle certificateContext = X509Native.GetCertificateContext(certificate)) using (SafeNCryptKeyHandle privateKeyHandle = X509Native.TryAcquireCngPrivateKey(certificateContext)) { if (privateKeyHandle == null) { // fall back to CAPI if we cannot acquire the key using CNG. return((RSA)certificate.PrivateKey); } CngKey key = CngKey.Open(privateKeyHandle, CngKeyHandleOpenOptions.None); return(new RSACng(key)); } }
public static ECDsa GetECDsaPrivateKey(this X509Certificate2 certificate) { if (certificate == null) { throw new ArgumentNullException("certificate"); } //Check cert for private key and confirm it is ECDSA cert if (!certificate.HasPrivateKey || !IsECDsa(certificate)) { return(null); } using (SafeCertContextHandle certificateContext = X509Native.GetCertificateContext(certificate)) using (SafeNCryptKeyHandle privateKeyHandle = X509Native.TryAcquireCngPrivateKey(certificateContext)) { CngKey key = CngKey.Open(privateKeyHandle, CngKeyHandleOpenOptions.None); return(new ECDsaCng(key)); } }