routerkeys.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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(LOG_NOTICE, LD_OR,
  345. "Found secret key but not %s. Regenerating.",
  346. public_fname);
  347. }
  348. }
  349. }
  350. }
  351. /* If the secret key is absent and it's not allowed to be, fail. */
  352. if (!have_secret && found_public &&
  353. !(flags & INIT_ED_KEY_MISSING_SECRET_OK)) {
  354. if (have_encrypted_secret_file) {
  355. tor_log(severity, LD_OR, "We needed to load a secret key from %s, "
  356. "but it was encrypted. Try tor --keygen instead.",
  357. secret_fname);
  358. } else {
  359. tor_log(severity, LD_OR, "We needed to load a secret key from %s, "
  360. "but couldn't find it.", secret_fname);
  361. }
  362. goto err;
  363. }
  364. /* If it's absent, and we're not supposed to make a new keypair, fail. */
  365. if (!have_secret && !found_public && !(flags & INIT_ED_KEY_CREATE)) {
  366. if (split) {
  367. tor_log(severity, LD_OR, "No key found in %s or %s.",
  368. secret_fname, public_fname);
  369. } else {
  370. tor_log(severity, LD_OR, "No key found in %s.", secret_fname);
  371. }
  372. goto err;
  373. }
  374. /* If the secret key is absent, but the encrypted key would be present,
  375. * that's an error */
  376. if (!have_secret && !found_public && have_encrypted_secret_file) {
  377. tor_assert(!encrypt_key);
  378. tor_log(severity, LD_OR, "Found an encrypted secret key, "
  379. "but not public key file %s!", public_fname);
  380. goto err;
  381. }
  382. /* if it's absent, make a new keypair and save it. */
  383. if (!have_secret && !found_public) {
  384. tor_free(keypair);
  385. keypair = ed_key_new(signing_key, flags, now, lifetime,
  386. cert_type, &cert);
  387. if (!keypair) {
  388. tor_log(severity, LD_OR, "Couldn't create keypair");
  389. goto err;
  390. }
  391. created_pk = created_sk = created_cert = 1;
  392. if (write_secret_key(&keypair->seckey,
  393. encrypt_key,
  394. secret_fname, tag, encrypted_secret_fname) < 0
  395. ||
  396. (split &&
  397. ed25519_pubkey_write_to_file(&keypair->pubkey, public_fname, tag) < 0)
  398. ||
  399. (cert &&
  400. crypto_write_tagged_contents_to_file(cert_fname, "ed25519v1-cert",
  401. tag, cert->encoded, cert->encoded_len) < 0)) {
  402. tor_log(severity, LD_OR, "Couldn't write keys or cert to file.");
  403. goto err;
  404. }
  405. goto done;
  406. }
  407. /* If we're not supposed to get a cert, we're done. */
  408. if (! (flags & INIT_ED_KEY_NEEDCERT))
  409. goto done;
  410. /* Read a cert. */
  411. tor_free(got_tag);
  412. uint8_t certbuf[256];
  413. ssize_t cert_body_len = crypto_read_tagged_contents_from_file(
  414. cert_fname, "ed25519v1-cert",
  415. &got_tag, certbuf, sizeof(certbuf));
  416. if (cert_body_len >= 0 && !strcmp(got_tag, tag))
  417. cert = tor_cert_parse(certbuf, cert_body_len);
  418. /* If we got it, check it to the extent we can. */
  419. int bad_cert = 0;
  420. if (! cert) {
  421. tor_log(severity, LD_OR, "Cert was unparseable");
  422. bad_cert = 1;
  423. } else if (!tor_memeq(cert->signed_key.pubkey, keypair->pubkey.pubkey,
  424. ED25519_PUBKEY_LEN)) {
  425. tor_log(severity, LD_OR, "Cert was for wrong key");
  426. bad_cert = 1;
  427. } else if (signing_key &&
  428. tor_cert_checksig(cert, &signing_key->pubkey, now) < 0 &&
  429. (signing_key || cert->cert_expired)) {
  430. tor_log(severity, LD_OR, "Can't check certificate");
  431. bad_cert = 1;
  432. } else if (signing_key && cert->signing_key_included &&
  433. ! ed25519_pubkey_eq(&signing_key->pubkey, &cert->signing_key)) {
  434. tor_log(severity, LD_OR, "Certificate signed by unexpectd key!");
  435. bad_cert = 1;
  436. }
  437. if (bad_cert) {
  438. tor_cert_free(cert);
  439. cert = NULL;
  440. }
  441. /* If we got a cert, we're done. */
  442. if (cert)
  443. goto done;
  444. /* If we didn't get a cert, and we're not supposed to make one, fail. */
  445. if (!signing_key || !(flags & INIT_ED_KEY_CREATE)) {
  446. tor_log(severity, LD_OR, "Without signing key, can't create certificate");
  447. goto err;
  448. }
  449. /* We have keys but not a certificate, so make one. */
  450. uint32_t cert_flags = 0;
  451. if (flags & INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT)
  452. cert_flags |= CERT_FLAG_INCLUDE_SIGNING_KEY;
  453. cert = tor_cert_create(signing_key, cert_type,
  454. &keypair->pubkey,
  455. now, lifetime,
  456. cert_flags);
  457. if (! cert) {
  458. tor_log(severity, LD_OR, "Couldn't create certificate");
  459. goto err;
  460. }
  461. /* Write it to disk. */
  462. created_cert = 1;
  463. if (crypto_write_tagged_contents_to_file(cert_fname, "ed25519v1-cert",
  464. tag, cert->encoded, cert->encoded_len) < 0) {
  465. tor_log(severity, LD_OR, "Couldn't write cert to disk.");
  466. goto err;
  467. }
  468. done:
  469. if (cert_out)
  470. *cert_out = cert;
  471. else
  472. tor_cert_free(cert);
  473. goto cleanup;
  474. err:
  475. if (keypair)
  476. memwipe(keypair, 0, sizeof(*keypair));
  477. tor_free(keypair);
  478. tor_cert_free(cert);
  479. if (cert_out)
  480. *cert_out = NULL;
  481. if (created_sk)
  482. unlink(secret_fname);
  483. if (created_pk)
  484. unlink(public_fname);
  485. if (created_cert)
  486. unlink(cert_fname);
  487. cleanup:
  488. tor_free(encrypted_secret_fname);
  489. tor_free(secret_fname);
  490. tor_free(public_fname);
  491. tor_free(cert_fname);
  492. tor_free(got_tag);
  493. return keypair;
  494. }
  495. /**
  496. * Create a new signing key and (optionally) certficiate; do not read or write
  497. * from disk. See ed_key_init_from_file() for more information.
  498. */
  499. ed25519_keypair_t *
  500. ed_key_new(const ed25519_keypair_t *signing_key,
  501. uint32_t flags,
  502. time_t now,
  503. time_t lifetime,
  504. uint8_t cert_type,
  505. struct tor_cert_st **cert_out)
  506. {
  507. if (cert_out)
  508. *cert_out = NULL;
  509. const int extra_strong = !! (flags & INIT_ED_KEY_EXTRA_STRONG);
  510. ed25519_keypair_t *keypair = tor_malloc_zero(sizeof(ed25519_keypair_t));
  511. if (ed25519_keypair_generate(keypair, extra_strong) < 0)
  512. goto err;
  513. if (! (flags & INIT_ED_KEY_NEEDCERT))
  514. return keypair;
  515. tor_assert(signing_key);
  516. tor_assert(cert_out);
  517. uint32_t cert_flags = 0;
  518. if (flags & INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT)
  519. cert_flags |= CERT_FLAG_INCLUDE_SIGNING_KEY;
  520. tor_cert_t *cert = tor_cert_create(signing_key, cert_type,
  521. &keypair->pubkey,
  522. now, lifetime,
  523. cert_flags);
  524. if (! cert)
  525. goto err;
  526. *cert_out = cert;
  527. return keypair;
  528. err:
  529. tor_free(keypair);
  530. return NULL;
  531. }
  532. static ed25519_keypair_t *master_identity_key = NULL;
  533. static ed25519_keypair_t *master_signing_key = NULL;
  534. static ed25519_keypair_t *current_auth_key = NULL;
  535. static tor_cert_t *signing_key_cert = NULL;
  536. static tor_cert_t *link_cert_cert = NULL;
  537. static tor_cert_t *auth_key_cert = NULL;
  538. static uint8_t *rsa_ed_crosscert = NULL;
  539. static size_t rsa_ed_crosscert_len = 0;
  540. /**
  541. * Running as a server: load, reload, or refresh our ed25519 keys and
  542. * certificates, creating and saving new ones as needed.
  543. */
  544. int
  545. load_ed_keys(const or_options_t *options, time_t now)
  546. {
  547. ed25519_keypair_t *id = NULL;
  548. ed25519_keypair_t *sign = NULL;
  549. ed25519_keypair_t *auth = NULL;
  550. const ed25519_keypair_t *sign_signing_key_with_id = NULL;
  551. const ed25519_keypair_t *use_signing = NULL;
  552. const tor_cert_t *check_signing_cert = NULL;
  553. tor_cert_t *sign_cert = NULL;
  554. tor_cert_t *auth_cert = NULL;
  555. #define FAIL(msg) do { \
  556. log_warn(LD_OR, (msg)); \
  557. goto err; \
  558. } while (0)
  559. #define SET_KEY(key, newval) do { \
  560. ed25519_keypair_free(key); \
  561. key = (newval); \
  562. } while (0)
  563. #define SET_CERT(cert, newval) do { \
  564. tor_cert_free(cert); \
  565. cert = (newval); \
  566. } while (0)
  567. #define EXPIRES_SOON(cert, interval) \
  568. (!(cert) || (cert)->valid_until < now + (interval))
  569. /* XXXX support encrypted identity keys fully */
  570. /* First try to get the signing key to see how it is. */
  571. if (master_signing_key) {
  572. check_signing_cert = signing_key_cert;
  573. use_signing = master_signing_key;
  574. } else {
  575. char *fname =
  576. options_get_datadir_fname2(options, "keys", "ed25519_signing");
  577. sign = ed_key_init_from_file(
  578. fname,
  579. INIT_ED_KEY_NEEDCERT|
  580. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT,
  581. LOG_INFO,
  582. NULL, 0, 0, CERT_TYPE_ID_SIGNING, &sign_cert);
  583. tor_free(fname);
  584. check_signing_cert = sign_cert;
  585. use_signing = sign;
  586. }
  587. const int need_new_signing_key =
  588. NULL == use_signing ||
  589. EXPIRES_SOON(check_signing_cert, 0) ||
  590. options->command == CMD_KEYGEN;
  591. const int want_new_signing_key =
  592. need_new_signing_key ||
  593. EXPIRES_SOON(check_signing_cert, options->TestingSigningKeySlop);
  594. if (need_new_signing_key) {
  595. log_notice(LD_OR, "It looks like I need to generate and sign a new "
  596. "medium-term signing key, because %s. To do that, I need to "
  597. "load (or create) the permanent master identity key.",
  598. (NULL == use_signing) ? "I don't have one" :
  599. EXPIRES_SOON(check_signing_cert, 0) ? "the one I have is expired" :
  600. "you asked me to make one with --keygen");
  601. } else if (want_new_signing_key) {
  602. log_notice(LD_OR, "It looks like I should try to generate and sign a "
  603. "new medium-term signing key, because the one I have is "
  604. "going to expire soon. To do that, I'm going to have to try to "
  605. "load the permanent master identity key.");
  606. }
  607. {
  608. uint32_t flags =
  609. (INIT_ED_KEY_SPLIT|
  610. INIT_ED_KEY_EXTRA_STRONG|INIT_ED_KEY_NO_REPAIR);
  611. if (! use_signing)
  612. flags |= INIT_ED_KEY_CREATE;
  613. if (! need_new_signing_key)
  614. flags |= INIT_ED_KEY_MISSING_SECRET_OK;
  615. if (! want_new_signing_key)
  616. flags |= INIT_ED_KEY_OMIT_SECRET;
  617. if (options->command == CMD_KEYGEN)
  618. flags |= INIT_ED_KEY_TRY_ENCRYPTED;
  619. /* Check the key directory */
  620. if (check_private_dir(options->DataDirectory, CPD_CREATE, options->User)) {
  621. log_err(LD_OR, "Can't create/check datadirectory %s",
  622. options->DataDirectory);
  623. goto err;
  624. }
  625. char *fname = get_datadir_fname("keys");
  626. if (check_private_dir(fname, CPD_CREATE, options->User) < 0) {
  627. log_err(LD_OR, "Problem creating/checking key directory %s", fname);
  628. tor_free(fname);
  629. goto err;
  630. }
  631. tor_free(fname);
  632. fname = options_get_datadir_fname2(options, "keys", "ed25519_master_id");
  633. id = ed_key_init_from_file(
  634. fname,
  635. flags,
  636. LOG_WARN, NULL, 0, 0, 0, NULL);
  637. tor_free(fname);
  638. if (!id) {
  639. if (need_new_signing_key) {
  640. FAIL("Missing identity key");
  641. } else {
  642. log_warn(LD_OR, "Master public key was absent; inferring from "
  643. "public key in signing certificate and saving to disk.");
  644. tor_assert(check_signing_cert);
  645. id = tor_malloc_zero(sizeof(*id));
  646. memcpy(&id->pubkey, &check_signing_cert->signing_key,
  647. sizeof(ed25519_public_key_t));
  648. fname = options_get_datadir_fname2(options, "keys",
  649. "ed25519_master_id_public_key");
  650. if (ed25519_pubkey_write_to_file(&id->pubkey, fname, "type0") < 0) {
  651. log_warn(LD_OR, "Error while attempting to write master public key "
  652. "to disk");
  653. tor_free(fname);
  654. goto err;
  655. }
  656. tor_free(fname);
  657. }
  658. }
  659. if (tor_mem_is_zero((char*)id->seckey.seckey, sizeof(id->seckey)))
  660. sign_signing_key_with_id = NULL;
  661. else
  662. sign_signing_key_with_id = id;
  663. }
  664. if (master_identity_key &&
  665. !ed25519_pubkey_eq(&id->pubkey, &master_identity_key->pubkey)) {
  666. FAIL("Identity key on disk does not match key we loaded earlier!");
  667. }
  668. if (need_new_signing_key && NULL == sign_signing_key_with_id)
  669. FAIL("Can't load master key make a new signing key.");
  670. if (sign_cert) {
  671. if (! sign_cert->signing_key_included)
  672. FAIL("Loaded a signing cert with no key included!");
  673. if (! ed25519_pubkey_eq(&sign_cert->signing_key, &id->pubkey))
  674. FAIL("The signing cert we have was not signed with the master key "
  675. "we loaded!");
  676. if (tor_cert_checksig(sign_cert, &id->pubkey, 0) < 0)
  677. FAIL("The signing cert we loaded was not signed correctly!");
  678. }
  679. if (want_new_signing_key && sign_signing_key_with_id) {
  680. uint32_t flags = (INIT_ED_KEY_CREATE|
  681. INIT_ED_KEY_REPLACE|
  682. INIT_ED_KEY_EXTRA_STRONG|
  683. INIT_ED_KEY_NEEDCERT|
  684. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT);
  685. char *fname =
  686. options_get_datadir_fname2(options, "keys", "ed25519_signing");
  687. sign = ed_key_init_from_file(fname,
  688. flags, LOG_WARN,
  689. sign_signing_key_with_id, now,
  690. options->SigningKeyLifetime,
  691. CERT_TYPE_ID_SIGNING, &sign_cert);
  692. tor_free(fname);
  693. if (!sign)
  694. FAIL("Missing signing key");
  695. use_signing = sign;
  696. tor_assert(sign_cert->signing_key_included);
  697. tor_assert(ed25519_pubkey_eq(&sign_cert->signing_key, &id->pubkey));
  698. tor_assert(ed25519_pubkey_eq(&sign_cert->signed_key, &sign->pubkey));
  699. } else if (want_new_signing_key) {
  700. static ratelim_t missing_master = RATELIM_INIT(3600);
  701. log_fn_ratelim(&missing_master, LOG_WARN, LD_OR,
  702. "Signing key will expire soon, but I can't load the "
  703. "master key to sign a new one!");
  704. }
  705. tor_assert(use_signing);
  706. /* At this point we no longer need our secret identity key. So wipe
  707. * it, if we loaded it in the first place. */
  708. memwipe(id->seckey.seckey, 0, sizeof(id->seckey));
  709. if (options->command == CMD_KEYGEN)
  710. goto end;
  711. if (!rsa_ed_crosscert && server_mode(options)) {
  712. uint8_t *crosscert;
  713. ssize_t crosscert_len = tor_make_rsa_ed25519_crosscert(&id->pubkey,
  714. get_server_identity_key(),
  715. now+10*365*86400,/*XXXX*/
  716. &crosscert);
  717. rsa_ed_crosscert_len = crosscert_len;
  718. rsa_ed_crosscert = crosscert;
  719. }
  720. if (!current_auth_key ||
  721. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop)) {
  722. auth = ed_key_new(use_signing, INIT_ED_KEY_NEEDCERT,
  723. now,
  724. options->TestingAuthKeyLifetime,
  725. CERT_TYPE_SIGNING_AUTH, &auth_cert);
  726. if (!auth)
  727. FAIL("Can't create auth key");
  728. }
  729. /* We've generated or loaded everything. Put them in memory. */
  730. end:
  731. if (! master_identity_key) {
  732. SET_KEY(master_identity_key, id);
  733. } else {
  734. tor_free(id);
  735. }
  736. if (sign) {
  737. SET_KEY(master_signing_key, sign);
  738. SET_CERT(signing_key_cert, sign_cert);
  739. }
  740. if (auth) {
  741. SET_KEY(current_auth_key, auth);
  742. SET_CERT(auth_key_cert, auth_cert);
  743. }
  744. return 0;
  745. err:
  746. ed25519_keypair_free(id);
  747. ed25519_keypair_free(sign);
  748. ed25519_keypair_free(auth);
  749. tor_cert_free(sign_cert);
  750. tor_cert_free(auth_cert);
  751. return -1;
  752. }
  753. /**DOCDOC*/
  754. int
  755. generate_ed_link_cert(const or_options_t *options, time_t now)
  756. {
  757. const tor_x509_cert_t *link = NULL, *id = NULL;
  758. tor_cert_t *link_cert = NULL;
  759. if (tor_tls_get_my_certs(1, &link, &id) < 0 || link == NULL) {
  760. log_warn(LD_OR, "Can't get my x509 link cert.");
  761. return -1;
  762. }
  763. const digests_t *digests = tor_x509_cert_get_cert_digests(link);
  764. if (link_cert_cert &&
  765. ! EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop) &&
  766. fast_memeq(digests->d[DIGEST_SHA256], link_cert_cert->signed_key.pubkey,
  767. DIGEST256_LEN)) {
  768. return 0;
  769. }
  770. ed25519_public_key_t dummy_key;
  771. memcpy(dummy_key.pubkey, digests->d[DIGEST_SHA256], DIGEST256_LEN);
  772. link_cert = tor_cert_create(get_master_signing_keypair(),
  773. CERT_TYPE_SIGNING_LINK,
  774. &dummy_key,
  775. now,
  776. options->TestingLinkCertLifetime, 0);
  777. if (link_cert) {
  778. SET_CERT(link_cert_cert, link_cert);
  779. }
  780. return 0;
  781. }
  782. #undef FAIL
  783. #undef SET_KEY
  784. #undef SET_CERT
  785. int
  786. should_make_new_ed_keys(const or_options_t *options, const time_t now)
  787. {
  788. if (!master_identity_key ||
  789. !master_signing_key ||
  790. !current_auth_key ||
  791. !link_cert_cert ||
  792. EXPIRES_SOON(signing_key_cert, options->TestingSigningKeySlop) ||
  793. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop) ||
  794. EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop))
  795. return 1;
  796. const tor_x509_cert_t *link = NULL, *id = NULL;
  797. if (tor_tls_get_my_certs(1, &link, &id) < 0 || link == NULL)
  798. return 1;
  799. const digests_t *digests = tor_x509_cert_get_cert_digests(link);
  800. if (!fast_memeq(digests->d[DIGEST_SHA256],
  801. link_cert_cert->signed_key.pubkey,
  802. DIGEST256_LEN)) {
  803. return 1;
  804. }
  805. return 0;
  806. }
  807. #undef EXPIRES_SOON
  808. const ed25519_public_key_t *
  809. get_master_identity_key(void)
  810. {
  811. if (!master_identity_key)
  812. return NULL;
  813. return &master_identity_key->pubkey;
  814. }
  815. const ed25519_keypair_t *
  816. get_master_signing_keypair(void)
  817. {
  818. return master_signing_key;
  819. }
  820. const struct tor_cert_st *
  821. get_master_signing_key_cert(void)
  822. {
  823. return signing_key_cert;
  824. }
  825. const ed25519_keypair_t *
  826. get_current_auth_keypair(void)
  827. {
  828. return current_auth_key;
  829. }
  830. const tor_cert_t *
  831. get_current_link_cert_cert(void)
  832. {
  833. return link_cert_cert;
  834. }
  835. const tor_cert_t *
  836. get_current_auth_key_cert(void)
  837. {
  838. return auth_key_cert;
  839. }
  840. void
  841. get_master_rsa_crosscert(const uint8_t **cert_out,
  842. size_t *size_out)
  843. {
  844. *cert_out = rsa_ed_crosscert;
  845. *size_out = rsa_ed_crosscert_len;
  846. }
  847. /** Construct cross-certification for the master identity key with
  848. * the ntor onion key. Store the sign of the corresponding ed25519 public key
  849. * in *<b>sign_out</b>. */
  850. tor_cert_t *
  851. make_ntor_onion_key_crosscert(const curve25519_keypair_t *onion_key,
  852. const ed25519_public_key_t *master_id_key, time_t now, time_t lifetime,
  853. int *sign_out)
  854. {
  855. tor_cert_t *cert = NULL;
  856. ed25519_keypair_t ed_onion_key;
  857. if (ed25519_keypair_from_curve25519_keypair(&ed_onion_key, sign_out,
  858. onion_key) < 0)
  859. goto end;
  860. cert = tor_cert_create(&ed_onion_key, CERT_TYPE_ONION_ID, master_id_key,
  861. now, lifetime, 0);
  862. end:
  863. memwipe(&ed_onion_key, 0, sizeof(ed_onion_key));
  864. return cert;
  865. }
  866. /** Construct and return an RSA signature for the TAP onion key to
  867. * cross-certify the RSA and Ed25519 identity keys. Set <b>len_out</b> to its
  868. * length. */
  869. uint8_t *
  870. make_tap_onion_key_crosscert(const crypto_pk_t *onion_key,
  871. const ed25519_public_key_t *master_id_key,
  872. const crypto_pk_t *rsa_id_key,
  873. int *len_out)
  874. {
  875. uint8_t signature[PK_BYTES];
  876. uint8_t signed_data[DIGEST_LEN + ED25519_PUBKEY_LEN];
  877. *len_out = 0;
  878. crypto_pk_get_digest(rsa_id_key, (char*)signed_data);
  879. memcpy(signed_data + DIGEST_LEN, master_id_key->pubkey, ED25519_PUBKEY_LEN);
  880. int r = crypto_pk_private_sign(onion_key,
  881. (char*)signature, sizeof(signature),
  882. (const char*)signed_data, sizeof(signed_data));
  883. if (r < 0)
  884. return NULL;
  885. *len_out = r;
  886. return tor_memdup(signature, r);
  887. }
  888. /** Check whether an RSA-TAP cross-certification is correct. Return 0 if it
  889. * is, -1 if it isn't. */
  890. int
  891. check_tap_onion_key_crosscert(const uint8_t *crosscert,
  892. int crosscert_len,
  893. const crypto_pk_t *onion_pkey,
  894. const ed25519_public_key_t *master_id_pkey,
  895. const uint8_t *rsa_id_digest)
  896. {
  897. uint8_t *cc = tor_malloc(crypto_pk_keysize(onion_pkey));
  898. int cc_len =
  899. crypto_pk_public_checksig(onion_pkey,
  900. (char*)cc,
  901. crypto_pk_keysize(onion_pkey),
  902. (const char*)crosscert,
  903. crosscert_len);
  904. if (cc_len < 0) {
  905. goto err;
  906. }
  907. if (cc_len < DIGEST_LEN + ED25519_PUBKEY_LEN) {
  908. log_warn(LD_DIR, "Short signature on cross-certification with TAP key");
  909. goto err;
  910. }
  911. if (tor_memneq(cc, rsa_id_digest, DIGEST_LEN) ||
  912. tor_memneq(cc + DIGEST_LEN, master_id_pkey->pubkey,
  913. ED25519_PUBKEY_LEN)) {
  914. log_warn(LD_DIR, "Incorrect cross-certification with TAP key");
  915. goto err;
  916. }
  917. tor_free(cc);
  918. return 0;
  919. err:
  920. tor_free(cc);
  921. return -1;
  922. }
  923. void
  924. routerkeys_free_all(void)
  925. {
  926. ed25519_keypair_free(master_identity_key);
  927. ed25519_keypair_free(master_signing_key);
  928. ed25519_keypair_free(current_auth_key);
  929. tor_cert_free(signing_key_cert);
  930. tor_cert_free(link_cert_cert);
  931. tor_cert_free(auth_key_cert);
  932. master_identity_key = master_signing_key = NULL;
  933. current_auth_key = NULL;
  934. signing_key_cert = link_cert_cert = auth_key_cert = NULL;
  935. }