hs_client.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_client.c
  5. * \brief Implement next generation hidden service client functionality
  6. **/
  7. #include "or.h"
  8. #include "hs_circuit.h"
  9. #include "hs_ident.h"
  10. #include "connection_edge.h"
  11. #include "container.h"
  12. #include "rendclient.h"
  13. #include "hs_descriptor.h"
  14. #include "hs_cache.h"
  15. #include "hs_cell.h"
  16. #include "hs_ident.h"
  17. #include "config.h"
  18. #include "directory.h"
  19. #include "hs_client.h"
  20. #include "router.h"
  21. #include "routerset.h"
  22. #include "circuitlist.h"
  23. #include "circuituse.h"
  24. #include "connection.h"
  25. #include "circpathbias.h"
  26. #include "connection.h"
  27. #include "hs_ntor.h"
  28. #include "circuitbuild.h"
  29. /* Get all connections that are waiting on a circuit and flag them back to
  30. * waiting for a hidden service descriptor for the given service key
  31. * service_identity_pk. */
  32. static void
  33. flag_all_conn_wait_desc(const ed25519_public_key_t *service_identity_pk)
  34. {
  35. tor_assert(service_identity_pk);
  36. smartlist_t *conns =
  37. connection_list_by_type_state(CONN_TYPE_AP, AP_CONN_STATE_CIRCUIT_WAIT);
  38. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  39. edge_connection_t *edge_conn;
  40. if (BUG(!CONN_IS_EDGE(conn))) {
  41. continue;
  42. }
  43. edge_conn = TO_EDGE_CONN(conn);
  44. if (edge_conn->hs_ident &&
  45. ed25519_pubkey_eq(&edge_conn->hs_ident->identity_pk,
  46. service_identity_pk)) {
  47. connection_ap_mark_as_non_pending_circuit(TO_ENTRY_CONN(conn));
  48. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  49. }
  50. } SMARTLIST_FOREACH_END(conn);
  51. smartlist_free(conns);
  52. }
  53. /* Remove tracked HSDir requests from our history for this hidden service
  54. * identity public key. */
  55. static void
  56. purge_hid_serv_request(const ed25519_public_key_t *identity_pk)
  57. {
  58. char base64_blinded_pk[ED25519_BASE64_LEN + 1];
  59. ed25519_public_key_t blinded_pk;
  60. tor_assert(identity_pk);
  61. /* Get blinded pubkey of hidden service. It is possible that we just moved
  62. * to a new time period meaning that we won't be able to purge the request
  63. * from the previous time period. That is fine because they will expire at
  64. * some point and we don't care about those anymore. */
  65. hs_build_blinded_pubkey(identity_pk, NULL, 0,
  66. hs_get_time_period_num(approx_time()), &blinded_pk);
  67. if (BUG(ed25519_public_to_base64(base64_blinded_pk, &blinded_pk) < 0)) {
  68. return;
  69. }
  70. /* Purge last hidden service request from cache for this blinded key. */
  71. hs_purge_hid_serv_from_last_hid_serv_requests(base64_blinded_pk);
  72. }
  73. /* A v3 HS circuit successfully connected to the hidden service. Update the
  74. * stream state at <b>hs_conn_ident</b> appropriately. */
  75. static void
  76. note_connection_attempt_succeeded(const hs_ident_edge_conn_t *hs_conn_ident)
  77. {
  78. tor_assert(hs_conn_ident);
  79. /* Remove from the hid serv cache all requests for that service so we can
  80. * query the HSDir again later on for various reasons. */
  81. purge_hid_serv_request(&hs_conn_ident->identity_pk);
  82. /* The v2 subsystem cleans up the intro point time out flag at this stage.
  83. * We don't try to do it here because we still need to keep intact the intro
  84. * point state for future connections. Even though we are able to connect to
  85. * the service, doesn't mean we should reset the timed out intro points.
  86. *
  87. * It is not possible to have successfully connected to an intro point
  88. * present in our cache that was on error or timed out. Every entry in that
  89. * cache have a 2 minutes lifetime so ultimately the intro point(s) state
  90. * will be reset and thus possible to be retried. */
  91. }
  92. /* Given the pubkey of a hidden service in <b>onion_identity_pk</b>, fetch its
  93. * descriptor by launching a dir connection to <b>hsdir</b>. Return 1 on
  94. * success or -1 on error. */
  95. static int
  96. directory_launch_v3_desc_fetch(const ed25519_public_key_t *onion_identity_pk,
  97. const routerstatus_t *hsdir)
  98. {
  99. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  100. ed25519_public_key_t blinded_pubkey;
  101. char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
  102. hs_ident_dir_conn_t hs_conn_dir_ident;
  103. int retval;
  104. tor_assert(hsdir);
  105. tor_assert(onion_identity_pk);
  106. /* Get blinded pubkey */
  107. hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
  108. current_time_period, &blinded_pubkey);
  109. /* ...and base64 it. */
  110. retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
  111. if (BUG(retval < 0)) {
  112. return -1;
  113. }
  114. /* Copy onion pk to a dir_ident so that we attach it to the dir conn */
  115. ed25519_pubkey_copy(&hs_conn_dir_ident.identity_pk, onion_identity_pk);
  116. /* Setup directory request */
  117. directory_request_t *req =
  118. directory_request_new(DIR_PURPOSE_FETCH_HSDESC);
  119. directory_request_set_routerstatus(req, hsdir);
  120. directory_request_set_indirection(req, DIRIND_ANONYMOUS);
  121. directory_request_set_resource(req, base64_blinded_pubkey);
  122. directory_request_fetch_set_hs_ident(req, &hs_conn_dir_ident);
  123. directory_initiate_request(req);
  124. directory_request_free(req);
  125. log_info(LD_REND, "Descriptor fetch request for service %s with blinded "
  126. "key %s to directory %s",
  127. safe_str_client(ed25519_fmt(onion_identity_pk)),
  128. safe_str_client(base64_blinded_pubkey),
  129. safe_str_client(routerstatus_describe(hsdir)));
  130. /* Cleanup memory. */
  131. memwipe(&blinded_pubkey, 0, sizeof(blinded_pubkey));
  132. memwipe(base64_blinded_pubkey, 0, sizeof(base64_blinded_pubkey));
  133. memwipe(&hs_conn_dir_ident, 0, sizeof(hs_conn_dir_ident));
  134. return 1;
  135. }
  136. /** Return the HSDir we should use to fetch the descriptor of the hidden
  137. * service with identity key <b>onion_identity_pk</b>. */
  138. static routerstatus_t *
  139. pick_hsdir_v3(const ed25519_public_key_t *onion_identity_pk)
  140. {
  141. int retval;
  142. char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
  143. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  144. smartlist_t *responsible_hsdirs;
  145. ed25519_public_key_t blinded_pubkey;
  146. routerstatus_t *hsdir_rs = NULL;
  147. tor_assert(onion_identity_pk);
  148. responsible_hsdirs = smartlist_new();
  149. /* Get blinded pubkey of hidden service */
  150. hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
  151. current_time_period, &blinded_pubkey);
  152. /* ...and base64 it. */
  153. retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
  154. if (BUG(retval < 0)) {
  155. return NULL;
  156. }
  157. /* Get responsible hsdirs of service for this time period */
  158. hs_get_responsible_hsdirs(&blinded_pubkey, current_time_period, 0, 1,
  159. responsible_hsdirs);
  160. log_debug(LD_REND, "Found %d responsible HSDirs and about to pick one.",
  161. smartlist_len(responsible_hsdirs));
  162. /* Pick an HSDir from the responsible ones. The ownership of
  163. * responsible_hsdirs is given to this function so no need to free it. */
  164. hsdir_rs = hs_pick_hsdir(responsible_hsdirs, base64_blinded_pubkey);
  165. return hsdir_rs;
  166. }
  167. /** Fetch a v3 descriptor using the given <b>onion_identity_pk</b>.
  168. *
  169. * On success, 1 is returned. If no hidden service is left to ask, return 0.
  170. * On error, -1 is returned. */
  171. static int
  172. fetch_v3_desc(const ed25519_public_key_t *onion_identity_pk)
  173. {
  174. routerstatus_t *hsdir_rs =NULL;
  175. tor_assert(onion_identity_pk);
  176. hsdir_rs = pick_hsdir_v3(onion_identity_pk);
  177. if (!hsdir_rs) {
  178. log_info(LD_REND, "Couldn't pick a v3 hsdir.");
  179. return 0;
  180. }
  181. return directory_launch_v3_desc_fetch(onion_identity_pk, hsdir_rs);
  182. }
  183. /* Make sure that the given v3 origin circuit circ is a valid correct
  184. * introduction circuit. This will BUG() on any problems and hard assert if
  185. * the anonymity of the circuit is not ok. Return 0 on success else -1 where
  186. * the circuit should be mark for closed immediately. */
  187. static int
  188. intro_circ_is_ok(const origin_circuit_t *circ)
  189. {
  190. int ret = 0;
  191. tor_assert(circ);
  192. if (BUG(TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
  193. TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
  194. TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACKED)) {
  195. ret = -1;
  196. }
  197. if (BUG(circ->hs_ident == NULL)) {
  198. ret = -1;
  199. }
  200. if (BUG(!hs_ident_intro_circ_is_valid(circ->hs_ident))) {
  201. ret = -1;
  202. }
  203. /* This can stop the tor daemon but we want that since if we don't have
  204. * anonymity on this circuit, something went really wrong. */
  205. assert_circ_anonymity_ok(circ, get_options());
  206. return ret;
  207. }
  208. /* Find a descriptor intro point object that matches the given ident in the
  209. * given descriptor desc. Return NULL if not found. */
  210. static const hs_desc_intro_point_t *
  211. find_desc_intro_point_by_ident(const hs_ident_circuit_t *ident,
  212. const hs_descriptor_t *desc)
  213. {
  214. const hs_desc_intro_point_t *intro_point = NULL;
  215. tor_assert(ident);
  216. tor_assert(desc);
  217. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  218. const hs_desc_intro_point_t *, ip) {
  219. if (ed25519_pubkey_eq(&ident->intro_auth_pk,
  220. &ip->auth_key_cert->signed_key)) {
  221. intro_point = ip;
  222. break;
  223. }
  224. } SMARTLIST_FOREACH_END(ip);
  225. return intro_point;
  226. }
  227. /* Find a descriptor intro point object from the descriptor object desc that
  228. * matches the given legacy identity digest in legacy_id. Return NULL if not
  229. * found. */
  230. static hs_desc_intro_point_t *
  231. find_desc_intro_point_by_legacy_id(const char *legacy_id,
  232. const hs_descriptor_t *desc)
  233. {
  234. hs_desc_intro_point_t *ret_ip = NULL;
  235. tor_assert(legacy_id);
  236. tor_assert(desc);
  237. /* We will go over every intro point and try to find which one is linked to
  238. * that circuit. Those lists are small so it's not that expensive. */
  239. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  240. hs_desc_intro_point_t *, ip) {
  241. SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
  242. const hs_desc_link_specifier_t *, lspec) {
  243. /* Not all tor node have an ed25519 identity key so we still rely on the
  244. * legacy identity digest. */
  245. if (lspec->type != LS_LEGACY_ID) {
  246. continue;
  247. }
  248. if (fast_memneq(legacy_id, lspec->u.legacy_id, DIGEST_LEN)) {
  249. break;
  250. }
  251. /* Found it. */
  252. ret_ip = ip;
  253. goto end;
  254. } SMARTLIST_FOREACH_END(lspec);
  255. } SMARTLIST_FOREACH_END(ip);
  256. end:
  257. return ret_ip;
  258. }
  259. /* Send an INTRODUCE1 cell along the intro circuit and populate the rend
  260. * circuit identifier with the needed key material for the e2e encryption.
  261. * Return 0 on success, -1 if there is a transient error such that an action
  262. * has been taken to recover and -2 if there is a permanent error indicating
  263. * that both circuits were closed. */
  264. static int
  265. send_introduce1(origin_circuit_t *intro_circ,
  266. origin_circuit_t *rend_circ)
  267. {
  268. int status;
  269. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  270. const ed25519_public_key_t *service_identity_pk = NULL;
  271. const hs_desc_intro_point_t *ip;
  272. tor_assert(rend_circ);
  273. if (intro_circ_is_ok(intro_circ) < 0) {
  274. goto perm_err;
  275. }
  276. service_identity_pk = &intro_circ->hs_ident->identity_pk;
  277. /* For logging purposes. There will be a time where the hs_ident will have a
  278. * version number but for now there is none because it's all v3. */
  279. hs_build_address(service_identity_pk, HS_VERSION_THREE, onion_address);
  280. log_info(LD_REND, "Sending INTRODUCE1 cell to service %s on circuit %u",
  281. safe_str_client(onion_address), TO_CIRCUIT(intro_circ)->n_circ_id);
  282. /* 1) Get descriptor from our cache. */
  283. const hs_descriptor_t *desc =
  284. hs_cache_lookup_as_client(service_identity_pk);
  285. if (desc == NULL || !hs_client_any_intro_points_usable(service_identity_pk,
  286. desc)) {
  287. log_info(LD_REND, "Request to %s %s. Trying to fetch a new descriptor.",
  288. safe_str_client(onion_address),
  289. (desc) ? "didn't have usable intro points" :
  290. "didn't have a descriptor");
  291. hs_client_refetch_hsdesc(service_identity_pk);
  292. /* We just triggered a refetch, make sure every connections are back
  293. * waiting for that descriptor. */
  294. flag_all_conn_wait_desc(service_identity_pk);
  295. /* We just asked for a refetch so this is a transient error. */
  296. goto tran_err;
  297. }
  298. /* We need to find which intro point in the descriptor we are connected to
  299. * on intro_circ. */
  300. ip = find_desc_intro_point_by_ident(intro_circ->hs_ident, desc);
  301. if (BUG(ip == NULL)) {
  302. /* If we can find a descriptor from this introduction circuit ident, we
  303. * must have a valid intro point object. Permanent error. */
  304. goto perm_err;
  305. }
  306. /* Send the INTRODUCE1 cell. */
  307. if (hs_circ_send_introduce1(intro_circ, rend_circ, ip,
  308. desc->subcredential) < 0) {
  309. /* Unable to send the cell, the intro circuit has been marked for close so
  310. * this is a permanent error. */
  311. tor_assert_nonfatal(TO_CIRCUIT(intro_circ)->marked_for_close);
  312. goto perm_err;
  313. }
  314. /* Cell has been sent successfully. Copy the introduction point
  315. * authentication and encryption key in the rendezvous circuit identifier so
  316. * we can compute the ntor keys when we receive the RENDEZVOUS2 cell. */
  317. memcpy(&rend_circ->hs_ident->intro_enc_pk, &ip->enc_key,
  318. sizeof(rend_circ->hs_ident->intro_enc_pk));
  319. ed25519_pubkey_copy(&rend_circ->hs_ident->intro_auth_pk,
  320. &intro_circ->hs_ident->intro_auth_pk);
  321. /* Now, we wait for an ACK or NAK on this circuit. */
  322. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  323. CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT);
  324. /* Set timestamp_dirty, because circuit_expire_building expects it to
  325. * specify when a circuit entered the _C_INTRODUCE_ACK_WAIT state. */
  326. TO_CIRCUIT(intro_circ)->timestamp_dirty = time(NULL);
  327. pathbias_count_use_attempt(intro_circ);
  328. /* Success. */
  329. status = 0;
  330. goto end;
  331. perm_err:
  332. /* Permanent error: it is possible that the intro circuit was closed prior
  333. * because we weren't able to send the cell. Make sure we don't double close
  334. * it which would result in a warning. */
  335. if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
  336. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  337. }
  338. circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_INTERNAL);
  339. status = -2;
  340. goto end;
  341. tran_err:
  342. status = -1;
  343. end:
  344. memwipe(onion_address, 0, sizeof(onion_address));
  345. return status;
  346. }
  347. /* Using the introduction circuit circ, setup the authentication key of the
  348. * intro point this circuit has extended to. */
  349. static void
  350. setup_intro_circ_auth_key(origin_circuit_t *circ)
  351. {
  352. const hs_descriptor_t *desc;
  353. const hs_desc_intro_point_t *ip;
  354. tor_assert(circ);
  355. desc = hs_cache_lookup_as_client(&circ->hs_ident->identity_pk);
  356. if (BUG(desc == NULL)) {
  357. /* Opening intro circuit without the descriptor is no good... */
  358. goto end;
  359. }
  360. /* We will go over every intro point and try to find which one is linked to
  361. * that circuit. Those lists are small so it's not that expensive. */
  362. ip = find_desc_intro_point_by_legacy_id(
  363. circ->build_state->chosen_exit->identity_digest, desc);
  364. if (ip) {
  365. /* We got it, copy its authentication key to the identifier. */
  366. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  367. &ip->auth_key_cert->signed_key);
  368. goto end;
  369. }
  370. /* Reaching this point means we didn't find any intro point for this circuit
  371. * which is not suppose to happen. */
  372. tor_assert_nonfatal_unreached();
  373. end:
  374. return;
  375. }
  376. /* Called when an introduction circuit has opened. */
  377. static void
  378. client_intro_circ_has_opened(origin_circuit_t *circ)
  379. {
  380. tor_assert(circ);
  381. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  382. log_info(LD_REND, "Introduction circuit %u has opened. Attaching streams.",
  383. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  384. /* This is an introduction circuit so we'll attach the correct
  385. * authentication key to the circuit identifier so it can be identified
  386. * properly later on. */
  387. setup_intro_circ_auth_key(circ);
  388. connection_ap_attach_pending(1);
  389. }
  390. /* Called when a rendezvous circuit has opened. */
  391. static void
  392. client_rendezvous_circ_has_opened(origin_circuit_t *circ)
  393. {
  394. tor_assert(circ);
  395. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  396. log_info(LD_REND, "Rendezvous circuit has opened to %s.",
  397. safe_str_client(
  398. extend_info_describe(circ->build_state->chosen_exit)));
  399. /* Ignore returned value, nothing we can really do. On failure, the circuit
  400. * will be marked for close. */
  401. hs_circ_send_establish_rendezvous(circ);
  402. /* Register rend circuit in circuitmap if it's still alive. */
  403. if (!TO_CIRCUIT(circ)->marked_for_close) {
  404. hs_circuitmap_register_rend_circ_client_side(circ,
  405. circ->hs_ident->rendezvous_cookie);
  406. }
  407. }
  408. /* This is an helper function that convert a descriptor intro point object ip
  409. * to a newly allocated extend_info_t object fully initialized. Return NULL if
  410. * we can't convert it for which chances are that we are missing or malformed
  411. * link specifiers. */
  412. static extend_info_t *
  413. desc_intro_point_to_extend_info(const hs_desc_intro_point_t *ip)
  414. {
  415. extend_info_t *ei;
  416. smartlist_t *lspecs = smartlist_new();
  417. tor_assert(ip);
  418. /* We first encode the descriptor link specifiers into the binary
  419. * representation which is a trunnel object. */
  420. SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
  421. const hs_desc_link_specifier_t *, desc_lspec) {
  422. link_specifier_t *lspec = hs_desc_lspec_to_trunnel(desc_lspec);
  423. smartlist_add(lspecs, lspec);
  424. } SMARTLIST_FOREACH_END(desc_lspec);
  425. /* Explicitely put the direct connection option to 0 because this is client
  426. * side and there is no such thing as a non anonymous client. */
  427. ei = hs_get_extend_info_from_lspecs(lspecs, &ip->onion_key, 0);
  428. SMARTLIST_FOREACH(lspecs, link_specifier_t *, ls, link_specifier_free(ls));
  429. smartlist_free(lspecs);
  430. return ei;
  431. }
  432. /* Return true iff the intro point ip for the service service_pk is usable.
  433. * This function checks if the intro point is in the client intro state cache
  434. * and checks at the failures. It is considered usable if:
  435. * - No error happened (INTRO_POINT_FAILURE_GENERIC)
  436. * - It is not flagged as timed out (INTRO_POINT_FAILURE_TIMEOUT)
  437. * - The unreachable count is lower than
  438. * MAX_INTRO_POINT_REACHABILITY_FAILURES (INTRO_POINT_FAILURE_UNREACHABLE)
  439. */
  440. static int
  441. intro_point_is_usable(const ed25519_public_key_t *service_pk,
  442. const hs_desc_intro_point_t *ip)
  443. {
  444. const hs_cache_intro_state_t *state;
  445. tor_assert(service_pk);
  446. tor_assert(ip);
  447. state = hs_cache_client_intro_state_find(service_pk,
  448. &ip->auth_key_cert->signed_key);
  449. if (state == NULL) {
  450. /* This means we've never encountered any problem thus usable. */
  451. goto usable;
  452. }
  453. if (state->error) {
  454. log_info(LD_REND, "Intro point with auth key %s had an error. Not usable",
  455. safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
  456. goto not_usable;
  457. }
  458. if (state->timed_out) {
  459. log_info(LD_REND, "Intro point with auth key %s timed out. Not usable",
  460. safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
  461. goto not_usable;
  462. }
  463. if (state->unreachable_count >= MAX_INTRO_POINT_REACHABILITY_FAILURES) {
  464. log_info(LD_REND, "Intro point with auth key %s unreachable. Not usable",
  465. safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
  466. goto not_usable;
  467. }
  468. usable:
  469. return 1;
  470. not_usable:
  471. return 0;
  472. }
  473. /* Using a descriptor desc, return a newly allocated extend_info_t object of a
  474. * randomly picked introduction point from its list. Return NULL if none are
  475. * usable. */
  476. static extend_info_t *
  477. client_get_random_intro(const ed25519_public_key_t *service_pk)
  478. {
  479. extend_info_t *ei = NULL, *ei_excluded = NULL;
  480. smartlist_t *usable_ips = NULL;
  481. const hs_descriptor_t *desc;
  482. const hs_desc_encrypted_data_t *enc_data;
  483. const or_options_t *options = get_options();
  484. tor_assert(service_pk);
  485. desc = hs_cache_lookup_as_client(service_pk);
  486. if (desc == NULL || !hs_client_any_intro_points_usable(service_pk,
  487. desc)) {
  488. log_info(LD_REND, "Unable to randomly select an introduction point "
  489. "because descriptor %s.",
  490. (desc) ? "doesn't have usable intro point" : "is missing");
  491. goto end;
  492. }
  493. enc_data = &desc->encrypted_data;
  494. usable_ips = smartlist_new();
  495. smartlist_add_all(usable_ips, enc_data->intro_points);
  496. while (smartlist_len(usable_ips) != 0) {
  497. int idx;
  498. const hs_desc_intro_point_t *ip;
  499. /* Pick a random intro point and immediately remove it from the usable
  500. * list so we don't pick it again if we have to iterate more. */
  501. idx = crypto_rand_int(smartlist_len(usable_ips));
  502. ip = smartlist_get(usable_ips, idx);
  503. smartlist_del(usable_ips, idx);
  504. /* We need to make sure we have a usable intro points which is in a good
  505. * state in our cache. */
  506. if (!intro_point_is_usable(service_pk, ip)) {
  507. continue;
  508. }
  509. /* Generate an extend info object from the intro point object. */
  510. ei = desc_intro_point_to_extend_info(ip);
  511. if (ei == NULL) {
  512. /* We can get here for instance if the intro point is a private address
  513. * and we aren't allowed to extend to those. */
  514. continue;
  515. }
  516. /* Test the pick against ExcludeNodes. */
  517. if (routerset_contains_extendinfo(options->ExcludeNodes, ei)) {
  518. /* If this pick is in the ExcludeNodes list, we keep its reference so if
  519. * we ever end up not being able to pick anything else and StrictNodes is
  520. * unset, we'll use it. */
  521. ei_excluded = ei;
  522. continue;
  523. }
  524. /* Good pick! Let's go with this. */
  525. goto end;
  526. }
  527. /* Reaching this point means a couple of things. Either we can't use any of
  528. * the intro point listed because the IP address can't be extended to or it
  529. * is listed in the ExcludeNodes list. In the later case, if StrictNodes is
  530. * set, we are forced to not use anything. */
  531. ei = ei_excluded;
  532. if (options->StrictNodes) {
  533. log_warn(LD_REND, "Every introduction points are in the ExcludeNodes set "
  534. "and StrictNodes is set. We can't connect.");
  535. ei = NULL;
  536. }
  537. end:
  538. smartlist_free(usable_ips);
  539. return ei;
  540. }
  541. /* For this introduction circuit, we'll look at if we have any usable
  542. * introduction point left for this service. If so, we'll use the circuit to
  543. * re-extend to a new intro point. Else, we'll close the circuit and its
  544. * corresponding rendezvous circuit. Return 0 if we are re-extending else -1
  545. * if we are closing the circuits.
  546. *
  547. * This is called when getting an INTRODUCE_ACK cell with a NACK. */
  548. static int
  549. close_or_reextend_intro_circ(origin_circuit_t *intro_circ)
  550. {
  551. int ret = -1;
  552. const hs_descriptor_t *desc;
  553. origin_circuit_t *rend_circ;
  554. tor_assert(intro_circ);
  555. desc = hs_cache_lookup_as_client(&intro_circ->hs_ident->identity_pk);
  556. if (BUG(desc == NULL)) {
  557. /* We can't continue without a descriptor. */
  558. goto close;
  559. }
  560. /* We still have the descriptor, great! Let's try to see if we can
  561. * re-extend by looking up if there are any usable intro points. */
  562. if (!hs_client_any_intro_points_usable(&intro_circ->hs_ident->identity_pk,
  563. desc)) {
  564. goto close;
  565. }
  566. /* Try to re-extend now. */
  567. if (hs_client_reextend_intro_circuit(intro_circ) < 0) {
  568. goto close;
  569. }
  570. /* Success on re-extending. Don't return an error. */
  571. ret = 0;
  572. goto end;
  573. close:
  574. /* Change the intro circuit purpose before so we don't report an intro point
  575. * failure again triggering an extra descriptor fetch. The circuit can
  576. * already be closed on failure to re-extend. */
  577. if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
  578. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  579. CIRCUIT_PURPOSE_C_INTRODUCE_ACKED);
  580. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_FINISHED);
  581. }
  582. /* Close the related rendezvous circuit. */
  583. rend_circ = hs_circuitmap_get_rend_circ_client_side(
  584. intro_circ->hs_ident->rendezvous_cookie);
  585. /* The rendezvous circuit might have collapsed while the INTRODUCE_ACK was
  586. * inflight so we can't expect one every time. */
  587. if (rend_circ) {
  588. circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_FINISHED);
  589. }
  590. end:
  591. return ret;
  592. }
  593. /* Called when we get an INTRODUCE_ACK success status code. Do the appropriate
  594. * actions for the rendezvous point and finally close intro_circ. */
  595. static void
  596. handle_introduce_ack_success(origin_circuit_t *intro_circ)
  597. {
  598. origin_circuit_t *rend_circ = NULL;
  599. tor_assert(intro_circ);
  600. log_info(LD_REND, "Received INTRODUCE_ACK ack! Informing rendezvous");
  601. /* Get the rendezvous circuit for this rendezvous cookie. */
  602. uint8_t *rendezvous_cookie = intro_circ->hs_ident->rendezvous_cookie;
  603. rend_circ = hs_circuitmap_get_rend_circ_client_side(rendezvous_cookie);
  604. if (rend_circ == NULL) {
  605. log_warn(LD_REND, "Can't find any rendezvous circuit. Stopping");
  606. goto end;
  607. }
  608. assert_circ_anonymity_ok(rend_circ, get_options());
  609. circuit_change_purpose(TO_CIRCUIT(rend_circ),
  610. CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED);
  611. /* Set timestamp_dirty, because circuit_expire_building expects it to
  612. * specify when a circuit entered the
  613. * CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED state. */
  614. TO_CIRCUIT(rend_circ)->timestamp_dirty = time(NULL);
  615. end:
  616. /* We don't need the intro circuit anymore. It did what it had to do! */
  617. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  618. CIRCUIT_PURPOSE_C_INTRODUCE_ACKED);
  619. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_FINISHED);
  620. /* XXX: Close pending intro circuits we might have in parallel. */
  621. return;
  622. }
  623. /* Called when we get an INTRODUCE_ACK failure status code. Depending on our
  624. * failure cache status, either close the circuit or re-extend to a new
  625. * introduction point. */
  626. static void
  627. handle_introduce_ack_bad(origin_circuit_t *circ, int status)
  628. {
  629. tor_assert(circ);
  630. log_info(LD_REND, "Received INTRODUCE_ACK nack by %s. Reason: %u",
  631. safe_str_client(extend_info_describe(circ->build_state->chosen_exit)),
  632. status);
  633. /* It's a NAK. The introduction point didn't relay our request. */
  634. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_INTRODUCING);
  635. /* Note down this failure in the intro point failure cache. Depending on how
  636. * many times we've tried this intro point, close it or reextend. */
  637. hs_cache_client_intro_state_note(&circ->hs_ident->identity_pk,
  638. &circ->hs_ident->intro_auth_pk,
  639. INTRO_POINT_FAILURE_GENERIC);
  640. }
  641. /* Called when we get an INTRODUCE_ACK on the intro circuit circ. The encoded
  642. * cell is in payload of length payload_len. Return 0 on success else a
  643. * negative value. The circuit is either close or reuse to re-extend to a new
  644. * introduction point. */
  645. static int
  646. handle_introduce_ack(origin_circuit_t *circ, const uint8_t *payload,
  647. size_t payload_len)
  648. {
  649. int status, ret = -1;
  650. tor_assert(circ);
  651. tor_assert(circ->build_state);
  652. tor_assert(circ->build_state->chosen_exit);
  653. assert_circ_anonymity_ok(circ, get_options());
  654. tor_assert(payload);
  655. status = hs_cell_parse_introduce_ack(payload, payload_len);
  656. switch (status) {
  657. case HS_CELL_INTRO_ACK_SUCCESS:
  658. ret = 0;
  659. handle_introduce_ack_success(circ);
  660. goto end;
  661. case HS_CELL_INTRO_ACK_FAILURE:
  662. case HS_CELL_INTRO_ACK_BADFMT:
  663. case HS_CELL_INTRO_ACK_NORELAY:
  664. handle_introduce_ack_bad(circ, status);
  665. /* We are going to see if we have to close the circuits (IP and RP) or we
  666. * can re-extend to a new intro point. */
  667. ret = close_or_reextend_intro_circ(circ);
  668. break;
  669. default:
  670. log_info(LD_PROTOCOL, "Unknown INTRODUCE_ACK status code %u from %s",
  671. status,
  672. safe_str_client(extend_info_describe(circ->build_state->chosen_exit)));
  673. break;
  674. }
  675. end:
  676. return ret;
  677. }
  678. /* Called when we get a RENDEZVOUS2 cell on the rendezvous circuit circ. The
  679. * encoded cell is in payload of length payload_len. Return 0 on success or a
  680. * negative value on error. On error, the circuit is marked for close. */
  681. static int
  682. handle_rendezvous2(origin_circuit_t *circ, const uint8_t *payload,
  683. size_t payload_len)
  684. {
  685. int ret = -1;
  686. curve25519_public_key_t server_pk;
  687. uint8_t auth_mac[DIGEST256_LEN] = {0};
  688. uint8_t handshake_info[CURVE25519_PUBKEY_LEN + sizeof(auth_mac)] = {0};
  689. hs_ntor_rend_cell_keys_t keys;
  690. const hs_ident_circuit_t *ident;
  691. tor_assert(circ);
  692. tor_assert(payload);
  693. /* Make things easier. */
  694. ident = circ->hs_ident;
  695. tor_assert(ident);
  696. if (hs_cell_parse_rendezvous2(payload, payload_len, handshake_info,
  697. sizeof(handshake_info)) < 0) {
  698. goto err;
  699. }
  700. /* Get from the handshake info the SERVER_PK and AUTH_MAC. */
  701. memcpy(&server_pk, handshake_info, CURVE25519_PUBKEY_LEN);
  702. memcpy(auth_mac, handshake_info + CURVE25519_PUBKEY_LEN, sizeof(auth_mac));
  703. /* Generate the handshake info. */
  704. if (hs_ntor_client_get_rendezvous1_keys(&ident->intro_auth_pk,
  705. &ident->rendezvous_client_kp,
  706. &ident->intro_enc_pk, &server_pk,
  707. &keys) < 0) {
  708. log_info(LD_REND, "Unable to compute the rendezvous keys.");
  709. goto err;
  710. }
  711. /* Critical check, make sure that the MAC matches what we got with what we
  712. * computed just above. */
  713. if (!hs_ntor_client_rendezvous2_mac_is_good(&keys, auth_mac)) {
  714. log_info(LD_REND, "Invalid MAC in RENDEZVOUS2. Rejecting cell.");
  715. goto err;
  716. }
  717. /* Setup the e2e encryption on the circuit and finalize its state. */
  718. if (hs_circuit_setup_e2e_rend_circ(circ, keys.ntor_key_seed,
  719. sizeof(keys.ntor_key_seed), 0) < 0) {
  720. log_info(LD_REND, "Unable to setup the e2e encryption.");
  721. goto err;
  722. }
  723. /* Success. Hidden service connection finalized! */
  724. ret = 0;
  725. goto end;
  726. err:
  727. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  728. end:
  729. memwipe(&keys, 0, sizeof(keys));
  730. return ret;
  731. }
  732. /* ========== */
  733. /* Public API */
  734. /* ========== */
  735. /** A circuit just finished connecting to a hidden service that the stream
  736. * <b>conn</b> has been waiting for. Let the HS subsystem know about this. */
  737. void
  738. hs_client_note_connection_attempt_succeeded(const edge_connection_t *conn)
  739. {
  740. tor_assert(connection_edge_is_rendezvous_stream(conn));
  741. if (BUG(conn->rend_data && conn->hs_ident)) {
  742. log_warn(LD_BUG, "Stream had both rend_data and hs_ident..."
  743. "Prioritizing hs_ident");
  744. }
  745. if (conn->hs_ident) { /* It's v3: pass it to the prop224 handler */
  746. note_connection_attempt_succeeded(conn->hs_ident);
  747. return;
  748. } else if (conn->rend_data) { /* It's v2: pass it to the legacy handler */
  749. rend_client_note_connection_attempt_ended(conn->rend_data);
  750. return;
  751. }
  752. }
  753. /* With the given encoded descriptor in desc_str and the service key in
  754. * service_identity_pk, decode the descriptor and set the desc pointer with a
  755. * newly allocated descriptor object.
  756. *
  757. * Return 0 on success else a negative value and desc is set to NULL. */
  758. int
  759. hs_client_decode_descriptor(const char *desc_str,
  760. const ed25519_public_key_t *service_identity_pk,
  761. hs_descriptor_t **desc)
  762. {
  763. int ret;
  764. uint8_t subcredential[DIGEST256_LEN];
  765. ed25519_public_key_t blinded_pubkey;
  766. tor_assert(desc_str);
  767. tor_assert(service_identity_pk);
  768. tor_assert(desc);
  769. /* Create subcredential for this HS so that we can decrypt */
  770. {
  771. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  772. hs_build_blinded_pubkey(service_identity_pk, NULL, 0, current_time_period,
  773. &blinded_pubkey);
  774. hs_get_subcredential(service_identity_pk, &blinded_pubkey, subcredential);
  775. }
  776. /* Parse descriptor */
  777. ret = hs_desc_decode_descriptor(desc_str, subcredential, desc);
  778. memwipe(subcredential, 0, sizeof(subcredential));
  779. if (ret < 0) {
  780. log_warn(LD_GENERAL, "Could not parse received descriptor as client");
  781. goto err;
  782. }
  783. /* Make sure the descriptor signing key cross certifies with the computed
  784. * blinded key. Without this validation, anyone knowing the subcredential
  785. * and onion address can forge a descriptor. */
  786. if (tor_cert_checksig((*desc)->plaintext_data.signing_key_cert,
  787. &blinded_pubkey, approx_time()) < 0) {
  788. log_warn(LD_GENERAL, "Descriptor signing key certificate signature "
  789. "doesn't validate with computed blinded key.");
  790. goto err;
  791. }
  792. return 0;
  793. err:
  794. return -1;
  795. }
  796. /* Return true iff there are at least one usable intro point in the service
  797. * descriptor desc. */
  798. int
  799. hs_client_any_intro_points_usable(const ed25519_public_key_t *service_pk,
  800. const hs_descriptor_t *desc)
  801. {
  802. tor_assert(service_pk);
  803. tor_assert(desc);
  804. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  805. const hs_desc_intro_point_t *, ip) {
  806. if (intro_point_is_usable(service_pk, ip)) {
  807. goto usable;
  808. }
  809. } SMARTLIST_FOREACH_END(ip);
  810. return 0;
  811. usable:
  812. return 1;
  813. }
  814. /** Launch a connection to a hidden service directory to fetch a hidden
  815. * service descriptor using <b>identity_pk</b> to get the necessary keys.
  816. *
  817. * On success, 1 is returned. If no hidden service is left to ask, return 0.
  818. * On error, -1 is returned. (retval is only used by unittests right now) */
  819. int
  820. hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
  821. {
  822. tor_assert(identity_pk);
  823. /* Are we configured to fetch descriptors? */
  824. if (!get_options()->FetchHidServDescriptors) {
  825. log_warn(LD_REND, "We received an onion address for a hidden service "
  826. "descriptor but we are configured to not fetch.");
  827. return 0;
  828. }
  829. /* Check if fetching a desc for this HS is useful to us right now */
  830. {
  831. const hs_descriptor_t *cached_desc = NULL;
  832. cached_desc = hs_cache_lookup_as_client(identity_pk);
  833. if (cached_desc && hs_client_any_intro_points_usable(identity_pk,
  834. cached_desc)) {
  835. log_warn(LD_GENERAL, "We would fetch a v3 hidden service descriptor "
  836. "but we already have a useable descriprot.");
  837. return 0;
  838. }
  839. }
  840. return fetch_v3_desc(identity_pk);
  841. }
  842. /* This is called when we are trying to attach an AP connection to these
  843. * hidden service circuits from connection_ap_handshake_attach_circuit().
  844. * Return 0 on success, -1 for a transient error that is actions were
  845. * triggered to recover or -2 for a permenent error where both circuits will
  846. * marked for close.
  847. *
  848. * The following supports every hidden service version. */
  849. int
  850. hs_client_send_introduce1(origin_circuit_t *intro_circ,
  851. origin_circuit_t *rend_circ)
  852. {
  853. return (intro_circ->hs_ident) ? send_introduce1(intro_circ, rend_circ) :
  854. rend_client_send_introduction(intro_circ,
  855. rend_circ);
  856. }
  857. /* Called when the client circuit circ has been established. It can be either
  858. * an introduction or rendezvous circuit. This function handles all hidden
  859. * service versions. */
  860. void
  861. hs_client_circuit_has_opened(origin_circuit_t *circ)
  862. {
  863. tor_assert(circ);
  864. /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
  865. * identifier hs_ident. Can't be both. */
  866. switch (TO_CIRCUIT(circ)->purpose) {
  867. case CIRCUIT_PURPOSE_C_INTRODUCING:
  868. if (circ->hs_ident) {
  869. client_intro_circ_has_opened(circ);
  870. } else {
  871. rend_client_introcirc_has_opened(circ);
  872. }
  873. break;
  874. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  875. if (circ->hs_ident) {
  876. client_rendezvous_circ_has_opened(circ);
  877. } else {
  878. rend_client_rendcirc_has_opened(circ);
  879. }
  880. break;
  881. default:
  882. tor_assert_nonfatal_unreached();
  883. }
  884. }
  885. /* Called when we receive a RENDEZVOUS_ESTABLISHED cell. Change the state of
  886. * the circuit to CIRCUIT_PURPOSE_C_REND_READY. Return 0 on success else a
  887. * negative value and the circuit marked for close. */
  888. int
  889. hs_client_receive_rendezvous_acked(origin_circuit_t *circ,
  890. const uint8_t *payload, size_t payload_len)
  891. {
  892. tor_assert(circ);
  893. tor_assert(payload);
  894. (void) payload_len;
  895. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  896. log_warn(LD_PROTOCOL, "Got a RENDEZVOUS_ESTABLISHED but we were not "
  897. "expecting one. Closing circuit.");
  898. goto err;
  899. }
  900. log_info(LD_REND, "Received an RENDEZVOUS_ESTABLISHED. This circuit is "
  901. "now ready for rendezvous.");
  902. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_REND_READY);
  903. /* Set timestamp_dirty, because circuit_expire_building expects it to
  904. * specify when a circuit entered the _C_REND_READY state. */
  905. TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
  906. /* From a path bias point of view, this circuit is now successfully used.
  907. * Waiting any longer opens us up to attacks from malicious hidden services.
  908. * They could induce the client to attempt to connect to their hidden
  909. * service and never reply to the client's rend requests */
  910. pathbias_mark_use_success(circ);
  911. /* If we already have the introduction circuit built, make sure we send
  912. * the INTRODUCE cell _now_ */
  913. connection_ap_attach_pending(1);
  914. return 0;
  915. err:
  916. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  917. return -1;
  918. }
  919. /* This is called when a descriptor has arrived following a fetch request and
  920. * has been stored in the client cache. Every entry connection that matches
  921. * the service identity key in the ident will get attached to the hidden
  922. * service circuit. */
  923. void
  924. hs_client_desc_has_arrived(const hs_ident_dir_conn_t *ident)
  925. {
  926. time_t now = time(NULL);
  927. smartlist_t *conns = NULL;
  928. tor_assert(ident);
  929. conns = connection_list_by_type_state(CONN_TYPE_AP,
  930. AP_CONN_STATE_RENDDESC_WAIT);
  931. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
  932. const hs_descriptor_t *desc;
  933. entry_connection_t *entry_conn = TO_ENTRY_CONN(base_conn);
  934. const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(entry_conn);
  935. /* Only consider the entry connections that matches the service for which
  936. * we just fetched its descriptor. */
  937. if (!edge_conn->hs_ident ||
  938. !ed25519_pubkey_eq(&ident->identity_pk,
  939. &edge_conn->hs_ident->identity_pk)) {
  940. continue;
  941. }
  942. assert_connection_ok(base_conn, now);
  943. /* We were just called because we stored the descriptor for this service
  944. * so not finding a descriptor means we have a bigger problem. */
  945. desc = hs_cache_lookup_as_client(&ident->identity_pk);
  946. if (BUG(desc == NULL)) {
  947. goto end;
  948. }
  949. if (!hs_client_any_intro_points_usable(&ident->identity_pk, desc)) {
  950. log_info(LD_REND, "Hidden service descriptor is unusable. "
  951. "Closing streams.");
  952. connection_mark_unattached_ap(entry_conn,
  953. END_STREAM_REASON_RESOLVEFAILED);
  954. /* We are unable to use the descriptor so remove the directory request
  955. * from the cache so the next connection can try again. */
  956. note_connection_attempt_succeeded(edge_conn->hs_ident);
  957. goto end;
  958. }
  959. log_info(LD_REND, "Descriptor has arrived. Launching circuits.");
  960. /* Restart their timeout values, so they get a fair shake at connecting to
  961. * the hidden service. XXX: Improve comment on why this is needed. */
  962. base_conn->timestamp_created = now;
  963. base_conn->timestamp_lastread = now;
  964. base_conn->timestamp_lastwritten = now;
  965. /* Change connection's state into waiting for a circuit. */
  966. base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  967. connection_ap_mark_as_pending_circuit(entry_conn);
  968. } SMARTLIST_FOREACH_END(base_conn);
  969. end:
  970. /* We don't have ownership of the objects in this list. */
  971. smartlist_free(conns);
  972. }
  973. /* Return a newly allocated extend_info_t for a randomly chosen introduction
  974. * point for the given edge connection identifier ident. Return NULL if we
  975. * can't pick any usable introduction points. */
  976. extend_info_t *
  977. hs_client_get_random_intro_from_edge(const edge_connection_t *edge_conn)
  978. {
  979. tor_assert(edge_conn);
  980. return (edge_conn->hs_ident) ?
  981. client_get_random_intro(&edge_conn->hs_ident->identity_pk) :
  982. rend_client_get_random_intro(edge_conn->rend_data);
  983. }
  984. /* Called when get an INTRODUCE_ACK cell on the introduction circuit circ.
  985. * Return 0 on success else a negative value is returned. The circuit will be
  986. * closed or reuse to extend again to another intro point. */
  987. int
  988. hs_client_receive_introduce_ack(origin_circuit_t *circ,
  989. const uint8_t *payload, size_t payload_len)
  990. {
  991. int ret = -1;
  992. tor_assert(circ);
  993. tor_assert(payload);
  994. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  995. log_warn(LD_PROTOCOL, "Unexpected INTRODUCE_ACK on circuit %u.",
  996. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  997. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  998. goto end;
  999. }
  1000. ret = (circ->hs_ident) ? handle_introduce_ack(circ, payload, payload_len) :
  1001. rend_client_introduction_acked(circ, payload,
  1002. payload_len);
  1003. /* For path bias: This circuit was used successfully. NACK or ACK counts. */
  1004. pathbias_mark_use_success(circ);
  1005. end:
  1006. return ret;
  1007. }
  1008. /* Called when get a RENDEZVOUS2 cell on the rendezvous circuit circ. Return
  1009. * 0 on success else a negative value is returned. The circuit will be closed
  1010. * on error. */
  1011. int
  1012. hs_client_receive_rendezvous2(origin_circuit_t *circ,
  1013. const uint8_t *payload, size_t payload_len)
  1014. {
  1015. int ret = -1;
  1016. tor_assert(circ);
  1017. tor_assert(payload);
  1018. /* Circuit can possibly be in both state because we could receive a
  1019. * RENDEZVOUS2 cell before the INTRODUCE_ACK has been received. */
  1020. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  1021. TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  1022. log_warn(LD_PROTOCOL, "Unexpected RENDEZVOUS2 cell on circuit %u. "
  1023. "Closing circuit.",
  1024. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  1025. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  1026. goto end;
  1027. }
  1028. log_info(LD_REND, "Got RENDEZVOUS2 cell from hidden service on circuit %u.",
  1029. TO_CIRCUIT(circ)->n_circ_id);
  1030. ret = (circ->hs_ident) ? handle_rendezvous2(circ, payload, payload_len) :
  1031. rend_client_receive_rendezvous(circ, payload,
  1032. payload_len);
  1033. end:
  1034. return ret;
  1035. }
  1036. /* Extend the introduction circuit circ to another valid introduction point
  1037. * for the hidden service it is trying to connect to, or mark it and launch a
  1038. * new circuit if we can't extend it. Return 0 on success or possible
  1039. * success. Return -1 and mark the introduction circuit for close on permanent
  1040. * failure.
  1041. *
  1042. * On failure, the caller is responsible for marking the associated rendezvous
  1043. * circuit for close. */
  1044. int
  1045. hs_client_reextend_intro_circuit(origin_circuit_t *circ)
  1046. {
  1047. int ret = -1;
  1048. extend_info_t *ei;
  1049. tor_assert(circ);
  1050. ei = (circ->hs_ident) ?
  1051. client_get_random_intro(&circ->hs_ident->identity_pk) :
  1052. rend_client_get_random_intro(circ->rend_data);
  1053. if (ei == NULL) {
  1054. log_warn(LD_REND, "No usable introduction points left. Closing.");
  1055. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  1056. goto end;
  1057. }
  1058. if (circ->remaining_relay_early_cells) {
  1059. log_info(LD_REND, "Re-extending circ %u, this time to %s.",
  1060. (unsigned int) TO_CIRCUIT(circ)->n_circ_id,
  1061. safe_str_client(extend_info_describe(ei)));
  1062. ret = circuit_extend_to_new_exit(circ, ei);
  1063. if (ret == 0) {
  1064. /* We were able to extend so update the timestamp so we avoid expiring
  1065. * this circuit too early. The intro circuit is short live so the
  1066. * linkability issue is minimized, we just need the circuit to hold a
  1067. * bit longer so we can introduce. */
  1068. TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
  1069. }
  1070. } else {
  1071. log_info(LD_REND, "Closing intro circ %u (out of RELAY_EARLY cells).",
  1072. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  1073. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
  1074. /* connection_ap_handshake_attach_circuit will launch a new intro circ. */
  1075. ret = 0;
  1076. }
  1077. end:
  1078. extend_info_free(ei);
  1079. return ret;
  1080. }
  1081. /* Release all the storage held by the client subsystem. */
  1082. void
  1083. hs_client_free_all(void)
  1084. {
  1085. /* Purge the hidden service request cache. */
  1086. hs_purge_last_hid_serv_requests();
  1087. }