carlos / platform Public

edge: serve the import when the inner chain returns an unusable cert

Astra spec R2 P1-4: a compatible CARLOS entry in the snapshot does not
mean the inner chain returns it. A TLS 1.3-only ECDSA client offers no
TLS 1.2 ECDSA suite, so helloSupportsECDSA reads it as wanting RSA and
the mirror returns the +rsa entry it cannot use. The import layer now
checks the certificate the inner chain returns with SupportsCertificate
and serves a qualifying import instead of a failed handshake. Without a
compatible CARLOS entry the order is unchanged: the import is served
without calling the inner chain, so no RSA order starts on the issuer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 1350dc0a09b25eac30ea555be0adfcf6f08febbf parent 50895e9
2 files changed, +107 −26
  • internal/edge/certimport.go +19 −3
  • internal/edge/certimport_test.go +88 −23
diff --git a/internal/edge/certimport.go b/internal/edge/certimport.go
index 50c03e5e7..924fdbcd4 100644
--- a/internal/edge/certimport.go
+++ b/internal/edge/certimport.go
@@ -102,7 +102,9 @@ 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;
+// 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.
@@ -121,14 +123,28 @@ func (m *certMirror) wrapImported(inner func(*tls.ClientHelloInfo) (*tls.Certifi
if imp == nil {
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 {
- return inner(hello)
+ carlos = true
+ break
}
}
- if hello.SupportsCertificate(imp) != nil || deployed == nil || !deployed(name) {
+ 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 imp, nil
}
}
diff --git a/internal/edge/certimport_test.go b/internal/edge/certimport_test.go
index 3288530db..fcbd22916 100644
--- a/internal/edge/certimport_test.go
+++ b/internal/edge/certimport_test.go
@@ -447,26 +447,64 @@ func TestImportServedAndALPNPassesThrough(t *testing.T) {
}
}
+// carlosRSAPEM is a CARLOS +rsa entry for host in autocert's format, issued by ca.
+func carlosRSAPEM(t *testing.T, ca *e2eCA, host string) []byte {
+ t.Helper()
+ key := testImportRSAKey(t)
+ tmpl := &x509.Certificate{SerialNumber: big.NewInt(9), Subject: pkix.Name{CommonName: host}, DNSNames: []string{host},
+ NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(80 * 24 * time.Hour),
+ KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}}
+ der, err := x509.CreateCertificate(rand.Reader, tmpl, ca.root, &key.PublicKey, ca.key)
+ if err != nil {
+ t.Fatal(err)
+ }
+ pkcs8, err := x509.MarshalPKCS8PrivateKey(key)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var buf bytes.Buffer
+ _ = pem.Encode(&buf, &pem.Block{Type: "PRIVATE KEY", Bytes: pkcs8})
+ _ = pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: der})
+ return buf.Bytes()
+}
+
+// realLayer is the import layer over the real chain below it on a gated
+// non-issuer: the mirror over an offline autocert.
+func realLayer(t *testing.T, s release.Store) func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
+ t.Helper()
+ m := newCertMirror(s, nil)
+ inner, _ := gatedChain(t, m, autocert.DirCache(t.TempDir()), importHost)
+ refreshMirror(t, m)
+ return m.wrapImported(inner, deployedOnly(importHost))
+}
+
+func entryLeaf(t *testing.T, body []byte) *x509.Certificate {
+ t.Helper()
+ c, err := parseCertEntry(body)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return c.Leaf
+}
+
// CARLOS's own certificate wins for every hello it suits; an RSA import still
// serves the RSA-only hello an ECDSA CARLOS certificate cannot.
func TestImportCarlosWinsWhenCompatible(t *testing.T) {
ca := newE2ECA(t)
s := release.NewDirStore(t.TempDir())
rsaImport := goodImport(t, ca, importHost, true)
+ carlos := hostCertPEM(t, importHost, time.Now().Add(80*24*time.Hour)) // ECDSA
putBucket(t, s, domains.ImportName(importHost), rsaImport)
- putBucket(t, s, importHost, hostCertPEM(t, importHost, time.Now().Add(80*24*time.Hour))) // ECDSA
- get, probe, _ := importLayer(t, s, deployedOnly(importHost))
+ putBucket(t, s, importHost, carlos)
+ get := realLayer(t, s)
- if _, err := get(modernHello(importHost)); !errors.Is(err, errInnerProbe) {
- t.Fatalf("an ECDSA-capable hello = %v; want the CARLOS certificate (the inner chain)", err)
+ if c, err := get(modernHello(importHost)); err != nil || !c.Leaf.Equal(entryLeaf(t, carlos)) {
+ t.Fatalf("an ECDSA-capable hello: %v; want the CARLOS certificate", err)
}
c, err := get(rsaOnlyHello(importHost))
if err != nil || c == nil || !c.Leaf.Equal(leafOfImport(t, rsaImport)) {
t.Fatalf("an RSA-only hello: %v; want the RSA import", err)
}
- if n := probe.count(); n != 1 {
- t.Fatalf("inner chain calls = %d, want 1", n)
- }
}
// A CARLOS +rsa entry the hello supports wins over an RSA import too.
@@ -474,22 +512,11 @@ func TestImportCarlosRSAEntryWins(t *testing.T) {
ca := newE2ECA(t)
s := release.NewDirStore(t.TempDir())
putBucket(t, s, domains.ImportName(importHost), goodImport(t, ca, importHost, true))
- key := testImportRSAKey(t)
- tmpl := &x509.Certificate{SerialNumber: big.NewInt(9), Subject: pkix.Name{CommonName: importHost}, DNSNames: []string{importHost},
- NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(80 * 24 * time.Hour),
- KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}}
- der, err := x509.CreateCertificate(rand.Reader, tmpl, ca.root, &key.PublicKey, ca.key)
- if err != nil {
- t.Fatal(err)
- }
- var buf bytes.Buffer
- pkcs8, _ := x509.MarshalPKCS8PrivateKey(key)
- _ = pem.Encode(&buf, &pem.Block{Type: "PRIVATE KEY", Bytes: pkcs8})
- _ = pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: der})
- putBucket(t, s, importHost+"+rsa", buf.Bytes())
- get, _, _ := importLayer(t, s, deployedOnly(importHost))
- if _, err := get(rsaOnlyHello(importHost)); !errors.Is(err, errInnerProbe) {
- t.Fatalf("an RSA-only hello with a CARLOS +rsa entry = %v; want the inner chain", err)
+ carlosRSA := carlosRSAPEM(t, ca, importHost)
+ putBucket(t, s, importHost+"+rsa", carlosRSA)
+ get := realLayer(t, s)
+ if c, err := get(rsaOnlyHello(importHost)); err != nil || !c.Leaf.Equal(entryLeaf(t, carlosRSA)) {
+ t.Fatalf("an RSA-only hello with a CARLOS +rsa entry: %v; want the CARLOS entry", err)
}
}
@@ -814,3 +841,41 @@ 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
+// 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) {
+ ca := newE2ECA(t)
+ s := release.NewDirStore(t.TempDir())
+ ecdsaImport := goodImport(t, ca, importHost, false)
+ 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+"+rsa", carlosRSAPEM(t, ca, importHost))
+
+ m := newCertMirror(s, nil)
+ inner, _ := gatedChain(t, m, autocert.DirCache(t.TempDir()), importHost)
+ refreshMirror(t, m)
+ get := m.wrapImported(inner, deployedOnly(importHost))
+
+ tls13ECDSA := &tls.ClientHelloInfo{
+ ServerName: importHost,
+ SupportedVersions: []uint16{tls.VersionTLS13},
+ SignatureSchemes: []tls.SignatureScheme{tls.ECDSAWithP256AndSHA256},
+ SupportedCurves: []tls.CurveID{tls.X25519, tls.CurveP256},
+ CipherSuites: []uint16{tls.TLS_AES_128_GCM_SHA256},
+ }
+ if c, err := inner(tls13ECDSA); err != nil || tls13ECDSA.SupportsCertificate(c) == nil {
+ 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)
+ }
+ // 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)
+ }
+}