hs_client.c 41 KB

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