hs_circuit.c 44 KB

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