hs_client.c 41 KB

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