routerkeys.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. tor_free(got_tag);
  250. uint8_t certbuf[256];
  251. ssize_t cert_body_len = crypto_read_tagged_contents_from_file(
  252. cert_fname, "ed25519v1-cert",
  253. &got_tag, certbuf, sizeof(certbuf));
  254. if (cert_body_len >= 0 && !strcmp(got_tag, tag))
  255. cert = tor_cert_parse(certbuf, cert_body_len);
  256. /* If we got it, check it to the extent we can. */
  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 (signing_key &&
  266. tor_cert_checksig(cert, &signing_key->pubkey, now) < 0 &&
  267. (signing_key || cert->cert_expired)) {
  268. tor_log(severity, LD_OR, "Can't check certificate");
  269. bad_cert = 1;
  270. }
  271. if (bad_cert) {
  272. tor_cert_free(cert);
  273. cert = NULL;
  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. if (keypair)
  306. memwipe(keypair, 0, sizeof(*keypair));
  307. tor_free(keypair);
  308. tor_cert_free(cert);
  309. if (cert_out)
  310. *cert_out = NULL;
  311. if (created_sk)
  312. unlink(secret_fname);
  313. if (created_pk)
  314. unlink(public_fname);
  315. if (created_cert)
  316. unlink(cert_fname);
  317. cleanup:
  318. tor_free(encrypted_secret_fname);
  319. tor_free(secret_fname);
  320. tor_free(public_fname);
  321. tor_free(cert_fname);
  322. tor_free(got_tag);
  323. return keypair;
  324. }
  325. /**
  326. * Create a new signing key and (optionally) certficiate; do not read or write
  327. * from disk. See ed_key_init_from_file() for more information.
  328. */
  329. ed25519_keypair_t *
  330. ed_key_new(const ed25519_keypair_t *signing_key,
  331. uint32_t flags,
  332. time_t now,
  333. time_t lifetime,
  334. uint8_t cert_type,
  335. struct tor_cert_st **cert_out)
  336. {
  337. if (cert_out)
  338. *cert_out = NULL;
  339. const int extra_strong = !! (flags & INIT_ED_KEY_EXTRA_STRONG);
  340. ed25519_keypair_t *keypair = tor_malloc_zero(sizeof(ed25519_keypair_t));
  341. if (ed25519_keypair_generate(keypair, extra_strong) < 0)
  342. goto err;
  343. if (! (flags & INIT_ED_KEY_NEEDCERT))
  344. return keypair;
  345. tor_assert(signing_key);
  346. tor_assert(cert_out);
  347. uint32_t cert_flags = 0;
  348. if (flags & INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT)
  349. cert_flags |= CERT_FLAG_INCLUDE_SIGNING_KEY;
  350. tor_cert_t *cert = tor_cert_create(signing_key, cert_type,
  351. &keypair->pubkey,
  352. now, lifetime,
  353. cert_flags);
  354. if (! cert)
  355. goto err;
  356. *cert_out = cert;
  357. return keypair;
  358. err:
  359. tor_free(keypair);
  360. return NULL;
  361. }
  362. static ed25519_keypair_t *master_identity_key = NULL;
  363. static ed25519_keypair_t *master_signing_key = NULL;
  364. static ed25519_keypair_t *current_auth_key = NULL;
  365. static tor_cert_t *signing_key_cert = NULL;
  366. static tor_cert_t *link_cert_cert = NULL;
  367. static tor_cert_t *auth_key_cert = NULL;
  368. static uint8_t *rsa_ed_crosscert = NULL;
  369. static size_t rsa_ed_crosscert_len = 0;
  370. /**
  371. * Running as a server: load, reload, or refresh our ed25519 keys and
  372. * certificates, creating and saving new ones as needed.
  373. */
  374. int
  375. load_ed_keys(const or_options_t *options, time_t now)
  376. {
  377. ed25519_keypair_t *id = NULL;
  378. ed25519_keypair_t *sign = NULL;
  379. ed25519_keypair_t *auth = NULL;
  380. const ed25519_keypair_t *sign_signing_key_with_id = NULL;
  381. const ed25519_keypair_t *use_signing = NULL;
  382. const tor_cert_t *check_signing_cert = NULL;
  383. tor_cert_t *sign_cert = NULL;
  384. tor_cert_t *auth_cert = NULL;
  385. #define FAIL(msg) do { \
  386. log_warn(LD_OR, (msg)); \
  387. goto err; \
  388. } while (0)
  389. #define SET_KEY(key, newval) do { \
  390. ed25519_keypair_free(key); \
  391. key = (newval); \
  392. } while (0)
  393. #define SET_CERT(cert, newval) do { \
  394. tor_cert_free(cert); \
  395. cert = (newval); \
  396. } while (0)
  397. #define EXPIRES_SOON(cert, interval) \
  398. (!(cert) || (cert)->valid_until < now + (interval))
  399. /* XXXX support encrypted identity keys fully */
  400. /* First try to get the signing key to see how it is. */
  401. if (master_signing_key) {
  402. check_signing_cert = signing_key_cert;
  403. use_signing = master_signing_key;
  404. } else {
  405. char *fname =
  406. options_get_datadir_fname2(options, "keys", "ed25519_signing");
  407. sign = ed_key_init_from_file(
  408. fname,
  409. INIT_ED_KEY_NEEDCERT|
  410. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT,
  411. LOG_INFO,
  412. NULL, 0, 0, CERT_TYPE_ID_SIGNING, &sign_cert);
  413. tor_free(fname);
  414. check_signing_cert = sign_cert;
  415. use_signing = sign;
  416. }
  417. const int need_new_signing_key =
  418. NULL == use_signing ||
  419. EXPIRES_SOON(check_signing_cert, 0) ||
  420. options->command == CMD_KEYGEN;
  421. const int want_new_signing_key =
  422. need_new_signing_key ||
  423. EXPIRES_SOON(check_signing_cert, options->TestingSigningKeySlop);
  424. {
  425. uint32_t flags =
  426. (INIT_ED_KEY_CREATE|INIT_ED_KEY_SPLIT|
  427. INIT_ED_KEY_EXTRA_STRONG);
  428. if (! need_new_signing_key)
  429. flags |= INIT_ED_KEY_MISSING_SECRET_OK;
  430. if (! want_new_signing_key)
  431. flags |= INIT_ED_KEY_OMIT_SECRET;
  432. if (options->command == CMD_KEYGEN)
  433. flags |= INIT_ED_KEY_TRY_ENCRYPTED;
  434. char *fname =
  435. options_get_datadir_fname2(options, "keys", "ed25519_master_id");
  436. id = ed_key_init_from_file(
  437. fname,
  438. flags,
  439. LOG_WARN, NULL, 0, 0, 0, NULL);
  440. tor_free(fname);
  441. if (!id)
  442. FAIL("Missing identity key");
  443. if (tor_mem_is_zero((char*)id->seckey.seckey, sizeof(id->seckey)))
  444. sign_signing_key_with_id = NULL;
  445. else
  446. sign_signing_key_with_id = id;
  447. }
  448. if (need_new_signing_key && NULL == sign_signing_key_with_id)
  449. FAIL("Can't load master key make a new signing key.");
  450. if (want_new_signing_key && sign_signing_key_with_id) {
  451. uint32_t flags = (INIT_ED_KEY_CREATE|
  452. INIT_ED_KEY_REPLACE|
  453. INIT_ED_KEY_EXTRA_STRONG|
  454. INIT_ED_KEY_NEEDCERT|
  455. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT);
  456. char *fname =
  457. options_get_datadir_fname2(options, "keys", "ed25519_signing");
  458. sign = ed_key_init_from_file(fname,
  459. flags, LOG_WARN,
  460. sign_signing_key_with_id, now,
  461. options->SigningKeyLifetime,
  462. CERT_TYPE_ID_SIGNING, &sign_cert);
  463. tor_free(fname);
  464. if (!sign)
  465. FAIL("Missing signing key");
  466. use_signing = sign;
  467. } else if (want_new_signing_key) {
  468. static ratelim_t missing_master = RATELIM_INIT(3600);
  469. log_fn_ratelim(&missing_master, LOG_WARN, LD_OR,
  470. "Signing key will expire soon, but I can't load the "
  471. "master key to sign a new one!");
  472. }
  473. tor_assert(use_signing);
  474. /* At this point we no longer need our secret identity key. So wipe
  475. * it, if we loaded it in the first place. */
  476. memwipe(id->seckey.seckey, 0, sizeof(id->seckey));
  477. if (!rsa_ed_crosscert && server_mode(options)) {
  478. uint8_t *crosscert;
  479. ssize_t crosscert_len = tor_make_rsa_ed25519_crosscert(&id->pubkey,
  480. get_server_identity_key(),
  481. now+10*365*86400,/*XXXX*/
  482. &crosscert);
  483. rsa_ed_crosscert_len = crosscert_len;
  484. rsa_ed_crosscert = crosscert;
  485. }
  486. if (!current_auth_key ||
  487. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop)) {
  488. auth = ed_key_new(use_signing, INIT_ED_KEY_NEEDCERT,
  489. now,
  490. options->TestingAuthKeyLifetime,
  491. CERT_TYPE_SIGNING_AUTH, &auth_cert);
  492. if (!auth)
  493. FAIL("Can't create auth key");
  494. }
  495. /* We've generated or loaded everything. Put them in memory. */
  496. if (! master_identity_key) {
  497. SET_KEY(master_identity_key, id);
  498. } else {
  499. tor_free(id);
  500. }
  501. if (sign) {
  502. SET_KEY(master_signing_key, sign);
  503. SET_CERT(signing_key_cert, sign_cert);
  504. }
  505. if (auth) {
  506. SET_KEY(current_auth_key, auth);
  507. SET_CERT(auth_key_cert, auth_cert);
  508. }
  509. return 0;
  510. err:
  511. ed25519_keypair_free(id);
  512. ed25519_keypair_free(sign);
  513. ed25519_keypair_free(auth);
  514. tor_cert_free(sign_cert);
  515. tor_cert_free(auth_cert);
  516. return -1;
  517. }
  518. /**DOCDOC*/
  519. int
  520. generate_ed_link_cert(const or_options_t *options, time_t now)
  521. {
  522. const tor_x509_cert_t *link = NULL, *id = NULL;
  523. tor_cert_t *link_cert = NULL;
  524. if (tor_tls_get_my_certs(1, &link, &id) < 0 || link == NULL) {
  525. log_warn(LD_OR, "Can't get my x509 link cert.");
  526. return -1;
  527. }
  528. const digests_t *digests = tor_x509_cert_get_cert_digests(link);
  529. if (link_cert_cert &&
  530. ! EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop) &&
  531. fast_memeq(digests->d[DIGEST_SHA256], link_cert_cert->signed_key.pubkey,
  532. DIGEST256_LEN)) {
  533. return 0;
  534. }
  535. ed25519_public_key_t dummy_key;
  536. memcpy(dummy_key.pubkey, digests->d[DIGEST_SHA256], DIGEST256_LEN);
  537. link_cert = tor_cert_create(get_master_signing_keypair(),
  538. CERT_TYPE_SIGNING_LINK,
  539. &dummy_key,
  540. now,
  541. options->TestingLinkCertLifetime, 0);
  542. if (link_cert) {
  543. SET_CERT(link_cert_cert, link_cert);
  544. }
  545. return 0;
  546. }
  547. #undef FAIL
  548. #undef SET_KEY
  549. #undef SET_CERT
  550. int
  551. should_make_new_ed_keys(const or_options_t *options, const time_t now)
  552. {
  553. if (!master_identity_key ||
  554. !master_signing_key ||
  555. !current_auth_key ||
  556. !link_cert_cert ||
  557. EXPIRES_SOON(signing_key_cert, options->TestingSigningKeySlop) ||
  558. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop) ||
  559. EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop))
  560. return 1;
  561. const tor_x509_cert_t *link = NULL, *id = NULL;
  562. if (tor_tls_get_my_certs(1, &link, &id) < 0 || link == NULL)
  563. return 1;
  564. const digests_t *digests = tor_x509_cert_get_cert_digests(link);
  565. if (!fast_memeq(digests->d[DIGEST_SHA256],
  566. link_cert_cert->signed_key.pubkey,
  567. DIGEST256_LEN)) {
  568. return 1;
  569. }
  570. return 0;
  571. }
  572. #undef EXPIRES_SOON
  573. const ed25519_public_key_t *
  574. get_master_identity_key(void)
  575. {
  576. if (!master_identity_key)
  577. return NULL;
  578. return &master_identity_key->pubkey;
  579. }
  580. const ed25519_keypair_t *
  581. get_master_signing_keypair(void)
  582. {
  583. return master_signing_key;
  584. }
  585. const struct tor_cert_st *
  586. get_master_signing_key_cert(void)
  587. {
  588. return signing_key_cert;
  589. }
  590. const ed25519_keypair_t *
  591. get_current_auth_keypair(void)
  592. {
  593. return current_auth_key;
  594. }
  595. const tor_cert_t *
  596. get_current_link_cert_cert(void)
  597. {
  598. return link_cert_cert;
  599. }
  600. const tor_cert_t *
  601. get_current_auth_key_cert(void)
  602. {
  603. return auth_key_cert;
  604. }
  605. void
  606. get_master_rsa_crosscert(const uint8_t **cert_out,
  607. size_t *size_out)
  608. {
  609. *cert_out = rsa_ed_crosscert;
  610. *size_out = rsa_ed_crosscert_len;
  611. }
  612. /** Construct cross-certification for the master identity key with
  613. * the ntor onion key. Store the sign of the corresponding ed25519 public key
  614. * in *<b>sign_out</b>. */
  615. tor_cert_t *
  616. make_ntor_onion_key_crosscert(const curve25519_keypair_t *onion_key,
  617. const ed25519_public_key_t *master_id_key, time_t now, time_t lifetime,
  618. int *sign_out)
  619. {
  620. tor_cert_t *cert = NULL;
  621. ed25519_keypair_t ed_onion_key;
  622. if (ed25519_keypair_from_curve25519_keypair(&ed_onion_key, sign_out,
  623. onion_key) < 0)
  624. goto end;
  625. cert = tor_cert_create(&ed_onion_key, CERT_TYPE_ONION_ID, master_id_key,
  626. now, lifetime, 0);
  627. end:
  628. memwipe(&ed_onion_key, 0, sizeof(ed_onion_key));
  629. return cert;
  630. }
  631. /** Construct and return an RSA signature for the TAP onion key to
  632. * cross-certify the RSA and Ed25519 identity keys. Set <b>len_out</b> to its
  633. * length. */
  634. uint8_t *
  635. make_tap_onion_key_crosscert(const crypto_pk_t *onion_key,
  636. const ed25519_public_key_t *master_id_key,
  637. const crypto_pk_t *rsa_id_key,
  638. int *len_out)
  639. {
  640. uint8_t signature[PK_BYTES];
  641. uint8_t signed_data[DIGEST_LEN + ED25519_PUBKEY_LEN];
  642. *len_out = 0;
  643. crypto_pk_get_digest(rsa_id_key, (char*)signed_data);
  644. memcpy(signed_data + DIGEST_LEN, master_id_key->pubkey, ED25519_PUBKEY_LEN);
  645. int r = crypto_pk_private_sign(onion_key,
  646. (char*)signature, sizeof(signature),
  647. (const char*)signed_data, sizeof(signed_data));
  648. if (r < 0)
  649. return NULL;
  650. *len_out = r;
  651. return tor_memdup(signature, r);
  652. }
  653. /** Check whether an RSA-TAP cross-certification is correct. Return 0 if it
  654. * is, -1 if it isn't. */
  655. int
  656. check_tap_onion_key_crosscert(const uint8_t *crosscert,
  657. int crosscert_len,
  658. const crypto_pk_t *onion_pkey,
  659. const ed25519_public_key_t *master_id_pkey,
  660. const uint8_t *rsa_id_digest)
  661. {
  662. uint8_t *cc = tor_malloc(crypto_pk_keysize(onion_pkey));
  663. int cc_len =
  664. crypto_pk_public_checksig(onion_pkey,
  665. (char*)cc,
  666. crypto_pk_keysize(onion_pkey),
  667. (const char*)crosscert,
  668. crosscert_len);
  669. if (cc_len < 0) {
  670. goto err;
  671. }
  672. if (cc_len < DIGEST_LEN + ED25519_PUBKEY_LEN) {
  673. log_warn(LD_DIR, "Short signature on cross-certification with TAP key");
  674. goto err;
  675. }
  676. if (tor_memneq(cc, rsa_id_digest, DIGEST_LEN) ||
  677. tor_memneq(cc + DIGEST_LEN, master_id_pkey->pubkey,
  678. ED25519_PUBKEY_LEN)) {
  679. log_warn(LD_DIR, "Incorrect cross-certification with TAP key");
  680. goto err;
  681. }
  682. tor_free(cc);
  683. return 0;
  684. err:
  685. tor_free(cc);
  686. return -1;
  687. }
  688. void
  689. routerkeys_free_all(void)
  690. {
  691. ed25519_keypair_free(master_identity_key);
  692. ed25519_keypair_free(master_signing_key);
  693. ed25519_keypair_free(current_auth_key);
  694. tor_cert_free(signing_key_cert);
  695. tor_cert_free(link_cert_cert);
  696. tor_cert_free(auth_key_cert);
  697. master_identity_key = master_signing_key = NULL;
  698. current_auth_key = NULL;
  699. signing_key_cert = link_cert_cert = auth_key_cert = NULL;
  700. }