hs_circuit.c 38 KB

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