-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add support for importing PKCS7 files as X509Certificate2Collections. #2912
Conversation
|
@dotnet-bot test this please (either GitHub or mac #2 is acting up) |
|
@dotnet-bot test this please (and if you ask it nicely, I bet the Windows crypto intermittent failure will behave; @bartonjs should really figure out what's going on there...) |
|
@dotnet-bot test this please |
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.
Why is the SetHandle necessary?
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.
Or, maybe I've misunderstood what "Shared" means here. I was assuming it meant that multiple consumers could be holding on to the same instance, and none of them should free/dispose anything, so we make ReleaseHandle a nop. Not the case?
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.
We have a SafeHandle tracking the PKCS#7 object. Then someone wants to make use of p7->d.signed->certs. We shouldn't free that value when we're done with it, because it may either cause problems with freeing the PKCS#7 later, or we merely have blocked ourselves out of using the data any longer.
So this is shared with another safehandle.
Having written out the explanation, I'm wondering if this SafeHandle needs a field reference to the owning SafeHandle (to ensure it's lifetime WRT finalization), have IsInvalid take that handle's IsInvalid into account, and ReleaseHandle just set that field to null.
Or maybe just downgrade it to IntPtr.
Since I've clearly caused confusion, I'm very open to feedback on making it easier to follow (and if it's just a comment that's certainly doable, too).
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.
Thanks for the explanation.
I'm wondering if this SafeHandle needs a field reference to the owning SafeHandle (to ensure it's lifetime WRT finalization), have IsInvalid take that handle's IsInvalid into account, and ReleaseHandle just set that field to null.
I'm actually not sure what the best practice is for SafeHandles that wrap portions of other SafeHandles. @jkotas? Maybe the creation of the "child" handle AddRef's the parent and its ReleaseHandle ReleaseRef's the parent?
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.
Maybe the creation of the "child" handle AddRef's the parent and its ReleaseHandle ReleaseRef's the parent?
Yes, this is the right patern to use for safe handles that point to container members, and where the container has to be kept alive for as long as the container member is alive.
(Windows crypto and networking APIs tend to have this problem too - I believe that you will find this in desktop reference sources if you look around.)
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 found an example in SafeCredentialReference; then added some Interlocked.Increment and Interlocked.Decrement and Console.WriteLines to let me test this out:
Starting: System.Security.Cryptography.X509Certificates.Tests
There is/are 1 active SafePkcs7Handle(s)
There is/are 0 remaining SafePkcs7Handle(s)
There is/are 1 active SafePkcs7Handle(s)
There is/are 0 remaining SafePkcs7Handle(s)
There is/are 1 active SafePkcs7Handle(s)
There is/are 0 remaining SafePkcs7Handle(s)
There is/are 1 active SafePkcs7Handle(s)
There is/are 0 remaining SafePkcs7Handle(s)
There is/are 1 active SafePkcs7Handle(s)
There is/are 0 remaining SafePkcs7Handle(s)
There is/are 1 active SafePkcs7Handle(s)
There is/are 0 remaining SafePkcs7Handle(s)
There is/are 1 active SafePkcs7Handle(s)
There is/are 0 remaining SafePkcs7Handle(s)
There is/are 1 active SafePkcs7Handle(s)
There is/are 0 remaining SafePkcs7Handle(s)
Finished: System.Security.Cryptography.X509Certificates.Tests
So things work the way that I expected them to. Thanks @jkotas.
|
Some minor questions, but LGTM. |
|
CI seems to be consistently failing in the TestPrivateKey test: |
|
@dotnet-bot test this please |
|
I've never seen this particular error before: So it still hasn't quite made it to "run the tests on a machine other than mine". |
|
@dotnet-bot test this please (fail things all you like, I just want to see the linux results) |
|
@bartonjs I saw that on OpenSuSE...let me poke around a bit. |
|
New commits, for your viewing pleasure. I'll squash when people seem happy with the changes. |
|
@dotnet-bot test this please (I pushed new commits while you were napping) |
|
LGTM |
Several tests were added for X509Certificate2Collection Import, particularly regarding PKCS#7 content, and the new and existing tests were extracted to CollectionImportTests. A few tests were added which have no file contents yet, issue 2635 tracks getting an updated testdata package to test reading the files by name. The tests were verified manually, and in general have their file contents included in the TestData class anyways. Reading a PKCS#7 blob from X509Certificate2 will throw a CryptographicException whether or not the file is signed, issue 2910 tracks making it follow the Windows/MSDN behavior on the success path as well (there is no test asset available currently to see the success path on Windows). All PKCS file structure interpretation was moved to PkcsFormatReader, and the functions were consolidated across the matrix of [File vs Blob] x [Collection vs Single]. This should prevent the behaviors from getting out of sync. X509Certificate2Collection.Import now also works for X509-DER and X509-PEM inputs, matching the Windows behavior.
Add support for importing PKCS7 files as X509Certificate2Collections.
Add support for importing PKCS7 files as X509Certificate2Collections. Commit migrated from dotnet/corefx@36969f1
Several tests were added for X509Certificate2Collection Import, particularly regarding PKCS#7 content, and the new and existing tests were extracted to CollectionImportTests.
A few tests were added which have no file contents yet, issue 2635 tracks getting an updated testdata package to test reading the files by name. The tests were verified manually, and in general have their file contents included in the TestData class anyways.
Reading a PKCS#7 blob from X509Certificate2 will throw a CryptographicException whether or not the file is signed, issue 2910 tracks making it follow the Windows/MSDN behavior on the success path as well (there is no test asset available currently to see the success path on Windows).
All PKCS file structure interpretation was moved to PkcsFormatReader, and the functions were consolidated across the matrix of [File vs Blob] x [Collection vs Single]. This should prevent the behaviors from getting out of sync.
X509Certificate2Collection.Import now also works for X509-DER and X509-PEM inputs, matching the Windows behavior.
Fixes #2849.