hs_circuit.c 46 KB

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