hs_intropoint.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* Copyright (c) 2016-2019, 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 "core/or/or.h"
  9. #include "app/config/config.h"
  10. #include "core/or/channel.h"
  11. #include "core/or/circuitlist.h"
  12. #include "core/or/circuituse.h"
  13. #include "core/or/relay.h"
  14. #include "feature/rend/rendmid.h"
  15. #include "feature/stats/rephist.h"
  16. #include "lib/crypt_ops/crypto_format.h"
  17. /* Trunnel */
  18. #include "trunnel/ed25519_cert.h"
  19. #include "trunnel/hs/cell_common.h"
  20. #include "trunnel/hs/cell_establish_intro.h"
  21. #include "trunnel/hs/cell_introduce1.h"
  22. #include "feature/hs/hs_circuitmap.h"
  23. #include "feature/hs/hs_descriptor.h"
  24. #include "feature/hs/hs_intropoint.h"
  25. #include "feature/hs/hs_common.h"
  26. #include "core/or/or_circuit_st.h"
  27. /** Extract the authentication key from an ESTABLISH_INTRO or INTRODUCE1 using
  28. * the given <b>cell_type</b> from <b>cell</b> and place it in
  29. * <b>auth_key_out</b>. */
  30. STATIC void
  31. get_auth_key_from_cell(ed25519_public_key_t *auth_key_out,
  32. unsigned int cell_type, const void *cell)
  33. {
  34. size_t auth_key_len;
  35. const uint8_t *key_array;
  36. tor_assert(auth_key_out);
  37. tor_assert(cell);
  38. switch (cell_type) {
  39. case RELAY_COMMAND_ESTABLISH_INTRO:
  40. {
  41. const trn_cell_establish_intro_t *c_cell = cell;
  42. key_array = trn_cell_establish_intro_getconstarray_auth_key(c_cell);
  43. auth_key_len = trn_cell_establish_intro_getlen_auth_key(c_cell);
  44. break;
  45. }
  46. case RELAY_COMMAND_INTRODUCE1:
  47. {
  48. const trn_cell_introduce1_t *c_cell = cell;
  49. key_array = trn_cell_introduce1_getconstarray_auth_key(cell);
  50. auth_key_len = trn_cell_introduce1_getlen_auth_key(c_cell);
  51. break;
  52. }
  53. default:
  54. /* Getting here is really bad as it means we got a unknown cell type from
  55. * this file where every call has an hardcoded value. */
  56. tor_assert_unreached(); /* LCOV_EXCL_LINE */
  57. }
  58. tor_assert(key_array);
  59. tor_assert(auth_key_len == sizeof(auth_key_out->pubkey));
  60. memcpy(auth_key_out->pubkey, key_array, auth_key_len);
  61. }
  62. /** We received an ESTABLISH_INTRO <b>cell</b>. Verify its signature and MAC,
  63. * given <b>circuit_key_material</b>. Return 0 on success else -1 on error. */
  64. STATIC int
  65. verify_establish_intro_cell(const trn_cell_establish_intro_t *cell,
  66. const uint8_t *circuit_key_material,
  67. size_t circuit_key_material_len)
  68. {
  69. /* We only reach this function if the first byte of the cell is 0x02 which
  70. * means that auth_key_type is of ed25519 type, hence this check should
  71. * always pass. See hs_intro_received_establish_intro(). */
  72. if (BUG(cell->auth_key_type != TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519)) {
  73. return -1;
  74. }
  75. /* Make sure the auth key length is of the right size for this type. For
  76. * EXTRA safety, we check both the size of the array and the length which
  77. * must be the same. Safety first!*/
  78. if (trn_cell_establish_intro_getlen_auth_key(cell) != ED25519_PUBKEY_LEN ||
  79. trn_cell_establish_intro_get_auth_key_len(cell) != ED25519_PUBKEY_LEN) {
  80. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  81. "ESTABLISH_INTRO auth key length is invalid");
  82. return -1;
  83. }
  84. const uint8_t *msg = cell->start_cell;
  85. /* Verify the sig */
  86. {
  87. ed25519_signature_t sig_struct;
  88. const uint8_t *sig_array =
  89. trn_cell_establish_intro_getconstarray_sig(cell);
  90. /* Make sure the signature length is of the right size. For EXTRA safety,
  91. * we check both the size of the array and the length which must be the
  92. * same. Safety first!*/
  93. if (trn_cell_establish_intro_getlen_sig(cell) != sizeof(sig_struct.sig) ||
  94. trn_cell_establish_intro_get_sig_len(cell) != sizeof(sig_struct.sig)) {
  95. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  96. "ESTABLISH_INTRO sig len is invalid");
  97. return -1;
  98. }
  99. /* We are now sure that sig_len is of the right size. */
  100. memcpy(sig_struct.sig, sig_array, cell->sig_len);
  101. ed25519_public_key_t auth_key;
  102. get_auth_key_from_cell(&auth_key, RELAY_COMMAND_ESTABLISH_INTRO, cell);
  103. const size_t sig_msg_len = cell->end_sig_fields - msg;
  104. int sig_mismatch = ed25519_checksig_prefixed(&sig_struct,
  105. msg, sig_msg_len,
  106. ESTABLISH_INTRO_SIG_PREFIX,
  107. &auth_key);
  108. if (sig_mismatch) {
  109. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  110. "ESTABLISH_INTRO signature not as expected");
  111. return -1;
  112. }
  113. }
  114. /* Verify the MAC */
  115. {
  116. const size_t auth_msg_len = cell->end_mac_fields - msg;
  117. uint8_t mac[DIGEST256_LEN];
  118. crypto_mac_sha3_256(mac, sizeof(mac),
  119. circuit_key_material, circuit_key_material_len,
  120. msg, auth_msg_len);
  121. if (tor_memneq(mac, cell->handshake_mac, sizeof(mac))) {
  122. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  123. "ESTABLISH_INTRO handshake_auth not as expected");
  124. return -1;
  125. }
  126. }
  127. return 0;
  128. }
  129. /* Send an INTRO_ESTABLISHED cell to <b>circ</b>. */
  130. MOCK_IMPL(int,
  131. hs_intro_send_intro_established_cell,(or_circuit_t *circ))
  132. {
  133. int ret;
  134. uint8_t *encoded_cell = NULL;
  135. ssize_t encoded_len, result_len;
  136. trn_cell_intro_established_t *cell;
  137. trn_cell_extension_t *ext;
  138. tor_assert(circ);
  139. /* Build the cell payload. */
  140. cell = trn_cell_intro_established_new();
  141. ext = trn_cell_extension_new();
  142. trn_cell_extension_set_num(ext, 0);
  143. trn_cell_intro_established_set_extensions(cell, ext);
  144. /* Encode the cell to binary format. */
  145. encoded_len = trn_cell_intro_established_encoded_len(cell);
  146. tor_assert(encoded_len > 0);
  147. encoded_cell = tor_malloc_zero(encoded_len);
  148. result_len = trn_cell_intro_established_encode(encoded_cell, encoded_len,
  149. cell);
  150. tor_assert(encoded_len == result_len);
  151. ret = relay_send_command_from_edge(0, TO_CIRCUIT(circ),
  152. RELAY_COMMAND_INTRO_ESTABLISHED,
  153. (char *) encoded_cell, encoded_len,
  154. NULL);
  155. /* On failure, the above function will close the circuit. */
  156. trn_cell_intro_established_free(cell);
  157. tor_free(encoded_cell);
  158. return ret;
  159. }
  160. /** We received an ESTABLISH_INTRO <b>parsed_cell</b> on <b>circ</b>. It's
  161. * well-formed and passed our verifications. Perform appropriate actions to
  162. * establish an intro point. */
  163. static int
  164. handle_verified_establish_intro_cell(or_circuit_t *circ,
  165. const trn_cell_establish_intro_t *parsed_cell)
  166. {
  167. /* Get the auth key of this intro point */
  168. ed25519_public_key_t auth_key;
  169. get_auth_key_from_cell(&auth_key, RELAY_COMMAND_ESTABLISH_INTRO,
  170. parsed_cell);
  171. /* Then notify the hidden service that the intro point is established by
  172. sending an INTRO_ESTABLISHED cell */
  173. if (hs_intro_send_intro_established_cell(circ)) {
  174. log_warn(LD_PROTOCOL, "Couldn't send INTRO_ESTABLISHED cell.");
  175. return -1;
  176. }
  177. /* Associate intro point auth key with this circuit. */
  178. hs_circuitmap_register_intro_circ_v3_relay_side(circ, &auth_key);
  179. /* Repurpose this circuit into an intro circuit. */
  180. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
  181. return 0;
  182. }
  183. /** We just received an ESTABLISH_INTRO cell in <b>circ</b> with payload in
  184. * <b>request</b>. Handle it by making <b>circ</b> an intro circuit. Return 0
  185. * if everything went well, or -1 if there were errors. */
  186. static int
  187. handle_establish_intro(or_circuit_t *circ, const uint8_t *request,
  188. size_t request_len)
  189. {
  190. int cell_ok, retval = -1;
  191. trn_cell_establish_intro_t *parsed_cell = NULL;
  192. tor_assert(circ);
  193. tor_assert(request);
  194. log_info(LD_REND, "Received an ESTABLISH_INTRO request on circuit %" PRIu32,
  195. circ->p_circ_id);
  196. /* Check that the circuit is in shape to become an intro point */
  197. if (!hs_intro_circuit_is_suitable_for_establish_intro(circ)) {
  198. goto err;
  199. }
  200. /* Parse the cell */
  201. ssize_t parsing_result = trn_cell_establish_intro_parse(&parsed_cell,
  202. request, request_len);
  203. if (parsing_result < 0) {
  204. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  205. "Rejecting %s ESTABLISH_INTRO cell.",
  206. parsing_result == -1 ? "invalid" : "truncated");
  207. goto err;
  208. }
  209. cell_ok = verify_establish_intro_cell(parsed_cell,
  210. (uint8_t *) circ->rend_circ_nonce,
  211. sizeof(circ->rend_circ_nonce));
  212. if (cell_ok < 0) {
  213. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  214. "Failed to verify ESTABLISH_INTRO cell.");
  215. goto err;
  216. }
  217. /* This cell is legit. Take the appropriate actions. */
  218. cell_ok = handle_verified_establish_intro_cell(circ, parsed_cell);
  219. if (cell_ok < 0) {
  220. goto err;
  221. }
  222. /* We are done! */
  223. retval = 0;
  224. goto done;
  225. err:
  226. /* When sending the intro establish ack, on error the circuit can be marked
  227. * as closed so avoid a double close. */
  228. if (!TO_CIRCUIT(circ)->marked_for_close) {
  229. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  230. }
  231. done:
  232. trn_cell_establish_intro_free(parsed_cell);
  233. return retval;
  234. }
  235. /* Return True if circuit is suitable for being an intro circuit. */
  236. static int
  237. circuit_is_suitable_intro_point(const or_circuit_t *circ,
  238. const char *log_cell_type_str)
  239. {
  240. tor_assert(circ);
  241. tor_assert(log_cell_type_str);
  242. /* Basic circuit state sanity checks. */
  243. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
  244. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  245. "Rejecting %s on non-OR circuit.", log_cell_type_str);
  246. return 0;
  247. }
  248. if (circ->base_.n_chan) {
  249. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  250. "Rejecting %s on non-edge circuit.", log_cell_type_str);
  251. return 0;
  252. }
  253. /* Suitable. */
  254. return 1;
  255. }
  256. /* Return True if circuit is suitable for being service-side intro circuit. */
  257. int
  258. hs_intro_circuit_is_suitable_for_establish_intro(const or_circuit_t *circ)
  259. {
  260. return circuit_is_suitable_intro_point(circ, "ESTABLISH_INTRO");
  261. }
  262. /* We just received an ESTABLISH_INTRO cell in <b>circ</b>. Figure out of it's
  263. * a legacy or a next gen cell, and pass it to the appropriate handler. */
  264. int
  265. hs_intro_received_establish_intro(or_circuit_t *circ, const uint8_t *request,
  266. size_t request_len)
  267. {
  268. tor_assert(circ);
  269. tor_assert(request);
  270. if (request_len == 0) {
  271. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Empty ESTABLISH_INTRO cell.");
  272. goto err;
  273. }
  274. /* Using the first byte of the cell, figure out the version of
  275. * ESTABLISH_INTRO and pass it to the appropriate cell handler */
  276. const uint8_t first_byte = request[0];
  277. switch (first_byte) {
  278. case TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_LEGACY0:
  279. case TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_LEGACY1:
  280. return rend_mid_establish_intro_legacy(circ, request, request_len);
  281. case TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519:
  282. return handle_establish_intro(circ, request, request_len);
  283. default:
  284. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  285. "Unrecognized AUTH_KEY_TYPE %u.", first_byte);
  286. goto err;
  287. }
  288. err:
  289. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  290. return -1;
  291. }
  292. /* Send an INTRODUCE_ACK cell onto the circuit <b>circ</b> with the status
  293. * value in <b>status</b>. Depending on the status, it can be ACK or a NACK.
  294. * Return 0 on success else a negative value on error which will close the
  295. * circuit. */
  296. static int
  297. send_introduce_ack_cell(or_circuit_t *circ, uint16_t status)
  298. {
  299. int ret = -1;
  300. uint8_t *encoded_cell = NULL;
  301. ssize_t encoded_len, result_len;
  302. trn_cell_introduce_ack_t *cell;
  303. trn_cell_extension_t *ext;
  304. tor_assert(circ);
  305. /* Setup the INTRODUCE_ACK cell. We have no extensions so the N_EXTENSIONS
  306. * field is set to 0 by default with a new object. */
  307. cell = trn_cell_introduce_ack_new();
  308. ret = trn_cell_introduce_ack_set_status(cell, status);
  309. /* We have no cell extensions in an INTRODUCE_ACK cell. */
  310. ext = trn_cell_extension_new();
  311. trn_cell_extension_set_num(ext, 0);
  312. trn_cell_introduce_ack_set_extensions(cell, ext);
  313. /* A wrong status is a very bad code flow error as this value is controlled
  314. * by the code in this file and not an external input. This means we use a
  315. * code that is not known by the trunnel ABI. */
  316. tor_assert(ret == 0);
  317. /* Encode the payload. We should never fail to get the encoded length. */
  318. encoded_len = trn_cell_introduce_ack_encoded_len(cell);
  319. tor_assert(encoded_len > 0);
  320. encoded_cell = tor_malloc_zero(encoded_len);
  321. result_len = trn_cell_introduce_ack_encode(encoded_cell, encoded_len, cell);
  322. tor_assert(encoded_len == result_len);
  323. ret = relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
  324. RELAY_COMMAND_INTRODUCE_ACK,
  325. (char *) encoded_cell, encoded_len,
  326. NULL);
  327. /* On failure, the above function will close the circuit. */
  328. trn_cell_introduce_ack_free(cell);
  329. tor_free(encoded_cell);
  330. return ret;
  331. }
  332. /* Validate a parsed INTRODUCE1 <b>cell</b>. Return 0 if valid or else a
  333. * negative value for an invalid cell that should be NACKed. */
  334. STATIC int
  335. validate_introduce1_parsed_cell(const trn_cell_introduce1_t *cell)
  336. {
  337. size_t legacy_key_id_len;
  338. const uint8_t *legacy_key_id;
  339. tor_assert(cell);
  340. /* This code path SHOULD NEVER be reached if the cell is a legacy type so
  341. * safety net here. The legacy ID must be zeroes in this case. */
  342. legacy_key_id_len = trn_cell_introduce1_getlen_legacy_key_id(cell);
  343. legacy_key_id = trn_cell_introduce1_getconstarray_legacy_key_id(cell);
  344. if (BUG(!fast_mem_is_zero((char *) legacy_key_id, legacy_key_id_len))) {
  345. goto invalid;
  346. }
  347. /* The auth key of an INTRODUCE1 should be of type ed25519 thus leading to a
  348. * known fixed length as well. */
  349. if (trn_cell_introduce1_get_auth_key_type(cell) !=
  350. TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519) {
  351. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  352. "Rejecting invalid INTRODUCE1 cell auth key type. "
  353. "Responding with NACK.");
  354. goto invalid;
  355. }
  356. if (trn_cell_introduce1_get_auth_key_len(cell) != ED25519_PUBKEY_LEN ||
  357. trn_cell_introduce1_getlen_auth_key(cell) != ED25519_PUBKEY_LEN) {
  358. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  359. "Rejecting invalid INTRODUCE1 cell auth key length. "
  360. "Responding with NACK.");
  361. goto invalid;
  362. }
  363. if (trn_cell_introduce1_getlen_encrypted(cell) == 0) {
  364. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  365. "Rejecting invalid INTRODUCE1 cell encrypted length. "
  366. "Responding with NACK.");
  367. goto invalid;
  368. }
  369. return 0;
  370. invalid:
  371. return -1;
  372. }
  373. /* We just received a non legacy INTRODUCE1 cell on <b>client_circ</b> with
  374. * the payload in <b>request</b> of size <b>request_len</b>. Return 0 if
  375. * everything went well, or -1 if an error occurred. This function is in charge
  376. * of sending back an INTRODUCE_ACK cell and will close client_circ on error.
  377. */
  378. STATIC int
  379. handle_introduce1(or_circuit_t *client_circ, const uint8_t *request,
  380. size_t request_len)
  381. {
  382. int ret = -1;
  383. or_circuit_t *service_circ;
  384. trn_cell_introduce1_t *parsed_cell;
  385. uint16_t status = TRUNNEL_HS_INTRO_ACK_STATUS_SUCCESS;
  386. tor_assert(client_circ);
  387. tor_assert(request);
  388. /* Parse cell. Note that we can only parse the non encrypted section for
  389. * which we'll use the authentication key to find the service introduction
  390. * circuit and relay the cell on it. */
  391. ssize_t cell_size = trn_cell_introduce1_parse(&parsed_cell, request,
  392. request_len);
  393. if (cell_size < 0) {
  394. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  395. "Rejecting %s INTRODUCE1 cell. Responding with NACK.",
  396. cell_size == -1 ? "invalid" : "truncated");
  397. /* Inform client that the INTRODUCE1 has a bad format. */
  398. status = TRUNNEL_HS_INTRO_ACK_STATUS_BAD_FORMAT;
  399. goto send_ack;
  400. }
  401. /* Once parsed validate the cell format. */
  402. if (validate_introduce1_parsed_cell(parsed_cell) < 0) {
  403. /* Inform client that the INTRODUCE1 has bad format. */
  404. status = TRUNNEL_HS_INTRO_ACK_STATUS_BAD_FORMAT;
  405. goto send_ack;
  406. }
  407. /* Find introduction circuit through our circuit map. */
  408. {
  409. ed25519_public_key_t auth_key;
  410. get_auth_key_from_cell(&auth_key, RELAY_COMMAND_INTRODUCE1, parsed_cell);
  411. service_circ = hs_circuitmap_get_intro_circ_v3_relay_side(&auth_key);
  412. if (service_circ == NULL) {
  413. char b64_key[ED25519_BASE64_LEN + 1];
  414. ed25519_public_to_base64(b64_key, &auth_key);
  415. log_info(LD_REND, "No intro circuit found for INTRODUCE1 cell "
  416. "with auth key %s from circuit %" PRIu32 ". "
  417. "Responding with NACK.",
  418. safe_str(b64_key), client_circ->p_circ_id);
  419. /* Inform the client that we don't know the requested service ID. */
  420. status = TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID;
  421. goto send_ack;
  422. }
  423. }
  424. /* Relay the cell to the service on its intro circuit with an INTRODUCE2
  425. * cell which is the same exact payload. */
  426. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(service_circ),
  427. RELAY_COMMAND_INTRODUCE2,
  428. (char *) request, request_len, NULL)) {
  429. log_warn(LD_PROTOCOL, "Unable to send INTRODUCE2 cell to the service.");
  430. /* Inform the client that we can't relay the cell. Use the unknown ID
  431. * status code since it means that we do not know the service. */
  432. status = TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID;
  433. goto send_ack;
  434. }
  435. /* Success! Send an INTRODUCE_ACK success status onto the client circuit. */
  436. status = TRUNNEL_HS_INTRO_ACK_STATUS_SUCCESS;
  437. ret = 0;
  438. send_ack:
  439. /* Send INTRODUCE_ACK or INTRODUCE_NACK to client */
  440. if (send_introduce_ack_cell(client_circ, status) < 0) {
  441. log_warn(LD_PROTOCOL, "Unable to send an INTRODUCE ACK status %d "
  442. "to client.", status);
  443. /* Circuit has been closed on failure of transmission. */
  444. goto done;
  445. }
  446. done:
  447. trn_cell_introduce1_free(parsed_cell);
  448. return ret;
  449. }
  450. /* Identify if the encoded cell we just received is a legacy one or not. The
  451. * <b>request</b> should be at least DIGEST_LEN bytes long. */
  452. STATIC int
  453. introduce1_cell_is_legacy(const uint8_t *request)
  454. {
  455. tor_assert(request);
  456. /* If the first 20 bytes of the cell (DIGEST_LEN) are NOT zeroes, it
  457. * indicates a legacy cell (v2). */
  458. if (!fast_mem_is_zero((const char *) request, DIGEST_LEN)) {
  459. /* Legacy cell. */
  460. return 1;
  461. }
  462. /* Not a legacy cell. */
  463. return 0;
  464. }
  465. /* Return true iff the circuit <b>circ</b> is suitable for receiving an
  466. * INTRODUCE1 cell. */
  467. STATIC int
  468. circuit_is_suitable_for_introduce1(const or_circuit_t *circ)
  469. {
  470. tor_assert(circ);
  471. /* Is this circuit an intro point circuit? */
  472. if (!circuit_is_suitable_intro_point(circ, "INTRODUCE1")) {
  473. return 0;
  474. }
  475. if (circ->already_received_introduce1) {
  476. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  477. "Blocking multiple introductions on the same circuit. "
  478. "Someone might be trying to attack a hidden service through "
  479. "this relay.");
  480. return 0;
  481. }
  482. /* Disallow single hop client circuit. */
  483. if (channel_is_client(circ->p_chan)) {
  484. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  485. "Single hop client was rejected while trying to introduce. "
  486. "Closing circuit.");
  487. return 0;
  488. }
  489. return 1;
  490. }
  491. /* We just received an INTRODUCE1 cell on <b>circ</b>. Figure out which type
  492. * it is and pass it to the appropriate handler. Return 0 on success else a
  493. * negative value and the circuit is closed. */
  494. int
  495. hs_intro_received_introduce1(or_circuit_t *circ, const uint8_t *request,
  496. size_t request_len)
  497. {
  498. int ret;
  499. tor_assert(circ);
  500. tor_assert(request);
  501. /* A cell that can't hold a DIGEST_LEN is invalid as we need to check if
  502. * it's a legacy cell or not using the first DIGEST_LEN bytes. */
  503. if (request_len < DIGEST_LEN) {
  504. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Invalid INTRODUCE1 cell length.");
  505. goto err;
  506. }
  507. /* Make sure we have a circuit that can have an INTRODUCE1 cell on it. */
  508. if (!circuit_is_suitable_for_introduce1(circ)) {
  509. /* We do not send a NACK because the circuit is not suitable for any kind
  510. * of response or transmission as it's a violation of the protocol. */
  511. goto err;
  512. }
  513. /* Mark the circuit that we got this cell. None are allowed after this as a
  514. * DoS mitigation since one circuit with one client can hammer a service. */
  515. circ->already_received_introduce1 = 1;
  516. /* We are sure here to have at least DIGEST_LEN bytes. */
  517. if (introduce1_cell_is_legacy(request)) {
  518. /* Handle a legacy cell. */
  519. ret = rend_mid_introduce_legacy(circ, request, request_len);
  520. } else {
  521. /* Handle a non legacy cell. */
  522. ret = handle_introduce1(circ, request, request_len);
  523. }
  524. return ret;
  525. err:
  526. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  527. return -1;
  528. }
  529. /* Clear memory allocated by the given intropoint object ip (but don't free the
  530. * object itself). */
  531. void
  532. hs_intropoint_clear(hs_intropoint_t *ip)
  533. {
  534. if (ip == NULL) {
  535. return;
  536. }
  537. tor_cert_free(ip->auth_key_cert);
  538. SMARTLIST_FOREACH(ip->link_specifiers, link_specifier_t *, ls,
  539. link_specifier_free(ls));
  540. smartlist_free(ip->link_specifiers);
  541. memset(ip, 0, sizeof(hs_intropoint_t));
  542. }