hs_intropoint.c 28 KB

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