keypin.c 11 KB

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