routerkeys.c 29 KB

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