hs_intropoint.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Copyright (c) 2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_intropoint.c
  5. * \brief Implement next generation introductions point functionality
  6. **/
  7. #define HS_INTROPOINT_PRIVATE
  8. #include "or.h"
  9. #include "circuitlist.h"
  10. #include "circuituse.h"
  11. #include "relay.h"
  12. #include "rendmid.h"
  13. #include "rephist.h"
  14. #include "hs/cell_establish_intro.h"
  15. #include "hs/cell_common.h"
  16. #include "hs_circuitmap.h"
  17. #include "hs_intropoint.h"
  18. #include "hs_common.h"
  19. /** Extract the authentication key from an ESTABLISH_INTRO <b>cell</b> and
  20. * place it in <b>auth_key_out</b>. */
  21. STATIC void
  22. get_auth_key_from_establish_intro_cell(ed25519_public_key_t *auth_key_out,
  23. const hs_cell_establish_intro_t *cell)
  24. {
  25. tor_assert(auth_key_out);
  26. const uint8_t *key_array =
  27. hs_cell_establish_intro_getconstarray_auth_key(cell);
  28. tor_assert(key_array);
  29. tor_assert(hs_cell_establish_intro_getlen_auth_key(cell) ==
  30. sizeof(auth_key_out->pubkey));
  31. memcpy(auth_key_out->pubkey, key_array, cell->auth_key_len);
  32. }
  33. /** We received an ESTABLISH_INTRO <b>cell</b>. Verify its signature and MAC,
  34. * given <b>circuit_key_material</b>. Return 0 on success else -1 on error. */
  35. STATIC int
  36. verify_establish_intro_cell(const hs_cell_establish_intro_t *cell,
  37. const uint8_t *circuit_key_material,
  38. size_t circuit_key_material_len)
  39. {
  40. /* We only reach this function if the first byte of the cell is 0x02 which
  41. * means that auth_key_type is AUTH_KEY_ED25519, hence this check should
  42. * always pass. See hs_intro_received_establish_intro(). */
  43. if (BUG(cell->auth_key_type != AUTH_KEY_ED25519)) {
  44. return -1;
  45. }
  46. /* Make sure the auth key length is of the right size for this type. For
  47. * EXTRA safety, we check both the size of the array and the length which
  48. * must be the same. Safety first!*/
  49. if (hs_cell_establish_intro_getlen_auth_key(cell) != ED25519_PUBKEY_LEN ||
  50. hs_cell_establish_intro_get_auth_key_len(cell) != ED25519_PUBKEY_LEN) {
  51. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  52. "ESTABLISH_INTRO auth key length is invalid");
  53. return -1;
  54. }
  55. const uint8_t *msg = cell->start_cell;
  56. /* Verify the sig */
  57. {
  58. ed25519_signature_t sig_struct;
  59. const uint8_t *sig_array = hs_cell_establish_intro_getconstarray_sig(cell);
  60. if (hs_cell_establish_intro_getlen_sig(cell) != sizeof(sig_struct.sig)) {
  61. log_warn(LD_PROTOCOL, "ESTABLISH_INTRO sig len is invalid");
  62. return -1;
  63. }
  64. /* We are now sure that sig_len is of the right size. */
  65. memcpy(sig_struct.sig, sig_array, cell->sig_len);
  66. ed25519_public_key_t auth_key;
  67. get_auth_key_from_establish_intro_cell(&auth_key, cell);
  68. const size_t sig_msg_len = cell->end_sig_fields - msg;
  69. int sig_mismatch = ed25519_checksig_prefixed(&sig_struct,
  70. (uint8_t*) msg, sig_msg_len,
  71. ESTABLISH_INTRO_SIG_PREFIX,
  72. &auth_key);
  73. if (sig_mismatch) {
  74. log_warn(LD_PROTOCOL, "ESTABLISH_INTRO signature not as expected");
  75. return -1;
  76. }
  77. }
  78. /* Verify the MAC */
  79. {
  80. const size_t auth_msg_len = cell->end_mac_fields - msg;
  81. uint8_t mac[DIGEST256_LEN];
  82. crypto_mac_sha3_256(mac, sizeof(mac),
  83. circuit_key_material, circuit_key_material_len,
  84. msg, auth_msg_len);
  85. if (tor_memneq(mac, cell->handshake_mac, sizeof(mac))) {
  86. log_warn(LD_PROTOCOL, "ESTABLISH_INTRO handshake_auth not as expected");
  87. return -1;
  88. }
  89. }
  90. return 0;
  91. }
  92. /* Send an INTRO_ESTABLISHED cell to <b>circ</b>. */
  93. MOCK_IMPL(int,
  94. hs_intro_send_intro_established_cell,(or_circuit_t *circ))
  95. {
  96. int ret;
  97. uint8_t *encoded_cell = NULL;
  98. ssize_t encoded_len, result_len;
  99. hs_cell_intro_established_t *cell;
  100. cell_extension_t *ext;
  101. tor_assert(circ);
  102. /* Build the cell payload. */
  103. cell = hs_cell_intro_established_new();
  104. ext = cell_extension_new();
  105. cell_extension_set_num(ext, 0);
  106. hs_cell_intro_established_set_extensions(cell, ext);
  107. /* Encode the cell to binary format. */
  108. encoded_len = hs_cell_intro_established_encoded_len(cell);
  109. tor_assert(encoded_len > 0);
  110. encoded_cell = tor_malloc_zero(encoded_len);
  111. result_len = hs_cell_intro_established_encode(encoded_cell, encoded_len,
  112. cell);
  113. tor_assert(encoded_len == result_len);
  114. ret = relay_send_command_from_edge(0, TO_CIRCUIT(circ),
  115. RELAY_COMMAND_INTRO_ESTABLISHED,
  116. (char *) encoded_cell, encoded_len,
  117. NULL);
  118. /* On failure, the above function will close the circuit. */
  119. hs_cell_intro_established_free(cell);
  120. tor_free(encoded_cell);
  121. return ret;
  122. }
  123. /** We received an ESTABLISH_INTRO <b>parsed_cell</b> on <b>circ</b>. It's
  124. * well-formed and passed our verifications. Perform appropriate actions to
  125. * establish an intro point. */
  126. static int
  127. handle_verified_establish_intro_cell(or_circuit_t *circ,
  128. const hs_cell_establish_intro_t *parsed_cell)
  129. {
  130. /* Get the auth key of this intro point */
  131. ed25519_public_key_t auth_key;
  132. get_auth_key_from_establish_intro_cell(&auth_key, parsed_cell);
  133. /* Then notify the hidden service that the intro point is established by
  134. sending an INTRO_ESTABLISHED cell */
  135. if (hs_intro_send_intro_established_cell(circ)) {
  136. log_warn(LD_BUG, "Couldn't send INTRO_ESTABLISHED cell.");
  137. return -1;
  138. }
  139. /* Associate intro point auth key with this circuit. */
  140. hs_circuitmap_register_intro_circ_v3(circ, &auth_key);
  141. /* Repurpose this circuit into an intro circuit. */
  142. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
  143. return 0;
  144. }
  145. /** We just received an ESTABLISH_INTRO cell in <b>circ</b> with payload in
  146. * <b>request</b>. Handle it by making <b>circ</b> an intro circuit. Return 0
  147. * if everything went well, or -1 if there were errors. */
  148. static int
  149. handle_establish_intro(or_circuit_t *circ, const uint8_t *request,
  150. size_t request_len)
  151. {
  152. int cell_ok, retval = -1;
  153. hs_cell_establish_intro_t *parsed_cell = NULL;
  154. tor_assert(circ);
  155. tor_assert(request);
  156. log_info(LD_REND, "Received an ESTABLISH_INTRO request on circuit %" PRIu32,
  157. circ->p_circ_id);
  158. /* Check that the circuit is in shape to become an intro point */
  159. if (!hs_intro_circuit_is_suitable(circ)) {
  160. goto err;
  161. }
  162. /* Parse the cell */
  163. ssize_t parsing_result = hs_cell_establish_intro_parse(&parsed_cell,
  164. request, request_len);
  165. if (parsing_result < 0) {
  166. log_warn(LD_PROTOCOL, "Rejecting %s ESTABLISH_INTRO cell.",
  167. parsing_result == -1 ? "invalid" : "truncated");
  168. goto err;
  169. }
  170. cell_ok = verify_establish_intro_cell(parsed_cell,
  171. (uint8_t *) circ->rend_circ_nonce,
  172. sizeof(circ->rend_circ_nonce));
  173. if (cell_ok < 0) {
  174. log_warn(LD_PROTOCOL, "Failed to verify ESTABLISH_INTRO cell.");
  175. goto err;
  176. }
  177. /* This cell is legit. Take the appropriate actions. */
  178. cell_ok = handle_verified_establish_intro_cell(circ, parsed_cell);
  179. if (cell_ok < 0) {
  180. goto err;
  181. }
  182. log_warn(LD_GENERAL, "Established prop224 intro point on circuit %" PRIu32,
  183. circ->p_circ_id);
  184. /* We are done! */
  185. retval = 0;
  186. goto done;
  187. err:
  188. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  189. done:
  190. hs_cell_establish_intro_free(parsed_cell);
  191. return retval;
  192. }
  193. /* Return True if circuit is suitable for becoming an intro circuit. */
  194. int
  195. hs_intro_circuit_is_suitable(const or_circuit_t *circ)
  196. {
  197. /* Basic circuit state sanity checks. */
  198. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
  199. log_warn(LD_PROTOCOL, "Rejecting ESTABLISH_INTRO on non-OR circuit.");
  200. return 0;
  201. }
  202. if (circ->base_.n_chan) {
  203. log_warn(LD_PROTOCOL, "Rejecting ESTABLISH_INTRO on non-edge circuit.");
  204. return 0;
  205. }
  206. return 1;
  207. }
  208. /* We just received an ESTABLISH_INTRO cell in <b>circ</b>. Figure out of it's
  209. * a legacy or a next gen cell, and pass it to the appropriate handler. */
  210. int
  211. hs_intro_received_establish_intro(or_circuit_t *circ, const uint8_t *request,
  212. size_t request_len)
  213. {
  214. tor_assert(circ);
  215. tor_assert(request);
  216. if (request_len == 0) {
  217. log_warn(LD_PROTOCOL, "Empty ESTABLISH_INTRO cell.");
  218. goto err;
  219. }
  220. /* Using the first byte of the cell, figure out the version of
  221. * ESTABLISH_INTRO and pass it to the appropriate cell handler */
  222. const uint8_t first_byte = request[0];
  223. switch (first_byte) {
  224. case HS_INTRO_AUTH_KEY_TYPE_LEGACY0:
  225. case HS_INTRO_AUTH_KEY_TYPE_LEGACY1:
  226. return rend_mid_establish_intro_legacy(circ, request, request_len);
  227. case HS_INTRO_AUTH_KEY_TYPE_ED25519:
  228. return handle_establish_intro(circ, request, request_len);
  229. default:
  230. log_warn(LD_PROTOCOL, "Unrecognized AUTH_KEY_TYPE %u.", first_byte);
  231. goto err;
  232. }
  233. err:
  234. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  235. return -1;
  236. }