hs_circuit.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "hs_circuit.h"
  12. #include "hs_ident.h"
  13. #include "hs_ntor.h"
  14. /* A circuit is about to become an e2e rendezvous circuit. Check
  15. * <b>circ_purpose</b> and ensure that it's properly set. Return true iff
  16. * circuit purpose is properly set, otherwise return false. */
  17. static int
  18. circuit_purpose_is_correct_for_rend(unsigned int circ_purpose, int is_service_side)
  19. {
  20. if (is_service_side) {
  21. if (circ_purpose != CIRCUIT_PURPOSE_S_CONNECT_REND) {
  22. log_fn(LOG_PROTOCOL_WARN, LD_GENERAL,
  23. "HS e2e circuit setup with wrong purpose(%d)", circ_purpose);
  24. return 0;
  25. }
  26. }
  27. if (!is_service_side) {
  28. if (circ_purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  29. circ_purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  30. log_fn(LOG_PROTOCOL_WARN, LD_GENERAL,
  31. "Client e2e circuit setup with wrong purpose(%d)", circ_purpose);
  32. return 0;
  33. }
  34. }
  35. return 1;
  36. }
  37. /* Create and return a crypt path for the final hop of a v3 prop224 rendezvous
  38. * circuit. Initialize the crypt path crypto using the output material from the
  39. * ntor key exchange at <b>ntor_key_seed</b>.
  40. *
  41. * If <b>is_service_side</b> is set, we are the hidden service and the final
  42. * hop of the rendezvous circuit is the client on the other side. */
  43. static crypt_path_t *
  44. create_rend_cpath(const uint8_t *ntor_key_seed, size_t seed_len,
  45. int is_service_side)
  46. {
  47. uint8_t keys[HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN];
  48. crypt_path_t *cpath = NULL;
  49. /* Do the key expansion */
  50. if (hs_ntor_circuit_key_expansion(ntor_key_seed, seed_len,
  51. keys, sizeof(keys)) < 0) {
  52. goto err;
  53. }
  54. /* Setup the cpath */
  55. cpath = tor_malloc_zero(sizeof(crypt_path_t));
  56. cpath->magic = CRYPT_PATH_MAGIC;
  57. if (circuit_init_cpath_crypto(cpath, (char*)keys, sizeof(keys),
  58. is_service_side, 1) < 0) {
  59. tor_free(cpath);
  60. goto err;
  61. }
  62. err:
  63. memwipe(keys, 0, sizeof(keys));
  64. return cpath;
  65. }
  66. /* We are a v2 legacy HS client: Create and return a crypt path for the hidden
  67. * service on the other side of the rendezvous circuit <b>circ</b>. Initialize
  68. * the crypt path crypto using the body of the RENDEZVOUS1 cell at
  69. * <b>rend_cell_body</b> (which must be at least DH_KEY_LEN+DIGEST_LEN bytes).
  70. */
  71. static crypt_path_t *
  72. create_rend_cpath_legacy(origin_circuit_t *circ, const uint8_t *rend_cell_body)
  73. {
  74. crypt_path_t *hop = NULL;
  75. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  76. /* first DH_KEY_LEN bytes are g^y from the service. Finish the dh
  77. * handshake...*/
  78. tor_assert(circ->build_state);
  79. tor_assert(circ->build_state->pending_final_cpath);
  80. hop = circ->build_state->pending_final_cpath;
  81. tor_assert(hop->rend_dh_handshake_state);
  82. if (crypto_dh_compute_secret(LOG_PROTOCOL_WARN, hop->rend_dh_handshake_state,
  83. (char*)rend_cell_body, DH_KEY_LEN,
  84. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  85. log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
  86. goto err;
  87. }
  88. /* ... and set up cpath. */
  89. if (circuit_init_cpath_crypto(hop,
  90. keys+DIGEST_LEN, sizeof(keys)-DIGEST_LEN,
  91. 0, 0) < 0)
  92. goto err;
  93. /* Check whether the digest is right... */
  94. if (tor_memneq(keys, rend_cell_body+DH_KEY_LEN, DIGEST_LEN)) {
  95. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  96. goto err;
  97. }
  98. /* clean up the crypto stuff we just made */
  99. crypto_dh_free(hop->rend_dh_handshake_state);
  100. hop->rend_dh_handshake_state = NULL;
  101. goto done;
  102. err:
  103. hop = NULL;
  104. done:
  105. memwipe(keys, 0, sizeof(keys));
  106. return hop;
  107. }
  108. /* Append the final <b>hop</b> to the cpath of the rend <b>circ</b>, and mark
  109. * <b>circ</b> ready for use to transfer HS relay cells. */
  110. static void
  111. finalize_rend_circuit(origin_circuit_t *circ, crypt_path_t *hop,
  112. int is_service_side)
  113. {
  114. tor_assert(circ);
  115. tor_assert(hop);
  116. /* Notify the circuit state machine that we are splicing this circuit */
  117. int new_circ_purpose = is_service_side ?
  118. CIRCUIT_PURPOSE_S_REND_JOINED : CIRCUIT_PURPOSE_C_REND_JOINED;
  119. circuit_change_purpose(TO_CIRCUIT(circ), new_circ_purpose);
  120. /* All is well. Extend the circuit. */
  121. hop->state = CPATH_STATE_OPEN;
  122. /* Set the windows to default. */
  123. hop->package_window = circuit_initial_package_window();
  124. hop->deliver_window = CIRCWINDOW_START;
  125. /* Now that this circuit has finished connecting to its destination,
  126. * make sure circuit_get_open_circ_or_launch is willing to return it
  127. * so we can actually use it. */
  128. circ->hs_circ_has_timed_out = 0;
  129. /* Append the hop to the cpath of this circuit */
  130. onion_append_to_cpath(&circ->cpath, hop);
  131. /* In legacy code, 'pending_final_cpath' points to the final hop we just
  132. * appended to the cpath. We set the original pointer to NULL so that we
  133. * don't double free it. */
  134. if (circ->build_state) {
  135. circ->build_state->pending_final_cpath = NULL;
  136. }
  137. /* Finally, mark circuit as ready to be used for client streams */
  138. if (!is_service_side) {
  139. circuit_try_attaching_streams(circ);
  140. }
  141. }
  142. /* Circuit <b>circ</b> just finished the rend ntor key exchange. Use the key
  143. * exchange output material at <b>ntor_key_seed</b> and setup <b>circ</b> to
  144. * serve as a rendezvous end-to-end circuit between the client and the
  145. * service. If <b>is_service_side</b> is set, then we are the hidden service
  146. * and the other side is the client.
  147. *
  148. * Return 0 if the operation went well; in case of error return -1. */
  149. int
  150. hs_circuit_setup_e2e_rend_circ(origin_circuit_t *circ,
  151. const uint8_t *ntor_key_seed, size_t seed_len,
  152. int is_service_side)
  153. {
  154. if (BUG(!circuit_purpose_is_correct_for_rend(TO_CIRCUIT(circ)->purpose,
  155. is_service_side))) {
  156. return -1;
  157. }
  158. crypt_path_t *hop = create_rend_cpath(ntor_key_seed, seed_len,
  159. is_service_side);
  160. if (!hop) {
  161. log_warn(LD_REND, "Couldn't get v3 %s cpath!",
  162. is_service_side ? "service-side" : "client-side");
  163. return -1;
  164. }
  165. finalize_rend_circuit(circ, hop, is_service_side);
  166. return 0;
  167. }
  168. /* We are a v2 legacy HS client and we just received a RENDEZVOUS1 cell
  169. * <b>rend_cell_body</b> on <b>circ</b>. Finish up the DH key exchange and then
  170. * extend the crypt path of <b>circ</b> so that the hidden service is on the
  171. * other side. */
  172. int
  173. hs_circuit_setup_e2e_rend_circ_legacy_client(origin_circuit_t *circ,
  174. const uint8_t *rend_cell_body)
  175. {
  176. if (BUG(!circuit_purpose_is_correct_for_rend(
  177. TO_CIRCUIT(circ)->purpose, 0))) {
  178. return -1;
  179. }
  180. crypt_path_t *hop = create_rend_cpath_legacy(circ, rend_cell_body);
  181. if (!hop) {
  182. log_warn(LD_GENERAL, "Couldn't get v2 cpath.");
  183. return -1;
  184. }
  185. finalize_rend_circuit(circ, hop, 0);
  186. return 0;
  187. }