| 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) |
| } |
| } |
| |