routerkeys.c 35 KB

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