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