routerkeys.c 45 KB

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