hs_circuit.c 41 KB

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