routerkeys.c 33 KB

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