Pin the root public key (SPKI) so the fingerprint survives in-place renewal #15
@@ -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<X509Certificate2>()
|
||||
.Any(c => Convert.ToHexStringLower(SHA256.HashData(c.RawData)) == expected);
|
||||
.Any(c => Convert.ToHexStringLower(SHA256.HashData(c.PublicKey.ExportSubjectPublicKeyInfo())) == expected);
|
||||
if (!matched)
|
||||
{
|
||||
logger.LogError(
|
||||
|
||||
@@ -81,9 +81,11 @@ public sealed class SelfSignedTlsManager
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user