Macros | Functions | Variables
hs_common.c File Reference
#include "core/or/or.h"
#include "app/config/config.h"
#include "core/or/circuitbuild.h"
#include "core/or/policies.h"
#include "feature/dirauth/shared_random_state.h"
#include "feature/hs/hs_cache.h"
#include "feature/hs/hs_circuitmap.h"
#include "feature/hs/hs_client.h"
#include "feature/hs/hs_common.h"
#include "feature/hs/hs_ident.h"
#include "feature/hs/hs_service.h"
#include "feature/hs_common/shared_random_client.h"
#include "feature/nodelist/describe.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/nodelist.h"
#include "feature/nodelist/routerset.h"
#include "feature/rend/rendcommon.h"
#include "feature/rend/rendservice.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/crypt_ops/crypto_util.h"
#include "core/or/edge_connection_st.h"
#include "feature/nodelist/networkstatus_st.h"
#include "feature/nodelist/node_st.h"
#include "core/or/origin_circuit_st.h"
#include "feature/nodelist/routerstatus_st.h"
#include "trunnel/ed25519_cert.h"

Go to the source code of this file.




static int set_unix_port (edge_connection_t *conn, rend_service_port_config_t *p)
static int add_unix_port (smartlist_t *ports, rend_service_port_config_t *p)
static int compare_digest_to_fetch_hsdir_index (const void *_key, const void **_member)
static int compare_digest_to_store_first_hsdir_index (const void *_key, const void **_member)
static int compare_digest_to_store_second_hsdir_index (const void *_key, const void **_member)
static int compare_node_fetch_hsdir_index (const void **a, const void **b)
static int compare_node_store_first_hsdir_index (const void **a, const void **b)
static int compare_node_store_second_hsdir_index (const void **a, const void **b)
char * hs_path_from_filename (const char *directory, const char *filename)
int hs_check_service_private_dir (const char *username, const char *path, unsigned int dir_group_readable, unsigned int create)
int hs_get_service_max_rend_failures (void)
STATIC uint64_t get_time_period_length (void)
uint64_t hs_get_time_period_num (time_t now)
uint64_t hs_get_next_time_period_num (time_t now)
uint64_t hs_get_previous_time_period_num (time_t now)
time_t hs_get_start_time_of_next_time_period (time_t now)
static rend_data_trend_data_alloc (uint32_t version)
void rend_data_free_ (rend_data_t *data)
rend_data_trend_data_dup (const rend_data_t *data)
static int compute_desc_id (rend_data_t *rend_data)
rend_data_trend_data_service_create (const char *onion_address, const char *pk_digest, const uint8_t *cookie, rend_auth_type_t auth_type)
rend_data_trend_data_client_create (const char *onion_address, const char *desc_id, const char *cookie, rend_auth_type_t auth_type)
const char * rend_data_get_address (const rend_data_t *rend_data)
const char * rend_data_get_desc_id (const rend_data_t *rend_data, uint8_t replica, size_t *len_out)
const uint8_t * rend_data_get_pk_digest (const rend_data_t *rend_data, size_t *len_out)
static void compute_disaster_srv (uint64_t time_period_num, uint8_t *srv_out)
STATIC void get_disaster_srv (uint64_t time_period_num, uint8_t *srv_out)
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)
static void build_hs_checksum (const ed25519_public_key_t *key, uint8_t version, uint8_t *checksum_out)
static void build_hs_address (const ed25519_public_key_t *key, const uint8_t *checksum, uint8_t version, char *addr_out)
static void hs_parse_address_impl (const char *address, ed25519_public_key_t *key_out, uint8_t *checksum_out, uint8_t *version_out)
void hs_get_subcredential (const ed25519_public_key_t *identity_pk, const ed25519_public_key_t *blinded_pk, uint8_t *subcred_out)
int hs_set_conn_addr_port (const smartlist_t *ports, edge_connection_t *conn)
int hs_parse_address (const char *address, ed25519_public_key_t *key_out, uint8_t *checksum_out, uint8_t *version_out)
int hs_address_is_valid (const char *address)
void hs_build_address (const ed25519_public_key_t *key, uint8_t version, char *addr_out)
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)
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)
 MOCK_IMPL (int, hs_in_period_between_tp_and_srv,(const networkstatus_t *consensus, time_t now))
