routerkeys.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /* Copyright (c) 2014, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #include "config.h"
  5. #include "routerkeys.h"
  6. #include "torcert.h"
  7. /**
  8. * Read an ed25519 key and associated certificates from files beginning with
  9. * <b>fname</b>, with certificate type <b>cert_type</b>. On failure, return
  10. * NULL; on success return the keypair.
  11. *
  12. * If INIT_ED_KEY_CREATE is set in <b>flags</b>, then create the key (and
  13. * certificate if requested) if it doesn't exist, and save it to disk.
  14. *
  15. * If INIT_ED_KEY_NEEDCERT is set in <b>flags</b>, load/create a certificate
  16. * too and store it in *<b>cert_out</b>. Fail if the cert can't be
  17. * found/created. To create a certificate, <b>signing_key</b> must be set to
  18. * the key that should sign it; <b>now</b> to the current time, and
  19. * <b>lifetime</b> to the lifetime of the key.
  20. *
  21. * If INIT_ED_KEY_REPLACE is set in <b>flags</b>, then create and save new key
  22. * whether we can read the old one or not.
  23. *
  24. * If INIT_ED_KEY_EXTRA_STRONG is set in <b>flags</b>, set the extra_strong
  25. * flag when creating the secret key.
  26. *
  27. * If INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT is set in <b>flags</b>, and
  28. * we create a new certificate, create it with the signing key embedded.
  29. *
  30. * If INIT_ED_KEY_SPLIT is set in <b>flags</b>, and we create a new key,
  31. * store the public key in a separate file from the secret key.
  32. *
  33. * If INIT_ED_KEY_MISSING_SECRET_OK is set in <b>flags</b>, and we find a
  34. * public key file but no secret key file, return successfully anyway.
  35. *
  36. * If INIT_ED_KEY_OMIT_SECRET is set in <b>flags</b>, do not even try to
  37. * load or return a secret key (but create and save on if needed).
  38. */
  39. ed25519_keypair_t *
  40. ed_key_init_from_file(const char *fname, uint32_t flags,
  41. int severity,
  42. const ed25519_keypair_t *signing_key,
  43. time_t now,
  44. time_t lifetime,
  45. uint8_t cert_type,
  46. struct tor_cert_st **cert_out)
  47. {
  48. char *secret_fname = NULL;
  49. char *public_fname = NULL;
  50. char *cert_fname = NULL;
  51. int created_pk = 0, created_sk = 0, created_cert = 0;
  52. const int try_to_load = ! (flags & INIT_ED_KEY_REPLACE);
  53. char tag[8];
  54. tor_snprintf(tag, sizeof(tag), "type%d", (int)cert_type);
  55. tor_cert_t *cert = NULL;
  56. char *got_tag = NULL;
  57. ed25519_keypair_t *keypair = tor_malloc_zero(sizeof(ed25519_keypair_t));
  58. tor_asprintf(&secret_fname, "%s_secret_key", fname);
  59. tor_asprintf(&public_fname, "%s_public_key", fname);
  60. tor_asprintf(&cert_fname, "%s_cert", fname);
  61. /* Try to read the secret key. */
  62. const int have_secret = try_to_load &&
  63. !(flags & INIT_ED_KEY_OMIT_SECRET) &&
  64. ed25519_seckey_read_from_file(&keypair->seckey,
  65. &got_tag, secret_fname) == 0;
  66. if (have_secret) {
  67. if (strcmp(got_tag, tag)) {
  68. tor_log(severity, LD_OR, "%s has wrong tag", secret_fname);
  69. goto err;
  70. }
  71. /* Derive the public key */
  72. if (ed25519_public_key_generate(&keypair->pubkey, &keypair->seckey)<0) {
  73. tor_log(severity, LD_OR, "%s can't produce a public key", secret_fname);
  74. goto err;
  75. }
  76. }
  77. /* If it's absent and that's okay, try to read the pubkey. */
  78. int found_public = 0;
  79. if (!have_secret && try_to_load) {
  80. tor_free(got_tag);
  81. found_public = ed25519_pubkey_read_from_file(&keypair->pubkey,
  82. &got_tag, public_fname) == 0;
  83. if (found_public && strcmp(got_tag, tag)) {
  84. tor_log(severity, LD_OR, "%s has wrong tag", public_fname);
  85. goto err;
  86. }
  87. }
  88. /* If the secret key is absent and it's not allowed to be, fail. */
  89. if (!have_secret && found_public && !(flags & INIT_ED_KEY_MISSING_SECRET_OK))
  90. goto err;
  91. /* If it's absent, and we're not supposed to make a new keypair, fail. */
  92. if (!have_secret && !found_public && !(flags & INIT_ED_KEY_CREATE))
  93. goto err;
  94. /* if it's absent, make a new keypair and save it. */
  95. if (!have_secret && !found_public) {
  96. const int split = !! (flags & INIT_ED_KEY_SPLIT);
  97. tor_free(keypair);
  98. keypair = ed_key_new(signing_key, flags, now, lifetime,
  99. cert_type, &cert);
  100. if (!keypair) {
  101. tor_log(severity, LD_OR, "Couldn't create keypair");
  102. goto err;
  103. }
  104. created_pk = created_sk = created_cert = 1;
  105. if (ed25519_seckey_write_to_file(&keypair->seckey, secret_fname, tag) < 0
  106. ||
  107. (split &&
  108. ed25519_pubkey_write_to_file(&keypair->pubkey, public_fname, tag) < 0)
  109. ||
  110. (cert &&
  111. crypto_write_tagged_contents_to_file(cert_fname, "ed25519v1-cert",
  112. tag, cert->encoded, cert->encoded_len) < 0)) {
  113. tor_log(severity, LD_OR, "Couldn't write keys or cert to file.");
  114. goto err;
  115. }
  116. goto done;
  117. }
  118. /* If we're not supposed to get a cert, we're done. */
  119. if (! (flags & INIT_ED_KEY_NEEDCERT))
  120. goto done;
  121. /* Read a cert. */
  122. uint8_t certbuf[256];
  123. ssize_t cert_body_len = crypto_read_tagged_contents_from_file(
  124. cert_fname, "ed25519v1-cert",
  125. &got_tag, certbuf, sizeof(certbuf));
  126. if (cert_body_len >= 0 && !strcmp(got_tag, tag))
  127. cert = tor_cert_parse(certbuf, cert_body_len);
  128. /* If we got it, check it to the extent we can. */
  129. if (cert) {
  130. int bad_cert = 0;
  131. if (! cert) {
  132. tor_log(severity, LD_OR, "Cert was unparseable");
  133. bad_cert = 1;
  134. } else if (!tor_memeq(cert->signed_key.pubkey, keypair->pubkey.pubkey,
  135. ED25519_PUBKEY_LEN)) {
  136. tor_log(severity, LD_OR, "Cert was for wrong key");
  137. bad_cert = 1;
  138. } else if (tor_cert_checksig(cert, &signing_key->pubkey, now) < 0 &&
  139. (signing_key || cert->cert_expired)) {
  140. tor_log(severity, LD_OR, "Can't check certificate");
  141. bad_cert = 1;
  142. }
  143. if (bad_cert) {
  144. tor_cert_free(cert);
  145. cert = NULL;
  146. }
  147. }
  148. /* If we got a cert, we're done. */
  149. if (cert)
  150. goto done;
  151. /* If we didn't get a cert, and we're not supposed to make one, fail. */
  152. if (!signing_key || !(flags & INIT_ED_KEY_CREATE))
  153. goto err;
  154. /* We have keys but not a certificate, so make one. */
  155. uint32_t cert_flags = 0;
  156. if (flags & INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT)
  157. cert_flags |= CERT_FLAG_INCLUDE_SIGNING_KEY;
  158. cert = tor_cert_create(signing_key, cert_type,
  159. &keypair->pubkey,
  160. now, lifetime,
  161. cert_flags);
  162. if (! cert)
  163. goto err;
  164. /* Write it to disk. */
  165. created_cert = 1;
  166. if (crypto_write_tagged_contents_to_file(cert_fname, "ed25519v1-cert",
  167. tag, cert->encoded, cert->encoded_len) < 0) {
  168. tor_log(severity, LD_OR, "Couldn't write cert to disk.");
  169. goto err;
  170. }
  171. done:
  172. if (cert_out)
  173. *cert_out = cert;
  174. else
  175. tor_cert_free(cert);
  176. goto cleanup;
  177. err:
  178. memwipe(keypair, 0, sizeof(*keypair));
  179. tor_free(keypair);
  180. tor_cert_free(cert);
  181. if (cert_out)
  182. *cert_out = NULL;
  183. if (created_sk)
  184. unlink(secret_fname);
  185. if (created_pk)
  186. unlink(public_fname);
  187. if (created_cert)
  188. unlink(cert_fname);
  189. cleanup:
  190. tor_free(secret_fname);
  191. tor_free(public_fname);
  192. tor_free(cert_fname);
  193. return keypair;
  194. }
  195. /**
  196. * Create a new signing key and (optionally) certficiate; do not read or write
  197. * from disk. See ed_key_init_from_file() for more information.
  198. */
  199. ed25519_keypair_t *
  200. ed_key_new(const ed25519_keypair_t *signing_key,
  201. uint32_t flags,
  202. time_t now,
  203. time_t lifetime,
  204. uint8_t cert_type,
  205. struct tor_cert_st **cert_out)
  206. {
  207. if (cert_out)
  208. *cert_out = NULL;
  209. const int extra_strong = !! (flags & INIT_ED_KEY_EXTRA_STRONG);
  210. ed25519_keypair_t *keypair = tor_malloc_zero(sizeof(ed25519_keypair_t));
  211. if (ed25519_keypair_generate(keypair, extra_strong) < 0)
  212. goto err;
  213. if (! (flags & INIT_ED_KEY_NEEDCERT))
  214. return keypair;
  215. tor_assert(signing_key);
  216. tor_assert(cert_out);
  217. uint32_t cert_flags = 0;
  218. if (flags & INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT)
  219. cert_flags |= CERT_FLAG_INCLUDE_SIGNING_KEY;
  220. tor_cert_t *cert = tor_cert_create(signing_key, cert_type,
  221. &keypair->pubkey,
  222. now, lifetime,
  223. cert_flags);
  224. if (! cert)
  225. goto err;
  226. *cert_out = cert;
  227. return keypair;
  228. err:
  229. tor_free(keypair);
  230. return NULL;
  231. }
  232. static ed25519_keypair_t *master_identity_key = NULL;
  233. static ed25519_keypair_t *master_signing_key = NULL;
  234. static ed25519_keypair_t *current_link_key = NULL;
  235. static ed25519_keypair_t *current_auth_key = NULL;
  236. static tor_cert_t *signing_key_cert = NULL;
  237. static tor_cert_t *link_key_cert = NULL;
  238. static tor_cert_t *auth_key_cert = NULL;
  239. /**
  240. * Running as a server: load, reload, or refresh our ed25519 keys and
  241. * certificates, creating and saving new ones as needed.
  242. */
  243. int
  244. load_ed_keys(const or_options_t *options, time_t now)
  245. {
  246. ed25519_keypair_t *id = NULL;
  247. ed25519_keypair_t *sign = NULL;
  248. ed25519_keypair_t *link = NULL;
  249. ed25519_keypair_t *auth = NULL;
  250. const ed25519_keypair_t *sign_signing_key_with_id = NULL;
  251. const ed25519_keypair_t *use_signing = NULL;
  252. const tor_cert_t *check_signing_cert = NULL;
  253. tor_cert_t *sign_cert = NULL;
  254. tor_cert_t *link_cert = NULL;
  255. tor_cert_t *auth_cert = NULL;
  256. #define FAIL(msg) do { \
  257. log_warn(LD_OR, (msg)); \
  258. goto err; \
  259. } while (0)
  260. #define SET_KEY(key, newval) do { \
  261. ed25519_keypair_free(key); \
  262. key = (newval); \
  263. } while (0)
  264. #define SET_CERT(cert, newval) do { \
  265. tor_cert_free(cert); \
  266. cert = (newval); \
  267. } while (0)
  268. #define EXPIRES_SOON(cert, interval) \
  269. (!(cert) || (cert)->valid_until < now + (interval))
  270. /* XXXX support encrypted identity keys fully */
  271. /* First try to get the signing key to see how it is. */
  272. if (master_signing_key) {
  273. check_signing_cert = signing_key_cert;
  274. use_signing = master_signing_key;
  275. } else {
  276. sign = ed_key_init_from_file(
  277. options_get_datadir_fname2(options, "keys", "ed25519_signing"),
  278. INIT_ED_KEY_NEEDCERT|
  279. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT,
  280. LOG_INFO,
  281. NULL, 0, 0, CERT_TYPE_ID_SIGNING, &sign_cert);
  282. check_signing_cert = sign_cert;
  283. use_signing = sign;
  284. }
  285. const int need_new_signing_key =
  286. NULL == use_signing ||
  287. EXPIRES_SOON(check_signing_cert, 0);
  288. const int want_new_signing_key =
  289. need_new_signing_key ||
  290. EXPIRES_SOON(check_signing_cert, options->TestingSigningKeySlop);
  291. {
  292. uint32_t flags =
  293. (INIT_ED_KEY_CREATE|INIT_ED_KEY_SPLIT|
  294. INIT_ED_KEY_EXTRA_STRONG);
  295. if (! need_new_signing_key)
  296. flags |= INIT_ED_KEY_MISSING_SECRET_OK;
  297. if (! want_new_signing_key)
  298. flags |= INIT_ED_KEY_OMIT_SECRET;
  299. id = ed_key_init_from_file(
  300. options_get_datadir_fname2(options, "keys", "ed25519_master_id"),
  301. flags,
  302. LOG_WARN, NULL, 0, 0, 0, NULL);
  303. if (!id)
  304. FAIL("Missing identity key");
  305. if (tor_mem_is_zero((char*)id->seckey.seckey, sizeof(id->seckey)))
  306. sign_signing_key_with_id = NULL;
  307. else
  308. sign_signing_key_with_id = id;
  309. }
  310. if (need_new_signing_key && NULL == sign_signing_key_with_id)
  311. FAIL("Can't load master key make a new signing key.");
  312. if (want_new_signing_key && sign_signing_key_with_id) {
  313. uint32_t flags = (INIT_ED_KEY_CREATE|
  314. INIT_ED_KEY_REPLACE|
  315. INIT_ED_KEY_EXTRA_STRONG|
  316. INIT_ED_KEY_NEEDCERT|
  317. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT);
  318. sign = ed_key_init_from_file(
  319. options_get_datadir_fname2(options, "keys", "ed25519_signing"),
  320. flags, LOG_WARN,
  321. sign_signing_key_with_id, now,
  322. options->SigningKeyLifetime,
  323. CERT_TYPE_ID_SIGNING, &sign_cert);
  324. if (!sign)
  325. FAIL("Missing signing key");
  326. use_signing = sign;
  327. } else if (want_new_signing_key) {
  328. static ratelim_t missing_master = RATELIM_INIT(3600);
  329. log_fn_ratelim(&missing_master, LOG_WARN, LD_OR,
  330. "Signing key will expire soon, but I can't load the "
  331. "master key to sign a new one!");
  332. }
  333. tor_assert(use_signing);
  334. /* At this point we no longer need our secret identity key. So wipe
  335. * it, if we loaded it in the first place. */
  336. memwipe(id->seckey.seckey, 0, sizeof(id->seckey));
  337. if (!current_link_key ||
  338. EXPIRES_SOON(link_key_cert, options->TestingLinkKeySlop)) {
  339. link = ed_key_new(use_signing, INIT_ED_KEY_NEEDCERT,
  340. now,
  341. options->TestingLinkKeyLifetime,
  342. CERT_TYPE_SIGNING_LINK, &link_cert);
  343. if (!link)
  344. FAIL("Can't create link key");
  345. }
  346. if (!current_auth_key ||
  347. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop)) {
  348. auth = ed_key_new(use_signing, INIT_ED_KEY_NEEDCERT,
  349. now,
  350. options->TestingAuthKeyLifetime,
  351. CERT_TYPE_SIGNING_AUTH, &auth_cert);
  352. if (!auth)
  353. FAIL("Can't create auth key");
  354. }
  355. /* We've generated or loaded everything. Put them in memory. */
  356. if (! master_identity_key) {
  357. SET_KEY(master_identity_key, id);
  358. } else {
  359. tor_free(id);
  360. }
  361. if (sign) {
  362. SET_KEY(master_signing_key, sign);
  363. SET_CERT(signing_key_cert, sign_cert);
  364. }
  365. if (link) {
  366. SET_KEY(current_link_key, link);
  367. SET_CERT(link_key_cert, link_cert);
  368. }
  369. if (auth) {
  370. SET_KEY(current_auth_key, auth);
  371. SET_CERT(auth_key_cert, auth_cert);
  372. }
  373. return 0;
  374. err:
  375. ed25519_keypair_free(id);
  376. ed25519_keypair_free(sign);
  377. ed25519_keypair_free(link);
  378. ed25519_keypair_free(auth);
  379. tor_cert_free(sign_cert);
  380. tor_cert_free(link_cert);
  381. tor_cert_free(auth_cert);
  382. return -1;
  383. #undef FAIL
  384. #undef SET_KEY
  385. #undef SET_CERT
  386. }
  387. int
  388. should_make_new_ed_keys(const or_options_t *options, const time_t now)
  389. {
  390. return (!master_identity_key ||
  391. !master_signing_key ||
  392. !current_link_key ||
  393. !current_auth_key ||
  394. EXPIRES_SOON(signing_key_cert, options->TestingSigningKeySlop) ||
  395. EXPIRES_SOON(link_key_cert, options->TestingLinkKeySlop) ||
  396. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop));
  397. }
  398. #undef EXPIRES_SOON
  399. const ed25519_public_key_t *
  400. get_master_identity_key(void)
  401. {
  402. if (!master_identity_key)
  403. return NULL;
  404. return &master_identity_key->pubkey;
  405. }
  406. const ed25519_keypair_t *
  407. get_master_signing_keypair(void)
  408. {
  409. return master_signing_key;
  410. }
  411. const struct tor_cert_st *
  412. get_master_signing_key_cert(void)
  413. {
  414. return signing_key_cert;
  415. }
  416. const ed25519_keypair_t *
  417. get_current_link_keypair(void)
  418. {
  419. return current_link_key;
  420. }
  421. const ed25519_keypair_t *
  422. get_current_auth_keypair(void)
  423. {
  424. return current_auth_key;
  425. }
  426. const tor_cert_t *
  427. get_current_link_key_cert(void)
  428. {
  429. return link_key_cert;
  430. }
  431. const tor_cert_t *
  432. get_current_auth_key_cert(void)
  433. {
  434. return auth_key_cert;
  435. }
  436. /** Construct cross-certification for the master identity key with
  437. * the ntor onion key. Store the sign of the corresponding ed25519 public key
  438. * in *<b>sign_out</b>. */
  439. tor_cert_t *
  440. make_ntor_onion_key_crosscert(const curve25519_keypair_t *onion_key,
  441. const ed25519_public_key_t *master_id_key, time_t now, time_t lifetime,
  442. int *sign_out)
  443. {
  444. tor_cert_t *cert = NULL;
  445. ed25519_keypair_t ed_onion_key;
  446. if (ed25519_keypair_from_curve25519_keypair(&ed_onion_key, sign_out,
  447. onion_key) < 0)
  448. goto end;
  449. cert = tor_cert_create(&ed_onion_key, CERT_TYPE_ONION_ID, master_id_key,
  450. now, lifetime, 0);
  451. end:
  452. memwipe(&ed_onion_key, 0, sizeof(ed_onion_key));
  453. return cert;
  454. }
  455. /** Construct and return an RSA signature for the TAP onion key to
  456. * cross-certify the RSA and Ed25519 identity keys. Set <b>len_out</b> to its
  457. * length. */
  458. uint8_t *
  459. make_tap_onion_key_crosscert(const crypto_pk_t *onion_key,
  460. const ed25519_public_key_t *master_id_key,
  461. const crypto_pk_t *rsa_id_key,
  462. int *len_out)
  463. {
  464. uint8_t signature[PK_BYTES];
  465. uint8_t signed_data[DIGEST_LEN + ED25519_PUBKEY_LEN];
  466. *len_out = 0;
  467. crypto_pk_get_digest(rsa_id_key, (char*)signed_data);
  468. memcpy(signed_data + DIGEST_LEN, master_id_key->pubkey, ED25519_PUBKEY_LEN);
  469. int r = crypto_pk_private_sign(onion_key,
  470. (char*)signature, sizeof(signature),
  471. (const char*)signed_data, sizeof(signed_data));
  472. if (r < 0)
  473. return NULL;
  474. *len_out = r;
  475. return tor_memdup(signature, r);
  476. }
  477. /** Check whether an RSA-TAP cross-certification is correct. Return 0 if it
  478. * is, -1 if it isn't. */
  479. int
  480. check_tap_onion_key_crosscert(const uint8_t *crosscert,
  481. int crosscert_len,
  482. const crypto_pk_t *onion_pkey,
  483. const ed25519_public_key_t *master_id_pkey,
  484. const uint8_t *rsa_id_digest)
  485. {
  486. uint8_t *cc = tor_malloc(crypto_pk_keysize(onion_pkey));
  487. int cc_len =
  488. crypto_pk_public_checksig(onion_pkey,
  489. (char*)cc,
  490. crypto_pk_keysize(onion_pkey),
  491. (const char*)crosscert,
  492. crosscert_len);
  493. if (cc_len < 0) {
  494. goto err;
  495. }
  496. if (cc_len < DIGEST_LEN + ED25519_PUBKEY_LEN) {
  497. log_warn(LD_DIR, "Short signature on cross-certification with TAP key");
  498. goto err;
  499. }
  500. if (tor_memneq(cc, rsa_id_digest, DIGEST_LEN) ||
  501. tor_memneq(cc + DIGEST_LEN, master_id_pkey->pubkey,
  502. ED25519_PUBKEY_LEN)) {
  503. log_warn(LD_DIR, "Incorrect cross-certification with TAP key");
  504. goto err;
  505. }
  506. tor_free(cc);
  507. return 0;
  508. err:
  509. tor_free(cc);
  510. return -1;
  511. }
  512. void
  513. routerkeys_free_all(void)
  514. {
  515. ed25519_keypair_free(master_identity_key);
  516. ed25519_keypair_free(master_signing_key);
  517. ed25519_keypair_free(current_link_key);
  518. ed25519_keypair_free(current_auth_key);
  519. tor_cert_free(signing_key_cert);
  520. tor_cert_free(link_key_cert);
  521. tor_cert_free(auth_key_cert);
  522. master_identity_key = master_signing_key = NULL;
  523. current_link_key = current_auth_key = NULL;
  524. signing_key_cert = link_key_cert = auth_key_cert = NULL;
  525. }