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:
@@ -99,3 +99,67 @@ func TestNVWriteAndPresence(t *testing.T) {
|
||||
t.Fatalf("WriteNVCert rewrite: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Regression for the bench failure "key at handle is not ECC ... selector value
|
||||
// was 1": an older provisioning path left an RSA key at 0x81010001, and EnsureKey
|
||||
// fatally failed trying to read it as ECC. It must now evict the stale key and
|
||||
// create a fresh ECC one.
|
||||
func TestEnsureKeyEvictsStaleNonECC(t *testing.T) {
|
||||
tpm := openSim(t)
|
||||
const h = tpm2.TPMHandle(0x81010001)
|
||||
|
||||
// Pre-populate the handle with an RSA key (mimics tpm2-tools/openssl default).
|
||||
rsaTmpl := tpm2.TPMTPublic{
|
||||
Type: tpm2.TPMAlgRSA,
|
||||
NameAlg: tpm2.TPMAlgSHA256,
|
||||
ObjectAttributes: tpm2.TPMAObject{
|
||||
FixedTPM: true, FixedParent: true, SensitiveDataOrigin: true,
|
||||
UserWithAuth: true, SignEncrypt: true, NoDA: true,
|
||||
},
|
||||
Parameters: tpm2.NewTPMUPublicParms(tpm2.TPMAlgRSA, &tpm2.TPMSRSAParms{
|
||||
Symmetric: tpm2.TPMTSymDefObject{Algorithm: tpm2.TPMAlgNull},
|
||||
Scheme: tpm2.TPMTRSAScheme{
|
||||
Scheme: tpm2.TPMAlgRSASSA,
|
||||
Details: tpm2.NewTPMUAsymScheme(tpm2.TPMAlgRSASSA, &tpm2.TPMSSigSchemeRSASSA{
|
||||
HashAlg: tpm2.TPMAlgSHA256,
|
||||
}),
|
||||
},
|
||||
KeyBits: 2048,
|
||||
}),
|
||||
}
|
||||
cp, err := tpm2.CreatePrimary{
|
||||
PrimaryHandle: tpm2.TPMRHOwner, InPublic: tpm2.New2B(rsaTmpl),
|
||||
}.Execute(tpm)
|
||||
if err != nil {
|
||||
t.Fatalf("create RSA primary: %v", err)
|
||||
}
|
||||
if _, err := (tpm2.EvictControl{
|
||||
Auth: tpm2.TPMRHOwner,
|
||||
ObjectHandle: &tpm2.NamedHandle{Handle: cp.ObjectHandle, Name: cp.Name},
|
||||
PersistentHandle: h,
|
||||
}).Execute(tpm); err != nil {
|
||||
t.Fatalf("persist stale RSA key: %v", err)
|
||||
}
|
||||
_, _ = (tpm2.FlushContext{FlushHandle: cp.ObjectHandle}).Execute(tpm)
|
||||
|
||||
// EnsureKey must self-heal: evict the RSA key, create + persist a fresh ECC key.
|
||||
signer, created, err := tpmkey.EnsureKey(tpm, h)
|
||||
if err != nil {
|
||||
t.Fatalf("EnsureKey over stale RSA key: %v", err)
|
||||
}
|
||||
if !created {
|
||||
t.Errorf("EnsureKey should have recreated the key (created=true)")
|
||||
}
|
||||
if _, ok := signer.Public().(*ecdsa.PublicKey); !ok {
|
||||
t.Fatalf("recreated signer is not ECDSA")
|
||||
}
|
||||
// and it must be usable for signing (the whole point of the recovery).
|
||||
dig := sha256.Sum256([]byte("post-heal"))
|
||||
sig, err := signer.Sign(rand.Reader, dig[:], nil)
|
||||
if err != nil {
|
||||
t.Fatalf("sign after heal: %v", err)
|
||||
}
|
||||
if !ecdsa.VerifyASN1(signer.Public().(*ecdsa.PublicKey), dig[:], sig) {
|
||||
t.Fatalf("post-heal ECDSA signature did not verify")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user