hs_service.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* Copyright (c) 2016-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_service.h
  5. * \brief Header file containing service data for the HS subsytem.
  6. **/
  7. #ifndef TOR_HS_SERVICE_H
  8. #define TOR_HS_SERVICE_H
  9. #include "lib/crypt_ops/crypto_curve25519.h"
  10. #include "lib/crypt_ops/crypto_ed25519.h"
  11. #include "feature/hs_common/replaycache.h"
  12. #include "feature/hs/hs_common.h"
  13. #include "feature/hs/hs_descriptor.h"
  14. #include "feature/hs/hs_ident.h"
  15. #include "feature/hs/hs_intropoint.h"
  16. /* Trunnel */
  17. #include "trunnel/hs/cell_establish_intro.h"
  18. /* When loading and configuring a service, this is the default version it will
  19. * be configured for as it is possible that no HiddenServiceVersion is
  20. * present. */
  21. #define HS_SERVICE_DEFAULT_VERSION HS_VERSION_THREE
  22. /* As described in the specification, service publishes their next descriptor
  23. * at a random time between those two values (in seconds). */
  24. #define HS_SERVICE_NEXT_UPLOAD_TIME_MIN (60 * 60)
  25. #define HS_SERVICE_NEXT_UPLOAD_TIME_MAX (120 * 60)
  26. /* Service side introduction point. */
  27. typedef struct hs_service_intro_point_t {
  28. /* Top level intropoint "shared" data between client/service. */
  29. hs_intropoint_t base;
  30. /* Onion key of the introduction point used to extend to it for the ntor
  31. * handshake. */
  32. curve25519_public_key_t onion_key;
  33. /* Authentication keypair used to create the authentication certificate
  34. * which is published in the descriptor. */
  35. ed25519_keypair_t auth_key_kp;
  36. /* Encryption keypair for the "ntor" type. */
  37. curve25519_keypair_t enc_key_kp;
  38. /* Legacy key if that intro point doesn't support v3. This should be used if
  39. * the base object legacy flag is set. */
  40. crypto_pk_t *legacy_key;
  41. /* Legacy key SHA1 public key digest. This should be used only if the base
  42. * object legacy flag is set. */
  43. uint8_t legacy_key_digest[DIGEST_LEN];
  44. /* Amount of INTRODUCE2 cell accepted from this intro point. */
  45. uint64_t introduce2_count;
  46. /* Maximum number of INTRODUCE2 cell this intro point should accept. */
  47. uint64_t introduce2_max;
  48. /* The time at which this intro point should expire and stop being used. */
  49. time_t time_to_expire;
  50. /* The amount of circuit creation we've made to this intro point. This is
  51. * incremented every time we do a circuit relaunch on this intro point which
  52. * is triggered when the circuit dies but the node is still in the
  53. * consensus. After MAX_INTRO_POINT_CIRCUIT_RETRIES, we give up on it. */
  54. uint32_t circuit_retries;
  55. /* Set if this intro point has an established circuit. */
  56. unsigned int circuit_established : 1;
  57. /* Replay cache recording the encrypted part of an INTRODUCE2 cell that the
  58. * circuit associated with this intro point has received. This is used to
  59. * prevent replay attacks. */
  60. replaycache_t *replay_cache;
  61. /* Support the INTRO2 DoS defense. If set, the DoS extension described by
  62. * proposal 305 is sent. */
  63. unsigned int support_intro2_dos_defense : 1;
  64. } hs_service_intro_point_t;
  65. /* Object handling introduction points of a service. */
  66. typedef struct hs_service_intropoints_t {
  67. /* The time at which we've started our retry period to build circuits. We
  68. * don't want to stress circuit creation so we can only retry for a certain
  69. * time and then after we stop and wait. */
  70. time_t retry_period_started;
  71. /* Number of circuit we've launched during a single retry period. */
  72. unsigned int num_circuits_launched;
  73. /* Contains the current hs_service_intro_point_t objects indexed by
  74. * authentication public key. */
  75. digest256map_t *map;
  76. /* Contains node's identity key digest that were introduction point for this
  77. * descriptor but were retried to many times. We keep those so we avoid
  78. * re-picking them over and over for a circuit retry period.
  79. * XXX: Once we have #22173, change this to only use ed25519 identity. */
  80. digestmap_t *failed_id;
  81. } hs_service_intropoints_t;
  82. /* Representation of a service descriptor.
  83. *
  84. * Some elements of the descriptor are mutable whereas others are immutable:
  85. * Immutable elements are initialized once when the descriptor is built (when
  86. * service descriptors gets rotated). This means that these elements are
  87. * initialized once and then they don't change for the lifetime of the
  88. * descriptor. See build_service_descriptor().
  89. *
  90. * Mutable elements are initialized when we build the descriptor but they are
  91. * also altered during the lifetime of the descriptor. They could be
  92. * _refreshed_ everytime we upload the descriptor (which happens multiple times
  93. * over the lifetime of the descriptor), or through periodic events. We do this
  94. * for elements like the descriptor revision counter and various
  95. * certificates. See refresh_service_descriptor() and
  96. * update_service_descriptor_intro_points().
  97. */
  98. typedef struct hs_service_descriptor_t {
  99. /* Immutable: Client authorization ephemeral keypair. */
  100. curve25519_keypair_t auth_ephemeral_kp;
  101. /* Immutable: Descriptor cookie used to encrypt the descriptor, when the
  102. * client authorization is enabled */
  103. uint8_t descriptor_cookie[HS_DESC_DESCRIPTOR_COOKIE_LEN];
  104. /* Immutable: Descriptor signing keypair. */
  105. ed25519_keypair_t signing_kp;
  106. /* Immutable: Blinded keypair derived from the master identity public key. */
  107. ed25519_keypair_t blinded_kp;
  108. /* Immutable: The time period number this descriptor has been created for. */
  109. uint64_t time_period_num;
  110. /** Immutable: The OPE cipher for encrypting revision counters for this
  111. * descriptor. Tied to the descriptor blinded key. */
  112. struct crypto_ope_t *ope_cipher;
  113. /* Mutable: Decoded descriptor. This object is used for encoding when the
  114. * service publishes the descriptor. */
  115. hs_descriptor_t *desc;
  116. /* Mutable: When is the next time when we should upload the descriptor. */
  117. time_t next_upload_time;
  118. /* Mutable: Introduction points assign to this descriptor which contains
  119. * hs_service_intropoints_t object indexed by authentication key (the RSA key
  120. * if the node is legacy). */
  121. hs_service_intropoints_t intro_points;
  122. /* Mutable: True iff we have missing intro points for this descriptor because
  123. * we couldn't pick any nodes. */
  124. unsigned int missing_intro_points : 1;
  125. /** Mutable: List of the responsible HSDirs (their b64ed identity digest)
  126. * last time we uploaded this descriptor. If the set of responsible HSDirs
  127. * is different from this list, this means we received new dirinfo and we
  128. * need to reupload our descriptor. */
  129. smartlist_t *previous_hsdirs;
  130. } hs_service_descriptor_t;
  131. /* Service key material. */
  132. typedef struct hs_service_keys_t {
  133. /* Master identify public key. */
  134. ed25519_public_key_t identity_pk;
  135. /* Master identity private key. */
  136. ed25519_secret_key_t identity_sk;
  137. /* True iff the key is kept offline which means the identity_sk MUST not be
  138. * used in that case. */
  139. unsigned int is_identify_key_offline : 1;
  140. } hs_service_keys_t;
  141. /** Service side configuration of client authorization. */
  142. typedef struct hs_service_authorized_client_t {
  143. /* The client auth public key used to encrypt the descriptor cookie. */
  144. curve25519_public_key_t client_pk;
  145. } hs_service_authorized_client_t;
  146. /** Which protocol to use for exporting HS client circuit ID. */
  147. typedef enum {
  148. /** Don't expose the circuit id. */
  149. HS_CIRCUIT_ID_PROTOCOL_NONE,
  150. /** Use the HAProxy proxy protocol. */
  151. HS_CIRCUIT_ID_PROTOCOL_HAPROXY
  152. } hs_circuit_id_protocol_t;
  153. /* Service configuration. The following are set from the torrc options either
  154. * set by the configuration file or by the control port. Nothing else should
  155. * change those values. */
  156. typedef struct hs_service_config_t {
  157. /* Protocol version of the service. Specified by HiddenServiceVersion
  158. * option. */
  159. uint32_t version;
  160. /* Have we explicitly set HiddenServiceVersion? */
  161. unsigned int hs_version_explicitly_set : 1;
  162. /* List of rend_service_port_config_t */
  163. smartlist_t *ports;
  164. /* Path on the filesystem where the service persistent data is stored. NULL
  165. * if the service is ephemeral. Specified by HiddenServiceDir option. */
  166. char *directory_path;
  167. /* The maximum number of simultaneous streams per rendezvous circuit that
  168. * are allowed to be created. No limit if 0. Specified by
  169. * HiddenServiceMaxStreams option. */
  170. uint64_t max_streams_per_rdv_circuit;
  171. /* If true, we close circuits that exceed the max_streams_per_rdv_circuit
  172. * limit. Specified by HiddenServiceMaxStreamsCloseCircuit option. */
  173. unsigned int max_streams_close_circuit : 1;
  174. /* How many introduction points this service has. Specified by
  175. * HiddenServiceNumIntroductionPoints option. */
  176. unsigned int num_intro_points;
  177. /* True iff the client auth is enabled. */
  178. unsigned int is_client_auth_enabled : 1;
  179. /* List of hs_service_authorized_client_t's of clients that may access this
  180. * service. Specified by HiddenServiceAuthorizeClient option. */
  181. smartlist_t *clients;
  182. /* True iff we allow request made on unknown ports. Specified by
  183. * HiddenServiceAllowUnknownPorts option. */
  184. unsigned int allow_unknown_ports : 1;
  185. /* If true, this service is a Single Onion Service. Specified by
  186. * HiddenServiceSingleHopMode and HiddenServiceNonAnonymousMode options. */
  187. unsigned int is_single_onion : 1;
  188. /* If true, allow group read permissions on the directory_path. Specified by
  189. * HiddenServiceDirGroupReadable option. */
  190. unsigned int dir_group_readable : 1;
  191. /* Is this service ephemeral? */
  192. unsigned int is_ephemeral : 1;
  193. /* Does this service export the circuit ID of its clients? */
  194. hs_circuit_id_protocol_t circuit_id_protocol;
  195. /* DoS defenses. For the ESTABLISH_INTRO cell extension. */
  196. unsigned int has_dos_defense_enabled : 1;
  197. uint32_t intro_dos_rate_per_sec;
  198. uint32_t intro_dos_burst_per_sec;
  199. } hs_service_config_t;
  200. /* Service state. */
  201. typedef struct hs_service_state_t {
  202. /* The time at which we've started our retry period to build circuits. We
  203. * don't want to stress circuit creation so we can only retry for a certain
  204. * time and then after we stop and wait. */
  205. time_t intro_circ_retry_started_time;
  206. /* Number of circuit we've launched during a single retry period. This
  207. * should never go over MAX_INTRO_CIRCS_PER_PERIOD. */
  208. unsigned int num_intro_circ_launched;
  209. /* Replay cache tracking the REND_COOKIE found in INTRODUCE2 cell to detect
  210. * repeats. Clients may send INTRODUCE1 cells for the same rendezvous point
  211. * through two or more different introduction points; when they do, this
  212. * keeps us from launching multiple simultaneous attempts to connect to the
  213. * same rend point. */
  214. replaycache_t *replay_cache_rend_cookie;
  215. /* When is the next time we should rotate our descriptors. This is has to be
  216. * done at the start time of the next SRV protocol run. */
  217. time_t next_rotation_time;
  218. } hs_service_state_t;
  219. /* Representation of a service running on this tor instance. */
  220. typedef struct hs_service_t {
  221. /* Onion address base32 encoded and NUL terminated. We keep it for logging
  222. * purposes so we don't have to build it everytime. */
  223. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  224. /* Hashtable node: use to look up the service by its master public identity
  225. * key in the service global map. */
  226. HT_ENTRY(hs_service_t) hs_service_node;
  227. /* Service state which contains various flags and counters. */
  228. hs_service_state_t state;
  229. /* Key material of the service. */
  230. hs_service_keys_t keys;
  231. /* Configuration of the service. */
  232. hs_service_config_t config;
  233. /* Current descriptor. */
  234. hs_service_descriptor_t *desc_current;
  235. /* Next descriptor. */
  236. hs_service_descriptor_t *desc_next;
  237. /* XXX: Credential (client auth.) #20700. */
  238. } hs_service_t;
  239. /* For the service global hash map, we define a specific type for it which
  240. * will make it safe to use and specific to some controlled parameters such as
  241. * the hashing function and how to compare services. */
  242. typedef HT_HEAD(hs_service_ht, hs_service_t) hs_service_ht;
  243. /* API */
  244. /* Global initializer and cleanup function. */
  245. void hs_service_init(void);
  246. void hs_service_free_all(void);
  247. /* Service new/free functions. */
  248. hs_service_t *hs_service_new(const or_options_t *options);
  249. void hs_service_free_(hs_service_t *service);
  250. #define hs_service_free(s) FREE_AND_NULL(hs_service_t, hs_service_free_, (s))
  251. MOCK_DECL(unsigned int, hs_service_get_num_services,(void));
  252. void hs_service_stage_services(const smartlist_t *service_list);
  253. int hs_service_load_all_keys(void);
  254. int hs_service_get_version_from_key(const hs_service_t *service);
  255. void hs_service_lists_fnames_for_sandbox(smartlist_t *file_list,
  256. smartlist_t *dir_list);
  257. int hs_service_set_conn_addr_port(const origin_circuit_t *circ,
  258. edge_connection_t *conn);
  259. void hs_service_map_has_changed(void);
  260. void hs_service_dir_info_changed(void);
  261. void hs_service_run_scheduled_events(time_t now);
  262. void hs_service_circuit_has_opened(origin_circuit_t *circ);
  263. int hs_service_receive_intro_established(origin_circuit_t *circ,
  264. const uint8_t *payload,
  265. size_t payload_len);
  266. int hs_service_receive_introduce2(origin_circuit_t *circ,
  267. const uint8_t *payload,
  268. size_t payload_len);
  269. void hs_service_intro_circ_has_closed(origin_circuit_t *circ);
  270. char *hs_service_lookup_current_desc(const ed25519_public_key_t *pk);
  271. hs_service_add_ephemeral_status_t
  272. hs_service_add_ephemeral(ed25519_secret_key_t *sk, smartlist_t *ports,
  273. int max_streams_per_rdv_circuit,
  274. int max_streams_close_circuit, char **address_out);
  275. int hs_service_del_ephemeral(const char *address);
  276. /* Used outside of the HS subsystem by the control port command HSPOST. */
  277. void hs_service_upload_desc_to_dir(const char *encoded_desc,
  278. const uint8_t version,
  279. const ed25519_public_key_t *identity_pk,
  280. const ed25519_public_key_t *blinded_pk,
  281. const routerstatus_t *hsdir_rs);
  282. hs_circuit_id_protocol_t
  283. hs_service_exports_circuit_id(const ed25519_public_key_t *pk);
  284. #ifdef HS_SERVICE_PRIVATE
  285. #ifdef TOR_UNIT_TESTS
  286. /* Useful getters for unit tests. */
  287. STATIC unsigned int get_hs_service_map_size(void);
  288. STATIC int get_hs_service_staging_list_size(void);
  289. STATIC hs_service_ht *get_hs_service_map(void);
  290. STATIC hs_service_t *get_first_service(void);
  291. STATIC hs_service_intro_point_t *service_intro_point_find_by_ident(
  292. const hs_service_t *service,
  293. const hs_ident_circuit_t *ident);
  294. #endif /* defined(TOR_UNIT_TESTS) */
  295. /* Service accessors. */
  296. STATIC hs_service_t *find_service(hs_service_ht *map,
  297. const ed25519_public_key_t *pk);
  298. STATIC void remove_service(hs_service_ht *map, hs_service_t *service);
  299. STATIC int register_service(hs_service_ht *map, hs_service_t *service);
  300. /* Service introduction point functions. */
  301. STATIC hs_service_intro_point_t *service_intro_point_new(const node_t *node);
  302. STATIC void service_intro_point_free_(hs_service_intro_point_t *ip);
  303. #define service_intro_point_free(ip) \
  304. FREE_AND_NULL(hs_service_intro_point_t, \
  305. service_intro_point_free_, (ip))
  306. STATIC void service_intro_point_add(digest256map_t *map,
  307. hs_service_intro_point_t *ip);
  308. STATIC void service_intro_point_remove(const hs_service_t *service,
  309. const hs_service_intro_point_t *ip);
  310. STATIC hs_service_intro_point_t *service_intro_point_find(
  311. const hs_service_t *service,
  312. const ed25519_public_key_t *auth_key);
  313. /* Service descriptor functions. */
  314. STATIC hs_service_descriptor_t *service_descriptor_new(void);
  315. STATIC hs_service_descriptor_t *service_desc_find_by_intro(
  316. const hs_service_t *service,
  317. const hs_service_intro_point_t *ip);
  318. /* Helper functions. */
  319. STATIC int client_filename_is_valid(const char *filename);
  320. STATIC hs_service_authorized_client_t *
  321. parse_authorized_client(const char *client_key_str);
  322. STATIC void get_objects_from_ident(const hs_ident_circuit_t *ident,
  323. hs_service_t **service,
  324. hs_service_intro_point_t **ip,
  325. hs_service_descriptor_t **desc);
  326. STATIC const node_t *
  327. get_node_from_intro_point(const hs_service_intro_point_t *ip);
  328. STATIC int can_service_launch_intro_circuit(hs_service_t *service,
  329. time_t now);
  330. STATIC int intro_point_should_expire(const hs_service_intro_point_t *ip,
  331. time_t now);
  332. STATIC void run_housekeeping_event(time_t now);
  333. STATIC void rotate_all_descriptors(time_t now);
  334. STATIC void build_all_descriptors(time_t now);
  335. STATIC void update_all_descriptors_intro_points(time_t now);
  336. STATIC void run_upload_descriptor_event(time_t now);
  337. STATIC void service_descriptor_free_(hs_service_descriptor_t *desc);
  338. #define service_descriptor_free(d) \
  339. FREE_AND_NULL(hs_service_descriptor_t, \
  340. service_descriptor_free_, (d))
  341. STATIC void
  342. service_authorized_client_free_(hs_service_authorized_client_t *client);
  343. #define service_authorized_client_free(c) \
  344. FREE_AND_NULL(hs_service_authorized_client_t, \
  345. service_authorized_client_free_, (c))
  346. STATIC int
  347. write_address_to_file(const hs_service_t *service, const char *fname_);
  348. STATIC void upload_descriptor_to_all(const hs_service_t *service,
  349. hs_service_descriptor_t *desc);
  350. STATIC void service_desc_schedule_upload(hs_service_descriptor_t *desc,
  351. time_t now,
  352. int descriptor_changed);
  353. STATIC int service_desc_hsdirs_changed(const hs_service_t *service,
  354. const hs_service_descriptor_t *desc);
  355. STATIC int service_authorized_client_config_equal(
  356. const hs_service_config_t *config1,
  357. const hs_service_config_t *config2);
  358. STATIC void service_clear_config(hs_service_config_t *config);
  359. #endif /* defined(HS_SERVICE_PRIVATE) */
  360. #endif /* !defined(TOR_HS_SERVICE_H) */