hs_circuit.c 6.9 KB

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