# Architecture Carapace has one job: let an AI agent *use* a secret without ever *holding* it, with the guarantee enforced by hardware rather than by trusting the operator. ## Components ``` agent / SDK ──attested TLS (cert pinned via eat_nonce)──▶ ENCLAVE (CVM :8443) CLI / web ──encrypt locally to KMS public key──────────▶ SERVER (untrusted) ENCLAVE ──pulls ciphertext, auth = attestation JWT───▶ SERVER ENCLAVE ──WIF principalSet(image_digest)─────────────▶ Cloud KMS (HSM) ``` | Component | Trust | Role | |---|---|---| | **Enclave** | Trusted (attested) | Decrypts secrets, performs egress requests with the credential injected, redacts responses, signs receipts | | **Server** | Untrusted | Accounts, API keys, ciphertext and policy storage, receipt storage. Never sees plaintext. Not in the request data path. | | **CLI / SDK / web** | User's device | Verifies attestation, encrypts secrets locally, sends agent requests to the enclave | | **Cloud KMS** | Google TCB | Holds the asymmetric decryption key; releases decryption only to the attested workload | ## Key management - One Cloud KMS key: `ASYMMETRIC_DECRYPT`, `RSA_DECRYPT_OAEP_4096_SHA256`, protection level `HSM`. - `roles/cloudkms.cryptoKeyDecrypter` is granted **only** to a Workload Identity Federation `principalSet` keyed on the enclave image digest. The WIF provider condition requires the stack's STS audience, `hwmodel == GCP_AMD_SEV`, `swname == CONFIDENTIAL_SPACE`, `dbgstat == disabled-since-boot`, `secboot == true`, a `STABLE` launcher image, an allowed image digest, this project, and the enclave service account. - `GCP_AMD_SEV` means AMD SEV memory encryption, not SEV-SNP. The token is rooted in the VM's vTPM measured boot, which Google attests. AMD SEV, Google's vTPM and Shielded VM firmware, and the Confidential Space image are therefore in the TCB. SEV-SNP is an open question until Google documents its `hwmodel` claim. - The same `principalSet` also holds `roles/cloudkms.publicKeyViewer`: the enclave's boot self-test reads the public key with the same federated credentials it decrypts with. - The key ring's IAM policy is authoritative and empty. - The server's service account holds only `roles/cloudkms.publicKeyViewer`. - No service account attached to the VM has KMS permissions, and the enclave has no fallback to ambient credentials. ## Envelope format (v1) ``` header = {v: 1, secret_id, owner_id, owner_pk, version, policy} aad = sha256(canonical_json(header)) dek = random(32) ct = AES-256-GCM(dek, nonce, plaintext, aad) wrapped = RSA-OAEP-SHA256(kms_public_key, dek) body = header + {kms_key_version, wrapped, nonce, ct} sig = Ed25519(owner_key, "carapace-envelope-v1" || 0x0A || canonical_json(body)) stored = body + {sig} ``` The policy is stored in cleartext because the enclave needs to read it, but the enclave recomputes the AAD from the stored policy. An operator who widens a secret's allowlist makes decryption fail. `owner_pk` is the user's Ed25519 owner key, generated by the CLI and never sent to the server, and `sig` covers the whole stored object. The enclave learns which owner key to expect from the agent's API key (`cpk__`), not from the store, and verifies the signature before it calls KMS. An operator therefore cannot substitute an envelope of their own under a victim's ids either. `version` increases on every re-seal and is floored by the agent's grant, which bounds rollback. ## Authorization API keys are minted by the CLI, not the server. The server stores only a lookup hash of each key. What a key may use is an owner-signed *grant* (`{owner_pk, key_bind, secrets: {secret_id: min_version}, iat, exp}`) that the enclave fetches by lookup hash and verifies against the key the agent presented: the grant's owner key must match the fingerprint in the key, the signature must verify, and the grant's `key_bind` (a hash the server cannot derive from the lookup hash) must match. Grants expire within 90 days; to extend one, the owner revokes the key and creates a new one (v0.1 has no `key renew`, see the CLI README). Full formats, the enclave verification order and the freshness semantics are in [`docs/design/owner-signing.md`](design/owner-signing.md). ## Network The enclave serves HTTPS on **port 8443**, and clients use `https://:8443`. The image runs as a non-root user (UID 65532) with no ambient capabilities, so it cannot bind a port below 1024. Granting `CAP_NET_BIND_SERVICE` is not an option either: the distroless image has no shell to set file capabilities, and letting the operator add capabilities (`tee.launch_policy.allow_capabilities`) would let them add any capability. The Confidential Space launcher opens only the ports the image `EXPOSE`s, to the same port number, and a VPC firewall rule cannot translate ports, so the listen port, the `EXPOSE`, the firewall rule and the `enclave_url` output all use the same number. Tests on both sides check that they agree. ## Attestation and client verification At boot the enclave generates a TLS key pair and an Ed25519 receipt key, then requests a Confidential Space token with `eat_nonce = sha256(tls_spki_der || receipt_pubkey)`. `GET /attestation` returns the token, TLS certificate, receipt public key, and KMS public key. Tokens use two audiences, and WIF accepts only one of them: - The **client token** served by `/attestation` has audience `carapace-attestation`. Anyone can fetch it, so WIF must never accept it. - The **STS token** has the stack-specific audience in `WIF_AUDIENCE`. By default that is the WIF provider's full resource name. It is the only entry in the provider's `allowedAudiences`, and the provider condition requires `assertion.aud` to equal it. The enclave uses this token only for the STS exchange and never publishes it. The launcher also writes a default token with audience `https://sts.googleapis.com` into the container. It is not tied to this stack, so it is never an allowed audience. `carapace verify`: 1. Verifies the token signature against Google's published JWKS, plus the issuer, audience, and expiry. 2. Checks the hardware, software, debug, and secure-boot claims. 3. Checks that the container image digest is one the user allowed (`--allow-digest`, compared by hand against the CI build output; release signature verification is not implemented yet). 4. Checks the deployment: project id, enclave service account, and the container env's `CONTROL_PLANE_URL` and `KMS_KEY_NAME`, against the values the user passes (or `carapace deploy` takes from the stack). 5. Checks that the nonce binds the served TLS certificate and receipt key. 6. Pins the TLS certificate and the deployment for all further enclave connections. 7. Refuses to encrypt if the KMS public key from the server differs from the one in the attested response. ## Injection policy Every secret carries a policy: ```json { "v": 1, "hosts": [{"match": "exact", "value": "api.github.com"}], "schemes": ["https"], "methods": ["GET", "POST"], "ports": [443], "inject": {"kind": "header", "name": "Authorization", "template": "Bearer {secret}"}, "limits": {"req_bytes": 1048576, "resp_bytes": 5242880, "rpm": 60, "timeout_s": 30} } ``` For each request, the enclave executor: - Validates the URL against the allowlist, HTTPS only. - Resolves DNS once, blocks private and link-local addresses, and connects to the pinned IP to prevent DNS rebinding. - Rejects agent-supplied `Host`, `Proxy-*`, and injection-target headers. - Sends `User-Agent: carapace-enclave` when neither the agent nor the injection sets one (GitHub, for one, rejects requests without it); an agent-supplied `User-Agent` passes through unchanged. - Does not follow redirects. - Caps request and response sizes. - Redacts the secret from response headers and body, in raw, base64, URL-encoded, and JSON-escaped forms. - Rate-limits per owner fingerprint and secret, at the policy's `rpm`, with an overall cap per owner fingerprint (and on KMS unwraps per owner). - Throttles a peer address after repeated refused authorizations, so an unauthenticated flood cannot spend the control-plane budget of every owner. - Emits one signed receipt per authorized request (outcome `ok`, `denied` by policy, or `error`). Requests refused before authorization leave no receipt; see [THREAT_MODEL.md](THREAT_MODEL.md#r6-refused-requests-leave-no-receipt). ## Receipts Receipts are hash-chained and signed by the per-boot Ed25519 key bound into the attestation nonce. The server stores them verbatim along with each boot's attestation token. `carapace audit verify` checks chains and signatures offline. Unsigned receipts never verify. ## Residual risks Summarized here; [THREAT_MODEL.md](THREAT_MODEL.md#residual-risks) has the full list: - A GCP project owner can change KMS IAM. That change is visible in Cloud Audit Logs. Self-hosters are their own project owner. - The key's IAM policy is not the only path to the key. A Cloud KMS role that decrypts, granted on the project, folder or organization, is inherited by the key, and a project Owner can grant one to anyone. Its holder can call `AsymmetricDecrypt` without an attested enclave and so unwrap any envelope's data key. The basic Editor role cannot: it has no KMS decrypt permission and cannot change IAM or the WIF pool (checked on a live project; it can stop or replace the enclave VM, which fails closed for decrypt, see [THREAT_MODEL.md, R1](THREAT_MODEL.md#r1-a-gcp-project-owner-can-decrypt)). The stack enables KMS Data Access logs, so every such call is logged, and the `foreign_decrypt` clause of the decrypt-path alert (on unless `enable_iam_alerts` is `false`) fires on any `AsymmetricDecrypt` against the key ring from a caller outside the stack's attestation pool. It detects the decrypt; it does not prevent it. Keep Owner, and any Cloud KMS role, to as few principals as possible. - Anyone with database write access can deny service, and can serve an *older* owner-signed grant or envelope in place of the current one. A withheld revocation, narrowing or rotation takes effect no later than the old grant's expiry (at most 90 days, chosen by the owner), and a running enclave refuses, best-effort, to go back to anything older than it has already verified in that boot. Sealing a rotated value under a fresh `secret_id` keeps it away from a revoked key; taking the *old* value away before expiry means rotating the credential at its provider. They cannot substitute secrets, edit policies, or authorize keys. - The enclave's clock comes from `max(VM clock, attestation token iat)` and never decreases within a boot. The VM operator cannot turn it back, and can hold it still only until the attestation token that gates KMS access expires (about an hour), after which the enclave can unwrap nothing. - The owner's device holds the owner signing key and is in the TCB, as it already is for plaintext at sealing time. - Google's hardware, the Confidential Space launcher, and the KMS HSMs are in the trusted computing base. ## Local development A one-command `docker compose` setup (server on SQLite, mock enclave, httpbin) is planned but does not exist yet. Today the server runs locally in `dev` mode (`server/README.md`), and a dev-only mock of Confidential Space and Cloud KMS (`enclave/mock`: `iss=mock://local` tokens and an in-memory RSA key) is used by the tests. The mock cannot reach production, for three reasons: - It is excluded from the production image, so any image that contains it has a different digest and no IAM binding. - The production entrypoint exits if the TEE socket is absent. - The CLI rejects mock tokens unless `--insecure-mock` is passed.