Skip to content

Commit

Permalink
[release/8.0-staging] Ensure proper cleanup of key files when not per…
Browse files Browse the repository at this point in the history
…sisting 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.
  • Loading branch information
github-actions[bot] authored Nov 25, 2024
1 parent 36320b3 commit f569844
Show file tree
Hide file tree
Showing 3 changed files with 402 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 @@ -495,7 +495,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 f569844

Please sign in to comment.