hs_client.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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_upload_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. /* Send an INTRODUCE1 cell along the intro circuit and populate the rend
  184. * circuit identifier with the needed key material for the e2e encryption.
  185. * Return 0 on success, -1 if there is a transient error such that an action
  186. * has been taken to recover and -2 if there is a permanent error indicating
  187. * that both circuits were closed. */
  188. static int
  189. send_introduce1(origin_circuit_t *intro_circ,
  190. origin_circuit_t *rend_circ)
  191. {
  192. int status;
  193. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  194. const ed25519_public_key_t *service_identity_pk = NULL;
  195. const hs_desc_intro_point_t *ip;
  196. assert_intro_circ_ok(intro_circ);
  197. tor_assert(rend_circ);
  198. service_identity_pk = &intro_circ->hs_ident->identity_pk;
  199. /* For logging purposes. There will be a time where the hs_ident will have a
  200. * version number but for now there is none because it's all v3. */
  201. hs_build_address(service_identity_pk, HS_VERSION_THREE, onion_address);
  202. log_info(LD_REND, "Sending INTRODUCE1 cell to service %s on circuit %u",
  203. safe_str_client(onion_address), TO_CIRCUIT(intro_circ)->n_circ_id);
  204. /* 1) Get descriptor from our cache. */
  205. const hs_descriptor_t *desc =
  206. hs_cache_lookup_as_client(service_identity_pk);
  207. if (desc == NULL || !hs_client_any_intro_points_usable(desc)) {
  208. log_info(LD_REND, "Request to %s %s. Trying to fetch a new descriptor.",
  209. safe_str_client(onion_address),
  210. (desc) ? "didn't have usable intro points" :
  211. "didn't have a descriptor");
  212. hs_client_refetch_hsdesc(service_identity_pk);
  213. /* We just triggered a refetch, make sure every connections are back
  214. * waiting for that descriptor. */
  215. flag_all_conn_wait_desc(service_identity_pk);
  216. /* We just asked for a refetch so this is a transient error. */
  217. goto tran_err;
  218. }
  219. /* We need to find which intro point in the descriptor we are connected to
  220. * on intro_circ. */
  221. ip = find_desc_intro_point_by_ident(intro_circ->hs_ident, desc);
  222. if (BUG(ip == NULL)) {
  223. /* If we can find a descriptor from this introduction circuit ident, we
  224. * must have a valid intro point object. Permanent error. */
  225. goto perm_err;
  226. }
  227. /* Send the INTRODUCE1 cell. */
  228. if (hs_circ_send_introduce1(intro_circ, rend_circ, ip,
  229. desc->subcredential) < 0) {
  230. /* Unable to send the cell, the intro circuit has been marked for close so
  231. * this is a permanent error. */
  232. tor_assert_nonfatal(TO_CIRCUIT(intro_circ)->marked_for_close);
  233. goto perm_err;
  234. }
  235. /* Cell has been sent successfully. Copy the introduction point
  236. * authentication and encryption key in the rendezvous circuit identifier so
  237. * we can compute the ntor keys when we receive the RENDEZVOUS2 cell. */
  238. memcpy(&rend_circ->hs_ident->intro_enc_pk, &ip->enc_key,
  239. sizeof(rend_circ->hs_ident->intro_enc_pk));
  240. ed25519_pubkey_copy(&rend_circ->hs_ident->intro_auth_pk,
  241. &intro_circ->hs_ident->intro_auth_pk);
  242. /* Now, we wait for an ACK or NAK on this circuit. */
  243. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  244. CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT);
  245. /* Set timestamp_dirty, because circuit_expire_building expects it to
  246. * specify when a circuit entered the _C_INTRODUCE_ACK_WAIT state. */
  247. TO_CIRCUIT(intro_circ)->timestamp_dirty = time(NULL);
  248. pathbias_count_use_attempt(intro_circ);
  249. /* Success. */
  250. status = 0;
  251. goto end;
  252. perm_err:
  253. /* Permanent error: it is possible that the intro circuit was closed prior
  254. * because we weren't able to send the cell. Make sure we don't double close
  255. * it which would result in a warning. */
  256. if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
  257. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  258. }
  259. circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_INTERNAL);
  260. status = -2;
  261. goto end;
  262. tran_err:
  263. status = -1;
  264. end:
  265. memwipe(onion_address, 0, sizeof(onion_address));
  266. return status;
  267. }
  268. /* Using the introduction circuit circ, setup the authentication key of the
  269. * intro point this circuit has extended to. */
  270. static void
  271. setup_intro_circ_auth_key(origin_circuit_t *circ)
  272. {
  273. const hs_descriptor_t *desc;
  274. tor_assert(circ);
  275. desc = hs_cache_lookup_as_client(&circ->hs_ident->identity_pk);
  276. if (BUG(desc == NULL)) {
  277. /* Opening intro circuit without the descriptor is no good... */
  278. goto end;
  279. }
  280. /* We will go over every intro point and try to find which one is linked to
  281. * that circuit. Those lists are small so it's not that expensive. */
  282. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  283. const hs_desc_intro_point_t *, ip) {
  284. SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
  285. const hs_desc_link_specifier_t *, lspec) {
  286. /* Not all tor node have an ed25519 identity key so we still rely on the
  287. * legacy identity digest. */
  288. if (lspec->type != LS_LEGACY_ID) {
  289. continue;
  290. }
  291. if (fast_memneq(circ->build_state->chosen_exit->identity_digest,
  292. lspec->u.legacy_id, DIGEST_LEN)) {
  293. break;
  294. }
  295. /* We got it, copy its authentication key to the identifier. */
  296. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  297. &ip->auth_key_cert->signed_key);
  298. goto end;
  299. } SMARTLIST_FOREACH_END(lspec);
  300. } SMARTLIST_FOREACH_END(ip);
  301. /* Reaching this point means we didn't find any intro point for this circuit
  302. * which is not suppose to happen. */
  303. tor_assert_nonfatal_unreached();
  304. end:
  305. return;
  306. }
  307. /* Called when an introduction circuit has opened. */
  308. static void
  309. client_intro_circ_has_opened(origin_circuit_t *circ)
  310. {
  311. tor_assert(circ);
  312. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  313. log_info(LD_REND, "Introduction circuit %u has opened. Attaching streams.",
  314. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  315. /* This is an introduction circuit so we'll attach the correct
  316. * authentication key to the circuit identifier so it can be identified
  317. * properly later on. */
  318. setup_intro_circ_auth_key(circ);
  319. connection_ap_attach_pending(1);
  320. }
  321. /* Called when a rendezvous circuit has opened. */
  322. static void
  323. client_rendezvous_circ_has_opened(origin_circuit_t *circ)
  324. {
  325. tor_assert(circ);
  326. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  327. log_info(LD_REND, "Rendezvous circuit has opened to %s.",
  328. safe_str_client(
  329. extend_info_describe(circ->build_state->chosen_exit)));
  330. /* Ignore returned value, nothing we can really do. On failure, the circuit
  331. * will be marked for close. */
  332. hs_circ_send_establish_rendezvous(circ);
  333. }
  334. /* This is an helper function that convert a descriptor intro point object ip
  335. * to a newly allocated extend_info_t object fully initialized. Return NULL if
  336. * we can't convert it for which chances are that we are missing or malformed
  337. * link specifiers. */
  338. static extend_info_t *
  339. desc_intro_point_to_extend_info(const hs_desc_intro_point_t *ip)
  340. {
  341. extend_info_t *ei;
  342. smartlist_t *lspecs = smartlist_new();
  343. tor_assert(ip);
  344. /* We first encode the descriptor link specifiers into the binary
  345. * representation which is a trunnel object. */
  346. SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
  347. const hs_desc_link_specifier_t *, desc_lspec) {
  348. link_specifier_t *lspec = hs_desc_encode_lspec(desc_lspec);
  349. smartlist_add(lspecs, lspec);
  350. } SMARTLIST_FOREACH_END(desc_lspec);
  351. /* Explicitely put the direct connection option to 0 because this is client
  352. * side and there is no such thing as a non anonymous client. */
  353. ei = hs_get_extend_info_from_lspecs(lspecs, &ip->onion_key, 0);
  354. SMARTLIST_FOREACH(lspecs, link_specifier_t *, ls, link_specifier_free(ls));
  355. smartlist_free(lspecs);
  356. return ei;
  357. }
  358. /* Using a descriptor desc, return a newly allocated extend_info_t object of a
  359. * randomly picked introduction point from its list. Return NULL if none are
  360. * usable. */
  361. static extend_info_t *
  362. client_get_random_intro(const ed25519_public_key_t *service_pk)
  363. {
  364. extend_info_t *ei = NULL, *ei_excluded = NULL;
  365. smartlist_t *usable_ips = NULL;
  366. const hs_descriptor_t *desc;
  367. const hs_desc_encrypted_data_t *enc_data;
  368. const or_options_t *options = get_options();
  369. tor_assert(service_pk);
  370. desc = hs_cache_lookup_as_client(service_pk);
  371. if (desc == NULL || !hs_client_any_intro_points_usable(desc)) {
  372. log_info(LD_REND, "Unable to randomly select an introduction point "
  373. "because descriptor %s.",
  374. (desc) ? "doesn't have usable intro point" : "is missing");
  375. goto end;
  376. }
  377. enc_data = &desc->encrypted_data;
  378. usable_ips = smartlist_new();
  379. smartlist_add_all(usable_ips, enc_data->intro_points);
  380. while (smartlist_len(usable_ips) != 0) {
  381. int idx;
  382. const hs_desc_intro_point_t *ip;
  383. /* Pick a random intro point and immediately remove it from the usable
  384. * list so we don't pick it again if we have to iterate more. */
  385. idx = crypto_rand_int(smartlist_len(usable_ips));
  386. ip = smartlist_get(usable_ips, idx);
  387. smartlist_del(usable_ips, idx);
  388. /* Generate an extend info object from the intro point object. */
  389. ei = desc_intro_point_to_extend_info(ip);
  390. if (ei == NULL) {
  391. /* We can get here for instance if the intro point is a private address
  392. * and we aren't allowed to extend to those. */
  393. continue;
  394. }
  395. /* Test the pick against ExcludeNodes. */
  396. if (routerset_contains_extendinfo(options->ExcludeNodes, ei)) {
  397. /* If this pick is in the ExcludeNodes list, we keep its reference so if
  398. * we ever end up not being able to pick anything else and StrictNodes is
  399. * unset, we'll use it. */
  400. ei_excluded = ei;
  401. continue;
  402. }
  403. /* XXX: Intro point can time out or just be unsuable, we need to keep
  404. * track of this and check against such cache. */
  405. /* Good pick! Let's go with this. */
  406. goto end;
  407. }
  408. /* Reaching this point means a couple of things. Either we can't use any of
  409. * the intro point listed because the IP address can't be extended to or it
  410. * is listed in the ExcludeNodes list. In the later case, if StrictNodes is
  411. * set, we are forced to not use anything. */
  412. ei = ei_excluded;
  413. if (options->StrictNodes) {
  414. log_warn(LD_REND, "Every introduction points are in the ExcludeNodes set "
  415. "and StrictNodes is set. We can't connect.");
  416. ei = NULL;
  417. }
  418. end:
  419. smartlist_free(usable_ips);
  420. return ei;
  421. }
  422. /* Called when we get an INTRODUCE_ACK success status code. Do the appropriate
  423. * actions for the rendezvous point and finally close intro_circ. */
  424. static void
  425. handle_introduce_ack_success(origin_circuit_t *intro_circ)
  426. {
  427. origin_circuit_t *rend_circ = NULL;
  428. tor_assert(intro_circ);
  429. log_info(LD_REND, "Received INTRODUCE_ACK ack! Informing rendezvous");
  430. /* Get the rendezvous circuit for this rendezvous cookie. */
  431. uint8_t *rendezvous_cookie = intro_circ->hs_ident->rendezvous_cookie;
  432. rend_circ = hs_circuitmap_get_rend_circ_client_side(rendezvous_cookie);
  433. if (rend_circ == NULL) {
  434. log_warn(LD_REND, "Can't find any rendezvous circuit. Stopping");
  435. goto end;
  436. }
  437. assert_circ_anonymity_ok(rend_circ, get_options());
  438. circuit_change_purpose(TO_CIRCUIT(rend_circ),
  439. CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED);
  440. /* Set timestamp_dirty, because circuit_expire_building expects it to
  441. * specify when a circuit entered the
  442. * CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED state. */
  443. TO_CIRCUIT(rend_circ)->timestamp_dirty = time(NULL);
  444. end:
  445. /* We don't need the intro circuit anymore. It did what it had to do! */
  446. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  447. CIRCUIT_PURPOSE_C_INTRODUCE_ACKED);
  448. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_FINISHED);
  449. /* XXX: Close pending intro circuits we might have in parallel. */
  450. return;
  451. }
  452. /* Called when we get an INTRODUCE_ACK failure status code. Depending on our
  453. * failure cache status, either close the circuit or re-extend to a new
  454. * introduction point. */
  455. static void
  456. handle_introduce_ack_bad(origin_circuit_t *circ, int status)
  457. {
  458. tor_assert(circ);
  459. log_info(LD_REND, "Received INTRODUCE_ACK nack by %s. Reason: %u",
  460. safe_str_client(extend_info_describe(circ->build_state->chosen_exit)),
  461. status);
  462. /* It's a NAK. The introduction point didn't relay our request. */
  463. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_INTRODUCING);
  464. /* XXX: Report this failure for the intro point failure cache. Depending on
  465. * how many times we've tried this intro point, close it or reextend. */
  466. }
  467. /* Called when we get an INTRODUCE_ACK on the intro circuit circ. The encoded
  468. * cell is in payload of length payload_len. Return 0 on success else a
  469. * negative value. The circuit is either close or reuse to re-extend to a new
  470. * introduction point. */
  471. static int
  472. handle_introduce_ack(origin_circuit_t *circ, const uint8_t *payload,
  473. size_t payload_len)
  474. {
  475. int status, ret = -1;
  476. tor_assert(circ);
  477. tor_assert(circ->build_state);
  478. tor_assert(circ->build_state->chosen_exit);
  479. assert_circ_anonymity_ok(circ, get_options());
  480. tor_assert(payload);
  481. status = hs_cell_parse_introduce_ack(payload, payload_len);
  482. switch (status) {
  483. case HS_CELL_INTRO_ACK_SUCCESS:
  484. ret = 0;
  485. handle_introduce_ack_success(circ);
  486. break;
  487. case HS_CELL_INTRO_ACK_FAILURE:
  488. case HS_CELL_INTRO_ACK_BADFMT:
  489. case HS_CELL_INTRO_ACK_NORELAY:
  490. handle_introduce_ack_bad(circ, status);
  491. break;
  492. default:
  493. log_info(LD_PROTOCOL, "Unknown INTRODUCE_ACK status code %u from %s",
  494. status,
  495. safe_str_client(extend_info_describe(circ->build_state->chosen_exit)));
  496. break;
  497. }
  498. return ret;
  499. }
  500. /* Called when we get a RENDEZVOUS2 cell on the rendezvous circuit circ. The
  501. * encoded cell is in payload of length payload_len. Return 0 on success or a
  502. * negative value on error. On error, the circuit is marked for close. */
  503. static int
  504. handle_rendezvous2(origin_circuit_t *circ, const uint8_t *payload,
  505. size_t payload_len)
  506. {
  507. int ret = -1;
  508. curve25519_public_key_t server_pk;
  509. uint8_t auth_mac[DIGEST256_LEN] = {0};
  510. uint8_t handshake_info[CURVE25519_PUBKEY_LEN + sizeof(auth_mac)] = {0};
  511. hs_ntor_rend_cell_keys_t keys;
  512. const hs_ident_circuit_t *ident;
  513. tor_assert(circ);
  514. tor_assert(payload);
  515. /* Make things easier. */
  516. ident = circ->hs_ident;
  517. tor_assert(ident);
  518. if (hs_cell_parse_rendezvous2(payload, payload_len, handshake_info,
  519. sizeof(handshake_info)) < 0) {
  520. goto err;
  521. }
  522. /* Get from the handshake info the SERVER_PK and AUTH_MAC. */
  523. memcpy(&server_pk, handshake_info, CURVE25519_PUBKEY_LEN);
  524. memcpy(auth_mac, handshake_info + CURVE25519_PUBKEY_LEN, sizeof(auth_mac));
  525. /* Generate the handshake info. */
  526. if (hs_ntor_client_get_rendezvous1_keys(&ident->intro_auth_pk,
  527. &ident->rendezvous_client_kp,
  528. &ident->intro_enc_pk, &server_pk,
  529. &keys) < 0) {
  530. log_info(LD_REND, "Unable to compute the rendezvous keys.");
  531. goto err;
  532. }
  533. /* Critical check, make sure that the MAC matches what we got with what we
  534. * computed just above. */
  535. if (!hs_ntor_client_rendezvous2_mac_is_good(&keys, auth_mac)) {
  536. log_info(LD_REND, "Invalid MAC in RENDEZVOUS2. Rejecting cell.");
  537. goto err;
  538. }
  539. /* Setup the e2e encryption on the circuit and finalize its state. */
  540. if (hs_circuit_setup_e2e_rend_circ(circ, keys.ntor_key_seed,
  541. sizeof(keys.ntor_key_seed), 0) < 0) {
  542. log_info(LD_REND, "Unable to setup the e2e encryption.");
  543. goto err;
  544. }
  545. /* Success. Hidden service connection finalized! */
  546. ret = 0;
  547. goto end;
  548. err:
  549. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  550. end:
  551. memwipe(&keys, 0, sizeof(keys));
  552. return ret;
  553. }
  554. /* ========== */
  555. /* Public API */
  556. /* ========== */
  557. /** A circuit just finished connecting to a hidden service that the stream
  558. * <b>conn</b> has been waiting for. Let the HS subsystem know about this. */
  559. void
  560. hs_client_note_connection_attempt_succeeded(const edge_connection_t *conn)
  561. {
  562. tor_assert(connection_edge_is_rendezvous_stream(conn));
  563. if (BUG(conn->rend_data && conn->hs_ident)) {
  564. log_warn(LD_BUG, "Stream had both rend_data and hs_ident..."
  565. "Prioritizing hs_ident");
  566. }
  567. if (conn->hs_ident) { /* It's v3: pass it to the prop224 handler */
  568. note_connection_attempt_succeeded(conn->hs_ident);
  569. return;
  570. } else if (conn->rend_data) { /* It's v2: pass it to the legacy handler */
  571. rend_client_note_connection_attempt_ended(conn->rend_data);
  572. return;
  573. }
  574. }
  575. /* With the given encoded descriptor in desc_str and the service key in
  576. * service_identity_pk, decode the descriptor and set the desc pointer with a
  577. * newly allocated descriptor object.
  578. *
  579. * Return 0 on success else a negative value and desc is set to NULL. */
  580. int
  581. hs_client_decode_descriptor(const char *desc_str,
  582. const ed25519_public_key_t *service_identity_pk,
  583. hs_descriptor_t **desc)
  584. {
  585. int ret;
  586. uint8_t subcredential[DIGEST256_LEN];
  587. tor_assert(desc_str);
  588. tor_assert(service_identity_pk);
  589. tor_assert(desc);
  590. /* Create subcredential for this HS so that we can decrypt */
  591. {
  592. ed25519_public_key_t blinded_pubkey;
  593. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  594. hs_build_blinded_pubkey(service_identity_pk, NULL, 0, current_time_period,
  595. &blinded_pubkey);
  596. hs_get_subcredential(service_identity_pk, &blinded_pubkey, subcredential);
  597. }
  598. /* Parse descriptor */
  599. ret = hs_desc_decode_descriptor(desc_str, subcredential, desc);
  600. memwipe(subcredential, 0, sizeof(subcredential));
  601. if (ret < 0) {
  602. log_warn(LD_GENERAL, "Could not parse received descriptor as client");
  603. goto err;
  604. }
  605. return 0;
  606. err:
  607. return -1;
  608. }
  609. /** Return true if there are any usable intro points in the v3 HS descriptor
  610. * <b>desc</b>. */
  611. int
  612. hs_client_any_intro_points_usable(const hs_descriptor_t *desc)
  613. {
  614. /* XXX stub waiting for more client-side work:
  615. equivalent to v2 rend_client_any_intro_points_usable() */
  616. tor_assert(desc);
  617. return 1;
  618. }
  619. /** Launch a connection to a hidden service directory to fetch a hidden
  620. * service descriptor using <b>identity_pk</b> to get the necessary keys.
  621. *
  622. * On success, 1 is returned. If no hidden service is left to ask, return 0.
  623. * On error, -1 is returned. (retval is only used by unittests right now) */
  624. int
  625. hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
  626. {
  627. tor_assert(identity_pk);
  628. /* Are we configured to fetch descriptors? */
  629. if (!get_options()->FetchHidServDescriptors) {
  630. log_warn(LD_REND, "We received an onion address for a hidden service "
  631. "descriptor but we are configured to not fetch.");
  632. return 0;
  633. }
  634. /* Check if fetching a desc for this HS is useful to us right now */
  635. {
  636. const hs_descriptor_t *cached_desc = NULL;
  637. cached_desc = hs_cache_lookup_as_client(identity_pk);
  638. if (cached_desc && hs_client_any_intro_points_usable(cached_desc)) {
  639. log_warn(LD_GENERAL, "We would fetch a v3 hidden service descriptor "
  640. "but we already have a useable descriprot.");
  641. return 0;
  642. }
  643. }
  644. return fetch_v3_desc(identity_pk);
  645. }
  646. /* This is called when we are trying to attach an AP connection to these
  647. * hidden service circuits from connection_ap_handshake_attach_circuit().
  648. * Return 0 on success, -1 for a transient error that is actions were
  649. * triggered to recover or -2 for a permenent error where both circuits will
  650. * marked for close.
  651. *
  652. * The following supports every hidden service version. */
  653. int
  654. hs_client_send_introduce1(origin_circuit_t *intro_circ,
  655. origin_circuit_t *rend_circ)
  656. {
  657. return (intro_circ->hs_ident) ? send_introduce1(intro_circ, rend_circ) :
  658. rend_client_send_introduction(intro_circ,
  659. rend_circ);
  660. }
  661. /* Called when the client circuit circ has been established. It can be either
  662. * an introduction or rendezvous circuit. This function handles all hidden
  663. * service versions. */
  664. void
  665. hs_client_circuit_has_opened(origin_circuit_t *circ)
  666. {
  667. tor_assert(circ);
  668. /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
  669. * identifier hs_ident. Can't be both. */
  670. switch (TO_CIRCUIT(circ)->purpose) {
  671. case CIRCUIT_PURPOSE_C_INTRODUCING:
  672. if (circ->hs_ident) {
  673. client_intro_circ_has_opened(circ);
  674. } else {
  675. rend_client_introcirc_has_opened(circ);
  676. }
  677. break;
  678. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  679. if (circ->hs_ident) {
  680. client_rendezvous_circ_has_opened(circ);
  681. } else {
  682. rend_client_rendcirc_has_opened(circ);
  683. }
  684. break;
  685. default:
  686. tor_assert_nonfatal_unreached();
  687. }
  688. }
  689. /* Called when we receive a RENDEZVOUS_ESTABLISHED cell. Change the state of
  690. * the circuit to CIRCUIT_PURPOSE_C_REND_READY. Return 0 on success else a
  691. * negative value and the circuit marked for close. */
  692. int
  693. hs_client_receive_rendezvous_acked(origin_circuit_t *circ,
  694. const uint8_t *payload, size_t payload_len)
  695. {
  696. tor_assert(circ);
  697. tor_assert(payload);
  698. (void) payload_len;
  699. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  700. log_warn(LD_PROTOCOL, "Got a RENDEZVOUS_ESTABLISHED but we were not "
  701. "expecting one. Closing circuit.");
  702. goto err;
  703. }
  704. log_info(LD_REND, "Received an RENDEZVOUS_ESTABLISHED. This circuit is "
  705. "now ready for rendezvous.");
  706. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_REND_READY);
  707. /* Set timestamp_dirty, because circuit_expire_building expects it to
  708. * specify when a circuit entered the _C_REND_READY state. */
  709. TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
  710. /* From a path bias point of view, this circuit is now successfully used.
  711. * Waiting any longer opens us up to attacks from malicious hidden services.
  712. * They could induce the client to attempt to connect to their hidden
  713. * service and never reply to the client's rend requests */
  714. pathbias_mark_use_success(circ);
  715. /* If we already have the introduction circuit built, make sure we send
  716. * the INTRODUCE cell _now_ */
  717. connection_ap_attach_pending(1);
  718. return 0;
  719. err:
  720. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  721. return -1;
  722. }
  723. /* This is called when a descriptor has arrived following a fetch request and
  724. * has been stored in the client cache. Every entry connection that matches
  725. * the service identity key in the ident will get attached to the hidden
  726. * service circuit. */
  727. void
  728. hs_client_desc_has_arrived(const hs_ident_dir_conn_t *ident)
  729. {
  730. time_t now = time(NULL);
  731. smartlist_t *conns = NULL;
  732. tor_assert(ident);
  733. conns = connection_list_by_type_state(CONN_TYPE_AP,
  734. AP_CONN_STATE_RENDDESC_WAIT);
  735. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
  736. const hs_descriptor_t *desc;
  737. entry_connection_t *entry_conn = TO_ENTRY_CONN(base_conn);
  738. const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(entry_conn);
  739. /* Only consider the entry connections that matches the service for which
  740. * we just fetched its descriptor. */
  741. if (!edge_conn->hs_ident ||
  742. !ed25519_pubkey_eq(&ident->identity_pk,
  743. &edge_conn->hs_ident->identity_pk)) {
  744. continue;
  745. }
  746. assert_connection_ok(base_conn, now);
  747. /* We were just called because we stored the descriptor for this service
  748. * so not finding a descriptor means we have a bigger problem. */
  749. desc = hs_cache_lookup_as_client(&ident->identity_pk);
  750. if (BUG(desc == NULL)) {
  751. goto end;
  752. }
  753. if (!hs_client_any_intro_points_usable(desc)) {
  754. log_info(LD_REND, "Hidden service descriptor is unusable. "
  755. "Closing streams.");
  756. connection_mark_unattached_ap(entry_conn,
  757. END_STREAM_REASON_RESOLVEFAILED);
  758. /* XXX: Note the connection attempt. */
  759. goto end;
  760. }
  761. log_info(LD_REND, "Descriptor has arrived. Launching circuits.");
  762. /* Restart their timeout values, so they get a fair shake at connecting to
  763. * the hidden service. XXX: Improve comment on why this is needed. */
  764. base_conn->timestamp_created = now;
  765. base_conn->timestamp_lastread = now;
  766. base_conn->timestamp_lastwritten = now;
  767. /* Change connection's state into waiting for a circuit. */
  768. base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  769. connection_ap_mark_as_pending_circuit(entry_conn);
  770. } SMARTLIST_FOREACH_END(base_conn);
  771. end:
  772. /* We don't have ownership of the objects in this list. */
  773. smartlist_free(conns);
  774. }
  775. /* Return a newly allocated extend_info_t for a randomly chosen introduction
  776. * point for the given edge connection identifier ident. Return NULL if we
  777. * can't pick any usable introduction points. */
  778. extend_info_t *
  779. hs_client_get_random_intro_from_edge(const edge_connection_t *edge_conn)
  780. {
  781. tor_assert(edge_conn);
  782. return (edge_conn->hs_ident) ?
  783. client_get_random_intro(&edge_conn->hs_ident->identity_pk) :
  784. rend_client_get_random_intro(edge_conn->rend_data);
  785. }
  786. /* Called when get an INTRODUCE_ACK cell on the introduction circuit circ.
  787. * Return 0 on success else a negative value is returned. The circuit will be
  788. * closed or reuse to extend again to another intro point. */
  789. int
  790. hs_client_receive_introduce_ack(origin_circuit_t *circ,
  791. const uint8_t *payload, size_t payload_len)
  792. {
  793. int ret = -1;
  794. tor_assert(circ);
  795. tor_assert(payload);
  796. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  797. log_warn(LD_PROTOCOL, "Unexpected INTRODUCE_ACK on circuit %u.",
  798. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  799. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  800. goto end;
  801. }
  802. ret = (circ->hs_ident) ? handle_introduce_ack(circ, payload, payload_len) :
  803. rend_client_introduction_acked(circ, payload,
  804. payload_len);
  805. /* For path bias: This circuit was used successfully. NACK or ACK counts. */
  806. pathbias_mark_use_success(circ);
  807. end:
  808. return ret;
  809. }
  810. /* Called when get a RENDEZVOUS2 cell on the rendezvous circuit circ. Return
  811. * 0 on success else a negative value is returned. The circuit will be closed
  812. * on error. */
  813. int
  814. hs_client_receive_rendezvous2(origin_circuit_t *circ,
  815. const uint8_t *payload, size_t payload_len)
  816. {
  817. int ret = -1;
  818. tor_assert(circ);
  819. tor_assert(payload);
  820. /* Circuit can possibly be in both state because we could receive a
  821. * RENDEZVOUS2 cell before the INTRODUCE_ACK has been received. */
  822. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  823. TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  824. log_warn(LD_PROTOCOL, "Unexpected RENDEZVOUS2 cell on circuit %u. "
  825. "Closing circuit.",
  826. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  827. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  828. goto end;
  829. }
  830. log_info(LD_REND, "Got RENDEZVOUS2 cell from hidden service on circuit %u.",
  831. TO_CIRCUIT(circ)->n_circ_id);
  832. ret = (circ->hs_ident) ? handle_rendezvous2(circ, payload, payload_len) :
  833. rend_client_receive_rendezvous(circ, payload,
  834. payload_len);
  835. end:
  836. return ret;
  837. }
  838. /* Extend the introduction circuit circ to another valid introduction point
  839. * for the hidden service it is trying to connect to, or mark it and launch a
  840. * new circuit if we can't extend it. Return 0 on success or possible
  841. * success. Return -1 and mark the introduction circuit for close on permanent
  842. * failure.
  843. *
  844. * On failure, the caller is responsible for marking the associated rendezvous
  845. * circuit for close. */
  846. int
  847. hs_client_reextend_intro_circuit(origin_circuit_t *circ)
  848. {
  849. int ret = -1;
  850. extend_info_t *ei;
  851. tor_assert(circ);
  852. ei = (circ->hs_ident) ?
  853. client_get_random_intro(&circ->hs_ident->identity_pk) :
  854. rend_client_get_random_intro(circ->rend_data);
  855. if (ei == NULL) {
  856. log_warn(LD_REND, "No usable introduction points left. Closing.");
  857. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  858. goto end;
  859. }
  860. if (circ->remaining_relay_early_cells) {
  861. log_info(LD_REND, "Re-extending circ %u, this time to %s.",
  862. (unsigned int) TO_CIRCUIT(circ)->n_circ_id,
  863. safe_str_client(extend_info_describe(ei)));
  864. ret = circuit_extend_to_new_exit(circ, ei);
  865. } else {
  866. log_info(LD_REND, "Closing intro circ %u (out of RELAY_EARLY cells).",
  867. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  868. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_FINISHED);
  869. /* connection_ap_handshake_attach_circuit will launch a new intro circ. */
  870. ret = 0;
  871. }
  872. end:
  873. extend_info_free(ei);
  874. return ret;
  875. }