Skip to content

Throw CryptographicException for a mismatched PKCS#8 EC key in X509Certificate2.CreateFromPem - #137862

Merged
vcsjones merged 8 commits into
dotnet:mainfrom
jacobwi:fix-ec-pem-key-mismatch
Nov 24, 2026
Merged

vcsjones merged 8 commits into
dotnet:mainfrom
jacobwi:fix-ec-pem-key-mismatch

Conversation

@jacobwi

@jacobwi jacobwi commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #137859

#122997 (backported to 10.0.5 in #123036) gave PKCS#8 EC keys their own loading path in ExtractKeyFromECPem and ExtractKeyFromEncryptedECPem. That path calls CopyWithPrivateKey, which throws ArgumentException when the key doesn't match the certificate, but the two callers only translate CryptographicException. So CreateFromPem, CreateFromEncryptedPem and their File variants throw ArgumentException for a mismatched EC key in PRIVATE KEY or ENCRYPTED PRIVATE KEY form. The SEC1 path and every other algorithm still go through ExtractKeyFromPem, which wraps it in the documented CryptographicException.

This catches the ArgumentException around the two CopyWithPrivateKey calls in ExtractKeyFromECPrivateKeyInfo and throws the documented CryptographicException, as ExtractKeyFromPem does. The callers' existing catch (CryptographicException) wraps it once more, so for PKCS#8 keys the ArgumentException sits two levels down.

It also adds mismatch tests for EC keys, which the suite only had for RSA:

  • ECDSA certificate with a mismatched SEC1, PKCS#8 and encrypted PKCS#8 key
  • ECDH certificate with a mismatched PKCS#8 and encrypted PKCS#8 key

Each asserts a CryptographicException with the ArgumentException inside it: directly for SEC1, as the RSA tests do, and one CryptographicException further down for PKCS#8.

Since the change that introduced this shipped in 10.0.5, this probably wants a release/10.0 backport.

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
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 18, 2026
@azure-pipelines

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
See info in area-owners.md if you want to be subscribed.

@jacobwi

jacobwi commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

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.
jacobwi and others added 3 commits November 18, 2026 17:18
CopyWithECPrivateKey runs outside the callers' catch (CryptographicException),
so a CryptographicException from CopyWithPrivateKey lost the
NoOrMismatchedPemKey wrapping it had before. Wrap it there as well.
@vcsjones

Copy link
Copy Markdown
Member

@jacobwi please avoid merging main into your branch as that is causing CI churn. Your last CI run was actually green. We'll let you know if there are things in CI that need to be addressed.

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.
@vcsjones
vcsjones merged commit cc72eec into dotnet:main Nov 24, 2026
67 of 70 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Security community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

X509Certificate2.CreateFromPem throws ArgumentException for a mismatched PKCS#8 EC key (regression in .NET 10)

3 participants