Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/https.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
20 changes: 17 additions & 3 deletions internal/certs/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os/user"
"path/filepath"
"slices"
"strings"
"time"
)

Expand All @@ -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
}

Expand All @@ -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") }

Expand Down
35 changes: 35 additions & 0 deletions internal/certs/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Loading