routerkeys.c 24 KB

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