routerkeys.c 24 KB

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