Skip to content

Commit

Permalink
[release/9.0-staging] Ensure proper cleanup of key files when not per…
Browse files Browse the repository at this point in the history
…sisting them (#109844)

* Ensure proper cleanup of key files when not persisting them

Code inspection suggested that keys imported into the CNG MachineKey store
from PFXImportCertStore were not getting properly cleaned up.  This change
adds tests that prove that supposition, and then fixes the bug so they pass.

* Support systems that have never had CAPI-DSS

* Review feedback

* Bump keysize to 2048
  * This caused the tests to be too slow, so reuse 6 random keys for all of them
* Remove the random ordering in machine-or-user for defaultkeyset (try both ways)
* Remove incorrect copy/paste comment
* Remove bad nullable annotation

---------

Co-authored-by: Jeremy Barton <[email protected]>
  • Loading branch information
github-actions[bot] and bartonjs authored Nov 26, 2024
1 parent 718c531 commit 2c860a7
Show file tree
Hide file tree
Showing 3 changed files with 541 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ internal static void DeleteKeyContainer(SafeCertContextHandle pCertContext)

string providerName = Marshal.PtrToStringUni((IntPtr)(pProvInfo->pwszProvName))!;
string keyContainerName = Marshal.PtrToStringUni((IntPtr)(pProvInfo->pwszContainerName))!;
CngKeyOpenOptions openOpts = CngKeyOpenOptions.None;

if ((pProvInfo->dwFlags & Interop.Crypt32.CryptAcquireContextFlags.CRYPT_MACHINE_KEYSET) != 0)
{
openOpts = CngKeyOpenOptions.MachineKey;
}

try
{
using (CngKey cngKey = CngKey.Open(keyContainerName, new CngProvider(providerName)))
using (CngKey cngKey = CngKey.Open(keyContainerName, new CngProvider(providerName), openOpts))
{
cngKey.Delete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,8 @@
Link="Common\Interop\Windows\Crypt32\Interop.MsgEncodingType.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
<Compile Include="X509Certificates\InteropTests.Windows.cs" />
<Compile Include="X509Certificates\InteropTests.Windows.cs" />
<Compile Include="X509Certificates\X509FilesystemTests.Windows.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />
Expand Down
Loading

0 comments on commit 2c860a7

Please sign in to comment.