Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ADR-0002: Wire protocol: TLS 1.3 over TCP 7360, length-prefixed protobuf

  • Status: accepted
  • Date: 2026-11-08
  • Deciders: maintainer, after the November 2026 research round
  • Tags: protocol, security

Context

Tether needs a transport that works on a home LAN today, through a self-hosted relay later (ADR-0010), and that can carry small control messages, medium blobs (icons, thumbnails, attachments) and, from M4, latency-sensitive video, audio and input streams. Both peers are authenticated by their own certificates (ADR-0003); there is no CA and no cloud.

We evaluated inheriting an existing protocol rather than designing one.

KDE Connect is the obvious candidate: mature, GPL, well-documented plugin payloads. Its transport, however, is a poor fit:

  • LAN-only by design: identity packets are UDP broadcasts on port 1716 and there is no concept of a connection that did not originate from a broadcast. Adding a relay would mean forking the core.
  • It needs a range of about 50 ports (TCP 1716-1764 for connections, plus per-transfer payload ports) so that several transfers can run at once. Firewall setup is the most common support question in its community, and a single ufw application profile cannot express it cleanly.
  • TLS is trust-on-first-use: the pairing step shows a key fingerprint but nothing forces the user to compare it, so a LAN attacker present at first pairing wins.
  • Its network-facing code accumulated several CVEs in 2025 (packet handling and plugin input validation in kdeconnectd), a reminder that a large legacy surface written for a friendly LAN is a liability once the network is hostile.
  • Packets are newline-delimited JSON with ad-hoc payload-transfer side channels; there is no schema we could share between Kotlin and Rust.

Its plugin payload schemas (notifications, SMS, telephony, mpris, battery) remain an excellent reference for which fields matter and are credited where we borrowed field semantics.

QUIC was the other serious option. It gives multiplexed streams without head-of-line blocking, connection migration across Wi-Fi/cellular and 0-RTT, all of which would help a relay path and M4 media. But as of November 2026:

  • There is no mature, embeddable QUIC implementation for Kotlin/Android that does not drag in Cronet (Google-shipped, Play-adjacent) or an NDK build of quiche/msquic/ngtcp2 with our own JNI; Netty’s QUIC codec is still incubator grade. We would be maintaining a QUIC binding on the phone.
  • On a LAN, TCP’s head-of-line blocking only matters between streams, which we solve with additional connections, and bandwidth/latency are not the bottleneck.
  • rustls supports TLS 1.3 over TCP everywhere we need it; Android’s Conscrypt supports TLS 1.3 since Android 10.

Protobuf vs alternatives. JSON has no cross-language schema. CBOR/MessagePack are compact but schemaless. gRPC brings HTTP/2 framing, a heavy Android client and an RPC model that fits request/response but not the event-stream shape of notifications and battery updates, and makes custom certificate pinning awkward on Android. Protobuf has first-class Kotlin (Wire) and Rust (prost) code generators, unknown-field forward compatibility and a oneof that maps exactly onto “one frame = one message”.

Decision

  • Transport: TCP, fixed port 7360 on both desktop and phone; both sides listen and either may dial. TLS 1.3 only, ALPN tether/1, mutual certificates verified by the rules in ADR-0003. No plaintext mode, ever.
  • Framing: each frame is a u32 big-endian length (max 4 MiB) followed by one tether.v1.Envelope. Envelope.seq starts at 1 per sender and increments per frame; receivers log gaps but never reject on them.
  • Messages: every message is defined in proto/tether/v1/*.proto and carried in Envelope.msg (oneof). New features add messages; they never change the meaning of existing field numbers. protocol_version in Hello is bumped only on incompatible change.
  • Capabilities: Hello.capabilities lists feature strings (battery, notifications, notifications.reply, blobs, …). A feature is active only when both sides advertise what it needs, so old and new builds interoperate.
  • Keepalive: Ping every 15 s when idle; drop after 45 s of silence; reconnect with exponential backoff 1 s to 60 s.
  • Control plus secondary connections: the first connection is the control channel. For bulk data the sender opens another TLS connection to the same peer (same verification), sends one StreamOpen{purpose, ref_id, index, size} frame and then raw bytes. This gives per-transfer flow control and avoids blocking notifications behind a file or video stream. Inline blobs are capped at 256 KiB; anything larger goes through a stream.
  • Relay-agnostic: a “connection” is any byte stream carrying this TLS session. The relay (ADR-0010) forwards opaque TLS bytes and never terminates TLS. Nothing in core may assume the peer address is on the local subnet.
  • QUIC is deferred, not rejected. The framing is transport-independent; QUIC can be added as a second transport (streams map to secondary connections) when a maintainable Android implementation exists.

Normative details live in docs/reference/protocol.md.

Consequences

Positive

  • One port for everything: a single ufw profile, one router rule, one line of onboarding text.
  • Shared schemas: the Kotlin and Rust types are generated from the same files and CI fails if either side forgets to regenerate.
  • Mutual TLS 1.3 with pinned keys gives confidentiality and authentication without any PKI or account.
  • Secondary connections keep M1 simple (not even used) while giving M2 files and M4 media a clean home.

Negative / risks

  • Both peers must implement both TLS roles and the connection-dedupe rule (keep the connection dialled by the lexicographically smaller device id). Mitigation: shared state-machine description in protocol.md and an in-process two-endpoint test on each side.
  • A custom protocol gets no third-party clients for free. Accepted: the schema is public and GPL; anyone can implement it.
  • 4 MiB frame cap means large payloads must use streams, which is more code than “just put it in the message”. Accepted deliberately.
  • TCP over a lossy Wi-Fi link will show head-of-line stalls inside a single video stream in M4. Mitigation: per-stream connections, and QUIC later if measured latency demands it.
  • Fixed port can collide with another service. Mitigation: port is configurable in both apps and advertised via mDNS/beacon/QR; 7360 is the default, not an assumption.

Alternatives considered

  • Adopt KDE Connect’s protocol (and interoperate with kdeconnectd): rejected for the transport reasons above; interoperability would also force us to carry its TOFU pairing model.
  • QUIC from day one: rejected for lack of a maintainable Android stack; kept as a planned second transport.
  • WebSocket/HTTP-based protocol: adds HTTP framing for nothing, and Android HTTP clients fight custom certificate verification. Rejected.
  • gRPC: rejected for client weight, HTTP/2 overhead and awkward pinning.
  • Noise protocol framework instead of TLS: smaller and elegant, but fewer audited implementations on Android and no hardware-key integration path. Rejected.
  • Dynamic/ephemeral ports or a port range: rejected (firewall UX, see KDE Connect).