/* * 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 #include #include #include #include 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; }