garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/notes/encrypted-chat.md

raw


topic: encrypted-chat triggers: [e2ee, cartopolis_e2ee, private_chat.rs, "src/e2ee.rs", openmls, MLS, "E2ee", "private chat", "direct message", "key package", "recovery key", V20__private_chats.sql, crates/e2ee, crates/chat, crates/simulator/Cargo.toml] updated: 2026-11-17

Private chats are end-to-end encrypted, and the server is only a relay

Direct messages and private groups are sealed with MLS (RFC 9420) on the devices. The simulator stores and delivers the sealed bytes and cannot open them: it does not link cartopolis_e2ee, the only crate that could (the simulator's tests link it, to drive real devices). Keep it that way: not containing the code that could open a message is how the server stays unable to. The same holds for cartopolis_chat (the engine, which links e2ee; today the simulator does not link it even in tests) and for cartopolis_calls' client feature (calls.md). Design and screens: docs/mockups/chat-world/. The web UI runs the same engine in a worker: web-chat.md.

What the server can and cannot see

It cannot see anything a private chat says: text, and later points, their pictures and shared real locations. It can see who has which devices, who is in which chat, and when each entry was written and by whom (e2ee_* tables, V20). That metadata is visible to the operator and is not hidden.

Here (nearby) and Global are not encrypted (public rooms were removed with protocol 39, communities.md) and cannot be: the server picks who hears a nearby line by where people stand. The UI must say which kind a chat is: every private chat's label starts with a padlock (private_chat::private_label, icons::LOCK), and nothing else carries one.

Why openmls with the pure-Rust provider

Measured 2026-09-26: openmls 0.9 + openmls_rust_crypto builds for aarch64-linux-android (NDK clang only) and for wasm32-unknown-unknown with the web build's flags, with no cc-built sources on either. The same constraint chose rustls over native-tls and ADPCM over Opus. Rejected: libsignal (not on crates.io, heavy native dependencies, and Signal's protocol has no standard group layer; MLS does). The web build needs getrandom 0.2's js feature for rand_core under the RustCrypto curves (crates/e2ee/Cargo.toml says so); without it the wasm build stops at getrandom.

The rules the whole thing rests on

Keys, backup, trust (decided 2026-09-26)

Recovering chats with a recovery key (2026-11-17)

A device that has a recovery key keeps a backup of every chat it holds: the newest MAX_HISTORY (300) messages each, with each chat's label, sealed (cartopolis_chat::backup) and stored in the picture store. The account row names it (accounts.chat_backup, V46; GET/PUT /api/pictures/backup, under that prefix because the public host routes it already). A new device signed in to an account that has one offers to open it; the key brings each chat's messages back in front of what the device reads itself, marked "from your backup", and lists the chats it is not in yet as readable, with no composer, until a member's device adds it (the section below).

Every device of every member (protocol 39)

A chat is joined by devices, and until 2026-10-10 only the devices that had a key package when the chat started were ever in it. A phone signed in after a chat was started on a browser was in none of them: it heard nothing, and starting the chat again found the existing one and said so. The server cannot fix that itself, since only a device in the chat can commit somebody in, so it finds who is missing and asks one that is.

A device that fell behind (2026-10-18)

A device that misses a commit can never catch up: every later line is sealed in an epoch it has no keys for, and what it seals is TooDistantInThePast to everyone else once the chat is more than MAX_PAST_EPOCHS (3) ahead. The viberfox/jeroen direct chat ended like that: the bot heard nothing jeroen wrote and jeroen saw nothing the bot sent, with only a warning in a log. Ways in: a commit that fails to apply, a lost own commit (OwnCommit::Lost), and until 2026-10-18 a kill between sending and saving (drive_private_chats now saves first).

The repair needs no new message:

Limits: a stuck device that only reads is noticed only when it next sends, and an older client shows the refusal but does not drop the chat (its next sync holds it again), so it needs the update. What was said while it was out reaches it only through the shared history.

Moderation in encrypted chats

The server-side word filter (net.rs's ChatSend arm) and reports that keep the reported line server-side (user-safety.md) cannot work on sealed text.

Points and real-location sharing ride inside the same sealed messages

cartopolis_e2ee::Payload is append-only (postcard numbers variants by position): Text, then Object (an ActivityStreams JSON object, today always a point, cartopolis_core::point), then Location / LocationStopped. An older build meets a newer variant as "a message this version can't display".

Groups

A group is created with its members and its name in one go: the creator asks for every member's key packages at once, creates the chat, adds everybody who has a device in one commit, and then sends the name as the chat's first sealed message (Payload::GroupName), so the server never learns what a group is called. Members with no device are named back to the creator as "not added".

Key-package claims are a bucket (net::CLAIM_BURST, refilling every MIN_CLAIM_INTERVAL) rather than a fixed gap, because a group claims for every member at once; past the bucket the server answers 429 with a reason, never silence, since an empty answer reads as "they have no app that can chat privately". Adding or removing people after a group starts is not built.