hs_circuit.c 45 KB

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