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