Tor  0.4.8.0-alpha-dev
hs_circuit.c
Go to the documentation of this file.
1 /* Copyright (c) 2017-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 /**
5  * \file hs_circuit.c
6  **/
7 
8 #define HS_CIRCUIT_PRIVATE
9 
10 #include "core/or/or.h"
11 #include "app/config/config.h"
12 #include "core/crypto/hs_ntor.h"
13 #include "core/or/circuitbuild.h"
14 #include "core/or/circuitlist.h"
15 #include "core/or/circuituse.h"
16 #include "core/or/policies.h"
17 #include "core/or/relay.h"
18 #include "core/or/crypt_path.h"
19 #include "core/or/extendinfo.h"
22 #include "feature/client/circpathbias.h"
23 #include "feature/hs/hs_cell.h"
24 #include "feature/hs/hs_circuit.h"
25 #include "feature/hs/hs_ob.h"
27 #include "feature/hs/hs_client.h"
28 #include "feature/hs/hs_ident.h"
29 #include "feature/hs/hs_metrics.h"
30 #include "feature/hs/hs_service.h"
33 #include "feature/stats/rephist.h"
37 
38 /* Trunnel. */
39 #include "trunnel/ed25519_cert.h"
40 #include "trunnel/hs/cell_establish_intro.h"
41 
44 #include "core/or/crypt_path_st.h"
47 
48 /** A circuit is about to become an e2e rendezvous circuit. Check
49  * <b>circ_purpose</b> and ensure that it's properly set. Return true iff
50  * circuit purpose is properly set, otherwise return false. */
51 static int
52 circuit_purpose_is_correct_for_rend(unsigned int circ_purpose,
53  int is_service_side)
54 {
55  if (is_service_side) {
56  if (circ_purpose != CIRCUIT_PURPOSE_S_CONNECT_REND) {
57  log_warn(LD_BUG,
58  "HS e2e circuit setup with wrong purpose (%d)", circ_purpose);
59  return 0;
60  }
61  }
62 
63  if (!is_service_side) {
64  if (circ_purpose != CIRCUIT_PURPOSE_C_REND_READY &&
66  log_warn(LD_BUG,
67  "Client e2e circuit setup with wrong purpose (%d)", circ_purpose);
68  return 0;
69  }
70  }
71 
72  return 1;
73 }
74 
75 /** Create and return a crypt path for the final hop of a v3 prop224 rendezvous
76  * circuit. Initialize the crypt path crypto using the output material from the
77  * ntor key exchange at <b>ntor_key_seed</b>.
78  *
79  * If <b>is_service_side</b> is set, we are the hidden service and the final
80  * hop of the rendezvous circuit is the client on the other side. */
81 static crypt_path_t *
82 create_rend_cpath(const uint8_t *ntor_key_seed, size_t seed_len,
83  int is_service_side)
84 {
85  uint8_t keys[HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN];
86  crypt_path_t *cpath = NULL;
87 
88  /* Do the key expansion */
89  if (hs_ntor_circuit_key_expansion(ntor_key_seed, seed_len,
90  keys, sizeof(keys)) < 0) {
91  goto err;
92  }
93 
94  /* Setup the cpath */
95  cpath = tor_malloc_zero(sizeof(crypt_path_t));
96  cpath->magic = CRYPT_PATH_MAGIC;
97 
98  if (cpath_init_circuit_crypto(cpath, (char*)keys, sizeof(keys),
99  is_service_side, 1) < 0) {
100  tor_free(cpath);
101  goto err;
102  }
103 
104  err:
105  memwipe(keys, 0, sizeof(keys));
106  return cpath;
107 }
108 
109 /** Append the final <b>hop</b> to the cpath of the rend <b>circ</b>, and mark
110  * <b>circ</b> ready for use to transfer HS relay cells. */
111 static void
113  int is_service_side)
114 {
115  tor_assert(circ);
116  tor_assert(hop);
117 
118  /* Notify the circuit state machine that we are splicing this circuit */
119  int new_circ_purpose = is_service_side ?
121  circuit_change_purpose(TO_CIRCUIT(circ), new_circ_purpose);
122 
123  /* All is well. Extend the circuit. */
124  hop->state = CPATH_STATE_OPEN;
125  /* Set the windows to default. */
128 
129  /* If congestion control, transfer ccontrol onto the cpath. */
130  if (TO_CIRCUIT(circ)->ccontrol) {
131  hop->ccontrol = TO_CIRCUIT(circ)->ccontrol;
132  TO_CIRCUIT(circ)->ccontrol = NULL;
133  }
134 
135  /* Append the hop to the cpath of this circuit */
136  cpath_extend_linked_list(&circ->cpath, hop);
137 
138  /* Finally, mark circuit as ready to be used for client streams */
139  if (!is_service_side) {
141  }
142 }
143 
144 /** For a given circuit and a service introduction point object, register the
145  * intro circuit to the circuitmap. */
146 static void
148  origin_circuit_t *circ)
149 {
150  tor_assert(ip);
151  tor_assert(circ);
152 
154  &ip->auth_key_kp.pubkey);
155 }
156 
157 /** Return the number of opened introduction circuit for the given circuit that
158  * is matching its identity key. */
159 static unsigned int
161  const hs_service_descriptor_t *desc)
162 {
163  unsigned int count = 0;
164 
165  tor_assert(service);
166  tor_assert(desc);
167 
168  DIGEST256MAP_FOREACH(desc->intro_points.map, key,
169  const hs_service_intro_point_t *, ip) {
170  const circuit_t *circ;
172  if (ocirc == NULL) {
173  continue;
174  }
175  circ = TO_CIRCUIT(ocirc);
178  /* Having a circuit not for the requested service is really bad. */
180  &ocirc->hs_ident->identity_pk));
181  /* Only count opened circuit and skip circuit that will be closed. */
182  if (!circ->marked_for_close && circ->state == CIRCUIT_STATE_OPEN) {
183  count++;
184  }
185  } DIGEST256MAP_FOREACH_END;
186  return count;
187 }
188 
189 /** From a given service, rendezvous cookie and handshake info, create a
190  * rendezvous point circuit identifier. This can't fail. */
193  const uint8_t *rendezvous_cookie,
194  const curve25519_public_key_t *server_pk,
195  const hs_ntor_rend_cell_keys_t *keys)
196 {
197  hs_ident_circuit_t *ident;
198  uint8_t handshake_info[CURVE25519_PUBKEY_LEN + DIGEST256_LEN];
199 
200  tor_assert(service);
201  tor_assert(rendezvous_cookie);
202  tor_assert(server_pk);
203  tor_assert(keys);
204 
205  ident = hs_ident_circuit_new(&service->keys.identity_pk);
206  /* Copy the RENDEZVOUS_COOKIE which is the unique identifier. */
207  memcpy(ident->rendezvous_cookie, rendezvous_cookie,
208  sizeof(ident->rendezvous_cookie));
209  /* Build the HANDSHAKE_INFO which looks like this:
210  * SERVER_PK [32 bytes]
211  * AUTH_INPUT_MAC [32 bytes]
212  */
213  memcpy(handshake_info, server_pk->public_key, CURVE25519_PUBKEY_LEN);
214  memcpy(handshake_info + CURVE25519_PUBKEY_LEN, keys->rend_cell_auth_mac,
215  DIGEST256_LEN);
216  tor_assert(sizeof(ident->rendezvous_handshake_info) ==
217  sizeof(handshake_info));
218  memcpy(ident->rendezvous_handshake_info, handshake_info,
219  sizeof(ident->rendezvous_handshake_info));
220  /* Finally copy the NTOR_KEY_SEED for e2e encryption on the circuit. */
221  tor_assert(sizeof(ident->rendezvous_ntor_key_seed) ==
222  sizeof(keys->ntor_key_seed));
223  memcpy(ident->rendezvous_ntor_key_seed, keys->ntor_key_seed,
224  sizeof(ident->rendezvous_ntor_key_seed));
225  return ident;
226 }
227 
228 /** From a given service and service intro point, create an introduction point
229  * circuit identifier. This can't fail. */
230 static hs_ident_circuit_t *
232  const hs_service_intro_point_t *ip)
233 {
234  hs_ident_circuit_t *ident;
235 
236  tor_assert(service);
237  tor_assert(ip);
238 
239  ident = hs_ident_circuit_new(&service->keys.identity_pk);
240  ed25519_pubkey_copy(&ident->intro_auth_pk, &ip->auth_key_kp.pubkey);
241 
242  return ident;
243 }
244 
245 /** For a given introduction point and an introduction circuit, send the
246  * ESTABLISH_INTRO cell. The service object is used for logging. This can fail
247  * and if so, the circuit is closed and the intro point object is flagged
248  * that the circuit is not established anymore which is important for the
249  * retry mechanism. */
250 static void
253 {
254  ssize_t cell_len;
255  uint8_t payload[RELAY_PAYLOAD_SIZE];
256 
257  tor_assert(service);
258  tor_assert(ip);
259  tor_assert(circ);
260 
261  /* Encode establish intro cell. */
263  &service->config, ip, payload);
264  if (cell_len < 0) {
265  log_warn(LD_REND, "Unable to encode ESTABLISH_INTRO cell for service %s "
266  "on circuit %u. Closing circuit.",
267  safe_str_client(service->onion_address),
268  TO_CIRCUIT(circ)->n_circ_id);
269  goto err;
270  }
271 
272  /* Send the cell on the circuit. */
273  if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
274  RELAY_COMMAND_ESTABLISH_INTRO,
275  (char *) payload, cell_len,
276  circ->cpath->prev) < 0) {
277  log_info(LD_REND, "Unable to send ESTABLISH_INTRO cell for service %s "
278  "on circuit %u.",
279  safe_str_client(service->onion_address),
280  TO_CIRCUIT(circ)->n_circ_id);
281  /* On error, the circuit has been closed. */
282  goto done;
283  }
284 
285  /* Record the attempt to use this circuit. */
287  goto done;
288 
289  err:
290  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
291  done:
292  memwipe(payload, 0, sizeof(payload));
293 }
294 
295 /** Return a string constant describing the anonymity of service. */
296 static const char *
298 {
299  if (service->config.is_single_onion) {
300  return "single onion";
301  } else {
302  return "hidden";
303  }
304 }
305 
306 /** For a given service, the ntor onion key and a rendezvous cookie, launch a
307  * circuit to the rendezvous point specified by the link specifiers. On
308  * success, a circuit identifier is attached to the circuit with the needed
309  * data. This function will try to open a circuit for a maximum value of
310  * MAX_REND_FAILURES then it will give up. */
311 MOCK_IMPL(STATIC void,
313  const hs_service_intro_point_t *ip,
315 {
316  int circ_needs_uptime;
317  time_t now = time(NULL);
318  extend_info_t *info = NULL;
319  origin_circuit_t *circ;
320 
321  tor_assert(service);
322  tor_assert(ip);
323  tor_assert(data);
324 
325  circ_needs_uptime = hs_service_requires_uptime_circ(service->config.ports);
326 
327  /* Get the extend info data structure for the chosen rendezvous point
328  * specified by the given link specifiers. */
330  &data->onion_pk,
331  service->config.is_single_onion);
332  if (info == NULL) {
333  /* We are done here, we can't extend to the rendezvous point. */
334  log_fn(LOG_PROTOCOL_WARN, LD_REND,
335  "Not enough info to open a circuit to a rendezvous point for "
336  "%s service %s.",
338  safe_str_client(service->onion_address));
339  goto end;
340  }
341 
342  for (int i = 0; i < MAX_REND_FAILURES; i++) {
344  if (circ_needs_uptime) {
345  circ_flags |= CIRCLAUNCH_NEED_UPTIME;
346  }
347  /* Firewall and policies are checked when getting the extend info.
348  *
349  * We only use a one-hop path on the first attempt. If the first attempt
350  * fails, we use a 3-hop path for reachability / reliability.
351  * See the comment in retry_service_rendezvous_point() for details. */
352  if (service->config.is_single_onion && i == 0) {
353  circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
354  }
355 
357  circ_flags);
358  if (circ != NULL) {
359  /* Stop retrying, we have a circuit! */
360  break;
361  }
362  }
363  if (circ == NULL) {
364  log_warn(LD_REND, "Giving up on launching a rendezvous circuit to %s "
365  "for %s service %s",
366  safe_str_client(extend_info_describe(info)),
368  safe_str_client(service->onion_address));
369  goto end;
370  }
371  /* Update metrics with this new rendezvous circuit launched. */
373 
374  log_info(LD_REND, "Rendezvous circuit launched to %s with cookie %s "
375  "for %s service %s",
376  safe_str_client(extend_info_describe(info)),
377  safe_str_client(hex_str((const char *) data->rendezvous_cookie,
378  REND_COOKIE_LEN)),
380  safe_str_client(service->onion_address));
381  tor_assert(circ->build_state);
382  /* Rendezvous circuit have a specific timeout for the time spent on trying
383  * to connect to the rendezvous point. */
385 
386  /* Create circuit identifier and key material. */
387  {
389  curve25519_keypair_t ephemeral_kp;
390  /* No need for extra strong, this is only for this circuit life time. This
391  * key will be used for the RENDEZVOUS1 cell that will be sent on the
392  * circuit once opened. */
393  curve25519_keypair_generate(&ephemeral_kp, 0);
394  if (hs_ntor_service_get_rendezvous1_keys(&ip->auth_key_kp.pubkey,
395  &ip->enc_key_kp,
396  &ephemeral_kp, &data->client_pk,
397  &keys) < 0) {
398  /* This should not really happened but just in case, don't make tor
399  * freak out, close the circuit and move on. */
400  log_info(LD_REND, "Unable to get RENDEZVOUS1 key material for "
401  "service %s",
402  safe_str_client(service->onion_address));
403  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
404  goto end;
405  }
406  circ->hs_ident = create_rp_circuit_identifier(service,
407  data->rendezvous_cookie,
408  &ephemeral_kp.pubkey, &keys);
409  memwipe(&ephemeral_kp, 0, sizeof(ephemeral_kp));
410  memwipe(&keys, 0, sizeof(keys));
411  tor_assert(circ->hs_ident);
412  }
413 
414  /* Setup congestion control if asked by the client from the INTRO cell. */
415  if (data->cc_enabled) {
417  service->config.is_single_onion);
418  }
419 
420  end:
421  extend_info_free(info);
422 }
423 
424 /** Return true iff the given service rendezvous circuit circ is allowed for a
425  * relaunch to the rendezvous point. */
426 static int
428 {
429  tor_assert(circ);
430  /* This is initialized when allocating an origin circuit. */
431  tor_assert(circ->build_state);
433 
434  /* XXX: Retrying under certain condition. This is related to #22455. */
435 
436  /* We check failure_count >= hs_get_service_max_rend_failures()-1 below, and
437  * the -1 is because we increment the failure count for our current failure
438  * *after* this clause. */
439  int max_rend_failures = hs_get_service_max_rend_failures() - 1;
440 
441  /* A failure count that has reached maximum allowed or circuit that expired,
442  * we skip relaunching. */
443  if (circ->build_state->failure_count > max_rend_failures ||
444  circ->build_state->expiry_time <= time(NULL)) {
445  log_info(LD_REND, "Attempt to build a rendezvous circuit to %s has "
446  "failed with %d attempts and expiry time %ld. "
447  "Giving up building.",
448  safe_str_client(
450  circ->build_state->failure_count,
451  (long int) circ->build_state->expiry_time);
452  goto disallow;
453  }
454 
455  /* Allowed to relaunch. */
456  return 1;
457  disallow:
458  return 0;
459 }
460 
461 /** Retry the rendezvous point of circ by launching a new circuit to it. */
462 static void
464 {
465  int flags = 0;
466  origin_circuit_t *new_circ;
467  cpath_build_state_t *bstate;
468 
469  tor_assert(circ);
470  /* This is initialized when allocating an origin circuit. */
471  tor_assert(circ->build_state);
473 
474  /* Ease our life. */
475  bstate = circ->build_state;
476 
477  log_info(LD_REND, "Retrying rendezvous point circuit to %s",
478  safe_str_client(extend_info_describe(bstate->chosen_exit)));
479 
480  /* Get the current build state flags for the next circuit. */
481  flags |= (bstate->need_uptime) ? CIRCLAUNCH_NEED_UPTIME : 0;
482  flags |= (bstate->need_capacity) ? CIRCLAUNCH_NEED_CAPACITY : 0;
483  flags |= (bstate->is_internal) ? CIRCLAUNCH_IS_INTERNAL : 0;
484 
485  /* We do NOT add the onehop tunnel flag even though it might be a single
486  * onion service. The reason is that if we failed once to connect to the RP
487  * with a direct connection, we consider that chances are that we will fail
488  * again so try a 3-hop circuit and hope for the best. Because the service
489  * has no anonymity (single onion), this change of behavior won't affect
490  * security directly. */
491 
493  bstate->chosen_exit, flags);
494  if (new_circ == NULL) {
495  log_warn(LD_REND, "Failed to launch rendezvous circuit to %s",
496  safe_str_client(extend_info_describe(bstate->chosen_exit)));
497 
499  HS_METRICS_ERR_RDV_RETRY);
500 
501  goto done;
502  }
503 
504  /* Transfer build state information to the new circuit state in part to
505  * catch any other failures. */
506  new_circ->build_state->failure_count = bstate->failure_count+1;
507  new_circ->build_state->expiry_time = bstate->expiry_time;
508  new_circ->hs_ident = hs_ident_circuit_dup(circ->hs_ident);
509 
510  /* Setup congestion control if asked by the client from the INTRO cell. */
511  if (TO_CIRCUIT(circ)->ccontrol) {
512  /* As per above, in this case, we are a full 3 hop rend, even if we're a
513  * single-onion service. */
515  TO_CIRCUIT(circ)->ccontrol->sendme_inc,
516  false);
517  }
518 
519  done:
520  return;
521 }
522 
523 /** Using the given descriptor intro point ip, the node of the
524  * rendezvous point rp_node and the service's subcredential, populate the
525  * already allocated intro1_data object with the needed key material and link
526  * specifiers.
527  *
528  * Return 0 on success or a negative value if we couldn't properly filled the
529  * introduce1 data from the RP node. In other word, it means the RP node is
530  * unusable to use in the introduction. */
531 static int
533  const node_t *rp_node,
534  const hs_subcredential_t *subcredential,
535  hs_cell_introduce1_data_t *intro1_data)
536 {
537  int ret = -1;
538  smartlist_t *rp_lspecs;
539 
540  tor_assert(ip);
541  tor_assert(rp_node);
542  tor_assert(subcredential);
543  tor_assert(intro1_data);
544 
545  /* Build the link specifiers from the node at the end of the rendezvous
546  * circuit that we opened for this introduction. */
547  rp_lspecs = node_get_link_specifier_smartlist(rp_node, 0);
548  if (smartlist_len(rp_lspecs) == 0) {
549  /* We can't rendezvous without link specifiers. */
550  smartlist_free(rp_lspecs);
551  goto end;
552  }
553 
554  /* Populate the introduce1 data object. */
555  memset(intro1_data, 0, sizeof(hs_cell_introduce1_data_t));
556  intro1_data->auth_pk = &ip->auth_key_cert->signed_key;
557  intro1_data->enc_pk = &ip->enc_key;
558  intro1_data->subcredential = subcredential;
559  intro1_data->link_specifiers = rp_lspecs;
560  intro1_data->onion_pk = node_get_curve25519_onion_key(rp_node);
561  if (intro1_data->onion_pk == NULL) {
562  /* We can't rendezvous without the curve25519 onion key. */
563  goto end;
564  }
565 
566  /* Success, we have valid introduce data. */
567  ret = 0;
568 
569  end:
570  return ret;
571 }
572 
573 /** Helper: cleanup function for client circuit. This is for every HS version.
574  * It is called from hs_circ_cleanup_on_close() entry point. */
575 static void
577 {
578  tor_assert(circ);
579 
580  if (circuit_is_hs_v3(circ)) {
582  }
583  /* It is possible the circuit has an HS purpose but no identifier (hs_ident).
584  * Thus possible that this passes through. */
585 }
586 
587 /** Helper: cleanup function for client circuit. This is for every HS version.
588  * It is called from hs_circ_cleanup_on_free() entry point. */
589 static void
591 {
592  tor_assert(circ);
593 
594  if (circuit_is_hs_v3(circ)) {
596  }
597  /* It is possible the circuit has an HS purpose but no identifier (hs_ident).
598  * Thus possible that this passes through. */
599 }
600 
601 /* ========== */
602 /* Public API */
603 /* ========== */
604 
605 /** Setup on the given circuit congestion control with the given parameters.
606  *
607  * This function assumes that congestion control is enabled on the network and
608  * so it is the caller responsability to make sure of it. */
609 void
611  uint8_t sendme_inc, bool is_single_onion)
612 {
613  circuit_t *circ = NULL;
614  circuit_params_t circ_params = {0};
615 
616  tor_assert(origin_circ);
617 
618  /* Ease our lives */
619  circ = TO_CIRCUIT(origin_circ);
620 
621  circ_params.cc_enabled = true;
622  circ_params.sendme_inc_cells = sendme_inc;
623 
624  /* It is setup on the circuit in order to indicate that congestion control is
625  * enabled. It will be transferred to the RP crypt_path_t once the handshake
626  * is finalized in finalize_rend_circuit() for both client and service
627  * because the final hop is not available until then. */
628 
629  if (is_single_onion) {
630  circ->ccontrol = congestion_control_new(&circ_params, CC_PATH_ONION_SOS);
631  } else {
632  if (get_options()->HSLayer3Nodes) {
633  circ->ccontrol = congestion_control_new(&circ_params, CC_PATH_ONION_VG);
634  } else {
635  circ->ccontrol = congestion_control_new(&circ_params, CC_PATH_ONION);
636  }
637  }
638 }
639 
640 /** Return an introduction point circuit matching the given intro point object.
641  * NULL is returned is no such circuit can be found. */
644 {
645  tor_assert(ip);
646 
648 }
649 
650 /** Return an introduction point established circuit matching the given intro
651  * point object. The circuit purpose has to be CIRCUIT_PURPOSE_S_INTRO. NULL
652  * is returned is no such circuit can be found. */
655 {
656  origin_circuit_t *circ;
657 
658  tor_assert(ip);
659 
661 
662  /* Only return circuit if it is established. */
663  return (circ && TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_INTRO) ?
664  circ : NULL;
665 }
666 
667 /** Called when we fail building a rendezvous circuit at some point other than
668  * the last hop: launches a new circuit to the same rendezvous point.
669  *
670  * We currently relaunch connections to rendezvous points if:
671  * - A rendezvous circuit timed out before connecting to RP.
672  * - The rendezvous circuit failed to connect to the RP.
673  *
674  * We avoid relaunching a connection to this rendezvous point if:
675  * - We have already tried MAX_REND_FAILURES times to connect to this RP,
676  * - We've been trying to connect to this RP for more than MAX_REND_TIMEOUT
677  * seconds, or
678  * - We've already retried this specific rendezvous circuit.
679  */
680 void
682 {
683  tor_assert(circ);
685 
686  /* Check if we are allowed to relaunch to the rendezvous point of circ. */
688  goto done;
689  }
690 
691  /* Legacy services don't have a hidden service ident. */
692  if (circ->hs_ident) {
694  }
695 
696  done:
697  return;
698 }
699 
700 /** For a given service and a service intro point, launch a circuit to the
701  * extend info ei. If the service is a single onion, and direct_conn is true,
702  * a one-hop circuit will be requested.
703  *
704  * Return 0 if the circuit was successfully launched and tagged
705  * with the correct identifier. On error, a negative value is returned. */
706 int
708  const hs_service_intro_point_t *ip,
709  extend_info_t *ei,
710  bool direct_conn)
711 {
712  /* Standard flags for introduction circuit. */
713  int ret = -1, circ_flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
714  origin_circuit_t *circ;
715 
716  tor_assert(service);
717  tor_assert(ip);
718  tor_assert(ei);
719 
720  /* Update circuit flags in case of a single onion service that requires a
721  * direct connection. */
722  tor_assert_nonfatal(ip->circuit_retries > 0);
723  /* Only single onion services can make direct conns */
724  if (BUG(!service->config.is_single_onion && direct_conn)) {
725  goto end;
726  }
727  /* We only use a one-hop path on the first attempt. If the first attempt
728  * fails, we use a 3-hop path for reachability / reliability. */
729  if (direct_conn && ip->circuit_retries == 1) {
730  circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
731  }
732 
733  log_info(LD_REND, "Launching a circuit to intro point %s for service %s.",
734  safe_str_client(extend_info_describe(ei)),
735  safe_str_client(service->onion_address));
736 
737  /* Note down the launch for the retry period. Even if the circuit fails to
738  * be launched, we still want to respect the retry period to avoid stress on
739  * the circuit subsystem. */
740  service->state.num_intro_circ_launched++;
742  ei, circ_flags);
743  if (circ == NULL) {
744  goto end;
745  }
746 
747  /* Setup the circuit identifier and attach it to it. */
748  circ->hs_ident = create_intro_circuit_identifier(service, ip);
749  tor_assert(circ->hs_ident);
750  /* Register circuit in the global circuitmap. */
751  register_intro_circ(ip, circ);
752 
753  /* Success. */
754  ret = 0;
755  end:
756  return ret;
757 }
758 
759 /** Called when a service introduction point circuit is done building. Given
760  * the service and intro point object, this function will send the
761  * ESTABLISH_INTRO cell on the circuit. Return 0 on success. Return 1 if the
762  * circuit has been repurposed to General because we already have too many
763  * opened. */
764 int
767  const hs_service_descriptor_t *desc,
768  origin_circuit_t *circ)
769 {
770  int ret = 0;
771  unsigned int num_intro_circ, num_needed_circ;
772 
773  tor_assert(service);
774  tor_assert(ip);
775  tor_assert(desc);
776  tor_assert(circ);
777 
778  /* Count opened circuits that have sent ESTABLISH_INTRO cells or are already
779  * established introduction circuits */
780  num_intro_circ = count_opened_desc_intro_point_circuits(service, desc);
781  num_needed_circ = service->config.num_intro_points;
782  if (num_intro_circ > num_needed_circ) {
783  /* There are too many opened valid intro circuit for what the service
784  * needs so repurpose this one. */
785 
786  /* XXX: Legacy code checks options->ExcludeNodes and if not NULL it just
787  * closes the circuit. I have NO idea why it does that so it hasn't been
788  * added here. I can only assume in case our ExcludeNodes list changes but
789  * in that case, all circuit are flagged unusable (config.c). --dgoulet */
790 
791  log_info(LD_CIRC | LD_REND, "Introduction circuit just opened but we "
792  "have enough for service %s. Repurposing "
793  "it to general and leaving internal.",
794  safe_str_client(service->onion_address));
796  /* Remove it from the circuitmap. */
798  /* Cleaning up the hidden service identifier and repurpose. */
799  hs_ident_circuit_free(circ->hs_ident);
800  circ->hs_ident = NULL;
801  if (circuit_should_use_vanguards(TO_CIRCUIT(circ)->purpose))
803  else
805 
806  /* Inform that this circuit just opened for this new purpose. */
807  circuit_has_opened(circ);
808  /* This return value indicate to the caller that the IP object should be
809  * removed from the service because it's corresponding circuit has just
810  * been repurposed. */
811  ret = 1;
812  goto done;
813  }
814 
815  log_info(LD_REND, "Introduction circuit %u established for service %s.",
816  TO_CIRCUIT(circ)->n_circ_id,
817  safe_str_client(service->onion_address));
819 
820  /* Time to send an ESTABLISH_INTRO cell on this circuit. On error, this call
821  * makes sure the circuit gets closed. */
822  send_establish_intro(service, ip, circ);
823 
824  done:
825  return ret;
826 }
827 
828 /** Called when a service rendezvous point circuit is done building. Given the
829  * service and the circuit, this function will send a RENDEZVOUS1 cell on the
830  * circuit using the information in the circuit identifier. If the cell can't
831  * be sent, the circuit is closed. */
832 void
834  origin_circuit_t *circ)
835 {
836  size_t payload_len;
837  uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
838 
839  tor_assert(service);
840  tor_assert(circ);
841  tor_assert(circ->hs_ident);
842 
843  /* Some useful logging. */
844  log_info(LD_REND, "Rendezvous circuit %u has opened with cookie %s "
845  "for service %s",
846  TO_CIRCUIT(circ)->n_circ_id,
847  hex_str((const char *) circ->hs_ident->rendezvous_cookie,
849  safe_str_client(service->onion_address));
851 
852  /* This can't fail. */
853  payload_len = hs_cell_build_rendezvous1(
855  sizeof(circ->hs_ident->rendezvous_cookie),
857  sizeof(circ->hs_ident->rendezvous_handshake_info),
858  payload);
859 
860  /* Pad the payload with random bytes so it matches the size of a legacy cell
861  * which is normally always bigger. Also, the size of a legacy cell is
862  * always smaller than the RELAY_PAYLOAD_SIZE so this is safe. */
863  if (payload_len < HS_LEGACY_RENDEZVOUS_CELL_SIZE) {
864  crypto_rand((char *) payload + payload_len,
865  HS_LEGACY_RENDEZVOUS_CELL_SIZE - payload_len);
866  payload_len = HS_LEGACY_RENDEZVOUS_CELL_SIZE;
867  }
868 
869  if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
870  RELAY_COMMAND_RENDEZVOUS1,
871  (const char *) payload,
872  payload_len,
873  circ->cpath->prev) < 0) {
874  /* On error, circuit is closed. */
875  log_warn(LD_REND, "Unable to send RENDEZVOUS1 cell on circuit %u "
876  "for service %s",
877  TO_CIRCUIT(circ)->n_circ_id,
878  safe_str_client(service->onion_address));
879 
881  HS_METRICS_ERR_RDV_RENDEZVOUS1);
882  goto done;
883  }
884 
885  /* Setup end-to-end rendezvous circuit between the client and us. */
888  sizeof(circ->hs_ident->rendezvous_ntor_key_seed),
889  1) < 0) {
890  log_warn(LD_GENERAL, "Failed to setup circ");
891 
892  hs_metrics_failed_rdv(&service->keys.identity_pk, HS_METRICS_ERR_RDV_E2E);
893  goto done;
894  }
895 
896  done:
897  memwipe(payload, 0, sizeof(payload));
898 }
899 
900 /** Circ has been expecting an INTRO_ESTABLISHED cell that just arrived. Handle
901  * the INTRO_ESTABLISHED cell payload of length payload_len arriving on the
902  * given introduction circuit circ. The service is only used for logging
903  * purposes. Return 0 on success else a negative value. */
904 int
906  const hs_service_intro_point_t *ip,
907  origin_circuit_t *circ,
908  const uint8_t *payload, size_t payload_len)
909 {
910  int ret = -1;
911 
912  tor_assert(service);
913  tor_assert(ip);
914  tor_assert(circ);
915  tor_assert(payload);
916 
917  if (BUG(TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)) {
918  goto done;
919  }
920 
921  /* Try to parse the payload into a cell making sure we do actually have a
922  * valid cell. */
923  if (hs_cell_parse_intro_established(payload, payload_len) < 0) {
924  log_warn(LD_REND, "Unable to parse the INTRO_ESTABLISHED cell on "
925  "circuit %u for service %s",
926  TO_CIRCUIT(circ)->n_circ_id,
927  safe_str_client(service->onion_address));
928  goto done;
929  }
930 
931  /* Switch the purpose to a fully working intro point. */
933  /* Getting a valid INTRODUCE_ESTABLISHED means we've successfully used the
934  * circuit so update our pathbias subsystem. */
936  /* Success. */
937  ret = 0;
938 
939  done:
940  return ret;
941 }
942 
943 /**
944  * Go into <b>data</b> and add the right subcredential to be able to handle
945  * this incoming cell.
946  *
947  * <b>desc_subcred</b> is the subcredential of the descriptor that corresponds
948  * to the intro point that received this intro request. This subcredential
949  * should be used if we are not an onionbalance instance.
950  *
951  * Return 0 if everything went well, or -1 in case of internal error.
952  */
953 static int
956  const hs_subcredential_t *desc_subcred)
957 {
958  /* Handle the simple case first: We are not an onionbalance instance and we
959  * should just use the regular descriptor subcredential */
960  if (!hs_ob_service_is_instance(service)) {
961  data->n_subcredentials = 1;
962  data->subcredentials = desc_subcred;
963  return 0;
964  }
965 
966  /* This should not happen since we should have made onionbalance
967  * subcredentials when we created our descriptors. */
968  if (BUG(!service->state.ob_subcreds)) {
969  return -1;
970  }
971 
972  /* We are an onionbalance instance: */
973  data->n_subcredentials = service->state.n_ob_subcreds;
974  data->subcredentials = service->state.ob_subcreds;
975 
976  return 0;
977 }
978 
979 /** We just received an INTRODUCE2 cell on the established introduction circuit
980  * circ. Handle the INTRODUCE2 payload of size payload_len for the given
981  * circuit and service. This cell is associated with the intro point object ip
982  * and the subcredential. Return 0 on success else a negative value. */
983 int
985  const origin_circuit_t *circ,
987  const hs_subcredential_t *subcredential,
988  const uint8_t *payload, size_t payload_len)
989 {
990  int ret = -1;
991  time_t elapsed;
993 
994  tor_assert(service);
995  tor_assert(circ);
996  tor_assert(ip);
997  tor_assert(subcredential);
998  tor_assert(payload);
999 
1000  /* Populate the data structure with everything we need for the cell to be
1001  * parsed, decrypted and key material computed correctly. */
1002  data.auth_pk = &ip->auth_key_kp.pubkey;
1003  data.enc_kp = &ip->enc_key_kp;
1004  data.payload = payload;
1005  data.payload_len = payload_len;
1006  data.link_specifiers = smartlist_new();
1007  data.replay_cache = ip->replay_cache;
1008  data.cc_enabled = 0;
1009 
1010  if (get_subcredential_for_handling_intro2_cell(service, &data,
1011  subcredential)) {
1013  HS_METRICS_ERR_INTRO_REQ_SUBCREDENTIAL);
1014  goto done;
1015  }
1016 
1017  if (hs_cell_parse_introduce2(&data, circ, service) < 0) {
1018  hs_metrics_reject_intro_req(service, HS_METRICS_ERR_INTRO_REQ_INTRODUCE2);
1019  goto done;
1020  }
1021 
1022  /* Check whether we've seen this REND_COOKIE before to detect repeats. */
1025  data.rendezvous_cookie, sizeof(data.rendezvous_cookie),
1026  &elapsed)) {
1027  /* A Tor client will send a new INTRODUCE1 cell with the same REND_COOKIE
1028  * as its previous one if its intro circ times out while in state
1029  * CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT. If we received the first
1030  * INTRODUCE1 cell (the intro-point relay converts it into an INTRODUCE2
1031  * cell), we are already trying to connect to that rend point (and may
1032  * have already succeeded); drop this cell. */
1033  log_info(LD_REND, "We received an INTRODUCE2 cell with same REND_COOKIE "
1034  "field %ld seconds ago. Dropping cell.",
1035  (long int) elapsed);
1037  HS_METRICS_ERR_INTRO_REQ_INTRODUCE2_REPLAY);
1038  goto done;
1039  }
1040 
1041  /* At this point, we just confirmed that the full INTRODUCE2 cell is valid
1042  * so increment our counter that we've seen one on this intro point. */
1043  ip->introduce2_count++;
1044 
1045  /* Launch rendezvous circuit with the onion key and rend cookie. */
1046  launch_rendezvous_point_circuit(service, ip, &data);
1047  /* Success. */
1048  ret = 0;
1049 
1050  done:
1051  link_specifier_smartlist_free(data.link_specifiers);
1052  memwipe(&data, 0, sizeof(data));
1053  return ret;
1054 }
1055 
1056 /** Circuit <b>circ</b> just finished the rend ntor key exchange. Use the key
1057  * exchange output material at <b>ntor_key_seed</b> and setup <b>circ</b> to
1058  * serve as a rendezvous end-to-end circuit between the client and the
1059  * service. If <b>is_service_side</b> is set, then we are the hidden service
1060  * and the other side is the client.
1061  *
1062  * Return 0 if the operation went well; in case of error return -1. */
1063 int
1065  const uint8_t *ntor_key_seed, size_t seed_len,
1066  int is_service_side)
1067 {
1068  if (BUG(!circuit_purpose_is_correct_for_rend(TO_CIRCUIT(circ)->purpose,
1069  is_service_side))) {
1070  return -1;
1071  }
1072 
1073  crypt_path_t *hop = create_rend_cpath(ntor_key_seed, seed_len,
1074  is_service_side);
1075  if (!hop) {
1076  log_warn(LD_REND, "Couldn't get v3 %s cpath!",
1077  is_service_side ? "service-side" : "client-side");
1078  return -1;
1079  }
1080 
1081  finalize_rend_circuit(circ, hop, is_service_side);
1082 
1083  return 0;
1084 }
1085 
1086 /** Given the introduction circuit intro_circ, the rendezvous circuit
1087  * rend_circ, a descriptor intro point object ip and the service's
1088  * subcredential, send an INTRODUCE1 cell on intro_circ.
1089  *
1090  * This will also setup the circuit identifier on rend_circ containing the key
1091  * material for the handshake and e2e encryption. Return 0 on success else
1092  * negative value. Because relay_send_command_from_edge() closes the circuit
1093  * on error, it is possible that intro_circ is closed on error. */
1094 int
1096  origin_circuit_t *rend_circ,
1097  const hs_desc_intro_point_t *ip,
1098  const hs_subcredential_t *subcredential)
1099 {
1100  int ret = -1;
1101  ssize_t payload_len;
1102  uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
1103  hs_cell_introduce1_data_t intro1_data;
1104 
1105  tor_assert(intro_circ);
1106  tor_assert(rend_circ);
1107  tor_assert(ip);
1108  tor_assert(subcredential);
1109 
1110  /* It is undefined behavior in hs_cell_introduce1_data_clear() if intro1_data
1111  * has been declared on the stack but not initialized. Here, we set it to 0.
1112  */
1113  memset(&intro1_data, 0, sizeof(hs_cell_introduce1_data_t));
1114 
1115  /* This takes various objects in order to populate the introduce1 data
1116  * object which is used to build the content of the cell. */
1117  const node_t *exit_node = build_state_get_exit_node(rend_circ->build_state);
1118  if (exit_node == NULL) {
1119  log_info(LD_REND, "Unable to get rendezvous point for circuit %u. "
1120  "Failing.", TO_CIRCUIT(intro_circ)->n_circ_id);
1121  goto done;
1122  }
1123 
1124  /* We should never select an invalid rendezvous point in theory but if we
1125  * do, this function will fail to populate the introduce data. */
1126  if (setup_introduce1_data(ip, exit_node, subcredential, &intro1_data) < 0) {
1127  log_info(LD_REND, "Unable to setup INTRODUCE1 data. The chosen rendezvous "
1128  "point is unusable. Closing circuit.");
1129  goto close;
1130  }
1131 
1132  /* If the rend circ was set up for congestion control, add that to the
1133  * intro data, to signal it in an extension */
1134  if (TO_CIRCUIT(rend_circ)->ccontrol) {
1135  intro1_data.cc_enabled = 1;
1136  }
1137 
1138  /* Final step before we encode a cell, we setup the circuit identifier which
1139  * will generate both the rendezvous cookie and client keypair for this
1140  * connection. Those are put in the ident. */
1141  intro1_data.rendezvous_cookie = rend_circ->hs_ident->rendezvous_cookie;
1142  intro1_data.client_kp = &rend_circ->hs_ident->rendezvous_client_kp;
1143 
1144  memcpy(intro_circ->hs_ident->rendezvous_cookie,
1145  rend_circ->hs_ident->rendezvous_cookie,
1146  sizeof(intro_circ->hs_ident->rendezvous_cookie));
1147 
1148  /* From the introduce1 data object, this will encode the INTRODUCE1 cell
1149  * into payload which is then ready to be sent as is. */
1150  payload_len = hs_cell_build_introduce1(&intro1_data, payload);
1151  if (BUG(payload_len < 0)) {
1152  goto close;
1153  }
1154 
1155  if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(intro_circ),
1156  RELAY_COMMAND_INTRODUCE1,
1157  (const char *) payload, payload_len,
1158  intro_circ->cpath->prev) < 0) {
1159  /* On error, circuit is closed. */
1160  log_warn(LD_REND, "Unable to send INTRODUCE1 cell on circuit %u.",
1161  TO_CIRCUIT(intro_circ)->n_circ_id);
1162  goto done;
1163  }
1164 
1165  /* Success. */
1166  ret = 0;
1167  goto done;
1168 
1169  close:
1170  circuit_mark_for_close(TO_CIRCUIT(rend_circ), END_CIRC_REASON_INTERNAL);
1171  done:
1172  hs_cell_introduce1_data_clear(&intro1_data);
1173  memwipe(payload, 0, sizeof(payload));
1174  return ret;
1175 }
1176 
1177 /** Send an ESTABLISH_RENDEZVOUS cell along the rendezvous circuit circ. On
1178  * success, 0 is returned else -1 and the circuit is marked for close. */
1179 int
1181 {
1182  ssize_t cell_len = 0;
1183  uint8_t cell[RELAY_PAYLOAD_SIZE] = {0};
1184 
1185  tor_assert(circ);
1187 
1188  log_info(LD_REND, "Send an ESTABLISH_RENDEZVOUS cell on circuit %u",
1189  TO_CIRCUIT(circ)->n_circ_id);
1190 
1191  /* Set timestamp_dirty, because circuit_expire_building expects it,
1192  * and the rend cookie also means we've used the circ. */
1193  TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
1194 
1195  /* We've attempted to use this circuit. Probe it if we fail */
1197 
1198  /* Generate the RENDEZVOUS_COOKIE and place it in the identifier so we can
1199  * complete the handshake when receiving the acknowledgement. */
1201  /* Generate the client keypair. No need to be extra strong, not long term */
1203 
1204  cell_len =
1206  cell);
1207  if (BUG(cell_len < 0)) {
1208  goto err;
1209  }
1210 
1211  if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
1212  RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
1213  (const char *) cell, cell_len,
1214  circ->cpath->prev) < 0) {
1215  /* Circuit has been marked for close */
1216  log_warn(LD_REND, "Unable to send ESTABLISH_RENDEZVOUS cell on "
1217  "circuit %u", TO_CIRCUIT(circ)->n_circ_id);
1218  memwipe(cell, 0, cell_len);
1219  goto err;
1220  }
1221 
1222  memwipe(cell, 0, cell_len);
1223  return 0;
1224  err:
1225  return -1;
1226 }
1227 
1228 /** Circuit cleanup strategy:
1229  *
1230  * What follows is a series of functions that notifies the HS subsystem of 3
1231  * different circuit cleanup phase: close, free and repurpose.
1232  *
1233  * Tor can call any of those in any orders so they have to be safe between
1234  * each other. In other words, the free should never depend on close to be
1235  * called before.
1236  *
1237  * The "on_close()" is called from circuit_mark_for_close() which is
1238  * considered the tor fast path and thus as little work as possible should
1239  * done in that function. Currently, we only remove the circuit from the HS
1240  * circuit map and move on.
1241  *
1242  * The "on_free()" is called from circuit circuit_free_() and it is very
1243  * important that at the end of the function, no state or objects related to
1244  * this circuit remains alive.
1245  *
1246  * The "on_repurpose()" is called from circuit_change_purpose() for which we
1247  * simply remove it from the HS circuit map. We do not have other cleanup
1248  * requirements after that.
1249  *
1250  * NOTE: The onion service code, specifically the service code, cleans up
1251  * lingering objects or state if any of its circuit disappear which is why
1252  * our cleanup strategy doesn't involve any service specific actions. As long
1253  * as the circuit is removed from the HS circuit map, it won't be used.
1254  */
1255 
1256 /** We are about to close this <b>circ</b>. Clean it up from any related HS
1257  * data structures. This function can be called multiple times safely for the
1258  * same circuit. */
1259 void
1261 {
1262  tor_assert(circ);
1263 
1266  }
1267 
1269  if (circuit_is_hs_v3(circ)) {
1271  }
1272  }
1273 
1274  /* On close, we simply remove it from the circuit map. It can not be used
1275  * anymore. We keep this code path fast and lean. */
1276 
1277  if (circ->hs_token) {
1279  }
1280 }
1281 
1282 /** We are about to free this <b>circ</b>. Clean it up from any related HS
1283  * data structures. This function can be called multiple times safely for the
1284  * same circuit. */
1285 void
1287 {
1288  tor_assert(circ);
1289 
1290  /* NOTE: Bulk of the work of cleaning up a circuit is done here. */
1291 
1294  }
1295 
1296  /* We have no assurance that the given HS circuit has been closed before and
1297  * thus removed from the HS map. This actually happens in unit tests. */
1298  if (circ->hs_token) {
1300  }
1301 }
1302 
1303 /** We are about to repurpose this <b>circ</b>. Clean it up from any related
1304  * HS data structures. This function can be called multiple times safely for
1305  * the same circuit. */
1306 void
1308 {
1309  tor_assert(circ);
1310 
1311  /* On repurpose, we simply remove it from the circuit map but we do not do
1312  * the on_free actions since we don't treat a repurpose as something we need
1313  * to report in the client cache failure. */
1314 
1315  if (circ->hs_token) {
1317  }
1318 
1319  switch (circ->purpose) {
1321  /* This circuit was connecting to a rendezvous point but it is being
1322  * repurposed so we need to relaunch an attempt else the client will be
1323  * left hanging waiting for the rendezvous. */
1325  break;
1326  default:
1327  break;
1328  }
1329 }
1330 
1331 /** Return true iff the given established client rendezvous circuit was sent
1332  * into the INTRODUCE1 cell. This is called so we can take a decision on
1333  * expiring or not the circuit.
1334  *
1335  * The caller MUST make sure the circuit is an established client rendezvous
1336  * circuit (purpose: CIRCUIT_PURPOSE_C_REND_READY).
1337  *
1338  * This function supports all onion service versions. */
1339 bool
1341 {
1342  tor_assert(circ);
1343  /* This can only be called for a rendezvous circuit that is an established
1344  * confirmed rendezsvous circuit but without an introduction ACK. */
1346 
1347  /* When the INTRODUCE1 cell is sent, the introduction encryption public
1348  * key is copied in the rendezvous circuit hs identifier. If it is a valid
1349  * key, we know that this circuit is waiting the ACK on the introduction
1350  * circuit. We want to _not_ spare the circuit if the key was never set. */
1351 
1352  if (circ->hs_ident) {
1353  /* v3. */
1355  return true;
1356  }
1357  } else {
1358  /* A circuit with an HS purpose without an hs_ident in theory can not
1359  * happen. In case, scream loudly and return false to the caller that the
1360  * rendezvous was not sent in the INTRO1 cell. */
1362  }
1363 
1364  /* The rendezvous has not been specified in the INTRODUCE1 cell. */
1365  return false;
1366 }
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
void pathbias_count_use_attempt(origin_circuit_t *circ)
Definition: circpathbias.c:615
void pathbias_mark_use_success(origin_circuit_t *circ)
Definition: circpathbias.c:672
void circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
Definition: circuitbuild.c:355
const node_t * build_state_get_exit_node(cpath_build_state_t *state)
Header file for circuitbuild.c.
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:177
int32_t circuit_initial_package_window(void)
Definition: circuitlist.c:986
Header file for circuitlist.c.
#define CIRCUIT_PURPOSE_S_CONNECT_REND
Definition: circuitlist.h:107
#define CIRCUIT_STATE_OPEN
Definition: circuitlist.h:32
#define CIRCUIT_PURPOSE_C_REND_JOINED
Definition: circuitlist.h:88
#define CIRCUIT_PURPOSE_S_INTRO
Definition: circuitlist.h:104
#define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
Definition: circuitlist.h:86
#define CIRCUIT_PURPOSE_S_REND_JOINED
Definition: circuitlist.h:110
#define CIRCUIT_PURPOSE_C_REND_READY
Definition: circuitlist.h:83
#define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
Definition: circuitlist.h:101
#define CIRCUIT_PURPOSE_C_ESTABLISH_REND
Definition: circuitlist.h:81
#define CIRCUIT_PURPOSE_C_GENERAL
Definition: circuitlist.h:70
#define CIRCUIT_PURPOSE_HS_VANGUARDS
Definition: circuitlist.h:131
bool circuit_is_hs_v3(const circuit_t *circ)
Definition: circuituse.c:1963
bool circuit_purpose_is_hs_service(const uint8_t purpose)
Definition: circuituse.c:1948
void circuit_change_purpose(circuit_t *circ, uint8_t new_purpose)
Definition: circuituse.c:3028
int circuit_should_use_vanguards(uint8_t purpose)
Definition: circuituse.c:1980
void circuit_has_opened(origin_circuit_t *circ)
Definition: circuituse.c:1631
bool circuit_purpose_is_hs_client(const uint8_t purpose)
Definition: circuituse.c:1940
origin_circuit_t * circuit_launch_by_extend_info(uint8_t purpose, extend_info_t *extend_info, int flags)
Definition: circuituse.c:2051
void circuit_try_attaching_streams(origin_circuit_t *circ)
Definition: circuituse.c:1706
Header file for circuituse.c.
#define CIRCLAUNCH_NEED_CAPACITY
Definition: circuituse.h:43
#define CIRCLAUNCH_ONEHOP_TUNNEL
Definition: circuituse.h:39
#define CIRCLAUNCH_IS_INTERNAL
Definition: circuituse.h:46
#define CIRCLAUNCH_NEED_UPTIME
Definition: circuituse.h:41
const or_options_t * get_options(void)
Definition: config.c:926
Header file for config.c.
congestion_control_t * congestion_control_new(const circuit_params_t *params, cc_path_t path)
Public APIs for congestion control.
static uint8_t congestion_control_sendme_inc(void)
Structure definitions for congestion control.
Circuit-build-stse structure.
int cpath_init_circuit_crypto(crypt_path_t *cpath, const char *key_data, size_t key_data_len, int reverse, int is_hs_v3)
Definition: crypt_path.c:148
void cpath_extend_linked_list(crypt_path_t **head_ptr, crypt_path_t *new_hop)
Definition: crypt_path.c:42
Header file for crypt_path.c.
Path structures for origin circuits.
int curve25519_keypair_generate(curve25519_keypair_t *keypair_out, int extra_strong)
int curve25519_public_key_is_ok(const curve25519_public_key_t *key)
Headers for crypto_dh.c.
void ed25519_pubkey_copy(ed25519_public_key_t *dest, const ed25519_public_key_t *src)
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
void crypto_rand(char *to, size_t n)
Definition: crypto_rand.c:479
Common functions for using (pseudo-)random number generators.
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:55
Common functions for cryptographic routines.
const char * extend_info_describe(const extend_info_t *ei)
Definition: describe.c:224
Header file for describe.c.
#define DIGEST256_LEN
Definition: digest_sizes.h:23
Header for core/or/extendinfo.c.
ssize_t hs_cell_build_establish_rendezvous(const uint8_t *rendezvous_cookie, uint8_t *cell_out)
Definition: hs_cell.c:1055
ssize_t hs_cell_parse_intro_established(const uint8_t *payload, size_t payload_len)
Definition: hs_cell.c:701
ssize_t hs_cell_build_establish_intro(const char *circ_nonce, const hs_service_config_t *service_config, const hs_service_intro_point_t *ip, uint8_t *cell_out)
Definition: hs_cell.c:587
void hs_cell_introduce1_data_clear(hs_cell_introduce1_data_t *data)
Definition: hs_cell.c:1122
ssize_t hs_cell_build_rendezvous1(const uint8_t *rendezvous_cookie, size_t rendezvous_cookie_len, const uint8_t *rendezvous_handshake_info, size_t rendezvous_handshake_info_len, uint8_t *cell_out)
Definition: hs_cell.c:980
ssize_t hs_cell_build_introduce1(const hs_cell_introduce1_data_t *data, uint8_t *cell_out)
Definition: hs_cell.c:1015
ssize_t hs_cell_parse_introduce2(hs_cell_introduce2_data_t *data, const origin_circuit_t *circ, const hs_service_t *service)
Definition: hs_cell.c:817
Header file containing cell data for the whole HS subsystem.
STATIC hs_ident_circuit_t * create_rp_circuit_identifier(const hs_service_t *service, const uint8_t *rendezvous_cookie, const curve25519_public_key_t *server_pk, const hs_ntor_rend_cell_keys_t *keys)
Definition: hs_circuit.c:192
void hs_circ_service_rp_has_opened(const hs_service_t *service, origin_circuit_t *circ)
Definition: hs_circuit.c:833
static int get_subcredential_for_handling_intro2_cell(const hs_service_t *service, hs_cell_introduce2_data_t *data, const hs_subcredential_t *desc_subcred)
Definition: hs_circuit.c:954
void hs_circ_cleanup_on_repurpose(circuit_t *circ)
Definition: hs_circuit.c:1307
static void cleanup_on_close_client_circ(circuit_t *circ)
Definition: hs_circuit.c:576
static void register_intro_circ(const hs_service_intro_point_t *ip, origin_circuit_t *circ)
Definition: hs_circuit.c:147
origin_circuit_t * hs_circ_service_get_established_intro_circ(const hs_service_intro_point_t *ip)
Definition: hs_circuit.c:654
int hs_circ_launch_intro_point(hs_service_t *service, const hs_service_intro_point_t *ip, extend_info_t *ei, bool direct_conn)
Definition: hs_circuit.c:707
static void finalize_rend_circuit(origin_circuit_t *circ, crypt_path_t *hop, int is_service_side)
Definition: hs_circuit.c:112
static hs_ident_circuit_t * create_intro_circuit_identifier(const hs_service_t *service, const hs_service_intro_point_t *ip)
Definition: hs_circuit.c:231
void hs_circ_setup_congestion_control(origin_circuit_t *origin_circ, uint8_t sendme_inc, bool is_single_onion)
Definition: hs_circuit.c:610
STATIC void launch_rendezvous_point_circuit(const hs_service_t *service, const hs_service_intro_point_t *ip, const hs_cell_introduce2_data_t *data)
Definition: hs_circuit.c:314
static const char * get_service_anonymity_string(const hs_service_t *service)
Definition: hs_circuit.c:297
int hs_circ_send_introduce1(origin_circuit_t *intro_circ, origin_circuit_t *rend_circ, const hs_desc_intro_point_t *ip, const hs_subcredential_t *subcredential)
Definition: hs_circuit.c:1095
static void retry_service_rendezvous_point(const origin_circuit_t *circ)
Definition: hs_circuit.c:463
static crypt_path_t * create_rend_cpath(const uint8_t *ntor_key_seed, size_t seed_len, int is_service_side)
Definition: hs_circuit.c:82
int hs_circ_service_intro_has_opened(hs_service_t *service, hs_service_intro_point_t *ip, const hs_service_descriptor_t *desc, origin_circuit_t *circ)
Definition: hs_circuit.c:765
static void cleanup_on_free_client_circ(circuit_t *circ)
Definition: hs_circuit.c:590
void hs_circ_cleanup_on_close(circuit_t *circ)
Definition: hs_circuit.c:1260
static void send_establish_intro(const hs_service_t *service, hs_service_intro_point_t *ip, origin_circuit_t *circ)
Definition: hs_circuit.c:251
int hs_circ_handle_introduce2(const hs_service_t *service, const origin_circuit_t *circ, hs_service_intro_point_t *ip, const hs_subcredential_t *subcredential, const uint8_t *payload, size_t payload_len)
Definition: hs_circuit.c:984
int hs_circ_handle_intro_established(const hs_service_t *service, const hs_service_intro_point_t *ip, origin_circuit_t *circ, const uint8_t *payload, size_t payload_len)
Definition: hs_circuit.c:905
static int can_relaunch_service_rendezvous_point(const origin_circuit_t *circ)
Definition: hs_circuit.c:427
static unsigned int count_opened_desc_intro_point_circuits(const hs_service_t *service, const hs_service_descriptor_t *desc)
Definition: hs_circuit.c:160
origin_circuit_t * hs_circ_service_get_intro_circ(const hs_service_intro_point_t *ip)
Definition: hs_circuit.c:643
bool hs_circ_is_rend_sent_in_intro1(const origin_circuit_t *circ)
Definition: hs_circuit.c:1340
void hs_circ_retry_service_rendezvous_point(const origin_circuit_t *circ)
Definition: hs_circuit.c:681
static int circuit_purpose_is_correct_for_rend(unsigned int circ_purpose, int is_service_side)
Definition: hs_circuit.c:52
static int setup_introduce1_data(const hs_desc_intro_point_t *ip, const node_t *rp_node, const hs_subcredential_t *subcredential, hs_cell_introduce1_data_t *intro1_data)
Definition: hs_circuit.c:532
int hs_circ_send_establish_rendezvous(origin_circuit_t *circ)
Definition: hs_circuit.c:1180
int hs_circuit_setup_e2e_rend_circ(origin_circuit_t *circ, const uint8_t *ntor_key_seed, size_t seed_len, int is_service_side)
Definition: hs_circuit.c:1064
void hs_circ_cleanup_on_free(circuit_t *circ)
Definition: hs_circuit.c:1286
Header file containing circuit data for the whole HS subsystem.
void hs_circuitmap_remove_circuit(circuit_t *circ)
origin_circuit_t * hs_circuitmap_get_intro_circ_v3_service_side(const ed25519_public_key_t *auth_key)
void hs_circuitmap_register_intro_circ_v3_service_side(origin_circuit_t *circ, const ed25519_public_key_t *auth_key)
Header file for hs_circuitmap.c.
void hs_client_circuit_cleanup_on_close(const circuit_t *circ)
Definition: hs_client.c:1933
void hs_client_circuit_cleanup_on_free(const circuit_t *circ)
Definition: hs_client.c:1964
Header file containing client data for the HS subsystem.
int hs_get_service_max_rend_failures(void)
Definition: hs_common.c:233
extend_info_t * hs_get_extend_info_from_lspecs(const smartlist_t *lspecs, const curve25519_public_key_t *onion_key, int direct_conn)
Definition: hs_common.c:1596
int hs_service_requires_uptime_circ(const smartlist_t *ports)
Definition: hs_common.c:1016
#define HS_LEGACY_RENDEZVOUS_CELL_SIZE
Definition: hs_common.h:128
#define MAX_REND_FAILURES
Definition: hs_common.h:44
#define MAX_REND_TIMEOUT
Definition: hs_common.h:47
hs_ident_circuit_t * hs_ident_circuit_dup(const hs_ident_circuit_t *src)
Definition: hs_ident.c:37
hs_ident_circuit_t * hs_ident_circuit_new(const ed25519_public_key_t *identity_pk)
Definition: hs_ident.c:16
Header file containing circuit and connection identifier data for the whole HS subsystem.
#define HS_REND_COOKIE_LEN
Definition: hs_ident.h:30
Header for feature/hs/hs_metrics.c.
#define hs_metrics_new_rdv(i)
Definition: hs_metrics.h:73
#define hs_metrics_failed_rdv(i, reason)
Definition: hs_metrics.h:63
#define hs_metrics_reject_intro_req(s, reason)
Definition: hs_metrics.h:42
int hs_ntor_circuit_key_expansion(const uint8_t *ntor_key_seed, size_t seed_len, uint8_t *keys_out, size_t keys_out_len)
Definition: hs_ntor.c:615
Header for hs_ntor.c.
bool hs_ob_service_is_instance(const hs_service_t *service)
Definition: hs_ob.c:201
Header file for the specific code for onion balance.
void hs_service_circuit_cleanup_on_close(const circuit_t *circ)
Definition: hs_service.c:3674
Header file containing service data for the HS subsystem.
#define log_fn(severity, domain, args,...)
Definition: log.h:283
#define LD_REND
Definition: log.h:84
#define LD_BUG
Definition: log.h:86
#define LD_GENERAL
Definition: log.h:62
#define LD_CIRC
Definition: log.h:82
#define LOG_INFO
Definition: log.h:45
#define tor_free(p)
Definition: malloc.h:56
Node information structure.
const curve25519_public_key_t * node_get_curve25519_onion_key(const node_t *node)
Definition: nodelist.c:2016
Header file for nodelist.c.
Header file for onion_crypto.c.
Master header file for Tor-specific functionality.
#define REND_COOKIE_LEN
Definition: or.h:344
#define TO_CIRCUIT(x)
Definition: or.h:836
#define RELAY_PAYLOAD_SIZE
Definition: or.h:485
#define CIRCWINDOW_START
Definition: or.h:385
Origin circuit structure.
Header file for policies.c.
Header file for relay.c.
Header file for rephist.c.
int replaycache_add_test_and_elapsed(replaycache_t *r, const void *data, size_t len, time_t *elapsed)
Definition: replaycache.c:195
smartlist_t * smartlist_new(void)
uint8_t sendme_inc_cells
Definition: onion_crypto.h:36
struct hs_token_t * hs_token
Definition: circuit_st.h:217
uint8_t state
Definition: circuit_st.h:111
uint16_t marked_for_close
Definition: circuit_st.h:190
uint8_t purpose
Definition: circuit_st.h:112
struct congestion_control_t * ccontrol
Definition: circuit_st.h:250
extend_info_t * chosen_exit
struct crypt_path_t * prev
Definition: crypt_path_st.h:80
uint8_t state
Definition: crypt_path_st.h:73
char rend_circ_nonce[DIGEST_LEN]
Definition: crypt_path_st.h:63
struct congestion_control_t * ccontrol
Definition: crypt_path_st.h:89
const ed25519_public_key_t * auth_pk
Definition: hs_cell.h:30
const struct hs_subcredential_t * subcredential
Definition: hs_cell.h:34
const curve25519_keypair_t * client_kp
Definition: hs_cell.h:40
const curve25519_public_key_t * enc_pk
Definition: hs_cell.h:32
unsigned int cc_enabled
Definition: hs_cell.h:44
const uint8_t * rendezvous_cookie
Definition: hs_cell.h:38
const curve25519_public_key_t * onion_pk
Definition: hs_cell.h:36
smartlist_t * link_specifiers
Definition: hs_cell.h:42
curve25519_public_key_t onion_pk
Definition: hs_cell.h:78
const ed25519_public_key_t * auth_pk
Definition: hs_cell.h:57
const curve25519_keypair_t * enc_kp
Definition: hs_cell.h:61
uint8_t rendezvous_cookie[REND_COOKIE_LEN]
Definition: hs_cell.h:80
replaycache_t * replay_cache
Definition: hs_cell.h:86
unsigned int cc_enabled
Definition: hs_cell.h:90
const struct hs_subcredential_t * subcredentials
Definition: hs_cell.h:69
const uint8_t * payload
Definition: hs_cell.h:71
curve25519_public_key_t client_pk
Definition: hs_cell.h:82
smartlist_t * link_specifiers
Definition: hs_cell.h:84
curve25519_public_key_t enc_key
tor_cert_t * auth_key_cert
uint8_t rendezvous_cookie[HS_REND_COOKIE_LEN]
Definition: hs_ident.h:60
ed25519_public_key_t intro_auth_pk
Definition: hs_ident.h:51
curve25519_keypair_t rendezvous_client_kp
Definition: hs_ident.h:72
curve25519_public_key_t intro_enc_pk
Definition: hs_ident.h:55
uint8_t rendezvous_ntor_key_seed[DIGEST256_LEN]
Definition: hs_ident.h:76
ed25519_public_key_t identity_pk
Definition: hs_ident.h:45
uint8_t rendezvous_handshake_info[CURVE25519_PUBKEY_LEN+DIGEST256_LEN]
Definition: hs_ident.h:68
unsigned int is_single_onion
Definition: hs_service.h:243
smartlist_t * ports
Definition: hs_service.h:214
unsigned int num_intro_points
Definition: hs_service.h:231
hs_service_intropoints_t intro_points
Definition: hs_service.h:163
ed25519_keypair_t auth_key_kp
Definition: hs_service.h:55
replaycache_t * replay_cache
Definition: hs_service.h:85
curve25519_keypair_t enc_key_kp
Definition: hs_service.h:58
digest256map_t * map
Definition: hs_service.h:104
ed25519_public_key_t identity_pk
Definition: hs_service.h:179
replaycache_t * replay_cache_rend_cookie
Definition: hs_service.h:281
unsigned int num_intro_circ_launched
Definition: hs_service.h:274
hs_service_state_t state
Definition: hs_service.h:307
char onion_address[HS_SERVICE_ADDR_LEN_BASE32+1]
Definition: hs_service.h:300
hs_service_config_t config
Definition: hs_service.h:313
hs_service_keys_t keys
Definition: hs_service.h:310
Definition: node_st.h:34
struct hs_ident_circuit_t * hs_ident
crypt_path_t * cpath
cpath_build_state_t * build_state
ed25519_public_key_t signed_key
Definition: torcert.h:32
#define STATIC
Definition: testsupport.h:32
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
#define tor_assert_nonfatal_unreached()
Definition: util_bug.h:176
#define tor_assert(expr)
Definition: util_bug.h:102
#define CURVE25519_PUBKEY_LEN
Definition: x25519_sizes.h:20