routerkeys.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. const char *loaded_secret_fname = NULL;
  181. int created_pk = 0, created_sk = 0, created_cert = 0;
  182. const int try_to_load = ! (flags & INIT_ED_KEY_REPLACE);
  183. const int encrypt_key = !! (flags & INIT_ED_KEY_TRY_ENCRYPTED);
  184. const int norepair = !! (flags & INIT_ED_KEY_NO_REPAIR);
  185. const int split = !! (flags & INIT_ED_KEY_SPLIT);
  186. /* we don't support setting both of these flags at once. */
  187. tor_assert((flags & (INIT_ED_KEY_NO_REPAIR|INIT_ED_KEY_NEEDCERT)) !=
  188. (INIT_ED_KEY_NO_REPAIR|INIT_ED_KEY_NEEDCERT));
  189. char tag[8];
  190. tor_snprintf(tag, sizeof(tag), "type%d", (int)cert_type);
  191. tor_cert_t *cert = NULL;
  192. char *got_tag = NULL;
  193. ed25519_keypair_t *keypair = tor_malloc_zero(sizeof(ed25519_keypair_t));
  194. tor_asprintf(&secret_fname, "%s_secret_key", fname);
  195. tor_asprintf(&encrypted_secret_fname, "%s_secret_key_encrypted", fname);
  196. tor_asprintf(&public_fname, "%s_public_key", fname);
  197. tor_asprintf(&cert_fname, "%s_cert", fname);
  198. /* Try to read the secret key. */
  199. int have_secret = 0;
  200. if (try_to_load &&
  201. !(flags & INIT_ED_KEY_OMIT_SECRET)) {
  202. int rv = ed25519_seckey_read_from_file(&keypair->seckey,
  203. &got_tag, secret_fname);
  204. if (rv == 0) {
  205. have_secret = 1;
  206. loaded_secret_fname = secret_fname;
  207. } else {
  208. if (errno != ENOENT && norepair) {
  209. tor_log(severity, LD_OR, "Unable to read %s: %s", secret_fname,
  210. strerror(errno));
  211. goto err;
  212. }
  213. }
  214. }
  215. /* Should we try for an encrypted key? */
  216. if (!have_secret && try_to_load && encrypt_key) {
  217. int r = read_encrypted_secret_key(&keypair->seckey,
  218. encrypted_secret_fname);
  219. if (r > 0) {
  220. have_secret = 1;
  221. got_tag = tor_strdup(tag);
  222. loaded_secret_fname = encrypted_secret_fname;
  223. } else if (errno != ENOENT && norepair) {
  224. tor_log(severity, LD_OR, "Unable to read %s: %s", encrypted_secret_fname,
  225. strerror(errno));
  226. goto err;
  227. }
  228. }
  229. if (have_secret) {
  230. if (strcmp(got_tag, tag)) {
  231. tor_log(severity, LD_OR, "%s has wrong tag", loaded_secret_fname);
  232. goto err;
  233. }
  234. /* Derive the public key */
  235. if (ed25519_public_key_generate(&keypair->pubkey, &keypair->seckey)<0) {
  236. tor_log(severity, LD_OR, "%s can't produce a public key",
  237. loaded_secret_fname);
  238. goto err;
  239. }
  240. }
  241. /* If it's absent and that's okay, or if we do split keys here, try to re
  242. * the pubkey. */
  243. int found_public = 0;
  244. if ((!have_secret && try_to_load) || (have_secret && split)) {
  245. ed25519_public_key_t pubkey_tmp;
  246. tor_free(got_tag);
  247. found_public = ed25519_pubkey_read_from_file(&pubkey_tmp,
  248. &got_tag, public_fname) == 0;
  249. if (!found_public && errno != ENOENT && norepair) {
  250. tor_log(severity, LD_OR, "Unable to read %s: %s", public_fname,
  251. strerror(errno));
  252. goto err;
  253. }
  254. if (found_public && strcmp(got_tag, tag)) {
  255. tor_log(severity, LD_OR, "%s has wrong tag", public_fname);
  256. goto err;
  257. }
  258. if (found_public) {
  259. if (have_secret) {
  260. /* If we have a secret key and we're reloading the public key,
  261. * the key must match! */
  262. if (! ed25519_pubkey_eq(&keypair->pubkey, &pubkey_tmp)) {
  263. tor_log(severity, LD_OR, "%s does not match %s!",
  264. public_fname, loaded_secret_fname);
  265. goto err;
  266. }
  267. } else {
  268. tor_assert(split);
  269. memcpy(&keypair->pubkey, &pubkey_tmp, sizeof(pubkey_tmp));
  270. }
  271. }
  272. }
  273. /* If the secret key is absent and it's not allowed to be, fail. */
  274. if (!have_secret && found_public && !(flags & INIT_ED_KEY_MISSING_SECRET_OK))
  275. goto err;
  276. /* If it's absent, and we're not supposed to make a new keypair, fail. */
  277. if (!have_secret && !found_public && !(flags & INIT_ED_KEY_CREATE))
  278. goto err;
  279. /* if it's absent, make a new keypair and save it. */
  280. if (!have_secret && !found_public) {
  281. tor_free(keypair);
  282. keypair = ed_key_new(signing_key, flags, now, lifetime,
  283. cert_type, &cert);
  284. if (!keypair) {
  285. tor_log(severity, LD_OR, "Couldn't create keypair");
  286. goto err;
  287. }
  288. created_pk = created_sk = created_cert = 1;
  289. if (write_secret_key(&keypair->seckey,
  290. encrypt_key,
  291. secret_fname, tag, encrypted_secret_fname) < 0
  292. ||
  293. (split &&
  294. ed25519_pubkey_write_to_file(&keypair->pubkey, public_fname, tag) < 0)
  295. ||
  296. (cert &&
  297. crypto_write_tagged_contents_to_file(cert_fname, "ed25519v1-cert",
  298. tag, cert->encoded, cert->encoded_len) < 0)) {
  299. tor_log(severity, LD_OR, "Couldn't write keys or cert to file.");
  300. goto err;
  301. }
  302. goto done;
  303. }
  304. /* If we're not supposed to get a cert, we're done. */
  305. if (! (flags & INIT_ED_KEY_NEEDCERT))
  306. goto done;
  307. /* Read a cert. */
  308. tor_free(got_tag);
  309. uint8_t certbuf[256];
  310. ssize_t cert_body_len = crypto_read_tagged_contents_from_file(
  311. cert_fname, "ed25519v1-cert",
  312. &got_tag, certbuf, sizeof(certbuf));
  313. if (cert_body_len >= 0 && !strcmp(got_tag, tag))
  314. cert = tor_cert_parse(certbuf, cert_body_len);
  315. /* If we got it, check it to the extent we can. */
  316. int bad_cert = 0;
  317. if (! cert) {
  318. tor_log(severity, LD_OR, "Cert was unparseable");
  319. bad_cert = 1;
  320. } else if (!tor_memeq(cert->signed_key.pubkey, keypair->pubkey.pubkey,
  321. ED25519_PUBKEY_LEN)) {
  322. tor_log(severity, LD_OR, "Cert was for wrong key");
  323. bad_cert = 1;
  324. } else if (signing_key &&
  325. tor_cert_checksig(cert, &signing_key->pubkey, now) < 0 &&
  326. (signing_key || cert->cert_expired)) {
  327. tor_log(severity, LD_OR, "Can't check certificate");
  328. bad_cert = 1;
  329. } else if (signing_key && cert->signing_key_included &&
  330. ! ed25519_pubkey_eq(&signing_key->pubkey, &cert->signing_key)) {
  331. tor_log(severity, LD_OR, "Certificate signed by unexpectd key!");
  332. bad_cert = 1;
  333. }
  334. if (bad_cert) {
  335. tor_cert_free(cert);
  336. cert = NULL;
  337. }
  338. /* If we got a cert, we're done. */
  339. if (cert)
  340. goto done;
  341. /* If we didn't get a cert, and we're not supposed to make one, fail. */
  342. if (!signing_key || !(flags & INIT_ED_KEY_CREATE))
  343. goto err;
  344. /* We have keys but not a certificate, so make one. */
  345. uint32_t cert_flags = 0;
  346. if (flags & INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT)
  347. cert_flags |= CERT_FLAG_INCLUDE_SIGNING_KEY;
  348. cert = tor_cert_create(signing_key, cert_type,
  349. &keypair->pubkey,
  350. now, lifetime,
  351. cert_flags);
  352. if (! cert)
  353. goto err;
  354. /* Write it to disk. */
  355. created_cert = 1;
  356. if (crypto_write_tagged_contents_to_file(cert_fname, "ed25519v1-cert",
  357. tag, cert->encoded, cert->encoded_len) < 0) {
  358. tor_log(severity, LD_OR, "Couldn't write cert to disk.");
  359. goto err;
  360. }
  361. done:
  362. if (cert_out)
  363. *cert_out = cert;
  364. else
  365. tor_cert_free(cert);
  366. goto cleanup;
  367. err:
  368. if (keypair)
  369. memwipe(keypair, 0, sizeof(*keypair));
  370. tor_free(keypair);
  371. tor_cert_free(cert);
  372. if (cert_out)
  373. *cert_out = NULL;
  374. if (created_sk)
  375. unlink(secret_fname);
  376. if (created_pk)
  377. unlink(public_fname);
  378. if (created_cert)
  379. unlink(cert_fname);
  380. cleanup:
  381. tor_free(encrypted_secret_fname);
  382. tor_free(secret_fname);
  383. tor_free(public_fname);
  384. tor_free(cert_fname);
  385. tor_free(got_tag);
  386. return keypair;
  387. }
  388. /**
  389. * Create a new signing key and (optionally) certficiate; do not read or write
  390. * from disk. See ed_key_init_from_file() for more information.
  391. */
  392. ed25519_keypair_t *
  393. ed_key_new(const ed25519_keypair_t *signing_key,
  394. uint32_t flags,
  395. time_t now,
  396. time_t lifetime,
  397. uint8_t cert_type,
  398. struct tor_cert_st **cert_out)
  399. {
  400. if (cert_out)
  401. *cert_out = NULL;
  402. const int extra_strong = !! (flags & INIT_ED_KEY_EXTRA_STRONG);
  403. ed25519_keypair_t *keypair = tor_malloc_zero(sizeof(ed25519_keypair_t));
  404. if (ed25519_keypair_generate(keypair, extra_strong) < 0)
  405. goto err;
  406. if (! (flags & INIT_ED_KEY_NEEDCERT))
  407. return keypair;
  408. tor_assert(signing_key);
  409. tor_assert(cert_out);
  410. uint32_t cert_flags = 0;
  411. if (flags & INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT)
  412. cert_flags |= CERT_FLAG_INCLUDE_SIGNING_KEY;
  413. tor_cert_t *cert = tor_cert_create(signing_key, cert_type,
  414. &keypair->pubkey,
  415. now, lifetime,
  416. cert_flags);
  417. if (! cert)
  418. goto err;
  419. *cert_out = cert;
  420. return keypair;
  421. err:
  422. tor_free(keypair);
  423. return NULL;
  424. }
  425. static ed25519_keypair_t *master_identity_key = NULL;
  426. static ed25519_keypair_t *master_signing_key = NULL;
  427. static ed25519_keypair_t *current_auth_key = NULL;
  428. static tor_cert_t *signing_key_cert = NULL;
  429. static tor_cert_t *link_cert_cert = NULL;
  430. static tor_cert_t *auth_key_cert = NULL;
  431. static uint8_t *rsa_ed_crosscert = NULL;
  432. static size_t rsa_ed_crosscert_len = 0;
  433. /**
  434. * Running as a server: load, reload, or refresh our ed25519 keys and
  435. * certificates, creating and saving new ones as needed.
  436. */
  437. int
  438. load_ed_keys(const or_options_t *options, time_t now)
  439. {
  440. ed25519_keypair_t *id = NULL;
  441. ed25519_keypair_t *sign = NULL;
  442. ed25519_keypair_t *auth = NULL;
  443. const ed25519_keypair_t *sign_signing_key_with_id = NULL;
  444. const ed25519_keypair_t *use_signing = NULL;
  445. const tor_cert_t *check_signing_cert = NULL;
  446. tor_cert_t *sign_cert = NULL;
  447. tor_cert_t *auth_cert = NULL;
  448. #define FAIL(msg) do { \
  449. log_warn(LD_OR, (msg)); \
  450. goto err; \
  451. } while (0)
  452. #define SET_KEY(key, newval) do { \
  453. ed25519_keypair_free(key); \
  454. key = (newval); \
  455. } while (0)
  456. #define SET_CERT(cert, newval) do { \
  457. tor_cert_free(cert); \
  458. cert = (newval); \
  459. } while (0)
  460. #define EXPIRES_SOON(cert, interval) \
  461. (!(cert) || (cert)->valid_until < now + (interval))
  462. /* XXXX support encrypted identity keys fully */
  463. /* First try to get the signing key to see how it is. */
  464. if (master_signing_key) {
  465. check_signing_cert = signing_key_cert;
  466. use_signing = master_signing_key;
  467. } else {
  468. char *fname =
  469. options_get_datadir_fname2(options, "keys", "ed25519_signing");
  470. sign = ed_key_init_from_file(
  471. fname,
  472. INIT_ED_KEY_NEEDCERT|
  473. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT,
  474. LOG_INFO,
  475. NULL, 0, 0, CERT_TYPE_ID_SIGNING, &sign_cert);
  476. tor_free(fname);
  477. check_signing_cert = sign_cert;
  478. use_signing = sign;
  479. }
  480. const int need_new_signing_key =
  481. NULL == use_signing ||
  482. EXPIRES_SOON(check_signing_cert, 0) ||
  483. options->command == CMD_KEYGEN;
  484. const int want_new_signing_key =
  485. need_new_signing_key ||
  486. EXPIRES_SOON(check_signing_cert, options->TestingSigningKeySlop);
  487. {
  488. uint32_t flags =
  489. (INIT_ED_KEY_CREATE|INIT_ED_KEY_SPLIT|
  490. INIT_ED_KEY_EXTRA_STRONG|INIT_ED_KEY_NO_REPAIR);
  491. if (! need_new_signing_key)
  492. flags |= INIT_ED_KEY_MISSING_SECRET_OK;
  493. if (! want_new_signing_key)
  494. flags |= INIT_ED_KEY_OMIT_SECRET;
  495. if (options->command == CMD_KEYGEN)
  496. flags |= INIT_ED_KEY_TRY_ENCRYPTED;
  497. /* Check the key directory */
  498. if (check_private_dir(options->DataDirectory, CPD_CREATE, options->User)) {
  499. log_err(LD_OR, "Can't create/check datadirectory %s",
  500. options->DataDirectory);
  501. goto err;
  502. }
  503. char *fname = get_datadir_fname("keys");
  504. if (check_private_dir(fname, CPD_CREATE, options->User) < 0) {
  505. log_err(LD_OR, "Problem creating/checking key directory %s", fname);
  506. tor_free(fname);
  507. goto err;
  508. }
  509. tor_free(fname);
  510. fname = options_get_datadir_fname2(options, "keys", "ed25519_master_id");
  511. id = ed_key_init_from_file(
  512. fname,
  513. flags,
  514. LOG_WARN, NULL, 0, 0, 0, NULL);
  515. tor_free(fname);
  516. if (!id)
  517. FAIL("Missing identity key");
  518. if (tor_mem_is_zero((char*)id->seckey.seckey, sizeof(id->seckey)))
  519. sign_signing_key_with_id = NULL;
  520. else
  521. sign_signing_key_with_id = id;
  522. }
  523. if (master_identity_key &&
  524. !ed25519_pubkey_eq(&id->pubkey, &master_identity_key->pubkey)) {
  525. FAIL("Identity key on disk does not match key we loaded earlier!");
  526. }
  527. if (need_new_signing_key && NULL == sign_signing_key_with_id)
  528. FAIL("Can't load master key make a new signing key.");
  529. if (sign_cert) {
  530. if (! sign_cert->signing_key_included)
  531. FAIL("Loaded a signing cert with no key included!");
  532. if (! ed25519_pubkey_eq(&sign_cert->signing_key, &id->pubkey))
  533. FAIL("The signing cert we have was not signed with the master key "
  534. "we loaded!");
  535. if (tor_cert_checksig(sign_cert, &id->pubkey, 0) < 0)
  536. FAIL("The signing cert we loaded was not signed correctly!");
  537. }
  538. if (want_new_signing_key && sign_signing_key_with_id) {
  539. uint32_t flags = (INIT_ED_KEY_CREATE|
  540. INIT_ED_KEY_REPLACE|
  541. INIT_ED_KEY_EXTRA_STRONG|
  542. INIT_ED_KEY_NEEDCERT|
  543. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT);
  544. char *fname =
  545. options_get_datadir_fname2(options, "keys", "ed25519_signing");
  546. sign = ed_key_init_from_file(fname,
  547. flags, LOG_WARN,
  548. sign_signing_key_with_id, now,
  549. options->SigningKeyLifetime,
  550. CERT_TYPE_ID_SIGNING, &sign_cert);
  551. tor_free(fname);
  552. if (!sign)
  553. FAIL("Missing signing key");
  554. use_signing = sign;
  555. tor_assert(sign_cert->signing_key_included);
  556. tor_assert(ed25519_pubkey_eq(&sign_cert->signing_key, &id->pubkey));
  557. tor_assert(ed25519_pubkey_eq(&sign_cert->signed_key, &sign->pubkey));
  558. } else if (want_new_signing_key) {
  559. static ratelim_t missing_master = RATELIM_INIT(3600);
  560. log_fn_ratelim(&missing_master, LOG_WARN, LD_OR,
  561. "Signing key will expire soon, but I can't load the "
  562. "master key to sign a new one!");
  563. }
  564. tor_assert(use_signing);
  565. /* At this point we no longer need our secret identity key. So wipe
  566. * it, if we loaded it in the first place. */
  567. memwipe(id->seckey.seckey, 0, sizeof(id->seckey));
  568. if (!rsa_ed_crosscert && server_mode(options)) {
  569. uint8_t *crosscert;
  570. ssize_t crosscert_len = tor_make_rsa_ed25519_crosscert(&id->pubkey,
  571. get_server_identity_key(),
  572. now+10*365*86400,/*XXXX*/
  573. &crosscert);
  574. rsa_ed_crosscert_len = crosscert_len;
  575. rsa_ed_crosscert = crosscert;
  576. }
  577. if (!current_auth_key ||
  578. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop)) {
  579. auth = ed_key_new(use_signing, INIT_ED_KEY_NEEDCERT,
  580. now,
  581. options->TestingAuthKeyLifetime,
  582. CERT_TYPE_SIGNING_AUTH, &auth_cert);
  583. if (!auth)
  584. FAIL("Can't create auth key");
  585. }
  586. /* We've generated or loaded everything. Put them in memory. */
  587. if (! master_identity_key) {
  588. SET_KEY(master_identity_key, id);
  589. } else {
  590. tor_free(id);
  591. }
  592. if (sign) {
  593. SET_KEY(master_signing_key, sign);
  594. SET_CERT(signing_key_cert, sign_cert);
  595. }
  596. if (auth) {
  597. SET_KEY(current_auth_key, auth);
  598. SET_CERT(auth_key_cert, auth_cert);
  599. }
  600. return 0;
  601. err:
  602. ed25519_keypair_free(id);
  603. ed25519_keypair_free(sign);
  604. ed25519_keypair_free(auth);
  605. tor_cert_free(sign_cert);
  606. tor_cert_free(auth_cert);
  607. return -1;
  608. }
  609. /**DOCDOC*/
  610. int
  611. generate_ed_link_cert(const or_options_t *options, time_t now)
  612. {
  613. const tor_x509_cert_t *link = NULL, *id = NULL;
  614. tor_cert_t *link_cert = NULL;
  615. if (tor_tls_get_my_certs(1, &link, &id) < 0 || link == NULL) {
  616. log_warn(LD_OR, "Can't get my x509 link cert.");
  617. return -1;
  618. }
  619. const digests_t *digests = tor_x509_cert_get_cert_digests(link);
  620. if (link_cert_cert &&
  621. ! EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop) &&
  622. fast_memeq(digests->d[DIGEST_SHA256], link_cert_cert->signed_key.pubkey,
  623. DIGEST256_LEN)) {
  624. return 0;
  625. }
  626. ed25519_public_key_t dummy_key;
  627. memcpy(dummy_key.pubkey, digests->d[DIGEST_SHA256], DIGEST256_LEN);
  628. link_cert = tor_cert_create(get_master_signing_keypair(),
  629. CERT_TYPE_SIGNING_LINK,
  630. &dummy_key,
  631. now,
  632. options->TestingLinkCertLifetime, 0);
  633. if (link_cert) {
  634. SET_CERT(link_cert_cert, link_cert);
  635. }
  636. return 0;
  637. }
  638. #undef FAIL
  639. #undef SET_KEY
  640. #undef SET_CERT
  641. int
  642. should_make_new_ed_keys(const or_options_t *options, const time_t now)
  643. {
  644. if (!master_identity_key ||
  645. !master_signing_key ||
  646. !current_auth_key ||
  647. !link_cert_cert ||
  648. EXPIRES_SOON(signing_key_cert, options->TestingSigningKeySlop) ||
  649. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop) ||
  650. EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop))
  651. return 1;
  652. const tor_x509_cert_t *link = NULL, *id = NULL;
  653. if (tor_tls_get_my_certs(1, &link, &id) < 0 || link == NULL)
  654. return 1;
  655. const digests_t *digests = tor_x509_cert_get_cert_digests(link);
  656. if (!fast_memeq(digests->d[DIGEST_SHA256],
  657. link_cert_cert->signed_key.pubkey,
  658. DIGEST256_LEN)) {
  659. return 1;
  660. }
  661. return 0;
  662. }
  663. #undef EXPIRES_SOON
  664. const ed25519_public_key_t *
  665. get_master_identity_key(void)
  666. {
  667. if (!master_identity_key)
  668. return NULL;
  669. return &master_identity_key->pubkey;
  670. }
  671. const ed25519_keypair_t *
  672. get_master_signing_keypair(void)
  673. {
  674. return master_signing_key;
  675. }
  676. const struct tor_cert_st *
  677. get_master_signing_key_cert(void)
  678. {
  679. return signing_key_cert;
  680. }
  681. const ed25519_keypair_t *
  682. get_current_auth_keypair(void)
  683. {
  684. return current_auth_key;
  685. }
  686. const tor_cert_t *
  687. get_current_link_cert_cert(void)
  688. {
  689. return link_cert_cert;
  690. }
  691. const tor_cert_t *
  692. get_current_auth_key_cert(void)
  693. {
  694. return auth_key_cert;
  695. }
  696. void
  697. get_master_rsa_crosscert(const uint8_t **cert_out,
  698. size_t *size_out)
  699. {
  700. *cert_out = rsa_ed_crosscert;
  701. *size_out = rsa_ed_crosscert_len;
  702. }
  703. /** Construct cross-certification for the master identity key with
  704. * the ntor onion key. Store the sign of the corresponding ed25519 public key
  705. * in *<b>sign_out</b>. */
  706. tor_cert_t *
  707. make_ntor_onion_key_crosscert(const curve25519_keypair_t *onion_key,
  708. const ed25519_public_key_t *master_id_key, time_t now, time_t lifetime,
  709. int *sign_out)
  710. {
  711. tor_cert_t *cert = NULL;
  712. ed25519_keypair_t ed_onion_key;
  713. if (ed25519_keypair_from_curve25519_keypair(&ed_onion_key, sign_out,
  714. onion_key) < 0)
  715. goto end;
  716. cert = tor_cert_create(&ed_onion_key, CERT_TYPE_ONION_ID, master_id_key,
  717. now, lifetime, 0);
  718. end:
  719. memwipe(&ed_onion_key, 0, sizeof(ed_onion_key));
  720. return cert;
  721. }
  722. /** Construct and return an RSA signature for the TAP onion key to
  723. * cross-certify the RSA and Ed25519 identity keys. Set <b>len_out</b> to its
  724. * length. */
  725. uint8_t *
  726. make_tap_onion_key_crosscert(const crypto_pk_t *onion_key,
  727. const ed25519_public_key_t *master_id_key,
  728. const crypto_pk_t *rsa_id_key,
  729. int *len_out)
  730. {
  731. uint8_t signature[PK_BYTES];
  732. uint8_t signed_data[DIGEST_LEN + ED25519_PUBKEY_LEN];
  733. *len_out = 0;
  734. crypto_pk_get_digest(rsa_id_key, (char*)signed_data);
  735. memcpy(signed_data + DIGEST_LEN, master_id_key->pubkey, ED25519_PUBKEY_LEN);
  736. int r = crypto_pk_private_sign(onion_key,
  737. (char*)signature, sizeof(signature),
  738. (const char*)signed_data, sizeof(signed_data));
  739. if (r < 0)
  740. return NULL;
  741. *len_out = r;
  742. return tor_memdup(signature, r);
  743. }
  744. /** Check whether an RSA-TAP cross-certification is correct. Return 0 if it
  745. * is, -1 if it isn't. */
  746. int
  747. check_tap_onion_key_crosscert(const uint8_t *crosscert,
  748. int crosscert_len,
  749. const crypto_pk_t *onion_pkey,
  750. const ed25519_public_key_t *master_id_pkey,
  751. const uint8_t *rsa_id_digest)
  752. {
  753. uint8_t *cc = tor_malloc(crypto_pk_keysize(onion_pkey));
  754. int cc_len =
  755. crypto_pk_public_checksig(onion_pkey,
  756. (char*)cc,
  757. crypto_pk_keysize(onion_pkey),
  758. (const char*)crosscert,
  759. crosscert_len);
  760. if (cc_len < 0) {
  761. goto err;
  762. }
  763. if (cc_len < DIGEST_LEN + ED25519_PUBKEY_LEN) {
  764. log_warn(LD_DIR, "Short signature on cross-certification with TAP key");
  765. goto err;
  766. }
  767. if (tor_memneq(cc, rsa_id_digest, DIGEST_LEN) ||
  768. tor_memneq(cc + DIGEST_LEN, master_id_pkey->pubkey,
  769. ED25519_PUBKEY_LEN)) {
  770. log_warn(LD_DIR, "Incorrect cross-certification with TAP key");
  771. goto err;
  772. }
  773. tor_free(cc);
  774. return 0;
  775. err:
  776. tor_free(cc);
  777. return -1;
  778. }
  779. void
  780. routerkeys_free_all(void)
  781. {
  782. ed25519_keypair_free(master_identity_key);
  783. ed25519_keypair_free(master_signing_key);
  784. ed25519_keypair_free(current_auth_key);
  785. tor_cert_free(signing_key_cert);
  786. tor_cert_free(link_cert_cert);
  787. tor_cert_free(auth_key_cert);
  788. master_identity_key = master_signing_key = NULL;
  789. current_auth_key = NULL;
  790. signing_key_cert = link_cert_cert = auth_key_cert = NULL;
  791. }