keypin.c 15 KB

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