fleet-enrol: self-heal a stale non-ECC key at the enrol handle

Bench boot failed with "fleet-enrol: ensure key at 0x81010001: key at handle
is not ECC: did not contain eccDetail (selector value was 1)": a real host had
an RSA key persisted at 0x81010001 (left by an older tpm2-tools/openssl
provisioning path), and EnsureKey fatally errored trying to read it as our ECC
signing key — so enrolment died before the CSR/POST and the host fell through
to a normal boot (no fingerprint, nothing at the broker).

EnsureKey now evicts a wrong-type/unusable key at the handle and creates a
fresh ECC key. Safe on the enrol path: it only runs on an empty-NV host, so no
issued cert references the old key. A valid ECC key is still reused (idempotent
retried enrol).

Regression: simtest TestEnsureKeyEvictsStaleNonECC pre-plants an RSA key and
asserts EnsureKey recreates a usable ECC signer (passes against the go-tpm
simulator).
This commit is contained in:
Audiobuzz
2026-07-02 11:43:03 +00:00
parent 1e1265b5a0
commit 63cd4049ff
2 changed files with 81 additions and 2 deletions
+17 -2
View File
@@ -108,10 +108,25 @@ func signerFromPub(tpm transport.TPM, handle tpm2.TPMHandle, name tpm2.TPM2BName
// ensureKey returns a signer for the persistent key at handle, creating +
// persisting a fresh ECDSA key there if none exists. Returns created=true when
// a new key was generated.
//
// If the handle already holds an object that is NOT our ECC signing key (e.g. an
// RSA key left by an older tpm2-tools/openssl provisioning path — go-tpm reports
// "key at handle is not ECC"), it is treated as STALE and evicted, then a fresh
// ECC key is created. Safe on the enrol path: this only runs on an empty-NV host,
// so no issued cert references the old key. Only a valid ECC key is reused.
func EnsureKey(tpm transport.TPM, handle tpm2.TPMHandle) (signer *Signer, created bool, err error) {
if rp, e := (tpm2.ReadPublic{ObjectHandle: handle}).Execute(tpm); e == nil {
s, e2 := signerFromPub(tpm, handle, rp.Name, rp.OutPublic)
return s, false, e2
if s, e2 := signerFromPub(tpm, handle, rp.Name, rp.OutPublic); e2 == nil {
return s, false, nil // our ECC key is already there — reuse it
}
// Stale/wrong-type key occupying the handle — evict so we can recreate.
if _, e2 := (tpm2.EvictControl{
Auth: tpm2.TPMRHOwner,
ObjectHandle: &tpm2.NamedHandle{Handle: handle, Name: rp.Name},
PersistentHandle: handle,
}).Execute(tpm); e2 != nil {
return nil, false, fmt.Errorf("evict stale (non-ECC) key at handle: %w", e2)
}
}
cp, err := tpm2.CreatePrimary{