-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Fixing X509Certificate2Collection.Export on Unix with multiple certs and private key #26152
Conversation
…n a single certificate and one of them has a private key, don't skip it
There are test failures on OSX, see:
This is for the test that test the existing (and correct) behavior, so I think that this was the case before my changed, but there was no test to trigger this behavior. There is also the |
@@ -138,11 +140,17 @@ private byte[] ExportPfx(SafePasswordHandle password) | |||
throw Interop.Crypto.CreateOpenSslCryptographicException(); | |||
} | |||
|
|||
return Interop.Crypto.OpenSslEncode( | |||
var result = Interop.Crypto.OpenSslEncode( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improper use of var
, please specify the type.
Interop.Crypto.GetPkcs12DerSize, | ||
Interop.Crypto.EncodePkcs12, | ||
pkcs12); | ||
|
||
// ensure certs handle isn't finalized while raw handle(s) is in use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammar: Something like "ensure cert handles aren't finalized while the raw handles are in use"
{ | ||
private static X509Certificate2 CreateCert(string name) | ||
{ | ||
var rsa = new RSACryptoServiceProvider(2048); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RSA.Create
. Never RSACryptoServiceProvider
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the new test moves to the other file I'd just continue using the test certificates that the rest of the library does, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the new test moves to the other file
I don't follow what you mean here?
{ | ||
var rsa = new RSACryptoServiceProvider(2048); | ||
var csr = new CertificateRequest("CN=" + name, rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); | ||
var cert = csr.CreateSelfSigned(DateTime.Today.AddDays(-1), DateTime.Today.AddDays(1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improper var (just return the value)
{ | ||
// without private keys | ||
new X509Certificate2(CreateCert("one").Export(X509ContentType.Cert)), | ||
new X509Certificate2(CreateCert("one").Export(X509ContentType.Cert)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using two different names seems better. Also, this test is redundant to CollectionTests::ExportUnrelatedPfx.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that it tests the same thing, in particular, this test is testing the actual fix, and if it was duplicating the other, it wouldn't be needed, because ExportUnrelatedPfx
would already be breaking on Linux.
In particular, ExportUnrelatedPfx
isn't testing certs with private keys.
var col = new X509Certificate2Collection | ||
{ | ||
// without private keys | ||
new X509Certificate2(CreateCert("one").Export(X509ContentType.Cert)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I, personally, prefer reading RawData
than calling Export(Cert). They're the same thing, though.
var col = new X509Certificate2Collection | ||
{ | ||
// without private keys | ||
new X509Certificate2(CreateCert("one").Export(X509ContentType.Cert)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All allocated certificates must be explicitly disposed (so that this test library can be run with finalization detection enabled to see if anything in the X509Certificates library is causing finalization)
} | ||
|
||
[Fact] | ||
public static void CanAddMultipleCertsWithSinglePrivateKey() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add this in CollectionTests.cs, at around line 867.
Assert.Equal(1, col.Cast<X509Certificate2>().Count(x => x.HasPrivateKey)); | ||
Assert.Equal(2, col.Count); | ||
|
||
var buffer = col.Export(X509ContentType.Pfx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improper use of var
, please specify the type.
var buffer = col.Export(X509ContentType.Pfx); | ||
|
||
var newCol = new X509Certificate2Collection(); | ||
newCol.Import(buffer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please import via Cert.Import
and the ImportedCollection disposable class. (See CollectionTests.ExportUnrelatedPfx for an example)
Thanks for jumping on this. Aside from a bad use of
Actually, there is 😄. corefx/src/System.Security.Cryptography.X509Certificates/tests/CollectionTests.cs Lines 735 to 737 in 71ff5e9
|
Okay, updated the PR based on the review, thanks. |
@dotnet-bot Test OSX x64 Debug Build please |
@ayende I pulled this down on a mac, and it looks like there's something different making it fail on macOS. If you want to add |
@bartonjs Done |
@dotnet-bot Test Linux x64 Release Build please |
@dotnet-bot Test Windows x64 Debug Build please (#26349) |
Thanks for taking care of this, @ayende. |
See previous discussion of this bug here: https://github.com/dotnet/corefx/issues/26125
This passes tests on Ubuntu 16.04 machine