hs_circuit.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "circuitbuild.h"
  8. #include "circuitlist.h"
  9. #include "circuituse.h"
  10. #include "config.h"
  11. #include "rephist.h"
  12. #include "router.h"
  13. #include "hs_circuit.h"
  14. #include "hs_ident.h"
  15. #include "hs_ntor.h"
  16. #include "hs_service.h"
  17. /* Trunnel. */
  18. #include "hs/cell_common.h"
  19. #include "hs/cell_establish_intro.h"
  20. /* A circuit is about to become an e2e rendezvous circuit. Check
  21. * <b>circ_purpose</b> and ensure that it's properly set. Return true iff
  22. * circuit purpose is properly set, otherwise return false. */
  23. static int
  24. circuit_purpose_is_correct_for_rend(unsigned int circ_purpose,
  25. int is_service_side)
  26. {
  27. if (is_service_side) {
  28. if (circ_purpose != CIRCUIT_PURPOSE_S_CONNECT_REND) {
  29. log_warn(LD_BUG,
  30. "HS e2e circuit setup with wrong purpose (%d)", circ_purpose);
  31. return 0;
  32. }
  33. }
  34. if (!is_service_side) {
  35. if (circ_purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  36. circ_purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  37. log_warn(LD_BUG,
  38. "Client e2e circuit setup with wrong purpose (%d)", circ_purpose);
  39. return 0;
  40. }
  41. }
  42. return 1;
  43. }
  44. /* Create and return a crypt path for the final hop of a v3 prop224 rendezvous
  45. * circuit. Initialize the crypt path crypto using the output material from the
  46. * ntor key exchange at <b>ntor_key_seed</b>.
  47. *
  48. * If <b>is_service_side</b> is set, we are the hidden service and the final
  49. * hop of the rendezvous circuit is the client on the other side. */
  50. static crypt_path_t *
  51. create_rend_cpath(const uint8_t *ntor_key_seed, size_t seed_len,
  52. int is_service_side)
  53. {
  54. uint8_t keys[HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN];
  55. crypt_path_t *cpath = NULL;
  56. /* Do the key expansion */
  57. if (hs_ntor_circuit_key_expansion(ntor_key_seed, seed_len,
  58. keys, sizeof(keys)) < 0) {
  59. goto err;
  60. }
  61. /* Setup the cpath */
  62. cpath = tor_malloc_zero(sizeof(crypt_path_t));
  63. cpath->magic = CRYPT_PATH_MAGIC;
  64. if (circuit_init_cpath_crypto(cpath, (char*)keys, sizeof(keys),
  65. is_service_side, 1) < 0) {
  66. tor_free(cpath);
  67. goto err;
  68. }
  69. err:
  70. memwipe(keys, 0, sizeof(keys));
  71. return cpath;
  72. }
  73. /* We are a v2 legacy HS client: Create and return a crypt path for the hidden
  74. * service on the other side of the rendezvous circuit <b>circ</b>. Initialize
  75. * the crypt path crypto using the body of the RENDEZVOUS1 cell at
  76. * <b>rend_cell_body</b> (which must be at least DH_KEY_LEN+DIGEST_LEN bytes).
  77. */
  78. static crypt_path_t *
  79. create_rend_cpath_legacy(origin_circuit_t *circ, const uint8_t *rend_cell_body)
  80. {
  81. crypt_path_t *hop = NULL;
  82. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  83. /* first DH_KEY_LEN bytes are g^y from the service. Finish the dh
  84. * handshake...*/
  85. tor_assert(circ->build_state);
  86. tor_assert(circ->build_state->pending_final_cpath);
  87. hop = circ->build_state->pending_final_cpath;
  88. tor_assert(hop->rend_dh_handshake_state);
  89. if (crypto_dh_compute_secret(LOG_PROTOCOL_WARN, hop->rend_dh_handshake_state,
  90. (char*)rend_cell_body, DH_KEY_LEN,
  91. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  92. log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
  93. goto err;
  94. }
  95. /* ... and set up cpath. */
  96. if (circuit_init_cpath_crypto(hop,
  97. keys+DIGEST_LEN, sizeof(keys)-DIGEST_LEN,
  98. 0, 0) < 0)
  99. goto err;
  100. /* Check whether the digest is right... */
  101. if (tor_memneq(keys, rend_cell_body+DH_KEY_LEN, DIGEST_LEN)) {
  102. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  103. goto err;
  104. }
  105. /* clean up the crypto stuff we just made */
  106. crypto_dh_free(hop->rend_dh_handshake_state);
  107. hop->rend_dh_handshake_state = NULL;
  108. goto done;
  109. err:
  110. hop = NULL;
  111. done:
  112. memwipe(keys, 0, sizeof(keys));
  113. return hop;
  114. }
  115. /* Append the final <b>hop</b> to the cpath of the rend <b>circ</b>, and mark
  116. * <b>circ</b> ready for use to transfer HS relay cells. */
  117. static void
  118. finalize_rend_circuit(origin_circuit_t *circ, crypt_path_t *hop,
  119. int is_service_side)
  120. {
  121. tor_assert(circ);
  122. tor_assert(hop);
  123. /* Notify the circuit state machine that we are splicing this circuit */
  124. int new_circ_purpose = is_service_side ?
  125. CIRCUIT_PURPOSE_S_REND_JOINED : CIRCUIT_PURPOSE_C_REND_JOINED;
  126. circuit_change_purpose(TO_CIRCUIT(circ), new_circ_purpose);
  127. /* All is well. Extend the circuit. */
  128. hop->state = CPATH_STATE_OPEN;
  129. /* Set the windows to default. */
  130. hop->package_window = circuit_initial_package_window();
  131. hop->deliver_window = CIRCWINDOW_START;
  132. /* Now that this circuit has finished connecting to its destination,
  133. * make sure circuit_get_open_circ_or_launch is willing to return it
  134. * so we can actually use it. */
  135. circ->hs_circ_has_timed_out = 0;
  136. /* Append the hop to the cpath of this circuit */
  137. onion_append_to_cpath(&circ->cpath, hop);
  138. /* In legacy code, 'pending_final_cpath' points to the final hop we just
  139. * appended to the cpath. We set the original pointer to NULL so that we
  140. * don't double free it. */
  141. if (circ->build_state) {
  142. circ->build_state->pending_final_cpath = NULL;
  143. }
  144. /* Finally, mark circuit as ready to be used for client streams */
  145. if (!is_service_side) {
  146. circuit_try_attaching_streams(circ);
  147. }
  148. }
  149. /* For a given circuit and a service introduction point object, register the
  150. * intro circuit to the circuitmap. This supports legacy intro point. */
  151. static void
  152. register_intro_circ(const hs_service_intro_point_t *ip,
  153. origin_circuit_t *circ)
  154. {
  155. tor_assert(ip);
  156. tor_assert(circ);
  157. if (ip->base.is_only_legacy) {
  158. uint8_t digest[DIGEST_LEN];
  159. if (BUG(crypto_pk_get_digest(ip->legacy_key, (char *) digest) < 0)) {
  160. return;
  161. }
  162. hs_circuitmap_register_intro_circ_v2_service_side(circ, digest);
  163. } else {
  164. hs_circuitmap_register_intro_circ_v3_service_side(circ,
  165. &ip->auth_key_kp.pubkey);
  166. }
  167. }
  168. /* From a given service and service intro point, create an introduction point
  169. * circuit identifier. This can't fail. */
  170. static hs_ident_circuit_t *
  171. create_intro_circuit_identifier(const hs_service_t *service,
  172. const hs_service_intro_point_t *ip)
  173. {
  174. hs_ident_circuit_t *ident;
  175. tor_assert(service);
  176. tor_assert(ip);
  177. ident = hs_ident_circuit_new(&service->keys.identity_pk,
  178. HS_IDENT_CIRCUIT_INTRO);
  179. ed25519_pubkey_copy(&ident->intro_auth_pk, &ip->auth_key_kp.pubkey);
  180. return ident;
  181. }
  182. /* For a given service and a service intro point, launch a circuit to the
  183. * extend info ei. If the service is a single onion, a one-hop circuit will be
  184. * requested. Return 0 if the circuit was successfully launched and tagged
  185. * with the correct identifier. On error, a negative value is returned. */
  186. int
  187. hs_circ_launch_intro_point(hs_service_t *service,
  188. const hs_service_intro_point_t *ip,
  189. extend_info_t *ei, time_t now)
  190. {
  191. /* Standard flags for introduction circuit. */
  192. int ret = -1, circ_flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  193. origin_circuit_t *circ;
  194. tor_assert(service);
  195. tor_assert(ip);
  196. tor_assert(ei);
  197. /* Update circuit flags in case of a single onion service that requires a
  198. * direct connection. */
  199. if (service->config.is_single_onion) {
  200. circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
  201. }
  202. log_info(LD_REND, "Launching a circuit to intro point %s for service %s.",
  203. safe_str_client(extend_info_describe(ei)),
  204. safe_str_client(service->onion_address));
  205. /* Note down that we are about to use an internal circuit. */
  206. rep_hist_note_used_internal(now, circ_flags & CIRCLAUNCH_NEED_UPTIME,
  207. circ_flags & CIRCLAUNCH_NEED_CAPACITY);
  208. /* Note down the launch for the retry period. Even if the circuit fails to
  209. * be launched, we still want to respect the retry period to avoid stress on
  210. * the circuit subsystem. */
  211. service->state.num_intro_circ_launched++;
  212. circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  213. ei, circ_flags);
  214. if (circ == NULL) {
  215. goto end;
  216. }
  217. /* Setup the circuit identifier and attach it to it. */
  218. circ->hs_ident = create_intro_circuit_identifier(service, ip);
  219. tor_assert(circ->hs_ident);
  220. /* Register circuit in the global circuitmap. */
  221. register_intro_circ(ip, circ);
  222. /* Success. */
  223. ret = 0;
  224. end:
  225. return ret;
  226. }
  227. /* Circuit <b>circ</b> just finished the rend ntor key exchange. Use the key
  228. * exchange output material at <b>ntor_key_seed</b> and setup <b>circ</b> to
  229. * serve as a rendezvous end-to-end circuit between the client and the
  230. * service. If <b>is_service_side</b> is set, then we are the hidden service
  231. * and the other side is the client.
  232. *
  233. * Return 0 if the operation went well; in case of error return -1. */
  234. int
  235. hs_circuit_setup_e2e_rend_circ(origin_circuit_t *circ,
  236. const uint8_t *ntor_key_seed, size_t seed_len,
  237. int is_service_side)
  238. {
  239. if (BUG(!circuit_purpose_is_correct_for_rend(TO_CIRCUIT(circ)->purpose,
  240. is_service_side))) {
  241. return -1;
  242. }
  243. crypt_path_t *hop = create_rend_cpath(ntor_key_seed, seed_len,
  244. is_service_side);
  245. if (!hop) {
  246. log_warn(LD_REND, "Couldn't get v3 %s cpath!",
  247. is_service_side ? "service-side" : "client-side");
  248. return -1;
  249. }
  250. finalize_rend_circuit(circ, hop, is_service_side);
  251. return 0;
  252. }
  253. /* We are a v2 legacy HS client and we just received a RENDEZVOUS1 cell
  254. * <b>rend_cell_body</b> on <b>circ</b>. Finish up the DH key exchange and then
  255. * extend the crypt path of <b>circ</b> so that the hidden service is on the
  256. * other side. */
  257. int
  258. hs_circuit_setup_e2e_rend_circ_legacy_client(origin_circuit_t *circ,
  259. const uint8_t *rend_cell_body)
  260. {
  261. if (BUG(!circuit_purpose_is_correct_for_rend(
  262. TO_CIRCUIT(circ)->purpose, 0))) {
  263. return -1;
  264. }
  265. crypt_path_t *hop = create_rend_cpath_legacy(circ, rend_cell_body);
  266. if (!hop) {
  267. log_warn(LD_GENERAL, "Couldn't get v2 cpath.");
  268. return -1;
  269. }
  270. finalize_rend_circuit(circ, hop, 0);
  271. return 0;
  272. }