routerkeys.c 45 KB

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