hs_circuit.c 44 KB

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