#ifndef TOR_HS_COMMON_H
#define TOR_HS_COMMON_H
#include "core/or/or.h"
#include "lib/defs/x25519_sizes.h"
struct curve25519_public_key_t;
struct ed25519_public_key_t;
struct ed25519_keypair_t;
#include "trunnel/ed25519_cert.h"
#define HS_VERSION_THREE 3
#define HS_VERSION_MIN HS_VERSION_THREE
#define HS_VERSION_MAX HS_VERSION_THREE
#define NUM_INTRO_POINTS_DEFAULT 3
#define NUM_INTRO_POINTS_MAX 10
#define NUM_INTRO_POINTS_EXTRA 2
#define INTRO_CIRC_RETRY_PERIOD (60*5)
#define MAX_INTRO_CIRCS_PER_PERIOD 10
#define MAX_REND_FAILURES 1
#define MAX_REND_TIMEOUT 30
#define ESTABLISH_INTRO_SIG_PREFIX "Tor establish-intro cell v1"
#define HS_TIME_PERIOD_LENGTH_DEFAULT 1440
#define HS_TIME_PERIOD_LENGTH_MIN 30
#define HS_TIME_PERIOD_LENGTH_MAX (60 * 24 * 10)
#define HS_SERVICE_ADDR_CHECKSUM_PREFIX ".onion checksum"
#define HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN \
(sizeof(HS_SERVICE_ADDR_CHECKSUM_PREFIX) - 1)
#define HS_SERVICE_ADDR_CHECKSUM_INPUT_LEN \
(HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN + ED25519_PUBKEY_LEN + sizeof(uint8_t))
#define HS_SERVICE_ADDR_CHECKSUM_LEN_USED 2
#define HS_SERVICE_ADDR_LEN \
(ED25519_PUBKEY_LEN + HS_SERVICE_ADDR_CHECKSUM_LEN_USED + sizeof(uint8_t))
#define HS_SERVICE_ADDR_LEN_BASE32 \
(CEIL_DIV(HS_SERVICE_ADDR_LEN * 8, 5))
#define HS_TIME_PERIOD_LENGTH_DEFAULT 1440
#define HS_TIME_PERIOD_LENGTH_MIN 30
#define HS_TIME_PERIOD_LENGTH_MAX (60 * 24 * 10)
#define HS_TIME_PERIOD_ROTATION_OFFSET (12 * 60)
#define HS_KEYBLIND_NONCE_PREFIX "key-blind"
#define HS_KEYBLIND_NONCE_PREFIX_LEN (sizeof(HS_KEYBLIND_NONCE_PREFIX) - 1)
#define HS_KEYBLIND_NONCE_LEN \
(HS_KEYBLIND_NONCE_PREFIX_LEN + sizeof(uint64_t) + sizeof(uint64_t))
#define HS_CREDENTIAL_PREFIX "credential"
#define HS_CREDENTIAL_PREFIX_LEN (sizeof(HS_CREDENTIAL_PREFIX) - 1)
#define HS_SUBCREDENTIAL_PREFIX "subcredential"
#define HS_SUBCREDENTIAL_PREFIX_LEN (sizeof(HS_SUBCREDENTIAL_PREFIX) - 1)
#define HS_INDEX_PREFIX "store-at-idx"
#define HS_INDEX_PREFIX_LEN (sizeof(HS_INDEX_PREFIX) - 1)
#define HSDIR_INDEX_PREFIX "node-idx"
#define HSDIR_INDEX_PREFIX_LEN (sizeof(HSDIR_INDEX_PREFIX) - 1)
#define HS_SRV_DISASTER_PREFIX "shared-random-disaster"
#define HS_SRV_DISASTER_PREFIX_LEN (sizeof(HS_SRV_DISASTER_PREFIX) - 1)
#define HS_DEFAULT_HSDIR_N_REPLICAS 2
#define HS_DEFAULT_HSDIR_SPREAD_STORE 4
#define HS_DEFAULT_HSDIR_SPREAD_FETCH 3
#define HS_LEGACY_RENDEZVOUS_CELL_SIZE \
(REND_COOKIE_LEN + DH1024_KEY_LEN + DIGEST_LEN)
typedef enum {
HS_AUTH_KEY_TYPE_LEGACY = 1,
HS_AUTH_KEY_TYPE_ED25519 = 2,
} hs_auth_key_type_t;
typedef enum {
RSAE_BADAUTH = -5,
RSAE_BADVIRTPORT = -4,
RSAE_ADDREXISTS = -3,
RSAE_BADPRIVKEY = -2,
RSAE_INTERNAL = -1,
RSAE_OKAY = 0
} hs_service_add_ephemeral_status_t;
typedef struct hs_port_config_t {
uint16_t virtual_port;
unsigned int is_unix_addr:1;
uint16_t real_port;
tor_addr_t real_addr;
char unix_addr[FLEXIBLE_ARRAY_MEMBER];
} hs_port_config_t;
void hs_init(void);
void hs_free_all(void);
void hs_cleanup_circ(circuit_t *circ);
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);
char *hs_path_from_filename(const char *directory, const char *filename);
void hs_build_address(const struct ed25519_public_key_t *key, uint8_t version,
char *addr_out);
int hs_address_is_valid(const char *address);
int hs_parse_address(const char *address, struct ed25519_public_key_t *key_out,
uint8_t *checksum_out, uint8_t *version_out);
int hs_parse_address_no_log(const char *address,
struct ed25519_public_key_t *key_out,
uint8_t *checksum_out, uint8_t *version_out,
const char **errmsg);
void hs_build_blinded_pubkey(const struct ed25519_public_key_t *pubkey,
const uint8_t *secret, size_t secret_len,
uint64_t time_period_num,
struct ed25519_public_key_t *pubkey_out);
void hs_build_blinded_keypair(const struct ed25519_keypair_t *kp,
const uint8_t *secret, size_t secret_len,
uint64_t time_period_num,
struct ed25519_keypair_t *kp_out);
int hs_service_requires_uptime_circ(const smartlist_t *ports);
routerstatus_t *pick_hsdir(const char *desc_id, const char *desc_id_base32);
struct hs_subcredential_t;
void hs_get_subcredential(const struct ed25519_public_key_t *identity_pk,
const struct ed25519_public_key_t *blinded_pk,
struct hs_subcredential_t *subcred_out);
uint64_t hs_get_previous_time_period_num(time_t now);
uint64_t hs_get_time_period_num(time_t now);
uint64_t hs_get_next_time_period_num(time_t now);
time_t hs_get_start_time_of_next_time_period(time_t now);
MOCK_DECL(int, hs_in_period_between_tp_and_srv,
(const networkstatus_t *consensus, time_t now));
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);
void hs_build_hsdir_index(const struct ed25519_public_key_t *identity_pk,
const uint8_t *srv, uint64_t period_num,
uint8_t *hsdir_index_out);
void hs_build_hs_index(uint64_t replica,
const struct ed25519_public_key_t *blinded_pk,
uint64_t period_num, uint8_t *hs_index_out);
int32_t hs_get_hsdir_n_replicas(void);
int32_t hs_get_hsdir_spread_fetch(void);
int32_t hs_get_hsdir_spread_store(void);
void hs_get_responsible_hsdirs(const struct ed25519_public_key_t *blinded_pk,
uint64_t time_period_num,
int use_second_hsdir_index,
int for_fetching, smartlist_t *responsible_dirs);
routerstatus_t *hs_pick_hsdir(smartlist_t *responsible_dirs,
const char *req_key_str,
bool *is_rate_limited_out);
time_t hs_hsdir_requery_period(const or_options_t *options);
time_t hs_lookup_last_hid_serv_request(routerstatus_t *hs_dir,
const char *desc_id_base32,
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 *desc_id);
void hs_purge_last_hid_serv_requests(void);
int hs_set_conn_addr_port(const smartlist_t *ports, edge_connection_t *conn);
hs_port_config_t *hs_parse_port_config(const char *string, const char *sep,
char **err_msg_out);
void hs_port_config_free_(hs_port_config_t *p);
#define hs_port_config_free(p) \
FREE_AND_NULL(hs_port_config_t, hs_port_config_free_, (p))
void hs_inc_rdv_stream_counter(origin_circuit_t *circ);
void hs_dec_rdv_stream_counter(origin_circuit_t *circ);
extend_info_t *hs_get_extend_info_from_lspecs(const smartlist_t *lspecs,
const struct curve25519_public_key_t *onion_key,
int direct_conn);
link_specifier_t *link_specifier_dup(const link_specifier_t *src);
#ifdef HS_COMMON_PRIVATE
STATIC void get_disaster_srv(uint64_t time_period_num, uint8_t *srv_out);
#define REND_HID_SERV_DIR_REQUERY_PERIOD (15 * 60)
#define REND_HID_SERV_DIR_REQUERY_PERIOD_TESTING (5)
#ifdef TOR_UNIT_TESTS
STATIC strmap_t *get_last_hid_serv_requests(void);
STATIC uint64_t get_time_period_length(void);
STATIC uint8_t *get_first_cached_disaster_srv(void);
STATIC uint8_t *get_second_cached_disaster_srv(void);
#endif
#endif
#endif