Skip to content

On Linux, cannot create PFX with private key if has more than single cert in collection #24554

Closed

Description

@ayende

Sample code:

var collection = new X509Certificate2Collection();
collection.Add(certificateWithPrivateKey);
collection.Add(anotherCertWithoutPrivateKey);

var output = collection.Export(X509ContentType.Pkcs12);

The output is a pfx value that has a single certificate in place, the second one, without the private key.

I tracked it down to this piece of code:
https://github.com/dotnet/corefx/blob/master/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/ExportProvider.cs#L75

In particular, this loop:

https://github.com/dotnet/corefx/blob/d7dd6a4b03a7c3bb03379e03ff9dbd656913cc85/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/ExportProvider.cs#L101-L126

Looking at the code, I think that the code is missing setting of privateCertHandle and privateCertKeyHandle, which cause it to skip the entry with the private key.

[EDIT] Add C# syntax highlighting by @karelz

Activity

  1. ayende commented on Jan 3, 2018

    @ayende
    ContributorAuthor

    Note that the same code on Windows generates the PFX properly

  2. danmoseley commented on Jan 3, 2018

    @danmoseley
    Contributor

    Do you want to offer a PR + test @ayende ?

  3. ayende commented on Jan 3, 2018

    @ayende
    ContributorAuthor

    @danmosemsft Yes, I can do that.
    One thing that I wonder, there is the GC.KeepAlive call there, which is strange.
    The value that is there is already kept alive because is is on _certs, no? Am I missing something?

  4. danmoseley commented on Jan 3, 2018

    @danmoseley
    Contributor

    @ayende it looks like from 1dd166f comment that it was added for consistency/defensively.

  5. ayende commented on Jan 3, 2018

    @ayende
    ContributorAuthor

    Okay, some of them looks legit, if the GC is really active, some are probably not relevant since the instance is holding this, I'll match this style, just in case.

  6. bartonjs commented on Jan 25, 2018

    @bartonjs
    Member
  7. transferred this issue fromdotnet/corefxon Jan 31, 2020
  8. added this to the 2.1.0 milestone on Jan 31, 2020
  9. ghost locked as resolved and limited conversation to collaborators on Dec 19, 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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions