hs_intropoint.c 9.4 KB

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