routerkeys.c 45 KB

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