hs_intropoint.c 26 KB

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