hs_client.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. /* Get all connections that are waiting on a circuit and flag them back to
  26. * waiting for a hidden service descriptor for the given service key
  27. * service_identity_pk. */
  28. static void
  29. flag_all_conn_wait_desc(const ed25519_public_key_t *service_identity_pk)
  30. {
  31. tor_assert(service_identity_pk);
  32. smartlist_t *conns =
  33. connection_list_by_type_state(CONN_TYPE_AP, AP_CONN_STATE_CIRCUIT_WAIT);
  34. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  35. edge_connection_t *edge_conn;
  36. if (BUG(!CONN_IS_EDGE(conn))) {
  37. continue;
  38. }
  39. edge_conn = TO_EDGE_CONN(conn);
  40. if (edge_conn->hs_ident &&
  41. ed25519_pubkey_eq(&edge_conn->hs_ident->identity_pk,
  42. service_identity_pk)) {
  43. connection_ap_mark_as_non_pending_circuit(TO_ENTRY_CONN(conn));
  44. conn->state = AP_CONN_STATE_RENDDESC_WAIT;
  45. }
  46. } SMARTLIST_FOREACH_END(conn);
  47. smartlist_free(conns);
  48. }
  49. /* A v3 HS circuit successfully connected to the hidden service. Update the
  50. * stream state at <b>hs_conn_ident</b> appropriately. */
  51. static void
  52. note_connection_attempt_succeeded(const hs_ident_edge_conn_t *hs_conn_ident)
  53. {
  54. (void) hs_conn_ident;
  55. /* TODO: When implementing client side */
  56. return;
  57. }
  58. /* Given the pubkey of a hidden service in <b>onion_identity_pk</b>, fetch its
  59. * descriptor by launching a dir connection to <b>hsdir</b>. Return 1 on
  60. * success or -1 on error. */
  61. static int
  62. directory_launch_v3_desc_fetch(const ed25519_public_key_t *onion_identity_pk,
  63. const routerstatus_t *hsdir)
  64. {
  65. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  66. ed25519_public_key_t blinded_pubkey;
  67. char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
  68. hs_ident_dir_conn_t hs_conn_dir_ident;
  69. int retval;
  70. tor_assert(hsdir);
  71. tor_assert(onion_identity_pk);
  72. /* Get blinded pubkey */
  73. hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
  74. current_time_period, &blinded_pubkey);
  75. /* ...and base64 it. */
  76. retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
  77. if (BUG(retval < 0)) {
  78. return -1;
  79. }
  80. /* Copy onion pk to a dir_ident so that we attach it to the dir conn */
  81. ed25519_pubkey_copy(&hs_conn_dir_ident.identity_pk, onion_identity_pk);
  82. /* Setup directory request */
  83. directory_request_t *req =
  84. directory_request_new(DIR_PURPOSE_FETCH_HSDESC);
  85. directory_request_set_routerstatus(req, hsdir);
  86. directory_request_set_indirection(req, DIRIND_ANONYMOUS);
  87. directory_request_set_resource(req, base64_blinded_pubkey);
  88. directory_request_upload_set_hs_ident(req, &hs_conn_dir_ident);
  89. directory_initiate_request(req);
  90. directory_request_free(req);
  91. log_info(LD_REND, "Descriptor fetch request for service %s with blinded "
  92. "key %s to directory %s",
  93. safe_str_client(ed25519_fmt(onion_identity_pk)),
  94. safe_str_client(base64_blinded_pubkey),
  95. safe_str_client(routerstatus_describe(hsdir)));
  96. /* Cleanup memory. */
  97. memwipe(&blinded_pubkey, 0, sizeof(blinded_pubkey));
  98. memwipe(base64_blinded_pubkey, 0, sizeof(base64_blinded_pubkey));
  99. memwipe(&hs_conn_dir_ident, 0, sizeof(hs_conn_dir_ident));
  100. return 1;
  101. }
  102. /** Return the HSDir we should use to fetch the descriptor of the hidden
  103. * service with identity key <b>onion_identity_pk</b>. */
  104. static routerstatus_t *
  105. pick_hsdir_v3(const ed25519_public_key_t *onion_identity_pk)
  106. {
  107. int retval;
  108. char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
  109. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  110. smartlist_t *responsible_hsdirs;
  111. ed25519_public_key_t blinded_pubkey;
  112. routerstatus_t *hsdir_rs = NULL;
  113. tor_assert(onion_identity_pk);
  114. responsible_hsdirs = smartlist_new();
  115. /* Get blinded pubkey of hidden service */
  116. hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
  117. current_time_period, &blinded_pubkey);
  118. /* ...and base64 it. */
  119. retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
  120. if (BUG(retval < 0)) {
  121. return NULL;
  122. }
  123. /* Get responsible hsdirs of service for this time period */
  124. hs_get_responsible_hsdirs(&blinded_pubkey, current_time_period, 0, 1,
  125. responsible_hsdirs);
  126. log_debug(LD_REND, "Found %d responsible HSDirs and about to pick one.",
  127. smartlist_len(responsible_hsdirs));
  128. /* Pick an HSDir from the responsible ones. The ownership of
  129. * responsible_hsdirs is given to this function so no need to free it. */
  130. hsdir_rs = hs_pick_hsdir(responsible_hsdirs, base64_blinded_pubkey);
  131. return hsdir_rs;
  132. }
  133. /** Fetch a v3 descriptor using the given <b>onion_identity_pk</b>.
  134. *
  135. * On success, 1 is returned. If no hidden service is left to ask, return 0.
  136. * On error, -1 is returned. */
  137. static int
  138. fetch_v3_desc(const ed25519_public_key_t *onion_identity_pk)
  139. {
  140. routerstatus_t *hsdir_rs =NULL;
  141. tor_assert(onion_identity_pk);
  142. hsdir_rs = pick_hsdir_v3(onion_identity_pk);
  143. if (!hsdir_rs) {
  144. log_info(LD_REND, "Couldn't pick a v3 hsdir.");
  145. return 0;
  146. }
  147. return directory_launch_v3_desc_fetch(onion_identity_pk, hsdir_rs);
  148. }
  149. /* Make sure that the given origin circuit circ is a valid correct
  150. * introduction circuit. This asserts on validation failure. */
  151. static void
  152. assert_intro_circ_ok(const origin_circuit_t *circ)
  153. {
  154. tor_assert(circ);
  155. tor_assert(circ->base_.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  156. tor_assert(circ->hs_ident);
  157. tor_assert(hs_ident_intro_circ_is_valid(circ->hs_ident));
  158. assert_circ_anonymity_ok(circ, get_options());
  159. }
  160. /* Find a descriptor intro point object that matches the given ident in the
  161. * given descriptor desc. Return NULL if not found. */
  162. static const hs_desc_intro_point_t *
  163. find_desc_intro_point_by_ident(const hs_ident_circuit_t *ident,
  164. const hs_descriptor_t *desc)
  165. {
  166. const hs_desc_intro_point_t *intro_point = NULL;
  167. tor_assert(ident);
  168. tor_assert(desc);
  169. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  170. const hs_desc_intro_point_t *, ip) {
  171. if (ed25519_pubkey_eq(&ident->intro_auth_pk,
  172. &ip->auth_key_cert->signed_key)) {
  173. intro_point = ip;
  174. break;
  175. }
  176. } SMARTLIST_FOREACH_END(ip);
  177. return intro_point;
  178. }
  179. /* Send an INTRODUCE1 cell along the intro circuit and populate the rend
  180. * circuit identifier with the needed key material for the e2e encryption.
  181. * Return 0 on success, -1 if there is a transient error such that an action
  182. * has been taken to recover and -2 if there is a permanent error indicating
  183. * that both circuits were closed. */
  184. static int
  185. send_introduce1(origin_circuit_t *intro_circ,
  186. origin_circuit_t *rend_circ)
  187. {
  188. int status;
  189. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  190. const ed25519_public_key_t *service_identity_pk = NULL;
  191. const hs_desc_intro_point_t *ip;
  192. assert_intro_circ_ok(intro_circ);
  193. tor_assert(rend_circ);
  194. service_identity_pk = &intro_circ->hs_ident->identity_pk;
  195. /* For logging purposes. There will be a time where the hs_ident will have a
  196. * version number but for now there is none because it's all v3. */
  197. hs_build_address(service_identity_pk, HS_VERSION_THREE, onion_address);
  198. log_info(LD_REND, "Sending INTRODUCE1 cell to service %s on circuit %u",
  199. safe_str_client(onion_address), TO_CIRCUIT(intro_circ)->n_circ_id);
  200. /* 1) Get descriptor from our cache. */
  201. const hs_descriptor_t *desc =
  202. hs_cache_lookup_as_client(service_identity_pk);
  203. if (desc == NULL || !hs_client_any_intro_points_usable(desc)) {
  204. log_info(LD_REND, "Request to %s %s. Trying to fetch a new descriptor.",
  205. safe_str_client(onion_address),
  206. (desc) ? "didn't have usable intro points" :
  207. "didn't have a descriptor");
  208. hs_client_refetch_hsdesc(service_identity_pk);
  209. /* We just triggered a refetch, make sure every connections are back
  210. * waiting for that descriptor. */
  211. flag_all_conn_wait_desc(service_identity_pk);
  212. /* We just asked for a refetch so this is a transient error. */
  213. goto tran_err;
  214. }
  215. /* We need to find which intro point in the descriptor we are connected to
  216. * on intro_circ. */
  217. ip = find_desc_intro_point_by_ident(intro_circ->hs_ident, desc);
  218. if (BUG(ip == NULL)) {
  219. /* If we can find a descriptor from this introduction circuit ident, we
  220. * must have a valid intro point object. Permanent error. */
  221. goto perm_err;
  222. }
  223. /* Send the INTRODUCE1 cell. */
  224. if (hs_circ_send_introduce1(intro_circ, rend_circ, ip,
  225. desc->subcredential) < 0) {
  226. /* Unable to send the cell, the intro circuit has been marked for close so
  227. * this is a permanent error. */
  228. tor_assert_nonfatal(TO_CIRCUIT(intro_circ)->marked_for_close);
  229. goto perm_err;
  230. }
  231. /* Cell has been sent successfully. Copy the introduction point
  232. * authentication and encryption key in the rendezvous circuit identifier so
  233. * we can compute the ntor keys when we receive the RENDEZVOUS2 cell. */
  234. memcpy(&rend_circ->hs_ident->intro_enc_pk, &ip->enc_key,
  235. sizeof(rend_circ->hs_ident->intro_enc_pk));
  236. ed25519_pubkey_copy(&rend_circ->hs_ident->intro_auth_pk,
  237. &intro_circ->hs_ident->intro_auth_pk);
  238. /* Now, we wait for an ACK or NAK on this circuit. */
  239. circuit_change_purpose(TO_CIRCUIT(intro_circ),
  240. CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT);
  241. /* Set timestamp_dirty, because circuit_expire_building expects it to
  242. * specify when a circuit entered the _C_INTRODUCE_ACK_WAIT state. */
  243. TO_CIRCUIT(intro_circ)->timestamp_dirty = time(NULL);
  244. pathbias_count_use_attempt(intro_circ);
  245. /* Success. */
  246. status = 0;
  247. goto end;
  248. perm_err:
  249. /* Permanent error: it is possible that the intro circuit was closed prior
  250. * because we weren't able to send the cell. Make sure we don't double close
  251. * it which would result in a warning. */
  252. if (!TO_CIRCUIT(intro_circ)->marked_for_close) {
  253. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  254. }
  255. circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_INTERNAL);
  256. status = -2;
  257. goto end;
  258. tran_err:
  259. status = -1;
  260. end:
  261. memwipe(onion_address, 0, sizeof(onion_address));
  262. return status;
  263. }
  264. /* ========== */
  265. /* Public API */
  266. /* ========== */
  267. /** A circuit just finished connecting to a hidden service that the stream
  268. * <b>conn</b> has been waiting for. Let the HS subsystem know about this. */
  269. void
  270. hs_client_note_connection_attempt_succeeded(const edge_connection_t *conn)
  271. {
  272. tor_assert(connection_edge_is_rendezvous_stream(conn));
  273. if (BUG(conn->rend_data && conn->hs_ident)) {
  274. log_warn(LD_BUG, "Stream had both rend_data and hs_ident..."
  275. "Prioritizing hs_ident");
  276. }
  277. if (conn->hs_ident) { /* It's v3: pass it to the prop224 handler */
  278. note_connection_attempt_succeeded(conn->hs_ident);
  279. return;
  280. } else if (conn->rend_data) { /* It's v2: pass it to the legacy handler */
  281. rend_client_note_connection_attempt_ended(conn->rend_data);
  282. return;
  283. }
  284. }
  285. /* With the given encoded descriptor in desc_str and the service key in
  286. * service_identity_pk, decode the descriptor and set the desc pointer with a
  287. * newly allocated descriptor object.
  288. *
  289. * Return 0 on success else a negative value and desc is set to NULL. */
  290. int
  291. hs_client_decode_descriptor(const char *desc_str,
  292. const ed25519_public_key_t *service_identity_pk,
  293. hs_descriptor_t **desc)
  294. {
  295. int ret;
  296. uint8_t subcredential[DIGEST256_LEN];
  297. tor_assert(desc_str);
  298. tor_assert(service_identity_pk);
  299. tor_assert(desc);
  300. /* Create subcredential for this HS so that we can decrypt */
  301. {
  302. ed25519_public_key_t blinded_pubkey;
  303. uint64_t current_time_period = hs_get_time_period_num(approx_time());
  304. hs_build_blinded_pubkey(service_identity_pk, NULL, 0, current_time_period,
  305. &blinded_pubkey);
  306. hs_get_subcredential(service_identity_pk, &blinded_pubkey, subcredential);
  307. }
  308. /* Parse descriptor */
  309. ret = hs_desc_decode_descriptor(desc_str, subcredential, desc);
  310. memwipe(subcredential, 0, sizeof(subcredential));
  311. if (ret < 0) {
  312. log_warn(LD_GENERAL, "Could not parse received descriptor as client");
  313. goto err;
  314. }
  315. return 0;
  316. err:
  317. return -1;
  318. }
  319. /** Return true if there are any usable intro points in the v3 HS descriptor
  320. * <b>desc</b>. */
  321. int
  322. hs_client_any_intro_points_usable(const hs_descriptor_t *desc)
  323. {
  324. /* XXX stub waiting for more client-side work:
  325. equivalent to v2 rend_client_any_intro_points_usable() */
  326. tor_assert(desc);
  327. return 1;
  328. }
  329. /** Launch a connection to a hidden service directory to fetch a hidden
  330. * service descriptor using <b>identity_pk</b> to get the necessary keys.
  331. *
  332. * On success, 1 is returned. If no hidden service is left to ask, return 0.
  333. * On error, -1 is returned. (retval is only used by unittests right now) */
  334. int
  335. hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
  336. {
  337. tor_assert(identity_pk);
  338. /* Are we configured to fetch descriptors? */
  339. if (!get_options()->FetchHidServDescriptors) {
  340. log_warn(LD_REND, "We received an onion address for a hidden service "
  341. "descriptor but we are configured to not fetch.");
  342. return 0;
  343. }
  344. /* Check if fetching a desc for this HS is useful to us right now */
  345. {
  346. const hs_descriptor_t *cached_desc = NULL;
  347. cached_desc = hs_cache_lookup_as_client(identity_pk);
  348. if (cached_desc && hs_client_any_intro_points_usable(cached_desc)) {
  349. log_warn(LD_GENERAL, "We would fetch a v3 hidden service descriptor "
  350. "but we already have a useable descriprot.");
  351. return 0;
  352. }
  353. }
  354. return fetch_v3_desc(identity_pk);
  355. }
  356. /* This is called when we are trying to attach an AP connection to these
  357. * hidden service circuits from connection_ap_handshake_attach_circuit().
  358. * Return 0 on success, -1 for a transient error that is actions were
  359. * triggered to recover or -2 for a permenent error where both circuits will
  360. * marked for close.
  361. *
  362. * The following supports every hidden service version. */
  363. int
  364. hs_client_send_introduce1(origin_circuit_t *intro_circ,
  365. origin_circuit_t *rend_circ)
  366. {
  367. return (intro_circ->hs_ident) ? send_introduce1(intro_circ, rend_circ) :
  368. rend_client_send_introduction(intro_circ,
  369. rend_circ);
  370. }