Tor  0.4.5.0-alpha-dev
hs_service.c
Go to the documentation of this file.
1 /* Copyright (c) 2016-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 /**
5  * \file hs_service.c
6  * \brief Implement next generation hidden service functionality
7  **/
8 
9 #define HS_SERVICE_PRIVATE
10 
11 #include "core/or/or.h"
12 #include "app/config/config.h"
13 #include "app/config/statefile.h"
15 #include "core/mainloop/mainloop.h"
16 #include "core/or/circuitbuild.h"
17 #include "core/or/circuitlist.h"
18 #include "core/or/circuituse.h"
19 #include "core/or/extendinfo.h"
20 #include "core/or/relay.h"
21 #include "feature/client/circpathbias.h"
25 #include "feature/keymgt/loadkey.h"
35 
36 #include "feature/hs/hs_circuit.h"
37 #include "feature/hs/hs_common.h"
38 #include "feature/hs/hs_config.h"
39 #include "feature/hs/hs_control.h"
41 #include "feature/hs/hs_ident.h"
43 #include "feature/hs/hs_service.h"
44 #include "feature/hs/hs_stats.h"
45 #include "feature/hs/hs_ob.h"
46 
49 #include "core/or/extend_info_st.h"
53 #include "app/config/or_state_st.h"
55 
56 #include "lib/encoding/confline.h"
58 
59 /* Trunnel */
60 #include "trunnel/ed25519_cert.h"
61 #include "trunnel/hs/cell_common.h"
62 #include "trunnel/hs/cell_establish_intro.h"
63 
64 #ifdef HAVE_SYS_STAT_H
65 #include <sys/stat.h>
66 #endif
67 #ifdef HAVE_UNISTD_H
68 #include <unistd.h>
69 #endif
70 
71 #ifndef COCCI
72 /** Helper macro. Iterate over every service in the global map. The var is the
73  * name of the service pointer. */
74 #define FOR_EACH_SERVICE_BEGIN(var) \
75  STMT_BEGIN \
76  hs_service_t **var##_iter, *var; \
77  HT_FOREACH(var##_iter, hs_service_ht, hs_service_map) { \
78  var = *var##_iter;
79 #define FOR_EACH_SERVICE_END } STMT_END ;
80 
81 /** Helper macro. Iterate over both current and previous descriptor of a
82  * service. The var is the name of the descriptor pointer. This macro skips
83  * any descriptor object of the service that is NULL. */
84 #define FOR_EACH_DESCRIPTOR_BEGIN(service, var) \
85  STMT_BEGIN \
86  hs_service_descriptor_t *var; \
87  for (int var ## _loop_idx = 0; var ## _loop_idx < 2; \
88  ++var ## _loop_idx) { \
89  (var ## _loop_idx == 0) ? (var = service->desc_current) : \
90  (var = service->desc_next); \
91  if (var == NULL) continue;
92 #define FOR_EACH_DESCRIPTOR_END } STMT_END ;
93 #endif /* !defined(COCCI) */
94 
95 /* Onion service directory file names. */
96 static const char fname_keyfile_prefix[] = "hs_ed25519";
97 static const char dname_client_pubkeys[] = "authorized_clients";
98 static const char fname_hostname[] = "hostname";
99 static const char address_tld[] = "onion";
100 
101 /** Staging list of service object. When configuring service, we add them to
102  * this list considered a staging area and they will get added to our global
103  * map once the keys have been loaded. These two steps are separated because
104  * loading keys requires that we are an actual running tor process. */
106 
107 /** True if the list of available router descriptors might have changed which
108  * might result in an altered hash ring. Check if the hash ring changed and
109  * reupload if needed */
111 
112 /* Static declaration. */
113 static int load_client_keys(hs_service_t *service);
115  time_t now, bool is_current);
116 static int build_service_desc_superencrypted(const hs_service_t *service,
118 static void move_descriptors(hs_service_t *src, hs_service_t *dst);
119 static int service_encode_descriptor(const hs_service_t *service,
120  const hs_service_descriptor_t *desc,
121  const ed25519_keypair_t *signing_kp,
122  char **encoded_out);
123 
124 /** Helper: Function to compare two objects in the service map. Return 1 if the
125  * two service have the same master public identity key. */
126 static inline int
127 hs_service_ht_eq(const hs_service_t *first, const hs_service_t *second)
128 {
129  tor_assert(first);
130  tor_assert(second);
131  /* Simple key compare. */
132  return ed25519_pubkey_eq(&first->keys.identity_pk,
133  &second->keys.identity_pk);
134 }
135 
136 /** Helper: Function for the service hash table code below. The key used is the
137  * master public identity key which is ultimately the onion address. */
138 static inline unsigned int
140 {
141  tor_assert(service);
142  return (unsigned int) siphash24g(service->keys.identity_pk.pubkey,
143  sizeof(service->keys.identity_pk.pubkey));
144 }
145 
146 /** This is _the_ global hash map of hidden services which indexed the service
147  * contained in it by master public identity key which is roughly the onion
148  * address of the service. */
149 static struct hs_service_ht *hs_service_map;
150 
151 /* Register the service hash table. */
152 HT_PROTOTYPE(hs_service_ht, /* Name of hashtable. */
153  hs_service_t, /* Object contained in the map. */
154  hs_service_node, /* The name of the HT_ENTRY member. */
155  hs_service_ht_hash, /* Hashing function. */
156  hs_service_ht_eq); /* Compare function for objects. */
157 
158 HT_GENERATE2(hs_service_ht, hs_service_t, hs_service_node,
160  0.6, tor_reallocarray, tor_free_);
161 
162 /** Query the given service map with a public key and return a service object
163  * if found else NULL. It is also possible to set a directory path in the
164  * search query. If pk is NULL, then it will be set to zero indicating the
165  * hash table to compare the directory path instead. */
167 find_service(hs_service_ht *map, const ed25519_public_key_t *pk)
168 {
169  hs_service_t dummy_service;
170  tor_assert(map);
171  tor_assert(pk);
172  memset(&dummy_service, 0, sizeof(dummy_service));
173  ed25519_pubkey_copy(&dummy_service.keys.identity_pk, pk);
174  return HT_FIND(hs_service_ht, map, &dummy_service);
175 }
176 
177 /** Register the given service in the given map. If the service already exists
178  * in the map, -1 is returned. On success, 0 is returned and the service
179  * ownership has been transferred to the global map. */
180 STATIC int
181 register_service(hs_service_ht *map, hs_service_t *service)
182 {
183  tor_assert(map);
184  tor_assert(service);
186 
187  if (find_service(map, &service->keys.identity_pk)) {
188  /* Existing service with the same key. Do not register it. */
189  return -1;
190  }
191  /* Taking ownership of the object at this point. */
192  HT_INSERT(hs_service_ht, map, service);
193 
194  /* If we just modified the global map, we notify. */
195  if (map == hs_service_map) {
197  }
198 
199  return 0;
200 }
201 
202 /** Remove a given service from the given map. If service is NULL or the
203  * service key is unset, return gracefully. */
204 STATIC void
205 remove_service(hs_service_ht *map, hs_service_t *service)
206 {
207  hs_service_t *elm;
208 
209  tor_assert(map);
210 
211  /* Ignore if no service or key is zero. */
212  if (BUG(service == NULL) ||
213  BUG(ed25519_public_key_is_zero(&service->keys.identity_pk))) {
214  return;
215  }
216 
217  elm = HT_REMOVE(hs_service_ht, map, service);
218  if (elm) {
219  tor_assert(elm == service);
220  } else {
221  log_warn(LD_BUG, "Could not find service in the global map "
222  "while removing service %s",
223  escaped(service->config.directory_path));
224  }
225 
226  /* If we just modified the global map, we notify. */
227  if (map == hs_service_map) {
229  }
230 }
231 
232 /** Set the default values for a service configuration object <b>c</b>. */
233 static void
235  const or_options_t *options)
236 {
237  (void) options;
238  tor_assert(c);
239  c->ports = smartlist_new();
240  c->directory_path = NULL;
244  c->allow_unknown_ports = 0;
245  c->is_single_onion = 0;
246  c->dir_group_readable = 0;
247  c->is_ephemeral = 0;
248  c->has_dos_defense_enabled = HS_CONFIG_V3_DOS_DEFENSE_DEFAULT;
249  c->intro_dos_rate_per_sec = HS_CONFIG_V3_DOS_DEFENSE_RATE_PER_SEC_DEFAULT;
250  c->intro_dos_burst_per_sec = HS_CONFIG_V3_DOS_DEFENSE_BURST_PER_SEC_DEFAULT;
251 }
252 
253 /** From a service configuration object config, clear everything from it
254  * meaning free allocated pointers and reset the values. */
255 STATIC void
257 {
258  if (config == NULL) {
259  return;
260  }
261  tor_free(config->directory_path);
262  if (config->ports) {
264  rend_service_port_config_free(p););
265  smartlist_free(config->ports);
266  }
267  if (config->clients) {
269  service_authorized_client_free(p));
270  smartlist_free(config->clients);
271  }
272  if (config->ob_master_pubkeys) {
274  tor_free(k));
275  smartlist_free(config->ob_master_pubkeys);
276  }
277  memset(config, 0, sizeof(*config));
278 }
279 
280 /** Helper function to return a human readable description of the given intro
281  * point object.
282  *
283  * This function is not thread-safe. Each call to this invalidates the
284  * previous values returned by it. */
285 static const char *
287 {
288  /* Hex identity digest of the IP prefixed by the $ sign and ends with NUL
289  * byte hence the plus two. */
290  static char buf[HEX_DIGEST_LEN + 2];
291  const char *legacy_id = NULL;
292 
294  const link_specifier_t *, lspec) {
295  if (link_specifier_get_ls_type(lspec) == LS_LEGACY_ID) {
296  legacy_id = (const char *)
297  link_specifier_getconstarray_un_legacy_id(lspec);
298  break;
299  }
300  } SMARTLIST_FOREACH_END(lspec);
301 
302  /* For now, we only print the identity digest but we could improve this with
303  * much more information such as the ed25519 identity has well. */
304  buf[0] = '$';
305  if (legacy_id) {
306  base16_encode(buf + 1, HEX_DIGEST_LEN + 1, legacy_id, DIGEST_LEN);
307  }
308 
309  return buf;
310 }
311 
312 /** Return the lower bound of maximum INTRODUCE2 cells per circuit before we
313  * rotate intro point (defined by a consensus parameter or the default
314  * value). */
315 static int32_t
317 {
318  /* The [0, 2147483647] range is quite large to accommodate anything we decide
319  * in the future. */
320  return networkstatus_get_param(NULL, "hs_intro_min_introduce2",
322  0, INT32_MAX);
323 }
324 
325 /** Return the upper bound of maximum INTRODUCE2 cells per circuit before we
326  * rotate intro point (defined by a consensus parameter or the default
327  * value). */
328 static int32_t
330 {
331  /* The [0, 2147483647] range is quite large to accommodate anything we decide
332  * in the future. */
333  return networkstatus_get_param(NULL, "hs_intro_max_introduce2",
334  INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS,
335  0, INT32_MAX);
336 }
337 
338 /** Return the minimum lifetime in seconds of an introduction point defined by
339  * a consensus parameter or the default value. */
340 static int32_t
342 {
343 #define MIN_INTRO_POINT_LIFETIME_TESTING 10
344  if (get_options()->TestingTorNetwork) {
345  return MIN_INTRO_POINT_LIFETIME_TESTING;
346  }
347 
348  /* The [0, 2147483647] range is quite large to accommodate anything we decide
349  * in the future. */
350  return networkstatus_get_param(NULL, "hs_intro_min_lifetime",
352  0, INT32_MAX);
353 }
354 
355 /** Return the maximum lifetime in seconds of an introduction point defined by
356  * a consensus parameter or the default value. */
357 static int32_t
359 {
360 #define MAX_INTRO_POINT_LIFETIME_TESTING 30
361  if (get_options()->TestingTorNetwork) {
362  return MAX_INTRO_POINT_LIFETIME_TESTING;
363  }
364 
365  /* The [0, 2147483647] range is quite large to accommodate anything we decide
366  * in the future. */
367  return networkstatus_get_param(NULL, "hs_intro_max_lifetime",
369  0, INT32_MAX);
370 }
371 
372 /** Return the number of extra introduction point defined by a consensus
373  * parameter or the default value. */
374 static int32_t
376 {
377  /* The [0, 128] range bounds the number of extra introduction point allowed.
378  * Above 128 intro points, it's getting a bit crazy. */
379  return networkstatus_get_param(NULL, "hs_intro_num_extra",
380  NUM_INTRO_POINTS_EXTRA, 0, 128);
381 }
382 
383 /** Helper: Function that needs to return 1 for the HT for each loop which
384  * frees every service in an hash map. */
385 static int
386 ht_free_service_(struct hs_service_t *service, void *data)
387 {
388  (void) data;
389  hs_service_free(service);
390  /* This function MUST return 1 so the given object is then removed from the
391  * service map leading to this free of the object being safe. */
392  return 1;
393 }
394 
395 /** Free every service that can be found in the global map. Once done, clear
396  * and free the global map. */
397 static void
399 {
400  if (hs_service_map) {
401  /* The free helper function returns 1 so this is safe. */
402  hs_service_ht_HT_FOREACH_FN(hs_service_map, ht_free_service_, NULL);
403  HT_CLEAR(hs_service_ht, hs_service_map);
405  hs_service_map = NULL;
406  }
407 
409  /* Cleanup staging list. */
411  hs_service_free(s));
412  smartlist_free(hs_service_staging_list);
414  }
415 }
416 
417 /** Free a given service intro point object. */
418 STATIC void
420 {
421  if (!ip) {
422  return;
423  }
424  memwipe(&ip->auth_key_kp, 0, sizeof(ip->auth_key_kp));
425  memwipe(&ip->enc_key_kp, 0, sizeof(ip->enc_key_kp));
426  crypto_pk_free(ip->legacy_key);
429  tor_free(ip);
430 }
431 
432 /** Helper: free an hs_service_intro_point_t object. This function is used by
433  * digest256map_free() which requires a void * pointer. */
434 static void
436 {
438 }
439 
440 /** Return a newly allocated service intro point and fully initialized from the
441  * given node_t node, if non NULL.
442  *
443  * If node is NULL, returns a hs_service_intro_point_t with an empty link
444  * specifier list and no onion key. (This is used for testing.)
445  * On any other error, NULL is returned.
446  *
447  * node must be an node_t with an IPv4 address. */
450 {
452 
453  ip = tor_malloc_zero(sizeof(*ip));
454  /* We'll create the key material. No need for extra strong, those are short
455  * term keys. */
457 
458  { /* Set introduce2 max cells limit */
459  int32_t min_introduce2_cells = get_intro_point_min_introduce2();
460  int32_t max_introduce2_cells = get_intro_point_max_introduce2();
461  if (BUG(max_introduce2_cells < min_introduce2_cells)) {
462  goto err;
463  }
464  ip->introduce2_max = crypto_rand_int_range(min_introduce2_cells,
465  max_introduce2_cells);
466  }
467  { /* Set intro point lifetime */
468  int32_t intro_point_min_lifetime = get_intro_point_min_lifetime();
469  int32_t intro_point_max_lifetime = get_intro_point_max_lifetime();
470  if (BUG(intro_point_max_lifetime < intro_point_min_lifetime)) {
471  goto err;
472  }
473  ip->time_to_expire = approx_time() +
474  crypto_rand_int_range(intro_point_min_lifetime,intro_point_max_lifetime);
475  }
476 
477  ip->replay_cache = replaycache_new(0, 0);
478 
479  /* Initialize the base object. We don't need the certificate object. */
480  ip->base.link_specifiers = node_get_link_specifier_smartlist(node, 0);
481 
482  if (node == NULL) {
483  goto done;
484  }
485 
486  /* Generate the encryption key for this intro point. */
488  /* Figure out if this chosen node supports v3 or is legacy only.
489  * NULL nodes are used in the unit tests. */
490  if (!node_supports_ed25519_hs_intro(node)) {
491  ip->base.is_only_legacy = 1;
492  /* Legacy mode that is doesn't support v3+ with ed25519 auth key. */
493  ip->legacy_key = crypto_pk_new();
494  if (crypto_pk_generate_key(ip->legacy_key) < 0) {
495  goto err;
496  }
498  (char *) ip->legacy_key_digest) < 0) {
499  goto err;
500  }
501  }
502 
503  /* Flag if this intro point supports the INTRO2 dos defenses. */
506 
507  /* Finally, copy onion key from the node. */
508  memcpy(&ip->onion_key, node_get_curve25519_onion_key(node),
509  sizeof(ip->onion_key));
510 
511  done:
512  return ip;
513  err:
514  service_intro_point_free(ip);
515  return NULL;
516 }
517 
518 /** Add the given intro point object to the given intro point map. The intro
519  * point MUST have its RSA encryption key set if this is a legacy type or the
520  * authentication key set otherwise. */
521 STATIC void
523 {
524  hs_service_intro_point_t *old_ip_entry;
525 
526  tor_assert(map);
527  tor_assert(ip);
528 
529  old_ip_entry = digest256map_set(map, ip->auth_key_kp.pubkey.pubkey, ip);
530  /* Make sure we didn't just try to double-add an intro point */
531  tor_assert_nonfatal(!old_ip_entry);
532 }
533 
534 /** For a given service, remove the intro point from that service's descriptors
535  * (check both current and next descriptor) */
536 STATIC void
538  const hs_service_intro_point_t *ip)
539 {
540  tor_assert(service);
541  tor_assert(ip);
542 
543  /* Trying all descriptors. */
544  FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
545  /* We'll try to remove the descriptor on both descriptors which is not
546  * very expensive to do instead of doing loopup + remove. */
547  digest256map_remove(desc->intro_points.map,
548  ip->auth_key_kp.pubkey.pubkey);
549  } FOR_EACH_DESCRIPTOR_END;
550 }
551 
552 /** For a given service and authentication key, return the intro point or NULL
553  * if not found. This will check both descriptors in the service. */
556  const ed25519_public_key_t *auth_key)
557 {
558  hs_service_intro_point_t *ip = NULL;
559 
560  tor_assert(service);
561  tor_assert(auth_key);
562 
563  /* Trying all descriptors to find the right intro point.
564  *
565  * Even if we use the same node as intro point in both descriptors, the node
566  * will have a different intro auth key for each descriptor since we generate
567  * a new one everytime we pick an intro point.
568  *
569  * After #22893 gets implemented, intro points will be moved to be
570  * per-service instead of per-descriptor so this function will need to
571  * change.
572  */
573  FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
574  if ((ip = digest256map_get(desc->intro_points.map,
575  auth_key->pubkey)) != NULL) {
576  break;
577  }
578  } FOR_EACH_DESCRIPTOR_END;
579 
580  return ip;
581 }
582 
583 /** For a given service and intro point, return the descriptor for which the
584  * intro point is assigned to. NULL is returned if not found. */
587  const hs_service_intro_point_t *ip)
588 {
589  hs_service_descriptor_t *descp = NULL;
590 
591  tor_assert(service);
592  tor_assert(ip);
593 
594  FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
595  if (digest256map_get(desc->intro_points.map,
596  ip->auth_key_kp.pubkey.pubkey)) {
597  descp = desc;
598  break;
599  }
600  } FOR_EACH_DESCRIPTOR_END;
601 
602  return descp;
603 }
604 
605 /** From a circuit identifier, get all the possible objects associated with the
606  * ident. If not NULL, service, ip or desc are set if the object can be found.
607  * They are untouched if they can't be found.
608  *
609  * This is an helper function because we do those lookups often so it's more
610  * convenient to simply call this functions to get all the things at once. */
611 STATIC void
613  hs_service_t **service, hs_service_intro_point_t **ip,
615 {
616  hs_service_t *s;
617 
618  tor_assert(ident);
619 
620  /* Get service object from the circuit identifier. */
622  if (s && service) {
623  *service = s;
624  }
625 
626  /* From the service object, get the intro point object of that circuit. The
627  * following will query both descriptors intro points list. */
628  if (s && ip) {
629  *ip = service_intro_point_find(s, &ident->intro_auth_pk);
630  }
631 
632  /* Get the descriptor for this introduction point and service. */
633  if (s && ip && *ip && desc) {
634  *desc = service_desc_find_by_intro(s, *ip);
635  }
636 }
637 
638 /** From a given intro point, return the first link specifier of type
639  * encountered in the link specifier list. Return NULL if it can't be found.
640  *
641  * The caller does NOT have ownership of the object, the intro point does. */
642 static link_specifier_t *
644 {
645  link_specifier_t *lnk_spec = NULL;
646 
647  tor_assert(ip);
648 
650  link_specifier_t *, ls) {
651  if (link_specifier_get_ls_type(ls) == type) {
652  lnk_spec = ls;
653  goto end;
654  }
655  } SMARTLIST_FOREACH_END(ls);
656 
657  end:
658  return lnk_spec;
659 }
660 
661 /** Given a service intro point, return the node_t associated to it. This can
662  * return NULL if the given intro point has no legacy ID or if the node can't
663  * be found in the consensus. */
664 STATIC const node_t *
666 {
667  const link_specifier_t *ls;
668 
669  tor_assert(ip);
670 
671  ls = get_link_spec_by_type(ip, LS_LEGACY_ID);
672  if (BUG(!ls)) {
673  return NULL;
674  }
675  /* XXX In the future, we want to only use the ed25519 ID (#22173). */
676  return node_get_by_id(
677  (const char *) link_specifier_getconstarray_un_legacy_id(ls));
678 }
679 
680 /** Given a service intro point, return the extend_info_t for it. This can
681  * return NULL if the node can't be found for the intro point or the extend
682  * info can't be created for the found node. If direct_conn is set, the extend
683  * info is validated on if we can connect directly. */
684 static extend_info_t *
686  unsigned int direct_conn)
687 {
688  extend_info_t *info = NULL;
689  const node_t *node;
690 
691  tor_assert(ip);
692 
693  node = get_node_from_intro_point(ip);
694  if (node == NULL) {
695  /* This can happen if the relay serving as intro point has been removed
696  * from the consensus. In that case, the intro point will be removed from
697  * the descriptor during the scheduled events. */
698  goto end;
699  }
700 
701  /* In the case of a direct connection (single onion service), it is possible
702  * our firewall policy won't allow it so this can return a NULL value. */
703  info = extend_info_from_node(node, direct_conn);
704 
705  end:
706  return info;
707 }
708 
709 /** Return the number of introduction points that are established for the
710  * given descriptor. */
711 MOCK_IMPL(STATIC unsigned int,
713 {
714  unsigned int count = 0;
715 
716  tor_assert(desc);
717 
718  DIGEST256MAP_FOREACH(desc->intro_points.map, key,
719  const hs_service_intro_point_t *, ip) {
721  } DIGEST256MAP_FOREACH_END;
722 
723  return count;
724 }
725 
726 /** For a given service and descriptor of that service, close all active
727  * directory connections. */
728 static void
730  const hs_service_descriptor_t *desc)
731 {
732  unsigned int count = 0;
733  smartlist_t *dir_conns;
734 
735  tor_assert(service);
736  tor_assert(desc);
737 
738  /* Close pending HS desc upload connections for the blinded key of 'desc'. */
739  dir_conns = connection_list_by_type_purpose(CONN_TYPE_DIR,
741  SMARTLIST_FOREACH_BEGIN(dir_conns, connection_t *, conn) {
742  dir_connection_t *dir_conn = TO_DIR_CONN(conn);
743  if (ed25519_pubkey_eq(&dir_conn->hs_ident->identity_pk,
744  &service->keys.identity_pk) &&
745  ed25519_pubkey_eq(&dir_conn->hs_ident->blinded_pk,
746  &desc->blinded_kp.pubkey)) {
747  connection_mark_for_close(conn);
748  count++;
749  continue;
750  }
751  } SMARTLIST_FOREACH_END(conn);
752 
753  log_info(LD_REND, "Closed %u active service directory connections for "
754  "descriptor %s of service %s",
755  count, safe_str_client(ed25519_fmt(&desc->blinded_kp.pubkey)),
756  safe_str_client(service->onion_address));
757  /* We don't have ownership of the objects in this list. */
758  smartlist_free(dir_conns);
759 }
760 
761 /** Close all rendezvous circuits for the given service. */
762 static void
764 {
765  origin_circuit_t *ocirc = NULL;
766 
767  tor_assert(service);
768 
769  /* The reason we go over all circuit instead of using the circuitmap API is
770  * because most hidden service circuits are rendezvous circuits so there is
771  * no real improvement at getting all rendezvous circuits from the
772  * circuitmap and then going over them all to find the right ones.
773  * Furthermore, another option would have been to keep a list of RP cookies
774  * for a service but it creates an engineering complexity since we don't
775  * have a "RP circuit closed" event to clean it up properly so we avoid a
776  * memory DoS possibility. */
777 
778  while ((ocirc = circuit_get_next_service_rp_circ(ocirc))) {
779  /* Only close circuits that are v3 and for this service. */
780  if (ocirc->hs_ident != NULL &&
782  &service->keys.identity_pk)) {
783  /* Reason is FINISHED because service has been removed and thus the
784  * circuit is considered old/uneeded. When freed, it is removed from the
785  * hs circuitmap. */
786  circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
787  }
788  }
789 }
790 
791 /** Close the circuit(s) for the given map of introduction points. */
792 static void
794 {
795  tor_assert(intro_points);
796 
797  DIGEST256MAP_FOREACH(intro_points->map, key,
798  const hs_service_intro_point_t *, ip) {
800  if (ocirc) {
801  /* Reason is FINISHED because service has been removed and thus the
802  * circuit is considered old/uneeded. When freed, the circuit is removed
803  * from the HS circuitmap. */
804  circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
805  }
806  } DIGEST256MAP_FOREACH_END;
807 }
808 
809 /** Close all introduction circuits for the given service. */
810 static void
812 {
813  tor_assert(service);
814 
815  FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
816  close_intro_circuits(&desc->intro_points);
817  } FOR_EACH_DESCRIPTOR_END;
818 }
819 
820 /** Close any circuits related to the given service. */
821 static void
823 {
824  tor_assert(service);
825 
826  /* Only support for version >= 3. */
827  if (BUG(service->config.version < HS_VERSION_THREE)) {
828  return;
829  }
830  /* Close intro points. */
832  /* Close rendezvous points. */
833  close_service_rp_circuits(service);
834 }
835 
836 /** Move every ephemeral services from the src service map to the dst service
837  * map. It is possible that a service can't be register to the dst map which
838  * won't stop the process of moving them all but will trigger a log warn. */
839 static void
840 move_ephemeral_services(hs_service_ht *src, hs_service_ht *dst)
841 {
842  hs_service_t **iter, **next;
843 
844  tor_assert(src);
845  tor_assert(dst);
846 
847  /* Iterate over the map to find ephemeral service and move them to the other
848  * map. We loop using this method to have a safe removal process. */
849  for (iter = HT_START(hs_service_ht, src); iter != NULL; iter = next) {
850  hs_service_t *s = *iter;
851  if (!s->config.is_ephemeral) {
852  /* Yeah, we are in a very manual loop :). */
853  next = HT_NEXT(hs_service_ht, src, iter);
854  continue;
855  }
856  /* Remove service from map and then register to it to the other map.
857  * Reminder that "*iter" and "s" are the same thing. */
858  next = HT_NEXT_RMV(hs_service_ht, src, iter);
859  if (register_service(dst, s) < 0) {
860  log_warn(LD_BUG, "Ephemeral service key is already being used. "
861  "Skipping.");
862  }
863  }
864 }
865 
866 /** Return a const string of the directory path escaped. If this is an
867  * ephemeral service, it returns "[EPHEMERAL]". This can only be called from
868  * the main thread because escaped() uses a static variable. */
869 static const char *
871 {
872  return (s->config.is_ephemeral) ? "[EPHEMERAL]" :
874 }
875 
876 /** Move the hidden service state from <b>src</b> to <b>dst</b>. We do this
877  * when we receive a SIGHUP: <b>dst</b> is the post-HUP service */
878 static void
879 move_hs_state(hs_service_t *src_service, hs_service_t *dst_service)
880 {
881  tor_assert(src_service);
882  tor_assert(dst_service);
883 
884  hs_service_state_t *src = &src_service->state;
885  hs_service_state_t *dst = &dst_service->state;
886 
887  /* Let's do a shallow copy */
890  /* Freeing a NULL replaycache triggers an info LD_BUG. */
891  if (dst->replay_cache_rend_cookie != NULL) {
893  }
894 
896  src->replay_cache_rend_cookie = NULL; /* steal pointer reference */
897 
899 
900  if (src->ob_subcreds) {
901  dst->ob_subcreds = src->ob_subcreds;
902  dst->n_ob_subcreds = src->n_ob_subcreds;
903 
904  src->ob_subcreds = NULL; /* steal pointer reference */
905  }
906 }
907 
908 /** Register services that are in the staging list. Once this function returns,
909  * the global service map will be set with the right content and all non
910  * surviving services will be cleaned up. */
911 static void
913 {
914  struct hs_service_ht *new_service_map;
915 
917 
918  /* Allocate a new map that will replace the current one. */
919  new_service_map = tor_malloc_zero(sizeof(*new_service_map));
920  HT_INIT(hs_service_ht, new_service_map);
921 
922  /* First step is to transfer all ephemeral services from the current global
923  * map to the new one we are constructing. We do not prune ephemeral
924  * services as the only way to kill them is by deleting it from the control
925  * port or stopping the tor daemon. */
926  move_ephemeral_services(hs_service_map, new_service_map);
927 
929  hs_service_t *s;
930 
931  /* Check if that service is already in our global map and if so, we'll
932  * transfer the intro points to it. */
933  s = find_service(hs_service_map, &snew->keys.identity_pk);
934  if (s) {
935  /* Pass ownership of the descriptors from s (the current service) to
936  * snew (the newly configured one). */
937  move_descriptors(s, snew);
938  move_hs_state(s, snew);
939  /* Remove the service from the global map because after this, we need to
940  * go over the remaining service in that map that aren't surviving the
941  * reload to close their circuits. */
943  hs_service_free(s);
944  }
945  /* Great, this service is now ready to be added to our new map. */
946  if (BUG(register_service(new_service_map, snew) < 0)) {
947  /* This should never happen because prior to registration, we validate
948  * every service against the entire set. Not being able to register a
949  * service means we failed to validate correctly. In that case, don't
950  * break tor and ignore the service but tell user. */
951  log_warn(LD_BUG, "Unable to register service with directory %s",
952  service_escaped_dir(snew));
954  hs_service_free(snew);
955  }
956  } SMARTLIST_FOREACH_END(snew);
957 
958  /* Close any circuits associated with the non surviving services. Every
959  * service in the current global map are roaming. */
960  FOR_EACH_SERVICE_BEGIN(service) {
961  close_service_circuits(service);
962  } FOR_EACH_SERVICE_END;
963 
964  /* Time to make the switch. We'll clear the staging list because its content
965  * has now changed ownership to the map. */
968  hs_service_map = new_service_map;
969  /* We've just register services into the new map and now we've replaced the
970  * global map with it so we have to notify that the change happened. When
971  * registering a service, the notify is only triggered if the destination
972  * map is the global map for which in here it was not. */
974 }
975 
976 /** Write the onion address of a given service to the given filename fname_ in
977  * the service directory. Return 0 on success else -1 on error. */
978 STATIC int
979 write_address_to_file(const hs_service_t *service, const char *fname_)
980 {
981  int ret = -1;
982  char *fname = NULL;
983  char *addr_buf = NULL;
984 
985  tor_assert(service);
986  tor_assert(fname_);
987 
988  /* Construct the full address with the onion tld and write the hostname file
989  * to disk. */
990  tor_asprintf(&addr_buf, "%s.%s\n", service->onion_address, address_tld);
991  /* Notice here that we use the given "fname_". */
992  fname = hs_path_from_filename(service->config.directory_path, fname_);
993  if (write_str_to_file(fname, addr_buf, 0) < 0) {
994  log_warn(LD_REND, "Could not write onion address to hostname file %s",
995  escaped(fname));
996  goto end;
997  }
998 
999 #ifndef _WIN32
1000  if (service->config.dir_group_readable) {
1001  /* Mode to 0640. */
1002  if (chmod(fname, S_IRUSR | S_IWUSR | S_IRGRP) < 0) {
1003  log_warn(LD_FS, "Unable to make onion service hostname file %s "
1004  "group-readable.", escaped(fname));
1005  }
1006  }
1007 #endif /* !defined(_WIN32) */
1008 
1009  /* Success. */
1010  ret = 0;
1011  end:
1012  tor_free(fname);
1013  tor_free(addr_buf);
1014  return ret;
1015 }
1016 
1017 /** Load and/or generate private keys for the given service. On success, the
1018  * hostname file will be written to disk along with the master private key iff
1019  * the service is not configured for offline keys. Return 0 on success else -1
1020  * on failure. */
1021 static int
1023 {
1024  int ret = -1;
1025  char *fname = NULL;
1026  ed25519_keypair_t *kp;
1027  const hs_service_config_t *config;
1028 
1029  tor_assert(service);
1030 
1031  config = &service->config;
1032 
1033  /* Create and fix permission on service directory. We are about to write
1034  * files to that directory so make sure it exists and has the right
1035  * permissions. We do this here because at this stage we know that Tor is
1036  * actually running and the service we have has been validated. */
1038  config->directory_path,
1039  config->dir_group_readable, 1) < 0) {
1040  goto end;
1041  }
1042 
1043  /* Try to load the keys from file or generate it if not found. */
1044  fname = hs_path_from_filename(config->directory_path, fname_keyfile_prefix);
1045  /* Don't ask for key creation, we want to know if we were able to load it or
1046  * we had to generate it. Better logging! */
1047  kp = ed_key_init_from_file(fname, INIT_ED_KEY_SPLIT, LOG_INFO, NULL, 0, 0,
1048  0, NULL, NULL);
1049  if (!kp) {
1050  log_info(LD_REND, "Unable to load keys from %s. Generating it...", fname);
1051  /* We'll now try to generate the keys and for it we want the strongest
1052  * randomness for it. The keypair will be written in different files. */
1053  uint32_t key_flags = INIT_ED_KEY_CREATE | INIT_ED_KEY_EXTRA_STRONG |
1054  INIT_ED_KEY_SPLIT;
1055  kp = ed_key_init_from_file(fname, key_flags, LOG_WARN, NULL, 0, 0, 0,
1056  NULL, NULL);
1057  if (!kp) {
1058  log_warn(LD_REND, "Unable to generate keys and save in %s.", fname);
1059  goto end;
1060  }
1061  }
1062 
1063  /* Copy loaded or generated keys to service object. */
1064  ed25519_pubkey_copy(&service->keys.identity_pk, &kp->pubkey);
1065  memcpy(&service->keys.identity_sk, &kp->seckey,
1066  sizeof(service->keys.identity_sk));
1067  /* This does a proper memory wipe. */
1068  ed25519_keypair_free(kp);
1069 
1070  /* Build onion address from the newly loaded keys. */
1071  tor_assert(service->config.version <= UINT8_MAX);
1072  hs_build_address(&service->keys.identity_pk,
1073  (uint8_t) service->config.version,
1074  service->onion_address);
1075 
1076  /* Write onion address to hostname file. */
1077  if (write_address_to_file(service, fname_hostname) < 0) {
1078  goto end;
1079  }
1080 
1081  /* Load all client authorization keys in the service. */
1082  if (load_client_keys(service) < 0) {
1083  goto end;
1084  }
1085 
1086  /* Succes. */
1087  ret = 0;
1088  end:
1089  tor_free(fname);
1090  return ret;
1091 }
1092 
1093 /** Check if the client file name is valid or not. Return 1 if valid,
1094  * otherwise return 0. */
1095 STATIC int
1096 client_filename_is_valid(const char *filename)
1097 {
1098  int ret = 1;
1099  const char *valid_extension = ".auth";
1100 
1101  tor_assert(filename);
1102 
1103  /* The file extension must match and the total filename length can't be the
1104  * length of the extension else we do not have a filename. */
1105  if (!strcmpend(filename, valid_extension) &&
1106  strlen(filename) != strlen(valid_extension)) {
1107  ret = 1;
1108  } else {
1109  ret = 0;
1110  }
1111 
1112  return ret;
1113 }
1114 
1115 /** Parse an authorized client from a string. The format of a client string
1116  * looks like (see rend-spec-v3.txt):
1117  *
1118  * <auth-type>:<key-type>:<base32-encoded-public-key>
1119  *
1120  * The <auth-type> can only be "descriptor".
1121  * The <key-type> can only be "x25519".
1122  *
1123  * Return the key on success, return NULL, otherwise. */
1125 parse_authorized_client(const char *client_key_str)
1126 {
1127  char *auth_type = NULL;
1128  char *key_type = NULL;
1129  char *pubkey_b32 = NULL;
1130  hs_service_authorized_client_t *client = NULL;
1131  smartlist_t *fields = smartlist_new();
1132 
1133  tor_assert(client_key_str);
1134 
1135  smartlist_split_string(fields, client_key_str, ":",
1136  SPLIT_SKIP_SPACE, 0);
1137  /* Wrong number of fields. */
1138  if (smartlist_len(fields) != 3) {
1139  log_warn(LD_REND, "Unknown format of client authorization file.");
1140  goto err;
1141  }
1142 
1143  auth_type = smartlist_get(fields, 0);
1144  key_type = smartlist_get(fields, 1);
1145  pubkey_b32 = smartlist_get(fields, 2);
1146 
1147  /* Currently, the only supported auth type is "descriptor". */
1148  if (strcmp(auth_type, "descriptor")) {
1149  log_warn(LD_REND, "Client authorization auth type '%s' not supported.",
1150  auth_type);
1151  goto err;
1152  }
1153 
1154  /* Currently, the only supported key type is "x25519". */
1155  if (strcmp(key_type, "x25519")) {
1156  log_warn(LD_REND, "Client authorization key type '%s' not supported.",
1157  key_type);
1158  goto err;
1159  }
1160 
1161  /* We expect a specific length of the base32 encoded key so make sure we
1162  * have that so we don't successfully decode a value with a different length
1163  * and end up in trouble when copying the decoded key into a fixed length
1164  * buffer. */
1165  if (strlen(pubkey_b32) != BASE32_NOPAD_LEN(CURVE25519_PUBKEY_LEN)) {
1166  log_warn(LD_REND, "Client authorization encoded base32 public key "
1167  "length is invalid: %s", pubkey_b32);
1168  goto err;
1169  }
1170 
1171  client = tor_malloc_zero(sizeof(hs_service_authorized_client_t));
1172  if (base32_decode((char *) client->client_pk.public_key,
1173  sizeof(client->client_pk.public_key),
1174  pubkey_b32, strlen(pubkey_b32)) !=
1175  sizeof(client->client_pk.public_key)) {
1176  log_warn(LD_REND, "Client authorization public key cannot be decoded: %s",
1177  pubkey_b32);
1178  goto err;
1179  }
1180 
1181  /* Success. */
1182  goto done;
1183 
1184  err:
1185  service_authorized_client_free(client);
1186  done:
1187  /* It is also a good idea to wipe the public key. */
1188  if (pubkey_b32) {
1189  memwipe(pubkey_b32, 0, strlen(pubkey_b32));
1190  }
1191  tor_assert(fields);
1192  SMARTLIST_FOREACH(fields, char *, s, tor_free(s));
1193  smartlist_free(fields);
1194  return client;
1195 }
1196 
1197 /** Load all the client public keys for the given service. Return 0 on
1198  * success else -1 on failure. */
1199 static int
1201 {
1202  int ret = -1;
1203  char *client_key_str = NULL;
1204  char *client_key_file_path = NULL;
1205  char *client_keys_dir_path = NULL;
1206  hs_service_config_t *config;
1207  smartlist_t *file_list = NULL;
1208 
1209  tor_assert(service);
1210 
1211  config = &service->config;
1212 
1213  /* Before calling this function, we already call load_service_keys to make
1214  * sure that the directory exists with the right permission. So, if we
1215  * cannot create a client pubkey key directory, we consider it as a bug. */
1216  client_keys_dir_path = hs_path_from_filename(config->directory_path,
1217  dname_client_pubkeys);
1218  if (BUG(hs_check_service_private_dir(get_options()->User,
1219  client_keys_dir_path,
1220  config->dir_group_readable, 1) < 0)) {
1221  goto end;
1222  }
1223 
1224  /* If the list of clients already exists, we must clear it first. */
1225  if (config->clients) {
1227  service_authorized_client_free(p));
1228  smartlist_free(config->clients);
1229  }
1230 
1231  config->clients = smartlist_new();
1232 
1233  file_list = tor_listdir(client_keys_dir_path);
1234  if (file_list == NULL) {
1235  log_warn(LD_REND, "Client authorization directory %s can't be listed.",
1236  client_keys_dir_path);
1237  goto end;
1238  }
1239 
1240  SMARTLIST_FOREACH_BEGIN(file_list, const char *, filename) {
1241  hs_service_authorized_client_t *client = NULL;
1242  log_info(LD_REND, "Loading a client authorization key file %s...",
1243  filename);
1244 
1245  if (!client_filename_is_valid(filename)) {
1246  log_warn(LD_REND, "Client authorization unrecognized filename %s. "
1247  "File must end in .auth. Ignoring.", filename);
1248  continue;
1249  }
1250 
1251  /* Create a full path for a file. */
1252  client_key_file_path = hs_path_from_filename(client_keys_dir_path,
1253  filename);
1254  client_key_str = read_file_to_str(client_key_file_path, 0, NULL);
1255 
1256  /* If we cannot read the file, continue with the next file. */
1257  if (!client_key_str) {
1258  log_warn(LD_REND, "Client authorization file %s can't be read. "
1259  "Corrupted or verify permission? Ignoring.",
1260  client_key_file_path);
1261  tor_free(client_key_file_path);
1262  continue;
1263  }
1264  tor_free(client_key_file_path);
1265 
1266  client = parse_authorized_client(client_key_str);
1267  /* Wipe and free immediately after using it. */
1268  memwipe(client_key_str, 0, strlen(client_key_str));
1269  tor_free(client_key_str);
1270 
1271  if (client) {
1272  smartlist_add(config->clients, client);
1273  log_info(LD_REND, "Loaded a client authorization key file %s.",
1274  filename);
1275  }
1276 
1277  } SMARTLIST_FOREACH_END(filename);
1278 
1279  /* If the number of clients is greater than zero, set the flag to be true. */
1280  if (smartlist_len(config->clients) > 0) {
1281  config->is_client_auth_enabled = 1;
1282  }
1283 
1284  /* Success. */
1285  ret = 0;
1286  end:
1287  if (client_key_str) {
1288  memwipe(client_key_str, 0, strlen(client_key_str));
1289  }
1290  if (file_list) {
1291  SMARTLIST_FOREACH(file_list, char *, s, tor_free(s));
1292  smartlist_free(file_list);
1293  }
1294  tor_free(client_key_str);
1295  tor_free(client_key_file_path);
1296  tor_free(client_keys_dir_path);
1297  return ret;
1298 }
1299 
1300 /** Release all storage held in <b>client</b>. */
1301 STATIC void
1303 {
1304  if (!client) {
1305  return;
1306  }
1307  memwipe(&client->client_pk, 0, sizeof(client->client_pk));
1308  tor_free(client);
1309 }
1310 
1311 /** Free a given service descriptor object and all key material is wiped. */
1312 STATIC void
1314 {
1315  if (!desc) {
1316  return;
1317  }
1318  hs_descriptor_free(desc->desc);
1319  memwipe(&desc->signing_kp, 0, sizeof(desc->signing_kp));
1320  memwipe(&desc->blinded_kp, 0, sizeof(desc->blinded_kp));
1321  /* Cleanup all intro points. */
1322  digest256map_free(desc->intro_points.map, service_intro_point_free_void);
1323  digestmap_free(desc->intro_points.failed_id, tor_free_);
1324  if (desc->previous_hsdirs) {
1325  SMARTLIST_FOREACH(desc->previous_hsdirs, char *, s, tor_free(s));
1326  smartlist_free(desc->previous_hsdirs);
1327  }
1328  crypto_ope_free(desc->ope_cipher);
1329  tor_free(desc);
1330 }
1331 
1332 /** Return a newly allocated service descriptor object. */
1335 {
1336  hs_service_descriptor_t *sdesc = tor_malloc_zero(sizeof(*sdesc));
1337  sdesc->desc = tor_malloc_zero(sizeof(hs_descriptor_t));
1338  /* Initialize the intro points map. */
1339  sdesc->intro_points.map = digest256map_new();
1340  sdesc->intro_points.failed_id = digestmap_new();
1341  sdesc->previous_hsdirs = smartlist_new();
1342  return sdesc;
1343 }
1344 
1345 /** Allocate and return a deep copy of client. */
1348 {
1349  hs_service_authorized_client_t *client_dup = NULL;
1350 
1351  tor_assert(client);
1352 
1353  client_dup = tor_malloc_zero(sizeof(hs_service_authorized_client_t));
1354  /* Currently, the public key is the only component of
1355  * hs_service_authorized_client_t. */
1356  memcpy(client_dup->client_pk.public_key,
1357  client->client_pk.public_key,
1359 
1360  return client_dup;
1361 }
1362 
1363 /** If two authorized clients are equal, return 0. If the first one should come
1364  * before the second, return less than zero. If the first should come after
1365  * the second, return greater than zero. */
1366 static int
1368  const hs_service_authorized_client_t *client2)
1369 {
1370  tor_assert(client1);
1371  tor_assert(client2);
1372 
1373  /* Currently, the public key is the only component of
1374  * hs_service_authorized_client_t. */
1375  return tor_memcmp(client1->client_pk.public_key,
1376  client2->client_pk.public_key,
1378 }
1379 
1380 /** Helper for sorting authorized clients. */
1381 static int
1382 compare_service_authorzized_client_(const void **_a, const void **_b)
1383 {
1384  const hs_service_authorized_client_t *a = *_a, *b = *_b;
1385  return service_authorized_client_cmp(a, b);
1386 }
1387 
1388 /** If the list of hs_service_authorized_client_t's is different between
1389  * src and dst, return 1. Otherwise, return 0. */
1390 STATIC int
1392  const hs_service_config_t *config2)
1393 {
1394  int ret = 0;
1395  int i;
1396  smartlist_t *sl1 = smartlist_new();
1397  smartlist_t *sl2 = smartlist_new();
1398 
1399  tor_assert(config1);
1400  tor_assert(config2);
1401  tor_assert(config1->clients);
1402  tor_assert(config2->clients);
1403 
1404  /* If the number of clients is different, it is obvious that the list
1405  * changes. */
1406  if (smartlist_len(config1->clients) != smartlist_len(config2->clients)) {
1407  goto done;
1408  }
1409 
1410  /* We do not want to mutate config1 and config2, so we will duplicate both
1411  * entire client lists here. */
1412  SMARTLIST_FOREACH(config1->clients,
1415 
1416  SMARTLIST_FOREACH(config2->clients,
1419 
1422 
1423  for (i = 0; i < smartlist_len(sl1); i++) {
1424  /* If the clients at index i in both lists differ, the whole configs
1425  * differ. */
1426  if (service_authorized_client_cmp(smartlist_get(sl1, i),
1427  smartlist_get(sl2, i))) {
1428  goto done;
1429  }
1430  }
1431 
1432  /* Success. */
1433  ret = 1;
1434 
1435  done:
1436  if (sl1) {
1438  service_authorized_client_free(p));
1439  smartlist_free(sl1);
1440  }
1441  if (sl2) {
1443  service_authorized_client_free(p));
1444  smartlist_free(sl2);
1445  }
1446  return ret;
1447 }
1448 
1449 /** Move descriptor(s) from the src service to the dst service and modify their
1450  * content if necessary. We do this during SIGHUP when we re-create our
1451  * hidden services. */
1452 static void
1454 {
1455  tor_assert(src);
1456  tor_assert(dst);
1457 
1458  if (src->desc_current) {
1459  /* Nothing should be there, but clean it up just in case */
1460  if (BUG(dst->desc_current)) {
1461  service_descriptor_free(dst->desc_current);
1462  }
1463  dst->desc_current = src->desc_current;
1464  src->desc_current = NULL;
1465  }
1466 
1467  if (src->desc_next) {
1468  /* Nothing should be there, but clean it up just in case */
1469  if (BUG(dst->desc_next)) {
1470  service_descriptor_free(dst->desc_next);
1471  }
1472  dst->desc_next = src->desc_next;
1473  src->desc_next = NULL;
1474  }
1475 
1476  /* If the client authorization changes, we must rebuild the superencrypted
1477  * section and republish the descriptors. */
1478  int client_auth_changed =
1480  if (client_auth_changed && dst->desc_current) {
1481  /* We have to clear the superencrypted content first. */
1484  if (build_service_desc_superencrypted(dst, dst->desc_current) < 0) {
1485  goto err;
1486  }
1487  service_desc_schedule_upload(dst->desc_current, time(NULL), 1);
1488  }
1489  if (client_auth_changed && dst->desc_next) {
1490  /* We have to clear the superencrypted content first. */
1493  if (build_service_desc_superencrypted(dst, dst->desc_next) < 0) {
1494  goto err;
1495  }
1496  service_desc_schedule_upload(dst->desc_next, time(NULL), 1);
1497  }
1498 
1499  return;
1500 
1501  err:
1502  /* If there is an error, free all descriptors to make it clean and generate
1503  * them later. */
1504  service_descriptor_free(dst->desc_current);
1505  service_descriptor_free(dst->desc_next);
1506 }
1507 
1508 /** From the given service, remove all expired failing intro points for each
1509  * descriptor. */
1510 static void
1512 {
1513  tor_assert(service);
1514 
1515  /* For both descriptors, cleanup the failing intro points list. */
1516  FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
1517  DIGESTMAP_FOREACH_MODIFY(desc->intro_points.failed_id, key, time_t *, t) {
1518  time_t failure_time = *t;
1519  if ((failure_time + INTRO_CIRC_RETRY_PERIOD) <= now) {
1520  MAP_DEL_CURRENT(key);
1521  tor_free(t);
1522  }
1524  } FOR_EACH_DESCRIPTOR_END;
1525 }
1526 
1527 /** For the given descriptor desc, put all node_t object found from its failing
1528  * intro point list and put them in the given node_list. */
1529 static void
1531  smartlist_t *node_list)
1532 {
1533  tor_assert(desc);
1534  tor_assert(node_list);
1535 
1536  DIGESTMAP_FOREACH(desc->intro_points.failed_id, key, time_t *, t) {
1537  (void) t; /* Make gcc happy. */
1538  const node_t *node = node_get_by_id(key);
1539  if (node) {
1540  smartlist_add(node_list, (void *) node);
1541  }
1543 }
1544 
1545 /** For the given failing intro point ip, we add its time of failure to the
1546  * failed map and index it by identity digest (legacy ID) in the descriptor
1547  * desc failed id map. */
1548 static void
1550  hs_service_descriptor_t *desc, time_t now)
1551 {
1552  time_t *time_of_failure, *prev_ptr;
1553  const link_specifier_t *legacy_ls;
1554 
1555  tor_assert(ip);
1556  tor_assert(desc);
1557 
1558  time_of_failure = tor_malloc_zero(sizeof(time_t));
1559  *time_of_failure = now;
1560  legacy_ls = get_link_spec_by_type(ip, LS_LEGACY_ID);
1561  tor_assert(legacy_ls);
1562  prev_ptr = digestmap_set(
1563  desc->intro_points.failed_id,
1564  (const char *) link_specifier_getconstarray_un_legacy_id(legacy_ls),
1565  time_of_failure);
1566  tor_free(prev_ptr);
1567 }
1568 
1569 /** Using a given descriptor signing keypair signing_kp, a service intro point
1570  * object ip and the time now, setup the content of an already allocated
1571  * descriptor intro desc_ip.
1572  *
1573  * Return 0 on success else a negative value. */
1574 static int
1576  const hs_service_intro_point_t *ip,
1577  time_t now, hs_desc_intro_point_t *desc_ip)
1578 {
1579  int ret = -1;
1580  time_t nearest_hour = now - (now % 3600);
1581 
1582  tor_assert(signing_kp);
1583  tor_assert(ip);
1584  tor_assert(desc_ip);
1585 
1586  /* Copy the onion key. */
1587  memcpy(&desc_ip->onion_key, &ip->onion_key, sizeof(desc_ip->onion_key));
1588 
1589  /* Key and certificate material. */
1590  desc_ip->auth_key_cert = tor_cert_create(signing_kp,
1591  CERT_TYPE_AUTH_HS_IP_KEY,
1592  &ip->auth_key_kp.pubkey,
1593  nearest_hour,
1595  CERT_FLAG_INCLUDE_SIGNING_KEY);
1596  if (desc_ip->auth_key_cert == NULL) {
1597  log_warn(LD_REND, "Unable to create intro point auth-key certificate");
1598  goto done;
1599  }
1600 
1601  /* Copy link specifier(s). */
1603  const link_specifier_t *, ls) {
1604  if (BUG(!ls)) {
1605  goto done;
1606  }
1607  link_specifier_t *copy = link_specifier_dup(ls);
1608  if (BUG(!copy)) {
1609  goto done;
1610  }
1611  smartlist_add(desc_ip->link_specifiers, copy);
1612  } SMARTLIST_FOREACH_END(ls);
1613 
1614  /* For a legacy intro point, we'll use an RSA/ed cross certificate. */
1615  if (ip->base.is_only_legacy) {
1616  desc_ip->legacy.key = crypto_pk_dup_key(ip->legacy_key);
1617  /* Create cross certification cert. */
1618  ssize_t cert_len = tor_make_rsa_ed25519_crosscert(
1619  &signing_kp->pubkey,
1620  desc_ip->legacy.key,
1621  nearest_hour + HS_DESC_CERT_LIFETIME,
1622  &desc_ip->legacy.cert.encoded);
1623  if (cert_len < 0) {
1624  log_warn(LD_REND, "Unable to create enc key legacy cross cert.");
1625  goto done;
1626  }
1627  desc_ip->legacy.cert.len = cert_len;
1628  }
1629 
1630  /* Encryption key and its cross certificate. */
1631  {
1632  ed25519_public_key_t ed25519_pubkey;
1633 
1634  /* Use the public curve25519 key. */
1635  memcpy(&desc_ip->enc_key, &ip->enc_key_kp.pubkey,
1636  sizeof(desc_ip->enc_key));
1637  /* The following can't fail. */
1639  &ip->enc_key_kp.pubkey,
1640  0);
1641  desc_ip->enc_key_cert = tor_cert_create(signing_kp,
1642  CERT_TYPE_CROSS_HS_IP_KEYS,
1643  &ed25519_pubkey, nearest_hour,
1645  CERT_FLAG_INCLUDE_SIGNING_KEY);
1646  if (desc_ip->enc_key_cert == NULL) {
1647  log_warn(LD_REND, "Unable to create enc key curve25519 cross cert.");
1648  goto done;
1649  }
1650  }
1651  /* Success. */
1652  ret = 0;
1653 
1654  done:
1655  return ret;
1656 }
1657 
1658 /** Using the given descriptor from the given service, build the descriptor
1659  * intro point list so we can then encode the descriptor for publication. This
1660  * function does not pick intro points, they have to be in the descriptor
1661  * current map. Cryptographic material (keys) must be initialized in the
1662  * descriptor for this function to make sense. */
1663 static void
1665  hs_service_descriptor_t *desc, time_t now)
1666 {
1667  hs_desc_encrypted_data_t *encrypted;
1668 
1669  tor_assert(service);
1670  tor_assert(desc);
1671 
1672  /* Ease our life. */
1673  encrypted = &desc->desc->encrypted_data;
1674  /* Cleanup intro points, we are about to set them from scratch. */
1676 
1677  DIGEST256MAP_FOREACH(desc->intro_points.map, key,
1678  const hs_service_intro_point_t *, ip) {
1680  /* Ignore un-established intro points. They can linger in that list
1681  * because their circuit has not opened and they haven't been removed
1682  * yet even though we have enough intro circuits.
1683  *
1684  * Due to #31561, it can stay in that list until rotation so this check
1685  * prevents to publish an intro point without a circuit. */
1686  continue;
1687  }
1689  if (setup_desc_intro_point(&desc->signing_kp, ip, now, desc_ip) < 0) {
1690  hs_desc_intro_point_free(desc_ip);
1691  continue;
1692  }
1693  /* We have a valid descriptor intro point. Add it to the list. */
1694  smartlist_add(encrypted->intro_points, desc_ip);
1695  } DIGEST256MAP_FOREACH_END;
1696 }
1697 
1698 /** Build the descriptor signing key certificate. */
1699 static void
1701 {
1702  hs_desc_plaintext_data_t *plaintext;
1703 
1704  tor_assert(desc);
1705  tor_assert(desc->desc);
1706 
1707  /* Ease our life a bit. */
1708  plaintext = &desc->desc->plaintext_data;
1709 
1710  /* Get rid of what we have right now. */
1711  tor_cert_free(plaintext->signing_key_cert);
1712 
1713  /* Fresh certificate for the signing key. */
1714  plaintext->signing_key_cert =
1715  tor_cert_create(&desc->blinded_kp, CERT_TYPE_SIGNING_HS_DESC,
1716  &desc->signing_kp.pubkey, now, HS_DESC_CERT_LIFETIME,
1717  CERT_FLAG_INCLUDE_SIGNING_KEY);
1718  /* If the cert creation fails, the descriptor encoding will fail and thus
1719  * ultimately won't be uploaded. We'll get a stack trace to help us learn
1720  * where the call came from and the tor_cert_create() will log the error. */
1721  tor_assert_nonfatal(plaintext->signing_key_cert);
1722 }
1723 
1724 /** Populate the descriptor encrypted section from the given service object.
1725  * This will generate a valid list of introduction points that can be used
1726  * after for circuit creation. Return 0 on success else -1 on error. */
1727 static int
1730 {
1731  hs_desc_encrypted_data_t *encrypted;
1732 
1733  tor_assert(service);
1734  tor_assert(desc);
1735 
1736  encrypted = &desc->desc->encrypted_data;
1737 
1738  encrypted->create2_ntor = 1;
1739  encrypted->single_onion_service = service->config.is_single_onion;
1740 
1741  /* Setup introduction points from what we have in the service. */
1742  if (encrypted->intro_points == NULL) {
1743  encrypted->intro_points = smartlist_new();
1744  }
1745  /* We do NOT build introduction point yet, we only do that once the circuit
1746  * have been opened. Until we have the right number of introduction points,
1747  * we do not encode anything in the descriptor. */
1748 
1749  /* XXX: Support client authorization (#20700). */
1750  encrypted->intro_auth_types = NULL;
1751  return 0;
1752 }
1753 
1754 /** Populate the descriptor superencrypted section from the given service
1755  * object. This will generate a valid list of hs_desc_authorized_client_t
1756  * of clients that are authorized to use the service. Return 0 on success
1757  * else -1 on error. */
1758 static int
1761 {
1762  const hs_service_config_t *config;
1763  int i;
1764  hs_desc_superencrypted_data_t *superencrypted;
1765 
1766  tor_assert(service);
1767  tor_assert(desc);
1768 
1769  superencrypted = &desc->desc->superencrypted_data;
1770  config = &service->config;
1771 
1772  /* The ephemeral key pair is already generated, so this should not give
1773  * an error. */
1774  if (BUG(!curve25519_public_key_is_ok(&desc->auth_ephemeral_kp.pubkey))) {
1775  return -1;
1776  }
1777  memcpy(&superencrypted->auth_ephemeral_pubkey,
1778  &desc->auth_ephemeral_kp.pubkey,
1779  sizeof(curve25519_public_key_t));
1780 
1781  /* Test that subcred is not zero because we might use it below */
1782  if (BUG(fast_mem_is_zero((char*)desc->desc->subcredential.subcred,
1783  DIGEST256_LEN))) {
1784  return -1;
1785  }
1786 
1787  /* Create a smartlist to store clients */
1788  superencrypted->clients = smartlist_new();
1789 
1790  /* We do not need to build the desc authorized client if the client
1791  * authorization is disabled */
1792  if (config->is_client_auth_enabled) {
1794  hs_service_authorized_client_t *, client) {
1795  hs_desc_authorized_client_t *desc_client;
1796  desc_client = tor_malloc_zero(sizeof(hs_desc_authorized_client_t));
1797 
1798  /* Prepare the client for descriptor and then add to the list in the
1799  * superencrypted part of the descriptor */
1801  &client->client_pk,
1802  &desc->auth_ephemeral_kp.seckey,
1803  desc->descriptor_cookie, desc_client);
1804  smartlist_add(superencrypted->clients, desc_client);
1805 
1806  } SMARTLIST_FOREACH_END(client);
1807  }
1808 
1809  /* We cannot let the number of auth-clients to be zero, so we need to
1810  * make it be 16. If it is already a multiple of 16, we do not need to
1811  * do anything. Otherwise, add the additional ones to make it a
1812  * multiple of 16. */
1813  int num_clients = smartlist_len(superencrypted->clients);
1814  int num_clients_to_add;
1815  if (num_clients == 0) {
1816  num_clients_to_add = HS_DESC_AUTH_CLIENT_MULTIPLE;
1817  } else if (num_clients % HS_DESC_AUTH_CLIENT_MULTIPLE == 0) {
1818  num_clients_to_add = 0;
1819  } else {
1820  num_clients_to_add =
1822  - (num_clients % HS_DESC_AUTH_CLIENT_MULTIPLE);
1823  }
1824 
1825  for (i = 0; i < num_clients_to_add; i++) {
1826  hs_desc_authorized_client_t *desc_client =
1828  smartlist_add(superencrypted->clients, desc_client);
1829  }
1830 
1831  /* Shuffle the list to prevent the client know the position in the
1832  * config. */
1833  smartlist_shuffle(superencrypted->clients);
1834 
1835  return 0;
1836 }
1837 
1838 /** Populate the descriptor plaintext section from the given service object.
1839  * The caller must make sure that the keys in the descriptors are valid that
1840  * is are non-zero. This can't fail. */
1841 static void
1844 {
1845  hs_desc_plaintext_data_t *plaintext;
1846 
1847  tor_assert(service);
1848  tor_assert(desc);
1849  tor_assert(!fast_mem_is_zero((char *) &desc->blinded_kp,
1850  sizeof(desc->blinded_kp)));
1851  tor_assert(!fast_mem_is_zero((char *) &desc->signing_kp,
1852  sizeof(desc->signing_kp)));
1853 
1854  /* Set the subcredential. */
1855  hs_get_subcredential(&service->keys.identity_pk, &desc->blinded_kp.pubkey,
1856  &desc->desc->subcredential);
1857 
1858  plaintext = &desc->desc->plaintext_data;
1859 
1860  plaintext->version = service->config.version;
1862  /* Copy public key material to go in the descriptor. */
1863  ed25519_pubkey_copy(&plaintext->signing_pubkey, &desc->signing_kp.pubkey);
1864  ed25519_pubkey_copy(&plaintext->blinded_pubkey, &desc->blinded_kp.pubkey);
1865 
1866  /* Create the signing key certificate. This will be updated before each
1867  * upload but we create it here so we don't complexify our unit tests. */
1869 }
1870 
1871 /** Compute the descriptor's OPE cipher for encrypting revision counters. */
1872 static crypto_ope_t *
1874 {
1875  /* Compute OPE key as H("rev-counter-generation" | blinded privkey) */
1876  uint8_t key[DIGEST256_LEN];
1877  crypto_digest_t *digest = crypto_digest256_new(DIGEST_SHA3_256);
1878  const char ope_key_prefix[] = "rev-counter-generation";
1879  const ed25519_secret_key_t *eph_privkey = &hs_desc->blinded_kp.seckey;
1880  crypto_digest_add_bytes(digest, ope_key_prefix, sizeof(ope_key_prefix));
1881  crypto_digest_add_bytes(digest, (char*)eph_privkey->seckey,
1882  sizeof(eph_privkey->seckey));
1883  crypto_digest_get_digest(digest, (char *)key, sizeof(key));
1884  crypto_digest_free(digest);
1885 
1886  return crypto_ope_new(key);
1887 }
1888 
1889 /** For the given service and descriptor object, create the key material which
1890  * is the blinded keypair, the descriptor signing keypair, the ephemeral
1891  * keypair, and the descriptor cookie. Return 0 on success else -1 on error
1892  * where the generated keys MUST be ignored. */
1893 static int
1896 {
1897  int ret = -1;
1898  ed25519_keypair_t kp;
1899 
1900  tor_assert(desc);
1901  tor_assert(!fast_mem_is_zero((char *) &service->keys.identity_pk,
1903 
1904  /* XXX: Support offline key feature (#18098). */
1905 
1906  /* Copy the identity keys to the keypair so we can use it to create the
1907  * blinded key. */
1908  memcpy(&kp.pubkey, &service->keys.identity_pk, sizeof(kp.pubkey));
1909  memcpy(&kp.seckey, &service->keys.identity_sk, sizeof(kp.seckey));
1910  /* Build blinded keypair for this time period. */
1911  hs_build_blinded_keypair(&kp, NULL, 0, desc->time_period_num,
1912  &desc->blinded_kp);
1913  /* Let's not keep too much traces of our keys in memory. */
1914  memwipe(&kp, 0, sizeof(kp));
1915 
1916  /* Compute the OPE cipher struct (it's tied to the current blinded key) */
1917  log_info(LD_GENERAL,
1918  "Getting OPE for TP#%u", (unsigned) desc->time_period_num);
1919  tor_assert_nonfatal(!desc->ope_cipher);
1921 
1922  /* No need for extra strong, this is a temporary key only for this
1923  * descriptor. Nothing long term. */
1924  if (ed25519_keypair_generate(&desc->signing_kp, 0) < 0) {
1925  log_warn(LD_REND, "Can't generate descriptor signing keypair for "
1926  "service %s",
1927  safe_str_client(service->onion_address));
1928  goto end;
1929  }
1930 
1931  /* No need for extra strong, this is a temporary key only for this
1932  * descriptor. Nothing long term. */
1933  if (curve25519_keypair_generate(&desc->auth_ephemeral_kp, 0) < 0) {
1934  log_warn(LD_REND, "Can't generate auth ephemeral keypair for "
1935  "service %s",
1936  safe_str_client(service->onion_address));
1937  goto end;
1938  }
1939 
1940  /* Random descriptor cookie to be used as a part of a key to encrypt the
1941  * descriptor, only if the client auth is enabled will it be used. */
1943  sizeof(desc->descriptor_cookie));
1944 
1945  /* Success. */
1946  ret = 0;
1947  end:
1948  return ret;
1949 }
1950 
1951 /** Given a service and the current time, build a descriptor for the service.
1952  * This function does not pick introduction point, this needs to be done by
1953  * the update function. On success, desc_out will point to the newly allocated
1954  * descriptor object.
1955  *
1956  * This can error if we are unable to create keys or certificate. */
1957 static void
1958 build_service_descriptor(hs_service_t *service, uint64_t time_period_num,
1959  hs_service_descriptor_t **desc_out)
1960 {
1961  char *encoded_desc;
1963 
1964  tor_assert(service);
1965  tor_assert(desc_out);
1966 
1967  desc = service_descriptor_new();
1968 
1969  /* Set current time period */
1970  desc->time_period_num = time_period_num;
1971 
1972  /* Create the needed keys so we can setup the descriptor content. */
1973  if (build_service_desc_keys(service, desc) < 0) {
1974  goto err;
1975  }
1976  /* Setup plaintext descriptor content. */
1977  build_service_desc_plaintext(service, desc);
1978 
1979  /* Setup superencrypted descriptor content. */
1980  if (build_service_desc_superencrypted(service, desc) < 0) {
1981  goto err;
1982  }
1983  /* Setup encrypted descriptor content. */
1984  if (build_service_desc_encrypted(service, desc) < 0) {
1985  goto err;
1986  }
1987 
1988  /* Let's make sure that we've created a descriptor that can actually be
1989  * encoded properly. This function also checks if the encoded output is
1990  * decodable after. */
1991  if (BUG(service_encode_descriptor(service, desc, &desc->signing_kp,
1992  &encoded_desc) < 0)) {
1993  goto err;
1994  }
1995  tor_free(encoded_desc);
1996 
1997  /* Assign newly built descriptor to the next slot. */
1998  *desc_out = desc;
1999 
2000  /* Fire a CREATED control port event. */
2002  &desc->blinded_kp.pubkey);
2003 
2004  /* If we are an onionbalance instance, we refresh our keys when we rotate
2005  * descriptors. */
2006  hs_ob_refresh_keys(service);
2007 
2008  return;
2009 
2010  err:
2011  service_descriptor_free(desc);
2012 }
2013 
2014 /** Build both descriptors for the given service that has just booted up.
2015  * Because it's a special case, it deserves its special function ;). */
2016 static void
2018 {
2019  uint64_t current_desc_tp, next_desc_tp;
2020 
2021  tor_assert(service);
2022  /* These are the conditions for a new service. */
2023  tor_assert(!service->desc_current);
2024  tor_assert(!service->desc_next);
2025 
2026  /*
2027  * +------------------------------------------------------------------+
2028  * | |
2029  * | 00:00 12:00 00:00 12:00 00:00 12:00 |
2030  * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
2031  * | |
2032  * | $==========|-----------$===========|-----------$===========| |
2033  * | ^ ^ |
2034  * | A B |
2035  * +------------------------------------------------------------------+
2036  *
2037  * Case A: The service boots up before a new time period, the current time
2038  * period is thus TP#1 and the next is TP#2 which for both we have access to
2039  * their SRVs.
2040  *
2041  * Case B: The service boots up inside TP#2, we can't use the TP#3 for the
2042  * next descriptor because we don't have the SRV#3 so the current should be
2043  * TP#1 and next TP#2.
2044  */
2045 
2046  if (hs_in_period_between_tp_and_srv(NULL, now)) {
2047  /* Case B from the above, inside of the new time period. */
2048  current_desc_tp = hs_get_previous_time_period_num(0); /* TP#1 */
2049  next_desc_tp = hs_get_time_period_num(0); /* TP#2 */
2050  } else {
2051  /* Case A from the above, outside of the new time period. */
2052  current_desc_tp = hs_get_time_period_num(0); /* TP#1 */
2053  next_desc_tp = hs_get_next_time_period_num(0); /* TP#2 */
2054  }
2055 
2056  /* Build descriptors. */
2057  build_service_descriptor(service, current_desc_tp, &service->desc_current);
2058  build_service_descriptor(service, next_desc_tp, &service->desc_next);
2059  log_info(LD_REND, "Hidden service %s has just started. Both descriptors "
2060  "built. Now scheduled for upload.",
2061  safe_str_client(service->onion_address));
2062 }
2063 
2064 /** Build descriptors for each service if needed. There are conditions to build
2065  * a descriptor which are details in the function. */
2066 STATIC void
2068 {
2069  FOR_EACH_SERVICE_BEGIN(service) {
2070 
2071  /* A service booting up will have both descriptors to NULL. No other cases
2072  * makes both descriptor non existent. */
2073  if (service->desc_current == NULL && service->desc_next == NULL) {
2074  build_descriptors_for_new_service(service, now);
2075  continue;
2076  }
2077 
2078  /* Reaching this point means we are pass bootup so at runtime. We should
2079  * *never* have an empty current descriptor. If the next descriptor is
2080  * empty, we'll try to build it for the next time period. This only
2081  * happens when we rotate meaning that we are guaranteed to have a new SRV
2082  * at that point for the next time period. */
2083  if (BUG(service->desc_current == NULL)) {
2084  continue;
2085  }
2086 
2087  if (service->desc_next == NULL) {
2089  &service->desc_next);
2090  log_info(LD_REND, "Hidden service %s next descriptor successfully "
2091  "built. Now scheduled for upload.",
2092  safe_str_client(service->onion_address));
2093  }
2094  } FOR_EACH_DESCRIPTOR_END;
2095 }
2096 
2097 /** Randomly pick a node to become an introduction point but not present in the
2098  * given exclude_nodes list. The chosen node is put in the exclude list
2099  * regardless of success or not because in case of failure, the node is simply
2100  * unsusable from that point on.
2101  *
2102  * If direct_conn is set, try to pick a node that our local firewall/policy
2103  * allows us to connect to directly. If we can't find any, return NULL.
2104  * This function supports selecting dual-stack nodes for direct single onion
2105  * service IPv6 connections. But it does not send IPv6 addresses in link
2106  * specifiers. (Current clients don't use IPv6 addresses to extend, and
2107  * direct client connections to intro points are not supported.)
2108  *
2109  * Return a newly allocated service intro point ready to be used for encoding.
2110  * Return NULL on error. */
2111 static hs_service_intro_point_t *
2112 pick_intro_point(unsigned int direct_conn, smartlist_t *exclude_nodes)
2113 {
2114  const or_options_t *options = get_options();
2115  const node_t *node;
2116  hs_service_intro_point_t *ip = NULL;
2117  /* Normal 3-hop introduction point flags. */
2118  router_crn_flags_t flags = CRN_NEED_UPTIME | CRN_NEED_DESC;
2119  /* Single onion flags. */
2120  router_crn_flags_t direct_flags = flags | CRN_PREF_ADDR | CRN_DIRECT_CONN;
2121 
2122  node = router_choose_random_node(exclude_nodes, options->ExcludeNodes,
2123  direct_conn ? direct_flags : flags);
2124 
2125  /* If we are in single onion mode, retry node selection for a 3-hop
2126  * path */
2127  if (direct_conn && !node) {
2128  log_info(LD_REND,
2129  "Unable to find an intro point that we can connect to "
2130  "directly, falling back to a 3-hop path.");
2131  node = router_choose_random_node(exclude_nodes, options->ExcludeNodes,
2132  flags);
2133  }
2134 
2135  if (!node) {
2136  goto err;
2137  }
2138 
2139  /* We have a suitable node, add it to the exclude list. We do this *before*
2140  * we can validate the extend information because even in case of failure,
2141  * we don't want to use that node anymore. */
2142  smartlist_add(exclude_nodes, (void *) node);
2143 
2144  /* Create our objects and populate them with the node information. */
2145  ip = service_intro_point_new(node);
2146 
2147  if (ip == NULL) {
2148  goto err;
2149  }
2150 
2151  log_info(LD_REND, "Picked intro point: %s", node_describe(node));
2152  return ip;
2153  err:
2154  service_intro_point_free(ip);
2155  return NULL;
2156 }
2157 
2158 /** For a given descriptor from the given service, pick any needed intro points
2159  * and update the current map with those newly picked intro points. Return the
2160  * number node that might have been added to the descriptor current map. */
2161 static unsigned int
2164 {
2165  int i = 0, num_needed_ip;
2166  smartlist_t *exclude_nodes = smartlist_new();
2167 
2168  tor_assert(service);
2169  tor_assert(desc);
2170 
2171  /* Compute how many intro points we actually need to open. */
2172  num_needed_ip = service->config.num_intro_points -
2173  digest256map_size(desc->intro_points.map);
2174  if (BUG(num_needed_ip < 0)) {
2175  /* Let's not make tor freak out here and just skip this. */
2176  goto done;
2177  }
2178 
2179  /* We want to end up with config.num_intro_points intro points, but if we
2180  * have no intro points at all (chances are they all cycled or we are
2181  * starting up), we launch get_intro_point_num_extra() extra circuits and
2182  * use the first config.num_intro_points that complete. See proposal #155,
2183  * section 4 for the rationale of this which is purely for performance.
2184  *
2185  * The ones after the first config.num_intro_points will be converted to
2186  * 'General' internal circuits and then we'll drop them from the list of
2187  * intro points. */
2188  if (digest256map_size(desc->intro_points.map) == 0) {
2189  num_needed_ip += get_intro_point_num_extra();
2190  }
2191 
2192  /* Build an exclude list of nodes of our intro point(s). The expiring intro
2193  * points are OK to pick again because this is afterall a concept of round
2194  * robin so they are considered valid nodes to pick again. */
2195  DIGEST256MAP_FOREACH(desc->intro_points.map, key,
2196  hs_service_intro_point_t *, ip) {
2197  const node_t *intro_node = get_node_from_intro_point(ip);
2198  if (intro_node) {
2199  smartlist_add(exclude_nodes, (void*)intro_node);
2200  }
2201  } DIGEST256MAP_FOREACH_END;
2202  /* Also, add the failing intro points that our descriptor encounteered in
2203  * the exclude node list. */
2204  setup_intro_point_exclude_list(desc, exclude_nodes);
2205 
2206  for (i = 0; i < num_needed_ip; i++) {
2208 
2209  /* This function will add the picked intro point node to the exclude nodes
2210  * list so we don't pick the same one at the next iteration. */
2211  ip = pick_intro_point(service->config.is_single_onion, exclude_nodes);
2212  if (ip == NULL) {
2213  /* If we end up unable to pick an introduction point it is because we
2214  * can't find suitable node and calling this again is highly unlikely to
2215  * give us a valid node all of the sudden. */
2216  log_info(LD_REND, "Unable to find a suitable node to be an "
2217  "introduction point for service %s.",
2218  safe_str_client(service->onion_address));
2219  goto done;
2220  }
2221  /* Valid intro point object, add it to the descriptor current map. */
2223  }
2224  /* We've successfully picked all our needed intro points thus none are
2225  * missing which will tell our upload process to expect the number of
2226  * circuits to be the number of configured intro points circuits and not the
2227  * number of intro points object that we have. */
2228  desc->missing_intro_points = 0;
2229 
2230  /* Success. */
2231  done:
2232  /* We don't have ownership of the node_t object in this list. */
2233  smartlist_free(exclude_nodes);
2234  return i;
2235 }
2236 
2237 /** Clear previous cached HSDirs in <b>desc</b>. */
2238 static void
2240 {
2241  if (BUG(!desc->previous_hsdirs)) {
2242  return;
2243  }
2244 
2245  SMARTLIST_FOREACH(desc->previous_hsdirs, char*, s, tor_free(s));
2247 }
2248 
2249 /** Note that we attempted to upload <b>desc</b> to <b>hsdir</b>. */
2250 static void
2252 {
2253  char b64_digest[BASE64_DIGEST_LEN+1] = {0};
2254  digest_to_base64(b64_digest, hsdir->identity);
2255 
2256  if (BUG(!desc->previous_hsdirs)) {
2257  return;
2258  }
2259 
2260  if (!smartlist_contains_string(desc->previous_hsdirs, b64_digest)) {
2261  smartlist_add_strdup(desc->previous_hsdirs, b64_digest);
2262  }
2263 }
2264 
2265 /** Schedule an upload of <b>desc</b>. If <b>descriptor_changed</b> is set, it
2266  * means that this descriptor is dirty. */
2267 STATIC void
2269  time_t now,
2270  int descriptor_changed)
2271 
2272 {
2273  desc->next_upload_time = now;
2274 
2275  /* If the descriptor changed, clean up the old HSDirs list. We want to
2276  * re-upload no matter what. */
2277  if (descriptor_changed) {
2279  }
2280 }
2281 
2282 /** Pick missing intro points for this descriptor if needed. */
2283 static void
2285  hs_service_descriptor_t *desc, time_t now)
2286 {
2287  unsigned int num_intro_points;
2288 
2289  tor_assert(service);
2290  tor_assert(desc);
2291  tor_assert(desc->desc);
2292 
2293  num_intro_points = digest256map_size(desc->intro_points.map);
2294 
2295  /* Pick any missing introduction point(s). */
2296  if (num_intro_points < service->config.num_intro_points) {
2297  unsigned int num_new_intro_points = pick_needed_intro_points(service,
2298  desc);
2299  if (num_new_intro_points != 0) {
2300  log_info(LD_REND, "Service %s just picked %u intro points and wanted "
2301  "%u for %s descriptor. It currently has %d intro "
2302  "points. Launching ESTABLISH_INTRO circuit shortly.",
2303  safe_str_client(service->onion_address),
2304  num_new_intro_points,
2305  service->config.num_intro_points - num_intro_points,
2306  (desc == service->desc_current) ? "current" : "next",
2307  num_intro_points);
2308  /* We'll build those introduction point into the descriptor once we have
2309  * confirmation that the circuits are opened and ready. However,
2310  * indicate that this descriptor should be uploaded from now on. */
2311  service_desc_schedule_upload(desc, now, 1);
2312  }
2313  /* Were we able to pick all the intro points we needed? If not, we'll
2314  * flag the descriptor that it's missing intro points because it
2315  * couldn't pick enough which will trigger a descriptor upload. */
2316  if ((num_new_intro_points + num_intro_points) <
2317  service->config.num_intro_points) {
2318  desc->missing_intro_points = 1;
2319  }
2320  }
2321 }
2322 
2323 /** Update descriptor intro points for each service if needed. We do this as
2324  * part of the periodic event because we need to establish intro point circuits
2325  * before we publish descriptors. */
2326 STATIC void
2328 {
2329  FOR_EACH_SERVICE_BEGIN(service) {
2330  /* We'll try to update each descriptor that is if certain conditions apply
2331  * in order for the descriptor to be updated. */
2332  FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
2333  update_service_descriptor_intro_points(service, desc, now);
2334  } FOR_EACH_DESCRIPTOR_END;
2335  } FOR_EACH_SERVICE_END;
2336 }
2337 
2338 /** Return true iff the given intro point has expired that is it has been used
2339  * for too long or we've reached our max seen INTRODUCE2 cell. */
2340 STATIC int
2342  time_t now)
2343 {
2344  tor_assert(ip);
2345 
2346  if (ip->introduce2_count >= ip->introduce2_max) {
2347  goto expired;
2348  }
2349 
2350  if (ip->time_to_expire <= now) {
2351  goto expired;
2352  }
2353 
2354  /* Not expiring. */
2355  return 0;
2356  expired:
2357  return 1;
2358 }
2359 
2360 /** Return true iff we should remove the intro point ip from its service.
2361  *
2362  * We remove an intro point from the service descriptor list if one of
2363  * these criteria is met:
2364  * - It has expired (either in INTRO2 count or in time).
2365  * - No node was found (fell off the consensus).
2366  * - We are over the maximum amount of retries.
2367  *
2368  * If an established or pending circuit is found for the given ip object, this
2369  * return false indicating it should not be removed. */
2370 static bool
2372 {
2373  bool ret = false;
2374 
2375  tor_assert(ip);
2376 
2377  /* Any one of the following needs to be True to furfill the criteria to
2378  * remove an intro point. */
2379  bool has_no_retries = (ip->circuit_retries >
2381  bool has_no_node = (get_node_from_intro_point(ip) == NULL);
2382  bool has_expired = intro_point_should_expire(ip, now);
2383 
2384  /* If the node fell off the consensus or the IP has expired, we have to
2385  * remove it now. */
2386  if (has_no_node || has_expired) {
2387  ret = true;
2388  goto end;
2389  }
2390 
2391  /* Pass this point, even though we might be over the retry limit, we check
2392  * if a circuit (established or pending) exists. In that case, we should not
2393  * remove it because it might simply be valid and opened at the previous
2394  * scheduled event for the last retry. */
2395 
2396  /* Do we simply have an existing circuit regardless of its state? */
2398  goto end;
2399  }
2400 
2401  /* Getting here means we have _no_ circuits so then return if we have any
2402  * remaining retries. */
2403  ret = has_no_retries;
2404 
2405  end:
2406  /* Meaningful log in case we are about to remove the IP. */
2407  if (ret) {
2408  log_info(LD_REND, "Intro point %s%s (retried: %u times). "
2409  "Removing it.",
2411  has_expired ? " has expired" :
2412  (has_no_node) ? " fell off the consensus" : "",
2413  ip->circuit_retries);
2414  }
2415  return ret;
2416 }
2417 
2418 /** Go over the given set of intro points for each service and remove any
2419  * invalid ones.
2420  *
2421  * If an intro point is removed, the circuit (if any) is immediately close.
2422  * If a circuit can't be found, the intro point is kept if it hasn't reached
2423  * its maximum circuit retry value and thus should be retried. */
2424 static void
2425 cleanup_intro_points(hs_service_t *service, time_t now)
2426 {
2427  /* List of intro points to close. We can't mark the intro circuits for close
2428  * in the modify loop because doing so calls back into the HS subsystem and
2429  * we need to keep that code path outside of the service/desc loop so those
2430  * maps don't get modified during the close making us in a possible
2431  * use-after-free situation. */
2432  smartlist_t *ips_to_free = smartlist_new();
2433 
2434  tor_assert(service);
2435 
2436  /* For both descriptors, cleanup the intro points. */
2437  FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
2438  /* Go over the current intro points we have, make sure they are still
2439  * valid and remove any of them that aren't. */
2440  DIGEST256MAP_FOREACH_MODIFY(desc->intro_points.map, key,
2441  hs_service_intro_point_t *, ip) {
2442  if (should_remove_intro_point(ip, now)) {
2443  /* We've retried too many times, remember it as a failed intro point
2444  * so we don't pick it up again for INTRO_CIRC_RETRY_PERIOD sec. */
2445  if (ip->circuit_retries > MAX_INTRO_POINT_CIRCUIT_RETRIES) {
2447  }
2448 
2449  /* Remove intro point from descriptor map and add it to the list of
2450  * ips to free for which we'll also try to close the intro circuit. */
2451  MAP_DEL_CURRENT(key);
2452  smartlist_add(ips_to_free, ip);
2453  }
2454  } DIGEST256MAP_FOREACH_END;
2455  } FOR_EACH_DESCRIPTOR_END;
2456 
2457  /* Go over the intro points to free and close their circuit if any. */
2459  /* See if we need to close the intro point circuit as well */
2460 
2461  /* XXX: Legacy code does NOT close circuits like this: it keeps the circuit
2462  * open until a new descriptor is uploaded and then closed all expiring
2463  * intro point circuit. Here, we close immediately and because we just
2464  * discarded the intro point, a new one will be selected, a new descriptor
2465  * created and uploaded. There is no difference to an attacker between the
2466  * timing of a new consensus and intro point rotation (possibly?). */
2468  if (ocirc && !TO_CIRCUIT(ocirc)->marked_for_close) {
2469  circuit_mark_for_close(TO_CIRCUIT(ocirc), END_CIRC_REASON_FINISHED);
2470  }
2471 
2472  /* Cleanup the intro point */
2473  service_intro_point_free(ip);
2474  } SMARTLIST_FOREACH_END(ip);
2475 
2476  smartlist_free(ips_to_free);
2477 }
2478 
2479 /** Set the next rotation time of the descriptors for the given service for the
2480  * time now. */
2481 static void
2483 {
2484  tor_assert(service);
2485 
2486  service->state.next_rotation_time =
2489 
2490  {
2491  char fmt_time[ISO_TIME_LEN + 1];
2492  format_local_iso_time(fmt_time, service->state.next_rotation_time);
2493  log_info(LD_REND, "Next descriptor rotation time set to %s for %s",
2494  fmt_time, safe_str_client(service->onion_address));
2495  }
2496 }
2497 
2498 /** Return true iff the service should rotate its descriptor. The time now is
2499  * only used to fetch the live consensus and if none can be found, this
2500  * returns false. */
2501 static unsigned int
2503 {
2504  const networkstatus_t *ns;
2505 
2506  tor_assert(service);
2507 
2509  if (ns == NULL) {
2510  goto no_rotation;
2511  }
2512 
2513  if (ns->valid_after >= service->state.next_rotation_time) {
2514  /* In theory, we should never get here with no descriptors. We can never
2515  * have a NULL current descriptor except when tor starts up. The next
2516  * descriptor can be NULL after a rotation but we build a new one right
2517  * after.
2518  *
2519  * So, when tor starts, the next rotation time is set to the start of the
2520  * next SRV period using the consensus valid after time so it should
2521  * always be set to a future time value. This means that we should never
2522  * reach this point at bootup that is this check safeguards tor in never
2523  * allowing a rotation if the valid after time is smaller than the next
2524  * rotation time.
2525  *
2526  * This is all good in theory but we've had a NULL descriptor issue here
2527  * so this is why we BUG() on both with extra logging to try to understand
2528  * how this can possibly happens. We'll simply ignore and tor should
2529  * recover from this by skipping rotation and building the missing
2530  * descriptors just after this. */
2531  if (BUG(service->desc_current == NULL || service->desc_next == NULL)) {
2532  log_warn(LD_BUG, "Service descriptor is NULL (%p/%p). Next rotation "
2533  "time is %ld (now: %ld). Valid after time from "
2534  "consensus is %ld",
2535  service->desc_current, service->desc_next,
2536  (long)service->state.next_rotation_time,
2537  (long)now,
2538  (long)ns->valid_after);
2539  goto no_rotation;
2540  }
2541  goto rotation;
2542  }
2543 
2544  no_rotation:
2545  return 0;
2546  rotation:
2547  return 1;
2548 }
2549 
2550 /** Rotate the service descriptors of the given service. The current descriptor
2551  * will be freed, the next one put in as the current and finally the next
2552  * descriptor pointer is NULLified. */
2553 static void
2555 {
2556  if (service->desc_current) {
2557  /* Close all IP circuits for the descriptor. */
2559  /* We don't need this one anymore, we won't serve any clients coming with
2560  * this service descriptor. */
2561  service_descriptor_free(service->desc_current);
2562  }
2563  /* The next one become the current one and emptying the next will trigger
2564  * a descriptor creation for it. */
2565  service->desc_current = service->desc_next;
2566  service->desc_next = NULL;
2567 
2568  /* We've just rotated, set the next time for the rotation. */
2569  set_rotation_time(service);
2570 }
2571 
2572 /** Rotate descriptors for each service if needed. A non existing current
2573  * descriptor will trigger a descriptor build for the next time period. */
2574 STATIC void
2576 {
2577  /* XXX We rotate all our service descriptors at once. In the future it might
2578  * be wise, to rotate service descriptors independently to hide that all
2579  * those descriptors are on the same tor instance */
2580 
2581  FOR_EACH_SERVICE_BEGIN(service) {
2582 
2583  /* Note for a service booting up: Both descriptors are NULL in that case
2584  * so this function might return true if we are in the timeframe for a
2585  * rotation leading to basically swapping two NULL pointers which is
2586  * harmless. However, the side effect is that triggering a rotation will
2587  * update the service state and avoid doing anymore rotations after the
2588  * two descriptors have been built. */
2589  if (!should_rotate_descriptors(service, now)) {
2590  continue;
2591  }
2592 
2593  log_info(LD_REND, "Time to rotate our descriptors (%p / %p) for %s",
2594  service->desc_current, service->desc_next,
2595  safe_str_client(service->onion_address));
2596 
2597  rotate_service_descriptors(service);
2598  } FOR_EACH_SERVICE_END;
2599 }
2600 
2601 /** Scheduled event run from the main loop. Make sure all our services are up
2602  * to date and ready for the other scheduled events. This includes looking at
2603  * the introduction points status and descriptor rotation time. */
2604 STATIC void
2606 {
2607  /* Note that nothing here opens circuit(s) nor uploads descriptor(s). We are
2608  * simply moving things around or removing unneeded elements. */
2609 
2610  FOR_EACH_SERVICE_BEGIN(service) {
2611 
2612  /* If the service is starting off, set the rotation time. We can't do that
2613  * at configure time because the get_options() needs to be set for setting
2614  * that time that uses the voting interval. */
2615  if (service->state.next_rotation_time == 0) {
2616  /* Set the next rotation time of the descriptors. If it's Oct 25th
2617  * 23:47:00, the next rotation time is when the next SRV is computed
2618  * which is at Oct 26th 00:00:00 that is in 13 minutes. */
2619  set_rotation_time(service);
2620  }
2621 
2622  /* Cleanup invalid intro points from the service descriptor. */
2623  cleanup_intro_points(service, now);
2624 
2625  /* Remove expired failing intro point from the descriptor failed list. We
2626  * reset them at each INTRO_CIRC_RETRY_PERIOD. */
2627  remove_expired_failing_intro(service, now);
2628 
2629  /* At this point, the service is now ready to go through the scheduled
2630  * events guaranteeing a valid state. Intro points might be missing from
2631  * the descriptors after the cleanup but the update/build process will
2632  * make sure we pick those missing ones. */
2633  } FOR_EACH_SERVICE_END;
2634 }
2635 
2636 /** Scheduled event run from the main loop. Make sure all descriptors are up to
2637  * date. Once this returns, each service descriptor needs to be considered for
2638  * new introduction circuits and then for upload. */
2639 static void
2641 {
2642  /* For v2 services, this step happens in the upload event. */
2643 
2644  /* Run v3+ events. */
2645  /* We start by rotating the descriptors only if needed. */
2647 
2648  /* Then, we'll try to build new descriptors that we might need. The
2649  * condition is that the next descriptor is non existing because it has
2650  * been rotated or we just started up. */
2651  build_all_descriptors(now);
2652 
2653  /* Finally, we'll check if we should update the descriptors' intro
2654  * points. Missing introduction points will be picked in this function which
2655  * is useful for newly built descriptors. */
2657 }
2658 
2659 /** For the given service, launch any intro point circuits that could be
2660  * needed. This considers every descriptor of the service. */
2661 static void
2663 {
2664  tor_assert(service);
2665 
2666  /* For both descriptors, try to launch any missing introduction point
2667  * circuits using the current map. */
2668  FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
2669  /* Keep a ref on if we need a direct connection. We use this often. */
2670  bool direct_conn = service->config.is_single_onion;
2671 
2672  DIGEST256MAP_FOREACH_MODIFY(desc->intro_points.map, key,
2673  hs_service_intro_point_t *, ip) {
2674  extend_info_t *ei;
2675 
2676  /* Skip the intro point that already has an existing circuit
2677  * (established or not). */
2679  continue;
2680  }
2681  ei = get_extend_info_from_intro_point(ip, direct_conn);
2682 
2683  /* If we can't connect directly to the intro point, get an extend_info
2684  * for a multi-hop path instead. */
2685  if (ei == NULL && direct_conn) {
2686  direct_conn = false;
2688  }
2689 
2690  if (ei == NULL) {
2691  /* This is possible if we can get a node_t but not the extend info out
2692  * of it. In this case, we remove the intro point and a new one will
2693  * be picked at the next main loop callback. */
2694  MAP_DEL_CURRENT(key);
2695  service_intro_point_free(ip);
2696  continue;
2697  }
2698 
2699  /* Launch a circuit to the intro point. */
2700  ip->circuit_retries++;
2701  if (hs_circ_launch_intro_point(service, ip, ei, direct_conn) < 0) {
2702  log_info(LD_REND, "Unable to launch intro circuit to node %s "
2703  "for service %s.",
2704  safe_str_client(extend_info_describe(ei)),
2705  safe_str_client(service->onion_address));
2706  /* Intro point will be retried if possible after this. */
2707  }
2708  extend_info_free(ei);
2709  } DIGEST256MAP_FOREACH_END;
2710  } FOR_EACH_DESCRIPTOR_END;
2711 }
2712 
2713 /** Don't try to build more than this many circuits before giving up for a
2714  * while. Dynamically calculated based on the configured number of intro
2715  * points for the given service and how many descriptor exists. The default
2716  * use case of 3 introduction points and two descriptors will allow 28
2717  * circuits for a retry period (((3 + 2) + (3 * 3)) * 2). */
2718 static unsigned int
2720 {
2721  unsigned int count = 0;
2722  unsigned int multiplier = 0;
2723  unsigned int num_wanted_ip;
2724 
2725  tor_assert(service);
2726  tor_assert(service->config.num_intro_points <=
2727  HS_CONFIG_V3_MAX_INTRO_POINTS);
2728 
2729 /** For a testing network, allow to do it for the maximum amount so circuit
2730  * creation and rotation and so on can actually be tested without limit. */
2731 #define MAX_INTRO_POINT_CIRCUIT_RETRIES_TESTING -1
2732  if (get_options()->TestingTorNetwork) {
2733  return MAX_INTRO_POINT_CIRCUIT_RETRIES_TESTING;
2734  }
2735 
2736  num_wanted_ip = service->config.num_intro_points;
2737 
2738  /* The calculation is as follow. We have a number of intro points that we
2739  * want configured as a torrc option (num_intro_points). We then add an
2740  * extra value so we can launch multiple circuits at once and pick the
2741  * quickest ones. For instance, we want 3 intros, we add 2 extra so we'll
2742  * pick 5 intros and launch 5 circuits. */
2743  count += (num_wanted_ip + get_intro_point_num_extra());
2744 
2745  /* Then we add the number of retries that is possible to do for each intro
2746  * point. If we want 3 intros, we'll allow 3 times the number of possible
2747  * retry. */
2748  count += (num_wanted_ip * MAX_INTRO_POINT_CIRCUIT_RETRIES);
2749 
2750  /* Then, we multiply by a factor of 2 if we have both descriptor or 0 if we
2751  * have none. */
2752  multiplier += (service->desc_current) ? 1 : 0;
2753  multiplier += (service->desc_next) ? 1 : 0;
2754 
2755  return (count * multiplier);
2756 }
2757 
2758 /** For the given service, return 1 if the service is allowed to launch more
2759  * introduction circuits else 0 if the maximum has been reached for the retry
2760  * period of INTRO_CIRC_RETRY_PERIOD. */
2761 STATIC int
2763 {
2764  tor_assert(service);
2765 
2766  /* Consider the intro circuit retry period of the service. */
2767  if (now > (service->state.intro_circ_retry_started_time +
2769  service->state.intro_circ_retry_started_time = now;
2770  service->state.num_intro_circ_launched = 0;
2771  goto allow;
2772  }
2773  /* Check if we can still launch more circuits in this period. */
2774  if (service->state.num_intro_circ_launched <=
2775  get_max_intro_circ_per_period(service)) {
2776  goto allow;
2777  }
2778 
2779  /* Rate limit log that we've reached our circuit creation limit. */
2780  {
2781  char *msg;
2782  time_t elapsed_time = now - service->state.intro_circ_retry_started_time;
2783  static ratelim_t rlimit = RATELIM_INIT(INTRO_CIRC_RETRY_PERIOD);
2784  if ((msg = rate_limit_log(&rlimit, now))) {
2785  log_info(LD_REND, "Hidden service %s exceeded its circuit launch limit "
2786  "of %u per %d seconds. It launched %u circuits in "
2787  "the last %ld seconds. Will retry in %ld seconds.",
2788  safe_str_client(service->onion_address),
2791  service->state.num_intro_circ_launched,
2792  (long int) elapsed_time,
2793  (long int) (INTRO_CIRC_RETRY_PERIOD - elapsed_time));
2794  tor_free(msg);
2795  }
2796  }
2797 
2798  /* Not allow. */
2799  return 0;
2800  allow:
2801  return 1;
2802 }
2803 
2804 /** Scheduled event run from the main loop. Make sure we have all the circuits
2805  * we need for each service. */
2806 static void
2808 {
2809  /* Make sure we can actually have enough information or able to build
2810  * internal circuits as required by services. */
2811  if (router_have_consensus_path() == CONSENSUS_PATH_UNKNOWN ||
2813  return;
2814  }
2815 
2816  /* Run v2 check. */
2817  if (rend_num_services() > 0) {
2819  }
2820 
2821  /* Run v3+ check. */
2822  FOR_EACH_SERVICE_BEGIN(service) {
2823  /* For introduction circuit, we need to make sure we don't stress too much
2824  * circuit creation so make sure this service is respecting that limit. */
2825  if (can_service_launch_intro_circuit(service, now)) {
2826  /* Launch intro point circuits if needed. */
2827  launch_intro_point_circuits(service);
2828  /* Once the circuits have opened, we'll make sure to update the
2829  * descriptor intro point list and cleanup any extraneous. */
2830  }
2831  } FOR_EACH_SERVICE_END;
2832 }
2833 
2834 /** Encode and sign the service descriptor desc and upload it to the given
2835  * hidden service directory. This does nothing if PublishHidServDescriptors
2836  * is false. */
2837 static void
2839  hs_service_descriptor_t *desc, const node_t *hsdir)
2840 {
2841  char *encoded_desc = NULL;
2842 
2843  tor_assert(service);
2844  tor_assert(desc);
2845  tor_assert(hsdir);
2846 
2847  /* Let's avoid doing that if tor is configured to not publish. */
2848  if (!get_options()->PublishHidServDescriptors) {
2849  log_info(LD_REND, "Service %s not publishing descriptor. "
2850  "PublishHidServDescriptors is set to 0.",
2851  safe_str_client(service->onion_address));
2852  goto end;
2853  }
2854 
2855  /* First of all, we'll encode the descriptor. This should NEVER fail but
2856  * just in case, let's make sure we have an actual usable descriptor. */
2857  if (BUG(service_encode_descriptor(service, desc, &desc->signing_kp,
2858  &encoded_desc) < 0)) {
2859  goto end;
2860  }
2861 
2862  /* Time to upload the descriptor to the directory. */
2863  hs_service_upload_desc_to_dir(encoded_desc, service->config.version,
2864  &service->keys.identity_pk,
2865  &desc->blinded_kp.pubkey, hsdir->rs);
2866 
2867  /* Add this node to previous_hsdirs list */
2868  service_desc_note_upload(desc, hsdir);
2869 
2870  /* Logging so we know where it was sent. */
2871  {
2872  int is_next_desc = (service->desc_next == desc);
2873  const uint8_t *idx = (is_next_desc) ? hsdir->hsdir_index.store_second:
2874  hsdir->hsdir_index.store_first;
2875  char *blinded_pubkey_log_str =
2876  tor_strdup(hex_str((char*)&desc->blinded_kp.pubkey.pubkey, 32));
2877  /* This log message is used by Chutney as part of its bootstrap
2878  * detection mechanism. Please don't change without first checking
2879  * Chutney. */
2880  log_info(LD_REND, "Service %s %s descriptor of revision %" PRIu64
2881  " initiated upload request to %s with index %s (%s)",
2882  safe_str_client(service->onion_address),
2883  (is_next_desc) ? "next" : "current",
2885  safe_str_client(node_describe(hsdir)),
2886  safe_str_client(hex_str((const char *) idx, 32)),
2887  safe_str_client(blinded_pubkey_log_str));
2888  tor_free(blinded_pubkey_log_str);
2889 
2890  /* Fire a UPLOAD control port event. */
2892  &desc->blinded_kp.pubkey, idx);
2893  }
2894 
2895  end:
2896  tor_free(encoded_desc);
2897  return;
2898 }
2899 
2900 /** Set the revision counter in <b>hs_desc</b>. We do this by encrypting a
2901  * timestamp using an OPE scheme and using the ciphertext as our revision
2902  * counter.
2903  *
2904  * If <b>is_current</b> is true, then this is the current HS descriptor,
2905  * otherwise it's the next one. */
2906 static void
2908  bool is_current)
2909 {
2910  uint64_t rev_counter = 0;
2911 
2912  /* Get current time */
2913  time_t srv_start = 0;
2914 
2915  /* As our revision counter plaintext value, we use the seconds since the
2916  * start of the SR protocol run that is relevant to this descriptor. This is
2917  * guaranteed to be a positive value since we need the SRV to start making a
2918  * descriptor (so that we know where to upload it).
2919  *
2920  * Depending on whether we are building the current or the next descriptor,
2921  * services use a different SRV value. See [SERVICEUPLOAD] in
2922  * rend-spec-v3.txt:
2923  *
2924  * In particular, for the current descriptor (aka first descriptor), Tor
2925  * always uses the previous SRV for uploading the descriptor, and hence we
2926  * should use the start time of the previous protocol run here.
2927  *
2928  * Whereas for the next descriptor (aka second descriptor), Tor always uses
2929  * the current SRV for uploading the descriptor. and hence we use the start
2930  * time of the current protocol run.
2931  */
2932  if (is_current) {
2934  } else {
2936  }
2937 
2938  log_info(LD_REND, "Setting rev counter for TP #%u: "
2939  "SRV started at %d, now %d (%s)",
2940  (unsigned) hs_desc->time_period_num, (int)srv_start,
2941  (int)now, is_current ? "current" : "next");
2942 
2943  tor_assert_nonfatal(now >= srv_start);
2944 
2945  /* Compute seconds elapsed since the start of the time period. That's the
2946  * number of seconds of how long this blinded key has been active. */
2947  time_t seconds_since_start_of_srv = now - srv_start;
2948 
2949  /* Increment by one so that we are definitely sure this is strictly
2950  * positive and not zero. */
2951  seconds_since_start_of_srv++;
2952 
2953  /* Check for too big inputs. */
2954  if (BUG(seconds_since_start_of_srv > OPE_INPUT_MAX)) {
2955  seconds_since_start_of_srv = OPE_INPUT_MAX;
2956  }
2957 
2958  /* Now we compute the final revision counter value by encrypting the
2959  plaintext using our OPE cipher: */
2960  tor_assert(hs_desc->ope_cipher);
2961  rev_counter = crypto_ope_encrypt(hs_desc->ope_cipher,
2962  (int) seconds_since_start_of_srv);
2963 
2964  /* The OPE module returns CRYPTO_OPE_ERROR in case of errors. */
2965  tor_assert_nonfatal(rev_counter < CRYPTO_OPE_ERROR);
2966 
2967  log_info(LD_REND, "Encrypted revision counter %d to %" PRIu64,
2968  (int) seconds_since_start_of_srv, rev_counter);
2969 
2970  hs_desc->desc->plaintext_data.revision_counter = rev_counter;
2971 }
2972 
2973 /** Encode and sign the service descriptor desc and upload it to the
2974  * responsible hidden service directories. If for_next_period is true, the set
2975  * of directories are selected using the next hsdir_index. This does nothing
2976  * if PublishHidServDescriptors is false. */
2977 STATIC void
2980 {
2981  smartlist_t *responsible_dirs = NULL;
2982 
2983  tor_assert(service);
2984  tor_assert(desc);
2985 
2986  /* We'll first cancel any directory request that are ongoing for this
2987  * descriptor. It is possible that we can trigger multiple uploads in a
2988  * short time frame which can lead to a race where the second upload arrives
2989  * before the first one leading to a 400 malformed descriptor response from
2990  * the directory. Closing all pending requests avoids that. */
2991  close_directory_connections(service, desc);
2992 
2993  /* Get our list of responsible HSDir. */
2994  responsible_dirs = smartlist_new();
2995  /* The parameter 0 means that we aren't a client so tell the function to use
2996  * the spread store consensus paremeter. */
2998  service->desc_next == desc, 0, responsible_dirs);
2999 
3000  /** Clear list of previous hsdirs since we are about to upload to a new
3001  * list. Let's keep it up to date. */
3003 
3004  /* For each responsible HSDir we have, initiate an upload command. */
3005  SMARTLIST_FOREACH_BEGIN(responsible_dirs, const routerstatus_t *,
3006  hsdir_rs) {
3007  const node_t *hsdir_node = node_get_by_id(hsdir_rs->identity_digest);
3008  /* Getting responsible hsdir implies that the node_t object exists for the
3009  * routerstatus_t found in the consensus else we have a problem. */
3010  tor_assert(hsdir_node);
3011  /* Upload this descriptor to the chosen directory. */
3012  upload_descriptor_to_hsdir(service, desc, hsdir_node);
3013  } SMARTLIST_FOREACH_END(hsdir_rs);
3014 
3015  /* Set the next upload time for this descriptor. Even if we are configured
3016  * to not upload, we still want to follow the right cycle of life for this
3017  * descriptor. */
3018  desc->next_upload_time =
3021  {
3022  char fmt_next_time[ISO_TIME_LEN+1];
3023  format_local_iso_time(fmt_next_time, desc->next_upload_time);
3024  log_debug(LD_REND, "Service %s set to upload a descriptor at %s",
3025  safe_str_client(service->onion_address), fmt_next_time);
3026  }
3027 
3028  smartlist_free(responsible_dirs);
3029  return;
3030 }
3031 
3032 /** The set of HSDirs have changed: check if the change affects our descriptor
3033  * HSDir placement, and if it does, reupload the desc. */
3034 STATIC int
3036  const hs_service_descriptor_t *desc)
3037 {
3038  int should_reupload = 0;
3039  smartlist_t *responsible_dirs = smartlist_new();
3040 
3041  /* No desc upload has happened yet: it will happen eventually */
3042  if (!desc->previous_hsdirs || !smartlist_len(desc->previous_hsdirs)) {
3043  goto done;
3044  }
3045 
3046  /* Get list of responsible hsdirs */
3048  service->desc_next == desc, 0, responsible_dirs);
3049 
3050  /* Check if any new hsdirs have been added to the responsible hsdirs set:
3051  * Iterate over the list of new hsdirs, and reupload if any of them is not
3052  * present in the list of previous hsdirs.
3053  */
3054  SMARTLIST_FOREACH_BEGIN(responsible_dirs, const routerstatus_t *, hsdir_rs) {
3055  char b64_digest[BASE64_DIGEST_LEN+1] = {0};
3056  digest_to_base64(b64_digest, hsdir_rs->identity_digest);
3057 
3058  if (!smartlist_contains_string(desc->previous_hsdirs, b64_digest)) {
3059  should_reupload = 1;
3060  break;
3061  }
3062  } SMARTLIST_FOREACH_END(hsdir_rs);
3063 
3064  done:
3065  smartlist_free(responsible_dirs);
3066 
3067  return should_reupload;
3068 }
3069 
3070 /** These are all the reasons why a descriptor upload can't occur. We use
3071  * those to log the reason properly with the right rate limiting and for the
3072  * right descriptor. */
3073 typedef enum {
3074  LOG_DESC_UPLOAD_REASON_MISSING_IPS = 0,
3075  LOG_DESC_UPLOAD_REASON_IP_NOT_ESTABLISHED = 1,
3076  LOG_DESC_UPLOAD_REASON_NOT_TIME = 2,
3077  LOG_DESC_UPLOAD_REASON_NO_LIVE_CONSENSUS = 3,
3078  LOG_DESC_UPLOAD_REASON_NO_DIRINFO = 4,
3080 
3081 /** Maximum number of reasons. This is used to allocate the static array of
3082  * all rate limiting objects. */
3083 #define LOG_DESC_UPLOAD_REASON_MAX LOG_DESC_UPLOAD_REASON_NO_DIRINFO
3085 /** Log the reason why we can't upload the given descriptor for the given
3086  * service. This takes a message string (allocated by the caller) and a
3087  * reason.
3088  *
3089  * Depending on the reason and descriptor, different rate limit applies. This
3090  * is done because this function will basically be called every second. Each
3091  * descriptor for each reason uses its own log rate limit object in order to
3092  * avoid message suppression for different reasons and descriptors. */
3093 static void
3095  const hs_service_descriptor_t *desc, const char *msg,
3096  const log_desc_upload_reason_t reason)
3097 {
3098  /* Writing the log every minute shouldn't be too annoying for log rate limit
3099  * since this can be emitted every second for each descriptor.
3100  *
3101  * However, for one specific case, we increase it to 10 minutes because it
3102  * is hit constantly, as an expected behavior, which is the reason
3103  * indicating that it is not the time to upload. */
3104  static ratelim_t limits[2][LOG_DESC_UPLOAD_REASON_MAX + 1] =
3105  { { RATELIM_INIT(60), RATELIM_INIT(60), RATELIM_INIT(60 * 10),
3106  RATELIM_INIT(60), RATELIM_INIT(60) },
3107  { RATELIM_INIT(60), RATELIM_INIT(60), RATELIM_INIT(60 * 10),
3108  RATELIM_INIT(60), RATELIM_INIT(60) },
3109  };
3110  bool is_next_desc = false;
3111  unsigned int rlim_pos = 0;
3112  ratelim_t *rlim = NULL;
3113 
3114  tor_assert(service);
3115  tor_assert(desc);
3116  tor_assert(msg);
3117 
3118  /* Make sure the reason value is valid. It should never happen because we
3119  * control that value in the code flow but will be apparent during
3120  * development if a reason is added but LOG_DESC_UPLOAD_REASON_NUM_ is not
3121  * updated. */
3122  if (BUG(reason > LOG_DESC_UPLOAD_REASON_MAX)) {
3123  return;
3124  }
3125 
3126  /* Ease our life. Flag that tells us if the descriptor is the next one. */
3127  is_next_desc = (service->desc_next == desc);
3128 
3129  /* Current descriptor is the first element in the ratelimit object array.
3130  * The next descriptor is the second element. */
3131  rlim_pos = (is_next_desc ? 1 : 0);
3132  /* Get the ratelimit object for the reason _and_ right descriptor. */
3133  rlim = &limits[rlim_pos][reason];
3134 
3136  "Service %s can't upload its %s descriptor: %s",
3137  safe_str_client(service->onion_address),
3138  (is_next_desc) ? "next" : "current", msg);
3139 }
3140 
3141 /** Return 1 if the given descriptor from the given service can be uploaded
3142  * else return 0 if it can not. */
3143 static int
3145  const hs_service_descriptor_t *desc, time_t now)
3146 {
3147  char *msg = NULL;
3148  unsigned int num_intro_points, count_ip_established;
3149 
3150  tor_assert(service);
3151  tor_assert(desc);
3152 
3153  /* If this descriptors has missing intro points that is that it couldn't get
3154  * them all when it was time to pick them, it means that we should upload
3155  * instead of waiting an arbitrary amount of time breaking the service.
3156  * Else, if we have no missing intro points, we use the value taken from the
3157  * service configuration. */
3158  if (desc->missing_intro_points) {
3159  num_intro_points = digest256map_size(desc->intro_points.map);
3160  } else {
3161  num_intro_points = service->config.num_intro_points;
3162  }
3163 
3164  /* This means we tried to pick intro points but couldn't get any so do not
3165  * upload descriptor in this case. We need at least one for the service to
3166  * be reachable. */
3167  if (desc->missing_intro_points && num_intro_points == 0) {
3168  msg = tor_strdup("Missing intro points");
3169  log_cant_upload_desc(service, desc, msg,
3170  LOG_DESC_UPLOAD_REASON_MISSING_IPS);
3171  goto cannot;
3172  }
3173 
3174  /* Check if all our introduction circuit have been established for all the
3175  * intro points we have selected. */
3176  count_ip_established = count_desc_circuit_established(desc);
3177  if (count_ip_established != num_intro_points) {
3178  tor_asprintf(&msg, "Intro circuits aren't yet all established (%d/%d).",
3179  count_ip_established, num_intro_points);
3180  log_cant_upload_desc(service, desc, msg,
3181  LOG_DESC_UPLOAD_REASON_IP_NOT_ESTABLISHED);
3182  goto cannot;
3183  }
3184 
3185  /* Is it the right time to upload? */
3186  if (desc->next_upload_time > now) {
3187  tor_asprintf(&msg, "Next upload time is %ld, it is now %ld.",
3188  (long int) desc->next_upload_time, (long int) now);
3189  log_cant_upload_desc(service, desc, msg,
3190  LOG_DESC_UPLOAD_REASON_NOT_TIME);
3191  goto cannot;
3192  }
3193 
3194  /* Don't upload desc if we don't have a live consensus */
3196  msg = tor_strdup("No live consensus");
3197  log_cant_upload_desc(service, desc, msg,
3198  LOG_DESC_UPLOAD_REASON_NO_LIVE_CONSENSUS);
3199  goto cannot;
3200  }
3201 
3202  /* Do we know enough router descriptors to have adequate vision of the HSDir
3203  hash ring? */
3205  msg = tor_strdup("Not enough directory information");
3206  log_cant_upload_desc(service, desc, msg,
3207  LOG_DESC_UPLOAD_REASON_NO_DIRINFO);
3208  goto cannot;
3209  }
3210 
3211  /* Can upload! */
3212  return 1;
3213 
3214  cannot:
3215  tor_free(msg);
3216  return 0;
3217 }
3218 
3219 /** Refresh the given service descriptor meaning this will update every mutable
3220  * field that needs to be updated before we upload.
3221  *
3222  * This should ONLY be called before uploading a descriptor. It assumes that
3223  * the descriptor has been built (desc->desc) and that all intro point
3224  * circuits have been established. */
3225 static void
3227  hs_service_descriptor_t *desc, time_t now)
3228 {
3229  /* There are few fields that we consider "mutable" in the descriptor meaning
3230  * we need to update them regurlarly over the lifetime fo the descriptor.
3231  * The rest are set once and should not be modified.
3232  *
3233  * - Signing key certificate.
3234  * - Revision counter.
3235  * - Introduction points which includes many thing. See
3236  * hs_desc_intro_point_t. and the setup_desc_intro_point() function.
3237  */
3238 
3239  /* Create the signing key certificate. */
3240  build_desc_signing_key_cert(desc, now);
3241 
3242  /* Build the intro points descriptor section. The refresh step is just
3243  * before we upload so all circuits have been properly established. */
3244  build_desc_intro_points(service, desc, now);
3245 
3246  /* Set the desc revision counter right before uploading */
3247  set_descriptor_revision_counter(desc, now, service->desc_current == desc);
3248 }
3249 
3250 /** Scheduled event run from the main loop. Try to upload the descriptor for
3251  * each service. */
3252 STATIC void
3254 {
3255  /* v2 services use the same function for descriptor creation and upload so
3256  * we do everything here because the intro circuits were checked before. */
3257  if (rend_num_services() > 0) {
3260  }
3261 
3262  /* Run v3+ check. */
3263  FOR_EACH_SERVICE_BEGIN(service) {
3264  FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
3265  /* If we were asked to re-examine the hash ring, and it changed, then
3266  schedule an upload */
3268  service_desc_hsdirs_changed(service, desc)) {
3269  service_desc_schedule_upload(desc, now, 0);
3270  }
3271 
3272  /* Can this descriptor be uploaded? */
3273  if (!should_service_upload_descriptor(service, desc, now)) {
3274  continue;
3275  }
3276 
3277  log_info(LD_REND, "Initiating upload for hidden service %s descriptor "
3278  "for service %s with %u/%u introduction points%s.",
3279  (desc == service->desc_current) ? "current" : "next",
3280  safe_str_client(service->onion_address),
3281  digest256map_size(desc->intro_points.map),
3282  service->config.num_intro_points,
3283  (desc->missing_intro_points) ? " (couldn't pick more)" : "");
3284 
3285  /* We are about to upload so we need to do one last step which is to
3286  * update the service's descriptor mutable fields in order to upload a
3287  * coherent descriptor. */
3288  refresh_service_descriptor(service, desc, now);
3289 
3290  /* Proceed with the upload, the descriptor is ready to be encoded. */
3291  upload_descriptor_to_all(service, desc);
3292  } FOR_EACH_DESCRIPTOR_END;
3293  } FOR_EACH_SERVICE_END;
3294 
3295  /* We are done considering whether to republish rend descriptors */
3297 }
3298 
3299 /** Called when the introduction point circuit is done building and ready to be
3300  * used. */
3301 static void
3303 {
3304  hs_service_t *service = NULL;
3305  hs_service_intro_point_t *ip = NULL;
3306  hs_service_descriptor_t *desc = NULL;
3307 
3308  tor_assert(circ);
3309 
3310  /* Let's do some basic sanity checking of the circ state */
3311  if (BUG(!circ->cpath)) {
3312  return;
3313  }
3314  if (BUG(TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)) {
3315  return;
3316  }
3317  if (BUG(!circ->hs_ident)) {
3318  return;
3319  }
3320 
3321  /* Get the corresponding service and intro point. */
3322  get_objects_from_ident(circ->hs_ident, &service, &ip, &desc);
3323 
3324  if (service == NULL) {
3325  log_warn(LD_REND, "Unknown service identity key %s on the introduction "
3326  "circuit %u. Can't find onion service.",
3327  safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
3328  TO_CIRCUIT(circ)->n_circ_id);
3329  goto err;
3330  }
3331  if (ip == NULL) {
3332  log_warn(LD_REND, "Unknown introduction point auth key on circuit %u "
3333  "for service %s",
3334  TO_CIRCUIT(circ)->n_circ_id,
3335  safe_str_client(service->onion_address));
3336  goto err;
3337  }
3338  /* We can't have an IP object without a descriptor. */
3339  tor_assert(desc);
3340 
3341  if (hs_circ_service_intro_has_opened(service, ip, desc, circ)) {
3342  /* Getting here means that the circuit has been re-purposed because we
3343  * have enough intro circuit opened. Remove the IP from the service. */
3344  service_intro_point_remove(service, ip);
3345  service_intro_point_free(ip);
3346  }
3347 
3348  goto done;
3349 
3350  err:
3351  /* Close circuit, we can't use it. */
3352  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOSUCHSERVICE);
3353  done:
3354  return;
3355 }
3356 
3357 /** Called when a rendezvous circuit is done building and ready to be used. */
3358 static void
3360 {
3361  hs_service_t *service = NULL;
3362 
3363  tor_assert(circ);
3364  tor_assert(circ->cpath);
3365  /* Getting here means this is a v3 rendezvous circuit. */
3366  tor_assert(circ->hs_ident);
3368 
3369  /* Declare the circuit dirty to avoid reuse, and for path-bias. We set the
3370  * timestamp regardless of its content because that circuit could have been
3371  * cannibalized so in any cases, we are about to use that circuit more. */
3372  TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
3374 
3375  /* Get the corresponding service and intro point. */
3376  get_objects_from_ident(circ->hs_ident, &service, NULL, NULL);
3377  if (service == NULL) {
3378  log_warn(LD_REND, "Unknown service identity key %s on the rendezvous "
3379  "circuit %u with cookie %s. Can't find onion service.",
3380  safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
3381  TO_CIRCUIT(circ)->n_circ_id,
3382  hex_str((const char *) circ->hs_ident->rendezvous_cookie,
3383  REND_COOKIE_LEN));
3384  goto err;
3385  }
3386 
3387  /* If the cell can't be sent, the circuit will be closed within this
3388  * function. */
3389  hs_circ_service_rp_has_opened(service, circ);
3390  goto done;
3391 
3392  err:
3393  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOSUCHSERVICE);
3394  done:
3395  return;
3396 }
3397 
3398 /** We've been expecting an INTRO_ESTABLISHED cell on this circuit and it just
3399  * arrived. Handle the INTRO_ESTABLISHED cell arriving on the given
3400  * introduction circuit. Return 0 on success else a negative value. */
3401 static int
3403  const uint8_t *payload,
3404  size_t payload_len)
3405 {
3406  hs_service_t *service = NULL;
3407  hs_service_intro_point_t *ip = NULL;
3408 
3409  tor_assert(circ);
3410  tor_assert(payload);
3412 
3413  /* We need the service and intro point for this cell. */
3414  get_objects_from_ident(circ->hs_ident, &service, &ip, NULL);
3415 
3416  /* Get service object from the circuit identifier. */
3417  if (service == NULL) {
3418  log_warn(LD_REND, "Unknown service identity key %s on the introduction "
3419  "circuit %u. Can't find onion service.",
3420  safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
3421  TO_CIRCUIT(circ)->n_circ_id);
3422  goto err;
3423  }
3424  if (ip == NULL) {
3425  /* We don't recognize the key. */
3426  log_warn(LD_REND, "Introduction circuit established without an intro "
3427  "point object on circuit %u for service %s",
3428  TO_CIRCUIT(circ)->n_circ_id,
3429  safe_str_client(service->onion_address));
3430  goto err;
3431  }
3432 
3433  /* Try to parse the payload into a cell making sure we do actually have a
3434  * valid cell. On success, the ip object and circuit purpose is updated to
3435  * reflect the fact that the introduction circuit is established. */
3436  if (hs_circ_handle_intro_established(service, ip, circ, payload,
3437  payload_len) < 0) {
3438  goto err;
3439  }
3440 
3441  log_info(LD_REND, "Successfully received an INTRO_ESTABLISHED cell "
3442  "on circuit %u for service %s",
3443  TO_CIRCUIT(circ)->n_circ_id,
3444  safe_str_client(service->onion_address));
3445  return 0;
3446 
3447  err:
3448  return -1;
3449 }
3450 
3451 /** We just received an INTRODUCE2 cell on the established introduction circuit
3452  * circ. Handle the cell and return 0 on success else a negative value. */
3453 static int
3454 service_handle_introduce2(origin_circuit_t *circ, const uint8_t *payload,
3455  size_t payload_len)
3456 {
3457  hs_service_t *service = NULL;
3458  hs_service_intro_point_t *ip = NULL;
3459  hs_service_descriptor_t *desc = NULL;
3460 
3461  tor_assert(circ);
3462  tor_assert(payload);
3463  tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_INTRO);
3464 
3465  /* We'll need every object associated with this circuit. */
3466  get_objects_from_ident(circ->hs_ident, &service, &ip, &desc);
3467 
3468  /* Get service object from the circuit identifier. */
3469  if (service == NULL) {
3470  log_warn(LD_BUG, "Unknown service identity key %s when handling "
3471  "an INTRODUCE2 cell on circuit %u",
3472  safe_str_client(ed25519_fmt(&circ->hs_ident->identity_pk)),
3473  TO_CIRCUIT(circ)->n_circ_id);
3474  goto err;
3475  }
3476  if (ip == NULL) {
3477  /* We don't recognize the key. */
3478  log_warn(LD_BUG, "Unknown introduction auth key when handling "
3479  "an INTRODUCE2 cell on circuit %u for service %s",
3480  TO_CIRCUIT(circ)->n_circ_id,
3481  safe_str_client(service->onion_address));
3482  goto err;
3483  }
3484  /* If we have an IP object, we MUST have a descriptor object. */
3485  tor_assert(desc);
3486 
3487  /* The following will parse, decode and launch the rendezvous point circuit.
3488  * Both current and legacy cells are handled. */
3489  if (hs_circ_handle_introduce2(service, circ, ip, &desc->desc->subcredential,
3490  payload, payload_len) < 0) {
3491  goto err;
3492  }
3493 
3494  return 0;
3495  err:
3496  return -1;
3497 }
3498 
3499 /** Add to list every filename used by service. This is used by the sandbox
3500  * subsystem. */
3501 static void
3503 {
3504  const char *s_dir;
3505  char fname[128] = {0};
3506 
3507  tor_assert(service);
3508  tor_assert(list);
3509 
3510  /* Ease our life. */
3511  s_dir = service->config.directory_path;
3512  /* The hostname file. */
3513  smartlist_add(list, hs_path_from_filename(s_dir, fname_hostname));
3514  /* The key files splitted in two. */
3515  tor_snprintf(fname, sizeof(fname), "%s_secret_key", fname_keyfile_prefix);
3516  smartlist_add(list, hs_path_from_filename(s_dir, fname));
3517  tor_snprintf(fname, sizeof(fname), "%s_public_key", fname_keyfile_prefix);
3518  smartlist_add(list, hs_path_from_filename(s_dir, fname));
3519 }
3520 
3521 /** Return true iff the given service identity key is present on disk. */
3522 static int
3523 service_key_on_disk(const char *directory_path)
3524 {
3525  int ret = 0;
3526  char *fname;
3527  ed25519_keypair_t *kp = NULL;
3528 
3529  tor_assert(directory_path);
3530 
3531  /* Build the v3 key path name and then try to load it. */
3532  fname = hs_path_from_filename(directory_path, fname_keyfile_prefix);
3533  kp = ed_key_init_from_file(fname, INIT_ED_KEY_SPLIT,
3534  LOG_DEBUG, NULL, 0, 0, 0, NULL, NULL);
3535  if (kp) {
3536  ret = 1;
3537  }
3538 
3539  ed25519_keypair_free(kp);
3540  tor_free(fname);
3541 
3542  return ret;
3543 }
3544 
3545 /** This is a proxy function before actually calling hs_desc_encode_descriptor
3546  * because we need some preprocessing here */
3547 static int
3549  const hs_service_descriptor_t *desc,
3550  const ed25519_keypair_t *signing_kp,
3551  char **encoded_out)
3552 {
3553  int ret;
3554  const uint8_t *descriptor_cookie = NULL;
3555 
3556  tor_assert(service);
3557  tor_assert(desc);
3558  tor_assert(encoded_out);
3559 
3560  /* If the client authorization is enabled, send the descriptor cookie to
3561  * hs_desc_encode_descriptor. Otherwise, send NULL */
3562  if (service->config.is_client_auth_enabled) {
3563  descriptor_cookie = desc->descriptor_cookie;
3564  }
3565 
3566  ret = hs_desc_encode_descriptor(desc->desc, signing_kp,
3567  descriptor_cookie, encoded_out);
3568 
3569  return ret;
3570 }
3571 
3572 /* ========== */
3573 /* Public API */
3574 /* ========== */
3575 
3576 /** This is called everytime the service map (v2 or v3) changes that is if an
3577  * element is added or removed. */
3578 void
3580 {
3581  /* If we now have services where previously we had not, we need to enable
3582  * the HS service main loop event. If we changed to having no services, we
3583  * need to disable the event. */
3585 }
3586 
3587 /** Upload an encoded descriptor in encoded_desc of the given version. This
3588  * descriptor is for the service identity_pk and blinded_pk used to setup the
3589  * directory connection identifier. It is uploaded to the directory hsdir_rs
3590  * routerstatus_t object.
3591  *
3592  * NOTE: This function does NOT check for PublishHidServDescriptors because it
3593  * is only used by the control port command HSPOST outside of this subsystem.
3594  * Inside this code, upload_descriptor_to_hsdir() should be used. */
3595 void
3596 hs_service_upload_desc_to_dir(const char *encoded_desc,
3597  const uint8_t version,
3598  const ed25519_public_key_t *identity_pk,
3599  const ed25519_public_key_t *blinded_pk,
3600  const routerstatus_t *hsdir_rs)
3601 {
3602  char version_str[4] = {0};
3603  directory_request_t *dir_req;
3604  hs_ident_dir_conn_t ident;
3605 
3606  tor_assert(encoded_desc);
3607  tor_assert(identity_pk);
3608  tor_assert(blinded_pk);
3609  tor_assert(hsdir_rs);
3610 
3611  /* Setup the connection identifier. */
3612  memset(&ident, 0, sizeof(ident));
3613  hs_ident_dir_conn_init(identity_pk, blinded_pk, &ident);
3614 
3615  /* This is our resource when uploading which is used to construct the URL
3616  * with the version number: "/tor/hs/<version>/publish". */
3617  tor_snprintf(version_str, sizeof(version_str), "%u", version);
3618 
3619  /* Build the directory request for this HSDir. */
3621  directory_request_set_routerstatus(dir_req, hsdir_rs);
3623  directory_request_set_resource(dir_req, version_str);
3624  directory_request_set_payload(dir_req, encoded_desc,
3625  strlen(encoded_desc));
3626  /* The ident object is copied over the directory connection object once
3627  * the directory request is initiated. */
3628  directory_request_upload_set_hs_ident(dir_req, &ident);
3629 
3630  /* Initiate the directory request to the hsdir.*/
3631  directory_initiate_request(dir_req);
3632  directory_request_free(dir_req);
3633 }
3634 
3635 /** Add the ephemeral service using the secret key sk and ports. Both max
3636  * streams parameter will be set in the newly created service.
3637  *
3638  * Ownership of sk and ports is passed to this routine. Regardless of
3639  * success/failure, callers should not touch these values after calling this
3640  * routine, and may assume that correct cleanup has been done on failure.
3641  *
3642  * Return an appropriate hs_service_add_ephemeral_status_t. */
3645  int max_streams_per_rdv_circuit,
3646  int max_streams_close_circuit, char **address_out)
3647 {
3649  hs_service_t *service = NULL;
3650 
3651  tor_assert(sk);
3652  tor_assert(ports);
3653  tor_assert(address_out);
3654 
3655  service = hs_service_new(get_options());
3656 
3657  /* Setup the service configuration with specifics. A default service is
3658  * HS_VERSION_TWO so explicitly set it. */
3659  service->config.version = HS_VERSION_THREE;
3660  service->config.max_streams_per_rdv_circuit = max_streams_per_rdv_circuit;
3661  service->config.max_streams_close_circuit = !!max_streams_close_circuit;
3662  service->config.is_ephemeral = 1;
3663  smartlist_free(service->config.ports);
3664  service->config.ports = ports;
3665 
3666  /* Handle the keys. */
3667  memcpy(&service->keys.identity_sk, sk, sizeof(service->keys.identity_sk));
3669  &service->keys.identity_sk) < 0) {
3670  log_warn(LD_CONFIG, "Unable to generate ed25519 public key"
3671  "for v3 service.");
3672  ret = RSAE_BADPRIVKEY;
3673  goto err;
3674  }
3675 
3676  if (ed25519_validate_pubkey(&service->keys.identity_pk) < 0) {
3677  log_warn(LD_CONFIG, "Bad ed25519 private key was provided");
3678  ret = RSAE_BADPRIVKEY;
3679  goto err;
3680  }
3681 
3682  /* Make sure we have at least one port. */
3683  if (smartlist_len(service->config.ports) == 0) {
3684  log_warn(LD_CONFIG, "At least one VIRTPORT/TARGET must be specified "
3685  "for v3 service.");
3686  ret = RSAE_BADVIRTPORT;
3687  goto err;
3688  }
3689 
3690  /* Build the onion address for logging purposes but also the control port
3691  * uses it for the HS_DESC event. */
3692  hs_build_address(&service->keys.identity_pk,
3693  (uint8_t) service->config.version,
3694  service->onion_address);
3695 
3696  /* The only way the registration can fail is if the service public key
3697  * already exists. */
3698  if (BUG(register_service(hs_service_map, service) < 0)) {
3699  log_warn(LD_CONFIG, "Onion Service private key collides with an "
3700  "existing v3 service.");
3701  ret = RSAE_ADDREXISTS;
3702  goto err;
3703  }
3704 
3705  log_info(LD_CONFIG, "Added ephemeral v3 onion service: %s",
3706  safe_str_client(service->onion_address));
3707 
3708  *address_out = tor_strdup(service->onion_address);
3709  ret = RSAE_OKAY;
3710  goto end;
3711 
3712  err:
3713  hs_service_free(service);
3714 
3715  end:
3716  memwipe(sk, 0, sizeof(ed25519_secret_key_t));
3717  tor_free(sk);
3718  return ret;
3719 }
3720 
3721 /** For the given onion address, delete the ephemeral service. Return 0 on
3722  * success else -1 on error. */
3723 int
3724 hs_service_del_ephemeral(const char *address)
3725 {
3726  uint8_t version;
3728  hs_service_t *service = NULL;
3729 
3730  tor_assert(address);
3731 
3732  if (hs_parse_address(address, &pk, NULL, &version) < 0) {
3733  log_warn(LD_CONFIG, "Requested malformed v3 onion address for removal.");
3734  goto err;
3735  }
3736 
3737  if (version != HS_VERSION_THREE) {
3738  log_warn(LD_CONFIG, "Requested version of onion address for removal "
3739  "is not supported.");
3740  goto err;
3741  }
3742 
3743  service = find_service(hs_service_map, &pk);
3744  if (service == NULL) {
3745  log_warn(LD_CONFIG, "Requested non-existent v3 hidden service for "
3746  "removal.");
3747  goto err;
3748  }
3749 
3750  if (!service->config.is_ephemeral) {
3751  log_warn(LD_CONFIG, "Requested non-ephemeral v3 hidden service for "
3752  "removal.");
3753  goto err;
3754  }
3755 
3756  /* Close introduction circuits, remove from map and finally free. Notice
3757  * that the rendezvous circuits aren't closed in order for any existing
3758  * connections to finish. We let the application terminate them. */
3760  remove_service(hs_service_map, service);
3761  hs_service_free(service);
3762 
3763  log_info(LD_CONFIG, "Removed ephemeral v3 hidden service: %s",
3764  safe_str_client(address));
3765  return 0;
3766 
3767  err:
3768  return -1;
3769 }
3770 
3771 /** Using the ed25519 public key pk, find a service for that key and return the
3772  * current encoded descriptor as a newly allocated string or NULL if not
3773  * found. This is used by the control port subsystem. */
3774 char *
3776 {
3777  const hs_service_t *service;
3778 
3779  tor_assert(pk);
3780 
3781  service = find_service(hs_service_map, pk);
3782  if (service && service->desc_current) {
3783  char *encoded_desc = NULL;
3784  /* No matter what is the result (which should never be a failure), return
3785  * the encoded variable, if success it will contain the right thing else
3786  * it will be NULL. */
3787  service_encode_descriptor(service,
3788  service->desc_current,
3789  &service->desc_current->signing_kp,
3790  &encoded_desc);
3791  return encoded_desc;
3792  }
3793 
3794  return NULL;
3795 }
3796 
3797 /** Return the number of service we have configured and usable. */
3798 MOCK_IMPL(unsigned int,
3800 {
3801  if (hs_service_map == NULL) {
3802  return 0;
3803  }
3804  return HT_SIZE(hs_service_map);
3805 }
3806 
3807 /** Given conn, a rendezvous edge connection acting as an exit stream, look up
3808  * the hidden service for the circuit circ, and look up the port and address
3809  * based on the connection port. Assign the actual connection address.
3810  *
3811  * Return 0 on success. Return -1 on failure and the caller should NOT close
3812  * the circuit. Return -2 on failure and the caller MUST close the circuit for
3813  * security reasons. */
3814 int
3816  edge_connection_t *conn)
3817 {
3818  hs_service_t *service = NULL;
3819 
3820  tor_assert(circ);
3821  tor_assert(conn);
3823  tor_assert(circ->hs_ident);
3824 
3825  get_objects_from_ident(circ->hs_ident, &service, NULL, NULL);
3826 
3827  if (service == NULL) {
3828  log_warn(LD_REND, "Unable to find any hidden service associated "
3829  "identity key %s on rendezvous circuit %u.",
3830  ed25519_fmt(&circ->hs_ident->identity_pk),
3831  TO_CIRCUIT(circ)->n_circ_id);
3832  /* We want the caller to close the circuit because it's not a valid
3833  * service so no danger. Attempting to bruteforce the entire key space by
3834  * opening circuits to learn which service is being hosted here is
3835  * impractical. */
3836  goto err_close;
3837  }
3838 
3839  /* Enforce the streams-per-circuit limit, and refuse to provide a mapping if
3840  * this circuit will exceed the limit. */
3841  if (service->config.max_streams_per_rdv_circuit > 0 &&
3842  (circ->hs_ident->num_rdv_streams >=
3843  service->config.max_streams_per_rdv_circuit)) {
3844 #define MAX_STREAM_WARN_INTERVAL 600
3845  static struct ratelim_t stream_ratelim =
3846  RATELIM_INIT(MAX_STREAM_WARN_INTERVAL);
3847  log_fn_ratelim(&stream_ratelim, LOG_WARN, LD_REND,
3848  "Maximum streams per circuit limit reached on "
3849  "rendezvous circuit %u for service %s. Circuit has "
3850  "%" PRIu64 " out of %" PRIu64 " streams. %s.",
3851  TO_CIRCUIT(circ)->n_circ_id,
3852  service->onion_address,
3853  circ->hs_ident->num_rdv_streams,
3856  "Closing circuit" : "Ignoring open stream request");
3857  if (service->config.max_streams_close_circuit) {
3858  /* Service explicitly configured to close immediately. */
3859  goto err_close;
3860  }
3861  /* Exceeding the limit makes tor silently ignore the stream creation
3862  * request and keep the circuit open. */
3863  goto err_no_close;
3864  }
3865 
3866  /* Find a virtual port of that service mathcing the one in the connection if
3867  * successful, set the address in the connection. */
3868  if (hs_set_conn_addr_port(service->config.ports, conn) < 0) {
3869  log_info(LD_REND, "No virtual port mapping exists for port %d for "
3870  "hidden service %s.",
3871  TO_CONN(conn)->port, service->onion_address);
3872  if (service->config.allow_unknown_ports) {
3873  /* Service explicitly allow connection to unknown ports so close right
3874  * away because we do not care about port mapping. */
3875  goto err_close;
3876  }
3877  /* If the service didn't explicitly allow it, we do NOT close the circuit
3878  * here to raise the bar in terms of performance for port mapping. */
3879  goto err_no_close;
3880  }
3881 
3882  /* Success. */
3883  return 0;
3884  err_close:
3885  /* Indicate the caller that the circuit should be closed. */
3886  return -2;
3887  err_no_close:
3888  /* Indicate the caller to NOT close the circuit. */
3889  return -1;
3890 }
3891 
3892 /** Does the service with identity pubkey <b>pk</b> export the circuit IDs of
3893  * its clients? */
3896 {
3897  hs_service_t *service = find_service(hs_service_map, pk);
3898  if (!service) {
3900  }
3901 
3902  return service->config.circuit_id_protocol;
3903 }
3904 
3905 /** Add to file_list every filename used by a configured hidden service, and to
3906  * dir_list every directory path used by a configured hidden service. This is
3907  * used by the sandbox subsystem to allowlist those. */
3908 void
3910  smartlist_t *dir_list)
3911 {
3912  tor_assert(file_list);
3913  tor_assert(dir_list);
3914 
3915  /* Add files and dirs for legacy services. */
3916  rend_services_add_filenames_to_lists(file_list, dir_list);
3917 
3918  /* Add files and dirs for v3+. */
3919  FOR_EACH_SERVICE_BEGIN(service) {
3920  /* Skip ephemeral service, they don't touch the disk. */
3921  if (service->config.is_ephemeral) {
3922  continue;
3923  }
3924  service_add_fnames_to_list(service, file_list);
3925  smartlist_add_strdup(dir_list, service->config.directory_path);
3926  smartlist_add_strdup(dir_list, dname_client_pubkeys);
3927  } FOR_EACH_DESCRIPTOR_END;
3928 }
3929 
3930 /** Called when our internal view of the directory has changed. We might have
3931  * received a new batch of descriptors which might affect the shape of the
3932  * HSDir hash ring. Signal that we should reexamine the hash ring and
3933  * re-upload our HS descriptors if needed. */
3934 void
3936 {
3937  if (hs_service_get_num_services() > 0) {
3938  /* New directory information usually goes every consensus so rate limit
3939  * every 30 minutes to not be too conservative. */
3940  static struct ratelim_t dir_info_changed_ratelim = RATELIM_INIT(30 * 60);
3941  log_fn_ratelim(&dir_info_changed_ratelim, LOG_INFO, LD_REND,
3942  "New dirinfo arrived: consider reuploading descriptor");
3944  }
3945 }
3946 
3947 /** Called when we get an INTRODUCE2 cell on the circ. Respond to the cell and
3948  * launch a circuit to the rendezvous point. */
3949 int
3950 hs_service_receive_introduce2(origin_circuit_t *circ, const uint8_t *payload,
3951  size_t payload_len)
3952 {
3953  int ret = -1;
3954 
3955  tor_assert(circ);
3956  tor_assert(payload);
3957 
3958  /* Do some initial validation and logging before we parse the cell */
3959  if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_INTRO) {
3960  log_warn(LD_PROTOCOL, "Received an INTRODUCE2 cell on a "
3961  "non introduction circuit of purpose %d",
3962  TO_CIRCUIT(circ)->purpose);
3963  goto done;
3964  }
3965 
3966  if (circ->hs_ident) {
3967  ret = service_handle_introduce2(circ, payload, payload_len);
3969  } else {
3970  ret = rend_service_receive_introduction(circ, payload, payload_len);
3972  }
3973 
3974  done:
3975  return ret;
3976 }
3977 
3978 /** Called when we get an INTRO_ESTABLISHED cell. Mark the circuit as an
3979  * established introduction point. Return 0 on success else a negative value
3980  * and the circuit is closed. */
3981 int
3983  const uint8_t *payload,
3984  size_t payload_len)
3985 {
3986  int ret = -1;
3987 
3988  tor_assert(circ);
3989  tor_assert(payload);
3990 
3991  if (TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
3992  log_warn(LD_PROTOCOL, "Received an INTRO_ESTABLISHED cell on a "
3993  "non introduction circuit of purpose %d",
3994  TO_CIRCUIT(circ)->purpose);
3995  goto err;
3996  }
3997 
3998  /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
3999  * identifier hs_ident. Can't be both. */
4000  if (circ->hs_ident) {
4001  ret = service_handle_intro_established(circ, payload, payload_len);
4002  } else {
4003  ret = rend_service_intro_established(circ, payload, payload_len);
4004  }
4005 
4006  if (ret < 0) {
4007  goto err;
4008  }
4009  return 0;
4010  err:
4011  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
4012  return -1;
4013 }
4014 
4015 /** Called when any kind of hidden service circuit is done building thus
4016  * opened. This is the entry point from the circuit subsystem. */
4017 void
4019 {
4020  tor_assert(circ);
4021 
4022  /* Handle both version. v2 uses rend_data and v3 uses the hs circuit
4023  * identifier hs_ident. Can't be both. */
4024  switch (TO_CIRCUIT(circ)->purpose) {
4026  if (circ->hs_ident) {
4028  } else {
4030  }
4031  break;
4033  if (circ->hs_ident) {
4035  } else {
4037  }
4038  break;
4039  default:
4040  tor_assert(0);
4041  }
4042 }
4043 
4044 /** Return the service version by looking at the key in the service directory.
4045  * If the key is not found or unrecognized, -1 is returned. Else, the service
4046  * version is returned. */
4047 int
4049 {
4050  int version = -1; /* Unknown version. */
4051  const char *directory_path;
4052 
4053  tor_assert(service);
4054 
4055  /* We'll try to load the key for version 3. If not found, we'll try version
4056  * 2 and if not found, we'll send back an unknown version (-1). */
4057  directory_path = service->config.directory_path;
4058 
4059  /* Version 3 check. */
4060  if (service_key_on_disk(directory_path)) {
4061  version = HS_VERSION_THREE;
4062  goto end;
4063  }
4064  /* Version 2 check. */
4065  if (rend_service_key_on_disk(directory_path)) {
4066  version = HS_VERSION_TWO;
4067  goto end;
4068  }
4069 
4070  end:
4071  return version;
4072 }
4073 
4074 /** Load and/or generate keys for all onion services including the client
4075  * authorization if any. Return 0 on success, -1 on failure. */
4076 int
4078 {
4079  /* Load v2 service keys if we have v2. */
4080  if (rend_num_services() != 0) {
4081  if (rend_service_load_all_keys(NULL) < 0) {
4082  goto err;
4083  }
4084  }
4085 
4086  /* Load or/and generate them for v3+. */
4088  /* Ignore ephemeral service, they already have their keys set. */
4089  if (service->config.is_ephemeral) {
4090  continue;
4091  }
4092  log_info(LD_REND, "Loading v3 onion service keys from %s",
4093  service_escaped_dir(service));
4094  if (load_service_keys(service) < 0) {
4095  goto err;
4096  }
4097  } SMARTLIST_FOREACH_END(service);
4098 
4099  /* Final step, the staging list contains service in a quiescent state that
4100  * is ready to be used. Register them to the global map. Once this is over,
4101  * the staging list will be cleaned up. */
4103 
4104  /* All keys have been loaded successfully. */
4105  return 0;
4106  err:
4107  return -1;
4108 }
4109 
4110 /** Log the status of introduction points for all version 3 onion services
4111  * at log severity <b>severity</b>.
4112  */
4113 void
4115 {
4116  origin_circuit_t *circ;
4117 
4119 
4120  tor_log(severity, LD_GENERAL, "Service configured in %s:",
4121  service_escaped_dir(hs));
4122  FOR_EACH_DESCRIPTOR_BEGIN(hs, desc) {
4123 
4124  DIGEST256MAP_FOREACH(desc->intro_points.map, key,
4125  hs_service_intro_point_t *, ip) {
4126  const node_t *intro_node;
4127  const char *nickname;
4128 
4129  intro_node = get_node_from_intro_point(ip);
4130  if (!intro_node) {
4131  tor_log(severity, LD_GENERAL, " Couldn't find intro point, "
4132  "skipping");
4133  continue;
4134  }
4135  nickname = node_get_nickname(intro_node);
4136  if (!nickname) {
4137  continue;
4138  }
4139 
4140  circ = hs_circ_service_get_intro_circ(ip);
4141  if (!circ) {
4142  tor_log(severity, LD_GENERAL, " Intro point at %s: no circuit",
4143  nickname);
4144  continue;
4145  }
4146  tor_log(severity, LD_GENERAL, " Intro point %s: circuit is %s",
4147  nickname, circuit_state_to_string(circ->base_.state));
4148  } DIGEST256MAP_FOREACH_END;
4149 
4150  } FOR_EACH_DESCRIPTOR_END;
4151  } FOR_EACH_SERVICE_END;
4152 }
4153 
4154 /** Put all service object in the given service list. After this, the caller
4155  * looses ownership of every elements in the list and responsible to free the
4156  * list pointer. */
4157 void
4159 {
4160  tor_assert(service_list);
4161  /* This list is freed at registration time but this function can be called
4162  * multiple time. */
4163  if (hs_service_staging_list == NULL) {
4165  }
4166  /* Add all service object to our staging list. Caller is responsible for
4167  * freeing the service_list. */
4169 }
4170 
4171 /** Allocate and initilize a service object. The service configuration will
4172  * contain the default values. Return the newly allocated object pointer. This
4173  * function can't fail. */
4174 hs_service_t *
4176 {
4177  hs_service_t *service = tor_malloc_zero(sizeof(hs_service_t));
4178  /* Set default configuration value. */
4179  set_service_default_config(&service->config, options);
4180  /* Set the default service version. */
4182  /* Allocate the CLIENT_PK replay cache in service state. */
4183  service->state.replay_cache_rend_cookie =
4185 
4186  return service;
4187 }
4188 
4189 /** Free the given <b>service</b> object and all its content. This function
4190  * also takes care of wiping service keys from memory. It is safe to pass a
4191  * NULL pointer. */
4192 void
4194 {
4195  if (service == NULL) {
4196  return;
4197  }
4198 
4199  /* Free descriptors. Go over both descriptor with this loop. */
4200  FOR_EACH_DESCRIPTOR_BEGIN(service, desc) {
4201  service_descriptor_free(desc);
4202  } FOR_EACH_DESCRIPTOR_END;
4203 
4204  /* Free service configuration. */
4205  service_clear_config(&service->config);
4206 
4207  /* Free replay cache from state. */
4208  if (service->state.replay_cache_rend_cookie) {
4210  }
4211 
4212  /* Free onionbalance subcredentials (if any) */
4213  if (service->state.ob_subcreds) {
4214  tor_free(service->state.ob_subcreds);
4215  }
4216 
4217  /* Wipe service keys. */
4218  memwipe(&service->keys.identity_sk, 0, sizeof(service->keys.identity_sk));
4219 
4220  tor_free(service);
4221 }
4222 
4223 /** Periodic callback. Entry point from the main loop to the HS service
4224  * subsystem. This is call every second. This is skipped if tor can't build a
4225  * circuit or the network is disabled. */
4226 void
4228 {
4229  /* First thing we'll do here is to make sure our services are in a
4230  * quiescent state for the scheduled events. */
4232 
4233  /* Order matters here. We first make sure the descriptor object for each
4234  * service contains the latest data. Once done, we check if we need to open
4235  * new introduction circuit. Finally, we try to upload the descriptor for
4236  * each service. */
4237 
4238  /* Make sure descriptors are up to date. */
4240  /* Make sure services have enough circuits. */
4242  /* Upload the descriptors if needed/possible. */
4244 }
4245 
4246 /** Initialize the service HS subsystem. */
4247 void
4249 {
4250  /* Should never be called twice. */
4253 
4254  /* v2 specific. */
4255  rend_service_init();
4256 
4257  hs_service_map = tor_malloc_zero(sizeof(struct hs_service_ht));
4258  HT_INIT(hs_service_ht, hs_service_map);
4259 
4261 }
4262 
4263 /** Release all global storage of the hidden service subsystem. */
4264 void
4266 {
4268  service_free_all();
4270 }
4271 
4272 #ifdef TOR_UNIT_TESTS
4273 
4274 /** Return the global service map size. Only used by unit test. */
4275 STATIC unsigned int
4276 get_hs_service_map_size(void)
4277 {
4278  return HT_SIZE(hs_service_map);
4279 }
4280 
4281 /** Return the staging list size. Only used by unit test. */
4282 STATIC int
4283 get_hs_service_staging_list_size(void)
4284 {
4285  return smartlist_len(hs_service_staging_list);
4286 }
4287 
4288 STATIC hs_service_ht *
4289 get_hs_service_map(void)
4290 {
4291  return hs_service_map;
4292 }
4293 
4295 get_first_service(void)
4296 {
4297  hs_service_t **obj = HT_START(hs_service_ht, hs_service_map);
4298  if (obj == NULL) {
4299  return NULL;
4300  }
4301  return *obj;
4302 }
4303 
4304 #endif /* defined(TOR_UNIT_TESTS) */
client_filename_is_valid
STATIC int client_filename_is_valid(const char *filename)
Definition: hs_service.c:1096
intro_point_should_expire
STATIC int intro_point_should_expire(const hs_service_intro_point_t *ip, time_t now)
Definition: hs_service.c:2341
hs_service_t::config
hs_service_config_t config
Definition: hs_service.h:309
crypto_rand_int_range
int crypto_rand_int_range(unsigned int min, unsigned int max)
Definition: crypto_rand_numeric.c:71
rotate_all_descriptors
STATIC void rotate_all_descriptors(time_t now)
Definition: hs_service.c:2575
hs_config_free_all
void hs_config_free_all(void)
Definition: hs_config.c:740
tor_free
#define tor_free(p)
Definition: malloc.h:52
hs_service.h
Header file containing service data for the HS subsytem.
CURVE25519_PUBKEY_LEN
#define CURVE25519_PUBKEY_LEN
Definition: x25519_sizes.h:20
LOG_DEBUG
#define LOG_DEBUG
Definition: log.h:42
close_service_rp_circuits
static void close_service_rp_circuits(hs_service_t *service)
Definition: hs_service.c:763
tor_free_
void tor_free_(void *mem)
Definition: malloc.c:227
smartlist_split_string
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)
Definition: smartlist_split.c:37
FOR_EACH_SERVICE_BEGIN
#define FOR_EACH_SERVICE_BEGIN(var)
Definition: hs_service.c:74
hs_service_intro_point_t::base
hs_intropoint_t base
Definition: hs_service.h:40
hex_str
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
parse_authorized_client
STATIC hs_service_authorized_client_t * parse_authorized_client(const char *client_key_str)
Definition: hs_service.c:1125
ht_free_service_
static int ht_free_service_(struct hs_service_t *service, void *data)
Definition: hs_service.c:386
service_authorized_client_free_
STATIC void service_authorized_client_free_(hs_service_authorized_client_t *client)
Definition: hs_service.c:1302
HS_VERSION_TWO
#define HS_VERSION_TWO
Definition: hs_common.h:24
service_intro_point_free_void
static void service_intro_point_free_void(void *obj)
Definition: hs_service.c:435
move_hs_state
static void move_hs_state(hs_service_t *src_service, hs_service_t *dst_service)
Definition: hs_service.c:879
INTRO_CIRC_RETRY_PERIOD
#define INTRO_CIRC_RETRY_PERIOD
Definition: hs_common.h:41
setup_desc_intro_point
static int setup_desc_intro_point(const ed25519_keypair_t *signing_kp, const hs_service_intro_point_t *ip, time_t now, hs_desc_intro_point_t *desc_ip)
Definition: hs_service.c:1575
service_authorized_client_dup
static hs_service_authorized_client_t * service_authorized_client_dup(const hs_service_authorized_client_t *client)
Definition: hs_service.c:1347
HS_DESC_CERT_LIFETIME
#define HS_DESC_CERT_LIFETIME
Definition: hs_descriptor.h:38
hs_service_receive_introduce2
int hs_service_receive_introduce2(origin_circuit_t *circ, const uint8_t *payload, size_t payload_len)
Definition: hs_service.c:3950
refresh_service_descriptor
static void refresh_service_descriptor(const hs_service_t *service, hs_service_descriptor_t *desc, time_t now)
Definition: hs_service.c:3226
hs_ident.h
Header file containing circuit and connection identifier data for the whole HS subsytem.
close_directory_connections
static void close_directory_connections(const hs_service_t *service, const hs_service_descriptor_t *desc)
Definition: hs_service.c:729
memwipe
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:55
hs_service_staging_list
static smartlist_t * hs_service_staging_list
Definition: hs_service.c:105
smartlist_add_strdup
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
Definition: smartlist_core.c:137
upload_descriptor_to_hsdir
static void upload_descriptor_to_hsdir(const hs_service_t *service, hs_service_descriptor_t *desc, const node_t *hsdir)
Definition: hs_service.c:2838
INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS
#define INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS
Definition: or.h:1082
directory_initiate_request
void directory_initiate_request(directory_request_t *request)
Definition: dirclient.c:1235
hs_service_exports_circuit_id
hs_circuit_id_protocol_t hs_service_exports_circuit_id(const ed25519_public_key_t *pk)
Definition: hs_service.c:3895
approx_time
time_t approx_time(void)
Definition: approx_time.c:32
ed25519_validate_pubkey
int ed25519_validate_pubkey(const ed25519_public_key_t *pubkey)
Definition: crypto_ed25519.c:796
MOCK_IMPL
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
tor_cert_create
tor_cert_t * tor_cert_create(const ed25519_keypair_t *signing_key, uint8_t cert_type, const ed25519_public_key_t *signed_key, time_t now, time_t lifetime, uint32_t flags)
Definition: torcert.c:131
hs_circ_service_rp_has_opened
void hs_circ_service_rp_has_opened(const hs_service_t *service, origin_circuit_t *circ)
Definition: hs_circuit.c:871
hs_service_init
void hs_service_init(void)
Definition: hs_service.c:4248
crypto_ope_t
Definition: crypto_ope.c:41
hs_service_free_
void hs_service_free_(hs_service_t *service)
Definition: hs_service.c:4193
rend_service_load_all_keys
int rend_service_load_all_keys(const smartlist_t *service_list)
Definition: rendservice.c:1389
hs_ident_dir_conn_t::blinded_pk
ed25519_public_key_t blinded_pk
Definition: hs_ident.h:95
hs_service_t::desc_next
hs_service_descriptor_t * desc_next
Definition: hs_service.h:314
sr_state_get_start_time_of_previous_protocol_run
time_t sr_state_get_start_time_of_previous_protocol_run(void)
Definition: shared_random_client.c:287
hs_set_conn_addr_port
int hs_set_conn_addr_port(const smartlist_t *ports, edge_connection_t *conn)
Definition: hs_common.c:857
hs_desc_encrypted_data_t
Definition: hs_descriptor.h:158
DIGESTMAP_FOREACH_MODIFY
#define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar)
Definition: map.h:165
hs_service_intropoints_t
Definition: hs_service.h:86
tor_assert
#define tor_assert(expr)
Definition: util_bug.h:102
hs_desc_intro_point_t::link_specifiers
smartlist_t * link_specifiers
Definition: hs_descriptor.h:102
crypto_ope.h
header for crypto_ope.c
CONN_TYPE_DIR
#define CONN_TYPE_DIR
Definition: connection.h:55
crypto_pk_new
crypto_pk_t * crypto_pk_new(void)
Definition: crypto_rsa_nss.c:165
LD_BUG
#define LD_BUG
Definition: log.h:86
ed25519_keypair_generate
int ed25519_keypair_generate(ed25519_keypair_t *keypair_out, int extra_strong)
Definition: crypto_ed25519.c:214
ed25519_pubkey_eq
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
Definition: crypto_ed25519.c:642
ed_key_init_from_file
ed25519_keypair_t * ed_key_init_from_file(const char *fname, uint32_t flags, int severity, const ed25519_keypair_t *signing_key, time_t now, time_t lifetime, uint8_t cert_type, struct tor_cert_st **cert_out, const or_options_t *options)
Definition: loadkey.c:379
hs_get_next_time_period_num
uint64_t hs_get_next_time_period_num(time_t now)
Definition: hs_common.c:305
hs_ident_circuit_t::identity_pk
ed25519_public_key_t identity_pk
Definition: hs_ident.h:45
rend_service_port_config_t
Definition: hs_common.h:153
fast_mem_is_zero
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
get_node_from_intro_point
STATIC const node_t * get_node_from_intro_point(const hs_service_intro_point_t *ip)
Definition: hs_service.c:665
circuituse.h
Header file for circuituse.c.
HS_CIRCUIT_ID_PROTOCOL_NONE
@ HS_CIRCUIT_ID_PROTOCOL_NONE
Definition: hs_service.h:189
close_intro_circuits
static void close_intro_circuits(hs_service_intropoints_t *intro_points)
Definition: hs_service.c:793
count_desc_circuit_established
STATIC unsigned int count_desc_circuit_established(const hs_service_descriptor_t *desc)
Definition: hs_service.c:712
MAX_INTRO_POINT_CIRCUIT_RETRIES
#define MAX_INTRO_POINT_CIRCUIT_RETRIES
Definition: or.h:1103
LD_GENERAL
#define LD_GENERAL
Definition: log.h:62
INTRO_POINT_LIFETIME_MAX_SECONDS
#define INTRO_POINT_LIFETIME_MAX_SECONDS
Definition: or.h:1098
hs_in_period_between_tp_and_srv
int hs_in_period_between_tp_and_srv(const networkstatus_t *consensus, time_t now)
Definition: hs_common.c:1105
base16_encode
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
DIGESTMAP_FOREACH_END
#define DIGESTMAP_FOREACH_END
Definition: map.h:168
DIR_PURPOSE_UPLOAD_HSDESC
#define DIR_PURPOSE_UPLOAD_HSDESC
Definition: directory.h:73
service_desc_note_upload
static void service_desc_note_upload(hs_service_descriptor_t *desc, const node_t *hsdir)
Definition: hs_service.c:2251
loadkey.h
Header file for loadkey.c.
crypto_pk_dup_key
crypto_pk_t * crypto_pk_dup_key(crypto_pk_t *orig)
Definition: crypto_rsa_nss.c:351
describe.h
Header file for describe.c.
hs_service_receive_intro_established
int hs_service_receive_intro_established(origin_circuit_t *circ, const uint8_t *payload, size_t payload_len)
Definition: hs_service.c:3982
hs_service_add_ephemeral
hs_service_add_ephemeral_status_t hs_service_add_ephemeral(ed25519_secret_key_t *sk, smartlist_t *ports, int max_streams_per_rdv_circuit, int max_streams_close_circuit, char **address_out)
Definition: hs_service.c:3644
get_max_intro_circ_per_period
static unsigned int get_max_intro_circ_per_period(const hs_service_t *service)
Definition: hs_service.c:2719
smartlist_add_all
void smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
Definition: smartlist_core.c:125
HS_SERVICE_NEXT_UPLOAD_TIME_MIN
#define HS_SERVICE_NEXT_UPLOAD_TIME_MIN
Definition: hs_service.h:33
hs_desc_build_fake_authorized_client
hs_desc_authorized_client_t * hs_desc_build_fake_authorized_client(void)
Definition: hs_descriptor.c:2865
pathbias_count_use_attempt
void pathbias_count_use_attempt(origin_circuit_t *circ)
Definition: circpathbias.c:604
get_extend_info_from_intro_point
static extend_info_t * get_extend_info_from_intro_point(const hs_service_intro_point_t *ip, unsigned int direct_conn)
Definition: hs_service.c:685
build_service_desc_encrypted
static int build_service_desc_encrypted(const hs_service_t *service, hs_service_descriptor_t *desc)
Definition: hs_service.c:1728
node_get_by_id
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:223
service_encode_descriptor
static int service_encode_descriptor(const hs_service_t *service, const hs_service_descriptor_t *desc, const ed25519_keypair_t *signing_kp, char **encoded_out)
Definition: hs_service.c:3548
hs_service_config_t::has_dos_defense_enabled
unsigned int has_dos_defense_enabled
Definition: hs_service.h:252
networkstatus_get_live_consensus
networkstatus_t * networkstatus_get_live_consensus(time_t now)
Definition: networkstatus.c:1416
smartlist_add
void smartlist_add(smartlist_t *sl, void *element)
Definition: smartlist_core.c:117
hs_descriptor_t::plaintext_data
hs_desc_plaintext_data_t plaintext_data
Definition: hs_descriptor.h:228
hs_service_ht_hash
static unsigned int hs_service_ht_hash(const hs_service_t *service)
Definition: hs_service.c:139
run_housekeeping_event
STATIC void run_housekeeping_event(time_t now)
Definition: hs_service.c:2605
hs_service_map_has_changed
void hs_service_map_has_changed(void)
Definition: hs_service.c:3579
hs_service_descriptor_t::previous_hsdirs
smartlist_t * previous_hsdirs
Definition: hs_service.h:166
hs_desc_plaintext_data_t::version
uint32_t version
Definition: hs_descriptor.h:196
replaycache_new
replaycache_t * replaycache_new(time_t horizon, time_t interval)
Definition: replaycache.c:47
LOG_DESC_UPLOAD_REASON_MAX
#define LOG_DESC_UPLOAD_REASON_MAX
Definition: hs_service.c:3083
hs_desc_plaintext_data_t::signing_pubkey
ed25519_public_key_t signing_pubkey
Definition: hs_descriptor.h:207
service_clear_config
STATIC void service_clear_config(hs_service_config_t *config)
Definition: hs_service.c:256
hs_service_del_ephemeral
int hs_service_del_ephemeral(const char *address)
Definition: hs_service.c:3724
ed25519_public_key_is_zero
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
Definition: crypto_ed25519.c:227
routerstatus_t
Definition: routerstatus_st.h:19
ed25519_pubkey_copy
void ed25519_pubkey_copy(ed25519_public_key_t *dest, const ed25519_public_key_t *src)
Definition: crypto_ed25519.c:654
hs_build_blinded_keypair
void hs_build_blinded_keypair(const ed25519_keypair_t *kp, const uint8_t *secret, size_t secret_len, uint64_t time_period_num, ed25519_keypair_t *blinded_kp_out)
Definition: hs_common.c:1069
HS_SERVICE_NEXT_UPLOAD_TIME_MAX
#define HS_SERVICE_NEXT_UPLOAD_TIME_MAX
Definition: hs_service.h:35
hs_service_intropoints_t::map
digest256map_t * map
Definition: hs_service.h:97
close_service_circuits
static void close_service_circuits(hs_service_t *service)
Definition: hs_service.c:822
hs_build_address
void hs_build_address(const ed25519_public_key_t *key, uint8_t version, char *addr_out)
Definition: hs_common.c:1019
hs_service_config_t::ob_master_pubkeys
smartlist_t * ob_master_pubkeys
Definition: hs_service.h:258
hs_service_config_t::is_single_onion
unsigned int is_single_onion
Definition: hs_service.h:239
write_address_to_file
STATIC int write_address_to_file(const hs_service_t *service, const char *fname_)
Definition: hs_service.c:979
HS_DESC_DEFAULT_LIFETIME
#define HS_DESC_DEFAULT_LIFETIME
Definition: hs_descriptor.h:29
hs_service_state_t
Definition: hs_service.h:262
set_service_default_config
static void set_service_default_config(hs_service_config_t *c, const or_options_t *options)
Definition: hs_service.c:234
service_handle_intro_established
static int service_handle_intro_established(origin_circuit_t *circ, const uint8_t *payload, size_t payload_len)
Definition: hs_service.c:3402
ED25519_PUBKEY_LEN
#define ED25519_PUBKEY_LEN
Definition: x25519_sizes.h:27
crypto_digest_add_bytes
void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data, size_t len)
Definition: crypto_digest_nss.c:308
smartlist_new
smartlist_t * smartlist_new(void)
Definition: smartlist_core.c:26
hs_desc_intro_point_t::auth_key_cert
tor_cert_t * auth_key_cert
Definition: hs_descriptor.h:111
hs_circ_service_get_intro_circ
origin_circuit_t * hs_circ_service_get_intro_circ(const hs_service_intro_point_t *ip)
Definition: hs_circuit.c:662
crypto_digest_t
Definition: crypto_digest_nss.c:166
HS_SERVICE_DEFAULT_VERSION
#define HS_SERVICE_DEFAULT_VERSION
Definition: hs_service.h:29
hs_desc_superencrypted_data_t::clients
smartlist_t * clients
Definition: hs_descriptor.h:183
hs_service_config_t::dir_group_readable
unsigned int dir_group_readable
Definition: hs_service.h:243
tor_snprintf
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
hs_control_desc_event_upload
void hs_control_desc_event_upload(const char *onion_address, const char *hsdir_id_digest, const ed25519_public_key_t *blinded_pk, const uint8_t *hsdir_index)
Definition: hs_control.c:133
hs_service_descriptor_t::desc
hs_descriptor_t * desc
Definition: hs_service.h:148
statefile.h
Header for statefile.c.
hs_desc_encrypted_data_t::single_onion_service
unsigned int single_onion_service
Definition: hs_descriptor.h:168
strcmpend
int strcmpend(const char *s1, const char *s2)
Definition: util_string.c:242
SMARTLIST_FOREACH
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Definition: smartlist_foreach.h:112
hs_service_descriptor_t::auth_ephemeral_kp
curve25519_keypair_t auth_ephemeral_kp
Definition: hs_service.h:125
rend_consider_descriptor_republication
void rend_consider_descriptor_republication(void)
Definition: rendservice.c:4358
extend_info_describe
const char * extend_info_describe(const extend_info_t *ei)
Definition: describe.c:202
ed25519_public_key_generate
int ed25519_public_key_generate(ed25519_public_key_t *pubkey_out, const ed25519_secret_key_t *seckey)
Definition: crypto_ed25519.c:202
hs_service_run_scheduled_events
void hs_service_run_scheduled_events(time_t now)
Definition: hs_service.c:4227
hs_circ_handle_introduce2
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:1018
hs_service_t::state
hs_service_state_t state
Definition: hs_service.h:303
hs_desc_intro_point_t::legacy
struct hs_desc_intro_point_t::@16 legacy
hs_desc_encrypted_data_t::intro_auth_types
smartlist_t * intro_auth_types
Definition: hs_descriptor.h:165
hs_service_descriptor_t::missing_intro_points
unsigned int missing_intro_points
Definition: hs_service.h:160
hs_circ_launch_intro_point
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:743
base32_decode
int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:90
hs_intropoint_t::link_specifiers
smartlist_t * link_specifiers
Definition: hs_intropoint.h:24
build_service_desc_plaintext
static void build_service_desc_plaintext(const hs_service_t *service, hs_service_descriptor_t *desc)
Definition: hs_service.c:1842
networkstatus.h
Header file for networkstatus.c.
hs_service_state_t::replay_cache_rend_cookie
replaycache_t * replay_cache_rend_cookie
Definition: hs_service.h:277
hsdir_index_t::store_first
uint8_t store_first[DIGEST256_LEN]
Definition: hsdir_index_st.h:24
should_service_upload_descriptor
static int should_service_upload_descriptor(const hs_service_t *service, const hs_service_descriptor_t *desc, time_t now)
Definition: hs_service.c:3144
ed25519_public_key_from_curve25519_public_key
int ed25519_public_key_from_curve25519_public_key(ed25519_public_key_t *pubkey, const curve25519_public_key_t *pubkey_in, int signbit)
Definition: crypto_ed25519.c:493
hs_service_intro_point_t::time_to_expire
time_t time_to_expire
Definition: hs_service.h:67
replaycache_free
#define replaycache_free(r)
Definition: replaycache.h:42
hs_service_set_conn_addr_port
int hs_service_set_conn_addr_port(const origin_circuit_t *circ, edge_connection_t *conn)
Definition: hs_service.c:3815
ed25519_fmt
const char * ed25519_fmt(const ed25519_public_key_t *pkey)
Definition: crypto_format.c:193
crypto_util.h
Common functions for cryptographic routines.
hs_service_add_ephemeral_status_t
hs_service_add_ephemeral_status_t
Definition: hs_common.h:142
rend_service_intro_established
int rend_service_intro_established(origin_circuit_t *circuit, const uint8_t *request, size_t request_len)
Definition: rendservice.c:3408
build_service_desc_keys
static int build_service_desc_keys(const hs_service_t *service, hs_service_descriptor_t *desc)
Definition: hs_service.c:1894
service_descriptor_free_
STATIC void service_descriptor_free_(hs_service_descriptor_t *desc)
Definition: hs_service.c:1313
hs_service_config_t::directory_path
char * directory_path
Definition: hs_service.h:211
hs_intropoint_t::is_only_legacy
unsigned int is_only_legacy
Definition: hs_intropoint.h:19
remove_service
STATIC void remove_service(hs_service_ht *map, hs_service_t *service)
Definition: hs_service.c:205
hs_service_stage_services
void hs_service_stage_services(const smartlist_t *service_list)
Definition: hs_service.c:4158
hs_service_intro_point_t::enc_key_kp
curve25519_keypair_t enc_key_kp
Definition: hs_service.h:51
circuitlist.h
Header file for circuitlist.c.
hs_desc_intro_point_t::enc_key
curve25519_public_key_t enc_key
Definition: hs_descriptor.h:114
mainloop.h
Header file for mainloop.c.
write_str_to_file
int write_str_to_file(const char *fname, const char *str, int bin)
Definition: files.c:258
DIGEST_LEN
#define DIGEST_LEN
Definition: digest_sizes.h:20
hs_ob_refresh_keys
void hs_ob_refresh_keys(hs_service_t *service)
Definition: hs_ob.c:364
launch_intro_point_circuits
static void launch_intro_point_circuits(hs_service_t *service)
Definition: hs_service.c:2662
build_desc_intro_points
static void build_desc_intro_points(const hs_service_t *service, hs_service_descriptor_t *desc, time_t now)
Definition: hs_service.c:1664
hs_desc_superencrypted_data_t::auth_ephemeral_pubkey
curve25519_public_key_t auth_ephemeral_pubkey
Definition: hs_descriptor.h:179
hs_service_intro_point_t::support_intro2_dos_defense
unsigned int support_intro2_dos_defense
Definition: hs_service.h:82
hs_desc_plaintext_data_t::revision_counter
uint64_t revision_counter
Definition: hs_descriptor.h:216
rotate_service_descriptors
static void rotate_service_descriptors(hs_service_t *service)
Definition: hs_service.c:2554
rate_limit_log
char * rate_limit_log(ratelim_t *lim, time_t now)
Definition: ratelim.c:41
hs_service_lists_fnames_for_sandbox
void hs_service_lists_fnames_for_sandbox(smartlist_t *file_list, smartlist_t *dir_list)
Definition: hs_service.c:3909
service_free_all
static void service_free_all(void)
Definition: hs_service.c:398
HS_DESC_AUTH_CLIENT_MULTIPLE
#define HS_DESC_AUTH_CLIENT_MULTIPLE
Definition: hs_descriptor.h:66
build_descriptors_for_new_service
static void build_descriptors_for_new_service(hs_service_t *service, time_t now)
Definition: hs_service.c:2017
crypto_format.h
Header for crypto_format.c.
RSAE_BADVIRTPORT
@ RSAE_BADVIRTPORT
Definition: hs_common.h:144
hs_service_authorized_client_t::client_pk
curve25519_public_key_t client_pk
Definition: hs_service.h:183
register_service
STATIC int register_service(hs_service_ht *map, hs_service_t *service)
Definition: hs_service.c:181
service_intro_point_find
STATIC hs_service_intro_point_t * service_intro_point_find(const hs_service_t *service, const ed25519_public_key_t *auth_key)
Definition: hs_service.c:555
hs_intropoint_clear
void hs_intropoint_clear(hs_intropoint_t *ip)
Definition: hs_intropoint.c:810
rend_service_free_all
void rend_service_free_all(void)
Definition: rendservice.c:285
hs_descriptor_clear_intro_points
void hs_descriptor_clear_intro_points(hs_descriptor_t *desc)
Definition: hs_descriptor.c:2947
hs_service_free_all
void hs_service_free_all(void)
Definition: hs_service.c:4265
hs_ident_circuit_t::intro_auth_pk
ed25519_public_key_t intro_auth_pk
Definition: hs_ident.h:51
LD_FS
#define LD_FS
Definition: log.h:70
hs_get_previous_time_period_num
uint64_t hs_get_previous_time_period_num(time_t now)
Definition: hs_common.c:314
load_client_keys
static int load_client_keys(hs_service_t *service)
Definition: hs_service.c:1200
hs_path_from_filename
char * hs_path_from_filename(const char *directory, const char *filename)
Definition: hs_common.c:179
hs_service_config_t::is_client_auth_enabled
unsigned int is_client_auth_enabled
Definition: hs_service.h:227
service_intro_point_add
STATIC void service_intro_point_add(digest256map_t *map, hs_service_intro_point_t *ip)
Definition: hs_service.c:522
hs_service_state_t::intro_circ_retry_started_time
time_t intro_circ_retry_started_time
Definition: hs_service.h:266
rend_service_receive_introduction
int rend_service_receive_introduction(origin_circuit_t *circuit, const uint8_t *request, size_t request_len)
Definition: rendservice.c:1881
rend_consider_services_intro_points
void rend_consider_services_intro_points(time_t now)
Definition: rendservice.c:4115
service_intro_point_free_
STATIC void service_intro_point_free_(hs_service_intro_point_t *ip)
Definition: hs_service.c:419
service_add_fnames_to_list
static void service_add_fnames_to_list(const hs_service_t *service, smartlist_t *list)
Definition: hs_service.c:3502
service_authorized_client_cmp
static int service_authorized_client_cmp(const hs_service_authorized_client_t *client1, const hs_service_authorized_client_t *client2)
Definition: hs_service.c:1367
node_t
Definition: node_st.h:34
origin_circuit_t
Definition: origin_circuit_st.h:79
directory_request_set_routerstatus
void directory_request_set_routerstatus(directory_request_t *req, const routerstatus_t *status)
Definition: dirclient.c:1162
directory_request_set_payload
void directory_request_set_payload(directory_request_t *req, const char *payload, size_t payload_len)
Definition: dirclient.c:1056
hs_ident_dir_conn_init
void hs_ident_dir_conn_init(const ed25519_public_key_t *identity_pk, const ed25519_public_key_t *blinded_pk, hs_ident_dir_conn_t *ident)
Definition: hs_ident.c:69
hs_descriptor_t::superencrypted_data
hs_desc_superencrypted_data_t superencrypted_data
Definition: hs_descriptor.h:233
hs_service_upload_desc_to_dir
void hs_service_upload_desc_to_dir(const char *encoded_desc, const uint8_t version, const ed25519_public_key_t *identity_pk, const ed25519_public_key_t *blinded_pk, const routerstatus_t *hsdir_rs)
Definition: hs_service.c:3596
hs_stats.h
Header file for hs_stats.c.
generate_ope_cipher_for_desc
static crypto_ope_t * generate_ope_cipher_for_desc(const hs_service_descriptor_t *hs_desc)
Definition: hs_service.c:1873
hs_desc_intro_point_t::key
crypto_pk_t * key
Definition: hs_descriptor.h:126
build_desc_signing_key_cert
static void build_desc_signing_key_cert(hs_service_descriptor_t *desc, time_t now)
Definition: hs_service.c:1700
service_intro_point_remove
STATIC void service_intro_point_remove(const hs_service_t *service, const hs_service_intro_point_t *ip)
Definition: hs_service.c:537
NUM_INTRO_POINTS_DEFAULT
#define NUM_INTRO_POINTS_DEFAULT
Definition: hs_common.h:33
crypto_strongest_rand
void crypto_strongest_rand(uint8_t *out, size_t out_len)
Definition: crypto_rand.c:340
compare_service_authorzized_client_
static int compare_service_authorzized_client_(const void **_a, const void **_b)
Definition: hs_service.c:1382
directory.h
Header file for directory.c.
register_all_services
static void register_all_services(void)
Definition: hs_service.c:912
crypto_pk_get_digest
int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
Definition: crypto_rsa.c:356
extend_info_from_node
extend_info_t * extend_info_from_node(const node_t *node, int for_direct_connect)
Definition: extendinfo.c:92
node_supports_ed25519_hs_intro
bool node_supports_ed25519_hs_intro(const node_t *node)
Definition: nodelist.c:1180
hs_desc_authorized_client_t
Definition: hs_descriptor.h:143
run_build_descriptor_event
static void run_build_descriptor_event(time_t now)
Definition: hs_service.c:2640
ed25519_public_key_t
Definition: crypto_ed25519.h:23
node_supports_establish_intro_dos_extension
bool node_supports_establish_intro_dos_extension(const node_t *node)
Definition: nodelist.c:1205
or_options_t::ExcludeNodes
struct routerset_t * ExcludeNodes
Definition: or_options_st.h:102
hs_service_config_t::version
uint32_t version
Definition: hs_service.h:201
REND_COOKIE_LEN
#define REND_COOKIE_LEN
Definition: or.h:400
hs_service_intro_point_t::legacy_key_digest
uint8_t legacy_key_digest[DIGEST_LEN]
Definition: hs_service.h:58
hs_check_service_private_dir
int hs_check_service_private_dir(const char *username, const char *path, unsigned int dir_group_readable, unsigned int create)
Definition: hs_common.c:201
LD_REND
#define LD_REND
Definition: log.h:84
escaped
const char * escaped(const char *s)
Definition: escape.c:126
hs_service_intro_point_t::introduce2_count
uint64_t introduce2_count
Definition: hs_service.h:61
hs_desc_intro_point_t::enc_key_cert
tor_cert_t * enc_key_cert
Definition: hs_descriptor.h:119
hs_circ_service_get_established_intro_circ
origin_circuit_t * hs_circ_service_get_established_intro_circ(const hs_service_intro_point_t *ip)
Definition: hs_circuit.c:678
build_service_desc_superencrypted
static int build_service_desc_superencrypted(const hs_service_t *service, hs_service_descriptor_t *desc)
Definition: hs_service.c:1759
hs_service_config_t
Definition: hs_service.h:198
hs_desc_plaintext_data_t::blinded_pubkey
ed25519_public_key_t blinded_pubkey
Definition: hs_descriptor.h:211
hs_circuit_id_protocol_t
hs_circuit_id_protocol_t
Definition: hs_service.h:187
log_cant_upload_desc
static void log_cant_upload_desc(const hs_service_t *service, const hs_service_descriptor_t *desc, const char *msg, const log_desc_upload_reason_t reason)
Definition: hs_service.c:3094
get_intro_point_min_introduce2
static int32_t get_intro_point_min_introduce2(void)
Definition: hs_service.c:316
crypto_ope_encrypt
uint64_t crypto_ope_encrypt(const crypto_ope_t *ope, int plaintext)
Definition: crypto_ope.c:165
curve25519_keypair_generate
int curve25519_keypair_generate(curve25519_keypair_t *keypair_out, int extra_strong)
Definition: crypto_curve25519.c:190
hs_desc_plaintext_data_t
Definition: hs_descriptor.h:193
get_intro_point_min_lifetime
static int32_t get_intro_point_min_lifetime(void)
Definition: hs_service.c:341
nodelist.h
Header file for nodelist.c.
hs_control.h
Header file containing control port event related code.
cleanup_intro_points
static void cleanup_intro_points(hs_service_t *service, time_t now)
Definition: hs_service.c:2425
hs_service_descriptor_t::signing_kp
ed25519_keypair_t signing_kp
Definition: hs_service.h:132
hs_ident_dir_conn_t
Definition: hs_ident.h:87
hs_descriptor_t
Definition: hs_descriptor.h:226
have_completed_a_circuit
int have_completed_a_circuit(void)
Definition: mainloop.c:220
hs_desc_intro_point_t::cert
struct hs_desc_intro_point_t::@16::@17 cert
hs_desc_superencrypted_data_free_contents
void hs_desc_superencrypted_data_free_contents(hs_desc_superencrypted_data_t *desc)
Definition: hs_descriptor.c:2713
should_remove_intro_point
static bool should_remove_intro_point(hs_service_intro_point_t *ip, time_t now)
Definition: hs_service.c:2371
smartlist_contains_string
int smartlist_contains_string(const smartlist_t *sl, const char *element)
Definition: smartlist.c:93
hs_service_intro_point_t::onion_key
curve25519_public_key_t onion_key
Definition: hs_service.h:44
rendservice.h
Header file for rendservice.c.
hs_desc_build_authorized_client
void hs_desc_build_authorized_client(const hs_subcredential_t *subcredential, const curve25519_public_key_t *client_auth_pk, const curve25519_secret_key_t *auth_ephemeral_sk, const uint8_t *descriptor_cookie, hs_desc_authorized_client_t *client_out)
Definition: hs_descriptor.c:2884
LD_CONFIG
#define LD_CONFIG
Definition: log.h:68
BASE64_DIGEST_LEN
#define BASE64_DIGEST_LEN
Definition: crypto_digest.h:26
DIGEST256_LEN
#define DIGEST256_LEN
Definition: digest_sizes.h:23
node_select.h
Header file for node_select.c.
extendinfo.h
Header for core/or/extendinfo.c.
hsdir_index_t::store_second
uint8_t store_second[DIGEST256_LEN]
Definition: hsdir_index_st.h:26
hs_desc_intro_point_t::onion_key
curve25519_public_key_t onion_key
Definition: hs_descriptor.h:106
hs_service_config_t::allow_unknown_ports
unsigned int allow_unknown_ports
Definition: hs_service.h:235
ed25519_secret_key_t::seckey
uint8_t seckey[ED25519_SECKEY_LEN]
Definition: crypto_ed25519.h:35
hs_service_keys_t::identity_pk
ed25519_public_key_t identity_pk
Definition: hs_service.h:172
RSAE_BADPRIVKEY
@ RSAE_BADPRIVKEY
Definition: hs_common.h:146
service_desc_schedule_upload
STATIC void service_desc_schedule_upload(hs_service_descriptor_t *desc, time_t now, int descriptor_changed)
Definition: hs_service.c:2268
hs_service_descriptor_t::blinded_kp
ed25519_keypair_t blinded_kp
Definition: hs_service.h:136
consider_republishing_hs_descriptors
static int consider_republishing_hs_descriptors
Definition: hs_service.c:110
crypto_digest256_new
crypto_digest_t * crypto_digest256_new(digest_algorithm_t algorithm)
Definition: crypto_digest_nss.c:272
connection_t
Definition: connection_st.h:45
hs_service_state_t::next_rotation_time
time_t next_rotation_time
Definition: hs_service.h:281
pick_needed_intro_points
static unsigned int pick_needed_intro_points(hs_service_t *service, hs_service_descriptor_t *desc)
Definition: hs_service.c:2162
hs_service_config_t::num_intro_points
unsigned int num_intro_points
Definition: hs_service.h:224
crypto_ope_new
crypto_ope_t * crypto_ope_new(const uint8_t *key)
Definition: crypto_ope.c:129
get_intro_point_num_extra
static int32_t get_intro_point_num_extra(void)
Definition: hs_service.c:375
directory_request_upload_set_hs_ident
void directory_request_upload_set_hs_ident(directory_request_t *req, const hs_ident_dir_conn_t *ident)
Definition: dirclient.c:1111
hs_ident_dir_conn_t::identity_pk
ed25519_public_key_t identity_pk
Definition: hs_ident.h:90
hs_service_config_t::max_streams_close_circuit
unsigned int max_streams_close_circuit
Definition: hs_service.h:220
service_intro_point_new
STATIC hs_service_intro_point_t * service_intro_point_new(const node_t *node)
Definition: hs_service.c:449
LOG_INFO
#define LOG_INFO
Definition: log.h:45
run_upload_descriptor_event
STATIC void run_upload_descriptor_event(time_t now)
Definition: hs_service.c:3253
crypto_rand.h
Common functions for using (pseudo-)random number generators.
remove_expired_failing_intro
static void remove_expired_failing_intro(hs_service_t *service, time_t now)
Definition: hs_service.c:1511
hs_service_intropoints_t::failed_id
digestmap_t * failed_id
Definition: hs_service.h:103
hs_service_dir_info_changed
void hs_service_dir_info_changed(void)
Definition: hs_service.c:3935
get_options
const or_options_t * get_options(void)
Definition: config.c:928
or_state_st.h
The or_state_t structure, which represents Tor's state file.
service_intro_circ_has_opened
static void service_intro_circ_has_opened(origin_circuit_t *circ)
Definition: hs_service.c:3302
tor_make_rsa_ed25519_crosscert
ssize_t tor_make_rsa_ed25519_crosscert(const ed25519_public_key_t *ed_key, const crypto_pk_t *rsa_key, time_t expires, uint8_t **cert)
Definition: torcert.c:331
tor_memcmp
int tor_memcmp(const void *a, const void *b, size_t len)
Definition: di_ops.c:31
set_descriptor_revision_counter
static void set_descriptor_revision_counter(hs_service_descriptor_t *hs_desc, time_t now, bool is_current)
Definition: hs_service.c:2907
link_specifier_dup
link_specifier_t * link_specifier_dup(const link_specifier_t *src)
Definition: hs_common.c:1873
pick_intro_point
static hs_service_intro_point_t * pick_intro_point(unsigned int direct_conn, smartlist_t *exclude_nodes)
Definition: hs_service.c:2112
service_desc_clear_previous_hsdirs
static void service_desc_clear_previous_hsdirs(hs_service_descriptor_t *desc)
Definition: hs_service.c:2239
circuitbuild.h
Header file for circuitbuild.c.
CIRCUIT_PURPOSE_S_REND_JOINED
#define CIRCUIT_PURPOSE_S_REND_JOINED
Definition: circuitlist.h:112
hs_service_t
Definition: hs_service.h:293
curve25519_public_key_is_ok
int curve25519_public_key_is_ok(const curve25519_public_key_t *key)
Definition: crypto_curve25519.c:132
hs_get_responsible_hsdirs
void hs_get_responsible_hsdirs(const ed25519_public_key_t *blinded_pk, uint64_t time_period_num, int use_second_hsdir_index, int for_fetching, smartlist_t *responsible_dirs)
Definition: hs_common.c:1341
log_desc_upload_reason_t
log_desc_upload_reason_t
Definition: hs_service.c:3073
hs_service_intro_point_t::introduce2_max
uint64_t introduce2_max
Definition: hs_service.h:64
hs_service_t::keys
hs_service_keys_t keys
Definition: hs_service.h:306
smartlist_clear
void smartlist_clear(smartlist_t *sl)
Definition: smartlist_core.c:50
directory_request_set_resource
void directory_request_set_resource(directory_request_t *req, const char *resource)
Definition: dirclient.c:1045
hs_service_state_t::num_intro_circ_launched
unsigned int num_intro_circ_launched
Definition: hs_service.h:270
crypto_digest_free
#define crypto_digest_free(d)
Definition: crypto_digest.h:130
hs_stats_note_introduce2_cell
void hs_stats_note_introduce2_cell(int is_hsv3)
Definition: hs_stats.c:22
extend_info_st.h
Extend-info structure.
routerstatus_st.h
Routerstatus (consensus entry) structure.
node_describe
const char * node_describe(const node_t *node)
Definition: describe.c:142
service_desc_hsdirs_changed
STATIC int service_desc_hsdirs_changed(const hs_service_t *service, const hs_service_descriptor_t *desc)
Definition: hs_service.c:3035
hs_common.h
Header file containing common data for the whole HS subsytem.
upload_descriptor_to_all
STATIC void upload_descriptor_to_all(const hs_service_t *service, hs_service_descriptor_t *desc)
Definition: hs_service.c:2978
curve25519_public_key_t
Definition: crypto_curve25519.h:24
edge_connection_st.h
Edge-connection structure.
origin_circuit_t::cpath
crypt_path_t * cpath
Definition: origin_circuit_st.h:129
hs_circ_service_intro_has_opened
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:803
hs_desc_intro_point_new
hs_desc_intro_point_t * hs_desc_intro_point_new(void)
Definition: hs_descriptor.c:2836
move_descriptors
static void move_descriptors(hs_service_t *src, hs_service_t *dst)
Definition: hs_service.c:1453
service_handle_introduce2
static int service_handle_introduce2(origin_circuit_t *circ, const uint8_t *payload, size_t payload_len)
Definition: hs_service.c:3454
relay.h
Header file for relay.c.
RSAE_ADDREXISTS
@ RSAE_ADDREXISTS
Definition: hs_common.h:145
connection.h
Header file for connection.c.
hs_desc_plaintext_data_t::signing_key_cert
tor_cert_t * signing_key_cert
Definition: hs_descriptor.h:203
describe_intro_point
static const char * describe_intro_point(const hs_service_intro_point_t *ip)
Definition: hs_service.c:286
hs_service_descriptor_t::time_period_num
uint64_t time_period_num
Definition: hs_service.h:140
remember_failing_intro_point
static void remember_failing_intro_point(const hs_service_intro_point_t *ip, hs_service_descriptor_t *desc, time_t now)
Definition: hs_service.c:1549
hs_service_load_all_keys
int hs_service_load_all_keys(void)
Definition: hs_service.c:4077
confline.h
Header for confline.c.
CIRCUIT_PURPOSE_S_CONNECT_REND
#define CIRCUIT_PURPOSE_S_CONNECT_REND
Definition: circuitlist.h:109
get_objects_from_ident
STATIC void get_objects_from_ident(const hs_ident_circuit_t *ident, hs_service_t **service, hs_service_intro_point_t **ip, hs_service_descriptor_t **desc)
Definition: hs_service.c:612
hs_service_descriptor_t::descriptor_cookie
uint8_t descriptor_cookie[HS_DESC_DESCRIPTOR_COOKIE_LEN]
Definition: hs_service.h:129
directory_request_new
directory_request_t * directory_request_new(uint8_t dir_purpose)
Definition: dirclient.c:948
smartlist_shuffle
void smartlist_shuffle(smartlist_t *sl)
Definition: crypto_rand.c:602
build_service_descriptor
static void build_service_descriptor(hs_service_t *service, uint64_t time_period_num, hs_service_descriptor_t **desc_out)
Definition: hs_service.c:1958
hs_service_new
hs_service_t * hs_service_new(const or_options_t *options)
Definition: hs_service.c:4175
hs_desc_encrypted_data_t::create2_ntor
unsigned int create2_ntor
Definition: hs_descriptor.h:161
networkstatus_t::valid_after
time_t valid_after
Definition: networkstatus_st.h:33
nickname.h
Header file for nickname.c.
hs_config.h
Header file containing configuration ABI/API for the HS subsytem.
service_key_on_disk
static int service_key_on_disk(const char *directory_path)
Definition: hs_service.c:3523
hs_ident_circuit_t
Definition: hs_ident.h:42
dirclient.h
Header file for dirclient.c.
CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
#define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
Definition: circuitlist.h:103
tor_asprintf
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
hs_service_intro_point_t::auth_key_kp
ed25519_keypair_t auth_key_kp
Definition: hs_service.h:48
hs_service_descriptor_t
Definition: hs_service.h:123
hs_service_intro_point_t::replay_cache
replaycache_t * replay_cache
Definition: hs_service.h:78
hs_get_time_period_num
uint64_t hs_get_time_period_num(time_t now)
Definition: hs_common.c:270
CIRCUIT_PURPOSE_S_INTRO
#define CIRCUIT_PURPOSE_S_INTRO
Definition: circuitlist.h:106
SMARTLIST_FOREACH_BEGIN
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
Definition: smartlist_foreach.h:78
rend_consider_services_upload
void rend_consider_services_upload(time_t now)
Definition: rendservice.c:4299
hs_ident_circuit_t::rendezvous_cookie
uint8_t rendezvous_cookie[HS_REND_COOKIE_LEN]
Definition: hs_ident.h:60
log_fn_ratelim
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Definition: log.h:292
node_get_nickname
const char * node_get_nickname(const node_t *node)
Definition: nodelist.c:1369
run_build_circuit_event
static void run_build_circuit_event(time_t now)
Definition: hs_service.c:2807
SMARTLIST_DEL_CURRENT
#define SMARTLIST_DEL_CURRENT(sl, var)
Definition: smartlist_foreach.h:120
HS_VERSION_THREE
#define HS_VERSION_THREE
Definition: hs_common.h:26
hs_service_config_t::circuit_id_protocol
hs_circuit_id_protocol_t circuit_id_protocol
Definition: hs_service.h:249
load_service_keys
static int load_service_keys(hs_service_t *service)
Definition: hs_service.c:1022
service_rendezvous_circ_has_opened
static void service_rendezvous_circ_has_opened(origin_circuit_t *circ)
Definition: hs_service.c:3359
dir_connection_st.h
Client/server directory connection structure.
sr_state_get_start_time_of_current_protocol_run
time_t sr_state_get_start_time_of_current_protocol_run(void)
Definition: shared_random_client.c:239
MAP_DEL_CURRENT
#define MAP_DEL_CURRENT(keyvar)
Definition: map.h:140
HEX_DIGEST_LEN
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
rend_num_services
int rend_num_services(void)
Definition: rendservice.c:202
LOG_WARN
#define LOG_WARN
Definition: log.h:53
node_st.h
Node information structure.
hs_service_config_t::ports
smartlist_t * ports
Definition: hs_service.h:207
hs_desc_encrypted_data_t::intro_points
smartlist_t * intro_points
Definition: hs_descriptor.h:171
router_have_consensus_path
consensus_path_type_t router_have_consensus_path(void)
Definition: nodelist.c:2379
hs_service_descriptor_t::ope_cipher
struct crypto_ope_t * ope_cipher
Definition: hs_service.h:144
hs_descriptor_t::encrypted_data
hs_desc_encrypted_data_t encrypted_data
Definition: hs_descriptor.h:238
edge_connection_t
Definition: edge_connection_st.h:21
get_link_spec_by_type
static link_specifier_t * get_link_spec_by_type(const hs_service_intro_point_t *ip, uint8_t type)
Definition: hs_service.c:643
hs_ident_circuit_t::num_rdv_streams
uint64_t num_rdv_streams
Definition: hs_ident.h:81
get_intro_point_max_lifetime
static int32_t get_intro_point_max_lifetime(void)
Definition: hs_service.c:358
hs_ob.h
Header file for the specific code for onion balance.
hs_intropoint.h
Header file for hs_intropoint.c.
hs_service_t::desc_current
hs_service_descriptor_t * desc_current
Definition: hs_service.h:312
service_escaped_dir
static const char * service_escaped_dir(const hs_service_t *s)
Definition: hs_service.c:870
FOR_EACH_DESCRIPTOR_BEGIN
#define FOR_EACH_DESCRIPTOR_BEGIN(service, var)
Definition: hs_service.c:84
TO_DIR_CONN
dir_connection_t * TO_DIR_CONN(connection_t *c)
Definition: directory.c:89
NUM_INTRO_POINTS_EXTRA
#define NUM_INTRO_POINTS_EXTRA
Definition: hs_common.h:38
HT_PROTOTYPE
HT_PROTOTYPE(hs_circuitmap_ht, circuit_t, hs_circuitmap_node, hs_circuit_hash_token, hs_circuits_have_same_token)
format_local_iso_time
void format_local_iso_time(char *buf, time_t t)
Definition: time_fmt.c:285
hs_service_config_t::clients
smartlist_t * clients
Definition: hs_service.h:231
sr_state_get_protocol_run_duration
unsigned int sr_state_get_protocol_run_duration(void)
Definition: shared_random_client.c:307
hs_service_lookup_current_desc
char * hs_service_lookup_current_desc(const ed25519_public_key_t *pk)
Definition: hs_service.c:3775
shared_random_client.h
Header file for shared_random_client.c.
close_service_intro_circuits
static void close_service_intro_circuits(hs_service_t *service)
Definition: hs_service.c:811
config.h
Header file for config.c.
setup_intro_point_exclude_list
static void setup_intro_point_exclude_list(const hs_service_descriptor_t *desc, smartlist_t *node_list)
Definition: hs_service.c:1530
hs_service_intro_point_t::circuit_retries
uint32_t circuit_retries
Definition: hs_service.h:73
router_choose_random_node
const node_t * router_choose_random_node(smartlist_t *excludedsmartlist, routerset_t *excludedset, router_crn_flags_t flags)
Definition: node_select.c:979
hs_service_free
#define hs_service_free(s)
Definition: hs_service.h:336
hs_service_intro_point_t::legacy_key
crypto_pk_t * legacy_key
Definition: hs_service.h:55
hs_service_t::onion_address
char onion_address[HS_SERVICE_ADDR_LEN_BASE32+1]
Definition: hs_service.h:296
hs_descriptor_t::subcredential
hs_subcredential_t subcredential
Definition: hs_descriptor.h:242
hs_service_descriptor_t::next_upload_time
time_t next_upload_time
Definition: hs_service.h:151
node_get_curve25519_onion_key
const curve25519_public_key_t * node_get_curve25519_onion_key(const node_t *node)
Definition: nodelist.c:1935
circuit_state_to_string
const char * circuit_state_to_string(int state)
Definition: circuitlist.c:764
hs_service_circuit_has_opened
void hs_service_circuit_has_opened(origin_circuit_t *circ)
Definition: hs_service.c:4018
hs_service_keys_t::identity_sk
ed25519_secret_key_t identity_sk
Definition: hs_service.h:174
hs_parse_address
int hs_parse_address(const char *address, ed25519_public_key_t *key_out, uint8_t *checksum_out, uint8_t *version_out)
Definition: hs_common.c:958
hs_service_authorized_client_t
Definition: hs_service.h:181
service_desc_find_by_intro
STATIC hs_service_descriptor_t * service_desc_find_by_intro(const hs_service_t *service, const hs_service_intro_point_t *ip)
Definition: hs_service.c:586
move_ephemeral_services
static void move_ephemeral_services(hs_service_ht *src, hs_service_ht *dst)
Definition: hs_service.c:840
tor_log
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:628
set_rotation_time
static void set_rotation_time(hs_service_t *service)
Definition: hs_service.c:2482
circuit_get_next_service_rp_circ
origin_circuit_t * circuit_get_next_service_rp_circ(origin_circuit_t *start)
Definition: circuitlist.c:1786
origin_circuit_t::hs_ident
struct hs_ident_circuit_t * hs_ident
Definition: origin_circuit_st.h:136
build_all_descriptors
STATIC void build_all_descriptors(time_t now)
Definition: hs_service.c:2067
hs_service_map
static struct hs_service_ht * hs_service_map
Definition: hs_service.c:149
TO_CIRCUIT
#define TO_CIRCUIT(x)
Definition: or.h:965
or_options_t
Definition: or_options_st.h:45
update_all_descriptors_intro_points
STATIC void update_all_descriptors_intro_points(time_t now)
Definition: hs_service.c:2327
hs_desc_encode_descriptor
int hs_desc_encode_descriptor(const hs_descriptor_t *desc, const ed25519_keypair_t *signing_kp, const uint8_t *descriptor_cookie, char **encoded_out)
Definition: hs_descriptor.c:2652
hs_service_config_t::is_ephemeral
unsigned int is_ephemeral
Definition: hs_service.h:246
TO_CONN
#define TO_CONN(c)
Definition: or.h:736
hs_service_descriptor_t::intro_points
hs_service_intropoints_t intro_points
Definition: hs_service.h:156
STATIC
#define STATIC
Definition: testsupport.h:32
hs_control_desc_event_created
void hs_control_desc_event_created(const char *onion_address, const ed25519_public_key_t *blinded_pk)
Definition: hs_control.c:111
digest_to_base64
void digest_to_base64(char *d64, const char *digest)
Definition: crypto_format.c:275
hs_circ_handle_intro_established
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:937
hs_desc_superencrypted_data_t
Definition: hs_descriptor.h:176
router_crn_flags_t
router_crn_flags_t
Definition: node_select.h:16
INTRO_POINT_LIFETIME_MIN_SECONDS
#define INTRO_POINT_LIFETIME_MIN_SECONDS
Definition: or.h:1093
hs_service_dump_stats
void hs_service_dump_stats(int severity)
Definition: hs_service.c:4114
dir_connection_t
Definition: dir_connection_st.h:21
hs_service_ht_eq
static int hs_service_ht_eq(const hs_service_t *first, const hs_service_t *second)
Definition: hs_service.c:127
router_have_minimum_dir_info
int router_have_minimum_dir_info(void)
Definition: nodelist.c:2346
hs_circuit.h
Header file containing circuit data for the whole HS subsytem.
networkstatus_get_param
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
Definition: networkstatus.c:2495
ed25519_secret_key_t
Definition: crypto_ed25519.h:28
networkstatus_st.h
Networkstatus consensus/vote structure.
rend_services_add_filenames_to_lists
void rend_services_add_filenames_to_lists(smartlist_t *open_lst, smartlist_t *stat_lst)
Definition: rendservice.c:1427
DIRIND_ANONYMOUS
@ DIRIND_ANONYMOUS
Definition: dirclient.h:39
smartlist_sort
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
directory_request_t
struct directory_request_t directory_request_t
Definition: dirclient.h:52
networkstatus_t
Definition: networkstatus_st.h:26
hs_service_intro_point_t
Definition: hs_service.h:38
hs_get_subcredential
void hs_get_subcredential(const ed25519_public_key_t *identity_pk, const ed25519_public_key_t *blinded_pk, hs_subcredential_t *subcred_out)
Definition: hs_common.c:816
origin_circuit_st.h
Origin circuit structure.
rescan_periodic_events
void rescan_periodic_events(const or_options_t *options)
Definition: mainloop.c:1586
hs_desc_intro_point_t
Definition: hs_descriptor.h:99
get_intro_point_max_introduce2
static int32_t get_intro_point_max_introduce2(void)
Definition: hs_service.c:329
hs_descriptor.h
Header file for hs_descriptor.c.
smartlist_t
Definition: smartlist_core.h:26
circuit_t::state
uint8_t state
Definition: circuit_st.h:110
should_rotate_descriptors
static unsigned int should_rotate_descriptors(hs_service_t *service, time_t now)
Definition: hs_service.c:2502
crypto_digest_get_digest
void crypto_digest_get_digest(crypto_digest_t *digest, char *out, size_t out_len)
Definition: crypto_digest_nss.c:348
hs_service_get_num_services
unsigned int hs_service_get_num_services(void)
Definition: hs_service.c:3799
hs_service_config_t::max_streams_per_rdv_circuit
uint64_t max_streams_per_rdv_circuit
Definition: hs_service.h:216
REND_REPLAY_TIME_INTERVAL
#define REND_REPLAY_TIME_INTERVAL
Definition: or.h:480
service_descriptor_new
STATIC hs_service_descriptor_t * service_descriptor_new(void)
Definition: hs_service.c:1334
directory_request_set_indirection
void directory_request_set_indirection(directory_request_t *req, dir_indirection_t indirection)
Definition: dirclient.c:1032
RSAE_OKAY
@ RSAE_OKAY
Definition: hs_common.h:148
DIGESTMAP_FOREACH
#define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar)
Definition: map.h:154
can_service_launch_intro_circuit
STATIC int can_service_launch_intro_circuit(hs_service_t *service, time_t now)
Definition: hs_service.c:2762
hs_service_get_version_from_key
int hs_service_get_version_from_key(const hs_service_t *service)
Definition: hs_service.c:4048
extend_info_t
Definition: extend_info_st.h:27
tor_listdir
smartlist_t * tor_listdir(const char *dirname)
Definition: dir.c:307
rend_service_rendezvous_has_opened
void rend_service_rendezvous_has_opened(origin_circuit_t *circuit)
Definition: rendservice.c:3469
rend_service_intro_has_opened
void rend_service_intro_has_opened(origin_circuit_t *circuit)
Definition: rendservice.c:3276
update_service_descriptor_intro_points
static void update_service_descriptor_intro_points(hs_service_t *service, hs_service_descriptor_t *desc, time_t now)
Definition: hs_service.c:2284
node_t::identity
char identity[DIGEST_LEN]
Definition: node_st.h:46
find_service
STATIC hs_service_t * find_service(hs_service_ht *map, const ed25519_public_key_t *pk)
Definition: hs_service.c:167
service_authorized_client_config_equal
STATIC int service_authorized_client_config_equal(const hs_service_config_t *config1, const hs_service_config_t *config2)
Definition: hs_service.c:1391
ratelim_t
Definition: ratelim.h:42
or.h
Master header file for Tor-specific functionality.
ed25519_keypair_t
Definition: crypto_ed25519.h:39
OPE_INPUT_MAX
#define OPE_INPUT_MAX
Definition: crypto_ope.h:31
LD_PROTOCOL
#define LD_PROTOCOL
Definition: log.h:72
hs_desc_plaintext_data_t::lifetime_sec
uint32_t lifetime_sec
Definition: hs_descriptor.h:199