hs_circuit.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_circuit.c
  5. **/
  6. #include "or.h"
  7. #include "circpathbias.h"
  8. #include "circuitbuild.h"
  9. #include "circuitlist.h"
  10. #include "circuituse.h"
  11. #include "config.h"
  12. #include "relay.h"
  13. #include "rephist.h"
  14. #include "router.h"
  15. #include "hs_cell.h"
  16. #include "hs_circuit.h"
  17. #include "hs_ident.h"
  18. #include "hs_ntor.h"
  19. #include "hs_service.h"
  20. /* Trunnel. */
  21. #include "hs/cell_common.h"
  22. #include "hs/cell_establish_intro.h"
  23. /* A circuit is about to become an e2e rendezvous circuit. Check
  24. * <b>circ_purpose</b> and ensure that it's properly set. Return true iff
  25. * circuit purpose is properly set, otherwise return false. */
  26. static int
  27. circuit_purpose_is_correct_for_rend(unsigned int circ_purpose,
  28. int is_service_side)
  29. {
  30. if (is_service_side) {
  31. if (circ_purpose != CIRCUIT_PURPOSE_S_CONNECT_REND) {
  32. log_warn(LD_BUG,
  33. "HS e2e circuit setup with wrong purpose (%d)", circ_purpose);
  34. return 0;
  35. }
  36. }
  37. if (!is_service_side) {
  38. if (circ_purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  39. circ_purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  40. log_warn(LD_BUG,
  41. "Client e2e circuit setup with wrong purpose (%d)", circ_purpose);
  42. return 0;
  43. }
  44. }
  45. return 1;
  46. }
  47. /* Create and return a crypt path for the final hop of a v3 prop224 rendezvous
  48. * circuit. Initialize the crypt path crypto using the output material from the
  49. * ntor key exchange at <b>ntor_key_seed</b>.
  50. *
  51. * If <b>is_service_side</b> is set, we are the hidden service and the final
  52. * hop of the rendezvous circuit is the client on the other side. */
  53. static crypt_path_t *
  54. create_rend_cpath(const uint8_t *ntor_key_seed, size_t seed_len,
  55. int is_service_side)
  56. {
  57. uint8_t keys[HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN];
  58. crypt_path_t *cpath = NULL;
  59. /* Do the key expansion */
  60. if (hs_ntor_circuit_key_expansion(ntor_key_seed, seed_len,
  61. keys, sizeof(keys)) < 0) {
  62. goto err;
  63. }
  64. /* Setup the cpath */
  65. cpath = tor_malloc_zero(sizeof(crypt_path_t));
  66. cpath->magic = CRYPT_PATH_MAGIC;
  67. if (circuit_init_cpath_crypto(cpath, (char*)keys, sizeof(keys),
  68. is_service_side, 1) < 0) {
  69. tor_free(cpath);
  70. goto err;
  71. }
  72. err:
  73. memwipe(keys, 0, sizeof(keys));
  74. return cpath;
  75. }
  76. /* We are a v2 legacy HS client: Create and return a crypt path for the hidden
  77. * service on the other side of the rendezvous circuit <b>circ</b>. Initialize
  78. * the crypt path crypto using the body of the RENDEZVOUS1 cell at
  79. * <b>rend_cell_body</b> (which must be at least DH_KEY_LEN+DIGEST_LEN bytes).
  80. */
  81. static crypt_path_t *
  82. create_rend_cpath_legacy(origin_circuit_t *circ, const uint8_t *rend_cell_body)
  83. {
  84. crypt_path_t *hop = NULL;
  85. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  86. /* first DH_KEY_LEN bytes are g^y from the service. Finish the dh
  87. * handshake...*/
  88. tor_assert(circ->build_state);
  89. tor_assert(circ->build_state->pending_final_cpath);
  90. hop = circ->build_state->pending_final_cpath;
  91. tor_assert(hop->rend_dh_handshake_state);
  92. if (crypto_dh_compute_secret(LOG_PROTOCOL_WARN, hop->rend_dh_handshake_state,
  93. (char*)rend_cell_body, DH_KEY_LEN,
  94. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  95. log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
  96. goto err;
  97. }
  98. /* ... and set up cpath. */
  99. if (circuit_init_cpath_crypto(hop,
  100. keys+DIGEST_LEN, sizeof(keys)-DIGEST_LEN,
  101. 0, 0) < 0)
  102. goto err;
  103. /* Check whether the digest is right... */
  104. if (tor_memneq(keys, rend_cell_body+DH_KEY_LEN, DIGEST_LEN)) {
  105. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  106. goto err;
  107. }
  108. /* clean up the crypto stuff we just made */
  109. crypto_dh_free(hop->rend_dh_handshake_state);
  110. hop->rend_dh_handshake_state = NULL;
  111. goto done;
  112. err:
  113. hop = NULL;
  114. done:
  115. memwipe(keys, 0, sizeof(keys));
  116. return hop;
  117. }
  118. /* Append the final <b>hop</b> to the cpath of the rend <b>circ</b>, and mark
  119. * <b>circ</b> ready for use to transfer HS relay cells. */
  120. static void
  121. finalize_rend_circuit(origin_circuit_t *circ, crypt_path_t *hop,
  122. int is_service_side)
  123. {
  124. tor_assert(circ);
  125. tor_assert(hop);
  126. /* Notify the circuit state machine that we are splicing this circuit */
  127. int new_circ_purpose = is_service_side ?
  128. CIRCUIT_PURPOSE_S_REND_JOINED : CIRCUIT_PURPOSE_C_REND_JOINED;
  129. circuit_change_purpose(TO_CIRCUIT(circ), new_circ_purpose);
  130. /* All is well. Extend the circuit. */
  131. hop->state = CPATH_STATE_OPEN;
  132. /* Set the windows to default. */
  133. hop->package_window = circuit_initial_package_window();
  134. hop->deliver_window = CIRCWINDOW_START;
  135. /* Now that this circuit has finished connecting to its destination,
  136. * make sure circuit_get_open_circ_or_launch is willing to return it
  137. * so we can actually use it. */
  138. circ->hs_circ_has_timed_out = 0;
  139. /* Append the hop to the cpath of this circuit */
  140. onion_append_to_cpath(&circ->cpath, hop);
  141. /* In legacy code, 'pending_final_cpath' points to the final hop we just
  142. * appended to the cpath. We set the original pointer to NULL so that we
  143. * don't double free it. */
  144. if (circ->build_state) {
  145. circ->build_state->pending_final_cpath = NULL;
  146. }
  147. /* Finally, mark circuit as ready to be used for client streams */
  148. if (!is_service_side) {
  149. circuit_try_attaching_streams(circ);
  150. }
  151. }
  152. /* For a given circuit and a service introduction point object, register the
  153. * intro circuit to the circuitmap. This supports legacy intro point. */
  154. static void
  155. register_intro_circ(const hs_service_intro_point_t *ip,
  156. origin_circuit_t *circ)
  157. {
  158. tor_assert(ip);
  159. tor_assert(circ);
  160. if (ip->base.is_only_legacy) {
  161. uint8_t digest[DIGEST_LEN];
  162. if (BUG(crypto_pk_get_digest(ip->legacy_key, (char *) digest) < 0)) {
  163. return;
  164. }
  165. hs_circuitmap_register_intro_circ_v2_service_side(circ, digest);
  166. } else {
  167. hs_circuitmap_register_intro_circ_v3_service_side(circ,
  168. &ip->auth_key_kp.pubkey);
  169. }
  170. }
  171. /* Return the number of opened introduction circuit for the given circuit that
  172. * is matching its identity key. */
  173. static unsigned int
  174. count_opened_desc_intro_point_circuits(const hs_service_t *service,
  175. const hs_service_descriptor_t *desc)
  176. {
  177. unsigned int count = 0;
  178. tor_assert(service);
  179. tor_assert(desc);
  180. DIGEST256MAP_FOREACH(desc->intro_points.map, key,
  181. const hs_service_intro_point_t *, ip) {
  182. circuit_t *circ;
  183. origin_circuit_t *ocirc;
  184. if (ip->base.is_only_legacy) {
  185. uint8_t digest[DIGEST_LEN];
  186. if (BUG(crypto_pk_get_digest(ip->legacy_key, (char *) digest) < 0)) {
  187. continue;
  188. }
  189. ocirc = hs_circuitmap_get_intro_circ_v2_service_side(digest);
  190. } else {
  191. ocirc =
  192. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  193. }
  194. if (ocirc == NULL) {
  195. continue;
  196. }
  197. circ = TO_CIRCUIT(ocirc);
  198. tor_assert(circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  199. circ->purpose == CIRCUIT_PURPOSE_S_INTRO);
  200. /* Having a circuit not for the requested service is really bad. */
  201. tor_assert(ed25519_pubkey_eq(&service->keys.identity_pk,
  202. &ocirc->hs_ident->identity_pk));
  203. /* Only count opened circuit and skip circuit that will be closed. */
  204. if (!circ->marked_for_close && circ->state == CIRCUIT_STATE_OPEN) {
  205. count++;
  206. }
  207. } DIGEST256MAP_FOREACH_END;
  208. return count;
  209. }
  210. /* From a given service and service intro point, create an introduction point
  211. * circuit identifier. This can't fail. */
  212. static hs_ident_circuit_t *
  213. create_intro_circuit_identifier(const hs_service_t *service,
  214. const hs_service_intro_point_t *ip)
  215. {
  216. hs_ident_circuit_t *ident;
  217. tor_assert(service);
  218. tor_assert(ip);
  219. ident = hs_ident_circuit_new(&service->keys.identity_pk,
  220. HS_IDENT_CIRCUIT_INTRO);
  221. ed25519_pubkey_copy(&ident->intro_auth_pk, &ip->auth_key_kp.pubkey);
  222. return ident;
  223. }
  224. /* For a given introduction point and an introduction circuit, send the
  225. * ESTABLISH_INTRO cell. The service object is used for logging. This can fail
  226. * and if so, the circuit is closed and the intro point object is flagged
  227. * that the circuit is not established anymore which is important for the
  228. * retry mechanism. */
  229. static void
  230. send_establish_intro(const hs_service_t *service,
  231. hs_service_intro_point_t *ip, origin_circuit_t *circ)
  232. {
  233. ssize_t cell_len;
  234. uint8_t payload[RELAY_PAYLOAD_SIZE];
  235. tor_assert(service);
  236. tor_assert(ip);
  237. tor_assert(circ);
  238. /* Encode establish intro cell. */
  239. cell_len = hs_cell_build_establish_intro(circ->cpath->prev->rend_circ_nonce,
  240. ip, payload);
  241. if (cell_len < 0) {
  242. log_warn(LD_REND, "Unable to encode ESTABLISH_INTRO cell for service %s "
  243. "on circuit %u. Closing circuit.",
  244. safe_str_client(service->onion_address),
  245. TO_CIRCUIT(circ)->n_circ_id);
  246. goto err;
  247. }
  248. /* Send the cell on the circuit. */
  249. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
  250. RELAY_COMMAND_ESTABLISH_INTRO,
  251. (char *) payload, cell_len,
  252. circ->cpath->prev) < 0) {
  253. log_info(LD_REND, "Unable to send ESTABLISH_INTRO cell for service %s "
  254. "on circuit %u.",
  255. safe_str_client(service->onion_address),
  256. TO_CIRCUIT(circ)->n_circ_id);
  257. /* On error, the circuit has been closed. */
  258. goto done;
  259. }
  260. /* Record the attempt to use this circuit. */
  261. pathbias_count_use_attempt(circ);
  262. goto done;
  263. err:
  264. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  265. done:
  266. memwipe(payload, 0, sizeof(payload));
  267. }
  268. /* ========== */
  269. /* Public API */
  270. /* ========== */
  271. /* For a given service and a service intro point, launch a circuit to the
  272. * extend info ei. If the service is a single onion, a one-hop circuit will be
  273. * requested. Return 0 if the circuit was successfully launched and tagged
  274. * with the correct identifier. On error, a negative value is returned. */
  275. int
  276. hs_circ_launch_intro_point(hs_service_t *service,
  277. const hs_service_intro_point_t *ip,
  278. extend_info_t *ei, time_t now)
  279. {
  280. /* Standard flags for introduction circuit. */
  281. int ret = -1, circ_flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  282. origin_circuit_t *circ;
  283. tor_assert(service);
  284. tor_assert(ip);
  285. tor_assert(ei);
  286. /* Update circuit flags in case of a single onion service that requires a
  287. * direct connection. */
  288. if (service->config.is_single_onion) {
  289. circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
  290. }
  291. log_info(LD_REND, "Launching a circuit to intro point %s for service %s.",
  292. safe_str_client(extend_info_describe(ei)),
  293. safe_str_client(service->onion_address));
  294. /* Note down that we are about to use an internal circuit. */
  295. rep_hist_note_used_internal(now, circ_flags & CIRCLAUNCH_NEED_UPTIME,
  296. circ_flags & CIRCLAUNCH_NEED_CAPACITY);
  297. /* Note down the launch for the retry period. Even if the circuit fails to
  298. * be launched, we still want to respect the retry period to avoid stress on
  299. * the circuit subsystem. */
  300. service->state.num_intro_circ_launched++;
  301. circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  302. ei, circ_flags);
  303. if (circ == NULL) {
  304. goto end;
  305. }
  306. /* Setup the circuit identifier and attach it to it. */
  307. circ->hs_ident = create_intro_circuit_identifier(service, ip);
  308. tor_assert(circ->hs_ident);
  309. /* Register circuit in the global circuitmap. */
  310. register_intro_circ(ip, circ);
  311. /* Success. */
  312. ret = 0;
  313. end:
  314. return ret;
  315. }
  316. /* Called when a service introduction point circuit is done building. Given
  317. * the service and intro point object, this function will send the
  318. * ESTABLISH_INTRO cell on the circuit. Return 0 on success. Return 1 if the
  319. * circuit has been repurposed to General because we already have too many
  320. * opened. */
  321. int
  322. hs_circ_service_intro_has_opened(hs_service_t *service,
  323. hs_service_intro_point_t *ip,
  324. const hs_service_descriptor_t *desc,
  325. origin_circuit_t *circ)
  326. {
  327. int ret = 0;
  328. unsigned int num_intro_circ, num_needed_circ;
  329. tor_assert(service);
  330. tor_assert(ip);
  331. tor_assert(desc);
  332. tor_assert(circ);
  333. num_intro_circ = count_opened_desc_intro_point_circuits(service, desc);
  334. num_needed_circ = service->config.num_intro_points;
  335. if (num_intro_circ > num_needed_circ) {
  336. /* There are too many opened valid intro circuit for what the service
  337. * needs so repurpose this one. */
  338. /* XXX: Legacy code checks options->ExcludeNodes and if not NULL it just
  339. * closes the circuit. I have NO idea why it does that so it hasn't been
  340. * added here. I can only assume in case our ExcludeNodes list changes but
  341. * in that case, all circuit are flagged unusable (config.c). --dgoulet */
  342. log_info(LD_CIRC | LD_REND, "Introduction circuit just opened but we "
  343. "have enough for service %s. Repurposing "
  344. "it to general and leaving internal.",
  345. safe_str_client(service->onion_address));
  346. tor_assert(circ->build_state->is_internal);
  347. /* Remove it from the circuitmap. */
  348. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  349. /* Cleaning up the hidden service identifier and repurpose. */
  350. hs_ident_circuit_free(circ->hs_ident);
  351. circ->hs_ident = NULL;
  352. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_GENERAL);
  353. /* Inform that this circuit just opened for this new purpose. */
  354. circuit_has_opened(circ);
  355. /* This return value indicate to the caller that the IP object should be
  356. * removed from the service because it's corresponding circuit has just
  357. * been repurposed. */
  358. ret = 1;
  359. goto done;
  360. }
  361. log_info(LD_REND, "Introduction circuit %u established for service %s.",
  362. TO_CIRCUIT(circ)->n_circ_id,
  363. safe_str_client(service->onion_address));
  364. circuit_log_path(LOG_INFO, LD_REND, circ);
  365. /* Time to send an ESTABLISH_INTRO cell on this circuit. On error, this call
  366. * makes sure the circuit gets closed. */
  367. send_establish_intro(service, ip, circ);
  368. done:
  369. return ret;
  370. }
  371. /* Circuit <b>circ</b> just finished the rend ntor key exchange. Use the key
  372. * exchange output material at <b>ntor_key_seed</b> and setup <b>circ</b> to
  373. * serve as a rendezvous end-to-end circuit between the client and the
  374. * service. If <b>is_service_side</b> is set, then we are the hidden service
  375. * and the other side is the client.
  376. *
  377. * Return 0 if the operation went well; in case of error return -1. */
  378. int
  379. hs_circuit_setup_e2e_rend_circ(origin_circuit_t *circ,
  380. const uint8_t *ntor_key_seed, size_t seed_len,
  381. int is_service_side)
  382. {
  383. if (BUG(!circuit_purpose_is_correct_for_rend(TO_CIRCUIT(circ)->purpose,
  384. is_service_side))) {
  385. return -1;
  386. }
  387. crypt_path_t *hop = create_rend_cpath(ntor_key_seed, seed_len,
  388. is_service_side);
  389. if (!hop) {
  390. log_warn(LD_REND, "Couldn't get v3 %s cpath!",
  391. is_service_side ? "service-side" : "client-side");
  392. return -1;
  393. }
  394. finalize_rend_circuit(circ, hop, is_service_side);
  395. return 0;
  396. }
  397. /* We are a v2 legacy HS client and we just received a RENDEZVOUS1 cell
  398. * <b>rend_cell_body</b> on <b>circ</b>. Finish up the DH key exchange and then
  399. * extend the crypt path of <b>circ</b> so that the hidden service is on the
  400. * other side. */
  401. int
  402. hs_circuit_setup_e2e_rend_circ_legacy_client(origin_circuit_t *circ,
  403. const uint8_t *rend_cell_body)
  404. {
  405. if (BUG(!circuit_purpose_is_correct_for_rend(
  406. TO_CIRCUIT(circ)->purpose, 0))) {
  407. return -1;
  408. }
  409. crypt_path_t *hop = create_rend_cpath_legacy(circ, rend_cell_body);
  410. if (!hop) {
  411. log_warn(LD_GENERAL, "Couldn't get v2 cpath.");
  412. return -1;
  413. }
  414. finalize_rend_circuit(circ, hop, 0);
  415. return 0;
  416. }