Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ECDsa support in X509SecurityKey and JsonWebKeyConverter.ConvertFromX509SecurityKey #2377

Open
wants to merge 21 commits into
base: dev
Choose a base branch
from

Conversation

joegoldman2
Copy link
Contributor

@joegoldman2 joegoldman2 commented Oct 21, 2023

Fixes #1943.
Fixes #2217.

@jaanclaeys
Copy link

I applaud the fix, but ...

I think we need a better solution going forward in the future. I am pretty sure we will see more algorithms in the future on certificates (Dilithium e.g.)

Problem is probably here with X509Certificate2 and the extension methods. Trying each private key method might be a bit of a performance hit.
Having an informed constructor might help here ? Or even different subclasses X509RsaSecurityKey / X509EcdsaSecurityKey, or fix this in X509Certificate2 itself ?

@joegoldman2
Copy link
Contributor Author

I don't think that creating sub-classes is the right solution. In many cases, the algorithm of the key in the certificate is not known in advance and X509SecurityKey is helping to abstract it.
If the algorithm is known, you can simply do:

X509Certificate2 certificate = ...;
ECDsa ecdsa = certificate.GetECDsaPrivateKey();
ECDsaSecurityKey key = new ECDsaSecurityKey(ecdsa);

And no need for any new X509EcdsaSecurityKey or equivalent.

Otherwise, I agree that the current fix isn't as future-proof as we'd like, but I think it's a good first fix before perhaps a deeper refactoring.

@joegoldman2 joegoldman2 requested a review from a team as a code owner April 19, 2024 15:41
@keegan-caruso
Copy link
Contributor

Similar comment to #2379, can we add tests for this?

@joegoldman2
Copy link
Contributor Author

Similar comment to #2379, can we add tests for this?

Can you please check the initial comment of this PR?

@keegan-caruso
Copy link
Contributor

I can commit an EC cert if that is what you would like to do.

I think in-memory certs would also be fine here if we can use them. One less thing to suppress for test data.

@joegoldman2
Copy link
Contributor Author

I generated a self-signed cert this way:

$cert = New-SelfSignedCertificate -DnsName "CN=KeyStoreTestCertificate" -KeyAlgorithm ECDSA_nistP256 -KeyExportPolicy Exportable -CertStoreLocation Cert:\CurrentUser\My
$cert | Export-PfxCertificate -FilePath "Certificate.pfx" -Password (ConvertTo-SecureString -String "abcd" -Force -AsPlainText)
$pfxBytes = [System.IO.File]::ReadAllBytes("Certificate.pfx")
$base64Cert = [System.Convert]::ToBase64String($pfxBytes)

And added a unit test.

@joegoldman2 joegoldman2 changed the title Add ECDsa support in X509SecurityKey Add ECDsa support in X509SecurityKey and JsonWebKeyConverter.ConvertFromX509SecurityKey Jul 2, 2024
@joegoldman2
Copy link
Contributor Author

The unit tests are not passing yet. I'm running into a issue that I'm not sure to understand and I opened a discussion in the runtime repo: dotnet/runtime#104294.

@joegoldman2
Copy link
Contributor Author

I updated the unit tests not to use the self-signed certificate generated by me but an existing EC key + CertificateRequest to generate the certificate in memory. All unit tests are passing now.

The parameter representAsRsaKey exposed by JsonWebKeyConverter.ConvertFromX509SecurityKey should be renamed as now ECDsa is supported. I'm open to suggestions.

Otherwise, this PR is ready to be reviewed.

@brentschmaltz
Copy link
Member

@jennyf19 marked as 8.

@brentschmaltz brentschmaltz added the IdentityModel8x Future breaking issues/features for IdentityModel 8x label Jul 8, 2024
@jennyf19
Copy link
Collaborator

jennyf19 commented Aug 6, 2024

@keegan-caruso to take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IdentityModel8x Future breaking issues/features for IdentityModel 8x
Projects
None yet
5 participants