hs_circuit.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 0 if circuit
  16. * purpose is properly set, otherwise return -1. */
  17. static int
  18. rend_circuit_validate_purpose(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_warn(LD_GENERAL, "HS e2e circuit setup with wrong purpose(%d)",
  23. circ_purpose);
  24. return -1;
  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_warn(LD_GENERAL, "Client e2e circuit setup with wrong purpose(%d)",
  31. circ_purpose);
  32. return -1;
  33. }
  34. }
  35. return 0;
  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. get_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,
  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. get_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, keys+DIGEST_LEN, 0, 0)<0)
  86. goto err;
  87. /* Check whether the digest is right... */
  88. if (tor_memneq(keys, rend_cell_body+DH_KEY_LEN, DIGEST_LEN)) {
  89. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  90. goto err;
  91. }
  92. /* clean up the crypto stuff we just made */
  93. crypto_dh_free(hop->rend_dh_handshake_state);
  94. hop->rend_dh_handshake_state = NULL;
  95. goto done;
  96. err:
  97. hop = NULL;
  98. done:
  99. memwipe(keys, 0, sizeof(keys));
  100. return hop;
  101. }
  102. /* Append the final <b>hop</b> to the cpath of the rend <b>circ</b>, and mark
  103. * <b>circ</b> ready for use to transfer HS relay cells. */
  104. static void
  105. finalize_rend_circuit(origin_circuit_t *circ, crypt_path_t *hop,
  106. int is_service_side)
  107. {
  108. tor_assert(circ);
  109. tor_assert(hop);
  110. /* Notify the circuit state machine that we are splicing this circuit */
  111. int new_circ_purpose = is_service_side ?
  112. CIRCUIT_PURPOSE_S_REND_JOINED : CIRCUIT_PURPOSE_C_REND_JOINED;
  113. circuit_change_purpose(TO_CIRCUIT(circ), new_circ_purpose);
  114. /* All is well. Extend the circuit. */
  115. hop->state = CPATH_STATE_OPEN;
  116. /* Set the windows to default. These are the windows that the client thinks
  117. * the service has. */
  118. hop->package_window = circuit_initial_package_window();
  119. hop->deliver_window = CIRCWINDOW_START;
  120. /* Now that this circuit has finished connecting to its destination,
  121. * make sure circuit_get_open_circ_or_launch is willing to return it
  122. * so we can actually use it. */
  123. circ->hs_circ_has_timed_out = 0;
  124. /* Append the hop to the cpath of this circuit */
  125. onion_append_to_cpath(&circ->cpath, hop);
  126. /* In legacy code, 'pending_final_cpath' points to the final hop we just
  127. * appended to the cpath. We set the original pointer to NULL so that we
  128. * don't double free it. */
  129. if (circ->build_state) {
  130. circ->build_state->pending_final_cpath = NULL;
  131. }
  132. /* Finally, mark circuit as ready to be used for client streams */
  133. if (!is_service_side) {
  134. circuit_try_attaching_streams(circ);
  135. }
  136. }
  137. /* Circuit <b>circ</b> just finished the rend ntor key exchange. Use the key
  138. * exchange output material at <b>ntor_key_seed</b> and setup <b>circ</b> to
  139. * serve as a rendezvous end-to-end circuit between the client and the
  140. * service. If <b>is_service_side</b> is set, then we are the hidden service
  141. * and the other side is the client.
  142. *
  143. * Return 0 if the operation went well; in case of error return -1. */
  144. int
  145. hs_circuit_setup_e2e_rend_circ(origin_circuit_t *circ,
  146. const uint8_t *ntor_key_seed,
  147. int is_service_side)
  148. {
  149. if (BUG(rend_circuit_validate_purpose(TO_CIRCUIT(circ)->purpose,
  150. is_service_side)) < 0) {
  151. return -1;
  152. }
  153. crypt_path_t *hop = get_rend_cpath(ntor_key_seed, is_service_side);
  154. if (!hop) {
  155. log_warn(LD_REND, "Couldn't get v3 %s cpath!",
  156. is_service_side ? "service-side" : "client-side");
  157. return -1;
  158. }
  159. finalize_rend_circuit(circ, hop, is_service_side);
  160. return 0;
  161. }
  162. /* We are a v2 legacy HS client and we just received a RENDEZVOUS1 cell
  163. * <b>rend_cell_body</b> on <b>circ</b>. Finish up the DH key exchange and then
  164. * extend the crypt path of <b>circ</b> so that the hidden service is on the
  165. * other side. */
  166. int
  167. hs_circuit_setup_e2e_rend_circ_legacy_client(origin_circuit_t *circ,
  168. const uint8_t *rend_cell_body)
  169. {
  170. if (BUG(rend_circuit_validate_purpose(TO_CIRCUIT(circ)->purpose, 0)) < 0) {
  171. return -1;
  172. }
  173. crypt_path_t *hop = get_rend_cpath_legacy(circ, rend_cell_body);
  174. if (!hop) {
  175. log_warn(LD_GENERAL, "Couldn't get v2 cpath.");
  176. return -1;
  177. }
  178. finalize_rend_circuit(circ, hop, 0);
  179. return 0;
  180. }