diff --git a/docs/https.md b/docs/https.md index ad588a03..154ee83b 100644 --- a/docs/https.md +++ b/docs/https.md @@ -32,7 +32,7 @@ A secured site redirects plain `http://` requests to `https://`. If you open a s When you install Flock, it creates its own certificate authority (CA) and adds it to your system's trust store: your login keychain on macOS, Trusted Root on Windows. On Linux, it goes into the system's trust store, into Firefox through a policy, and into the certificate databases of Chrome and other Chromium browsers, Snap and Flatpak versions included. -The CA's root certificate is **name-constrained** to your local TLD and `localhost`. Even if its private key ever leaked, it could not be used to issue certificates for real websites. +The CA's root certificate is **name-constrained** to your local TLD and `localhost`. Even if its private key ever leaked, it could not be used to issue certificates for real websites. When you change the TLD, Flock creates a new CA for it, and `flock install` adds that one to the trust store. Each secured site gets a certificate for `acme.test` and `*.acme.test`. Certificates for deeper subdomains and for the site's aliases are issued on demand the first time they're requested. diff --git a/internal/certs/certs.go b/internal/certs/certs.go index e6de6bc9..f2070bcd 100644 --- a/internal/certs/certs.go +++ b/internal/certs/certs.go @@ -20,6 +20,7 @@ import ( "os/user" "path/filepath" "slices" + "strings" "time" ) @@ -37,13 +38,17 @@ type CA struct { key *ecdsa.PrivateKey } -// Ensure loads the CA from dir, creating it on first use. +// Ensure loads the CA from dir, creating it on first use. A CA made for another TLD is replaced: +// its name constraints make browsers reject what it signs for tld. The new one isn't trusted +// yet, which `flock install` notices by its fingerprint and fixes. func Ensure(dir, tld string) (*CA, error) { ca := &CA{dir: dir} - if err := ca.load(); err == nil { + err := ca.load() + switch { + case err == nil && ca.permits(tld): return ca, nil - } else if !errors.Is(err, os.ErrNotExist) { + case err != nil && !errors.Is(err, os.ErrNotExist): return nil, err } @@ -54,6 +59,15 @@ func Ensure(dir, tld string) (*CA, error) { return ca, nil } +// permits reports whether the CA's name constraints allow names under tld. A CA without +// constraints allows every name. +func (ca *CA) permits(tld string) bool { + permitted := ca.cert.PermittedDNSDomains + return len(permitted) == 0 || slices.ContainsFunc(permitted, func(d string) bool { + return strings.EqualFold(d, "."+tld) + }) +} + // CertPath is the PEM file of the root certificate (what gets trusted by the OS). func (ca *CA) CertPath() string { return filepath.Join(ca.dir, "ca.crt") } diff --git a/internal/certs/certs_test.go b/internal/certs/certs_test.go index f9d21735..a3b612ff 100644 --- a/internal/certs/certs_test.go +++ b/internal/certs/certs_test.go @@ -59,6 +59,41 @@ func TestReusesCAAndCurrentCerts(t *testing.T) { } } +func TestReplacesCAWhenTheTLDChanges(t *testing.T) { + dir := t.TempDir() + old, _ := Ensure(dir, "wsl") + if _, _, err := old.SiteCert("app.wsl"); err != nil { + t.Fatal(err) + } + + ca, err := Ensure(dir, "test") + if err != nil { + t.Fatal(err) + } + if ca.Fingerprint() == old.Fingerprint() { + t.Fatal("CA for .wsl was kept for .test") + } + certPath, _, err := ca.SiteCert("app.test") + if err != nil { + t.Fatal(err) + } + if err := verify(t, ca, certPath, "app.test"); err != nil { + t.Fatalf("app.test: %v", err) + } + + again, err := Ensure(dir, "test") + if err != nil || again.Fingerprint() != ca.Fingerprint() { + t.Fatalf("new CA was not reused: %v", err) + } +} + +func TestKeepsCAWithoutNameConstraints(t *testing.T) { + ca := &CA{cert: &x509.Certificate{}} + if !ca.permits("test") { + t.Fatal("a CA without name constraints allows every TLD") + } +} + func TestRootIsNameConstrainedToTheTLD(t *testing.T) { ca, _ := Ensure(t.TempDir(), "test")