hs_client.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. /* Copyright (c) 2016-2018, 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. #define HS_CLIENT_PRIVATE
  8. #include "or/or.h"
  9. #include "or/circpathbias.h"
  10. #include "or/circuitbuild.h"
  11. #include "or/circuitlist.h"
  12. #include "or/circuituse.h"
  13. #include "or/config.h"
  14. #include "or/connection.h"
  15. #include "or/connection_edge.h"
  16. #include "lib/crypt_ops/crypto_format.h"
  17. #include "lib/crypt_ops/crypto_rand.h"
  18. #include "lib/crypt_ops/crypto_util.h"
  19. #include "or/directory.h"
  20. #include "or/hs_cache.h"
  21. #include "or/hs_cell.h"
  22. #include "or/hs_circuit.h"
  23. #include "or/hs_circuitmap.h"
  24. #include "or/hs_client.h"
  25. #include "or/hs_control.h"
  26. #include "or/hs_descriptor.h"
  27. #include "or/hs_ident.h"
  28. #include "or/hs_ntor.h"
  29. #include "or/networkstatus.h"
  30. #include "or/nodelist.h"
  31. #include "or/reasons.h"
  32. #include "or/rendclient.h"
  33. #include "or/router.h"
  34. #include "or/routerset.h"
  35. #include "or/cpath_build_state_st.h"
  36. #include "or/dir_connection_st.h"
  37. #include "or/entry_connection_st.h"
  38. #include "or/extend_info_st.h"
  39. #include "or/origin_circuit_st.h"
  40. /* Return a human-readable string for the client fetch status code. */
  41. static const char *
  42. fetch_status_to_string(hs_client_fetch_status_t status)
  43. {
  44. switch (status) {
  45. case HS_CLIENT_FETCH_ERROR:
  46. return "Internal error";
  47. case HS_CLIENT_FETCH_LAUNCHED:
  48. return "Descriptor fetch launched";
  49. case HS_CLIENT_FETCH_HAVE_DESC:
  50. return "Already have descriptor";
  51. case HS_CLIENT_FETCH_NO_HSDIRS:
  52. return "No more HSDir available to query";
  53. case HS_CLIENT_FETCH_NOT_ALLOWED:
  54. return "Fetching descriptors is not allowed";
  55. case HS_CLIENT_FETCH_MISSING_INFO:
  56. return "Missing directory information";
  57. case HS_CLIENT_FETCH_PENDING:
  58. return "Pending descriptor fetch";
  59. default:
  60. return "(Unknown client fetch status code)";
  61. }
  62. }
  63. /* Return true iff tor should close the SOCKS request(s) for the descriptor
  64. * fetch that ended up with this given status code. */
  65. static int
  66. fetch_status_should_close_socks(hs_client_fetch_status_t status)
  67. {
  68. switch (status) {
  69. case HS_CLIENT_FETCH_NO_HSDIRS:
  70. /* No more HSDir to query, we can't complete the SOCKS request(s). */
  71. case HS_CLIENT_FETCH_ERROR:
  72. /* The fetch triggered an internal error. */
  73. case HS_CLIENT_FETCH_NOT_ALLOWED:
  74. /* Client is not allowed to fetch (FetchHidServDescriptors 0). */
  75. goto close;
  76. case HS_CLIENT_FETCH_MISSING_INFO:
  77. case HS_CLIENT_FETCH_HAVE_DESC:
  78. case HS_CLIENT_FETCH_PENDING:
  79. case HS_CLIENT_FETCH_LAUNCHED:
  80. /* The rest doesn't require tor to close the SOCKS request(s). */
  81. goto no_close;
  82. }
  83. no_close:
  84. return 0;
  85. close:
  86. return 1;
  87. }
  88. /* Cancel all descriptor fetches currently in progress. */
  89. static void
  90. cancel_descriptor_fetches(void)
  91. {
  92. smartlist_t *conns =
  93. connection_list_by_type_state(CONN_TYPE_DIR, DIR_PURPOSE_FETCH_HSDESC);
  94. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  95. const hs_ident_dir_conn_t *ident = TO_DIR_CONN(conn)->hs_ident;
  96. if (BUG(ident == NULL)) {
  97. /* A directory connection fetching a service descriptor can't have an
  98. * empty hidden service identifier. */
  99. continue;
  100. }
  101. log_debug(LD_REND, "Marking for close a directory connection fetching "
  102. "a hidden service descriptor for service %s.",
  103. safe_str_client(ed25519_fmt(&ident->identity_pk)));
  104. connection_mark_for_close(conn);
  105. } SMARTLIST_FOREACH_END(conn);
  106. /* No ownership of the objects in this list. */
  107. smartlist_free(conns);
  108. log_info(LD_REND, "Hidden service client descriptor fetches cancelled.");
  109. }
  110. /* Get all connections that are waiting on a circuit and flag them back to
  111. * waiting for a hidden service descriptor for the given service key
  112. * service_identity_pk. */
  113. static void
  114. flag_all_conn_wait_desc(const ed25519_public_key_t *service_identity_pk)
  115. {
  116. tor_assert(service_identity_pk);
  117. smartlist_t *conns =
  118. connection_list_by_type_state(CONN_TYPE_AP, AP_CONN_STATE_CIRCUIT_WAIT);
  119. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  120. edge_connection_t *edge_conn;
  121. if (BUG(!CONN_IS_EDGE(conn))) {
  122. continue;
  123. }
  124. edge_conn = TO_EDGE_CONN(conn);
  125. if (edge_conn->hs_ident &&
  126. ed25519_pubkey_eq(&edge_conn->hs_ident->identity_pk,
  127. service_identity_pk)) {
  128. connection_ap_mark_as_non_pending_circuit(TO_ENTRY_CONN(conn));
  129. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  130. }
  131. } SMARTLIST_FOREACH_END(conn);
  132. smartlist_free(conns);
  133. }
  134. /* Remove tracked HSDir requests from our history for this hidden service
  135. * identity public key. */
  136. static void
  137. purge_hid_serv_request(const ed25519_public_key_t *identity_pk)
  138. {
  139. char base64_blinded_pk[ED25519_BASE64_LEN + 1];
  140. ed25519_public_key_t blinded_pk;
  141. tor_assert(identity_pk);
  142. /* Get blinded pubkey of hidden service. It is possible that we just moved
  143. * to a new time period meaning that we won't be able to purge the request
  144. * from the previous time period. That is fine because they will expire at
  145. * some point and we don't care about those anymore. */
  146. hs_build_blinded_pubkey(identity_pk, NULL, 0,
  147. hs_get_time_period_num(0), &blinded_pk);
  148. if (BUG(ed25519_public_to_base64(base64_blinded_pk, &blinded_pk) < 0)) {
  149. return;
  150. }
  151. /* Purge last hidden service request from cache for this blinded key. */
  152. hs_purge_hid_serv_from_last_hid_serv_requests(base64_blinded_pk);
  153. }
  154. /* Return true iff there is at least one pending directory descriptor request
  155. * for the service identity_pk. */
  156. static int
  157. directory_request_is_pending(const ed25519_public_key_t *identity_pk)
  158. {
  159. int ret = 0;
  160. smartlist_t *conns =
  161. connection_list_by_type_purpose(CONN_TYPE_DIR, DIR_PURPOSE_FETCH_HSDESC);
  162. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  163. const hs_ident_dir_conn_t *ident = TO_DIR_CONN(conn)->hs_ident;
  164. if (BUG(ident == NULL)) {
  165. /* A directory connection fetching a service descriptor can't have an
  166. * empty hidden service identifier. */
  167. continue;
  168. }
  169. if (!ed25519_pubkey_eq(identity_pk, &ident->identity_pk)) {
  170. continue;
  171. }
  172. ret = 1;
  173. break;
  174. } SMARTLIST_FOREACH_END(conn);
  175. /* No ownership of the objects in this list. */
  176. smartlist_free(conns);
  177. return ret;
  178. }
  179. /* We failed to fetch a descriptor for the service with <b>identity_pk</b>
  180. * because of <b>status</b>. Find all pending SOCKS connections for this
  181. * service that are waiting on the descriptor and close them with
  182. * <b>reason</b>. */
  183. static void
  184. close_all_socks_conns_waiting_for_desc(const ed25519_public_key_t *identity_pk,
  185. hs_client_fetch_status_t status,
  186. int reason)
  187. {
  188. unsigned int count = 0;
  189. time_t now = approx_time();
  190. smartlist_t *conns =
  191. connection_list_by_type_state(CONN_TYPE_AP, AP_CONN_STATE_RENDDESC_WAIT);
  192. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
  193. entry_connection_t *entry_conn = TO_ENTRY_CONN(base_conn);
  194. const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(entry_conn);
  195. /* Only consider the entry connections that matches the service for which
  196. * we tried to get the descriptor */
  197. if (!edge_conn->hs_ident ||
  198. !ed25519_pubkey_eq(identity_pk,
  199. &edge_conn->hs_ident->identity_pk)) {
  200. continue;
  201. }
  202. assert_connection_ok(base_conn, now);
  203. /* Unattach the entry connection which will close for the reason. */
  204. connection_mark_unattached_ap(entry_conn, reason);
  205. count++;
  206. } SMARTLIST_FOREACH_END(base_conn);
  207. if (count > 0) {
  208. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  209. hs_build_address(identity_pk, HS_VERSION_THREE, onion_address);
  210. log_notice(LD_REND, "Closed %u streams for service %s.onion "
  211. "for reason %s. Fetch status: %s.",
  212. count, safe_str_client(onion_address),
  213. stream_end_reason_to_string(reason),
  214. fetch_status_to_string(status));
  215. }
  216. /* No ownership of the object(s) in this list. */
  217. smartlist_free(conns);
  218. }
  219. /* Find all pending SOCKS connection waiting for a descriptor and retry them
  220. * all. This is called when the directory information changed. */
  221. static void
  222. retry_all_socks_conn_waiting_for_desc(void)
  223. {
  224. smartlist_t *conns =
  225. connection_list_by_type_state(CONN_TYPE_AP, AP_CONN_STATE_RENDDESC_WAIT);
  226. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
  227. hs_client_fetch_status_t status;
  228. const edge_connection_t *edge_conn =
  229. ENTRY_TO_EDGE_CONN(TO_ENTRY_CONN(base_conn));
  230. /* Ignore non HS or non v3 connection. */
  231. if (edge_conn->hs_ident == NULL) {
  232. continue;
  233. }
  234. /* In this loop, we will possibly try to fetch a descriptor for the
  235. * pending connections because we just got more directory information.
  236. * However, the refetch process can cleanup all SOCKS request so the same
  237. * service if an internal error happens. Thus, we can end up with closed
  238. * connections in our list. */
  239. if (base_conn->marked_for_close) {
  240. continue;
  241. }
  242. /* XXX: There is an optimization we could do which is that for a service
  243. * key, we could check if we can fetch and remember that decision. */
  244. /* Order a refetch in case it works this time. */
  245. status = hs_client_refetch_hsdesc(&edge_conn->hs_ident->identity_pk);
  246. if (BUG(status == HS_CLIENT_FETCH_HAVE_DESC)) {
  247. /* This case is unique because it can NOT happen in theory. Once we get
  248. * a new descriptor, the HS client subsystem is notified immediately and
  249. * the connections waiting for it are handled which means the state will
  250. * change from renddesc wait state. Log this and continue to next
  251. * connection. */
  252. continue;
  253. }
  254. /* In the case of an error, either all SOCKS connections have been
  255. * closed or we are still missing directory information. Leave the
  256. * connection in renddesc wait state so when we get more info, we'll be
  257. * able to try it again. */
  258. } SMARTLIST_FOREACH_END(base_conn);
  259. /* We don't have ownership of those objects. */
  260. smartlist_free(conns);
  261. }
  262. /* A v3 HS circuit successfully connected to the hidden service. Update the
  263. * stream state at <b>hs_conn_ident</b> appropriately. */
  264. static void
  265. note_connection_attempt_succeeded(const hs_ident_edge_conn_t *hs_conn_ident)
  266. {
  267. tor_assert(hs_conn_ident);
  268. /* Remove from the hid serv cache all requests for that service so we can
  269. * query the HSDir again later on for various reasons. */
  270. purge_hid_serv_request(&hs_conn_ident->identity_pk);
  271. /* The v2 subsystem cleans up the intro point time out flag at this stage.
  272. * We don't try to do it here because we still need to keep intact the intro
  273. * point state for future connections. Even though we are able to connect to
  274. * the service, doesn't mean we should reset the timed out intro points.
  275. *
  276. * It is not possible to have successfully connected to an intro point
  277. * present in our cache that was on error or timed out. Every entry in that
  278. * cache have a 2 minutes lifetime so ultimately the intro point(s) state
  279. * will be reset and thus possible to be retried. */
  280. }
  281. /* Given the pubkey of a hidden service in <b>onion_identity_pk</b>, fetch its
  282. * descriptor by launching a dir connection to <b>hsdir</b>. Return a
  283. * hs_client_fetch_status_t status code depending on how it went. */
  284. static hs_client_fetch_status_t
  285. directory_launch_v3_desc_fetch(const ed25519_public_key_t *onion_identity_pk,
  286. const routerstatus_t *hsdir)
  287. {
  288. uint64_t current_time_period = hs_get_time_period_num(0);
  289. ed25519_public_key_t blinded_pubkey;
  290. char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
  291. hs_ident_dir_conn_t hs_conn_dir_ident;
  292. int retval;
  293. tor_assert(hsdir);
  294. tor_assert(onion_identity_pk);
  295. /* Get blinded pubkey */
  296. hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
  297. current_time_period, &blinded_pubkey);
  298. /* ...and base64 it. */
  299. retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
  300. if (BUG(retval < 0)) {
  301. return HS_CLIENT_FETCH_ERROR;
  302. }
  303. /* Copy onion pk to a dir_ident so that we attach it to the dir conn */
  304. hs_ident_dir_conn_init(onion_identity_pk, &blinded_pubkey,
  305. &hs_conn_dir_ident);
  306. /* Setup directory request */
  307. directory_request_t *req =
  308. directory_request_new(DIR_PURPOSE_FETCH_HSDESC);
  309. directory_request_set_routerstatus(req, hsdir);
  310. directory_request_set_indirection(req, DIRIND_ANONYMOUS);
  311. directory_request_set_resource(req, base64_blinded_pubkey);
  312. directory_request_fetch_set_hs_ident(req, &hs_conn_dir_ident);
  313. directory_initiate_request(req);
  314. directory_request_free(req);
  315. log_info(LD_REND, "Descriptor fetch request for service %s with blinded "
  316. "key %s to directory %s",
  317. safe_str_client(ed25519_fmt(onion_identity_pk)),
  318. safe_str_client(base64_blinded_pubkey),
  319. safe_str_client(routerstatus_describe(hsdir)));
  320. /* Fire a REQUESTED event on the control port. */
  321. hs_control_desc_event_requested(onion_identity_pk, base64_blinded_pubkey,
  322. hsdir);
  323. /* Cleanup memory. */
  324. memwipe(&blinded_pubkey, 0, sizeof(blinded_pubkey));
  325. memwipe(base64_blinded_pubkey, 0, sizeof(base64_blinded_pubkey));
  326. memwipe(&hs_conn_dir_ident, 0, sizeof(hs_conn_dir_ident));
  327. return HS_CLIENT_FETCH_LAUNCHED;
  328. }
  329. /** Return the HSDir we should use to fetch the descriptor of the hidden
  330. * service with identity key <b>onion_identity_pk</b>. */
  331. STATIC routerstatus_t *
  332. pick_hsdir_v3(const ed25519_public_key_t *onion_identity_pk)
  333. {
  334. int retval;
  335. char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
  336. uint64_t current_time_period = hs_get_time_period_num(0);
  337. smartlist_t *responsible_hsdirs = NULL;
  338. ed25519_public_key_t blinded_pubkey;
  339. routerstatus_t *hsdir_rs = NULL;
  340. tor_assert(onion_identity_pk);
  341. /* Get blinded pubkey of hidden service */
  342. hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
  343. current_time_period, &blinded_pubkey);
  344. /* ...and base64 it. */
  345. retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
  346. if (BUG(retval < 0)) {
  347. return NULL;
  348. }
  349. /* Get responsible hsdirs of service for this time period */
  350. responsible_hsdirs = smartlist_new();
  351. hs_get_responsible_hsdirs(&blinded_pubkey, current_time_period,
  352. 0, 1, responsible_hsdirs);
  353. log_debug(LD_REND, "Found %d responsible HSDirs and about to pick one.",
  354. smartlist_len(responsible_hsdirs));
  355. /* Pick an HSDir from the responsible ones. The ownership of
  356. * responsible_hsdirs is given to this function so no need to free it. */
  357. hsdir_rs = hs_pick_hsdir(responsible_hsdirs, base64_blinded_pubkey);
  358. return hsdir_rs;
  359. }
  360. /** Fetch a v3 descriptor using the given <b>onion_identity_pk</b>.
  361. *
  362. * On success, HS_CLIENT_FETCH_LAUNCHED is returned. Otherwise, an error from
  363. * hs_client_fetch_status_t is returned. */
  364. MOCK_IMPL(STATIC hs_client_fetch_status_t,
  365. fetch_v3_desc, (const ed25519_public_key_t *onion_identity_pk))
  366. {
  367. routerstatus_t *hsdir_rs =NULL;
  368. tor_assert(onion_identity_pk);
  369. hsdir_rs = pick_hsdir_v3(onion_identity_pk);
  370. if (!hsdir_rs) {
  371. log_info(LD_REND, "Couldn't pick a v3 hsdir.");
  372. return HS_CLIENT_FETCH_NO_HSDIRS;
  373. }
  374. return directory_launch_v3_desc_fetch(onion_identity_pk, hsdir_rs);
  375. }
  376. /* Make sure that the given v3 origin circuit circ is a valid correct
  377. * introduction circuit. This will BUG() on any problems and hard assert if
  378. * the anonymity of the circuit is not ok. Return 0 on success else -1 where
  379. * the circuit should be mark for closed immediately. */
  380. static int
  381. intro_circ_is_ok(const origin_circuit_t *circ)
  382. {
  383. int ret = 0;
  384. tor_assert(circ);
  385. if (BUG(TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
  386. TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
  387. TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACKED)) {
  388. ret = -1;
  389. }
  390. if (BUG(circ->hs_ident == NULL)) {
  391. ret = -1;
  392. }
  393. if (BUG(!hs_ident_intro_circ_is_valid(circ->hs_ident))) {
  394. ret = -1;
  395. }
  396. /* This can stop the tor daemon but we want that since if we don't have
  397. * anonymity on this circuit, something went really wrong. */
  398. assert_circ_anonymity_ok(circ, get_options());
  399. return ret;
  400. }
  401. /* Find a descriptor intro point object that matches the given ident in the
  402. * given descriptor desc. Return NULL if not found. */
  403. static const hs_desc_intro_point_t *
  404. find_desc_intro_point_by_ident(const hs_ident_circuit_t *ident,
  405. const hs_descriptor_t *desc)
  406. {
  407. const hs_desc_intro_point_t *intro_point = NULL;
  408. tor_assert(ident);
  409. tor_assert(desc);
  410. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  411. const hs_desc_intro_point_t *, ip) {
  412. if (ed25519_pubkey_eq(&ident->intro_auth_pk,
  413. &ip->auth_key_cert->signed_key)) {
  414. intro_point = ip;
  415. break;
  416. }
  417. } SMARTLIST_FOREACH_END(ip);
  418. return intro_point;
  419. }
  420. /* Find a descriptor intro point object from the descriptor object desc that
  421. * matches the given legacy identity digest in legacy_id. Return NULL if not
  422. * found. */
  423. static hs_desc_intro_point_t *
  424. find_desc_intro_point_by_legacy_id(const char *legacy_id,
  425. const hs_descriptor_t *desc)
  426. {
  427. hs_desc_intro_point_t *ret_ip = NULL;
  428. tor_assert(legacy_id);
  429. tor_assert(desc);
  430. /* We will go over every intro point and try to find which one is linked to
  431. * that circuit. Those lists are small so it's not that expensive. */
  432. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  433. hs_desc_intro_point_t *, ip) {
  434. SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
  435. const hs_desc_link_specifier_t *, lspec) {
  436. /* Not all tor node have an ed25519 identity key so we still rely on the
  437. * legacy identity digest. */
  438. if (lspec->type != LS_LEGACY_ID) {
  439. continue;
  440. }
  441. if (fast_memneq(legacy_id, lspec->u.legacy_id, DIGEST_LEN)) {
  442. break;
  443. }
  444. /* Found it. */
  445. ret_ip = ip;
  446. goto end;
  447. } SMARTLIST_FOREACH_END(lspec);
  448. } SMARTLIST_FOREACH_END(ip);
  449. end:
  450. return ret_ip;
  451. }
  452. /* Send an INTRODUCE1 cell along the intro circuit and populate the rend
  453. * circuit identifier with the needed key material for the e2e encryption.
  454. * Return 0 on success, -1 if there is a transient error such that an action
  455. * has been taken to recover and -2 if there is a permanent error indicating
  456. * that both circuits were closed. */
  457. static int
  458. send_introduce1(origin_circuit_t *intro_circ,
  459. origin_circuit_t *rend_circ)
  460. {
  461. int status;
  462. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  463. const ed25519_public_key_t *service_identity_pk = NULL;
  464. const hs_desc_intro_point_t *ip;
  465. tor_assert(rend_circ);
  466. if (intro_circ_is_ok(intro_circ) < 0) {
  467. goto perm_err;
  468. }
  469. service_identity_pk = &intro_circ->hs_ident->identity_pk;
  470. /* For logging purposes. There will be a time where the hs_ident will have a
  471. * version number but for now there is none because it's all v3. */
  472. hs_build_address(service_identity_pk, HS_VERSION_THREE, onion_address);
  473. log_info(LD_REND, "Sending INTRODUCE1 cell to service %s on circuit %u",
  474. safe_str_client(onion_address), TO_CIRCUIT(intro_circ)->n_circ_id);
  475. /* 1) Get descriptor from our cache. */
  476. const hs_descriptor_t *desc =
  477. hs_cache_lookup_as_client(service_identity_pk);
  478. if (desc == NULL || !hs_client_any_intro_points_usable(service_identity_pk,
  479. desc)) {
  480. log_info(LD_REND, "Request to %s %s. Trying to fetch a new descriptor.",
  481. safe_str_client(onion_address),
  482. (desc) ? "didn't have usable intro points" :
  483. "didn't have a descriptor");
  484. hs_client_refetch_hsdesc(service_identity_pk);
  485. /* We just triggered a refetch, make sure every connections are back
  486. * waiting for that descriptor. */
  487. flag_all_conn_wait_desc(service_identity_pk);
  488. /* We just asked for a refetch so this is a transient error. */
  489. goto tran_err;
  490. }
  491. /* We need to find which intro point in the descriptor we are connected to
  492. * on intro_circ. */
  493. ip = find_desc_intro_point_by_ident(intro_circ->hs_ident, desc);
  494. if (BUG(ip == NULL)) {
  495. /* If we can find a descriptor from this introduction circuit ident, we
  496. * must have a valid intro point object. Permanent error. */
  497. goto perm_err;
  498. }
  499. /* Send the INTRODUCE1 cell. */
  500. if (hs_circ_send_introduce1(intro_circ, rend_circ, ip,
  501. desc->subcredential) < 0) {
  502. /* Unable to send the cell, the intro circuit has been marked for close so
  503. * this is a permanent error. */
  504. tor_assert_nonfatal(TO_CIRCUIT(intro_circ)->marked_for_close);
  505. goto perm_err;
  506. }
  507. /* Cell has been sent successfully. Copy the introduction point
  508. * authentication and encryption key in the rendezvous circuit identifier so
  509. * we can compute the ntor keys when we receive the RENDEZVOUS2 cell. */
  510. memcpy(&rend_circ->hs_ident->intro_enc_pk, &ip->enc_key,
  511. sizeof(rend_circ->hs_ident->intro_enc_pk));
  512. ed25519_pubkey_copy(&rend_circ->hs_ident->intro_auth_pk,
  513. &intro_circ->hs_ident->intro_auth_pk);
  514. /* Now, we wait for an ACK or NAK on this circuit. */
  515. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  516. CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT);
  517. /* Set timestamp_dirty, because circuit_expire_building expects it to
  518. * specify when a circuit entered the _C_INTRODUCE_ACK_WAIT state. */
  519. TO_CIRCUIT(intro_circ)->timestamp_dirty = time(NULL);
  520. pathbias_count_use_attempt(intro_circ);
  521. /* Success. */
  522. status = 0;
  523. goto end;
  524. perm_err:
  525. /* Permanent error: it is possible that the intro circuit was closed prior
  526. * because we weren't able to send the cell. Make sure we don't double close
  527. * it which would result in a warning. */
  528. if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
  529. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  530. }
  531. circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_INTERNAL);
  532. status = -2;
  533. goto end;
  534. tran_err:
  535. status = -1;
  536. end:
  537. memwipe(onion_address, 0, sizeof(onion_address));
  538. return status;
  539. }
  540. /* Using the introduction circuit circ, setup the authentication key of the
  541. * intro point this circuit has extended to. */
  542. static void
  543. setup_intro_circ_auth_key(origin_circuit_t *circ)
  544. {
  545. const hs_descriptor_t *desc;
  546. const hs_desc_intro_point_t *ip;
  547. tor_assert(circ);
  548. desc = hs_cache_lookup_as_client(&circ->hs_ident->identity_pk);
  549. if (BUG(desc == NULL)) {
  550. /* Opening intro circuit without the descriptor is no good... */
  551. goto end;
  552. }
  553. /* We will go over every intro point and try to find which one is linked to
  554. * that circuit. Those lists are small so it's not that expensive. */
  555. ip = find_desc_intro_point_by_legacy_id(
  556. circ->build_state->chosen_exit->identity_digest, desc);
  557. if (ip) {
  558. /* We got it, copy its authentication key to the identifier. */
  559. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  560. &ip->auth_key_cert->signed_key);
  561. goto end;
  562. }
  563. /* Reaching this point means we didn't find any intro point for this circuit
  564. * which is not suppose to happen. */
  565. tor_assert_nonfatal_unreached();
  566. end:
  567. return;
  568. }
  569. /* Called when an introduction circuit has opened. */
  570. static void
  571. client_intro_circ_has_opened(origin_circuit_t *circ)
  572. {
  573. tor_assert(circ);
  574. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  575. log_info(LD_REND, "Introduction circuit %u has opened. Attaching streams.",
  576. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  577. /* This is an introduction circuit so we'll attach the correct
  578. * authentication key to the circuit identifier so it can be identified
  579. * properly later on. */
  580. setup_intro_circ_auth_key(circ);
  581. connection_ap_attach_pending(1);
  582. }
  583. /* Called when a rendezvous circuit has opened. */
  584. static void
  585. client_rendezvous_circ_has_opened(origin_circuit_t *circ)
  586. {
  587. tor_assert(circ);
  588. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  589. const extend_info_t *rp_ei = circ->build_state->chosen_exit;
  590. /* Check that we didn't accidentally choose a node that does not understand
  591. * the v3 rendezvous protocol */
  592. if (rp_ei) {
  593. const node_t *rp_node = node_get_by_id(rp_ei->identity_digest);
  594. if (rp_node) {
  595. if (BUG(!node_supports_v3_rendezvous_point(rp_node))) {
  596. return;
  597. }
  598. }
  599. }
  600. log_info(LD_REND, "Rendezvous circuit has opened to %s.",
  601. safe_str_client(extend_info_describe(rp_ei)));
  602. /* Ignore returned value, nothing we can really do. On failure, the circuit
  603. * will be marked for close. */
  604. hs_circ_send_establish_rendezvous(circ);
  605. /* Register rend circuit in circuitmap if it's still alive. */
  606. if (!TO_CIRCUIT(circ)->marked_for_close) {
  607. hs_circuitmap_register_rend_circ_client_side(circ,
  608. circ->hs_ident->rendezvous_cookie);
  609. }
  610. }
  611. /* This is an helper function that convert a descriptor intro point object ip
  612. * to a newly allocated extend_info_t object fully initialized. Return NULL if
  613. * we can't convert it for which chances are that we are missing or malformed
  614. * link specifiers. */
  615. STATIC extend_info_t *
  616. desc_intro_point_to_extend_info(const hs_desc_intro_point_t *ip)
  617. {
  618. extend_info_t *ei;
  619. smartlist_t *lspecs = smartlist_new();
  620. tor_assert(ip);
  621. /* We first encode the descriptor link specifiers into the binary
  622. * representation which is a trunnel object. */
  623. SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
  624. const hs_desc_link_specifier_t *, desc_lspec) {
  625. link_specifier_t *lspec = hs_desc_lspec_to_trunnel(desc_lspec);
  626. smartlist_add(lspecs, lspec);
  627. } SMARTLIST_FOREACH_END(desc_lspec);
  628. /* Explicitly put the direct connection option to 0 because this is client
  629. * side and there is no such thing as a non anonymous client. */
  630. ei = hs_get_extend_info_from_lspecs(lspecs, &ip->onion_key, 0);
  631. SMARTLIST_FOREACH(lspecs, link_specifier_t *, ls, link_specifier_free(ls));
  632. smartlist_free(lspecs);
  633. return ei;
  634. }
  635. /* Return true iff the intro point ip for the service service_pk is usable.
  636. * This function checks if the intro point is in the client intro state cache
  637. * and checks at the failures. It is considered usable if:
  638. * - No error happened (INTRO_POINT_FAILURE_GENERIC)
  639. * - It is not flagged as timed out (INTRO_POINT_FAILURE_TIMEOUT)
  640. * - The unreachable count is lower than
  641. * MAX_INTRO_POINT_REACHABILITY_FAILURES (INTRO_POINT_FAILURE_UNREACHABLE)
  642. */
  643. static int
  644. intro_point_is_usable(const ed25519_public_key_t *service_pk,
  645. const hs_desc_intro_point_t *ip)
  646. {
  647. const hs_cache_intro_state_t *state;
  648. tor_assert(service_pk);
  649. tor_assert(ip);
  650. state = hs_cache_client_intro_state_find(service_pk,
  651. &ip->auth_key_cert->signed_key);
  652. if (state == NULL) {
  653. /* This means we've never encountered any problem thus usable. */
  654. goto usable;
  655. }
  656. if (state->error) {
  657. log_info(LD_REND, "Intro point with auth key %s had an error. Not usable",
  658. safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
  659. goto not_usable;
  660. }
  661. if (state->timed_out) {
  662. log_info(LD_REND, "Intro point with auth key %s timed out. Not usable",
  663. safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
  664. goto not_usable;
  665. }
  666. if (state->unreachable_count >= MAX_INTRO_POINT_REACHABILITY_FAILURES) {
  667. log_info(LD_REND, "Intro point with auth key %s unreachable. Not usable",
  668. safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)));
  669. goto not_usable;
  670. }
  671. usable:
  672. return 1;
  673. not_usable:
  674. return 0;
  675. }
  676. /* Using a descriptor desc, return a newly allocated extend_info_t object of a
  677. * randomly picked introduction point from its list. Return NULL if none are
  678. * usable. */
  679. STATIC extend_info_t *
  680. client_get_random_intro(const ed25519_public_key_t *service_pk)
  681. {
  682. extend_info_t *ei = NULL, *ei_excluded = NULL;
  683. smartlist_t *usable_ips = NULL;
  684. const hs_descriptor_t *desc;
  685. const hs_desc_encrypted_data_t *enc_data;
  686. const or_options_t *options = get_options();
  687. /* Calculate the onion address for logging purposes */
  688. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  689. tor_assert(service_pk);
  690. desc = hs_cache_lookup_as_client(service_pk);
  691. /* Assume the service is v3 if the descriptor is missing. This is ok,
  692. * because we only use the address in log messages */
  693. hs_build_address(service_pk,
  694. desc ? desc->plaintext_data.version : HS_VERSION_THREE,
  695. onion_address);
  696. if (desc == NULL || !hs_client_any_intro_points_usable(service_pk,
  697. desc)) {
  698. log_info(LD_REND, "Unable to randomly select an introduction point "
  699. "for service %s because descriptor %s. We can't connect.",
  700. safe_str_client(onion_address),
  701. (desc) ? "doesn't have any usable intro points"
  702. : "is missing (assuming v3 onion address)");
  703. goto end;
  704. }
  705. enc_data = &desc->encrypted_data;
  706. usable_ips = smartlist_new();
  707. smartlist_add_all(usable_ips, enc_data->intro_points);
  708. while (smartlist_len(usable_ips) != 0) {
  709. int idx;
  710. const hs_desc_intro_point_t *ip;
  711. /* Pick a random intro point and immediately remove it from the usable
  712. * list so we don't pick it again if we have to iterate more. */
  713. idx = crypto_rand_int(smartlist_len(usable_ips));
  714. ip = smartlist_get(usable_ips, idx);
  715. smartlist_del(usable_ips, idx);
  716. /* We need to make sure we have a usable intro points which is in a good
  717. * state in our cache. */
  718. if (!intro_point_is_usable(service_pk, ip)) {
  719. continue;
  720. }
  721. /* Generate an extend info object from the intro point object. */
  722. ei = desc_intro_point_to_extend_info(ip);
  723. if (ei == NULL) {
  724. /* We can get here for instance if the intro point is a private address
  725. * and we aren't allowed to extend to those. */
  726. log_info(LD_REND, "Unable to select introduction point with auth key %s "
  727. "for service %s, because we could not extend to it.",
  728. safe_str_client(ed25519_fmt(&ip->auth_key_cert->signed_key)),
  729. safe_str_client(onion_address));
  730. continue;
  731. }
  732. /* Test the pick against ExcludeNodes. */
  733. if (routerset_contains_extendinfo(options->ExcludeNodes, ei)) {
  734. /* If this pick is in the ExcludeNodes list, we keep its reference so if
  735. * we ever end up not being able to pick anything else and StrictNodes is
  736. * unset, we'll use it. */
  737. if (ei_excluded) {
  738. /* If something was already here free it. After the loop is gone we
  739. * will examine the last excluded intro point, and that's fine since
  740. * that's random anyway */
  741. extend_info_free(ei_excluded);
  742. }
  743. ei_excluded = ei;
  744. continue;
  745. }
  746. /* Good pick! Let's go with this. */
  747. goto end;
  748. }
  749. /* Reaching this point means a couple of things. Either we can't use any of
  750. * the intro point listed because the IP address can't be extended to or it
  751. * is listed in the ExcludeNodes list. In the later case, if StrictNodes is
  752. * set, we are forced to not use anything. */
  753. ei = ei_excluded;
  754. if (options->StrictNodes) {
  755. log_warn(LD_REND, "Every introduction point for service %s is in the "
  756. "ExcludeNodes set and StrictNodes is set. We can't connect.",
  757. safe_str_client(onion_address));
  758. extend_info_free(ei);
  759. ei = NULL;
  760. } else {
  761. log_fn(LOG_PROTOCOL_WARN, LD_REND, "Every introduction point for service "
  762. "%s is unusable or we can't extend to it. We can't connect.",
  763. safe_str_client(onion_address));
  764. }
  765. end:
  766. smartlist_free(usable_ips);
  767. memwipe(onion_address, 0, sizeof(onion_address));
  768. return ei;
  769. }
  770. /* For this introduction circuit, we'll look at if we have any usable
  771. * introduction point left for this service. If so, we'll use the circuit to
  772. * re-extend to a new intro point. Else, we'll close the circuit and its
  773. * corresponding rendezvous circuit. Return 0 if we are re-extending else -1
  774. * if we are closing the circuits.
  775. *
  776. * This is called when getting an INTRODUCE_ACK cell with a NACK. */
  777. static int
  778. close_or_reextend_intro_circ(origin_circuit_t *intro_circ)
  779. {
  780. int ret = -1;
  781. const hs_descriptor_t *desc;
  782. origin_circuit_t *rend_circ;
  783. tor_assert(intro_circ);
  784. desc = hs_cache_lookup_as_client(&intro_circ->hs_ident->identity_pk);
  785. if (BUG(desc == NULL)) {
  786. /* We can't continue without a descriptor. */
  787. goto close;
  788. }
  789. /* We still have the descriptor, great! Let's try to see if we can
  790. * re-extend by looking up if there are any usable intro points. */
  791. if (!hs_client_any_intro_points_usable(&intro_circ->hs_ident->identity_pk,
  792. desc)) {
  793. goto close;
  794. }
  795. /* Try to re-extend now. */
  796. if (hs_client_reextend_intro_circuit(intro_circ) < 0) {
  797. goto close;
  798. }
  799. /* Success on re-extending. Don't return an error. */
  800. ret = 0;
  801. goto end;
  802. close:
  803. /* Change the intro circuit purpose before so we don't report an intro point
  804. * failure again triggering an extra descriptor fetch. The circuit can
  805. * already be closed on failure to re-extend. */
  806. if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
  807. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  808. CIRCUIT_PURPOSE_C_INTRODUCE_ACKED);
  809. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_FINISHED);
  810. }
  811. /* Close the related rendezvous circuit. */
  812. rend_circ = hs_circuitmap_get_rend_circ_client_side(
  813. intro_circ->hs_ident->rendezvous_cookie);
  814. /* The rendezvous circuit might have collapsed while the INTRODUCE_ACK was
  815. * inflight so we can't expect one every time. */
  816. if (rend_circ) {
  817. circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_FINISHED);
  818. }
  819. end:
  820. return ret;
  821. }
  822. /* Called when we get an INTRODUCE_ACK success status code. Do the appropriate
  823. * actions for the rendezvous point and finally close intro_circ. */
  824. static void
  825. handle_introduce_ack_success(origin_circuit_t *intro_circ)
  826. {
  827. origin_circuit_t *rend_circ = NULL;
  828. tor_assert(intro_circ);
  829. log_info(LD_REND, "Received INTRODUCE_ACK ack! Informing rendezvous");
  830. /* Get the rendezvous circuit for this rendezvous cookie. */
  831. uint8_t *rendezvous_cookie = intro_circ->hs_ident->rendezvous_cookie;
  832. rend_circ =
  833. hs_circuitmap_get_established_rend_circ_client_side(rendezvous_cookie);
  834. if (rend_circ == NULL) {
  835. log_warn(LD_REND, "Can't find any rendezvous circuit. Stopping");
  836. goto end;
  837. }
  838. assert_circ_anonymity_ok(rend_circ, get_options());
  839. /* It is possible to get a RENDEZVOUS2 cell before the INTRODUCE_ACK which
  840. * means that the circuit will be joined and already transmitting data. In
  841. * that case, simply skip the purpose change and close the intro circuit
  842. * like it should be. */
  843. if (TO_CIRCUIT(rend_circ)->purpose == CIRCUIT_PURPOSE_C_REND_JOINED) {
  844. goto end;
  845. }
  846. circuit_change_purpose(TO_CIRCUIT(rend_circ),
  847. CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED);
  848. /* Set timestamp_dirty, because circuit_expire_building expects it to
  849. * specify when a circuit entered the
  850. * CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED state. */
  851. TO_CIRCUIT(rend_circ)->timestamp_dirty = time(NULL);
  852. end:
  853. /* We don't need the intro circuit anymore. It did what it had to do! */
  854. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  855. CIRCUIT_PURPOSE_C_INTRODUCE_ACKED);
  856. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_FINISHED);
  857. /* XXX: Close pending intro circuits we might have in parallel. */
  858. return;
  859. }
  860. /* Called when we get an INTRODUCE_ACK failure status code. Depending on our
  861. * failure cache status, either close the circuit or re-extend to a new
  862. * introduction point. */
  863. static void
  864. handle_introduce_ack_bad(origin_circuit_t *circ, int status)
  865. {
  866. tor_assert(circ);
  867. log_info(LD_REND, "Received INTRODUCE_ACK nack by %s. Reason: %u",
  868. safe_str_client(extend_info_describe(circ->build_state->chosen_exit)),
  869. status);
  870. /* It's a NAK. The introduction point didn't relay our request. */
  871. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_INTRODUCING);
  872. /* Note down this failure in the intro point failure cache. Depending on how
  873. * many times we've tried this intro point, close it or reextend. */
  874. hs_cache_client_intro_state_note(&circ->hs_ident->identity_pk,
  875. &circ->hs_ident->intro_auth_pk,
  876. INTRO_POINT_FAILURE_GENERIC);
  877. }
  878. /* Called when we get an INTRODUCE_ACK on the intro circuit circ. The encoded
  879. * cell is in payload of length payload_len. Return 0 on success else a
  880. * negative value. The circuit is either close or reuse to re-extend to a new
  881. * introduction point. */
  882. static int
  883. handle_introduce_ack(origin_circuit_t *circ, const uint8_t *payload,
  884. size_t payload_len)
  885. {
  886. int status, ret = -1;
  887. tor_assert(circ);
  888. tor_assert(circ->build_state);
  889. tor_assert(circ->build_state->chosen_exit);
  890. assert_circ_anonymity_ok(circ, get_options());
  891. tor_assert(payload);
  892. status = hs_cell_parse_introduce_ack(payload, payload_len);
  893. switch (status) {
  894. case HS_CELL_INTRO_ACK_SUCCESS:
  895. ret = 0;
  896. handle_introduce_ack_success(circ);
  897. goto end;
  898. case HS_CELL_INTRO_ACK_FAILURE:
  899. case HS_CELL_INTRO_ACK_BADFMT:
  900. case HS_CELL_INTRO_ACK_NORELAY:
  901. handle_introduce_ack_bad(circ, status);
  902. /* We are going to see if we have to close the circuits (IP and RP) or we
  903. * can re-extend to a new intro point. */
  904. ret = close_or_reextend_intro_circ(circ);
  905. break;
  906. default:
  907. log_info(LD_PROTOCOL, "Unknown INTRODUCE_ACK status code %u from %s",
  908. status,
  909. safe_str_client(extend_info_describe(circ->build_state->chosen_exit)));
  910. break;
  911. }
  912. end:
  913. return ret;
  914. }
  915. /* Called when we get a RENDEZVOUS2 cell on the rendezvous circuit circ. The
  916. * encoded cell is in payload of length payload_len. Return 0 on success or a
  917. * negative value on error. On error, the circuit is marked for close. */
  918. STATIC int
  919. handle_rendezvous2(origin_circuit_t *circ, const uint8_t *payload,
  920. size_t payload_len)
  921. {
  922. int ret = -1;
  923. curve25519_public_key_t server_pk;
  924. uint8_t auth_mac[DIGEST256_LEN] = {0};
  925. uint8_t handshake_info[CURVE25519_PUBKEY_LEN + sizeof(auth_mac)] = {0};
  926. hs_ntor_rend_cell_keys_t keys;
  927. const hs_ident_circuit_t *ident;
  928. tor_assert(circ);
  929. tor_assert(payload);
  930. /* Make things easier. */
  931. ident = circ->hs_ident;
  932. tor_assert(ident);
  933. if (hs_cell_parse_rendezvous2(payload, payload_len, handshake_info,
  934. sizeof(handshake_info)) < 0) {
  935. goto err;
  936. }
  937. /* Get from the handshake info the SERVER_PK and AUTH_MAC. */
  938. memcpy(&server_pk, handshake_info, CURVE25519_PUBKEY_LEN);
  939. memcpy(auth_mac, handshake_info + CURVE25519_PUBKEY_LEN, sizeof(auth_mac));
  940. /* Generate the handshake info. */
  941. if (hs_ntor_client_get_rendezvous1_keys(&ident->intro_auth_pk,
  942. &ident->rendezvous_client_kp,
  943. &ident->intro_enc_pk, &server_pk,
  944. &keys) < 0) {
  945. log_info(LD_REND, "Unable to compute the rendezvous keys.");
  946. goto err;
  947. }
  948. /* Critical check, make sure that the MAC matches what we got with what we
  949. * computed just above. */
  950. if (!hs_ntor_client_rendezvous2_mac_is_good(&keys, auth_mac)) {
  951. log_info(LD_REND, "Invalid MAC in RENDEZVOUS2. Rejecting cell.");
  952. goto err;
  953. }
  954. /* Setup the e2e encryption on the circuit and finalize its state. */
  955. if (hs_circuit_setup_e2e_rend_circ(circ, keys.ntor_key_seed,
  956. sizeof(keys.ntor_key_seed), 0) < 0) {
  957. log_info(LD_REND, "Unable to setup the e2e encryption.");
  958. goto err;
  959. }
  960. /* Success. Hidden service connection finalized! */
  961. ret = 0;
  962. goto end;
  963. err:
  964. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  965. end:
  966. memwipe(&keys, 0, sizeof(keys));
  967. return ret;
  968. }
  969. /* Return true iff the client can fetch a descriptor for this service public
  970. * identity key and status_out if not NULL is untouched. If the client can
  971. * _not_ fetch the descriptor and if status_out is not NULL, it is set with
  972. * the fetch status code. */
  973. static unsigned int
  974. can_client_refetch_desc(const ed25519_public_key_t *identity_pk,
  975. hs_client_fetch_status_t *status_out)
  976. {
  977. hs_client_fetch_status_t status;
  978. tor_assert(identity_pk);
  979. /* Are we configured to fetch descriptors? */
  980. if (!get_options()->FetchHidServDescriptors) {
  981. log_warn(LD_REND, "We received an onion address for a hidden service "
  982. "descriptor but we are configured to not fetch.");
  983. status = HS_CLIENT_FETCH_NOT_ALLOWED;
  984. goto cannot;
  985. }
  986. /* Without a live consensus we can't do any client actions. It is needed to
  987. * compute the hashring for a service. */
  988. if (!networkstatus_get_live_consensus(approx_time())) {
  989. log_info(LD_REND, "Can't fetch descriptor for service %s because we "
  990. "are missing a live consensus. Stalling connection.",
  991. safe_str_client(ed25519_fmt(identity_pk)));
  992. status = HS_CLIENT_FETCH_MISSING_INFO;
  993. goto cannot;
  994. }
  995. if (!router_have_minimum_dir_info()) {
  996. log_info(LD_REND, "Can't fetch descriptor for service %s because we "
  997. "dont have enough descriptors. Stalling connection.",
  998. safe_str_client(ed25519_fmt(identity_pk)));
  999. status = HS_CLIENT_FETCH_MISSING_INFO;
  1000. goto cannot;
  1001. }
  1002. /* Check if fetching a desc for this HS is useful to us right now */
  1003. {
  1004. const hs_descriptor_t *cached_desc = NULL;
  1005. cached_desc = hs_cache_lookup_as_client(identity_pk);
  1006. if (cached_desc && hs_client_any_intro_points_usable(identity_pk,
  1007. cached_desc)) {
  1008. log_info(LD_GENERAL, "We would fetch a v3 hidden service descriptor "
  1009. "but we already have a usable descriptor.");
  1010. status = HS_CLIENT_FETCH_HAVE_DESC;
  1011. goto cannot;
  1012. }
  1013. }
  1014. /* Don't try to refetch while we have a pending request for it. */
  1015. if (directory_request_is_pending(identity_pk)) {
  1016. log_info(LD_REND, "Already a pending directory request. Waiting on it.");
  1017. status = HS_CLIENT_FETCH_PENDING;
  1018. goto cannot;
  1019. }
  1020. /* Yes, client can fetch! */
  1021. return 1;
  1022. cannot:
  1023. if (status_out) {
  1024. *status_out = status;
  1025. }
  1026. return 0;
  1027. }
  1028. /* ========== */
  1029. /* Public API */
  1030. /* ========== */
  1031. /** A circuit just finished connecting to a hidden service that the stream
  1032. * <b>conn</b> has been waiting for. Let the HS subsystem know about this. */
  1033. void
  1034. hs_client_note_connection_attempt_succeeded(const edge_connection_t *conn)
  1035. {
  1036. tor_assert(connection_edge_is_rendezvous_stream(conn));
  1037. if (BUG(conn->rend_data && conn->hs_ident)) {
  1038. log_warn(LD_BUG, "Stream had both rend_data and hs_ident..."
  1039. "Prioritizing hs_ident");
  1040. }
  1041. if (conn->hs_ident) { /* It's v3: pass it to the prop224 handler */
  1042. note_connection_attempt_succeeded(conn->hs_ident);
  1043. return;
  1044. } else if (conn->rend_data) { /* It's v2: pass it to the legacy handler */
  1045. rend_client_note_connection_attempt_ended(conn->rend_data);
  1046. return;
  1047. }
  1048. }
  1049. /* With the given encoded descriptor in desc_str and the service key in
  1050. * service_identity_pk, decode the descriptor and set the desc pointer with a
  1051. * newly allocated descriptor object.
  1052. *
  1053. * Return 0 on success else a negative value and desc is set to NULL. */
  1054. int
  1055. hs_client_decode_descriptor(const char *desc_str,
  1056. const ed25519_public_key_t *service_identity_pk,
  1057. hs_descriptor_t **desc)
  1058. {
  1059. int ret;
  1060. uint8_t subcredential[DIGEST256_LEN];
  1061. ed25519_public_key_t blinded_pubkey;
  1062. tor_assert(desc_str);
  1063. tor_assert(service_identity_pk);
  1064. tor_assert(desc);
  1065. /* Create subcredential for this HS so that we can decrypt */
  1066. {
  1067. uint64_t current_time_period = hs_get_time_period_num(0);
  1068. hs_build_blinded_pubkey(service_identity_pk, NULL, 0, current_time_period,
  1069. &blinded_pubkey);
  1070. hs_get_subcredential(service_identity_pk, &blinded_pubkey, subcredential);
  1071. }
  1072. /* Parse descriptor */
  1073. ret = hs_desc_decode_descriptor(desc_str, subcredential, desc);
  1074. memwipe(subcredential, 0, sizeof(subcredential));
  1075. if (ret < 0) {
  1076. log_warn(LD_GENERAL, "Could not parse received descriptor as client.");
  1077. if (get_options()->SafeLogging_ == SAFELOG_SCRUB_NONE) {
  1078. log_warn(LD_GENERAL, "%s", escaped(desc_str));
  1079. }
  1080. goto err;
  1081. }
  1082. /* Make sure the descriptor signing key cross certifies with the computed
  1083. * blinded key. Without this validation, anyone knowing the subcredential
  1084. * and onion address can forge a descriptor. */
  1085. tor_cert_t *cert = (*desc)->plaintext_data.signing_key_cert;
  1086. if (tor_cert_checksig(cert,
  1087. &blinded_pubkey, approx_time()) < 0) {
  1088. log_warn(LD_GENERAL, "Descriptor signing key certificate signature "
  1089. "doesn't validate with computed blinded key: %s",
  1090. tor_cert_describe_signature_status(cert));
  1091. goto err;
  1092. }
  1093. return 0;
  1094. err:
  1095. return -1;
  1096. }
  1097. /* Return true iff there are at least one usable intro point in the service
  1098. * descriptor desc. */
  1099. int
  1100. hs_client_any_intro_points_usable(const ed25519_public_key_t *service_pk,
  1101. const hs_descriptor_t *desc)
  1102. {
  1103. tor_assert(service_pk);
  1104. tor_assert(desc);
  1105. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  1106. const hs_desc_intro_point_t *, ip) {
  1107. if (intro_point_is_usable(service_pk, ip)) {
  1108. goto usable;
  1109. }
  1110. } SMARTLIST_FOREACH_END(ip);
  1111. return 0;
  1112. usable:
  1113. return 1;
  1114. }
  1115. /** Launch a connection to a hidden service directory to fetch a hidden
  1116. * service descriptor using <b>identity_pk</b> to get the necessary keys.
  1117. *
  1118. * A hs_client_fetch_status_t code is returned. */
  1119. int
  1120. hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
  1121. {
  1122. hs_client_fetch_status_t status;
  1123. tor_assert(identity_pk);
  1124. if (!can_client_refetch_desc(identity_pk, &status)) {
  1125. return status;
  1126. }
  1127. /* Try to fetch the desc and if we encounter an unrecoverable error, mark
  1128. * the desc as unavailable for now. */
  1129. status = fetch_v3_desc(identity_pk);
  1130. if (fetch_status_should_close_socks(status)) {
  1131. close_all_socks_conns_waiting_for_desc(identity_pk, status,
  1132. END_STREAM_REASON_RESOLVEFAILED);
  1133. /* Remove HSDir fetch attempts so that we can retry later if the user
  1134. * wants us to regardless of if we closed any connections. */
  1135. purge_hid_serv_request(identity_pk);
  1136. }
  1137. return status;
  1138. }
  1139. /* This is called when we are trying to attach an AP connection to these
  1140. * hidden service circuits from connection_ap_handshake_attach_circuit().
  1141. * Return 0 on success, -1 for a transient error that is actions were
  1142. * triggered to recover or -2 for a permenent error where both circuits will
  1143. * marked for close.
  1144. *
  1145. * The following supports every hidden service version. */
  1146. int
  1147. hs_client_send_introduce1(origin_circuit_t *intro_circ,
  1148. origin_circuit_t *rend_circ)
  1149. {
  1150. return (intro_circ->hs_ident) ? send_introduce1(intro_circ, rend_circ) :
  1151. rend_client_send_introduction(intro_circ,
  1152. rend_circ);
  1153. }
  1154. /* Called when the client circuit circ has been established. It can be either
  1155. * an introduction or rendezvous circuit. This function handles all hidden
  1156. * service versions. */
  1157. void
  1158. hs_client_circuit_has_opened(origin_circuit_t *circ)
  1159. {
  1160. tor_assert(circ);
  1161. /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
  1162. * identifier hs_ident. Can't be both. */
  1163. switch (TO_CIRCUIT(circ)->purpose) {
  1164. case CIRCUIT_PURPOSE_C_INTRODUCING:
  1165. if (circ->hs_ident) {
  1166. client_intro_circ_has_opened(circ);
  1167. } else {
  1168. rend_client_introcirc_has_opened(circ);
  1169. }
  1170. break;
  1171. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  1172. if (circ->hs_ident) {
  1173. client_rendezvous_circ_has_opened(circ);
  1174. } else {
  1175. rend_client_rendcirc_has_opened(circ);
  1176. }
  1177. break;
  1178. default:
  1179. tor_assert_nonfatal_unreached();
  1180. }
  1181. }
  1182. /* Called when we receive a RENDEZVOUS_ESTABLISHED cell. Change the state of
  1183. * the circuit to CIRCUIT_PURPOSE_C_REND_READY. Return 0 on success else a
  1184. * negative value and the circuit marked for close. */
  1185. int
  1186. hs_client_receive_rendezvous_acked(origin_circuit_t *circ,
  1187. const uint8_t *payload, size_t payload_len)
  1188. {
  1189. tor_assert(circ);
  1190. tor_assert(payload);
  1191. (void) payload_len;
  1192. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  1193. log_warn(LD_PROTOCOL, "Got a RENDEZVOUS_ESTABLISHED but we were not "
  1194. "expecting one. Closing circuit.");
  1195. goto err;
  1196. }
  1197. log_info(LD_REND, "Received an RENDEZVOUS_ESTABLISHED. This circuit is "
  1198. "now ready for rendezvous.");
  1199. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_REND_READY);
  1200. /* Set timestamp_dirty, because circuit_expire_building expects it to
  1201. * specify when a circuit entered the _C_REND_READY state. */
  1202. TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
  1203. /* From a path bias point of view, this circuit is now successfully used.
  1204. * Waiting any longer opens us up to attacks from malicious hidden services.
  1205. * They could induce the client to attempt to connect to their hidden
  1206. * service and never reply to the client's rend requests */
  1207. pathbias_mark_use_success(circ);
  1208. /* If we already have the introduction circuit built, make sure we send
  1209. * the INTRODUCE cell _now_ */
  1210. connection_ap_attach_pending(1);
  1211. return 0;
  1212. err:
  1213. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  1214. return -1;
  1215. }
  1216. /* This is called when a descriptor has arrived following a fetch request and
  1217. * has been stored in the client cache. Every entry connection that matches
  1218. * the service identity key in the ident will get attached to the hidden
  1219. * service circuit. */
  1220. void
  1221. hs_client_desc_has_arrived(const hs_ident_dir_conn_t *ident)
  1222. {
  1223. time_t now = time(NULL);
  1224. smartlist_t *conns = NULL;
  1225. tor_assert(ident);
  1226. conns = connection_list_by_type_state(CONN_TYPE_AP,
  1227. AP_CONN_STATE_RENDDESC_WAIT);
  1228. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
  1229. const hs_descriptor_t *desc;
  1230. entry_connection_t *entry_conn = TO_ENTRY_CONN(base_conn);
  1231. const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(entry_conn);
  1232. /* Only consider the entry connections that matches the service for which
  1233. * we just fetched its descriptor. */
  1234. if (!edge_conn->hs_ident ||
  1235. !ed25519_pubkey_eq(&ident->identity_pk,
  1236. &edge_conn->hs_ident->identity_pk)) {
  1237. continue;
  1238. }
  1239. assert_connection_ok(base_conn, now);
  1240. /* We were just called because we stored the descriptor for this service
  1241. * so not finding a descriptor means we have a bigger problem. */
  1242. desc = hs_cache_lookup_as_client(&ident->identity_pk);
  1243. if (BUG(desc == NULL)) {
  1244. goto end;
  1245. }
  1246. if (!hs_client_any_intro_points_usable(&ident->identity_pk, desc)) {
  1247. log_info(LD_REND, "Hidden service descriptor is unusable. "
  1248. "Closing streams.");
  1249. connection_mark_unattached_ap(entry_conn,
  1250. END_STREAM_REASON_RESOLVEFAILED);
  1251. /* We are unable to use the descriptor so remove the directory request
  1252. * from the cache so the next connection can try again. */
  1253. note_connection_attempt_succeeded(edge_conn->hs_ident);
  1254. goto end;
  1255. }
  1256. log_info(LD_REND, "Descriptor has arrived. Launching circuits.");
  1257. /* Because the connection can now proceed to opening circuit and
  1258. * ultimately connect to the service, reset those timestamp so the
  1259. * connection is considered "fresh" and can continue without being closed
  1260. * too early. */
  1261. base_conn->timestamp_created = now;
  1262. base_conn->timestamp_last_read_allowed = now;
  1263. base_conn->timestamp_last_write_allowed = now;
  1264. /* Change connection's state into waiting for a circuit. */
  1265. base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  1266. connection_ap_mark_as_pending_circuit(entry_conn);
  1267. } SMARTLIST_FOREACH_END(base_conn);
  1268. end:
  1269. /* We don't have ownership of the objects in this list. */
  1270. smartlist_free(conns);
  1271. }
  1272. /* Return a newly allocated extend_info_t for a randomly chosen introduction
  1273. * point for the given edge connection identifier ident. Return NULL if we
  1274. * can't pick any usable introduction points. */
  1275. extend_info_t *
  1276. hs_client_get_random_intro_from_edge(const edge_connection_t *edge_conn)
  1277. {
  1278. tor_assert(edge_conn);
  1279. return (edge_conn->hs_ident) ?
  1280. client_get_random_intro(&edge_conn->hs_ident->identity_pk) :
  1281. rend_client_get_random_intro(edge_conn->rend_data);
  1282. }
  1283. /* Called when get an INTRODUCE_ACK cell on the introduction circuit circ.
  1284. * Return 0 on success else a negative value is returned. The circuit will be
  1285. * closed or reuse to extend again to another intro point. */
  1286. int
  1287. hs_client_receive_introduce_ack(origin_circuit_t *circ,
  1288. const uint8_t *payload, size_t payload_len)
  1289. {
  1290. int ret = -1;
  1291. tor_assert(circ);
  1292. tor_assert(payload);
  1293. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1294. log_warn(LD_PROTOCOL, "Unexpected INTRODUCE_ACK on circuit %u.",
  1295. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  1296. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  1297. goto end;
  1298. }
  1299. ret = (circ->hs_ident) ? handle_introduce_ack(circ, payload, payload_len) :
  1300. rend_client_introduction_acked(circ, payload,
  1301. payload_len);
  1302. /* For path bias: This circuit was used successfully. NACK or ACK counts. */
  1303. pathbias_mark_use_success(circ);
  1304. end:
  1305. return ret;
  1306. }
  1307. /* Called when get a RENDEZVOUS2 cell on the rendezvous circuit circ. Return
  1308. * 0 on success else a negative value is returned. The circuit will be closed
  1309. * on error. */
  1310. int
  1311. hs_client_receive_rendezvous2(origin_circuit_t *circ,
  1312. const uint8_t *payload, size_t payload_len)
  1313. {
  1314. int ret = -1;
  1315. tor_assert(circ);
  1316. tor_assert(payload);
  1317. /* Circuit can possibly be in both state because we could receive a
  1318. * RENDEZVOUS2 cell before the INTRODUCE_ACK has been received. */
  1319. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  1320. TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  1321. log_warn(LD_PROTOCOL, "Unexpected RENDEZVOUS2 cell on circuit %u. "
  1322. "Closing circuit.",
  1323. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  1324. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  1325. goto end;
  1326. }
  1327. log_info(LD_REND, "Got RENDEZVOUS2 cell from hidden service on circuit %u.",
  1328. TO_CIRCUIT(circ)->n_circ_id);
  1329. ret = (circ->hs_ident) ? handle_rendezvous2(circ, payload, payload_len) :
  1330. rend_client_receive_rendezvous(circ, payload,
  1331. payload_len);
  1332. end:
  1333. return ret;
  1334. }
  1335. /* Extend the introduction circuit circ to another valid introduction point
  1336. * for the hidden service it is trying to connect to, or mark it and launch a
  1337. * new circuit if we can't extend it. Return 0 on success or possible
  1338. * success. Return -1 and mark the introduction circuit for close on permanent
  1339. * failure.
  1340. *
  1341. * On failure, the caller is responsible for marking the associated rendezvous
  1342. * circuit for close. */
  1343. int
  1344. hs_client_reextend_intro_circuit(origin_circuit_t *circ)
  1345. {
  1346. int ret = -1;
  1347. extend_info_t *ei;
  1348. tor_assert(circ);
  1349. ei = (circ->hs_ident) ?
  1350. client_get_random_intro(&circ->hs_ident->identity_pk) :
  1351. rend_client_get_random_intro(circ->rend_data);
  1352. if (ei == NULL) {
  1353. log_warn(LD_REND, "No usable introduction points left. Closing.");
  1354. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  1355. goto end;
  1356. }
  1357. if (circ->remaining_relay_early_cells) {
  1358. log_info(LD_REND, "Re-extending circ %u, this time to %s.",
  1359. (unsigned int) TO_CIRCUIT(circ)->n_circ_id,
  1360. safe_str_client(extend_info_describe(ei)));
  1361. ret = circuit_extend_to_new_exit(circ, ei);
  1362. if (ret == 0) {
  1363. /* We were able to extend so update the timestamp so we avoid expiring
  1364. * this circuit too early. The intro circuit is short live so the
  1365. * linkability issue is minimized, we just need the circuit to hold a
  1366. * bit longer so we can introduce. */
  1367. TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
  1368. }
  1369. } else {
  1370. log_info(LD_REND, "Closing intro circ %u (out of RELAY_EARLY cells).",
  1371. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  1372. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
  1373. /* connection_ap_handshake_attach_circuit will launch a new intro circ. */
  1374. ret = 0;
  1375. }
  1376. end:
  1377. extend_info_free(ei);
  1378. return ret;
  1379. }
  1380. /* Release all the storage held by the client subsystem. */
  1381. void
  1382. hs_client_free_all(void)
  1383. {
  1384. /* Purge the hidden service request cache. */
  1385. hs_purge_last_hid_serv_requests();
  1386. }
  1387. /* Purge all potentially remotely-detectable state held in the hidden
  1388. * service client code. Called on SIGNAL NEWNYM. */
  1389. void
  1390. hs_client_purge_state(void)
  1391. {
  1392. /* v2 subsystem. */
  1393. rend_client_purge_state();
  1394. /* Cancel all descriptor fetches. Do this first so once done we are sure
  1395. * that our descriptor cache won't modified. */
  1396. cancel_descriptor_fetches();
  1397. /* Purge the introduction point state cache. */
  1398. hs_cache_client_intro_state_purge();
  1399. /* Purge the descriptor cache. */
  1400. hs_cache_purge_as_client();
  1401. /* Purge the last hidden service request cache. */
  1402. hs_purge_last_hid_serv_requests();
  1403. log_info(LD_REND, "Hidden service client state has been purged.");
  1404. }
  1405. /* Called when our directory information has changed. */
  1406. void
  1407. hs_client_dir_info_changed(void)
  1408. {
  1409. /* We have possibly reached the minimum directory information or new
  1410. * consensus so retry all pending SOCKS connection in
  1411. * AP_CONN_STATE_RENDDESC_WAIT state in order to fetch the descriptor. */
  1412. retry_all_socks_conn_waiting_for_desc();
  1413. }