routerkeys.c 25 KB

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