You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
X509Certificate2.CopyWithPrivateKey breaks with PKCS.11 OpenSSL Providers and ECDSA #133171
On Linux, we use PKCS.11 through an OpenSSL Engine to interact with an on-device HSM. In trying to switch to OpenSSL providers, it looks like this works with the deprecated OpenSSL engine but not a Provider. RSA works with Providers.
You can clone that codebase and run ./start.sh engine and it'll work, and ./start.sh provider and it'll fail. Based on #109243 (comment) it seems like it should work. If you switch this to RSA, it works fine with both engines and providers.
Expected behavior
This should work with providers too.
Engine output from reproduction steps:
Engine "pkcs11" set.
Workaround for OpenSSL 3.0.13 30 Jan 2024 enabled
Workaround for OpenSSL 3.0.13 30 Jan 2024 enabled
Subject: CN=hsm-poc
Load mode: engine
Has private key: True
Signature valid: True
Actual behavior
Providers:
Engine "pkcs11" set.
Workaround for OpenSSL 3.0.13 30 Jan 2024 enabled
Unhandled exception. System.Security.Cryptography.CryptographicException: Error occurred during a cryptographic operation.
at Interop.Crypto.EvpPKeyGetEcKeyParameters(SafeEvpPKeyHandle key, Boolean includePrivate)
at System.Security.Cryptography.ECOpenSsl.ExportNamedCurveParametersFromEvpPKeyUsingParams(SafeEvpPKeyHandle pkey, String curveName, Boolean includePrivateParameters)
at System.Security.Cryptography.ECOpenSsl.ExportParameters(SafeEvpPKeyHandle pkey, Boolean includePrivateParameters)
at System.Security.Cryptography.X509Certificates.ECDsaCertificateExtensions.CopyWithPrivateKey(X509Certificate2 certificate, ECDsa privateKey)
at Program.<Main>$(String[] args) in /src/HsmPoc/Program.cs:line 29
Regression?
This worked with OpenSSL Engines and works with OpenSSL Providers with RSA. I don't think OpenSSL Providers with ECDSA has ever worked.
Known Workarounds
Use engines or use RSA.
Configuration
$ dotnet --version
10.0.301
I've seen this on AMD64 in Linux Docker on macOS via Rosetta (see reproduction PoC), and in ARM64 on an embedded Linux device.
This only applies to Linux because of OpenSSL. It seems to be architecture independent within that.
For completeness sake, I just pushed a line to main that dumps out the current .Net version and then made a branch called dotnet-11 which tries the preview. And I saw the same, broken behavior with .Net 11 Preview.
Exporting the public key results in a cryptographic exception, which we expect to always work (we want the public key in the certificate to match the public part of the key) since even an HSM / PKCS#11 backed key should be able to answer the question: "What are your public parameters?"
I'm looking to see if this is a problem with how we export the public key.
If useful, I get the same error from a hardware HSM on an ARM device with OpenSSL providers. And the engine works fine, so I'm not sure how that is interacting differently with PKCS.11.
Version scope: the first affected environment documented in this issue uses .NET SDK 10.0.301. The behavior was later reproduced in a .NET 11 preview, so this is not an 11-only regression. Milestone planning is moving to 12.0.0; potential 10.0/11.0 servicing backports can be considered separately.
Activity
dotnet-policy-service commentedon Sep 3, 2026
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
See info in area-owners.md if you want to be subscribed.
vcsjones commentedon Sep 3, 2026
This might work in .NET 11 because of #127190 and #129565. Would you be willing to try a preview of .NET 11 to determine if it works for you?
vcsjones commentedon Sep 3, 2026
Actually I can still reproduce this in .NET 11 with your docker image (which was very helpful thank you!) so I can take a look at this.
wjohnstonsolventum commentedon Sep 3, 2026
For completeness sake, I just pushed a line to main that dumps out the current .Net version and then made a branch called
dotnet-11which tries the preview. And I saw the same, broken behavior with .Net 11 Preview.Thanks for looking into this!
vcsjones commentedon Sep 3, 2026
Okay, so the general problem is this doesn't work seem to work.
Exporting the public key results in a cryptographic exception, which we expect to always work (we want the public key in the certificate to match the public part of the key) since even an HSM / PKCS#11 backed key should be able to answer the question: "What are your public parameters?"
I'm looking to see if this is a problem with how we export the public key.
vcsjones commentedon Sep 3, 2026
Okay so the softhsm2 project does not support exporting the public key via
OSSL_PKEY_PARAM_PUB_KEY. This is what .NET 11 uses hereruntime/src/native/libs/System.Security.Cryptography.Native/pal_ecc_import_export.c
Lines 710 to 711 in 685a287
softhsm supports the following parameters:
https://github.com/openssl-projects/pkcs11-provider/blob/c7a5c8b62a0ff012b16574f01651254ef7e664ee/src/kmgmt/ec.c#L532-L534
I suppose we could query the provider for "what means do you have to export the public key?" and use the right one.
wjohnstonsolventum commentedon Sep 3, 2026
If useful, I get the same error from a hardware HSM on an ARM device with OpenSSL providers. And the engine works fine, so I'm not sure how that is interacting differently with PKCS.11.
vcsjones commentedon Sep 4, 2026
I have a fix for the issue up here. #133205
Once that gets merged for .NET 12, we'll open up pull requests to get it back ported to previous versions.
wjohnstonsolventum commentedon Sep 4, 2026
This is awesome, thanks!
jeffhandley commentedon Sep 9, 2026
Version scope: the first affected environment documented in this issue uses .NET SDK 10.0.301. The behavior was later reproduced in a .NET 11 preview, so this is not an 11-only regression. Milestone planning is moving to 12.0.0; potential 10.0/11.0 servicing backports can be considered separately.
[release/11.0] Fix EC public key export for provider keys (#133254)