hs_circuit.c 45 KB

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