Attempt to load X.509 keys as ECDH keys first#115249
Merged
vcsjones merged 2 commits intodotnet:mainfrom May 3, 2025
Merged
Conversation
Windows CNG EC keys always have a key usage attached to them. ECDH keys can be treated as either ECDH or ECDSA. However, a CNG key with ECDSA usage can only be used as ECDSA. When we import a PEM aggregate with an ECC private key, we should attempt to import it as ECDH first, if the certificate's key usage permits it. This will allow the imported key to act as either ECDH or ECDSA. However, if we attempt to import as ECDSA first, it will succeed however not have the correct key usage, preventing it from being used as ECDH.
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones |
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR ensures that EC private keys in PEM are imported as ECDH when permitted before falling back to ECDsa, and adds tests to verify both unencrypted and encrypted PKCS#8 EC key imports support ECDH and ECDsa.
- Added two new tests covering EC PKCS#8 imports for both ECDH and ECDsa usage in unencrypted and encrypted forms.
- Reordered the
Oids.EcPublicKeycases inX509Certificate2.CreateFromPemandCreateFromEncryptedPemso ECDiffieHellman is attempted prior to ECDsa.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Security.Cryptography/tests/X509Certificates/X509Certificate2PemTests.cs | Added CreateFromPem_EC_Pkcs8_Success and CreateFromEncryptedPem_EC_Pkcs8_Success test methods. |
| src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs | Swapped EC import order: try ECDiffieHellman before ECDsa for PEM key imports. |
Comments suppressed due to low confidence (2)
src/libraries/System.Security.Cryptography/tests/X509Certificates/X509Certificate2PemTests.cs:272
- [nitpick] The test name
CreateFromPem_EC_Pkcs8_Successdoesn't indicate that it covers both ECDH and ECDsa imports; consider renaming it to something likeCreateFromPem_ECKey_EcdhAndEcdsa_Successfor clarity.
public static void CreateFromPem_EC_Pkcs8_Success()
src/libraries/System.Security.Cryptography/tests/X509Certificates/X509Certificate2PemTests.cs:271
- There’s no test covering a certificate restricted to ECDSA usage only; consider adding one to verify that import correctly falls back to ECDsa and does not allow ECDH in that scenario.
[Fact]
bartonjs
reviewed
May 2, 2025
src/libraries/System.Security.Cryptography/tests/X509Certificates/X509Certificate2PemTests.cs
Outdated
Show resolved
Hide resolved
bartonjs
reviewed
May 2, 2025
src/libraries/System.Security.Cryptography/tests/X509Certificates/X509Certificate2PemTests.cs
Outdated
Show resolved
Hide resolved
bartonjs
approved these changes
May 2, 2025
This was referenced May 3, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Windows CNG EC keys always have a key usage attached to them. ECDH keys can be treated as either ECDH or ECDSA. However, a CNG key with ECDSA usage can only be used as ECDSA.
When we import a PEM aggregate with an ECC private key, we should attempt to import it as ECDH first, if the certificate's key usage permits it. This will allow the imported key to act as either ECDH or ECDSA.
However, if we attempt to import as ECDSA first, it will succeed however not have the correct key usage, preventing it from being used as ECDH.
Fixes #115232