routerkeys.c 41 KB

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