diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index df6c228..46b6b8b 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -85,7 +85,8 @@ There is **no address, name, phone or email** in the record. ## Personal data (GDPR) Only non-personal data is replicated. Exact address and contact details are -released by the owner's node on request over an encrypted channel. Deletion = +released by the owner as a signed `grant` message inside an end-to-end +encrypted thread (see Messaging). Deletion = signed tombstone, honored by honest nodes, plus the owner's node ceasing to serve details. @@ -130,6 +131,167 @@ within 30 days. revocations every 30 minutes, so newcomers hear about them without waiting for a sync. +## Messaging (wire format, draft, not implemented yet) + +A browser-only buyer contacts a listing's owner, and the owner can answer and +release the exact address. Messages are **end-to-end encrypted**, short, +text-only and **anonymous towards the network**: relays see that *someone* +wrote about listing X, and when, not who or what. + +### Model + +- A **thread** is one buyer talking to one listing. The buyer's browser makes a + fresh X25519 **thread key** per thread (kept in browser storage; the + bookmarkable reply link is derived from it). It is never reused across + listings, so threads cannot be linked. +- **Buyer -> owner** messages are sealed to the listing's current `device` key. + **Owner -> buyer** messages are sealed to the thread public key. +- Both travel as an **envelope**: an unsigned, encrypted, proof-of-work-stamped + record gossiped on the listing's region topic and kept by every node that + replicates the region for at most 14 days. The owner's node collects its + envelopes on sync, including after being offline. +- Only the **owner's replies are signed** (device key + certificate), so a + buyer can tell the real owner from an impersonator. Buyer messages are + authenticated only by knowing the thread key, which only the owner learns. + +### Envelope (what is gossiped) + +Canonical JSON, integers only, max **10 KB**: + +| field | notes | +|---|---| +| `v` | 1 | +| `listing` | `{author, id}` of the listing this thread is about | +| `to` | `owner` or `buyer` | +| `rcpt` | `owner`: the listing's `device` public key (64 hex). `buyer`: the **mailbox tag** (32 hex) = first 16 bytes of `sha256("allmende-mailbox-v1\n" ‖ thread public key)` | +| `epk` | sender's fresh ephemeral X25519 public key (64 hex), new for every envelope | +| `created_at`, `expires_at` | unix seconds; `expires_at - created_at` in (0, 14 days] | +| `ct` | hex ciphertext, see below | +| `pow` | nonce; `sha256` of the canonical bytes of the envelope (including `pow`) has at least *message difficulty* leading zero bits (default **14**, lower than listings; configurable per node) | + +The envelope's identity is the sha256 of its canonical bytes; nodes dedupe on it. +The topic is `/allmende/1/messages/`, with the region taken from the +listing's geohash. + +### Sealing + +``` +rpk = X25519 public key of the recipient + (owner: the device ed25519 key converted with the standard birational map, + as libsodium's crypto_sign_ed25519_pk_to_curve25519; buyer: the thread key) +esk = fresh X25519 secret key, epk its public key +key = HKDF-SHA256(ikm = X25519(esk, rpk), salt = epk ‖ rpk, info = "allmende-seal-v1", 32 bytes) +header = canonical JSON of the envelope without `ct` and `pow` +ct = ChaCha20-Poly1305(key, nonce = 12 zero bytes, aad = header, padded plaintext) +``` + +The nonce may be fixed because every envelope uses a fresh `esk`, hence a fresh +`key`. The AAD binds listing, recipient and timestamps, so a relay cannot +alter or re-date an envelope without making it undecryptable. An all-zero +shared secret (low-order point) is rejected. Reusing the device key for both +signing and Diffie-Hellman follows libsodium practice and is separated by the +different contexts; no separate encryption key is added to certificates. + +**Padding:** plaintext = canonical JSON, then `0x80`, then zero bytes up to the +next size of 512, 1024, 2048 or 4096 bytes, so length reveals little. +Plaintext before padding is at most 4096 bytes. Relay and owner/buyer limits +are in UTF-8 bytes. + +### Plaintext (inside `ct`) + +Canonical JSON. Every message has: + +| field | notes | +|---|---| +| `t` | `text` or `grant` | +| `thread` | the thread public key (64 hex) | +| `n` | the sender's counter in this thread, from 1 (orders messages despite clock skew) | +| `sent_at` | unix seconds | + +- **`text`** (either direction): `text`, at most 1000 bytes in the buyer's first + message and 2000 bytes afterwards. No control characters (same text rules as + listings). +- **`grant`** (owner only): the signed **address release**: + `address` (<= 300 bytes), optional `phone` (<= 40), `email` (<= 120), `note` + (<= 1000, free text from the owner, e.g. "Saturday at 10, ring twice"), + `valid_until` (unix seconds, in `(sent_at, sent_at + 30 days]`). + +Every **owner -> buyer** plaintext (`text` and `grant`) also carries +`listing` (`{author, id}`), `device`, `cert` and `sig`. `sig` is the device +signature over the canonical plaintext without `sig`, with the prefix +`allmende-reply-v1\n`. Because it covers `listing` and `thread`, a reply cannot +be replayed into another thread. The browser accepts it only if `cert` is a +valid certificate of the listing's `author` for `device`, valid at `sent_at`, +and the signature verifies. As elsewhere, a browser cannot know about later +revocations; the UI says so. + +The buyer's UI shows a `grant` as a distinct card ("Address released by the +owner of this listing") and hides it after `valid_until`. Hiding is advisory: +the browser had the data and cannot be forced to forget it. + +### Relay validation (before gossiping, ciphertext only) + +1. size, canonical encoding, `v`, field formats, `to`/`rcpt` shape; +2. lifetime and expiry; a `created_at` far in the future is ignored; +3. proof-of-work at the node's message difficulty; +4. `to = owner`: the listing must be known, live (not expired or deleted) and + in this region, and `rcpt` must equal its current `device`; +5. `to = buyer`: no listing check (the owner may have deleted the listing since, + and replies must still arrive), only the checks above; +6. per-queue caps: at most 200 live envelopes per listing (`owner`) and 50 per + mailbox (`buyer`). When full, the envelope with the **least work** is + evicted (oldest first on ties), so filling a queue costs more than the + proof-of-work of the messages it displaces. + +Invalid envelopes are rejected and the relaying peer loses score. Unknown +listing, wrong `rcpt` after a device change, and clock failures are **ignored +without penalty**, as with listings. Relays **cannot** check content, so the +text rules below are enforced by endpoints. + +### Endpoint rules (owner's node and buyer's browser) + +- The owner's node trial-decrypts only envelopes addressed to its own + `device` key for its own listings, and drops anything that fails AEAD or + has a malformed plaintext. Buyer messages are shown as plain text, never as + clickable links. +- **First-message restriction:** until the owner has sent a message in a + thread, the buyer's messages must be short text with **no links**: no + `://`, no `www.`, no hostname-like `label.tld` token. The owner's node hides + violating messages (marked "contains a link") and the buyer's browser + refuses to send them. This is a heuristic against scams, not a guarantee. +- **Owner-side rate limit:** a node accepts at most N new threads per listing + per day (default 20). Surplus first messages stay queued and unread until + they expire; highest work first, then oldest. +- Messages carry **no attachments and no images**. +- The decrypted inbox is stored only on the owner's node, in its database. The + exact address is a private per-listing field there, never signed or + gossiped; "release address" pre-fills a `grant` that the owner can edit. + +### Transport (planned) + +- **Gossip** on `/allmende/1/messages/`; **sync** extends + `/allmende/1/sync` with a message stream paged by `(created_at, hash)`. +- **HTTP**: `POST /api/v1/messages` (no token; the node validates, stores and + gossips the envelope; rate limited per client) and + `GET /api/v1/messages?mailbox=&after=` for the buyer's + browser to poll. The owner's decrypted inbox and replies use the existing + admin token. +- A gateway sees the buyer's IP address and mailbox tag. Buyers can use any + gateway; nothing else about the conversation is exposed. + +### Limits of this design + +- No forward secrecy: a leaked device key reads envelopes still stored + (<= 14 days) or captured earlier. A device enrolled after recovery cannot read + older envelopes. +- Buyer storage loss loses the thread (no account to recover it). +- Only the listing's current device can read buyer messages, not the owner's + other devices. +- Everything in a thread is visible to the owner's node. Abuse reporting for + messages is not designed yet; the size cap, text-only content and link ban + keep messages from carrying images or bulk content, and the legal review + (§184b StGB, GDPR) should cover messaging before launch. + ## HTTP API (`internal/api`) Public reads serve the signed records in their **canonical encoding**, so a @@ -186,7 +348,7 @@ Proof-of-work per listing and per revocation, plus subscribable blocklists ## Open -Buyer/seller messaging (E2E, store-and-forward), reciprocity rules, anti- +Messaging implementation and abuse reporting for messages, reciprocity rules, anti- discrimination (AGG) content rules, blocklists, peer scoring and rate limits, incremental sync and anti-entropy, DHT discovery, UI publishing, image hosting, recovering listings onto a fresh machine with an empty database, a helper to