hs_client.c 46 KB

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