Tor 0.4.9.13
Loading...
Searching...
No Matches
hs_common.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_common.c
6 * \brief Contains code shared between different HS protocol version as well
7 * as useful data structures and accessors used by other subsystems.
8 **/
9
10#define HS_COMMON_PRIVATE
11
12#include "core/or/or.h"
13
14#include "app/config/config.h"
16#include "core/or/policies.h"
17#include "core/or/extendinfo.h"
19#include "feature/hs/hs_cache.h"
23#include "feature/hs/hs_dos.h"
24#include "feature/hs/hs_ob.h"
25#include "feature/hs/hs_ident.h"
38#include "lib/net/resolve.h"
39
45
46/* Trunnel */
47#include "trunnel/ed25519_cert.h"
48
49/** Ed25519 Basepoint value. Taken from section 5 of
50 * https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-03 */
51static const char *str_ed25519_basepoint =
52 "(15112221349535400772501151409588531511"
53 "454012693041857206046113283949847762202, "
54 "463168356949264781694283940034751631413"
55 "07993866256225615783033603165251855960)";
56
57#ifdef HAVE_SYS_UN_H
58
59/** Given <b>ports</b>, a smartlist containing hs_port_config_t,
60 * add the given <b>p</b>, a AF_UNIX port to the list. Return 0 on success
61 * else return -ENOSYS if AF_UNIX is not supported (see function in the
62 * #else statement below). */
63static int
64add_unix_port(smartlist_t *ports, hs_port_config_t *p)
65{
66 tor_assert(ports);
67 tor_assert(p);
69
70 smartlist_add(ports, p);
71 return 0;
72}
73
74/** Given <b>conn</b> set it to use the given port <b>p</b> values. Return 0
75 * on success else return -ENOSYS if AF_UNIX is not supported (see function
76 * in the #else statement below). */
77static int
78set_unix_port(edge_connection_t *conn, hs_port_config_t *p)
79{
80 tor_assert(conn);
81 tor_assert(p);
83
84 conn->base_.socket_family = AF_UNIX;
85 tor_addr_make_unspec(&conn->base_.addr);
86 conn->base_.port = 1;
87 conn->base_.address = tor_strdup(p->unix_addr);
88 return 0;
89}
90
91#else /* !defined(HAVE_SYS_UN_H) */
92
93static int
94set_unix_port(edge_connection_t *conn, hs_port_config_t *p)
95{
96 (void) conn;
97 (void) p;
98 return -ENOSYS;
99}
100
101static int
102add_unix_port(smartlist_t *ports, hs_port_config_t *p)
103{
104 (void) ports;
105 (void) p;
106 return -ENOSYS;
107}
108
109#endif /* defined(HAVE_SYS_UN_H) */
110
111/** Helper function: The key is a digest that we compare to a node_t object
112 * current hsdir_index. */
113static int
114compare_digest_to_fetch_hsdir_index(const void *_key, const void **_member)
115{
116 const char *key = _key;
117 const node_t *node = *_member;
118 return tor_memcmp(key, node->hsdir_index.fetch, DIGEST256_LEN);
119}
120
121/** Helper function: The key is a digest that we compare to a node_t object
122 * next hsdir_index. */
123static int
125 const void **_member)
126{
127 const char *key = _key;
128 const node_t *node = *_member;
129 return tor_memcmp(key, node->hsdir_index.store_first, DIGEST256_LEN);
130}
131
132/** Helper function: The key is a digest that we compare to a node_t object
133 * next hsdir_index. */
134static int
136 const void **_member)
137{
138 const char *key = _key;
139 const node_t *node = *_member;
140 return tor_memcmp(key, node->hsdir_index.store_second, DIGEST256_LEN);
141}
142
143/** Helper function: Compare two node_t objects current hsdir_index. */
144static int
145compare_node_fetch_hsdir_index(const void **a, const void **b)
146{
147 const node_t *node1= *a;
148 const node_t *node2 = *b;
149 return tor_memcmp(node1->hsdir_index.fetch,
150 node2->hsdir_index.fetch,
152}
153
154/** Helper function: Compare two node_t objects next hsdir_index. */
155static int
156compare_node_store_first_hsdir_index(const void **a, const void **b)
157{
158 const node_t *node1= *a;
159 const node_t *node2 = *b;
160 return tor_memcmp(node1->hsdir_index.store_first,
161 node2->hsdir_index.store_first,
163}
164
165/** Helper function: Compare two node_t objects next hsdir_index. */
166static int
167compare_node_store_second_hsdir_index(const void **a, const void **b)
168{
169 const node_t *node1= *a;
170 const node_t *node2 = *b;
171 return tor_memcmp(node1->hsdir_index.store_second,
172 node2->hsdir_index.store_second,
174}
175
176/** Allocate and return a string containing the path to filename in directory.
177 * This function will never return NULL. The caller must free this path. */
178char *
179hs_path_from_filename(const char *directory, const char *filename)
180{
181 char *file_path = NULL;
182
183 tor_assert(directory);
184 tor_assert(filename);
185
186 tor_asprintf(&file_path, "%s%s%s", directory, PATH_SEPARATOR, filename);
187 return file_path;
188}
189
190/** Make sure that the directory for <b>service</b> is private, using the
191 * config <b>username</b>.
192 *
193 * If <b>create</b> is true:
194 * - if the directory exists, change permissions if needed,
195 * - if the directory does not exist, create it with the correct permissions.
196 * If <b>create</b> is false:
197 * - if the directory exists, check permissions,
198 * - if the directory does not exist, check if we think we can create it.
199 * Return 0 on success, -1 on failure. */
200int
201hs_check_service_private_dir(const char *username, const char *path,
202 unsigned int dir_group_readable,
203 unsigned int create)
204{
205 cpd_check_t check_opts = CPD_NONE;
206
207 tor_assert(path);
208
209 if (create) {
210 check_opts |= CPD_CREATE;
211 } else {
212 check_opts |= CPD_CHECK_MODE_ONLY;
213 check_opts |= CPD_CHECK;
214 }
215 if (dir_group_readable) {
216 check_opts |= CPD_GROUP_READ;
217 }
218 /* Check/create directory */
219 if (check_private_dir(path, check_opts, username) < 0) {
220 return -1;
221 }
222 return 0;
223}
224
225/* Default, minimum, and maximum values for the maximum rendezvous failures
226 * consensus parameter. */
227#define MAX_REND_FAILURES_DEFAULT 2
228#define MAX_REND_FAILURES_MIN 1
229#define MAX_REND_FAILURES_MAX 10
230
231/** How many times will a hidden service operator attempt to connect to
232 * a requested rendezvous point before giving up? */
233int
235{
236 return networkstatus_get_param(NULL, "hs_service_max_rdv_failures",
237 MAX_REND_FAILURES_DEFAULT,
238 MAX_REND_FAILURES_MIN,
239 MAX_REND_FAILURES_MAX);
240}
241
242/** Get the default HS time period length in minutes from the consensus. */
243STATIC uint64_t
245{
246 /* If we are on a test network, make the time period smaller than normal so
247 that we actually see it rotate. Specifically, make it the same length as
248 an SRV protocol run. */
249 if (get_options()->TestingTorNetwork) {
250 unsigned run_duration = sr_state_get_protocol_run_duration();
251 /* An SRV run should take more than a minute (it's 24 rounds) */
252 tor_assert_nonfatal(run_duration > 60);
253 /* Turn it from seconds to minutes before returning: */
255 }
256
257 int32_t time_period_length = networkstatus_get_param(NULL, "hsdir_interval",
261 /* Make sure it's a positive value. */
262 tor_assert(time_period_length > 0);
263 /* uint64_t will always be able to contain a positive int32_t */
264 return (uint64_t) time_period_length;
265}
266
267/** Get the HS time period number at time <b>now</b>. If <b>now</b> is not set,
268 * we try to get the time ourselves from a live consensus. */
269uint64_t
271{
272 uint64_t time_period_num;
273 time_t current_time;
274
275 /* If no time is specified, set current time based on consensus time, and
276 * only fall back to system time if that fails. */
277 if (now != 0) {
278 current_time = now;
279 } else {
280 networkstatus_t *ns =
283 current_time = ns ? ns->valid_after : approx_time();
284 }
285
286 /* Start by calculating minutes since the epoch */
287 uint64_t time_period_length = get_time_period_length();
288 uint64_t minutes_since_epoch = current_time / 60;
289
290 /* Apply the rotation offset as specified by prop224 (section
291 * [TIME-PERIODS]), so that new time periods synchronize nicely with SRV
292 * publication */
293 unsigned int time_period_rotation_offset = sr_state_get_phase_duration();
294 time_period_rotation_offset /= 60; /* go from seconds to minutes */
295 tor_assert(minutes_since_epoch > time_period_rotation_offset);
296 minutes_since_epoch -= time_period_rotation_offset;
297
298 /* Calculate the time period */
299 time_period_num = minutes_since_epoch / time_period_length;
300 return time_period_num;
301}
302
303/** Get the number of the _upcoming_ HS time period, given that the current
304 * time is <b>now</b>. If <b>now</b> is not set, we try to get the time from a
305 * live consensus. */
306uint64_t
308{
309 return hs_get_time_period_num(now) + 1;
310}
311
312/** Get the number of the _previous_ HS time period, given that the current
313 * time is <b>now</b>. If <b>now</b> is not set, we try to get the time from a
314 * live consensus. */
315uint64_t
317{
318 return hs_get_time_period_num(now) - 1;
319}
320
321/** Return the start time of the upcoming time period based on <b>now</b>. If
322 * <b>now</b> is not set, we try to get the time ourselves from a live
323 * consensus. */
324time_t
326{
327 uint64_t time_period_length = get_time_period_length();
328
329 /* Get start time of next time period */
330 uint64_t next_time_period_num = hs_get_next_time_period_num(now);
331 uint64_t start_of_next_tp_in_mins = next_time_period_num *time_period_length;
332
333 /* Apply rotation offset as specified by prop224 section [TIME-PERIODS] */
334 unsigned int time_period_rotation_offset = sr_state_get_phase_duration();
335 return (time_t)(start_of_next_tp_in_mins * 60 + time_period_rotation_offset);
336}
337
338/** Using the given time period number, compute the disaster shared random
339 * value and put it in srv_out. It MUST be at least DIGEST256_LEN bytes. */
340static void
341compute_disaster_srv(uint64_t time_period_num, uint8_t *srv_out)
342{
343 crypto_digest_t *digest;
344
345 tor_assert(srv_out);
346
347 digest = crypto_digest256_new(DIGEST_SHA3_256);
348
349 /* Start setting up payload:
350 * H("shared-random-disaster" | INT_8(period_length) | INT_8(period_num)) */
352 HS_SRV_DISASTER_PREFIX_LEN);
353
354 /* Setup INT_8(period_length) | INT_8(period_num) */
355 {
356 uint64_t time_period_length = get_time_period_length();
357 char period_stuff[sizeof(uint64_t)*2];
358 size_t offset = 0;
359 set_uint64(period_stuff, tor_htonll(time_period_length));
360 offset += sizeof(uint64_t);
361 set_uint64(period_stuff+offset, tor_htonll(time_period_num));
362 offset += sizeof(uint64_t);
363 tor_assert(offset == sizeof(period_stuff));
364
365 crypto_digest_add_bytes(digest, period_stuff, sizeof(period_stuff));
366 }
367
368 crypto_digest_get_digest(digest, (char *) srv_out, DIGEST256_LEN);
369 crypto_digest_free(digest);
370}
371
372/** Due to the high cost of computing the disaster SRV and that potentially we
373 * would have to do it thousands of times in a row, we always cache the
374 * computer disaster SRV (and its corresponding time period num) in case we
375 * want to reuse it soon after. We need to cache two SRVs, one for each active
376 * time period.
377 */
379static uint64_t cached_time_period_nums[2] = {0};
380
381/** Compute the disaster SRV value for this <b>time_period_num</b> and put it
382 * in <b>srv_out</b> (of size at least DIGEST256_LEN). First check our caches
383 * to see if we have already computed it. */
384STATIC void
385get_disaster_srv(uint64_t time_period_num, uint8_t *srv_out)
386{
387 if (time_period_num == cached_time_period_nums[0]) {
388 memcpy(srv_out, cached_disaster_srv[0], DIGEST256_LEN);
389 return;
390 } else if (time_period_num == cached_time_period_nums[1]) {
391 memcpy(srv_out, cached_disaster_srv[1], DIGEST256_LEN);
392 return;
393 } else {
394 int replace_idx;
395 // Replace the lower period number.
396 if (cached_time_period_nums[0] <= cached_time_period_nums[1]) {
397 replace_idx = 0;
398 } else {
399 replace_idx = 1;
400 }
401 cached_time_period_nums[replace_idx] = time_period_num;
402 compute_disaster_srv(time_period_num, cached_disaster_srv[replace_idx]);
403 memcpy(srv_out, cached_disaster_srv[replace_idx], DIGEST256_LEN);
404 return;
405 }
406}
407
408#ifdef TOR_UNIT_TESTS
409
410/** Get the first cached disaster SRV. Only used by unittests. */
411STATIC uint8_t *
412get_first_cached_disaster_srv(void)
413{
414 return cached_disaster_srv[0];
415}
416
417/** Get the second cached disaster SRV. Only used by unittests. */
418STATIC uint8_t *
419get_second_cached_disaster_srv(void)
420{
421 return cached_disaster_srv[1];
422}
423
424#endif /* defined(TOR_UNIT_TESTS) */
425
426/** When creating a blinded key, we need a parameter which construction is as
427 * follow: H(pubkey | [secret] | ed25519-basepoint | nonce).
428 *
429 * The nonce has a pre-defined format which uses the time period number
430 * period_num and the start of the period in second start_time_period.
431 *
432 * The secret of size secret_len is optional meaning that it can be NULL and
433 * thus will be ignored for the param construction.
434 *
435 * The result is put in param_out. */
436STATIC void
438 const uint8_t *secret, size_t secret_len,
439 uint64_t period_num, uint64_t period_length,
440 uint8_t *param_out)
441{
442 size_t offset = 0;
443 const char blind_str[] = "Derive temporary signing key";
444 uint8_t nonce[HS_KEYBLIND_NONCE_LEN];
445 crypto_digest_t *digest;
446
447 tor_assert(pubkey);
448 tor_assert(param_out);
449
450 /* Create the nonce N. The construction is as follow:
451 * N = "key-blind" || INT_8(period_num) || INT_8(period_length) */
452 memcpy(nonce, HS_KEYBLIND_NONCE_PREFIX, HS_KEYBLIND_NONCE_PREFIX_LEN);
453 offset += HS_KEYBLIND_NONCE_PREFIX_LEN;
454 set_uint64(nonce + offset, tor_htonll(period_num));
455 offset += sizeof(uint64_t);
456 set_uint64(nonce + offset, tor_htonll(period_length));
457 offset += sizeof(uint64_t);
458 tor_assert(offset == HS_KEYBLIND_NONCE_LEN);
459
460 /* Generate the parameter h and the construction is as follow:
461 * h = H(BLIND_STRING | pubkey | [secret] | ed25519-basepoint | N) */
462 digest = crypto_digest256_new(DIGEST_SHA3_256);
463 crypto_digest_add_bytes(digest, blind_str, sizeof(blind_str));
464 crypto_digest_add_bytes(digest, (char *) pubkey, ED25519_PUBKEY_LEN);
465 /* Optional secret. */
466 if (secret) {
467 crypto_digest_add_bytes(digest, (char *) secret, secret_len);
468 }
470 strlen(str_ed25519_basepoint));
471 crypto_digest_add_bytes(digest, (char *) nonce, sizeof(nonce));
472
473 /* Extract digest and put it in the param. */
474 crypto_digest_get_digest(digest, (char *) param_out, DIGEST256_LEN);
475 crypto_digest_free(digest);
476
477 memwipe(nonce, 0, sizeof(nonce));
478}
479
480/** Using an ed25519 public key and version to build the checksum of an
481 * address. Put in checksum_out. Format is:
482 * SHA3-256(".onion checksum" || PUBKEY || VERSION)
483 *
484 * checksum_out must be large enough to receive 32 bytes (DIGEST256_LEN). */
485static void
486build_hs_checksum(const ed25519_public_key_t *key, uint8_t version,
487 uint8_t *checksum_out)
488{
489 size_t offset = 0;
491
492 /* Build checksum data. */
496 memcpy(data + offset, key->pubkey, ED25519_PUBKEY_LEN);
497 offset += ED25519_PUBKEY_LEN;
498 set_uint8(data + offset, version);
499 offset += sizeof(version);
501
502 /* Hash the data payload to create the checksum. */
503 crypto_digest256((char *) checksum_out, data, sizeof(data),
504 DIGEST_SHA3_256);
505}
506
507/** Using an ed25519 public key, checksum and version to build the binary
508 * representation of a service address. Put in addr_out. Format is:
509 * addr_out = PUBKEY || CHECKSUM || VERSION
510 *
511 * addr_out must be large enough to receive HS_SERVICE_ADDR_LEN bytes. */
512static void
513build_hs_address(const ed25519_public_key_t *key, const uint8_t *checksum,
514 uint8_t version, char *addr_out)
515{
516 size_t offset = 0;
517
518 tor_assert(key);
519 tor_assert(checksum);
520
521 memcpy(addr_out, key->pubkey, ED25519_PUBKEY_LEN);
522 offset += ED25519_PUBKEY_LEN;
523 memcpy(addr_out + offset, checksum, HS_SERVICE_ADDR_CHECKSUM_LEN_USED);
525 set_uint8(addr_out + offset, version);
526 offset += sizeof(uint8_t);
528}
529
530/** Helper for hs_parse_address(): Using a binary representation of a service
531 * address, parse its content into the key_out, checksum_out and version_out.
532 * Any out variable can be NULL in case the caller would want only one field.
533 * checksum_out MUST at least be 2 bytes long. address must be at least
534 * HS_SERVICE_ADDR_LEN bytes but doesn't need to be NUL terminated. */
535static void
536hs_parse_address_impl(const char *address, ed25519_public_key_t *key_out,
537 uint8_t *checksum_out, uint8_t *version_out)
538{
539 size_t offset = 0;
540
541 tor_assert(address);
542
543 if (key_out) {
544 /* First is the key. */
545 memcpy(key_out->pubkey, address, ED25519_PUBKEY_LEN);
546 }
547 offset += ED25519_PUBKEY_LEN;
548 if (checksum_out) {
549 /* Followed by a 2 bytes checksum. */
550 memcpy(checksum_out, address + offset, HS_SERVICE_ADDR_CHECKSUM_LEN_USED);
551 }
553 if (version_out) {
554 /* Finally, version value is 1 byte. */
555 *version_out = get_uint8(address + offset);
556 }
557 offset += sizeof(uint8_t);
558 /* Extra safety. */
560}
561
562/** Using the given identity public key and a blinded public key, compute the
563 * subcredential and put it in subcred_out.
564 * This can't fail. */
565void
567 const ed25519_public_key_t *blinded_pk,
568 hs_subcredential_t *subcred_out)
569{
570 uint8_t credential[DIGEST256_LEN];
571 crypto_digest_t *digest;
572
573 tor_assert(identity_pk);
574 tor_assert(blinded_pk);
575 tor_assert(subcred_out);
576
577 /* First, build the credential. Construction is as follow:
578 * credential = H("credential" | public-identity-key) */
579 digest = crypto_digest256_new(DIGEST_SHA3_256);
581 HS_CREDENTIAL_PREFIX_LEN);
582 crypto_digest_add_bytes(digest, (const char *) identity_pk->pubkey,
584 crypto_digest_get_digest(digest, (char *) credential, DIGEST256_LEN);
585 crypto_digest_free(digest);
586
587 /* Now, compute the subcredential. Construction is as follow:
588 * subcredential = H("subcredential" | credential | blinded-public-key). */
589 digest = crypto_digest256_new(DIGEST_SHA3_256);
590 crypto_digest_add_bytes(digest, HS_SUBCREDENTIAL_PREFIX,
591 HS_SUBCREDENTIAL_PREFIX_LEN);
592 crypto_digest_add_bytes(digest, (const char *) credential,
593 sizeof(credential));
594 crypto_digest_add_bytes(digest, (const char *) blinded_pk->pubkey,
596 crypto_digest_get_digest(digest, (char *) subcred_out->subcred,
597 SUBCRED_LEN);
598 crypto_digest_free(digest);
599
600 memwipe(credential, 0, sizeof(credential));
601}
602
603/** From the given list of hidden service ports, find the ones that match the
604 * given edge connection conn, pick one at random and use it to set the
605 * connection address. Return 0 on success or -1 if none. */
606int
608{
609 hs_port_config_t *chosen_port;
610 unsigned int warn_once = 0;
611 smartlist_t *matching_ports;
612
613 tor_assert(ports);
614 tor_assert(conn);
615
616 matching_ports = smartlist_new();
618 if (TO_CONN(conn)->port != p->virtual_port) {
619 continue;
620 }
621 if (!(p->is_unix_addr)) {
622 smartlist_add(matching_ports, p);
623 } else {
624 if (add_unix_port(matching_ports, p)) {
625 if (!warn_once) {
626 /* Unix port not supported so warn only once. */
627 log_warn(LD_REND, "Saw AF_UNIX virtual port mapping for port %d "
628 "which is unsupported on this platform. "
629 "Ignoring it.",
630 TO_CONN(conn)->port);
631 }
632 warn_once++;
633 }
634 }
635 } SMARTLIST_FOREACH_END(p);
636
637 chosen_port = smartlist_choose(matching_ports);
638 smartlist_free(matching_ports);
639 if (chosen_port) {
640 if (conn->hs_ident) {
641 /* There is always a connection identifier at this point. Regardless of a
642 * Unix or TCP port, note the virtual port. */
643 conn->hs_ident->orig_virtual_port = chosen_port->virtual_port;
644 }
645
646 if (!(chosen_port->is_unix_addr)) {
647 /* Get a non-AF_UNIX connection ready for connection_exit_connect() */
648 tor_addr_copy(&TO_CONN(conn)->addr, &chosen_port->real_addr);
649 TO_CONN(conn)->port = chosen_port->real_port;
650 } else {
651 if (set_unix_port(conn, chosen_port)) {
652 /* Simply impossible to end up here else we were able to add a Unix
653 * port without AF_UNIX support... ? */
654 tor_assert(0);
655 }
656 }
657 }
658 return (chosen_port) ? 0 : -1;
659}
660
661/** Return a new hs_port_config_t with its path set to
662 * <b>socket_path</b> or empty if <b>socket_path</b> is NULL */
663static hs_port_config_t *
664hs_port_config_new(const char *socket_path)
665{
666 if (!socket_path)
667 return tor_malloc_zero(sizeof(hs_port_config_t) + 1);
668
669 const size_t pathlen = strlen(socket_path) + 1;
670 hs_port_config_t *conf =
671 tor_malloc_zero(sizeof(hs_port_config_t) + pathlen);
672 memcpy(conf->unix_addr, socket_path, pathlen);
673 conf->is_unix_addr = 1;
674 return conf;
675}
676
677/** Parses a virtual-port to real-port/socket mapping separated by
678 * the provided separator and returns a new hs_port_config_t,
679 * or NULL and an optional error string on failure.
680 *
681 * The format is: VirtualPort SEP (IP|RealPort|IP:RealPort|'socket':path)?
682 *
683 * IP defaults to 127.0.0.1; RealPort defaults to VirtualPort.
684 */
686hs_parse_port_config(const char *string, const char *sep,
687 char **err_msg_out)
688{
689 smartlist_t *sl;
690 int virtport;
691 int realport = 0;
692 uint16_t p;
693 tor_addr_t addr;
694 hs_port_config_t *result = NULL;
695 unsigned int is_unix_addr = 0;
696 const char *socket_path = NULL;
697 char *err_msg = NULL;
698 char *addrport = NULL;
699
700 sl = smartlist_new();
701 smartlist_split_string(sl, string, sep,
702 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
703 if (smartlist_len(sl) < 1 || BUG(smartlist_len(sl) > 2)) {
704 err_msg = tor_strdup("Bad syntax in hidden service port configuration.");
705 goto err;
706 }
707 virtport = (int)tor_parse_long(smartlist_get(sl,0), 10, 1, 65535, NULL,NULL);
708 if (!virtport) {
709 tor_asprintf(&err_msg, "Missing or invalid port %s in hidden service "
710 "port configuration", escaped(smartlist_get(sl,0)));
711
712 goto err;
713 }
714 if (smartlist_len(sl) == 1) {
715 /* No addr:port part; use default. */
716 realport = virtport;
717 tor_addr_from_ipv4h(&addr, 0x7F000001u); /* 127.0.0.1 */
718 } else {
719 int ret;
720
721 const char *addrport_element = smartlist_get(sl,1);
722 const char *rest = NULL;
723 int is_unix;
724 ret = port_cfg_line_extract_addrport(addrport_element, &addrport,
725 &is_unix, &rest);
726
727 if (ret < 0) {
728 tor_asprintf(&err_msg, "Couldn't process address <%s> from hidden "
729 "service configuration", addrport_element);
730 goto err;
731 }
732
733 if (rest && strlen(rest)) {
734 err_msg = tor_strdup("HiddenServicePort parse error: invalid port "
735 "mapping");
736 goto err;
737 }
738
739 if (is_unix) {
740 socket_path = addrport;
741 is_unix_addr = 1;
742 } else if (strchr(addrport, ':') || strchr(addrport, '.')) {
743 /* else try it as an IP:port pair if it has a : or . in it */
744 if (tor_addr_port_lookup(addrport, &addr, &p)<0) {
745 err_msg = tor_strdup("Unparseable address in hidden service port "
746 "configuration.");
747 goto err;
748 }
749 realport = p?p:virtport;
750 } else {
751 /* No addr:port, no addr -- must be port. */
752 realport = (int)tor_parse_long(addrport, 10, 1, 65535, NULL, NULL);
753 if (!realport) {
754 tor_asprintf(&err_msg, "Unparseable or out-of-range port %s in "
755 "hidden service port configuration.",
756 escaped(addrport));
757 goto err;
758 }
759 tor_addr_from_ipv4h(&addr, 0x7F000001u); /* Default to 127.0.0.1 */
760 }
761 }
762
763 /* Allow room for unix_addr */
764 result = hs_port_config_new(socket_path);
765 result->virtual_port = virtport;
766 result->is_unix_addr = is_unix_addr;
767 if (!is_unix_addr) {
768 result->real_port = realport;
769 tor_addr_copy(&result->real_addr, &addr);
770 result->unix_addr[0] = '\0';
771 }
772
773 err:
774 tor_free(addrport);
775 if (err_msg_out != NULL) {
776 *err_msg_out = err_msg;
777 } else {
778 tor_free(err_msg);
779 }
780 SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
781 smartlist_free(sl);
782
783 return result;
784}
785
786/** Release all storage held in a hs_port_config_t. */
787void
792
793/** Using a base32 representation of a service address, parse its content into
794 * the key_out, checksum_out and version_out. Any out variable can be NULL in
795 * case the caller would want only one field. checksum_out MUST at least be 2
796 * bytes long.
797 *
798 * Return 0 if parsing went well; return -1 in case of error and if errmsg is
799 * non NULL, a human readable string message is set. */
800int
801hs_parse_address_no_log(const char *address, ed25519_public_key_t *key_out,
802 uint8_t *checksum_out, uint8_t *version_out,
803 const char **errmsg)
804{
805 char decoded[HS_SERVICE_ADDR_LEN];
806
807 tor_assert(address);
808
809 if (errmsg) {
810 *errmsg = NULL;
811 }
812
813 /* Obvious length check. */
814 if (strlen(address) != HS_SERVICE_ADDR_LEN_BASE32) {
815 if (errmsg) {
816 *errmsg = "Invalid length";
817 }
818 goto invalid;
819 }
820
821 /* Decode address so we can extract needed fields. */
822 if (base32_decode(decoded, sizeof(decoded), address, strlen(address))
823 != sizeof(decoded)) {
824 if (errmsg) {
825 *errmsg = "Unable to base32 decode";
826 }
827 goto invalid;
828 }
829
830 /* Parse the decoded address into the fields we need. */
831 hs_parse_address_impl(decoded, key_out, checksum_out, version_out);
832
833 return 0;
834 invalid:
835 return -1;
836}
837
838/** Same has hs_parse_address_no_log() but emits a log warning on parsing
839 * failure. */
840int
841hs_parse_address(const char *address, ed25519_public_key_t *key_out,
842 uint8_t *checksum_out, uint8_t *version_out)
843{
844 const char *errmsg = NULL;
845 int ret = hs_parse_address_no_log(address, key_out, checksum_out,
846 version_out, &errmsg);
847 if (ret < 0) {
848 log_warn(LD_REND, "Service address %s failed to be parsed: %s",
849 escaped_safe_str(address), errmsg);
850 }
851 return ret;
852}
853
854/** Validate a given onion address. The length, the base32 decoding, and
855 * checksum are validated. Return 1 if valid else 0. */
856int
857hs_address_is_valid(const char *address)
858{
859 uint8_t version;
860 uint8_t checksum[HS_SERVICE_ADDR_CHECKSUM_LEN_USED];
861 uint8_t target_checksum[DIGEST256_LEN];
862 ed25519_public_key_t service_pubkey;
863
864 /* Parse the decoded address into the fields we need. */
865 if (hs_parse_address(address, &service_pubkey, checksum, &version) < 0) {
866 goto invalid;
867 }
868
869 /* Get the checksum it's supposed to be and compare it with what we have
870 * encoded in the address. */
871 build_hs_checksum(&service_pubkey, version, target_checksum);
872 if (tor_memcmp(checksum, target_checksum, sizeof(checksum))) {
873 log_warn(LD_REND, "Service address %s invalid checksum.",
874 escaped_safe_str(address));
875 goto invalid;
876 }
877
878 /* Validate that this pubkey does not have a torsion component. We need to do
879 * this on the prop224 client-side so that attackers can't give equivalent
880 * forms of an onion address to users. */
881 if (ed25519_validate_pubkey(&service_pubkey) < 0) {
882 log_warn(LD_REND, "Service address %s has bad pubkey .",
883 escaped_safe_str(address));
884 goto invalid;
885 }
886
887 /* Valid address. */
888 return 1;
889 invalid:
890 return 0;
891}
892
893/** Build a service address using an ed25519 public key and a given version.
894 * The returned address is base32 encoded and put in addr_out. The caller MUST
895 * make sure the addr_out is at least HS_SERVICE_ADDR_LEN_BASE32 + 1 long.
896 *
897 * Format is as follows:
898 * base32(PUBKEY || CHECKSUM || VERSION)
899 * CHECKSUM = H(".onion checksum" || PUBKEY || VERSION)
900 * */
901void
902hs_build_address(const ed25519_public_key_t *key, uint8_t version,
903 char *addr_out)
904{
905 uint8_t checksum[DIGEST256_LEN];
906 char address[HS_SERVICE_ADDR_LEN];
907
908 tor_assert(key);
909 tor_assert(addr_out);
910
911 /* Get the checksum of the address. */
912 build_hs_checksum(key, version, checksum);
913 /* Get the binary address representation. */
914 build_hs_address(key, checksum, version, address);
915
916 /* Encode the address. addr_out will be NUL terminated after this. */
917 base32_encode(addr_out, HS_SERVICE_ADDR_LEN_BASE32 + 1, address,
918 sizeof(address));
919 /* Validate what we just built. */
921}
922
923/** From a given ed25519 public key pk and an optional secret, compute a
924 * blinded public key and put it in blinded_pk_out. This is only useful to
925 * the client side because the client only has access to the identity public
926 * key of the service. */
927void
929 const uint8_t *secret, size_t secret_len,
930 uint64_t time_period_num,
931 ed25519_public_key_t *blinded_pk_out)
932{
933 /* Our blinding key API requires a 32 bytes parameter. */
934 uint8_t param[DIGEST256_LEN];
935
936 tor_assert(pk);
937 tor_assert(blinded_pk_out);
939
940 build_blinded_key_param(pk, secret, secret_len,
941 time_period_num, get_time_period_length(), param);
942 ed25519_public_blind(blinded_pk_out, pk, param);
943
944 memwipe(param, 0, sizeof(param));
945}
946
947/** From a given ed25519 keypair kp and an optional secret, compute a blinded
948 * keypair for the current time period and put it in blinded_kp_out. This is
949 * only useful by the service side because the client doesn't have access to
950 * the identity secret key. */
951void
953 const uint8_t *secret, size_t secret_len,
954 uint64_t time_period_num,
955 ed25519_keypair_t *blinded_kp_out)
956{
957 /* Our blinding key API requires a 32 bytes parameter. */
958 uint8_t param[DIGEST256_LEN];
959
960 tor_assert(kp);
961 tor_assert(blinded_kp_out);
962 /* Extra safety. A zeroed key is bad. */
963 tor_assert(!fast_mem_is_zero((char *) &kp->pubkey, ED25519_PUBKEY_LEN));
964 tor_assert(!fast_mem_is_zero((char *) &kp->seckey, ED25519_SECKEY_LEN));
965
966 build_blinded_key_param(&kp->pubkey, secret, secret_len,
967 time_period_num, get_time_period_length(), param);
968 ed25519_keypair_blind(blinded_kp_out, kp, param);
969
970 memwipe(param, 0, sizeof(param));
971}
972
973/** Return true if we are currently in the time segment between a new time
974 * period and a new SRV (in the real network that happens between 12:00 and
975 * 00:00 UTC). Here is a diagram showing exactly when this returns true:
976 *
977 * +------------------------------------------------------------------+
978 * | |
979 * | 00:00 12:00 00:00 12:00 00:00 12:00 |
980 * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
981 * | |
982 * | $==========|-----------$===========|-----------$===========| |
983 * | ^^^^^^^^^^^^ ^^^^^^^^^^^^ |
984 * | |
985 * +------------------------------------------------------------------+
986 */
987MOCK_IMPL(int,
989{
990 time_t valid_after;
991 time_t srv_start_time, tp_start_time;
992
993 if (!consensus) {
996 if (!consensus) {
997 return 0;
998 }
999 }
1000
1001 /* Get start time of next TP and of current SRV protocol run, and check if we
1002 * are between them. */
1003 valid_after = consensus->valid_after;
1005 tp_start_time = hs_get_start_time_of_next_time_period(srv_start_time);
1006
1007 if (valid_after >= srv_start_time && valid_after < tp_start_time) {
1008 return 0;
1009 }
1010
1011 return 1;
1012}
1013
1014/** Return 1 if any virtual port in ports needs a circuit with good uptime.
1015 * Else return 0. */
1016int
1018{
1019 tor_assert(ports);
1020
1022 if (smartlist_contains_int_as_string(get_options()->LongLivedPorts,
1023 p->virtual_port)) {
1024 return 1;
1025 }
1026 } SMARTLIST_FOREACH_END(p);
1027 return 0;
1028}
1029
1030/** Build hs_index which is used to find the responsible hsdirs. This index
1031 * value is used to select the responsible HSDir where their hsdir_index is
1032 * closest to this value.
1033 * SHA3-256("store-at-idx" | blinded_public_key |
1034 * INT_8(replicanum) | INT_8(period_length) | INT_8(period_num) )
1035 *
1036 * hs_index_out must be large enough to receive DIGEST256_LEN bytes. */
1037void
1038hs_build_hs_index(uint64_t replica, const ed25519_public_key_t *blinded_pk,
1039 uint64_t period_num, uint8_t *hs_index_out)
1040{
1041 crypto_digest_t *digest;
1042
1043 tor_assert(blinded_pk);
1044 tor_assert(hs_index_out);
1045
1046 /* Build hs_index. See construction at top of function comment. */
1047 digest = crypto_digest256_new(DIGEST_SHA3_256);
1048 crypto_digest_add_bytes(digest, HS_INDEX_PREFIX, HS_INDEX_PREFIX_LEN);
1049 crypto_digest_add_bytes(digest, (const char *) blinded_pk->pubkey,
1051
1052 /* Now setup INT_8(replicanum) | INT_8(period_length) | INT_8(period_num) */
1053 {
1054 uint64_t period_length = get_time_period_length();
1055 char buf[sizeof(uint64_t)*3];
1056 size_t offset = 0;
1057 set_uint64(buf, tor_htonll(replica));
1058 offset += sizeof(uint64_t);
1059 set_uint64(buf+offset, tor_htonll(period_length));
1060 offset += sizeof(uint64_t);
1061 set_uint64(buf+offset, tor_htonll(period_num));
1062 offset += sizeof(uint64_t);
1063 tor_assert(offset == sizeof(buf));
1064
1065 crypto_digest_add_bytes(digest, buf, sizeof(buf));
1066 }
1067
1068 crypto_digest_get_digest(digest, (char *) hs_index_out, DIGEST256_LEN);
1069 crypto_digest_free(digest);
1070}
1071
1072/** Build hsdir_index which is used to find the responsible hsdirs. This is the
1073 * index value that is compare to the hs_index when selecting an HSDir.
1074 * SHA3-256("node-idx" | node_identity |
1075 * shared_random_value | INT_8(period_length) | INT_8(period_num) )
1076 *
1077 * hsdir_index_out must be large enough to receive DIGEST256_LEN bytes. */
1078void
1080 const uint8_t *srv_value, uint64_t period_num,
1081 uint8_t *hsdir_index_out)
1082{
1083 crypto_digest_t *digest;
1084
1085 tor_assert(identity_pk);
1086 tor_assert(srv_value);
1087 tor_assert(hsdir_index_out);
1088
1089 /* Build hsdir_index. See construction at top of function comment. */
1090 digest = crypto_digest256_new(DIGEST_SHA3_256);
1091 crypto_digest_add_bytes(digest, HSDIR_INDEX_PREFIX, HSDIR_INDEX_PREFIX_LEN);
1092 crypto_digest_add_bytes(digest, (const char *) identity_pk->pubkey,
1094 crypto_digest_add_bytes(digest, (const char *) srv_value, DIGEST256_LEN);
1095
1096 {
1097 uint64_t time_period_length = get_time_period_length();
1098 char period_stuff[sizeof(uint64_t)*2];
1099 size_t offset = 0;
1100 set_uint64(period_stuff, tor_htonll(period_num));
1101 offset += sizeof(uint64_t);
1102 set_uint64(period_stuff+offset, tor_htonll(time_period_length));
1103 offset += sizeof(uint64_t);
1104 tor_assert(offset == sizeof(period_stuff));
1105
1106 crypto_digest_add_bytes(digest, period_stuff, sizeof(period_stuff));
1107 }
1108
1109 crypto_digest_get_digest(digest, (char *) hsdir_index_out, DIGEST256_LEN);
1110 crypto_digest_free(digest);
1111}
1112
1113/** Return a newly allocated buffer containing the current shared random value
1114 * or if not present, a disaster value is computed using the given time period
1115 * number. If a consensus is provided in <b>ns</b>, use it to get the SRV
1116 * value. This function can't fail. */
1117uint8_t *
1118hs_get_current_srv(uint64_t time_period_num, const networkstatus_t *ns)
1119{
1120 uint8_t *sr_value = tor_malloc_zero(DIGEST256_LEN);
1121 const sr_srv_t *current_srv = sr_get_current(ns);
1122
1123 if (current_srv) {
1124 memcpy(sr_value, current_srv->value, sizeof(current_srv->value));
1125 } else {
1126 /* Disaster mode. */
1127 get_disaster_srv(time_period_num, sr_value);
1128 }
1129 return sr_value;
1130}
1131
1132/** Return a newly allocated buffer containing the previous shared random
1133 * value or if not present, a disaster value is computed using the given time
1134 * period number. This function can't fail. */
1135uint8_t *
1136hs_get_previous_srv(uint64_t time_period_num, const networkstatus_t *ns)
1137{
1138 uint8_t *sr_value = tor_malloc_zero(DIGEST256_LEN);
1139 const sr_srv_t *previous_srv = sr_get_previous(ns);
1140
1141 if (previous_srv) {
1142 memcpy(sr_value, previous_srv->value, sizeof(previous_srv->value));
1143 } else {
1144 /* Disaster mode. */
1145 get_disaster_srv(time_period_num, sr_value);
1146 }
1147 return sr_value;
1148}
1149
1150/** Return the number of replicas defined by a consensus parameter or the
1151 * default value. */
1152int32_t
1154{
1155 /* The [1,16] range is a specification requirement. */
1156 return networkstatus_get_param(NULL, "hsdir_n_replicas",
1158}
1159
1160/** Return the spread fetch value defined by a consensus parameter or the
1161 * default value. */
1162int32_t
1164{
1165 /* The [1,128] range is a specification requirement. */
1166 return networkstatus_get_param(NULL, "hsdir_spread_fetch",
1168}
1169
1170/** Return the spread store value defined by a consensus parameter or the
1171 * default value. */
1172int32_t
1174{
1175 /* The [1,128] range is a specification requirement. */
1176 return networkstatus_get_param(NULL, "hsdir_spread_store",
1178}
1179
1180/** <b>node</b> is an HSDir so make sure that we have assigned an hsdir index.
1181 * Return 0 if everything is as expected, else return -1. */
1182static int
1184{
1186
1187 /* A node can't have an HSDir index without a descriptor since we need desc
1188 * to get its ed25519 key. for_direct_connect should be zero, since we
1189 * always use the consensus-indexed node's keys to build the hash ring, even
1190 * if some of the consensus-indexed nodes are also bridges. */
1191 if (!node_has_preferred_descriptor(node, 0)) {
1192 return 0;
1193 }
1194
1195 /* At this point, since the node has a desc, this node must also have an
1196 * hsdir index. If not, something went wrong, so BUG out. */
1197 if (BUG(fast_mem_is_zero((const char*)node->hsdir_index.fetch,
1198 DIGEST256_LEN))) {
1199 return 0;
1200 }
1201 if (BUG(fast_mem_is_zero((const char*)node->hsdir_index.store_first,
1202 DIGEST256_LEN))) {
1203 return 0;
1204 }
1205 if (BUG(fast_mem_is_zero((const char*)node->hsdir_index.store_second,
1206 DIGEST256_LEN))) {
1207 return 0;
1208 }
1209
1210 return 1;
1211}
1212
1213/** For a given blinded key and time period number, get the responsible HSDir
1214 * and put their routerstatus_t object in the responsible_dirs list. If
1215 * 'use_second_hsdir_index' is true, use the second hsdir_index of the node_t
1216 * is used. If 'for_fetching' is true, the spread fetch consensus parameter is
1217 * used else the spread store is used which is only for upload. This function
1218 * can't fail but it is possible that the responsible_dirs list contains fewer
1219 * nodes than expected.
1220 *
1221 * This function goes over the latest consensus routerstatus list and sorts it
1222 * by their node_t hsdir_index then does a binary search to find the closest
1223 * node. All of this makes it a bit CPU intensive so use it wisely. */
1224void
1226 uint64_t time_period_num, int use_second_hsdir_index,
1227 int for_fetching, smartlist_t *responsible_dirs)
1228{
1229 smartlist_t *sorted_nodes;
1230 /* The compare function used for the smartlist bsearch. We have two
1231 * different depending on is_next_period. */
1232 int (*cmp_fct)(const void *, const void **);
1233
1234 tor_assert(blinded_pk);
1235 tor_assert(responsible_dirs);
1236
1237 sorted_nodes = smartlist_new();
1238
1239 /* Make sure we actually have a live consensus */
1240 networkstatus_t *c =
1243 if (!c || smartlist_len(c->routerstatus_list) == 0) {
1244 log_warn(LD_REND, "No live consensus so we can't get the responsible "
1245 "hidden service directories.");
1246 goto done;
1247 }
1248
1249 /* Ensure the nodelist is fresh, since it contains the HSDir indices. */
1251
1252 /* Add every node_t that support HSDir v3 for which we do have a valid
1253 * hsdir_index already computed for them for this consensus. */
1254 {
1256 /* Even though this node_t object won't be modified and should be const,
1257 * we can't add const object in a smartlist_t. */
1258 node_t *n = node_get_mutable_by_id(rs->identity_digest);
1259 tor_assert(n);
1260 if (node_supports_v3_hsdir(n) && rs->is_hs_dir) {
1261 if (!node_has_hsdir_index(n)) {
1262 log_info(LD_GENERAL, "Node %s was found without hsdir index.",
1263 node_describe(n));
1264 continue;
1265 }
1266 smartlist_add(sorted_nodes, n);
1267 }
1268 } SMARTLIST_FOREACH_END(rs);
1269 }
1270 if (smartlist_len(sorted_nodes) == 0) {
1271 log_warn(LD_REND, "No nodes found to be HSDir or supporting v3.");
1272 goto done;
1273 }
1274
1275 /* First thing we have to do is sort all node_t by hsdir_index. The
1276 * is_next_period tells us if we want the current or the next one. Set the
1277 * bsearch compare function also while we are at it. */
1278 if (for_fetching) {
1281 } else if (use_second_hsdir_index) {
1284 } else {
1287 }
1288
1289 /* For all replicas, we'll select a set of HSDirs using the consensus
1290 * parameters and the sorted list. The replica starting at value 1 is
1291 * defined by the specification. */
1292 for (int replica = 1; replica <= hs_get_hsdir_n_replicas(); replica++) {
1293 int idx, start, found, n_added = 0;
1294 uint8_t hs_index[DIGEST256_LEN] = {0};
1295 /* Number of node to add to the responsible dirs list depends on if we are
1296 * trying to fetch or store. A client always fetches. */
1297 int n_to_add = (for_fetching) ? hs_get_hsdir_spread_fetch() :
1299
1300 /* Get the index that we should use to select the node. */
1301 hs_build_hs_index(replica, blinded_pk, time_period_num, hs_index);
1302 /* The compare function pointer has been set correctly earlier. */
1303 start = idx = smartlist_bsearch_idx(sorted_nodes, hs_index, cmp_fct,
1304 &found);
1305 /* Getting the length of the list if no member is greater than the key we
1306 * are looking for so start at the first element. */
1307 if (idx == smartlist_len(sorted_nodes)) {
1308 start = idx = 0;
1309 }
1310 while (n_added < n_to_add) {
1311 const node_t *node = smartlist_get(sorted_nodes, idx);
1312 /* If the node has already been selected which is possible between
1313 * replicas, the specification says to skip over. */
1314 if (!smartlist_contains(responsible_dirs, node->rs)) {
1315 smartlist_add(responsible_dirs, node->rs);
1316 ++n_added;
1317 }
1318 if (++idx == smartlist_len(sorted_nodes)) {
1319 /* Wrap if we've reached the end of the list. */
1320 idx = 0;
1321 }
1322 if (idx == start) {
1323 /* We've gone over the whole list, stop and avoid infinite loop. */
1324 break;
1325 }
1326 }
1327 }
1328
1329 done:
1330 smartlist_free(sorted_nodes);
1331}
1332
1333/*********************** HSDir request tracking ***************************/
1334
1335/** Return the period for which a hidden service directory cannot be queried
1336 * for the same descriptor ID again, taking TestingTorNetwork into account. */
1337time_t
1339{
1340 tor_assert(options);
1341
1342 if (options->TestingTorNetwork) {
1343 return REND_HID_SERV_DIR_REQUERY_PERIOD_TESTING;
1344 } else {
1345 return REND_HID_SERV_DIR_REQUERY_PERIOD;
1346 }
1347}
1348
1349/** Tracks requests for fetching hidden service descriptors. It's used by
1350 * hidden service clients, to avoid querying HSDirs that have already failed
1351 * giving back a descriptor. The same data structure is used to track v3 HS
1352 * descriptor requests.
1353 *
1354 * The string map is a key/value store that contains the last request times to
1355 * hidden service directories for certain queries. Specifically:
1356 *
1357 * key = base32(hsdir_identity) + base32(hs_identity)
1358 * value = time_t of last request for that hs_identity to that HSDir
1359 *
1360 * where 'hsdir_identity' is the identity digest of the HSDir node, and
1361 * 'hs_identity' is the ed25519 blinded public key of the HS for v3. */
1362static strmap_t *last_hid_serv_requests_ = NULL;
1363
1364/** Returns last_hid_serv_requests_, initializing it to a new strmap if
1365 * necessary. */
1366STATIC strmap_t *
1368{
1370 last_hid_serv_requests_ = strmap_new();
1372}
1373
1374/** Look up the last request time to hidden service directory <b>hs_dir</b>
1375 * for descriptor request key <b>req_key_str</b> which is the blinded key for
1376 * v3. If <b>set</b> is non-zero, assign the current time <b>now</b> and
1377 * return that. Otherwise, return the most recent request time, or 0 if no
1378 * such request has been sent before. */
1379time_t
1381 const char *req_key_str,
1382 time_t now, int set)
1383{
1384 char hsdir_id_base32[BASE32_DIGEST_LEN + 1];
1385 char *hsdir_desc_comb_id = NULL;
1386 time_t *last_request_ptr;
1387 strmap_t *last_hid_serv_requests = get_last_hid_serv_requests();
1388
1389 /* Create the key */
1390 base32_encode(hsdir_id_base32, sizeof(hsdir_id_base32),
1391 hs_dir->identity_digest, DIGEST_LEN);
1392 tor_asprintf(&hsdir_desc_comb_id, "%s%s", hsdir_id_base32, req_key_str);
1393
1394 if (set) {
1395 time_t *oldptr;
1396 last_request_ptr = tor_malloc_zero(sizeof(time_t));
1397 *last_request_ptr = now;
1398 oldptr = strmap_set(last_hid_serv_requests, hsdir_desc_comb_id,
1399 last_request_ptr);
1400 tor_free(oldptr);
1401 } else {
1402 last_request_ptr = strmap_get(last_hid_serv_requests,
1403 hsdir_desc_comb_id);
1404 }
1405
1406 tor_free(hsdir_desc_comb_id);
1407 return (last_request_ptr) ? *last_request_ptr : 0;
1408}
1409
1410/** Clean the history of request times to hidden service directories, so that
1411 * it does not contain requests older than REND_HID_SERV_DIR_REQUERY_PERIOD
1412 * seconds any more. */
1413void
1415{
1416 strmap_iter_t *iter;
1417 time_t cutoff = now - hs_hsdir_requery_period(get_options());
1418 strmap_t *last_hid_serv_requests = get_last_hid_serv_requests();
1419 for (iter = strmap_iter_init(last_hid_serv_requests);
1420 !strmap_iter_done(iter); ) {
1421 const char *key;
1422 void *val;
1423 time_t *ent;
1424 strmap_iter_get(iter, &key, &val);
1425 ent = (time_t *) val;
1426 if (*ent < cutoff) {
1427 iter = strmap_iter_next_rmv(last_hid_serv_requests, iter);
1428 tor_free(ent);
1429 } else {
1430 iter = strmap_iter_next(last_hid_serv_requests, iter);
1431 }
1432 }
1433}
1434
1435/** Remove all requests related to the descriptor request key string
1436 * <b>req_key_str</b> from the history of times of requests to hidden service
1437 * directories.
1438 *
1439 * This is called from purge_hid_serv_request(), which must be idempotent, so
1440 * any future changes to this function must leave it idempotent too. */
1441void
1443{
1444 strmap_iter_t *iter;
1445 strmap_t *last_hid_serv_requests = get_last_hid_serv_requests();
1446
1447 for (iter = strmap_iter_init(last_hid_serv_requests);
1448 !strmap_iter_done(iter); ) {
1449 const char *key;
1450 void *val;
1451 strmap_iter_get(iter, &key, &val);
1452
1453 /* XXX: The use of REND_DESC_ID_V2_LEN_BASE32 is very wrong in terms of
1454 * semantic, see #23305. */
1455
1456 /* This strmap contains variable-sized elements so this is a basic length
1457 * check on the strings we are about to compare. The key is variable sized
1458 * since it's composed as follows:
1459 * key = base32(hsdir_identity) + base32(req_key_str)
1460 * where 'req_key_str' is the ed25519 blinded public key of the HS v3. */
1461 if (strlen(key) < REND_DESC_ID_V2_LEN_BASE32 + strlen(req_key_str)) {
1462 iter = strmap_iter_next(last_hid_serv_requests, iter);
1463 continue;
1464 }
1465
1466 /* Check if the tracked request matches our request key */
1467 if (tor_memeq(key + REND_DESC_ID_V2_LEN_BASE32, req_key_str,
1468 strlen(req_key_str))) {
1469 iter = strmap_iter_next_rmv(last_hid_serv_requests, iter);
1470 tor_free(val);
1471 } else {
1472 iter = strmap_iter_next(last_hid_serv_requests, iter);
1473 }
1474 }
1475}
1476
1477/** Purge the history of request times to hidden service directories,
1478 * so that future lookups of an HS descriptor will not fail because we
1479 * accessed all of the HSDir relays responsible for the descriptor
1480 * recently. */
1481void
1483{
1484 /* Don't create the table if it doesn't exist yet (and it may very
1485 * well not exist if the user hasn't accessed any HSes)... */
1486 strmap_t *old_last_hid_serv_requests = last_hid_serv_requests_;
1487 /* ... and let get_last_hid_serv_requests re-create it for us if
1488 * necessary. */
1490
1491 if (old_last_hid_serv_requests != NULL) {
1492 log_info(LD_REND, "Purging client last-HS-desc-request-time table");
1493 strmap_free(old_last_hid_serv_requests, tor_free_);
1494 }
1495}
1496
1497/***********************************************************************/
1498
1499/** Given the list of responsible HSDirs in <b>responsible_dirs</b>, pick the
1500 * one that we should use to fetch a descriptor right now. Take into account
1501 * previous failed attempts at fetching this descriptor from HSDirs using the
1502 * string identifier <b>req_key_str</b>. We return whether we are rate limited
1503 * into *<b>is_rate_limited_out</b> if it is not NULL.
1504 *
1505 * Steals ownership of <b>responsible_dirs</b>.
1506 *
1507 * Return the routerstatus of the chosen HSDir if successful, otherwise return
1508 * NULL if no HSDirs are worth trying right now. */
1510hs_pick_hsdir(smartlist_t *responsible_dirs, const char *req_key_str,
1511 bool *is_rate_limited_out)
1512{
1513 smartlist_t *usable_responsible_dirs = smartlist_new();
1514 const or_options_t *options = get_options();
1515 routerstatus_t *hs_dir;
1516 time_t now = time(NULL);
1517 int excluded_some;
1518 bool rate_limited = false;
1519 int rate_limited_count = 0;
1520 int responsible_dirs_count = smartlist_len(responsible_dirs);
1521
1522 tor_assert(req_key_str);
1523
1524 /* Clean outdated request history first. */
1526
1527 /* Only select those hidden service directories to which we did not send a
1528 * request recently and for which we have a router descriptor here.
1529 *
1530 * Use for_direct_connect==0 even if we will be connecting to the node
1531 * directly, since we always use the key information in the
1532 * consensus-indexed node descriptors for building the index.
1533 **/
1534 SMARTLIST_FOREACH_BEGIN(responsible_dirs, routerstatus_t *, dir) {
1535 time_t last = hs_lookup_last_hid_serv_request(dir, req_key_str, 0, 0);
1536 const node_t *node = node_get_by_id(dir->identity_digest);
1537 if (last + hs_hsdir_requery_period(options) >= now ||
1538 !node || !node_has_preferred_descriptor(node, 0)) {
1539 SMARTLIST_DEL_CURRENT(responsible_dirs, dir);
1540 rate_limited_count++;
1541 continue;
1542 }
1543 if (!routerset_contains_node(options->ExcludeNodes, node)) {
1544 smartlist_add(usable_responsible_dirs, dir);
1545 }
1546 } SMARTLIST_FOREACH_END(dir);
1547
1548 if (rate_limited_count > 0 || responsible_dirs_count > 0) {
1549 rate_limited = rate_limited_count == responsible_dirs_count;
1550 }
1551
1552 excluded_some =
1553 smartlist_len(usable_responsible_dirs) < smartlist_len(responsible_dirs);
1554
1555 hs_dir = smartlist_choose(usable_responsible_dirs);
1556 if (!hs_dir && !options->StrictNodes) {
1557 hs_dir = smartlist_choose(responsible_dirs);
1558 }
1559
1560 smartlist_free(responsible_dirs);
1561 smartlist_free(usable_responsible_dirs);
1562 if (!hs_dir) {
1563 const char *warn_str = (rate_limited) ? "we are rate limited." :
1564 "we requested them all recently without success";
1565 log_info(LD_REND, "Could not pick one of the responsible hidden "
1566 "service directories, because %s.", warn_str);
1567 if (options->StrictNodes && excluded_some) {
1568 log_warn(LD_REND, "Could not pick a hidden service directory for the "
1569 "requested hidden service: they are all either down or "
1570 "excluded, and StrictNodes is set.");
1571 }
1572 } else {
1573 /* Remember that we are requesting a descriptor from this hidden service
1574 * directory now. */
1575 hs_lookup_last_hid_serv_request(hs_dir, req_key_str, now, 1);
1576 }
1577
1578 if (is_rate_limited_out != NULL) {
1579 *is_rate_limited_out = rate_limited;
1580 }
1581
1582 return hs_dir;
1583}
1584
1585/** Given a list of link specifiers lspecs, a curve 25519 onion_key, and
1586 * a direct connection boolean direct_conn (true for single onion services),
1587 * return a newly allocated extend_info_t object.
1588 *
1589 * This function always returns an extend info with a valid IP address and
1590 * ORPort, or NULL. If direct_conn is false, the IP address is always IPv4.
1591 *
1592 * It performs the following checks:
1593 * if the onion key is unusable, return NULL.
1594 * if there is no usable IP address, or legacy ID is missing, return NULL.
1595 * if direct_conn, and we can't reach any IP address, return NULL.
1596 */
1599 const curve25519_public_key_t *onion_key,
1600 int direct_conn)
1601{
1602 int have_v4 = 0, have_legacy_id = 0, have_ed25519_id = 0;
1603 char legacy_id[DIGEST_LEN] = {0};
1604 ed25519_public_key_t ed25519_pk;
1605 extend_info_t *info = NULL;
1606 tor_addr_port_t ap;
1607
1608 tor_addr_make_null(&ap.addr, AF_UNSPEC);
1609 ap.port = 0;
1610
1611 if (lspecs == NULL) {
1612 log_warn(LD_BUG, "Specified link specifiers is null");
1613 goto done;
1614 }
1615
1616 if (onion_key == NULL) {
1617 log_warn(LD_BUG, "Specified onion key is null");
1618 goto done;
1619 }
1620
1621 if (!curve25519_public_key_is_ok(onion_key)) {
1622 log_debug(LD_REND, "Invalid ntor onion key");
1623 goto done;
1624 }
1625
1626 if (smartlist_len(lspecs) == 0) {
1627 log_fn(LOG_PROTOCOL_WARN, LD_REND, "Empty link specifier list.");
1628 /* Return NULL. */
1629 goto done;
1630 }
1631
1632 SMARTLIST_FOREACH_BEGIN(lspecs, const link_specifier_t *, ls) {
1633 switch (link_specifier_get_ls_type(ls)) {
1634 case LS_IPV4:
1635 /* Skip if we already seen a v4. If direct_conn is true, we skip this
1636 * block because reachable_addr_choose_from_ls() will set ap. If
1637 * direct_conn is false, set ap to the first IPv4 address and port in
1638 * the link specifiers.*/
1639 if (have_v4 || direct_conn) continue;
1640 tor_addr_from_ipv4h(&ap.addr,
1641 link_specifier_get_un_ipv4_addr(ls));
1642 ap.port = link_specifier_get_un_ipv4_port(ls);
1643 have_v4 = 1;
1644 break;
1645 case LS_LEGACY_ID:
1646 /* Make sure we do have enough bytes for the legacy ID. */
1647 if (link_specifier_getlen_un_legacy_id(ls) < sizeof(legacy_id)) {
1648 break;
1649 }
1650 memcpy(legacy_id, link_specifier_getconstarray_un_legacy_id(ls),
1651 sizeof(legacy_id));
1652 have_legacy_id = 1;
1653 break;
1654 case LS_ED25519_ID:
1655 memcpy(ed25519_pk.pubkey,
1656 link_specifier_getconstarray_un_ed25519_id(ls),
1658 have_ed25519_id = 1;
1659 break;
1660 default:
1661 /* Ignore unknown. */
1662 break;
1663 }
1664 } SMARTLIST_FOREACH_END(ls);
1665
1666 /* Choose a preferred address first, but fall back to an allowed address. */
1667 if (direct_conn)
1668 reachable_addr_choose_from_ls(lspecs, 0, &ap);
1669
1670 /* Legacy ID is mandatory, and we require an IP address. */
1671 if (!tor_addr_port_is_valid_ap(&ap, 0)) {
1672 /* If we're missing the IP address, log a warning and return NULL. */
1673 log_info(LD_NET, "Unreachable or invalid IP address in link state");
1674 goto done;
1675 }
1676 if (!have_legacy_id) {
1677 /* If we're missing the legacy ID, log a warning and return NULL. */
1678 log_warn(LD_PROTOCOL, "Missing Legacy ID in link state");
1679 goto done;
1680 }
1681
1682 /* We will add support for falling back to a 3-hop path in a later
1683 * release. */
1684
1685 /* We'll validate now that the address we've picked isn't a private one. If
1686 * it is, are we allowed to extend to private addresses? */
1687 if (!extend_info_addr_is_allowed(&ap.addr)) {
1688 log_fn(LOG_PROTOCOL_WARN, LD_REND,
1689 "Requested address is private and we are not allowed to extend to "
1690 "it: %s:%u", safe_str(fmt_addr(&ap.addr)), ap.port);
1691 goto done;
1692 }
1693
1694 /* We do have everything for which we think we can connect successfully. */
1695 info = extend_info_new(NULL, legacy_id,
1696 (have_ed25519_id) ? &ed25519_pk : NULL,
1697 onion_key, &ap.addr, ap.port, NULL, false);
1698 done:
1699 return info;
1700}
1701
1702/***********************************************************************/
1703
1704/** Initialize the entire HS subsystem. This is called in tor_init() before any
1705 * torrc options are loaded. Only for >= v3. */
1706void
1708{
1711 hs_cache_init();
1712}
1713
1714/** Release and cleanup all memory of the HS subsystem (all version). This is
1715 * called by tor_free_all(). */
1716void
1725
1726/** For the given origin circuit circ, decrement the number of rendezvous
1727 * stream counter. This handles every hidden service version. */
1728void
1730{
1731 tor_assert(circ);
1732
1733 if (circ->hs_ident) {
1734 circ->hs_ident->num_rdv_streams--;
1735 } else {
1736 /* Should not be called if this circuit is not for hidden service. */
1738 }
1739}
1740
1741/** For the given origin circuit circ, increment the number of rendezvous
1742 * stream counter. This handles every hidden service version. */
1743void
1745{
1746 tor_assert(circ);
1747
1748 if (circ->hs_ident) {
1749 circ->hs_ident->num_rdv_streams++;
1750 } else {
1751 /* Should not be called if this circuit is not for hidden service. */
1753 }
1754}
1755
1756/** Return a newly allocated link specifier object that is a copy of dst. */
1757link_specifier_t *
1758link_specifier_dup(const link_specifier_t *src)
1759{
1760 link_specifier_t *dup = NULL;
1761 uint8_t *buf = NULL;
1762
1763 if (BUG(!src)) {
1764 goto err;
1765 }
1766
1767 ssize_t encoded_len_alloc = link_specifier_encoded_len(src);
1768 if (BUG(encoded_len_alloc < 0)) {
1769 goto err;
1770 }
1771
1772 buf = tor_malloc_zero(encoded_len_alloc);
1773 ssize_t encoded_len_data = link_specifier_encode(buf,
1774 encoded_len_alloc,
1775 src);
1776 if (BUG(encoded_len_data < 0)) {
1777 goto err;
1778 }
1779
1780 ssize_t parsed_len = link_specifier_parse(&dup, buf, encoded_len_alloc);
1781 if (BUG(parsed_len < 0)) {
1782 goto err;
1783 }
1784
1785 goto done;
1786
1787 err:
1788 dup = NULL;
1789
1790 done:
1791 tor_free(buf);
1792 return dup;
1793}
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition address.c:933
void tor_addr_make_unspec(tor_addr_t *a)
Definition address.c:225
void tor_addr_make_null(tor_addr_t *a, sa_family_t family)
Definition address.c:235
#define tor_addr_from_ipv4h(dest, v4addr)
Definition address.h:329
#define fmt_addr(a)
Definition address.h:241
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
void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition binascii.c:60
static void set_uint64(void *cp, uint64_t v)
Definition bytes.h:96
static uint8_t get_uint8(const void *cp)
Definition bytes.h:23
static void set_uint8(void *cp, uint8_t v)
Definition bytes.h:31
static uint64_t tor_htonll(uint64_t a)
Definition bytes.h:184
Header file for circuitbuild.c.
int port_cfg_line_extract_addrport(const char *line, char **addrport_out, int *is_unix_out, const char **rest_out)
Definition config.c:5993
const char * escaped_safe_str(const char *address)
Definition config.c:1161
const or_options_t * get_options(void)
Definition config.c:949
Header file for config.c.
int curve25519_public_key_is_ok(const curve25519_public_key_t *key)
Header for crypto_curve25519.c.
int crypto_digest256(char *digest, const char *m, size_t len, digest_algorithm_t algorithm)
#define BASE32_DIGEST_LEN
void crypto_digest_get_digest(crypto_digest_t *digest, char *out, size_t out_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)
int ed25519_validate_pubkey(const ed25519_public_key_t *pubkey)
int ed25519_keypair_blind(ed25519_keypair_t *out, const ed25519_keypair_t *inp, const uint8_t *param)
int ed25519_public_blind(ed25519_public_key_t *out, const ed25519_public_key_t *inp, const uint8_t *param)
void * smartlist_choose(const smartlist_t *sl)
Common functions for using (pseudo-)random number generators.
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition crypto_util.c:55
Common functions for cryptographic routines.
const char * node_describe(const node_t *node)
Definition describe.c:160
Header file for describe.c.
int tor_memeq(const void *a, const void *b, size_t sz)
Definition di_ops.c:107
int tor_memcmp(const void *a, const void *b, size_t len)
Definition di_ops.c:31
#define DIGEST_LEN
#define DIGEST256_LEN
int check_private_dir(const char *dirname, cpd_check_t check, const char *effective_user)
Definition dir.c:71
unsigned int cpd_check_t
Definition dir.h:20
Edge-connection structure.
const char * escaped(const char *s)
Definition escape.c:126
extend_info_t * extend_info_new(const char *nickname, const char *rsa_id_digest, const ed25519_public_key_t *ed_id, const curve25519_public_key_t *ntor_key, const tor_addr_t *addr, uint16_t port, const protover_summary_flags_t *pv, bool for_exit_use)
Definition extendinfo.c:34
int extend_info_addr_is_allowed(const tor_addr_t *addr)
Definition extendinfo.c:231
Header for core/or/extendinfo.c.
void hs_cache_free_all(void)
Definition hs_cache.c:1244
void hs_cache_init(void)
Definition hs_cache.c:1229
Header file for hs_cache.c.
void hs_circuitmap_free_all(void)
void hs_circuitmap_init(void)
Header file for hs_circuitmap.c.
void hs_client_free_all(void)
Definition hs_client.c:2764
Header file containing client data for the 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:1225
static strmap_t * last_hid_serv_requests_
Definition hs_common.c:1362
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:952
hs_port_config_t * hs_parse_port_config(const char *string, const char *sep, char **err_msg_out)
Definition hs_common.c:686
static void hs_parse_address_impl(const char *address, ed25519_public_key_t *key_out, uint8_t *checksum_out, uint8_t *version_out)
Definition hs_common.c:536
void hs_port_config_free_(hs_port_config_t *p)
Definition hs_common.c:788
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:566
static const char * str_ed25519_basepoint
Definition hs_common.c:51
time_t hs_lookup_last_hid_serv_request(routerstatus_t *hs_dir, const char *req_key_str, time_t now, int set)
Definition hs_common.c:1380
routerstatus_t * hs_pick_hsdir(smartlist_t *responsible_dirs, const char *req_key_str, bool *is_rate_limited_out)
Definition hs_common.c:1510
uint64_t hs_get_time_period_num(time_t now)
Definition hs_common.c:270
void hs_purge_last_hid_serv_requests(void)
Definition hs_common.c:1482
void hs_build_hs_index(uint64_t replica, const ed25519_public_key_t *blinded_pk, uint64_t period_num, uint8_t *hs_index_out)
Definition hs_common.c:1038
static int compare_digest_to_fetch_hsdir_index(const void *_key, const void **_member)
Definition hs_common.c:114
static int compare_digest_to_store_second_hsdir_index(const void *_key, const void **_member)
Definition hs_common.c:135
static int compare_node_store_second_hsdir_index(const void **a, const void **b)
Definition hs_common.c:167
time_t hs_get_start_time_of_next_time_period(time_t now)
Definition hs_common.c:325
uint8_t * hs_get_current_srv(uint64_t time_period_num, const networkstatus_t *ns)
Definition hs_common.c:1118
void hs_build_blinded_pubkey(const ed25519_public_key_t *pk, const uint8_t *secret, size_t secret_len, uint64_t time_period_num, ed25519_public_key_t *blinded_pk_out)
Definition hs_common.c:928
static int compare_node_store_first_hsdir_index(const void **a, const void **b)
Definition hs_common.c:156
void hs_purge_hid_serv_from_last_hid_serv_requests(const char *req_key_str)
Definition hs_common.c:1442
STATIC strmap_t * get_last_hid_serv_requests(void)
Definition hs_common.c:1367
uint64_t hs_get_next_time_period_num(time_t now)
Definition hs_common.c:307
int32_t hs_get_hsdir_n_replicas(void)
Definition hs_common.c:1153
uint8_t * hs_get_previous_srv(uint64_t time_period_num, const networkstatus_t *ns)
Definition hs_common.c:1136
STATIC void build_blinded_key_param(const ed25519_public_key_t *pubkey, const uint8_t *secret, size_t secret_len, uint64_t period_num, uint64_t period_length, uint8_t *param_out)
Definition hs_common.c:437
static void build_hs_checksum(const ed25519_public_key_t *key, uint8_t version, uint8_t *checksum_out)
Definition hs_common.c:486
void hs_build_address(const ed25519_public_key_t *key, uint8_t version, char *addr_out)
Definition hs_common.c:902
void hs_dec_rdv_stream_counter(origin_circuit_t *circ)
Definition hs_common.c:1729
static void build_hs_address(const ed25519_public_key_t *key, const uint8_t *checksum, uint8_t version, char *addr_out)
Definition hs_common.c:513
uint64_t hs_get_previous_time_period_num(time_t now)
Definition hs_common.c:316
static int compare_node_fetch_hsdir_index(const void **a, const void **b)
Definition hs_common.c:145
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:841
int hs_address_is_valid(const char *address)
Definition hs_common.c:857
int hs_get_service_max_rend_failures(void)
Definition hs_common.c:234
static hs_port_config_t * hs_port_config_new(const char *socket_path)
Definition hs_common.c:664
int32_t hs_get_hsdir_spread_fetch(void)
Definition hs_common.c:1163
static void compute_disaster_srv(uint64_t time_period_num, uint8_t *srv_out)
Definition hs_common.c:341
int hs_set_conn_addr_port(const smartlist_t *ports, edge_connection_t *conn)
Definition hs_common.c:607
time_t hs_hsdir_requery_period(const or_options_t *options)
Definition hs_common.c:1338
extend_info_t * hs_get_extend_info_from_lspecs(const smartlist_t *lspecs, const curve25519_public_key_t *onion_key, int direct_conn)
Definition hs_common.c:1598
void hs_init(void)
Definition hs_common.c:1707
static int compare_digest_to_store_first_hsdir_index(const void *_key, const void **_member)
Definition hs_common.c:124
void hs_inc_rdv_stream_counter(origin_circuit_t *circ)
Definition hs_common.c:1744
STATIC void get_disaster_srv(uint64_t time_period_num, uint8_t *srv_out)
Definition hs_common.c:385
void hs_build_hsdir_index(const ed25519_public_key_t *identity_pk, const uint8_t *srv_value, uint64_t period_num, uint8_t *hsdir_index_out)
Definition hs_common.c:1079
int hs_check_service_private_dir(const char *username, const char *path, unsigned int dir_group_readable, unsigned int create)
Definition hs_common.c:201
void hs_clean_last_hid_serv_requests(time_t now)
Definition hs_common.c:1414
void hs_free_all(void)
Definition hs_common.c:1717
link_specifier_t * link_specifier_dup(const link_specifier_t *src)
Definition hs_common.c:1758
STATIC uint64_t get_time_period_length(void)
Definition hs_common.c:244
static uint8_t cached_disaster_srv[2][DIGEST256_LEN]
Definition hs_common.c:378
int hs_service_requires_uptime_circ(const smartlist_t *ports)
Definition hs_common.c:1017
int hs_in_period_between_tp_and_srv(const networkstatus_t *consensus, time_t now)
Definition hs_common.c:988
int hs_parse_address_no_log(const char *address, ed25519_public_key_t *key_out, uint8_t *checksum_out, uint8_t *version_out, const char **errmsg)
Definition hs_common.c:801
int32_t hs_get_hsdir_spread_store(void)
Definition hs_common.c:1173
static int node_has_hsdir_index(const node_t *node)
Definition hs_common.c:1183
char * hs_path_from_filename(const char *directory, const char *filename)
Definition hs_common.c:179
Header file containing common data for the whole HS subsystem.
#define HS_TIME_PERIOD_LENGTH_MIN
Definition hs_common.h:79
#define HSDIR_INDEX_PREFIX
Definition hs_common.h:104
#define HS_INDEX_PREFIX
Definition hs_common.h:100
#define HS_TIME_PERIOD_LENGTH_MAX
Definition hs_common.h:81
#define HS_SERVICE_ADDR_CHECKSUM_PREFIX
Definition hs_common.h:53
#define HS_SRV_DISASTER_PREFIX
Definition hs_common.h:108
#define HS_SERVICE_ADDR_CHECKSUM_INPUT_LEN
Definition hs_common.h:61
#define HS_DEFAULT_HSDIR_SPREAD_FETCH
Definition hs_common.h:116
#define HS_DEFAULT_HSDIR_SPREAD_STORE
Definition hs_common.h:114
#define HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN
Definition hs_common.h:55
#define HS_KEYBLIND_NONCE_PREFIX
Definition hs_common.h:88
#define HS_SERVICE_ADDR_CHECKSUM_LEN_USED
Definition hs_common.h:64
#define HS_DEFAULT_HSDIR_N_REPLICAS
Definition hs_common.h:112
#define HS_SERVICE_ADDR_LEN
Definition hs_common.h:69
#define HS_CREDENTIAL_PREFIX
Definition hs_common.h:94
#define HS_TIME_PERIOD_LENGTH_DEFAULT
Definition hs_common.h:77
#define HS_SERVICE_ADDR_LEN_BASE32
Definition hs_common.h:73
Header file containing denial of service defenses for the HS subsystem for all versions.
Header file containing circuit and connection identifier data for the whole HS subsystem.
void hs_ob_free_all(void)
Definition hs_ob.c:406
Header file for the specific code for onion balance.
void hs_service_init(void)
void hs_service_free_all(void)
Header file containing service data for the HS subsystem.
#define log_fn(severity, domain, args,...)
Definition log.h:283
#define LD_REND
Definition log.h:84
#define LD_PROTOCOL
Definition log.h:72
#define LD_BUG
Definition log.h:86
#define LD_NET
Definition log.h:66
#define LD_GENERAL
Definition log.h:62
void tor_free_(void *mem)
Definition malloc.c:227
#define tor_free(p)
Definition malloc.h:56
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.
Node information structure.
const node_t * node_get_by_id(const char *identity_digest)
Definition nodelist.c:226
int node_has_preferred_descriptor(const node_t *node, int for_direct_connect)
Definition nodelist.c:1534
bool node_supports_v3_hsdir(const node_t *node)
Definition nodelist.c:1276
node_t * node_get_mutable_by_id(const char *identity_digest)
Definition nodelist.c:197
void nodelist_ensure_freshness(const networkstatus_t *ns)
Definition nodelist.c:1051
Header file for nodelist.c.
Master header file for Tor-specific functionality.
#define REND_DESC_ID_V2_LEN_BASE32
Definition or.h:398
#define TO_CONN(c)
Definition or.h:709
Origin circuit structure.
long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
Definition parse_int.c:59
void reachable_addr_choose_from_ls(const smartlist_t *lspecs, int pref_only, tor_addr_port_t *ap)
Definition policies.c:913
Header file for policies.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition printf.c:75
Header file for rendcommon.c.
int tor_addr_port_lookup(const char *s, tor_addr_t *addr_out, uint16_t *port_out)
Definition resolve.c:252
Header for resolve.c.
Header file for routermode.c.
int routerset_contains_node(const routerset_t *set, const node_t *node)
Definition routerset.c:353
Header file for routerset.c.
Routerstatus (consensus entry) structure.
unsigned int sr_state_get_protocol_run_duration(void)
time_t sr_state_get_start_time_of_current_protocol_run(void)
const sr_srv_t * sr_get_current(const networkstatus_t *ns)
const sr_srv_t * sr_get_previous(const networkstatus_t *ns)
unsigned int sr_state_get_phase_duration(void)
Header file for shared_random_client.c.
Header for shared_random_state.c.
int smartlist_contains_int_as_string(const smartlist_t *sl, int num)
Definition smartlist.c:147
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition smartlist.c:334
int smartlist_bsearch_idx(const smartlist_t *sl, const void *key, int(*compare)(const void *key, const void **member), int *found_out)
Definition smartlist.c:428
int smartlist_contains(const smartlist_t *sl, const void *element)
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
#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)
tor_addr_t addr
uint64_t num_rdv_streams
Definition hs_ident.h:87
uint16_t orig_virtual_port
Definition hs_ident.h:117
char unix_addr[FLEXIBLE_ARRAY_MEMBER]
Definition hs_common.h:153
tor_addr_t real_addr
Definition hs_common.h:151
uint16_t real_port
Definition hs_common.h:149
unsigned int is_unix_addr
Definition hs_common.h:147
uint16_t virtual_port
Definition hs_common.h:145
uint8_t fetch[DIGEST256_LEN]
uint8_t store_first[DIGEST256_LEN]
uint8_t store_second[DIGEST256_LEN]
smartlist_t * routerstatus_list
struct routerset_t * ExcludeNodes
struct hs_ident_circuit_t * hs_ident
char identity_digest[DIGEST_LEN]
uint8_t value[DIGEST256_LEN]
#define STATIC
Definition testsupport.h:32
#define MOCK_IMPL(rv, funcname, arglist)
#define tor_assert_nonfatal_unreached()
Definition util_bug.h:177
#define tor_assert(expr)
Definition util_bug.h:103
int fast_mem_is_zero(const char *mem, size_t len)
Definition util_string.c:76
#define ED25519_SECKEY_LEN
#define ED25519_PUBKEY_LEN