hs_intropoint.c 9.6 KB

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