dtchat: add the pairing SAS and the TOFU pin store (T5.4a)

pair.c derives the 6-digit SAS from both certificate pins (sorted, SHA-256, mod 1000000) so both sides compute the same code; peers.json keeps id -> pin and the blocked flag (0600, atomic store). dc_pins_check answers 1 for a known pin, 0 for an unknown id and -1 for a changed pin; only dc_pins_confirm stores a new pin after the person compared the SAS; a changed pin stays blocked until then. test_pair: known SAS answers (448183 for A+B, 500498 for A+A), order independence, malformed pins, empty store, confirm/check, changed pin, block persistence, save/reload. Standalone and ASan/UBSan clean. make mcde-host still blocked by pid 2126511.
This commit is contained in:
lotherk committed 2026-11-16 20:46:19 +00:00
1 parent dc44fc25a3
commit e9d8d9df4c
4 files changed
+443 -3

No files matched your search

+6 -3
View File
@@ -11,14 +11,14 @@ libDtChatIpc_la_LDFLAGS = -version-info 1:0:0
libDtChat_la_CPPFLAGS = -I$(top_srcdir)/include
libDtChat_la_SOURCES = core.c loop.c store.c conv.c text.c adapter.c instance.c \
irc.c identity.c peers.c local.c filexfer.c dc_internal.h
irc.c identity.c peers.c local.c filexfer.c pair.c dc_internal.h
libDtChat_la_LIBADD = libDtChatIpc.la $(CRYPTOLIB) $(LIBTLS)
libDtChat_la_LDFLAGS = -version-info 1:0:0
# Built by make mcde-host as well, so tools/mcde-tests.sh finds them in
# build/mcde-host; make check still builds and runs them.
noinst_PROGRAMS = test_json test_frame test_loop test_store test_conv test_text test_irc \
test_identity test_peers test_local test_filexfer test_adapter test_instance fake_adapter
test_identity test_peers test_local test_filexfer test_pair test_adapter test_instance fake_adapter
test_json_SOURCES = test_json.c
test_json_CPPFLAGS = -I$(top_srcdir)/include
test_json_LDADD = libDtChatIpc.la
@@ -62,5 +62,8 @@ test_local_LDADD = libDtChat.la libDtChatIpc.la $(CRYPTOLIB) $(LIBTLS) -lpthread
test_filexfer_SOURCES = test_filexfer.c
test_filexfer_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir)
test_filexfer_LDADD = libDtChat.la libDtChatIpc.la $(CRYPTOLIB) $(LIBTLS)
test_pair_SOURCES = test_pair.c
test_pair_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir)
test_pair_LDADD = libDtChat.la libDtChatIpc.la $(CRYPTOLIB)
TESTS = test_json test_frame test_loop test_store test_conv test_text test_irc \
test_identity test_peers test_local test_filexfer test_adapter test_instance
test_identity test_peers test_local test_filexfer test_pair test_adapter test_instance
+21
View File
@@ -299,6 +299,27 @@ void dc_file_recv_cancel(DcFileRecv *recv);
const char *dc_file_recv_path(const DcFileRecv *recv);
long long dc_file_recv_got(const DcFileRecv *recv);
/*
* Pairing and TOFU. dc_pair_sas() derives the 6-digit code from both
* certificate pins (order independent). peers.json keeps id -> pin and the
* blocked flag (0600 through the store); a changed pin answers -1 in
* dc_pins_check() and stays blocked until dc_pins_confirm() stores the new
* pin after the person compared a new SAS.
*/
char *dc_pair_sas(const char *pin_a, const char *pin_b);
typedef struct DcPins DcPins;
DcPins *dc_pins_load(const char *dir);
int dc_pins_save(DcPins *pins, const char *dir);
void dc_pins_free(DcPins *pins);
int dc_pins_check(const DcPins *pins, const char *id, const char *pin);
int dc_pins_confirm(DcPins *pins, const char *id, const char *pin);
int dc_pins_set_blocked(DcPins *pins, const char *id, int blocked);
int dc_pins_is_blocked(const DcPins *pins, const char *id);
size_t dc_pins_count(const DcPins *pins);
const char *dc_pins_id_at(const DcPins *pins, size_t index);
const char *dc_pins_pin_at(const DcPins *pins, size_t index);
/*
* IRC lines (RFC 1459/2812 + IRCv3 tags). dc_irc_parse() copies the tags,
* the prefix, the command, up to 15 parameters and the trailing text;
+310
View File
@@ -0,0 +1,310 @@
/*
* pair.c -- the pairing code and the TOFU pin store
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The 6-digit SAS is derived from both certificate pins (sorted, so both
* sides compute the same code). peers.json keeps the pin per identity id
* (0600 through the store). A known id with a different pin is a change:
* dc_pins_check() answers -1 and the caller blocks files and calls until the
* person has compared a new SAS and called dc_pins_confirm().
*/
#include "dc_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <openssl/evp.h>
typedef struct DcPin DcPin;
struct DcPin {
char *id;
char *pin;
int blocked;
};
struct DcPins {
DcPin *pin;
size_t count;
size_t capacity;
};
static int
normalize_pin(const char *text, char out[65])
{
size_t i = 0, o = 0;
if (text == NULL)
return -1;
if (strncasecmp(text, "sha256:", 7) == 0)
text += 7;
for (; text[i] != '\0'; i++) {
char c = text[i];
if (c == ':')
continue;
if (c >= 'A' && c <= 'F')
c = (char)(c - 'A' + 'a');
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')))
return -1;
if (o >= 64)
return -1;
out[o++] = c;
}
out[o] = '\0';
return o == 64 ? 0 : -1;
}
char *
dc_pair_sas(const char *pin_a, const char *pin_b)
{
char a[65], b[65], joined[129], md[EVP_MAX_MD_SIZE];
unsigned int mdlen = 0;
const char *first, *second;
unsigned int code;
char *out;
size_t i;
if (normalize_pin(pin_a, a) != 0 || normalize_pin(pin_b, b) != 0)
return NULL;
if (strcmp(a, b) <= 0) {
first = a;
second = b;
} else {
first = b;
second = a;
}
snprintf(joined, sizeof(joined), "%s%s", first, second);
if (EVP_Digest(joined, strlen(joined), (unsigned char *)md, &mdlen,
EVP_sha256(), NULL) != 1 || mdlen < 4)
return NULL;
code = ((unsigned int)(unsigned char)md[0] << 24) |
((unsigned int)(unsigned char)md[1] << 16) |
((unsigned int)(unsigned char)md[2] << 8) |
(unsigned int)(unsigned char)md[3];
code %= 1000000u;
out = malloc(8);
if (out == NULL)
return NULL;
for (i = 0; i < 6; i++)
out[5 - i] = (char)('0' + (code % 10u)), code /= 10u;
out[6] = '\0';
return out;
}
static DcPin *
pin_find(const DcPins *pins, const char *id)
{
size_t i;
if (pins == NULL || id == NULL)
return NULL;
for (i = 0; i < pins->count; i++)
if (strcmp(pins->pin[i].id, id) == 0)
return &pins->pin[i];
return NULL;
}
static DcPin *
pin_get(DcPins *pins, const char *id)
{
DcPin *pin;
if (pins->count == pins->capacity) {
size_t cap = pins->capacity != 0 ? pins->capacity * 2 : 8;
DcPin *next = realloc(pins->pin, cap * sizeof(*next));
if (next == NULL)
return NULL;
pins->pin = next;
pins->capacity = cap;
}
pin = &pins->pin[pins->count];
memset(pin, 0, sizeof(*pin));
pin->id = strdup(id);
if (pin->id == NULL)
return NULL;
pins->count++;
return pin;
}
DcPins *
dc_pins_load(const char *dir)
{
DcStore *store;
DcJson *json;
DcJson *peers;
DcPins *pins;
size_t i, n;
if (dir == NULL)
return NULL;
pins = calloc(1, sizeof(*pins));
if (pins == NULL)
return NULL;
store = dc_store_open(dir);
if (store == NULL) {
free(pins);
return NULL;
}
json = dc_store_load(store, "peers.json", NULL, NULL, 0);
dc_store_close(store);
if (json == NULL)
return pins; /* missing: an empty store */
peers = dc_json_get(json, "peers");
n = dc_json_array_size(peers);
for (i = 0; i < n; i++) {
DcJson *entry = dc_json_array_get(peers, i);
const char *id = dc_json_string(dc_json_get(entry, "id"));
const char *pin = dc_json_string(dc_json_get(entry, "pin"));
DcPin *p;
if (id == NULL || pin == NULL)
continue;
p = pin_get(pins, id);
if (p == NULL)
continue;
p->pin = strdup(pin);
p->blocked = dc_json_bool(dc_json_get(entry, "blocked"));
}
dc_json_free(json);
return pins;
}
int
dc_pins_save(DcPins *pins, const char *dir)
{
DcStore *store;
DcJson *json;
DcJson *peers;
size_t i;
int rc;
if (pins == NULL || dir == NULL)
return -1;
store = dc_store_open(dir);
if (store == NULL)
return -1;
json = dc_json_new_object();
peers = dc_json_new_array();
if (json == NULL || peers == NULL) {
dc_json_free(json);
dc_json_free(peers);
dc_store_close(store);
return -1;
}
for (i = 0; i < pins->count; i++) {
DcJson *entry = dc_json_new_object();
(void)dc_json_object_set(entry, "id",
dc_json_new_string(pins->pin[i].id));
(void)dc_json_object_set(entry, "pin",
dc_json_new_string(pins->pin[i].pin != NULL ?
pins->pin[i].pin : ""));
(void)dc_json_object_set(entry, "blocked",
dc_json_new_bool(pins->pin[i].blocked));
(void)dc_json_array_append(peers, entry);
}
(void)dc_json_object_set(json, "peers", peers);
rc = dc_store_save(store, "peers.json", json);
dc_json_free(json);
dc_store_close(store);
return rc;
}
void
dc_pins_free(DcPins *pins)
{
size_t i;
if (pins == NULL)
return;
for (i = 0; i < pins->count; i++) {
free(pins->pin[i].id);
free(pins->pin[i].pin);
}
free(pins->pin);
free(pins);
}
int
dc_pins_check(const DcPins *pins, const char *id, const char *pin)
{
DcPin *p = pin_find(pins, id);
char a[65], b[65];
if (p == NULL)
return 0;
if (p->pin == NULL || p->pin[0] == '\0')
return 0;
if (normalize_pin(pin, a) != 0 || normalize_pin(p->pin, b) != 0)
return -1;
return strcmp(a, b) == 0 ? 1 : -1;
}
int
dc_pins_confirm(DcPins *pins, const char *id, const char *pin)
{
DcPin *p;
char normalized[65];
if (pins == NULL || id == NULL || normalize_pin(pin, normalized) != 0)
return -1;
p = pin_find(pins, id);
if (p == NULL) {
p = pin_get(pins, id);
if (p == NULL)
return -1;
}
free(p->pin);
p->pin = strdup(normalized);
return p->pin != NULL ? 0 : -1;
}
int
dc_pins_set_blocked(DcPins *pins, const char *id, int blocked)
{
DcPin *p;
if (pins == NULL || id == NULL)
return -1;
p = pin_find(pins, id);
if (p == NULL) {
p = pin_get(pins, id);
if (p == NULL)
return -1;
}
p->blocked = blocked ? 1 : 0;
return 0;
}
int
dc_pins_is_blocked(const DcPins *pins, const char *id)
{
DcPin *p = pin_find(pins, id);
return p != NULL ? p->blocked : 0;
}
size_t
dc_pins_count(const DcPins *pins)
{
return pins != NULL ? pins->count : 0;
}
const char *
dc_pins_id_at(const DcPins *pins, size_t index)
{
return pins != NULL && index < pins->count ? pins->pin[index].id :
NULL;
}
const char *
dc_pins_pin_at(const DcPins *pins, size_t index)
{
return pins != NULL && index < pins->count ? pins->pin[index].pin :
NULL;
}
+106
View File
@@ -0,0 +1,106 @@
/*
* test_pair.c -- tests for the pairing code and the TOFU pin store (T5.4a)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "dc_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int fails;
#define CHECK(cond) do {\
if (!(cond)) {\
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__,\
#cond);\
fails++;\
}\
} while (0)
#define PIN_A \
"sha256:1111111111111111111111111111111111111111111111111111111111111111"
#define PIN_B \
"sha256:2222222222222222222222222222222222222222222222222222222222222222"
#define PIN_C \
"sha256:3333333333333333333333333333333333333333333333333333333333333333"
int
main(void)
{
char tmp[] = "/tmp/opencode/dtchat-pair-XXXXXX";
char dir[512];
DcPins *pins;
DcPins *again;
char *sas;
size_t i;
sas = dc_pair_sas(PIN_A, PIN_B);
CHECK(sas != NULL);
if (sas != NULL) {
CHECK(strlen(sas) == 6);
for (i = 0; i < 6; i++)
CHECK(sas[i] >= '0' && sas[i] <= '9');
/* known answer for these two pins */
CHECK(strcmp(sas, "448183") == 0);
free(sas);
}
sas = dc_pair_sas(PIN_B, PIN_A);
CHECK(sas != NULL && strcmp(sas, "448183") == 0);
free(sas);
sas = dc_pair_sas(PIN_A, PIN_A);
CHECK(sas != NULL && strcmp(sas, "500498") == 0);
free(sas);
CHECK(dc_pair_sas("nonsense", PIN_B) == NULL);
CHECK(dc_pair_sas(PIN_A, NULL) == NULL);
CHECK(mkdtemp(tmp) != NULL);
snprintf(dir, sizeof(dir), "%s/state", tmp);
pins = dc_pins_load(dir);
CHECK(pins != NULL);
if (pins == NULL)
return 1;
CHECK(dc_pins_count(pins) == 0);
CHECK(dc_pins_check(pins, "alice", PIN_A) == 0);
CHECK(dc_pins_confirm(pins, "alice", PIN_A) == 0);
CHECK(dc_pins_count(pins) == 1);
CHECK(dc_pins_check(pins, "alice", PIN_A) == 1);
CHECK(dc_pins_check(pins, "alice", PIN_B) == -1);
CHECK(dc_pins_check(pins, "bob", PIN_B) == 0);
CHECK(dc_pins_set_blocked(pins, "alice", 1) == 0);
CHECK(dc_pins_is_blocked(pins, "alice") == 1);
CHECK(dc_pins_confirm(pins, "bob", PIN_B) == 0);
CHECK(dc_pins_count(pins) == 2);
CHECK(dc_pins_save(pins, dir) == 0);
dc_pins_free(pins);
again = dc_pins_load(dir);
CHECK(again != NULL);
if (again != NULL) {
CHECK(dc_pins_count(again) == 2);
CHECK(dc_pins_check(again, "alice", PIN_A) == 1);
CHECK(dc_pins_is_blocked(again, "alice") == 1);
CHECK(dc_pins_check(again, "bob", PIN_B) == 1);
CHECK(dc_pins_is_blocked(again, "bob") == 0);
/* the person re-pairs: a new pin is stored after confirmation */
CHECK(dc_pins_check(again, "alice", PIN_C) == -1);
CHECK(dc_pins_confirm(again, "alice", PIN_C) == 0);
CHECK(dc_pins_check(again, "alice", PIN_C) == 1);
dc_pins_free(again);
}
{
char path[600];
snprintf(path, sizeof(path), "%s/peers.json", dir);
CHECK(access(path, F_OK) == 0);
}
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_pair: all ok\n");
return 0;
}