routerkeys.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /* Copyright (c) 2014-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file routerkeys.c
  5. *
  6. * \brief Functions and structures to handle generating and maintaining the
  7. * set of keypairs necessary to be an OR.
  8. *
  9. * The keys handled here now are the Ed25519 keys that Tor relays use to sign
  10. * descriptors, authenticate themselves on links, and identify one another
  11. * uniquely. Other keys are maintained in router.c and rendservice.c.
  12. *
  13. * (TODO: The keys in router.c should go here too.)
  14. */
  15. #include "or.h"
  16. #include "config.h"
  17. #include "router.h"
  18. #include "crypto_pwbox.h"
  19. #include "routerkeys.h"
  20. #include "torcert.h"
  21. #define ENC_KEY_HEADER "Boxed Ed25519 key"
  22. #define ENC_KEY_TAG "master"
  23. static ssize_t
  24. do_getpass(const char *prompt, char *buf, size_t buflen,
  25. int twice, const or_options_t *options)
  26. {
  27. if (options->keygen_force_passphrase == FORCE_PASSPHRASE_OFF) {
  28. tor_assert(buflen);
  29. buf[0] = 0;
  30. return 0;
  31. }
  32. char *prompt2 = NULL;
  33. char *buf2 = NULL;
  34. int fd = -1;
  35. ssize_t length = -1;
  36. if (options->use_keygen_passphrase_fd) {
  37. twice = 0;
  38. fd = options->keygen_passphrase_fd;
  39. length = read_all(fd, buf, buflen-1, 0);
  40. if (length >= 0)
  41. buf[length] = 0;
  42. goto done_reading;
  43. }
  44. if (twice) {
  45. const char msg[] = "One more time:";
  46. size_t p2len = strlen(prompt) + 1;
  47. if (p2len < sizeof(msg))
  48. p2len = sizeof(msg);
  49. prompt2 = tor_malloc(p2len);
  50. memset(prompt2, ' ', p2len);
  51. memcpy(prompt2 + p2len - sizeof(msg), msg, sizeof(msg));
  52. buf2 = tor_malloc_zero(buflen);
  53. }
  54. while (1) {
  55. length = tor_getpass(prompt, buf, buflen);
  56. if (length < 0)
  57. goto done_reading;
  58. if (! twice)
  59. break;
  60. ssize_t length2 = tor_getpass(prompt2, buf2, buflen);
  61. if (length != length2 || tor_memneq(buf, buf2, length)) {
  62. fprintf(stderr, "That didn't match.\n");
  63. } else {
  64. break;
  65. }
  66. }
  67. done_reading:
  68. if (twice) {
  69. tor_free(prompt2);
  70. memwipe(buf2, 0, buflen);
  71. tor_free(buf2);
  72. }
  73. if (options->keygen_force_passphrase == FORCE_PASSPHRASE_ON && length == 0)
  74. return -1;
  75. return length;
  76. }
  77. int
  78. read_encrypted_secret_key(ed25519_secret_key_t *out,
  79. const char *fname)
  80. {
  81. int r = -1;
  82. uint8_t *secret = NULL;
  83. size_t secret_len = 0;
  84. char pwbuf[256];
  85. uint8_t encrypted_key[256];
  86. char *tag = NULL;
  87. int saved_errno = 0;
  88. ssize_t encrypted_len = crypto_read_tagged_contents_from_file(fname,
  89. ENC_KEY_HEADER,
  90. &tag,
  91. encrypted_key,
  92. sizeof(encrypted_key));
  93. if (encrypted_len < 0) {
  94. saved_errno = errno;
  95. log_info(LD_OR, "%s is missing", fname);
  96. r = 0;
  97. goto done;
  98. }
  99. if (strcmp(tag, ENC_KEY_TAG)) {
  100. saved_errno = EINVAL;
  101. goto done;
  102. }
  103. while (1) {
  104. ssize_t pwlen =
  105. do_getpass("Enter passphrase for master key:", pwbuf, sizeof(pwbuf), 0,
  106. get_options());
  107. if (pwlen < 0) {
  108. saved_errno = EINVAL;
  109. goto done;
  110. }
  111. const int r_unbox = crypto_unpwbox(&secret, &secret_len,
  112. encrypted_key, encrypted_len,
  113. pwbuf, pwlen);
  114. if (r_unbox == UNPWBOX_CORRUPTED) {
  115. log_err(LD_OR, "%s is corrupted.", fname);
  116. saved_errno = EINVAL;
  117. goto done;
  118. } else if (r_unbox == UNPWBOX_OKAY) {
  119. break;
  120. }
  121. /* Otherwise, passphrase is bad, so try again till user does ctrl-c or gets
  122. * it right. */
  123. }
  124. if (secret_len != ED25519_SECKEY_LEN) {
  125. log_err(LD_OR, "%s is corrupted.", fname);
  126. saved_errno = EINVAL;
  127. goto done;
  128. }
  129. memcpy(out->seckey, secret, ED25519_SECKEY_LEN);
  130. r = 1;
  131. done:
  132. memwipe(encrypted_key, 0, sizeof(encrypted_key));
  133. memwipe(pwbuf, 0, sizeof(pwbuf));
  134. tor_free(tag);
  135. if (secret) {
  136. memwipe(secret, 0, secret_len);
  137. tor_free(secret);
  138. }
  139. if (saved_errno)
  140. errno = saved_errno;
  141. return r;
  142. }
  143. int
  144. write_encrypted_secret_key(const ed25519_secret_key_t *key,
  145. const char *fname)
  146. {
  147. int r = -1;
  148. char pwbuf0[256];
  149. uint8_t *encrypted_key = NULL;
  150. size_t encrypted_len = 0;
  151. if (do_getpass("Enter new passphrase:", pwbuf0, sizeof(pwbuf0), 1,
  152. get_options()) < 0) {
  153. log_warn(LD_OR, "NO/failed passphrase");
  154. return -1;
  155. }
  156. if (strlen(pwbuf0) == 0) {
  157. if (get_options()->keygen_force_passphrase == FORCE_PASSPHRASE_ON)
  158. return -1;
  159. else
  160. return 0;
  161. }
  162. if (crypto_pwbox(&encrypted_key, &encrypted_len,
  163. key->seckey, sizeof(key->seckey),
  164. pwbuf0, strlen(pwbuf0), 0) < 0) {
  165. log_warn(LD_OR, "crypto_pwbox failed!?");
  166. goto done;
  167. }
  168. if (crypto_write_tagged_contents_to_file(fname,
  169. ENC_KEY_HEADER,
  170. ENC_KEY_TAG,
  171. encrypted_key, encrypted_len) < 0)
  172. goto done;
  173. r = 1;
  174. done:
  175. if (encrypted_key) {
  176. memwipe(encrypted_key, 0, encrypted_len);
  177. tor_free(encrypted_key);
  178. }
  179. memwipe(pwbuf0, 0, sizeof(pwbuf0));
  180. return r;
  181. }
  182. static int
  183. write_secret_key(const ed25519_secret_key_t *key, int encrypted,
  184. const char *fname,
  185. const char *fname_tag,
  186. const char *encrypted_fname)
  187. {
  188. if (encrypted) {
  189. int r = write_encrypted_secret_key(key, encrypted_fname);
  190. if (r == 1) {
  191. /* Success! */
  192. /* Try to unlink the unencrypted key, if any existed before */
  193. if (strcmp(fname, encrypted_fname))
  194. unlink(fname);
  195. return r;
  196. } else if (r != 0) {
  197. /* Unrecoverable failure! */
  198. return r;
  199. }
  200. fprintf(stderr, "Not encrypting the secret key.\n");
  201. }
  202. return ed25519_seckey_write_to_file(key, fname, fname_tag);
  203. }
  204. /**
  205. * Read an ed25519 key and associated certificates from files beginning with
  206. * <b>fname</b>, with certificate type <b>cert_type</b>. On failure, return
  207. * NULL; on success return the keypair.
  208. *
  209. * If INIT_ED_KEY_CREATE is set in <b>flags</b>, then create the key (and
  210. * certificate if requested) if it doesn't exist, and save it to disk.
  211. *
  212. * If INIT_ED_KEY_NEEDCERT is set in <b>flags</b>, load/create a certificate
  213. * too and store it in *<b>cert_out</b>. Fail if the cert can't be
  214. * found/created. To create a certificate, <b>signing_key</b> must be set to
  215. * the key that should sign it; <b>now</b> to the current time, and
  216. * <b>lifetime</b> to the lifetime of the key.
  217. *
  218. * If INIT_ED_KEY_REPLACE is set in <b>flags</b>, then create and save new key
  219. * whether we can read the old one or not.
  220. *
  221. * If INIT_ED_KEY_EXTRA_STRONG is set in <b>flags</b>, set the extra_strong
  222. * flag when creating the secret key.
  223. *
  224. * If INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT is set in <b>flags</b>, and
  225. * we create a new certificate, create it with the signing key embedded.
  226. *
  227. * If INIT_ED_KEY_SPLIT is set in <b>flags</b>, and we create a new key,
  228. * store the public key in a separate file from the secret key.
  229. *
  230. * If INIT_ED_KEY_MISSING_SECRET_OK is set in <b>flags</b>, and we find a
  231. * public key file but no secret key file, return successfully anyway.
  232. *
  233. * If INIT_ED_KEY_OMIT_SECRET is set in <b>flags</b>, do not try to load a
  234. * secret key unless no public key is found. Do not return a secret key. (but
  235. * create and save one if needed).
  236. *
  237. * If INIT_ED_KEY_NO_LOAD_SECRET is set in <b>flags</b>, don't try to load
  238. * a secret key, no matter what.
  239. *
  240. * If INIT_ED_KEY_TRY_ENCRYPTED is set, we look for an encrypted secret key
  241. * and consider encrypting any new secret key.
  242. *
  243. * If INIT_ED_KEY_NO_REPAIR is set, and there is any issue loading the keys
  244. * from disk _other than their absence_ (full or partial), we do not try to
  245. * replace them.
  246. *
  247. * If INIT_ED_KEY_SUGGEST_KEYGEN is set, have log messages about failures
  248. * refer to the --keygen option.
  249. *
  250. * If INIT_ED_KEY_EXPLICIT_FNAME is set, use the provided file name for the
  251. * secret key file, encrypted or not.
  252. */
  253. ed25519_keypair_t *
  254. ed_key_init_from_file(const char *fname, uint32_t flags,
  255. int severity,
  256. const ed25519_keypair_t *signing_key,
  257. time_t now,
  258. time_t lifetime,
  259. uint8_t cert_type,
  260. struct tor_cert_st **cert_out)
  261. {
  262. char *secret_fname = NULL;
  263. char *encrypted_secret_fname = NULL;
  264. char *public_fname = NULL;
  265. char *cert_fname = NULL;
  266. const char *loaded_secret_fname = NULL;
  267. int created_pk = 0, created_sk = 0, created_cert = 0;
  268. const int try_to_load = ! (flags & INIT_ED_KEY_REPLACE);
  269. const int encrypt_key = !! (flags & INIT_ED_KEY_TRY_ENCRYPTED);
  270. const int norepair = !! (flags & INIT_ED_KEY_NO_REPAIR);
  271. const int split = !! (flags & INIT_ED_KEY_SPLIT);
  272. const int omit_secret = !! (flags & INIT_ED_KEY_OMIT_SECRET);
  273. const int offline_secret = !! (flags & INIT_ED_KEY_OFFLINE_SECRET);
  274. const int explicit_fname = !! (flags & INIT_ED_KEY_EXPLICIT_FNAME);
  275. /* we don't support setting both of these flags at once. */
  276. tor_assert((flags & (INIT_ED_KEY_NO_REPAIR|INIT_ED_KEY_NEEDCERT)) !=
  277. (INIT_ED_KEY_NO_REPAIR|INIT_ED_KEY_NEEDCERT));
  278. char tag[8];
  279. tor_snprintf(tag, sizeof(tag), "type%d", (int)cert_type);
  280. tor_cert_t *cert = NULL;
  281. char *got_tag = NULL;
  282. ed25519_keypair_t *keypair = tor_malloc_zero(sizeof(ed25519_keypair_t));
  283. if (explicit_fname) {
  284. secret_fname = tor_strdup(fname);
  285. encrypted_secret_fname = tor_strdup(fname);
  286. } else {
  287. tor_asprintf(&secret_fname, "%s_secret_key", fname);
  288. tor_asprintf(&encrypted_secret_fname, "%s_secret_key_encrypted", fname);
  289. }
  290. tor_asprintf(&public_fname, "%s_public_key", fname);
  291. tor_asprintf(&cert_fname, "%s_cert", fname);
  292. /* Try to read the secret key. */
  293. int have_secret = 0;
  294. int load_secret = try_to_load &&
  295. !offline_secret &&
  296. (!omit_secret || file_status(public_fname)==FN_NOENT);
  297. if (load_secret) {
  298. int rv = ed25519_seckey_read_from_file(&keypair->seckey,
  299. &got_tag, secret_fname);
  300. if (rv == 0) {
  301. have_secret = 1;
  302. loaded_secret_fname = secret_fname;
  303. tor_assert(got_tag);
  304. } else {
  305. if (errno != ENOENT && norepair) {
  306. tor_log(severity, LD_OR, "Unable to read %s: %s", secret_fname,
  307. strerror(errno));
  308. goto err;
  309. }
  310. }
  311. }
  312. /* Should we try for an encrypted key? */
  313. int have_encrypted_secret_file = 0;
  314. if (!have_secret && try_to_load && encrypt_key) {
  315. int r = read_encrypted_secret_key(&keypair->seckey,
  316. encrypted_secret_fname);
  317. if (r > 0) {
  318. have_secret = 1;
  319. have_encrypted_secret_file = 1;
  320. tor_free(got_tag); /* convince coverity we aren't leaking */
  321. got_tag = tor_strdup(tag);
  322. loaded_secret_fname = encrypted_secret_fname;
  323. } else if (errno != ENOENT && norepair) {
  324. tor_log(severity, LD_OR, "Unable to read %s: %s",
  325. encrypted_secret_fname, strerror(errno));
  326. goto err;
  327. }
  328. } else {
  329. if (try_to_load) {
  330. /* Check if it's there anyway, so we don't replace it. */
  331. if (file_status(encrypted_secret_fname) != FN_NOENT)
  332. have_encrypted_secret_file = 1;
  333. }
  334. }
  335. if (have_secret) {
  336. if (strcmp(got_tag, tag)) {
  337. tor_log(severity, LD_OR, "%s has wrong tag", loaded_secret_fname);
  338. goto err;
  339. }
  340. /* Derive the public key */
  341. if (ed25519_public_key_generate(&keypair->pubkey, &keypair->seckey)<0) {
  342. tor_log(severity, LD_OR, "%s can't produce a public key",
  343. loaded_secret_fname);
  344. goto err;
  345. }
  346. }
  347. /* If we do split keys here, try to read the pubkey. */
  348. int found_public = 0;
  349. if (try_to_load && (!have_secret || split)) {
  350. ed25519_public_key_t pubkey_tmp;
  351. tor_free(got_tag);
  352. found_public = ed25519_pubkey_read_from_file(&pubkey_tmp,
  353. &got_tag, public_fname) == 0;
  354. if (!found_public && errno != ENOENT && norepair) {
  355. tor_log(severity, LD_OR, "Unable to read %s: %s", public_fname,
  356. strerror(errno));
  357. goto err;
  358. }
  359. if (found_public && strcmp(got_tag, tag)) {
  360. tor_log(severity, LD_OR, "%s has wrong tag", public_fname);
  361. goto err;
  362. }
  363. if (found_public) {
  364. if (have_secret) {
  365. /* If we have a secret key and we're reloading the public key,
  366. * the key must match! */
  367. if (! ed25519_pubkey_eq(&keypair->pubkey, &pubkey_tmp)) {
  368. tor_log(severity, LD_OR, "%s does not match %s! If you are trying "
  369. "to restore from backup, make sure you didn't mix up the "
  370. "key files. If you are absolutely sure that %s is the right "
  371. "key for this relay, delete %s or move it out of the way.",
  372. public_fname, loaded_secret_fname,
  373. loaded_secret_fname, public_fname);
  374. goto err;
  375. }
  376. } else {
  377. /* We only have the public key; better use that. */
  378. tor_assert(split);
  379. memcpy(&keypair->pubkey, &pubkey_tmp, sizeof(pubkey_tmp));
  380. }
  381. } else {
  382. /* We have no public key file, but we do have a secret key, make the
  383. * public key file! */
  384. if (have_secret) {
  385. if (ed25519_pubkey_write_to_file(&keypair->pubkey, public_fname, tag)
  386. < 0) {
  387. tor_log(severity, LD_OR, "Couldn't repair %s", public_fname);
  388. goto err;
  389. } else {
  390. tor_log(LOG_NOTICE, LD_OR,
  391. "Found secret key but not %s. Regenerating.",
  392. public_fname);
  393. }
  394. }
  395. }
  396. }
  397. /* If the secret key is absent and it's not allowed to be, fail. */
  398. if (!have_secret && found_public &&
  399. !(flags & INIT_ED_KEY_MISSING_SECRET_OK)) {
  400. if (have_encrypted_secret_file) {
  401. tor_log(severity, LD_OR, "We needed to load a secret key from %s, "
  402. "but it was encrypted. Try 'tor --keygen' instead, so you "
  403. "can enter the passphrase.",
  404. secret_fname);
  405. } else if (offline_secret) {
  406. tor_log(severity, LD_OR, "We wanted to load a secret key from %s, "
  407. "but you're keeping it offline. (OfflineMasterKey is set.)",
  408. secret_fname);
  409. } else {
  410. tor_log(severity, LD_OR, "We needed to load a secret key from %s, "
  411. "but couldn't find it. %s", secret_fname,
  412. (flags & INIT_ED_KEY_SUGGEST_KEYGEN) ?
  413. "If you're keeping your master secret key offline, you will "
  414. "need to run 'tor --keygen' to generate new signing keys." :
  415. "Did you forget to copy it over when you copied the rest of the "
  416. "signing key material?");
  417. }
  418. goto err;
  419. }
  420. /* If it's absent, and we're not supposed to make a new keypair, fail. */
  421. if (!have_secret && !found_public && !(flags & INIT_ED_KEY_CREATE)) {
  422. if (split) {
  423. tor_log(severity, LD_OR, "No key found in %s or %s.",
  424. secret_fname, public_fname);
  425. } else {
  426. tor_log(severity, LD_OR, "No key found in %s.", secret_fname);
  427. }
  428. goto err;
  429. }
  430. /* If the secret key is absent, but the encrypted key would be present,
  431. * that's an error */
  432. if (!have_secret && !found_public && have_encrypted_secret_file) {
  433. tor_assert(!encrypt_key);
  434. tor_log(severity, LD_OR, "Found an encrypted secret key, "
  435. "but not public key file %s!", public_fname);
  436. goto err;
  437. }
  438. /* if it's absent, make a new keypair... */
  439. if (!have_secret && !found_public) {
  440. tor_free(keypair);
  441. keypair = ed_key_new(signing_key, flags, now, lifetime,
  442. cert_type, &cert);
  443. if (!keypair) {
  444. tor_log(severity, LD_OR, "Couldn't create keypair");
  445. goto err;
  446. }
  447. created_pk = created_sk = created_cert = 1;
  448. }
  449. /* Write it to disk if we're supposed to do with a new passphrase, or if
  450. * we just created it. */
  451. if (created_sk || (have_secret && get_options()->change_key_passphrase)) {
  452. if (write_secret_key(&keypair->seckey,
  453. encrypt_key,
  454. secret_fname, tag, encrypted_secret_fname) < 0
  455. ||
  456. (split &&
  457. ed25519_pubkey_write_to_file(&keypair->pubkey, public_fname, tag) < 0)
  458. ||
  459. (cert &&
  460. crypto_write_tagged_contents_to_file(cert_fname, "ed25519v1-cert",
  461. tag, cert->encoded, cert->encoded_len) < 0)) {
  462. tor_log(severity, LD_OR, "Couldn't write keys or cert to file.");
  463. goto err;
  464. }
  465. goto done;
  466. }
  467. /* If we're not supposed to get a cert, we're done. */
  468. if (! (flags & INIT_ED_KEY_NEEDCERT))
  469. goto done;
  470. /* Read a cert. */
  471. tor_free(got_tag);
  472. uint8_t certbuf[256];
  473. ssize_t cert_body_len = crypto_read_tagged_contents_from_file(
  474. cert_fname, "ed25519v1-cert",
  475. &got_tag, certbuf, sizeof(certbuf));
  476. if (cert_body_len >= 0 && !strcmp(got_tag, tag))
  477. cert = tor_cert_parse(certbuf, cert_body_len);
  478. /* If we got it, check it to the extent we can. */
  479. int bad_cert = 0;
  480. if (! cert) {
  481. tor_log(severity, LD_OR, "Cert was unparseable");
  482. bad_cert = 1;
  483. } else if (!tor_memeq(cert->signed_key.pubkey, keypair->pubkey.pubkey,
  484. ED25519_PUBKEY_LEN)) {
  485. tor_log(severity, LD_OR, "Cert was for wrong key");
  486. bad_cert = 1;
  487. } else if (signing_key &&
  488. tor_cert_checksig(cert, &signing_key->pubkey, now) < 0) {
  489. tor_log(severity, LD_OR, "Can't check certificate");
  490. bad_cert = 1;
  491. } else if (cert->cert_expired) {
  492. tor_log(severity, LD_OR, "Certificate is expired");
  493. bad_cert = 1;
  494. } else if (signing_key && cert->signing_key_included &&
  495. ! ed25519_pubkey_eq(&signing_key->pubkey, &cert->signing_key)) {
  496. tor_log(severity, LD_OR, "Certificate signed by unexpectd key!");
  497. bad_cert = 1;
  498. }
  499. if (bad_cert) {
  500. tor_cert_free(cert);
  501. cert = NULL;
  502. }
  503. /* If we got a cert, we're done. */
  504. if (cert)
  505. goto done;
  506. /* If we didn't get a cert, and we're not supposed to make one, fail. */
  507. if (!signing_key || !(flags & INIT_ED_KEY_CREATE)) {
  508. tor_log(severity, LD_OR, "Without signing key, can't create certificate");
  509. goto err;
  510. }
  511. /* We have keys but not a certificate, so make one. */
  512. uint32_t cert_flags = 0;
  513. if (flags & INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT)
  514. cert_flags |= CERT_FLAG_INCLUDE_SIGNING_KEY;
  515. cert = tor_cert_create(signing_key, cert_type,
  516. &keypair->pubkey,
  517. now, lifetime,
  518. cert_flags);
  519. if (! cert) {
  520. tor_log(severity, LD_OR, "Couldn't create certificate");
  521. goto err;
  522. }
  523. /* Write it to disk. */
  524. created_cert = 1;
  525. if (crypto_write_tagged_contents_to_file(cert_fname, "ed25519v1-cert",
  526. tag, cert->encoded, cert->encoded_len) < 0) {
  527. tor_log(severity, LD_OR, "Couldn't write cert to disk.");
  528. goto err;
  529. }
  530. done:
  531. if (cert_out)
  532. *cert_out = cert;
  533. else
  534. tor_cert_free(cert);
  535. goto cleanup;
  536. err:
  537. if (keypair)
  538. memwipe(keypair, 0, sizeof(*keypair));
  539. tor_free(keypair);
  540. tor_cert_free(cert);
  541. if (cert_out)
  542. *cert_out = NULL;
  543. if (created_sk)
  544. unlink(secret_fname);
  545. if (created_pk)
  546. unlink(public_fname);
  547. if (created_cert)
  548. unlink(cert_fname);
  549. cleanup:
  550. tor_free(encrypted_secret_fname);
  551. tor_free(secret_fname);
  552. tor_free(public_fname);
  553. tor_free(cert_fname);
  554. tor_free(got_tag);
  555. return keypair;
  556. }
  557. /**
  558. * Create a new signing key and (optionally) certficiate; do not read or write
  559. * from disk. See ed_key_init_from_file() for more information.
  560. */
  561. ed25519_keypair_t *
  562. ed_key_new(const ed25519_keypair_t *signing_key,
  563. uint32_t flags,
  564. time_t now,
  565. time_t lifetime,
  566. uint8_t cert_type,
  567. struct tor_cert_st **cert_out)
  568. {
  569. if (cert_out)
  570. *cert_out = NULL;
  571. const int extra_strong = !! (flags & INIT_ED_KEY_EXTRA_STRONG);
  572. ed25519_keypair_t *keypair = tor_malloc_zero(sizeof(ed25519_keypair_t));
  573. if (ed25519_keypair_generate(keypair, extra_strong) < 0)
  574. goto err;
  575. if (! (flags & INIT_ED_KEY_NEEDCERT))
  576. return keypair;
  577. tor_assert(signing_key);
  578. tor_assert(cert_out);
  579. uint32_t cert_flags = 0;
  580. if (flags & INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT)
  581. cert_flags |= CERT_FLAG_INCLUDE_SIGNING_KEY;
  582. tor_cert_t *cert = tor_cert_create(signing_key, cert_type,
  583. &keypair->pubkey,
  584. now, lifetime,
  585. cert_flags);
  586. if (! cert)
  587. goto err;
  588. *cert_out = cert;
  589. return keypair;
  590. err:
  591. tor_free(keypair);
  592. return NULL;
  593. }
  594. static ed25519_keypair_t *master_identity_key = NULL;
  595. static ed25519_keypair_t *master_signing_key = NULL;
  596. static ed25519_keypair_t *current_auth_key = NULL;
  597. static tor_cert_t *signing_key_cert = NULL;
  598. static tor_cert_t *link_cert_cert = NULL;
  599. static tor_cert_t *auth_key_cert = NULL;
  600. static uint8_t *rsa_ed_crosscert = NULL;
  601. static size_t rsa_ed_crosscert_len = 0;
  602. /**
  603. * Running as a server: load, reload, or refresh our ed25519 keys and
  604. * certificates, creating and saving new ones as needed.
  605. */
  606. int
  607. load_ed_keys(const or_options_t *options, time_t now)
  608. {
  609. ed25519_keypair_t *id = NULL;
  610. ed25519_keypair_t *sign = NULL;
  611. ed25519_keypair_t *auth = NULL;
  612. const ed25519_keypair_t *sign_signing_key_with_id = NULL;
  613. const ed25519_keypair_t *use_signing = NULL;
  614. const tor_cert_t *check_signing_cert = NULL;
  615. tor_cert_t *sign_cert = NULL;
  616. tor_cert_t *auth_cert = NULL;
  617. #define FAIL(msg) do { \
  618. log_warn(LD_OR, (msg)); \
  619. goto err; \
  620. } while (0)
  621. #define SET_KEY(key, newval) do { \
  622. if ((key) != (newval)) \
  623. ed25519_keypair_free(key); \
  624. key = (newval); \
  625. } while (0)
  626. #define SET_CERT(cert, newval) do { \
  627. if ((cert) != (newval)) \
  628. tor_cert_free(cert); \
  629. cert = (newval); \
  630. } while (0)
  631. #define EXPIRES_SOON(cert, interval) \
  632. (!(cert) || (cert)->valid_until < now + (interval))
  633. /* XXXX support encrypted identity keys fully */
  634. /* First try to get the signing key to see how it is. */
  635. {
  636. char *fname =
  637. options_get_datadir_fname2(options, "keys", "ed25519_signing");
  638. sign = ed_key_init_from_file(
  639. fname,
  640. INIT_ED_KEY_NEEDCERT|
  641. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT,
  642. LOG_INFO,
  643. NULL, 0, 0, CERT_TYPE_ID_SIGNING, &sign_cert);
  644. tor_free(fname);
  645. check_signing_cert = sign_cert;
  646. use_signing = sign;
  647. }
  648. if (!use_signing && master_signing_key) {
  649. check_signing_cert = signing_key_cert;
  650. use_signing = master_signing_key;
  651. }
  652. const int offline_master =
  653. options->OfflineMasterKey && options->command != CMD_KEYGEN;
  654. const int need_new_signing_key =
  655. NULL == use_signing ||
  656. EXPIRES_SOON(check_signing_cert, 0) ||
  657. (options->command == CMD_KEYGEN && ! options->change_key_passphrase);
  658. const int want_new_signing_key =
  659. need_new_signing_key ||
  660. EXPIRES_SOON(check_signing_cert, options->TestingSigningKeySlop);
  661. /* We can only create a master key if we haven't been told that the
  662. * master key will always be offline. Also, if we have a signing key,
  663. * then we shouldn't make a new master ID key. */
  664. const int can_make_master_id_key = !offline_master &&
  665. NULL == use_signing;
  666. if (need_new_signing_key) {
  667. log_notice(LD_OR, "It looks like I need to generate and sign a new "
  668. "medium-term signing key, because %s. To do that, I need to "
  669. "load%s the permanent master identity key.",
  670. (NULL == use_signing) ? "I don't have one" :
  671. EXPIRES_SOON(check_signing_cert, 0) ? "the one I have is expired" :
  672. "you asked me to make one with --keygen",
  673. can_make_master_id_key ? " (or create)" : "");
  674. } else if (want_new_signing_key && !offline_master) {
  675. log_notice(LD_OR, "It looks like I should try to generate and sign a "
  676. "new medium-term signing key, because the one I have is "
  677. "going to expire soon. To do that, I'm going to have to try to "
  678. "load the permanent master identity key.");
  679. } else if (want_new_signing_key) {
  680. log_notice(LD_OR, "It looks like I should try to generate and sign a "
  681. "new medium-term signing key, because the one I have is "
  682. "going to expire soon. But OfflineMasterKey is set, so I "
  683. "won't try to load a permanent master identity key is set. "
  684. "You will need to use 'tor --keygen' make a new signing key "
  685. "and certificate.");
  686. }
  687. {
  688. uint32_t flags =
  689. (INIT_ED_KEY_SPLIT|
  690. INIT_ED_KEY_EXTRA_STRONG|INIT_ED_KEY_NO_REPAIR);
  691. if (can_make_master_id_key)
  692. flags |= INIT_ED_KEY_CREATE;
  693. if (! need_new_signing_key)
  694. flags |= INIT_ED_KEY_MISSING_SECRET_OK;
  695. if (! want_new_signing_key || offline_master)
  696. flags |= INIT_ED_KEY_OMIT_SECRET;
  697. if (offline_master)
  698. flags |= INIT_ED_KEY_OFFLINE_SECRET;
  699. if (options->command == CMD_KEYGEN)
  700. flags |= INIT_ED_KEY_TRY_ENCRYPTED;
  701. /* Check the key directory */
  702. if (check_private_dir(options->DataDirectory, CPD_CREATE, options->User)) {
  703. log_err(LD_OR, "Can't create/check datadirectory %s",
  704. options->DataDirectory);
  705. goto err;
  706. }
  707. char *fname = get_datadir_fname("keys");
  708. if (check_private_dir(fname, CPD_CREATE, options->User) < 0) {
  709. log_err(LD_OR, "Problem creating/checking key directory %s", fname);
  710. tor_free(fname);
  711. goto err;
  712. }
  713. tor_free(fname);
  714. if (options->master_key_fname) {
  715. fname = tor_strdup(options->master_key_fname);
  716. flags |= INIT_ED_KEY_EXPLICIT_FNAME;
  717. } else {
  718. fname = options_get_datadir_fname2(options, "keys", "ed25519_master_id");
  719. }
  720. id = ed_key_init_from_file(
  721. fname,
  722. flags,
  723. LOG_WARN, NULL, 0, 0, 0, NULL);
  724. tor_free(fname);
  725. if (!id) {
  726. if (need_new_signing_key) {
  727. if (offline_master)
  728. FAIL("Can't load master identity key; OfflineMasterKey is set.");
  729. else
  730. FAIL("Missing identity key");
  731. } else {
  732. log_warn(LD_OR, "Master public key was absent; inferring from "
  733. "public key in signing certificate and saving to disk.");
  734. tor_assert(check_signing_cert);
  735. id = tor_malloc_zero(sizeof(*id));
  736. memcpy(&id->pubkey, &check_signing_cert->signing_key,
  737. sizeof(ed25519_public_key_t));
  738. fname = options_get_datadir_fname2(options, "keys",
  739. "ed25519_master_id_public_key");
  740. if (ed25519_pubkey_write_to_file(&id->pubkey, fname, "type0") < 0) {
  741. log_warn(LD_OR, "Error while attempting to write master public key "
  742. "to disk");
  743. tor_free(fname);
  744. goto err;
  745. }
  746. tor_free(fname);
  747. }
  748. }
  749. if (tor_mem_is_zero((char*)id->seckey.seckey, sizeof(id->seckey)))
  750. sign_signing_key_with_id = NULL;
  751. else
  752. sign_signing_key_with_id = id;
  753. }
  754. if (master_identity_key &&
  755. !ed25519_pubkey_eq(&id->pubkey, &master_identity_key->pubkey)) {
  756. FAIL("Identity key on disk does not match key we loaded earlier!");
  757. }
  758. if (need_new_signing_key && NULL == sign_signing_key_with_id)
  759. FAIL("Can't load master key make a new signing key.");
  760. if (sign_cert) {
  761. if (! sign_cert->signing_key_included)
  762. FAIL("Loaded a signing cert with no key included!");
  763. if (! ed25519_pubkey_eq(&sign_cert->signing_key, &id->pubkey))
  764. FAIL("The signing cert we have was not signed with the master key "
  765. "we loaded!");
  766. if (tor_cert_checksig(sign_cert, &id->pubkey, 0) < 0)
  767. FAIL("The signing cert we loaded was not signed correctly!");
  768. }
  769. if (want_new_signing_key && sign_signing_key_with_id) {
  770. uint32_t flags = (INIT_ED_KEY_CREATE|
  771. INIT_ED_KEY_REPLACE|
  772. INIT_ED_KEY_EXTRA_STRONG|
  773. INIT_ED_KEY_NEEDCERT|
  774. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT);
  775. char *fname =
  776. options_get_datadir_fname2(options, "keys", "ed25519_signing");
  777. ed25519_keypair_free(sign);
  778. tor_cert_free(sign_cert);
  779. sign = ed_key_init_from_file(fname,
  780. flags, LOG_WARN,
  781. sign_signing_key_with_id, now,
  782. options->SigningKeyLifetime,
  783. CERT_TYPE_ID_SIGNING, &sign_cert);
  784. tor_free(fname);
  785. if (!sign)
  786. FAIL("Missing signing key");
  787. use_signing = sign;
  788. tor_assert(sign_cert->signing_key_included);
  789. tor_assert(ed25519_pubkey_eq(&sign_cert->signing_key, &id->pubkey));
  790. tor_assert(ed25519_pubkey_eq(&sign_cert->signed_key, &sign->pubkey));
  791. } else if (want_new_signing_key) {
  792. static ratelim_t missing_master = RATELIM_INIT(3600);
  793. log_fn_ratelim(&missing_master, LOG_WARN, LD_OR,
  794. "Signing key will expire soon, but I can't load the "
  795. "master key to sign a new one!");
  796. }
  797. tor_assert(use_signing);
  798. /* At this point we no longer need our secret identity key. So wipe
  799. * it, if we loaded it in the first place. */
  800. memwipe(id->seckey.seckey, 0, sizeof(id->seckey));
  801. if (options->command == CMD_KEYGEN)
  802. goto end;
  803. if (!rsa_ed_crosscert && server_mode(options)) {
  804. uint8_t *crosscert;
  805. ssize_t crosscert_len = tor_make_rsa_ed25519_crosscert(&id->pubkey,
  806. get_server_identity_key(),
  807. now+10*365*86400,/*XXXX*/
  808. &crosscert);
  809. rsa_ed_crosscert_len = crosscert_len;
  810. rsa_ed_crosscert = crosscert;
  811. }
  812. if (!current_auth_key ||
  813. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop)) {
  814. auth = ed_key_new(use_signing, INIT_ED_KEY_NEEDCERT,
  815. now,
  816. options->TestingAuthKeyLifetime,
  817. CERT_TYPE_SIGNING_AUTH, &auth_cert);
  818. if (!auth)
  819. FAIL("Can't create auth key");
  820. }
  821. /* We've generated or loaded everything. Put them in memory. */
  822. end:
  823. if (! master_identity_key) {
  824. SET_KEY(master_identity_key, id);
  825. } else {
  826. tor_free(id);
  827. }
  828. if (sign) {
  829. SET_KEY(master_signing_key, sign);
  830. SET_CERT(signing_key_cert, sign_cert);
  831. }
  832. if (auth) {
  833. SET_KEY(current_auth_key, auth);
  834. SET_CERT(auth_key_cert, auth_cert);
  835. }
  836. return 0;
  837. err:
  838. ed25519_keypair_free(id);
  839. ed25519_keypair_free(sign);
  840. ed25519_keypair_free(auth);
  841. tor_cert_free(sign_cert);
  842. tor_cert_free(auth_cert);
  843. return -1;
  844. }
  845. /* DOCDOC */
  846. int
  847. generate_ed_link_cert(const or_options_t *options, time_t now)
  848. {
  849. const tor_x509_cert_t *link_ = NULL, *id = NULL;
  850. tor_cert_t *link_cert = NULL;
  851. if (tor_tls_get_my_certs(1, &link_, &id) < 0 || link_ == NULL) {
  852. log_warn(LD_OR, "Can't get my x509 link cert.");
  853. return -1;
  854. }
  855. const common_digests_t *digests = tor_x509_cert_get_cert_digests(link_);
  856. if (link_cert_cert &&
  857. ! EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop) &&
  858. fast_memeq(digests->d[DIGEST_SHA256], link_cert_cert->signed_key.pubkey,
  859. DIGEST256_LEN)) {
  860. return 0;
  861. }
  862. ed25519_public_key_t dummy_key;
  863. memcpy(dummy_key.pubkey, digests->d[DIGEST_SHA256], DIGEST256_LEN);
  864. link_cert = tor_cert_create(get_master_signing_keypair(),
  865. CERT_TYPE_SIGNING_LINK,
  866. &dummy_key,
  867. now,
  868. options->TestingLinkCertLifetime, 0);
  869. if (link_cert) {
  870. SET_CERT(link_cert_cert, link_cert);
  871. }
  872. return 0;
  873. }
  874. #undef FAIL
  875. #undef SET_KEY
  876. #undef SET_CERT
  877. int
  878. should_make_new_ed_keys(const or_options_t *options, const time_t now)
  879. {
  880. if (!master_identity_key ||
  881. !master_signing_key ||
  882. !current_auth_key ||
  883. !link_cert_cert ||
  884. EXPIRES_SOON(signing_key_cert, options->TestingSigningKeySlop) ||
  885. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop) ||
  886. EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop))
  887. return 1;
  888. const tor_x509_cert_t *link_ = NULL, *id = NULL;
  889. if (tor_tls_get_my_certs(1, &link_, &id) < 0 || link_ == NULL)
  890. return 1;
  891. const common_digests_t *digests = tor_x509_cert_get_cert_digests(link_);
  892. if (!fast_memeq(digests->d[DIGEST_SHA256],
  893. link_cert_cert->signed_key.pubkey,
  894. DIGEST256_LEN)) {
  895. return 1;
  896. }
  897. return 0;
  898. }
  899. #undef EXPIRES_SOON
  900. const ed25519_public_key_t *
  901. get_master_identity_key(void)
  902. {
  903. if (!master_identity_key)
  904. return NULL;
  905. return &master_identity_key->pubkey;
  906. }
  907. const ed25519_keypair_t *
  908. get_master_signing_keypair(void)
  909. {
  910. return master_signing_key;
  911. }
  912. const struct tor_cert_st *
  913. get_master_signing_key_cert(void)
  914. {
  915. return signing_key_cert;
  916. }
  917. const ed25519_keypair_t *
  918. get_current_auth_keypair(void)
  919. {
  920. return current_auth_key;
  921. }
  922. const tor_cert_t *
  923. get_current_link_cert_cert(void)
  924. {
  925. return link_cert_cert;
  926. }
  927. const tor_cert_t *
  928. get_current_auth_key_cert(void)
  929. {
  930. return auth_key_cert;
  931. }
  932. void
  933. get_master_rsa_crosscert(const uint8_t **cert_out,
  934. size_t *size_out)
  935. {
  936. *cert_out = rsa_ed_crosscert;
  937. *size_out = rsa_ed_crosscert_len;
  938. }
  939. /** Construct cross-certification for the master identity key with
  940. * the ntor onion key. Store the sign of the corresponding ed25519 public key
  941. * in *<b>sign_out</b>. */
  942. tor_cert_t *
  943. make_ntor_onion_key_crosscert(const curve25519_keypair_t *onion_key,
  944. const ed25519_public_key_t *master_id_key, time_t now, time_t lifetime,
  945. int *sign_out)
  946. {
  947. tor_cert_t *cert = NULL;
  948. ed25519_keypair_t ed_onion_key;
  949. if (ed25519_keypair_from_curve25519_keypair(&ed_onion_key, sign_out,
  950. onion_key) < 0)
  951. goto end;
  952. cert = tor_cert_create(&ed_onion_key, CERT_TYPE_ONION_ID, master_id_key,
  953. now, lifetime, 0);
  954. end:
  955. memwipe(&ed_onion_key, 0, sizeof(ed_onion_key));
  956. return cert;
  957. }
  958. /** Construct and return an RSA signature for the TAP onion key to
  959. * cross-certify the RSA and Ed25519 identity keys. Set <b>len_out</b> to its
  960. * length. */
  961. uint8_t *
  962. make_tap_onion_key_crosscert(const crypto_pk_t *onion_key,
  963. const ed25519_public_key_t *master_id_key,
  964. const crypto_pk_t *rsa_id_key,
  965. int *len_out)
  966. {
  967. uint8_t signature[PK_BYTES];
  968. uint8_t signed_data[DIGEST_LEN + ED25519_PUBKEY_LEN];
  969. *len_out = 0;
  970. crypto_pk_get_digest(rsa_id_key, (char*)signed_data);
  971. memcpy(signed_data + DIGEST_LEN, master_id_key->pubkey, ED25519_PUBKEY_LEN);
  972. int r = crypto_pk_private_sign(onion_key,
  973. (char*)signature, sizeof(signature),
  974. (const char*)signed_data, sizeof(signed_data));
  975. if (r < 0)
  976. return NULL;
  977. *len_out = r;
  978. return tor_memdup(signature, r);
  979. }
  980. /** Check whether an RSA-TAP cross-certification is correct. Return 0 if it
  981. * is, -1 if it isn't. */
  982. int
  983. check_tap_onion_key_crosscert(const uint8_t *crosscert,
  984. int crosscert_len,
  985. const crypto_pk_t *onion_pkey,
  986. const ed25519_public_key_t *master_id_pkey,
  987. const uint8_t *rsa_id_digest)
  988. {
  989. uint8_t *cc = tor_malloc(crypto_pk_keysize(onion_pkey));
  990. int cc_len =
  991. crypto_pk_public_checksig(onion_pkey,
  992. (char*)cc,
  993. crypto_pk_keysize(onion_pkey),
  994. (const char*)crosscert,
  995. crosscert_len);
  996. if (cc_len < 0) {
  997. goto err;
  998. }
  999. if (cc_len < DIGEST_LEN + ED25519_PUBKEY_LEN) {
  1000. log_warn(LD_DIR, "Short signature on cross-certification with TAP key");
  1001. goto err;
  1002. }
  1003. if (tor_memneq(cc, rsa_id_digest, DIGEST_LEN) ||
  1004. tor_memneq(cc + DIGEST_LEN, master_id_pkey->pubkey,
  1005. ED25519_PUBKEY_LEN)) {
  1006. log_warn(LD_DIR, "Incorrect cross-certification with TAP key");
  1007. goto err;
  1008. }
  1009. tor_free(cc);
  1010. return 0;
  1011. err:
  1012. tor_free(cc);
  1013. return -1;
  1014. }
  1015. void
  1016. routerkeys_free_all(void)
  1017. {
  1018. ed25519_keypair_free(master_identity_key);
  1019. ed25519_keypair_free(master_signing_key);
  1020. ed25519_keypair_free(current_auth_key);
  1021. tor_cert_free(signing_key_cert);
  1022. tor_cert_free(link_cert_cert);
  1023. tor_cert_free(auth_key_cert);
  1024. master_identity_key = master_signing_key = NULL;
  1025. current_auth_key = NULL;
  1026. signing_key_cert = link_cert_cert = auth_key_cert = NULL;
  1027. }