routerkeys.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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 "core/or/or.h"
  16. #include "app/config/config.h"
  17. #include "feature/relay/router.h"
  18. #include "feature/relay/routerkeys.h"
  19. #include "feature/relay/routermode.h"
  20. #include "feature/keymgt/loadkey.h"
  21. #include "feature/nodelist/torcert.h"
  22. #include "lib/crypt_ops/crypto_util.h"
  23. #include "lib/tls/tortls.h"
  24. #include "lib/tls/x509.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. static ed25519_keypair_t *master_identity_key = NULL;
  31. static ed25519_keypair_t *master_signing_key = NULL;
  32. static ed25519_keypair_t *current_auth_key = NULL;
  33. static tor_cert_t *signing_key_cert = NULL;
  34. static tor_cert_t *link_cert_cert = NULL;
  35. static tor_cert_t *auth_key_cert = NULL;
  36. static uint8_t *rsa_ed_crosscert = NULL;
  37. static size_t rsa_ed_crosscert_len = 0;
  38. static time_t rsa_ed_crosscert_expiration = 0;
  39. /**
  40. * Running as a server: load, reload, or refresh our ed25519 keys and
  41. * certificates, creating and saving new ones as needed.
  42. *
  43. * Return -1 on failure; 0 on success if the signing key was not replaced;
  44. * and 1 on success if the signing key was replaced.
  45. */
  46. int
  47. load_ed_keys(const or_options_t *options, time_t now)
  48. {
  49. ed25519_keypair_t *id = NULL;
  50. ed25519_keypair_t *sign = NULL;
  51. ed25519_keypair_t *auth = NULL;
  52. const ed25519_keypair_t *sign_signing_key_with_id = NULL;
  53. const ed25519_keypair_t *use_signing = NULL;
  54. const tor_cert_t *check_signing_cert = NULL;
  55. tor_cert_t *sign_cert = NULL;
  56. tor_cert_t *auth_cert = NULL;
  57. int signing_key_changed = 0;
  58. // It is later than 1972, since otherwise there would be no C compilers.
  59. // (Try to diagnose #22466.)
  60. tor_assert_nonfatal(now >= 2 * 365 * 86400);
  61. #define FAIL(msg) do { \
  62. log_warn(LD_OR, (msg)); \
  63. goto err; \
  64. } while (0)
  65. #define SET_KEY(key, newval) do { \
  66. if ((key) != (newval)) \
  67. ed25519_keypair_free(key); \
  68. key = (newval); \
  69. } while (0)
  70. #define SET_CERT(cert, newval) do { \
  71. if ((cert) != (newval)) \
  72. tor_cert_free(cert); \
  73. cert = (newval); \
  74. } while (0)
  75. #define HAPPENS_SOON(when, interval) \
  76. ((when) < now + (interval))
  77. #define EXPIRES_SOON(cert, interval) \
  78. (!(cert) || HAPPENS_SOON((cert)->valid_until, (interval)))
  79. /* XXXX support encrypted identity keys fully */
  80. /* First try to get the signing key to see how it is. */
  81. {
  82. char *fname =
  83. options_get_keydir_fname(options, "ed25519_signing");
  84. sign = ed_key_init_from_file(
  85. fname,
  86. INIT_ED_KEY_NEEDCERT|
  87. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT,
  88. LOG_INFO,
  89. NULL, 0, 0, CERT_TYPE_ID_SIGNING, &sign_cert, options);
  90. tor_free(fname);
  91. check_signing_cert = sign_cert;
  92. use_signing = sign;
  93. }
  94. if (use_signing) {
  95. /* We loaded a signing key with its certificate. */
  96. if (! master_signing_key) {
  97. /* We didn't know one before! */
  98. signing_key_changed = 1;
  99. } else if (! ed25519_pubkey_eq(&use_signing->pubkey,
  100. &master_signing_key->pubkey) ||
  101. ! tor_memeq(use_signing->seckey.seckey,
  102. master_signing_key->seckey.seckey,
  103. ED25519_SECKEY_LEN)) {
  104. /* We loaded a different signing key than the one we knew before. */
  105. signing_key_changed = 1;
  106. }
  107. }
  108. if (!use_signing && master_signing_key) {
  109. /* We couldn't load a signing key, but we already had one loaded */
  110. check_signing_cert = signing_key_cert;
  111. use_signing = master_signing_key;
  112. }
  113. const int offline_master =
  114. options->OfflineMasterKey && options->command != CMD_KEYGEN;
  115. const int need_new_signing_key =
  116. NULL == use_signing ||
  117. EXPIRES_SOON(check_signing_cert, 0) ||
  118. (options->command == CMD_KEYGEN && ! options->change_key_passphrase);
  119. const int want_new_signing_key =
  120. need_new_signing_key ||
  121. EXPIRES_SOON(check_signing_cert, options->TestingSigningKeySlop);
  122. /* We can only create a master key if we haven't been told that the
  123. * master key will always be offline. Also, if we have a signing key,
  124. * then we shouldn't make a new master ID key. */
  125. const int can_make_master_id_key = !offline_master &&
  126. NULL == use_signing;
  127. if (need_new_signing_key) {
  128. log_notice(LD_OR, "It looks like I need to generate and sign a new "
  129. "medium-term signing key, because %s. To do that, I "
  130. "need to load%s the permanent master identity key. "
  131. "If the master identity key was not moved or encrypted "
  132. "with a passphrase, this will be done automatically and "
  133. "no further action is required. Otherwise, provide the "
  134. "necessary data using 'tor --keygen' to do it manually.",
  135. (NULL == use_signing) ? "I don't have one" :
  136. EXPIRES_SOON(check_signing_cert, 0) ? "the one I have is expired" :
  137. "you asked me to make one with --keygen",
  138. can_make_master_id_key ? " (or create)" : "");
  139. } else if (want_new_signing_key && !offline_master) {
  140. log_notice(LD_OR, "It looks like I should try to generate and sign a "
  141. "new medium-term signing key, because the one I have is "
  142. "going to expire soon. To do that, I'm going to have to "
  143. "try to load the permanent master identity key. "
  144. "If the master identity key was not moved or encrypted "
  145. "with a passphrase, this will be done automatically and "
  146. "no further action is required. Otherwise, provide the "
  147. "necessary data using 'tor --keygen' to do it manually.");
  148. } else if (want_new_signing_key) {
  149. log_notice(LD_OR, "It looks like I should try to generate and sign a "
  150. "new medium-term signing key, because the one I have is "
  151. "going to expire soon. But OfflineMasterKey is set, so I "
  152. "won't try to load a permanent master identity key. You "
  153. "will need to use 'tor --keygen' to make a new signing "
  154. "key and certificate.");
  155. }
  156. {
  157. uint32_t flags =
  158. (INIT_ED_KEY_SPLIT|
  159. INIT_ED_KEY_EXTRA_STRONG|INIT_ED_KEY_NO_REPAIR);
  160. if (can_make_master_id_key)
  161. flags |= INIT_ED_KEY_CREATE;
  162. if (! need_new_signing_key)
  163. flags |= INIT_ED_KEY_MISSING_SECRET_OK;
  164. if (! want_new_signing_key || offline_master)
  165. flags |= INIT_ED_KEY_OMIT_SECRET;
  166. if (offline_master)
  167. flags |= INIT_ED_KEY_OFFLINE_SECRET;
  168. if (options->command == CMD_KEYGEN)
  169. flags |= INIT_ED_KEY_TRY_ENCRYPTED;
  170. /* Check/Create the key directory */
  171. if (create_keys_directory(options) < 0)
  172. return -1;
  173. char *fname;
  174. if (options->master_key_fname) {
  175. fname = tor_strdup(options->master_key_fname);
  176. flags |= INIT_ED_KEY_EXPLICIT_FNAME;
  177. } else {
  178. fname = options_get_keydir_fname(options, "ed25519_master_id");
  179. }
  180. id = ed_key_init_from_file(
  181. fname,
  182. flags,
  183. LOG_WARN, NULL, 0, 0, 0, NULL, options);
  184. tor_free(fname);
  185. if (!id) {
  186. if (need_new_signing_key) {
  187. if (offline_master)
  188. FAIL("Can't load master identity key; OfflineMasterKey is set.");
  189. else
  190. FAIL("Missing identity key");
  191. } else {
  192. log_warn(LD_OR, "Master public key was absent; inferring from "
  193. "public key in signing certificate and saving to disk.");
  194. tor_assert(check_signing_cert);
  195. id = tor_malloc_zero(sizeof(*id));
  196. memcpy(&id->pubkey, &check_signing_cert->signing_key,
  197. sizeof(ed25519_public_key_t));
  198. fname = options_get_keydir_fname(options,
  199. "ed25519_master_id_public_key");
  200. if (ed25519_pubkey_write_to_file(&id->pubkey, fname, "type0") < 0) {
  201. log_warn(LD_OR, "Error while attempting to write master public key "
  202. "to disk");
  203. tor_free(fname);
  204. goto err;
  205. }
  206. tor_free(fname);
  207. }
  208. }
  209. if (tor_mem_is_zero((char*)id->seckey.seckey, sizeof(id->seckey)))
  210. sign_signing_key_with_id = NULL;
  211. else
  212. sign_signing_key_with_id = id;
  213. }
  214. if (master_identity_key &&
  215. !ed25519_pubkey_eq(&id->pubkey, &master_identity_key->pubkey)) {
  216. FAIL("Identity key on disk does not match key we loaded earlier!");
  217. }
  218. if (need_new_signing_key && NULL == sign_signing_key_with_id)
  219. FAIL("Can't load master key make a new signing key.");
  220. if (sign_cert) {
  221. if (! sign_cert->signing_key_included)
  222. FAIL("Loaded a signing cert with no key included!");
  223. if (! ed25519_pubkey_eq(&sign_cert->signing_key, &id->pubkey))
  224. FAIL("The signing cert we have was not signed with the master key "
  225. "we loaded!");
  226. if (tor_cert_checksig(sign_cert, &id->pubkey, 0) < 0) {
  227. log_warn(LD_OR, "The signing cert we loaded was not signed "
  228. "correctly: %s!",
  229. tor_cert_describe_signature_status(sign_cert));
  230. goto err;
  231. }
  232. }
  233. if (want_new_signing_key && sign_signing_key_with_id) {
  234. uint32_t flags = (INIT_ED_KEY_CREATE|
  235. INIT_ED_KEY_REPLACE|
  236. INIT_ED_KEY_EXTRA_STRONG|
  237. INIT_ED_KEY_NEEDCERT|
  238. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT);
  239. char *fname =
  240. options_get_keydir_fname(options, "ed25519_signing");
  241. ed25519_keypair_free(sign);
  242. tor_cert_free(sign_cert);
  243. sign = ed_key_init_from_file(fname,
  244. flags, LOG_WARN,
  245. sign_signing_key_with_id, now,
  246. options->SigningKeyLifetime,
  247. CERT_TYPE_ID_SIGNING, &sign_cert, options);
  248. tor_free(fname);
  249. if (!sign)
  250. FAIL("Missing signing key");
  251. use_signing = sign;
  252. signing_key_changed = 1;
  253. tor_assert(sign_cert->signing_key_included);
  254. tor_assert(ed25519_pubkey_eq(&sign_cert->signing_key, &id->pubkey));
  255. tor_assert(ed25519_pubkey_eq(&sign_cert->signed_key, &sign->pubkey));
  256. } else if (want_new_signing_key) {
  257. static ratelim_t missing_master = RATELIM_INIT(3600);
  258. log_fn_ratelim(&missing_master, LOG_WARN, LD_OR,
  259. "Signing key will expire soon, but I can't load the "
  260. "master key to sign a new one!");
  261. }
  262. tor_assert(use_signing);
  263. /* At this point we no longer need our secret identity key. So wipe
  264. * it, if we loaded it in the first place. */
  265. memwipe(id->seckey.seckey, 0, sizeof(id->seckey));
  266. if (options->command == CMD_KEYGEN)
  267. goto end;
  268. if (server_mode(options) &&
  269. (!rsa_ed_crosscert ||
  270. HAPPENS_SOON(rsa_ed_crosscert_expiration, 30*86400))) {
  271. uint8_t *crosscert;
  272. time_t expiration = now+6*30*86400; /* 6 months in the future. */
  273. ssize_t crosscert_len = tor_make_rsa_ed25519_crosscert(&id->pubkey,
  274. get_server_identity_key(),
  275. expiration,
  276. &crosscert);
  277. tor_free(rsa_ed_crosscert);
  278. rsa_ed_crosscert_len = crosscert_len;
  279. rsa_ed_crosscert = crosscert;
  280. rsa_ed_crosscert_expiration = expiration;
  281. }
  282. if (!current_auth_key ||
  283. signing_key_changed ||
  284. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop)) {
  285. auth = ed_key_new(use_signing, INIT_ED_KEY_NEEDCERT,
  286. now,
  287. options->TestingAuthKeyLifetime,
  288. CERT_TYPE_SIGNING_AUTH, &auth_cert);
  289. if (!auth)
  290. FAIL("Can't create auth key");
  291. }
  292. /* We've generated or loaded everything. Put them in memory. */
  293. end:
  294. if (! master_identity_key) {
  295. SET_KEY(master_identity_key, id);
  296. } else {
  297. tor_free(id);
  298. }
  299. if (sign) {
  300. SET_KEY(master_signing_key, sign);
  301. SET_CERT(signing_key_cert, sign_cert);
  302. }
  303. if (auth) {
  304. SET_KEY(current_auth_key, auth);
  305. SET_CERT(auth_key_cert, auth_cert);
  306. }
  307. return signing_key_changed;
  308. err:
  309. ed25519_keypair_free(id);
  310. ed25519_keypair_free(sign);
  311. ed25519_keypair_free(auth);
  312. tor_cert_free(sign_cert);
  313. tor_cert_free(auth_cert);
  314. return -1;
  315. }
  316. /**
  317. * Retrieve our currently-in-use Ed25519 link certificate and id certificate,
  318. * and, if they would expire soon (based on the time <b>now</b>, generate new
  319. * certificates (without embedding the public part of the signing key inside).
  320. * If <b>force</b> is true, always generate a new certificate.
  321. *
  322. * The signed_key from the current id->signing certificate will be used to
  323. * sign the new key within newly generated X509 certificate.
  324. *
  325. * Returns -1 upon error. Otherwise, returns 0 upon success (either when the
  326. * current certificate is still valid, or when a new certificate was
  327. * successfully generated, or no certificate was needed).
  328. */
  329. int
  330. generate_ed_link_cert(const or_options_t *options, time_t now,
  331. int force)
  332. {
  333. const tor_x509_cert_t *link_ = NULL, *id = NULL;
  334. tor_cert_t *link_cert = NULL;
  335. if (tor_tls_get_my_certs(1, &link_, &id) < 0 || link_ == NULL) {
  336. if (!server_mode(options)) {
  337. /* No need to make an Ed25519->Link cert: we are a client */
  338. return 0;
  339. }
  340. log_warn(LD_OR, "Can't get my x509 link cert.");
  341. return -1;
  342. }
  343. const common_digests_t *digests = tor_x509_cert_get_cert_digests(link_);
  344. if (force == 0 &&
  345. link_cert_cert &&
  346. ! EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop) &&
  347. fast_memeq(digests->d[DIGEST_SHA256], link_cert_cert->signed_key.pubkey,
  348. DIGEST256_LEN)) {
  349. return 0;
  350. }
  351. ed25519_public_key_t dummy_key;
  352. memcpy(dummy_key.pubkey, digests->d[DIGEST_SHA256], DIGEST256_LEN);
  353. link_cert = tor_cert_create(get_master_signing_keypair(),
  354. CERT_TYPE_SIGNING_LINK,
  355. &dummy_key,
  356. now,
  357. options->TestingLinkCertLifetime, 0);
  358. if (link_cert) {
  359. SET_CERT(link_cert_cert, link_cert);
  360. }
  361. return 0;
  362. }
  363. #undef FAIL
  364. #undef SET_KEY
  365. #undef SET_CERT
  366. /**
  367. * Return 1 if any of the following are true:
  368. *
  369. * - if one of our Ed25519 signing, auth, or link certificates would expire
  370. * soon w.r.t. the time <b>now</b>,
  371. * - if we do not currently have a link certificate, or
  372. * - if our cached Ed25519 link certificate is not same as the one we're
  373. * currently using.
  374. *
  375. * Otherwise, returns 0.
  376. */
  377. int
  378. should_make_new_ed_keys(const or_options_t *options, const time_t now)
  379. {
  380. if (!master_identity_key ||
  381. !master_signing_key ||
  382. !current_auth_key ||
  383. !link_cert_cert ||
  384. EXPIRES_SOON(signing_key_cert, options->TestingSigningKeySlop) ||
  385. EXPIRES_SOON(auth_key_cert, options->TestingAuthKeySlop) ||
  386. EXPIRES_SOON(link_cert_cert, options->TestingLinkKeySlop))
  387. return 1;
  388. const tor_x509_cert_t *link_ = NULL, *id = NULL;
  389. if (tor_tls_get_my_certs(1, &link_, &id) < 0 || link_ == NULL)
  390. return 1;
  391. const common_digests_t *digests = tor_x509_cert_get_cert_digests(link_);
  392. if (!fast_memeq(digests->d[DIGEST_SHA256],
  393. link_cert_cert->signed_key.pubkey,
  394. DIGEST256_LEN)) {
  395. return 1;
  396. }
  397. return 0;
  398. }
  399. #undef EXPIRES_SOON
  400. #undef HAPPENS_SOON
  401. #ifdef TOR_UNIT_TESTS
  402. /* Helper for unit tests: populate the ed25519 keys without saving or
  403. * loading */
  404. void
  405. init_mock_ed_keys(const crypto_pk_t *rsa_identity_key)
  406. {
  407. routerkeys_free_all();
  408. #define MAKEKEY(k) \
  409. k = tor_malloc_zero(sizeof(*k)); \
  410. if (ed25519_keypair_generate(k, 0) < 0) { \
  411. log_warn(LD_BUG, "Couldn't make a keypair"); \
  412. goto err; \
  413. }
  414. MAKEKEY(master_identity_key);
  415. MAKEKEY(master_signing_key);
  416. MAKEKEY(current_auth_key);
  417. #define MAKECERT(cert, signing, signed_, type, flags) \
  418. cert = tor_cert_create(signing, \
  419. type, \
  420. &signed_->pubkey, \
  421. time(NULL), 86400, \
  422. flags); \
  423. if (!cert) { \
  424. log_warn(LD_BUG, "Couldn't make a %s certificate!", #cert); \
  425. goto err; \
  426. }
  427. MAKECERT(signing_key_cert,
  428. master_identity_key, master_signing_key, CERT_TYPE_ID_SIGNING,
  429. CERT_FLAG_INCLUDE_SIGNING_KEY);
  430. MAKECERT(auth_key_cert,
  431. master_signing_key, current_auth_key, CERT_TYPE_SIGNING_AUTH, 0);
  432. if (generate_ed_link_cert(get_options(), time(NULL), 0) < 0) {
  433. log_warn(LD_BUG, "Couldn't make link certificate");
  434. goto err;
  435. }
  436. rsa_ed_crosscert_len = tor_make_rsa_ed25519_crosscert(
  437. &master_identity_key->pubkey,
  438. rsa_identity_key,
  439. time(NULL)+86400,
  440. &rsa_ed_crosscert);
  441. return;
  442. err:
  443. routerkeys_free_all();
  444. tor_assert_nonfatal_unreached();
  445. }
  446. #undef MAKEKEY
  447. #undef MAKECERT
  448. #endif /* defined(TOR_UNIT_TESTS) */
  449. /**
  450. * Print the ISO8601-formated <b>expiration</b> for a certificate with
  451. * some <b>description</b> to stdout.
  452. *
  453. * For example, for a signing certificate, this might print out:
  454. * signing-cert-expiry: 2017-07-25 08:30:15 UTC
  455. */
  456. static void
  457. print_cert_expiration(const char *expiration,
  458. const char *description)
  459. {
  460. fprintf(stderr, "%s-cert-expiry: %s\n", description, expiration);
  461. }
  462. /**
  463. * Log when a certificate, <b>cert</b>, with some <b>description</b> and
  464. * stored in a file named <b>fname</b>, is going to expire.
  465. */
  466. static void
  467. log_ed_cert_expiration(const tor_cert_t *cert,
  468. const char *description,
  469. const char *fname) {
  470. char expiration[ISO_TIME_LEN+1];
  471. if (BUG(!cert)) { /* If the specified key hasn't been loaded */
  472. log_warn(LD_OR, "No %s key loaded; can't get certificate expiration.",
  473. description);
  474. } else {
  475. format_local_iso_time(expiration, cert->valid_until);
  476. log_notice(LD_OR, "The %s certificate stored in %s is valid until %s.",
  477. description, fname, expiration);
  478. print_cert_expiration(expiration, description);
  479. }
  480. }
  481. /**
  482. * Log when our master signing key certificate expires. Used when tor is given
  483. * the --key-expiration command-line option.
  484. *
  485. * Returns 0 on success and 1 on failure.
  486. */
  487. static int
  488. log_master_signing_key_cert_expiration(const or_options_t *options)
  489. {
  490. const tor_cert_t *signing_key;
  491. char *fn = NULL;
  492. int failed = 0;
  493. time_t now = approx_time();
  494. fn = options_get_keydir_fname(options, "ed25519_signing_cert");
  495. /* Try to grab our cached copy of the key. */
  496. signing_key = get_master_signing_key_cert();
  497. tor_assert(server_identity_key_is_set());
  498. /* Load our keys from disk, if necessary. */
  499. if (!signing_key) {
  500. failed = load_ed_keys(options, now) < 0;
  501. signing_key = get_master_signing_key_cert();
  502. }
  503. /* If we do have a signing key, log the expiration time. */
  504. if (signing_key) {
  505. log_ed_cert_expiration(signing_key, "signing", fn);
  506. } else {
  507. log_warn(LD_OR, "Could not load signing key certificate from %s, so " \
  508. "we couldn't learn anything about certificate expiration.", fn);
  509. }
  510. tor_free(fn);
  511. return failed;
  512. }
  513. /**
  514. * Log when a key certificate expires. Used when tor is given the
  515. * --key-expiration command-line option.
  516. *
  517. * If an command argument is given, which should specify the type of
  518. * key to get expiry information about (currently supported arguments
  519. * are "sign"), get info about that type of certificate. Otherwise,
  520. * print info about the supported arguments.
  521. *
  522. * Returns 0 on success and -1 on failure.
  523. */
  524. int
  525. log_cert_expiration(void)
  526. {
  527. const or_options_t *options = get_options();
  528. const char *arg = options->command_arg;
  529. if (!strcmp(arg, "sign")) {
  530. return log_master_signing_key_cert_expiration(options);
  531. } else {
  532. fprintf(stderr, "No valid argument to --key-expiration found!\n");
  533. fprintf(stderr, "Currently recognised arguments are: 'sign'\n");
  534. return -1;
  535. }
  536. }
  537. const ed25519_public_key_t *
  538. get_master_identity_key(void)
  539. {
  540. if (!master_identity_key)
  541. return NULL;
  542. return &master_identity_key->pubkey;
  543. }
  544. /** Return true iff <b>id</b> is our Ed25519 master identity key. */
  545. int
  546. router_ed25519_id_is_me(const ed25519_public_key_t *id)
  547. {
  548. return id && master_identity_key &&
  549. ed25519_pubkey_eq(id, &master_identity_key->pubkey);
  550. }
  551. #ifdef TOR_UNIT_TESTS
  552. /* only exists for the unit tests, since otherwise the identity key
  553. * should be used to sign nothing but the signing key. */
  554. const ed25519_keypair_t *
  555. get_master_identity_keypair(void)
  556. {
  557. return master_identity_key;
  558. }
  559. #endif /* defined(TOR_UNIT_TESTS) */
  560. const ed25519_keypair_t *
  561. get_master_signing_keypair(void)
  562. {
  563. return master_signing_key;
  564. }
  565. const struct tor_cert_st *
  566. get_master_signing_key_cert(void)
  567. {
  568. return signing_key_cert;
  569. }
  570. const ed25519_keypair_t *
  571. get_current_auth_keypair(void)
  572. {
  573. return current_auth_key;
  574. }
  575. const tor_cert_t *
  576. get_current_link_cert_cert(void)
  577. {
  578. return link_cert_cert;
  579. }
  580. const tor_cert_t *
  581. get_current_auth_key_cert(void)
  582. {
  583. return auth_key_cert;
  584. }
  585. void
  586. get_master_rsa_crosscert(const uint8_t **cert_out,
  587. size_t *size_out)
  588. {
  589. *cert_out = rsa_ed_crosscert;
  590. *size_out = rsa_ed_crosscert_len;
  591. }
  592. /** Construct cross-certification for the master identity key with
  593. * the ntor onion key. Store the sign of the corresponding ed25519 public key
  594. * in *<b>sign_out</b>. */
  595. tor_cert_t *
  596. make_ntor_onion_key_crosscert(const curve25519_keypair_t *onion_key,
  597. const ed25519_public_key_t *master_id_key, time_t now, time_t lifetime,
  598. int *sign_out)
  599. {
  600. tor_cert_t *cert = NULL;
  601. ed25519_keypair_t ed_onion_key;
  602. if (ed25519_keypair_from_curve25519_keypair(&ed_onion_key, sign_out,
  603. onion_key) < 0)
  604. goto end;
  605. cert = tor_cert_create(&ed_onion_key, CERT_TYPE_ONION_ID, master_id_key,
  606. now, lifetime, 0);
  607. end:
  608. memwipe(&ed_onion_key, 0, sizeof(ed_onion_key));
  609. return cert;
  610. }
  611. /** Construct and return an RSA signature for the TAP onion key to
  612. * cross-certify the RSA and Ed25519 identity keys. Set <b>len_out</b> to its
  613. * length. */
  614. uint8_t *
  615. make_tap_onion_key_crosscert(const crypto_pk_t *onion_key,
  616. const ed25519_public_key_t *master_id_key,
  617. const crypto_pk_t *rsa_id_key,
  618. int *len_out)
  619. {
  620. uint8_t signature[PK_BYTES];
  621. uint8_t signed_data[DIGEST_LEN + ED25519_PUBKEY_LEN];
  622. *len_out = 0;
  623. if (crypto_pk_get_digest(rsa_id_key, (char*)signed_data) < 0) {
  624. return NULL;
  625. }
  626. memcpy(signed_data + DIGEST_LEN, master_id_key->pubkey, ED25519_PUBKEY_LEN);
  627. int r = crypto_pk_private_sign(onion_key,
  628. (char*)signature, sizeof(signature),
  629. (const char*)signed_data, sizeof(signed_data));
  630. if (r < 0)
  631. return NULL;
  632. *len_out = r;
  633. return tor_memdup(signature, r);
  634. }
  635. void
  636. routerkeys_free_all(void)
  637. {
  638. ed25519_keypair_free(master_identity_key);
  639. ed25519_keypair_free(master_signing_key);
  640. ed25519_keypair_free(current_auth_key);
  641. tor_cert_free(signing_key_cert);
  642. tor_cert_free(link_cert_cert);
  643. tor_cert_free(auth_key_cert);
  644. tor_free(rsa_ed_crosscert);
  645. master_identity_key = master_signing_key = NULL;
  646. current_auth_key = NULL;
  647. signing_key_cert = link_cert_cert = auth_key_cert = NULL;
  648. rsa_ed_crosscert = NULL; // redundant
  649. rsa_ed_crosscert_len = 0;
  650. }