routerkeys.c 45 KB

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