ADR 0006: COSE Envelope Modes and Key Management
Status
Accepted — 2026-09-24. Proposed and accepted the same day; the maintainer recorded all ten decisions below (cratestack#1003). P0's envelope is implemented in the cratestack-cose crate (cratestack#1005): unary Mac0/Sign1, the external AAD, nonce replay and the shared test vectors, plus the auth feature's adapters. The server layer that calls it from the generated routers is cratestack#1006 (shipped in 0.14.0; see Signed transport); the Rust client is cratestack#1007. A proof of concept exists outside this repository (maintainer, 2026-09-24). The design and the measurements below come from that work; the proof of concept's fixture was reconstructed for the vectors because its script was not available.
Amended 2026-09-30 (binding version 2). The AAD no longer binds the whole-schema digest. It binds the digest of the op being called, so a server-only schema edit no longer refuses every signed client (cratestack#1123, epic #1030; §4, "Op contracts"). Binding version 1 froze with 0.15.0, so this is a breaking wire change with a flag day: a version 2 verifier refuses version 1.
Amended 2026-10-09 (negotiated payload types). The inner payload is no longer fixed to CBOR. The type was already element 8 of the AAD (§4), so only which string goes in became negotiable: two unbound selector headers, Cratestack-Payload-Type and Cratestack-Payload-Accept, default to application/cbor, and a response binding names the response payload's own type (cratestack#1168; §2, "Payload types"). No binding-version change, no COSE header change, and a message that names no type is byte-identical to 0.15.3's.
Amended 2026-11-06 (compact wire keys). The compact payload types application/vnd.cratestack.compact+cbor and application/vnd.cratestack.compact+json are sealable payload types like any other (§2, "Payload types"). The AAD already binds the op contract digest, which fixes the key table, and the payload type, which fixes the key mode, so no binding-version change and nothing new is bound. A route permits a compact type exactly when it declares its plain counterpart; the layer's opt-in set must list the compact type itself. Any +cbor-seq type, the compact one included, is never sealable. The Rust client negotiates the mode per op, as unsealed calls do, and the Dart and wasm sealers take the payload type on their call binding (cratestack design compact-wire-keys.md).
Amended 2026-11-15 (signed subscription open with unsigned frames) (since 0.20.4; cratestack#1378). A subscription may opt in to a signed open whose frames are not signed. This is weaker than the signed subscription binding §6 plans, and the threat model says exactly how much weaker. See Amendment: signed subscription open with unsigned frames.
Prepared retries (since 0.18.1, cratestack#1297). A durable prepared call retains the inner payload, not a reusable outer envelope. Each sealed attempt needs fresh claims and the matching response-opening state; see idempotency.
This fills the slot ADR 0001 reserved as "ADR 0006: COSE Envelope Modes and Key Management". It keeps 0001's envelope principle ("COSE is not a codec. COSE wraps encoded bytes") and its processing order (HTTP body → envelope.open → codec.decode).
Date
2026-09-24 (proposed and accepted)
Decisions
The maintainer's decisions, recorded 2026-09-24 in cratestack#1003. Where a decision differs from the recommendation or needs more than one line, the paragraph below the table says what it means.
| # | Question | Recommendation | Decision |
|---|---|---|---|
| D1 | Shape: a signing envelope over CborCodec, or a sibling CratestackCodec? | Envelope. The codec trait has no key, no request context and no async, and signing needs all three (§1). | Accepted: envelope. |
| D2 | Wire modes: mac0, sign1, and chain for streams? | All three. chain is what makes signed streaming affordable (§6). | Accepted: all three. |
| D3 | Replay model for device keys: a per-device counter with a sliding window, replacing timestamp skew plus a nonce store? | Yes. The current 300 s skew window rejects any mutation that sat in an offline queue longer than that (§5). | Accepted: window for device keys; nonce stays for service keys. |
| D4 | Schema-bound integer keys: a separate packed codec, shipped later? | Yes, a separate ticket (§9). | Accepted: separate ticket. |
| D5 | Register CBOR tags 48900 (existing error sentinel) and 48901 (checkpoint) with IANA (First Come First Served)? | Yes, before 1.0. | Accepted: register before 1.0. |
| Q1 | Sign unary GET responses by default in Required mode (+85 B each), or only mutations and streams? | — | Sign them. In Required mode every response is signed, GETs included. |
| Q2 | Default server key algorithm: EdDSA or ES256? Cloud HSMs and KMSs support P-256 more widely. | — | EdDSA by default, ES256 opt-in, both from P0. |
| Q3 | Checkpoint cadence per op or global? Is 64 items / 32 KiB / 2 s right for 2G links? | — | Global 64 items / 32 KiB / 2 s, with the per-op @stream(checkpoint: 16|64|256) override. |
| Q4 | Counter atomicity: JsonFileStateStore writes with a plain std::fs::write (cratestack-core/src/store/client_state.rs), which is not crash-atomic. Should window mode require the SQLite store, or should the JSON store move to write-temp-then-rename first? | — | A store contract, not a store choice. See below. |
| Q5 | Post-quantum: accept that algorithm agility plus chain mode is the migration path (ML-DSA-44 signatures are about 2.4 KB)? | — | Agility + chain, with a hybrid slot reserved now. See below. |
A decision that changes a public trait (D1, the async CratestackEnvelope below) is breaking. That is acceptable before 1.0, and today only NoEnvelope implements it.
Q2, what "opt-in" means. The server's response-signing key is EdDSA unless a deployment configures ES256, typically because the key lives in a cloud KMS or HSM, or because a browser client holds it as a non-extractable WebCrypto key. The CoseSigner / CoseVerifierResolver traits (§1) already carry the algorithm, so both ship in P0. The shared test vectors (P0) are byte-exact for EdDSA, which is deterministic. For ES256, whose signatures are randomized, they assert verification instead of comparing bytes.
Q4, the counter lives behind the store trait. The client already persists state through the ClientStateStore trait (cratestack-core/src/store/client_state.rs), which has in-memory, JSON file, SQLite (cratestack-client-store-sqlite) and Redis (cratestack-client-store-redis) implementations, and applications can provide their own. window mode does not pick a store. It adds a contract to the trait: persisting a queued signed frame and advancing the counter is one atomic operation, and a crash leaves either both or neither. Each shipped implementation meets it in its own way:
- the JSON file store writes a temporary file, fsyncs it, then renames it over the old one. Today it uses a plain
std::fs::write, which is not crash-atomic. - SQLite uses one transaction.
- Redis uses
MULTI/EXECor a Lua script.
A third-party store that meets the contract works in window mode unchanged. The exact trait method is designed in P2 (#1015).
Q5, the reserved hybrid slot. No post-quantum algorithm ships now. Algorithm agility (the protected alg header) plus chain mode is the migration path. Chain mode amortizes one large signature (ML-DSA-44 is about 2.4 KB) over a checkpoint window. What "reserved" commits P0 to:
- Nothing assumes a signature length. Buffers, size limits and the chain checkpoint parser size the signature from
alg, never from a hard-coded 64 bytes. - A hybrid is one
algvalue. A composite algorithm identifier (a classical and a post-quantum signature over the sameSig_structure, as in the IETF composite-signature work) goes in the existingalgheader, and the composite key's thumbprint prefix is thekid. The COSE structure, the AAD and the checkpoint payload do not change, so adding it is not a wire break. - Resolution is by
(kid, alg), asCoseVerifierResolver::resolvealready specifies. One device can hold a classical key and a hybrid key side by side during migration. - The AAD's binding-version field (§4, currently
1) is the escape hatch if a future scheme needs a different binding.
Decisions taken while scoping P0 (maintainer, 2026-09-24, on cratestack#1003). They amend the sections named below.
- Algorithm identifiers (§3): -19 (Ed25519) and -9 (ESP256), the fully specified algorithms of RFC 9864, which deprecates -8 (EdDSA) and -7 (ES256). Both encode in one byte, so no size changes. Nothing has shipped with -8/-7, so verifiers accept only -19 and -9 (and the Mac0 ids).
- REST responses are bound to the resource, not only to the route shape (§4). The AAD gains
path_params: the matched path parameter values in template order, as the router decoded them (empty for RPC, whose op id and body digest already bind it). With the template alone, a signedGET /accounts/1response would verify as the answer toGET /accounts/2. That gap matters because Q1 signs every GET response. - Crate placement (§11):
cratestack-cosesits at L2 with an optionalauthfeature. Without the feature it depends oncratestack-coreonly, so clients and the wasm/napi builds stay free ofcratestack-auth's Redis, reqwest and rustls dependencies. With it, the crate owns everything auth-specific too: theServiceSigningKeyandDeviceKeyResolveradapters, the Redis nonce bridge, and the COSE enrolment code.cratestack_auth::{build,parse}_cose_enroll_responsemove there, which is a breaking change (no known in-repo consumers). - P0 defaults:
- The shared vectors cover both
ctishapes: 16 random bytes (P0noncemode) and a 2-byte counter (the §3 measurements).ctiis injectable on the seal side for that purpose. - Multi-replica
noncereplay bridgescratestack-auth's existing Redis nonce store, keyed by(kid, cti). DeviceKeyResolvergains a required lookup-by-thumbprint method. This is breaking, so implementors get a compile error instead of every COSE device request failing silently.- A Mac0
kidis the RFC 9679 thumbprint prefix of the key. Deployers list their key ids so the thumbprints can be precomputed, and secrets shorter than 32 bytes are rejected. - Errors, per §10: every failed check is the same coarse
401; a backend outage (key resolver or nonce store) is a500. - The schema SHA first hashed the raw
.cstacktext, so a comment-only schema edit would reject every signed client. cratestack#1065 replaced it with a canonical schema identity (§4, "Schema identity (#1065)"). That still refused every client on any schema edit, so binding version 2 replaced it in the AAD with the called op's contract digest (§4, "Op contracts").
- The shared vectors cover both
Decisions for binding version 2 (maintainer, 2026-09-30, on cratestack#1123). They amend §4 and §10.
- The AAD binds the op's contract digest, not the schema's. Element 7 is
contract_sha, the digest of the wire closure of the op being called.BINDING_VERSIONis2. - An unbound
Cratestack-Contractheader selects the digest. It carries the first 8 bytes of the digest (unpadded base64url, 11 characters) and chooses which of the digests the server already accepts for the op to verify under. The signature covers all 32 bytes, so the header widens nothing. - A digest the server does not accept is an unsigned
426 contract_unsupported, answered before any key is looked up. Without the header, the server tries the accepted digests newest first, at most a configured number (default 4). - The response is sealed under the digest the request used.
- Version 2 verifiers refuse version 1, with no opt-in.
- A signed
/rpc/batchbinds the whole-contract digest for now, accepted only when it equals the server's current one. Per-frame digests are a follow-up. - The compatible-history lock (digests of older contracts a server still accepts, checked by a classifier) shipped in the change after binding version 2 (cratestack#1123, 2026-09-30): see §4, "The compatible-history lock". Without a lock the accepted list per op has one member.
Decisions from the P0 security review (maintainer, 2026-09-24, on cratestack#1005). They amend §1, §4 and §10.
- The AAD binds the recipient (§4). A new
audienceelement carries a configured logical service identifier, not the Host header, which gateways rewrite. Without it:- one signed request opens at any two services that share a schema and a route, each with its own nonce store;
- in Mac0 mode, a service's own outgoing request is a valid incoming request to itself.
- Signed responses to bodiless requests are fresh (§4). A GET has no body, so its request digest used to be
SHA-256("")every time, and a year-old signed response still verified for a new GET of the same URL. The client now sendsCratestack-Nonce: 16 random bytes, base64url without padding, on every request it wants a verified response to, which inRequiredmode is all of them. For an unsigned request,request_digest = SHA-256(nonce ‖ payload). Each signed response is then bound to exactly one request, with no server state. - Encode in place stays (§1), through an additive hook.
CratestackCodecgains a providedencode_intoandCratestackEnvelopea providedseal_value. The COSE envelope overridesseal_valueto encode straight into its output buffer, so neither trait merged in cratestack#1066 breaks. HMAC and ES256 compute over the MAC/Sig structure incrementally. So does Ed25519, using the two-pass PureEdDSA streaming ined25519-dalek'sMultipartSigner(maintainer, 2026-09-25). That is the same computation as thehazmatmodule's streaming API, but dalek expands the key itself, which avoids the key-leak misusehazmatwarns about. No algorithm makes a contiguous to-be-signed copy. Streamed signatures are byte-identical to the standardsignfor every vector and a range of payload sizes. The streamed verify is as strict asverify_strict: dalek's streaming verify does not check that R decompresses, that R is not small-order, or that the public key is not weak, so cratestack adds those checks itself, and tests prove each one matters. - A response binds the kind of request it answers (§4). The response AAD carries
request_kind:0means the request was unsigned and its digest isSHA-256(nonce ‖ payload);1means it was signed and its digest isSHA-256(request COSE bytes). Without it, the two digest forms could collide: a signed requestCre-presented as an unsigned twin with nonceC[..16]and bodyC[16..]produced a server response that verified as the answer toC. A response carriesrequest_kind,request_digestandstatustogether; a request carries none of them. - An empty
audienceis misuse (a500), because it silently gives up the protection. A service's inbound audience must differ from the audience it uses when sending to peers: a shared name such asinternaldefeats reflection protection. - Kept as specified: the replay store is keyed by
(kid, cti). Two keys that share an 8-byte kid could interfere, but targeting that costs about 2⁶⁴ work and only causes a denial of service. The Mac0KeyProvideradapter refuses an empty id list, a repeated id, and two ids that resolve to the same secret; it reads the keys once at load, so a rotation means a rebuild. - Security hardening, found by the same review and implemented in #1005:
- the verified principal is the thumbprint of the key that actually verified, and that key's
kidmust match the header; - an HMAC key is bound to exactly one of alg 4 or 5, so a 256/256 deployment never accepts a 64-bit tag;
- ESP256 signatures are low-S only, which keeps "one message, one encoding";
- the skew bound is validated when the envelope is built.
- the verified principal is the thumbprint of the key that actually verified, and that key's
Context
What exists today
Verified against cratestack main on 2026-09-24.
| Piece | Where | Relevance |
|---|---|---|
CratestackCodec: sync encode/decode, no context | cratestack-core/src/codec.rs | Cannot sign: no key, no request, no async |
CratestackEnvelope: open_request/seal_response over bytes, sync | same file | The right seam, but only NoEnvelope implements it and no router calls it (at writing; see the update below the table) |
HmacEnvelope + SealedEnvelope (HS256) | cratestack-core/src/envelope.rs | A separate async API that does not implement the trait |
Ed25519 Authorization: Signature … with contentSha256 | cratestack-auth/src/signed_request/ | DeviceKeyResolver, NonceStore, skew window |
COSE_Sign1 via coset (EdDSA, kid) for enrolment | cratestack-auth/src/cose_enroll.rs | The only coset user |
application/cbor-seq for @stream, error sentinel Tag(48900, RpcErrorBody) | cratestack-axum/src/transport/stream_sequence.rs, RPC_STREAM_ERROR_TAG in cratestack-core/src/rpc.rs | The streaming path to extend |
CborSeqChunkDecoder | cratestack-client-rust/src/streaming.rs | Boundary scanner that learns one more tag |
RuntimeEnvelopeConfig::CoseSign1 | cratestack-client-rust/src/runtime/handle.rs, mirrored in cratestack-client-flutter | A placeholder, rejected at runtime: "COSE envelope support is not implemented yet" (at writing; see the update below the table) |
x-cratestack-schema-sha | cratestack-client-rust/src/client/headers.rs | Goes into the AAD |
Rust-backed CBOR for JS (cratestack-cbor-wasm, -napi), FRB for Flutter | crates/ | One Rust COSE implementation can serve every client |
Update (0.19.x): the two "not yet" rows above are no longer true. The server wiring shipped in 0.14.0 (cratestack#1006):
EnvelopeLayerandcratestack_cose::CoseEnvelopelive incrates/cratestack-axum/src/envelope_layer/behind theenvelope/cosefeatures. The Rust client shipped in 0.15.0 (cratestack#1007):RuntimeEnvelopeConfig::CoseSign1/CoseMac0only name the envelope, and the host supplies the keys throughRuntimeHandle::with_envelope(crates/cratestack-client-rust/src/runtime/handle.rs, featurecose). What is still refused is a config that names an envelope with none supplied, viaRuntimeHandle::new(runtime/sealing.rs,Sealing::agrees_with: "…build the handle withRuntimeHandle::with_envelope").cratestack-client-fluttercarries acosefeature (FlutterClientEnvelope), andpackage:cratestack_cbor/cose.dartseals and opens through it (0.15.4, cratestack#1151). The old message, "COSE envelope support is not implemented yet", no longer exists in the source.
Two defects in today's HmacEnvelope, independent of this proposal:
- It signs a different serialization than it sends. The MAC covers
serde_json::to_vec(&body), while the envelope travels as CBOR. Verification works only because both sides re-serialize aserde_json::Valueidentically. Any value that JSON represents lossily (bytes, big integers, CBOR tags, float edge cases) breaks that silently. COSE removes the issue: the payload is a byte string, and the verifier checks the bytes it received. - It spends bytes badly: 147 B per message, against 41 B for the COSE_Mac0 equivalent.
Codec parity today
Snapshot (2026-09). This table records the state when the ADR was accepted. The COSE column has since changed for the Rust, Dart and Flutter clients (see the update above); the cbor-seq and TypeScript cells were not re-audited here, so check the RPC transport guide before relying on them.
The goal is that every client and server surface can speak application/cbor, application/cbor-seq and the COSE media types.
| Surface | CBOR | cbor-seq | COSE |
|---|---|---|---|
| Server, RPC | yes | @stream responses | — |
| Server, REST | yes | list-returning procedures | — |
| Rust client, RPC | yes | yes | placeholder, rejected (now sealed via with_envelope, 0.15.0) |
| Rust client, REST (generated) | yes | the runtime has post_list_streamed; codegen never calls it | — |
| TypeScript client, RPC | yes (@cratestack/cbor) | yes | — |
| TypeScript client, REST | no: Accept: application/json is hardcoded | no | — |
| Dart client, RPC / Flutter | yes (FRB) | yes | placeholder, rejected (now sealed via cose.dart, 0.15.4) |
| Dart client, REST | yes | no | — |
| Upload direction | — | nowhere: /rpc/batch is one CBOR array | — |
| Subscriptions | — | SSE (text) only | — |
cbor-seq parity is tracked separately from COSE because it needs no decision from this ADR. It is a prerequisite for §6 and §7 on every surface.
Measurements
These numbers come from the proof of concept (coset 0.4.2, ed25519-dalek, hmac/sha2, minicbor-serde configured like CborCodec). The fixture is a 7-field payment row: 112 B as CBOR with string keys. The bench script is not yet in this repository; bringing it in is part of P0's shared test vectors.
Unary, one row:
| Wire shape | Bytes | Overhead |
|---|---|---|
Plain application/cbor | 112 | — |
HmacEnvelope today | 259 | +147 (+131%) |
Ed25519 Authorization: Signature today | 112 + 304 header | +304 |
COSE_Mac0 HMAC 256/64, kid(8), {iat, cti} | 153 | +41 (+37%) |
COSE_Mac0 HMAC 256/256, kid(8), {iat, cti} | 178 | +66 |
COSE_Sign1 EdDSA, kid(8), {iat, cti} (request) | 210 | +98 (+88%) |
| COSE_Sign1 EdDSA, kid(8) only (response) | 197 | +85 |
Signature headers are unique per request, so HTTP/2 HPACK cannot compress them, and clients rarely compress request bodies. Raw size is the uplink cost, and on the target links the uplink is usually the scarcer direction.
Streams, 1 000 rows:
| Mode | Raw | zstd-3 | vs plain (zstd) |
|---|---|---|---|
Plain application/cbor-seq | 112 371 | 21 919 | — |
| Per-item COSE_Sign1 | 203 115 | 93 100 | ×4.25 |
| Per-item COSE_Mac0 HMAC 256/64 | 146 115 | 35 317 | ×1.61 |
| Chain, Sign1 checkpoint every 16 | 120 294 | 29 207 | +33% |
| Chain, Sign1 checkpoint every 64 | 114 385 | 23 795 | +8.6% |
| Chain, Sign1 checkpoint every 256 | 112 876 | 22 396 | +2.2% |
Ed25519 signatures are 64 bytes of incompressible noise. Signing every item costs +81% raw but ×4.25 after compression, because the signatures break the redundancy compressors exploit. Chained checkpoints keep a compressed stream within a few percent of unsigned.
Integer keys (§9), same rows, unsigned: 112 → 57 B per row raw (−49%); zstd total 21 919 → 18 471 (−16%).
Goals and non-goals
Goals
- End-to-end integrity and origin authentication of payloads. They must survive TLS-terminating hops (CDNs, load balancers, operator and corporate proxies), storage in the offline SQLite store, and relay between devices.
- Low byte overhead, especially on the uplink and on streams.
- Streaming as a first-class case: incremental verification, detectable truncation, and resumption (P3).
- Offline-first: a mutation signed when it was created stays valid for upload days later, without opening a replay hole.
- Auth headers become optional for signed traffic. A verified COSE key identifies the caller, so writes do not also need
Authorization: Signature(§12). - No canonicalization anywhere. Verify the exact bytes that were sent.
- One Rust implementation shared by the Rust, JS (wasm/napi) and Flutter (FRB) clients.
Non-goals (v1)
- Confidentiality. TLS covers transit; COSE_Encrypt0 for data at rest can come later on the same structure.
- Multi-signer COSE_Sign, or COSE_Mac with recipients.
- Replacing TLS or the enrolment protocol.
Decision (proposed)
1. Layering: the codec stays simple, the envelope binds
typed value ──CborCodec──▶ payload bytes ──CoseEnvelope──▶ COSE_Sign1 / COSE_Mac0 / chain
▲ │
└──────── verified as-is ◀─────┘The payload bstr is the codec output, byte for byte. The sealer encodes straight into the COSE buffer: it reserves up to 9 bytes for the bstr head, encodes, then patches the head. The call path is the provided CratestackEnvelope::seal_value over CratestackCodec::encode_into (see "Decisions from the P0 security review"); seal(payload) remains for callers that already hold encoded bytes. The verifier checks those bytes and hands the same slice to CborCodec::decode. Nothing is re-serialized.
The envelope is not a new CratestackCodec implementation. Signing needs a key, request context (route, method, request digest) and async (so KMS and HSM backends can plug in). Adding those to the codec trait would touch every C: CratestackCodec bound across the server and all clients. The envelope seam already exists and only needs to become real:
/// Replaces the current sync, bytes-only trait (only `NoEnvelope` implements it today).
pub trait CratestackEnvelope: Clone + Send + Sync + 'static {
fn media_type(&self, shape: BodyShape) -> &'static str;
fn seal<'a>(&'a self, payload: Bytes, bind: &'a Binding<'a>)
-> BoxFuture<'a, Result<Bytes, CratestackError>>;
/// Verifies, runs replay checks, records the principal (kid → device/service)
/// in ctx, and returns the payload slice (zero-copy into `body`).
fn open<'a>(&'a self, body: Bytes, bind: &'a Binding<'a>, ctx: &'a mut CratestackContext)
-> BoxFuture<'a, Result<Bytes, CratestackError>>;
/// `None` = this envelope does not sign streams (plain cbor-seq passes through).
fn stream_sealer(&self, bind: Binding<'static>) -> Option<Box<dyn StreamSealer>>;
fn stream_opener(&self, bind: Binding<'static>) -> Option<Box<dyn StreamOpener>>;
}
/// Everything that goes into external_aad. Built by router/client, never sent (§4).
pub struct Binding<'a> {
pub audience: &'a str, // configured logical service identifier (the recipient)
pub method: &'a str,
pub route: &'a str, // op_id for RPC; route template for REST
pub path_params: &'a [&'a str], // REST: matched values in template order; RPC: empty
pub query: Option<&'a str>, // canonical_query()
pub contract_sha: &'a [u8; 32], // the called op's contract digest (binding version 2)
pub payload_media_type: &'a str, // "application/cbor"
pub response: Option<ResponseBinding>, // responses only; None on requests
}
/// All three travel together: a half-built response binding does not compile.
pub struct ResponseBinding {
pub request: RequestDigest, // { kind: Unsigned (0) | Signed (1), digest: [u8; 32] }
pub status: u16,
}Keys are signed with, not exported. KeyProvider::resolve_signing_key returns raw key bytes. That fits HMAC, but not asymmetric keys held in an HSM or KMS. Add:
#[async_trait]
pub trait CoseSigner: Send + Sync + 'static {
fn alg(&self) -> coset::iana::Algorithm;
fn kid(&self) -> &[u8]; // 8-byte thumbprint prefix, §3
async fn sign(&self, to_be_signed: &[u8]) -> Result<Vec<u8>, CratestackError>;
}
#[async_trait]
pub trait CoseVerifierResolver: Send + Sync + 'static {
/// May return several candidates on kid-prefix collision; the opener tries each.
async fn resolve(&self, kid: &[u8], alg: coset::iana::Algorithm)
-> Result<Vec<VerifyingKey>, CratestackError>;
}Adapters: KeyProvider → Mac0 keys, DeviceKeyResolver → device Ed25519 keys, and ServiceSigningKey → a CoseSigner.
2. Media types
| Body | Media type | Basis |
|---|---|---|
| Unary signed | application/cose; cose-type="cose-sign1" | RFC 9052 §2 |
| Unary MAC'd | application/cose; cose-type="cose-mac0" | RFC 9052 §2 |
| Signed stream (chain or per-item) | application/vnd.cratestack.cose-seq+cbor-seq | RFC 8742 +cbor-seq suffix |
The inner payload media type is not sent in a COSE header (label 3 would cost bytes on every message). It is bound through the AAD instead (§4), so a verifier cannot be tricked into decoding the payload as something else.
Payload types (amended 2026-10-09). The type bound in the AAD is carried by two unbound selector headers, the same pattern as Cratestack-Contract (they select among what the verifier already allows and never widen it, because the AAD carries the whole string): Cratestack-Payload-Type (a request's type; on a sealed response, that response's type) and Cratestack-Payload-Accept (the response types the client reads, in order). Absent means application/cbor. A request binding names the request payload's type; a response binding names the response payload's own type. A payload type is a lowercase type/subtype of token characters without parameters or wildcards; an envelope (application/cose*), a stream (application/cbor-seq, text/event-stream) and a multipart body are never sealable. What an op allows is the layer's opt-in set intersected with the types its route declares (a compact type counts as declared when its plain counterpart is, amended 2026-11-06); /rpc/batch stays CBOR. A selector repeated or malformed is a 400, a request type the op does not allow a 415, and no acceptable response type a 406, all unsigned and before any key lookup; a header that lies fails the signature (the coarse 401). The client never decodes a type it did not request.
Why compact keys have no +cose type (amended 2026-11-06). A sealed body with compact wire keys is application/cose outside and application/vnd.cratestack.compact+cbor (or +json) as its payload type, not application/vnd.cratestack.compact+cose or application/cose; keys=compact. The outer type names the COSE structure (Sign1 or Mac0), and the key mode belongs to the payload. Only the payload type is in the AAD (element 8), so it is the one place the mode is authenticated; an outer spelling would be unbound and would duplicate it. This is the same path JSON takes, which is why compact needed no binding-version change. The cost is that a sealed compact call shows in Cratestack-Payload-Type, not in Content-Type (cratestack docs/design/compact-wire-keys.md §5.1).
Whether a request or response is sealed at all is still negotiated by the existing Accept and Content-Type headers (application/cose); the inner payload type is the selector headers above. The router gains a policy: Required (unsigned requests → 401 unauthenticated), Optional (verify when present), or Off.
3. Message layout
; Unary: COSE_Sign1 = tag 18, COSE_Mac0 = tag 17 (always tagged on the wire)
protected = {
1 => int, ; alg: -19 Ed25519 (default), -9 ESP256 (RFC 9864); Mac0: 4 = HMAC 256/64, 5 = HMAC 256/256
4 => bstr .size 8, ; kid: first 8 bytes of the RFC 9679 COSE Key Thumbprint
? 15 => { ; CWT Claims (RFC 9597), requests only
? 6 => int, ; iat, seconds
? 7 => bstr, ; cti: device counter (1–4 bytes) or 16 random bytes, §5
}
}
unprotected = {} ; always empty: nothing unauthenticated on the wire
payload = bstr .cbor Body- kid = an 8-byte thumbprint prefix. It is derived from the key, so no kid strings are stored, and rotation is self-describing. The birthday bound is about 2³² keys, and the resolver returns several candidates on the rare collision.
- Algorithms by direction.
- Device → server: Ed25519 (-19). Device keys are already Ed25519, and these messages must be provable later.
- Server → client: Ed25519 by default, or ESP256 (-9) where the key lives in an HSM or as non-extractable WebCrypto (Q2).
- Service → service inside one trust domain: Mac0 HMAC 256/256, or HMAC 256/64 on constrained links. A 64-bit tag is safe only because forgery requires online attempts, and those are rate limited.
- Why not Mac0 for devices? It is cheaper (41 B against 98 B), but with a symmetric key the server could forge device messages, so non-repudiation is lost.
4. External AAD: binding without bytes on the wire
external_aad = bstr .cbor [
2, ; binding version
audience: tstr, ; the recipient: a configured logical service id, never the Host header
method: tstr,
route: tstr, ; RPC op_id (stable across prefix rewrites); REST route template
path_params: [* tstr], ; REST: matched path parameter values in template order; RPC: []
query: tstr / null, ; canonical_query()
contract_sha: bstr .size 32, ; the called op's contract digest (see "Op contracts")
payload_type: tstr, ; "application/cbor"
bound_headers: [ ; request headers with semantics, exactly as sent (UTF-8, untrimmed)
idempotency_key: tstr / null,
if_match: tstr / null,
],
? request_kind: uint, ; responses: 0 = unsigned request, 1 = signed request
? request_digest: bstr .size 32, ; responses: kind 1 → SHA-256 over the request's COSE bytes;
; kind 0 → SHA-256(Cratestack-Nonce ‖ payload)
? status: uint, ; responses
]bound_headers (added 2026-09-26, cratestack#1006, inside version 1 before the freeze).
- A request binding has 9 elements and a response binding 12.
bound_headersis always present:[null, null]when a request carries neither header, and a response repeats its request's values. - A request that carries either header twice, or a value that is not UTF-8, is refused with an unsigned
400before anything is bound. - An empty value binds as
"", notnull. - It defeats an on-path party that strips
Idempotency-Keyfrom a re-sealed retry (the operation would run twice), or that strips or changesIf-Match(a conditional update would become a blind one). - Response headers (
ETag,Retry-After) remain unauthenticated.
Binding versions.
- Version 1 bound
schema_sha, the whole-schema identity below. Every change before its freeze was part of it:path_params,audience, the nonce-based unsigned digest, and cratestack#1065's schema identity. It froze with 0.15.0, the first release in which generated routers and clients put the envelope on the wire (cratestack#1006, #1007). (Decision 2026-09-26, taken when v0.13.0 was cut: tying the freeze to the crate's first release would have forced a new version for #1065 with no peer to be compatible with.) - Version 2 (cratestack#1123, 2026-09-30, 0.15.2) replaces element 7 with
contract_sha, the called op's contract digest. Every other element keeps its position, so the array lengths (9 and 12) are unchanged, and the version number is the domain separation: the same 32 bytes under 1 and 2 never produce the same AAD. - From 0.15.0 on, any change to the element list or to how an element is derived bumps the version. Verifiers reject versions they do not accept, and a version 2 verifier accepts only 2: accepting version 1 would cost a second trial verification on every failure (the version is not on the wire) and keep a downgrade path alive for peers that, one day after 0.15.0, almost certainly had not shipped. Version 1 peers must be upgraded together with their servers.
Both sides rebuild this from context they already have, so it costs 0 bytes on the wire. It defeats:
- cross-endpoint replay: a body signed for
payment.createfails onpayment.refund; - response swapping: a response is bound to its request and its status code;
- wire-shape drift, per op: a client whose wire shape for an op differs from the server's fails closed for that op, and only that op;
- cross-service replay and reflection: the
audiencenames the recipient; - header stripping:
bound_headerscoversIdempotency-KeyandIf-Match; - stale responses: an unsigned request's digest includes the client's
Cratestack-Nonce, so a signed response answers exactly one request; - digest-form confusion:
request_kindkeeps a response to an unsigned request from verifying as the response to a signed one.
Use the op_id, never the raw URL path, because gateways rewrite prefixes. (The same fact made Router::nest break descriptor lookup in cratestack#877.)
Schema identity (#1065). Version 1 bound schema_sha, the schema's canonical identity, which is not a hash of the .cstack text. Version 2 no longer binds it: it remains the identity the warn-only x-cratestack-schema-sha drift header carries (SCHEMA_SHA256), and the op contract digests below are what the AAD carries. It is SHA-256(b"cratestack/schema-identity/v1\0" ‖ canonical JSON), computed by cratestack_core::schema_digest over the parsed schema, and every producer calls that one function: the three include_*_schema! macros and the CLI that bakes the constant into generated Dart and TypeScript clients. The digest used to be computed twice, over the source bytes, once in the macros crate and once in the CLI.
- Excluded: source spans,
///docs, and the whitespace of attribute text outside string literals, including"""SQL bodies (@default( false )is@default(false)). Comments never reach the parser's output. The contents of"...",'...'and"""..."""literals stay verbatim, because@@sql("...")bodies and regexes carry meaning in their whitespace. Still digest-changing, by design (fail-loud): reordering attributes, a trailing comma in[...], and- 1versus-1. - Sorted by name: top-level declarations (models, types, enums, mixins, procedures, views, queries) and the fields of models, types, mixins and views, so moving a declaration is not a contract change. When the positional P3 codec (§9) lands, whatever assigns its ids (declared in the schema or kept in a lockfile) must enter the identity, or the domain tag must move to
/v2; field order is sorted away today, so ids cannot ride on it. - Declared order kept: enum variants, attribute lists and procedure arguments. No codec encodes a variant by its index (enums travel by name), but the first variant is the type's
Defaultand Postgres orders an enum by declaration, so the order is part of the contract. - Built by hand, versioned by tag. The canonical form is written node by node rather than by serialising the IR, so a new IR field enters the digest by a deliberate edit and never through a
#[serde(default)]. A golden digest test pins it; the/v1tag makes any later change to the form a new identity on purpose. - Cost (why version 2 exists): the whole IR is hashed, not only the wire surface, so a server-only edit (a policy, an
@@index, a view's SQL) also changed the digest, and every client built before the edit was refused with an unsigned401indistinguishable from a revoked key. Mobile clients cannot be force-updated, so underRequiredeach schema deploy would lock out every installed app.
This was inside version 1: it landed before the freeze above.
Op contracts (#1123). What the AAD protects is decode agreement: the server must never decode a signed payload (and the client must never decode a signed response) under a wire shape other than the one the signer encoded it under. The attack or confusion is re-interpretation of signed bytes: a field retyped, an arity changed, an enum variant reordered or removed, a field moved server-only, Page<T> against T[]. Cross-op and cross-service replay are already defeated by route and audience. The binding never guaranteed server behaviour: handler code, policies, validators and SQL do not change how bytes decode, so a digest that moves on such an edit refuses for no security reason. So the digest is per op, over what decodes:
- In the digest: the op's transport, key and kind, its input and output roots (a procedure's arguments and return type; for a model op the verb and the model), and the closure: every model, type, enum and view reachable from the roots through field types, generic arguments, relations and
@computed(params:), each in its wire projection (@server_onlyfields removed, since they are on no wire). The key is the RPCop_id, or"<METHOD> <route template>"on REST, so@api_versionis in it. - Out of it (
DROPPED_ATTRIBUTES, each entry reviewed against the generators that read it, with a test that pins the readers):@@allow,@@deny, procedure@allow/@deny/@authorize,@@index,@@sqlbodies,@@audit,@@retain,@@internal(it decides whether an op exists, which is the key),@@unique,@@soft_delete,@@subscribe/@@emit,@no_idempotency,@no_rate_limit,@isolation,@pii,@sensitive,@db_enforce, field@unique, view@from, procedure@deprecated, and the value validators (@length,@regex,@email,@range, ...). Thedatasource, config blocks,extensionblocks, theauthblock, the schema-level MCP config, queries, unreachable declarations, every other op, docs and spans are out entirely. - Fail-loud: an attribute not on the reviewed list stays in, including attributes added later, so a new one moves digests until someone reviews it onto the list.
- Derivation.
SHA-256(b"cratestack/op-contract/v1\0" ‖ canonical JSON), written node by node with every IR node destructured exhaustively, like the schema identity. If the derivation rules change, the tag moves toop-contract/v2. A second digest,client_contract_digest = SHA-256(b"cratestack/client-contract/v1\0" ‖ canonical JSON of the sorted [key, hex(op digest)] table), is the build identity of the whole client-facing contract, and what a signed/rpc/batchbinds. - Not caught, as before: a semantic change with no shape change (an
Intswitching from major to minor units). A later@contract_revision(n)attribute could force a break on purpose.
Choosing the digest (binding version 2). The AAD is never sent, and a server may accept more than one digest for an op, so the sender names its digest in the unbound Cratestack-Contract header: the first 8 bytes, unpadded base64url. The header is never trusted: it only selects among digests the server already accepts, and the signature covers all 32 bytes, so a lie is the ordinary 401.
- Header present and naming an accepted digest: one verification.
- Header present and naming none: an unsigned
426withRpcErrorBody { code: "contract_unsupported" }(RESTCONTRACT_UNSUPPORTED), before any key lookup. - Header absent (a hand-built client): the op's accepted digests are tried newest first, capped (default 4). A nonce is recorded only after a message verifies, so a failed trial burns nothing, and the COSE envelope parses the message and resolves its key once (
open_request_any), so a forged message costs one parse, one key resolution and at most the cap in verifications. - The response is sealed under the digest the request opened under.
- A signed
/rpc/batchhas one AAD and many ops: until frames carry their own digests it binds the whole-contract digest, accepted only when equal to the server's. - The accepted list per op is
[current], plus the older digests of a committed lock that a conservative classifier judges wire-compatible (next paragraph).
The compatible-history lock (#1123). A server built with include_server_schema!(.., contracts = "x.contracts.lock") accepts, per op, [current, ...locked, newest first]. The lock is a committed, content-addressed JSON file (format, domain, contracts: canonical op contract by digest, generations: client contract digest, date, note and op digests, oldest first) maintained by cratestack contract lock|check|prune. It widens nothing a peer can influence: the accepted set is fixed at build time from the file, the macro recomputes every stored contract's digest, and the selector header still only chooses among accepted digests. Admission is decided by cratestack_core::classify(old, new) per op, at compile time (an incompatible entry is a compile error naming the op and reason) and in contract check. It is security-critical and so refuses by default: identical op identity, return type and kept attributes; additions only as optional (or @default on a model op's own model, the one place a create input omits the field; a type or a model used as an argument decodes it as required, so there it is refused, and so is an added FindMany<T>? argument); required to optional only on input-only declarations; enum variants appended only to input-only enums; any added field on output-only declarations; every removal, retype, insertion, reorder or attribute change refused. Removing a field is refused even though the server would decode the message, because the signed value would be silently ignored. A round-trip test encodes values of the old generated types and decodes them as the new ones (and the reverse for replies) for each compatible rule (fixture-based, not random inputs), and pins that the refused @default edit really fails to decode. tests/vectors/contract.json pins the canonical contract bytes, digests and client contract digest of a fixture schema, for a future non-Rust sealer; the generated TypeScript and Dart constants are checked against it too, which is parity (both generators call the same Rust function), while the independent check is recomputing the digests from the canonical strings with plain SHA-256.
5. Freshness and replay, offline-aware
Both HmacEnvelope (ENVELOPE_DEFAULT_CLOCK_SKEW_SECS = 300) and the signed-request verifier gate freshness on a clock-skew window. A mutation signed in a dead zone and uploaded three hours later is rejected. Signing at upload time instead loses when and where the data was created.
| Mode | cti | Server state | For |
|---|---|---|---|
window | per-kid monotonic counter, minimal big-endian (1–4 B) | per kid: highest counter plus a 64-bit (configurable 256) bitmap | device keys |
nonce | 16 random bytes | existing NonceStore, expiring with the skew window | shared/service keys, multi-replica senders |
window is the anti-replay window of IPsec (RFC 4303 §3.4.3) and OSCORE (RFC 8613 §7.4):
- No time limit on validity: a request signed days ago is accepted if its counter is new.
- Out-of-order delivery within the window is tolerated, which covers parallel batch uploads.
- Server state is O(1) per device.
iatis still signed, for audit and for an optional per-op policy such as@signed(max_age: "30d").- The device persists the counter in the same write as the queued mutation. A reused counter reads as a replay (Q4). Reinstalling the app re-enrols with a fresh key.
- Multi-replica servers keep the per-kid window in Redis or PG with compare-and-set, using the backends the idempotency and rate-limit stores already use.
Retry is not replay. Derive the idempotency key from (kid, cti) when no Idempotency-Key header is present. The first delivery executes. A byte-identical resend within the idempotency TTL gets the stored response. A resend after the TTL is rejected by the window. The handler never runs twice.
6. Streams: chain mode
Items stay bare CBOR, as in today's application/cbor-seq. The server keeps a running hash and periodically emits a signed checkpoint:
h₀ = SHA-256(external_aad) ; binds the chain to this request, 0 bytes sent
hᵢ = SHA-256(hᵢ₋₁ ‖ itemᵢ_bytes)
checkpoint = #6.48901(COSE_Sign1) ; payload:
[ seq: uint, h_seq: bstr .size 32, ? resume: bstr, ? end: true / RpcErrorBody ]- Cadence: every 64 items, 32 KiB or 2 s, whichever comes first (Q3). Ops can override with
@stream(checkpoint: 16|64|256). - The terminal checkpoint is mandatory. It carries
end: true, or theRpcErrorBodyon failure. In signed mode it replaces the unsignedTag(48900, …)sentinel, so a forged error is detectable too. - Truncation is detectable. A body that ends without a terminal checkpoint is
Incomplete. Today a stream cut short by a middlebox that closes cleanly looks like a short, successful stream. - Reordering, dropping or injecting items breaks
hat the next checkpoint. - Detection:
RPC_STREAM_ERROR_TAGgets a siblingRPC_STREAM_CHECKPOINT_TAG = 48901. The RustCborSeqChunkDecoderand the TS scanner (packages/cratestack-ts-types/src/cbor-seq.ts) classify the leading tag. The tag wrapper keeps a checkpoint distinct from a legitimate item that is itself a COSE_Sign1.
Client consumption modes:
Verifiedbuffers items until their checkpoint verifies. Added latency is at most one checkpoint interval, and memory is at most N items.Optimisticreleases items at once and raises a tamper error at the checkpoint. This fits the offline store: a SQLite transaction per checkpoint window, committed when it verifies and rolled back when it doesn't.
Per-item mode (each item its own COSE_Sign1 with cti = seq) is opt-in per op, for items that must be verifiable on their own later.
Resumable streams (P3): a checkpoint may carry resume, an opaque server cursor. Because the checkpoint is server-signed, the server can accept it back statelessly. The client sends Cratestack-Resume: <base64url of the last verified checkpoint>, and the server verifies its own signature and continues the chain from h_seq. A sync that dies at 80% resumes at 80%. This works only for producers that can restart from a cursor (keyset-paginated reads), marked @stream(resumable).
Subscriptions: SSE is text, so binary COSE would need base64 (+33%). Signed subscriptions get a GET cbor-seq binding in chain mode with a time-based cadence, rather than signing over SSE.
7. Uploads: signing the offline queue
- When a mutation is created offline, the Rust runtime wraps it as a COSE_Sign1
RpcRequestframe withcti = next counterand persists those exact bytes. The queue is tamper-evident at rest, andiatrecords the device clock at creation. - On connectivity, the client flushes with
POST /rpc/batchandContent-Type: application/vnd.cratestack.cose-seq+cbor-seq. Each frame is independently signed. - The server verifies each frame on its own. Results return as a chain-signed cbor-seq keyed by
cti, so a partial failure fails only its frames. - After a dropped connection the client resends the unacknowledged frames. Anything already applied hits the §5 idempotency path.
BATCH_MAX_ITEMS (1 000) and DEFAULT_BODY_LIMIT_BYTES (2 MiB) apply unchanged. The client splits large queues into several batches.
8. Key distribution
- Server signing keys are published as a COSE_KeySet at
/.well-known/cratestack/cose-keys, next to the existingjwks_router. The client pins at enrolment: the enrolment response is already a COSE_Sign1 (cose_enroll.rs) and gains the response-signing key thumbprint(s). - Device keys use the existing
DeviceKeyResolver, indexed additionally by thumbprint prefix. - Rotation: the keyset holds old and new keys, and the kid selects between them.
9. Schema-bound integer keys (separate codec, P3)
The payload saving (−49% raw, −16% compressed) is larger than anything signing costs, and it helps unsigned traffic too. It is orthogonal to COSE and ships as CborPackedCodec (D4). It needs stable field ids, via explicit @wire(n) or ids locked in a committed lockfile, with renumbering a schema error. The op contract digest in the AAD makes a mismatch fail closed when signed; unsigned, the existing x-cratestack-schema-sha check is the guard.
10. Errors and threat model
- Verification failures return
401withRpcErrorBody{code: "unauthenticated"}and a coarse reason. The response must never reveal which check failed. A backend failure (the key resolver or the nonce store is unreachable) is a500, logged server-side: it says nothing about the message, and operators can tell an outage from an attack. - One refusal is unsigned and not a
401:426 contract_unsupported(binding version 2). It answers aCratestack-Contractselector that names no digest accepted for the op, before any key lookup, so it reveals only that this op's wire shape is no longer served: public information, since the digests ship in every client binary. It is not one of the checks this section forbids telling apart (signature, key, freshness, replay, audience, AAD). Being unsigned it is a hint, never proof: the client surfacesEnvelopeError::ContractUnsupported { op }and must not treat the server's contract as known. The client reads the body only for its code, so a proxy's own426staysUnsigned. It deliberately carries noUpgradeheader: RFC 9110 §15.5.22 asks for one, butUpgradeis connection-specific (§7.8), forbidden on HTTP/2 (RFC 9113 §8.2.2) and stripped by proxies, and no protocol token names an op contract;426is kept because every other 4xx already means something else here (409idempotency and transactions,410heuristically cacheable,412conditional headers,400the malformed selector). - Error responses are signed too, or a hop could inject fake errors.
- A client in
Requiredmode rejects unsigned or wrongly signed responses. It never falls back to plain. - Why sign over TLS at all: TLS ends at the first terminating hop. Signed data stays verifiable at rest and when relayed between devices. Financial audit needs non-repudiation. Signed responses can be cached by untrusted edges.
- Existing mechanisms:
HmacEnvelopebecomes Mac0 mode: deprecated for one minor release, then removed.Authorization: Signaturestays for bodiless requests (GET). For writes in signed mode, COSE subsumes it (method and route are in the AAD) and saves about 300 B of headers.- The 48900 sentinel is unchanged in unsigned mode.
- The idempotency stream bypass still applies to signed streams.
11. One implementation, every client
One Rust crate, cratestack-cose, implements this ADR's wire format itself, with a strict parser. It uses coset only as an interop oracle in its tests and for the legacy enrolment challenge, whose code moved into its auth feature (cratestack#1005). It sits at L2 with an optional auth feature (see "Decisions taken while scoping P0"): without the feature it depends on cratestack-core only. It is consumed by:
cratestack-client-rust(native, plus the Flutter runtime via FRB), which lifts the "not implemented" guard;cratestack-cbor-wasmand-napi, which gainseal,openand a chunk-fedStreamOpener. The React Native story in cratestack#893 plans a Rust-backed codec on device and inherits this.
Cross-language signature bugs almost always come from two implementations disagreeing on bytes. There is no second implementation in TS or Dart. The TS cbor-seq scanner stays for framing, but chain verification (hashing, signature checks) goes through the wasm/napi exports. Generated Dart clients that bypass the Rust runtime require the runtime for signed mode until there is demand.
Implementation status (since 0.15.4, cratestack#1151); the decision above is unchanged. The Dart and Flutter clients now reach the one implementation through the bridge: cratestack_cbor exposes cratestack-cose over flutter_rust_bridge natively and the cratestack-cbor-wasm build on the web, as package:cratestack_cbor/cose.dart. That release seals with in-memory keys (HMAC and Ed25519) in Required mode only. Signing with a key in a platform keystore, and the chain mode streams need, are not part of it. See the guide's The Dart client.
12. Placement relative to the existing layers
This section was added while reviewing the proposal against the codebase. It is what "auth headers become optional" requires.
- The envelope must run before rate limiting and idempotency. Both layers derive their caller key from the
Authorizationheader by default: they hash it, fall back to theConnectInfopeer, and refuse with412when neither exists (cratestack-axum/src/{ratelimit/key_fn.rs,idempotency/}). A COSE-only client sends noAuthorizationheader, so it would be bucketed by IP address or refused. The envelope'sopentherefore runs first and inserts the verified kid as aVerifiedPrincipalrequest extension. Rate limiting already treats that as a non-caller-mintableprinc:bucket that needs no cardinality budget (cratestack#871), which closes the "unverified header mints buckets" class for signed traffic. The idempotency principal fingerprint needs the same hook. This is P0, not a later phase: without it, the P0 wiring breaks existing throttling and replay behaviour for COSE clients. - The
(kid, cti)idempotency key belongs in the L3OpExecutor(ADR 0015). Its input is not an HTTP request, which is exactly the property that motivated building L3. Idempotency admission (slice 1) and rate-limit admission (slice 2, cratestack#877) already decide there. The in-process path from ADR 0018 then gets the same replay semantics without a second implementation.
Consequences
Positive
- One signing mechanism replaces
HmacEnvelopeand, for writes,Authorization: Signature, at a quarter to a third of their byte cost. - Signed streams cost single-digit percent over unsigned, and truncation becomes detectable, which it is not today.
- Offline mutations stay valid for upload indefinitely without a replay hole.
Negative
- Breaking change to
CratestackEnvelope, and toRuntimeEnvelopeConfigconsumers once the placeholder becomes real. - New server state per device key (the window), with compare-and-set on multi-replica deployments.
- Clients that bypass the Rust runtime cannot use signed mode.
Alternatives considered
- COSE as a
CratestackCodecimplementation. Rejected (§1): no key, no request context, noasync, and it would touch every codec bound in the workspace. - Per-item signing as the stream default. Rejected on the measurements: ×4.25 after compression. It remains available per op.
- Timestamp skew plus a nonce store for device keys. Rejected for devices (§5): it cannot accept an offline mutation older than the skew window. It stays the mode for service keys.
- Mac0 for device traffic. Rejected (§3): a symmetric key lets the server forge device messages.
Phasing
Tracked by the epic cratestack#1030 (one story per phase). The unsigned streaming it builds on is cratestack#1029 (cbor-seq parity).
| Phase | Scope |
|---|---|
| P0 | Accept this ADR. Async CratestackEnvelope + Binding. cratestack-cose with unary Mac0/Sign1 (EdDSA and ES256, Q2), AAD, nonce replay, and no hard-coded signature length (Q5). Axum + Rust client wiring behind Required/Optional, including the §12 principal handoff. Shared test vectors (fixed keys, deterministic Ed25519) checked in. |
| P1 | chain streams for @stream. Decoders learn tag 48901. Verified/Optimistic consumption. wasm/napi exports. Flutter guard lifted. COSE_KeySet endpoint and enrolment pinning. IANA registration of 48900/48901. |
| P2 | window replay for device keys. Signed offline queue. Signed /rpc/batch upload. Idempotency key derived from (kid, cti) at L3. HmacEnvelope deprecation. |
| P3 | Resumable streams (@stream(resumable)). Signed subscription binding. CborPackedCodec (separate ticket). |
Test plan (acceptance)
- Vectors: identical bytes from Rust native, wasm and napi for the same key and payload. Deterministic Ed25519 makes this exact.
- Tampering: flip one bit in each of the payload, the protected header, the AAD route, the AAD contract digest and the request digest → reject. A request bound under another op's digest (
neg-contract-cross-op) and one signed over a version 1 AAD (neg-binding-v1) → reject. Assert on encoded bytes, not decoded values. - Contract lock: a locked, compatible older digest opens and its response seals under it; an incompatible lock entry is a compile error naming the op and reason. Candidates that differ in anything but the contract digest, and an empty candidate list, are refused before any key lookup.
- Streams: truncate before the terminal checkpoint →
Incomplete. Reorder, drop or insert an item → reject at the next checkpoint. A forged error item → reject. A legitimate COSE_Sign1 item → not treated as a checkpoint. - Replay: resend the same bytes → idempotent response, handler not re-run. Counter below the window → reject. Out of order within the window → accept. Counter reuse → reject.
- Offline: mutation signed at T, uploaded at T+72 h → accepted in
windowmode, rejected innoncemode. - Layer placement (§12): a COSE-only request with no
Authorizationheader and noConnectInfo→ not refused with412, and charged to itsprinc:bucket. - Fuzzing:
CborSeqChunkDecoderover arbitrary bytes containing tags 18, 48900 and 48901.
Amendment: signed subscription open with unsigned frames
(since 0.20.4, cratestack#1378)
Status: accepted on 2026-11-15; the server half (part B1) and the generated Rust, TypeScript and Dart clients (part B2) are implemented (since 0.20.4, cratestack#1378). The TypeScript sealer is RPC only. The text above stays as accepted; this section appends to it. It builds on the cbor-seq subscription binding of ADR 0005.
Why
Today a signed route that is a subscription gets a sealed 406, because §6 plans signed subscriptions as a chain binding that has not been built. A client that signs every request therefore cannot subscribe at all. Some consumers do not need signed frames: they use the stream only as a nudge to re-read, and the re-read is itself sealed. This amendment lets them sign the open and accept an unsigned stream, and makes the cost explicit.
Decisions
- Opt-in syntax:
@frames(unsigned)on asubscription procedure. The set is closed.sealedandchainare reserved and refused with a message naming #1020 and #1008. It is refused on unary and@streamprocedures, and a duplicate is refused. - Contract:
@framesis part of the op contract, so it moves the op digest, and adding or removing it is wire-incompatible. The generated table that tells the envelope layer which subscriptions opted in defaults to empty, so a missing table fails closed. - Server: the open is verified exactly like a unary request (signature,
kid, audience, replay window, nonce, contract), and@allowis evaluated at open against the principal from that signature.- Without the opt-in, the answer stays the sealed
406. - With it, the original
Acceptmust name a stream type, or the answer is a sealed406. - A
2xxstream carriesCratestack-Frames: unsignedandCache-Control: no-store. Any other status (400,403,404, ...) is sealed as usual.
- Without the opt-in, the answer stays the sealed
- Lifetime: a signed open has a configurable maximum stream life, one hour by default. It ends with a terminal
unavailableerror frame, and the earlier of that and any credential expiry wins. The client re-seals on resubscribe with a fresh nonce, so a replayed open cannot be reused. - Clients: a client accepts a
2xxonly when it has the requested streamContent-Type, carries the marker, and the generated target saysframes: unsigned. Anything else is refused. A dropped marker, or a marker on an op that did not opt in, is an error, not a silent downgrade. - Model feeds are refused.
@@subscribestreams rows, which are values, not ids. They stay out of unsigned frames; see the threat model.
Wire
(since 0.20.4)
- Routes. RPC:
POST /rpc/subscribe/{op_id}, the sealed message as the body, orGETwith the input as a query. REST:GET /$procs/<name>with the sealed message as the body. A browser'sfetch, wasmreqwestand Dart web cannot send aGETwith a body, so a REST open is for native clients, and the RPCPOSTis the form every client can send (cratestack#1407). A sealedHEADon a subscription is a sealed406. - Which opens. The layer reads the generated
SUBSCRIPTION_FRAMEStable. On RPC a row matches by bare op id, and only for a route bound as a subscription, so a unary call to the same op id is not an open. On REST the row is"<METHOD> <path>"and matches the method as sent. An empty table (the default for a hand-built layer) opens nothing. - Naming the stream. The request's single
Acceptmust contain, literally,application/cbor-seqortext/event-stream; the first one listed wins, and parameters and case variants do not match.*/*alone, noAccept, or twoAcceptheaders is a sealed406. The layer readsAcceptbefore rewriting it for the router, then sets the one stream type, so the handler negotiates exactly that binding. - Order. Verify the open (signature,
kid, audience, replay, nonce, contract), check theAccept, map the principal, set the stream deadline, then call the handler. An unmarked RPC subscription is refused before its handler runs. - Answer. A
2xxstream in the requested type, produced by a subscription binding, is returned unsealed withCratestack-Frames: unsignedandCache-Control: no-store, and withoutCratestack-Payload-Type. Every other answer (400,403,404, a stream of another type, a body that no binding produced) is sealed, so a refusal is never mistaken for frames. This is the one exception to "a signed request is always answered sealed", and it holds in every mode that accepts a signed request (Required,OptionalandUnsignedRequestSealedReply). - Verification failures are unchanged: the coarse unsigned
401(replay, tamper, wrong audience, a signer the principal mapper refuses) and the unsigned426for an old op contract. An unsignedPOSTto a subscribe route underpolicy(EnvelopeMode::Required)was already refused (a405) and is now the coarse unsigned401; it reached the handler unsigned under any policy whoseunresolved_modeis not strict (for examplepolicy(EnvelopeMode::Required).unresolved_mode(Optional), or a customEnvelopePolicy), because the resolver did not bindPOSTthere, a pre-existing bypass this closes. Underpolicy(EnvelopeMode::UnsignedRequestSealedReply), an unsignedPOSTto a subscribe route changes from a405to the unsigned sealed-reply path, which refuses a subscription with the unsigned406before the handler runs. - Lifetime.
EnvelopeLayerBuilder::max_signed_stream_life(default one hour;buildrefuses zero and anything aboveMAX_SIGNED_STREAM_LIFE, 24 hours) puts a deadline in the request extensions. The stream then ends with the terminalunavailableframe, messagestream lifetime, and the credential's ownexpstill wins if earlier.
Threat model
What is protected: the open. A tampered, replayed or wrongly addressed open is a 401 and the handler never runs, and the principal that @allow sees is the one the signature names.
What is not protected: after a signed open, the integrity and authenticity of the frames. The request's Accept, the Cratestack-Frames marker and the frames themselves are all outside any signature.
- A relay or a man in the middle inside TLS termination can forge a
200with frames, drop frames, or end the stream. - The marker only tells an honest client what kind of stream it is reading. It is no evidence about who sent it.
- Clients must therefore treat frames as hints only. A frame carries an id, never a value, and the client acts on it by re-reading through a sealed call. This is the poke pattern of the A24 ADR-020 (a24-context#506).
- Worst case of a forged stream: extra sealed re-reads, or missed pokes until the next re-read. It can never produce a wrong value, because no value is taken from a frame.
- Confidentiality relies on TLS alone. Nothing here encrypts the stream.
- Model feeds carry rows, so a forged feed would put wrong values in front of the application. That is why they are refused for unsigned frames.
An application that cannot meet the hints-only rule must not use @frames(unsigned).
Added with the server half:
- A forged
unavailablecan cause a resubscribe storm. A relay that injects the terminal frame makes every client reopen at once. Clients resubscribe with jittered backoff. - Refusal detail is lost once sealed. A client sees the status and the sealed body, not the plain message a refusal would carry on an open stream.
- No per-principal cap on concurrent signed streams. One credential may hold many opens until each lifetime ends. Front the router with
RateLimitLayer::for_subscriptions. - The lifetime bounds exposure, not forgery. It limits how long one signed open vouches for a stream; it does not authenticate any frame inside it.
Deferred
Signed frames, and a ready frame sealed and bound to the request digest, are deferred to #1020, with chain in #1008. The Phasing row "Signed subscription binding" (P3) is unchanged and still open.
Also deferred:
- A REST
POSTopen. REST subscribe staysGET, so a browser cannot open one signed over REST (together with the TypeScriptsealedFetchGETbug, #1407). - The Flutter bridge (
execute_streamed) is out of scope for this amendment.