diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index acc52f6..cfe58f9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,6 +38,17 @@ jobs: - name: test run: go test -race ./... + - name: fuzz (10s per target) + run: | + for t in FuzzUntar:./pkg/archive FuzzReadFile:./pkg/archive FuzzParse:./pkg/license \ + FuzzVerifyKeyResponse:./pkg/online FuzzVerifyCheckResponse:./pkg/online \ + FuzzVerifyAttestedKeyResponse:./pkg/online FuzzCheckChallenge:./pkg/online; do + go test -run='^$' -fuzz="^${t%%:*}\$" -fuzztime=10s "${t#*:}" + done + + - name: test Python helper + run: python3 -m unittest discover -s helpers/python -v + docs-diagrams: runs-on: ubuntu-latest steps: diff --git a/CHANGELOG.md b/CHANGELOG.md index 568c011..95ba2d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,30 @@ GitHub releases. ## [Unreleased] +### Added (IMPROVEMENTS.md items) +- **Attested protocol:** server-issued single-use challenge (`POST /v1/challenge`), + evidence over `AttestationBinding(license, challenge, ephemeral key)`, and the + secret returned sealed to the ephemeral X25519 key. `online.AttestationNonce` + is replaced by `online.AttestationBinding`. +- **Policy-bound server secrets:** `binding.secret_v` 2 (default for new + licenses) derives the secret per binding policy, so an online server cannot + release an attested secret. `issue --server-secret-version 0` for old loaders; + `cryptship-server --no-legacy-secrets` once all licenses are reissued. +- **Seats:** `issue --max-machines N` + `cryptship-server --enforce-seats` + (`--seat-lease`, `--seats-file`, `--license-keys`). +- **Binding-derived state key** (`crypto.DeriveStateKey`, `keyprovider.Unlocker`) + seals the clock-rollback and revocation-grace records; the revocation-grace + record is no longer a plain-text timestamp. +- **Opt-in, off by default:** `CRYPTSHIP_APP_USER` (separate-uid extraction and + privilege drop) and `CRYPTSHIP_EXEC_MEMFD` (single ELF binary from a memfd); + `pkg/harden` and `helpers/python/cryptship_harden.py`. +- `issue --expires-end-of-day`; `--expires` with `--days` is now an error. +- 502/503/504 count as an outage for `CRYPTSHIP_ONLINE_GRACE`. +- Fuzz tests (license parsing, tar extraction, response decoders, challenges). + +### Fixed +- `scripts/check-mermaid.sh` docker fallback on Linux (temp dir was 0700). + ### Security / fixed (from a full code review) - **Online grace cache reworked.** It now caches the **content key** instead of the per-license binding secret (no portable license-wide credential), and the diff --git a/README.md b/README.md index 5ab145e..de2be2f 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,9 @@ Two checks remain ordinary code in the loader: the **signature** and the unwraps on its own machine, so for enforced expiry use the `online` provider. The loader also refuses to start when the clock is more than 24 hours behind the latest start it recorded for an expiring license (sealed in -`CRYPTSHIP_STATE_DIR`), but deleting that state resets the check: it stops a -casual clock rollback, not a determined one. With binding `none`, the license +`CRYPTSHIP_STATE_DIR` under a key derived from the license's binding material), +but deleting that state resets the check: it stops a casual clock rollback, not +a determined one. With binding `none`, the license itself carries the key material: treat it like a secret. ## Quickstart @@ -112,15 +113,20 @@ acquired**. That is a single pluggable interface (`pkg/keyprovider`): |---|---|---|---| | **Air-gapped** | `offline` | machine fingerprint, or none | expiry only | | **On-prem / cloud** | `online` | per-license secret held by your server | **real** — the server declines | -| **Confidential** | `attested` | SEV-SNP/TDX attestation gates the key | planned: interfaces + test-only verifier today | +| **Confidential** | `attested` | SEV-SNP/TDX attestation gates the key | protocol built (server challenge, key sealed to an ephemeral TEE key); SEV-SNP/TDX verifiers need hardware, test-only verifier today | The loader, license format, crypto, and app are identical across all modes — only the key provider differs. `offline` (none/strict/pool) and `online` are built and tested; `online` uses the reference `cryptship-server` (a stateless key broker). `attested` is built behind an `Attester` interface (loader) and an -`attest.Verifier` interface (server), each with a test-only implementation. There -is no production verifier yet, so `cryptship-server --require-attestation` -refuses to start rather than accept evidence it cannot check. Real SEV-SNP/TDX +`attest.Verifier` interface (server), each with a test-only implementation. The +protocol is replay-safe: the server issues a single-use challenge, the evidence +commits to it and to an ephemeral key made inside the TEE, and the secret comes +back encrypted to that key. Server secrets are derived per binding policy +(`binding.secret_v` 2), so an `online` server cannot release an `attested` +secret even with the same master key. There is no production verifier yet, so +`cryptship-server --require-attestation` refuses to start rather than accept +evidence it cannot check. Real SEV-SNP/TDX quote generation needs confidential-compute hardware (see [`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md)). @@ -140,7 +146,11 @@ source reconstruction: with the seccomp profile + non-root + dropped caps + read-only + tmpfs from `deploy/`. Stops other non-root users in the container; anything running as the app's own uid, container root or host root can still read the decrypted - files. + files. Optional: `CRYPTSHIP_APP_USER` extracts as a separate owner so the app + cannot modify its code, `CRYPTSHIP_EXEC_MEMFD` runs a single binary from + memory with no decrypted file on disk, and `pkg/harden` / + `helpers/python/cryptship_harden.py` let the app make itself non-dumpable + again after exec. - **Per-license watermark** — the loader drops `.cryptship-manifest.json` and exposes `CRYPTSHIP_LICENSE_ID` / `CRYPTSHIP_CUSTOMER` to the app, so a leaked instance traces to its licensee (a deterrent — strippable). @@ -163,19 +173,19 @@ for the full schema and the threat model. **Built & tested:** AES-256-GCM payload encryption; Ed25519 licenses (detached signature over the stored bytes); context-bound KEK (format v2: editing any claim breaks the unwrap); machine binding (none / strict / pool); online key provider + reference -`cryptship-server`; signing-key rotation (key ring); configurable revocation -(open / closed / grace) with **signed** revocation checks; **live `--revoke-file` -revocation** (no restart); **online outage grace** (`CRYPTSHIP_ONLINE_GRACE`, a -machine-sealed cached content key with authenticated validity); the -compile/transform seam; loader + container hardening; per-license watermarking; -and the attested (confidential-compute) provider behind `Attester` / -`attest.Verifier` interfaces with test-only implementations (no production -verifier yet). - -**Roadmap:** a real SEV-SNP/TDX `Attester` + Trustee-style KBS integration (and -the attested replay fix: server-issued challenge + ephemeral TEE key), TPM-sealed -binding, cloud-KMS wrapping, seat management, and chunked AEAD for very large -payloads. +`cryptship-server` (policy-bound server secrets, live denylist, seat limits, +sealed outage grace); signing-key rotation (key ring); signed, nonce-bound +revocation checks with configurable failure policy (open / closed / grace); +clock and grace state sealed under a binding-derived key; the compile/transform +seam; loader + container hardening (separate-uid extraction, memfd exec, +dumpable helpers); per-license watermarking; the attested (confidential-compute) +protocol behind `Attester` / `attest.Verifier` interfaces with test-only +implementations (no production verifier yet); and fuzz tests for license +parsing, extraction and the response decoders. + +**Roadmap:** real SEV-SNP/TDX attesters and verifiers, TPM-sealed binding and +cloud instance identity, cloud-KMS wrapping, server-signed time for offline +expiry, and chunked AEAD for very large payloads. ## Crypto diff --git a/cmd/cryptship-loader/clock.go b/cmd/cryptship-loader/clock.go index 452051d..56be5f7 100644 --- a/cmd/cryptship-loader/clock.go +++ b/cmd/cryptship-loader/clock.go @@ -1,9 +1,7 @@ package main import ( - "crypto/hkdf" - "crypto/sha256" - "encoding/binary" + "bytes" "errors" "fmt" "io/fs" @@ -11,7 +9,6 @@ import ( "path/filepath" "time" - "github.com/AI2TH/cryptship/pkg/crypto" "github.com/AI2TH/cryptship/pkg/license" ) @@ -25,81 +22,57 @@ const clockStateInfo = "cryptship/clock-state/v1" // checkClock is a best-effort clock-rollback check for licenses that expire. // It keeps the latest start time seen for the license in the state dir, -// sealed (AES-GCM) under a key derived from the content key, so the file -// cannot be edited to an earlier time without the key. If the clock is now -// more than clockTolerance behind that time, the loader refuses to start. +// sealed (AES-GCM) under a key derived from stateKey — the binding-derived +// state key (crypto.DeriveStateKey of this machine's fingerprint or the +// server secret, and the license claims) — so the file cannot be edited, or +// copied from another license or machine, without that binding. If the clock +// is now more than clockTolerance behind that time, the loader refuses to +// start. +// +// A record written by an older loader (sealed under a key derived from the +// content key, legacyKey) is still honoured once and rewritten under +// stateKey, so upgrading the loader does not reset the check. // // Limits, by design: deleting the state file (or running with a fresh state // dir) resets the check, and a state dir that cannot be written disables it // with a warning. It raises the bar against casually setting the clock back // to keep an expired license running; for enforced expiry use the online // provider. -func checkClock(dir string, c license.Claims, contentKey []byte, now time.Time) error { +func checkClock(dir string, c license.Claims, stateKey, legacyKey []byte, now time.Time) error { if c.NotAfter == 0 { return nil // nothing expires, nothing to protect } - key, err := hkdf.Key(sha256.New, contentKey, []byte(c.LicenseID), clockStateInfo, crypto.KeySize) - if err != nil { - return err - } - defer crypto.Wipe(key) - aad := []byte(clockStateInfo + "\x00" + c.LicenseID) - path := filepath.Join(dir, "clock-"+escapeID(c.LicenseID)) + st := sealedState{info: clockStateInfo, licenseID: c.LicenseID, path: filepath.Join(dir, "clock-"+escapeID(c.LicenseID))} latest := now.Unix() - switch blob, err := os.ReadFile(path); { - case err == nil: - pt, oerr := crypto.Open(key, blob, aad) - if oerr != nil || len(pt) != 8 { - warnf("clock state %s is not valid for this license and build; starting it again", path) - break + last, err := st.read(stateKey) + if errors.Is(err, errBadState) && legacyKey != nil && !bytes.Equal(legacyKey, stateKey) { + if l, lerr := st.read(legacyKey); lerr == nil { + last, err = l, nil // migrate a record from an older loader } - last := int64(binary.BigEndian.Uint64(pt)) + } + switch { + case err == nil: if now.Unix() < last-int64(clockTolerance/time.Second) { return fmt.Errorf("system clock (%s) is more than %s behind the latest start recorded for license %s (%s); refusing to start. Correct the clock; the record is %s", - now.UTC().Format(time.RFC3339), clockTolerance, c.LicenseID, time.Unix(last, 0).UTC().Format(time.RFC3339), path) + now.UTC().Format(time.RFC3339), clockTolerance, c.LicenseID, time.Unix(last, 0).UTC().Format(time.RFC3339), st.path) } if last > latest { latest = last } case errors.Is(err, fs.ErrNotExist): + case errors.Is(err, errBadState): + warnf("clock state %s is not valid for this license and build; starting it again", st.path) default: - warnf("clock-rollback check: cannot read %s: %v", path, err) + warnf("clock-rollback check: cannot read %s: %v", st.path, err) } - var b [8]byte - binary.BigEndian.PutUint64(b[:], uint64(latest)) - sealed, err := crypto.Seal(key, b[:], aad) - if err != nil { - return err - } - if err := writeFileAtomic(path, sealed); err != nil { - warnf("clock-rollback check disabled: cannot write %s: %v", path, err) + if err := st.write(stateKey, latest); err != nil { + warnf("clock-rollback check disabled: cannot write %s: %v", st.path, err) } return nil } -// writeFileAtomic writes via a temp file and rename, so a crash never leaves -// a truncated state file. -func writeFileAtomic(path string, data []byte) error { - if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { - return err - } - tmp, err := os.CreateTemp(filepath.Dir(path), ".tmp-"+filepath.Base(path)+"-*") - if err != nil { - return err - } - defer os.Remove(tmp.Name()) - if _, err := tmp.Write(data); err != nil { - tmp.Close() - return err - } - if err := tmp.Close(); err != nil { - return err - } - return os.Rename(tmp.Name(), path) -} - func warnf(format string, args ...any) { fmt.Fprintf(os.Stderr, "cryptship-loader: WARNING: "+format+"\n", args...) } diff --git a/cmd/cryptship-loader/clock_test.go b/cmd/cryptship-loader/clock_test.go index 105fa4f..76009fc 100644 --- a/cmd/cryptship-loader/clock_test.go +++ b/cmd/cryptship-loader/clock_test.go @@ -18,23 +18,23 @@ func TestCheckClockDetectsRollback(t *testing.T) { now := time.Now() c := license.Claims{LicenseID: "lic_clock", NotAfter: now.Add(30 * 24 * time.Hour).Unix()} - if err := checkClock(dir, c, key, now); err != nil { + if err := checkClock(dir, c, key, nil, now); err != nil { t.Fatalf("first start: %v", err) } // Small step back (NTP correction): allowed. - if err := checkClock(dir, c, key, now.Add(-time.Hour)); err != nil { + if err := checkClock(dir, c, key, nil, now.Add(-time.Hour)); err != nil { t.Fatalf("1h back: %v", err) } // The record keeps the latest time, not the last one. - if err := checkClock(dir, c, key, now.Add(-clockTolerance-time.Hour)); err == nil || !strings.Contains(err.Error(), "behind") { + if err := checkClock(dir, c, key, nil, now.Add(-clockTolerance-time.Hour)); err == nil || !strings.Contains(err.Error(), "behind") { t.Fatalf("rollback past tolerance: err = %v", err) } // Moving forward is fine and raises the record. later := now.Add(10 * 24 * time.Hour) - if err := checkClock(dir, c, key, later); err != nil { + if err := checkClock(dir, c, key, nil, later); err != nil { t.Fatal(err) } - if err := checkClock(dir, c, key, now); err == nil { + if err := checkClock(dir, c, key, nil, now); err == nil { t.Fatal("rollback below the raised record accepted") } } @@ -44,7 +44,7 @@ func TestCheckClockStateIsSealed(t *testing.T) { key, _ := crypto.RandomKey() now := time.Now() c := license.Claims{LicenseID: "lic_seal", NotAfter: now.Add(time.Hour).Unix()} - if err := checkClock(dir, c, key, now.Add(48*time.Hour)); err != nil { + if err := checkClock(dir, c, key, nil, now.Add(48*time.Hour)); err != nil { t.Fatal(err) } path := filepath.Join(dir, "clock-lic_seal") @@ -60,11 +60,11 @@ func TestCheckClockStateIsSealed(t *testing.T) { if err := os.WriteFile(filepath.Join(dir, "clock-lic_other"), blob, 0o600); err != nil { t.Fatal(err) } - if err := checkClock(dir, other, key, now); err != nil { + if err := checkClock(dir, other, key, nil, now); err != nil { t.Fatalf("a record copied from another license must be ignored, got %v", err) } // Licenses without expiry are not checked and leave no state. - if err := checkClock(dir, license.Claims{LicenseID: "lic_forever"}, key, now); err != nil { + if err := checkClock(dir, license.Claims{LicenseID: "lic_forever"}, key, nil, now); err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(dir, "clock-lic_forever")); !os.IsNotExist(err) { @@ -81,7 +81,35 @@ func TestCheckClockUnwritableStateDirDoesNotBlock(t *testing.T) { key, _ := crypto.RandomKey() now := time.Now() c := license.Claims{LicenseID: "lic_ro", NotAfter: now.Add(time.Hour).Unix()} - if err := checkClock(filepath.Join(file, "state"), c, key, now); err != nil { + if err := checkClock(filepath.Join(file, "state"), c, key, nil, now); err != nil { t.Fatalf("unwritable state dir must not stop the loader: %v", err) } } + +// A record sealed by an older loader (content-key-derived) is honoured once +// and rewritten under the binding-derived state key. +func TestCheckClockMigratesLegacyRecord(t *testing.T) { + dir := t.TempDir() + contentKey, _ := crypto.RandomKey() + stateKey, _ := crypto.RandomKey() + now := time.Now() + c := license.Claims{LicenseID: "lic_mig", NotAfter: now.Add(90 * 24 * time.Hour).Unix()} + // Older loader: record sealed under the content key, 10 days ahead. + if err := checkClock(dir, c, contentKey, nil, now.Add(10*24*time.Hour)); err != nil { + t.Fatal(err) + } + if err := checkClock(dir, c, stateKey, contentKey, now); err == nil { + t.Fatal("legacy record ignored: rollback after upgrade not detected") + } + // After a valid start the record is under the state key only. + if err := checkClock(dir, c, stateKey, contentKey, now.Add(10*24*time.Hour)); err != nil { + t.Fatal(err) + } + st := sealedState{info: clockStateInfo, licenseID: c.LicenseID, path: filepath.Join(dir, "clock-lic_mig")} + if _, err := st.read(stateKey); err != nil { + t.Fatalf("record not rewritten under the state key: %v", err) + } + if _, err := st.read(contentKey); err == nil { + t.Fatal("record still readable with the content key") + } +} diff --git a/cmd/cryptship-loader/exec_test.go b/cmd/cryptship-loader/exec_test.go new file mode 100644 index 0000000..8cf1b56 --- /dev/null +++ b/cmd/cryptship-loader/exec_test.go @@ -0,0 +1,238 @@ +package main + +import ( + "crypto/ed25519" + "crypto/rand" + "encoding/base64" + "encoding/json" + "fmt" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "testing" + + "github.com/AI2TH/cryptship/pkg/archive" + "github.com/AI2TH/cryptship/pkg/crypto" + "github.com/AI2TH/cryptship/pkg/license" +) + +// The loader's last step replaces the process, so end-to-end tests run it in +// a child: the test binary re-executes itself with CRYPTSHIP_TEST_LOADER set, +// and the child calls run() with the arguments in CRYPTSHIP_TEST_ARGS. +func TestMain(m *testing.M) { + if os.Getenv("CRYPTSHIP_TEST_LOADER") == "1" { + var args []string + _ = json.Unmarshal([]byte(os.Getenv("CRYPTSHIP_TEST_ARGS")), &args) + if err := run(parseConfig(args)); err != nil { + os.Stderr.WriteString("loader: " + err.Error() + "\n") + os.Exit(1) + } + os.Exit(0) + } + os.Exit(m.Run()) +} + +// loaderFixture writes a vendor key ring, a binding-"none" license and the +// encrypted payload of appDir, and returns the loader environment. +func loaderFixture(t *testing.T, appDir string) []string { + t.Helper() + work := t.TempDir() + pub, priv, _ := ed25519.GenerateKey(rand.Reader) + contentKey, _ := crypto.RandomKey() + tarBytes, err := archive.TarDir(appDir) + if err != nil { + t.Fatal(err) + } + blob, err := crypto.SealPayload(contentKey, tarBytes, "build-e2e") + if err != nil { + t.Fatal(err) + } + secret, _ := crypto.RandomKey() + c := license.Claims{Version: license.FormatVersion, LicenseID: "lic_e2e", CustomerID: "acme", Product: "w", + PayloadID: "build-e2e", SigningKeyID: "v1", Binding: license.Binding{Policy: license.BindNone}, UnboundSecret: secret} + bctx, _ := c.BindingContext() + kek, _ := crypto.DeriveKEK(secret, []byte(c.LicenseID), bctx) + wk, _ := crypto.WrapKey(kek, contentKey) + c.WrappedKeys = [][]byte{wk} + lic, err := license.Sign(priv, c) + if err != nil { + t.Fatal(err) + } + lb, _ := lic.Marshal() + files := map[string][]byte{ + "license.json": lb, "payload.enc": blob, + "vendor.pub": []byte(base64.StdEncoding.EncodeToString(pub) + "\n"), + } + for name, b := range files { + if err := os.WriteFile(filepath.Join(work, name), b, 0o644); err != nil { + t.Fatal(err) + } + } + return []string{ + "CRYPTSHIP_LICENSE=" + filepath.Join(work, "license.json"), + "CRYPTSHIP_PAYLOAD=" + filepath.Join(work, "payload.enc"), + "CRYPTSHIP_PUBKEY=" + filepath.Join(work, "vendor.pub"), + "CRYPTSHIP_STATE_DIR=" + filepath.Join(work, "state"), + } +} + +// runLoader runs the loader in a child process with env and args and returns +// its stdout; stderr is folded into the error. +func runLoader(t *testing.T, env []string, args ...string) (string, error) { + t.Helper() + a, _ := json.Marshal(args) + cmd := exec.Command(os.Args[0], "-test.run=^$") + cmd.Env = append(append(os.Environ(), env...), "CRYPTSHIP_TEST_LOADER=1", "CRYPTSHIP_TEST_ARGS="+string(a)) + var stderr strings.Builder + cmd.Stderr = &stderr + out, err := cmd.Output() + if err != nil { + err = fmt.Errorf("%w: %s", err, stderr.String()) + } + return string(out), err +} + +func requireLinuxRoot(t *testing.T) { + t.Helper() + if runtime.GOOS != "linux" || os.Geteuid() != 0 { + t.Skip("needs Linux and root (run the suite in the golang container)") + } +} + +// As root with CRYPTSHIP_APP_USER, the app runs as that user, can read and run +// its code, but cannot change it; the tree is owner:appgroup, 0750/0640. +func TestExtractAsSeparateOwnerAndDropPrivileges(t *testing.T) { + requireLinuxRoot(t) + app := t.TempDir() + if err := os.WriteFile(filepath.Join(app, "data.txt"), []byte("payload-data"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(app, "entrypoint"), []byte("#!/bin/sh\nid -u; id -g\n(echo x >> data.txt) 2>/dev/null && echo WRITABLE || echo READONLY\n(touch new) 2>/dev/null && echo CREATED || echo NOCREATE\ncat data.txt; echo\nstat -c '%u:%g %a' . data.txt entrypoint\n"), 0o755); err != nil { + t.Fatal(err) + } + // The extract dir's parent must be traversable by the app user. + parent := t.TempDir() + if err := os.Chmod(parent, 0o755); err != nil { + t.Fatal(err) + } + for d := filepath.Dir(parent); d != "/"; d = filepath.Dir(d) { + _ = os.Chmod(d, 0o755) + } + extract := filepath.Join(parent, "run", "app") + env := append(loaderFixture(t, app), "CRYPTSHIP_EXTRACT="+extract, "CRYPTSHIP_APP_USER=65532:65533") + + out, err := runLoader(t, env) + if err != nil { + t.Fatalf("loader: %v\n%s", err, out) + } + want := []string{"65532", "65533", "READONLY", "NOCREATE", "payload-data", "0:65533 750", "0:65533 640", "0:65533 750"} + if got := strings.Split(strings.TrimSpace(out), "\n"); strings.Join(got, "|") != strings.Join(want, "|") { + t.Fatalf("app output:\n%s\nwant %q", out, want) + } + + // A non-root uid as extract owner works too. + env = append(env, "CRYPTSHIP_EXTRACT_OWNER=65531") + if out, err = runLoader(t, env); err != nil || !strings.Contains(out, "65531:65533 640") { + t.Fatalf("custom owner: %v\n%s", err, out) + } +} + +func TestAppUserValidation(t *testing.T) { + for _, tc := range []struct{ user, owner string }{ + {"0", ""}, {"65532:0", ""}, {"65532", "65532"}, {"abc", ""}, {"-1", ""}, + } { + t.Setenv("CRYPTSHIP_APP_USER", tc.user) + t.Setenv("CRYPTSHIP_EXTRACT_OWNER", tc.owner) + if _, err := appUserFromEnv(); err == nil { + t.Errorf("user %q owner %q: accepted", tc.user, tc.owner) + } + } + t.Setenv("CRYPTSHIP_APP_USER", "") + if u, err := appUserFromEnv(); u != nil || err != nil { + t.Fatalf("unset: %v %v", u, err) + } + if os.Geteuid() != 0 { + t.Setenv("CRYPTSHIP_APP_USER", "65532") + t.Setenv("CRYPTSHIP_EXTRACT_OWNER", "") + if _, err := appUserFromEnv(); err == nil { + t.Fatal("accepted without root") + } + } +} + +// buildArgEchoer compiles a minimal static ELF that prints os.Args[1]. It +// ignores argv[0], so it runs correctly from a memfd regardless of the exe name. +func buildArgEchoer(t *testing.T, out string) { + t.Helper() + src := filepath.Join(t.TempDir(), "main.go") + const code = `package main + +import ( + "fmt" + "os" +) + +func main() { + if len(os.Args) > 1 { + fmt.Println(os.Args[1]) + } +} +` + if err := os.WriteFile(src, []byte(code), 0o644); err != nil { + t.Fatal(err) + } + cmd := exec.Command("go", "build", "-o", out, src) + cmd.Env = append(os.Environ(), "CGO_ENABLED=0") + if b, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("build arg-echoer: %v\n%s", err, b) + } +} + +// CRYPTSHIP_EXEC_MEMFD runs a single ELF binary from memory: nothing is +// extracted, and the extract dir is never created. +func TestExecMemfd(t *testing.T) { + if runtime.GOOS != "linux" || (runtime.GOARCH != "amd64" && runtime.GOARCH != "arm64") { + t.Skip("memfd mode is Linux amd64/arm64 only") + } + app := t.TempDir() + if err := os.MkdirAll(filepath.Join(app, "bin"), 0o755); err != nil { + t.Fatal(err) + } + // A tiny, self-contained ELF that echoes its first argument. Built here + // rather than borrowing the system `echo`: some echo / multi-call binaries + // reject a mismatched argv[0] when run from a memfd (argv[0]="tool" vs the + // exe path "/memfd:cryptship-app"), which made this test fail on some + // runners while passing on others. + buildArgEchoer(t, filepath.Join(app, "bin", "tool")) + if err := os.WriteFile(filepath.Join(app, "script.sh"), []byte("#!/bin/sh\necho hi\n"), 0o755); err != nil { + t.Fatal(err) + } + extract := filepath.Join(t.TempDir(), "never", "created") + env := append(loaderFixture(t, app), "CRYPTSHIP_EXTRACT="+extract, "CRYPTSHIP_EXEC_MEMFD=bin/tool") + + out, err := runLoader(t, env, "--", "tool", "hello-from-memfd") + if err != nil || strings.TrimSpace(out) != "hello-from-memfd" { + t.Fatalf("memfd exec: %v\n%s", err, out) + } + if _, err := os.Stat(extract); !os.IsNotExist(err) { + t.Fatal("memfd mode created the extract dir") + } + + // With a privilege drop too (root only). + if os.Geteuid() == 0 { + out, err = runLoader(t, append(env, "CRYPTSHIP_APP_USER=65532"), "--", "tool", "dropped") + if err != nil || strings.TrimSpace(out) != "dropped" { + t.Fatalf("memfd exec with privilege drop: %v\n%s", err, out) + } + } + + // Scripts and missing entries are refused with a clear error. + for _, entry := range []string{"script.sh", "missing"} { + bad := append(loaderFixture(t, app), "CRYPTSHIP_EXTRACT="+extract, "CRYPTSHIP_EXEC_MEMFD="+entry) + if out, err := runLoader(t, bad); err == nil { + t.Fatalf("%s: ran\n%s", entry, out) + } + } +} diff --git a/cmd/cryptship-loader/harden_linux.go b/cmd/cryptship-loader/harden_linux.go index 4bd3f71..c2637b6 100644 --- a/cmd/cryptship-loader/harden_linux.go +++ b/cmd/cryptship-loader/harden_linux.go @@ -2,10 +2,7 @@ package main -import "syscall" - -// PR_SET_DUMPABLE from . -const prSetDumpable = 4 +import "github.com/AI2TH/cryptship/pkg/harden" // hardenProcess marks the loader process non-dumpable (prctl PR_SET_DUMPABLE 0). // This blocks ptrace, process_vm_readv, and core dumps by a non-root user, and @@ -13,10 +10,9 @@ const prSetDumpable = 4 // payload while they live in the loader's memory. // // IMPORTANT: execve resets the dumpable flag, so this protects the loader / -// decrypt window, NOT the application after exec. To harden the running app, -// run the container as non-root with dropped capabilities and the seccomp -// profile in deploy/ (and, against host root, use a confidential-compute key -// provider — see docs/ARCHITECTURE.md). -func hardenProcess() { - _, _, _ = syscall.Syscall(syscall.SYS_PRCTL, prSetDumpable, 0, 0) -} +// decrypt window, NOT the application after exec. The application can set it +// again first thing (pkg/harden for Go, helpers/python/cryptship_harden.py for +// Python); also run the container as non-root with dropped capabilities and +// the seccomp profile in deploy/ (and, against host root, use a +// confidential-compute key provider — see docs/ARCHITECTURE.md). +func hardenProcess() { _ = harden.NonDumpable() } diff --git a/cmd/cryptship-loader/main.go b/cmd/cryptship-loader/main.go index aa88fa4..ac6a729 100644 --- a/cmd/cryptship-loader/main.go +++ b/cmd/cryptship-loader/main.go @@ -8,9 +8,7 @@ package main import ( - "bytes" "context" - "crypto/ed25519" "crypto/rand" "encoding/hex" "encoding/json" @@ -22,7 +20,6 @@ import ( "os" "os/exec" "path/filepath" - "strconv" "strings" "syscall" "time" @@ -39,7 +36,10 @@ type config struct { payloadPath string pubKeyPath string extractDir string - command []string + // memfdEntry, when set (CRYPTSHIP_EXEC_MEMFD), names a single ELF binary in + // the payload that is executed from a memfd; nothing is extracted. + memfdEntry string + command []string } func main() { @@ -63,6 +63,7 @@ func parseConfig(args []string) config { payloadPath: env("CRYPTSHIP_PAYLOAD", "/cryptship/payload.enc"), pubKeyPath: env("CRYPTSHIP_PUBKEY", "/cryptship/vendor.pub"), extractDir: env("CRYPTSHIP_EXTRACT", "/run/cryptship/app"), + memfdEntry: os.Getenv("CRYPTSHIP_EXEC_MEMFD"), } for i, a := range args { if a == "--" { @@ -77,6 +78,10 @@ func parseConfig(args []string) config { func run(cfg config) error { // 0. Mark the loader non-dumpable while the key/plaintext are in its memory. hardenProcess() + user, err := appUserFromEnv() + if err != nil { + return err + } // 1. Load and verify the license signature against the key ring. ringBytes, err := os.ReadFile(cfg.pubKeyPath) @@ -103,23 +108,33 @@ func run(cfg config) error { if lic.Claims.Expired(time.Now().Unix()) { return fmt.Errorf("license outside validity window (expires %d)", lic.Claims.NotAfter) } - if err := enforceRevocation(lic, ring); err != nil { - return err - } // 3. Acquire the content key via a provider matching the binding policy. provider, err := keyprovider.Select(lic, buildProviders(ring)...) if err != nil { return err } - contentKey, err := provider.ContentKey(context.Background(), lic) + keys, err := keyprovider.Unlock(context.Background(), provider, lic) if err != nil { return fmt.Errorf("key acquisition (%s): %w", provider.Name(), err) } - defer crypto.Wipe(contentKey) + defer keys.Wipe() + contentKey := keys.ContentKey + // Local state is sealed under the binding-derived state key. A + // third-party provider without one falls back to the content key. + stateKey := keys.StateKey + if stateKey == nil { + stateKey = contentKey + } + + // 3a. Optional revocation check. It runs after key acquisition so the + // grace record can be sealed under a key only this license unlocks. + if err := enforceRevocation(lic, ring, stateKey); err != nil { + return err + } // 3b. Refuse a clock set back to keep an expiring license alive. - if err := checkClock(stateDir(), lic.Claims, contentKey, time.Now()); err != nil { + if err := checkClock(stateDir(), lic.Claims, stateKey, contentKey, time.Now()); err != nil { return err } @@ -133,6 +148,20 @@ func run(cfg config) error { return fmt.Errorf("decrypt payload (tampered or wrong key): %w", err) } + // 5a. Single-binary payloads can run from memory with nothing on disk. + if cfg.memfdEntry != "" { + bin, err := archive.ReadFile(tarBytes, cfg.memfdEntry, archive.DefaultMaxExtractBytes) + crypto.Wipe(tarBytes) + if err != nil { + return fmt.Errorf("CRYPTSHIP_EXEC_MEMFD: %w", err) + } + argv := cfg.command + if len(argv) == 0 { + argv = []string{filepath.Base(cfg.memfdEntry)} + } + return execMemfd(bin, argv, appEnv(lic.Claims), user) + } + // 5. Extract into the runtime dir, enforcing/warning tmpfs before writing. if err := prepareDir(cfg.extractDir); err != nil { return err @@ -147,9 +176,15 @@ func run(cfg config) error { if err := writeManifest(cfg.extractDir, lic.Claims); err != nil { fmt.Fprintln(os.Stderr, "cryptship-loader: WARNING: manifest not written:", err) } + if user != nil { + // The app may read and run its code, but not change it. + if err := secureTree(cfg.extractDir, user.owner, user.gid); err != nil { + return fmt.Errorf("hand extract dir to uid %d, gid %d: %w", user.owner, user.gid, err) + } + } // 6. Hand off to the real application. - return execApp(cfg, lic.Claims) + return execApp(cfg, lic.Claims, user) } // manifestName is the per-license watermark written after extraction. Its @@ -197,29 +232,39 @@ func hasEntry(entries []os.DirEntry, name string) bool { return false } -func execApp(cfg config, c license.Claims) error { +func execApp(cfg config, c license.Claims, user *appUser) error { if len(cfg.command) == 0 { cfg.command = []string{"./entrypoint"} } if err := os.Chdir(cfg.extractDir); err != nil { return fmt.Errorf("chdir: %w", err) } + if user != nil { + if err := dropPrivileges(user); err != nil { + return err + } + } resolved, err := exec.LookPath(cfg.command[0]) if err != nil { return fmt.Errorf("locate command %q: %w", cfg.command[0], err) } + return syscall.Exec(resolved, cfg.command, appEnv(c)) +} + +// appEnv is the app's environment: the loader's, minus CRYPTSHIP_* settings, +// plus the license watermark variables. +func appEnv(c license.Claims) []string { var envv []string for _, kv := range os.Environ() { if !strings.HasPrefix(kv, "CRYPTSHIP_") { envv = append(envv, kv) } } - envv = append(envv, + return append(envv, "CRYPTSHIP_LICENSE_ID="+c.LicenseID, "CRYPTSHIP_CUSTOMER="+c.CustomerID, "CRYPTSHIP_PAYLOAD_ID="+c.PayloadID, ) - return syscall.Exec(resolved, cfg.command, envv) } // writeManifest drops a per-license marker into the extracted tree so a leaked @@ -260,35 +305,46 @@ func stateFile(kind, licenseID string) string { // an id cannot point a state file outside the state dir. func escapeID(licenseID string) string { return url.PathEscape(licenseID) } +// revocationStateInfo labels the sealed revocation-grace record. +const revocationStateInfo = "cryptship/revocation-grace/v1" + // enforceRevocation performs the optional revocation check and applies the // operator's failure policy (CRYPTSHIP_REVOCATION): "open" (default, run on -// error), "closed" (refuse on error), "grace:" (run if a prior successful -// check is younger than ). Only "closed" is a hard guarantee. +// error), "closed" (refuse on error), "grace:" (run if a prior signed +// "not revoked" answer is younger than ). Only "closed" is a hard +// guarantee. // // A cryptship-server signs the check response; the loader verifies it against // the vendor key ring, so an on-path attacker or captive portal cannot forge -// "not revoked". In "closed" mode an UNSIGNED "active" answer is rejected. -func enforceRevocation(lic *license.License, ring license.KeyRing) error { +// "not revoked". In "closed" mode an UNSIGNED "active" answer is rejected, and +// only a signed answer refreshes the grace record. +// +// The time of the last signed success is sealed (AES-GCM) under a key derived +// from secret, so editing or copying the record does not extend grace, and a +// record dated in the future (clock set back) does not count. +func enforceRevocation(lic *license.License, ring license.KeyRing, secret []byte) error { if lic.Claims.RevocationURL == "" { return nil } mode := env("CRYPTSHIP_REVOCATION", "open") - sf := stateFile("revocation", lic.Claims.LicenseID) + st := sealedState{info: revocationStateInfo, licenseID: lic.Claims.LicenseID, path: stateFile("revocation", lic.Claims.LicenseID)} revoked, signed, err := checkRevocation(lic, ring) if err == nil && revoked { return fmt.Errorf("license %s has been revoked", lic.Claims.LicenseID) } - if err == nil && (signed || mode != "closed") { + if err == nil && signed { // Only a SIGNED "active" answer refreshes the grace clock; an unsigned // answer (captive portal / on-path) must not be able to extend grace. - if signed { - _ = os.MkdirAll(filepath.Dir(sf), 0o700) - _ = os.WriteFile(sf, []byte(strconv.FormatInt(time.Now().Unix(), 10)), 0o600) + if werr := st.write(secret, time.Now().Unix()); werr != nil && strings.HasPrefix(mode, "grace:") { + warnf("revocation grace record not written: %v", werr) } return nil } - if err == nil && !signed && mode == "closed" { + if err == nil { // an unsigned "not revoked": accepted unless closed, never refreshes grace + if mode != "closed" { + return nil + } err = errors.New("revocation response was unsigned; CRYPTSHIP_REVOCATION=closed requires a signing cryptship-server") } @@ -296,30 +352,37 @@ func enforceRevocation(lic *license.License, ring license.KeyRing) error { case mode == "closed": return fmt.Errorf("revocation check failed and CRYPTSHIP_REVOCATION=closed: %w", err) case strings.HasPrefix(mode, "grace:"): - if d, perr := time.ParseDuration(strings.TrimPrefix(mode, "grace:")); perr == nil { - if b, rerr := os.ReadFile(sf); rerr == nil { - // A last-success time in the future (clock set back, or a - // hand-edited file) does not count as within grace. - if last, lerr := strconv.ParseInt(strings.TrimSpace(string(b)), 10, 64); lerr == nil && - last <= time.Now().Unix()+license.ClockSkew && - time.Since(time.Unix(last, 0)) <= d { - fmt.Fprintln(os.Stderr, "cryptship-loader: revocation check failed, within grace:", err) - return nil - } + d, perr := time.ParseDuration(strings.TrimPrefix(mode, "grace:")) + if perr != nil { + return fmt.Errorf("bad CRYPTSHIP_REVOCATION %q: %w", mode, perr) + } + last, rerr := st.read(secret) + now := time.Now().Unix() + switch { + case rerr != nil: + if errors.Is(rerr, errBadState) { + warnf("revocation grace record %s is not valid for this license; ignored", st.path) } + case last > now+license.ClockSkew: + // A last success in the future (clock set back) is not within grace. + warnf("revocation grace record %s is dated in the future; ignored", st.path) + case time.Since(time.Unix(last, 0)) <= d: + fmt.Fprintln(os.Stderr, "cryptship-loader: revocation check failed, within grace:", err) + return nil } - return fmt.Errorf("revocation check failed and no successful check within grace: %w", err) + return fmt.Errorf("revocation check failed and no signed successful check within grace: %w", err) default: fmt.Fprintln(os.Stderr, "cryptship-loader: revocation check skipped (fail-open):", err) return nil } } -// checkRevocation returns (revoked, signed, err). It sends a nonce and, if the -// server returns a signed online.CheckResponse that verifies against the ring -// and echoes the nonce, trusts its Revoked flag (signed=true). Otherwise it -// falls back to the legacy status-code reading (200 active / 410 revoked), -// reported as unsigned. +// checkRevocation returns (revoked, signed, err). It sends a nonce; a signed +// online.CheckResponse that verifies against the ring and answers this license +// and nonce is authoritative (signed=true). A signed answer that fails to +// verify or has expired is an error. Anything else (not a signed answer, or +// one for another license or nonce) falls back to the legacy status-code +// reading (200 active / 410 revoked), reported as unsigned. func checkRevocation(lic *license.License, ring license.KeyRing) (revoked bool, signed bool, err error) { u, err := url.Parse(lic.Claims.RevocationURL) if err != nil { @@ -345,23 +408,17 @@ func checkRevocation(lic *license.License, ring license.KeyRing) (revoked bool, return false, false, err } defer resp.Body.Close() - body, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10)) - - // Signed path: a valid, nonce-matching CheckResponse is authoritative. - var cr online.CheckResponse - if json.Unmarshal(body, &cr) == nil && len(cr.Sig) > 0 && - cr.LicenseID == lic.Claims.LicenseID && bytes.Equal(cr.Nonce, nonce) { - pub, ok := ring[cr.SigningKeyID] - if !ok { - pub, ok = ring["*"] - } - if !ok || len(pub) != ed25519.PublicKeySize || !ed25519.Verify(pub, online.CheckSigningInput(cr), cr.Sig) { - return false, false, errors.New("revocation check: bad response signature") - } - if cr.NotAfter != 0 && time.Now().Unix() > cr.NotAfter { - return false, false, errors.New("revocation check: response expired") - } + body, err := io.ReadAll(io.LimitReader(resp.Body, online.MaxResponseBytes)) + if err != nil { + return false, false, err + } + + cr, verr := online.VerifyCheckResponse(body, ring, lic.Claims.LicenseID, nonce, time.Now()) + switch { + case verr == nil: return cr.Revoked, true, nil + case !errors.Is(verr, online.ErrUnsigned) && !errors.Is(verr, online.ErrMismatch): + return false, false, fmt.Errorf("revocation check: %w", verr) } // Legacy unsigned path. diff --git a/cmd/cryptship-loader/main_test.go b/cmd/cryptship-loader/main_test.go index 70ebde6..6860e22 100644 --- a/cmd/cryptship-loader/main_test.go +++ b/cmd/cryptship-loader/main_test.go @@ -1,13 +1,9 @@ package main import ( - "net/http" - "net/http/httptest" "os" "path/filepath" - "strconv" "testing" - "time" "github.com/AI2TH/cryptship/pkg/license" ) @@ -82,33 +78,3 @@ func TestStateFileStaysInStateDir(t *testing.T) { t.Errorf("plain id renamed: %s", got) } } - -// grace: a last-success time in the future is not "within grace". -func TestRevocationGraceIgnoresFutureTimestamp(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.WriteHeader(http.StatusServiceUnavailable) - })) - defer ts.Close() - dir := t.TempDir() - t.Setenv("CRYPTSHIP_STATE_DIR", dir) - t.Setenv("CRYPTSHIP_REVOCATION", "grace:1h") - lic := &license.License{Claims: license.Claims{LicenseID: "lic_g", RevocationURL: ts.URL}} - write := func(ts int64) { - if err := os.WriteFile(stateFile("revocation", "lic_g"), []byte(strconv.FormatInt(ts, 10)), 0o600); err != nil { - t.Fatal(err) - } - } - - write(time.Now().Add(-10 * time.Minute).Unix()) - if err := enforceRevocation(lic, license.KeyRing{}); err != nil { - t.Fatalf("recent success should be within grace: %v", err) - } - write(time.Now().Add(-2 * time.Hour).Unix()) - if err := enforceRevocation(lic, license.KeyRing{}); err == nil { - t.Fatal("old success accepted as within grace") - } - write(time.Now().Add(365 * 24 * time.Hour).Unix()) - if err := enforceRevocation(lic, license.KeyRing{}); err == nil { - t.Fatal("future success timestamp accepted as within grace") - } -} diff --git a/cmd/cryptship-loader/memfd_linux.go b/cmd/cryptship-loader/memfd_linux.go new file mode 100644 index 0000000..872b2a7 --- /dev/null +++ b/cmd/cryptship-loader/memfd_linux.go @@ -0,0 +1,69 @@ +//go:build linux + +package main + +import ( + "errors" + "fmt" + "os" + "syscall" + "unsafe" + + "github.com/AI2TH/cryptship/pkg/crypto" +) + +// From and . +const ( + mfdCloexec = 0x1 + mfdAllowSealing = 0x2 + fAddSeals = 1033 + fSealSeal = 0x1 + fSealShrink = 0x2 + fSealGrow = 0x4 + fSealWrite = 0x8 +) + +// execMemfd runs a single-binary payload without writing it to any +// filesystem: the binary goes into an anonymous memfd, which is sealed +// against changes and executed through /proc/self/fd (what fexecve does). bin +// is wiped once copied. The memfd is close-on-exec, so this works for ELF +// executables; a "#!" script cannot be run this way (its interpreter could +// not open the closed fd). +// +// The binary still lives in RAM, and /proc//exe of the running app reads +// it back for anyone allowed to ptrace the app (its own uid, root): have the +// app make itself non-dumpable again (pkg/harden, cryptship_harden.py). +func execMemfd(bin []byte, argv, envv []string, user *appUser) error { + defer crypto.Wipe(bin) + if sysMemfdCreate == 0 { + return errors.New("CRYPTSHIP_EXEC_MEMFD: memfd_create is not wired up for this architecture") + } + if len(bin) < 4 || string(bin[:4]) != "\x7fELF" { + return errors.New("CRYPTSHIP_EXEC_MEMFD: the entry is not an ELF executable (scripts cannot run from a close-on-exec memfd)") + } + name, _ := syscall.BytePtrFromString("cryptship-app") + r, _, errno := syscall.Syscall(sysMemfdCreate, uintptr(unsafe.Pointer(name)), mfdCloexec|mfdAllowSealing, 0) + if errno != 0 { + return fmt.Errorf("memfd_create: %w", errno) + } + fd := int(r) + f := os.NewFile(uintptr(fd), "memfd:cryptship-app") + if _, err := f.Write(bin); err != nil { + f.Close() + return fmt.Errorf("write memfd: %w", err) + } + if _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), fAddSeals, fSealSeal|fSealShrink|fSealGrow|fSealWrite); errno != 0 { + f.Close() + return fmt.Errorf("seal memfd: %w", errno) + } + if user != nil { + if err := dropPrivileges(user); err != nil { + f.Close() + return err + } + } + crypto.Wipe(bin) + err := syscall.Exec(fmt.Sprintf("/proc/self/fd/%d", fd), argv, envv) + f.Close() + return fmt.Errorf("exec memfd: %w", err) +} diff --git a/cmd/cryptship-loader/memfd_linux_amd64.go b/cmd/cryptship-loader/memfd_linux_amd64.go new file mode 100644 index 0000000..61e94bf --- /dev/null +++ b/cmd/cryptship-loader/memfd_linux_amd64.go @@ -0,0 +1,6 @@ +//go:build linux && amd64 + +package main + +// memfd_create on linux/amd64 (the stdlib syscall package predates it). +const sysMemfdCreate = 319 diff --git a/cmd/cryptship-loader/memfd_linux_arm64.go b/cmd/cryptship-loader/memfd_linux_arm64.go new file mode 100644 index 0000000..1611563 --- /dev/null +++ b/cmd/cryptship-loader/memfd_linux_arm64.go @@ -0,0 +1,7 @@ +//go:build linux && arm64 + +package main + +import "syscall" + +const sysMemfdCreate = syscall.SYS_MEMFD_CREATE diff --git a/cmd/cryptship-loader/memfd_linux_otherarch.go b/cmd/cryptship-loader/memfd_linux_otherarch.go new file mode 100644 index 0000000..4c1ea31 --- /dev/null +++ b/cmd/cryptship-loader/memfd_linux_otherarch.go @@ -0,0 +1,6 @@ +//go:build linux && !amd64 && !arm64 + +package main + +// memfd mode is wired up for amd64 and arm64 only. +const sysMemfdCreate = 0 diff --git a/cmd/cryptship-loader/memfd_other.go b/cmd/cryptship-loader/memfd_other.go new file mode 100644 index 0000000..fd4430f --- /dev/null +++ b/cmd/cryptship-loader/memfd_other.go @@ -0,0 +1,9 @@ +//go:build !linux + +package main + +import "errors" + +func execMemfd([]byte, []string, []string, *appUser) error { + return errors.New("CRYPTSHIP_EXEC_MEMFD is only supported on Linux") +} diff --git a/cmd/cryptship-loader/privileges.go b/cmd/cryptship-loader/privileges.go new file mode 100644 index 0000000..7d319ed --- /dev/null +++ b/cmd/cryptship-loader/privileges.go @@ -0,0 +1,63 @@ +package main + +import ( + "errors" + "fmt" + "os" + "strconv" + "strings" +) + +// appUser is the identity the application runs as when the loader starts as +// root (CRYPTSHIP_APP_USER). The extracted tree is then owned by a different +// uid (CRYPTSHIP_EXTRACT_OWNER, default root) and the app's group, mode +// 0750/0640: the app can read and run its code but not change it, and no +// other non-root user can read it. +type appUser struct { + uid, gid int + owner int // owner uid of the extracted files +} + +// appUserFromEnv parses CRYPTSHIP_APP_USER ("uid" or "uid:gid") and +// CRYPTSHIP_EXTRACT_OWNER. It returns nil when CRYPTSHIP_APP_USER is unset. +// Setting it without running as root is an error, never a silent fallback. +func appUserFromEnv() (*appUser, error) { + v := os.Getenv("CRYPTSHIP_APP_USER") + if v == "" { + return nil, nil + } + uidStr, gidStr, hasGid := strings.Cut(v, ":") + uid, err := parseID(uidStr) + if err != nil { + return nil, fmt.Errorf("CRYPTSHIP_APP_USER: %w", err) + } + gid := uid + if hasGid { + if gid, err = parseID(gidStr); err != nil { + return nil, fmt.Errorf("CRYPTSHIP_APP_USER: %w", err) + } + } + owner := 0 + if o := os.Getenv("CRYPTSHIP_EXTRACT_OWNER"); o != "" { + if owner, err = parseID(o); err != nil { + return nil, fmt.Errorf("CRYPTSHIP_EXTRACT_OWNER: %w", err) + } + } + switch { + case uid == 0 || gid == 0: + return nil, errors.New("CRYPTSHIP_APP_USER must name a non-root uid and gid") + case owner == uid: + return nil, errors.New("CRYPTSHIP_EXTRACT_OWNER must differ from the app uid, or the app could change its own code") + case os.Geteuid() != 0: + return nil, fmt.Errorf("CRYPTSHIP_APP_USER is set, but the loader runs as uid %d; it must start as root to extract as another uid and drop privileges", os.Geteuid()) + } + return &appUser{uid: uid, gid: gid, owner: owner}, nil +} + +func parseID(s string) (int, error) { + n, err := strconv.Atoi(strings.TrimSpace(s)) + if err != nil || n < 0 || n > 1<<31-2 { + return 0, fmt.Errorf("bad numeric id %q", s) + } + return n, nil +} diff --git a/cmd/cryptship-loader/privileges_linux.go b/cmd/cryptship-loader/privileges_linux.go new file mode 100644 index 0000000..daa8efa --- /dev/null +++ b/cmd/cryptship-loader/privileges_linux.go @@ -0,0 +1,59 @@ +//go:build linux + +package main + +import ( + "fmt" + "io/fs" + "os" + "path/filepath" + "syscall" +) + +// secureTree gives every entry under dir (and dir itself) to owner:gid: +// directories and executables 0750, other files 0640, symlinks re-owned only. +// It runs as root before the app starts, on a tree only root has written. The +// mode is set before the owner, while root still owns the entry, so the only +// capability needed is CAP_CHOWN (plus CAP_SETUID and CAP_SETGID to drop). +func secureTree(dir string, owner, gid int) error { + return filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.Type()&fs.ModeSymlink == 0 { + fi, err := d.Info() + if err != nil { + return err + } + mode := fs.FileMode(0o640) + if d.IsDir() || fi.Mode().Perm()&0o111 != 0 { + mode = 0o750 + } + if err := os.Chmod(p, mode); err != nil { + return err + } + } + return os.Lchown(p, owner, gid) + }) +} + +// dropPrivileges switches the whole process (all threads) to the app's uid +// and gid with no supplementary groups, then checks root cannot be regained. +func dropPrivileges(u *appUser) error { + if err := syscall.Setgroups([]int{}); err != nil { + return fmt.Errorf("setgroups: %w", err) + } + if err := syscall.Setgid(u.gid); err != nil { + return fmt.Errorf("setgid %d: %w", u.gid, err) + } + if err := syscall.Setuid(u.uid); err != nil { + return fmt.Errorf("setuid %d: %w", u.uid, err) + } + if os.Getuid() != u.uid || os.Geteuid() != u.uid || os.Getgid() != u.gid || os.Getegid() != u.gid { + return fmt.Errorf("privilege drop incomplete: uid %d/%d gid %d/%d", os.Getuid(), os.Geteuid(), os.Getgid(), os.Getegid()) + } + if err := syscall.Setuid(0); err == nil { + return fmt.Errorf("privilege drop failed: root can be regained") + } + return nil +} diff --git a/cmd/cryptship-loader/privileges_other.go b/cmd/cryptship-loader/privileges_other.go new file mode 100644 index 0000000..7c919e6 --- /dev/null +++ b/cmd/cryptship-loader/privileges_other.go @@ -0,0 +1,10 @@ +//go:build !linux + +package main + +import "errors" + +var errNotLinux = errors.New("CRYPTSHIP_APP_USER is only supported on Linux") + +func secureTree(string, int, int) error { return errNotLinux } +func dropPrivileges(*appUser) error { return errNotLinux } diff --git a/cmd/cryptship-loader/revocation_test.go b/cmd/cryptship-loader/revocation_test.go new file mode 100644 index 0000000..4ff7b9b --- /dev/null +++ b/cmd/cryptship-loader/revocation_test.go @@ -0,0 +1,209 @@ +package main + +import ( + "crypto/ed25519" + "crypto/rand" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "strconv" + "strings" + "testing" + "time" + + "github.com/AI2TH/cryptship/pkg/crypto" + "github.com/AI2TH/cryptship/pkg/license" + "github.com/AI2TH/cryptship/pkg/licenseserver" + "github.com/AI2TH/cryptship/pkg/online" +) + +// revocationFixture is a signing cryptship-server plus a license that points +// its revocation_url at it. +type revocationFixture struct { + srv *licenseserver.Server + ts *httptest.Server + ring license.KeyRing + lic *license.License + secret []byte +} + +func newRevocationFixture(t *testing.T) *revocationFixture { + t.Helper() + pub, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + srv := &licenseserver.Server{Master: make([]byte, 32), SignKey: priv, KeyID: "v1", Revoked: map[string]bool{}} + ts := httptest.NewServer(srv.Handler()) + t.Cleanup(ts.Close) + secret, _ := crypto.RandomKey() + t.Setenv("CRYPTSHIP_STATE_DIR", t.TempDir()) + return &revocationFixture{ + srv: srv, ts: ts, ring: license.KeyRing{"v1": pub}, secret: secret, + lic: &license.License{Claims: license.Claims{LicenseID: "lic_r", RevocationURL: ts.URL + "/v1/check"}}, + } +} + +func (f *revocationFixture) graceState() sealedState { + return sealedState{info: revocationStateInfo, licenseID: f.lic.Claims.LicenseID, path: stateFile("revocation", f.lic.Claims.LicenseID)} +} + +func TestRevocationSignedAnswer(t *testing.T) { + f := newRevocationFixture(t) + for _, mode := range []string{"open", "closed", "grace:1h"} { + t.Setenv("CRYPTSHIP_REVOCATION", mode) + if err := enforceRevocation(f.lic, f.ring, f.secret); err != nil { + t.Fatalf("%s: active license refused: %v", mode, err) + } + } + if _, err := f.graceState().read(f.secret); err != nil { + t.Fatalf("a signed success must record a sealed grace time: %v", err) + } + f.srv.Revoked["lic_r"] = true + for _, mode := range []string{"open", "closed", "grace:1h"} { + t.Setenv("CRYPTSHIP_REVOCATION", mode) + if err := enforceRevocation(f.lic, f.ring, f.secret); err == nil || !strings.Contains(err.Error(), "revoked") { + t.Fatalf("%s: revoked license: err = %v", mode, err) + } + } +} + +// An on-path attacker answers in place of the server. +func TestRevocationRejectsForgedAndReplayedAnswers(t *testing.T) { + f := newRevocationFixture(t) + f.srv.Revoked["lic_r"] = true + + // Capture one genuine signed answer ("revoked: true") for a fixed nonce. + resp, err := http.Get(f.ts.URL + "/v1/check?license_id=lic_r&nonce=00112233445566778899aabbccddeeff") + if err != nil { + t.Fatal(err) + } + var captured online.CheckResponse + if err := json.NewDecoder(resp.Body).Decode(&captured); err != nil { + t.Fatal(err) + } + resp.Body.Close() + + attackers := map[string]func(w http.ResponseWriter, r *http.Request){ + "captive portal 200": func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }, + "flipped revoked flag": func(w http.ResponseWriter, r *http.Request) { + c := captured + c.Nonce = mustHex(t, r.URL.Query().Get("nonce")) + c.Revoked = false // signature no longer matches + _ = json.NewEncoder(w).Encode(c) + }, + "replayed answer": func(w http.ResponseWriter, _ *http.Request) { + _ = json.NewEncoder(w).Encode(captured) // genuine, but for another nonce + }, + "unknown signer": func(w http.ResponseWriter, r *http.Request) { + _, other, _ := ed25519.GenerateKey(rand.Reader) + c := online.CheckResponse{LicenseID: "lic_r", Nonce: mustHex(t, r.URL.Query().Get("nonce")), NotAfter: time.Now().Add(time.Hour).Unix()} + online.SignCheck(other, "v1", &c) + _ = json.NewEncoder(w).Encode(c) + }, + } + for name, h := range attackers { + ts := httptest.NewServer(http.HandlerFunc(h)) + f.lic.Claims.RevocationURL = ts.URL + t.Setenv("CRYPTSHIP_REVOCATION", "closed") + if err := enforceRevocation(f.lic, f.ring, f.secret); err == nil { + t.Errorf("%s: forged answer accepted in closed mode", name) + } + // In open / grace mode an unsigned answer may let the app run, but + // it must never refresh the grace record. + t.Setenv("CRYPTSHIP_REVOCATION", "grace:1h") + _ = enforceRevocation(f.lic, f.ring, f.secret) + ts.Close() + } + if _, err := os.Stat(f.graceState().path); !os.IsNotExist(err) { + t.Fatal("a forged answer wrote a grace record") + } +} + +func TestRevocationUnsignedNeverRefreshesGrace(t *testing.T) { + f := newRevocationFixture(t) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) })) + defer ts.Close() + f.lic.Claims.RevocationURL = ts.URL + for _, mode := range []string{"open", "grace:1h"} { + t.Setenv("CRYPTSHIP_REVOCATION", mode) + if err := enforceRevocation(f.lic, f.ring, f.secret); err != nil { + t.Fatalf("%s mode accepts an unsigned 200: %v", mode, err) + } + } + t.Setenv("CRYPTSHIP_REVOCATION", "closed") + if err := enforceRevocation(f.lic, f.ring, f.secret); err == nil { + t.Fatal("closed mode accepted an unsigned 200") + } + if _, err := os.Stat(f.graceState().path); !os.IsNotExist(err) { + t.Fatal("an unsigned answer wrote a grace record") + } +} + +func TestRevocationGraceIsSealed(t *testing.T) { + f := newRevocationFixture(t) + down := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusServiceUnavailable) + })) + defer down.Close() + f.lic.Claims.RevocationURL = down.URL + t.Setenv("CRYPTSHIP_REVOCATION", "grace:1h") + st := f.graceState() + write := func(ts int64) { + t.Helper() + if err := st.write(f.secret, ts); err != nil { + t.Fatal(err) + } + } + + write(time.Now().Add(-10 * time.Minute).Unix()) + if err := enforceRevocation(f.lic, f.ring, f.secret); err != nil { + t.Fatalf("recent signed success should be within grace: %v", err) + } + write(time.Now().Add(-2 * time.Hour).Unix()) + if err := enforceRevocation(f.lic, f.ring, f.secret); err == nil { + t.Fatal("old success accepted as within grace") + } + write(time.Now().Add(365 * 24 * time.Hour).Unix()) + if err := enforceRevocation(f.lic, f.ring, f.secret); err == nil { + t.Fatal("future success timestamp accepted as within grace") + } + + // A hand-written plain-text timestamp (the old format) is not trusted. + if err := os.WriteFile(st.path, []byte(strconv.FormatInt(time.Now().Unix(), 10)), 0o600); err != nil { + t.Fatal(err) + } + if err := enforceRevocation(f.lic, f.ring, f.secret); err == nil { + t.Fatal("plain-text grace record accepted") + } + + // A record sealed for another license, or under another key, is not trusted. + other := sealedState{info: revocationStateInfo, licenseID: "lic_other", path: st.path} + if err := other.write(f.secret, time.Now().Unix()); err != nil { + t.Fatal(err) + } + if err := enforceRevocation(f.lic, f.ring, f.secret); err == nil { + t.Fatal("grace record of another license accepted") + } + otherKey, _ := crypto.RandomKey() + if err := st.write(otherKey, time.Now().Unix()); err != nil { + t.Fatal(err) + } + if err := enforceRevocation(f.lic, f.ring, f.secret); err == nil { + t.Fatal("grace record sealed under another key accepted") + } +} + +func mustHex(t *testing.T, s string) []byte { + t.Helper() + b := make([]byte, len(s)/2) + for i := range b { + v, err := strconv.ParseUint(s[2*i:2*i+2], 16, 8) + if err != nil { + t.Fatal(err) + } + b[i] = byte(v) + } + return b +} diff --git a/cmd/cryptship-loader/state.go b/cmd/cryptship-loader/state.go new file mode 100644 index 0000000..f9f0b06 --- /dev/null +++ b/cmd/cryptship-loader/state.go @@ -0,0 +1,91 @@ +package main + +import ( + "crypto/hkdf" + "crypto/sha256" + "encoding/binary" + "errors" + "os" + "path/filepath" + + "github.com/AI2TH/cryptship/pkg/crypto" +) + +// Loader state (the revocation-grace and clock-rollback records) is a single +// timestamp per license, sealed with AES-256-GCM so it cannot be edited or +// moved between licenses without the key. Each record kind has its own key and +// AAD, both derived from the per-license secret the caller passes in. + +// errBadState means a state record exists but does not open under this +// license's key (edited, copied from another license, or an older format). +var errBadState = errors.New("state record is not valid for this license") + +// sealedState names one kind of sealed per-license timestamp record. +type sealedState struct { + info string // HKDF info and AAD prefix, e.g. "cryptship/clock-state/v1" + licenseID string + path string +} + +func (s sealedState) key(secret []byte) ([]byte, error) { + return hkdf.Key(sha256.New, secret, []byte(s.licenseID), s.info, crypto.KeySize) +} + +func (s sealedState) aad() []byte { return []byte(s.info + "\x00" + s.licenseID) } + +// read returns the sealed timestamp. A missing file returns an error that +// satisfies errors.Is(err, fs.ErrNotExist); a record that does not open +// returns errBadState. +func (s sealedState) read(secret []byte) (int64, error) { + blob, err := os.ReadFile(s.path) + if err != nil { + return 0, err + } + key, err := s.key(secret) + if err != nil { + return 0, err + } + defer crypto.Wipe(key) + pt, err := crypto.Open(key, blob, s.aad()) + if err != nil || len(pt) != 8 { + return 0, errBadState + } + return int64(binary.BigEndian.Uint64(pt)), nil +} + +// write seals t and replaces the record atomically. +func (s sealedState) write(secret []byte, t int64) error { + key, err := s.key(secret) + if err != nil { + return err + } + defer crypto.Wipe(key) + var b [8]byte + binary.BigEndian.PutUint64(b[:], uint64(t)) + sealed, err := crypto.Seal(key, b[:], s.aad()) + if err != nil { + return err + } + return writeFileAtomic(s.path, sealed) +} + +// writeFileAtomic writes via a temp file and rename, so a crash never leaves +// a truncated state file. +func writeFileAtomic(path string, data []byte) error { + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { + return err + } + tmp, err := os.CreateTemp(filepath.Dir(path), ".tmp-"+filepath.Base(path)+"-*") + if err != nil { + return err + } + defer os.Remove(tmp.Name()) + if _, err := tmp.Write(data); err != nil { + tmp.Close() + return err + } + if err := tmp.Close(); err != nil { + return err + } + return os.Rename(tmp.Name(), path) +} diff --git a/cmd/cryptship-server/main.go b/cmd/cryptship-server/main.go index ae7b055..ada27f0 100644 --- a/cmd/cryptship-server/main.go +++ b/cmd/cryptship-server/main.go @@ -22,6 +22,7 @@ import ( "time" "github.com/AI2TH/cryptship/pkg/attest" + "github.com/AI2TH/cryptship/pkg/license" "github.com/AI2TH/cryptship/pkg/licenseserver" ) @@ -33,6 +34,11 @@ func main() { ttl := flag.Duration("ttl", 24*time.Hour, "validity of each key answer") revoke := flag.String("revoke", "", "comma-separated license ids to deny (static)") revokeFile := flag.String("revoke-file", "", "path to a live denylist (one license id per line); edit it to revoke without restarting") + noLegacy := flag.Bool("no-legacy-secrets", false, "refuse licenses issued without binding.secret_v 2 (their secret does not bind the policy, so this server could release attested secrets)") + enforceSeats := flag.Bool("enforce-seats", false, "enforce binding.max distinct machines per license (loaders send their signed license and machine fingerprint)") + seatLease := flag.Duration("seat-lease", 30*24*time.Hour, "with --enforce-seats: how long a machine keeps its seat after its last start") + seatsFile := flag.String("seats-file", "", "with --enforce-seats: persist seats in this JSON file") + licenseKeys := flag.String("license-keys", "", "with --enforce-seats: vendor public key / key ring that signs licenses (default: the public half of --sign)") requireAtt := flag.Bool("require-attestation", false, "require verified TEE attestation (fails to start: no verifier is built in yet)") flag.Parse() @@ -58,12 +64,23 @@ func main() { TTL: *ttl, Revoked: revoked, RevokedFile: *revokeFile, + + NoLegacySecrets: *noLegacy, + } + if *enforceSeats { + srv.Seats = &licenseserver.SeatTracker{Lease: *seatLease, Path: *seatsFile} + if *licenseKeys != "" { + b, err := os.ReadFile(*licenseKeys) + must(err, "read --license-keys") + srv.LicenseKeys, err = license.ParseKeyRing(b) + must(err, "parse --license-keys") + } } verifier, err := attestationVerifier(*requireAtt, builtinVerifier()) must(err, "--require-attestation") srv.Verifier = verifier - fmt.Fprintf(os.Stderr, "cryptship-server listening on %s (attestation required: %v)\n", *addr, *requireAtt) + fmt.Fprintf(os.Stderr, "cryptship-server listening on %s (attestation required: %v, seats enforced: %v)\n", *addr, *requireAtt, *enforceSeats) if err := http.ListenAndServe(*addr, srv.Handler()); err != nil { must(err, "serve") } diff --git a/cmd/cryptship/main.go b/cmd/cryptship/main.go index 6fd8723..69cf8e6 100644 --- a/cmd/cryptship/main.go +++ b/cmd/cryptship/main.go @@ -229,7 +229,8 @@ func cmdIssue(args []string) error { customer := fs.String("customer", "", "customer id/name (required)") product := fs.String("product", "", "product name (required)") edition := fs.String("edition", "", "edition/tier") - expires := fs.String("expires", "", "expiry date YYYY-MM-DD") + expires := fs.String("expires", "", "expiry date YYYY-MM-DD: the license expires at 00:00:00 UTC that day (see --expires-end-of-day)") + endOfDay := fs.Bool("expires-end-of-day", false, "with --expires: stay valid through 23:59:59 UTC of that date") days := fs.Int("days", 0, "expiry as N days from now (alternative to --expires)") binding := fs.String("binding", "", "binding policy: none|strict|pool|online|attested (required)") server := fs.String("server", "", "license/key-broker server URL (online/attested binding)") @@ -237,6 +238,8 @@ func cmdIssue(args []string) error { revURL := fs.String("revocation-url", "", "optional revocation check URL") out := fs.String("out", "license.json", "output license path") licVersion := fs.Int("license-version", license.FormatVersion, "license format version: 2 binds every claim into the KEK; 1 only for loaders older than v2 support") + maxMachines := fs.Int("max-machines", 0, "online/attested: most distinct machines that may use the license at once (enforced by cryptship-server --enforce-seats; 0 = no limit)") + secretVersion := fs.Int("server-secret-version", license.SecretVersion2, "online/attested: server secret derivation; 2 binds the policy (an online server cannot release attested secrets), 0 = legacy, for loaders and servers that predate it") var fps stringList fs.Var(&fps, "fingerprint", "bound machine fingerprint (repeatable; strict=1, pool>=1)") var ents stringList @@ -282,7 +285,7 @@ func cmdIssue(args []string) error { } lid = "lic_" + h } - notAfter, err := resolveExpiry(*expires, *days) + notAfter, err := resolveExpiry(*expires, *days, *endOfDay) if err != nil { return err } @@ -313,7 +316,17 @@ func cmdIssue(args []string) error { SigningKeyID: *keyID, } - if err := wrapInto(&claims, license.BindingPolicy(*binding), contentKey, fps, *server, master); err != nil { + if *secretVersion != license.SecretVersionLegacy && *secretVersion != license.SecretVersion2 { + return fmt.Errorf("issue: --server-secret-version must be %d or %d", license.SecretVersionLegacy, license.SecretVersion2) + } + if *maxMachines < 0 { + return errors.New("issue: --max-machines must be >= 0") + } + if *maxMachines > 0 && *binding != string(license.BindOnline) && *binding != string(license.BindAttested) { + return errors.New("issue: --max-machines applies to online/attested binding (strict/pool are limited by their fingerprints)") + } + sb := serverBinding{URL: *server, Master: master, SecretVersion: *secretVersion, MaxMachines: *maxMachines} + if err := wrapInto(&claims, license.BindingPolicy(*binding), contentKey, fps, sb); err != nil { return err } if err := claims.Validate(); err != nil { @@ -333,12 +346,20 @@ func cmdIssue(args []string) error { } fmt.Printf("issued %s for %s/%s (binding=%s) -> %s\n", lid, *customer, *product, *binding, *out) if notAfter != 0 { - fmt.Printf("expires: %s\n", time.Unix(notAfter, 0).UTC().Format("2006-01-02")) + fmt.Printf("expires: %s\n", time.Unix(notAfter, 0).UTC().Format(time.RFC3339)) } return nil } -func wrapInto(c *license.Claims, policy license.BindingPolicy, contentKey []byte, fps []string, server string, master []byte) error { +// serverBinding is what an online/attested license needs from the issuer. +type serverBinding struct { + URL string + Master []byte + SecretVersion int + MaxMachines int // binding.max: seats, enforced by the server +} + +func wrapInto(c *license.Claims, policy license.BindingPolicy, contentKey []byte, fps []string, sb serverBinding) error { switch policy { case license.BindNone: secret, err := crypto.RandomKey() @@ -364,14 +385,17 @@ func wrapInto(c *license.Claims, policy license.BindingPolicy, contentKey []byte return wrapPerFingerprint(c, contentKey, fps) case license.BindOnline, license.BindAttested: - if server == "" || len(master) == 0 { + if sb.URL == "" || len(sb.Master) == 0 { return errors.New("issue: online/attested binding needs --server and --server-master-key") } - c.Binding = license.Binding{Policy: policy, Server: server} - secret, err := online.DeriveSecret(master, c.LicenseID) + // secret_v and max are claims (in the v2 KEK context), so they are + // set before wrapping. + c.Binding = license.Binding{Policy: policy, Server: sb.URL, SecretVersion: sb.SecretVersion, Max: sb.MaxMachines} + secret, err := online.DeriveSecretFor(sb.Master, c.LicenseID, string(policy), sb.SecretVersion) if err != nil { return err } + defer crypto.Wipe(secret) return wrapWith(c, secret, contentKey) default: @@ -476,12 +500,24 @@ func (s *stringList) Set(v string) error { return nil } -func resolveExpiry(date string, days int) (int64, error) { +// resolveExpiry turns --expires / --days into not_after (unix seconds, UTC). +// --expires D alone means 00:00:00 UTC on D; with endOfDay, 23:59:59 UTC on +// D (the license is valid while now <= not_after). +func resolveExpiry(date string, days int, endOfDay bool) (int64, error) { + if endOfDay && date == "" { + return 0, errors.New("issue: --expires-end-of-day needs --expires") + } + if date != "" && days > 0 { + return 0, errors.New("issue: give --expires or --days, not both") + } if date != "" { t, err := time.Parse("2006-01-02", date) if err != nil { return 0, fmt.Errorf("issue: bad --expires: %w", err) } + if endOfDay { + t = t.Add(24*time.Hour - time.Second) + } return t.Unix(), nil } if days > 0 { diff --git a/cmd/cryptship/main_test.go b/cmd/cryptship/main_test.go index 5403c21..4a7996d 100644 --- a/cmd/cryptship/main_test.go +++ b/cmd/cryptship/main_test.go @@ -7,10 +7,12 @@ import ( "os" "path/filepath" "testing" + "time" "github.com/AI2TH/cryptship/pkg/crypto" "github.com/AI2TH/cryptship/pkg/keyprovider" "github.com/AI2TH/cryptship/pkg/license" + "github.com/AI2TH/cryptship/pkg/online" ) // Each fingerprint of a pool license must unwrap the content key after a @@ -24,7 +26,7 @@ func TestWrapIntoPoolEveryFingerprintUnwraps(t *testing.T) { for _, v := range []int{license.FormatVersion1, license.FormatVersion2} { c := license.Claims{Version: v, LicenseID: "lic_pool", CustomerID: "acme", Product: "w", PayloadID: "p", SigningKeyID: "v1"} - if err := wrapInto(&c, license.BindPool, contentKey, fps, "", nil); err != nil { + if err := wrapInto(&c, license.BindPool, contentKey, fps, serverBinding{}); err != nil { t.Fatal(err) } if err := c.Validate(); err != nil { @@ -56,19 +58,45 @@ func TestWrapIntoNoneAndOnline(t *testing.T) { master, _ := crypto.RandomKey() c := license.Claims{Version: license.FormatVersion, LicenseID: "lic_none", PayloadID: "p"} - if err := wrapInto(&c, license.BindNone, contentKey, nil, "", nil); err != nil { + if err := wrapInto(&c, license.BindNone, contentKey, nil, serverBinding{}); err != nil { t.Fatal(err) } if got, err := keyprovider.Unwrap(c, c.UnboundSecret); err != nil || !bytes.Equal(got, contentKey) { t.Fatalf("none: err = %v", err) } - c = license.Claims{Version: license.FormatVersion, LicenseID: "lic_online", PayloadID: "p"} - if err := wrapInto(&c, license.BindOnline, contentKey, nil, "https://keys.example", master); err != nil { - t.Fatal(err) + for _, policy := range []license.BindingPolicy{license.BindOnline, license.BindAttested} { + for _, sv := range []int{license.SecretVersionLegacy, license.SecretVersion2} { + c = license.Claims{Version: license.FormatVersion, LicenseID: "lic_srv", PayloadID: "p"} + if err := wrapInto(&c, policy, contentKey, nil, serverBinding{URL: "https://keys.example", Master: master, SecretVersion: sv, MaxMachines: 3}); err != nil { + t.Fatal(err) + } + if err := c.Validate(); err != nil { + t.Fatal(err) + } + if c.Binding.SecretVersion != sv || c.Binding.Max != 3 { + t.Fatalf("%s: secret_v = %d, want %d", policy, c.Binding.SecretVersion, sv) + } + want, _ := online.DeriveSecretFor(master, "lic_srv", string(policy), sv) + if got, err := keyprovider.Unwrap(c, want); err != nil || !bytes.Equal(got, contentKey) { + t.Fatalf("%s v%d: err = %v", policy, sv, err) + } + // Under v2 the other policy's secret does not unwrap. + if sv == license.SecretVersion2 { + otherPolicy := license.BindOnline + if policy == license.BindOnline { + otherPolicy = license.BindAttested + } + wrong, _ := online.DeriveSecretFor(master, "lic_srv", string(otherPolicy), sv) + if _, err := keyprovider.Unwrap(c, wrong); err == nil { + t.Fatalf("%s: the %s secret unwrapped it", policy, otherPolicy) + } + } + } } - if err := c.Validate(); err != nil { - t.Fatal(err) + c = license.Claims{Version: license.FormatVersion, LicenseID: "lic_x", PayloadID: "p"} + if err := wrapInto(&c, license.BindOnline, contentKey, nil, serverBinding{URL: "https://keys.example", Master: master, SecretVersion: 7}); err == nil { + t.Fatal("unknown secret version accepted") } } @@ -88,3 +116,34 @@ func TestMasterKeyRefusesOverwrite(t *testing.T) { t.Fatalf("--force: %v", err) } } + +func TestResolveExpiry(t *testing.T) { + start, _ := time.Parse(time.RFC3339, "2027-03-31T00:00:00Z") + end, _ := time.Parse(time.RFC3339, "2027-03-31T23:59:59Z") + if got, err := resolveExpiry("2027-03-31", 0, false); err != nil || got != start.Unix() { + t.Fatalf("start of day: %d %v", got, err) + } + got, err := resolveExpiry("2027-03-31", 0, true) + if err != nil || got != end.Unix() { + t.Fatalf("end of day: %d %v", got, err) + } + c := license.Claims{NotAfter: got} + if c.Expired(end.Unix()) { + t.Fatal("expired during the last second of the day") + } + if !c.Expired(end.Unix() + 1) { + t.Fatal("still valid at 00:00:00 the next day") + } + for _, tc := range []struct { + date string + days int + eod bool + }{{"", 0, true}, {"2027-03-31", 5, false}, {"31/03/2027", 0, false}} { + if _, err := resolveExpiry(tc.date, tc.days, tc.eod); err == nil { + t.Errorf("%+v: accepted", tc) + } + } + if got, err := resolveExpiry("", 0, false); err != nil || got != 0 { + t.Fatalf("no expiry: %d %v", got, err) + } +} diff --git a/deploy/README.md b/deploy/README.md index f1c54d3..d6c9066 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -38,6 +38,64 @@ disabled, and `CRYPTSHIP_REVOCATION=grace:...` has no record to fall back on. To keep them, mount a small writable volume owned by that uid at `CRYPTSHIP_STATE_DIR` (e.g. `-v cryptship-state:/var/lib/cryptship`). +## Variant (opt-in, off by default): the app cannot change its own code + +In the setup above the loader and the app share uid 65532, so the app (or +anything that compromises it) can rewrite the extracted code. To prevent that, +start the loader as root and let it drop privileges: + +```bash +docker run --rm \ + --user 0:0 \ + -e CRYPTSHIP_APP_USER=65532:65532 \ + --read-only \ + --tmpfs /run/cryptship:rw,mode=0755 \ + --cap-drop=ALL --cap-add=CHOWN --cap-add=SETUID --cap-add=SETGID \ + --security-opt=no-new-privileges \ + --security-opt seccomp=deploy/seccomp.json \ + -e CRYPTSHIP_REQUIRE_TMPFS=1 \ + your-cryptship-image +``` + +- The loader extracts into `/run/cryptship/app`, gives the tree to root (or + `CRYPTSHIP_EXTRACT_OWNER=`) and group 65532 with modes 0750 / 0640, then + drops to 65532:65532 with no supplementary groups and checks it cannot get + root back before it execs the app. +- The app can read and run its code but not modify, add or delete files there, + and no other non-root user can read it. The app's own uid can still read and + copy the code. +- Only `CAP_CHOWN`, `CAP_SETUID` and `CAP_SETGID` are needed, and only by the + loader: the app starts with none (a uid change clears capabilities, and + `no-new-privileges` stops it regaining any). +- The tmpfs mount point must be traversable by the app user (`mode=0755`). +- Root without `CAP_DAC_OVERRIDE` cannot write a state dir owned by 65532, so + mount the state volume owned by root in this mode, or accept the warning that + the clock check is off. +- Set without root, `CRYPTSHIP_APP_USER` is an error, never a silent fallback. + +## Variant (opt-in, off by default): no decrypted file at all (single binary) + +This does not fit a multi-file interpreter app (Python, Node); it is for a +payload that is one native executable. + +For a payload that is one ELF executable (Go, Rust, a Nuitka `--onefile` +build), set `CRYPTSHIP_EXEC_MEMFD=`. The loader copies that +entry into a sealed, close-on-exec memfd and executes it from there; nothing is +written to the extract dir. The arguments after `--` become argv +(`cryptship-loader -- server --port 8080`). Scripts (`#!`) cannot run this way. +The binary still exists in RAM and can be read back through +`/proc//exe` by anyone allowed to ptrace the app, so combine it with the +next point. + +## Make the app non-dumpable again + +The loader is non-dumpable while it holds the key, but execve resets that, so +the app starts dumpable. Have the app call `prctl(PR_SET_DUMPABLE, 0)` first: +`harden.NonDumpable()` from `github.com/AI2TH/cryptship/pkg/harden` in Go, or +`cryptship_harden.set_non_dumpable()` from `helpers/python/cryptship_harden.py` +in Python (standard library only). After that, other processes of the same uid +cannot ptrace it or read its `/proc//mem` or `/proc//exe`. + ## Kubernetes ```yaml @@ -77,7 +135,7 @@ spec: |---|---| | Someone with just the image (not running it) | **Yes** — ciphertext only. | | Another non-root user in the container | **Yes** — seccomp + non-root + dropped caps block ptrace/mem-read of the app. | -| Code running as the app's own uid | **No** — it can read the decrypted files in the extract directory. | +| Code running as the app's own uid | **Read: no** — it can read the decrypted files. **Modify: yes** with `CRYPTSHIP_APP_USER` (the tree belongs to another uid). With the dumpable helpers it cannot ptrace the running app. | | **Container or host root with a privileged sidecar** | **No** — root can run a process outside this profile and read `/proc//mem`. | | Host root dumping guest RAM | **No** — needs a confidential-compute key provider (SEV-SNP/TDX), on the roadmap. | diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 7d020c8..7d0381e 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -19,6 +19,7 @@ cost; it does not eliminate the risk. | Casual customer | `docker save`, unpack layers, read source | **Defeated** — layers contain only ciphertext | | Curious customer | grep the image, inspect the loader binary | **Defeated** — no key in the image; loader logic is public but useless without license material | | Licensee sharing a license | copy `license.json` to another machine | **Defeated for bound licenses** — content key is wrapped to a machine fingerprint | +| Code running as the app's uid | read or patch the extracted files | **Read: not defeated. Patch: defeated** with `CRYPTSHIP_APP_USER` (the tree belongs to another uid, group-readable only) | | Root on the deploy box | dump tmpfs / `/proc/` at runtime | **Not defeated** without a confidential-compute backend. Documented honestly. | | Funded reverse-engineer | debugger, instrument the loader, dump memory | **Not defeated** — goal is only to make this cost more than a license | @@ -34,7 +35,9 @@ pkg/machine software machine fingerprint (stable host identifiers -> hash) pkg/archive tar pack + traversal-safe untar pkg/keyprovider Provider interface; `offline`, `online` and `attested` providers pkg/attest attestation Verifier interface; attesttest = test-only attester/verifier -pkg/online, pkg/licenseserver online key protocol and the reference key server +pkg/online, pkg/licenseserver online/attested key protocol, signed revocation + answers, seats, and the reference key server +pkg/harden PR_SET_DUMPABLE helper for Go apps (helpers/python for Python) cmd/cryptship vendor CLI: keygen, master-key, fingerprint, pack, issue, inspect cmd/cryptship-loader image ENTRYPOINT: verify -> get key -> decrypt -> exec cmd/cryptship-server reference key server for the `online` provider @@ -49,8 +52,26 @@ KEK = HKDF(binding_material, salt = license_id, info = SHA-256(binding context)) -> wraps the content key binding_material = machine fingerprint (strict / pool) = per-license secret (none) + = server secret (online / attested) +state key = HKDF(binding_material, salt = license_id, + info = "cryptship/state-key/v1" ‖ SHA-256(binding context)) + -> seals the loader's local state +server secret (binding.secret_v): + 2 (default) = HKDF(master, info = "cryptship/server-secret/v2" ‖ policy ‖ license_id) + 0 (legacy) = HKDF(master, salt = license_id, info = "cryptship/online-secret/v1") ``` +- The **server secret** of a `secret_v` 2 license depends on the binding policy, + so the `online` and `attested` secrets of a license differ. The plain + `/v1/key` path only ever derives the `online` one; the `attested` one is only + released through the attested protocol. A server without a verifier that + shares the master key therefore cannot release an attested secret. Legacy + (`secret_v` absent) licenses keep the old derivation and still work; + `cryptship-server --no-legacy-secrets` refuses them once all are reissued. +- The **state key** seals the clock-rollback and revocation-grace records. It + comes from the binding material, so a record is tied to this license and this + binding (machine, server secret), not to the build's content key. + - The **content key** encrypts the payload once per build. The same content key is wrapped differently into each customer's license. - The **KEK** never leaves the machine and is never stored; it is re-derived at @@ -130,30 +151,43 @@ image ──> loader + payload.enc + license.json + vendor.pub (no source, no 1. read vendor.pub, license.json 2. verify Ed25519 signature -> fail closed 3. check not_after / issued_at window -> fail closed -4. (optional) revocation_url check -> 410 Gone = revoked (fail closed); network error = - CRYPTSHIP_REVOCATION policy: open (run, default), closed (refuse), - grace: (run if the last good check is younger) -5. provider.ContentKey(license): +4. provider.Unlock(license) -> content key + state key: offline: derive KEK from machine fingerprint (or unbound secret), unwrap one of wrapped_keys -> fail closed on no match online: fetch the per-license binding secret from the key server - (signed, nonce-bound reply) -> fail closed if unreachable - attested: verified attester evidence gates the key (no production - verifier yet; the server refuses --require-attestation) + (signed, nonce-bound reply) -> fail closed if unreachable, + unless CRYPTSHIP_ONLINE_GRACE covers an outage + attested: challenge -> evidence over (challenge, ephemeral key) -> + secret sealed to the ephemeral key (no production verifier + yet; the server refuses --require-attestation) +5. (optional) revocation_url check: a signed, nonce-bound answer. Revoked = + refuse. An unsigned 200 counts as "not revoked" except in closed mode. + On failure CRYPTSHIP_REVOCATION decides: open (run, default), closed + (refuse), grace: (run if the last signed "not revoked", sealed + under the state key, is younger) 6. clock-rollback check (licenses with not_after): refuse if now is more than 24h behind the latest start recorded in CRYPTSHIP_STATE_DIR. The record is - sealed under a key derived from the content key; deleting it resets the - check, so it is best effort + sealed under the state key; deleting it resets the check, so it is best + effort 7. OpenPayload(content_key, payload.enc, payload_id) -> AEAD; fail closed -8. untar into CRYPTSHIP_EXTRACT (should be tmpfs; only an empty dir or one an - earlier run marked is emptied first) -9. exec the app (args after `--`), app becomes PID 1 +8. CRYPTSHIP_EXEC_MEMFD set: copy that one ELF entry into a sealed memfd and + exec it from there (nothing on disk); skip 9 +9. untar into CRYPTSHIP_EXTRACT (should be tmpfs; only an empty dir or one an + earlier run marked is emptied first); with CRYPTSHIP_APP_USER (loader runs + as root), give the tree to CRYPTSHIP_EXTRACT_OWNER:app-gid, 0750/0640 +10. with CRYPTSHIP_APP_USER, drop to the app uid/gid (no supplementary groups); + exec the app (args after `--`), app becomes PID 1 ``` +The revocation check runs after key acquisition (step 4) only so that its grace +record can be sealed under the state key; nothing is decrypted before it. + Runtime config (env): `CRYPTSHIP_LICENSE`, `CRYPTSHIP_PAYLOAD`, `CRYPTSHIP_PUBKEY`, `CRYPTSHIP_EXTRACT`, `CRYPTSHIP_REVOCATION` -(open | closed | grace:), `CRYPTSHIP_STATE_DIR` (revocation and clock -state) and +(open | closed | grace:), `CRYPTSHIP_STATE_DIR` (revocation, online +grace and clock state), `CRYPTSHIP_ONLINE_GRACE` (online outage cache), +`CRYPTSHIP_APP_USER` / `CRYPTSHIP_EXTRACT_OWNER` (separate-uid extraction and +privilege drop), `CRYPTSHIP_EXEC_MEMFD` (run one ELF binary from memory) and `CRYPTSHIP_REQUIRE_TMPFS` (refuse to run if the extract dir is not tmpfs). Put the extract dir on tmpfs: `docker run --tmpfs /run/cryptship` or k8s `emptyDir { medium: Memory }`. @@ -169,21 +203,39 @@ state) and the loader POSTs a nonce; the server returns the per-license secret (derived from a master secret via HKDF) signed with the vendor key and echoing the nonce. The loader verifies and unwraps. Revocation is immediate — the server - declines (410 revoked; 403 attestation rejected). Reference server: `cmd/cryptship-server`; demo: + declines (410 revoked; 403 refused; 409 seat limit). With + `issue --max-machines N` and `cryptship-server --enforce-seats`, at most N + distinct machines hold a seat at once (a seat lapses after `--seat-lease`, + 30 days by default). Seats count the fingerprints loaders report, so a + modified loader can evade them: they limit honest over-deployment, not a + determined customer. Reference server: `cmd/cryptship-server`; demo: `scripts/demo-online.sh`. - **attested** — like online, but the key broker releases the secret only after verifying TEE attestation evidence the loader attaches (via an `Attester`). Defends against host root **when the guest is vendor-controlled**; needs - confidential-compute hardware. The evidence must commit to - `online.AttestationNonce(license_id, nonce)`, so it cannot be reused for another - request nonce or license. Built behind the `Attester` (loader) and - `attest.Verifier` (server) interfaces, with test-only implementations; a real - SEV-SNP/TDX attester and verifier are the roadmap item. Until then - `cryptship-server --require-attestation` refuses to start. Known gap: the nonce - is chosen by the loader, not the server, and the secret is not encrypted to a - key held in the TEE, so a captured request with genuine evidence can be - replayed. A real deployment needs a server challenge and an ephemeral TEE key - in REPORT_DATA (as Trustee does). + confidential-compute hardware. The protocol follows the Trustee / KBS + pattern: + 1. the loader gets a challenge from `POST /v1/challenge` (single use, MACed + by the server, valid for 2 minutes); + 2. it makes an ephemeral X25519 key pair and asks the attester for evidence + whose REPORT_DATA holds `online.AttestationBinding(license_id, challenge, + ephemeral public key)` (SHA-256, length-prefixed fields); + 3. the server spends the challenge, verifies the evidence against that + binding, and returns the secret encrypted to the ephemeral key (X25519, + HKDF-SHA256, AES-256-GCM), signed with the vendor key. + + A captured request cannot be replayed (the challenge is spent), and a + captured answer is useless without the ephemeral private key, which never + leaves the TEE. A server with a verifier refuses plain requests for attested + secrets; one without a verifier refuses the attested protocol, and under + `secret_v` 2 cannot derive attested secrets at all. Built behind the + `Attester` (loader) and `attest.Verifier` (server) interfaces, with test-only + implementations; a real SEV-SNP/TDX attester and verifier (signature chain, + REPORT_DATA check, measurement allow-list) need the hardware and are the + roadmap item. Until then `cryptship-server --require-attestation` refuses to + start. The challenge's single use is tracked in memory, so after a server + restart a challenge can be reused until it expires; the answer is still + sealed to the original ephemeral key. The software fingerprint (`pkg/machine`) derives one independent fingerprint per readable host identifier (`/etc/machine-id`, DMI UUID/serial); the loader tries @@ -211,10 +263,22 @@ remote execution. Python bytecode, Nuitka for native). Proof: `scripts/demo-compiled.sh`. - **Loader / container hardening** — the loader calls `prctl(PR_SET_DUMPABLE, 0)` for its own lifetime (protects the key and decrypted payload during the decrypt - window; execve resets the flag, so the running app is protected by the - container config instead). `deploy/` ships a seccomp profile denying - `ptrace`/`process_vm_readv`/`process_vm_writev`/`kcmp`, plus non-root, - cap-drop, read-only-rootfs and tmpfs settings for docker and k8s. + window). execve resets the flag, so the app should set it again first thing: + `pkg/harden.NonDumpable()` for Go, `helpers/python/cryptship_harden.py` for + Python. Until it does, the same uid can ptrace it. `deploy/` ships a seccomp + profile denying `ptrace`/`process_vm_readv`/`process_vm_writev`/`kcmp`, plus + non-root, cap-drop, read-only-rootfs and tmpfs settings for docker and k8s. +- **Separate-uid extraction** — with `CRYPTSHIP_APP_USER=uid[:gid]` the loader + starts as root, extracts, gives the tree to `CRYPTSHIP_EXTRACT_OWNER` (default + root) and the app's group with modes 0750/0640, and drops to the app uid/gid + before exec. The app can read and run its code but cannot change it, and no + other non-root user can read it. It does not stop the app's own uid from + reading (and copying) the code. +- **memfd exec** — `CRYPTSHIP_EXEC_MEMFD=` runs one ELF binary from the + payload out of a sealed, close-on-exec memfd (via `/proc/self/fd`), so no + decrypted file exists on any filesystem. Scripts cannot run this way. The + binary is still in RAM, and `/proc//exe` reads it back for anyone who + may ptrace the app, hence the dumpable helpers above. - **Watermarking** — the loader writes a per-license `.cryptship-manifest.json` into the extracted tree and exposes `CRYPTSHIP_LICENSE_ID` / `CRYPTSHIP_CUSTOMER` to the app, so a leaked instance names its licensee @@ -229,18 +293,24 @@ remote execution. (static) or `--revoke-file` (a live denylist re-read per request — no restart); running instances keep running. By default an online-bound app does not start while the server is unreachable; `CRYPTSHIP_ONLINE_GRACE=` lets it ride out - an outage for that long. The cache holds the **content key** (not the - per-license secret), sealed to a machine/state-dir key, with `cached_at` and - the server's `not_after` **authenticated** (editing them to widen the window - fails the open) and both enforced; a reachable server that says revoked still - wins. **Trade-off (honest):** on a host the licensee controls, enabling grace + an outage (no answer, or 502/503/504 from a proxy) for that long. The cache + holds the **content key** and the local state key (not the per-license + secret), sealed to a machine/state-dir key, with `cached_at` and the server's + `not_after` **authenticated** (editing them to widen the window fails the + open) and both enforced; a reachable server that says revoked still wins. + **Trade-off (honest):** on a host the licensee controls, enabling grace writes a sealed content key to disk, so for the window it is extractable much like the running process already is — use it where availability matters more - than instant offline revocation, or on hosts you trust. Off by default. + than instant offline revocation, or on hosts you trust. Anyone who can block + the server can use the window on purpose, so keep it short. Off by default. - **Signed `revocation_url` check:** the loader sends a nonce and verifies the - server's signed answer against the key ring, so an on-path attacker or captive - portal cannot forge "not revoked"; `CRYPTSHIP_REVOCATION=closed` also rejects - an unsigned "active" answer. + server's signed answer (same envelope and vendor key as key answers, its own + domain label) against the key ring, so an on-path attacker or captive portal + cannot forge "not revoked". `closed` rejects an unsigned "active" answer; + `open` and `grace` accept one from a generic endpoint, but only a signed + success refreshes the grace record. That record is sealed under the state + key (editing or copying it does not extend grace) and ignored if dated in + the future. Deleting it only removes grace. ## Roadmap @@ -248,13 +318,17 @@ remote execution. useless off the physical machine, even to root. 2. **Online provider + reference license server** — IMPLEMENTED (signed, nonce-bound replies; live `--revoke-file` denylist; signed `revocation_url` - check; `CRYPTSHIP_ONLINE_GRACE` outage caching). Still open: seat management - (the `machine` field is sent but not enforced) and a machine (re)activation - API. + check; `CRYPTSHIP_ONLINE_GRACE` outage caching; policy-bound server secrets; + seat enforcement). Still open: a machine (re)activation API (seats are freed + by lease expiry or `SeatTracker.Release`), server-signed time for offline + expiry, and an authenticated admin API for revocation. 3. **Cloud KMS provider** — wrap the content key with AWS/GCP/Azure KMS; unwrap gated by cloud IAM + signed instance identity. -4. **Confidential-compute provider** — SEV-SNP/TDX with remote attestation; the - only backend that defends against a root-privileged host. +4. **Confidential-compute provider** — the challenge / ephemeral-key protocol is + IMPLEMENTED and tested with the test verifier. Still open: SEV-SNP + (`go-sev-guest`) and TDX (`go-tdx-guest`) attesters and verifiers, which need + the hardware. This is the only backend that defends against a + root-privileged host. 5. **Pre-encryption compile/bundle** — IMPLEMENTED as `pack --transform-cmd`: runs your compiler/obfuscator over the app before sealing, so a dump yields compiled artifacts, not clean source (`scripts/compile-python.sh` for Python diff --git a/docs/DIAGRAMS.md b/docs/DIAGRAMS.md index b70fc27..a67c8e7 100644 --- a/docs/DIAGRAMS.md +++ b/docs/DIAGRAMS.md @@ -96,7 +96,7 @@ flowchart TD S(["container start"]) --> H["loader: mark non-dumpable"] H --> V{"verify license signature
(vendor.pub key ring)"} V -- fail --> X1["refuse"] - V -- ok --> E{"expiry + revocation"} + V -- ok --> E{"expiry"} E -- fail --> X2["refuse"] E -- ok --> SEL["select provider by binding policy"] SEL --> OFF["offline: KEK from fingerprint
or unbound secret"] @@ -104,24 +104,30 @@ flowchart TD OFF --> UNW{"unwrap the wrapped key"} NET --> UNW UNW -- "wrong key -> AEAD fails" --> X3["refuse"] - UNW -- ok --> DEC["decrypt payload into tmpfs"] + UNW -- "ok: content key + state key" --> RC{"signed revocation check
+ clock check
(state sealed by state key)"} + RC -- fail --> X4["refuse"] + RC -- ok --> MODE{"CRYPTSHIP_EXEC_MEMFD?"} + MODE -- yes --> MFD["copy one ELF into a sealed memfd"] + MODE -- no --> DEC["decrypt payload into tmpfs"] DEC --> WM["write watermark manifest"] - WM --> EXE["exec app as PID 1"] + WM --> OWN["CRYPTSHIP_APP_USER: chown tree,
drop to app uid"] + OWN --> EXE["exec app as PID 1"] + MFD --> EXE ``` --- -## Online / attested sequence +## Online sequence ```mermaid sequenceDiagram participant L as loader (in container) participant S as cryptship-server (key broker) L->>L: verify license + expiry - L->>S: POST /v1/key — license_id, nonce [, attestation] + L->>S: POST /v1/key - license_id, nonce, policy, secret_v [, license, machine] S->>S: denylist check (revocation) - S->>S: attestation check (attested mode only) - S->>S: secret = HKDF of master and license_id + S->>S: seat check (with --enforce-seats) + S->>S: secret = HKDF of master, policy online, license_id S-->>L: signed response - secret and not_after, echoes nonce L->>L: verify signature and nonce L->>L: derive KEK, unwrap content key @@ -132,6 +138,26 @@ Revocation is immediate: the server returns `410 Gone` and the app refuses to start. The signed, nonce-bound response means a proxy cannot forge or replay a "yes". +## Attested sequence + +```mermaid +sequenceDiagram + participant L as loader (inside the TEE) + participant S as cryptship-server (key broker with a verifier) + L->>S: POST /v1/challenge - license_id + S-->>L: challenge (single use, MACed, 2 minutes) + L->>L: make ephemeral X25519 key pair + L->>L: evidence with REPORT_DATA = hash of license_id, challenge, ephemeral public key + L->>S: POST /v1/key - challenge, evidence, ephemeral public key + S->>S: spend challenge, verify evidence against that hash + S->>S: secret = HKDF of master, policy attested, license_id + S-->>L: signed response - secret encrypted to the ephemeral key + L->>L: decrypt secret, derive KEK, unwrap content key +``` + +A replayed request fails (the challenge is spent), and a captured answer cannot +be opened without the ephemeral private key, which stays in the TEE. + --- ## Key hierarchy diff --git a/docs/HOWTO.md b/docs/HOWTO.md index 2e80fa4..a8c52c1 100644 --- a/docs/HOWTO.md +++ b/docs/HOWTO.md @@ -140,8 +140,14 @@ immediate). See §5 for running the server. ```bash cryptship master-key --out master.key # the server's master secret (SECRET) cryptship issue ... --binding online \ - --server https://lic.example.com --server-master-key master.key ... + --server https://lic.example.com --server-master-key master.key \ + --max-machines 5 ... # optional seat limit (server --enforce-seats) ``` +New online/attested licenses carry `binding.secret_v: 2`: the server secret is +derived per binding policy, so an online server can never release an attested +license's secret. Loaders from before this field reject such licenses +(`unknown field "secret_v"`); until they are updated, issue with +`--server-secret-version 0` (the old derivation). ### attested — confidential computing Like online, but the key broker releases the secret only after verifying TEE @@ -150,7 +156,11 @@ can't read the memory — **and only when you control the guest** (a locked imag it can't root into). Needs CC hardware and a verifier on the server; today the loader side is an `Attester` interface and the server side an `attest.Verifier` interface, with test-only implementations in `pkg/attest/attesttest` (see -ARCHITECTURE.md → roadmap). +ARCHITECTURE.md → roadmap). The wire protocol is done: the loader fetches a +single-use challenge, the evidence's REPORT_DATA commits to +`online.AttestationBinding(license_id, challenge, ephemeral public key)`, and the +server returns the secret encrypted to that ephemeral key, so neither a captured +request nor a captured answer can be reused. ```bash cryptship issue ... --binding attested \ --server https://kbs.example.com --server-master-key master.key ... @@ -199,6 +209,22 @@ isn't memory-backed. Full docker + Kubernetes settings are in [../deploy/README.md](../deploy/README.md). This stops a non-root attacker — not host root (that needs the `attested` mode). +Three optional extras (details and a `docker run` recipe in +[../deploy/README.md](../deploy/README.md)): + +- **The app cannot change its own code:** start the loader as root with + `CRYPTSHIP_APP_USER=65532:65532`. It extracts, gives the tree to root (or + `CRYPTSHIP_EXTRACT_OWNER`) and the app's group (0750/0640), and drops to + 65532 before exec. It needs `CAP_CHOWN`, `CAP_SETUID` and `CAP_SETGID`. +- **No decrypted file at all, for a single binary:** + `CRYPTSHIP_EXEC_MEMFD=bin/server` runs that ELF entry of the payload from a + sealed memfd (`cryptship-loader -- server --flag` sets argv). Scripts and + interpreters with many files cannot use this. +- **Make the app non-dumpable again:** execve resets the loader's + `PR_SET_DUMPABLE`. Call `harden.NonDumpable()` (`github.com/AI2TH/cryptship/pkg/harden`) + at the top of a Go `main`, or ship `helpers/python/cryptship_harden.py` with a + Python app and call `cryptship_harden.set_non_dumpable()` first. + The loader also exposes the license identity to your app for watermarking: `CRYPTSHIP_LICENSE_ID`, `CRYPTSHIP_CUSTOMER`, `CRYPTSHIP_PAYLOAD_ID`, and drops a `.cryptship-manifest.json` into the extracted tree. @@ -223,27 +249,41 @@ cryptship-server \ - `--require-attestation` makes the server refuse to start: no SEV-SNP/TDX verifier is built in yet, and accepting evidence it cannot verify would give no protection. To use `attested` today, implement `attest.Verifier` (signature - chain, `online.AttestationNonce` in REPORT_DATA, measurement allow-list) and set - `licenseserver.Server.Verifier`, or front the key broker with a verifier such as - Confidential Containers Trustee. -- The server cannot tell an `attested` license from an `online` one. A server - without a verifier releases the secret of an attested license to anyone who - asks, so give the attestation server its own master key and never run a - non-verifying server with that key. + chain, `online.AttestationBinding` in REPORT_DATA, measurement allow-list) and + set `licenseserver.Server.Verifier`, or front the key broker with a verifier + such as Confidential Containers Trustee. +- Attested secrets are safe on a non-verifying server for licenses with + `binding.secret_v: 2` (the default): their secret is derived per policy and + only released through the attested protocol. Licenses issued without it + (`--server-secret-version 0`, or before the field existed) use the old + derivation, which a non-verifying server *will* release for an attested + license. Reissue them, then start the server with `--no-legacy-secrets`; until + then, give attested licenses their own master key. +- Seats: `--enforce-seats` limits each license to its `binding.max` distinct + machines (`issue --max-machines N`; 0 = no limit). A machine keeps its seat + for `--seat-lease` (default `720h`) after its last start; `--seats-file` + persists seats across restarts, and `--license-keys` names the key ring that + signs licenses if it is not the `--sign` key. Loaders report their machine + fingerprint, so mount `/etc/machine-id`; a modified loader can lie, so treat + seats as a limit for honest customers. A separate revocation check (`revocation_url` in the license) is **signed**: the loader sends a nonce and verifies the server's signed answer against the key ring, so a captive portal or on-path attacker cannot forge "not revoked". `CRYPTSHIP_REVOCATION=closed` additionally rejects any *unsigned* "active" answer. -The loader's behaviour on a *network failure* is the operator's choice: -`open` (default — run), `closed` (refuse), `grace:` (run if the last good -check is younger than ``). +The loader's behaviour when the check fails is the operator's choice: +`open` (default — run), `closed` (refuse), `grace:` (run if the last +*signed* "not revoked" is younger than ``). The grace record is sealed +under a key derived from the license's binding material, so editing or copying +it does not extend grace. For the **online** binding itself, set `CRYPTSHIP_ONLINE_GRACE=` (e.g. `12h`) -to let a cached, machine-sealed secret ride out a license-server **outage** for -that long. It is off by default (every start needs the server); it never bypasses -revocation (a reachable server that says revoked still wins) and never applies to -`attested` (which must always re-attest). +to let a cached, machine-sealed content key ride out a license-server **outage** (no +answer, or 502/503/504 from a proxy) for that long. It is off by default (every +start needs the server); it never bypasses revocation (a reachable server that +says revoked still wins) and never applies to `attested` (which must always +re-attest). Anyone who can block the server can use the window, so keep it +short. --- @@ -281,8 +321,11 @@ accepts both versions. | `CRYPTSHIP_EXTRACT` | `/run/cryptship/app` | decrypt dir (put on tmpfs) | | `CRYPTSHIP_REQUIRE_TMPFS` | unset | `1` = refuse if not tmpfs | | `CRYPTSHIP_REVOCATION` | `open` | `open` / `closed` / `grace:` (revocation-URL failure policy) | -| `CRYPTSHIP_ONLINE_GRACE` | unset | `` (e.g. `12h`): ride out an online license-server outage using a machine-sealed cached secret; off by default | +| `CRYPTSHIP_ONLINE_GRACE` | unset | `` (e.g. `12h`): ride out an online license-server outage (no answer, or 502/503/504) using a machine-sealed cached content key; off by default | | `CRYPTSHIP_STATE_DIR` | `/var/lib/cryptship` | revocation-grace, online-grace, and clock-rollback state (best effort: if it is not writable those checks degrade with a warning) | +| `CRYPTSHIP_APP_USER` | unset | `uid[:gid]`: loader starts as root, extracts as another owner, drops to this user before exec (error if not root) | +| `CRYPTSHIP_EXTRACT_OWNER` | `0` | with `CRYPTSHIP_APP_USER`: owner uid of the extracted tree (must differ from the app uid) | +| `CRYPTSHIP_EXEC_MEMFD` | unset | ``: run that single ELF binary from a memfd; nothing is extracted | ### CLI commands | Command | Purpose | @@ -293,7 +336,11 @@ accepts both versions. | `cryptship pack` | (optionally compile, then) encrypt an app dir | | `cryptship issue` | issue a signed license for a binding mode | | `cryptship inspect [--pub ...]` | print claims and verify the signature | -| `cryptship-server` | reference license / key-broker server | +| `cryptship-server` | reference license / key-broker server (`--revoke-file`, `--enforce-seats`, `--no-legacy-secrets`) | + +`cryptship issue --expires 2027-03-31` ends the license at 00:00:00 UTC on that +date; add `--expires-end-of-day` to keep it valid through 23:59:59 UTC. `--days N` +counts from now. ### Keep secret vs. ship - **Secret (never commit / never put in the image):** `vendor.key`, `content.key`, diff --git a/helpers/python/cryptship_harden.py b/helpers/python/cryptship_harden.py new file mode 100644 index 0000000..c5ef115 --- /dev/null +++ b/helpers/python/cryptship_harden.py @@ -0,0 +1,51 @@ +"""Re-apply PR_SET_DUMPABLE for a Python app started by cryptship-loader. + +The loader marks itself non-dumpable while it holds the content key, but +execve resets that flag, so the app starts dumpable: another process of the +same uid can ptrace it or read /proc//mem and /proc//exe. Call +set_non_dumpable() as the first thing the app does: + + import cryptship_harden + cryptship_harden.set_non_dumpable() + +This raises the bar against other processes of the same uid; it does not stop +root. Standard library only (ctypes). On non-Linux systems it does nothing and +returns False. +""" + +import ctypes +import os +import sys + +PR_GET_DUMPABLE = 3 +PR_SET_DUMPABLE = 4 + + +def _prctl(option, arg2=0): + libc = ctypes.CDLL(None, use_errno=True) + prctl = libc.prctl + prctl.argtypes = [ctypes.c_int, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong] + prctl.restype = ctypes.c_int + rc = prctl(option, arg2, 0, 0, 0) + if rc < 0: + err = ctypes.get_errno() + raise OSError(err, os.strerror(err)) + return rc + + +def set_non_dumpable(): + """Mark this process non-dumpable. Returns True on Linux, False elsewhere. + + Raises OSError if the prctl call fails on Linux. + """ + if not sys.platform.startswith("linux"): + return False + _prctl(PR_SET_DUMPABLE, 0) + return True + + +def is_dumpable(): + """Return the current dumpable flag (always True outside Linux).""" + if not sys.platform.startswith("linux"): + return True + return _prctl(PR_GET_DUMPABLE) != 0 diff --git a/helpers/python/test_cryptship_harden.py b/helpers/python/test_cryptship_harden.py new file mode 100644 index 0000000..c6f72cf --- /dev/null +++ b/helpers/python/test_cryptship_harden.py @@ -0,0 +1,26 @@ +import subprocess +import sys +import unittest + +import cryptship_harden + + +@unittest.skipUnless(sys.platform.startswith("linux"), "PR_SET_DUMPABLE is Linux-only") +class SetNonDumpableTest(unittest.TestCase): + def test_sets_flag(self): + # Run in a child so this test process keeps its own flag. + code = ( + "import cryptship_harden as h\n" + "assert h.is_dumpable()\n" + "assert h.set_non_dumpable() is True\n" + "assert not h.is_dumpable()\n" + "print('ok')\n" + ) + out = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True, + cwd=__import__("os").path.dirname(__file__) or ".") + self.assertEqual(out.returncode, 0, out.stderr) + self.assertEqual(out.stdout.strip(), "ok") + + +if __name__ == "__main__": + unittest.main() diff --git a/pkg/archive/fuzz_test.go b/pkg/archive/fuzz_test.go new file mode 100644 index 0000000..9b24f12 --- /dev/null +++ b/pkg/archive/fuzz_test.go @@ -0,0 +1,80 @@ +package archive + +import ( + "archive/tar" + "io/fs" + "os" + "path/filepath" + "strings" + "testing" +) + +// FuzzUntar: whatever the archive, extraction never panics, never writes +// outside dest, and never leaves a symlink in dest that resolves outside it. +func FuzzUntar(f *testing.F) { + seeds := [][]entry{ + {{name: "a.txt", typ: tar.TypeReg, body: []byte("x")}, {name: "d/", typ: tar.TypeDir}, {name: "d/l", typ: tar.TypeSymlink, link: "../a.txt"}}, + {{name: "../escape", typ: tar.TypeReg, body: []byte("x")}}, + {{name: "/abs", typ: tar.TypeReg, body: []byte("x")}}, + {{name: "s", typ: tar.TypeSymlink, link: "."}, {name: "s/../x", typ: tar.TypeReg, body: []byte("x")}}, + {{name: "s", typ: tar.TypeSymlink, link: "/etc"}}, + {{name: "d/", typ: tar.TypeDir}, {name: "d/s", typ: tar.TypeSymlink, link: "../.."}}, + {{name: "s", typ: tar.TypeSymlink, link: "d"}, {name: "d/", typ: tar.TypeDir}, {name: "s/f", typ: tar.TypeReg, body: []byte("x")}}, + {{name: "h", typ: tar.TypeLink, link: "/etc/passwd"}}, + } + for _, s := range seeds { + f.Add(buildTar(f, s)) + } + f.Add([]byte("not a tar")) + + f.Fuzz(func(t *testing.T, data []byte) { + parent := t.TempDir() + dest := filepath.Join(parent, "dest") + _ = UntarLimit(data, dest, 1<<20) // errors are fine; escapes are not + + entries, err := os.ReadDir(parent) + if err != nil { + t.Fatal(err) + } + for _, e := range entries { + if e.Name() != "dest" { + t.Fatalf("wrote outside dest: %s", e.Name()) + } + } + destReal, err := filepath.EvalSymlinks(dest) + if err != nil { + return // nothing extracted + } + _ = filepath.WalkDir(dest, func(p string, d fs.DirEntry, err error) error { + if err != nil || d.Type()&fs.ModeSymlink == 0 { + return nil + } + target, _ := os.Readlink(p) + if lex := filepath.Clean(filepath.Join(filepath.Dir(p), target)); !within(dest, lex) { + t.Fatalf("symlink %s -> %s points outside dest", p, target) + } + if real, err := filepath.EvalSymlinks(p); err == nil && !within(destReal, real) { + t.Fatalf("symlink %s resolves outside dest: %s", p, real) + } + return nil + }) + }) +} + +func within(root, p string) bool { + return p == root || strings.HasPrefix(p, root+string(filepath.Separator)) +} + +// FuzzReadFile: reading one entry from an arbitrary archive never panics and +// never returns more than the limit. +func FuzzReadFile(f *testing.F) { + f.Add(buildTar(f, []entry{{name: "bin/app", typ: tar.TypeReg, body: []byte("\x7fELF")}}), "bin/app") + f.Add(buildTar(f, []entry{{name: "l", typ: tar.TypeSymlink, link: "bin/app"}}), "l") + f.Add([]byte{}, "../x") + f.Fuzz(func(t *testing.T, data []byte, name string) { + b, err := ReadFile(data, name, 64) + if err == nil && len(b) > 64 { + t.Fatalf("returned %d bytes over the limit", len(b)) + } + }) +} diff --git a/pkg/archive/tar.go b/pkg/archive/tar.go index c05bffc..d403730 100644 --- a/pkg/archive/tar.go +++ b/pkg/archive/tar.go @@ -246,3 +246,42 @@ func checkLinkTarget(name, linkname string) error { } return nil } + +// ReadFile returns the contents of the regular-file entry called name from a +// tar archive, without writing anything to disk. Symlinks are not followed: a +// symlink, directory or missing entry is an error, as is an entry larger than +// maxBytes. +func ReadFile(data []byte, name string, maxBytes int64) ([]byte, error) { + want, err := cleanEntry(name) + if err != nil { + return nil, err + } + tr := tar.NewReader(bytes.NewReader(data)) + for { + hdr, err := tr.Next() + if err == io.EOF { + return nil, fmt.Errorf("archive: no entry %q", name) + } + if err != nil { + return nil, err + } + got, err := cleanEntry(hdr.Name) + if err != nil { + return nil, err + } + if got != want { + continue + } + if hdr.Typeflag != tar.TypeReg { + return nil, fmt.Errorf("archive: entry %q is not a regular file", name) + } + if hdr.Size < 0 || hdr.Size > maxBytes { + return nil, fmt.Errorf("archive: %q (%d bytes) exceeds limit of %d bytes", name, hdr.Size, maxBytes) + } + b := make([]byte, hdr.Size) + if _, err := io.ReadFull(tr, b); err != nil { + return nil, err + } + return b, nil + } +} diff --git a/pkg/archive/tar_test.go b/pkg/archive/tar_test.go index bb80cd1..708d280 100644 --- a/pkg/archive/tar_test.go +++ b/pkg/archive/tar_test.go @@ -16,7 +16,7 @@ type entry struct { mode int64 } -func buildTar(t *testing.T, entries []entry) []byte { +func buildTar(t testing.TB, entries []entry) []byte { t.Helper() var buf bytes.Buffer tw := tar.NewWriter(&buf) @@ -278,3 +278,33 @@ func TestTarDirRejectsWhatUntarRefuses(t *testing.T) { }) } } + +func TestReadFile(t *testing.T) { + src := t.TempDir() + if err := os.MkdirAll(filepath.Join(src, "bin"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(src, "bin", "app"), []byte("ELF..."), 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink("bin/app", filepath.Join(src, "link")); err != nil { + t.Fatal(err) + } + data, err := TarDir(src) + if err != nil { + t.Fatal(err) + } + for _, name := range []string{"bin/app", "./bin/app", "bin//app"} { + if b, err := ReadFile(data, name, 1<<20); err != nil || string(b) != "ELF..." { + t.Fatalf("%s: %q %v", name, b, err) + } + } + for _, name := range []string{"link", "bin", "missing", "../bin/app", "/bin/app"} { + if _, err := ReadFile(data, name, 1<<20); err == nil { + t.Errorf("%s: expected an error", name) + } + } + if _, err := ReadFile(data, "bin/app", 3); err == nil { + t.Error("size limit not enforced") + } +} diff --git a/pkg/attest/attest.go b/pkg/attest/attest.go index 05b6518..321c39a 100644 --- a/pkg/attest/attest.go +++ b/pkg/attest/attest.go @@ -19,8 +19,9 @@ type Measurement struct { } // Verifier checks attestation evidence. nonce is the value the evidence must -// commit to (online.AttestationNonce of the request), so evidence made for a -// different request or license is rejected. Verify returns an error for any +// commit to in REPORT_DATA (online.AttestationBinding of the license id, the +// server's challenge and the loader's ephemeral public key), so evidence made +// for another challenge, license or key is rejected. Verify returns an error for any // evidence it cannot fully verify; it never accepts by default. type Verifier interface { Verify(evidence, nonce []byte) (Measurement, error) diff --git a/pkg/crypto/wrap.go b/pkg/crypto/wrap.go index cb4bf0e..f9cf935 100644 --- a/pkg/crypto/wrap.go +++ b/pkg/crypto/wrap.go @@ -7,9 +7,10 @@ import ( // Domain-separation labels. Changing any of these is a breaking format change. const ( - kekInfo = "cryptship/kek/v2" // v2: context-bound - wrapAAD = "cryptship/wrapped-key/v1" - blobAAD = "cryptship/payload/v1" + kekInfo = "cryptship/kek/v2" // v2: context-bound + stateInfo = "cryptship/state-key/v1" + wrapAAD = "cryptship/wrapped-key/v1" + blobAAD = "cryptship/payload/v1" ) // DeriveKEK derives a 32-byte key-encryption key from binding material, a @@ -23,6 +24,16 @@ func DeriveKEK(bindingMaterial, salt, context []byte) ([]byte, error) { return hkdf.Key(sha256.New, bindingMaterial, salt, kekInfo+"\x00"+string(h[:]), KeySize) } +// DeriveStateKey derives a 32-byte key for sealing the loader's local state +// (clock-rollback and revocation-grace records) from the same binding +// material, salt and claims context as the KEK, under a different HKDF label. +// It is per license and per binding (machine fingerprint, server secret), and +// is independent of the content key and the KEK. +func DeriveStateKey(bindingMaterial, salt, context []byte) ([]byte, error) { + h := sha256.Sum256(context) + return hkdf.Key(sha256.New, bindingMaterial, salt, stateInfo+"\x00"+string(h[:]), KeySize) +} + // WrapKey encrypts a content key under a KEK. func WrapKey(kek, contentKey []byte) ([]byte, error) { return Seal(kek, contentKey, []byte(wrapAAD)) diff --git a/pkg/harden/harden.go b/pkg/harden/harden.go new file mode 100644 index 0000000..98f4fb8 --- /dev/null +++ b/pkg/harden/harden.go @@ -0,0 +1,12 @@ +// Package harden has small process-hardening helpers for applications that +// cryptship-loader starts. +// +// The loader marks itself non-dumpable (prctl PR_SET_DUMPABLE 0) while it +// holds the content key, but execve resets that flag, so the application +// starts dumpable: the same uid can ptrace it and read /proc//mem and +// /proc//exe. Call NonDumpable first thing in main to close that again. +// A Python equivalent lives in helpers/python/cryptship_harden.py. +// +// This raises the bar against other processes of the same uid; it does not +// stop root (see docs/ARCHITECTURE.md, threat model). +package harden diff --git a/pkg/harden/harden_linux.go b/pkg/harden/harden_linux.go new file mode 100644 index 0000000..27355ae --- /dev/null +++ b/pkg/harden/harden_linux.go @@ -0,0 +1,33 @@ +//go:build linux + +package harden + +import "syscall" + +// From . +const ( + prGetDumpable = 3 + prSetDumpable = 4 +) + +// NonDumpable marks the calling process non-dumpable: no core dumps, and +// ptrace, process_vm_readv and /proc//{mem,exe} are refused to +// non-root processes. The flag applies to the whole process. +func NonDumpable() error { + if _, _, errno := syscall.RawSyscall(syscall.SYS_PRCTL, prSetDumpable, 0, 0); errno != 0 { + return errno + } + return nil +} + +// Dumpable reports the process's current dumpable flag. +func Dumpable() (bool, error) { + r, _, errno := syscall.RawSyscall(syscall.SYS_PRCTL, prGetDumpable, 0, 0) + if errno != 0 { + return false, errno + } + return r != 0, nil +} + +// Supported reports whether NonDumpable has an effect on this platform. +func Supported() bool { return true } diff --git a/pkg/harden/harden_other.go b/pkg/harden/harden_other.go new file mode 100644 index 0000000..a01685c --- /dev/null +++ b/pkg/harden/harden_other.go @@ -0,0 +1,12 @@ +//go:build !linux + +package harden + +// NonDumpable is a no-op outside Linux (the loader ships in Linux containers). +func NonDumpable() error { return nil } + +// Dumpable always reports true outside Linux. +func Dumpable() (bool, error) { return true, nil } + +// Supported reports whether NonDumpable has an effect on this platform. +func Supported() bool { return false } diff --git a/pkg/harden/harden_test.go b/pkg/harden/harden_test.go new file mode 100644 index 0000000..9e66db6 --- /dev/null +++ b/pkg/harden/harden_test.go @@ -0,0 +1,19 @@ +package harden + +import "testing" + +func TestNonDumpable(t *testing.T) { + if !Supported() { + t.Skip("PR_SET_DUMPABLE is Linux-only") + } + if err := NonDumpable(); err != nil { + t.Fatal(err) + } + d, err := Dumpable() + if err != nil { + t.Fatal(err) + } + if d { + t.Fatal("process is still dumpable") + } +} diff --git a/pkg/keyprovider/attested.go b/pkg/keyprovider/attested.go index 7632a64..7bafcfc 100644 --- a/pkg/keyprovider/attested.go +++ b/pkg/keyprovider/attested.go @@ -2,17 +2,24 @@ package keyprovider import ( "context" + "crypto/ecdh" + "crypto/rand" + "encoding/json" "errors" + "time" + "github.com/AI2TH/cryptship/pkg/crypto" "github.com/AI2TH/cryptship/pkg/license" + "github.com/AI2TH/cryptship/pkg/machine" "github.com/AI2TH/cryptship/pkg/online" ) // Attester produces TEE attestation evidence (e.g. an AMD SEV-SNP report or an // Intel TDX quote). Real implementations read /dev/sev-guest or /dev/tdx_guest -// and commit bindData (online.AttestationNonce of the license id and request -// nonce) into the report's REPORT_DATA field. ErrNoTEE is returned where no -// TEE is available. pkg/attest/attesttest has a test-only implementation. +// and put bindData (online.AttestationBinding of the license id, the server's +// challenge and the loader's ephemeral public key) into the report's +// REPORT_DATA field. ErrNoTEE is returned where no TEE is available. +// pkg/attest/attesttest has a test-only implementation. type Attester interface { Name() string Evidence(ctx context.Context, bindData []byte) ([]byte, error) @@ -22,14 +29,23 @@ type Attester interface { // cannot be satisfied here. var ErrNoTEE = errors.New("attested: no confidential-compute TEE available on this host") -// Attested releases the content key only after the loader sends TEE attestation -// evidence that the key broker verifies before returning the binding secret. -// The network flow reuses the online request (with the evidence attached). +// Attested releases the content key only after the key broker verifies TEE +// attestation evidence. The flow (see pkg/online/attested.go): // -// NOTE: real SEV-SNP/TDX quote generation requires confidential-compute -// hardware. DefaultAttester() reports ErrNoTEE on ordinary hosts; integrators -// supply a platform Attester (and run a key broker that verifies the quote, -// e.g. Confidential Containers Trustee). See docs/ARCHITECTURE.md. +// 1. fetch a single-use challenge from the server (POST /v1/challenge); +// 2. make an ephemeral X25519 key pair in this process (inside the TEE); +// 3. ask the Attester for evidence committing to +// online.AttestationBinding(license id, challenge, ephemeral public key); +// 4. POST /v1/key with the challenge, evidence and ephemeral public key; +// 5. verify the signed answer and decrypt the secret with the ephemeral key. +// +// A captured request cannot be replayed (the challenge is spent), and a +// captured answer is useless without the ephemeral private key. +// +// NOTE: real SEV-SNP/TDX evidence needs confidential-compute hardware. +// DefaultAttester() reports ErrNoTEE on ordinary hosts; integrators supply a +// platform Attester (and a server-side attest.Verifier). See +// docs/ARCHITECTURE.md. type Attested struct { Keys license.KeyRing Client HTTPClient @@ -44,12 +60,59 @@ func (Attested) Handles(p license.BindingPolicy) bool { return p == license.Bind // ContentKey implements Provider. func (a Attested) ContentKey(ctx context.Context, lic *license.License) ([]byte, error) { + return contentKeyOnly(a.Unlock(ctx, lic)) +} + +// Unlock implements Unlocker. Attested never uses the outage grace cache: +// every start re-attests. +func (a Attested) Unlock(ctx context.Context, lic *license.License) (Unlocked, error) { if a.Attester == nil { - return nil, ErrNoTEE + return Unlocked{}, ErrNoTEE + } + c := lic.Claims + + chBody, err := postJSON(ctx, a.Client, c, "/v1/challenge", online.ChallengeRequest{LicenseID: c.LicenseID}) + if err != nil { + return Unlocked{}, err + } + var ch online.ChallengeResponse + if err := json.Unmarshal(chBody, &ch); err != nil || len(ch.Challenge) == 0 { + return Unlocked{}, errors.New("attested: bad challenge from server") + } + + eph, err := ecdh.X25519().GenerateKey(rand.Reader) + if err != nil { + return Unlocked{}, err + } + ephPub := eph.PublicKey().Bytes() + evidence, err := a.Attester.Evidence(ctx, online.AttestationBinding(c.LicenseID, ch.Challenge, ephPub)) + if err != nil { + return Unlocked{}, err + } + + m, _ := machine.ID() + body, err := postJSON(ctx, a.Client, c, "/v1/key", online.KeyRequest{ + LicenseID: c.LicenseID, Machine: m, Nonce: ch.Challenge, + Attestation: evidence, EphemeralPub: ephPub, + Policy: string(c.Binding.Policy), SecretVersion: c.Binding.SecretVersion, + License: licenseJSON(lic), + }) + if err != nil { + return Unlocked{}, err + } + kr, err := online.VerifyAttestedKeyResponse(body, a.Keys, c.LicenseID, ch.Challenge, ephPub, time.Now()) + if err != nil { + return Unlocked{}, err + } + secret, err := online.OpenFromServer(eph, kr.ServerPub, c.LicenseID, ch.Challenge, kr.SealedSecret) + if err != nil { + return Unlocked{}, err + } + defer crypto.Wipe(secret) + if len(secret) != online.SecretSize { + return Unlocked{}, errors.New("attested: secret has the wrong size") } - return fetchAndUnwrap(ctx, a.Keys, a.Client, lic, func(nonce []byte) ([]byte, error) { - return a.Attester.Evidence(ctx, online.AttestationNonce(lic.Claims.LicenseID, nonce)) - }, nil) // attested never uses the outage grace cache — it must always re-attest + return UnwrapKeys(c, secret) } // noTEE is the default attester on hosts without confidential-compute hardware. diff --git a/pkg/keyprovider/attested_test.go b/pkg/keyprovider/attested_test.go new file mode 100644 index 0000000..d373772 --- /dev/null +++ b/pkg/keyprovider/attested_test.go @@ -0,0 +1,94 @@ +package keyprovider + +import ( + "bytes" + "context" + "crypto/ed25519" + "crypto/rand" + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/AI2TH/cryptship/pkg/attest/attesttest" + "github.com/AI2TH/cryptship/pkg/crypto" + "github.com/AI2TH/cryptship/pkg/license" + "github.com/AI2TH/cryptship/pkg/licenseserver" +) + +// recordingClient keeps every /v1/key request and response body. +type recordingClient struct { + inner *http.Client + reqs, answers [][]byte +} + +func (r *recordingClient) Do(req *http.Request) (*http.Response, error) { + var body []byte + if req.Body != nil { + body, _ = io.ReadAll(req.Body) + req.Body = io.NopCloser(bytes.NewReader(body)) + } + resp, err := r.inner.Do(req) + if err != nil || !strings.HasSuffix(req.URL.Path, "/v1/key") { + return resp, err + } + ans, _ := io.ReadAll(resp.Body) + resp.Body = io.NopCloser(bytes.NewReader(ans)) + r.reqs, r.answers = append(r.reqs, body), append(r.answers, ans) + return resp, nil +} + +// TestAttestedReplayIsRefused: an on-path observer who captures a complete, +// genuine attested exchange can neither replay the request nor use the +// answer. +func TestAttestedReplayIsRefused(t *testing.T) { + pub, priv, _ := ed25519.GenerateKey(rand.Reader) + master := make([]byte, 32) + if _, err := rand.Read(master); err != nil { + t.Fatal(err) + } + contentKey, _ := crypto.RandomKey() + srv := &licenseserver.Server{Master: master, SignKey: priv, KeyID: "v1", Revoked: map[string]bool{}, Verifier: attesttest.Verifier{}} + ts := httptest.NewServer(srv.Handler()) + defer ts.Close() + lic := serverBoundLicense(t, priv, master, contentKey, license.BindAttested, ts.URL) + + rec := &recordingClient{inner: ts.Client()} + got, err := (Attested{Keys: license.KeyRing{"v1": pub}, Client: rec, Attester: attesttest.Attester{}}).ContentKey(context.Background(), lic) + if err != nil || !bytes.Equal(got, contentKey) { + t.Fatalf("genuine attested start failed: %v", err) + } + if len(rec.reqs) != 1 { + t.Fatalf("recorded %d key requests", len(rec.reqs)) + } + + // Replay the captured request byte for byte: the challenge is spent. + resp, err := http.Post(ts.URL+"/v1/key", "application/json", bytes.NewReader(rec.reqs[0])) + if err != nil { + t.Fatal(err) + } + resp.Body.Close() + if resp.StatusCode != http.StatusForbidden { + t.Fatalf("replayed request: status %d, want 403", resp.StatusCode) + } + + // The captured answer carries no plaintext secret. + if bytes.Contains(rec.answers[0], []byte(`"secret"`)) { + t.Fatal("attested answer contains a plaintext secret field") + } +} + +func TestAttestedNeedsVerifierOnServer(t *testing.T) { + pub, priv, _ := ed25519.GenerateKey(rand.Reader) + master := make([]byte, 32) + contentKey, _ := crypto.RandomKey() + ts := httptest.NewServer((&licenseserver.Server{Master: master, SignKey: priv, KeyID: "v1"}).Handler()) + defer ts.Close() + lic := serverBoundLicense(t, priv, master, contentKey, license.BindAttested, ts.URL) + _, err := (Attested{Keys: license.KeyRing{"v1": pub}, Client: ts.Client(), Attester: attesttest.Attester{}}).ContentKey(context.Background(), lic) + if !errors.Is(err, ErrDenied) { + t.Fatalf("server without a verifier: err = %v, want ErrDenied", err) + } +} diff --git a/pkg/keyprovider/grace_test.go b/pkg/keyprovider/grace_test.go index b9a23a3..52e3ef1 100644 --- a/pkg/keyprovider/grace_test.go +++ b/pkg/keyprovider/grace_test.go @@ -5,7 +5,10 @@ import ( "context" "crypto/ed25519" "crypto/rand" + "errors" + "net/http" "net/http/httptest" + "sync/atomic" "testing" "time" @@ -82,3 +85,62 @@ func TestOnlineGraceExpires(t *testing.T) { t.Fatal("stale cache should not satisfy an outage") } } + +// A start served from the grace cache gets the same state key as a normal +// start, so the loader's sealed clock and revocation records stay readable. +func TestOnlineGraceKeepsStateKey(t *testing.T) { + pub, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + master := make([]byte, 32) + if _, err := rand.Read(master); err != nil { + t.Fatal(err) + } + contentKey, _ := crypto.RandomKey() + ts := httptest.NewServer((&licenseserver.Server{Master: master, SignKey: priv, KeyID: "v1"}).Handler()) + lic := serverBoundLicense(t, priv, master, contentKey, license.BindOnline, ts.URL) + o := Online{Keys: license.KeyRing{"v1": pub}, Client: ts.Client(), CacheDir: t.TempDir(), Grace: time.Hour} + live, err := o.Unlock(context.Background(), lic) + if err != nil || len(live.StateKey) != 32 { + t.Fatalf("live: %v", err) + } + ts.Close() + cached, err := o.Unlock(context.Background(), lic) + if err != nil { + t.Fatalf("grace: %v", err) + } + if !bytes.Equal(cached.ContentKey, contentKey) || !bytes.Equal(cached.StateKey, live.StateKey) { + t.Fatal("grace start returned different keys") + } +} + +// A 503 from a proxy in front of the server is an outage: grace applies. A +// revoked answer is not. +func TestOnlineGraceOnGatewayErrorButNotRevocation(t *testing.T) { + pub, priv, master, contentKey := newKeys(t) + srv := &licenseserver.Server{Master: master, SignKey: priv, KeyID: "v1", Revoked: map[string]bool{}} + var down atomic.Bool + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if down.Load() { + http.Error(w, "upstream down", http.StatusServiceUnavailable) + return + } + srv.Handler().ServeHTTP(w, r) + })) + defer ts.Close() + lic := serverBoundLicense(t, priv, master, contentKey, license.BindOnline, ts.URL) + o := Online{Keys: license.KeyRing{"v1": pub}, Client: ts.Client(), CacheDir: t.TempDir(), Grace: time.Hour} + if _, err := o.ContentKey(context.Background(), lic); err != nil { + t.Fatal(err) + } + down.Store(true) + if got, err := o.ContentKey(context.Background(), lic); err != nil || !bytes.Equal(got, contentKey) { + t.Fatalf("503 should be ridden out: %v", err) + } + down.Store(false) + srv.Revoked[lic.Claims.LicenseID] = true + if _, err := o.ContentKey(context.Background(), lic); !errors.Is(err, ErrRevoked) { + t.Fatalf("revoked: err = %v, want ErrRevoked (grace must not cover revocation)", err) + } +} diff --git a/pkg/keyprovider/offline.go b/pkg/keyprovider/offline.go index f8950fd..83fc4c2 100644 --- a/pkg/keyprovider/offline.go +++ b/pkg/keyprovider/offline.go @@ -22,28 +22,33 @@ func (Offline) Handles(p license.BindingPolicy) bool { } // ContentKey implements Provider. -func (Offline) ContentKey(_ context.Context, lic *license.License) ([]byte, error) { +func (o Offline) ContentKey(ctx context.Context, lic *license.License) ([]byte, error) { + return contentKeyOnly(o.Unlock(ctx, lic)) +} + +// Unlock implements Unlocker. +func (Offline) Unlock(_ context.Context, lic *license.License) (Unlocked, error) { c := lic.Claims switch c.Binding.Policy { case license.BindNone: if len(c.UnboundSecret) == 0 { - return nil, errors.New("offline: none-binding license is missing its unbound secret") + return Unlocked{}, errors.New("offline: none-binding license is missing its unbound secret") } - return Unwrap(c, c.UnboundSecret) + return UnwrapKeys(c, c.UnboundSecret) case license.BindStrict, license.BindPool: cands, err := machine.Candidates() if err != nil { - return nil, fmt.Errorf("offline: %w", err) + return Unlocked{}, fmt.Errorf("offline: %w", err) } for _, cand := range cands { - if key, err := Unwrap(c, []byte(cand.ID)); err == nil { - return key, nil + if u, err := UnwrapKeys(c, []byte(cand.ID)); err == nil { + return u, nil } } - return nil, fmt.Errorf("offline: %w (checked %d fingerprint(s) for license %s)", ErrNotBound, len(cands), c.LicenseID) + return Unlocked{}, fmt.Errorf("offline: %w (checked %d fingerprint(s) for license %s)", ErrNotBound, len(cands), c.LicenseID) default: - return nil, fmt.Errorf("offline: unsupported policy %q", c.Binding.Policy) + return Unlocked{}, fmt.Errorf("offline: unsupported policy %q", c.Binding.Policy) } } diff --git a/pkg/keyprovider/online.go b/pkg/keyprovider/online.go index 7024acf..d78b07d 100644 --- a/pkg/keyprovider/online.go +++ b/pkg/keyprovider/online.go @@ -3,7 +3,6 @@ package keyprovider import ( "bytes" "context" - "crypto/ed25519" "crypto/rand" "encoding/json" "errors" @@ -52,11 +51,16 @@ func (Online) Handles(p license.BindingPolicy) bool { return p == license.BindOn // ContentKey implements Provider. func (o Online) ContentKey(ctx context.Context, lic *license.License) ([]byte, error) { + return contentKeyOnly(o.Unlock(ctx, lic)) +} + +// Unlock implements Unlocker. +func (o Online) Unlock(ctx context.Context, lic *license.License) (Unlocked, error) { var g *graceConfig if o.CacheDir != "" && o.Grace > 0 { g = &graceConfig{dir: o.CacheDir, window: o.Grace} } - return fetchAndUnwrap(ctx, o.Keys, o.Client, lic, nil, g) + return fetchAndUnwrap(ctx, o.Keys, o.Client, lic, g) } // graceConfig configures the outage cache (online only). @@ -65,51 +69,81 @@ type graceConfig struct { window time.Duration } -// fetchAndUnwrap runs the request / verify / unwrap flow shared by the online -// and attested providers. evidence is nil for online; for attested it returns -// evidence bound to the request nonce. grace is non-nil only for online. -func fetchAndUnwrap(ctx context.Context, keys license.KeyRing, client HTTPClient, lic *license.License, evidence func(nonce []byte) ([]byte, error), grace *graceConfig) ([]byte, error) { +// fetchAndUnwrap runs the online request / verify / unwrap flow. grace is nil +// when the outage cache is disabled. +func fetchAndUnwrap(ctx context.Context, keys license.KeyRing, client HTTPClient, lic *license.License, grace *graceConfig) (Unlocked, error) { c := lic.Claims + nonce := make([]byte, 16) + if _, err := rand.Read(nonce); err != nil { + return Unlocked{}, err + } + m, _ := machine.ID() + respBody, err := postJSON(ctx, client, c, "/v1/key", online.KeyRequest{ + LicenseID: c.LicenseID, Machine: m, Nonce: nonce, + Policy: string(c.Binding.Policy), SecretVersion: c.Binding.SecretVersion, + License: licenseJSON(lic), + }) + if err != nil { + // Server unreachable: ride out the outage with the cached content key, if any. + var ue *unreachableError + if grace != nil && errors.As(err, &ue) { + if u, gerr := graceUnwrap(grace, c); gerr == nil { + fmt.Fprintln(os.Stderr, "cryptship: license server unreachable, using grace cache:", err) + return u, nil + } else if !errors.Is(gerr, os.ErrNotExist) { + fmt.Fprintln(os.Stderr, "cryptship: grace cache present but unusable:", gerr) + } + } + return Unlocked{}, err + } + kr, err := online.VerifyKeyResponse(respBody, keys, c.LicenseID, nonce, time.Now()) + if err != nil { + return Unlocked{}, err + } + defer crypto.Wipe(kr.Secret) + u, err := UnwrapKeys(c, kr.Secret) + if err != nil { + return Unlocked{}, err + } + if grace != nil { + // Cache the content key (and the local state key), not the + // per-license secret: the content key is per build and already lives + // in the running app's memory, and the state key only seals local + // state, so caching them adds no portable license-wide credential. + graceStore(grace, c, u, kr.NotAfter) // best effort + } + return u, nil +} + +// unreachableError marks a transport failure (no answer from the server), the +// only failure the outage grace cache may cover. +type unreachableError struct{ err error } + +func (e *unreachableError) Error() string { return "online: " + e.err.Error() } +func (e *unreachableError) Unwrap() error { return e.err } + +// postJSON POSTs v to the license's server at path and returns the body of a +// 200 answer. 410 maps to ErrRevoked and 403 to ErrDenied; a transport +// failure is an *unreachableError. +func postJSON(ctx context.Context, client HTTPClient, c license.Claims, path string, v any) ([]byte, error) { if c.Binding.Server == "" { return nil, errors.New("online: license has no binding.server") } if client == nil { return nil, errors.New("online: no HTTP client configured") } - nonce := make([]byte, 16) - if _, err := rand.Read(nonce); err != nil { - return nil, err - } - var attestation []byte - if evidence != nil { - var err error - if attestation, err = evidence(nonce); err != nil { - return nil, err - } - } - m, _ := machine.ID() - body, err := json.Marshal(online.KeyRequest{LicenseID: c.LicenseID, Machine: m, Nonce: nonce, Attestation: attestation}) + body, err := json.Marshal(v) if err != nil { return nil, err } - req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(c.Binding.Server, "/")+"/v1/key", bytes.NewReader(body)) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(c.Binding.Server, "/")+path, bytes.NewReader(body)) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json") - resp, err := client.Do(req) if err != nil { - // Server unreachable: ride out the outage with the cached content key, if any. - if grace != nil { - if key, gerr := graceUnwrap(grace, c); gerr == nil { - fmt.Fprintln(os.Stderr, "cryptship: license server unreachable, using grace cache:", err) - return key, nil - } else if !errors.Is(gerr, os.ErrNotExist) { - fmt.Fprintln(os.Stderr, "cryptship: grace cache present but unusable:", gerr) - } - } - return nil, fmt.Errorf("online: %w", err) + return nil, &unreachableError{err} } defer resp.Body.Close() switch resp.StatusCode { @@ -118,42 +152,32 @@ func fetchAndUnwrap(ctx context.Context, keys license.KeyRing, client HTTPClient return nil, ErrRevoked case http.StatusForbidden: return nil, ErrDenied + case http.StatusConflict: + msg, _ := io.ReadAll(io.LimitReader(resp.Body, 512)) + return nil, fmt.Errorf("%w: %s", ErrSeatLimit, strings.TrimSpace(string(msg))) + case http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout: + // A proxy in front of the server reports it down: an outage. + return nil, &unreachableError{fmt.Errorf("server returned %s", resp.Status)} default: return nil, fmt.Errorf("online: server returned %s", resp.Status) } - - var kr online.KeyResponse - if err := json.NewDecoder(io.LimitReader(resp.Body, 64<<10)).Decode(&kr); err != nil { - return nil, fmt.Errorf("online: bad response: %w", err) - } - pub, ok := keys[kr.SigningKeyID] - if !ok { - pub, ok = keys["*"] - } - if !ok { - return nil, fmt.Errorf("online: response signed by unknown key %q", kr.SigningKeyID) - } - if kr.LicenseID != c.LicenseID || !bytes.Equal(kr.Nonce, nonce) || len(kr.Secret) != online.SecretSize { - return nil, errors.New("online: response does not match request") - } - if kr.NotAfter != 0 && time.Now().Unix() > kr.NotAfter { - return nil, errors.New("online: response already expired") + b, err := io.ReadAll(io.LimitReader(resp.Body, online.MaxResponseBytes)) + if err != nil { + return nil, fmt.Errorf("online: read response: %w", err) } - if len(pub) != ed25519.PublicKeySize || !ed25519.Verify(pub, online.ResponseSigningInput(kr), kr.Sig) { - return nil, errors.New("online: bad response signature") + return b, nil +} + +// licenseJSON is the signed license as sent for seat enforcement. +func licenseJSON(lic *license.License) json.RawMessage { + if len(lic.ClaimsRaw) == 0 { + return nil } - defer crypto.Wipe(kr.Secret) - key, err := Unwrap(c, kr.Secret) + b, err := lic.Marshal() if err != nil { - return nil, err + return nil } - if grace != nil { - // Cache the content key, not the per-license secret: the content key is - // per build and already lives in the running app's memory, so caching it - // adds no portable license-wide credential. - graceStore(grace, c, key, kr.NotAfter) // best effort - } - return key, nil + return b } // --- outage grace cache --- @@ -209,14 +233,18 @@ func gracePath(dir, licenseID string) string { return filepath.Join(dir, "online-grace-"+url.PathEscape(licenseID)) } -func graceStore(g *graceConfig, c license.Claims, contentKey []byte, serverNotAfter int64) { +func graceStore(g *graceConfig, c license.Claims, u Unlocked, serverNotAfter int64) { kek, err := graceKEK(g.dir, c.LicenseID) if err != nil { return } defer crypto.Wipe(kek) cachedAt := time.Now().Unix() - sealed, err := crypto.Seal(kek, contentKey, graceAD(c.LicenseID, cachedAt, serverNotAfter)) + // The sealed value is content key || state key (64 bytes); a 32-byte + // record from an older loader holds the content key only. + pt := append(append([]byte{}, u.ContentKey...), u.StateKey...) + defer crypto.Wipe(pt) + sealed, err := crypto.Seal(kek, pt, graceAD(c.LicenseID, cachedAt, serverNotAfter)) if err != nil { return } @@ -230,38 +258,46 @@ func graceStore(g *graceConfig, c license.Claims, contentKey []byte, serverNotAf _ = os.WriteFile(gracePath(g.dir, c.LicenseID), rec, 0o600) } -// graceUnwrap returns the cached content key if the cache is present, within the +// graceUnwrap returns the cached keys if the cache is present, within the // local window AND the server-stated NotAfter, not clock-rolled-back, and its // authenticated timestamps are intact. -func graceUnwrap(g *graceConfig, c license.Claims) ([]byte, error) { +func graceUnwrap(g *graceConfig, c license.Claims) (Unlocked, error) { b, err := os.ReadFile(gracePath(g.dir, c.LicenseID)) if err != nil { - return nil, err + return Unlocked{}, err } var rec graceRecord if err := json.Unmarshal(b, &rec); err != nil { - return nil, err + return Unlocked{}, err } now := time.Now().Unix() if rec.CachedAt > now+license.ClockSkew { - return nil, errors.New("grace cache: cached in the future (clock rollback)") + return Unlocked{}, errors.New("grace cache: cached in the future (clock rollback)") } if time.Since(time.Unix(rec.CachedAt, 0)) > g.window { - return nil, errors.New("grace cache: older than the grace window") + return Unlocked{}, errors.New("grace cache: older than the grace window") } if rec.NotAfter != 0 && now > rec.NotAfter { - return nil, errors.New("grace cache: past the server-stated validity") + return Unlocked{}, errors.New("grace cache: past the server-stated validity") } kek, err := graceKEK(g.dir, c.LicenseID) if err != nil { - return nil, err + return Unlocked{}, err } defer crypto.Wipe(kek) // Open also authenticates CachedAt/NotAfter via the AAD: editing them to // widen the window makes this fail. - contentKey, err := crypto.Open(kek, rec.Sealed, graceAD(c.LicenseID, rec.CachedAt, rec.NotAfter)) + pt, err := crypto.Open(kek, rec.Sealed, graceAD(c.LicenseID, rec.CachedAt, rec.NotAfter)) if err != nil { - return nil, fmt.Errorf("grace cache: %w (tampered, or a different machine)", err) + return Unlocked{}, fmt.Errorf("grace cache: %w (tampered, or a different machine)", err) + } + switch len(pt) { + case 2 * crypto.KeySize: + return Unlocked{ContentKey: pt[:crypto.KeySize], StateKey: pt[crypto.KeySize:]}, nil + case crypto.KeySize: + return Unlocked{ContentKey: pt}, nil + default: + crypto.Wipe(pt) + return Unlocked{}, errors.New("grace cache: malformed record") } - return contentKey, nil } diff --git a/pkg/keyprovider/provider.go b/pkg/keyprovider/provider.go index b9c3806..185db26 100644 --- a/pkg/keyprovider/provider.go +++ b/pkg/keyprovider/provider.go @@ -28,8 +28,49 @@ var ( // ErrDenied means the server refused to release the key for another // reason (403 Forbidden), such as rejected attestation evidence. ErrDenied = errors.New("keyprovider: server refused to release the key (e.g. attestation rejected)") + // ErrSeatLimit means the server refused because the license is already + // in use on its maximum number of machines (409 Conflict). + ErrSeatLimit = errors.New("keyprovider: license seat limit reached") ) +// Unlocked is what a provider recovers for a license. +type Unlocked struct { + // ContentKey decrypts the payload. + ContentKey []byte + // StateKey seals the loader's local state (crypto.DeriveStateKey of the + // binding material that unwrapped the content key). Nil if the provider + // does not implement Unlocker. + StateKey []byte +} + +// Wipe zeroes both keys. +func (u Unlocked) Wipe() { + crypto.Wipe(u.ContentKey) + crypto.Wipe(u.StateKey) +} + +// Unlocker is implemented by providers that also return a state key. All the +// built-in providers do. +type Unlocker interface { + Unlock(ctx context.Context, lic *license.License) (Unlocked, error) +} + +// Unlock uses p's Unlock if it has one, and ContentKey otherwise (with no +// state key). +func Unlock(ctx context.Context, p Provider, lic *license.License) (Unlocked, error) { + if u, ok := p.(Unlocker); ok { + return u.Unlock(ctx, lic) + } + k, err := p.ContentKey(ctx, lic) + return Unlocked{ContentKey: k}, err +} + +// contentKeyOnly adapts an Unlock result to ContentKey. +func contentKeyOnly(u Unlocked, err error) ([]byte, error) { + crypto.Wipe(u.StateKey) + return u.ContentKey, err +} + // Select returns the first provider that handles the license's binding policy. func Select(lic *license.License, providers ...Provider) (Provider, error) { for _, p := range providers { @@ -44,19 +85,32 @@ func Select(lic *license.License, providers ...Provider) (Provider, error) { // tries every wrapped key until one authenticates. A returned key is genuine // (a wrong KEK fails the AEAD tag). func Unwrap(c license.Claims, material []byte) ([]byte, error) { + return contentKeyOnly(UnwrapKeys(c, material)) +} + +// UnwrapKeys is Unwrap that also derives the state key from the binding +// material that unwrapped the content key. +func UnwrapKeys(c license.Claims, material []byte) (Unlocked, error) { bctx, err := c.BindingContext() if err != nil { - return nil, err + return Unlocked{}, err } kek, err := crypto.DeriveKEK(material, []byte(c.LicenseID), bctx) if err != nil { - return nil, err + return Unlocked{}, err } defer crypto.Wipe(kek) for _, wk := range c.WrappedKeys { - if key, err := crypto.UnwrapKey(kek, wk); err == nil { - return key, nil + key, err := crypto.UnwrapKey(kek, wk) + if err != nil { + continue + } + sk, err := crypto.DeriveStateKey(material, []byte(c.LicenseID), bctx) + if err != nil { + crypto.Wipe(key) + return Unlocked{}, err } + return Unlocked{ContentKey: key, StateKey: sk}, nil } - return nil, ErrNotBound + return Unlocked{}, ErrNotBound } diff --git a/pkg/keyprovider/provider_test.go b/pkg/keyprovider/provider_test.go index 6659f85..f371d2d 100644 --- a/pkg/keyprovider/provider_test.go +++ b/pkg/keyprovider/provider_test.go @@ -38,8 +38,14 @@ func wrapForTest(t *testing.T, c license.Claims, material, contentKey []byte) [] // serverBoundLicense builds a license whose content key is wrapped under the KEK // derived from the server-held binding secret (what `cryptship issue --binding -// online|attested` does). +// online|attested` does), with secret version 2. func serverBoundLicense(t *testing.T, priv ed25519.PrivateKey, master, contentKey []byte, policy license.BindingPolicy, server string) *license.License { + t.Helper() + return serverBoundLicenseV(t, priv, master, contentKey, policy, server, license.SecretVersion2) +} + +// serverBoundLicenseV is serverBoundLicense with an explicit secret version. +func serverBoundLicenseV(t *testing.T, priv ed25519.PrivateKey, master, contentKey []byte, policy license.BindingPolicy, server string, secretVersion int) *license.License { t.Helper() now := time.Now().Unix() c := license.Claims{ @@ -51,9 +57,9 @@ func serverBoundLicense(t *testing.T, priv ed25519.PrivateKey, master, contentKe IssuedAt: now, NotAfter: now + 3600, SigningKeyID: "v1", - Binding: license.Binding{Policy: policy, Server: server}, + Binding: license.Binding{Policy: policy, Server: server, SecretVersion: secretVersion}, } - secret, err := online.DeriveSecret(master, c.LicenseID) + secret, err := online.DeriveSecretFor(master, c.LicenseID, string(policy), secretVersion) if err != nil { t.Fatal(err) } @@ -147,9 +153,9 @@ func TestAttestedProviderRoundTrip(t *testing.T) { t.Fatalf("no-TEE: err = %v, want ErrNoTEE", err) } - // Evidence that is not bound to this request's nonce (e.g. replayed from - // an earlier request) is refused by the server. - stale := staleAttester{bindData: online.AttestationNonce(lic.Claims.LicenseID, []byte("an-older-nonce!!"))} + // Evidence that is not bound to this request's challenge and ephemeral + // key (e.g. replayed from an earlier request) is refused by the server. + stale := staleAttester{bindData: online.AttestationBinding(lic.Claims.LicenseID, []byte("an-older-challenge"), make([]byte, 32))} if _, err := (Attested{Keys: ring, Client: ts.Client(), Attester: stale}).ContentKey(context.Background(), lic); !errors.Is(err, ErrDenied) { t.Fatalf("evidence for another nonce: err = %v, want ErrDenied", err) } @@ -163,3 +169,39 @@ func (staleAttester) Name() string { return "stale" } func (s staleAttester) Evidence(ctx context.Context, _ []byte) ([]byte, error) { return attesttest.Attester{}.Evidence(ctx, s.bindData) } + +// The state key comes from the binding material: each fingerprint of a pool +// license gives a different one, none equals the content key, and it is +// stable across starts. +func TestStateKeyIsBindingDerived(t *testing.T) { + contentKey, _ := crypto.RandomKey() + now := time.Now().Unix() + c := license.Claims{ + Version: license.FormatVersion, LicenseID: "lic_p", Product: "p", PayloadID: "b", + IssuedAt: now, NotAfter: now + 3600, Binding: license.Binding{Policy: license.BindPool, Max: 2}, + } + c.WrappedKeys = [][]byte{wrapForTest(t, c, []byte("fp-a"), contentKey), wrapForTest(t, c, []byte("fp-b"), contentKey)} + a, err := UnwrapKeys(c, []byte("fp-a")) + if err != nil { + t.Fatal(err) + } + a2, _ := UnwrapKeys(c, []byte("fp-a")) + b, _ := UnwrapKeys(c, []byte("fp-b")) + if !bytes.Equal(a.ContentKey, contentKey) || !bytes.Equal(b.ContentKey, contentKey) { + t.Fatal("content key mismatch") + } + if len(a.StateKey) != 32 || bytes.Equal(a.StateKey, b.StateKey) || bytes.Equal(a.StateKey, contentKey) || !bytes.Equal(a.StateKey, a2.StateKey) { + t.Fatal("state key must be 32 bytes, per binding, stable, and not the content key") + } + // Another license with the same content key and fingerprint gets another state key. + c2 := c + c2.LicenseID = "lic_q" + c2.WrappedKeys = [][]byte{wrapForTest(t, c2, []byte("fp-a"), contentKey)} + other, _ := UnwrapKeys(c2, []byte("fp-a")) + if bytes.Equal(other.StateKey, a.StateKey) { + t.Fatal("state key shared across licenses") + } + if _, err := UnwrapKeys(c, []byte("fp-x")); !errors.Is(err, ErrNotBound) { + t.Fatalf("unbound fingerprint: %v", err) + } +} diff --git a/pkg/keyprovider/secretversion_test.go b/pkg/keyprovider/secretversion_test.go new file mode 100644 index 0000000..65f2029 --- /dev/null +++ b/pkg/keyprovider/secretversion_test.go @@ -0,0 +1,153 @@ +package keyprovider + +import ( + "bytes" + "context" + "crypto/ed25519" + "crypto/rand" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/AI2TH/cryptship/pkg/attest/attesttest" + "github.com/AI2TH/cryptship/pkg/crypto" + "github.com/AI2TH/cryptship/pkg/license" + "github.com/AI2TH/cryptship/pkg/licenseserver" + "github.com/AI2TH/cryptship/pkg/online" +) + +func newKeys(t *testing.T) (ed25519.PublicKey, ed25519.PrivateKey, []byte, []byte) { + t.Helper() + pub, priv, _ := ed25519.GenerateKey(rand.Reader) + master := make([]byte, 32) + if _, err := rand.Read(master); err != nil { + t.Fatal(err) + } + contentKey, _ := crypto.RandomKey() + return pub, priv, master, contentKey +} + +// An online server (no verifier) that shares the master key must not be able +// to release a secret that unwraps a version-2 attested license, whatever +// policy or secret version the caller asks for. +func TestOnlineServerCannotReleaseAttestedSecret(t *testing.T) { + pub, priv, master, contentKey := newKeys(t) + ts := httptest.NewServer((&licenseserver.Server{Master: master, SignKey: priv, KeyID: "v1"}).Handler()) + defer ts.Close() + lic := serverBoundLicense(t, priv, master, contentKey, license.BindAttested, ts.URL) + keys := map[string]ed25519.PublicKey{"v1": pub} + + for _, policy := range []string{"", "online", "attested", "none"} { + for _, sv := range []int{0, 2, 3} { + nonce := make([]byte, 16) + _, _ = rand.Read(nonce) + b, _ := json.Marshal(online.KeyRequest{LicenseID: lic.Claims.LicenseID, Nonce: nonce, Policy: policy, SecretVersion: sv}) + resp, err := http.Post(ts.URL+"/v1/key", "application/json", bytes.NewReader(b)) + if err != nil { + t.Fatal(err) + } + body, _ := io.ReadAll(resp.Body) + resp.Body.Close() + if resp.StatusCode != http.StatusOK { + continue + } + kr, err := online.VerifyKeyResponse(body, keys, lic.Claims.LicenseID, nonce, time.Now()) + if err != nil { + t.Fatal(err) + } + if _, err := Unwrap(lic.Claims, kr.Secret); err == nil { + t.Fatalf("policy %q secret_v %d: an online server released a secret that unwraps the attested license", policy, sv) + } + } + } + // The real attested path still fails there (no verifier) ... + if _, err := (Attested{Keys: keys, Client: ts.Client(), Attester: attesttest.Attester{}}).ContentKey(context.Background(), lic); !errors.Is(err, ErrDenied) { + t.Fatalf("attested on an online server: %v", err) + } + // ... and works on a verifying server with the same master key. + vs := httptest.NewServer((&licenseserver.Server{Master: master, SignKey: priv, KeyID: "v1", Verifier: attesttest.Verifier{}}).Handler()) + defer vs.Close() + lic2 := serverBoundLicense(t, priv, master, contentKey, license.BindAttested, vs.URL) + if got, err := (Attested{Keys: keys, Client: vs.Client(), Attester: attesttest.Attester{}}).ContentKey(context.Background(), lic2); err != nil || !bytes.Equal(got, contentKey) { + t.Fatalf("attested on a verifying server: %v", err) + } +} + +// Licenses issued before secret version 2 keep working, until the server is +// told to refuse the legacy derivation. +func TestLegacySecretVersion(t *testing.T) { + pub, priv, master, contentKey := newKeys(t) + keys := license.KeyRing{"v1": pub} + srv := &licenseserver.Server{Master: master, SignKey: priv, KeyID: "v1"} + ts := httptest.NewServer(srv.Handler()) + defer ts.Close() + + legacy := serverBoundLicenseV(t, priv, master, contentKey, license.BindOnline, ts.URL, license.SecretVersionLegacy) + if got, err := (Online{Keys: keys, Client: ts.Client()}).ContentKey(context.Background(), legacy); err != nil || !bytes.Equal(got, contentKey) { + t.Fatalf("legacy online license: %v", err) + } + v2 := serverBoundLicense(t, priv, master, contentKey, license.BindOnline, ts.URL) + if got, err := (Online{Keys: keys, Client: ts.Client()}).ContentKey(context.Background(), v2); err != nil || !bytes.Equal(got, contentKey) { + t.Fatalf("v2 online license: %v", err) + } + + srv.NoLegacySecrets = true + if _, err := (Online{Keys: keys, Client: ts.Client()}).ContentKey(context.Background(), legacy); !errors.Is(err, ErrDenied) { + t.Fatalf("legacy license with NoLegacySecrets: err = %v, want ErrDenied", err) + } + if _, err := (Online{Keys: keys, Client: ts.Client()}).ContentKey(context.Background(), v2); err != nil { + t.Fatalf("v2 license with NoLegacySecrets: %v", err) + } +} + +// A verifying server also serves v2 online licenses (their secret cannot open +// an attested license), but never legacy ones over the plain path. +func TestVerifyingServerServesOnlineV2Only(t *testing.T) { + pub, priv, master, contentKey := newKeys(t) + keys := license.KeyRing{"v1": pub} + ts := httptest.NewServer((&licenseserver.Server{Master: master, SignKey: priv, KeyID: "v1", Verifier: attesttest.Verifier{}}).Handler()) + defer ts.Close() + v2 := serverBoundLicense(t, priv, master, contentKey, license.BindOnline, ts.URL) + if _, err := (Online{Keys: keys, Client: ts.Client()}).ContentKey(context.Background(), v2); err != nil { + t.Fatalf("v2 online on a verifying server: %v", err) + } + legacy := serverBoundLicenseV(t, priv, master, contentKey, license.BindOnline, ts.URL, license.SecretVersionLegacy) + if _, err := (Online{Keys: keys, Client: ts.Client()}).ContentKey(context.Background(), legacy); !errors.Is(err, ErrDenied) { + t.Fatalf("legacy online on a verifying server: err = %v, want ErrDenied", err) + } +} + +// A full seat table surfaces as ErrSeatLimit, and the loader sends its +// license so the server can read binding.max. +func TestOnlineSeatLimit(t *testing.T) { + pub, priv, master, contentKey := newKeys(t) + srv := &licenseserver.Server{Master: master, SignKey: priv, KeyID: "v1", Seats: &licenseserver.SeatTracker{}} + ts := httptest.NewServer(srv.Handler()) + defer ts.Close() + now := time.Now().Unix() + c := license.Claims{ + Version: license.FormatVersion, LicenseID: "lic_seat", PayloadID: "b", IssuedAt: now, NotAfter: now + 3600, SigningKeyID: "v1", + Binding: license.Binding{Policy: license.BindOnline, Server: ts.URL, Max: 1, SecretVersion: license.SecretVersion2}, + } + secret, _ := online.DeriveSecretFor(master, c.LicenseID, "online", license.SecretVersion2) + c.WrappedKeys = [][]byte{wrapForTest(t, c, secret, contentKey)} + lic, err := license.Sign(priv, c) + if err != nil { + t.Fatal(err) + } + // Another machine holds the only seat. + if err := srv.Seats.Claim("lic_seat", "some-other-machine", 1, time.Now()); err != nil { + t.Fatal(err) + } + if _, err := (Online{Keys: license.KeyRing{"v1": pub}, Client: ts.Client()}).ContentKey(context.Background(), lic); !errors.Is(err, ErrSeatLimit) { + t.Fatalf("err = %v, want ErrSeatLimit", err) + } + srv.Seats.Release("lic_seat", "some-other-machine") + if _, err := (Online{Keys: license.KeyRing{"v1": pub}, Client: ts.Client()}).ContentKey(context.Background(), lic); err != nil && !errors.Is(err, ErrSeatLimit) { + t.Fatalf("after release: %v", err) + } +} diff --git a/pkg/license/fuzz_test.go b/pkg/license/fuzz_test.go new file mode 100644 index 0000000..6c9dd31 --- /dev/null +++ b/pkg/license/fuzz_test.go @@ -0,0 +1,61 @@ +package license + +import ( + "bytes" + "crypto/ed25519" + "crypto/rand" + "encoding/json" + "testing" +) + +// FuzzParse: Parse never panics; whatever it accepts round-trips through +// Marshal, has a supported version, and never verifies under an unrelated key. +func FuzzParse(f *testing.F) { + _, priv, _ := ed25519.GenerateKey(rand.Reader) + other, _, _ := ed25519.GenerateKey(rand.Reader) + for _, c := range []Claims{ + {Version: FormatVersion, LicenseID: "lic", PayloadID: "p", SigningKeyID: "v1", Binding: Binding{Policy: BindNone}, UnboundSecret: make([]byte, 32), WrappedKeys: [][]byte{{1, 2}}}, + {Version: FormatVersion1, LicenseID: "lic", PayloadID: "p", Binding: Binding{Policy: BindOnline, Server: "https://k", Max: 3, SecretVersion: SecretVersion2}, WrappedKeys: [][]byte{{1}}}, + } { + lic, err := Sign(priv, c) + if err != nil { + f.Fatal(err) + } + b, _ := lic.Marshal() + f.Add(b) + } + f.Add([]byte(`{"claims":{"v":99},"sig_alg":"ed25519","sig":""}`)) + f.Add([]byte(`{"claims":null}`)) + f.Add([]byte(`{"claims":{"v":2,"extra":1}}`)) + + f.Fuzz(func(t *testing.T, data []byte) { + lic, err := Parse(data) + if err != nil { + return + } + if lic.Claims.Version < FormatVersion1 || lic.Claims.Version > FormatVersion { + t.Fatalf("accepted version %d", lic.Claims.Version) + } + _, _ = lic.Claims.BindingContext() + _ = lic.Claims.Validate() + _ = lic.Claims.Expired(0) + if lic.Verify(other) == nil { + t.Fatal("verified under an unrelated key") + } + b, err := lic.Marshal() + if err != nil { + t.Fatalf("marshal: %v", err) + } + again, err := Parse(b) + if err != nil { + t.Fatalf("re-parse of marshalled license: %v", err) + } + // encoding/json compacts raw claims, so whitespace in hand-written + // claims is not preserved; compact claims must survive byte for byte. + var compact bytes.Buffer + if json.Compact(&compact, lic.ClaimsRaw) == nil && bytes.Equal(compact.Bytes(), lic.ClaimsRaw) && + (!bytes.Equal(again.ClaimsRaw, lic.ClaimsRaw) || !bytes.Equal(again.Sig, lic.Sig)) { + t.Fatalf("round trip changed the signed bytes:\n%s\n%s", lic.ClaimsRaw, again.ClaimsRaw) + } + }) +} diff --git a/pkg/license/license.go b/pkg/license/license.go index 3f9b724..28f831a 100644 --- a/pkg/license/license.go +++ b/pkg/license/license.go @@ -58,8 +58,19 @@ type Binding struct { Server string `json:"server,omitempty"` // ProviderData is opaque, provider-defined bytes (e.g. attestation policy id). ProviderData []byte `json:"provider_data,omitempty"` + // SecretVersion selects how the server derives the binding secret + // (online, attested). 0 (absent) is the legacy derivation from the master + // secret and license id alone; SecretVersion2 also binds the policy, so + // an online server cannot produce an attested license's secret. + SecretVersion int `json:"secret_v,omitempty"` } +// Server secret derivation versions (Binding.SecretVersion). +const ( + SecretVersionLegacy = 0 + SecretVersion2 = 2 +) + // Claims is the signed portion of a license. Field order is significant: the // v2 KEK context hashes the encoding/json form of this struct, so fields must // not be reordered or renamed, and any field added later must be `omitempty` @@ -184,6 +195,9 @@ func (c Claims) Validate() error { if c.NotAfter != 0 && c.NotAfter <= c.IssuedAt { return errors.New("cryptship/license: not_after <= issued_at") } + if c.Binding.SecretVersion != SecretVersionLegacy && c.Binding.Policy != BindOnline && c.Binding.Policy != BindAttested { + return errors.New("cryptship/license: binding.secret_v applies only to online/attested binding") + } switch c.Binding.Policy { case BindNone: if len(c.UnboundSecret) != 32 || len(c.WrappedKeys) != 1 { @@ -197,6 +211,9 @@ func (c Claims) Validate() error { if c.Binding.Server == "" { return errors.New("cryptship/license: online/attested binding needs binding.server") } + if v := c.Binding.SecretVersion; v != SecretVersionLegacy && v != SecretVersion2 { + return fmt.Errorf("cryptship/license: unsupported binding.secret_v %d", v) + } if len(c.UnboundSecret) != 0 || len(c.WrappedKeys) < 1 { return errors.New("cryptship/license: online/attested binding needs wrapped keys and no unbound_secret") } @@ -208,7 +225,20 @@ func (c Claims) Validate() error { // Marshal serialises a license for storage. It must stay compact: the signature // covers ClaimsRaw byte-for-byte, and Sign produces compact bytes. -func (l *License) Marshal() ([]byte, error) { return json.Marshal(l) } +// +// HTML escaping is off: encoding/json would otherwise rewrite '&', '<' and +// '>' inside the raw claims as \u0026 and so on, changing the signed bytes +// of a license whose claims contain them literally (found by FuzzParse). +// Licenses from Sign are unaffected, since their claims are already escaped. +func (l *License) Marshal() ([]byte, error) { + var buf bytes.Buffer + enc := json.NewEncoder(&buf) + enc.SetEscapeHTML(false) + if err := enc.Encode(l); err != nil { + return nil, err + } + return bytes.TrimSuffix(buf.Bytes(), []byte("\n")), nil +} // Parse decodes a license and its claims view. func Parse(b []byte) (*License, error) { diff --git a/pkg/license/license_test.go b/pkg/license/license_test.go index 4cde9bf..5f481fd 100644 --- a/pkg/license/license_test.go +++ b/pkg/license/license_test.go @@ -1,6 +1,7 @@ package license import ( + "bytes" "crypto/ed25519" "crypto/rand" "encoding/json" @@ -145,3 +146,42 @@ func TestExpired(t *testing.T) { t.Fatalf("no-expiry license reported expired") } } + +func TestValidateSecretVersion(t *testing.T) { + base := Claims{Version: FormatVersion, LicenseID: "l", PayloadID: "p", WrappedKeys: [][]byte{{1}}, + Binding: Binding{Policy: BindOnline, Server: "https://k"}} + for _, sv := range []int{SecretVersionLegacy, SecretVersion2} { + c := base + c.Binding.SecretVersion = sv + if err := c.Validate(); err != nil { + t.Fatalf("secret_v %d: %v", sv, err) + } + } + c := base + c.Binding.SecretVersion = 1 + if err := c.Validate(); err == nil { + t.Fatal("secret_v 1 accepted") + } + c = Claims{Version: FormatVersion, LicenseID: "l", PayloadID: "p", WrappedKeys: [][]byte{{1}}, + Binding: Binding{Policy: BindStrict, SecretVersion: SecretVersion2}} + if err := c.Validate(); err == nil { + t.Fatal("secret_v accepted on a strict license") + } +} + +// Marshal keeps the signed claim bytes, even with '&', '<' or '>' in them. +func TestMarshalKeepsSignedBytes(t *testing.T) { + raw := []byte(`{"claims":{"v":2,"license_id":"l","customer_id":"Smith & Sons ","product":"p","payload_id":"x","issued_at":0,"not_after":0,"binding":{"policy":"none"},"wrapped_keys":null,"signing_key_id":""},"sig_alg":"ed25519","sig":"AA=="}`) + lic, err := Parse(raw) + if err != nil { + t.Fatal(err) + } + out, err := lic.Marshal() + if err != nil { + t.Fatal(err) + } + again, err := Parse(out) + if err != nil || !bytes.Equal(again.ClaimsRaw, lic.ClaimsRaw) { + t.Fatalf("claims changed: %s", out) + } +} diff --git a/pkg/licenseserver/seats.go b/pkg/licenseserver/seats.go new file mode 100644 index 0000000..4dca7ec --- /dev/null +++ b/pkg/licenseserver/seats.go @@ -0,0 +1,140 @@ +package licenseserver + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "sync" + "time" +) + +// ErrSeatLimit means a license already has its maximum number of machines. +var ErrSeatLimit = errors.New("seat limit reached") + +// SeatTracker enforces binding.max distinct machines per license. A machine +// holds a seat from its last key request until Lease passes without another +// one, so a retired machine frees its seat on its own. +// +// Seats count the machine fingerprints loaders report. A modified loader can +// report any value, so this is an honest-customer limit, not a cryptographic +// one: every machine that gets the secret can decrypt. +type SeatTracker struct { + // Lease is how long a seat is held after a machine's last request + // (default 30 days). + Lease time.Duration + // Path, when set, persists seats as JSON so a restart keeps them. + Path string + + mu sync.Mutex + loaded bool + seats map[string]map[string]int64 // license id -> machine -> last seen (unix) +} + +func (t *SeatTracker) lease() time.Duration { + if t.Lease <= 0 { + return 30 * 24 * time.Hour + } + return t.Lease +} + +// Claim records that machine uses licenseID at now. It fails with +// ErrSeatLimit if max other machines hold live seats. max <= 0 means no limit. +func (t *SeatTracker) Claim(licenseID, machine string, max int, now time.Time) error { + if max <= 0 { + return nil + } + if machine == "" { + return fmt.Errorf("%w: the loader sent no machine fingerprint (mount /etc/machine-id)", ErrSeatLimit) + } + t.mu.Lock() + defer t.mu.Unlock() + t.load() + m := t.seats[licenseID] + if m == nil { + m = map[string]int64{} + t.seats[licenseID] = m + } + cutoff := now.Add(-t.lease()).Unix() + for id, seen := range m { + if seen < cutoff { + delete(m, id) + } + } + if _, ok := m[machine]; !ok && len(m) >= max { + return fmt.Errorf("%w: license %s is in use on %d machine(s), the maximum", ErrSeatLimit, licenseID, max) + } + m[machine] = now.Unix() + t.save() + return nil +} + +// Release frees machine's seat on licenseID (for example after a machine is +// retired). +func (t *SeatTracker) Release(licenseID, machine string) { + t.mu.Lock() + defer t.mu.Unlock() + t.load() + delete(t.seats[licenseID], machine) + t.save() +} + +// InUse returns how many machines hold a live seat on licenseID at now. +func (t *SeatTracker) InUse(licenseID string, now time.Time) int { + t.mu.Lock() + defer t.mu.Unlock() + t.load() + n, cutoff := 0, now.Add(-t.lease()).Unix() + for _, seen := range t.seats[licenseID] { + if seen >= cutoff { + n++ + } + } + return n +} + +func (t *SeatTracker) load() { + if t.loaded { + return + } + t.loaded, t.seats = true, map[string]map[string]int64{} + if t.Path == "" { + return + } + if b, err := os.ReadFile(t.Path); err == nil { + if err := json.Unmarshal(b, &t.seats); err != nil || t.seats == nil { + fmt.Fprintf(os.Stderr, "cryptship-server: WARNING: ignoring unreadable seats file %s: %v\n", t.Path, err) + t.seats = map[string]map[string]int64{} + } + } +} + +func (t *SeatTracker) save() { + if t.Path == "" { + return + } + b, err := json.Marshal(t.seats) + if err == nil { + err = writeAtomic(t.Path, b) + } + if err != nil { + fmt.Fprintf(os.Stderr, "cryptship-server: WARNING: seats not saved to %s: %v\n", t.Path, err) + } +} + +func writeAtomic(path string, data []byte) error { + tmp, err := os.CreateTemp(filepath.Dir(path), ".tmp-"+filepath.Base(path)+"-*") + if err != nil { + return err + } + defer os.Remove(tmp.Name()) + if _, err := tmp.Write(data); err != nil { + tmp.Close() + return err + } + if err := tmp.Close(); err != nil { + return err + } + return os.Rename(tmp.Name(), path) +} diff --git a/pkg/licenseserver/seats_test.go b/pkg/licenseserver/seats_test.go new file mode 100644 index 0000000..1f2d2de --- /dev/null +++ b/pkg/licenseserver/seats_test.go @@ -0,0 +1,112 @@ +package licenseserver + +import ( + "bytes" + "crypto/ed25519" + "crypto/rand" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "path/filepath" + "testing" + "time" + + "github.com/AI2TH/cryptship/pkg/license" + "github.com/AI2TH/cryptship/pkg/online" +) + +func TestSeatTracker(t *testing.T) { + path := filepath.Join(t.TempDir(), "seats.json") + st := &SeatTracker{Lease: time.Hour, Path: path} + now := time.Now() + for _, m := range []string{"m1", "m2", "m1"} { + if err := st.Claim("lic", m, 2, now); err != nil { + t.Fatalf("%s: %v", m, err) + } + } + if err := st.Claim("lic", "m3", 2, now); !errors.Is(err, ErrSeatLimit) { + t.Fatalf("third machine: err = %v", err) + } + if err := st.Claim("lic_other", "m3", 2, now); err != nil { + t.Fatalf("seats are per license: %v", err) + } + if err := st.Claim("lic", "", 2, now); !errors.Is(err, ErrSeatLimit) { + t.Fatalf("no machine id: err = %v", err) + } + if err := st.Claim("lic", "m9", 0, now); err != nil { + t.Fatalf("max 0 is unlimited: %v", err) + } + + // Seats survive a restart. + st2 := &SeatTracker{Lease: time.Hour, Path: path} + if n := st2.InUse("lic", now); n != 2 { + t.Fatalf("after reload: %d seats, want 2", n) + } + if err := st2.Claim("lic", "m3", 2, now); !errors.Is(err, ErrSeatLimit) { + t.Fatalf("after reload, third machine: %v", err) + } + // A lapsed lease frees the seat; Release frees one at once. + if err := st2.Claim("lic", "m3", 2, now.Add(2*time.Hour)); err != nil { + t.Fatalf("after the lease lapsed: %v", err) + } + st2.Release("lic", "m3") + if n := st2.InUse("lic", now.Add(2*time.Hour)); n != 0 { + t.Fatalf("after release: %d seats", n) + } +} + +func seatLicense(t *testing.T, priv ed25519.PrivateKey, id string, max int) json.RawMessage { + t.Helper() + c := license.Claims{Version: license.FormatVersion, LicenseID: id, PayloadID: "p", SigningKeyID: "v1", + Binding: license.Binding{Policy: license.BindOnline, Server: "https://k", Max: max, SecretVersion: license.SecretVersion2}} + lic, err := license.Sign(priv, c) + if err != nil { + t.Fatal(err) + } + b, _ := lic.Marshal() + return b +} + +func TestServerEnforcesSeats(t *testing.T) { + s := newServer(t) + s.Seats = &SeatTracker{} + h := s.Handler() + lic := seatLicense(t, s.SignKey, "lic_1", 2) + req := func(machine string, l json.RawMessage, id string) int { + r := online.KeyRequest{LicenseID: id, Machine: machine, Nonce: []byte("0123456789abcdef"), + Policy: "online", SecretVersion: license.SecretVersion2, License: l} + b, _ := json.Marshal(r) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/v1/key", bytes.NewReader(b))) + return rec.Code + } + if c := req("m1", lic, "lic_1"); c != http.StatusOK { + t.Fatalf("m1: %d", c) + } + if c := req("m2", lic, "lic_1"); c != http.StatusOK { + t.Fatalf("m2: %d", c) + } + if c := req("m3", lic, "lic_1"); c != http.StatusConflict { + t.Fatalf("m3: %d, want 409", c) + } + if c := req("m1", lic, "lic_1"); c != http.StatusOK { + t.Fatalf("m1 again: %d", c) + } + _, other, _ := ed25519.GenerateKey(rand.Reader) + cases := map[string]int{ + "no license": req("m4", nil, "lic_1"), + "forged license": req("m4", seatLicense(t, other, "lic_1", 100), "lic_1"), + "another license's max": req("m4", seatLicense(t, s.SignKey, "lic_2", 100), "lic_1"), + "garbage": req("m4", json.RawMessage(`{"claims":{}}`), "lic_1"), + } + for name, code := range cases { + if code == http.StatusOK { + t.Errorf("%s: accepted", name) + } + } + // Unlimited license (max 0). + if c := req("anything", seatLicense(t, s.SignKey, "lic_3", 0), "lic_3"); c != http.StatusOK { + t.Fatalf("max 0: %d", c) + } +} diff --git a/pkg/licenseserver/server.go b/pkg/licenseserver/server.go index cb81ba3..46f6527 100644 --- a/pkg/licenseserver/server.go +++ b/pkg/licenseserver/server.go @@ -7,8 +7,10 @@ package licenseserver import ( "bufio" "crypto/ed25519" + "crypto/sha256" "encoding/hex" "encoding/json" + "errors" "fmt" "net/http" "os" @@ -18,6 +20,7 @@ import ( "github.com/AI2TH/cryptship/pkg/attest" "github.com/AI2TH/cryptship/pkg/crypto" + "github.com/AI2TH/cryptship/pkg/license" "github.com/AI2TH/cryptship/pkg/online" ) @@ -33,15 +36,68 @@ type Server struct { // (one license id per line; blank lines and # comments ignored). Editing it // revokes or restores a license WITHOUT restarting the server. RevokedFile string - // Verifier, when set, must accept the request's attestation evidence for - // online.AttestationNonce(license_id, nonce) before a secret is released. - // Nil means attestation is not required: the server cannot tell an attested - // license from an online one, so a server without a Verifier releases - // attested-license secrets to anyone who asks. + // Verifier, when set, enables the attested protocol: the request must + // carry a challenge from /v1/challenge, an ephemeral key, and evidence the + // Verifier accepts for online.AttestationBinding(license_id, challenge, + // ephemeral key); the secret is returned encrypted to that key. A server + // with a Verifier refuses plain (unattested) requests. Nil means the + // attested protocol is unavailable. Verifier attest.Verifier + // ChallengeTTL is how long a challenge stays valid (default 2 minutes). + ChallengeTTL time.Duration + // NoLegacySecrets refuses requests for the legacy (secret version 0) + // derivation, which does not bind the policy: under it, a server that + // skips attestation can release an attested license's secret. Set it once + // every online/attested license in use carries binding.secret_v 2. + NoLegacySecrets bool + // Seats, when set, enforces binding.max distinct machines per license. + // The loader then has to send its signed license; it is verified against + // LicenseKeys (default: the public half of SignKey). + Seats *SeatTracker + LicenseKeys license.KeyRing mu sync.Mutex denyWarned bool // warn once if RevokedFile becomes unreadable + + chalMu sync.Mutex + spent map[[32]byte]int64 // challenge hash -> expiry (unix), for single use +} + +func (s *Server) challengeTTL() time.Duration { + if s.ChallengeTTL == 0 { + return 2 * time.Minute + } + return s.ChallengeTTL +} + +// spendChallenge checks a challenge and marks it used. Each challenge is +// accepted at most once while this process runs; its expiry bounds reuse +// across a restart. +func (s *Server) spendChallenge(licenseID string, challenge []byte) error { + key, err := online.ChallengeKey(s.Master) + if err != nil { + return err + } + now := time.Now() + if err := online.CheckChallenge(key, licenseID, challenge, now); err != nil { + return err + } + h := sha256.Sum256(challenge) + s.chalMu.Lock() + defer s.chalMu.Unlock() + if s.spent == nil { + s.spent = map[[32]byte]int64{} + } + for k, exp := range s.spent { + if exp < now.Unix() { + delete(s.spent, k) + } + } + if _, used := s.spent[h]; used { + return online.ErrBadChallenge + } + s.spent[h] = now.Add(s.challengeTTL()).Unix() + 1 + return nil } func (s *Server) ttl() time.Duration { @@ -90,14 +146,49 @@ func (s *Server) warnDenyUnreadable(err error) { } } -// Handler exposes POST /v1/key and GET /v1/check. +// Handler exposes POST /v1/key, POST /v1/challenge and GET /v1/check. func (s *Server) Handler() http.Handler { mux := http.NewServeMux() mux.HandleFunc("/v1/key", s.handleKey) + mux.HandleFunc("/v1/challenge", s.handleChallenge) mux.HandleFunc("/v1/check", s.handleCheck) return mux } +// handleChallenge issues a single-use challenge for the attested protocol. +func (s *Server) handleChallenge(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + return + } + var req online.ChallengeRequest + if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<10)).Decode(&req); err != nil || req.LicenseID == "" { + http.Error(w, "bad request", http.StatusBadRequest) + return + } + if s.Verifier == nil { + http.Error(w, "attestation not supported by this server", http.StatusForbidden) + return + } + if s.isRevoked(req.LicenseID) { + http.Error(w, "revoked", http.StatusGone) + return + } + key, err := online.ChallengeKey(s.Master) + if err != nil { + http.Error(w, "server error", http.StatusInternalServerError) + return + } + notAfter := time.Now().Add(s.challengeTTL()) + ch, err := online.NewChallenge(key, req.LicenseID, notAfter) + if err != nil { + http.Error(w, "server error", http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(online.ChallengeResponse{Challenge: ch, NotAfter: notAfter.Unix()}) +} + func (s *Server) handleKey(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) @@ -112,15 +203,30 @@ func (s *Server) handleKey(w http.ResponseWriter, r *http.Request) { http.Error(w, "revoked", http.StatusGone) return } - if s.Verifier != nil { - if _, err := s.Verifier.Verify(req.Attestation, online.AttestationNonce(req.LicenseID, req.Nonce)); err != nil { - http.Error(w, "attestation rejected", http.StatusForbidden) - return - } + if len(req.EphemeralPub) != 0 { + s.handleAttestedKey(w, req) + return + } + // A plain request (no attestation protocol) can only ever get an online + // secret. Under secret version 2 the attested secret differs, so this + // path cannot release it. + switch { + case req.SecretVersion == license.SecretVersion2 && req.Policy != string(license.BindOnline): + http.Error(w, "attested licenses need the attested protocol", http.StatusForbidden) + return + case req.SecretVersion == license.SecretVersionLegacy && (s.NoLegacySecrets || s.Verifier != nil): + // Legacy secrets do not bind the policy; a server that verifies + // attestation must not hand them out without it. + http.Error(w, "legacy secret derivation refused by this server", http.StatusForbidden) + return + } + if code, err := s.claimSeat(req, license.BindOnline); err != nil { + http.Error(w, err.Error(), code) + return } - secret, err := online.DeriveSecret(s.Master, req.LicenseID) + secret, err := online.DeriveSecretFor(s.Master, req.LicenseID, string(license.BindOnline), req.SecretVersion) if err != nil { - http.Error(w, "server error", http.StatusInternalServerError) + http.Error(w, "unsupported secret version", http.StatusBadRequest) return } defer crypto.Wipe(secret) @@ -136,6 +242,88 @@ func (s *Server) handleKey(w http.ResponseWriter, r *http.Request) { _ = json.NewEncoder(w).Encode(resp) } +// handleAttestedKey serves the attested protocol: spend the challenge, verify +// the evidence against the challenge and ephemeral key, and return the secret +// encrypted to that key. +func (s *Server) handleAttestedKey(w http.ResponseWriter, req online.KeyRequest) { + if s.Verifier == nil { + http.Error(w, "attestation not supported by this server", http.StatusForbidden) + return + } + if len(req.EphemeralPub) != online.EphemeralKeySize { + http.Error(w, "bad request", http.StatusBadRequest) + return + } + if err := s.spendChallenge(req.LicenseID, req.Nonce); err != nil { + http.Error(w, "invalid or spent challenge", http.StatusForbidden) + return + } + if _, err := s.Verifier.Verify(req.Attestation, online.AttestationBinding(req.LicenseID, req.Nonce, req.EphemeralPub)); err != nil { + http.Error(w, "attestation rejected", http.StatusForbidden) + return + } + if req.SecretVersion == license.SecretVersionLegacy && s.NoLegacySecrets { + http.Error(w, "legacy secret derivation refused by this server", http.StatusForbidden) + return + } + if code, err := s.claimSeat(req, license.BindAttested); err != nil { + http.Error(w, err.Error(), code) + return + } + secret, err := online.DeriveSecretFor(s.Master, req.LicenseID, string(license.BindAttested), req.SecretVersion) + if err != nil { + http.Error(w, "unsupported secret version", http.StatusBadRequest) + return + } + defer crypto.Wipe(secret) + serverPub, sealed, err := online.SealToEphemeral(req.EphemeralPub, req.LicenseID, req.Nonce, secret) + if err != nil { + http.Error(w, "bad request", http.StatusBadRequest) + return + } + resp := online.KeyResponse{ + LicenseID: req.LicenseID, + Nonce: req.Nonce, + NotAfter: time.Now().Add(s.ttl()).Unix(), + SigningKeyID: s.KeyID, + ServerPub: serverPub, + SealedSecret: sealed, + } + resp.Sig = ed25519.Sign(s.SignKey, online.AttestedSigningInput(resp, req.EphemeralPub)) + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(resp) +} + +// claimSeat enforces seats when s.Seats is set: it verifies the license the +// loader sent, checks it is the requested license and policy, and claims a +// seat for the reported machine. It returns the HTTP status for a refusal. +func (s *Server) claimSeat(req online.KeyRequest, policy license.BindingPolicy) (int, error) { + if s.Seats == nil { + return 0, nil + } + if len(req.License) == 0 { + return http.StatusForbidden, errors.New("seat enforcement: the request must carry the signed license") + } + lic, err := license.Parse(req.License) + if err != nil { + return http.StatusBadRequest, errors.New("seat enforcement: unreadable license") + } + keys := s.LicenseKeys + if keys == nil { + keys = license.KeyRing{"*": s.SignKey.Public().(ed25519.PublicKey)} + } + if err := lic.VerifyWith(keys); err != nil { + return http.StatusForbidden, errors.New("seat enforcement: license signature does not verify") + } + if lic.Claims.LicenseID != req.LicenseID || lic.Claims.Binding.Policy != policy { + return http.StatusForbidden, errors.New("seat enforcement: license does not match the request") + } + if err := s.Seats.Claim(req.LicenseID, req.Machine, lic.Claims.Binding.Max, time.Now()); err != nil { + return http.StatusConflict, err + } + return 0, nil +} + // handleCheck answers a revocation check. With a ?nonce= it returns a SIGNED // online.CheckResponse (200) that the loader verifies, so a captive portal or // on-path attacker cannot forge "not revoked". Without a nonce it keeps the @@ -159,13 +347,12 @@ func (s *Server) handleCheck(w http.ResponseWriter, r *http.Request) { return } resp := online.CheckResponse{ - LicenseID: id, - Nonce: nonce, - Revoked: revoked, - NotAfter: time.Now().Add(s.ttl()).Unix(), - SigningKeyID: s.KeyID, + LicenseID: id, + Nonce: nonce, + Revoked: revoked, + NotAfter: time.Now().Add(s.ttl()).Unix(), } - resp.Sig = ed25519.Sign(s.SignKey, online.CheckSigningInput(resp)) + online.SignCheck(s.SignKey, s.KeyID, &resp) w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(resp) } diff --git a/pkg/licenseserver/server_test.go b/pkg/licenseserver/server_test.go index 7ad306a..e8e5705 100644 --- a/pkg/licenseserver/server_test.go +++ b/pkg/licenseserver/server_test.go @@ -2,12 +2,14 @@ package licenseserver import ( "bytes" + "crypto/ecdh" "crypto/ed25519" "crypto/rand" "encoding/json" "net/http" "net/http/httptest" "testing" + "time" "github.com/AI2TH/cryptship/pkg/attest/attesttest" "github.com/AI2TH/cryptship/pkg/online" @@ -29,30 +31,120 @@ func post(t *testing.T, h http.Handler, req online.KeyRequest) int { return rec.Code } -func TestKeyRequiresVerifiedAttestation(t *testing.T) { +// attestedRequest runs the client side of the attested protocol against h +// up to the key request. +func attestedRequest(t *testing.T, h http.Handler, licenseID string) (online.KeyRequest, *ecdh.PrivateKey) { + t.Helper() + b, _ := json.Marshal(online.ChallengeRequest{LicenseID: licenseID}) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/v1/challenge", bytes.NewReader(b))) + if rec.Code != http.StatusOK { + t.Fatalf("challenge: status %d", rec.Code) + } + var ch online.ChallengeResponse + if err := json.Unmarshal(rec.Body.Bytes(), &ch); err != nil { + t.Fatal(err) + } + eph, _ := ecdh.X25519().GenerateKey(rand.Reader) + pub := eph.PublicKey().Bytes() + ev, _ := attesttest.Attester{}.Evidence(t.Context(), online.AttestationBinding(licenseID, ch.Challenge, pub)) + return online.KeyRequest{LicenseID: licenseID, Nonce: ch.Challenge, Attestation: ev, EphemeralPub: pub}, eph +} + +func TestAttestedProtocol(t *testing.T) { s := newServer(t) s.Verifier = attesttest.Verifier{} h := s.Handler() - nonce := []byte("0123456789abcdef") - good, _ := attesttest.Attester{}.Evidence(t.Context(), online.AttestationNonce("lic_1", nonce)) + + // Genuine exchange: 200, signed, and the secret opens only with eph. + req, eph := attestedRequest(t, h, "lic_1") + b, _ := json.Marshal(req) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/v1/key", bytes.NewReader(b))) + if rec.Code != http.StatusOK { + t.Fatalf("valid attested request: %d", rec.Code) + } + keys := map[string]ed25519.PublicKey{"v1": s.SignKey.Public().(ed25519.PublicKey)} + kr, err := online.VerifyAttestedKeyResponse(rec.Body.Bytes(), keys, "lic_1", req.Nonce, req.EphemeralPub, time.Now()) + if err != nil { + t.Fatal(err) + } + secret, err := online.OpenFromServer(eph, kr.ServerPub, "lic_1", req.Nonce, kr.SealedSecret) + if err != nil { + t.Fatal(err) + } + if want, _ := online.DeriveSecret(s.Master, "lic_1"); !bytes.Equal(secret, want) { + t.Fatal("wrong secret") + } + // The same request again: challenge spent. + if got := post(t, h, req); got != http.StatusForbidden { + t.Errorf("replayed request: %d, want 403", got) + } + + fresh := func() online.KeyRequest { r, _ := attestedRequest(t, h, "lic_1"); return r } + attackerKey, _ := ecdh.X25519().GenerateKey(rand.Reader) + swapped := fresh() + swapped.EphemeralPub = attackerKey.PublicKey().Bytes() // genuine evidence, attacker's key + noEvidence := fresh() + noEvidence.Attestation = nil + junk := fresh() + junk.Attestation = []byte("anything") + forged := fresh() + forged.Nonce = bytes.Repeat([]byte{1}, online.ChallengeSize) + otherLic := fresh() + otherLic.LicenseID = "lic_2" + badKey := fresh() + badKey.EphemeralPub = []byte("short") cases := []struct { name string req online.KeyRequest want int }{ - {"valid evidence", online.KeyRequest{LicenseID: "lic_1", Nonce: nonce, Attestation: good}, http.StatusOK}, - {"no evidence", online.KeyRequest{LicenseID: "lic_1", Nonce: nonce}, http.StatusForbidden}, - {"arbitrary evidence", online.KeyRequest{LicenseID: "lic_1", Nonce: nonce, Attestation: []byte("anything")}, http.StatusForbidden}, - {"other nonce", online.KeyRequest{LicenseID: "lic_1", Nonce: []byte("fedcba9876543210"), Attestation: good}, http.StatusForbidden}, - {"other license", online.KeyRequest{LicenseID: "lic_2", Nonce: nonce, Attestation: good}, http.StatusForbidden}, - {"revoked", online.KeyRequest{LicenseID: "lic_revoked", Nonce: nonce, Attestation: good}, http.StatusGone}, + {"evidence for another ephemeral key", swapped, http.StatusForbidden}, + {"no evidence", noEvidence, http.StatusForbidden}, + {"arbitrary evidence", junk, http.StatusForbidden}, + {"forged challenge", forged, http.StatusForbidden}, + {"challenge for another license", otherLic, http.StatusForbidden}, + {"malformed ephemeral key", badKey, http.StatusBadRequest}, + {"plain request (no attestation protocol)", online.KeyRequest{LicenseID: "lic_1", Nonce: []byte("0123456789abcdef")}, http.StatusForbidden}, } for _, tc := range cases { if got := post(t, h, tc.req); got != tc.want { t.Errorf("%s: status %d, want %d", tc.name, got, tc.want) } } + + // An expired challenge is refused. + s.ChallengeTTL = -time.Second + if got := post(t, h, fresh()); got != http.StatusForbidden { + t.Errorf("expired challenge: %d, want 403", got) + } + s.ChallengeTTL = 0 + + // Revoked: no challenge is issued. + rb, _ := json.Marshal(online.ChallengeRequest{LicenseID: "lic_revoked"}) + rr := httptest.NewRecorder() + h.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/v1/challenge", bytes.NewReader(rb))) + if rr.Code != http.StatusGone { + t.Errorf("challenge for revoked license: %d, want 410", rr.Code) + } +} + +func TestAttestedRefusedWithoutVerifier(t *testing.T) { + s := newServer(t) + h := s.Handler() + rb, _ := json.Marshal(online.ChallengeRequest{LicenseID: "lic_1"}) + rr := httptest.NewRecorder() + h.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/v1/challenge", bytes.NewReader(rb))) + if rr.Code != http.StatusForbidden { + t.Fatalf("challenge without a verifier: %d, want 403", rr.Code) + } + eph, _ := ecdh.X25519().GenerateKey(rand.Reader) + req := online.KeyRequest{LicenseID: "lic_1", Nonce: make([]byte, online.ChallengeSize), EphemeralPub: eph.PublicKey().Bytes(), Attestation: []byte("x")} + if got := post(t, h, req); got != http.StatusForbidden { + t.Fatalf("attested request without a verifier: %d, want 403", got) + } } func TestKeyWithoutVerifier(t *testing.T) { diff --git a/pkg/online/attested.go b/pkg/online/attested.go new file mode 100644 index 0000000..b4e8ebc --- /dev/null +++ b/pkg/online/attested.go @@ -0,0 +1,224 @@ +package online + +import ( + "bytes" + "crypto/ecdh" + "crypto/ed25519" + "crypto/hkdf" + "crypto/hmac" + "crypto/rand" + "crypto/sha256" + "encoding/binary" + "encoding/json" + "errors" + "fmt" + "time" + + "github.com/AI2TH/cryptship/pkg/crypto" +) + +// The attested protocol (the Trustee / KBS pattern): +// +// 1. The loader asks the server for a challenge (POST /v1/challenge). The +// challenge is single-use, short-lived and MACed by the server, so the +// server, not the client, decides freshness. +// 2. The loader makes an ephemeral X25519 key pair inside the TEE and asks +// the attester for evidence whose REPORT_DATA commits to +// AttestationBinding(license id, challenge, ephemeral public key). +// 3. The server checks the challenge, verifies the evidence against that +// binding, and returns the binding secret encrypted to the ephemeral key +// (X25519 + HKDF-SHA256 + AES-256-GCM), signed with the vendor key. +// +// Replaying a captured request fails twice over: the challenge is spent, and +// even a fresh answer is encrypted to an ephemeral key that only the TEE that +// produced the evidence holds. + +// ChallengeRequest is POSTed to /v1/challenge. +type ChallengeRequest struct { + LicenseID string `json:"license_id"` +} + +// ChallengeResponse carries an opaque, server-authenticated challenge. +type ChallengeResponse struct { + Challenge []byte `json:"challenge"` + NotAfter int64 `json:"not_after"` +} + +// EphemeralKeySize is the size of an X25519 public key. +const EphemeralKeySize = 32 + +const ( + challengeVersion = 1 + challengeRandSize = 16 + // ChallengeSize is the encoded size of a challenge token: + // version (1) || random (16) || expiry (8) || HMAC-SHA256 (32). + ChallengeSize = 1 + challengeRandSize + 8 + sha256.Size +) + +// ErrBadChallenge means a challenge is malformed, forged, for another license, +// or expired. +var ErrBadChallenge = errors.New("online: invalid or expired challenge") + +// ChallengeKey derives the server's challenge MAC key from its master secret. +func ChallengeKey(master []byte) ([]byte, error) { + return hkdf.Key(sha256.New, master, nil, "cryptship/challenge-key/v1", 32) +} + +func challengeMAC(key []byte, licenseID string, body []byte) []byte { + m := hmac.New(sha256.New, key) + m.Write([]byte("cryptship/challenge/v1\x00")) + m.Write([]byte(licenseID)) + m.Write([]byte{0}) + m.Write(body) + return m.Sum(nil) +} + +// NewChallenge returns a fresh challenge for licenseID that expires at +// notAfter. It is stateless: the server recognises its own challenges by the +// MAC, and tracks spent ones separately. +func NewChallenge(key []byte, licenseID string, notAfter time.Time) ([]byte, error) { + body := make([]byte, 1+challengeRandSize, ChallengeSize) + body[0] = challengeVersion + if _, err := rand.Read(body[1:]); err != nil { + return nil, err + } + body = binary.BigEndian.AppendUint64(body, uint64(notAfter.Unix())) + return append(body, challengeMAC(key, licenseID, body)...), nil +} + +// CheckChallenge verifies that challenge was issued by the holder of key for +// licenseID and has not expired at now. It does not track reuse. +func CheckChallenge(key []byte, licenseID string, challenge []byte, now time.Time) error { + if len(challenge) != ChallengeSize || challenge[0] != challengeVersion { + return ErrBadChallenge + } + body, mac := challenge[:ChallengeSize-sha256.Size], challenge[ChallengeSize-sha256.Size:] + if !hmac.Equal(mac, challengeMAC(key, licenseID, body)) { + return ErrBadChallenge + } + if now.Unix() > int64(binary.BigEndian.Uint64(body[1+challengeRandSize:])) { + return ErrBadChallenge + } + return nil +} + +// AttestationBinding is the value attested-mode evidence must commit to (the +// SEV-SNP / TDX REPORT_DATA field): SHA-256 over a domain label, the license +// id, the server's challenge and the loader's ephemeral public key, each +// length-prefixed. Evidence made for another challenge, license or key does +// not verify. +func AttestationBinding(licenseID string, challenge, ephemeralPub []byte) []byte { + h := sha256.New() + h.Write([]byte("cryptship/attestation-binding/v2\x00")) + for _, f := range [][]byte{[]byte(licenseID), challenge, ephemeralPub} { + var n [4]byte + binary.BigEndian.PutUint32(n[:], uint32(len(f))) + h.Write(n[:]) + h.Write(f) + } + return h.Sum(nil) +} + +const sealInfo = "cryptship/attested-secret/v1" + +func sealAAD(licenseID string, challenge []byte) []byte { + b := append([]byte(sealInfo+"\x00"), licenseID...) + return append(append(b, 0), challenge...) +} + +func sealKey(shared, clientPub, serverPub []byte) ([]byte, error) { + salt := append(append([]byte{}, clientPub...), serverPub...) + return hkdf.Key(sha256.New, shared, salt, sealInfo, crypto.KeySize) +} + +// SealToEphemeral encrypts secret to the loader's ephemeral X25519 public key. +// It returns the server's one-time public key and the AES-256-GCM ciphertext, +// whose AAD binds the license id and challenge. +func SealToEphemeral(clientPub []byte, licenseID string, challenge, secret []byte) (serverPub, sealed []byte, err error) { + peer, err := ecdh.X25519().NewPublicKey(clientPub) + if err != nil { + return nil, nil, fmt.Errorf("online: bad ephemeral key: %w", err) + } + eph, err := ecdh.X25519().GenerateKey(rand.Reader) + if err != nil { + return nil, nil, err + } + shared, err := eph.ECDH(peer) // fails for low-order points + if err != nil { + return nil, nil, fmt.Errorf("online: bad ephemeral key: %w", err) + } + defer crypto.Wipe(shared) + serverPub = eph.PublicKey().Bytes() + key, err := sealKey(shared, clientPub, serverPub) + if err != nil { + return nil, nil, err + } + defer crypto.Wipe(key) + sealed, err = crypto.Seal(key, secret, sealAAD(licenseID, challenge)) + return serverPub, sealed, err +} + +// OpenFromServer reverses SealToEphemeral with the loader's ephemeral private +// key. +func OpenFromServer(priv *ecdh.PrivateKey, serverPub []byte, licenseID string, challenge, sealed []byte) ([]byte, error) { + peer, err := ecdh.X25519().NewPublicKey(serverPub) + if err != nil { + return nil, fmt.Errorf("online: bad server key: %w", err) + } + shared, err := priv.ECDH(peer) + if err != nil { + return nil, fmt.Errorf("online: bad server key: %w", err) + } + defer crypto.Wipe(shared) + key, err := sealKey(shared, priv.PublicKey().Bytes(), serverPub) + if err != nil { + return nil, err + } + defer crypto.Wipe(key) + secret, err := crypto.Open(key, sealed, sealAAD(licenseID, challenge)) + if err != nil { + return nil, errors.New("online: sealed secret does not open") + } + return secret, nil +} + +// AttestedSigningInput is the byte layout the server signs for an attested +// key answer. Every variable-length field is length-prefixed; like the other +// signing inputs it binds signing_key_id. +func AttestedSigningInput(r KeyResponse, clientPub []byte) []byte { + b := []byte("cryptship/attested-resp/v1\x00") + for _, f := range [][]byte{[]byte(r.LicenseID), []byte(r.SigningKeyID), r.Nonce, clientPub, r.ServerPub, r.SealedSecret} { + b = binary.BigEndian.AppendUint32(b, uint32(len(f))) + b = append(b, f...) + } + return binary.BigEndian.AppendUint64(b, uint64(r.NotAfter)) +} + +// VerifyAttestedKeyResponse decodes an attested key answer and accepts it only +// if it is signed by a key in keys over this license, challenge and ephemeral +// key, carries no plaintext secret, and has not expired at now. The caller +// then opens SealedSecret with OpenFromServer. +func VerifyAttestedKeyResponse(body []byte, keys map[string]ed25519.PublicKey, licenseID string, challenge, clientPub []byte, now time.Time) (KeyResponse, error) { + var r KeyResponse + if err := json.Unmarshal(body, &r); err != nil { + return KeyResponse{}, fmt.Errorf("online: bad response: %w", err) + } + if len(r.Sig) == 0 { + return KeyResponse{}, ErrUnsigned + } + pub, err := SigningKey(keys, r.SigningKeyID) + if err != nil { + return KeyResponse{}, err + } + if !ed25519.Verify(pub, AttestedSigningInput(r, clientPub), r.Sig) { + return KeyResponse{}, ErrBadSignature + } + if r.LicenseID != licenseID || len(challenge) == 0 || !bytes.Equal(r.Nonce, challenge) || + len(r.Secret) != 0 || len(r.ServerPub) != EphemeralKeySize || len(r.SealedSecret) == 0 { + return KeyResponse{}, ErrMismatch + } + if r.NotAfter != 0 && now.Unix() > r.NotAfter { + return KeyResponse{}, ErrExpired + } + return r, nil +} diff --git a/pkg/online/attested_test.go b/pkg/online/attested_test.go new file mode 100644 index 0000000..8579cff --- /dev/null +++ b/pkg/online/attested_test.go @@ -0,0 +1,142 @@ +package online + +import ( + "bytes" + "crypto/ecdh" + "crypto/ed25519" + "crypto/rand" + "encoding/json" + "errors" + "testing" + "time" +) + +func TestChallenge(t *testing.T) { + key, err := ChallengeKey([]byte("master-secret-master-secret-1234")) + if err != nil { + t.Fatal(err) + } + now := time.Now() + ch, err := NewChallenge(key, "lic", now.Add(time.Minute)) + if err != nil { + t.Fatal(err) + } + if len(ch) != ChallengeSize { + t.Fatalf("size %d", len(ch)) + } + if err := CheckChallenge(key, "lic", ch, now); err != nil { + t.Fatalf("fresh challenge: %v", err) + } + other, _ := ChallengeKey([]byte("another-master-another-master-12")) + tampered := bytes.Clone(ch) + tampered[5] ^= 1 + extended := bytes.Clone(ch) + extended[1+challengeRandSize] ^= 0x40 // push the expiry out + ch2, _ := NewChallenge(key, "lic", now.Add(time.Minute)) + cases := map[string]error{ + "other license": CheckChallenge(key, "lic_other", ch, now), + "other server": CheckChallenge(other, "lic", ch, now), + "tampered": CheckChallenge(key, "lic", tampered, now), + "extended": CheckChallenge(key, "lic", extended, now), + "expired": CheckChallenge(key, "lic", ch, now.Add(2*time.Minute)), + "truncated": CheckChallenge(key, "lic", ch[:10], now), + "empty": CheckChallenge(key, "lic", nil, now), + } + for name, err := range cases { + if !errors.Is(err, ErrBadChallenge) { + t.Errorf("%s: err = %v", name, err) + } + } + if bytes.Equal(ch, ch2) { + t.Fatal("two challenges are equal") + } +} + +func TestAttestationBindingIsUnambiguous(t *testing.T) { + pub := make([]byte, 32) + a := AttestationBinding("ab", []byte("c"), pub) + b := AttestationBinding("a", []byte("bc"), pub) + c := AttestationBinding("ab", []byte("c"), append(make([]byte, 31), 1)) + if bytes.Equal(a, b) || bytes.Equal(a, c) { + t.Fatal("binding collides across fields") + } + if len(a) != 32 { + t.Fatalf("binding is %d bytes; REPORT_DATA holds 64", len(a)) + } +} + +func TestSealToEphemeral(t *testing.T) { + client, _ := ecdh.X25519().GenerateKey(rand.Reader) + secret := bytes.Repeat([]byte{7}, SecretSize) + ch := []byte("challenge") + serverPub, sealed, err := SealToEphemeral(client.PublicKey().Bytes(), "lic", ch, secret) + if err != nil { + t.Fatal(err) + } + if bytes.Contains(sealed, secret) { + t.Fatal("secret in clear") + } + got, err := OpenFromServer(client, serverPub, "lic", ch, sealed) + if err != nil || !bytes.Equal(got, secret) { + t.Fatalf("open: %v", err) + } + // An eavesdropper with its own key, or the right key in another context, + // cannot open it. + eve, _ := ecdh.X25519().GenerateKey(rand.Reader) + if _, err := OpenFromServer(eve, serverPub, "lic", ch, sealed); err == nil { + t.Fatal("opened with another private key") + } + if _, err := OpenFromServer(client, serverPub, "lic_other", ch, sealed); err == nil { + t.Fatal("opened for another license") + } + if _, err := OpenFromServer(client, serverPub, "lic", []byte("other"), sealed); err == nil { + t.Fatal("opened for another challenge") + } + // Low-order / all-zero keys are refused. + if _, _, err := SealToEphemeral(make([]byte, 32), "lic", ch, secret); err == nil { + t.Fatal("sealed to the all-zero point") + } + if _, _, err := SealToEphemeral([]byte("short"), "lic", ch, secret); err == nil { + t.Fatal("sealed to a short key") + } +} + +func TestVerifyAttestedKeyResponse(t *testing.T) { + pub, priv, _ := ed25519.GenerateKey(rand.Reader) + keys := map[string]ed25519.PublicKey{"v1": pub} + client, _ := ecdh.X25519().GenerateKey(rand.Reader) + cpub := client.PublicKey().Bytes() + ch := []byte("challenge-bytes") + sp, sealed, _ := SealToEphemeral(cpub, "lic", ch, make([]byte, SecretSize)) + now := time.Now() + sign := func(r KeyResponse, forPub []byte) []byte { + r.SigningKeyID = "v1" + r.Sig = ed25519.Sign(priv, AttestedSigningInput(r, forPub)) + b, _ := json.Marshal(r) + return b + } + good := KeyResponse{LicenseID: "lic", Nonce: ch, NotAfter: now.Add(time.Hour).Unix(), ServerPub: sp, SealedSecret: sealed} + if _, err := VerifyAttestedKeyResponse(sign(good, cpub), keys, "lic", ch, cpub, now); err != nil { + t.Fatalf("good answer: %v", err) + } + leaky := good + leaky.Secret = make([]byte, SecretSize) + other, _ := ecdh.X25519().GenerateKey(rand.Reader) + cases := []struct { + name string + body []byte + want error + }{ + {"signed for another ephemeral key", sign(good, other.PublicKey().Bytes()), ErrBadSignature}, + {"plaintext secret", sign(leaky, cpub), ErrMismatch}, + {"unsigned", []byte(`{}`), ErrUnsigned}, + } + for _, tc := range cases { + if _, err := VerifyAttestedKeyResponse(tc.body, keys, "lic", ch, cpub, now); !errors.Is(err, tc.want) { + t.Errorf("%s: err = %v, want %v", tc.name, err, tc.want) + } + } + if _, err := VerifyAttestedKeyResponse(sign(good, cpub), keys, "lic", []byte("other"), cpub, now); !errors.Is(err, ErrMismatch) { + t.Errorf("other challenge: err = %v", err) + } +} diff --git a/pkg/online/fuzz_test.go b/pkg/online/fuzz_test.go new file mode 100644 index 0000000..a883184 --- /dev/null +++ b/pkg/online/fuzz_test.go @@ -0,0 +1,98 @@ +package online + +import ( + "bytes" + "crypto/ecdh" + "crypto/ed25519" + "crypto/rand" + "encoding/json" + "testing" + "time" +) + +var ( + fuzzPub, fuzzPriv, _ = ed25519.GenerateKey(rand.Reader) + fuzzKeys = map[string]ed25519.PublicKey{"v1": fuzzPub} + fuzzNonce = []byte("0123456789abcdef") +) + +// FuzzVerifyKeyResponse: the online answer decoder never panics, and anything +// it accepts answers this license and nonce with a well-formed secret. +func FuzzVerifyKeyResponse(f *testing.F) { + r := KeyResponse{LicenseID: "lic", Nonce: fuzzNonce, Secret: make([]byte, SecretSize), NotAfter: time.Now().Add(time.Hour).Unix(), SigningKeyID: "v1"} + r.Sig = ed25519.Sign(fuzzPriv, ResponseSigningInput(r)) + good, _ := json.Marshal(r) + f.Add(good) + f.Add([]byte(`{"license_id":"lic","nonce":"MDEyMzQ1Njc4OWFiY2RlZg==","secret":"","sig":"AA=="}`)) + f.Add([]byte(`null`)) + f.Fuzz(func(t *testing.T, body []byte) { + got, err := VerifyKeyResponse(body, fuzzKeys, "lic", fuzzNonce, time.Now()) + if err != nil { + return + } + if got.LicenseID != "lic" || !bytes.Equal(got.Nonce, fuzzNonce) || len(got.Secret) != SecretSize || + !ed25519.Verify(fuzzPub, ResponseSigningInput(got), got.Sig) { + t.Fatalf("accepted a non-matching answer: %+v", got) + } + }) +} + +// FuzzVerifyCheckResponse: same for revocation answers. +func FuzzVerifyCheckResponse(f *testing.F) { + r := CheckResponse{LicenseID: "lic", Nonce: fuzzNonce, NotAfter: time.Now().Add(time.Hour).Unix()} + SignCheck(fuzzPriv, "v1", &r) + good, _ := json.Marshal(r) + f.Add(good) + f.Add([]byte(`OK`)) + f.Fuzz(func(t *testing.T, body []byte) { + got, err := VerifyCheckResponse(body, fuzzKeys, "lic", fuzzNonce, time.Now()) + if err != nil { + return + } + if got.LicenseID != "lic" || !bytes.Equal(got.Nonce, fuzzNonce) || !ed25519.Verify(fuzzPub, CheckSigningInput(got), got.Sig) { + t.Fatalf("accepted a non-matching answer: %+v", got) + } + }) +} + +// FuzzVerifyAttestedKeyResponse: the attested decoder never panics, and what +// it accepts never carries a plaintext secret. +func FuzzVerifyAttestedKeyResponse(f *testing.F) { + client, _ := ecdh.X25519().GenerateKey(rand.Reader) + cpub := client.PublicKey().Bytes() + sp, sealed, _ := SealToEphemeral(cpub, "lic", fuzzNonce, make([]byte, SecretSize)) + r := KeyResponse{LicenseID: "lic", Nonce: fuzzNonce, NotAfter: time.Now().Add(time.Hour).Unix(), SigningKeyID: "v1", ServerPub: sp, SealedSecret: sealed} + r.Sig = ed25519.Sign(fuzzPriv, AttestedSigningInput(r, cpub)) + good, _ := json.Marshal(r) + f.Add(good) + f.Fuzz(func(t *testing.T, body []byte) { + got, err := VerifyAttestedKeyResponse(body, fuzzKeys, "lic", fuzzNonce, cpub, time.Now()) + if err != nil { + return + } + if len(got.Secret) != 0 || len(got.ServerPub) != EphemeralKeySize || !ed25519.Verify(fuzzPub, AttestedSigningInput(got, cpub), got.Sig) { + t.Fatalf("accepted a malformed attested answer: %+v", got) + } + _, _ = OpenFromServer(client, got.ServerPub, "lic", fuzzNonce, got.SealedSecret) + }) +} + +// FuzzCheckChallenge: arbitrary bytes do not crash the check, and a +// challenge that passes is bound to its license and server key: it never also +// passes for another license or under another server's key. +func FuzzCheckChallenge(f *testing.F) { + key, _ := ChallengeKey([]byte("fuzz-master-fuzz-master-fuzz-mas")) + other, _ := ChallengeKey([]byte("other-master-other-master-other-")) + ch, _ := NewChallenge(key, "lic", time.Now().Add(time.Hour)) + f.Add(ch) + f.Add(make([]byte, ChallengeSize)) + f.Fuzz(func(t *testing.T, c []byte) { + now := time.Now() + if CheckChallenge(key, "lic", c, now) != nil { + return + } + if CheckChallenge(key, "lic2", c, now) == nil || CheckChallenge(other, "lic", c, now) == nil { + t.Fatalf("challenge not bound to its license and key: %x", c) + } + }) +} diff --git a/pkg/online/protocol.go b/pkg/online/protocol.go index 892efe6..abc4f4f 100644 --- a/pkg/online/protocol.go +++ b/pkg/online/protocol.go @@ -6,6 +6,8 @@ import ( "crypto/hkdf" "crypto/sha256" "encoding/binary" + "encoding/json" + "errors" ) // SecretSize is the length of the per-license binding secret. @@ -15,41 +17,71 @@ const SecretSize = 32 type KeyRequest struct { LicenseID string `json:"license_id"` Machine string `json:"machine,omitempty"` // best-effort fingerprint, for seats - Nonce []byte `json:"nonce"` // 16 bytes, anti-replay + // Nonce is 16 random bytes for online; for attested it is the server's + // challenge from /v1/challenge. + Nonce []byte `json:"nonce"` // Attestation carries TEE evidence for the attested flow (empty otherwise). + // It must commit to AttestationBinding(license_id, nonce, ephemeral_pub). Attestation []byte `json:"attestation,omitempty"` + // EphemeralPub is the loader's one-time X25519 public key (attested only); + // the server encrypts the secret to it. + EphemeralPub []byte `json:"ephemeral_pub,omitempty"` + // Policy and SecretVersion come from the license (binding.policy, + // binding.secret_v) and select the server-side derivation. They are not + // trusted: asking for the wrong one only yields a secret that does not + // unwrap the license. + Policy string `json:"policy,omitempty"` + SecretVersion int `json:"secret_v,omitempty"` + // License is the signed license, for servers that enforce binding.max + // machines per license (seats). + License json.RawMessage `json:"license,omitempty"` } // KeyResponse is returned by the server. Sig covers ResponseSigningInput so a // TLS-terminating proxy cannot forge or replay it. type KeyResponse struct { - LicenseID string `json:"license_id"` - Nonce []byte `json:"nonce"` - Secret []byte `json:"secret"` // SecretSize bytes of binding material - NotAfter int64 `json:"not_after"` + LicenseID string `json:"license_id"` + Nonce []byte `json:"nonce"` + Secret []byte `json:"secret,omitempty"` // SecretSize bytes of binding material (online) + NotAfter int64 `json:"not_after"` + // Attested answers carry the secret only encrypted to the loader's + // ephemeral key (see SealToEphemeral), never in Secret. + ServerPub []byte `json:"server_pub,omitempty"` + SealedSecret []byte `json:"sealed_secret,omitempty"` SigningKeyID string `json:"signing_key_id"` Sig []byte `json:"sig"` } -// DeriveSecret recomputes the per-license binding secret from a master secret. -// The issuer and the server run this identically; nothing secret is stored in -// the license itself. +// DeriveSecret is the legacy (secret version 0) derivation of the per-license +// binding secret: HKDF over the master secret and license id only. The server +// cannot tell an online license from an attested one under it, so a server +// that skips attestation can release attested secrets. Kept so licenses issued +// before secret version 2 keep working; new licenses use DeriveSecretFor. func DeriveSecret(master []byte, licenseID string) ([]byte, error) { return hkdf.Key(sha256.New, master, []byte(licenseID), "cryptship/online-secret/v1", SecretSize) } -// AttestationNonce is the value attested-mode evidence must commit to (for -// example in the SEV-SNP / TDX REPORT_DATA field): a hash of the license id and -// the request nonce, so evidence made for one request or license does not -// verify for another. The loader's attester and the server's verifier both -// compute it. -func AttestationNonce(licenseID string, nonce []byte) []byte { - h := sha256.New() - h.Write([]byte("cryptship/attestation-nonce/v1\x00")) - h.Write([]byte(licenseID)) - h.Write([]byte{0}) - h.Write(nonce) - return h.Sum(nil) +// ErrSecretVersion is returned for an unknown secret version or policy. +var ErrSecretVersion = errors.New("online: unsupported secret version or policy") + +// DeriveSecretFor derives the binding secret for a license id, binding policy +// ("online" or "attested") and secret version. Version 2 binds the policy into +// the HKDF info, so the online secret and the attested secret of a license +// differ and an online-only server cannot produce attested secrets. Version 0 +// is DeriveSecret, whatever the policy. The issuer and the server run this +// identically; nothing secret is stored in the license. +func DeriveSecretFor(master []byte, licenseID, policy string, version int) ([]byte, error) { + switch version { + case 0: + return DeriveSecret(master, licenseID) + case 2: + if policy != "online" && policy != "attested" { + return nil, ErrSecretVersion + } + return hkdf.Key(sha256.New, master, nil, "cryptship/server-secret/v2\x00"+policy+"\x00"+licenseID, SecretSize) + default: + return nil, ErrSecretVersion + } } // CheckResponse is the signed answer to a revocation check (GET /v1/check with a diff --git a/pkg/online/protocol_test.go b/pkg/online/protocol_test.go new file mode 100644 index 0000000..bcef8b4 --- /dev/null +++ b/pkg/online/protocol_test.go @@ -0,0 +1,38 @@ +package online + +import ( + "bytes" + "encoding/hex" + "errors" + "testing" +) + +func TestDeriveSecretFor(t *testing.T) { + master := bytes.Repeat([]byte{9}, 32) + legacy, _ := DeriveSecret(master, "lic") + for _, p := range []string{"online", "attested", ""} { + if got, err := DeriveSecretFor(master, "lic", p, 0); err != nil || !bytes.Equal(got, legacy) { + t.Fatalf("version 0 must equal the legacy derivation (policy %q): %v", p, err) + } + } + on, err := DeriveSecretFor(master, "lic", "online", 2) + if err != nil { + t.Fatal(err) + } + at, _ := DeriveSecretFor(master, "lic", "attested", 2) + other, _ := DeriveSecretFor(master, "lic2", "online", 2) + if bytes.Equal(on, at) || bytes.Equal(on, legacy) || bytes.Equal(at, legacy) || bytes.Equal(on, other) { + t.Fatal("v2 secrets must differ by policy, license and from the legacy secret") + } + if _, err := DeriveSecretFor(master, "lic", "strict", 2); !errors.Is(err, ErrSecretVersion) { + t.Fatalf("v2 strict: %v", err) + } + if _, err := DeriveSecretFor(master, "lic", "online", 1); !errors.Is(err, ErrSecretVersion) { + t.Fatalf("version 1: %v", err) + } + // Golden vector, cross-checked with an independent HKDF (Python hmac): + // changing the v2 derivation strands every v2 license. + if got := hex.EncodeToString(on); got != "9282a9fef672b608e50e3bc0e4f301febad1373b6df28d1c3870ae44f7cf9338" { + t.Fatalf("v2 derivation changed: %s", got) + } +} diff --git a/pkg/online/verify.go b/pkg/online/verify.go new file mode 100644 index 0000000..e940eef --- /dev/null +++ b/pkg/online/verify.go @@ -0,0 +1,102 @@ +package online + +import ( + "bytes" + "crypto/ed25519" + "encoding/json" + "errors" + "fmt" + "time" +) + +// MaxResponseBytes caps how much of a server response the loader reads. +const MaxResponseBytes = 64 << 10 + +var ( + // ErrUnsigned means the body is not a signed response at all (not JSON, or + // no signature). Callers decide whether an unsigned answer is acceptable. + ErrUnsigned = errors.New("online: response is not signed") + // ErrBadSignature means the response is signed by an unknown key or the + // signature does not verify. + ErrBadSignature = errors.New("online: bad response signature") + // ErrMismatch means a correctly signed response answers a different + // license or nonce (for example a replayed answer). + ErrMismatch = errors.New("online: response does not match the request") + // ErrExpired means the response's not_after has passed. + ErrExpired = errors.New("online: response already expired") +) + +// SigningKey looks up the public key for a signing key id in keys, falling +// back to the wildcard id "*" (license.KeyRing has this shape). +func SigningKey(keys map[string]ed25519.PublicKey, id string) (ed25519.PublicKey, error) { + pub, ok := keys[id] + if !ok { + pub, ok = keys["*"] + } + if !ok || len(pub) != ed25519.PublicKeySize { + return nil, fmt.Errorf("%w: unknown signing key %q", ErrBadSignature, id) + } + return pub, nil +} + +// SignCheck fills in SigningKeyID and Sig for a revocation answer. The server +// signs check answers with the same vendor key and the same envelope (license +// id, nonce, not_after, key id, detached Ed25519 signature) as key answers; +// the signing inputs carry different domain labels, so one can never be +// passed off as the other. +func SignCheck(priv ed25519.PrivateKey, keyID string, r *CheckResponse) { + r.SigningKeyID = keyID + r.Sig = ed25519.Sign(priv, CheckSigningInput(*r)) +} + +// VerifyCheckResponse decodes a revocation answer and accepts it only if it is +// signed by a key in keys, answers licenseID and the nonce the caller sent, and +// has not expired at now. A body that is not a signed response returns +// ErrUnsigned so the caller can apply its policy for unsigned endpoints. +func VerifyCheckResponse(body []byte, keys map[string]ed25519.PublicKey, licenseID string, nonce []byte, now time.Time) (CheckResponse, error) { + var r CheckResponse + if err := json.Unmarshal(body, &r); err != nil || len(r.Sig) == 0 { + return CheckResponse{}, ErrUnsigned + } + pub, err := SigningKey(keys, r.SigningKeyID) + if err != nil { + return CheckResponse{}, err + } + if !ed25519.Verify(pub, CheckSigningInput(r), r.Sig) { + return CheckResponse{}, ErrBadSignature + } + if r.LicenseID != licenseID || len(nonce) == 0 || !bytes.Equal(r.Nonce, nonce) { + return CheckResponse{}, ErrMismatch + } + if r.NotAfter != 0 && now.Unix() > r.NotAfter { + return CheckResponse{}, ErrExpired + } + return r, nil +} + +// VerifyKeyResponse decodes a key answer and accepts it only if it is signed by +// a key in keys, answers licenseID and nonce, carries a secret of the expected +// shape, and has not expired at now. +func VerifyKeyResponse(body []byte, keys map[string]ed25519.PublicKey, licenseID string, nonce []byte, now time.Time) (KeyResponse, error) { + var r KeyResponse + if err := json.Unmarshal(body, &r); err != nil { + return KeyResponse{}, fmt.Errorf("online: bad response: %w", err) + } + if len(r.Sig) == 0 { + return KeyResponse{}, ErrUnsigned + } + pub, err := SigningKey(keys, r.SigningKeyID) + if err != nil { + return KeyResponse{}, err + } + if !ed25519.Verify(pub, ResponseSigningInput(r), r.Sig) { + return KeyResponse{}, ErrBadSignature + } + if r.LicenseID != licenseID || len(nonce) == 0 || !bytes.Equal(r.Nonce, nonce) || len(r.Secret) != SecretSize { + return KeyResponse{}, ErrMismatch + } + if r.NotAfter != 0 && now.Unix() > r.NotAfter { + return KeyResponse{}, ErrExpired + } + return r, nil +} diff --git a/pkg/online/verify_test.go b/pkg/online/verify_test.go new file mode 100644 index 0000000..05bba7f --- /dev/null +++ b/pkg/online/verify_test.go @@ -0,0 +1,75 @@ +package online + +import ( + "crypto/ed25519" + "crypto/rand" + "encoding/json" + "errors" + "testing" + "time" +) + +func TestVerifyCheckResponse(t *testing.T) { + pub, priv, _ := ed25519.GenerateKey(rand.Reader) + keys := map[string]ed25519.PublicKey{"v1": pub} + nonce := []byte("0123456789abcdef") + now := time.Now() + sign := func(r CheckResponse) []byte { + SignCheck(priv, "v1", &r) + b, _ := json.Marshal(r) + return b + } + good := CheckResponse{LicenseID: "lic", Nonce: nonce, NotAfter: now.Add(time.Hour).Unix()} + + if r, err := VerifyCheckResponse(sign(good), keys, "lic", nonce, now); err != nil || r.Revoked { + t.Fatalf("good answer: %+v %v", r, err) + } + revoked := good + revoked.Revoked = true + if r, err := VerifyCheckResponse(sign(revoked), keys, "lic", nonce, now); err != nil || !r.Revoked { + t.Fatalf("revoked answer: %+v %v", r, err) + } + + tampered := good + SignCheck(priv, "v1", &tampered) + tampered.Revoked = true + tb, _ := json.Marshal(tampered) + expired := good + expired.NotAfter = now.Add(-time.Minute).Unix() + + cases := []struct { + name string + body []byte + lic string + want error + }{ + {"not json", []byte("OK"), "lic", ErrUnsigned}, + {"no signature", []byte(`{"license_id":"lic"}`), "lic", ErrUnsigned}, + {"tampered", tb, "lic", ErrBadSignature}, + {"other license", sign(good), "lic_other", ErrMismatch}, + {"expired", sign(expired), "lic", ErrExpired}, + } + for _, tc := range cases { + if _, err := VerifyCheckResponse(tc.body, keys, tc.lic, nonce, now); !errors.Is(err, tc.want) { + t.Errorf("%s: err = %v, want %v", tc.name, err, tc.want) + } + } + if _, err := VerifyCheckResponse(sign(good), keys, "lic", []byte("fedcba9876543210"), now); !errors.Is(err, ErrMismatch) { + t.Errorf("replayed nonce: err = %v", err) + } + if _, err := VerifyCheckResponse(sign(good), map[string]ed25519.PublicKey{"v2": pub}, "lic", nonce, now); !errors.Is(err, ErrBadSignature) { + t.Errorf("unknown key id: err = %v", err) + } +} + +// A key answer's signature cannot be presented as a check answer. +func TestCheckAndKeySignaturesAreDomainSeparated(t *testing.T) { + pub, priv, _ := ed25519.GenerateKey(rand.Reader) + kr := KeyResponse{LicenseID: "lic", Nonce: []byte("0123456789abcdef"), NotAfter: time.Now().Add(time.Hour).Unix(), SigningKeyID: "v1"} + kr.Sig = ed25519.Sign(priv, ResponseSigningInput(kr)) + cr := CheckResponse{LicenseID: kr.LicenseID, Nonce: kr.Nonce, NotAfter: kr.NotAfter, SigningKeyID: "v1", Sig: kr.Sig} + b, _ := json.Marshal(cr) + if _, err := VerifyCheckResponse(b, map[string]ed25519.PublicKey{"v1": pub}, "lic", kr.Nonce, time.Now()); !errors.Is(err, ErrBadSignature) { + t.Fatalf("key-answer signature accepted on a check answer: %v", err) + } +} diff --git a/scripts/check-mermaid.sh b/scripts/check-mermaid.sh index 63c0be9..50d1719 100644 --- a/scripts/check-mermaid.sh +++ b/scripts/check-mermaid.sh @@ -13,6 +13,12 @@ IMG="ghcr.io/mermaid-js/mermaid-cli/mermaid-cli@sha256:18938e53497376d524e04b1fc # /var/folders and /tmp are not shared into the VM by default. tmp="$(mktemp -d "$ROOT/.mermaid-check.XXXXXX")" trap 'rm -rf "$tmp"' EXIT +# mktemp -d makes the dir 0700. On Linux a bind mount keeps that, so the +# mermaid-cli container user (uid 1001, not ours) could not read the blocks. +# Open it for reading only: the container mounts it read-only and writes the +# SVG to stdout. (Docker Desktop and colima on macOS remap ownership, which is +# why this only failed on Linux.) +chmod 0755 "$tmp" python3 - "$ROOT" "$tmp" <<'PY' import sys, re, pathlib @@ -23,7 +29,9 @@ for md in sorted(root.rglob("*.md")): continue for blk in re.findall(r"```mermaid\n(.*?)```", md.read_text(), re.S): i += 1 - (tmp / f"{i:03d}.mmd").write_text(blk) + p = tmp / f"{i:03d}.mmd" + p.write_text(blk) + p.chmod(0o644) print(f"extracted {i} mermaid block(s)") PY @@ -38,7 +46,7 @@ render() { # render if command -v mmdc >/dev/null 2>&1; then mmdc -q -p "$tmp/pp.json" -i "$1" -o "$2" else - docker run --rm -v "$tmp":/data "$IMG" -i "/data/$(basename "$1")" -o "/data/$(basename "$2")" + docker run --rm -v "$tmp":/data:ro "$IMG" -i "/data/$(basename "$1")" -o - -e svg > "$2" fi } diff --git a/scripts/demo.sh b/scripts/demo.sh index 06b700c..ce6614c 100644 --- a/scripts/demo.sh +++ b/scripts/demo.sh @@ -10,6 +10,8 @@ # every layer, grep for a marker planted in the app's source) # 5. RUN it (decrypts into tmpfs, app works) # 6. NEGATIVE: tamper the license -> loader refuses to run +# 7. HARDENED: loader starts as root, extracts as another owner, drops to +# the app user, which cannot modify its code set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" @@ -71,5 +73,21 @@ fi echo " loader refused it:" sed 's/^/ > /' "$WORK/err.txt" +echo +echo "== 7. HARDENED: start as root, extract as another owner, drop to 65532 ==" +docker run --rm --user 0:0 -e CRYPTSHIP_APP_USER=65532:65532 \ + --read-only --tmpfs /run/cryptship:rw,mode=0755 \ + --cap-drop=ALL --cap-add=CHOWN --cap-add=SETUID --cap-add=SETGID \ + --security-opt=no-new-privileges --security-opt seccomp="$ROOT/deploy/seccomp.json" \ + -e CRYPTSHIP_REQUIRE_TMPFS=1 \ + --entrypoint /cryptship-loader cryptship-demo -- sh -c ' + echo " app runs as uid $(id -u), gid $(id -g)" + python app.py | sed "s/^/ /" + if (echo x >> app.py) 2>/dev/null || (touch planted.py) 2>/dev/null; then + echo "!! the app could modify its own code"; exit 1 + fi + echo " the app cannot modify its code: app.py is $(stat -c "%u:%g %a" app.py)"' \ + 2> >(grep -v "clock-rollback check disabled" >&2) + echo echo "Demo complete."