Repository navigation
Throw CryptographicException for a mismatched PKCS#8 EC key in X509Certificate2.CreateFromPem - #137862
Merged
Conversation
ExtractKeyFromECPem and ExtractKeyFromEncryptedECPem call CopyWithPrivateKey, which throws ArgumentException when the key doesn't match the certificate, but they only translated CryptographicException. Wrap ArgumentException the same way the generic PEM path does, and add EC key mismatch tests. Fixes dotnet#137859
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security |
Contributor
Author
|
@dotnet-policy-service agree |
vcsjones
reviewed
Nov 18, 2026
ImportECPrivateKeyInfo (was ExtractKeyFromECPrivateKeyInfo) now only imports the key. The callers keep wrapping decode, decrypt and import failures in CryptographicException, and CopyWithECPrivateKey wraps only the ArgumentException from CopyWithPrivateKey, like ExtractKeyFromPem does.
vcsjones
reviewed
Nov 18, 2026
CopyWithECPrivateKey runs outside the callers' catch (CryptographicException), so a CryptographicException from CopyWithPrivateKey lost the NoOrMismatchedPemKey wrapping it had before. Wrap it there as well.
Open
3 tasks
This was referenced Nov 19, 2026
Member
|
@jacobwi please avoid merging |
bartonjs
reviewed
Nov 19, 2026
Put back the original structure of ExtractKeyFromECPrivateKeyInfo and catch the ArgumentException from its two CopyWithPrivateKey calls, as the other algorithms do. The callers' catch (CryptographicException) wraps it once more.
This was referenced Nov 20, 2026
bartonjs
approved these changes
Nov 24, 2026
vcsjones
approved these changes
Nov 24, 2026
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #137859
#122997 (backported to 10.0.5 in #123036) gave PKCS#8 EC keys their own loading path in
ExtractKeyFromECPemandExtractKeyFromEncryptedECPem. That path callsCopyWithPrivateKey, which throwsArgumentExceptionwhen the key doesn't match the certificate, but the two callers only translateCryptographicException. SoCreateFromPem,CreateFromEncryptedPemand theirFilevariants throwArgumentExceptionfor a mismatched EC key inPRIVATE KEYorENCRYPTED PRIVATE KEYform. The SEC1 path and every other algorithm still go throughExtractKeyFromPem, which wraps it in the documentedCryptographicException.This catches the
ArgumentExceptionaround the twoCopyWithPrivateKeycalls inExtractKeyFromECPrivateKeyInfoand throws the documentedCryptographicException, asExtractKeyFromPemdoes. The callers' existingcatch (CryptographicException)wraps it once more, so for PKCS#8 keys theArgumentExceptionsits two levels down.It also adds mismatch tests for EC keys, which the suite only had for RSA:
Each asserts a
CryptographicExceptionwith theArgumentExceptioninside it: directly for SEC1, as the RSA tests do, and oneCryptographicExceptionfurther down for PKCS#8.Since the change that introduced this shipped in 10.0.5, this probably wants a release/10.0 backport.