hs_client.c 34 KB

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