hs_service.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* Copyright (c) 2016-2017, 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 "crypto_curve25519.h"
  10. #include "crypto_ed25519.h"
  11. #include "replaycache.h"
  12. #include "hs_common.h"
  13. #include "hs_descriptor.h"
  14. #include "hs_ident.h"
  15. #include "hs_intropoint.h"
  16. /* Trunnel */
  17. #include "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_TWO
  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. /* Amount of INTRODUCE2 cell accepted from this intro point. */
  42. uint64_t introduce2_count;
  43. /* Maximum number of INTRODUCE2 cell this intro point should accept. */
  44. uint64_t introduce2_max;
  45. /* The time at which this intro point should expire and stop being used. */
  46. time_t time_to_expire;
  47. /* The amount of circuit creation we've made to this intro point. This is
  48. * incremented every time we do a circuit relaunch on this intro point which
  49. * is triggered when the circuit dies but the node is still in the
  50. * consensus. After MAX_INTRO_POINT_CIRCUIT_RETRIES, we give up on it. */
  51. uint32_t circuit_retries;
  52. /* Set if this intro point has an established circuit. */
  53. unsigned int circuit_established : 1;
  54. /* Replay cache recording the encrypted part of an INTRODUCE2 cell that the
  55. * circuit associated with this intro point has received. This is used to
  56. * prevent replay attacks. */
  57. replaycache_t *replay_cache;
  58. } hs_service_intro_point_t;
  59. /* Object handling introduction points of a service. */
  60. typedef struct hs_service_intropoints_t {
  61. /* The time at which we've started our retry period to build circuits. We
  62. * don't want to stress circuit creation so we can only retry for a certain
  63. * time and then after we stop and wait. */
  64. time_t retry_period_started;
  65. /* Number of circuit we've launched during a single retry period. */
  66. unsigned int num_circuits_launched;
  67. /* Contains the current hs_service_intro_point_t objects indexed by
  68. * authentication public key. */
  69. digest256map_t *map;
  70. /* Contains node's identity key digest that were introduction point for this
  71. * descriptor but were retried to many times. We keep those so we avoid
  72. * re-picking them over and over for a circuit retry period.
  73. * XXX: Once we have #22173, change this to only use ed25519 identity. */
  74. digestmap_t *failed_id;
  75. } hs_service_intropoints_t;
  76. /* Representation of a service descriptor. */
  77. typedef struct hs_service_descriptor_t {
  78. /* Decoded descriptor. This object is used for encoding when the service
  79. * publishes the descriptor. */
  80. hs_descriptor_t *desc;
  81. /* Descriptor signing keypair. */
  82. ed25519_keypair_t signing_kp;
  83. /* Blinded keypair derived from the master identity public key. */
  84. ed25519_keypair_t blinded_kp;
  85. /* When is the next time when we should upload the descriptor. */
  86. time_t next_upload_time;
  87. /* Introduction points assign to this descriptor which contains
  88. * hs_service_intropoints_t object indexed by authentication key (the RSA
  89. * key if the node is legacy). */
  90. hs_service_intropoints_t intro_points;
  91. /* The time period number this descriptor has been created for. */
  92. uint64_t time_period_num;
  93. /* True iff we have missing intro points for this descriptor because we
  94. * couldn't pick any nodes. */
  95. unsigned int missing_intro_points : 1;
  96. /* List of identity digests for hidden service directories to which we
  97. * couldn't upload this descriptor because we didn't have its router
  98. * descriptor at the time. If this list is non-empty, only the relays in this
  99. * list are re-tried to upload this descriptor when our directory information
  100. * have been updated. */
  101. smartlist_t *hsdir_missing_info;
  102. /** List of the responsible HSDirs (their b64ed identity digest) last time we
  103. * uploaded this descriptor. If the set of responsible HSDirs is different
  104. * from this list, this means we received new dirinfo and we need to
  105. * reupload our descriptor. This list is always sorted lexicographically. */
  106. smartlist_t *previous_hsdirs;
  107. } hs_service_descriptor_t;
  108. /* Service key material. */
  109. typedef struct hs_service_keys_t {
  110. /* Master identify public key. */
  111. ed25519_public_key_t identity_pk;
  112. /* Master identity private key. */
  113. ed25519_secret_key_t identity_sk;
  114. /* True iff the key is kept offline which means the identity_sk MUST not be
  115. * used in that case. */
  116. unsigned int is_identify_key_offline : 1;
  117. } hs_service_keys_t;
  118. /* Service configuration. The following are set from the torrc options either
  119. * set by the configuration file or by the control port. Nothing else should
  120. * change those values. */
  121. typedef struct hs_service_config_t {
  122. /* Protocol version of the service. Specified by HiddenServiceVersion
  123. * option. */
  124. uint32_t version;
  125. /* List of rend_service_port_config_t */
  126. smartlist_t *ports;
  127. /* Path on the filesystem where the service persistent data is stored. NULL
  128. * if the service is ephemeral. Specified by HiddenServiceDir option. */
  129. char *directory_path;
  130. /* The maximum number of simultaneous streams per rendezvous circuit that
  131. * are allowed to be created. No limit if 0. Specified by
  132. * HiddenServiceMaxStreams option. */
  133. uint64_t max_streams_per_rdv_circuit;
  134. /* If true, we close circuits that exceed the max_streams_per_rdv_circuit
  135. * limit. Specified by HiddenServiceMaxStreamsCloseCircuit option. */
  136. unsigned int max_streams_close_circuit : 1;
  137. /* How many introduction points this service has. Specified by
  138. * HiddenServiceNumIntroductionPoints option. */
  139. unsigned int num_intro_points;
  140. /* True iff we allow request made on unknown ports. Specified by
  141. * HiddenServiceAllowUnknownPorts option. */
  142. unsigned int allow_unknown_ports : 1;
  143. /* If true, this service is a Single Onion Service. Specified by
  144. * HiddenServiceSingleHopMode and HiddenServiceNonAnonymousMode options. */
  145. unsigned int is_single_onion : 1;
  146. /* If true, allow group read permissions on the directory_path. Specified by
  147. * HiddenServiceDirGroupReadable option. */
  148. unsigned int dir_group_readable : 1;
  149. /* Is this service ephemeral? */
  150. unsigned int is_ephemeral : 1;
  151. } hs_service_config_t;
  152. /* Service state. */
  153. typedef struct hs_service_state_t {
  154. /* The time at which we've started our retry period to build circuits. We
  155. * don't want to stress circuit creation so we can only retry for a certain
  156. * time and then after we stop and wait. */
  157. time_t intro_circ_retry_started_time;
  158. /* Number of circuit we've launched during a single retry period. This
  159. * should never go over MAX_INTRO_CIRCS_PER_PERIOD. */
  160. unsigned int num_intro_circ_launched;
  161. /* Indicate that the service has entered the overlap period. We use this
  162. * flag to check for descriptor rotation. */
  163. unsigned int in_overlap_period : 1;
  164. /* Replay cache tracking the REND_COOKIE found in INTRODUCE2 cell to detect
  165. * repeats. Clients may send INTRODUCE1 cells for the same rendezvous point
  166. * through two or more different introduction points; when they do, this
  167. * keeps us from launching multiple simultaneous attempts to connect to the
  168. * same rend point. */
  169. replaycache_t *replay_cache_rend_cookie;
  170. } hs_service_state_t;
  171. /* Representation of a service running on this tor instance. */
  172. typedef struct hs_service_t {
  173. /* Onion address base32 encoded and NUL terminated. We keep it for logging
  174. * purposes so we don't have to build it everytime. */
  175. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  176. /* Hashtable node: use to look up the service by its master public identity
  177. * key in the service global map. */
  178. HT_ENTRY(hs_service_t) hs_service_node;
  179. /* Service state which contains various flags and counters. */
  180. hs_service_state_t state;
  181. /* Key material of the service. */
  182. hs_service_keys_t keys;
  183. /* Configuration of the service. */
  184. hs_service_config_t config;
  185. /* Current descriptor. */
  186. hs_service_descriptor_t *desc_current;
  187. /* Next descriptor that we need for the overlap period for which we have to
  188. * keep two sets of opened introduction point circuits. */
  189. hs_service_descriptor_t *desc_next;
  190. /* XXX: Credential (client auth.) #20700. */
  191. } hs_service_t;
  192. /* For the service global hash map, we define a specific type for it which
  193. * will make it safe to use and specific to some controlled parameters such as
  194. * the hashing function and how to compare services. */
  195. typedef HT_HEAD(hs_service_ht, hs_service_t) hs_service_ht;
  196. /* API */
  197. /* Global initializer and cleanup function. */
  198. void hs_service_init(void);
  199. void hs_service_free_all(void);
  200. /* Service new/free functions. */
  201. hs_service_t *hs_service_new(const or_options_t *options);
  202. void hs_service_free(hs_service_t *service);
  203. unsigned int hs_service_get_num_services(void);
  204. void hs_service_stage_services(const smartlist_t *service_list);
  205. int hs_service_load_all_keys(void);
  206. void hs_service_lists_fnames_for_sandbox(smartlist_t *file_list,
  207. smartlist_t *dir_list);
  208. int hs_service_set_conn_addr_port(const origin_circuit_t *circ,
  209. edge_connection_t *conn);
  210. void hs_hsdir_set_changed_consider_reupload(void);
  211. void hs_service_dir_info_changed(void);
  212. void hs_service_run_scheduled_events(time_t now);
  213. void hs_service_circuit_has_opened(origin_circuit_t *circ);
  214. int hs_service_receive_intro_established(origin_circuit_t *circ,
  215. const uint8_t *payload,
  216. size_t payload_len);
  217. int hs_service_receive_introduce2(origin_circuit_t *circ,
  218. const uint8_t *payload,
  219. size_t payload_len);
  220. void hs_service_intro_circ_has_closed(origin_circuit_t *circ);
  221. #ifdef HS_SERVICE_PRIVATE
  222. #ifdef TOR_UNIT_TESTS
  223. /* Useful getters for unit tests. */
  224. STATIC unsigned int get_hs_service_map_size(void);
  225. STATIC int get_hs_service_staging_list_size(void);
  226. STATIC hs_service_ht *get_hs_service_map(void);
  227. STATIC hs_service_t *get_first_service(void);
  228. /* Service accessors. */
  229. STATIC hs_service_t *find_service(hs_service_ht *map,
  230. const ed25519_public_key_t *pk);
  231. STATIC void remove_service(hs_service_ht *map, hs_service_t *service);
  232. STATIC int register_service(hs_service_ht *map, hs_service_t *service);
  233. /* Service introduction point functions. */
  234. STATIC hs_service_intro_point_t *service_intro_point_new(
  235. const extend_info_t *ei,
  236. unsigned int is_legacy);
  237. STATIC void service_intro_point_free(hs_service_intro_point_t *ip);
  238. STATIC void service_intro_point_add(digest256map_t *map,
  239. hs_service_intro_point_t *ip);
  240. STATIC void service_intro_point_remove(const hs_service_t *service,
  241. const hs_service_intro_point_t *ip);
  242. STATIC hs_service_intro_point_t *service_intro_point_find(
  243. const hs_service_t *service,
  244. const ed25519_public_key_t *auth_key);
  245. STATIC hs_service_intro_point_t *service_intro_point_find_by_ident(
  246. const hs_service_t *service,
  247. const hs_ident_circuit_t *ident);
  248. /* Service descriptor functions. */
  249. STATIC hs_service_descriptor_t *service_descriptor_new(void);
  250. STATIC hs_service_descriptor_t *service_desc_find_by_intro(
  251. const hs_service_t *service,
  252. const hs_service_intro_point_t *ip);
  253. /* Helper functions. */
  254. STATIC void get_objects_from_ident(const hs_ident_circuit_t *ident,
  255. hs_service_t **service,
  256. hs_service_intro_point_t **ip,
  257. hs_service_descriptor_t **desc);
  258. STATIC const node_t *
  259. get_node_from_intro_point(const hs_service_intro_point_t *ip);
  260. STATIC int can_service_launch_intro_circuit(hs_service_t *service,
  261. time_t now);
  262. STATIC int intro_point_should_expire(const hs_service_intro_point_t *ip,
  263. time_t now);
  264. STATIC void run_housekeeping_event(time_t now);
  265. STATIC void rotate_all_descriptors(time_t now);
  266. STATIC void build_all_descriptors(time_t now);
  267. STATIC void update_all_descriptors(time_t now);
  268. STATIC void run_upload_descriptor_event(time_t now);
  269. STATIC char *
  270. encode_desc_rev_counter_for_state(const hs_service_descriptor_t *desc);
  271. STATIC void service_descriptor_free(hs_service_descriptor_t *desc);
  272. STATIC uint64_t
  273. check_state_line_for_service_rev_counter(const char *state_line,
  274. const ed25519_public_key_t *blinded_pubkey,
  275. int *service_found_out);
  276. STATIC int
  277. write_address_to_file(const hs_service_t *service, const char *fname_);
  278. STATIC void upload_descriptor_to_all(const hs_service_t *service,
  279. hs_service_descriptor_t *desc,
  280. int for_next_period);
  281. STATIC void service_desc_schedule_upload(hs_service_descriptor_t *desc,
  282. time_t now,
  283. int descriptor_changed);
  284. #endif /* TOR_UNIT_TESTS */
  285. #endif /* HS_SERVICE_PRIVATE */
  286. #endif /* TOR_HS_SERVICE_H */