diff --git a/docs/reference/dtchat-local-protocol.txt b/docs/reference/dtchat-local-protocol.txt index 0b461722c8..64868e1bce 100644 --- a/docs/reference/dtchat-local-protocol.txt +++ b/docs/reference/dtchat-local-protocol.txt @@ -9,7 +9,11 @@ itself, two dtchat instances on the same LAN, not a protocol helper process. - TLS over TCP, one connection per peer. The server certificate is the instance's self-signed Ed25519 X.509 certificate (`identity.c`, - `identity.json`, CN `dtchat`, ten years). + `identity.json`, CN `dtchat`, ten years). The client presents its own + identity certificate as well: the server asks for it (`verify_client` + optional) and compares the hash of the certificate that completed the + handshake with the pin in `hello`, so the claimed identity is bound to a + key. The chain is not checked (TOFU by pin), only the key's possession. - The pin is the SHA-256 of the DER certificate, written `sha256:<64 lowercase hex>`; it is the same value the certificate's `tls_peer_cert_hash()` produces (`SHA256:...` normalised). @@ -26,10 +30,12 @@ are configured by `--peer HOST:PORT` or the peer model. ## 2. Connection sequence -1. TCP connect, TLS handshake. The client does not verify the CA chain with - libtls (`insecure_noverifycert/noverifyname`); it verifies the pin from the +1. TCP connect, mutual TLS handshake (both sides present their identity + certificate). The client does not verify the CA chain with libtls + (`insecure_noverifycert/noverifyname`); it verifies the pin from the server's `hello-ack` against the certificate it actually saw - (`tls_peer_cert_hash`), which is the TOFU decision. + (`tls_peer_cert_hash`), which is the TOFU decision. The server does the + same with the client's pin in `hello`. 2. The client sends `hello`: ```json {"type":"hello","id":"","pin":"sha256:"} @@ -64,6 +70,10 @@ All are JSON lines. Unknown types are ignored (forward compatibility). | `file-done` | receiver → sender | `id`, `ok` | | `file-cancel` | both | `id`, optional `reason` | +File messages are only accepted from a peer whose pin is paired (known and +equal); before the SAS confirmation an offer is answered with `file-reject` +and reason `pairing required`. Text messages are allowed before pairing. + A file transfer is one offer id. The receiver writes `NAME.part`, checks the size and the SHA-256, and renames to a sanitized basename (no `/`, `\`, `..`, control characters) without overwriting (suffix `-1`, `-2`, ...). The limit diff --git a/docs/reference/dtchat-security-review.txt b/docs/reference/dtchat-security-review.txt new file mode 100644 index 0000000000..31a9d1ceca --- /dev/null +++ b/docs/reference/dtchat-security-review.txt @@ -0,0 +1,114 @@ +# 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. + +### 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. + + diff --git a/ext/src/mcde/lib/libDtChat/local.c b/ext/src/mcde/lib/libDtChat/local.c index 906aa12bba..4cc9de6f4d 100644 --- a/ext/src/mcde/lib/libDtChat/local.c +++ b/ext/src/mcde/lib/libDtChat/local.c @@ -59,6 +59,7 @@ struct DcLocalConn { size_t outoff; char peer_id[128]; char peer_pin[128]; + char cert_pin[128]; char own_pin[128]; char own_id[128]; char *expect_id; @@ -109,6 +110,7 @@ struct DcLocalPeer { int stopped; }; +static const char *dc_local_conn_peer_cert_pin(const DcLocalConn *conn); static void conn_pump(DcLocalConn *conn); static void conn_cb(int fd, short revents, void *arg); static void peer_connect(DcLocalPeer *peer); @@ -361,11 +363,19 @@ dispatch_line(DcLocalConn *conn, const char *line) *id == '\0' || pin == NULL) { conn->hello_ok = 0; } else { + char certpin[65], claimed[65]; + snprintf(conn->peer_id, sizeof(conn->peer_id), "%s", id); snprintf(conn->peer_pin, sizeof(conn->peer_pin), "%s", pin); conn->hello_ok = 1; + /* bind the claimed identity to the TLS key */ + if (normalize_pin(conn->peer_pin, claimed) != 0 || + normalize_pin(dc_local_conn_peer_cert_pin(conn), + certpin) != 0 || + strcmp(claimed, certpin) != 0) + conn->hello_ok = 0; if (conn->expect_id != NULL && strcmp(conn->expect_id, id) != 0) conn->hello_ok = 0; @@ -516,10 +526,14 @@ conn_pump(DcLocalConn *conn) char normalized[65]; if (normalize_pin(hash, - normalized) == 0) + normalized) == 0) { snprintf(conn->peer_pin, sizeof(conn->peer_pin), "%s", normalized); + snprintf(conn->cert_pin, + sizeof(conn->cert_pin), + "%s", normalized); + } } } if (conn->is_client) { @@ -771,6 +785,13 @@ dc_local_server_new(DcLoop *loop, const DcIdentity *identity, const char *host, (const uint8_t *)dc_identity_key_pem(identity), strlen(dc_identity_key_pem(identity))) != 0) goto fail; + /* + * Ask for the peer's certificate so the pin in its hello is bound to + * the key that completed the handshake. The chain is not verified + * (TOFU by pin), hence no CA and noverifycert. + */ + tls_config_verify_client_optional(server->cfg); + tls_config_insecure_noverifycert(server->cfg); server->ctx = tls_server(); if (server->ctx == NULL || tls_configure(server->ctx, server->cfg) != 0) goto fail; @@ -943,6 +964,15 @@ dc_local_connect(DcLoop *loop, const DcIdentity *identity, const char *host, goto fail; tls_config_insecure_noverifycert(cfg); tls_config_insecure_noverifyname(cfg); + if (tls_config_set_cert_mem(cfg, + (const uint8_t *)dc_identity_cert_pem(identity), + strlen(dc_identity_cert_pem(identity))) != 0 || + tls_config_set_key_mem(cfg, + (const uint8_t *)dc_identity_key_pem(identity), + strlen(dc_identity_key_pem(identity))) != 0) { + tls_config_free(cfg); + goto fail; + } conn->tls = tls_client(); if (conn->tls == NULL || tls_configure(conn->tls, cfg) != 0) { tls_config_free(cfg); @@ -988,6 +1018,12 @@ dc_local_conn_peer_pin(const DcLocalConn *conn) return conn != NULL ? conn->peer_pin : NULL; } +static const char * +dc_local_conn_peer_cert_pin(const DcLocalConn *conn) +{ + return conn != NULL ? conn->cert_pin : NULL; +} + /* ------------------------------ the peer link ---------------------------- */ static void diff --git a/ext/src/mcde/lib/libDtChat/test_local.c b/ext/src/mcde/lib/libDtChat/test_local.c index cadee0aecb..7e18a63ee9 100644 --- a/ext/src/mcde/lib/libDtChat/test_local.c +++ b/ext/src/mcde/lib/libDtChat/test_local.c @@ -43,8 +43,7 @@ mkidentity(char *base, size_t baselen, char *dir, size_t dirlen, /* -------------------- T5.4: the blocking TLS client --------------------- */ struct ClientArg { - const char *id; - const char *pin; + DcIdentity *identity; int port; int done; int ack_ok; @@ -65,7 +64,8 @@ client_thread(void *arg) snprintf(port, sizeof(port), "%d", c->port); snprintf(hello, sizeof(hello), "{\"type\":\"hello\",\"id\":\"%s\",\"pin\":\"%s\"}\n", - c->id, c->pin); + dc_identity_public_b64(c->identity), + dc_identity_pin(c->identity)); if (tls_init() != 0) goto out; cfg = tls_config_new(); @@ -73,6 +73,15 @@ client_thread(void *arg) goto out; tls_config_insecure_noverifycert(cfg); tls_config_insecure_noverifyname(cfg); + if (tls_config_set_cert_mem(cfg, + (const uint8_t *)dc_identity_cert_pem(c->identity), + strlen(dc_identity_cert_pem(c->identity))) != 0 || + tls_config_set_key_mem(cfg, + (const uint8_t *)dc_identity_key_pem(c->identity), + strlen(dc_identity_key_pem(c->identity))) != 0) { + tls_config_free(cfg); + goto out; + } ctx = tls_client(); if (ctx == NULL || tls_configure(ctx, cfg) != 0) { tls_config_free(cfg); @@ -195,44 +204,43 @@ hello_cb(DcLocalConn *conn, const DcJson *hello, void *arg) } static void -run_case(const char *want_id, const char *want_pin, const char *send_id, - const char *send_pin, int expect_ok, int expect_cb) +run_case(DcIdentity *client, const char *expect_id, const char *expect_pin, + int expect_ok, int expect_cb) { char base[256], dir[512]; DcIdentity *identity; DcLoop *loop; DcLocalServer *server; struct ServerCtx ctx; - struct ClientArg client; + struct ClientArg carg; pthread_t thread; long long deadline; memset(&ctx, 0, sizeof(ctx)); - memset(&client, 0, sizeof(client)); + memset(&carg, 0, sizeof(carg)); CHECK(mkidentity(base, sizeof(base), dir, sizeof(dir), &identity) == 0); if (identity == NULL) return; loop = dc_loop_new(); CHECK(loop != NULL); - server = dc_local_server_new(loop, identity, "127.0.0.1", 0, want_id, - want_pin, hello_cb, NULL, NULL, &ctx); + server = dc_local_server_new(loop, identity, "127.0.0.1", 0, expect_id, + expect_pin, hello_cb, NULL, NULL, &ctx); CHECK(server != NULL); if (server == NULL) return; - client.id = send_id; - client.pin = send_pin; - client.port = dc_local_server_port(server); - CHECK(pthread_create(&thread, NULL, client_thread, &client) == 0); + carg.identity = client; + carg.port = dc_local_server_port(server); + CHECK(pthread_create(&thread, NULL, client_thread, &carg) == 0); deadline = dc_loop_now_ms() + 5000; - while (!client.done && dc_loop_now_ms() < deadline) + while (!carg.done && dc_loop_now_ms() < deadline) (void)dc_loop_run(loop, 50); pthread_join(thread, NULL); - CHECK(client.ack_seen); - CHECK(client.ack_ok == expect_ok); + CHECK(carg.ack_seen); + CHECK(carg.ack_ok == expect_ok); if (expect_cb) { CHECK(ctx.called == 1); - CHECK(strcmp(ctx.id, send_id) == 0); - CHECK(strcmp(ctx.pin, send_pin) == 0); + CHECK(strcmp(ctx.id, dc_identity_public_b64(client)) == 0); + CHECK(strcmp(ctx.pin, dc_identity_pin(client)) == 0); } else { CHECK(ctx.called == 0); } @@ -487,22 +495,32 @@ test_reconnect(void) int main(void) { + char base_c[] = "/tmp/opencode/dtchat-local-c-XXXXXX"; + char base_o[] = "/tmp/opencode/dtchat-local-o-XXXXXX"; + char dir_c[512], dir_o[512]; + DcIdentity *client; + DcIdentity *other; + signal(SIGPIPE, SIG_IGN); - run_case("peer1", - "sha256:1111111111111111111111111111111111111111111111111111111111111111", - "peer1", - "sha256:1111111111111111111111111111111111111111111111111111111111111111", - 1, 1); - run_case("peer1", - "sha256:2222222222222222222222222222222222222222222222222222222222222222", - "peer1", - "sha256:1111111111111111111111111111111111111111111111111111111111111111", - 0, 0); - run_case(NULL, NULL, "peer2", - "sha256:1111111111111111111111111111111111111111111111111111111111111111", - 1, 1); + CHECK(mkdtemp(base_c) != NULL); + CHECK(mkdtemp(base_o) != NULL); + snprintf(dir_c, sizeof(dir_c), "%s/state", base_c); + snprintf(dir_o, sizeof(dir_o), "%s/state", base_o); + client = dc_identity_load_or_create(dir_c); + other = dc_identity_load_or_create(dir_o); + CHECK(client != NULL && other != NULL); + if (client == NULL || other == NULL) + return 1; + + run_case(client, dc_identity_public_b64(client), + dc_identity_pin(client), 1, 1); + run_case(client, dc_identity_public_b64(client), + dc_identity_pin(other), 0, 0); + run_case(client, NULL, NULL, 1, 1); test_protocol(); test_reconnect(); + dc_identity_free(client); + dc_identity_free(other); if (fails != 0) { fprintf(stderr, "%d test(s) failed\n", fails); return 1; diff --git a/ext/src/mcde/programs/dtchat/localui.c b/ext/src/mcde/programs/dtchat/localui.c index 23f96bf384..96f0adc247 100644 --- a/ext/src/mcde/programs/dtchat/localui.c +++ b/ext/src/mcde/programs/dtchat/localui.c @@ -40,6 +40,7 @@ struct Peer { DcFileRecv *recv; char recv_id[64]; int blocked; + int paired; Peer *next; }; @@ -197,6 +198,7 @@ pair_accept(PairCtx *ctx) ctx->name[0] != '\0' ? ctx->name : p->name); snprintf(p->pin, sizeof(p->pin), "%s", ctx->pin); p->blocked = 0; + p->paired = 1; announce(ctx->local, p, 1); } status(ctx->local, "peer paired"); @@ -498,6 +500,30 @@ handle_line(DtLocal *local, Peer *peer, DcLocalConn *conn, const DcJson *json) if (type == NULL || peer == NULL) return; + if (!peer->paired && + (strcmp(type, "file-offer") == 0 || + strcmp(type, "file-data") == 0 || + strcmp(type, "file-end") == 0 || + strcmp(type, "file-cancel") == 0 || + strcmp(type, "file-accept") == 0 || + strcmp(type, "file-done") == 0)) { + if (strcmp(type, "file-offer") == 0) { + DcJson *rej = dc_json_new_object(); + + (void)dc_json_object_set(rej, "type", + dc_json_new_string("file-reject")); + (void)dc_json_object_set(rej, "id", + dc_json_new_string(dc_json_string( + dc_json_get(json, "id")))); + (void)dc_json_object_set(rej, "reason", + dc_json_new_string("pairing required")); + if (conn != NULL) + (void)dc_local_conn_send_json(conn, rej); + dc_json_free(rej); + status(local, "file offer refused: peer is not paired"); + } + return; + } if (strcmp(type, "msg") == 0) { const char *text = dc_json_string(dc_json_get(json, "text")); @@ -585,6 +611,7 @@ server_hello(DcLocalConn *conn, const DcJson *hello, void *arg) peer->conn = conn; snprintf(peer->pin, sizeof(peer->pin), "%s", pin); if (known == 1) { + peer->paired = 1; announce(local, peer, 1); } else if (known == 0) { pair_dialog(local, peer->name, id, pin); @@ -648,6 +675,7 @@ link_state(DcLocalPeer *link, int online, void *arg) peer->conn = conn; known = dc_pins_check(local->pins, id, pin); if (known == 1) { + peer->paired = 1; announce(local, peer, 1); } else if (known == 0) { pair_dialog(local, peer->name, id, pin); @@ -688,6 +716,10 @@ local_file_send(const char *peer_id, const char *path, void *arg) status(local, "peer is offline"); return; } + if (!peer->paired) { + status(local, "peer is not paired"); + return; + } snprintf(peer->send_id, sizeof(peer->send_id), "f%d", ++local->seq); peer->send = dc_file_send_new(peer->conn, peer->send_id, path, err, sizeof(err));