hs_client.c 46 KB

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