routerkeys.c 45 KB

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