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
Sample code:
The
outputis 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
privateCertHandleandprivateCertKeyHandle, which cause it to skip the entry with the private key.[EDIT] Add C# syntax highlighting by @karelz