carlos / platform Public

edge: one selection invariant for imported names (#1429)

Replaces 1350dc0a's patch with a pure chooseCert: for a name with a
usable import, the certificate returned is one the hello supports
whenever any usable candidate is, a supported CARLOS certificate is
preferred over the import, and nil only when none is supported.
wrapImported now answers an imported deployment host from chooseCert
over the mirror's <host> and <host>+rsa entries and the import,
without consulting the inner chain, so no inner choice can disagree
(Astra spec R2 P1-4); nil still falls through to the inner chain and
HostPolicy. Every other name is unchanged.

The issuer's renewal timer for an imported name is armed by the warm,
which calls perHostCert below this layer; a test pins that. A seeded
randomized test checks the invariant over 6000 hello and candidate
sets, beside the TLS 1.3-only ECDSA regression through the whole chain.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 86082f48eeba0aedd7c6767e8ca198f50d7cd09c parent 5920e4f
2 files changed, +206 −44
  • internal/edge/certimport.go +52 −33
  • internal/edge/certimport_test.go +154 −11
diff --git a/internal/edge/certimport.go b/internal/edge/certimport.go
index 6be702861..e606228a6 100644
--- a/internal/edge/certimport.go
+++ b/internal/edge/certimport.go
@@ -101,52 +101,71 @@ func (m *certMirror) imports() map[string]importEntry {
//
// - a hello offering only acme-tls/1 goes to the inner chain: a
// tls-alpn-01 validation must reach its token certificate;
-// - a usable CARLOS entry (<host> or <host>+rsa, in date, naming the host)
-// that the hello supports wins: pass through, as long as the certificate
-// the inner chain returns is one the hello supports (else the import, if
-// it qualifies below);
-// - else a usable import the hello supports, for a deployment host (this
-// box routes it, or some box's edges file homes it exactly), is served;
-// - else pass through.
+// - a name with no usable import, or one that is not a deployment host
+// (this box routes it, or some box's edges file homes it exactly), goes
+// to the inner chain: every other name is served exactly as before;
+// - otherwise the answer is chooseCert's, over the mirror's CARLOS entries
+// (<host>, <host>+rsa) and the import, returned directly. The inner
+// chain is NOT consulted for an imported name, so no choice of its own
+// (helloSupportsECDSA, its RSA preference) can disagree with the
+// invariant chooseCert keeps (Astra spec R2 P1-4);
+// - only when chooseCert finds nothing the hello supports does the hello
+// go to the inner chain, where the issuer's HostPolicy refuses an
+// imported name until some box publishes it pointing.
//
-// Imports come from mirror memory only, never the local disk, so a removed
-// import is never served by a box that has listed since. deployed nil
-// serves no import at all.
+// Because hellos for an imported name stop here, autocert does not arm the
+// issuer's renewal timer for it from a handshake. The cert warm does: it
+// calls perHostCert, below this layer, for every name in its set on every
+// pass, and that call loads the cached certificate and re-arms the timer.
+//
+// Imports come from mirror memory only, never the local disk, so a box that
+// restarts never resurrects a removed import from its disk; a running box
+// stops serving one once a listing no longer returns it. deployed nil serves
+// no import at all.
func (m *certMirror) wrapImported(inner func(*tls.ClientHelloInfo) (*tls.Certificate, error), deployed func(string) bool) func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
return func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
if len(hello.SupportedProtos) == 1 && hello.SupportedProtos[0] == acme.ALPNProto {
return inner(hello)
}
name := serverName(hello.ServerName)
+ now := time.Now()
snap := m.snapshot()
- imp := snap.usableImport(name, time.Now())
- if imp == nil {
+ imp := snap.usableImport(name, now)
+ if imp == nil || deployed == nil || !deployed(name) {
return inner(hello)
}
- importOK := hello.SupportsCertificate(imp) == nil && deployed != nil && deployed(name)
- carlos := false
- for _, key := range [2]string{name, name + "+rsa"} {
- if c := snap.certs[key]; usableFor(c, name) && hello.SupportsCertificate(c) == nil {
- carlos = true
- break
- }
+ carlos := []*tls.Certificate{snap.certs[name], snap.certs[name+"+rsa"]}
+ if c := chooseCert(hello, carlos, imp, now, name); c != nil {
+ return c, nil
}
- if !carlos {
- if importOK {
- return imp, nil
- }
- return inner(hello)
- }
- // A compatible CARLOS entry exists, but the inner chain chooses on its
- // own terms (helloSupportsECDSA reads a TLS 1.3-only ECDSA hello as
- // wanting RSA): what it RETURNS must suit the hello, or the import
- // that does is served instead (Astra spec R2 P1-4).
- c, err := inner(hello)
- if (err == nil && hello.SupportsCertificate(c) == nil) || !importOK {
- return c, err
+ return inner(hello)
+ }
+}
+
+// chooseCert picks the certificate for an imported name from its CARLOS
+// entries (most preferred first; nil entries allowed) and its import.
+//
+// For a name with a usable import, the certificate returned is one the hello supports (hello.SupportsCertificate) whenever any usable candidate is; a supported CARLOS certificate is preferred over the import; nil only when no usable candidate is supported.
+//
+// Usable is: a leaf, in date at now, naming host. The caller has already
+// checked the import's binding (domains.ImportUsable).
+func chooseCert(hello *tls.ClientHelloInfo, carlos []*tls.Certificate, imported *tls.Certificate, now time.Time, host string) *tls.Certificate {
+ for _, c := range carlos {
+ if usableAt(c, host, now) && hello.SupportsCertificate(c) == nil {
+ return c
}
- return imp, nil
}
+ if usableAt(imported, host, now) && hello.SupportsCertificate(imported) == nil {
+ return imported
+ }
+ return nil
+}
+
+// usableAt reports whether cert has a leaf that is in date at now and names
+// host.
+func usableAt(cert *tls.Certificate, host string, now time.Time) bool {
+ return cert != nil && cert.Leaf != nil && !now.Before(cert.Leaf.NotBefore) &&
+ now.Before(cert.Leaf.NotAfter) && cert.Leaf.VerifyHostname(host) == nil
}
// pruneLocalImports deletes every regular *+import file in this box's cert
diff --git a/internal/edge/certimport_test.go b/internal/edge/certimport_test.go
index ba87e194f..53ab98ef1 100644
--- a/internal/edge/certimport_test.go
+++ b/internal/edge/certimport_test.go
@@ -15,6 +15,7 @@ import (
"errors"
"log"
"math/big"
+ mrand "math/rand/v2"
"os"
"path/filepath"
"strings"
@@ -842,17 +843,19 @@ func TestImportReconcileDeletesAtMostFivePerPass(t *testing.T) {
}
}
-// The layer checks the certificate the inner chain actually RETURNS, not
-// only that a compatible CARLOS entry exists (Astra spec R2 P1-4). A TLS
+// Astra spec R2 P1-4, through the whole chain on a non-issuer. A TLS
// 1.3-only ECDSA client offers no TLS 1.2 ECDSA suite, so the mirror's
-// helloSupportsECDSA reads it as wanting RSA and returns the +rsa entry it
-// cannot use; the import it can use is served instead of a failed handshake.
-func TestImportServedWhenTheInnerChainReturnsAnUnusableCertificate(t *testing.T) {
+// helloSupportsECDSA reads it as wanting RSA and the inner chain returns the
+// +rsa entry the client cannot use. For an imported name the inner chain is
+// not consulted: the client gets a certificate it supports, CARLOS's ECDSA
+// one first.
+func TestImportTLS13OnlyECDSAClientGetsASupportedCertificate(t *testing.T) {
ca := newE2ECA(t)
s := release.NewDirStore(t.TempDir())
ecdsaImport := goodImport(t, ca, importHost, false)
+ carlosECDSA := hostCertPEM(t, importHost, time.Now().Add(80*24*time.Hour))
putBucket(t, s, domains.ImportName(importHost), ecdsaImport)
- putBucket(t, s, importHost, hostCertPEM(t, importHost, time.Now().Add(80*24*time.Hour)))
+ putBucket(t, s, importHost, carlosECDSA)
putBucket(t, s, importHost+"+rsa", carlosRSAPEM(t, ca, importHost))
m := newCertMirror(s, nil)
@@ -871,12 +874,152 @@ func TestImportServedWhenTheInnerChainReturnsAnUnusableCertificate(t *testing.T)
t.Fatal("test bug: the inner chain must return a certificate this hello cannot use")
}
c, err := get(tls13ECDSA)
- if err != nil || c == nil || !c.Leaf.Equal(leafOfImport(t, ecdsaImport)) {
- t.Fatalf("got %v; want the ECDSA import", err)
+ if err != nil || c == nil || tls13ECDSA.SupportsCertificate(c) != nil {
+ t.Fatalf("got %v; want a certificate the hello supports", err)
}
- // A hello the returned CARLOS certificate suits still gets it.
- if c, err := get(modernHello(importHost)); err != nil || c.Leaf.Equal(leafOfImport(t, ecdsaImport)) {
- t.Fatalf("a modern hello got the import (%v); want CARLOS's certificate", err)
+ if !c.Leaf.Equal(entryLeaf(t, carlosECDSA)) {
+ t.Fatal("want CARLOS's ECDSA certificate, preferred over the import")
+ }
+}
+
+// The chain the warm calls (perHostCert, below the import layer) still
+// reaches autocert for an imported name once it points: that call is what
+// loads the cached certificate and arms the issuer's renewal timer, which
+// handshakes no longer do for such a name.
+func TestImportWarmPathStillReachesAutocert(t *testing.T) {
+ ca := newE2ECA(t)
+ s := release.NewDirStore(t.TempDir())
+ putJSON(t, s, "control/edges/annie.json", ownFile(true))
+ putBucket(t, s, domains.ImportName(importHost), goodImport(t, ca, importHost, true))
+ e, _ := importIssuer(t, s)
+ m := e.certs.Load()
+ local := newCountingCache(t)
+ carlos := hostCertPEM(t, importHost, time.Now().Add(80*24*time.Hour))
+ if err := local.Put(context.Background(), importHost, carlos); err != nil {
+ t.Fatal(err)
+ }
+ m.gated = func() bool { return false } // the issuer
+ perHost := m.wrapGetCertificate(offlineManager(t, local, e.issuerHostPolicy()).GetCertificate)
+ c, err := perHost(ecdsaHello(importHost))
+ if err != nil || !c.Leaf.Equal(entryLeaf(t, carlos)) {
+ t.Fatalf("the warm's call: %v; want CARLOS's cached certificate through autocert", err)
+ }
+ if local.count(importHost) == 0 {
+ t.Fatal("the warm's call never reached autocert's cache")
+ }
+}
+
+// The selection invariant, checked over randomized hellos and candidate
+// sets: TLS 1.2 and 1.3, TLS 1.3-only suites, ECDSA-only and RSA-PSS-only
+// signature schemes, P-384 offered or not; ECDSA P-256 and P-384 and RSA
+// CARLOS entries present, absent or expired; an RSA or ECDSA import.
+func TestChooseCertInvariant(t *testing.T) {
+ ca := newE2ECA(t)
+ now := time.Now()
+ mk := func(rsaKey bool, curve elliptic.Curve, notAfter time.Time) *tls.Certificate {
+ var priv crypto.Signer
+ if rsaKey {
+ priv = testImportRSAKey(t)
+ } else {
+ k, err := ecdsa.GenerateKey(curve, rand.Reader)
+ if err != nil {
+ t.Fatal(err)
+ }
+ priv = k
+ }
+ tmpl := &x509.Certificate{SerialNumber: big.NewInt(int64(len(importHost))), Subject: pkix.Name{CommonName: importHost},
+ DNSNames: []string{importHost}, NotBefore: now.Add(-72 * time.Hour), NotAfter: notAfter,
+ KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}}
+ der, err := x509.CreateCertificate(rand.Reader, tmpl, ca.root, priv.Public(), ca.key)
+ if err != nil {
+ t.Fatal(err)
+ }
+ leaf, err := x509.ParseCertificate(der)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return &tls.Certificate{Certificate: [][]byte{der}, PrivateKey: priv, Leaf: leaf}
+ }
+ live, dead := now.Add(60*24*time.Hour), now.Add(-time.Hour)
+ ecdsaChoices := []*tls.Certificate{nil, mk(false, elliptic.P256(), live), mk(false, elliptic.P384(), live), mk(false, elliptic.P256(), dead)}
+ rsaChoices := []*tls.Certificate{nil, mk(true, nil, live), mk(true, nil, dead)}
+ imports := []*tls.Certificate{mk(true, nil, live), mk(false, elliptic.P256(), live), mk(false, elliptic.P384(), live)}
+
+ versions := [][]uint16{{tls.VersionTLS12}, {tls.VersionTLS13}, {tls.VersionTLS13, tls.VersionTLS12}}
+ suites := [][]uint16{
+ {tls.TLS_AES_128_GCM_SHA256},
+ {tls.TLS_AES_128_GCM_SHA256, tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
+ {tls.TLS_AES_128_GCM_SHA256, tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
+ {tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
+ {tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
+ {tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
+ {tls.TLS_RSA_WITH_AES_128_GCM_SHA256},
+ }
+ schemes := [][]tls.SignatureScheme{
+ {tls.ECDSAWithP256AndSHA256},
+ {tls.ECDSAWithP384AndSHA384},
+ {tls.ECDSAWithP256AndSHA256, tls.ECDSAWithP384AndSHA384},
+ {tls.PSSWithSHA256},
+ {tls.PKCS1WithSHA256},
+ {tls.PSSWithSHA256, tls.PKCS1WithSHA256},
+ {tls.ECDSAWithP256AndSHA256, tls.PSSWithSHA256, tls.PKCS1WithSHA256},
+ nil,
+ }
+ curves := [][]tls.CurveID{{tls.X25519}, {tls.X25519, tls.CurveP256}, {tls.CurveP256, tls.CurveP384}, {tls.X25519, tls.CurveP256, tls.CurveP384}}
+
+ rng := mrand.New(mrand.NewPCG(1429, 2026))
+ pick := func(n int) int { return rng.IntN(n) }
+ var chose [2]int // CARLOS, import
+ none := 0
+ const cases = 6000
+ for i := range cases {
+ hello := &tls.ClientHelloInfo{
+ ServerName: importHost,
+ SupportedVersions: versions[pick(len(versions))],
+ CipherSuites: suites[pick(len(suites))],
+ SignatureSchemes: schemes[pick(len(schemes))],
+ SupportedCurves: curves[pick(len(curves))],
+ SupportedPoints: []uint8{0},
+ }
+ carlos := []*tls.Certificate{ecdsaChoices[pick(len(ecdsaChoices))], rsaChoices[pick(len(rsaChoices))]}
+ imp := imports[pick(len(imports))]
+ got := chooseCert(hello, carlos, imp, now, importHost)
+
+ var okCarlos, okAny []*tls.Certificate
+ for _, c := range carlos {
+ if c != nil && now.Before(c.Leaf.NotAfter) && hello.SupportsCertificate(c) == nil {
+ okCarlos = append(okCarlos, c)
+ }
+ }
+ okAny = append(okAny, okCarlos...)
+ if hello.SupportsCertificate(imp) == nil {
+ okAny = append(okAny, imp)
+ }
+ switch {
+ case len(okAny) == 0 && got != nil:
+ t.Fatalf("case %d: nothing usable is supported, yet a certificate was returned (hello %+v)", i, hello)
+ case len(okAny) > 0 && got == nil:
+ t.Fatalf("case %d: a usable candidate is supported, yet nil was returned (hello %+v)", i, hello)
+ case got != nil && hello.SupportsCertificate(got) != nil:
+ t.Fatalf("case %d: returned a certificate the hello does not support (hello %+v)", i, hello)
+ case got != nil && !now.Before(got.Leaf.NotAfter):
+ t.Fatalf("case %d: returned an expired certificate", i)
+ case len(okCarlos) > 0 && got == imp:
+ t.Fatalf("case %d: the import was preferred over a supported CARLOS certificate (hello %+v)", i, hello)
+ }
+ switch {
+ case got == nil:
+ none++
+ case got == imp:
+ chose[1]++
+ default:
+ chose[0]++
+ }
+ }
+ // The generator must exercise every outcome of the invariant.
+ t.Logf("%d cases: CARLOS %d, import %d, none %d", cases, chose[0], chose[1], none)
+ if chose[0] == 0 || chose[1] == 0 || none == 0 {
+ t.Fatalf("CARLOS %d, import %d, none %d: the generator is degenerate", chose[0], chose[1], none)
}
}