keypin.c 12 KB

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