hs_client.c 45 KB

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