Skip to content

CertificateRequest.CreateSelfSigned with ECDSA with invalid key usage #42751

@vcsjones

Description

@vcsjones

If you attempt to create a self signed certificate with CertificateRequest with an ECDsa key and a key usage that makes the certificate ineligible for ECDSA usage, it fails with a less-than-clear exception.

using ECDsa ecdsa = ECDsa.Create();
CertificateRequest req = new CertificateRequest("CN=who", ecdsa, HashAlgorithmName.SHA256);
req.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.KeyAgreement, true));
req.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(42));

Produces:

Unhandled exception. System.ArgumentException: The provided key does not match the public key algorithm for this certificate.
   at System.Security.Cryptography.X509Certificates.ECDsaCertificateExtensions.CopyWithPrivateKey(X509Certificate2 certificate, ECDsa privateKey)
   at System.Security.Cryptography.X509Certificates.CertificateRequest.CreateSelfSigned(DateTimeOffset notBefore, DateTimeOffset notAfter)

We should validate the key usages and key type in CreateSelfSigned and give a better exception.

Reproduced in Core 3.1 and 5.0 RC1.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions