hs_circuit.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /* Copyright (c) 2017-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_circuit.c
  5. **/
  6. #define HS_CIRCUIT_PRIVATE
  7. #include "core/or/or.h"
  8. #include "app/config/config.h"
  9. #include "core/crypto/hs_ntor.h"
  10. #include "core/or/circuitbuild.h"
  11. #include "core/or/circuitlist.h"
  12. #include "core/or/circuituse.h"
  13. #include "core/or/policies.h"
  14. #include "core/or/relay.h"
  15. #include "core/or/crypt_path.h"
  16. #include "feature/client/circpathbias.h"
  17. #include "feature/hs/hs_cell.h"
  18. #include "feature/hs/hs_circuit.h"
  19. #include "feature/hs/hs_circuitmap.h"
  20. #include "feature/hs/hs_ident.h"
  21. #include "feature/hs/hs_service.h"
  22. #include "feature/nodelist/describe.h"
  23. #include "feature/nodelist/nodelist.h"
  24. #include "feature/rend/rendservice.h"
  25. #include "feature/stats/rephist.h"
  26. #include "lib/crypt_ops/crypto_dh.h"
  27. #include "lib/crypt_ops/crypto_rand.h"
  28. #include "lib/crypt_ops/crypto_util.h"
  29. /* Trunnel. */
  30. #include "trunnel/ed25519_cert.h"
  31. #include "trunnel/hs/cell_common.h"
  32. #include "trunnel/hs/cell_establish_intro.h"
  33. #include "core/or/cpath_build_state_st.h"
  34. #include "core/or/crypt_path_st.h"
  35. #include "feature/nodelist/node_st.h"
  36. #include "core/or/origin_circuit_st.h"
  37. /* A circuit is about to become an e2e rendezvous circuit. Check
  38. * <b>circ_purpose</b> and ensure that it's properly set. Return true iff
  39. * circuit purpose is properly set, otherwise return false. */
  40. static int
  41. circuit_purpose_is_correct_for_rend(unsigned int circ_purpose,
  42. int is_service_side)
  43. {
  44. if (is_service_side) {
  45. if (circ_purpose != CIRCUIT_PURPOSE_S_CONNECT_REND) {
  46. log_warn(LD_BUG,
  47. "HS e2e circuit setup with wrong purpose (%d)", circ_purpose);
  48. return 0;
  49. }
  50. }
  51. if (!is_service_side) {
  52. if (circ_purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  53. circ_purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  54. log_warn(LD_BUG,
  55. "Client e2e circuit setup with wrong purpose (%d)", circ_purpose);
  56. return 0;
  57. }
  58. }
  59. return 1;
  60. }
  61. /* Create and return a crypt path for the final hop of a v3 prop224 rendezvous
  62. * circuit. Initialize the crypt path crypto using the output material from the
  63. * ntor key exchange at <b>ntor_key_seed</b>.
  64. *
  65. * If <b>is_service_side</b> is set, we are the hidden service and the final
  66. * hop of the rendezvous circuit is the client on the other side. */
  67. static crypt_path_t *
  68. create_rend_cpath(const uint8_t *ntor_key_seed, size_t seed_len,
  69. int is_service_side)
  70. {
  71. uint8_t keys[HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN];
  72. crypt_path_t *cpath = NULL;
  73. /* Do the key expansion */
  74. if (hs_ntor_circuit_key_expansion(ntor_key_seed, seed_len,
  75. keys, sizeof(keys)) < 0) {
  76. goto err;
  77. }
  78. /* Setup the cpath */
  79. cpath = tor_malloc_zero(sizeof(crypt_path_t));
  80. cpath->magic = CRYPT_PATH_MAGIC;
  81. if (cpath_init_circuit_crypto(cpath, (char*)keys, sizeof(keys),
  82. is_service_side, 1) < 0) {
  83. tor_free(cpath);
  84. goto err;
  85. }
  86. err:
  87. memwipe(keys, 0, sizeof(keys));
  88. return cpath;
  89. }
  90. /* We are a v2 legacy HS client: Create and return a crypt path for the hidden
  91. * service on the other side of the rendezvous circuit <b>circ</b>. Initialize
  92. * the crypt path crypto using the body of the RENDEZVOUS1 cell at
  93. * <b>rend_cell_body</b> (which must be at least DH1024_KEY_LEN+DIGEST_LEN
  94. * bytes).
  95. */
  96. static crypt_path_t *
  97. create_rend_cpath_legacy(origin_circuit_t *circ, const uint8_t *rend_cell_body)
  98. {
  99. crypt_path_t *hop = NULL;
  100. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  101. /* first DH1024_KEY_LEN bytes are g^y from the service. Finish the dh
  102. * handshake...*/
  103. tor_assert(circ->build_state);
  104. tor_assert(circ->build_state->pending_final_cpath);
  105. hop = circ->build_state->pending_final_cpath;
  106. tor_assert(hop->rend_dh_handshake_state);
  107. if (crypto_dh_compute_secret(LOG_PROTOCOL_WARN, hop->rend_dh_handshake_state,
  108. (char*)rend_cell_body, DH1024_KEY_LEN,
  109. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  110. log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
  111. goto err;
  112. }
  113. /* ... and set up cpath. */
  114. if (cpath_init_circuit_crypto(hop,
  115. keys+DIGEST_LEN, sizeof(keys)-DIGEST_LEN,
  116. 0, 0) < 0)
  117. goto err;
  118. /* Check whether the digest is right... */
  119. if (tor_memneq(keys, rend_cell_body+DH1024_KEY_LEN, DIGEST_LEN)) {
  120. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  121. goto err;
  122. }
  123. /* clean up the crypto stuff we just made */
  124. crypto_dh_free(hop->rend_dh_handshake_state);
  125. hop->rend_dh_handshake_state = NULL;
  126. goto done;
  127. err:
  128. hop = NULL;
  129. done:
  130. memwipe(keys, 0, sizeof(keys));
  131. return hop;
  132. }
  133. /* Append the final <b>hop</b> to the cpath of the rend <b>circ</b>, and mark
  134. * <b>circ</b> ready for use to transfer HS relay cells. */
  135. static void
  136. finalize_rend_circuit(origin_circuit_t *circ, crypt_path_t *hop,
  137. int is_service_side)
  138. {
  139. tor_assert(circ);
  140. tor_assert(hop);
  141. /* Notify the circuit state machine that we are splicing this circuit */
  142. int new_circ_purpose = is_service_side ?
  143. CIRCUIT_PURPOSE_S_REND_JOINED : CIRCUIT_PURPOSE_C_REND_JOINED;
  144. circuit_change_purpose(TO_CIRCUIT(circ), new_circ_purpose);
  145. /* All is well. Extend the circuit. */
  146. hop->state = CPATH_STATE_OPEN;
  147. /* Set the windows to default. */
  148. hop->package_window = circuit_initial_package_window();
  149. hop->deliver_window = CIRCWINDOW_START;
  150. /* Now that this circuit has finished connecting to its destination,
  151. * make sure circuit_get_open_circ_or_launch is willing to return it
  152. * so we can actually use it. */
  153. circ->hs_circ_has_timed_out = 0;
  154. /* Append the hop to the cpath of this circuit */
  155. cpath_extend_linked_list(&circ->cpath, hop);
  156. /* In legacy code, 'pending_final_cpath' points to the final hop we just
  157. * appended to the cpath. We set the original pointer to NULL so that we
  158. * don't double free it. */
  159. if (circ->build_state) {
  160. circ->build_state->pending_final_cpath = NULL;
  161. }
  162. /* Finally, mark circuit as ready to be used for client streams */
  163. if (!is_service_side) {
  164. circuit_try_attaching_streams(circ);
  165. }
  166. }
  167. /* For a given circuit and a service introduction point object, register the
  168. * intro circuit to the circuitmap. This supports legacy intro point. */
  169. static void
  170. register_intro_circ(const hs_service_intro_point_t *ip,
  171. origin_circuit_t *circ)
  172. {
  173. tor_assert(ip);
  174. tor_assert(circ);
  175. if (ip->base.is_only_legacy) {
  176. hs_circuitmap_register_intro_circ_v2_service_side(circ,
  177. ip->legacy_key_digest);
  178. } else {
  179. hs_circuitmap_register_intro_circ_v3_service_side(circ,
  180. &ip->auth_key_kp.pubkey);
  181. }
  182. }
  183. /* Return the number of opened introduction circuit for the given circuit that
  184. * is matching its identity key. */
  185. static unsigned int
  186. count_opened_desc_intro_point_circuits(const hs_service_t *service,
  187. const hs_service_descriptor_t *desc)
  188. {
  189. unsigned int count = 0;
  190. tor_assert(service);
  191. tor_assert(desc);
  192. DIGEST256MAP_FOREACH(desc->intro_points.map, key,
  193. const hs_service_intro_point_t *, ip) {
  194. const circuit_t *circ;
  195. const origin_circuit_t *ocirc = hs_circ_service_get_intro_circ(ip);
  196. if (ocirc == NULL) {
  197. continue;
  198. }
  199. circ = TO_CIRCUIT(ocirc);
  200. tor_assert(circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  201. circ->purpose == CIRCUIT_PURPOSE_S_INTRO);
  202. /* Having a circuit not for the requested service is really bad. */
  203. tor_assert(ed25519_pubkey_eq(&service->keys.identity_pk,
  204. &ocirc->hs_ident->identity_pk));
  205. /* Only count opened circuit and skip circuit that will be closed. */
  206. if (!circ->marked_for_close && circ->state == CIRCUIT_STATE_OPEN) {
  207. count++;
  208. }
  209. } DIGEST256MAP_FOREACH_END;
  210. return count;
  211. }
  212. /* From a given service, rendezvous cookie and handshake info, create a
  213. * rendezvous point circuit identifier. This can't fail. */
  214. STATIC hs_ident_circuit_t *
  215. create_rp_circuit_identifier(const hs_service_t *service,
  216. const uint8_t *rendezvous_cookie,
  217. const curve25519_public_key_t *server_pk,
  218. const hs_ntor_rend_cell_keys_t *keys)
  219. {
  220. hs_ident_circuit_t *ident;
  221. uint8_t handshake_info[CURVE25519_PUBKEY_LEN + DIGEST256_LEN];
  222. tor_assert(service);
  223. tor_assert(rendezvous_cookie);
  224. tor_assert(server_pk);
  225. tor_assert(keys);
  226. ident = hs_ident_circuit_new(&service->keys.identity_pk,
  227. HS_IDENT_CIRCUIT_RENDEZVOUS);
  228. /* Copy the RENDEZVOUS_COOKIE which is the unique identifier. */
  229. memcpy(ident->rendezvous_cookie, rendezvous_cookie,
  230. sizeof(ident->rendezvous_cookie));
  231. /* Build the HANDSHAKE_INFO which looks like this:
  232. * SERVER_PK [32 bytes]
  233. * AUTH_INPUT_MAC [32 bytes]
  234. */
  235. memcpy(handshake_info, server_pk->public_key, CURVE25519_PUBKEY_LEN);
  236. memcpy(handshake_info + CURVE25519_PUBKEY_LEN, keys->rend_cell_auth_mac,
  237. DIGEST256_LEN);
  238. tor_assert(sizeof(ident->rendezvous_handshake_info) ==
  239. sizeof(handshake_info));
  240. memcpy(ident->rendezvous_handshake_info, handshake_info,
  241. sizeof(ident->rendezvous_handshake_info));
  242. /* Finally copy the NTOR_KEY_SEED for e2e encryption on the circuit. */
  243. tor_assert(sizeof(ident->rendezvous_ntor_key_seed) ==
  244. sizeof(keys->ntor_key_seed));
  245. memcpy(ident->rendezvous_ntor_key_seed, keys->ntor_key_seed,
  246. sizeof(ident->rendezvous_ntor_key_seed));
  247. return ident;
  248. }
  249. /* From a given service and service intro point, create an introduction point
  250. * circuit identifier. This can't fail. */
  251. static hs_ident_circuit_t *
  252. create_intro_circuit_identifier(const hs_service_t *service,
  253. const hs_service_intro_point_t *ip)
  254. {
  255. hs_ident_circuit_t *ident;
  256. tor_assert(service);
  257. tor_assert(ip);
  258. ident = hs_ident_circuit_new(&service->keys.identity_pk,
  259. HS_IDENT_CIRCUIT_INTRO);
  260. ed25519_pubkey_copy(&ident->intro_auth_pk, &ip->auth_key_kp.pubkey);
  261. return ident;
  262. }
  263. /* For a given introduction point and an introduction circuit, send the
  264. * ESTABLISH_INTRO cell. The service object is used for logging. This can fail
  265. * and if so, the circuit is closed and the intro point object is flagged
  266. * that the circuit is not established anymore which is important for the
  267. * retry mechanism. */
  268. static void
  269. send_establish_intro(const hs_service_t *service,
  270. hs_service_intro_point_t *ip, origin_circuit_t *circ)
  271. {
  272. ssize_t cell_len;
  273. uint8_t payload[RELAY_PAYLOAD_SIZE];
  274. tor_assert(service);
  275. tor_assert(ip);
  276. tor_assert(circ);
  277. /* Encode establish intro cell. */
  278. cell_len = hs_cell_build_establish_intro(circ->cpath->prev->rend_circ_nonce,
  279. &service->config, ip, payload);
  280. if (cell_len < 0) {
  281. log_warn(LD_REND, "Unable to encode ESTABLISH_INTRO cell for service %s "
  282. "on circuit %u. Closing circuit.",
  283. safe_str_client(service->onion_address),
  284. TO_CIRCUIT(circ)->n_circ_id);
  285. goto err;
  286. }
  287. /* Send the cell on the circuit. */
  288. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
  289. RELAY_COMMAND_ESTABLISH_INTRO,
  290. (char *) payload, cell_len,
  291. circ->cpath->prev) < 0) {
  292. log_info(LD_REND, "Unable to send ESTABLISH_INTRO cell for service %s "
  293. "on circuit %u.",
  294. safe_str_client(service->onion_address),
  295. TO_CIRCUIT(circ)->n_circ_id);
  296. /* On error, the circuit has been closed. */
  297. goto done;
  298. }
  299. /* Record the attempt to use this circuit. */
  300. pathbias_count_use_attempt(circ);
  301. goto done;
  302. err:
  303. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  304. done:
  305. memwipe(payload, 0, sizeof(payload));
  306. }
  307. /* Return a string constant describing the anonymity of service. */
  308. static const char *
  309. get_service_anonymity_string(const hs_service_t *service)
  310. {
  311. if (service->config.is_single_onion) {
  312. return "single onion";
  313. } else {
  314. return "hidden";
  315. }
  316. }
  317. /* For a given service, the ntor onion key and a rendezvous cookie, launch a
  318. * circuit to the rendezvous point specified by the link specifiers. On
  319. * success, a circuit identifier is attached to the circuit with the needed
  320. * data. This function will try to open a circuit for a maximum value of
  321. * MAX_REND_FAILURES then it will give up. */
  322. static void
  323. launch_rendezvous_point_circuit(const hs_service_t *service,
  324. const hs_service_intro_point_t *ip,
  325. const hs_cell_introduce2_data_t *data)
  326. {
  327. int circ_needs_uptime;
  328. time_t now = time(NULL);
  329. extend_info_t *info = NULL;
  330. origin_circuit_t *circ;
  331. tor_assert(service);
  332. tor_assert(ip);
  333. tor_assert(data);
  334. circ_needs_uptime = hs_service_requires_uptime_circ(service->config.ports);
  335. /* Get the extend info data structure for the chosen rendezvous point
  336. * specified by the given link specifiers. */
  337. info = hs_get_extend_info_from_lspecs(data->link_specifiers,
  338. &data->onion_pk,
  339. service->config.is_single_onion);
  340. if (info == NULL) {
  341. /* We are done here, we can't extend to the rendezvous point.
  342. * If you're running an IPv6-only v3 single onion service on 0.3.2 or with
  343. * 0.3.2 clients, and somehow disable the option check, it will fail here.
  344. */
  345. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  346. "Not enough info to open a circuit to a rendezvous point for "
  347. "%s service %s.",
  348. get_service_anonymity_string(service),
  349. safe_str_client(service->onion_address));
  350. goto end;
  351. }
  352. for (int i = 0; i < MAX_REND_FAILURES; i++) {
  353. int circ_flags = CIRCLAUNCH_NEED_CAPACITY | CIRCLAUNCH_IS_INTERNAL;
  354. if (circ_needs_uptime) {
  355. circ_flags |= CIRCLAUNCH_NEED_UPTIME;
  356. }
  357. /* Firewall and policies are checked when getting the extend info. */
  358. if (service->config.is_single_onion) {
  359. circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
  360. }
  361. circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND, info,
  362. circ_flags);
  363. if (circ != NULL) {
  364. /* Stop retrying, we have a circuit! */
  365. break;
  366. }
  367. }
  368. if (circ == NULL) {
  369. log_warn(LD_REND, "Giving up on launching a rendezvous circuit to %s "
  370. "for %s service %s",
  371. safe_str_client(extend_info_describe(info)),
  372. get_service_anonymity_string(service),
  373. safe_str_client(service->onion_address));
  374. goto end;
  375. }
  376. log_info(LD_REND, "Rendezvous circuit launched to %s with cookie %s "
  377. "for %s service %s",
  378. safe_str_client(extend_info_describe(info)),
  379. safe_str_client(hex_str((const char *) data->rendezvous_cookie,
  380. REND_COOKIE_LEN)),
  381. get_service_anonymity_string(service),
  382. safe_str_client(service->onion_address));
  383. tor_assert(circ->build_state);
  384. /* Rendezvous circuit have a specific timeout for the time spent on trying
  385. * to connect to the rendezvous point. */
  386. circ->build_state->expiry_time = now + MAX_REND_TIMEOUT;
  387. /* Create circuit identifier and key material. */
  388. {
  389. hs_ntor_rend_cell_keys_t keys;
  390. curve25519_keypair_t ephemeral_kp;
  391. /* No need for extra strong, this is only for this circuit life time. This
  392. * key will be used for the RENDEZVOUS1 cell that will be sent on the
  393. * circuit once opened. */
  394. curve25519_keypair_generate(&ephemeral_kp, 0);
  395. if (hs_ntor_service_get_rendezvous1_keys(&ip->auth_key_kp.pubkey,
  396. &ip->enc_key_kp,
  397. &ephemeral_kp, &data->client_pk,
  398. &keys) < 0) {
  399. /* This should not really happened but just in case, don't make tor
  400. * freak out, close the circuit and move on. */
  401. log_info(LD_REND, "Unable to get RENDEZVOUS1 key material for "
  402. "service %s",
  403. safe_str_client(service->onion_address));
  404. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  405. goto end;
  406. }
  407. circ->hs_ident = create_rp_circuit_identifier(service,
  408. data->rendezvous_cookie,
  409. &ephemeral_kp.pubkey, &keys);
  410. memwipe(&ephemeral_kp, 0, sizeof(ephemeral_kp));
  411. memwipe(&keys, 0, sizeof(keys));
  412. tor_assert(circ->hs_ident);
  413. }
  414. end:
  415. extend_info_free(info);
  416. }
  417. /* Return true iff the given service rendezvous circuit circ is allowed for a
  418. * relaunch to the rendezvous point. */
  419. static int
  420. can_relaunch_service_rendezvous_point(const origin_circuit_t *circ)
  421. {
  422. tor_assert(circ);
  423. /* This is initialized when allocating an origin circuit. */
  424. tor_assert(circ->build_state);
  425. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  426. /* XXX: Retrying under certain condition. This is related to #22455. */
  427. /* Avoid to relaunch twice a circuit to the same rendezvous point at the
  428. * same time. */
  429. if (circ->hs_service_side_rend_circ_has_been_relaunched) {
  430. log_info(LD_REND, "Rendezvous circuit to %s has already been retried. "
  431. "Skipping retry.",
  432. safe_str_client(
  433. extend_info_describe(circ->build_state->chosen_exit)));
  434. goto disallow;
  435. }
  436. /* We check failure_count >= hs_get_service_max_rend_failures()-1 below, and
  437. * the -1 is because we increment the failure count for our current failure
  438. * *after* this clause. */
  439. int max_rend_failures = hs_get_service_max_rend_failures() - 1;
  440. /* A failure count that has reached maximum allowed or circuit that expired,
  441. * we skip relaunching. */
  442. if (circ->build_state->failure_count > max_rend_failures ||
  443. circ->build_state->expiry_time <= time(NULL)) {
  444. log_info(LD_REND, "Attempt to build a rendezvous circuit to %s has "
  445. "failed with %d attempts and expiry time %ld. "
  446. "Giving up building.",
  447. safe_str_client(
  448. extend_info_describe(circ->build_state->chosen_exit)),
  449. circ->build_state->failure_count,
  450. (long int) circ->build_state->expiry_time);
  451. goto disallow;
  452. }
  453. /* Allowed to relaunch. */
  454. return 1;
  455. disallow:
  456. return 0;
  457. }
  458. /* Retry the rendezvous point of circ by launching a new circuit to it. */
  459. static void
  460. retry_service_rendezvous_point(const origin_circuit_t *circ)
  461. {
  462. int flags = 0;
  463. origin_circuit_t *new_circ;
  464. cpath_build_state_t *bstate;
  465. tor_assert(circ);
  466. /* This is initialized when allocating an origin circuit. */
  467. tor_assert(circ->build_state);
  468. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  469. /* Ease our life. */
  470. bstate = circ->build_state;
  471. log_info(LD_REND, "Retrying rendezvous point circuit to %s",
  472. safe_str_client(extend_info_describe(bstate->chosen_exit)));
  473. /* Get the current build state flags for the next circuit. */
  474. flags |= (bstate->need_uptime) ? CIRCLAUNCH_NEED_UPTIME : 0;
  475. flags |= (bstate->need_capacity) ? CIRCLAUNCH_NEED_CAPACITY : 0;
  476. flags |= (bstate->is_internal) ? CIRCLAUNCH_IS_INTERNAL : 0;
  477. /* We do NOT add the onehop tunnel flag even though it might be a single
  478. * onion service. The reason is that if we failed once to connect to the RP
  479. * with a direct connection, we consider that chances are that we will fail
  480. * again so try a 3-hop circuit and hope for the best. Because the service
  481. * has no anonymity (single onion), this change of behavior won't affect
  482. * security directly. */
  483. new_circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND,
  484. bstate->chosen_exit, flags);
  485. if (new_circ == NULL) {
  486. log_warn(LD_REND, "Failed to launch rendezvous circuit to %s",
  487. safe_str_client(extend_info_describe(bstate->chosen_exit)));
  488. goto done;
  489. }
  490. /* Transfer build state information to the new circuit state in part to
  491. * catch any other failures. */
  492. new_circ->build_state->failure_count = bstate->failure_count+1;
  493. new_circ->build_state->expiry_time = bstate->expiry_time;
  494. new_circ->hs_ident = hs_ident_circuit_dup(circ->hs_ident);
  495. done:
  496. return;
  497. }
  498. /* Using the given descriptor intro point ip, the node of the
  499. * rendezvous point rp_node and the service's subcredential, populate the
  500. * already allocated intro1_data object with the needed key material and link
  501. * specifiers.
  502. *
  503. * Return 0 on success or a negative value if we couldn't properly filled the
  504. * introduce1 data from the RP node. In other word, it means the RP node is
  505. * unusable to use in the introduction. */
  506. static int
  507. setup_introduce1_data(const hs_desc_intro_point_t *ip,
  508. const node_t *rp_node,
  509. const uint8_t *subcredential,
  510. hs_cell_introduce1_data_t *intro1_data)
  511. {
  512. int ret = -1;
  513. smartlist_t *rp_lspecs;
  514. tor_assert(ip);
  515. tor_assert(rp_node);
  516. tor_assert(subcredential);
  517. tor_assert(intro1_data);
  518. /* Build the link specifiers from the node at the end of the rendezvous
  519. * circuit that we opened for this introduction. */
  520. rp_lspecs = node_get_link_specifier_smartlist(rp_node, 0);
  521. if (smartlist_len(rp_lspecs) == 0) {
  522. /* We can't rendezvous without link specifiers. */
  523. smartlist_free(rp_lspecs);
  524. goto end;
  525. }
  526. /* Populate the introduce1 data object. */
  527. memset(intro1_data, 0, sizeof(hs_cell_introduce1_data_t));
  528. if (ip->legacy.key != NULL) {
  529. intro1_data->is_legacy = 1;
  530. intro1_data->legacy_key = ip->legacy.key;
  531. }
  532. intro1_data->auth_pk = &ip->auth_key_cert->signed_key;
  533. intro1_data->enc_pk = &ip->enc_key;
  534. intro1_data->subcredential = subcredential;
  535. intro1_data->link_specifiers = rp_lspecs;
  536. intro1_data->onion_pk = node_get_curve25519_onion_key(rp_node);
  537. if (intro1_data->onion_pk == NULL) {
  538. /* We can't rendezvous without the curve25519 onion key. */
  539. goto end;
  540. }
  541. /* Success, we have valid introduce data. */
  542. ret = 0;
  543. end:
  544. return ret;
  545. }
  546. /* ========== */
  547. /* Public API */
  548. /* ========== */
  549. /* Return an introduction point circuit matching the given intro point object.
  550. * NULL is returned is no such circuit can be found. */
  551. origin_circuit_t *
  552. hs_circ_service_get_intro_circ(const hs_service_intro_point_t *ip)
  553. {
  554. tor_assert(ip);
  555. if (ip->base.is_only_legacy) {
  556. return hs_circuitmap_get_intro_circ_v2_service_side(ip->legacy_key_digest);
  557. } else {
  558. return hs_circuitmap_get_intro_circ_v3_service_side(
  559. &ip->auth_key_kp.pubkey);
  560. }
  561. }
  562. /* Called when we fail building a rendezvous circuit at some point other than
  563. * the last hop: launches a new circuit to the same rendezvous point. This
  564. * supports legacy service.
  565. *
  566. * We currently relaunch connections to rendezvous points if:
  567. * - A rendezvous circuit timed out before connecting to RP.
  568. * - The rendezvous circuit failed to connect to the RP.
  569. *
  570. * We avoid relaunching a connection to this rendezvous point if:
  571. * - We have already tried MAX_REND_FAILURES times to connect to this RP,
  572. * - We've been trying to connect to this RP for more than MAX_REND_TIMEOUT
  573. * seconds, or
  574. * - We've already retried this specific rendezvous circuit.
  575. */
  576. void
  577. hs_circ_retry_service_rendezvous_point(origin_circuit_t *circ)
  578. {
  579. tor_assert(circ);
  580. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  581. /* Check if we are allowed to relaunch to the rendezvous point of circ. */
  582. if (!can_relaunch_service_rendezvous_point(circ)) {
  583. goto done;
  584. }
  585. /* Flag the circuit that we are relaunching, to avoid to relaunch twice a
  586. * circuit to the same rendezvous point at the same time. */
  587. circ->hs_service_side_rend_circ_has_been_relaunched = 1;
  588. /* Legacy services don't have a hidden service ident. */
  589. if (circ->hs_ident) {
  590. retry_service_rendezvous_point(circ);
  591. } else {
  592. rend_service_relaunch_rendezvous(circ);
  593. }
  594. done:
  595. return;
  596. }
  597. /* For a given service and a service intro point, launch a circuit to the
  598. * extend info ei. If the service is a single onion, a one-hop circuit will be
  599. * requested. Return 0 if the circuit was successfully launched and tagged
  600. * with the correct identifier. On error, a negative value is returned. */
  601. int
  602. hs_circ_launch_intro_point(hs_service_t *service,
  603. const hs_service_intro_point_t *ip,
  604. extend_info_t *ei)
  605. {
  606. /* Standard flags for introduction circuit. */
  607. int ret = -1, circ_flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  608. origin_circuit_t *circ;
  609. tor_assert(service);
  610. tor_assert(ip);
  611. tor_assert(ei);
  612. /* Update circuit flags in case of a single onion service that requires a
  613. * direct connection. */
  614. if (service->config.is_single_onion) {
  615. circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
  616. }
  617. log_info(LD_REND, "Launching a circuit to intro point %s for service %s.",
  618. safe_str_client(extend_info_describe(ei)),
  619. safe_str_client(service->onion_address));
  620. /* Note down the launch for the retry period. Even if the circuit fails to
  621. * be launched, we still want to respect the retry period to avoid stress on
  622. * the circuit subsystem. */
  623. service->state.num_intro_circ_launched++;
  624. circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  625. ei, circ_flags);
  626. if (circ == NULL) {
  627. goto end;
  628. }
  629. /* Setup the circuit identifier and attach it to it. */
  630. circ->hs_ident = create_intro_circuit_identifier(service, ip);
  631. tor_assert(circ->hs_ident);
  632. /* Register circuit in the global circuitmap. */
  633. register_intro_circ(ip, circ);
  634. /* Success. */
  635. ret = 0;
  636. end:
  637. return ret;
  638. }
  639. /* Called when a service introduction point circuit is done building. Given
  640. * the service and intro point object, this function will send the
  641. * ESTABLISH_INTRO cell on the circuit. Return 0 on success. Return 1 if the
  642. * circuit has been repurposed to General because we already have too many
  643. * opened. */
  644. int
  645. hs_circ_service_intro_has_opened(hs_service_t *service,
  646. hs_service_intro_point_t *ip,
  647. const hs_service_descriptor_t *desc,
  648. origin_circuit_t *circ)
  649. {
  650. int ret = 0;
  651. unsigned int num_intro_circ, num_needed_circ;
  652. tor_assert(service);
  653. tor_assert(ip);
  654. tor_assert(desc);
  655. tor_assert(circ);
  656. /* Cound opened circuits that have sent ESTABLISH_INTRO cells or are already
  657. * established introduction circuits */
  658. num_intro_circ = count_opened_desc_intro_point_circuits(service, desc);
  659. num_needed_circ = service->config.num_intro_points;
  660. if (num_intro_circ > num_needed_circ) {
  661. /* There are too many opened valid intro circuit for what the service
  662. * needs so repurpose this one. */
  663. /* XXX: Legacy code checks options->ExcludeNodes and if not NULL it just
  664. * closes the circuit. I have NO idea why it does that so it hasn't been
  665. * added here. I can only assume in case our ExcludeNodes list changes but
  666. * in that case, all circuit are flagged unusable (config.c). --dgoulet */
  667. log_info(LD_CIRC | LD_REND, "Introduction circuit just opened but we "
  668. "have enough for service %s. Repurposing "
  669. "it to general and leaving internal.",
  670. safe_str_client(service->onion_address));
  671. tor_assert(circ->build_state->is_internal);
  672. /* Remove it from the circuitmap. */
  673. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  674. /* Cleaning up the hidden service identifier and repurpose. */
  675. hs_ident_circuit_free(circ->hs_ident);
  676. circ->hs_ident = NULL;
  677. if (circuit_should_use_vanguards(TO_CIRCUIT(circ)->purpose))
  678. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_HS_VANGUARDS);
  679. else
  680. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_GENERAL);
  681. /* Inform that this circuit just opened for this new purpose. */
  682. circuit_has_opened(circ);
  683. /* This return value indicate to the caller that the IP object should be
  684. * removed from the service because it's corresponding circuit has just
  685. * been repurposed. */
  686. ret = 1;
  687. goto done;
  688. }
  689. log_info(LD_REND, "Introduction circuit %u established for service %s.",
  690. TO_CIRCUIT(circ)->n_circ_id,
  691. safe_str_client(service->onion_address));
  692. circuit_log_path(LOG_INFO, LD_REND, circ);
  693. /* Time to send an ESTABLISH_INTRO cell on this circuit. On error, this call
  694. * makes sure the circuit gets closed. */
  695. send_establish_intro(service, ip, circ);
  696. done:
  697. return ret;
  698. }
  699. /* Called when a service rendezvous point circuit is done building. Given the
  700. * service and the circuit, this function will send a RENDEZVOUS1 cell on the
  701. * circuit using the information in the circuit identifier. If the cell can't
  702. * be sent, the circuit is closed. */
  703. void
  704. hs_circ_service_rp_has_opened(const hs_service_t *service,
  705. origin_circuit_t *circ)
  706. {
  707. size_t payload_len;
  708. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  709. tor_assert(service);
  710. tor_assert(circ);
  711. tor_assert(circ->hs_ident);
  712. /* Some useful logging. */
  713. log_info(LD_REND, "Rendezvous circuit %u has opened with cookie %s "
  714. "for service %s",
  715. TO_CIRCUIT(circ)->n_circ_id,
  716. hex_str((const char *) circ->hs_ident->rendezvous_cookie,
  717. REND_COOKIE_LEN),
  718. safe_str_client(service->onion_address));
  719. circuit_log_path(LOG_INFO, LD_REND, circ);
  720. /* This can't fail. */
  721. payload_len = hs_cell_build_rendezvous1(
  722. circ->hs_ident->rendezvous_cookie,
  723. sizeof(circ->hs_ident->rendezvous_cookie),
  724. circ->hs_ident->rendezvous_handshake_info,
  725. sizeof(circ->hs_ident->rendezvous_handshake_info),
  726. payload);
  727. /* Pad the payload with random bytes so it matches the size of a legacy cell
  728. * which is normally always bigger. Also, the size of a legacy cell is
  729. * always smaller than the RELAY_PAYLOAD_SIZE so this is safe. */
  730. if (payload_len < HS_LEGACY_RENDEZVOUS_CELL_SIZE) {
  731. crypto_rand((char *) payload + payload_len,
  732. HS_LEGACY_RENDEZVOUS_CELL_SIZE - payload_len);
  733. payload_len = HS_LEGACY_RENDEZVOUS_CELL_SIZE;
  734. }
  735. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
  736. RELAY_COMMAND_RENDEZVOUS1,
  737. (const char *) payload, payload_len,
  738. circ->cpath->prev) < 0) {
  739. /* On error, circuit is closed. */
  740. log_warn(LD_REND, "Unable to send RENDEZVOUS1 cell on circuit %u "
  741. "for service %s",
  742. TO_CIRCUIT(circ)->n_circ_id,
  743. safe_str_client(service->onion_address));
  744. goto done;
  745. }
  746. /* Setup end-to-end rendezvous circuit between the client and us. */
  747. if (hs_circuit_setup_e2e_rend_circ(circ,
  748. circ->hs_ident->rendezvous_ntor_key_seed,
  749. sizeof(circ->hs_ident->rendezvous_ntor_key_seed),
  750. 1) < 0) {
  751. log_warn(LD_GENERAL, "Failed to setup circ");
  752. goto done;
  753. }
  754. done:
  755. memwipe(payload, 0, sizeof(payload));
  756. }
  757. /* Circ has been expecting an INTRO_ESTABLISHED cell that just arrived. Handle
  758. * the INTRO_ESTABLISHED cell payload of length payload_len arriving on the
  759. * given introduction circuit circ. The service is only used for logging
  760. * purposes. Return 0 on success else a negative value. */
  761. int
  762. hs_circ_handle_intro_established(const hs_service_t *service,
  763. const hs_service_intro_point_t *ip,
  764. origin_circuit_t *circ,
  765. const uint8_t *payload, size_t payload_len)
  766. {
  767. int ret = -1;
  768. tor_assert(service);
  769. tor_assert(ip);
  770. tor_assert(circ);
  771. tor_assert(payload);
  772. if (BUG(TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)) {
  773. goto done;
  774. }
  775. /* Try to parse the payload into a cell making sure we do actually have a
  776. * valid cell. For a legacy node, it's an empty payload so as long as we
  777. * have the cell, we are good. */
  778. if (!ip->base.is_only_legacy &&
  779. hs_cell_parse_intro_established(payload, payload_len) < 0) {
  780. log_warn(LD_REND, "Unable to parse the INTRO_ESTABLISHED cell on "
  781. "circuit %u for service %s",
  782. TO_CIRCUIT(circ)->n_circ_id,
  783. safe_str_client(service->onion_address));
  784. goto done;
  785. }
  786. /* Switch the purpose to a fully working intro point. */
  787. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_S_INTRO);
  788. /* Getting a valid INTRODUCE_ESTABLISHED means we've successfully used the
  789. * circuit so update our pathbias subsystem. */
  790. pathbias_mark_use_success(circ);
  791. /* Success. */
  792. ret = 0;
  793. done:
  794. return ret;
  795. }
  796. /* We just received an INTRODUCE2 cell on the established introduction circuit
  797. * circ. Handle the INTRODUCE2 payload of size payload_len for the given
  798. * circuit and service. This cell is associated with the intro point object ip
  799. * and the subcredential. Return 0 on success else a negative value. */
  800. int
  801. hs_circ_handle_introduce2(const hs_service_t *service,
  802. const origin_circuit_t *circ,
  803. hs_service_intro_point_t *ip,
  804. const uint8_t *subcredential,
  805. const uint8_t *payload, size_t payload_len)
  806. {
  807. int ret = -1;
  808. time_t elapsed;
  809. hs_cell_introduce2_data_t data;
  810. tor_assert(service);
  811. tor_assert(circ);
  812. tor_assert(ip);
  813. tor_assert(subcredential);
  814. tor_assert(payload);
  815. /* Populate the data structure with everything we need for the cell to be
  816. * parsed, decrypted and key material computed correctly. */
  817. data.auth_pk = &ip->auth_key_kp.pubkey;
  818. data.enc_kp = &ip->enc_key_kp;
  819. data.subcredential = subcredential;
  820. data.payload = payload;
  821. data.payload_len = payload_len;
  822. data.link_specifiers = smartlist_new();
  823. data.replay_cache = ip->replay_cache;
  824. if (hs_cell_parse_introduce2(&data, circ, service) < 0) {
  825. goto done;
  826. }
  827. /* Check whether we've seen this REND_COOKIE before to detect repeats. */
  828. if (replaycache_add_test_and_elapsed(
  829. service->state.replay_cache_rend_cookie,
  830. data.rendezvous_cookie, sizeof(data.rendezvous_cookie),
  831. &elapsed)) {
  832. /* A Tor client will send a new INTRODUCE1 cell with the same REND_COOKIE
  833. * as its previous one if its intro circ times out while in state
  834. * CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT. If we received the first
  835. * INTRODUCE1 cell (the intro-point relay converts it into an INTRODUCE2
  836. * cell), we are already trying to connect to that rend point (and may
  837. * have already succeeded); drop this cell. */
  838. log_info(LD_REND, "We received an INTRODUCE2 cell with same REND_COOKIE "
  839. "field %ld seconds ago. Dropping cell.",
  840. (long int) elapsed);
  841. goto done;
  842. }
  843. /* At this point, we just confirmed that the full INTRODUCE2 cell is valid
  844. * so increment our counter that we've seen one on this intro point. */
  845. ip->introduce2_count++;
  846. /* Launch rendezvous circuit with the onion key and rend cookie. */
  847. launch_rendezvous_point_circuit(service, ip, &data);
  848. /* Success. */
  849. ret = 0;
  850. done:
  851. link_specifier_smartlist_free(data.link_specifiers);
  852. memwipe(&data, 0, sizeof(data));
  853. return ret;
  854. }
  855. /* Circuit <b>circ</b> just finished the rend ntor key exchange. Use the key
  856. * exchange output material at <b>ntor_key_seed</b> and setup <b>circ</b> to
  857. * serve as a rendezvous end-to-end circuit between the client and the
  858. * service. If <b>is_service_side</b> is set, then we are the hidden service
  859. * and the other side is the client.
  860. *
  861. * Return 0 if the operation went well; in case of error return -1. */
  862. int
  863. hs_circuit_setup_e2e_rend_circ(origin_circuit_t *circ,
  864. const uint8_t *ntor_key_seed, size_t seed_len,
  865. int is_service_side)
  866. {
  867. if (BUG(!circuit_purpose_is_correct_for_rend(TO_CIRCUIT(circ)->purpose,
  868. is_service_side))) {
  869. return -1;
  870. }
  871. crypt_path_t *hop = create_rend_cpath(ntor_key_seed, seed_len,
  872. is_service_side);
  873. if (!hop) {
  874. log_warn(LD_REND, "Couldn't get v3 %s cpath!",
  875. is_service_side ? "service-side" : "client-side");
  876. return -1;
  877. }
  878. finalize_rend_circuit(circ, hop, is_service_side);
  879. return 0;
  880. }
  881. /* We are a v2 legacy HS client and we just received a RENDEZVOUS1 cell
  882. * <b>rend_cell_body</b> on <b>circ</b>. Finish up the DH key exchange and then
  883. * extend the crypt path of <b>circ</b> so that the hidden service is on the
  884. * other side. */
  885. int
  886. hs_circuit_setup_e2e_rend_circ_legacy_client(origin_circuit_t *circ,
  887. const uint8_t *rend_cell_body)
  888. {
  889. if (BUG(!circuit_purpose_is_correct_for_rend(
  890. TO_CIRCUIT(circ)->purpose, 0))) {
  891. return -1;
  892. }
  893. crypt_path_t *hop = create_rend_cpath_legacy(circ, rend_cell_body);
  894. if (!hop) {
  895. log_warn(LD_GENERAL, "Couldn't get v2 cpath.");
  896. return -1;
  897. }
  898. finalize_rend_circuit(circ, hop, 0);
  899. return 0;
  900. }
  901. /* Given the introduction circuit intro_circ, the rendezvous circuit
  902. * rend_circ, a descriptor intro point object ip and the service's
  903. * subcredential, send an INTRODUCE1 cell on intro_circ.
  904. *
  905. * This will also setup the circuit identifier on rend_circ containing the key
  906. * material for the handshake and e2e encryption. Return 0 on success else
  907. * negative value. Because relay_send_command_from_edge() closes the circuit
  908. * on error, it is possible that intro_circ is closed on error. */
  909. int
  910. hs_circ_send_introduce1(origin_circuit_t *intro_circ,
  911. origin_circuit_t *rend_circ,
  912. const hs_desc_intro_point_t *ip,
  913. const uint8_t *subcredential)
  914. {
  915. int ret = -1;
  916. ssize_t payload_len;
  917. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  918. hs_cell_introduce1_data_t intro1_data;
  919. tor_assert(intro_circ);
  920. tor_assert(rend_circ);
  921. tor_assert(ip);
  922. tor_assert(subcredential);
  923. /* It is undefined behavior in hs_cell_introduce1_data_clear() if intro1_data
  924. * has been declared on the stack but not initialized. Here, we set it to 0.
  925. */
  926. memset(&intro1_data, 0, sizeof(hs_cell_introduce1_data_t));
  927. /* This takes various objects in order to populate the introduce1 data
  928. * object which is used to build the content of the cell. */
  929. const node_t *exit_node = build_state_get_exit_node(rend_circ->build_state);
  930. if (exit_node == NULL) {
  931. log_info(LD_REND, "Unable to get rendezvous point for circuit %u. "
  932. "Failing.", TO_CIRCUIT(intro_circ)->n_circ_id);
  933. goto done;
  934. }
  935. /* We should never select an invalid rendezvous point in theory but if we
  936. * do, this function will fail to populate the introduce data. */
  937. if (setup_introduce1_data(ip, exit_node, subcredential, &intro1_data) < 0) {
  938. log_warn(LD_REND, "Unable to setup INTRODUCE1 data. The chosen rendezvous "
  939. "point is unusable. Closing circuit.");
  940. goto close;
  941. }
  942. /* Final step before we encode a cell, we setup the circuit identifier which
  943. * will generate both the rendezvous cookie and client keypair for this
  944. * connection. Those are put in the ident. */
  945. intro1_data.rendezvous_cookie = rend_circ->hs_ident->rendezvous_cookie;
  946. intro1_data.client_kp = &rend_circ->hs_ident->rendezvous_client_kp;
  947. memcpy(intro_circ->hs_ident->rendezvous_cookie,
  948. rend_circ->hs_ident->rendezvous_cookie,
  949. sizeof(intro_circ->hs_ident->rendezvous_cookie));
  950. /* From the introduce1 data object, this will encode the INTRODUCE1 cell
  951. * into payload which is then ready to be sent as is. */
  952. payload_len = hs_cell_build_introduce1(&intro1_data, payload);
  953. if (BUG(payload_len < 0)) {
  954. goto close;
  955. }
  956. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(intro_circ),
  957. RELAY_COMMAND_INTRODUCE1,
  958. (const char *) payload, payload_len,
  959. intro_circ->cpath->prev) < 0) {
  960. /* On error, circuit is closed. */
  961. log_warn(LD_REND, "Unable to send INTRODUCE1 cell on circuit %u.",
  962. TO_CIRCUIT(intro_circ)->n_circ_id);
  963. goto done;
  964. }
  965. /* Success. */
  966. ret = 0;
  967. goto done;
  968. close:
  969. circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_INTERNAL);
  970. done:
  971. hs_cell_introduce1_data_clear(&intro1_data);
  972. memwipe(payload, 0, sizeof(payload));
  973. return ret;
  974. }
  975. /* Send an ESTABLISH_RENDEZVOUS cell along the rendezvous circuit circ. On
  976. * success, 0 is returned else -1 and the circuit is marked for close. */
  977. int
  978. hs_circ_send_establish_rendezvous(origin_circuit_t *circ)
  979. {
  980. ssize_t cell_len = 0;
  981. uint8_t cell[RELAY_PAYLOAD_SIZE] = {0};
  982. tor_assert(circ);
  983. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  984. log_info(LD_REND, "Send an ESTABLISH_RENDEZVOUS cell on circuit %u",
  985. TO_CIRCUIT(circ)->n_circ_id);
  986. /* Set timestamp_dirty, because circuit_expire_building expects it,
  987. * and the rend cookie also means we've used the circ. */
  988. TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
  989. /* We've attempted to use this circuit. Probe it if we fail */
  990. pathbias_count_use_attempt(circ);
  991. /* Generate the RENDEZVOUS_COOKIE and place it in the identifier so we can
  992. * complete the handshake when receiving the acknowledgement. */
  993. crypto_rand((char *) circ->hs_ident->rendezvous_cookie, HS_REND_COOKIE_LEN);
  994. /* Generate the client keypair. No need to be extra strong, not long term */
  995. curve25519_keypair_generate(&circ->hs_ident->rendezvous_client_kp, 0);
  996. cell_len =
  997. hs_cell_build_establish_rendezvous(circ->hs_ident->rendezvous_cookie,
  998. cell);
  999. if (BUG(cell_len < 0)) {
  1000. goto err;
  1001. }
  1002. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
  1003. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  1004. (const char *) cell, cell_len,
  1005. circ->cpath->prev) < 0) {
  1006. /* Circuit has been marked for close */
  1007. log_warn(LD_REND, "Unable to send ESTABLISH_RENDEZVOUS cell on "
  1008. "circuit %u", TO_CIRCUIT(circ)->n_circ_id);
  1009. memwipe(cell, 0, cell_len);
  1010. goto err;
  1011. }
  1012. memwipe(cell, 0, cell_len);
  1013. return 0;
  1014. err:
  1015. return -1;
  1016. }
  1017. /* We are about to close or free this <b>circ</b>. Clean it up from any
  1018. * related HS data structures. This function can be called multiple times
  1019. * safely for the same circuit. */
  1020. void
  1021. hs_circ_cleanup(circuit_t *circ)
  1022. {
  1023. tor_assert(circ);
  1024. /* If it's a service-side intro circ, notify the HS subsystem for the intro
  1025. * point circuit closing so it can be dealt with cleanly. */
  1026. if (circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  1027. circ->purpose == CIRCUIT_PURPOSE_S_INTRO) {
  1028. hs_service_intro_circ_has_closed(TO_ORIGIN_CIRCUIT(circ));
  1029. }
  1030. /* Clear HS circuitmap token for this circ (if any). Very important to be
  1031. * done after the HS subsystem has been notified of the close else the
  1032. * circuit will not be found.
  1033. *
  1034. * We do this at the close if possible because from that point on, the
  1035. * circuit is good as dead. We can't rely on removing it in the circuit
  1036. * free() function because we open a race window between the close and free
  1037. * where we can't register a new circuit for the same intro point. */
  1038. if (circ->hs_token) {
  1039. hs_circuitmap_remove_circuit(circ);
  1040. }
  1041. }