hs_client.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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. /* Get all connections that are waiting on a circuit and flag them back to
  28. * waiting for a hidden service descriptor for the given service key
  29. * service_identity_pk. */
  30. static void
  31. flag_all_conn_wait_desc(const ed25519_public_key_t *service_identity_pk)
  32. {
  33. tor_assert(service_identity_pk);
  34. smartlist_t *conns =
  35. connection_list_by_type_state(CONN_TYPE_AP, AP_CONN_STATE_CIRCUIT_WAIT);
  36. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  37. edge_connection_t *edge_conn;
  38. if (BUG(!CONN_IS_EDGE(conn))) {
  39. continue;
  40. }
  41. edge_conn = TO_EDGE_CONN(conn);
  42. if (edge_conn->hs_ident &&
  43. ed25519_pubkey_eq(&edge_conn->hs_ident->identity_pk,
  44. service_identity_pk)) {
  45. connection_ap_mark_as_non_pending_circuit(TO_ENTRY_CONN(conn));
  46. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  47. }
  48. } SMARTLIST_FOREACH_END(conn);
  49. smartlist_free(conns);
  50. }
  51. /* A v3 HS circuit successfully connected to the hidden service. Update the
  52. * stream state at <b>hs_conn_ident</b> appropriately. */
  53. static void
  54. note_connection_attempt_succeeded(const hs_ident_edge_conn_t *hs_conn_ident)
  55. {
  56. (void) hs_conn_ident;
  57. /* TODO: When implementing client side */
  58. return;
  59. }
  60. /* Given the pubkey of a hidden service in <b>onion_identity_pk</b>, fetch its
  61. * descriptor by launching a dir connection to <b>hsdir</b>. Return 1 on
  62. * success or -1 on error. */
  63. static int
  64. directory_launch_v3_desc_fetch(const ed25519_public_key_t *onion_identity_pk,
  65. const routerstatus_t *hsdir)
  66. {
  67. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  68. ed25519_public_key_t blinded_pubkey;
  69. char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
  70. hs_ident_dir_conn_t hs_conn_dir_ident;
  71. int retval;
  72. tor_assert(hsdir);
  73. tor_assert(onion_identity_pk);
  74. /* Get blinded pubkey */
  75. hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
  76. current_time_period, &blinded_pubkey);
  77. /* ...and base64 it. */
  78. retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
  79. if (BUG(retval < 0)) {
  80. return -1;
  81. }
  82. /* Copy onion pk to a dir_ident so that we attach it to the dir conn */
  83. ed25519_pubkey_copy(&hs_conn_dir_ident.identity_pk, onion_identity_pk);
  84. /* Setup directory request */
  85. directory_request_t *req =
  86. directory_request_new(DIR_PURPOSE_FETCH_HSDESC);
  87. directory_request_set_routerstatus(req, hsdir);
  88. directory_request_set_indirection(req, DIRIND_ANONYMOUS);
  89. directory_request_set_resource(req, base64_blinded_pubkey);
  90. directory_request_upload_set_hs_ident(req, &hs_conn_dir_ident);
  91. directory_initiate_request(req);
  92. directory_request_free(req);
  93. log_info(LD_REND, "Descriptor fetch request for service %s with blinded "
  94. "key %s to directory %s",
  95. safe_str_client(ed25519_fmt(onion_identity_pk)),
  96. safe_str_client(base64_blinded_pubkey),
  97. safe_str_client(routerstatus_describe(hsdir)));
  98. /* Cleanup memory. */
  99. memwipe(&blinded_pubkey, 0, sizeof(blinded_pubkey));
  100. memwipe(base64_blinded_pubkey, 0, sizeof(base64_blinded_pubkey));
  101. memwipe(&hs_conn_dir_ident, 0, sizeof(hs_conn_dir_ident));
  102. return 1;
  103. }
  104. /** Return the HSDir we should use to fetch the descriptor of the hidden
  105. * service with identity key <b>onion_identity_pk</b>. */
  106. static routerstatus_t *
  107. pick_hsdir_v3(const ed25519_public_key_t *onion_identity_pk)
  108. {
  109. int retval;
  110. char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
  111. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  112. smartlist_t *responsible_hsdirs;
  113. ed25519_public_key_t blinded_pubkey;
  114. routerstatus_t *hsdir_rs = NULL;
  115. tor_assert(onion_identity_pk);
  116. responsible_hsdirs = smartlist_new();
  117. /* Get blinded pubkey of hidden service */
  118. hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
  119. current_time_period, &blinded_pubkey);
  120. /* ...and base64 it. */
  121. retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
  122. if (BUG(retval < 0)) {
  123. return NULL;
  124. }
  125. /* Get responsible hsdirs of service for this time period */
  126. hs_get_responsible_hsdirs(&blinded_pubkey, current_time_period, 0, 1,
  127. responsible_hsdirs);
  128. log_debug(LD_REND, "Found %d responsible HSDirs and about to pick one.",
  129. smartlist_len(responsible_hsdirs));
  130. /* Pick an HSDir from the responsible ones. The ownership of
  131. * responsible_hsdirs is given to this function so no need to free it. */
  132. hsdir_rs = hs_pick_hsdir(responsible_hsdirs, base64_blinded_pubkey);
  133. return hsdir_rs;
  134. }
  135. /** Fetch a v3 descriptor using the given <b>onion_identity_pk</b>.
  136. *
  137. * On success, 1 is returned. If no hidden service is left to ask, return 0.
  138. * On error, -1 is returned. */
  139. static int
  140. fetch_v3_desc(const ed25519_public_key_t *onion_identity_pk)
  141. {
  142. routerstatus_t *hsdir_rs =NULL;
  143. tor_assert(onion_identity_pk);
  144. hsdir_rs = pick_hsdir_v3(onion_identity_pk);
  145. if (!hsdir_rs) {
  146. log_info(LD_REND, "Couldn't pick a v3 hsdir.");
  147. return 0;
  148. }
  149. return directory_launch_v3_desc_fetch(onion_identity_pk, hsdir_rs);
  150. }
  151. /* Make sure that the given origin circuit circ is a valid correct
  152. * introduction circuit. This asserts on validation failure. */
  153. static void
  154. assert_intro_circ_ok(const origin_circuit_t *circ)
  155. {
  156. tor_assert(circ);
  157. tor_assert(circ->base_.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  158. tor_assert(circ->hs_ident);
  159. tor_assert(hs_ident_intro_circ_is_valid(circ->hs_ident));
  160. assert_circ_anonymity_ok(circ, get_options());
  161. }
  162. /* Find a descriptor intro point object that matches the given ident in the
  163. * given descriptor desc. Return NULL if not found. */
  164. static const hs_desc_intro_point_t *
  165. find_desc_intro_point_by_ident(const hs_ident_circuit_t *ident,
  166. const hs_descriptor_t *desc)
  167. {
  168. const hs_desc_intro_point_t *intro_point = NULL;
  169. tor_assert(ident);
  170. tor_assert(desc);
  171. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  172. const hs_desc_intro_point_t *, ip) {
  173. if (ed25519_pubkey_eq(&ident->intro_auth_pk,
  174. &ip->auth_key_cert->signed_key)) {
  175. intro_point = ip;
  176. break;
  177. }
  178. } SMARTLIST_FOREACH_END(ip);
  179. return intro_point;
  180. }
  181. /* Send an INTRODUCE1 cell along the intro circuit and populate the rend
  182. * circuit identifier with the needed key material for the e2e encryption.
  183. * Return 0 on success, -1 if there is a transient error such that an action
  184. * has been taken to recover and -2 if there is a permanent error indicating
  185. * that both circuits were closed. */
  186. static int
  187. send_introduce1(origin_circuit_t *intro_circ,
  188. origin_circuit_t *rend_circ)
  189. {
  190. int status;
  191. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  192. const ed25519_public_key_t *service_identity_pk = NULL;
  193. const hs_desc_intro_point_t *ip;
  194. assert_intro_circ_ok(intro_circ);
  195. tor_assert(rend_circ);
  196. service_identity_pk = &intro_circ->hs_ident->identity_pk;
  197. /* For logging purposes. There will be a time where the hs_ident will have a
  198. * version number but for now there is none because it's all v3. */
  199. hs_build_address(service_identity_pk, HS_VERSION_THREE, onion_address);
  200. log_info(LD_REND, "Sending INTRODUCE1 cell to service %s on circuit %u",
  201. safe_str_client(onion_address), TO_CIRCUIT(intro_circ)->n_circ_id);
  202. /* 1) Get descriptor from our cache. */
  203. const hs_descriptor_t *desc =
  204. hs_cache_lookup_as_client(service_identity_pk);
  205. if (desc == NULL || !hs_client_any_intro_points_usable(desc)) {
  206. log_info(LD_REND, "Request to %s %s. Trying to fetch a new descriptor.",
  207. safe_str_client(onion_address),
  208. (desc) ? "didn't have usable intro points" :
  209. "didn't have a descriptor");
  210. hs_client_refetch_hsdesc(service_identity_pk);
  211. /* We just triggered a refetch, make sure every connections are back
  212. * waiting for that descriptor. */
  213. flag_all_conn_wait_desc(service_identity_pk);
  214. /* We just asked for a refetch so this is a transient error. */
  215. goto tran_err;
  216. }
  217. /* We need to find which intro point in the descriptor we are connected to
  218. * on intro_circ. */
  219. ip = find_desc_intro_point_by_ident(intro_circ->hs_ident, desc);
  220. if (BUG(ip == NULL)) {
  221. /* If we can find a descriptor from this introduction circuit ident, we
  222. * must have a valid intro point object. Permanent error. */
  223. goto perm_err;
  224. }
  225. /* Send the INTRODUCE1 cell. */
  226. if (hs_circ_send_introduce1(intro_circ, rend_circ, ip,
  227. desc->subcredential) < 0) {
  228. /* Unable to send the cell, the intro circuit has been marked for close so
  229. * this is a permanent error. */
  230. tor_assert_nonfatal(TO_CIRCUIT(intro_circ)->marked_for_close);
  231. goto perm_err;
  232. }
  233. /* Cell has been sent successfully. Copy the introduction point
  234. * authentication and encryption key in the rendezvous circuit identifier so
  235. * we can compute the ntor keys when we receive the RENDEZVOUS2 cell. */
  236. memcpy(&rend_circ->hs_ident->intro_enc_pk, &ip->enc_key,
  237. sizeof(rend_circ->hs_ident->intro_enc_pk));
  238. ed25519_pubkey_copy(&rend_circ->hs_ident->intro_auth_pk,
  239. &intro_circ->hs_ident->intro_auth_pk);
  240. /* Now, we wait for an ACK or NAK on this circuit. */
  241. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  242. CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT);
  243. /* Set timestamp_dirty, because circuit_expire_building expects it to
  244. * specify when a circuit entered the _C_INTRODUCE_ACK_WAIT state. */
  245. TO_CIRCUIT(intro_circ)->timestamp_dirty = time(NULL);
  246. pathbias_count_use_attempt(intro_circ);
  247. /* Success. */
  248. status = 0;
  249. goto end;
  250. perm_err:
  251. /* Permanent error: it is possible that the intro circuit was closed prior
  252. * because we weren't able to send the cell. Make sure we don't double close
  253. * it which would result in a warning. */
  254. if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
  255. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  256. }
  257. circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_INTERNAL);
  258. status = -2;
  259. goto end;
  260. tran_err:
  261. status = -1;
  262. end:
  263. memwipe(onion_address, 0, sizeof(onion_address));
  264. return status;
  265. }
  266. /* Using the introduction circuit circ, setup the authentication key of the
  267. * intro point this circuit has extended to. */
  268. static void
  269. setup_intro_circ_auth_key(origin_circuit_t *circ)
  270. {
  271. const hs_descriptor_t *desc;
  272. tor_assert(circ);
  273. desc = hs_cache_lookup_as_client(&circ->hs_ident->identity_pk);
  274. if (BUG(desc == NULL)) {
  275. /* Opening intro circuit without the descriptor is no good... */
  276. goto end;
  277. }
  278. /* We will go over every intro point and try to find which one is linked to
  279. * that circuit. Those lists are small so it's not that expensive. */
  280. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  281. const hs_desc_intro_point_t *, ip) {
  282. SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
  283. const hs_desc_link_specifier_t *, lspec) {
  284. /* Not all tor node have an ed25519 identity key so we still rely on the
  285. * legacy identity digest. */
  286. if (lspec->type != LS_LEGACY_ID) {
  287. continue;
  288. }
  289. if (fast_memneq(circ->build_state->chosen_exit->identity_digest,
  290. lspec->u.legacy_id, DIGEST_LEN)) {
  291. break;
  292. }
  293. /* We got it, copy its authentication key to the identifier. */
  294. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  295. &ip->auth_key_cert->signed_key);
  296. goto end;
  297. } SMARTLIST_FOREACH_END(lspec);
  298. } SMARTLIST_FOREACH_END(ip);
  299. /* Reaching this point means we didn't find any intro point for this circuit
  300. * which is not suppose to happen. */
  301. tor_assert_nonfatal_unreached();
  302. end:
  303. return;
  304. }
  305. /* Called when an introduction circuit has opened. */
  306. static void
  307. client_intro_circ_has_opened(origin_circuit_t *circ)
  308. {
  309. tor_assert(circ);
  310. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  311. log_info(LD_REND, "Introduction circuit %u has opened. Attaching streams.",
  312. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  313. /* This is an introduction circuit so we'll attach the correct
  314. * authentication key to the circuit identifier so it can be identified
  315. * properly later on. */
  316. setup_intro_circ_auth_key(circ);
  317. connection_ap_attach_pending(1);
  318. }
  319. /* Called when a rendezvous circuit has opened. */
  320. static void
  321. client_rendezvous_circ_has_opened(origin_circuit_t *circ)
  322. {
  323. tor_assert(circ);
  324. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  325. log_info(LD_REND, "Rendezvous circuit has opened to %s.",
  326. safe_str_client(
  327. extend_info_describe(circ->build_state->chosen_exit)));
  328. /* Ignore returned value, nothing we can really do. On failure, the circuit
  329. * will be marked for close. */
  330. hs_circ_send_establish_rendezvous(circ);
  331. }
  332. /* This is an helper function that convert a descriptor intro point object ip
  333. * to a newly allocated extend_info_t object fully initialized. Return NULL if
  334. * we can't convert it for which chances are that we are missing or malformed
  335. * link specifiers. */
  336. static extend_info_t *
  337. desc_intro_point_to_extend_info(const hs_desc_intro_point_t *ip)
  338. {
  339. extend_info_t *ei;
  340. smartlist_t *lspecs = smartlist_new();
  341. tor_assert(ip);
  342. /* We first encode the descriptor link specifiers into the binary
  343. * representation which is a trunnel object. */
  344. SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
  345. const hs_desc_link_specifier_t *, desc_lspec) {
  346. link_specifier_t *lspec = hs_desc_encode_lspec(desc_lspec);
  347. smartlist_add(lspecs, lspec);
  348. } SMARTLIST_FOREACH_END(desc_lspec);
  349. /* Explicitely put the direct connection option to 0 because this is client
  350. * side and there is no such thing as a non anonymous client. */
  351. ei = hs_get_extend_info_from_lspecs(lspecs, &ip->onion_key, 0);
  352. SMARTLIST_FOREACH(lspecs, link_specifier_t *, ls, link_specifier_free(ls));
  353. smartlist_free(lspecs);
  354. return ei;
  355. }
  356. /* Using a descriptor desc, return a newly allocated extend_info_t object of a
  357. * randomly picked introduction point from its list. Return NULL if none are
  358. * usable. */
  359. static extend_info_t *
  360. client_get_random_intro(const ed25519_public_key_t *service_pk)
  361. {
  362. extend_info_t *ei = NULL, *ei_excluded = NULL;
  363. smartlist_t *usable_ips = NULL;
  364. const hs_descriptor_t *desc;
  365. const hs_desc_encrypted_data_t *enc_data;
  366. const or_options_t *options = get_options();
  367. tor_assert(service_pk);
  368. desc = hs_cache_lookup_as_client(service_pk);
  369. if (desc == NULL || !hs_client_any_intro_points_usable(desc)) {
  370. log_info(LD_REND, "Unable to randomly select an introduction point "
  371. "because descriptor %s.",
  372. (desc) ? "doesn't have usable intro point" : "is missing");
  373. goto end;
  374. }
  375. enc_data = &desc->encrypted_data;
  376. usable_ips = smartlist_new();
  377. smartlist_add_all(usable_ips, enc_data->intro_points);
  378. while (smartlist_len(usable_ips) != 0) {
  379. int idx;
  380. const hs_desc_intro_point_t *ip;
  381. /* Pick a random intro point and immediately remove it from the usable
  382. * list so we don't pick it again if we have to iterate more. */
  383. idx = crypto_rand_int(smartlist_len(usable_ips));
  384. ip = smartlist_get(usable_ips, idx);
  385. smartlist_del(usable_ips, idx);
  386. /* Generate an extend info object from the intro point object. */
  387. ei = desc_intro_point_to_extend_info(ip);
  388. if (ei == NULL) {
  389. /* We can get here for instance if the intro point is a private address
  390. * and we aren't allowed to extend to those. */
  391. continue;
  392. }
  393. /* Test the pick against ExcludeNodes. */
  394. if (routerset_contains_extendinfo(options->ExcludeNodes, ei)) {
  395. /* If this pick is in the ExcludeNodes list, we keep its reference so if
  396. * we ever end up not being able to pick anything else and StrictNodes is
  397. * unset, we'll use it. */
  398. ei_excluded = ei;
  399. continue;
  400. }
  401. /* XXX: Intro point can time out or just be unsuable, we need to keep
  402. * track of this and check against such cache. */
  403. /* Good pick! Let's go with this. */
  404. goto end;
  405. }
  406. /* Reaching this point means a couple of things. Either we can't use any of
  407. * the intro point listed because the IP address can't be extended to or it
  408. * is listed in the ExcludeNodes list. In the later case, if StrictNodes is
  409. * set, we are forced to not use anything. */
  410. ei = ei_excluded;
  411. if (options->StrictNodes) {
  412. log_warn(LD_REND, "Every introduction points are in the ExcludeNodes set "
  413. "and StrictNodes is set. We can't connect.");
  414. ei = NULL;
  415. }
  416. end:
  417. smartlist_free(usable_ips);
  418. return ei;
  419. }
  420. /* Called when we get an INTRODUCE_ACK success status code. Do the appropriate
  421. * actions for the rendezvous point and finally close intro_circ. */
  422. static void
  423. handle_introduce_ack_success(origin_circuit_t *intro_circ)
  424. {
  425. origin_circuit_t *rend_circ = NULL;
  426. tor_assert(intro_circ);
  427. log_info(LD_REND, "Received INTRODUCE_ACK ack! Informing rendezvous");
  428. /* Get the rendezvous circuit matching this intro point circuit.
  429. * XXX Replace this by our hs circuitmap to support client? */
  430. rend_circ = circuit_get_ready_rend_by_hs_ident(intro_circ->hs_ident);
  431. if (rend_circ == NULL) {
  432. log_warn(LD_REND, "Can't find any rendezvous circuit. Stopping");
  433. goto end;
  434. }
  435. assert_circ_anonymity_ok(rend_circ, get_options());
  436. circuit_change_purpose(TO_CIRCUIT(rend_circ),
  437. CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED);
  438. /* Set timestamp_dirty, because circuit_expire_building expects it to
  439. * specify when a circuit entered the
  440. * CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED state. */
  441. TO_CIRCUIT(rend_circ)->timestamp_dirty = time(NULL);
  442. end:
  443. /* We don't need the intro circuit anymore. It did what it had to do! */
  444. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  445. CIRCUIT_PURPOSE_C_INTRODUCE_ACKED);
  446. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_FINISHED);
  447. /* XXX: Close pending intro circuits we might have in parallel. */
  448. return;
  449. }
  450. /* Called when we get an INTRODUCE_ACK failure status code. Depending on our
  451. * failure cache status, either close the circuit or re-extend to a new
  452. * introduction point. */
  453. static void
  454. handle_introduce_ack_bad(origin_circuit_t *circ, int status)
  455. {
  456. tor_assert(circ);
  457. log_info(LD_REND, "Received INTRODUCE_ACK nack by %s. Reason: %u",
  458. safe_str_client(extend_info_describe(circ->build_state->chosen_exit)),
  459. status);
  460. /* It's a NAK. The introduction point didn't relay our request. */
  461. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_INTRODUCING);
  462. /* XXX: Report this failure for the intro point failure cache. Depending on
  463. * how many times we've tried this intro point, close it or reextend. */
  464. }
  465. /* Called when we get an INTRODUCE_ACK on the intro circuit circ. The encoded
  466. * cell is in payload of length payload_len. Return 0 on success else a
  467. * negative value. The circuit is either close or reuse to re-extend to a new
  468. * introduction point. */
  469. static int
  470. handle_introduce_ack(origin_circuit_t *circ, const uint8_t *payload,
  471. size_t payload_len)
  472. {
  473. int status, ret = -1;
  474. tor_assert(circ);
  475. tor_assert(circ->build_state);
  476. tor_assert(circ->build_state->chosen_exit);
  477. assert_circ_anonymity_ok(circ, get_options());
  478. tor_assert(payload);
  479. status = hs_cell_parse_introduce_ack(payload, payload_len);
  480. switch (status) {
  481. case HS_CELL_INTRO_ACK_SUCCESS:
  482. ret = 0;
  483. handle_introduce_ack_success(circ);
  484. break;
  485. case HS_CELL_INTRO_ACK_FAILURE:
  486. case HS_CELL_INTRO_ACK_BADFMT:
  487. case HS_CELL_INTRO_ACK_NORELAY:
  488. handle_introduce_ack_bad(circ, status);
  489. break;
  490. default:
  491. log_info(LD_PROTOCOL, "Unknown INTRODUCE_ACK status code %u from %s",
  492. status,
  493. safe_str_client(extend_info_describe(circ->build_state->chosen_exit)));
  494. break;
  495. }
  496. return ret;
  497. }
  498. /* ========== */
  499. /* Public API */
  500. /* ========== */
  501. /** A circuit just finished connecting to a hidden service that the stream
  502. * <b>conn</b> has been waiting for. Let the HS subsystem know about this. */
  503. void
  504. hs_client_note_connection_attempt_succeeded(const edge_connection_t *conn)
  505. {
  506. tor_assert(connection_edge_is_rendezvous_stream(conn));
  507. if (BUG(conn->rend_data && conn->hs_ident)) {
  508. log_warn(LD_BUG, "Stream had both rend_data and hs_ident..."
  509. "Prioritizing hs_ident");
  510. }
  511. if (conn->hs_ident) { /* It's v3: pass it to the prop224 handler */
  512. note_connection_attempt_succeeded(conn->hs_ident);
  513. return;
  514. } else if (conn->rend_data) { /* It's v2: pass it to the legacy handler */
  515. rend_client_note_connection_attempt_ended(conn->rend_data);
  516. return;
  517. }
  518. }
  519. /* With the given encoded descriptor in desc_str and the service key in
  520. * service_identity_pk, decode the descriptor and set the desc pointer with a
  521. * newly allocated descriptor object.
  522. *
  523. * Return 0 on success else a negative value and desc is set to NULL. */
  524. int
  525. hs_client_decode_descriptor(const char *desc_str,
  526. const ed25519_public_key_t *service_identity_pk,
  527. hs_descriptor_t **desc)
  528. {
  529. int ret;
  530. uint8_t subcredential[DIGEST256_LEN];
  531. tor_assert(desc_str);
  532. tor_assert(service_identity_pk);
  533. tor_assert(desc);
  534. /* Create subcredential for this HS so that we can decrypt */
  535. {
  536. ed25519_public_key_t blinded_pubkey;
  537. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  538. hs_build_blinded_pubkey(service_identity_pk, NULL, 0, current_time_period,
  539. &blinded_pubkey);
  540. hs_get_subcredential(service_identity_pk, &blinded_pubkey, subcredential);
  541. }
  542. /* Parse descriptor */
  543. ret = hs_desc_decode_descriptor(desc_str, subcredential, desc);
  544. memwipe(subcredential, 0, sizeof(subcredential));
  545. if (ret < 0) {
  546. log_warn(LD_GENERAL, "Could not parse received descriptor as client");
  547. goto err;
  548. }
  549. return 0;
  550. err:
  551. return -1;
  552. }
  553. /** Return true if there are any usable intro points in the v3 HS descriptor
  554. * <b>desc</b>. */
  555. int
  556. hs_client_any_intro_points_usable(const hs_descriptor_t *desc)
  557. {
  558. /* XXX stub waiting for more client-side work:
  559. equivalent to v2 rend_client_any_intro_points_usable() */
  560. tor_assert(desc);
  561. return 1;
  562. }
  563. /** Launch a connection to a hidden service directory to fetch a hidden
  564. * service descriptor using <b>identity_pk</b> to get the necessary keys.
  565. *
  566. * On success, 1 is returned. If no hidden service is left to ask, return 0.
  567. * On error, -1 is returned. (retval is only used by unittests right now) */
  568. int
  569. hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
  570. {
  571. tor_assert(identity_pk);
  572. /* Are we configured to fetch descriptors? */
  573. if (!get_options()->FetchHidServDescriptors) {
  574. log_warn(LD_REND, "We received an onion address for a hidden service "
  575. "descriptor but we are configured to not fetch.");
  576. return 0;
  577. }
  578. /* Check if fetching a desc for this HS is useful to us right now */
  579. {
  580. const hs_descriptor_t *cached_desc = NULL;
  581. cached_desc = hs_cache_lookup_as_client(identity_pk);
  582. if (cached_desc && hs_client_any_intro_points_usable(cached_desc)) {
  583. log_warn(LD_GENERAL, "We would fetch a v3 hidden service descriptor "
  584. "but we already have a useable descriprot.");
  585. return 0;
  586. }
  587. }
  588. return fetch_v3_desc(identity_pk);
  589. }
  590. /* This is called when we are trying to attach an AP connection to these
  591. * hidden service circuits from connection_ap_handshake_attach_circuit().
  592. * Return 0 on success, -1 for a transient error that is actions were
  593. * triggered to recover or -2 for a permenent error where both circuits will
  594. * marked for close.
  595. *
  596. * The following supports every hidden service version. */
  597. int
  598. hs_client_send_introduce1(origin_circuit_t *intro_circ,
  599. origin_circuit_t *rend_circ)
  600. {
  601. return (intro_circ->hs_ident) ? send_introduce1(intro_circ, rend_circ) :
  602. rend_client_send_introduction(intro_circ,
  603. rend_circ);
  604. }
  605. /* Called when the client circuit circ has been established. It can be either
  606. * an introduction or rendezvous circuit. This function handles all hidden
  607. * service versions. */
  608. void
  609. hs_client_circuit_has_opened(origin_circuit_t *circ)
  610. {
  611. tor_assert(circ);
  612. /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
  613. * identifier hs_ident. Can't be both. */
  614. switch (TO_CIRCUIT(circ)->purpose) {
  615. case CIRCUIT_PURPOSE_C_INTRODUCING:
  616. if (circ->hs_ident) {
  617. client_intro_circ_has_opened(circ);
  618. } else {
  619. rend_client_introcirc_has_opened(circ);
  620. }
  621. break;
  622. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  623. if (circ->hs_ident) {
  624. client_rendezvous_circ_has_opened(circ);
  625. } else {
  626. rend_client_rendcirc_has_opened(circ);
  627. }
  628. break;
  629. default:
  630. tor_assert_nonfatal_unreached();
  631. }
  632. }
  633. /* Called when we receive a RENDEZVOUS_ESTABLISHED cell. Change the state of
  634. * the circuit to CIRCUIT_PURPOSE_C_REND_READY. Return 0 on success else a
  635. * negative value and the circuit marked for close. */
  636. int
  637. hs_client_receive_rendezvous_acked(origin_circuit_t *circ,
  638. const uint8_t *payload, size_t payload_len)
  639. {
  640. tor_assert(circ);
  641. tor_assert(payload);
  642. (void) payload_len;
  643. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  644. log_warn(LD_PROTOCOL, "Got a RENDEZVOUS_ESTABLISHED but we were not "
  645. "expecting one. Closing circuit.");
  646. goto err;
  647. }
  648. log_info(LD_REND, "Received an RENDEZVOUS_ESTABLISHED. This circuit is "
  649. "now ready for rendezvous.");
  650. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_REND_READY);
  651. /* Set timestamp_dirty, because circuit_expire_building expects it to
  652. * specify when a circuit entered the _C_REND_READY state. */
  653. TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
  654. /* From a path bias point of view, this circuit is now successfully used.
  655. * Waiting any longer opens us up to attacks from malicious hidden services.
  656. * They could induce the client to attempt to connect to their hidden
  657. * service and never reply to the client's rend requests */
  658. pathbias_mark_use_success(circ);
  659. /* If we already have the introduction circuit built, make sure we send
  660. * the INTRODUCE cell _now_ */
  661. connection_ap_attach_pending(1);
  662. return 0;
  663. err:
  664. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  665. return -1;
  666. }
  667. /* This is called when a descriptor has arrived following a fetch request and
  668. * has been stored in the client cache. Every entry connection that matches
  669. * the service identity key in the ident will get attached to the hidden
  670. * service circuit. */
  671. void
  672. hs_client_desc_has_arrived(const hs_ident_dir_conn_t *ident)
  673. {
  674. time_t now = time(NULL);
  675. smartlist_t *conns = NULL;
  676. tor_assert(ident);
  677. conns = connection_list_by_type_state(CONN_TYPE_AP,
  678. AP_CONN_STATE_RENDDESC_WAIT);
  679. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
  680. const hs_descriptor_t *desc;
  681. entry_connection_t *entry_conn = TO_ENTRY_CONN(base_conn);
  682. const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(entry_conn);
  683. /* Only consider the entry connections that matches the service for which
  684. * we just fetched its descriptor. */
  685. if (!edge_conn->hs_ident ||
  686. !ed25519_pubkey_eq(&ident->identity_pk,
  687. &edge_conn->hs_ident->identity_pk)) {
  688. continue;
  689. }
  690. assert_connection_ok(base_conn, now);
  691. /* We were just called because we stored the descriptor for this service
  692. * so not finding a descriptor means we have a bigger problem. */
  693. desc = hs_cache_lookup_as_client(&ident->identity_pk);
  694. if (BUG(desc == NULL)) {
  695. goto end;
  696. }
  697. if (!hs_client_any_intro_points_usable(desc)) {
  698. log_info(LD_REND, "Hidden service descriptor is unusable. "
  699. "Closing streams.");
  700. connection_mark_unattached_ap(entry_conn,
  701. END_STREAM_REASON_RESOLVEFAILED);
  702. /* XXX: Note the connection attempt. */
  703. goto end;
  704. }
  705. log_info(LD_REND, "Descriptor has arrived. Launching circuits.");
  706. /* Restart their timeout values, so they get a fair shake at connecting to
  707. * the hidden service. XXX: Improve comment on why this is needed. */
  708. base_conn->timestamp_created = now;
  709. base_conn->timestamp_lastread = now;
  710. base_conn->timestamp_lastwritten = now;
  711. /* Change connection's state into waiting for a circuit. */
  712. base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  713. connection_ap_mark_as_pending_circuit(entry_conn);
  714. } SMARTLIST_FOREACH_END(base_conn);
  715. end:
  716. /* We don't have ownership of the objects in this list. */
  717. smartlist_free(conns);
  718. }
  719. /* Return a newly allocated extend_info_t for a randomly chosen introduction
  720. * point for the given edge connection identifier ident. Return NULL if we
  721. * can't pick any usable introduction points. */
  722. extend_info_t *
  723. hs_client_get_random_intro_from_edge(const edge_connection_t *edge_conn)
  724. {
  725. tor_assert(edge_conn);
  726. return (edge_conn->hs_ident) ?
  727. client_get_random_intro(&edge_conn->hs_ident->identity_pk) :
  728. rend_client_get_random_intro(edge_conn->rend_data);
  729. }
  730. /* Called when get an INTRODUCE_ACK cell on the introduction circuit circ.
  731. * Return 0 on success else a negative value is returned. The circuit will be
  732. * closed or reuse to extend again to another intro point. */
  733. int
  734. hs_client_receive_introduce_ack(origin_circuit_t *circ,
  735. const uint8_t *payload, size_t payload_len)
  736. {
  737. int ret = -1;
  738. tor_assert(circ);
  739. tor_assert(payload);
  740. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  741. log_warn(LD_PROTOCOL, "Unexpected INTRODUCE_ACK on circuit %u.",
  742. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  743. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  744. goto end;
  745. }
  746. ret = (circ->hs_ident) ? handle_introduce_ack(circ, payload, payload_len) :
  747. rend_client_introduction_acked(circ, payload,
  748. payload_len);
  749. /* For path bias: This circuit was used successfully. NACK or ACK counts. */
  750. pathbias_mark_use_success(circ);
  751. end:
  752. return ret;
  753. }