int hs_service_requires_uptime_circ (const smartlist_t *ports)
void hs_build_hs_index (uint64_t replica, const ed25519_public_key_t *blinded_pk, uint64_t period_num, uint8_t *hs_index_out)
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)
uint8_t * hs_get_current_srv (uint64_t time_period_num, const networkstatus_t *ns)
uint8_t * hs_get_previous_srv (uint64_t time_period_num, const networkstatus_t *ns)
int32_t hs_get_hsdir_n_replicas (void)
int32_t hs_get_hsdir_spread_fetch (void)
int32_t hs_get_hsdir_spread_store (void)
static int node_has_hsdir_index (const node_t *node)
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)
time_t hs_hsdir_requery_period (const or_options_t *options)
STATIC strmap_t * get_last_hid_serv_requests (void)
time_t hs_lookup_last_hid_serv_request (routerstatus_t *hs_dir, const char *req_key_str, time_t now, int set)
void hs_clean_last_hid_serv_requests (time_t now)
void hs_purge_hid_serv_from_last_hid_serv_requests (const char *req_key_str)
void hs_purge_last_hid_serv_requests (void)
routerstatus_ths_pick_hsdir (smartlist_t *responsible_dirs, const char *req_key_str, bool *is_rate_limited_out)
extend_info_ths_get_extend_info_from_lspecs (const smartlist_t *lspecs, const curve25519_public_key_t *onion_key, int direct_conn)
void hs_init (void)
void hs_free_all (void)
void hs_dec_rdv_stream_counter (origin_circuit_t *circ)
void hs_inc_rdv_stream_counter (origin_circuit_t *circ)
link_specifier_t * link_specifier_dup (const link_specifier_t *src)


static const char * str_ed25519_basepoint
static uint8_t cached_disaster_srv [2][DIGEST256_LEN]
static uint64_t cached_time_period_nums [2] = {0}
static strmap_t * last_hid_serv_requests_ = NULL

Detailed Description

Contains code shared between different HS protocol version as well as useful data structures and accessors used by other subsystems. The rendcommon.c should only contains code relating to the v2 protocol.

Definition in file hs_common.c.

Function Documentation

◆ get_disaster_srv()

