routerkeys.c 20 KB

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