routerkeys.c 25 KB

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