-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Description
var cert = X509Certificate2.CreateFromPem(certificate, privateKey);
net 10 on windows: throw exception: System.Security.Cryptography.CryptographicException: Key is not a valid public or private key.
throw by System.Security.Cryptography.ECDiffieHellmanCng.ProcessPkcs8Response
why ECDiffieHellmanCng? should be ECDsa?
when use ExportECPrivateKeyPem export pkcs1 format, it's work fine.
.net 10 on windows can not parse ecdsa pkcs8 private key correctly?
ref code
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace CertDebug;
internal class Program
{
static void Main(string[] args)
{
Create("yourdomain.com", out var certificate, out var privateKey);
Console.WriteLine(certificate);
Console.WriteLine(privateKey);
/*
* on windows
*
* net8: work fine.
* net 10: throw exception: System.Security.Cryptography.CryptographicException: Key is not a valid public or private key.
* throw by System.Security.Cryptography.ECDiffieHellmanCng.ProcessPkcs8Response
* why ECDiffieHellmanCng? should be ECDsa?
*
* when use ExportECPrivateKeyPem, all work fine.
* .net 10 on windows can not parse ecdsa pkcs8 private key correctly?
*
* on mac
* both 2 work fine;
*/
var cert = X509Certificate2.CreateFromPem(certificate, privateKey);
Console.WriteLine(cert.Subject);
}
private static void Create(string commonName, out string certificate, out string privateKey)
{
CertificateRequest req = new(new X500DistinguishedName($"CN={commonName}"), ECDsa.Create(ECCurve.NamedCurves.nistP256), HashAlgorithmName.SHA256);
var cert = req.CreateSelfSigned(new DateTimeOffset(DateTime.Now), new DateTimeOffset(DateTime.Now + TimeSpan.FromDays(365)));
certificate = cert.ExportCertificatePem();
//privateKey = cert.GetECDsaPrivateKey()!.ExportECPrivateKeyPem();
privateKey = cert.GetECDsaPrivateKey()!.ExportPkcs8PrivateKeyPem();
}
}
Reproduction Steps
ref comment in above code.
Expected behavior
ecdsa private key should be parsed correctly.
Actual behavior
exception throw.
Regression?
.net 8 work fine.
only .net 10 on windows throw exception.
Known Workarounds
No response
Configuration
No response
Other information
No response