hs_client.c 48 KB

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