hs_common.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Copyright (c) 2016-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_common.h
  5. * \brief Header file containing common data for the whole HS subsytem.
  6. **/
  7. #ifndef TOR_HS_COMMON_H
  8. #define TOR_HS_COMMON_H
  9. #include "core/or/or.h"
  10. #include "lib/defs/x25519_sizes.h"
  11. struct curve25519_public_key_t;
  12. struct ed25519_public_key_t;
  13. struct ed25519_keypair_t;
  14. /* Trunnel */
  15. #include "trunnel/ed25519_cert.h"
  16. /* Protocol version 2. Use this instead of hardcoding "2" in the code base,
  17. * this adds a clearer semantic to the value when used. */
  18. #define HS_VERSION_TWO 2
  19. /* Version 3 of the protocol (prop224). */
  20. #define HS_VERSION_THREE 3
  21. /* Earliest and latest version we support. */
  22. #define HS_VERSION_MIN HS_VERSION_TWO
  23. #define HS_VERSION_MAX HS_VERSION_THREE
  24. /** Try to maintain this many intro points per service by default. */
  25. #define NUM_INTRO_POINTS_DEFAULT 3
  26. /** Maximum number of intro points per generic and version 2 service. */
  27. #define NUM_INTRO_POINTS_MAX 10
  28. /** Number of extra intro points we launch if our set of intro nodes is empty.
  29. * See proposal 155, section 4. */
  30. #define NUM_INTRO_POINTS_EXTRA 2
  31. /** If we can't build our intro circuits, don't retry for this long. */
  32. #define INTRO_CIRC_RETRY_PERIOD (60*5)
  33. /** Don't try to build more than this many circuits before giving up for a
  34. * while.*/
  35. #define MAX_INTRO_CIRCS_PER_PERIOD 10
  36. /** How many times will a hidden service operator attempt to connect to a
  37. * requested rendezvous point before giving up? */
  38. #define MAX_REND_FAILURES 1
  39. /** How many seconds should we spend trying to connect to a requested
  40. * rendezvous point before giving up? */
  41. #define MAX_REND_TIMEOUT 30
  42. /* String prefix for the signature of ESTABLISH_INTRO */
  43. #define ESTABLISH_INTRO_SIG_PREFIX "Tor establish-intro cell v1"
  44. /* The default HS time period length */
  45. #define HS_TIME_PERIOD_LENGTH_DEFAULT 1440 /* 1440 minutes == one day */
  46. /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
  47. #define HS_TIME_PERIOD_LENGTH_MIN 30 /* minutes */
  48. /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
  49. #define HS_TIME_PERIOD_LENGTH_MAX (60 * 24 * 10) /* 10 days or 14400 minutes */
  50. /* Prefix of the onion address checksum. */
  51. #define HS_SERVICE_ADDR_CHECKSUM_PREFIX ".onion checksum"
  52. /* Length of the checksum prefix minus the NUL terminated byte. */
  53. #define HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN \
  54. (sizeof(HS_SERVICE_ADDR_CHECKSUM_PREFIX) - 1)
  55. /* Length of the resulting checksum of the address. The construction of this
  56. * checksum looks like:
  57. * CHECKSUM = ".onion checksum" || PUBKEY || VERSION
  58. * where VERSION is 1 byte. This is pre-hashing. */
  59. #define HS_SERVICE_ADDR_CHECKSUM_INPUT_LEN \
  60. (HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN + ED25519_PUBKEY_LEN + sizeof(uint8_t))
  61. /* The amount of bytes we use from the address checksum. */
  62. #define HS_SERVICE_ADDR_CHECKSUM_LEN_USED 2
  63. /* Length of the binary encoded service address which is of course before the
  64. * base32 encoding. Construction is:
  65. * PUBKEY || CHECKSUM || VERSION
  66. * with 1 byte VERSION and 2 bytes CHECKSUM. The following is 35 bytes. */
  67. #define HS_SERVICE_ADDR_LEN \
  68. (ED25519_PUBKEY_LEN + HS_SERVICE_ADDR_CHECKSUM_LEN_USED + sizeof(uint8_t))
  69. /* Length of 'y' portion of 'y.onion' URL. This is base32 encoded and the
  70. * length ends up to 56 bytes (not counting the terminated NUL byte.) */
  71. #define HS_SERVICE_ADDR_LEN_BASE32 \
  72. (CEIL_DIV(HS_SERVICE_ADDR_LEN * 8, 5))
  73. /* The default HS time period length */
  74. #define HS_TIME_PERIOD_LENGTH_DEFAULT 1440 /* 1440 minutes == one day */
  75. /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
  76. #define HS_TIME_PERIOD_LENGTH_MIN 30 /* minutes */
  77. /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
  78. #define HS_TIME_PERIOD_LENGTH_MAX (60 * 24 * 10) /* 10 days or 14400 minutes */
  79. /* The time period rotation offset as seen in prop224 section [TIME-PERIODS] */
  80. #define HS_TIME_PERIOD_ROTATION_OFFSET (12 * 60) /* minutes */
  81. /* Keyblinding parameter construction is as follow:
  82. * "key-blind" || INT_8(period_num) || INT_8(start_period_sec) */
  83. #define HS_KEYBLIND_NONCE_PREFIX "key-blind"
  84. #define HS_KEYBLIND_NONCE_PREFIX_LEN (sizeof(HS_KEYBLIND_NONCE_PREFIX) - 1)
  85. #define HS_KEYBLIND_NONCE_LEN \
  86. (HS_KEYBLIND_NONCE_PREFIX_LEN + sizeof(uint64_t) + sizeof(uint64_t))
  87. /* Credential and subcredential prefix value. */
  88. #define HS_CREDENTIAL_PREFIX "credential"
  89. #define HS_CREDENTIAL_PREFIX_LEN (sizeof(HS_CREDENTIAL_PREFIX) - 1)
  90. #define HS_SUBCREDENTIAL_PREFIX "subcredential"
  91. #define HS_SUBCREDENTIAL_PREFIX_LEN (sizeof(HS_SUBCREDENTIAL_PREFIX) - 1)
  92. /* Node hidden service stored at index prefix value. */
  93. #define HS_INDEX_PREFIX "store-at-idx"
  94. #define HS_INDEX_PREFIX_LEN (sizeof(HS_INDEX_PREFIX) - 1)
  95. /* Node hidden service directory index prefix value. */
  96. #define HSDIR_INDEX_PREFIX "node-idx"
  97. #define HSDIR_INDEX_PREFIX_LEN (sizeof(HSDIR_INDEX_PREFIX) - 1)
  98. /* Prefix of the shared random value disaster mode. */
  99. #define HS_SRV_DISASTER_PREFIX "shared-random-disaster"
  100. #define HS_SRV_DISASTER_PREFIX_LEN (sizeof(HS_SRV_DISASTER_PREFIX) - 1)
  101. /* Default value of number of hsdir replicas (hsdir_n_replicas). */
  102. #define HS_DEFAULT_HSDIR_N_REPLICAS 2
  103. /* Default value of hsdir spread store (hsdir_spread_store). */
  104. #define HS_DEFAULT_HSDIR_SPREAD_STORE 4
  105. /* Default value of hsdir spread fetch (hsdir_spread_fetch). */
  106. #define HS_DEFAULT_HSDIR_SPREAD_FETCH 3
  107. /* The size of a legacy RENDEZVOUS1 cell which adds up to 168 bytes. It is
  108. * bigger than the 84 bytes needed for version 3 so we need to pad up to that
  109. * length so it is indistinguishable between versions. */
  110. #define HS_LEGACY_RENDEZVOUS_CELL_SIZE \
  111. (REND_COOKIE_LEN + DH1024_KEY_LEN + DIGEST_LEN)
  112. /* Type of authentication key used by an introduction point. */
  113. typedef enum {
  114. HS_AUTH_KEY_TYPE_LEGACY = 1,
  115. HS_AUTH_KEY_TYPE_ED25519 = 2,
  116. } hs_auth_key_type_t;
  117. /* Return value when adding an ephemeral service through the ADD_ONION
  118. * control port command. Both v2 and v3 share these. */
  119. typedef enum {
  120. RSAE_BADAUTH = -5, /**< Invalid auth_type/auth_clients */
  121. RSAE_BADVIRTPORT = -4, /**< Invalid VIRTPORT/TARGET(s) */
  122. RSAE_ADDREXISTS = -3, /**< Onion address collision */
  123. RSAE_BADPRIVKEY = -2, /**< Invalid public key */
  124. RSAE_INTERNAL = -1, /**< Internal error */
  125. RSAE_OKAY = 0 /**< Service added as expected */
  126. } hs_service_add_ephemeral_status_t;
  127. /* Represents the mapping from a virtual port of a rendezvous service to a
  128. * real port on some IP. */
  129. typedef struct rend_service_port_config_t {
  130. /* The incoming HS virtual port we're mapping */
  131. uint16_t virtual_port;
  132. /* Is this an AF_UNIX port? */
  133. unsigned int is_unix_addr:1;
  134. /* The outgoing TCP port to use, if !is_unix_addr */
  135. uint16_t real_port;
  136. /* The outgoing IPv4 or IPv6 address to use, if !is_unix_addr */
  137. tor_addr_t real_addr;
  138. /* The socket path to connect to, if is_unix_addr */
  139. char unix_addr[FLEXIBLE_ARRAY_MEMBER];
  140. } rend_service_port_config_t;
  141. void hs_init(void);
  142. void hs_free_all(void);
  143. void hs_cleanup_circ(circuit_t *circ);
  144. int hs_check_service_private_dir(const char *username, const char *path,
  145. unsigned int dir_group_readable,
  146. unsigned int create);
  147. int hs_get_service_max_rend_failures(void);
  148. char *hs_path_from_filename(const char *directory, const char *filename);
  149. void hs_build_address(const struct ed25519_public_key_t *key, uint8_t version,
  150. char *addr_out);
  151. int hs_address_is_valid(const char *address);
  152. int hs_parse_address(const char *address, struct ed25519_public_key_t *key_out,
  153. uint8_t *checksum_out, uint8_t *version_out);
  154. void hs_build_blinded_pubkey(const struct ed25519_public_key_t *pubkey,
  155. const uint8_t *secret, size_t secret_len,
  156. uint64_t time_period_num,
  157. struct ed25519_public_key_t *pubkey_out);
  158. void hs_build_blinded_keypair(const struct ed25519_keypair_t *kp,
  159. const uint8_t *secret, size_t secret_len,
  160. uint64_t time_period_num,
  161. struct ed25519_keypair_t *kp_out);
  162. int hs_service_requires_uptime_circ(const smartlist_t *ports);
  163. void rend_data_free_(rend_data_t *data);
  164. #define rend_data_free(data) \
  165. FREE_AND_NULL(rend_data_t, rend_data_free_, (data))
  166. rend_data_t *rend_data_dup(const rend_data_t *data);
  167. rend_data_t *rend_data_client_create(const char *onion_address,
  168. const char *desc_id,
  169. const char *cookie,
  170. rend_auth_type_t auth_type);
  171. rend_data_t *rend_data_service_create(const char *onion_address,
  172. const char *pk_digest,
  173. const uint8_t *cookie,
  174. rend_auth_type_t auth_type);
  175. const char *rend_data_get_address(const rend_data_t *rend_data);
  176. const char *rend_data_get_desc_id(const rend_data_t *rend_data,
  177. uint8_t replica, size_t *len_out);
  178. const uint8_t *rend_data_get_pk_digest(const rend_data_t *rend_data,
  179. size_t *len_out);
  180. routerstatus_t *pick_hsdir(const char *desc_id, const char *desc_id_base32);
  181. void hs_get_subcredential(const struct ed25519_public_key_t *identity_pk,
  182. const struct ed25519_public_key_t *blinded_pk,
  183. uint8_t *subcred_out);
  184. uint64_t hs_get_previous_time_period_num(time_t now);
  185. uint64_t hs_get_time_period_num(time_t now);
  186. uint64_t hs_get_next_time_period_num(time_t now);
  187. time_t hs_get_start_time_of_next_time_period(time_t now);
  188. link_specifier_t *hs_link_specifier_dup(const link_specifier_t *lspec);
  189. MOCK_DECL(int, hs_in_period_between_tp_and_srv,
  190. (const networkstatus_t *consensus, time_t now));
  191. uint8_t *hs_get_current_srv(uint64_t time_period_num,
  192. const networkstatus_t *ns);
  193. uint8_t *hs_get_previous_srv(uint64_t time_period_num,
  194. const networkstatus_t *ns);
  195. void hs_build_hsdir_index(const struct ed25519_public_key_t *identity_pk,
  196. const uint8_t *srv, uint64_t period_num,
  197. uint8_t *hsdir_index_out);
  198. void hs_build_hs_index(uint64_t replica,
  199. const struct ed25519_public_key_t *blinded_pk,
  200. uint64_t period_num, uint8_t *hs_index_out);
  201. int32_t hs_get_hsdir_n_replicas(void);
  202. int32_t hs_get_hsdir_spread_fetch(void);
  203. int32_t hs_get_hsdir_spread_store(void);
  204. void hs_get_responsible_hsdirs(const struct ed25519_public_key_t *blinded_pk,
  205. uint64_t time_period_num,
  206. int use_second_hsdir_index,
  207. int for_fetching, smartlist_t *responsible_dirs);
  208. routerstatus_t *hs_pick_hsdir(smartlist_t *responsible_dirs,
  209. const char *req_key_str);
  210. time_t hs_hsdir_requery_period(const or_options_t *options);
  211. time_t hs_lookup_last_hid_serv_request(routerstatus_t *hs_dir,
  212. const char *desc_id_base32,
  213. time_t now, int set);
  214. void hs_clean_last_hid_serv_requests(time_t now);
  215. void hs_purge_hid_serv_from_last_hid_serv_requests(const char *desc_id);
  216. void hs_purge_last_hid_serv_requests(void);
  217. int hs_set_conn_addr_port(const smartlist_t *ports, edge_connection_t *conn);
  218. void hs_inc_rdv_stream_counter(origin_circuit_t *circ);
  219. void hs_dec_rdv_stream_counter(origin_circuit_t *circ);
  220. extend_info_t *hs_get_extend_info_from_lspecs(const smartlist_t *lspecs,
  221. const struct curve25519_public_key_t *onion_key,
  222. int direct_conn);
  223. #ifdef HS_COMMON_PRIVATE
  224. STATIC void get_disaster_srv(uint64_t time_period_num, uint8_t *srv_out);
  225. /** The period for which a hidden service directory cannot be queried for
  226. * the same descriptor ID again. */
  227. #define REND_HID_SERV_DIR_REQUERY_PERIOD (15 * 60)
  228. /** Test networks generate a new consensus every 5 or 10 seconds.
  229. * So allow them to requery HSDirs much faster. */
  230. #define REND_HID_SERV_DIR_REQUERY_PERIOD_TESTING (5)
  231. #ifdef TOR_UNIT_TESTS
  232. STATIC strmap_t *get_last_hid_serv_requests(void);
  233. STATIC uint64_t get_time_period_length(void);
  234. STATIC uint8_t *get_first_cached_disaster_srv(void);
  235. STATIC uint8_t *get_second_cached_disaster_srv(void);
  236. #endif /* defined(TOR_UNIT_TESTS) */
  237. #endif /* defined(HS_COMMON_PRIVATE) */
  238. #endif /* !defined(TOR_HS_COMMON_H) */