hs_circuit.c 43 KB

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