routerkeys.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Copyright (c) 2014, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #include "config.h"
  5. #include "routerkeys.h"
  6. #include "torcert.h"
  7. /**
  8. * Read an ed25519 key and associated certificates from files beginning with
  9. * <b>fname</b>, with certificate type <b>cert_type</b>. On failure, return
  10. * NULL; on success return the keypair.
  11. *
  12. * If INIT_ED_KEY_CREATE is set in <b>flags</b>, then create the key (and
  13. * certificate if requested) if it doesn't exist, and save it to disk.
  14. *
  15. * If INIT_ED_KEY_NEEDCERT is set in <b>flags</b>, load/create a certificate
  16. * too and store it in *<b>cert_out</b>. Fail if the cert can't be
  17. * found/created. To create a certificate, <b>signing_key</b> must be set to
  18. * the key that should sign it; <b>now</b> to the current time, and
  19. * <b>lifetime</b> to the lifetime of the key.
  20. *
  21. * If INIT_ED_KEY_REPLACE is set in <b>flags</b>, then create and save new key
  22. * whether we can read the old one or not.
  23. *
  24. * If INIT_ED_KEY_EXTRA_STRONG is set in <b>flags</b>, set the extra_strong
  25. * flag when creating the secret key.
  26. *
  27. * If INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT is set in <b>flags</b>, and
  28. * we create a new certificate, create it with the signing key embedded.
  29. *
  30. * If INIT_ED_KEY_SPLIT is set in <b>flags</b>, and we create a new key,
  31. * store the public key in a separate file from the secret key.
  32. *
  33. * If INIT_ED_KEY_MISSING_SECRET_OK is set in <b>flags</b>, and we find a
  34. * public key file but no secret key file, return successfully anyway.
  35. */
  36. ed25519_keypair_t *
  37. ed_key_init_from_file(const char *fname, uint32_t flags,
  38. int severity,
  39. const ed25519_keypair_t *signing_key,
  40. time_t now,
  41. time_t lifetime,
  42. uint8_t cert_type,
  43. struct tor_cert_st **cert_out)
  44. {
  45. char *secret_fname = NULL;
  46. char *public_fname = NULL;
  47. char *cert_fname = NULL;
  48. int created_pk = 0, created_sk = 0, created_cert = 0;
  49. const int try_to_load = ! (flags & INIT_ED_KEY_REPLACE);
  50. char tag[8];
  51. tor_snprintf(tag, sizeof(tag), "type%d", (int)cert_type);
  52. tor_cert_t *cert = NULL;
  53. char *got_tag = NULL;
  54. ed25519_keypair_t *keypair = tor_malloc_zero(sizeof(ed25519_keypair_t));
  55. tor_asprintf(&secret_fname, "%s_secret_key", fname);
  56. tor_asprintf(&public_fname, "%s_public_key", fname);
  57. tor_asprintf(&cert_fname, "%s_cert", fname);
  58. /* Try to read the secret key. */
  59. const int have_secret = try_to_load &&
  60. ed25519_seckey_read_from_file(&keypair->seckey,
  61. &got_tag, secret_fname) == 0;
  62. if (have_secret) {
  63. if (strcmp(got_tag, tag)) {
  64. tor_log(severity, LD_OR, "%s has wrong tag", secret_fname);
  65. goto err;
  66. }
  67. /* Derive the public key */
  68. if (ed25519_public_key_generate(&keypair->pubkey, &keypair->seckey)<0) {
  69. tor_log(severity, LD_OR, "%s can't produce a public key", secret_fname);
  70. goto err;
  71. }
  72. }
  73. /* If it's absent and that's okay, try to read the pubkey. */
  74. int found_public = 0;
  75. if (!have_secret && try_to_load && (flags & INIT_ED_KEY_MISSING_SECRET_OK)) {
  76. tor_free(got_tag);
  77. found_public = ed25519_pubkey_read_from_file(&keypair->pubkey,
  78. &got_tag, public_fname) == 0;
  79. if (found_public && strcmp(got_tag, tag)) {
  80. tor_log(severity, LD_OR, "%s has wrong tag", public_fname);
  81. goto err;
  82. }
  83. }
  84. /* If it's absent, and we're not supposed to make a new keypair, fail. */
  85. if (!have_secret && !found_public && !(flags & INIT_ED_KEY_CREATE))
  86. goto err;
  87. /* if it's absent, make a new keypair and save it. */
  88. if (!have_secret && !found_public) {
  89. const int split = !! (flags & INIT_ED_KEY_SPLIT);
  90. tor_free(keypair);
  91. keypair = ed_key_new(signing_key, flags, now, lifetime,
  92. cert_type, &cert);
  93. if (!keypair) {
  94. tor_log(severity, LD_OR, "Couldn't create keypair");
  95. goto err;
  96. }
  97. created_pk = created_sk = created_cert = 1;
  98. if (ed25519_seckey_write_to_file(&keypair->seckey, secret_fname, tag) < 0
  99. ||
  100. (split &&
  101. ed25519_pubkey_write_to_file(&keypair->pubkey, public_fname, tag) < 0)
  102. ||
  103. (cert &&
  104. crypto_write_tagged_contents_to_file(cert_fname, "ed25519v1-cert",
  105. tag, cert->encoded, cert->encoded_len) < 0)) {
  106. tor_log(severity, LD_OR, "Couldn't write keys or cert to file.");
  107. goto err;
  108. }
  109. goto done;
  110. }
  111. /* If we're not supposed to get a cert, we're done. */
  112. if (! (flags & INIT_ED_KEY_NEEDCERT))
  113. goto done;
  114. /* Read a cert. */
  115. uint8_t certbuf[256];
  116. ssize_t cert_body_len = crypto_read_tagged_contents_from_file(
  117. cert_fname, "ed25519v1-cert",
  118. &got_tag, certbuf, sizeof(certbuf));
  119. if (cert_body_len >= 0 && !strcmp(got_tag, tag))
  120. cert = tor_cert_parse(certbuf, cert_body_len);
  121. /* If we got it, check it to the extent we can. */
  122. if (cert) {
  123. int bad_cert = 0;
  124. if (! cert) {
  125. tor_log(severity, LD_OR, "Cert was unparseable");
  126. bad_cert = 1;
  127. } else if (!tor_memeq(cert->signed_key.pubkey, keypair->pubkey.pubkey,
  128. ED25519_PUBKEY_LEN)) {
  129. tor_log(severity, LD_OR, "Cert was for wrong key");
  130. bad_cert = 1;
  131. } else if (tor_cert_checksig(cert, &signing_key->pubkey, now) < 0 &&
  132. (signing_key || cert->cert_expired)) {
  133. tor_log(severity, LD_OR, "Can't check certificate");
  134. bad_cert = 1;
  135. }
  136. if (bad_cert) {
  137. tor_cert_free(cert);
  138. cert = NULL;
  139. }
  140. }
  141. /* If we got a cert, we're done. */
  142. if (cert)
  143. goto done;
  144. /* If we didn't get a cert, and we're not supposed to make one, fail. */
  145. if (!signing_key || !(flags & INIT_ED_KEY_CREATE))
  146. goto err;
  147. /* We have keys but not a certificate, so make one. */
  148. uint32_t cert_flags = 0;
  149. if (flags & INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT)
  150. cert_flags |= CERT_FLAG_INCLUDE_SIGNING_KEY;
  151. cert = tor_cert_create(signing_key, cert_type,
  152. &keypair->pubkey,
  153. now, lifetime,
  154. cert_flags);
  155. if (! cert)
  156. goto err;
  157. /* Write it to disk. */
  158. created_cert = 1;
  159. if (crypto_write_tagged_contents_to_file(cert_fname, "ed25519v1-cert",
  160. tag, cert->encoded, cert->encoded_len) < 0) {
  161. tor_log(severity, LD_OR, "Couldn't write cert to disk.");
  162. goto err;
  163. }
  164. done:
  165. if (cert_out)
  166. *cert_out = cert;
  167. else
  168. tor_cert_free(cert);
  169. goto cleanup;
  170. err:
  171. memwipe(keypair, 0, sizeof(*keypair));
  172. tor_free(keypair);
  173. tor_cert_free(cert);
  174. if (cert_out)
  175. *cert_out = NULL;
  176. if (created_sk)
  177. unlink(secret_fname);
  178. if (created_pk)
  179. unlink(public_fname);
  180. if (created_cert)
  181. unlink(cert_fname);
  182. cleanup:
  183. tor_free(secret_fname);
  184. tor_free(public_fname);
  185. tor_free(cert_fname);
  186. return keypair;
  187. }
  188. /**
  189. * Create a new signing key and (optionally) certficiate; do not read or write
  190. * from disk. See ed_key_init_from_file() for more information.
  191. */
  192. ed25519_keypair_t *
  193. ed_key_new(const ed25519_keypair_t *signing_key,
  194. uint32_t flags,
  195. time_t now,
  196. time_t lifetime,
  197. uint8_t cert_type,
  198. struct tor_cert_st **cert_out)
  199. {
  200. if (cert_out)
  201. *cert_out = NULL;
  202. const int extra_strong = !! (flags & INIT_ED_KEY_EXTRA_STRONG);
  203. ed25519_keypair_t *keypair = tor_malloc_zero(sizeof(ed25519_keypair_t));
  204. if (ed25519_keypair_generate(keypair, extra_strong) < 0)
  205. goto err;
  206. if (! (flags & INIT_ED_KEY_NEEDCERT))
  207. return keypair;
  208. tor_assert(signing_key);
  209. tor_assert(cert_out);
  210. uint32_t cert_flags = 0;
  211. if (flags & INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT)
  212. cert_flags |= CERT_FLAG_INCLUDE_SIGNING_KEY;
  213. tor_cert_t *cert = tor_cert_create(signing_key, cert_type,
  214. &keypair->pubkey,
  215. now, lifetime,
  216. cert_flags);
  217. if (! cert)
  218. goto err;
  219. *cert_out = cert;
  220. return keypair;
  221. err:
  222. tor_free(keypair);
  223. return NULL;
  224. }
  225. static ed25519_keypair_t *master_identity_key = NULL;
  226. static ed25519_keypair_t *master_signing_key = NULL;
  227. static ed25519_keypair_t *current_link_key = NULL;
  228. static ed25519_keypair_t *current_auth_key = NULL;
  229. static tor_cert_t *signing_key_cert = NULL;
  230. static tor_cert_t *link_key_cert = NULL;
  231. static tor_cert_t *auth_key_cert = NULL;
  232. /**
  233. * Running as a server: load, reload, or refresh our ed25519 keys and
  234. * certificates, creating and saving new ones as needed.
  235. */
  236. int
  237. load_ed_keys(const or_options_t *options, time_t now)
  238. {
  239. ed25519_keypair_t *id = NULL;
  240. ed25519_keypair_t *sign = NULL;
  241. ed25519_keypair_t *link = NULL;
  242. ed25519_keypair_t *auth = NULL;
  243. const ed25519_keypair_t *use_signing = NULL;
  244. tor_cert_t *sign_cert = NULL;
  245. tor_cert_t *link_cert = NULL;
  246. tor_cert_t *auth_cert = NULL;
  247. #define FAIL(msg) do { \
  248. log_warn(LD_OR, (msg)); \
  249. goto err; \
  250. } while (0)
  251. #define SET_KEY(key, newval) do { \
  252. ed25519_keypair_free(key); \
  253. key = (newval); \
  254. } while (0)
  255. #define SET_CERT(cert, newval) do { \
  256. tor_cert_free(cert); \
  257. cert = (newval); \
  258. } while (0)
  259. #define EXPIRES_SOON(cert, interval) \
  260. (!(cert) || (cert)->valid_until < now + (interval))
  261. /* XXXX support encrypted identity keys fully */
  262. /* XXXX use options. */
  263. (void) options;
  264. id = ed_key_init_from_file(
  265. options_get_datadir_fname2(options, "keys", "ed25519_master_id"),
  266. (INIT_ED_KEY_CREATE|INIT_ED_KEY_SPLIT|
  267. INIT_ED_KEY_MISSING_SECRET_OK|
  268. INIT_ED_KEY_EXTRA_STRONG),
  269. LOG_WARN, NULL, 0, 0, 0, NULL);
  270. if (!id)
  271. FAIL("Missing identity key");
  272. if (!master_signing_key || EXPIRES_SOON(signing_key_cert, 86400/*???*/)) {
  273. uint32_t flags = (INIT_ED_KEY_CREATE|
  274. INIT_ED_KEY_EXTRA_STRONG|
  275. INIT_ED_KEY_NEEDCERT|
  276. INIT_ED_KEY_INCLUDE_SIGNING_KEY_IN_CERT);
  277. const ed25519_keypair_t *sign_with_id = id;
  278. if (master_signing_key) {
  279. flags |= INIT_ED_KEY_REPLACE; /* it's expired, so force-replace it. */
  280. }
  281. if (tor_mem_is_zero((char*)id->seckey.seckey, sizeof(id->seckey))) {
  282. sign_with_id = NULL;
  283. flags &= ~INIT_ED_KEY_CREATE;
  284. }
  285. sign = ed_key_init_from_file(
  286. options_get_datadir_fname2(options, "keys", "ed25519_signing"),
  287. flags, LOG_WARN,
  288. sign_with_id, now, 30*86400/*XXX option*/,
  289. CERT_TYPE_ID_SIGNING, &sign_cert);
  290. if (!sign)
  291. FAIL("Missing signing key");
  292. use_signing = sign;
  293. } else {
  294. use_signing = master_signing_key;
  295. }
  296. /* At this point we no longer need our secret identity key. So wipe
  297. * it, if we loaded it in the first place. */
  298. memwipe(id->seckey.seckey, 0, sizeof(id->seckey));
  299. if (!current_link_key || EXPIRES_SOON(link_key_cert, 7200/*???*/)) {
  300. link = ed_key_new(use_signing, INIT_ED_KEY_NEEDCERT,
  301. now, 2*86400/*XXX option??*/,
  302. CERT_TYPE_SIGNING_LINK, &link_cert);
  303. if (!link)
  304. FAIL("Can't create link key");
  305. }
  306. if (!current_auth_key || EXPIRES_SOON(auth_key_cert, 7200)/*???*/) {
  307. auth = ed_key_new(use_signing, INIT_ED_KEY_NEEDCERT,
  308. now, 2*86400/*XXX option??*/,
  309. CERT_TYPE_SIGNING_AUTH, &auth_cert);
  310. if (!auth)
  311. FAIL("Can't create auth key");
  312. }
  313. /* We've generated or loaded everything. Put them in memory. */
  314. if (! master_identity_key) {
  315. SET_KEY(master_identity_key, id);
  316. } else {
  317. tor_free(id);
  318. }
  319. if (sign) {
  320. SET_KEY(master_signing_key, sign);
  321. SET_CERT(signing_key_cert, sign_cert);
  322. }
  323. if (link) {
  324. SET_KEY(current_link_key, link);
  325. SET_CERT(link_key_cert, link_cert);
  326. }
  327. if (auth) {
  328. SET_KEY(current_auth_key, auth);
  329. SET_CERT(auth_key_cert, auth_cert);
  330. }
  331. return 0;
  332. err:
  333. ed25519_keypair_free(id);
  334. ed25519_keypair_free(sign);
  335. ed25519_keypair_free(link);
  336. ed25519_keypair_free(auth);
  337. tor_cert_free(sign_cert);
  338. tor_cert_free(link_cert);
  339. tor_cert_free(auth_cert);
  340. return -1;
  341. #undef FAIL
  342. #undef SET_KEY
  343. #undef SET_CERT
  344. #undef EXPIRES_SOON
  345. }
  346. const ed25519_public_key_t *
  347. get_master_identity_key(void)
  348. {
  349. if (!master_identity_key)
  350. return NULL;
  351. return &master_identity_key->pubkey;
  352. }
  353. const ed25519_keypair_t *
  354. get_master_signing_keypair(void)
  355. {
  356. return master_signing_key;
  357. }
  358. const struct tor_cert_st *
  359. get_master_signing_key_cert(void)
  360. {
  361. return signing_key_cert;
  362. }
  363. const ed25519_keypair_t *
  364. get_current_link_keypair(void)
  365. {
  366. return current_link_key;
  367. }
  368. const ed25519_keypair_t *
  369. get_current_auth_keypair(void)
  370. {
  371. return current_auth_key;
  372. }
  373. const tor_cert_t *
  374. get_current_link_key_cert(void)
  375. {
  376. return link_key_cert;
  377. }
  378. const tor_cert_t *
  379. get_current_auth_key_cert(void)
  380. {
  381. return auth_key_cert;
  382. }
  383. void
  384. routerkeys_free_all(void)
  385. {
  386. ed25519_keypair_free(master_identity_key);
  387. ed25519_keypair_free(master_signing_key);
  388. ed25519_keypair_free(current_link_key);
  389. ed25519_keypair_free(current_auth_key);
  390. tor_cert_free(signing_key_cert);
  391. tor_cert_free(link_key_cert);
  392. tor_cert_free(auth_key_cert);
  393. master_identity_key = master_signing_key = NULL;
  394. current_link_key = current_auth_key = NULL;
  395. signing_key_cert = link_key_cert = auth_key_cert = NULL;
  396. }