Skip to content

X509Certificate2.CopyWithPrivateKey breaks with PKCS.11 OpenSSL Providers and ECDSA #133171

Open

Description

@wjohnstonsolventum

Description

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.

Reproduction Steps

https://github.com/wjohnstonsolventum/dotnet-openssl-ecdsa-bug/blob/main/HsmPoc/Program.cs

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.

Other information

No response

Activity

  1. added
    untriaged
    on Sep 3, 2026
  2. dotnet-policy-service commented on Sep 3, 2026

    @dotnet-policy-service
    Contributor

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

  3. vcsjones commented on Sep 3, 2026

    @vcsjones
    Member

    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?

  4. vcsjones commented on Sep 3, 2026

    @vcsjones
    Member

    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.

  5. wjohnstonsolventum commented on Sep 3, 2026

    @wjohnstonsolventum
    Author

    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.

    Thanks for looking into this!

  6. vcsjones commented on Sep 3, 2026

    @vcsjones
    Member

    Okay, so the general problem is this doesn't work seem to work.

    Console.WriteLine("X: " + Convert.ToHexString(ecdsa.ExportParameters(false).Q.X));

    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.

  7. vcsjones commented on Sep 3, 2026

    @vcsjones
    Member

    Okay so the softhsm2 project does not support exporting the public key via OSSL_PKEY_PARAM_PUB_KEY. This is what .NET 11 uses here

    if (!EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PUB_KEY, NULL, 0, &pubKeyLen))
    goto error;

    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.

  8. wjohnstonsolventum commented on Sep 3, 2026

    @wjohnstonsolventum
    Author

    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.

  9. self-assigned this
    on Sep 3, 2026
  10. added this to the 11.0.0 milestone on Sep 4, 2026
  11. removed
    untriaged
    on Sep 4, 2026
  12. added theissue type on Sep 4, 2026
  13. vcsjones commented on Sep 4, 2026

    @vcsjones
    Member

    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.

  14. wjohnstonsolventum commented on Sep 4, 2026

    @wjohnstonsolventum
    Author

    This is awesome, thanks!

  15. jeffhandley commented on Sep 9, 2026

    @jeffhandley
    Member

    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.

  16. modified the milestones: 11.0.0, 12.0.0 on Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions