hs_client.c 45 KB

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