hs_circuit.c 44 KB

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