Fix software ksp provider cert deletion on windows#1036
Open
darkfronza wants to merge 2 commits into
Open
Conversation
Switch github.com/smallstep/go-attestation from v0.4.9 to a pre-release pseudo-version that includes the upstream changes required for the per-key key-scope option (Windows machine vs user key store) added in this branch. Change-Type: build Release-Note: no Audience: developer Impact: low Breaking: false Co-Authored-By: Claude <noreply@anthropic.com>
…IKMS When the Windows agent renews a non-attested endpoint cert, the cleanup path goes through tpmkms.LoadCertificateChain -> CAPIKMS bound to the Microsoft Platform Crypto Provider, but the actual key lives in the Microsoft Software Key Storage Provider. The pre-existing "containerName" branch in CAPIKMS opened the CNG key via the bound provider to derive a SubjectKeyID; that NCryptOpenKey can't open a Software-KSP container from a PCP handle, so the lookup failed and the old cert was never deleted. Each renewal then accumulated another cert bound to the same container. Three changes: - capi: in the containerName branch of getCertContext and DeleteCertificate, fall back to enumerating the store and matching by CERT_KEY_PROV_INFO container name when the key can't be opened. The property is set by CryptFindCertificateKeyProvInfo at store time regardless of provider, so the fallback works for any CNG-backed cert. Fix the long-broken CRYPT_KEY_PROV_INFO struct layout and the cryptFindCertificateKeyContainerName helper, which previously always returned "". - capi: stop the LoadCertificateChain walk when child.AuthorityKeyId is empty. Building "key-id=" with no value made getCertContext reject the URI; the right behavior for self-signed certs (and CAs that don't emit AKI) is to return the partial chain we have. - tpmkms: in loadCertificateChainFromWindowsCertificateStore and deleteCertificateFromWindowsCertificateStore, when GetPublicKey returns apiv1.NotFoundError (the key isn't in the TPM), hand CAPI the URI with "key=<container>" instead of "key-id=<SKI>". TPM-resident keys keep the existing SKI-indexed path. Adds Windows-only tests under kms/capi and kms/tpmkms that cover the fallback load+delete paths, the not-found shape, the indexed-path regression guard, and round-tripping the container-name property reader. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(capi,tpmkms): find/delete Software-KSP certs from a PCP-bound CAPIKMS
When the Windows agent renews a non-attested endpoint cert, the cleanup
path goes through tpmkms.LoadCertificateChain -> CAPIKMS bound to the
Microsoft Platform Crypto Provider, but the actual key lives in the
Microsoft Software Key Storage Provider. The pre-existing
"containerName" branch in CAPIKMS opened the CNG key via the bound
provider to derive a SubjectKeyID; that NCryptOpenKey can't open a
Software-KSP container from a PCP handle, so the lookup failed and the
old cert was never deleted. Each renewal then accumulated another cert
bound to the same container.
Three changes:
capi: in the containerName branch of getCertContext and
DeleteCertificate, fall back to enumerating the store and matching by
CERT_KEY_PROV_INFO container name when the key can't be opened. The
property is set by CryptFindCertificateKeyProvInfo at store time
regardless of provider, so the fallback works for any CNG-backed cert.
Fix the long-broken CRYPT_KEY_PROV_INFO struct layout and the
cryptFindCertificateKeyContainerName helper, which previously always
returned "".
capi: stop the LoadCertificateChain walk when child.AuthorityKeyId is
empty. Building "key-id=" with no value made getCertContext reject the
URI; the right behavior for self-signed certs (and CAs that don't emit
AKI) is to return the partial chain we have.
tpmkms: in loadCertificateChainFromWindowsCertificateStore and
deleteCertificateFromWindowsCertificateStore, when GetPublicKey
returns apiv1.NotFoundError (the key isn't in the TPM), hand CAPI the
URI with "key=" instead of "key-id=". TPM-resident
keys keep the existing SKI-indexed path.
Adds Windows-only tests under kms/capi and kms/tpmkms that cover the
fallback load+delete paths, the not-found shape, the indexed-path
regression guard, and round-tripping the container-name property reader.
Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com