Skip to content

Commit 2c860a7

Browse files
[release/9.0-staging] Ensure proper cleanup of key files when not persisting 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 <jbarton@microsoft.com>
1 parent 718c531 commit 2c860a7

File tree

3 files changed

+541
-2
lines changed

3 files changed

+541
-2
lines changed

src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeCertContextHandleWithKeyContainerDeletion.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ internal static void DeleteKeyContainer(SafeCertContextHandle pCertContext)
5050

5151
string providerName = Marshal.PtrToStringUni((IntPtr)(pProvInfo->pwszProvName))!;
5252
string keyContainerName = Marshal.PtrToStringUni((IntPtr)(pProvInfo->pwszContainerName))!;
53+
CngKeyOpenOptions openOpts = CngKeyOpenOptions.None;
54+
55+
if ((pProvInfo->dwFlags & Interop.Crypt32.CryptAcquireContextFlags.CRYPT_MACHINE_KEYSET) != 0)
56+
{
57+
openOpts = CngKeyOpenOptions.MachineKey;
58+
}
5359

5460
try
5561
{
56-
using (CngKey cngKey = CngKey.Open(keyContainerName, new CngProvider(providerName)))
62+
using (CngKey cngKey = CngKey.Open(keyContainerName, new CngProvider(providerName), openOpts))
5763
{
5864
cngKey.Delete();
5965
}

src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@
513513
Link="Common\Interop\Windows\Crypt32\Interop.MsgEncodingType.cs" />
514514
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
515515
Link="Common\Interop\Windows\Interop.Libraries.cs" />
516-
<Compile Include="X509Certificates\InteropTests.Windows.cs" />
516+
<Compile Include="X509Certificates\InteropTests.Windows.cs" />
517+
<Compile Include="X509Certificates\X509FilesystemTests.Windows.cs" />
517518
</ItemGroup>
518519
<ItemGroup>
519520
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />

0 commit comments

Comments
 (0)