routerkeys.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. /* Check the key directory */
  435. if (check_private_dir(options->DataDirectory, CPD_CREATE, options->User)) {
  436. log_err(LD_OR, "Can't create/check datadirectory %s",
  437. options->DataDirectory);
  438. goto err;
  439. }
  440. char *fname = get_datadir_fname("keys");
  441. if (check_private_dir(fname, CPD_CREATE, options->User) < 0) {
  442. log_err(LD_OR, "Problem creating/checking key directory %s", fname);
  443. tor_free(fname);
  444. goto err;
  445. }
  446. fname = options_get_datadir_fname2(options, "keys", "ed25519_master_id");
  447. id = ed_key_init_from_file(
  448. fname,
  449. flags,
  450. LOG_WARN, NULL, 0, 0, 0, NULL);
  451. tor_free(fname);
  452. if (!id)
  453. FAIL("Missing identity key");
  454. if (tor_mem_is_zero((char*)id->seckey.seckey, sizeof(id->seckey)))
  455. sign_signing_key_with_id = NULL;
  456. else
  457. sign_signing_key_with_id = id;
  458. }
  459. if (need_new_signing_key && NULL == sign_signing_key_with_id)
  460. FAIL("Can't load master key make a new signing key.");
  461. if (want_new_signing_key && sign_signing_key_with_id) {
  462. uint32_t flags = (INIT_ED_KEY_CREATE|
  463. INIT_ED_KEY_REPLACE|
  464. INIT_ED_KEY_EXTRA_STRONG|
  465. INIT_ED_KEY_NEEDCERT|
  466. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT);
  467. char *fname =
  468. options_get_datadir_fname2(options, "keys", "ed25519_signing");
  469. sign = ed_key_init_from_file(fname,
  470. flags, LOG_WARN,
  471. sign_signing_key_with_id, now,
  472. options->SigningKeyLifetime,
  473. CERT_TYPE_ID_SIGNING, &sign_cert);
  474. tor_free(fname);
  475. if (!sign)
  476. FAIL("Missing signing key");
  477. use_signing = sign;
  478. } else if (want_new_signing_key) {
  479. static ratelim_t missing_master = RATELIM_INIT(3600);
  480. log_fn_ratelim(&missing_master, LOG_WARN, LD_OR,
  481. "Signing key will expire soon, but I can't load the "
  482. "master key to sign a new one!");
  483. }
  484. tor_assert(use_signing);
  485. /* At this point we no longer need our secret identity key. So wipe
  486. * it, if we loaded it in the first place. */
  487. memwipe(id->seckey.seckey, 0, sizeof(id->seckey));
  488. if (!rsa_ed_crosscert && server_mode(options)) {
  489. uint8_t *crosscert;
  490. ssize_t crosscert_len = tor_make_rsa_ed25519_crosscert(&id->pubkey,
  491. get_server_identity_key(),
  492. now+10*365*86400,/*XXXX*/
  493. &crosscert);
  494. rsa_ed_crosscert_len = crosscert_len;
  495. rsa_ed_crosscert = crosscert;
  496. }
  497. if (!current_auth_key ||
  498. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop)) {
  499. auth = ed_key_new(use_signing, INIT_ED_KEY_NEEDCERT,
  500. now,
  501. options->TestingAuthKeyLifetime,
  502. CERT_TYPE_SIGNING_AUTH, &auth_cert);
  503. if (!auth)
  504. FAIL("Can't create auth key");
  505. }
  506. /* We've generated or loaded everything. Put them in memory. */
  507. if (! master_identity_key) {
  508. SET_KEY(master_identity_key, id);
  509. } else {
  510. tor_free(id);
  511. }
  512. if (sign) {
  513. SET_KEY(master_signing_key, sign);
  514. SET_CERT(signing_key_cert, sign_cert);
  515. }
  516. if (auth) {
  517. SET_KEY(current_auth_key, auth);
  518. SET_CERT(auth_key_cert, auth_cert);
  519. }
  520. return 0;
  521. err:
  522. ed25519_keypair_free(id);
  523. ed25519_keypair_free(sign);
  524. ed25519_keypair_free(auth);
  525. tor_cert_free(sign_cert);
  526. tor_cert_free(auth_cert);
  527. return -1;
  528. }
  529. /**DOCDOC*/
  530. int
  531. generate_ed_link_cert(const or_options_t *options, time_t now)
  532. {
  533. const tor_x509_cert_t *link = NULL, *id = NULL;
  534. tor_cert_t *link_cert = NULL;
  535. if (tor_tls_get_my_certs(1, &link, &id) < 0 || link == NULL) {
  536. log_warn(LD_OR, "Can't get my x509 link cert.");
  537. return -1;
  538. }
  539. const digests_t *digests = tor_x509_cert_get_cert_digests(link);
  540. if (link_cert_cert &&
  541. ! EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop) &&
  542. fast_memeq(digests->d[DIGEST_SHA256], link_cert_cert->signed_key.pubkey,
  543. DIGEST256_LEN)) {
  544. return 0;
  545. }
  546. ed25519_public_key_t dummy_key;
  547. memcpy(dummy_key.pubkey, digests->d[DIGEST_SHA256], DIGEST256_LEN);
  548. link_cert = tor_cert_create(get_master_signing_keypair(),
  549. CERT_TYPE_SIGNING_LINK,
  550. &dummy_key,
  551. now,
  552. options->TestingLinkCertLifetime, 0);
  553. if (link_cert) {
  554. SET_CERT(link_cert_cert, link_cert);
  555. }
  556. return 0;
  557. }
  558. #undef FAIL
  559. #undef SET_KEY
  560. #undef SET_CERT
  561. int
  562. should_make_new_ed_keys(const or_options_t *options, const time_t now)
  563. {
  564. if (!master_identity_key ||
  565. !master_signing_key ||
  566. !current_auth_key ||
  567. !link_cert_cert ||
  568. EXPIRES_SOON(signing_key_cert, options->TestingSigningKeySlop) ||
  569. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop) ||
  570. EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop))
  571. return 1;
  572. const tor_x509_cert_t *link = NULL, *id = NULL;
  573. if (tor_tls_get_my_certs(1, &link, &id) < 0 || link == NULL)
  574. return 1;
  575. const digests_t *digests = tor_x509_cert_get_cert_digests(link);
  576. if (!fast_memeq(digests->d[DIGEST_SHA256],
  577. link_cert_cert->signed_key.pubkey,
  578. DIGEST256_LEN)) {
  579. return 1;
  580. }
  581. return 0;
  582. }
  583. #undef EXPIRES_SOON
  584. const ed25519_public_key_t *
  585. get_master_identity_key(void)
  586. {
  587. if (!master_identity_key)
  588. return NULL;
  589. return &master_identity_key->pubkey;
  590. }
  591. const ed25519_keypair_t *
  592. get_master_signing_keypair(void)
  593. {
  594. return master_signing_key;
  595. }
  596. const struct tor_cert_st *
  597. get_master_signing_key_cert(void)
  598. {
  599. return signing_key_cert;
  600. }
  601. const ed25519_keypair_t *
  602. get_current_auth_keypair(void)
  603. {
  604. return current_auth_key;
  605. }
  606. const tor_cert_t *
  607. get_current_link_cert_cert(void)
  608. {
  609. return link_cert_cert;
  610. }
  611. const tor_cert_t *
  612. get_current_auth_key_cert(void)
  613. {
  614. return auth_key_cert;
  615. }
  616. void
  617. get_master_rsa_crosscert(const uint8_t **cert_out,
  618. size_t *size_out)
  619. {
  620. *cert_out = rsa_ed_crosscert;
  621. *size_out = rsa_ed_crosscert_len;
  622. }
  623. /** Construct cross-certification for the master identity key with
  624. * the ntor onion key. Store the sign of the corresponding ed25519 public key
  625. * in *<b>sign_out</b>. */
  626. tor_cert_t *
  627. make_ntor_onion_key_crosscert(const curve25519_keypair_t *onion_key,
  628. const ed25519_public_key_t *master_id_key, time_t now, time_t lifetime,
  629. int *sign_out)
  630. {
  631. tor_cert_t *cert = NULL;
  632. ed25519_keypair_t ed_onion_key;
  633. if (ed25519_keypair_from_curve25519_keypair(&ed_onion_key, sign_out,
  634. onion_key) < 0)
  635. goto end;
  636. cert = tor_cert_create(&ed_onion_key, CERT_TYPE_ONION_ID, master_id_key,
  637. now, lifetime, 0);
  638. end:
  639. memwipe(&ed_onion_key, 0, sizeof(ed_onion_key));
  640. return cert;
  641. }
  642. /** Construct and return an RSA signature for the TAP onion key to
  643. * cross-certify the RSA and Ed25519 identity keys. Set <b>len_out</b> to its
  644. * length. */
  645. uint8_t *
  646. make_tap_onion_key_crosscert(const crypto_pk_t *onion_key,
  647. const ed25519_public_key_t *master_id_key,
  648. const crypto_pk_t *rsa_id_key,
  649. int *len_out)
  650. {
  651. uint8_t signature[PK_BYTES];
  652. uint8_t signed_data[DIGEST_LEN + ED25519_PUBKEY_LEN];
  653. *len_out = 0;
  654. crypto_pk_get_digest(rsa_id_key, (char*)signed_data);
  655. memcpy(signed_data + DIGEST_LEN, master_id_key->pubkey, ED25519_PUBKEY_LEN);
  656. int r = crypto_pk_private_sign(onion_key,
  657. (char*)signature, sizeof(signature),
  658. (const char*)signed_data, sizeof(signed_data));
  659. if (r < 0)
  660. return NULL;
  661. *len_out = r;
  662. return tor_memdup(signature, r);
  663. }
  664. /** Check whether an RSA-TAP cross-certification is correct. Return 0 if it
  665. * is, -1 if it isn't. */
  666. int
  667. check_tap_onion_key_crosscert(const uint8_t *crosscert,
  668. int crosscert_len,
  669. const crypto_pk_t *onion_pkey,
  670. const ed25519_public_key_t *master_id_pkey,
  671. const uint8_t *rsa_id_digest)
  672. {
  673. uint8_t *cc = tor_malloc(crypto_pk_keysize(onion_pkey));
  674. int cc_len =
  675. crypto_pk_public_checksig(onion_pkey,
  676. (char*)cc,
  677. crypto_pk_keysize(onion_pkey),
  678. (const char*)crosscert,
  679. crosscert_len);
  680. if (cc_len < 0) {
  681. goto err;
  682. }
  683. if (cc_len < DIGEST_LEN + ED25519_PUBKEY_LEN) {
  684. log_warn(LD_DIR, "Short signature on cross-certification with TAP key");
  685. goto err;
  686. }
  687. if (tor_memneq(cc, rsa_id_digest, DIGEST_LEN) ||
  688. tor_memneq(cc + DIGEST_LEN, master_id_pkey->pubkey,
  689. ED25519_PUBKEY_LEN)) {
  690. log_warn(LD_DIR, "Incorrect cross-certification with TAP key");
  691. goto err;
  692. }
  693. tor_free(cc);
  694. return 0;
  695. err:
  696. tor_free(cc);
  697. return -1;
  698. }
  699. void
  700. routerkeys_free_all(void)
  701. {
  702. ed25519_keypair_free(master_identity_key);
  703. ed25519_keypair_free(master_signing_key);
  704. ed25519_keypair_free(current_auth_key);
  705. tor_cert_free(signing_key_cert);
  706. tor_cert_free(link_cert_cert);
  707. tor_cert_free(auth_key_cert);
  708. master_identity_key = master_signing_key = NULL;
  709. current_auth_key = NULL;
  710. signing_key_cert = link_cert_cert = auth_key_cert = NULL;
  711. }