hs_circuit.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_circuit.c
  5. **/
  6. #define HS_CIRCUIT_PRIVATE
  7. #include "or.h"
  8. #include "circpathbias.h"
  9. #include "circuitbuild.h"
  10. #include "circuitlist.h"
  11. #include "circuituse.h"
  12. #include "config.h"
  13. #include "crypto_rand.h"
  14. #include "crypto_util.h"
  15. #include "nodelist.h"
  16. #include "policies.h"
  17. #include "relay.h"
  18. #include "rendservice.h"
  19. #include "rephist.h"
  20. #include "router.h"
  21. #include "hs_cell.h"
  22. #include "hs_ident.h"
  23. #include "hs_ntor.h"
  24. #include "hs_service.h"
  25. #include "hs_circuit.h"
  26. /* Trunnel. */
  27. #include "ed25519_cert.h"
  28. #include "hs/cell_common.h"
  29. #include "hs/cell_establish_intro.h"
  30. #include "crypt_path_st.h"
  31. #include "origin_circuit_st.h"
  32. /* A circuit is about to become an e2e rendezvous circuit. Check
  33. * <b>circ_purpose</b> and ensure that it's properly set. Return true iff
  34. * circuit purpose is properly set, otherwise return false. */
  35. static int
  36. circuit_purpose_is_correct_for_rend(unsigned int circ_purpose,
  37. int is_service_side)
  38. {
  39. if (is_service_side) {
  40. if (circ_purpose != CIRCUIT_PURPOSE_S_CONNECT_REND) {
  41. log_warn(LD_BUG,
  42. "HS e2e circuit setup with wrong purpose (%d)", circ_purpose);
  43. return 0;
  44. }
  45. }
  46. if (!is_service_side) {
  47. if (circ_purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  48. circ_purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  49. log_warn(LD_BUG,
  50. "Client e2e circuit setup with wrong purpose (%d)", circ_purpose);
  51. return 0;
  52. }
  53. }
  54. return 1;
  55. }
  56. /* Create and return a crypt path for the final hop of a v3 prop224 rendezvous
  57. * circuit. Initialize the crypt path crypto using the output material from the
  58. * ntor key exchange at <b>ntor_key_seed</b>.
  59. *
  60. * If <b>is_service_side</b> is set, we are the hidden service and the final
  61. * hop of the rendezvous circuit is the client on the other side. */
  62. static crypt_path_t *
  63. create_rend_cpath(const uint8_t *ntor_key_seed, size_t seed_len,
  64. int is_service_side)
  65. {
  66. uint8_t keys[HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN];
  67. crypt_path_t *cpath = NULL;
  68. /* Do the key expansion */
  69. if (hs_ntor_circuit_key_expansion(ntor_key_seed, seed_len,
  70. keys, sizeof(keys)) < 0) {
  71. goto err;
  72. }
  73. /* Setup the cpath */
  74. cpath = tor_malloc_zero(sizeof(crypt_path_t));
  75. cpath->magic = CRYPT_PATH_MAGIC;
  76. if (circuit_init_cpath_crypto(cpath, (char*)keys, sizeof(keys),
  77. is_service_side, 1) < 0) {
  78. tor_free(cpath);
  79. goto err;
  80. }
  81. err:
  82. memwipe(keys, 0, sizeof(keys));
  83. return cpath;
  84. }
  85. /* We are a v2 legacy HS client: Create and return a crypt path for the hidden
  86. * service on the other side of the rendezvous circuit <b>circ</b>. Initialize
  87. * the crypt path crypto using the body of the RENDEZVOUS1 cell at
  88. * <b>rend_cell_body</b> (which must be at least DH_KEY_LEN+DIGEST_LEN bytes).
  89. */
  90. static crypt_path_t *
  91. create_rend_cpath_legacy(origin_circuit_t *circ, const uint8_t *rend_cell_body)
  92. {
  93. crypt_path_t *hop = NULL;
  94. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  95. /* first DH_KEY_LEN bytes are g^y from the service. Finish the dh
  96. * handshake...*/
  97. tor_assert(circ->build_state);
  98. tor_assert(circ->build_state->pending_final_cpath);
  99. hop = circ->build_state->pending_final_cpath;
  100. tor_assert(hop->rend_dh_handshake_state);
  101. if (crypto_dh_compute_secret(LOG_PROTOCOL_WARN, hop->rend_dh_handshake_state,
  102. (char*)rend_cell_body, DH_KEY_LEN,
  103. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
  104. log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
  105. goto err;
  106. }
  107. /* ... and set up cpath. */
  108. if (circuit_init_cpath_crypto(hop,
  109. keys+DIGEST_LEN, sizeof(keys)-DIGEST_LEN,
  110. 0, 0) < 0)
  111. goto err;
  112. /* Check whether the digest is right... */
  113. if (tor_memneq(keys, rend_cell_body+DH_KEY_LEN, DIGEST_LEN)) {
  114. log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
  115. goto err;
  116. }
  117. /* clean up the crypto stuff we just made */
  118. crypto_dh_free(hop->rend_dh_handshake_state);
  119. hop->rend_dh_handshake_state = NULL;
  120. goto done;
  121. err:
  122. hop = NULL;
  123. done:
  124. memwipe(keys, 0, sizeof(keys));
  125. return hop;
  126. }
  127. /* Append the final <b>hop</b> to the cpath of the rend <b>circ</b>, and mark
  128. * <b>circ</b> ready for use to transfer HS relay cells. */
  129. static void
  130. finalize_rend_circuit(origin_circuit_t *circ, crypt_path_t *hop,
  131. int is_service_side)
  132. {
  133. tor_assert(circ);
  134. tor_assert(hop);
  135. /* Notify the circuit state machine that we are splicing this circuit */
  136. int new_circ_purpose = is_service_side ?
  137. CIRCUIT_PURPOSE_S_REND_JOINED : CIRCUIT_PURPOSE_C_REND_JOINED;
  138. circuit_change_purpose(TO_CIRCUIT(circ), new_circ_purpose);
  139. /* All is well. Extend the circuit. */
  140. hop->state = CPATH_STATE_OPEN;
  141. /* Set the windows to default. */
  142. hop->package_window = circuit_initial_package_window();
  143. hop->deliver_window = CIRCWINDOW_START;
  144. /* Now that this circuit has finished connecting to its destination,
  145. * make sure circuit_get_open_circ_or_launch is willing to return it
  146. * so we can actually use it. */
  147. circ->hs_circ_has_timed_out = 0;
  148. /* Append the hop to the cpath of this circuit */
  149. onion_append_to_cpath(&circ->cpath, hop);
  150. /* In legacy code, 'pending_final_cpath' points to the final hop we just
  151. * appended to the cpath. We set the original pointer to NULL so that we
  152. * don't double free it. */
  153. if (circ->build_state) {
  154. circ->build_state->pending_final_cpath = NULL;
  155. }
  156. /* Finally, mark circuit as ready to be used for client streams */
  157. if (!is_service_side) {
  158. circuit_try_attaching_streams(circ);
  159. }
  160. }
  161. /* For a given circuit and a service introduction point object, register the
  162. * intro circuit to the circuitmap. This supports legacy intro point. */
  163. static void
  164. register_intro_circ(const hs_service_intro_point_t *ip,
  165. origin_circuit_t *circ)
  166. {
  167. tor_assert(ip);
  168. tor_assert(circ);
  169. if (ip->base.is_only_legacy) {
  170. hs_circuitmap_register_intro_circ_v2_service_side(circ,
  171. ip->legacy_key_digest);
  172. } else {
  173. hs_circuitmap_register_intro_circ_v3_service_side(circ,
  174. &ip->auth_key_kp.pubkey);
  175. }
  176. }
  177. /* Return the number of opened introduction circuit for the given circuit that
  178. * is matching its identity key. */
  179. static unsigned int
  180. count_opened_desc_intro_point_circuits(const hs_service_t *service,
  181. const hs_service_descriptor_t *desc)
  182. {
  183. unsigned int count = 0;
  184. tor_assert(service);
  185. tor_assert(desc);
  186. DIGEST256MAP_FOREACH(desc->intro_points.map, key,
  187. const hs_service_intro_point_t *, ip) {
  188. const circuit_t *circ;
  189. const origin_circuit_t *ocirc = hs_circ_service_get_intro_circ(ip);
  190. if (ocirc == NULL) {
  191. continue;
  192. }
  193. circ = TO_CIRCUIT(ocirc);
  194. tor_assert(circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  195. circ->purpose == CIRCUIT_PURPOSE_S_INTRO);
  196. /* Having a circuit not for the requested service is really bad. */
  197. tor_assert(ed25519_pubkey_eq(&service->keys.identity_pk,
  198. &ocirc->hs_ident->identity_pk));
  199. /* Only count opened circuit and skip circuit that will be closed. */
  200. if (!circ->marked_for_close && circ->state == CIRCUIT_STATE_OPEN) {
  201. count++;
  202. }
  203. } DIGEST256MAP_FOREACH_END;
  204. return count;
  205. }
  206. /* From a given service, rendezvous cookie and handshake info, create a
  207. * rendezvous point circuit identifier. This can't fail. */
  208. STATIC hs_ident_circuit_t *
  209. create_rp_circuit_identifier(const hs_service_t *service,
  210. const uint8_t *rendezvous_cookie,
  211. const curve25519_public_key_t *server_pk,
  212. const hs_ntor_rend_cell_keys_t *keys)
  213. {
  214. hs_ident_circuit_t *ident;
  215. uint8_t handshake_info[CURVE25519_PUBKEY_LEN + DIGEST256_LEN];
  216. tor_assert(service);
  217. tor_assert(rendezvous_cookie);
  218. tor_assert(server_pk);
  219. tor_assert(keys);
  220. ident = hs_ident_circuit_new(&service->keys.identity_pk,
  221. HS_IDENT_CIRCUIT_RENDEZVOUS);
  222. /* Copy the RENDEZVOUS_COOKIE which is the unique identifier. */
  223. memcpy(ident->rendezvous_cookie, rendezvous_cookie,
  224. sizeof(ident->rendezvous_cookie));
  225. /* Build the HANDSHAKE_INFO which looks like this:
  226. * SERVER_PK [32 bytes]
  227. * AUTH_INPUT_MAC [32 bytes]
  228. */
  229. memcpy(handshake_info, server_pk->public_key, CURVE25519_PUBKEY_LEN);
  230. memcpy(handshake_info + CURVE25519_PUBKEY_LEN, keys->rend_cell_auth_mac,
  231. DIGEST256_LEN);
  232. tor_assert(sizeof(ident->rendezvous_handshake_info) ==
  233. sizeof(handshake_info));
  234. memcpy(ident->rendezvous_handshake_info, handshake_info,
  235. sizeof(ident->rendezvous_handshake_info));
  236. /* Finally copy the NTOR_KEY_SEED for e2e encryption on the circuit. */
  237. tor_assert(sizeof(ident->rendezvous_ntor_key_seed) ==
  238. sizeof(keys->ntor_key_seed));
  239. memcpy(ident->rendezvous_ntor_key_seed, keys->ntor_key_seed,
  240. sizeof(ident->rendezvous_ntor_key_seed));
  241. return ident;
  242. }
  243. /* From a given service and service intro point, create an introduction point
  244. * circuit identifier. This can't fail. */
  245. static hs_ident_circuit_t *
  246. create_intro_circuit_identifier(const hs_service_t *service,
  247. const hs_service_intro_point_t *ip)
  248. {
  249. hs_ident_circuit_t *ident;
  250. tor_assert(service);
  251. tor_assert(ip);
  252. ident = hs_ident_circuit_new(&service->keys.identity_pk,
  253. HS_IDENT_CIRCUIT_INTRO);
  254. ed25519_pubkey_copy(&ident->intro_auth_pk, &ip->auth_key_kp.pubkey);
  255. return ident;
  256. }
  257. /* For a given introduction point and an introduction circuit, send the
  258. * ESTABLISH_INTRO cell. The service object is used for logging. This can fail
  259. * and if so, the circuit is closed and the intro point object is flagged
  260. * that the circuit is not established anymore which is important for the
  261. * retry mechanism. */
  262. static void
  263. send_establish_intro(const hs_service_t *service,
  264. hs_service_intro_point_t *ip, origin_circuit_t *circ)
  265. {
  266. ssize_t cell_len;
  267. uint8_t payload[RELAY_PAYLOAD_SIZE];
  268. tor_assert(service);
  269. tor_assert(ip);
  270. tor_assert(circ);
  271. /* Encode establish intro cell. */
  272. cell_len = hs_cell_build_establish_intro(circ->cpath->prev->rend_circ_nonce,
  273. ip, payload);
  274. if (cell_len < 0) {
  275. log_warn(LD_REND, "Unable to encode ESTABLISH_INTRO cell for service %s "
  276. "on circuit %u. Closing circuit.",
  277. safe_str_client(service->onion_address),
  278. TO_CIRCUIT(circ)->n_circ_id);
  279. goto err;
  280. }
  281. /* Send the cell on the circuit. */
  282. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
  283. RELAY_COMMAND_ESTABLISH_INTRO,
  284. (char *) payload, cell_len,
  285. circ->cpath->prev) < 0) {
  286. log_info(LD_REND, "Unable to send ESTABLISH_INTRO cell for service %s "
  287. "on circuit %u.",
  288. safe_str_client(service->onion_address),
  289. TO_CIRCUIT(circ)->n_circ_id);
  290. /* On error, the circuit has been closed. */
  291. goto done;
  292. }
  293. /* Record the attempt to use this circuit. */
  294. pathbias_count_use_attempt(circ);
  295. goto done;
  296. err:
  297. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  298. done:
  299. memwipe(payload, 0, sizeof(payload));
  300. }
  301. /* Return a string constant describing the anonymity of service. */
  302. static const char *
  303. get_service_anonymity_string(const hs_service_t *service)
  304. {
  305. if (service->config.is_single_onion) {
  306. return "single onion";
  307. } else {
  308. return "hidden";
  309. }
  310. }
  311. /* For a given service, the ntor onion key and a rendezvous cookie, launch a
  312. * circuit to the rendezvous point specified by the link specifiers. On
  313. * success, a circuit identifier is attached to the circuit with the needed
  314. * data. This function will try to open a circuit for a maximum value of
  315. * MAX_REND_FAILURES then it will give up. */
  316. static void
  317. launch_rendezvous_point_circuit(const hs_service_t *service,
  318. const hs_service_intro_point_t *ip,
  319. const hs_cell_introduce2_data_t *data)
  320. {
  321. int circ_needs_uptime;
  322. time_t now = time(NULL);
  323. extend_info_t *info = NULL;
  324. origin_circuit_t *circ;
  325. tor_assert(service);
  326. tor_assert(ip);
  327. tor_assert(data);
  328. circ_needs_uptime = hs_service_requires_uptime_circ(service->config.ports);
  329. /* Get the extend info data structure for the chosen rendezvous point
  330. * specified by the given link specifiers. */
  331. info = hs_get_extend_info_from_lspecs(data->link_specifiers,
  332. &data->onion_pk,
  333. service->config.is_single_onion);
  334. if (info == NULL) {
  335. /* We are done here, we can't extend to the rendezvous point.
  336. * If you're running an IPv6-only v3 single onion service on 0.3.2 or with
  337. * 0.3.2 clients, and somehow disable the option check, it will fail here.
  338. */
  339. log_fn(LOG_PROTOCOL_WARN, LD_REND,
  340. "Not enough info to open a circuit to a rendezvous point for "
  341. "%s service %s.",
  342. get_service_anonymity_string(service),
  343. safe_str_client(service->onion_address));
  344. goto end;
  345. }
  346. for (int i = 0; i < MAX_REND_FAILURES; i++) {
  347. int circ_flags = CIRCLAUNCH_NEED_CAPACITY | CIRCLAUNCH_IS_INTERNAL;
  348. if (circ_needs_uptime) {
  349. circ_flags |= CIRCLAUNCH_NEED_UPTIME;
  350. }
  351. /* Firewall and policies are checked when getting the extend info. */
  352. if (service->config.is_single_onion) {
  353. circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
  354. }
  355. circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND, info,
  356. circ_flags);
  357. if (circ != NULL) {
  358. /* Stop retrying, we have a circuit! */
  359. break;
  360. }
  361. }
  362. if (circ == NULL) {
  363. log_warn(LD_REND, "Giving up on launching a rendezvous circuit to %s "
  364. "for %s service %s",
  365. safe_str_client(extend_info_describe(info)),
  366. get_service_anonymity_string(service),
  367. safe_str_client(service->onion_address));
  368. goto end;
  369. }
  370. log_info(LD_REND, "Rendezvous circuit launched to %s with cookie %s "
  371. "for %s service %s",
  372. safe_str_client(extend_info_describe(info)),
  373. safe_str_client(hex_str((const char *) data->rendezvous_cookie,
  374. REND_COOKIE_LEN)),
  375. get_service_anonymity_string(service),
  376. safe_str_client(service->onion_address));
  377. tor_assert(circ->build_state);
  378. /* Rendezvous circuit have a specific timeout for the time spent on trying
  379. * to connect to the rendezvous point. */
  380. circ->build_state->expiry_time = now + MAX_REND_TIMEOUT;
  381. /* Create circuit identifier and key material. */
  382. {
  383. hs_ntor_rend_cell_keys_t keys;
  384. curve25519_keypair_t ephemeral_kp;
  385. /* No need for extra strong, this is only for this circuit life time. This
  386. * key will be used for the RENDEZVOUS1 cell that will be sent on the
  387. * circuit once opened. */
  388. curve25519_keypair_generate(&ephemeral_kp, 0);
  389. if (hs_ntor_service_get_rendezvous1_keys(&ip->auth_key_kp.pubkey,
  390. &ip->enc_key_kp,
  391. &ephemeral_kp, &data->client_pk,
  392. &keys) < 0) {
  393. /* This should not really happened but just in case, don't make tor
  394. * freak out, close the circuit and move on. */
  395. log_info(LD_REND, "Unable to get RENDEZVOUS1 key material for "
  396. "service %s",
  397. safe_str_client(service->onion_address));
  398. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  399. goto end;
  400. }
  401. circ->hs_ident = create_rp_circuit_identifier(service,
  402. data->rendezvous_cookie,
  403. &ephemeral_kp.pubkey, &keys);
  404. memwipe(&ephemeral_kp, 0, sizeof(ephemeral_kp));
  405. memwipe(&keys, 0, sizeof(keys));
  406. tor_assert(circ->hs_ident);
  407. }
  408. end:
  409. extend_info_free(info);
  410. }
  411. /* Return true iff the given service rendezvous circuit circ is allowed for a
  412. * relaunch to the rendezvous point. */
  413. static int
  414. can_relaunch_service_rendezvous_point(const origin_circuit_t *circ)
  415. {
  416. tor_assert(circ);
  417. /* This is initialized when allocating an origin circuit. */
  418. tor_assert(circ->build_state);
  419. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  420. /* XXX: Retrying under certain condition. This is related to #22455. */
  421. /* Avoid to relaunch twice a circuit to the same rendezvous point at the
  422. * same time. */
  423. if (circ->hs_service_side_rend_circ_has_been_relaunched) {
  424. log_info(LD_REND, "Rendezvous circuit to %s has already been retried. "
  425. "Skipping retry.",
  426. safe_str_client(
  427. extend_info_describe(circ->build_state->chosen_exit)));
  428. goto disallow;
  429. }
  430. /* We check failure_count >= hs_get_service_max_rend_failures()-1 below, and
  431. * the -1 is because we increment the failure count for our current failure
  432. * *after* this clause. */
  433. int max_rend_failures = hs_get_service_max_rend_failures() - 1;
  434. /* A failure count that has reached maximum allowed or circuit that expired,
  435. * we skip relaunching. */
  436. if (circ->build_state->failure_count > max_rend_failures ||
  437. circ->build_state->expiry_time <= time(NULL)) {
  438. log_info(LD_REND, "Attempt to build a rendezvous circuit to %s has "
  439. "failed with %d attempts and expiry time %ld. "
  440. "Giving up building.",
  441. safe_str_client(
  442. extend_info_describe(circ->build_state->chosen_exit)),
  443. circ->build_state->failure_count,
  444. (long int) circ->build_state->expiry_time);
  445. goto disallow;
  446. }
  447. /* Allowed to relaunch. */
  448. return 1;
  449. disallow:
  450. return 0;
  451. }
  452. /* Retry the rendezvous point of circ by launching a new circuit to it. */
  453. static void
  454. retry_service_rendezvous_point(const origin_circuit_t *circ)
  455. {
  456. int flags = 0;
  457. origin_circuit_t *new_circ;
  458. cpath_build_state_t *bstate;
  459. tor_assert(circ);
  460. /* This is initialized when allocating an origin circuit. */
  461. tor_assert(circ->build_state);
  462. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  463. /* Ease our life. */
  464. bstate = circ->build_state;
  465. log_info(LD_REND, "Retrying rendezvous point circuit to %s",
  466. safe_str_client(extend_info_describe(bstate->chosen_exit)));
  467. /* Get the current build state flags for the next circuit. */
  468. flags |= (bstate->need_uptime) ? CIRCLAUNCH_NEED_UPTIME : 0;
  469. flags |= (bstate->need_capacity) ? CIRCLAUNCH_NEED_CAPACITY : 0;
  470. flags |= (bstate->is_internal) ? CIRCLAUNCH_IS_INTERNAL : 0;
  471. /* We do NOT add the onehop tunnel flag even though it might be a single
  472. * onion service. The reason is that if we failed once to connect to the RP
  473. * with a direct connection, we consider that chances are that we will fail
  474. * again so try a 3-hop circuit and hope for the best. Because the service
  475. * has no anonymity (single onion), this change of behavior won't affect
  476. * security directly. */
  477. new_circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND,
  478. bstate->chosen_exit, flags);
  479. if (new_circ == NULL) {
  480. log_warn(LD_REND, "Failed to launch rendezvous circuit to %s",
  481. safe_str_client(extend_info_describe(bstate->chosen_exit)));
  482. goto done;
  483. }
  484. /* Transfer build state information to the new circuit state in part to
  485. * catch any other failures. */
  486. new_circ->build_state->failure_count = bstate->failure_count+1;
  487. new_circ->build_state->expiry_time = bstate->expiry_time;
  488. new_circ->hs_ident = hs_ident_circuit_dup(circ->hs_ident);
  489. done:
  490. return;
  491. }
  492. /* Add all possible link specifiers in node to lspecs.
  493. * legacy ID is mandatory thus MUST be present in node. If the primary address
  494. * is not IPv4, log a BUG() warning, and return an empty smartlist.
  495. * Includes ed25519 id and IPv6 link specifiers if present in the node. */
  496. static void
  497. get_lspecs_from_node(const node_t *node, smartlist_t *lspecs)
  498. {
  499. link_specifier_t *ls;
  500. tor_addr_port_t ap;
  501. tor_assert(node);
  502. tor_assert(lspecs);
  503. /* Get the relay's IPv4 address. */
  504. node_get_prim_orport(node, &ap);
  505. /* We expect the node's primary address to be a valid IPv4 address.
  506. * This conforms to the protocol, which requires either an IPv4 or IPv6
  507. * address (or both). */
  508. if (BUG(!tor_addr_is_v4(&ap.addr)) ||
  509. BUG(!tor_addr_port_is_valid_ap(&ap, 0))) {
  510. return;
  511. }
  512. ls = link_specifier_new();
  513. link_specifier_set_ls_type(ls, LS_IPV4);
  514. link_specifier_set_un_ipv4_addr(ls, tor_addr_to_ipv4h(&ap.addr));
  515. link_specifier_set_un_ipv4_port(ls, ap.port);
  516. /* Four bytes IPv4 and two bytes port. */
  517. link_specifier_set_ls_len(ls, sizeof(ap.addr.addr.in_addr) +
  518. sizeof(ap.port));
  519. smartlist_add(lspecs, ls);
  520. /* Legacy ID is mandatory and will always be present in node. */
  521. ls = link_specifier_new();
  522. link_specifier_set_ls_type(ls, LS_LEGACY_ID);
  523. memcpy(link_specifier_getarray_un_legacy_id(ls), node->identity,
  524. link_specifier_getlen_un_legacy_id(ls));
  525. link_specifier_set_ls_len(ls, link_specifier_getlen_un_legacy_id(ls));
  526. smartlist_add(lspecs, ls);
  527. /* ed25519 ID is only included if the node has it. */
  528. if (!ed25519_public_key_is_zero(&node->ed25519_id)) {
  529. ls = link_specifier_new();
  530. link_specifier_set_ls_type(ls, LS_ED25519_ID);
  531. memcpy(link_specifier_getarray_un_ed25519_id(ls), &node->ed25519_id,
  532. link_specifier_getlen_un_ed25519_id(ls));
  533. link_specifier_set_ls_len(ls, link_specifier_getlen_un_ed25519_id(ls));
  534. smartlist_add(lspecs, ls);
  535. }
  536. /* Check for IPv6. If so, include it as well. */
  537. if (node_has_ipv6_orport(node)) {
  538. ls = link_specifier_new();
  539. node_get_pref_ipv6_orport(node, &ap);
  540. link_specifier_set_ls_type(ls, LS_IPV6);
  541. size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls);
  542. const uint8_t *in6_addr = tor_addr_to_in6_addr8(&ap.addr);
  543. uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls);
  544. memcpy(ipv6_array, in6_addr, addr_len);
  545. link_specifier_set_un_ipv6_port(ls, ap.port);
  546. /* Sixteen bytes IPv6 and two bytes port. */
  547. link_specifier_set_ls_len(ls, addr_len + sizeof(ap.port));
  548. smartlist_add(lspecs, ls);
  549. }
  550. }
  551. /* Using the given descriptor intro point ip, the node of the
  552. * rendezvous point rp_node and the service's subcredential, populate the
  553. * already allocated intro1_data object with the needed key material and link
  554. * specifiers.
  555. *
  556. * If rp_node has an invalid primary address, intro1_data->link_specifiers
  557. * will be an empty list. Otherwise, this function can't fail. The ip
  558. * MUST be a valid object containing the needed keys and authentication
  559. * method. */
  560. static void
  561. setup_introduce1_data(const hs_desc_intro_point_t *ip,
  562. const node_t *rp_node,
  563. const uint8_t *subcredential,
  564. hs_cell_introduce1_data_t *intro1_data)
  565. {
  566. smartlist_t *rp_lspecs;
  567. tor_assert(ip);
  568. tor_assert(rp_node);
  569. tor_assert(subcredential);
  570. tor_assert(intro1_data);
  571. /* Build the link specifiers from the extend information of the rendezvous
  572. * circuit that we've picked previously. */
  573. rp_lspecs = smartlist_new();
  574. get_lspecs_from_node(rp_node, rp_lspecs);
  575. /* Populate the introduce1 data object. */
  576. memset(intro1_data, 0, sizeof(hs_cell_introduce1_data_t));
  577. if (ip->legacy.key != NULL) {
  578. intro1_data->is_legacy = 1;
  579. intro1_data->legacy_key = ip->legacy.key;
  580. }
  581. intro1_data->auth_pk = &ip->auth_key_cert->signed_key;
  582. intro1_data->enc_pk = &ip->enc_key;
  583. intro1_data->subcredential = subcredential;
  584. intro1_data->onion_pk = node_get_curve25519_onion_key(rp_node);
  585. intro1_data->link_specifiers = rp_lspecs;
  586. }
  587. /* ========== */
  588. /* Public API */
  589. /* ========== */
  590. /* Return an introduction point circuit matching the given intro point object.
  591. * NULL is returned is no such circuit can be found. */
  592. origin_circuit_t *
  593. hs_circ_service_get_intro_circ(const hs_service_intro_point_t *ip)
  594. {
  595. tor_assert(ip);
  596. if (ip->base.is_only_legacy) {
  597. return hs_circuitmap_get_intro_circ_v2_service_side(ip->legacy_key_digest);
  598. } else {
  599. return hs_circuitmap_get_intro_circ_v3_service_side(
  600. &ip->auth_key_kp.pubkey);
  601. }
  602. }
  603. /* Called when we fail building a rendezvous circuit at some point other than
  604. * the last hop: launches a new circuit to the same rendezvous point. This
  605. * supports legacy service.
  606. *
  607. * We currently relaunch connections to rendezvous points if:
  608. * - A rendezvous circuit timed out before connecting to RP.
  609. * - The rendezvous circuit failed to connect to the RP.
  610. *
  611. * We avoid relaunching a connection to this rendezvous point if:
  612. * - We have already tried MAX_REND_FAILURES times to connect to this RP,
  613. * - We've been trying to connect to this RP for more than MAX_REND_TIMEOUT
  614. * seconds, or
  615. * - We've already retried this specific rendezvous circuit.
  616. */
  617. void
  618. hs_circ_retry_service_rendezvous_point(origin_circuit_t *circ)
  619. {
  620. tor_assert(circ);
  621. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
  622. /* Check if we are allowed to relaunch to the rendezvous point of circ. */
  623. if (!can_relaunch_service_rendezvous_point(circ)) {
  624. goto done;
  625. }
  626. /* Flag the circuit that we are relaunching, to avoid to relaunch twice a
  627. * circuit to the same rendezvous point at the same time. */
  628. circ->hs_service_side_rend_circ_has_been_relaunched = 1;
  629. /* Legacy services don't have a hidden service ident. */
  630. if (circ->hs_ident) {
  631. retry_service_rendezvous_point(circ);
  632. } else {
  633. rend_service_relaunch_rendezvous(circ);
  634. }
  635. done:
  636. return;
  637. }
  638. /* For a given service and a service intro point, launch a circuit to the
  639. * extend info ei. If the service is a single onion, a one-hop circuit will be
  640. * requested. Return 0 if the circuit was successfully launched and tagged
  641. * with the correct identifier. On error, a negative value is returned. */
  642. int
  643. hs_circ_launch_intro_point(hs_service_t *service,
  644. const hs_service_intro_point_t *ip,
  645. extend_info_t *ei)
  646. {
  647. /* Standard flags for introduction circuit. */
  648. int ret = -1, circ_flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  649. origin_circuit_t *circ;
  650. tor_assert(service);
  651. tor_assert(ip);
  652. tor_assert(ei);
  653. /* Update circuit flags in case of a single onion service that requires a
  654. * direct connection. */
  655. if (service->config.is_single_onion) {
  656. circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
  657. }
  658. log_info(LD_REND, "Launching a circuit to intro point %s for service %s.",
  659. safe_str_client(extend_info_describe(ei)),
  660. safe_str_client(service->onion_address));
  661. /* Note down the launch for the retry period. Even if the circuit fails to
  662. * be launched, we still want to respect the retry period to avoid stress on
  663. * the circuit subsystem. */
  664. service->state.num_intro_circ_launched++;
  665. circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  666. ei, circ_flags);
  667. if (circ == NULL) {
  668. goto end;
  669. }
  670. /* Setup the circuit identifier and attach it to it. */
  671. circ->hs_ident = create_intro_circuit_identifier(service, ip);
  672. tor_assert(circ->hs_ident);
  673. /* Register circuit in the global circuitmap. */
  674. register_intro_circ(ip, circ);
  675. /* Success. */
  676. ret = 0;
  677. end:
  678. return ret;
  679. }
  680. /* Called when a service introduction point circuit is done building. Given
  681. * the service and intro point object, this function will send the
  682. * ESTABLISH_INTRO cell on the circuit. Return 0 on success. Return 1 if the
  683. * circuit has been repurposed to General because we already have too many
  684. * opened. */
  685. int
  686. hs_circ_service_intro_has_opened(hs_service_t *service,
  687. hs_service_intro_point_t *ip,
  688. const hs_service_descriptor_t *desc,
  689. origin_circuit_t *circ)
  690. {
  691. int ret = 0;
  692. unsigned int num_intro_circ, num_needed_circ;
  693. tor_assert(service);
  694. tor_assert(ip);
  695. tor_assert(desc);
  696. tor_assert(circ);
  697. /* Cound opened circuits that have sent ESTABLISH_INTRO cells or are already
  698. * established introduction circuits */
  699. num_intro_circ = count_opened_desc_intro_point_circuits(service, desc);
  700. num_needed_circ = service->config.num_intro_points;
  701. if (num_intro_circ > num_needed_circ) {
  702. /* There are too many opened valid intro circuit for what the service
  703. * needs so repurpose this one. */
  704. /* XXX: Legacy code checks options->ExcludeNodes and if not NULL it just
  705. * closes the circuit. I have NO idea why it does that so it hasn't been
  706. * added here. I can only assume in case our ExcludeNodes list changes but
  707. * in that case, all circuit are flagged unusable (config.c). --dgoulet */
  708. log_info(LD_CIRC | LD_REND, "Introduction circuit just opened but we "
  709. "have enough for service %s. Repurposing "
  710. "it to general and leaving internal.",
  711. safe_str_client(service->onion_address));
  712. tor_assert(circ->build_state->is_internal);
  713. /* Remove it from the circuitmap. */
  714. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  715. /* Cleaning up the hidden service identifier and repurpose. */
  716. hs_ident_circuit_free(circ->hs_ident);
  717. circ->hs_ident = NULL;
  718. if (circuit_should_use_vanguards(TO_CIRCUIT(circ)->purpose))
  719. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_HS_VANGUARDS);
  720. else
  721. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_GENERAL);
  722. /* Inform that this circuit just opened for this new purpose. */
  723. circuit_has_opened(circ);
  724. /* This return value indicate to the caller that the IP object should be
  725. * removed from the service because it's corresponding circuit has just
  726. * been repurposed. */
  727. ret = 1;
  728. goto done;
  729. }
  730. log_info(LD_REND, "Introduction circuit %u established for service %s.",
  731. TO_CIRCUIT(circ)->n_circ_id,
  732. safe_str_client(service->onion_address));
  733. circuit_log_path(LOG_INFO, LD_REND, circ);
  734. /* Time to send an ESTABLISH_INTRO cell on this circuit. On error, this call
  735. * makes sure the circuit gets closed. */
  736. send_establish_intro(service, ip, circ);
  737. done:
  738. return ret;
  739. }
  740. /* Called when a service rendezvous point circuit is done building. Given the
  741. * service and the circuit, this function will send a RENDEZVOUS1 cell on the
  742. * circuit using the information in the circuit identifier. If the cell can't
  743. * be sent, the circuit is closed. */
  744. void
  745. hs_circ_service_rp_has_opened(const hs_service_t *service,
  746. origin_circuit_t *circ)
  747. {
  748. size_t payload_len;
  749. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  750. tor_assert(service);
  751. tor_assert(circ);
  752. tor_assert(circ->hs_ident);
  753. /* Some useful logging. */
  754. log_info(LD_REND, "Rendezvous circuit %u has opened with cookie %s "
  755. "for service %s",
  756. TO_CIRCUIT(circ)->n_circ_id,
  757. hex_str((const char *) circ->hs_ident->rendezvous_cookie,
  758. REND_COOKIE_LEN),
  759. safe_str_client(service->onion_address));
  760. circuit_log_path(LOG_INFO, LD_REND, circ);
  761. /* This can't fail. */
  762. payload_len = hs_cell_build_rendezvous1(
  763. circ->hs_ident->rendezvous_cookie,
  764. sizeof(circ->hs_ident->rendezvous_cookie),
  765. circ->hs_ident->rendezvous_handshake_info,
  766. sizeof(circ->hs_ident->rendezvous_handshake_info),
  767. payload);
  768. /* Pad the payload with random bytes so it matches the size of a legacy cell
  769. * which is normally always bigger. Also, the size of a legacy cell is
  770. * always smaller than the RELAY_PAYLOAD_SIZE so this is safe. */
  771. if (payload_len < HS_LEGACY_RENDEZVOUS_CELL_SIZE) {
  772. crypto_rand((char *) payload + payload_len,
  773. HS_LEGACY_RENDEZVOUS_CELL_SIZE - payload_len);
  774. payload_len = HS_LEGACY_RENDEZVOUS_CELL_SIZE;
  775. }
  776. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
  777. RELAY_COMMAND_RENDEZVOUS1,
  778. (const char *) payload, payload_len,
  779. circ->cpath->prev) < 0) {
  780. /* On error, circuit is closed. */
  781. log_warn(LD_REND, "Unable to send RENDEZVOUS1 cell on circuit %u "
  782. "for service %s",
  783. TO_CIRCUIT(circ)->n_circ_id,
  784. safe_str_client(service->onion_address));
  785. goto done;
  786. }
  787. /* Setup end-to-end rendezvous circuit between the client and us. */
  788. if (hs_circuit_setup_e2e_rend_circ(circ,
  789. circ->hs_ident->rendezvous_ntor_key_seed,
  790. sizeof(circ->hs_ident->rendezvous_ntor_key_seed),
  791. 1) < 0) {
  792. log_warn(LD_GENERAL, "Failed to setup circ");
  793. goto done;
  794. }
  795. done:
  796. memwipe(payload, 0, sizeof(payload));
  797. }
  798. /* Circ has been expecting an INTRO_ESTABLISHED cell that just arrived. Handle
  799. * the INTRO_ESTABLISHED cell payload of length payload_len arriving on the
  800. * given introduction circuit circ. The service is only used for logging
  801. * purposes. Return 0 on success else a negative value. */
  802. int
  803. hs_circ_handle_intro_established(const hs_service_t *service,
  804. const hs_service_intro_point_t *ip,
  805. origin_circuit_t *circ,
  806. const uint8_t *payload, size_t payload_len)
  807. {
  808. int ret = -1;
  809. tor_assert(service);
  810. tor_assert(ip);
  811. tor_assert(circ);
  812. tor_assert(payload);
  813. if (BUG(TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)) {
  814. goto done;
  815. }
  816. /* Try to parse the payload into a cell making sure we do actually have a
  817. * valid cell. For a legacy node, it's an empty payload so as long as we
  818. * have the cell, we are good. */
  819. if (!ip->base.is_only_legacy &&
  820. hs_cell_parse_intro_established(payload, payload_len) < 0) {
  821. log_warn(LD_REND, "Unable to parse the INTRO_ESTABLISHED cell on "
  822. "circuit %u for service %s",
  823. TO_CIRCUIT(circ)->n_circ_id,
  824. safe_str_client(service->onion_address));
  825. goto done;
  826. }
  827. /* Switch the purpose to a fully working intro point. */
  828. circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_S_INTRO);
  829. /* Getting a valid INTRODUCE_ESTABLISHED means we've successfully used the
  830. * circuit so update our pathbias subsystem. */
  831. pathbias_mark_use_success(circ);
  832. /* Success. */
  833. ret = 0;
  834. done:
  835. return ret;
  836. }
  837. /* We just received an INTRODUCE2 cell on the established introduction circuit
  838. * circ. Handle the INTRODUCE2 payload of size payload_len for the given
  839. * circuit and service. This cell is associated with the intro point object ip
  840. * and the subcredential. Return 0 on success else a negative value. */
  841. int
  842. hs_circ_handle_introduce2(const hs_service_t *service,
  843. const origin_circuit_t *circ,
  844. hs_service_intro_point_t *ip,
  845. const uint8_t *subcredential,
  846. const uint8_t *payload, size_t payload_len)
  847. {
  848. int ret = -1;
  849. time_t elapsed;
  850. hs_cell_introduce2_data_t data;
  851. tor_assert(service);
  852. tor_assert(circ);
  853. tor_assert(ip);
  854. tor_assert(subcredential);
  855. tor_assert(payload);
  856. /* Populate the data structure with everything we need for the cell to be
  857. * parsed, decrypted and key material computed correctly. */
  858. data.auth_pk = &ip->auth_key_kp.pubkey;
  859. data.enc_kp = &ip->enc_key_kp;
  860. data.subcredential = subcredential;
  861. data.payload = payload;
  862. data.payload_len = payload_len;
  863. data.link_specifiers = smartlist_new();
  864. data.replay_cache = ip->replay_cache;
  865. if (hs_cell_parse_introduce2(&data, circ, service) < 0) {
  866. goto done;
  867. }
  868. /* Check whether we've seen this REND_COOKIE before to detect repeats. */
  869. if (replaycache_add_test_and_elapsed(
  870. service->state.replay_cache_rend_cookie,
  871. data.rendezvous_cookie, sizeof(data.rendezvous_cookie),
  872. &elapsed)) {
  873. /* A Tor client will send a new INTRODUCE1 cell with the same REND_COOKIE
  874. * as its previous one if its intro circ times out while in state
  875. * CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT. If we received the first
  876. * INTRODUCE1 cell (the intro-point relay converts it into an INTRODUCE2
  877. * cell), we are already trying to connect to that rend point (and may
  878. * have already succeeded); drop this cell. */
  879. log_info(LD_REND, "We received an INTRODUCE2 cell with same REND_COOKIE "
  880. "field %ld seconds ago. Dropping cell.",
  881. (long int) elapsed);
  882. goto done;
  883. }
  884. /* At this point, we just confirmed that the full INTRODUCE2 cell is valid
  885. * so increment our counter that we've seen one on this intro point. */
  886. ip->introduce2_count++;
  887. /* Launch rendezvous circuit with the onion key and rend cookie. */
  888. launch_rendezvous_point_circuit(service, ip, &data);
  889. /* Success. */
  890. ret = 0;
  891. done:
  892. SMARTLIST_FOREACH(data.link_specifiers, link_specifier_t *, lspec,
  893. link_specifier_free(lspec));
  894. smartlist_free(data.link_specifiers);
  895. memwipe(&data, 0, sizeof(data));
  896. return ret;
  897. }
  898. /* Circuit <b>circ</b> just finished the rend ntor key exchange. Use the key
  899. * exchange output material at <b>ntor_key_seed</b> and setup <b>circ</b> to
  900. * serve as a rendezvous end-to-end circuit between the client and the
  901. * service. If <b>is_service_side</b> is set, then we are the hidden service
  902. * and the other side is the client.
  903. *
  904. * Return 0 if the operation went well; in case of error return -1. */
  905. int
  906. hs_circuit_setup_e2e_rend_circ(origin_circuit_t *circ,
  907. const uint8_t *ntor_key_seed, size_t seed_len,
  908. int is_service_side)
  909. {
  910. if (BUG(!circuit_purpose_is_correct_for_rend(TO_CIRCUIT(circ)->purpose,
  911. is_service_side))) {
  912. return -1;
  913. }
  914. crypt_path_t *hop = create_rend_cpath(ntor_key_seed, seed_len,
  915. is_service_side);
  916. if (!hop) {
  917. log_warn(LD_REND, "Couldn't get v3 %s cpath!",
  918. is_service_side ? "service-side" : "client-side");
  919. return -1;
  920. }
  921. finalize_rend_circuit(circ, hop, is_service_side);
  922. return 0;
  923. }
  924. /* We are a v2 legacy HS client and we just received a RENDEZVOUS1 cell
  925. * <b>rend_cell_body</b> on <b>circ</b>. Finish up the DH key exchange and then
  926. * extend the crypt path of <b>circ</b> so that the hidden service is on the
  927. * other side. */
  928. int
  929. hs_circuit_setup_e2e_rend_circ_legacy_client(origin_circuit_t *circ,
  930. const uint8_t *rend_cell_body)
  931. {
  932. if (BUG(!circuit_purpose_is_correct_for_rend(
  933. TO_CIRCUIT(circ)->purpose, 0))) {
  934. return -1;
  935. }
  936. crypt_path_t *hop = create_rend_cpath_legacy(circ, rend_cell_body);
  937. if (!hop) {
  938. log_warn(LD_GENERAL, "Couldn't get v2 cpath.");
  939. return -1;
  940. }
  941. finalize_rend_circuit(circ, hop, 0);
  942. return 0;
  943. }
  944. /* Given the introduction circuit intro_circ, the rendezvous circuit
  945. * rend_circ, a descriptor intro point object ip and the service's
  946. * subcredential, send an INTRODUCE1 cell on intro_circ.
  947. *
  948. * This will also setup the circuit identifier on rend_circ containing the key
  949. * material for the handshake and e2e encryption. Return 0 on success else
  950. * negative value. Because relay_send_command_from_edge() closes the circuit
  951. * on error, it is possible that intro_circ is closed on error. */
  952. int
  953. hs_circ_send_introduce1(origin_circuit_t *intro_circ,
  954. origin_circuit_t *rend_circ,
  955. const hs_desc_intro_point_t *ip,
  956. const uint8_t *subcredential)
  957. {
  958. int ret = -1;
  959. ssize_t payload_len;
  960. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  961. hs_cell_introduce1_data_t intro1_data;
  962. tor_assert(intro_circ);
  963. tor_assert(rend_circ);
  964. tor_assert(ip);
  965. tor_assert(subcredential);
  966. /* It is undefined behavior in hs_cell_introduce1_data_clear() if intro1_data
  967. * has been declared on the stack but not initialized. Here, we set it to 0.
  968. */
  969. memset(&intro1_data, 0, sizeof(hs_cell_introduce1_data_t));
  970. /* This takes various objects in order to populate the introduce1 data
  971. * object which is used to build the content of the cell. */
  972. const node_t *exit_node = build_state_get_exit_node(rend_circ->build_state);
  973. if (exit_node == NULL) {
  974. log_info(LD_REND, "Unable to get rendezvous point for circuit %u. "
  975. "Failing.", TO_CIRCUIT(intro_circ)->n_circ_id);
  976. goto done;
  977. }
  978. setup_introduce1_data(ip, exit_node, subcredential, &intro1_data);
  979. /* If we didn't get any link specifiers, it's because our node was
  980. * bad. */
  981. if (BUG(!intro1_data.link_specifiers) ||
  982. !smartlist_len(intro1_data.link_specifiers)) {
  983. log_warn(LD_REND, "Unable to get link specifiers for INTRODUCE1 cell on "
  984. "circuit %u.", TO_CIRCUIT(intro_circ)->n_circ_id);
  985. goto done;
  986. }
  987. /* Final step before we encode a cell, we setup the circuit identifier which
  988. * will generate both the rendezvous cookie and client keypair for this
  989. * connection. Those are put in the ident. */
  990. intro1_data.rendezvous_cookie = rend_circ->hs_ident->rendezvous_cookie;
  991. intro1_data.client_kp = &rend_circ->hs_ident->rendezvous_client_kp;
  992. memcpy(intro_circ->hs_ident->rendezvous_cookie,
  993. rend_circ->hs_ident->rendezvous_cookie,
  994. sizeof(intro_circ->hs_ident->rendezvous_cookie));
  995. /* From the introduce1 data object, this will encode the INTRODUCE1 cell
  996. * into payload which is then ready to be sent as is. */
  997. payload_len = hs_cell_build_introduce1(&intro1_data, payload);
  998. if (BUG(payload_len < 0)) {
  999. goto done;
  1000. }
  1001. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(intro_circ),
  1002. RELAY_COMMAND_INTRODUCE1,
  1003. (const char *) payload, payload_len,
  1004. intro_circ->cpath->prev) < 0) {
  1005. /* On error, circuit is closed. */
  1006. log_warn(LD_REND, "Unable to send INTRODUCE1 cell on circuit %u.",
  1007. TO_CIRCUIT(intro_circ)->n_circ_id);
  1008. goto done;
  1009. }
  1010. /* Success. */
  1011. ret = 0;
  1012. goto done;
  1013. done:
  1014. hs_cell_introduce1_data_clear(&intro1_data);
  1015. memwipe(payload, 0, sizeof(payload));
  1016. return ret;
  1017. }
  1018. /* Send an ESTABLISH_RENDEZVOUS cell along the rendezvous circuit circ. On
  1019. * success, 0 is returned else -1 and the circuit is marked for close. */
  1020. int
  1021. hs_circ_send_establish_rendezvous(origin_circuit_t *circ)
  1022. {
  1023. ssize_t cell_len = 0;
  1024. uint8_t cell[RELAY_PAYLOAD_SIZE] = {0};
  1025. tor_assert(circ);
  1026. tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  1027. log_info(LD_REND, "Send an ESTABLISH_RENDEZVOUS cell on circuit %u",
  1028. TO_CIRCUIT(circ)->n_circ_id);
  1029. /* Set timestamp_dirty, because circuit_expire_building expects it,
  1030. * and the rend cookie also means we've used the circ. */
  1031. TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
  1032. /* We've attempted to use this circuit. Probe it if we fail */
  1033. pathbias_count_use_attempt(circ);
  1034. /* Generate the RENDEZVOUS_COOKIE and place it in the identifier so we can
  1035. * complete the handshake when receiving the acknowledgement. */
  1036. crypto_rand((char *) circ->hs_ident->rendezvous_cookie, HS_REND_COOKIE_LEN);
  1037. /* Generate the client keypair. No need to be extra strong, not long term */
  1038. curve25519_keypair_generate(&circ->hs_ident->rendezvous_client_kp, 0);
  1039. cell_len =
  1040. hs_cell_build_establish_rendezvous(circ->hs_ident->rendezvous_cookie,
  1041. cell);
  1042. if (BUG(cell_len < 0)) {
  1043. goto err;
  1044. }
  1045. if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
  1046. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  1047. (const char *) cell, cell_len,
  1048. circ->cpath->prev) < 0) {
  1049. /* Circuit has been marked for close */
  1050. log_warn(LD_REND, "Unable to send ESTABLISH_RENDEZVOUS cell on "
  1051. "circuit %u", TO_CIRCUIT(circ)->n_circ_id);
  1052. memwipe(cell, 0, cell_len);
  1053. goto err;
  1054. }
  1055. memwipe(cell, 0, cell_len);
  1056. return 0;
  1057. err:
  1058. return -1;
  1059. }
  1060. /* We are about to close or free this <b>circ</b>. Clean it up from any
  1061. * related HS data structures. This function can be called multiple times
  1062. * safely for the same circuit. */
  1063. void
  1064. hs_circ_cleanup(circuit_t *circ)
  1065. {
  1066. tor_assert(circ);
  1067. /* If it's a service-side intro circ, notify the HS subsystem for the intro
  1068. * point circuit closing so it can be dealt with cleanly. */
  1069. if (circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  1070. circ->purpose == CIRCUIT_PURPOSE_S_INTRO) {
  1071. hs_service_intro_circ_has_closed(TO_ORIGIN_CIRCUIT(circ));
  1072. }
  1073. /* Clear HS circuitmap token for this circ (if any). Very important to be
  1074. * done after the HS subsystem has been notified of the close else the
  1075. * circuit will not be found.
  1076. *
  1077. * We do this at the close if possible because from that point on, the
  1078. * circuit is good as dead. We can't rely on removing it in the circuit
  1079. * free() function because we open a race window between the close and free
  1080. * where we can't register a new circuit for the same intro point. */
  1081. if (circ->hs_token) {
  1082. hs_circuitmap_remove_circuit(circ);
  1083. }
  1084. }