hs_circuit.c 45 KB

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