routerkeys.c 36 KB

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