routerkeys.c 36 KB

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