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