STATIC void get_disaster_srv ( uint64_t  time_period_num,
uint8_t *  srv_out 

Compute the disaster SRV value for this time_period_num and put it in srv_out (of size at least DIGEST256_LEN). First check our caches to see if we have already computed it.

Definition at line 630 of file hs_common.c.

◆ get_last_hid_serv_requests()

STATIC strmap_t* get_last_hid_serv_requests ( void  )

Returns last_hid_serv_requests_, initializing it to a new strmap if necessary.

Definition at line 1455 of file hs_common.c.

References last_hid_serv_requests_.

Referenced by hs_clean_last_hid_serv_requests(), hs_lookup_last_hid_serv_request(), and hs_purge_hid_serv_from_last_hid_serv_requests().

◆ get_time_period_length()

STATIC uint64_t get_time_period_length ( void  )

Get the default HS time period length in minutes from the consensus.

Definition at line 239 of file hs_common.c.

References sr_state_get_protocol_run_duration().

Referenced by hs_get_time_period_num().

◆ hs_clean_last_hid_serv_requests()

void hs_clean_last_hid_serv_requests ( time_t  now)

Clean the history of request times to hidden service directories, so that it does not contain requests older than REND_HID_SERV_DIR_REQUERY_PERIOD seconds any more.

Definition at line 1502 of file hs_common.c.

References get_last_hid_serv_requests(), hs_hsdir_requery_period(), and tor_free.

◆ hs_get_next_time_period_num()

uint64_t hs_get_next_time_period_num ( time_t  now)

Get the number of the upcoming HS time period, given that the current time is now. If now is not set, we try to get the time from a live consensus.

Definition at line 300 of file hs_common.c.

References hs_get_time_period_num().

◆ hs_get_service_max_rend_failures()

int hs_get_service_max_rend_failures ( void  )

How many times will a hidden service operator attempt to connect to a requested rendezvous point before giving up?

Definition at line 229 of file hs_common.c.

◆ hs_get_time_period_num()

uint64_t hs_get_time_period_num ( time_t  now)

Get the HS time period number at time now. If now is not set, we try to get the time ourselves from a live consensus.

Definition at line 265 of file hs_common.c.

References approx_time(), get_time_period_length(), sr_state_get_phase_duration(), tor_assert(), and networkstatus_t::valid_after.

Referenced by hs_get_next_time_period_num().

◆ hs_hsdir_requery_period()

time_t hs_hsdir_requery_period ( const or_options_t options)

Return the period for which a hidden service directory cannot be queried for the same descriptor ID again, taking TestingTorNetwork into account.

Definition at line 1425 of file hs_common.c.

References or_options_t::TestingTorNetwork, and tor_assert().

Referenced by hs_clean_last_hid_serv_requests().

◆ hs_lookup_last_hid_serv_request()

time_t hs_lookup_last_hid_serv_request ( routerstatus_t hs_dir,
const char *  req_key_str,
time_t  now,
int  set 

Look up the last request time to hidden service directory hs_dir for descriptor request key req_key_str which is the descriptor ID for a v2 service or the blinded key for v3. If set is non-zero, assign the current time now and return that. Otherwise, return the most recent request time, or 0 if no such request has been sent before.

Definition at line 1468 of file hs_common.c.

References BASE32_DIGEST_LEN, base32_encode(), DIGEST_LEN, get_last_hid_serv_requests(), routerstatus_t::identity_digest, and tor_asprintf().

◆ hs_pick_hsdir()

routerstatus_t* hs_pick_hsdir ( smartlist_t responsible_dirs,
const char *  req_key_str,
bool *  is_rate_limited_out 

Given the list of responsible HSDirs in responsible_dirs, pick the one that we should use to fetch a descriptor right now. Take into account previous failed attempts at fetching this descriptor from HSDirs using the string identifier req_key_str. We return whether we are rate limited into *is_rate_limited_out if it is not NULL.

Steals ownership of responsible_dirs.

Return the routerstatus of the chosen HSDir if successful, otherwise return NULL if no HSDirs are worth trying right now.

Definition at line 1600 of file hs_common.c.

◆ hs_purge_hid_serv_from_last_hid_serv_requests()

void hs_purge_hid_serv_from_last_hid_serv_requests ( const char *  req_key_str)

Remove all requests related to the descriptor request key string req_key_str from the history of times of requests to hidden service directories.

This is called from rend_client_note_connection_attempt_ended(), which must be idempotent, so any future changes to this function must leave it idempotent too.

Definition at line 1531 of file hs_common.c.

References get_last_hid_serv_requests(), REND_DESC_ID_V2_LEN_BASE32, tor_free, and tor_memeq().

Referenced by purge_v2_hidserv_req().

◆ hs_purge_last_hid_serv_requests()

void hs_purge_last_hid_serv_requests ( void  )

Purge the history of request times to hidden service directories, so that future lookups of an HS descriptor will not fail because we accessed all of the HSDir relays responsible for the descriptor recently.

Definition at line 1572 of file hs_common.c.

References last_hid_serv_requests_.

Referenced by rend_client_purge_state().

◆ node_has_hsdir_index()

static int node_has_hsdir_index ( const node_t node)

node is an HSDir so make sure that we have assigned an hsdir index. Return 0 if everything is as expected, else return -1.

Definition at line 1272 of file hs_common.c.

References node_has_preferred_descriptor(), node_supports_v3_hsdir(), and tor_assert().

◆ rend_data_free_()

void rend_data_free_ ( rend_data_t data)

Free all storage associated with data

Definition at line 357 of file hs_common.c.

References rend_data_t::hsdirs_fp, SMARTLIST_FOREACH, tor_assert(), and tor_free.

Variable Documentation

◆ cached_disaster_srv

uint8_t cached_disaster_srv[2][DIGEST256_LEN]

Due to the high cost of computing the disaster SRV and that potentially we would have to do it thousands of times in a row, we always cache the computer disaster SRV (and its corresponding time period num) in case we want to reuse it soon after. We need to cache two SRVs, one for each active time period.

Definition at line 623 of file hs_common.c.

◆ last_hid_serv_requests_

strmap_t* last_hid_serv_requests_ = NULL

Tracks requests for fetching hidden service descriptors. It's used by hidden service clients, to avoid querying HSDirs that have already failed giving back a descriptor. The same data structure is used to track both v2 and v3 HS descriptor requests.

The string map is a key/value store that contains the last request times to hidden service directories for certain queries. Specifically:

key = base32(hsdir_identity) + base32(hs_identity) value = time_t of last request for that hs_identity to that HSDir

where 'hsdir_identity' is the identity digest of the HSDir node, and 'hs_identity' is the descriptor ID of the HS in the v2 case, or the ed25519 blinded public key of the HS in the v3 case.

Definition at line 1450 of file hs_common.c.

Referenced by get_last_hid_serv_requests(), and hs_purge_last_hid_serv_requests().

◆ str_ed25519_basepoint

const char* str_ed25519_basepoint
Initial value:
"454012693041857206046113283949847762202, "

Definition at line 47 of file hs_common.c.