routerkeys.c 32 KB

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