keypin.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* Copyright (c) 2014-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file keypin.c
  5. *
  6. * \brief Functions and structures for associating routers' RSA key
  7. * fingerprints with their ED25519 keys.
  8. */
  9. #define KEYPIN_PRIVATE
  10. #include "orconfig.h"
  11. #include "compat.h"
  12. #include "crypto.h"
  13. #include "crypto_format.h"
  14. #include "di_ops.h"
  15. #include "ht.h"
  16. #include "keypin.h"
  17. #include "siphash.h"
  18. #include "torint.h"
  19. #include "torlog.h"
  20. #include "util.h"
  21. #include "util_format.h"
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #ifdef HAVE_FCNTL_H
  26. #include <fcntl.h>
  27. #endif
  28. #ifdef _WIN32
  29. #include <io.h>
  30. #endif
  31. /**
  32. * @file keypin.c
  33. * @brief Key-pinning for RSA and Ed25519 identity keys at directory
  34. * authorities.
  35. *
  36. * This module implements a key-pinning mechanism to ensure that it's safe
  37. * to use RSA keys as identitifers even as we migrate to Ed25519 keys. It
  38. * remembers, for every Ed25519 key we've seen, what the associated Ed25519
  39. * key is. This way, if we see a different Ed25519 key with that RSA key,
  40. * we'll know that there's a mismatch.
  41. *
  42. * We persist these entries to disk using a simple format, where each line
  43. * has a base64-encoded RSA SHA1 hash, then a base64-endoded Ed25519 key.
  44. * Empty lines, misformed lines, and lines beginning with # are
  45. * ignored. Lines beginning with @ are reserved for future extensions.
  46. */
  47. static int keypin_journal_append_entry(const uint8_t *rsa_id_digest,
  48. const uint8_t *ed25519_id_key);
  49. static int keypin_check_and_add_impl(const uint8_t *rsa_id_digest,
  50. const uint8_t *ed25519_id_key,
  51. const int do_not_add,
  52. const int replace);
  53. static int keypin_add_or_replace_entry_in_map(keypin_ent_t *ent);
  54. static HT_HEAD(rsamap, keypin_ent_st) the_rsa_map = HT_INITIALIZER();
  55. static HT_HEAD(edmap, keypin_ent_st) the_ed_map = HT_INITIALIZER();
  56. /** Hashtable helper: compare two keypin table entries and return true iff
  57. * they have the same RSA key IDs. */
  58. static inline int
  59. keypin_ents_eq_rsa(const keypin_ent_t *a, const keypin_ent_t *b)
  60. {
  61. return tor_memeq(a->rsa_id, b->rsa_id, sizeof(a->rsa_id));
  62. }
  63. /** Hashtable helper: hash a keypin table entries based on its RSA key ID */
  64. static inline unsigned
  65. keypin_ent_hash_rsa(const keypin_ent_t *a)
  66. {
  67. return (unsigned) siphash24g(a->rsa_id, sizeof(a->rsa_id));
  68. }
  69. /** Hashtable helper: compare two keypin table entries and return true iff
  70. * they have the same ed25519 keys */
  71. static inline int
  72. keypin_ents_eq_ed(const keypin_ent_t *a, const keypin_ent_t *b)
  73. {
  74. return tor_memeq(a->ed25519_key, b->ed25519_key, sizeof(a->ed25519_key));
  75. }
  76. /** Hashtable helper: hash a keypin table entries based on its ed25519 key */
  77. static inline unsigned
  78. keypin_ent_hash_ed(const keypin_ent_t *a)
  79. {
  80. return (unsigned) siphash24g(a->ed25519_key, sizeof(a->ed25519_key));
  81. }
  82. HT_PROTOTYPE(rsamap, keypin_ent_st, rsamap_node, keypin_ent_hash_rsa,
  83. keypin_ents_eq_rsa)
  84. HT_GENERATE2(rsamap, keypin_ent_st, rsamap_node, keypin_ent_hash_rsa,
  85. keypin_ents_eq_rsa, 0.6, tor_reallocarray, tor_free_)
  86. HT_PROTOTYPE(edmap, keypin_ent_st, edmap_node, keypin_ent_hash_ed,
  87. keypin_ents_eq_ed)
  88. HT_GENERATE2(edmap, keypin_ent_st, edmap_node, keypin_ent_hash_ed,
  89. keypin_ents_eq_ed, 0.6, tor_reallocarray, tor_free_)
  90. /**
  91. * Check whether we already have an entry in the key pinning table for a
  92. * router with RSA ID digest <b>rsa_id_digest</b> or for ed25519 key
  93. * <b>ed25519_id_key</b>. If we have an entry that matches both keys,
  94. * return KEYPIN_FOUND. If we find an entry that matches one key but
  95. * not the other, return KEYPIN_MISMATCH. If we have no entry for either
  96. * key, add such an entry to the table and return KEYPIN_ADDED.
  97. *
  98. * If <b>replace_existing_entry</b> is true, then any time we would have said
  99. * KEYPIN_FOUND, we instead add this entry anyway and return KEYPIN_ADDED.
  100. */
  101. int
  102. keypin_check_and_add(const uint8_t *rsa_id_digest,
  103. const uint8_t *ed25519_id_key,
  104. const int replace_existing_entry)
  105. {
  106. return keypin_check_and_add_impl(rsa_id_digest, ed25519_id_key, 0,
  107. replace_existing_entry);
  108. }
  109. /**
  110. * As keypin_check_and_add, but do not add. Return KEYPIN_NOT_FOUND if
  111. * we would add.
  112. */
  113. int
  114. keypin_check(const uint8_t *rsa_id_digest,
  115. const uint8_t *ed25519_id_key)
  116. {
  117. return keypin_check_and_add_impl(rsa_id_digest, ed25519_id_key, 1, 0);
  118. }
  119. /**
  120. * Helper: implements keypin_check and keypin_check_and_add.
  121. */
  122. static int
  123. keypin_check_and_add_impl(const uint8_t *rsa_id_digest,
  124. const uint8_t *ed25519_id_key,
  125. const int do_not_add,
  126. const int replace)
  127. {
  128. keypin_ent_t search, *ent;
  129. memset(&search, 0, sizeof(search));
  130. memcpy(search.rsa_id, rsa_id_digest, sizeof(search.rsa_id));
  131. memcpy(search.ed25519_key, ed25519_id_key, sizeof(search.ed25519_key));
  132. /* Search by RSA key digest first */
  133. ent = HT_FIND(rsamap, &the_rsa_map, &search);
  134. if (ent) {
  135. tor_assert(fast_memeq(ent->rsa_id, rsa_id_digest, sizeof(ent->rsa_id)));
  136. if (tor_memeq(ent->ed25519_key, ed25519_id_key,sizeof(ent->ed25519_key))) {
  137. return KEYPIN_FOUND; /* Match on both keys. Great. */
  138. } else {
  139. if (!replace)
  140. return KEYPIN_MISMATCH; /* Found RSA with different Ed key */
  141. }
  142. }
  143. /* See if we know a different RSA key for this ed key */
  144. if (! replace) {
  145. ent = HT_FIND(edmap, &the_ed_map, &search);
  146. if (ent) {
  147. /* If we got here, then the ed key matches and the RSA doesn't */
  148. tor_assert(fast_memeq(ent->ed25519_key, ed25519_id_key,
  149. sizeof(ent->ed25519_key)));
  150. tor_assert(fast_memneq(ent->rsa_id, rsa_id_digest, sizeof(ent->rsa_id)));
  151. return KEYPIN_MISMATCH;
  152. }
  153. }
  154. /* Okay, this one is new to us. */
  155. if (do_not_add)
  156. return KEYPIN_NOT_FOUND;
  157. ent = tor_memdup(&search, sizeof(search));
  158. int r = keypin_add_or_replace_entry_in_map(ent);
  159. if (! replace) {
  160. tor_assert(r == 1);
  161. } else {
  162. tor_assert(r != 0);
  163. }
  164. keypin_journal_append_entry(rsa_id_digest, ed25519_id_key);
  165. return KEYPIN_ADDED;
  166. }
  167. /**
  168. * Helper: add <b>ent</b> to the hash tables.
  169. */
  170. MOCK_IMPL(STATIC void,
  171. keypin_add_entry_to_map, (keypin_ent_t *ent))
  172. {
  173. HT_INSERT(rsamap, &the_rsa_map, ent);
  174. HT_INSERT(edmap, &the_ed_map, ent);
  175. }
  176. /**
  177. * Helper: add 'ent' to the maps, replacing any entries that contradict it.
  178. * Take ownership of 'ent', freeing it if needed.
  179. *
  180. * Return 0 if the entry was a duplicate, -1 if there was a conflict,
  181. * and 1 if there was no conflict.
  182. */
  183. static int
  184. keypin_add_or_replace_entry_in_map(keypin_ent_t *ent)
  185. {
  186. int r = 1;
  187. keypin_ent_t *ent2 = HT_FIND(rsamap, &the_rsa_map, ent);
  188. keypin_ent_t *ent3 = HT_FIND(edmap, &the_ed_map, ent);
  189. if (ent2 &&
  190. fast_memeq(ent2->ed25519_key, ent->ed25519_key, DIGEST256_LEN)) {
  191. /* We already have this mapping stored. Ignore it. */
  192. tor_free(ent);
  193. return 0;
  194. } else if (ent2 || ent3) {
  195. /* We have a conflict. (If we had no entry, we would have ent2 == ent3
  196. * == NULL. If we had a non-conflicting duplicate, we would have found
  197. * it above.)
  198. *
  199. * We respond by having this entry (ent) supersede all entries that it
  200. * contradicts (ent2 and/or ent3). In other words, if we receive
  201. * <rsa,ed>, we remove all <rsa,ed'> and all <rsa',ed>, for rsa'!=rsa
  202. * and ed'!= ed.
  203. */
  204. const keypin_ent_t *t;
  205. if (ent2) {
  206. t = HT_REMOVE(rsamap, &the_rsa_map, ent2);
  207. tor_assert(ent2 == t);
  208. t = HT_REMOVE(edmap, &the_ed_map, ent2);
  209. tor_assert(ent2 == t);
  210. }
  211. if (ent3 && ent2 != ent3) {
  212. t = HT_REMOVE(rsamap, &the_rsa_map, ent3);
  213. tor_assert(ent3 == t);
  214. t = HT_REMOVE(edmap, &the_ed_map, ent3);
  215. tor_assert(ent3 == t);
  216. tor_free(ent3);
  217. }
  218. tor_free(ent2);
  219. r = -1;
  220. /* Fall through */
  221. }
  222. keypin_add_entry_to_map(ent);
  223. return r;
  224. }
  225. /**
  226. * Check whether we already have an entry in the key pinning table for a
  227. * router with RSA ID digest <b>rsa_id_digest</b>. If we have no such entry,
  228. * return KEYPIN_NOT_FOUND. If we find an entry that matches the RSA key but
  229. * which has an ed25519 key, return KEYPIN_MISMATCH.
  230. */
  231. int
  232. keypin_check_lone_rsa(const uint8_t *rsa_id_digest)
  233. {
  234. keypin_ent_t search, *ent;
  235. memset(&search, 0, sizeof(search));
  236. memcpy(search.rsa_id, rsa_id_digest, sizeof(search.rsa_id));
  237. /* Search by RSA key digest first */
  238. ent = HT_FIND(rsamap, &the_rsa_map, &search);
  239. if (ent) {
  240. return KEYPIN_MISMATCH;
  241. } else {
  242. return KEYPIN_NOT_FOUND;
  243. }
  244. }
  245. /** Open fd to the keypinning journal file. */
  246. static int keypin_journal_fd = -1;
  247. /** Open the key-pinning journal to append to <b>fname</b>. Return 0 on
  248. * success, -1 on failure. */
  249. int
  250. keypin_open_journal(const char *fname)
  251. {
  252. /* O_SYNC ??*/
  253. int fd = tor_open_cloexec(fname, O_WRONLY|O_CREAT|O_BINARY, 0600);
  254. if (fd < 0)
  255. goto err;
  256. if (tor_fd_seekend(fd) < 0)
  257. goto err;
  258. /* Add a newline in case the last line was only partially written */
  259. if (write(fd, "\n", 1) < 1)
  260. goto err;
  261. /* Add something about when we opened this file. */
  262. char buf[80];
  263. char tbuf[ISO_TIME_LEN+1];
  264. format_iso_time(tbuf, approx_time());
  265. tor_snprintf(buf, sizeof(buf), "@opened-at %s\n", tbuf);
  266. if (write_all(fd, buf, strlen(buf), 0) < 0)
  267. goto err;
  268. keypin_journal_fd = fd;
  269. return 0;
  270. err:
  271. if (fd >= 0)
  272. close(fd);
  273. return -1;
  274. }
  275. /** Close the keypinning journal file. */
  276. int
  277. keypin_close_journal(void)
  278. {
  279. if (keypin_journal_fd >= 0)
  280. close(keypin_journal_fd);
  281. keypin_journal_fd = -1;
  282. return 0;
  283. }
  284. /** Length of a keypinning journal line, including terminating newline. */
  285. #define JOURNAL_LINE_LEN (BASE64_DIGEST_LEN + BASE64_DIGEST256_LEN + 2)
  286. /** Add an entry to the keypinning journal to map <b>rsa_id_digest</b> and
  287. * <b>ed25519_id_key</b>. */
  288. static int
  289. keypin_journal_append_entry(const uint8_t *rsa_id_digest,
  290. const uint8_t *ed25519_id_key)
  291. {
  292. if (keypin_journal_fd == -1)
  293. return -1;
  294. char line[JOURNAL_LINE_LEN];
  295. digest_to_base64(line, (const char*)rsa_id_digest);
  296. line[BASE64_DIGEST_LEN] = ' ';
  297. digest256_to_base64(line + BASE64_DIGEST_LEN + 1,
  298. (const char*)ed25519_id_key);
  299. line[BASE64_DIGEST_LEN+1+BASE64_DIGEST256_LEN] = '\n';
  300. if (write_all(keypin_journal_fd, line, JOURNAL_LINE_LEN, 0)<0) {
  301. log_warn(LD_DIRSERV, "Error while adding a line to the key-pinning "
  302. "journal: %s", strerror(errno));
  303. keypin_close_journal();
  304. return -1;
  305. }
  306. return 0;
  307. }
  308. /** Load a journal from the <b>size</b>-byte region at <b>data</b>. Return 0
  309. * on success, -1 on failure. */
  310. STATIC int
  311. keypin_load_journal_impl(const char *data, size_t size)
  312. {
  313. const char *start = data, *end = data + size, *next;
  314. int n_corrupt_lines = 0;
  315. int n_entries = 0;
  316. int n_duplicates = 0;
  317. int n_conflicts = 0;
  318. for (const char *cp = start; cp < end; cp = next) {
  319. const char *eol = memchr(cp, '\n', end-cp);
  320. const char *eos = eol ? eol : end;
  321. const size_t len = eos - cp;
  322. next = eol ? eol + 1 : end;
  323. if (len == 0) {
  324. continue;
  325. }
  326. if (*cp == '@') {
  327. /* Lines that start with @ are reserved. Ignore for now. */
  328. continue;
  329. }
  330. if (*cp == '#') {
  331. /* Lines that start with # are comments. */
  332. continue;
  333. }
  334. /* Is it the right length? (The -1 here is for the newline.) */
  335. if (len != JOURNAL_LINE_LEN - 1) {
  336. /* Lines with a bad length are corrupt unless they are empty.
  337. * Ignore them either way */
  338. for (const char *s = cp; s < eos; ++s) {
  339. if (! TOR_ISSPACE(*s)) {
  340. ++n_corrupt_lines;
  341. break;
  342. }
  343. }
  344. continue;
  345. }
  346. keypin_ent_t *ent = keypin_parse_journal_line(cp);
  347. if (ent == NULL) {
  348. ++n_corrupt_lines;
  349. continue;
  350. }
  351. const int r = keypin_add_or_replace_entry_in_map(ent);
  352. if (r == 0) {
  353. ++n_duplicates;
  354. } else if (r == -1) {
  355. ++n_conflicts;
  356. }
  357. ++n_entries;
  358. }
  359. int severity = (n_corrupt_lines || n_duplicates) ? LOG_WARN : LOG_INFO;
  360. tor_log(severity, LD_DIRSERV,
  361. "Loaded %d entries from keypin journal. "
  362. "Found %d corrupt lines, %d duplicates, and %d conflicts.",
  363. n_entries, n_corrupt_lines, n_duplicates, n_conflicts);
  364. return 0;
  365. }
  366. /**
  367. * Load a journal from the file called <b>fname</b>. Return 0 on success,
  368. * -1 on failure.
  369. */
  370. int
  371. keypin_load_journal(const char *fname)
  372. {
  373. tor_mmap_t *map = tor_mmap_file(fname);
  374. if (!map) {
  375. if (errno == ENOENT)
  376. return 0;
  377. else
  378. return -1;
  379. }
  380. int r = keypin_load_journal_impl(map->data, map->size);
  381. tor_munmap_file(map);
  382. return r;
  383. }
  384. /** Parse a single keypinning journal line entry from <b>cp</b>. The input
  385. * does not need to be NUL-terminated, but it <em>does</em> need to have
  386. * KEYPIN_JOURNAL_LINE_LEN -1 bytes available to read. Return a new entry
  387. * on success, and NULL on failure.
  388. */
  389. STATIC keypin_ent_t *
  390. keypin_parse_journal_line(const char *cp)
  391. {
  392. /* XXXX assumes !USE_OPENSSL_BASE64 */
  393. keypin_ent_t *ent = tor_malloc_zero(sizeof(keypin_ent_t));
  394. if (base64_decode((char*)ent->rsa_id, sizeof(ent->rsa_id),
  395. cp, BASE64_DIGEST_LEN) != DIGEST_LEN ||
  396. cp[BASE64_DIGEST_LEN] != ' ' ||
  397. base64_decode((char*)ent->ed25519_key, sizeof(ent->ed25519_key),
  398. cp+BASE64_DIGEST_LEN+1, BASE64_DIGEST256_LEN) != DIGEST256_LEN) {
  399. tor_free(ent);
  400. return NULL;
  401. } else {
  402. return ent;
  403. }
  404. }
  405. /** Remove all entries from the keypinning table.*/
  406. void
  407. keypin_clear(void)
  408. {
  409. int bad_entries = 0;
  410. {
  411. keypin_ent_t **ent, **next, *this;
  412. for (ent = HT_START(rsamap, &the_rsa_map); ent != NULL; ent = next) {
  413. this = *ent;
  414. next = HT_NEXT_RMV(rsamap, &the_rsa_map, ent);
  415. keypin_ent_t *other_ent = HT_REMOVE(edmap, &the_ed_map, this);
  416. bad_entries += (other_ent != this);
  417. tor_free(this);
  418. }
  419. }
  420. bad_entries += HT_SIZE(&the_ed_map);
  421. HT_CLEAR(edmap,&the_ed_map);
  422. HT_CLEAR(rsamap,&the_rsa_map);
  423. if (bad_entries) {
  424. log_warn(LD_BUG, "Found %d discrepencies in the keypin database.",
  425. bad_entries);
  426. }
  427. }