routerkeys.c 24 KB

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