Skip to content

X509Certificate2 fails to import ECDSA private key from Pkcs12 on Linux #27130

Closed
dotnet/corefx
#42226

Description

@alfredr

I believe this is a bug. The following snippet asserts "true/true" on Windows and "false/false" on Linux.

var ecDsa = ECDsa.Create(ECCurve.NamedCurves.nistP521);
var x509 = new CertificateRequest(
    "CN=foo", ecDsa, HashAlgorithmName.SHA512).CreateSelfSigned(
        DateTime.UtcNow.Add(TimeSpan.FromMinutes(-1)),
        DateTime.UtcNow.Add(TimeSpan.FromDays(365)));

var pfxBytes = x509.Export(X509ContentType.Pkcs12, "password");
var cert2 = new X509Certificate2(pfxBytes, "password");
var privKey = cert2.GetECDsaPrivateKey();

Console.WriteLine("cert2.HasPrivateKey: {0}", cert2.HasPrivateKey);
Console.WriteLine("privKey != null: {0}", privKey != null);

cc: @bartonjs

Activity

  1. bartonjs commented on Aug 15, 2018

    @bartonjs
    Member

    Hm. I can reproduce it, but I don't yet understand it.

    Using the Pkcs12Info class in master, I see that the PFX blob does, in fact, contain the key and the certificate. But PKCS12_parse just didn't say they were matched. Pretty weird since PKCS12_create is what made it.

    New contents Confidentiality=Password
      New bag: 1.2.840.113549.1.12.10.1.3
        New attr (1.2.840.113549.1.9.21): 04148E3102431BAAB84703D0DFDB5941BD88A205EDFD
    New contents Confidentiality=None
      New bag: 1.2.840.113549.1.12.10.1.2
        New attr (1.2.840.113549.1.9.21): 04148E3102431BAAB84703D0DFDB5941BD88A205EDFD
    

    So that was a shrouded key bag with a key identifier and a certificate with the same key identifier.

  2. bartonjs commented on Jun 12, 2019

    @bartonjs
    Member

    It's possible, with the new Pkcs12Info class and ImportPkcs8PrivateKey/ImportEncryptedPkcs8PrivateKey to overcome this manually.

    I was hoping to replace our PFX code with a manual importer for the 3.0 release, but we've just run out of time. Hopefully this was just a bug in OpenSSL and they've fixed it (particularly since we've gained support for OpenSSL 1.1.x this release (and backported to 2.1/2.2 after this issue was opened)).

  3. bartonjs commented on Jun 20, 2019

    @bartonjs
    Member

    Some good news: This does look to be an OpenSSL bug that is fixed in the 1.1 line. On current builds of .NET Core 2.x OpenSSL 1.1.x can be preferentially loaded via setting the environment variable CLR_OPENSSL_VERSION_OVERRIDE to 1.1 (assuming you have libssl.so.1.1/libcrypto.so.1.1 in your library path)

  4. mletterle commented on Jul 4, 2019

    @mletterle
    Contributor

    @bartonjs said: I was hoping to replace our PFX code with a manual importer for the 3.0 release

    This might not be the appropriate venue, but I'd like to hear more about this. I'm working on porting an old .NET 2.0 web service that utilized certificates in the windows store to dotnet core on Linux and had to write my own pfx importer, I was surprised that there was both no official functionality nor anything community based that I could find.

  5. bartonjs commented on Jul 5, 2019

    @bartonjs
    Member

    @mletterle In .NET Core 3.0 there's a type, Pkcs12Info that you can use to go through the contents of a PFX. (And Pkcs12Builder to make new ones). They're both in the System.Security.Cryptography.Pkcs package.

  6. transferred this issue fromdotnet/corefxon Jan 31, 2020
  7. added this to the 5.0 milestone on Jan 31, 2020
  8. ghost locked as resolved and limited conversation to collaborators on Dec 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Securitybugos-linux

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions