hs_intropoint.c 21 KB

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