From fa5205715b89f104fc97a2c3b64a8c3f8e60ff984457d9e111099d9b09d01ed7 Mon Sep 17 00:00:00 2001 From: gsadmin Date: Tue, 11 Aug 2026 06:23:15 -0400 Subject: [PATCH] Pin the root public key (SPKI), not the cert, so the pin survives renewal [skip ci] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bootstrap-token fingerprint now hashes the root CA's SubjectPublicKeyInfo instead of the certificate DER. The in-place root renewal keeps the same key but re-issues the cert, so a cert hash would change at renewal (invalidating tokens minted just before it); the key hash stays constant, matching the same-key renewal design. The agent matches the pin against each fetched cert's SPKI — only the root carries that key. Test asserts the fingerprint is unchanged across an in-place renewal. Backend 104, Agent 60 tests pass. Co-Authored-By: Claude Fable 5 --- src/Agent/Enrollment/ServerTrust.cs | 5 ++++- src/Backend/Tls/SelfSignedTlsManager.cs | 11 +++++++---- tests/Backend.Tests/SelfSignedTlsTests.cs | 6 ++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Agent/Enrollment/ServerTrust.cs b/src/Agent/Enrollment/ServerTrust.cs index e18ad3e..6b07762 100644 --- a/src/Agent/Enrollment/ServerTrust.cs +++ b/src/Agent/Enrollment/ServerTrust.cs @@ -86,9 +86,12 @@ public static class ServerTrust return (ServerCaStatus.FetchFailed, []); } + // Pin on the public key (SubjectPublicKeyInfo), not the certificate, so it + // matches a root renewed in place (same key, re-issued cert). Only the root + // carries this key — the intermediate and leaf have their own. var expected = expectedFingerprint.Trim().ToLowerInvariant(); var matched = chain.Cast() - .Any(c => Convert.ToHexStringLower(SHA256.HashData(c.RawData)) == expected); + .Any(c => Convert.ToHexStringLower(SHA256.HashData(c.PublicKey.ExportSubjectPublicKeyInfo())) == expected); if (!matched) { logger.LogError( diff --git a/src/Backend/Tls/SelfSignedTlsManager.cs b/src/Backend/Tls/SelfSignedTlsManager.cs index 7ef1c5c..a5b2e60 100644 --- a/src/Backend/Tls/SelfSignedTlsManager.cs +++ b/src/Backend/Tls/SelfSignedTlsManager.cs @@ -81,9 +81,11 @@ public sealed class SelfSignedTlsManager } /// - /// SHA-256 (lowercase hex) of the root CA certificate. Baked into bootstrap - /// tokens so an enrolling agent can pin the CA it fetches over the untrusted - /// first-contact channel to the one this server actually publishes. Null + /// SHA-256 (lowercase hex) of the root CA's public key (SubjectPublicKeyInfo). + /// Baked into bootstrap tokens so an enrolling agent can pin the CA it fetches + /// over the untrusted first-contact channel to the key this server publishes. + /// Hashing the key rather than the certificate keeps the pin stable across an + /// in-place root renewal (which re-issues the cert but keeps the key). Null /// before the chain exists. /// public string? CaFingerprint() @@ -93,7 +95,8 @@ public sealed class SelfSignedTlsManager var root = persistence.Load(TlsRole.Root); return root is null ? null - : Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(root.RawData)); + : Convert.ToHexStringLower( + System.Security.Cryptography.SHA256.HashData(root.PublicKey.ExportSubjectPublicKeyInfo())); } } diff --git a/tests/Backend.Tests/SelfSignedTlsTests.cs b/tests/Backend.Tests/SelfSignedTlsTests.cs index 92c227b..c8b30d9 100644 --- a/tests/Backend.Tests/SelfSignedTlsTests.cs +++ b/tests/Backend.Tests/SelfSignedTlsTests.cs @@ -129,6 +129,7 @@ public sealed class SelfSignedTlsTests : IDisposable var originalRootKey = originalRoot.GetECDsaPublicKey()!.ExportSubjectPublicKeyInfo(); var originalIntermediate = File.ReadAllText(Path.Combine(directory, "intermediate.crt")); var originalLeaf = manager.ServerCertificate!.Thumbprint; + var fingerprintBefore = manager.CaFingerprint(); var changed = manager.EnsureChain(NullLogger.Instance); Assert.True(changed); @@ -142,6 +143,11 @@ public sealed class SelfSignedTlsTests : IDisposable Assert.Equal(originalIntermediate, File.ReadAllText(Path.Combine(directory, "intermediate.crt"))); Assert.Equal(originalLeaf, manager.ServerCertificate!.Thumbprint); + // The token pin is the root's public key, so it survives the renewal — a + // token minted before the renewal still matches after it. + Assert.Equal(fingerprintBefore, manager.CaFingerprint()); + Assert.NotNull(fingerprintBefore); + // The crux: an agent that still trusts only the ORIGINAL root validates the // current leaf, because the chain signs to the same key — no break, no // re-enrollment when the root renews. -- 2.52.0