hs_client.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 "circuitlist.h"
  22. #include "circuituse.h"
  23. #include "connection.h"
  24. #include "circpathbias.h"
  25. #include "connection.h"
  26. /* Get all connections that are waiting on a circuit and flag them back to
  27. * waiting for a hidden service descriptor for the given service key
  28. * service_identity_pk. */
  29. static void
  30. flag_all_conn_wait_desc(const ed25519_public_key_t *service_identity_pk)
  31. {
  32. tor_assert(service_identity_pk);
  33. smartlist_t *conns =
  34. connection_list_by_type_state(CONN_TYPE_AP, AP_CONN_STATE_CIRCUIT_WAIT);
  35. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  36. edge_connection_t *edge_conn;
  37. if (BUG(!CONN_IS_EDGE(conn))) {
  38. continue;
  39. }
  40. edge_conn = TO_EDGE_CONN(conn);
  41. if (edge_conn->hs_ident &&
  42. ed25519_pubkey_eq(&edge_conn->hs_ident->identity_pk,
  43. service_identity_pk)) {
  44. connection_ap_mark_as_non_pending_circuit(TO_ENTRY_CONN(conn));
  45. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  46. }
  47. } SMARTLIST_FOREACH_END(conn);
  48. smartlist_free(conns);
  49. }
  50. /* A v3 HS circuit successfully connected to the hidden service. Update the
  51. * stream state at <b>hs_conn_ident</b> appropriately. */
  52. static void
  53. note_connection_attempt_succeeded(const hs_ident_edge_conn_t *hs_conn_ident)
  54. {
  55. (void) hs_conn_ident;
  56. /* TODO: When implementing client side */
  57. return;
  58. }
  59. /* Given the pubkey of a hidden service in <b>onion_identity_pk</b>, fetch its
  60. * descriptor by launching a dir connection to <b>hsdir</b>. Return 1 on
  61. * success or -1 on error. */
  62. static int
  63. directory_launch_v3_desc_fetch(const ed25519_public_key_t *onion_identity_pk,
  64. const routerstatus_t *hsdir)
  65. {
  66. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  67. ed25519_public_key_t blinded_pubkey;
  68. char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
  69. hs_ident_dir_conn_t hs_conn_dir_ident;
  70. int retval;
  71. tor_assert(hsdir);
  72. tor_assert(onion_identity_pk);
  73. /* Get blinded pubkey */
  74. hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
  75. current_time_period, &blinded_pubkey);
  76. /* ...and base64 it. */
  77. retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
  78. if (BUG(retval < 0)) {
  79. return -1;
  80. }
  81. /* Copy onion pk to a dir_ident so that we attach it to the dir conn */
  82. ed25519_pubkey_copy(&hs_conn_dir_ident.identity_pk, onion_identity_pk);
  83. /* Setup directory request */
  84. directory_request_t *req =
  85. directory_request_new(DIR_PURPOSE_FETCH_HSDESC);
  86. directory_request_set_routerstatus(req, hsdir);
  87. directory_request_set_indirection(req, DIRIND_ANONYMOUS);
  88. directory_request_set_resource(req, base64_blinded_pubkey);
  89. directory_request_upload_set_hs_ident(req, &hs_conn_dir_ident);
  90. directory_initiate_request(req);
  91. directory_request_free(req);
  92. log_info(LD_REND, "Descriptor fetch request for service %s with blinded "
  93. "key %s to directory %s",
  94. safe_str_client(ed25519_fmt(onion_identity_pk)),
  95. safe_str_client(base64_blinded_pubkey),
  96. safe_str_client(routerstatus_describe(hsdir)));
  97. /* Cleanup memory. */
  98. memwipe(&blinded_pubkey, 0, sizeof(blinded_pubkey));
  99. memwipe(base64_blinded_pubkey, 0, sizeof(base64_blinded_pubkey));
  100. memwipe(&hs_conn_dir_ident, 0, sizeof(hs_conn_dir_ident));
  101. return 1;
  102. }
  103. /** Return the HSDir we should use to fetch the descriptor of the hidden
  104. * service with identity key <b>onion_identity_pk</b>. */
  105. static routerstatus_t *
  106. pick_hsdir_v3(const ed25519_public_key_t *onion_identity_pk)
  107. {
  108. int retval;
  109. char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
  110. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  111. smartlist_t *responsible_hsdirs;
  112. ed25519_public_key_t blinded_pubkey;
  113. routerstatus_t *hsdir_rs = NULL;
  114. tor_assert(onion_identity_pk);
  115. responsible_hsdirs = smartlist_new();
  116. /* Get blinded pubkey of hidden service */
  117. hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
  118. current_time_period, &blinded_pubkey);
  119. /* ...and base64 it. */
  120. retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
  121. if (BUG(retval < 0)) {
  122. return NULL;
  123. }
  124. /* Get responsible hsdirs of service for this time period */
  125. hs_get_responsible_hsdirs(&blinded_pubkey, current_time_period, 0, 1,
  126. responsible_hsdirs);
  127. log_debug(LD_REND, "Found %d responsible HSDirs and about to pick one.",
  128. smartlist_len(responsible_hsdirs));
  129. /* Pick an HSDir from the responsible ones. The ownership of
  130. * responsible_hsdirs is given to this function so no need to free it. */
  131. hsdir_rs = hs_pick_hsdir(responsible_hsdirs, base64_blinded_pubkey);
  132. return hsdir_rs;
  133. }
  134. /** Fetch a v3 descriptor using the given <b>onion_identity_pk</b>.
  135. *
  136. * On success, 1 is returned. If no hidden service is left to ask, return 0.
  137. * On error, -1 is returned. */
  138. static int
  139. fetch_v3_desc(const ed25519_public_key_t *onion_identity_pk)
  140. {
  141. routerstatus_t *hsdir_rs =NULL;
  142. tor_assert(onion_identity_pk);
  143. hsdir_rs = pick_hsdir_v3(onion_identity_pk);
  144. if (!hsdir_rs) {
  145. log_info(LD_REND, "Couldn't pick a v3 hsdir.");
  146. return 0;
  147. }
  148. return directory_launch_v3_desc_fetch(onion_identity_pk, hsdir_rs);
  149. }
  150. /* Make sure that the given origin circuit circ is a valid correct
  151. * introduction circuit. This asserts on validation failure. */
  152. static void
  153. assert_intro_circ_ok(const origin_circuit_t *circ)
  154. {
  155. tor_assert(circ);
  156. tor_assert(circ->base_.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  157. tor_assert(circ->hs_ident);
  158. tor_assert(hs_ident_intro_circ_is_valid(circ->hs_ident));
  159. assert_circ_anonymity_ok(circ, get_options());
  160. }
  161. /* Find a descriptor intro point object that matches the given ident in the
  162. * given descriptor desc. Return NULL if not found. */
  163. static const hs_desc_intro_point_t *
  164. find_desc_intro_point_by_ident(const hs_ident_circuit_t *ident,
  165. const hs_descriptor_t *desc)
  166. {
  167. const hs_desc_intro_point_t *intro_point = NULL;
  168. tor_assert(ident);
  169. tor_assert(desc);
  170. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  171. const hs_desc_intro_point_t *, ip) {
  172. if (ed25519_pubkey_eq(&ident->intro_auth_pk,
  173. &ip->auth_key_cert->signed_key)) {
  174. intro_point = ip;
  175. break;
  176. }
  177. } SMARTLIST_FOREACH_END(ip);
  178. return intro_point;
  179. }
  180. /* Send an INTRODUCE1 cell along the intro circuit and populate the rend
  181. * circuit identifier with the needed key material for the e2e encryption.
  182. * Return 0 on success, -1 if there is a transient error such that an action
  183. * has been taken to recover and -2 if there is a permanent error indicating
  184. * that both circuits were closed. */
  185. static int
  186. send_introduce1(origin_circuit_t *intro_circ,
  187. origin_circuit_t *rend_circ)
  188. {
  189. int status;
  190. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  191. const ed25519_public_key_t *service_identity_pk = NULL;
  192. const hs_desc_intro_point_t *ip;
  193. assert_intro_circ_ok(intro_circ);
  194. tor_assert(rend_circ);
  195. service_identity_pk = &intro_circ->hs_ident->identity_pk;
  196. /* For logging purposes. There will be a time where the hs_ident will have a
  197. * version number but for now there is none because it's all v3. */
  198. hs_build_address(service_identity_pk, HS_VERSION_THREE, onion_address);
  199. log_info(LD_REND, "Sending INTRODUCE1 cell to service %s on circuit %u",
  200. safe_str_client(onion_address), TO_CIRCUIT(intro_circ)->n_circ_id);
  201. /* 1) Get descriptor from our cache. */
  202. const hs_descriptor_t *desc =
  203. hs_cache_lookup_as_client(service_identity_pk);
  204. if (desc == NULL || !hs_client_any_intro_points_usable(desc)) {
  205. log_info(LD_REND, "Request to %s %s. Trying to fetch a new descriptor.",
  206. safe_str_client(onion_address),
  207. (desc) ? "didn't have usable intro points" :
  208. "didn't have a descriptor");
  209. hs_client_refetch_hsdesc(service_identity_pk);
  210. /* We just triggered a refetch, make sure every connections are back
  211. * waiting for that descriptor. */
  212. flag_all_conn_wait_desc(service_identity_pk);
  213. /* We just asked for a refetch so this is a transient error. */
  214. goto tran_err;
  215. }
  216. /* We need to find which intro point in the descriptor we are connected to
  217. * on intro_circ. */
  218. ip = find_desc_intro_point_by_ident(intro_circ->hs_ident, desc);
  219. if (BUG(ip == NULL)) {
  220. /* If we can find a descriptor from this introduction circuit ident, we
  221. * must have a valid intro point object. Permanent error. */
  222. goto perm_err;
  223. }
  224. /* Send the INTRODUCE1 cell. */
  225. if (hs_circ_send_introduce1(intro_circ, rend_circ, ip,
  226. desc->subcredential) < 0) {
  227. /* Unable to send the cell, the intro circuit has been marked for close so
  228. * this is a permanent error. */
  229. tor_assert_nonfatal(TO_CIRCUIT(intro_circ)->marked_for_close);
  230. goto perm_err;
  231. }
  232. /* Cell has been sent successfully. Copy the introduction point
  233. * authentication and encryption key in the rendezvous circuit identifier so
  234. * we can compute the ntor keys when we receive the RENDEZVOUS2 cell. */
  235. memcpy(&rend_circ->hs_ident->intro_enc_pk, &ip->enc_key,
  236. sizeof(rend_circ->hs_ident->intro_enc_pk));
  237. ed25519_pubkey_copy(&rend_circ->hs_ident->intro_auth_pk,
  238. &intro_circ->hs_ident->intro_auth_pk);
  239. /* Now, we wait for an ACK or NAK on this circuit. */
  240. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  241. CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT);
  242. /* Set timestamp_dirty, because circuit_expire_building expects it to
  243. * specify when a circuit entered the _C_INTRODUCE_ACK_WAIT state. */
  244. TO_CIRCUIT(intro_circ)->timestamp_dirty = time(NULL);
  245. pathbias_count_use_attempt(intro_circ);
  246. /* Success. */
  247. status = 0;
  248. goto end;
  249. perm_err:
  250. /* Permanent error: it is possible that the intro circuit was closed prior
  251. * because we weren't able to send the cell. Make sure we don't double close
  252. * it which would result in a warning. */
  253. if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
  254. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  255. }
  256. circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_INTERNAL);
  257. status = -2;
  258. goto end;
  259. tran_err:
  260. status = -1;
  261. end:
  262. memwipe(onion_address, 0, sizeof(onion_address));
  263. return status;
  264. }
  265. /* Using the introduction circuit circ, setup the authentication key of the
  266. * intro point this circuit has extended to. */
  267. static void
  268. setup_intro_circ_auth_key(origin_circuit_t *circ)
  269. {
  270. const hs_descriptor_t *desc;
  271. tor_assert(circ);
  272. desc = hs_cache_lookup_as_client(&circ->hs_ident->identity_pk);
  273. if (BUG(desc == NULL)) {
  274. /* Opening intro circuit without the descriptor is no good... */
  275. goto end;
  276. }
  277. /* We will go over every intro point and try to find which one is linked to
  278. * that circuit. Those lists are small so it's not that expensive. */
  279. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  280. const hs_desc_intro_point_t *, ip) {
  281. SMARTLIST_FOREACH_BEGIN(ip->link_specifiers,
  282. const hs_desc_link_specifier_t *, lspec) {
  283. /* Not all tor node have an ed25519 identity key so we still rely on the
  284. * legacy identity digest. */
  285. if (lspec->type != LS_LEGACY_ID) {
  286. continue;
  287. }
  288. if (fast_memneq(circ->build_state->chosen_exit->identity_digest,
  289. lspec->u.legacy_id, DIGEST_LEN)) {
  290. break;
  291. }
  292. /* We got it, copy its authentication key to the identifier. */
  293. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  294. &ip->auth_key_cert->signed_key);
  295. goto end;
  296. } SMARTLIST_FOREACH_END(lspec);
  297. } SMARTLIST_FOREACH_END(ip);
  298. /* Reaching this point means we didn't find any intro point for this circuit
  299. * which is not suppose to happen. */
  300. tor_assert_nonfatal_unreached();
  301. end:
  302. return;
  303. }
  304. /* Called when an introduction circuit has opened. */
  305. static void
  306. client_intro_circ_has_opened(origin_circuit_t *circ)
  307. {
  308. tor_assert(circ);
  309. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  310. log_info(LD_REND, "Introduction circuit %u has opened. Attaching streams.",
  311. (unsigned int) TO_CIRCUIT(circ)->n_circ_id);
  312. /* This is an introduction circuit so we'll attach the correct
  313. * authentication key to the circuit identifier so it can be identified
  314. * properly later on. */
  315. setup_intro_circ_auth_key(circ);
  316. connection_ap_attach_pending(1);
  317. }
  318. /* Called when a rendezvous circuit has opened. */
  319. static void
  320. client_rendezvous_circ_has_opened(origin_circuit_t *circ)
  321. {
  322. tor_assert(circ);
  323. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  324. log_info(LD_REND, "Rendezvous circuit has opened to %s.",
  325. safe_str_client(
  326. extend_info_describe(circ->build_state->chosen_exit)));
  327. /* Ignore returned value, nothing we can really do. On failure, the circuit
  328. * will be marked for close. */
  329. hs_circ_send_establish_rendezvous(circ);
  330. }
  331. /* ========== */
  332. /* Public API */
  333. /* ========== */
  334. /** A circuit just finished connecting to a hidden service that the stream
  335. * <b>conn</b> has been waiting for. Let the HS subsystem know about this. */
  336. void
  337. hs_client_note_connection_attempt_succeeded(const edge_connection_t *conn)
  338. {
  339. tor_assert(connection_edge_is_rendezvous_stream(conn));
  340. if (BUG(conn->rend_data && conn->hs_ident)) {
  341. log_warn(LD_BUG, "Stream had both rend_data and hs_ident..."
  342. "Prioritizing hs_ident");
  343. }
  344. if (conn->hs_ident) { /* It's v3: pass it to the prop224 handler */
  345. note_connection_attempt_succeeded(conn->hs_ident);
  346. return;
  347. } else if (conn->rend_data) { /* It's v2: pass it to the legacy handler */
  348. rend_client_note_connection_attempt_ended(conn->rend_data);
  349. return;
  350. }
  351. }
  352. /* With the given encoded descriptor in desc_str and the service key in
  353. * service_identity_pk, decode the descriptor and set the desc pointer with a
  354. * newly allocated descriptor object.
  355. *
  356. * Return 0 on success else a negative value and desc is set to NULL. */
  357. int
  358. hs_client_decode_descriptor(const char *desc_str,
  359. const ed25519_public_key_t *service_identity_pk,
  360. hs_descriptor_t **desc)
  361. {
  362. int ret;
  363. uint8_t subcredential[DIGEST256_LEN];
  364. tor_assert(desc_str);
  365. tor_assert(service_identity_pk);
  366. tor_assert(desc);
  367. /* Create subcredential for this HS so that we can decrypt */
  368. {
  369. ed25519_public_key_t blinded_pubkey;
  370. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  371. hs_build_blinded_pubkey(service_identity_pk, NULL, 0, current_time_period,
  372. &blinded_pubkey);
  373. hs_get_subcredential(service_identity_pk, &blinded_pubkey, subcredential);
  374. }
  375. /* Parse descriptor */
  376. ret = hs_desc_decode_descriptor(desc_str, subcredential, desc);
  377. memwipe(subcredential, 0, sizeof(subcredential));
  378. if (ret < 0) {
  379. log_warn(LD_GENERAL, "Could not parse received descriptor as client");
  380. goto err;
  381. }
  382. return 0;
  383. err:
  384. return -1;
  385. }
  386. /** Return true if there are any usable intro points in the v3 HS descriptor
  387. * <b>desc</b>. */
  388. int
  389. hs_client_any_intro_points_usable(const hs_descriptor_t *desc)
  390. {
  391. /* XXX stub waiting for more client-side work:
  392. equivalent to v2 rend_client_any_intro_points_usable() */
  393. tor_assert(desc);
  394. return 1;
  395. }
  396. /** Launch a connection to a hidden service directory to fetch a hidden
  397. * service descriptor using <b>identity_pk</b> to get the necessary keys.
  398. *
  399. * On success, 1 is returned. If no hidden service is left to ask, return 0.
  400. * On error, -1 is returned. (retval is only used by unittests right now) */
  401. int
  402. hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
  403. {
  404. tor_assert(identity_pk);
  405. /* Are we configured to fetch descriptors? */
  406. if (!get_options()->FetchHidServDescriptors) {
  407. log_warn(LD_REND, "We received an onion address for a hidden service "
  408. "descriptor but we are configured to not fetch.");
  409. return 0;
  410. }
  411. /* Check if fetching a desc for this HS is useful to us right now */
  412. {
  413. const hs_descriptor_t *cached_desc = NULL;
  414. cached_desc = hs_cache_lookup_as_client(identity_pk);
  415. if (cached_desc && hs_client_any_intro_points_usable(cached_desc)) {
  416. log_warn(LD_GENERAL, "We would fetch a v3 hidden service descriptor "
  417. "but we already have a useable descriprot.");
  418. return 0;
  419. }
  420. }
  421. return fetch_v3_desc(identity_pk);
  422. }
  423. /* This is called when we are trying to attach an AP connection to these
  424. * hidden service circuits from connection_ap_handshake_attach_circuit().
  425. * Return 0 on success, -1 for a transient error that is actions were
  426. * triggered to recover or -2 for a permenent error where both circuits will
  427. * marked for close.
  428. *
  429. * The following supports every hidden service version. */
  430. int
  431. hs_client_send_introduce1(origin_circuit_t *intro_circ,
  432. origin_circuit_t *rend_circ)
  433. {
  434. return (intro_circ->hs_ident) ? send_introduce1(intro_circ, rend_circ) :
  435. rend_client_send_introduction(intro_circ,
  436. rend_circ);
  437. }
  438. /* Called when the client circuit circ has been established. It can be either
  439. * an introduction or rendezvous circuit. This function handles all hidden
  440. * service versions. */
  441. void
  442. hs_client_circuit_has_opened(origin_circuit_t *circ)
  443. {
  444. tor_assert(circ);
  445. /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
  446. * identifier hs_ident. Can't be both. */
  447. switch (TO_CIRCUIT(circ)->purpose) {
  448. case CIRCUIT_PURPOSE_C_INTRODUCING:
  449. if (circ->hs_ident) {
  450. client_intro_circ_has_opened(circ);
  451. } else {
  452. rend_client_introcirc_has_opened(circ);
  453. }
  454. break;
  455. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  456. if (circ->hs_ident) {
  457. client_rendezvous_circ_has_opened(circ);
  458. } else {
  459. rend_client_rendcirc_has_opened(circ);
  460. }
  461. break;
  462. default:
  463. tor_assert_nonfatal_unreached();
  464. }
  465. }
  466. /* Called when we receive a RENDEZVOUS_ESTABLISHED cell. Change the state of
  467. * the circuit to CIRCUIT_PURPOSE_C_REND_READY. Return 0 on success else a
  468. * negative value and the circuit marked for close. */
  469. int
  470. hs_client_receive_rendezvous_acked(origin_circuit_t *circ,
  471. const uint8_t *payload, size_t payload_len)
  472. {
  473. tor_assert(circ);
  474. tor_assert(payload);
  475. (void) payload_len;
  476. if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  477. log_warn(LD_PROTOCOL, "Got a RENDEZVOUS_ESTABLISHED but we were not "
  478. "expecting one. Closing circuit.");
  479. goto err;
  480. }
  481. log_info(LD_REND, "Received an RENDEZVOUS_ESTABLISHED. This circuit is "
  482. "now ready for rendezvous.");
  483. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_REND_READY);
  484. /* Set timestamp_dirty, because circuit_expire_building expects it to
  485. * specify when a circuit entered the _C_REND_READY state. */
  486. TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
  487. /* From a path bias point of view, this circuit is now successfully used.
  488. * Waiting any longer opens us up to attacks from malicious hidden services.
  489. * They could induce the client to attempt to connect to their hidden
  490. * service and never reply to the client's rend requests */
  491. pathbias_mark_use_success(circ);
  492. /* If we already have the introduction circuit built, make sure we send
  493. * the INTRODUCE cell _now_ */
  494. connection_ap_attach_pending(1);
  495. return 0;
  496. err:
  497. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  498. return -1;
  499. }
  500. /* This is called when a descriptor has arrived following a fetch request and
  501. * has been stored in the client cache. Every entry connection that matches
  502. * the service identity key in the ident will get attached to the hidden
  503. * service circuit. */
  504. void
  505. hs_client_desc_has_arrived(const hs_ident_dir_conn_t *ident)
  506. {
  507. time_t now = time(NULL);
  508. smartlist_t *conns = NULL;
  509. tor_assert(ident);
  510. conns = connection_list_by_type_state(CONN_TYPE_AP,
  511. AP_CONN_STATE_RENDDESC_WAIT);
  512. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
  513. const hs_descriptor_t *desc;
  514. entry_connection_t *entry_conn = TO_ENTRY_CONN(base_conn);
  515. const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(entry_conn);
  516. /* Only consider the entry connections that matches the service for which
  517. * we just fetched its descriptor. */
  518. if (!edge_conn->hs_ident ||
  519. !ed25519_pubkey_eq(&ident->identity_pk,
  520. &edge_conn->hs_ident->identity_pk)) {
  521. continue;
  522. }
  523. assert_connection_ok(base_conn, now);
  524. /* We were just called because we stored the descriptor for this service
  525. * so not finding a descriptor means we have a bigger problem. */
  526. desc = hs_cache_lookup_as_client(&ident->identity_pk);
  527. if (BUG(desc == NULL)) {
  528. goto end;
  529. }
  530. if (!hs_client_any_intro_points_usable(desc)) {
  531. log_info(LD_REND, "Hidden service descriptor is unusable. "
  532. "Closing streams.");
  533. connection_mark_unattached_ap(entry_conn,
  534. END_STREAM_REASON_RESOLVEFAILED);
  535. /* XXX: Note the connection attempt. */
  536. goto end;
  537. }
  538. log_info(LD_REND, "Descriptor has arrived. Launching circuits.");
  539. /* Restart their timeout values, so they get a fair shake at connecting to
  540. * the hidden service. XXX: Improve comment on why this is needed. */
  541. base_conn->timestamp_created = now;
  542. base_conn->timestamp_lastread = now;
  543. base_conn->timestamp_lastwritten = now;
  544. /* Change connection's state into waiting for a circuit. */
  545. base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  546. connection_ap_mark_as_pending_circuit(entry_conn);
  547. } SMARTLIST_FOREACH_END(base_conn);
  548. end:
  549. /* We don't have ownership of the objects in this list. */
  550. smartlist_free(conns);
  551. }