carlos / platform Public

edge: an imported name under a wildcard goes to chooseCert (#1429)

Astra R3 P1: the wildcard layer sits above the import layer and served
its certificate without checking the hello, so an RSA-only client of an
exact name with a usable RSA import under an ECDSA CARLOS wildcard
failed TLS. The wildcard layer now passes such a name (a usable import
for a deployment host) down to the import layer, where the wildcard is
the first CARLOS candidate of chooseCert, still preferred over the
import for every client it suits. Every other name, covered or not, is
answered exactly as before.

Tests: a deterministic whole-chain test (RSA-only hello gets the
import, ECDSA hello gets the wildcard, a covered name with no import
gets the wildcard for every hello), and the randomized invariant now
includes ECDSA and RSA wildcard candidates. Removing the pass-down, or
the wildcard candidate, fails the whole-chain test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 6d9fe57d89040adb80dc91ef2d990ef989a0b039 parent f6f52a3
5 files changed, +105 −15
  • internal/edge/certimport.go +19 −3
  • internal/edge/certimport_e2e_test.go +1 −1
  • internal/edge/certimport_test.go +55 −8
  • internal/edge/edge.go +15 −3
  • internal/edge/wildcard.go +15 −0
diff --git a/internal/edge/certimport.go b/internal/edge/certimport.go
index 1f470a3c9..dfe5600f0 100644
--- a/internal/edge/certimport.go
+++ b/internal/edge/certimport.go
@@ -77,6 +77,13 @@ func (s *certSnapshot) usableImport(host string, now time.Time) *tls.Certificate
return e.cert
}
+// servesImport reports whether the import layer would consider host's
+// import at all: a usable import, for a deployment host. The wildcard layer
+// passes exactly these names down (wrapGetCertificateImports). Nil-safe.
+func (m *certMirror) servesImport(host string, deployed func(string) bool) bool {
+ return deployed != nil && deployed(host) && m.holdsUsableImport(host, time.Now())
+}
+
// holdsUsableImport reports whether the mirror's snapshot holds an import
// that would serve host right now. Nil-safe.
func (m *certMirror) holdsUsableImport(host string, now time.Time) bool {
@@ -115,8 +122,9 @@ func (m *certMirror) holdsUsableImport(host string, now time.Time) bool {
// 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) {
+// no import at all. wildcard is the wildcard certificate covering a name
+// (wildcardManager.certFor), nil when there is no wildcard manager.
+func (m *certMirror) wrapImported(inner func(*tls.ClientHelloInfo) (*tls.Certificate, error), deployed func(string) bool, wildcard func(string) *tls.Certificate) 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)
@@ -128,7 +136,15 @@ func (m *certMirror) wrapImported(inner func(*tls.ClientHelloInfo) (*tls.Certifi
if imp == nil || deployed == nil || !deployed(name) {
return inner(hello)
}
- carlos := []*tls.Certificate{snap.certs[name], snap.certs[name+"+rsa"]}
+ // The wildcard first: for a covered name it is what every client got
+ // before the import, and the wildcard layer above passes such a name
+ // down here (wrapGetCertificateImports) only so the import can answer
+ // the clients the wildcard does not suit.
+ var wild *tls.Certificate
+ if wildcard != nil {
+ wild = wildcard(name)
+ }
+ carlos := []*tls.Certificate{wild, snap.certs[name], snap.certs[name+"+rsa"]}
if c := chooseCert(hello, carlos, imp, now, name); c != nil {
return c, nil
}
diff --git a/internal/edge/certimport_e2e_test.go b/internal/edge/certimport_e2e_test.go
index 0fb0555b4..7ca5cfb9f 100644
--- a/internal/edge/certimport_e2e_test.go
+++ b/internal/edge/certimport_e2e_test.go
@@ -54,7 +54,7 @@ func TestImportE2EServeBeforePointingThenCarlosThenRemoval(t *testing.T) {
perHost := map[*e2eEdge]func(*tls.ClientHelloInfo) (*tls.Certificate, error){}
for _, b := range []*e2eEdge{edgeA, edgeB} {
perHost[b] = b.getCert
- full[b] = b.mirror.wrapImported(b.getCert, b.edge.deploymentHost)
+ full[b] = b.mirror.wrapImported(b.getCert, b.edge.deploymentHost, nil)
b.tlsConfig.GetCertificate = full[b]
}
lnA, lnB := edgeA.serveTLS(t), edgeB.serveTLS(t)
diff --git a/internal/edge/certimport_test.go b/internal/edge/certimport_test.go
index 5db6fd75a..aefbef16a 100644
--- a/internal/edge/certimport_test.go
+++ b/internal/edge/certimport_test.go
@@ -418,7 +418,7 @@ func importLayer(t *testing.T, s release.Store, deployed func(string) bool) (fun
m := newCertMirror(s, nil)
refreshMirror(t, m)
p := &innerProbe{}
- return m.wrapImported(p.get, deployed), p, m
+ return m.wrapImported(p.get, deployed, nil), p, m
}
// A usable import is served to a hello no CARLOS certificate suits, for a
@@ -476,7 +476,7 @@ func realLayer(t *testing.T, s release.Store) func(*tls.ClientHelloInfo) (*tls.C
m := newCertMirror(s, nil)
inner, _ := gatedChain(t, m, autocert.DirCache(t.TempDir()), importHost)
refreshMirror(t, m)
- return m.wrapImported(inner, deployedOnly(importHost))
+ return m.wrapImported(inner, deployedOnly(importHost), nil)
}
func entryLeaf(t *testing.T, body []byte) *x509.Certificate {
@@ -955,7 +955,7 @@ func TestImportTLS13OnlyECDSAClientGetsASupportedCertificate(t *testing.T) {
m := newCertMirror(s, nil)
inner, _ := gatedChain(t, m, autocert.DirCache(t.TempDir()), importHost)
refreshMirror(t, m)
- get := m.wrapImported(inner, deployedOnly(importHost))
+ get := m.wrapImported(inner, deployedOnly(importHost), nil)
tls13ECDSA := &tls.ClientHelloInfo{
ServerName: importHost,
@@ -1006,11 +1006,15 @@ func TestImportWarmPathStillReachesAutocert(t *testing.T) {
// 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.
+// CARLOS entries present, absent or expired; an ECDSA or RSA wildcard
+// covering the name, 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 {
+ mk := func(rsaKey bool, curve elliptic.Curve, notAfter time.Time, names ...string) *tls.Certificate {
+ if len(names) == 0 {
+ names = []string{importHost}
+ }
var priv crypto.Signer
if rsaKey {
priv = testImportRSAKey(t)
@@ -1021,8 +1025,8 @@ func TestChooseCertInvariant(t *testing.T) {
}
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,
+ tmpl := &x509.Certificate{SerialNumber: big.NewInt(int64(len(importHost))), Subject: pkix.Name{CommonName: names[0]},
+ DNSNames: names, 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 {
@@ -1036,6 +1040,8 @@ func TestChooseCertInvariant(t *testing.T) {
}
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)}
+ const wild = "*.import.test"
+ wildChoices := []*tls.Certificate{nil, mk(false, elliptic.P256(), live, wild), mk(true, nil, live, wild), mk(false, elliptic.P256(), dead, wild)}
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)}
@@ -1075,7 +1081,7 @@ func TestChooseCertInvariant(t *testing.T) {
SupportedCurves: curves[pick(len(curves))],
SupportedPoints: []uint8{0},
}
- carlos := []*tls.Certificate{ecdsaChoices[pick(len(ecdsaChoices))], rsaChoices[pick(len(rsaChoices))]}
+ carlos := []*tls.Certificate{wildChoices[pick(len(wildChoices))], ecdsaChoices[pick(len(ecdsaChoices))], rsaChoices[pick(len(rsaChoices))]}
imp := imports[pick(len(imports))]
got := chooseCert(hello, carlos, imp, now, importHost)
@@ -1261,3 +1267,44 @@ func TestImportReconcileRotatesPastPersistentFailures(t *testing.T) {
}
}
}
+
+// Astra R3 P1, deterministic and through the whole chain on a non-issuer:
+// the wildcard layer over the import layer over the mirror over a gated
+// autocert, as edge.go composes it. An exact name with a usable RSA import
+// under an ECDSA CARLOS wildcard: an RSA-only hello gets the import, an
+// ECDSA hello gets the wildcard. A name with no import under the same
+// wildcard is answered by the wildcard exactly as before, for both hellos.
+func TestImportUnderAWildcardThroughTheWholeChain(t *testing.T) {
+ const parent, plain = "import.test", "plain.import.test"
+ ca := newE2ECA(t)
+ s := release.NewDirStore(t.TempDir())
+ rsaImport := goodImport(t, ca, importHost, true)
+ putBucket(t, s, domains.ImportName(importHost), rsaImport)
+ m := newCertMirror(s, nil)
+ inner, _ := gatedChain(t, m, autocert.DirCache(t.TempDir()), importHost, plain)
+ refreshMirror(t, m)
+
+ w := newWildcardManager([]string{parent}, "", func() []string { return nil }, nil, "", autocert.DirCache(t.TempDir()), &fakeDNS{})
+ wildcard, _ := selfSignedWildcard(t, parent, time.Now().Add(80*24*time.Hour))
+ w.refresh()
+ w.mu.Lock()
+ w.certs[parent] = wildcard
+ w.mu.Unlock()
+
+ deployed := deployedOnly(importHost, plain)
+ chain := m.wrapImported(inner, deployed, w.certFor)
+ chain = wrapGetCertificateImports(w, chain, func(n string) bool { return m.servesImport(n, deployed) })
+
+ c, err := chain(rsaOnlyHello(importHost))
+ if err != nil || c == nil || !c.Leaf.Equal(leafOfImport(t, rsaImport)) {
+ t.Fatalf("RSA-only hello for the imported name: %v; want the RSA import", err)
+ }
+ if c, err := chain(modernHello(importHost)); err != nil || c != wildcard {
+ t.Fatalf("ECDSA hello for the imported name: %v; want the wildcard", err)
+ }
+ for _, h := range []*tls.ClientHelloInfo{modernHello(plain), rsaOnlyHello(plain), ecdsaHello(plain)} {
+ if c, err := chain(h); err != nil || c != wildcard {
+ t.Fatalf("a name with no import (%v): want the wildcard, as before", err)
+ }
+ }
+}
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 497e8916d..d1fb40345 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -5169,18 +5169,30 @@ func RunWithActivatorReady(args []string, act Activator, cache *Cache, ready fun
// the wildcard, and after perHostCert is captured, so the warm never
// sees an import and still orders CARLOS's own certificate. Served only
// to a hello no usable CARLOS certificate suits, for a deployment host.
+ // wm is read by the import layer only once handshakes start, after the
+ // wildcard manager below is built.
+ var wm *wildcardManager
+ var importedHere func(string) bool
if mirror != nil {
- getCert = mirror.wrapImported(getCert, e.deploymentHost)
+ wildcardFor := func(name string) *tls.Certificate {
+ if wm == nil {
+ return nil
+ }
+ return wm.certFor(name)
+ }
+ getCert = mirror.wrapImported(getCert, e.deploymentHost, wildcardFor)
+ importedHere = func(name string) bool { return mirror.servesImport(name, e.deploymentHost) }
}
// Wildcard certs (wildcard.go, ACME DNS-01): covered names — one label
// under a per-account or configured parent — are served the wildcard;
// everything else (custom domains, canary hosts, parents not yet
// obtained) stays on autocert's per-host HTTP-01 above. nil = not
// configured = exactly the pre-wildcard edge.
- var wm *wildcardManager
if w := newWildcardFromEnv(e, certCache); w != nil {
wm = w
- getCert = wrapGetCertificate(w, getCert)
+ // An exact name with a usable import is passed down to the import
+ // layer, where the wildcard is one of its candidates (#1429).
+ getCert = wrapGetCertificateImports(w, getCert, importedHere)
e.OnReload(w.refresh)
attachWildcardToMirror(mirror, w)
// A remote box's edges file landing (or leaving) can add or drop a
diff --git a/internal/edge/wildcard.go b/internal/edge/wildcard.go
index 7b2bbd9c8..8dc5ee1b0 100644
--- a/internal/edge/wildcard.go
+++ b/internal/edge/wildcard.go
@@ -1103,6 +1103,18 @@ func newWildcardFromEnv(e *Edge, cache autocert.Cache) *wildcardManager {
// everything else to inner (autocert's per-host path) — split out so the
// fall-through contract is testable without a TLS listener.
func wrapGetCertificate(w *wildcardManager, inner func(*tls.ClientHelloInfo) (*tls.Certificate, error)) func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
+ return wrapGetCertificateImports(w, inner, nil)
+}
+
+// wrapGetCertificateImports is wrapGetCertificate with one exception: an
+// exact name imported reports true for (an imported certificate the import
+// layer would serve, #1429) is passed down to inner, the import layer, even
+// when a wildcard covers it. There the wildcard is one of chooseCert's CARLOS
+// candidates, still preferred over the import for every client it suits, and
+// the import answers the clients it does not (an RSA-only client under an
+// ECDSA wildcard). Every other name is answered exactly as before. imported
+// nil passes nothing down.
+func wrapGetCertificateImports(w *wildcardManager, inner func(*tls.ClientHelloInfo) (*tls.Certificate, error), imported func(string) bool) func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
return func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
// A tls-alpn-01 validation handshake must reach autocert's challenge
// cert, never the wildcard — the CA is probing the per-host order the
@@ -1110,6 +1122,9 @@ func wrapGetCertificate(w *wildcardManager, inner func(*tls.ClientHelloInfo) (*t
if len(hello.SupportedProtos) == 1 && hello.SupportedProtos[0] == acme.ALPNProto {
return inner(hello)
}
+ if imported != nil && imported(serverName(hello.ServerName)) {
+ return inner(hello)
+ }
if c := w.certFor(hello.ServerName); c != nil {
return c, nil
}