# dtchat security review (T10.2)

Date: 2026-11-17.  Scope: the code in `lib/libDtChat` and
`programs/dtchat` at commit `daa7578c83` plus the changes of this review.
Method: reading the paths below, the protocol documents, and the tests
(`test_local`, `test_filexfer`, `test_pair`, `fuzz` with 1,000,000
iterations under ASan/UBSan, `tests/p2p.sh`, the UI pairing run under
Xvfb).  Not covered: media (T6.3), XMPP (T4), the Telegram/WhatsApp
helpers (T8/T9), and any operating system other than the Linux build host.

## Findings

### 1. High, fixed: the local peer's identity was only claimed, not bound

The server asked for no client certificate, so a peer could send any `id`
and `pin` in its `hello`; the pin is public (the certificate's SHA-256).  An
attacker on the LAN could impersonate a known peer for text, files and (in
T6) calls without owning its key.

Fix: the listener asks for the peer's certificate
(`tls_config_verify_client_optional`); the certificate chain is not verified
(TOFU by pin), but the server compares the hash of the certificate that
completed the handshake (`tls_peer_cert_hash`) with the pin in `hello` and
refuses a mismatch or a missing certificate.  The client presents its own
identity certificate.  `test_local` covers matching pin (accepted), another
identity's pin (rejected), and any pin (first contact).

### 2. Medium, fixed: unpaired peers could offer files

The plan allows only text requests before pairing.  The UI manager processed
`file-offer`/`file-data`/... before the SAS was confirmed.  Fix: each peer
carries `paired`; file messages from an unpaired peer are ignored, an offer
is answered with `file-reject` reason `pairing required`, and the send hook
refuses.  Text (`msg`) stays allowed, as specified.

### 3a. Informational: the test hooks (DTCHAT_TEST_*)

`DTCHAT_TEST_TRUST` accepts an unknown pin without the SAS dialog,
`DTCHAT_TEST_AUTOANSWER` answers an incoming ring, `DTCHAT_TEST_PTT` holds
push-to-talk from the start, and `DTCHAT_TEST_VIDEO` makes the call use the
test pattern instead of the camera (two instances on one machine cannot open
the same camera).  `DTCHAT_ACCOUNTS_DEBUG` logs the account runtime's
starts, states and messages to stderr, `DTCHAT_TEST_ACCOUNTS_SEND`
(`conv|text`) sends through a just-online account, and
`DTCHAT_TEST_UI_SEND` sends through the UI's normal send path,
`DTCHAT_TEST_UI_SEND_FILE` sends a file through it,
`DTCHAT_TELEGRAM_FAKE_FILE` and `DTCHAT_WHATSAPP_FAKE_FILE` make the fake
adapters offer a real local file (exercising the read/hash/event path),
`DTCHAT_TEST_ACCOUNTS_DIALOG` opens the accounts editor on start (for the
UI test), and
`DTCHAT_TEST_FILE_ACCEPT` accepts an incoming adapter file offer without the
dialog; the last three exist for the runtime test under Xvfb.  An accepted
file is written as NAME.part (0600) in the state directory; when the
transfer ends, the part file is hashed and renamed only if the digest matches
the one in the offer, or the one in file-end when the sender could not know
it before (Telegram and WhatsApp).  A reject or a mismatch removes the part
file.  All are test-only switches in
`localui.c` and `accounts_run.c`; a normal session never sets them, and the
two-machine tests (T5.9/T7.7) run without them.

### 3. Low, accepted: the private key has no passphrase

`identity.json` holds the Ed25519 private key (PKCS#8, base64) next to the
certificate.  The store creates the directory 0700 and the file 0600,
writes atomically and moves a corrupt file to `.bad`; any process running as
the user can read it.  A passphrase or the secret service would break the
headless mode; accepted for v1 and named in the ADR's limits.

### 4. Low, accepted: pins are public, the SAS is not a secret

The pin is the certificate's hash; the 6-digit SAS is derived from both
pins and is meant to be compared out of band, not to be secret.  An attacker
who can intercept the first connection *and* the person's comparison is a
different threat model (the SAS is the out-of-band channel).  After pairing a
changed pin blocks until a new SAS confirmation.

### 5. Informational: no application-level replay protection

Local traffic runs in TLS 1.2/1.3, which provides confidentiality, integrity
and replay protection per connection; messages are not queued or replayed by
the core.  Files are additionally verified by SHA-256 end to end.  The media
protocol (T6.3) will carry its own ChaCha20-Poly1305 nonce and replay window;
until then there is no separate nonce.

### 6. Informational: the listener's default address

The UI defaults `--listen` to port 0 on 127.0.0.1; the headless default is
the same.  A LAN address must be named explicitly (`--listen 0.0.0.0:PORT`),
so the default does not expose dtchat beyond the machine.

### 7. Informational: adapter processes

Adapters are separate processes started with `execl(path, path, "--stdio")`
— no shell, no command line interpolation.  stdout is the JSON protocol,
stderr is drained and discarded (not logged).  A line over 1 MiB, a NUL or
invalid JSON ends the child; the manager restarts with 2..60 s backoff unless
the adapter said `bye`.  Secrets arrive in `configure` only.

### 8. Informational: secrets in the UI and logs

The account editor stores secrets in a separate JSON object and shows only
`*` characters (the real value never leaves the masked field); the status
line and the transcript never print a secret.  The IRC adapter sends `PASS`
and the SASL payload only inside TLS and never logs them.  The headless
events carry no secrets.

### 9. Informational: file names and limits

The receiver takes the basename, drops `..`, `.`, `/`, `\` and control
characters, truncates to 200 bytes, writes `NAME.part` with mode 0600,
enforces 512 MiB, verifies size and SHA-256 and renames without overwriting
(`-1`, `-2`, ...).  Base64 decoding is length-checked; the fuzzer covers the
JSON and IRC parsers, not the file decoder (its input is bounded by the
receive buffer).

### 10. Informational: QR decoding

The request dialog accepts a base64 bitmap of at most 200x200 modules, checks
that enough bytes arrived and draws at most 2400x2400 pixels into a depth-1
pixmap.  No QR library, no terminal escape output.

### 11. Informational: parser limits

JSON: 1 MiB per line, depth 32, duplicate keys rejected.  IRC: 1 MiB line,
NUL rejected, at most 15 parameters.  The fuzzer runs 1,000,000 generated and
mutated inputs with ASan/UBSan; no crash, no leak.

## Open items for later phases

- The media protocol's nonce/replay window (T6.3), the SRTP-like header and
  the key agreement are not written yet; review them with T6.
- XMPP stanzas, TXT records and QR inputs get their own fuzz seeds when their
  parsers land (T4, T5.2).
- Telegram/WhatsApp helpers are separate processes; their secret stores and
  update paths are theirs to review.
- A field test on two real machines (T5.9) should confirm the listener's
  address choice, firewall behaviour and the SAS comparison with a person.

<!-- License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. -->
