hs_intropoint.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. /* Validate the cell DoS extension parameters. Return true iff they've been
  163. * bound check and can be used. Else return false. See proposal 305 for
  164. * details and reasons about this validation. */
  165. STATIC bool
  166. validate_cell_dos_extension_parameters(uint64_t intro2_rate_per_sec,
  167. uint64_t intro2_burst_per_sec)
  168. {
  169. bool ret = false;
  170. /* A value of 0 is valid in the sense that we accept it but we still disable
  171. * the defenses so return false. */
  172. if (intro2_rate_per_sec == 0 || intro2_burst_per_sec == 0) {
  173. log_info(LD_REND, "Intro point DoS defenses parameter set to 0.");
  174. goto end;
  175. }
  176. /* Bound check the received rate per second. MIN/MAX are inclusive. */
  177. if (!(intro2_rate_per_sec <= HS_CONFIG_V3_DOS_DEFENSE_RATE_PER_SEC_MAX &&
  178. intro2_rate_per_sec > HS_CONFIG_V3_DOS_DEFENSE_RATE_PER_SEC_MIN)) {
  179. log_info(LD_REND, "Intro point DoS defenses rate per second is "
  180. "invalid. Received value: %" PRIu64,
  181. intro2_rate_per_sec);
  182. goto end;
  183. }
  184. /* Bound check the received burst per second. MIN/MAX are inclusive. */
  185. if (!(intro2_burst_per_sec <= HS_CONFIG_V3_DOS_DEFENSE_BURST_PER_SEC_MAX &&
  186. intro2_burst_per_sec > HS_CONFIG_V3_DOS_DEFENSE_BURST_PER_SEC_MIN)) {
  187. log_info(LD_REND, "Intro point DoS defenses burst per second is "
  188. "invalid. Received value: %" PRIu64,
  189. intro2_burst_per_sec);
  190. goto end;
  191. }
  192. /* In a rate limiting scenario, burst can never be smaller than the rate. At
  193. * best it can be equal. */
  194. if (intro2_burst_per_sec < intro2_rate_per_sec) {
  195. log_info(LD_REND, "Intro point DoS defenses burst is smaller than rate. "
  196. "Rate: %" PRIu64 " vs Burst: %" PRIu64,
  197. intro2_rate_per_sec, intro2_burst_per_sec);
  198. goto end;
  199. }
  200. /* Passing validation. */
  201. ret = true;
  202. end:
  203. return ret;
  204. }
  205. /* Parse the cell DoS extension and apply defenses on the given circuit if
  206. * validation passes. If the cell extension is malformed or contains unusable
  207. * values, the DoS defenses is disabled on the circuit. */
  208. static void
  209. handle_establish_intro_cell_dos_extension(
  210. const trn_cell_extension_field_t *field,
  211. or_circuit_t *circ)
  212. {
  213. ssize_t ret;
  214. uint64_t intro2_rate_per_sec = 0, intro2_burst_per_sec = 0;
  215. trn_cell_extension_dos_t *dos = NULL;
  216. tor_assert(field);
  217. tor_assert(circ);
  218. ret = trn_cell_extension_dos_parse(&dos,
  219. trn_cell_extension_field_getconstarray_field(field),
  220. trn_cell_extension_field_getlen_field(field));
  221. if (ret < 0) {
  222. goto end;
  223. }
  224. for (size_t i = 0; i < trn_cell_extension_dos_get_n_params(dos); i++) {
  225. const trn_cell_extension_dos_param_t *param =
  226. trn_cell_extension_dos_getconst_params(dos, i);
  227. if (BUG(param == NULL)) {
  228. goto end;
  229. }
  230. switch (trn_cell_extension_dos_param_get_type(param)) {
  231. case TRUNNEL_DOS_PARAM_TYPE_INTRO2_RATE_PER_SEC:
  232. intro2_rate_per_sec = trn_cell_extension_dos_param_get_value(param);
  233. break;
  234. case TRUNNEL_DOS_PARAM_TYPE_INTRO2_BURST_PER_SEC:
  235. intro2_burst_per_sec = trn_cell_extension_dos_param_get_value(param);
  236. break;
  237. default:
  238. goto end;
  239. }
  240. }
  241. /* If invalid, we disable the defense on the circuit. */
  242. if (!validate_cell_dos_extension_parameters(intro2_rate_per_sec,
  243. intro2_burst_per_sec)) {
  244. circ->introduce2_dos_defense_enabled = 0;
  245. log_info(LD_REND, "Disabling INTRO2 DoS defenses on circuit id %u",
  246. circ->p_circ_id);
  247. goto end;
  248. }
  249. /* We passed validation, enable defenses and apply rate/burst. */
  250. circ->introduce2_dos_defense_enabled = 1;
  251. /* Initialize the INTRODUCE2 token bucket for the rate limiting. */
  252. token_bucket_ctr_init(&circ->introduce2_bucket,
  253. (uint32_t) intro2_rate_per_sec,
  254. (uint32_t) intro2_burst_per_sec,
  255. (uint32_t) approx_time());
  256. log_info(LD_REND, "Intro point DoS defenses enabled. Rate is %" PRIu64
  257. " and Burst is %" PRIu64,
  258. intro2_rate_per_sec, intro2_burst_per_sec);
  259. end:
  260. trn_cell_extension_dos_free(dos);
  261. return;
  262. }
  263. /* Parse every cell extension in the given ESTABLISH_INTRO cell. */
  264. static void
  265. handle_establish_intro_cell_extensions(
  266. const trn_cell_establish_intro_t *parsed_cell,
  267. or_circuit_t *circ)
  268. {
  269. const trn_cell_extension_t *extensions;
  270. tor_assert(parsed_cell);
  271. tor_assert(circ);
  272. extensions = trn_cell_establish_intro_getconst_extensions(parsed_cell);
  273. if (extensions == NULL) {
  274. goto end;
  275. }
  276. /* Go over all extensions. */
  277. for (size_t idx = 0; idx < trn_cell_extension_get_num(extensions); idx++) {
  278. const trn_cell_extension_field_t *field =
  279. trn_cell_extension_getconst_fields(extensions, idx);
  280. if (BUG(field == NULL)) {
  281. /* The number of extensions should match the number of fields. */
  282. break;
  283. }
  284. switch (trn_cell_extension_field_get_field_type(field)) {
  285. case TRUNNEL_CELL_EXTENSION_TYPE_DOS:
  286. /* After this, the circuit should be set for DoS defenses. */
  287. handle_establish_intro_cell_dos_extension(field, circ);
  288. break;
  289. default:
  290. /* Unknown extension. Skip over. */
  291. break;
  292. }
  293. }
  294. end:
  295. return;
  296. }
  297. /** We received an ESTABLISH_INTRO <b>parsed_cell</b> on <b>circ</b>. It's
  298. * well-formed and passed our verifications. Perform appropriate actions to
  299. * establish an intro point. */
  300. static int
  301. handle_verified_establish_intro_cell(or_circuit_t *circ,
  302. const trn_cell_establish_intro_t *parsed_cell)
  303. {
  304. /* Get the auth key of this intro point */
  305. ed25519_public_key_t auth_key;
  306. get_auth_key_from_cell(&auth_key, RELAY_COMMAND_ESTABLISH_INTRO,
  307. parsed_cell);
  308. /* Setup INTRODUCE2 defenses on the circuit. Must be done before parsing the
  309. * cell extension that can possibly change the defenses' values. */
  310. hs_dos_setup_default_intro2_defenses(circ);
  311. /* Handle cell extension if any. */
  312. handle_establish_intro_cell_extensions(parsed_cell, circ);
  313. /* Then notify the hidden service that the intro point is established by
  314. sending an INTRO_ESTABLISHED cell */
  315. if (hs_intro_send_intro_established_cell(circ)) {
  316. log_warn(LD_PROTOCOL, "Couldn't send INTRO_ESTABLISHED cell.");
  317. return -1;
  318. }
  319. /* Associate intro point auth key with this circuit. */
  320. hs_circuitmap_register_intro_circ_v3_relay_side(circ, &auth_key);
  321. /* Repurpose this circuit into an intro circuit. */
  322. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
  323. return 0;
  324. }
  325. /** We just received an ESTABLISH_INTRO cell in <b>circ</b> with payload in
  326. * <b>request</b>. Handle it by making <b>circ</b> an intro circuit. Return 0
  327. * if everything went well, or -1 if there were errors. */
  328. static int
  329. handle_establish_intro(or_circuit_t *circ, const uint8_t *request,
  330. size_t request_len)
  331. {
  332. int cell_ok, retval = -1;
  333. trn_cell_establish_intro_t *parsed_cell = NULL;
  334. tor_assert(circ);
  335. tor_assert(request);
  336. log_info(LD_REND, "Received an ESTABLISH_INTRO request on circuit %" PRIu32,
  337. circ->p_circ_id);
  338. /* Check that the circuit is in shape to become an intro point */
  339. if (!hs_intro_circuit_is_suitable_for_establish_intro(circ)) {
  340. goto err;
  341. }
  342. /* Parse the cell */
  343. ssize_t parsing_result = trn_cell_establish_intro_parse(&parsed_cell,
  344. request, request_len);
  345. if (parsing_result < 0) {
  346. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  347. "Rejecting %s ESTABLISH_INTRO cell.",
  348. parsing_result == -1 ? "invalid" : "truncated");
  349. goto err;
  350. }
  351. cell_ok = verify_establish_intro_cell(parsed_cell,
  352. (uint8_t *) circ->rend_circ_nonce,
  353. sizeof(circ->rend_circ_nonce));
  354. if (cell_ok < 0) {
  355. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  356. "Failed to verify ESTABLISH_INTRO cell.");
  357. goto err;
  358. }
  359. /* This cell is legit. Take the appropriate actions. */
  360. cell_ok = handle_verified_establish_intro_cell(circ, parsed_cell);
  361. if (cell_ok < 0) {
  362. goto err;
  363. }
  364. /* We are done! */
  365. retval = 0;
  366. goto done;
  367. err:
  368. /* When sending the intro establish ack, on error the circuit can be marked
  369. * as closed so avoid a double close. */
  370. if (!TO_CIRCUIT(circ)->marked_for_close) {
  371. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  372. }
  373. done:
  374. trn_cell_establish_intro_free(parsed_cell);
  375. return retval;
  376. }
  377. /* Return True if circuit is suitable for being an intro circuit. */
  378. static int
  379. circuit_is_suitable_intro_point(const or_circuit_t *circ,
  380. const char *log_cell_type_str)
  381. {
  382. tor_assert(circ);
  383. tor_assert(log_cell_type_str);
  384. /* Basic circuit state sanity checks. */
  385. if (circ->base_.purpose != CIRCUIT_PURPOSE_OR) {
  386. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  387. "Rejecting %s on non-OR circuit.", log_cell_type_str);
  388. return 0;
  389. }
  390. if (circ->base_.n_chan) {
  391. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  392. "Rejecting %s on non-edge circuit.", log_cell_type_str);
  393. return 0;
  394. }
  395. /* Suitable. */
  396. return 1;
  397. }
  398. /* Return True if circuit is suitable for being service-side intro circuit. */
  399. int
  400. hs_intro_circuit_is_suitable_for_establish_intro(const or_circuit_t *circ)
  401. {
  402. return circuit_is_suitable_intro_point(circ, "ESTABLISH_INTRO");
  403. }
  404. /* We just received an ESTABLISH_INTRO cell in <b>circ</b>. Figure out of it's
  405. * a legacy or a next gen cell, and pass it to the appropriate handler. */
  406. int
  407. hs_intro_received_establish_intro(or_circuit_t *circ, const uint8_t *request,
  408. size_t request_len)
  409. {
  410. tor_assert(circ);
  411. tor_assert(request);
  412. if (request_len == 0) {
  413. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Empty ESTABLISH_INTRO cell.");
  414. goto err;
  415. }
  416. /* Using the first byte of the cell, figure out the version of
  417. * ESTABLISH_INTRO and pass it to the appropriate cell handler */
  418. const uint8_t first_byte = request[0];
  419. switch (first_byte) {
  420. case TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_LEGACY0:
  421. case TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_LEGACY1:
  422. return rend_mid_establish_intro_legacy(circ, request, request_len);
  423. case TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519:
  424. return handle_establish_intro(circ, request, request_len);
  425. default:
  426. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  427. "Unrecognized AUTH_KEY_TYPE %u.", first_byte);
  428. goto err;
  429. }
  430. err:
  431. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  432. return -1;
  433. }
  434. /* Send an INTRODUCE_ACK cell onto the circuit <b>circ</b> with the status
  435. * value in <b>status</b>. Depending on the status, it can be ACK or a NACK.
  436. * Return 0 on success else a negative value on error which will close the
  437. * circuit. */
  438. static int
  439. send_introduce_ack_cell(or_circuit_t *circ, uint16_t status)
  440. {
  441. int ret = -1;
  442. uint8_t *encoded_cell = NULL;
  443. ssize_t encoded_len, result_len;
  444. trn_cell_introduce_ack_t *cell;
  445. trn_cell_extension_t *ext;
  446. tor_assert(circ);
  447. /* Setup the INTRODUCE_ACK cell. We have no extensions so the N_EXTENSIONS
  448. * field is set to 0 by default with a new object. */
  449. cell = trn_cell_introduce_ack_new();
  450. ret = trn_cell_introduce_ack_set_status(cell, status);
  451. /* We have no cell extensions in an INTRODUCE_ACK cell. */
  452. ext = trn_cell_extension_new();
  453. trn_cell_extension_set_num(ext, 0);
  454. trn_cell_introduce_ack_set_extensions(cell, ext);
  455. /* A wrong status is a very bad code flow error as this value is controlled
  456. * by the code in this file and not an external input. This means we use a
  457. * code that is not known by the trunnel ABI. */
  458. tor_assert(ret == 0);
  459. /* Encode the payload. We should never fail to get the encoded length. */
  460. encoded_len = trn_cell_introduce_ack_encoded_len(cell);
  461. tor_assert(encoded_len > 0);
  462. encoded_cell = tor_malloc_zero(encoded_len);
  463. result_len = trn_cell_introduce_ack_encode(encoded_cell, encoded_len, cell);
  464. tor_assert(encoded_len == result_len);
  465. ret = relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
  466. RELAY_COMMAND_INTRODUCE_ACK,
  467. (char *) encoded_cell, encoded_len,
  468. NULL);
  469. /* On failure, the above function will close the circuit. */
  470. trn_cell_introduce_ack_free(cell);
  471. tor_free(encoded_cell);
  472. return ret;
  473. }
  474. /* Validate a parsed INTRODUCE1 <b>cell</b>. Return 0 if valid or else a
  475. * negative value for an invalid cell that should be NACKed. */
  476. STATIC int
  477. validate_introduce1_parsed_cell(const trn_cell_introduce1_t *cell)
  478. {
  479. size_t legacy_key_id_len;
  480. const uint8_t *legacy_key_id;
  481. tor_assert(cell);
  482. /* This code path SHOULD NEVER be reached if the cell is a legacy type so
  483. * safety net here. The legacy ID must be zeroes in this case. */
  484. legacy_key_id_len = trn_cell_introduce1_getlen_legacy_key_id(cell);
  485. legacy_key_id = trn_cell_introduce1_getconstarray_legacy_key_id(cell);
  486. if (BUG(!fast_mem_is_zero((char *) legacy_key_id, legacy_key_id_len))) {
  487. goto invalid;
  488. }
  489. /* The auth key of an INTRODUCE1 should be of type ed25519 thus leading to a
  490. * known fixed length as well. */
  491. if (trn_cell_introduce1_get_auth_key_type(cell) !=
  492. TRUNNEL_HS_INTRO_AUTH_KEY_TYPE_ED25519) {
  493. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  494. "Rejecting invalid INTRODUCE1 cell auth key type. "
  495. "Responding with NACK.");
  496. goto invalid;
  497. }
  498. if (trn_cell_introduce1_get_auth_key_len(cell) != ED25519_PUBKEY_LEN ||
  499. trn_cell_introduce1_getlen_auth_key(cell) != ED25519_PUBKEY_LEN) {
  500. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  501. "Rejecting invalid INTRODUCE1 cell auth key length. "
  502. "Responding with NACK.");
  503. goto invalid;
  504. }
  505. if (trn_cell_introduce1_getlen_encrypted(cell) == 0) {
  506. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  507. "Rejecting invalid INTRODUCE1 cell encrypted length. "
  508. "Responding with NACK.");
  509. goto invalid;
  510. }
  511. return 0;
  512. invalid:
  513. return -1;
  514. }
  515. /* We just received a non legacy INTRODUCE1 cell on <b>client_circ</b> with
  516. * the payload in <b>request</b> of size <b>request_len</b>. Return 0 if
  517. * everything went well, or -1 if an error occurred. This function is in charge
  518. * of sending back an INTRODUCE_ACK cell and will close client_circ on error.
  519. */
  520. STATIC int
  521. handle_introduce1(or_circuit_t *client_circ, const uint8_t *request,
  522. size_t request_len)
  523. {
  524. int ret = -1;
  525. or_circuit_t *service_circ;
  526. trn_cell_introduce1_t *parsed_cell;
  527. uint16_t status = TRUNNEL_HS_INTRO_ACK_STATUS_SUCCESS;
  528. tor_assert(client_circ);
  529. tor_assert(request);
  530. /* Parse cell. Note that we can only parse the non encrypted section for
  531. * which we'll use the authentication key to find the service introduction
  532. * circuit and relay the cell on it. */
  533. ssize_t cell_size = trn_cell_introduce1_parse(&parsed_cell, request,
  534. request_len);
  535. if (cell_size < 0) {
  536. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  537. "Rejecting %s INTRODUCE1 cell. Responding with NACK.",
  538. cell_size == -1 ? "invalid" : "truncated");
  539. /* Inform client that the INTRODUCE1 has a bad format. */
  540. status = TRUNNEL_HS_INTRO_ACK_STATUS_BAD_FORMAT;
  541. goto send_ack;
  542. }
  543. /* Once parsed validate the cell format. */
  544. if (validate_introduce1_parsed_cell(parsed_cell) < 0) {
  545. /* Inform client that the INTRODUCE1 has bad format. */
  546. status = TRUNNEL_HS_INTRO_ACK_STATUS_BAD_FORMAT;
  547. goto send_ack;
  548. }
  549. /* Find introduction circuit through our circuit map. */
  550. {
  551. ed25519_public_key_t auth_key;
  552. get_auth_key_from_cell(&auth_key, RELAY_COMMAND_INTRODUCE1, parsed_cell);
  553. service_circ = hs_circuitmap_get_intro_circ_v3_relay_side(&auth_key);
  554. if (service_circ == NULL) {
  555. char b64_key[ED25519_BASE64_LEN + 1];
  556. ed25519_public_to_base64(b64_key, &auth_key);
  557. log_info(LD_REND, "No intro circuit found for INTRODUCE1 cell "
  558. "with auth key %s from circuit %" PRIu32 ". "
  559. "Responding with NACK.",
  560. safe_str(b64_key), client_circ->p_circ_id);
  561. /* Inform the client that we don't know the requested service ID. */
  562. status = TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID;
  563. goto send_ack;
  564. }
  565. }
  566. /* Before sending, lets make sure this cell can be sent on the service
  567. * circuit asking the DoS defenses. */
  568. if (!hs_dos_can_send_intro2(service_circ)) {
  569. char *msg;
  570. static ratelim_t rlimit = RATELIM_INIT(5 * 60);
  571. if ((msg = rate_limit_log(&rlimit, approx_time()))) {
  572. log_info(LD_PROTOCOL, "Can't relay INTRODUCE1 v3 cell due to DoS "
  573. "limitations. Sending NACK to client.");
  574. tor_free(msg);
  575. }
  576. status = TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID;
  577. goto send_ack;
  578. }
  579. /* Relay the cell to the service on its intro circuit with an INTRODUCE2
  580. * cell which is the same exact payload. */
  581. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(service_circ),
  582. RELAY_COMMAND_INTRODUCE2,
  583. (char *) request, request_len, NULL)) {
  584. log_warn(LD_PROTOCOL, "Unable to send INTRODUCE2 cell to the service.");
  585. /* Inform the client that we can't relay the cell. Use the unknown ID
  586. * status code since it means that we do not know the service. */
  587. status = TRUNNEL_HS_INTRO_ACK_STATUS_UNKNOWN_ID;
  588. goto send_ack;
  589. }
  590. /* Success! Send an INTRODUCE_ACK success status onto the client circuit. */
  591. status = TRUNNEL_HS_INTRO_ACK_STATUS_SUCCESS;
  592. ret = 0;
  593. send_ack:
  594. /* Send INTRODUCE_ACK or INTRODUCE_NACK to client */
  595. if (send_introduce_ack_cell(client_circ, status) < 0) {
  596. log_warn(LD_PROTOCOL, "Unable to send an INTRODUCE ACK status %d "
  597. "to client.", status);
  598. /* Circuit has been closed on failure of transmission. */
  599. goto done;
  600. }
  601. done:
  602. trn_cell_introduce1_free(parsed_cell);
  603. return ret;
  604. }
  605. /* Identify if the encoded cell we just received is a legacy one or not. The
  606. * <b>request</b> should be at least DIGEST_LEN bytes long. */
  607. STATIC int
  608. introduce1_cell_is_legacy(const uint8_t *request)
  609. {
  610. tor_assert(request);
  611. /* If the first 20 bytes of the cell (DIGEST_LEN) are NOT zeroes, it
  612. * indicates a legacy cell (v2). */
  613. if (!fast_mem_is_zero((const char *) request, DIGEST_LEN)) {
  614. /* Legacy cell. */
  615. return 1;
  616. }
  617. /* Not a legacy cell. */
  618. return 0;
  619. }
  620. /* Return true iff the circuit <b>circ</b> is suitable for receiving an
  621. * INTRODUCE1 cell. */
  622. STATIC int
  623. circuit_is_suitable_for_introduce1(const or_circuit_t *circ)
  624. {
  625. tor_assert(circ);
  626. /* Is this circuit an intro point circuit? */
  627. if (!circuit_is_suitable_intro_point(circ, "INTRODUCE1")) {
  628. return 0;
  629. }
  630. if (circ->already_received_introduce1) {
  631. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  632. "Blocking multiple introductions on the same circuit. "
  633. "Someone might be trying to attack a hidden service through "
  634. "this relay.");
  635. return 0;
  636. }
  637. /* Disallow single hop client circuit. */
  638. if (circ->p_chan && channel_is_client(circ->p_chan)) {
  639. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  640. "Single hop client was rejected while trying to introduce. "
  641. "Closing circuit.");
  642. return 0;
  643. }
  644. return 1;
  645. }
  646. /* We just received an INTRODUCE1 cell on <b>circ</b>. Figure out which type
  647. * it is and pass it to the appropriate handler. Return 0 on success else a
  648. * negative value and the circuit is closed. */
  649. int
  650. hs_intro_received_introduce1(or_circuit_t *circ, const uint8_t *request,
  651. size_t request_len)
  652. {
  653. int ret;
  654. tor_assert(circ);
  655. tor_assert(request);
  656. /* A cell that can't hold a DIGEST_LEN is invalid as we need to check if
  657. * it's a legacy cell or not using the first DIGEST_LEN bytes. */
  658. if (request_len < DIGEST_LEN) {
  659. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Invalid INTRODUCE1 cell length.");
  660. goto err;
  661. }
  662. /* Make sure we have a circuit that can have an INTRODUCE1 cell on it. */
  663. if (!circuit_is_suitable_for_introduce1(circ)) {
  664. /* We do not send a NACK because the circuit is not suitable for any kind
  665. * of response or transmission as it's a violation of the protocol. */
  666. goto err;
  667. }
  668. /* Mark the circuit that we got this cell. None are allowed after this as a
  669. * DoS mitigation since one circuit with one client can hammer a service. */
  670. circ->already_received_introduce1 = 1;
  671. /* We are sure here to have at least DIGEST_LEN bytes. */
  672. if (introduce1_cell_is_legacy(request)) {
  673. /* Handle a legacy cell. */
  674. ret = rend_mid_introduce_legacy(circ, request, request_len);
  675. } else {
  676. /* Handle a non legacy cell. */
  677. ret = handle_introduce1(circ, request, request_len);
  678. }
  679. return ret;
  680. err:
  681. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  682. return -1;
  683. }
  684. /* Clear memory allocated by the given intropoint object ip (but don't free the
  685. * object itself). */
  686. void
  687. hs_intropoint_clear(hs_intropoint_t *ip)
  688. {
  689. if (ip == NULL) {
  690. return;
  691. }
  692. tor_cert_free(ip->auth_key_cert);
  693. SMARTLIST_FOREACH(ip->link_specifiers, link_specifier_t *, ls,
  694. link_specifier_free(ls));
  695. smartlist_free(ip->link_specifiers);
  696. memset(ip, 0, sizeof(hs_intropoint_t));
  697. }