hs_client.c 53 KB

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