Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/8.0-staging] Ensure proper cleanup of key files when not persisting them #109845

Merged

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Nov 14, 2024

Backport of #109731 to release/8.0-staging

/cc @bartonjs

Customer Impact

  • Customer reported
  • Found internally

When loading a PKCS#12/PFX without specifying either the PersistKeySet or EphemeralKeySet option, the behavior of the framework is supposed to be to let the key be written to disk, and then remove the key when the cert is disposed/finalized.

When loading a PKCS#12/PFX to a single certificate (new X509Certificate2(pfx, ...) or X509CertificateLoader.LoadPkcs12(...)), key files belonging to any certificates that aren't being returned should also be removed from disk (before the method returns).

In both of these cases, key files written to the CNG machine key store are not being reliably cleaned up. Over time this causes system degradation as the cost of manipulating the directory increases.

Regression

  • Yes
  • No

The CNG cleanup code was added to .NET Core to support UWP, which didn't have machine keys. It just never got revisited.

CNG MachineKey keys do get properly cleaned up on .NET Framework, so there is a regression from NetFX.

Testing

This change adds tests that monitor the state of all known CAPI and CNG key directories and ensure that any files written during the import phase of the test are removed by the end of it.

Risk

Medium-Low.

The new tests ensure the behavior we expect. It is possible that there are a few users who have accidentally depended on the bad cleanup behavior and need to specify the PersistKeySet flag during import, but they are undoubtedly fewer in number than the users who don't want the keys persisted (and there's nothing the "don't want the keys persisted" users can easily do to correct the current bad behavior).

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.
* 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
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@bartonjs bartonjs added the Servicing-consider Issue for next servicing release review label Nov 20, 2024
Copy link
Member

@jeffhandley jeffhandley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @artl93

@jeffhandley jeffhandley added this to the 8.0.x milestone Nov 21, 2024
@bartonjs bartonjs added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Nov 25, 2024
@bartonjs bartonjs merged commit f569844 into release/8.0-staging Nov 25, 2024
110 of 114 checks passed
@bartonjs bartonjs deleted the backport/pr-109731-to-release/8.0-staging branch November 25, 2024 23:42
@github-actions github-actions bot locked and limited conversation to collaborators Dec 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Security Servicing-approved Approved for servicing release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants