hs_intropoint.c 21 KB

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