hs_circuit.c 7.0 KB

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