hs_circuit.c 45 KB

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