hs_client.c 42 KB

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