routerkeys.c 30 KB

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