hs_circuit.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_circuit.c
  5. **/
  6. #include "or.h"
  7. #include "circpathbias.h"
  8. #include "circuitbuild.h"
  9. #include "circuitlist.h"
  10. #include "circuituse.h"
  11. #include "config.h"
  12. #include "policies.h"
  13. #include "relay.h"
  14. #include "rephist.h"
  15. #include "router.h"
  16. #include "hs_cell.h"
  17. #include "hs_circuit.h"
  18. #include "hs_ident.h"
  19. #include "hs_ntor.h"
  20. #include "hs_service.h"
  21. /* Trunnel. */
  22. #include "ed25519_cert.h"
  23. #include "hs/cell_common.h"
  24. #include "hs/cell_establish_intro.h"
  25. /* A circuit is about to become an e2e rendezvous circuit. Check
  26. * <b>circ_purpose</b> and ensure that it's properly set. Return true iff
  27. * circuit purpose is properly set, otherwise return false. */
  28. static int
  29. circuit_purpose_is_correct_for_rend(unsigned int circ_purpose,
  30. int is_service_side)
  31. {
  32. if (is_service_side) {
  33. if (circ_purpose != CIRCUIT_PURPOSE_S_CONNECT_REND) {
  34. log_warn(LD_BUG,
  35. "HS e2e circuit setup with wrong purpose (%d)", circ_purpose);
  36. return 0;
  37. }
  38. }
  39. if (!is_service_side) {
  40. if (circ_purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  41. circ_purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  42. log_warn(LD_BUG,
  43. "Client e2e circuit setup with wrong purpose (%d)", circ_purpose);
  44. return 0;
  45. }
  46. }
  47. return 1;
  48. }
  49. /* Create and return a crypt path for the final hop of a v3 prop224 rendezvous
  50. * circuit. Initialize the crypt path crypto using the output material from the
  51. * ntor key exchange at <b>ntor_key_seed</b>.
  52. *
  53. * If <b>is_service_side</b> is set, we are the hidden service and the final
  54. * hop of the rendezvous circuit is the client on the other side. */
  55. static crypt_path_t *
  56. create_rend_cpath(const uint8_t *ntor_key_seed, size_t seed_len,
  57. int is_service_side)
  58. {
  59. uint8_t keys[HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN];
  60. crypt_path_t *cpath = NULL;
  61. /* Do the key expansion */
  62. if (hs_ntor_circuit_key_expansion(ntor_key_seed, seed_len,
  63. keys, sizeof(keys)) < 0) {
  64. goto err;
  65. }
  66. /* Setup the cpath */
  67. cpath = tor_malloc_zero(sizeof(crypt_path_t));
  68. cpath->magic = CRYPT_PATH_MAGIC;
  69. if (circuit_init_cpath_crypto(cpath, (char*)keys, sizeof(keys),
  70. is_service_side, 1) < 0) {
  71. tor_free(cpath);
  72. goto err;
  73. }
  74. err:
  75. memwipe(keys, 0, sizeof(keys));
  76. return cpath;
  77. }
  78. /* We are a v2 legacy HS client: Create and return a crypt path for the hidden
  79. * service on the other side of the rendezvous circuit <b>circ</b>. Initialize
  80. * the crypt path crypto using the body of the RENDEZVOUS1 cell at
  81. * <b>rend_cell_body</b> (which must be at least DH_KEY_LEN+DIGEST_LEN bytes).
  82. */
  83. static crypt_path_t *
  84. create_rend_cpath_legacy(origin_circuit_t *circ, const uint8_t *rend_cell_body)
  85. {
  86. crypt_path_t *hop = NULL;
  87. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  88. /* first DH_KEY_LEN bytes are g^y from the service. Finish the dh
  89. * handshake...*/
  90. tor_assert(circ->build_state);
  91. tor_assert(circ->build_state->pending_final_cpath);
  92. hop = circ->build_state->pending_final_cpath;
  93. tor_assert(hop->rend_dh_handshake_state);
  94. if (crypto_dh_compute_secret(LOG_PROTOCOL_WARN, hop->rend_dh_handshake_state,
  95. (char*)rend_cell_body, DH_KEY_LEN,
  96. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  97. log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
  98. goto err;
  99. }
  100. /* ... and set up cpath. */
  101. if (circuit_init_cpath_crypto(hop,
  102. keys+DIGEST_LEN, sizeof(keys)-DIGEST_LEN,
  103. 0, 0) < 0)
  104. goto err;
  105. /* Check whether the digest is right... */
  106. if (tor_memneq(keys, rend_cell_body+DH_KEY_LEN, DIGEST_LEN)) {
  107. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  108. goto err;
  109. }
  110. /* clean up the crypto stuff we just made */
  111. crypto_dh_free(hop->rend_dh_handshake_state);
  112. hop->rend_dh_handshake_state = NULL;
  113. goto done;
  114. err:
  115. hop = NULL;
  116. done:
  117. memwipe(keys, 0, sizeof(keys));
  118. return hop;
  119. }
  120. /* Append the final <b>hop</b> to the cpath of the rend <b>circ</b>, and mark
  121. * <b>circ</b> ready for use to transfer HS relay cells. */
  122. static void
  123. finalize_rend_circuit(origin_circuit_t *circ, crypt_path_t *hop,
  124. int is_service_side)
  125. {
  126. tor_assert(circ);
  127. tor_assert(hop);
  128. /* Notify the circuit state machine that we are splicing this circuit */
  129. int new_circ_purpose = is_service_side ?
  130. CIRCUIT_PURPOSE_S_REND_JOINED : CIRCUIT_PURPOSE_C_REND_JOINED;
  131. circuit_change_purpose(TO_CIRCUIT(circ), new_circ_purpose);
  132. /* All is well. Extend the circuit. */
  133. hop->state = CPATH_STATE_OPEN;
  134. /* Set the windows to default. */
  135. hop->package_window = circuit_initial_package_window();
  136. hop->deliver_window = CIRCWINDOW_START;
  137. /* Now that this circuit has finished connecting to its destination,
  138. * make sure circuit_get_open_circ_or_launch is willing to return it
  139. * so we can actually use it. */
  140. circ->hs_circ_has_timed_out = 0;
  141. /* Append the hop to the cpath of this circuit */
  142. onion_append_to_cpath(&circ->cpath, hop);
  143. /* In legacy code, 'pending_final_cpath' points to the final hop we just
  144. * appended to the cpath. We set the original pointer to NULL so that we
  145. * don't double free it. */
  146. if (circ->build_state) {
  147. circ->build_state->pending_final_cpath = NULL;
  148. }
  149. /* Finally, mark circuit as ready to be used for client streams */
  150. if (!is_service_side) {
  151. circuit_try_attaching_streams(circ);
  152. }
  153. }
  154. /* For a given circuit and a service introduction point object, register the
  155. * intro circuit to the circuitmap. This supports legacy intro point. */
  156. static void
  157. register_intro_circ(const hs_service_intro_point_t *ip,
  158. origin_circuit_t *circ)
  159. {
  160. tor_assert(ip);
  161. tor_assert(circ);
  162. if (ip->base.is_only_legacy) {
  163. uint8_t digest[DIGEST_LEN];
  164. if (BUG(crypto_pk_get_digest(ip->legacy_key, (char *) digest) < 0)) {
  165. return;
  166. }
  167. hs_circuitmap_register_intro_circ_v2_service_side(circ, digest);
  168. } else {
  169. hs_circuitmap_register_intro_circ_v3_service_side(circ,
  170. &ip->auth_key_kp.pubkey);
  171. }
  172. }
  173. /* Return the number of opened introduction circuit for the given circuit that
  174. * is matching its identity key. */
  175. static unsigned int
  176. count_opened_desc_intro_point_circuits(const hs_service_t *service,
  177. const hs_service_descriptor_t *desc)
  178. {
  179. unsigned int count = 0;
  180. tor_assert(service);
  181. tor_assert(desc);
  182. DIGEST256MAP_FOREACH(desc->intro_points.map, key,
  183. const hs_service_intro_point_t *, ip) {
  184. circuit_t *circ;
  185. origin_circuit_t *ocirc;
  186. if (ip->base.is_only_legacy) {
  187. uint8_t digest[DIGEST_LEN];
  188. if (BUG(crypto_pk_get_digest(ip->legacy_key, (char *) digest) < 0)) {
  189. continue;
  190. }
  191. ocirc = hs_circuitmap_get_intro_circ_v2_service_side(digest);
  192. } else {
  193. ocirc =
  194. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  195. }
  196. if (ocirc == NULL) {
  197. continue;
  198. }
  199. circ = TO_CIRCUIT(ocirc);
  200. tor_assert(circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  201. circ->purpose == CIRCUIT_PURPOSE_S_INTRO);
  202. /* Having a circuit not for the requested service is really bad. */
  203. tor_assert(ed25519_pubkey_eq(&service->keys.identity_pk,
  204. &ocirc->hs_ident->identity_pk));
  205. /* Only count opened circuit and skip circuit that will be closed. */
  206. if (!circ->marked_for_close && circ->state == CIRCUIT_STATE_OPEN) {
  207. count++;
  208. }
  209. } DIGEST256MAP_FOREACH_END;
  210. return count;
  211. }
  212. /* From a given service, rendezvous cookie and handshake infor, create a
  213. * rendezvous point circuit identifier. This can't fail. */
  214. static hs_ident_circuit_t *
  215. create_rp_circuit_identifier(const hs_service_t *service,
  216. const uint8_t *rendezvous_cookie,
  217. const curve25519_public_key_t *server_pk,
  218. const hs_ntor_rend_cell_keys_t *keys)
  219. {
  220. hs_ident_circuit_t *ident;
  221. uint8_t handshake_info[CURVE25519_PUBKEY_LEN + DIGEST256_LEN];
  222. tor_assert(service);
  223. tor_assert(rendezvous_cookie);
  224. tor_assert(server_pk);
  225. tor_assert(keys);
  226. ident = hs_ident_circuit_new(&service->keys.identity_pk,
  227. HS_IDENT_CIRCUIT_RENDEZVOUS);
  228. /* Copy the RENDEZVOUS_COOKIE which is the unique identifier. */
  229. memcpy(ident->rendezvous_cookie, rendezvous_cookie,
  230. sizeof(ident->rendezvous_cookie));
  231. /* Build the HANDSHAKE_INFO which looks like this:
  232. * SERVER_PK [32 bytes]
  233. * AUTH_INPUT_MAC [32 bytes]
  234. */
  235. memcpy(handshake_info, server_pk->public_key, CURVE25519_PUBKEY_LEN);
  236. memcpy(handshake_info + CURVE25519_PUBKEY_LEN, keys->rend_cell_auth_mac,
  237. DIGEST256_LEN);
  238. tor_assert(sizeof(ident->rendezvous_handshake_info) ==
  239. sizeof(handshake_info));
  240. memcpy(ident->rendezvous_handshake_info, handshake_info,
  241. sizeof(ident->rendezvous_handshake_info));
  242. /* Finally copy the NTOR_KEY_SEED for e2e encryption on the circuit. */
  243. tor_assert(sizeof(ident->rendezvous_ntor_key_seed) ==
  244. sizeof(keys->ntor_key_seed));
  245. memcpy(ident->rendezvous_ntor_key_seed, keys->ntor_key_seed,
  246. sizeof(ident->rendezvous_ntor_key_seed));
  247. return ident;
  248. }
  249. /* From a given service and service intro point, create an introduction point
  250. * circuit identifier. This can't fail. */
  251. static hs_ident_circuit_t *
  252. create_intro_circuit_identifier(const hs_service_t *service,
  253. const hs_service_intro_point_t *ip)
  254. {
  255. hs_ident_circuit_t *ident;
  256. tor_assert(service);
  257. tor_assert(ip);
  258. ident = hs_ident_circuit_new(&service->keys.identity_pk,
  259. HS_IDENT_CIRCUIT_INTRO);
  260. ed25519_pubkey_copy(&ident->intro_auth_pk, &ip->auth_key_kp.pubkey);
  261. return ident;
  262. }
  263. /* For a given introduction point and an introduction circuit, send the
  264. * ESTABLISH_INTRO cell. The service object is used for logging. This can fail
  265. * and if so, the circuit is closed and the intro point object is flagged
  266. * that the circuit is not established anymore which is important for the
  267. * retry mechanism. */
  268. static void
  269. send_establish_intro(const hs_service_t *service,
  270. hs_service_intro_point_t *ip, origin_circuit_t *circ)
  271. {
  272. ssize_t cell_len;
  273. uint8_t payload[RELAY_PAYLOAD_SIZE];
  274. tor_assert(service);
  275. tor_assert(ip);
  276. tor_assert(circ);
  277. /* Encode establish intro cell. */
  278. cell_len = hs_cell_build_establish_intro(circ->cpath->prev->rend_circ_nonce,
  279. ip, payload);
  280. if (cell_len < 0) {
  281. log_warn(LD_REND, "Unable to encode ESTABLISH_INTRO cell for service %s "
  282. "on circuit %u. Closing circuit.",
  283. safe_str_client(service->onion_address),
  284. TO_CIRCUIT(circ)->n_circ_id);
  285. goto err;
  286. }
  287. /* Send the cell on the circuit. */
  288. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
  289. RELAY_COMMAND_ESTABLISH_INTRO,
  290. (char *) payload, cell_len,
  291. circ->cpath->prev) < 0) {
  292. log_info(LD_REND, "Unable to send ESTABLISH_INTRO cell for service %s "
  293. "on circuit %u.",
  294. safe_str_client(service->onion_address),
  295. TO_CIRCUIT(circ)->n_circ_id);
  296. /* On error, the circuit has been closed. */
  297. goto done;
  298. }
  299. /* Record the attempt to use this circuit. */
  300. pathbias_count_use_attempt(circ);
  301. goto done;
  302. err:
  303. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  304. done:
  305. memwipe(payload, 0, sizeof(payload));
  306. }
  307. /* From a list of link specifier, an onion key and if we are requesting a
  308. * direct connection (ex: single onion service), return a newly allocated
  309. * extend_info_t object. This function checks the firewall policies and if we
  310. * are allowed to extend to the chosen address.
  311. *
  312. * if either IPv4 or legacy ID is missing, error.
  313. * if not direct_conn, IPv4 is prefered.
  314. * if direct_conn, IPv6 is prefered if we have one available.
  315. * if firewall does not allow the chosen address, error.
  316. *
  317. * Return NULL if we can fulfill the conditions. */
  318. static extend_info_t *
  319. get_rp_extend_info(const smartlist_t *link_specifiers,
  320. const curve25519_public_key_t *onion_key, int direct_conn)
  321. {
  322. int have_v4 = 0, have_v6 = 0, have_legacy_id = 0, have_ed25519_id = 0;
  323. char legacy_id[DIGEST_LEN] = {0};
  324. uint16_t port_v4 = 0, port_v6 = 0, port = 0;
  325. tor_addr_t addr_v4, addr_v6, *addr = NULL;
  326. ed25519_public_key_t ed25519_pk;
  327. extend_info_t *info = NULL;
  328. tor_assert(link_specifiers);
  329. tor_assert(onion_key);
  330. SMARTLIST_FOREACH_BEGIN(link_specifiers, const link_specifier_t *, ls) {
  331. switch (link_specifier_get_ls_type(ls)) {
  332. case LS_IPV4:
  333. /* Skip if we already seen a v4. */
  334. if (have_v4) continue;
  335. tor_addr_from_ipv4h(&addr_v4,
  336. link_specifier_get_un_ipv4_addr(ls));
  337. port_v4 = link_specifier_get_un_ipv4_port(ls);
  338. have_v4 = 1;
  339. break;
  340. case LS_IPV6:
  341. /* Skip if we already seen a v6. */
  342. if (have_v6) continue;
  343. tor_addr_from_ipv6_bytes(&addr_v6,
  344. (const char *) link_specifier_getconstarray_un_ipv6_addr(ls));
  345. port_v6 = link_specifier_get_un_ipv6_port(ls);
  346. have_v6 = 1;
  347. break;
  348. case LS_LEGACY_ID:
  349. /* Make sure we do have enough bytes for the legacy ID. */
  350. if (link_specifier_getlen_un_legacy_id(ls) < sizeof(legacy_id)) {
  351. break;
  352. }
  353. memcpy(legacy_id, link_specifier_getconstarray_un_legacy_id(ls),
  354. sizeof(legacy_id));
  355. have_legacy_id = 1;
  356. break;
  357. case LS_ED25519_ID:
  358. memcpy(ed25519_pk.pubkey,
  359. link_specifier_getconstarray_un_ed25519_id(ls),
  360. ED25519_PUBKEY_LEN);
  361. have_ed25519_id = 1;
  362. break;
  363. default:
  364. /* Ignore unknown. */
  365. break;
  366. }
  367. } SMARTLIST_FOREACH_END(ls);
  368. /* IPv4, legacy ID and ed25519 are mandatory. */
  369. if (!have_v4 || !have_legacy_id || !have_ed25519_id) {
  370. goto done;
  371. }
  372. /* By default, we pick IPv4 but this might change to v6 if certain
  373. * conditions are met. */
  374. addr = &addr_v4; port = port_v4;
  375. /* If we are NOT in a direct connection, we'll use our Guard and a 3-hop
  376. * circuit so we can't extend in IPv6. And at this point, we do have an IPv4
  377. * address available so go to validation. */
  378. if (!direct_conn) {
  379. goto validate;
  380. }
  381. /* From this point on, we have a request for a direct connection to the
  382. * rendezvous point so make sure we can actually connect through our
  383. * firewall. We'll prefer IPv6. */
  384. /* IPv6 test. */
  385. if (have_v6 &&
  386. fascist_firewall_allows_address_addr(&addr_v6, port_v6,
  387. FIREWALL_OR_CONNECTION, 1, 1)) {
  388. /* Direct connection and we can reach it in IPv6 so go for it. */
  389. addr = &addr_v6; port = port_v6;
  390. goto validate;
  391. }
  392. /* IPv4 test and we are sure we have a v4 because of the check above. */
  393. if (fascist_firewall_allows_address_addr(&addr_v4, port_v4,
  394. FIREWALL_OR_CONNECTION, 0, 0)) {
  395. /* Direct connection and we can reach it in IPv4 so go for it. */
  396. addr = &addr_v4; port = port_v4;
  397. goto validate;
  398. }
  399. validate:
  400. /* We'll validate now that the address we've picked isn't a private one. If
  401. * it is, are we allowing to extend to private address? */
  402. if (!extend_info_addr_is_allowed(addr)) {
  403. log_warn(LD_REND, "Rendezvous point address is private and it is not "
  404. "allowed to extend to it: %s:%u",
  405. fmt_addr(&addr_v4), port_v4);
  406. goto done;
  407. }
  408. /* We do have everything for which we think we can connect successfully. */
  409. info = extend_info_new(NULL, legacy_id, &ed25519_pk, NULL, onion_key,
  410. addr, port);
  411. done:
  412. return info;
  413. }
  414. /* For a given service, the ntor onion key and a rendezvous cookie, launch a
  415. * circuit to the rendezvous point specified by the link specifiers. On
  416. * success, a circuit identifier is attached to the circuit with the needed
  417. * data. This function will try to open a circuit for a maximum value of
  418. * MAX_REND_FAILURES then it will give up. */
  419. static void
  420. launch_rendezvous_point_circuit(const hs_service_t *service,
  421. const hs_service_intro_point_t *ip,
  422. const hs_cell_introduce2_data_t *data)
  423. {
  424. int circ_needs_uptime;
  425. time_t now = time(NULL);
  426. extend_info_t *info = NULL;
  427. origin_circuit_t *circ;
  428. tor_assert(service);
  429. tor_assert(ip);
  430. tor_assert(data);
  431. circ_needs_uptime = hs_service_requires_uptime_circ(service->config.ports);
  432. /* Help predict this next time */
  433. rep_hist_note_used_internal(now, circ_needs_uptime, 1);
  434. /* Get the extend info data structure for the chosen rendezvous point
  435. * specified by the given link specifiers. */
  436. info = get_rp_extend_info(data->link_specifiers, &data->onion_pk,
  437. service->config.is_single_onion);
  438. if (info == NULL) {
  439. /* We are done here, we can't extend to the rendezvous point. */
  440. goto end;
  441. }
  442. for (int i = 0; i < MAX_REND_FAILURES; i++) {
  443. int circ_flags = CIRCLAUNCH_NEED_CAPACITY | CIRCLAUNCH_IS_INTERNAL;
  444. if (circ_needs_uptime) {
  445. circ_flags |= CIRCLAUNCH_NEED_UPTIME;
  446. }
  447. /* Firewall and policies are checked when getting the extend info. */
  448. if (service->config.is_single_onion) {
  449. circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
  450. }
  451. circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND, info,
  452. circ_flags);
  453. if (circ != NULL) {
  454. /* Stop retrying, we have a circuit! */
  455. break;
  456. }
  457. }
  458. if (circ == NULL) {
  459. log_warn(LD_REND, "Giving up on launching rendezvous circuit to %s "
  460. "for service %s",
  461. safe_str_client(extend_info_describe(info)),
  462. safe_str_client(service->onion_address));
  463. goto end;
  464. }
  465. log_info(LD_REND, "Rendezvous circuit launched to %s with cookie %s "
  466. "for service %s",
  467. safe_str_client(extend_info_describe(info)),
  468. safe_str_client(hex_str((const char *) data->rendezvous_cookie,
  469. REND_COOKIE_LEN)),
  470. safe_str_client(service->onion_address));
  471. tor_assert(circ->build_state);
  472. /* Rendezvous circuit have a specific timeout for the time spent on trying
  473. * to connect to the rendezvous point. */
  474. circ->build_state->expiry_time = now + MAX_REND_TIMEOUT;
  475. /* Create circuit identifier and key material. */
  476. {
  477. hs_ntor_rend_cell_keys_t keys;
  478. curve25519_keypair_t ephemeral_kp;
  479. /* No need for extra strong, this is only for this circuit life time. This
  480. * key will be used for the RENDEZVOUS1 cell that will be sent on the
  481. * circuit once opened. */
  482. curve25519_keypair_generate(&ephemeral_kp, 0);
  483. if (hs_ntor_service_get_rendezvous1_keys(&ip->auth_key_kp.pubkey,
  484. &ip->enc_key_kp,
  485. &ephemeral_kp, &data->client_pk,
  486. &keys) < 0) {
  487. /* This should not really happened but just in case, don't make tor
  488. * freak out, close the circuit and move on. */
  489. log_info(LD_REND, "Unable to get RENDEZVOUS1 key material for "
  490. "service %s",
  491. safe_str_client(service->onion_address));
  492. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  493. goto end;
  494. }
  495. circ->hs_ident = create_rp_circuit_identifier(service,
  496. data->rendezvous_cookie,
  497. &ephemeral_kp.pubkey, &keys);
  498. memwipe(&ephemeral_kp, 0, sizeof(ephemeral_kp));
  499. memwipe(&keys, 0, sizeof(keys));
  500. tor_assert(circ->hs_ident);
  501. }
  502. end:
  503. extend_info_free(info);
  504. }
  505. /* ========== */
  506. /* Public API */
  507. /* ========== */
  508. /* For a given service and a service intro point, launch a circuit to the
  509. * extend info ei. If the service is a single onion, a one-hop circuit will be
  510. * requested. Return 0 if the circuit was successfully launched and tagged
  511. * with the correct identifier. On error, a negative value is returned. */
  512. int
  513. hs_circ_launch_intro_point(hs_service_t *service,
  514. const hs_service_intro_point_t *ip,
  515. extend_info_t *ei, time_t now)
  516. {
  517. /* Standard flags for introduction circuit. */
  518. int ret = -1, circ_flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  519. origin_circuit_t *circ;
  520. tor_assert(service);
  521. tor_assert(ip);
  522. tor_assert(ei);
  523. /* Update circuit flags in case of a single onion service that requires a
  524. * direct connection. */
  525. if (service->config.is_single_onion) {
  526. circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
  527. }
  528. log_info(LD_REND, "Launching a circuit to intro point %s for service %s.",
  529. safe_str_client(extend_info_describe(ei)),
  530. safe_str_client(service->onion_address));
  531. /* Note down that we are about to use an internal circuit. */
  532. rep_hist_note_used_internal(now, circ_flags & CIRCLAUNCH_NEED_UPTIME,
  533. circ_flags & CIRCLAUNCH_NEED_CAPACITY);
  534. /* Note down the launch for the retry period. Even if the circuit fails to
  535. * be launched, we still want to respect the retry period to avoid stress on
  536. * the circuit subsystem. */
  537. service->state.num_intro_circ_launched++;
  538. circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  539. ei, circ_flags);
  540. if (circ == NULL) {
  541. goto end;
  542. }
  543. /* Setup the circuit identifier and attach it to it. */
  544. circ->hs_ident = create_intro_circuit_identifier(service, ip);
  545. tor_assert(circ->hs_ident);
  546. /* Register circuit in the global circuitmap. */
  547. register_intro_circ(ip, circ);
  548. /* Success. */
  549. ret = 0;
  550. end:
  551. return ret;
  552. }
  553. /* Called when a service introduction point circuit is done building. Given
  554. * the service and intro point object, this function will send the
  555. * ESTABLISH_INTRO cell on the circuit. Return 0 on success. Return 1 if the
  556. * circuit has been repurposed to General because we already have too many
  557. * opened. */
  558. int
  559. hs_circ_service_intro_has_opened(hs_service_t *service,
  560. hs_service_intro_point_t *ip,
  561. const hs_service_descriptor_t *desc,
  562. origin_circuit_t *circ)
  563. {
  564. int ret = 0;
  565. unsigned int num_intro_circ, num_needed_circ;
  566. tor_assert(service);
  567. tor_assert(ip);
  568. tor_assert(desc);
  569. tor_assert(circ);
  570. num_intro_circ = count_opened_desc_intro_point_circuits(service, desc);
  571. num_needed_circ = service->config.num_intro_points;
  572. if (num_intro_circ > num_needed_circ) {
  573. /* There are too many opened valid intro circuit for what the service
  574. * needs so repurpose this one. */
  575. /* XXX: Legacy code checks options->ExcludeNodes and if not NULL it just
  576. * closes the circuit. I have NO idea why it does that so it hasn't been
  577. * added here. I can only assume in case our ExcludeNodes list changes but
  578. * in that case, all circuit are flagged unusable (config.c). --dgoulet */
  579. log_info(LD_CIRC | LD_REND, "Introduction circuit just opened but we "
  580. "have enough for service %s. Repurposing "
  581. "it to general and leaving internal.",
  582. safe_str_client(service->onion_address));
  583. tor_assert(circ->build_state->is_internal);
  584. /* Remove it from the circuitmap. */
  585. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  586. /* Cleaning up the hidden service identifier and repurpose. */
  587. hs_ident_circuit_free(circ->hs_ident);
  588. circ->hs_ident = NULL;
  589. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_GENERAL);
  590. /* Inform that this circuit just opened for this new purpose. */
  591. circuit_has_opened(circ);
  592. /* This return value indicate to the caller that the IP object should be
  593. * removed from the service because it's corresponding circuit has just
  594. * been repurposed. */
  595. ret = 1;
  596. goto done;
  597. }
  598. log_info(LD_REND, "Introduction circuit %u established for service %s.",
  599. TO_CIRCUIT(circ)->n_circ_id,
  600. safe_str_client(service->onion_address));
  601. circuit_log_path(LOG_INFO, LD_REND, circ);
  602. /* Time to send an ESTABLISH_INTRO cell on this circuit. On error, this call
  603. * makes sure the circuit gets closed. */
  604. send_establish_intro(service, ip, circ);
  605. done:
  606. return ret;
  607. }
  608. /* Handle an INTRO_ESTABLISHED cell payload of length payload_len arriving on
  609. * the given introduction circuit circ. The service is only used for logging
  610. * purposes. Return 0 on success else a negative value. */
  611. int
  612. hs_circ_handle_intro_established(const hs_service_t *service,
  613. const hs_service_intro_point_t *ip,
  614. origin_circuit_t *circ,
  615. const uint8_t *payload, size_t payload_len)
  616. {
  617. int ret = -1;
  618. tor_assert(service);
  619. tor_assert(ip);
  620. tor_assert(circ);
  621. tor_assert(payload);
  622. /* Try to parse the payload into a cell making sure we do actually have a
  623. * valid cell. For a legacy node, it's an empty payload so as long as we
  624. * have the cell, we are good. */
  625. if (!ip->base.is_only_legacy &&
  626. hs_cell_parse_intro_established(payload, payload_len) < 0) {
  627. log_warn(LD_REND, "Unable to parse the INTRO_ESTABLISHED cell on "
  628. "circuit %u for service %s",
  629. TO_CIRCUIT(circ)->n_circ_id,
  630. safe_str_client(service->onion_address));
  631. goto done;
  632. }
  633. /* Switch the purpose to a fully working intro point. */
  634. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_S_INTRO);
  635. /* Getting a valid INTRODUCE_ESTABLISHED means we've successfully used the
  636. * circuit so update our pathbias subsystem. */
  637. pathbias_mark_use_success(circ);
  638. /* Success. */
  639. ret = 0;
  640. done:
  641. return ret;
  642. }
  643. /* Handle an INTRODUCE2 unparsed payload of payload_len for the given circuit
  644. * and service. This cell is associated with the intro point object ip and the
  645. * subcredential. Return 0 on success else a negative value. */
  646. int
  647. hs_circ_handle_introduce2(const hs_service_t *service,
  648. const origin_circuit_t *circ,
  649. hs_service_intro_point_t *ip,
  650. const uint8_t *subcredential,
  651. const uint8_t *payload, size_t payload_len)
  652. {
  653. int ret = -1;
  654. hs_cell_introduce2_data_t data;
  655. tor_assert(service);
  656. tor_assert(circ);
  657. tor_assert(ip);
  658. tor_assert(subcredential);
  659. tor_assert(payload);
  660. /* Populate the data structure with everything we need for the cell to be
  661. * parsed, decrypted and key material computed correctly. */
  662. data.auth_pk = &ip->auth_key_kp.pubkey;
  663. data.enc_kp = &ip->enc_key_kp;
  664. data.subcredential = subcredential;
  665. data.payload = payload;
  666. data.payload_len = payload_len;
  667. data.link_specifiers = smartlist_new();
  668. if (hs_cell_parse_introduce2(&data, circ, service) < 0) {
  669. goto done;
  670. }
  671. /* At this point, we just confirmed that the full INTRODUCE2 cell is valid
  672. * so increment our counter that we've seen one on this intro point. */
  673. ip->introduce2_count++;
  674. /* Launch rendezvous circuit with the onion key and rend cookie. */
  675. launch_rendezvous_point_circuit(service, ip, &data);
  676. /* Success. */
  677. ret = 0;
  678. done:
  679. SMARTLIST_FOREACH(data.link_specifiers, link_specifier_t *, lspec,
  680. link_specifier_free(lspec));
  681. smartlist_free(data.link_specifiers);
  682. memwipe(&data, 0, sizeof(data));
  683. return ret;
  684. }
  685. /* Circuit <b>circ</b> just finished the rend ntor key exchange. Use the key
  686. * exchange output material at <b>ntor_key_seed</b> and setup <b>circ</b> to
  687. * serve as a rendezvous end-to-end circuit between the client and the
  688. * service. If <b>is_service_side</b> is set, then we are the hidden service
  689. * and the other side is the client.
  690. *
  691. * Return 0 if the operation went well; in case of error return -1. */
  692. int
  693. hs_circuit_setup_e2e_rend_circ(origin_circuit_t *circ,
  694. const uint8_t *ntor_key_seed, size_t seed_len,
  695. int is_service_side)
  696. {
  697. if (BUG(!circuit_purpose_is_correct_for_rend(TO_CIRCUIT(circ)->purpose,
  698. is_service_side))) {
  699. return -1;
  700. }
  701. crypt_path_t *hop = create_rend_cpath(ntor_key_seed, seed_len,
  702. is_service_side);
  703. if (!hop) {
  704. log_warn(LD_REND, "Couldn't get v3 %s cpath!",
  705. is_service_side ? "service-side" : "client-side");
  706. return -1;
  707. }
  708. finalize_rend_circuit(circ, hop, is_service_side);
  709. return 0;
  710. }
  711. /* We are a v2 legacy HS client and we just received a RENDEZVOUS1 cell
  712. * <b>rend_cell_body</b> on <b>circ</b>. Finish up the DH key exchange and then
  713. * extend the crypt path of <b>circ</b> so that the hidden service is on the
  714. * other side. */
  715. int
  716. hs_circuit_setup_e2e_rend_circ_legacy_client(origin_circuit_t *circ,
  717. const uint8_t *rend_cell_body)
  718. {
  719. if (BUG(!circuit_purpose_is_correct_for_rend(
  720. TO_CIRCUIT(circ)->purpose, 0))) {
  721. return -1;
  722. }
  723. crypt_path_t *hop = create_rend_cpath_legacy(circ, rend_cell_body);
  724. if (!hop) {
  725. log_warn(LD_GENERAL, "Couldn't get v2 cpath.");
  726. return -1;
  727. }
  728. finalize_rend_circuit(circ, hop, 0);
  729. return 0;
  730. }