Files
2026-11-05 09:05:13 +01:00

196 lines
12 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Tabellarius — the relay and MLS groups (design note, day 2)
*Tabellarius*: the Roman letter carrier. He carried sealed tablets and could not read them.
Written before the code. Every app syncs its own encrypted event log through one relay, between your own
devices and between people, without the relay being able to read anything or tell who is who.
## 1. Layers
```
AppEvent { kind, body } the app's change
→ TEV1 envelope (docs/TESSERA.md §8) signed by the app's subkey, carries its cert
→ TBA1 batch of envelopes one MLS application message
→ MLS PrivateMessage (OpenMLS) encrypted to the group's current epoch
→ relay mailbox ciphertext, seq, size, time — nothing else
```
**Deviation from the plan, on purpose.** The plan put the MLS ciphertext *inside* the envelope. But the
envelope carries the Tessera cert and the author's subkey in the clear, so a relay that stored envelopes would
learn who wrote what, and could link one person's mailboxes through their root key. The privacy rules win: the
envelope travels **inside** MLS. The relay stores only MLS ciphertext. Receivers still verify every envelope
on its own (`Envelope::verify` against the group members' roots), so an event stays attributable and
re-verifiable after it leaves MLS, for example in a history batch for a new device (§6).
## 2. Identity inside MLS
- **Ciphersuite** `MLS_128_DHKEMX25519_CHACHA20POLY1305_SHA256_Ed25519`. Handshake messages are encrypted
too (`PURE_CIPHERTEXT_WIRE_FORMAT_POLICY`): commits travel as PrivateMessages, so the relay does not learn
who added or removed whom.
- **MLS signature key**: a fresh Ed25519 key per device per group, generated by OpenMLS. The Tessera subkey
never leaves tessera (it has no byte export), so it cannot be the MLS key itself. Instead it vouches for it.
- **Credential** (`BasicCredential`, identity bytes, `TDC1`):
```
"TDC1" ‖ u16 len ‖ SubkeyCert ‖ mailbox_key (32) ‖ binding (64)
binding = Ed25519(subkey, "tessera credential v1\0" ‖ mls_signature_key ‖ mailbox_key) (Subkey::sign_credential_tbs)
```
A member is accepted only if its cert verifies under its root now, its cert is for the group's app, and the
binding signature by the cert's subkey covers its leaf's MLS signature key and mailbox key.
- **The group's roots** are the roots of its members' certs; `Envelope::verify` uses them. A **self group**
accepts only members whose root is the creator's root (your own devices).
- New members are checked when a commit adds them and, on joining, every member of the Welcome's tree.
A commit that adds an invalid member is not merged; it is reported as rejected.
## 3. The relay: mailboxes
A mailbox id is 32 bytes, base64url:
- a **group mailbox** is `SHA-256("tabellarius mailbox v1" ‖ MLS group id)`;
- an **invite mailbox** is 32 random bytes.
What the relay stores, and why (SQLite, nothing else):
| Table | Columns | Why |
|:--|:--|:--|
| `messages` | `mailbox_id, seq, size, received_at, ciphertext` | the mail |
| `keys` | `key, mailbox_id` — `key` is **unique across all mailboxes** | who may read and write a mailbox |
| `acks` | `key, seq` | retention: what every member has fetched |
| `nonces` | `key, nonce, expires` (5 minutes) | replay protection |
No accounts, no IP addresses, no user agents, no app names, no certs. The relay's own logs are counts and
status codes, never ids or keys. Caddy's access log for this vhost is off.
### Requests
Every request except `GET /v1/health` is signed by a **random per-mailbox Ed25519 key**. A device makes a new
one for every group it joins and every invite. The relay never sees a Tessera key or cert, so it cannot link
mailboxes to each other or to a person. A key registered in one mailbox cannot be registered in another.
```
Tab-Key: base64url(public key)
Tab-Time: Unix seconds
Tab-Nonce: base64url(16 random bytes)
Tab-Sig: base64url(Ed25519("tabellarius request v1\0" ‖ METHOD ‖ "\n" ‖ path?query ‖ "\n" ‖ time ‖ "\n" ‖ nonce ‖ "\n" ‖ hex(SHA-256(body))))
```
| Check | Answer |
|:--|:--|
| any header missing or malformed | 401 `unsigned` |
| `|now − time| > 120 s` | 401 `stale` |
| bad signature | 401 `bad_signature` |
| key not registered in this mailbox (except `PUT` creating it) | 403 `unregistered_key` |
| nonce already seen for this key | 401 `replayed` |
| more than 120 requests a minute for this key | 429 `slow_down` |
A rejected request changes nothing and stores nothing (not even its nonce).
| Endpoint | What |
|:--|:--|
| `PUT /v1/mailboxes/{id}` | create; the signing key becomes its first key. 409 `exists`, 409 `key_in_use` |
| `POST /v1/mailboxes/{id}/keys` `{"key"}` | a member registers another member's key. 409 `key_in_use` |
| `DELETE /v1/mailboxes/{id}/keys/{key}` | deregister (removal, or leaving). The last key gone → the mailbox and its mail are deleted |
| `POST /v1/mailboxes/{id}/messages` `{"messages":[b64…]}` | append; at most 100 per batch, 300 KiB each, 2 MiB per request. A message already in the mailbox (same bytes) is not stored again; its seq is returned. → `{"seqs":[…]}` |
| `GET /v1/mailboxes/{id}/messages?after=N` | up to 200 messages with `seq > N` → `{"messages":[{"seq","data"}],"more"}` |
| `POST /v1/mailboxes/{id}/ack` `{"seq"}` | this key has everything up to `seq` |
| `GET /v1/health` | `ok`, unsigned |
| `GET /v1/live` | v1.1b: a WebSocket of notices `{"mailbox","seq"}`; each subscription is signed in the first message (docs/V1_1B.md §3) |
Every answer carries `Access-Control-Allow-Origin: *`, and `OPTIONS` answers a preflight, so a PWA on another
origin can call the relay (docs/V1_1B.md §4). Caddy strips `Origin`, `Referer` and `User-Agent` first.
### Retention (config values)
A message is deleted once **every key of its mailbox has acknowledged it and it is older than 30 days**
(`retention_acked_days`). Every message is deleted after **90 days** regardless (`retention_max_days`). The
plan said "30 days after the last ack"; the relay does not record when an ack happened, so it counts from
receipt. That can delete earlier than the plan for a member who acked late, never later than 90 days.
## 4. Invites
An invite is a capability: whoever holds the token can ask to join, and the inviter's device accepts any
**valid** credential (a valid cert for the group's app; for a self group, the same root).
1. The inviter's `invite(group)` creates an invite mailbox (its own fresh key) and registers a second fresh
key whose secret goes into the token. It also makes a one-time HPKE key pair.
2. Token: `tab1:` + base64url(`relay url ‖ invite mailbox ‖ access key secret ‖ inviter HPKE public key ‖ app`),
about 150 bytes, small enough for a QR code. It carries no identity.
3. The joiner's `join(token)` makes its MLS signature key, a group mailbox key and a KeyPackage with its
credential. It HPKE-seals the KeyPackage to the inviter's key (`info = "tabellarius invite v1"`) and posts it
to the invite mailbox. The relay never sees the credential.
4. The inviter's next `sync` opens it and checks the credential (§2). It then commits the Add to the group
mailbox, registers the joiner's mailbox key there, and posts `TIW1 ‖ seq ‖ Welcome` to the invite mailbox
(`seq`: where the joiner starts reading the group mailbox). Then it sends the group's history (§6).
5. The joiner's next `sync` reads the Welcome, checks every member, joins, and deregisters its invite key.
## 5. Sync, offline first
- `send(group, events)` seals each event in an envelope (HLC from the device clock), applies them to the local
log, encrypts one TBA1 batch per MLS message, and queues the ciphertext in the **outbox**. It never needs the
relay.
- `sync(group)`:
1. uploads the outbox;
2. pulls since the cursor; processes commits (credential checks, then merge) and application messages
(every envelope verified, then `tessera::merge`);
3. acks.
Messages it sent itself are recognised by hash and skipped. If the relay is unreachable, `sync` returns
`relay_ok: false` and changes nothing; the app shows "last synced" quietly.
- Rejected events and members come back in `SyncReport::rejected` with the reason, never half-applied.
- Group state (OpenMLS storage, keys, cursors, the log, the outbox) is saved through a `StateStore`. The file
store encrypts it (XChaCha20-Poly1305) with a 32-byte key **held in Custos**. The state never lies on disk in
the clear.
## 6. History for a new member
MLS gives a new member nothing from before it joined. A new device of yours must still see the whole log, so
after an Add the inviter sends the existing envelopes as ordinary TBA1 batches. Each envelope is still signed
by its author and verified by the receiver, and duplicates disappear in `merge`.
## 7. Removal
`remove(group, member)` commits a Remove and deregisters the member's mailbox key (it is in the member's
credential). The removed device can no longer fetch (403). Even with the ciphertext in hand, it cannot decrypt
anything from the next epoch on.
## 7a. v1.1: revocation, renewal, backup (docs/V1_1.md)
- **Revocation.**
- A `TRV1` statement (docs/TESSERA.md §12) travels as an event of kind `tessera.revoke`.
- `log()` leaves out every event a complete revocation rules out, and the revocations themselves.
- Events ruled out on arrival are reported in `SyncReport::rejected`.
- `revoke(group, statements)` announces, uploads, then removes the revoked leaves and deregisters their
mailbox keys.
- A device of the same person that sees a revocation in force removes the leaves on its next sync.
- **Who may remove whom.** A commit that removes some of another person's devices, while that person stays in
the group, needs that person's revocation in the log for each one; otherwise it is not applied. Removing
all of a person's devices removes the person, as in v0.2.
- **Renewal.**
- `renew(cert)` takes a new cert for the same subkey and commits a self-update with the new credential in
every group.
- A member's updated credential is checked like a new member's.
- A device whose cert has expired cannot be joined (the joiner checks every leaf), so renew before expiry.
- **The root-sealed backup.**
- `backup(secret)` uploads the self group's log to the backup mailbox: id = SHA-256(`tabellarius backup
v1` ‖ the access key's public key), access key from the secret's mailbox seed. It goes in 200 KiB parts,
each `seal(nonce, aad = "tabellarius backup v1", id ‖ part ‖ parts ‖ chunk)`: the relay sees ciphertext
of known sizes, no header.
- It acks the previous backups, so the relay drops them after its retention.
- An unchanged log is not uploaded again.
- `restore(secret)` returns the envelopes of the latest complete backup; `import(group, envelopes)`
verifies each at its own time and sends them into a new self group.
- **Retention:** the relay keeps every message at most 90 days, so a backup survives 90 days after its
last upload.
## 8. Known limits (v1)
- **Concurrent commits.** If two members commit in the same epoch, the one the relay ordered second is
dropped by the others. Its sender sees the conflict on the next sync, discards its own commit, and must redo
it. Day 2 tests commits sequentially.
- The relay sees **timing and sizes**: when a mailbox is written and fetched, and how large each message is.
Fetching several mailboxes from one connection links them by IP. Caddy drops access logs, but the network
path still sees it.
- A leaked invite token lets its holder join until the inviter's device accepts the first answer. One answer
per token is accepted; the token is dead afterwards.
- A browser sends a CORS preflight before each request (the URL changes with `?after=N`): twice the round
trips (docs/V1_1B.md Status).
- Live subscribe (v1.1b) tells a client *that* a mailbox has mail, never what. Native apps still poll; the
browser package listens.