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