diff --git a/lib/libDtChat/local.c b/lib/libDtChat/local.c index 906aa12b..4cc9de6f 100644 --- a/lib/libDtChat/local.c +++ b/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/lib/libDtChat/test_local.c b/lib/libDtChat/test_local.c index cadee0ae..7e18a63e 100644 --- a/lib/libDtChat/test_local.c +++ b/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/programs/dtchat/localui.c b/programs/dtchat/localui.c index 23f96bf3..96f0adc2 100644 --- a/programs/dtchat/localui.c +++ b/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));