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
- The server orders commits by epoch. A membership change is accepted only if made in the chat's current epoch; the loser of two simultaneous changes retries after catching up. Everything else about agreement is MLS's job.
- A device never reads its own entries back. openmls does not return one's own application message as a message, so clients skip log entries whose sender device is themselves and show their own line from their own copy. (An early version reported them as "membership changed", which hid this.)
- Except its own commit, when the server's answer to it was lost. The
server answers a commit before it can know the answer arrived, so a phone
that suspends in between has a commit the chat took and a device that never
applied it. On reconnect the replayed log is the only proof: a device that
still holds a pending commit and finds its own commit in that epoch applies
it (
Device::own_commit,Engine::on_own_commit); a replay that ends with the cursor unmoved and the commit still pending means the server never took it, and it is dropped. Skipping it instead left the device in the old epoch for good, sealing every line with keys the device it had just added never had (TooDistantInThePaston every line, 2026-10-16). Nothing is sealed while a commit is pending (Engine::held): until it settles the device cannot know which epoch the chat is in. - Entries are applied strictly in order. A gap means a line was dropped (the
text backlog is bounded,
chat::TEXT_BACKLOG); the client asks again from its cursor instead of skipping, because a skipped commit breaks the chat for good. - Blocks withhold messages, never commits, for the same reason.
- A key package opens one invitation. The server deletes each one it hands out; devices keep a stock of 20 and top up on connect or when told they are below 5. Claims are throttled per connection so nobody's stock can be drained.
Keys, backup, trust (decided 2026-09-26)
- Keys live only on the device, in the Data namespace of
platform::storage(e2ee/<server>/<user>.bin), preloaded on the web because IndexedDB has no synchronous read. Losing that blob loses every private chat on the device. - History on a new device: encrypted backup unlocked by a recovery key.
Built for the web chat (next section); the app neither writes nor opens one
yet. Without it, a new device is added to its chats (below) and reads what a
member's device shares when it adds it (
communities.md), or nothing. - Web client: encrypted like the apps, with a note that a browser runs whatever code the server sends it.
- Identity proofs are out of scope for now. The server vouches for each
person's full address (
name@server, protocol 37,web-chat.md) at sign-in and nothing checks further; a key-change warning is the only defence against a swapped key until proofs or in-person verification exist.
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).
- The key: 120 random bits as 24 Crockford base-32 characters, shown once.
The backup key is HKDF-SHA256 of it and the backup ChaCha20-Poly1305 with a
fresh nonce each seal (
e2ee::backup_key,seal_with_key). No slow hash: the secret is random, never chosen, so guessing it is hopeless regardless. The mockup's 12 words (docs/mockups/chat-world/keys-and-trust.md) would need a word list in the wasm; the characters carry the same 120 bits. - No MLS keys in it, and no account key: a new device still joins each chat the usual way, so contacts are not spared a new device, and a chat whose every member has lost every device stays readable but can never be written to again.
- Re-sealed a minute after a change (
BACKUP_AFTER_MS), by every device of the account that has the key; the last write wins, and the server deletes the file it replaces. Two devices of one person hold nearly the same chats, so either's copy will do. - The device keeps its copy of the key (IndexedDB,
recovery|<account>), as it keeps its private keys: whoever has the device has the chats anyway. - Not built: replacing a lost key from a signed-in device (making a new one on a device with no key replaces the backup, and the old key with it), pictures inside the backup (their keys are there; the files stay in the picture store only while it keeps them), and the app.
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.
- The server tracks which devices hold which chat (
e2ee_group_devices, V30), from what it already sees: the device that registered the chat, the devices an invitation names, a device that writes to it, and every cursor a device syncs with. The last is what backfilled chats from before the table. - After every sync and every accepted commit it looks for member devices
not holding the chat (
e2ee::missing_devices), claims one key package each, and sendsE2eeAddDevicesto a device that does hold it: the asking one if it can, otherwise any that is connected. Nobody holding it online means it waits for the next one to sync. - An ask is not repeated for two minutes (
ASK_AGAIN_MS), because each spends one of the missing device's key packages. Devices not seen for 30 days are not added: a browser whose storage was cleared would otherwise collect a member slot in every chat for ever. - A device adds only devices of people already in the chat
(
Device::add_member_devicesdrops any other key package). The request comes from the server, and this is what keeps it from being a way to add a stranger. The server could always hand out a key package of its choosing when a chat starts, so this is no weaker than that. A device already in the chat is replaced, not skipped (next section). - A device that joins this way is labelled by who else is in the chat, not by who invited it: when your own laptop adds your phone, the inviter is you. A group's name is sent again after the add, as for a newcomer.
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:
- The server spots it. Every
E2eeSendcarries its epoch. A line from a past epoch whose ending commit is older thane2ee::BEHIND_AFTER_MS(2 min; younger is a device writing as a commit lands, a race) is refused withcartopolis_core::E2EE_BEHIND, and the device's hold, ask and any stale invitation for the chat are deleted. - The device drops the chat's keys and cursor on that refusal, keeping its
label, and syncs without it (
Engine::drop_to_rejoin). - The usual missing-device path does the rest: the sync makes the server
ask a device that holds the chat to add it, and
add_allowedtakes the stale leaf out in the same commit that adds the fresh key package (openmls's commit builder, as itsswap_membersdoes). The invitation joins it from there, and the adder shares the history as for any new device. - A device that sees a commit take it out (
Device::is_activefalse) drops the chat the same way, so a replacement it did not ask for, a server bug say, also ends in a rejoin rather than a device locked out.
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.
- The filter runs on the receiving device (
private_chat::PrivateChats::filter,cartopolis_core::Filter::seeded). It uses the built-in word list only: an operator's additions are server config and never reach a client. - Block and account deletion work as for the rest of chat (blocks are enforced by the server, which sees the sender).
- A report carries the reported private line from the reporter's device: the name menu on a private line opens the ordinary report with that line attached, as it does for nearby chat. The server cannot check it was really said. Message franking would make it checkable; not built.
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".
- A point is an ActivityStreams Note with a Place from day one, so the same object can be posted publicly over ActivityPub later without converting encrypted points on every phone.
- A shared real location ends on the devices. The server cannot read it,
so it cannot end it: the sender stops at
until_ms, and every receiver drops the position atuntil_mswhatever it last heard. The sender sends on the first fix, then at most every 10 s and only after moving 15 m, and at least once a minute (private_chat::share_due). - A share is never on without a sign of it. While any is live, a chip at
the top of the map names who can see the phone and has Stop, drawn even in
driver mode (
place_pins::sharing_chip). - Colours mean one thing each: a chat's points are the brand orange, a
person's real location is
theme::REAL_LOCATIONblue. - Desktop has no location provider, so sharing from it says so and stops.
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.