Skip to content

Commit

Permalink
Ensure proper cleanup of key files when not persisting them
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bartonjs authored Nov 14, 2024
1 parent 31e28ad commit ed2e02c
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 ed2e02c

Please sign in to comment.