hs_service.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_intropoint.h"
  15. /* Trunnel */
  16. #include "hs/cell_establish_intro.h"
  17. /* When loading and configuring a service, this is the default version it will
  18. * be configured for as it is possible that no HiddenServiceVersion is
  19. * present. */
  20. #define HS_SERVICE_DEFAULT_VERSION HS_VERSION_TWO
  21. /* Service side introduction point. */
  22. typedef struct hs_service_intro_point_t {
  23. /* Top level intropoint "shared" data between client/service. */
  24. hs_intropoint_t base;
  25. /* Authentication keypair used to create the authentication certificate
  26. * which is published in the descriptor. */
  27. ed25519_keypair_t auth_key_kp;
  28. /* Encryption private key. */
  29. curve25519_secret_key_t enc_key_sk;
  30. /* Amount of INTRODUCE2 cell accepted from this intro point. */
  31. uint64_t introduce2_count;
  32. /* Maximum number of INTRODUCE2 cell this intro point should accept. */
  33. uint64_t introduce2_max;
  34. /* The time at which this intro point should expire and stop being used. */
  35. time_t time_to_expire;
  36. /* The amount of circuit creation we've made to this intro point. This is
  37. * incremented every time we do a circuit relaunch on this intro point which
  38. * is triggered when the circuit dies but the node is still in the
  39. * consensus. After MAX_INTRO_POINT_CIRCUIT_RETRIES, we give up on it. */
  40. uint32_t circuit_retries;
  41. /* Set if this intro point has an established circuit. */
  42. unsigned int circuit_established : 1;
  43. /* Replay cache recording the encrypted part of an INTRODUCE2 cell that the
  44. * circuit associated with this intro point has received. This is used to
  45. * prevent replay attacks. */
  46. replaycache_t *replay_cache;
  47. } hs_service_intro_point_t;
  48. /* Object handling introduction points of a service. */
  49. typedef struct hs_service_intropoints_t {
  50. /* The time at which we've started our retry period to build circuits. We
  51. * don't want to stress circuit creation so we can only retry for a certain
  52. * time and then after we stop and wait. */
  53. time_t retry_period_started;
  54. /* Number of circuit we've launched during a single retry period. */
  55. unsigned int num_circuits_launched;
  56. /* Contains the current hs_service_intro_point_t objects indexed by
  57. * authentication public key. */
  58. digest256map_t *map;
  59. } hs_service_intropoints_t;
  60. /* Representation of a service descriptor. */
  61. typedef struct hs_service_descriptor_t {
  62. /* Decoded descriptor. This object is used for encoding when the service
  63. * publishes the descriptor. */
  64. hs_descriptor_t *desc;
  65. /* Descriptor signing keypair. */
  66. ed25519_keypair_t signing_kp;
  67. /* Blinded keypair derived from the master identity public key. */
  68. ed25519_keypair_t blinded_kp;
  69. /* When is the next time when we should upload the descriptor. */
  70. time_t next_upload_time;
  71. /* Introduction points assign to this descriptor which contains
  72. * hs_service_intropoints_t object indexed by authentication key (the RSA
  73. * key if the node is legacy). */
  74. hs_service_intropoints_t intro_points;
  75. } hs_service_descriptor_t;
  76. /* Service key material. */
  77. typedef struct hs_service_keys_t {
  78. /* Master identify public key. */
  79. ed25519_public_key_t identity_pk;
  80. /* Master identity private key. */
  81. ed25519_secret_key_t identity_sk;
  82. /* True iff the key is kept offline which means the identity_sk MUST not be
  83. * used in that case. */
  84. unsigned int is_identify_key_offline : 1;
  85. } hs_service_keys_t;
  86. /* Service configuration. The following are set from the torrc options either
  87. * set by the configuration file or by the control port. Nothing else should
  88. * change those values. */
  89. typedef struct hs_service_config_t {
  90. /* Protocol version of the service. Specified by HiddenServiceVersion
  91. * option. */
  92. uint32_t version;
  93. /* List of rend_service_port_config_t */
  94. smartlist_t *ports;
  95. /* Path on the filesystem where the service persistent data is stored. NULL
  96. * if the service is ephemeral. Specified by HiddenServiceDir option. */
  97. char *directory_path;
  98. /* The time period after which a descriptor is uploaded to the directories
  99. * in seconds. Specified by RendPostPeriod option. */
  100. uint32_t descriptor_post_period;
  101. /* The maximum number of simultaneous streams per rendezvous circuit that
  102. * are allowed to be created. No limit if 0. Specified by
  103. * HiddenServiceMaxStreams option. */
  104. uint64_t max_streams_per_rdv_circuit;
  105. /* If true, we close circuits that exceed the max_streams_per_rdv_circuit
  106. * limit. Specified by HiddenServiceMaxStreamsCloseCircuit option. */
  107. unsigned int max_streams_close_circuit : 1;
  108. /* How many introduction points this service has. Specified by
  109. * HiddenServiceNumIntroductionPoints option. */
  110. unsigned int num_intro_points;
  111. /* True iff we allow request made on unknown ports. Specified by
  112. * HiddenServiceAllowUnknownPorts option. */
  113. unsigned int allow_unknown_ports : 1;
  114. /* If true, this service is a Single Onion Service. Specified by
  115. * HiddenServiceSingleHopMode and HiddenServiceNonAnonymousMode options. */
  116. unsigned int is_single_onion : 1;
  117. /* If true, allow group read permissions on the directory_path. Specified by
  118. * HiddenServiceDirGroupReadable option. */
  119. unsigned int dir_group_readable : 1;
  120. /* Is this service ephemeral? */
  121. unsigned int is_ephemeral : 1;
  122. } hs_service_config_t;
  123. /* Service state. */
  124. typedef struct hs_service_state_t {
  125. /* The time at which we've started our retry period to build circuits. We
  126. * don't want to stress circuit creation so we can only retry for a certain
  127. * time and then after we stop and wait. */
  128. time_t intro_circ_retry_started_time;
  129. /* Number of circuit we've launched during a single retry period. This
  130. * should never go over MAX_INTRO_CIRCS_PER_PERIOD. */
  131. unsigned int num_intro_circ_launched;
  132. /* Indicate that the service has entered the overlap period. We use this
  133. * flag to check for descriptor rotation. */
  134. unsigned int in_overlap_period : 1;
  135. } hs_service_state_t;
  136. /* Representation of a service running on this tor instance. */
  137. typedef struct hs_service_t {
  138. /* Onion address base32 encoded and NUL terminated. We keep it for logging
  139. * purposes so we don't have to build it everytime. */
  140. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  141. /* Hashtable node: use to look up the service by its master public identity
  142. * key in the service global map. */
  143. HT_ENTRY(hs_service_t) hs_service_node;
  144. /* Service state which contains various flags and counters. */
  145. hs_service_state_t state;
  146. /* Key material of the service. */
  147. hs_service_keys_t keys;
  148. /* Configuration of the service. */
  149. hs_service_config_t config;
  150. /* Current descriptor. */
  151. hs_service_descriptor_t *desc_current;
  152. /* Next descriptor that we need for the overlap period for which we have to
  153. * keep two sets of opened introduction point circuits. */
  154. hs_service_descriptor_t *desc_next;
  155. /* XXX: Credential (client auth.) #20700. */
  156. } hs_service_t;
  157. /* For the service global hash map, we define a specific type for it which
  158. * will make it safe to use and specific to some controlled parameters such as
  159. * the hashing function and how to compare services. */
  160. typedef HT_HEAD(hs_service_ht, hs_service_t) hs_service_ht;
  161. /* API */
  162. /* Global initializer and cleanup function. */
  163. void hs_service_init(void);
  164. void hs_service_free_all(void);
  165. /* Service new/free functions. */
  166. hs_service_t *hs_service_new(const or_options_t *options);
  167. void hs_service_free(hs_service_t *service);
  168. void hs_service_stage_services(const smartlist_t *service_list);
  169. int hs_service_load_all_keys(void);
  170. /* These functions are only used by unit tests and we need to expose them else
  171. * hs_service.o ends up with no symbols in libor.a which makes clang throw a
  172. * warning at compile time. See #21825. */
  173. trn_cell_establish_intro_t *
  174. generate_establish_intro_cell(const uint8_t *circuit_key_material,
  175. size_t circuit_key_material_len);
  176. ssize_t
  177. get_establish_intro_payload(uint8_t *buf, size_t buf_len,
  178. const trn_cell_establish_intro_t *cell);
  179. #ifdef HS_SERVICE_PRIVATE
  180. #ifdef TOR_UNIT_TESTS
  181. /* Useful getters for unit tests. */
  182. STATIC unsigned int get_hs_service_map_size(void);
  183. STATIC int get_hs_service_staging_list_size(void);
  184. STATIC hs_service_ht *get_hs_service_map(void);
  185. STATIC hs_service_t *get_first_service(void);
  186. /* Service accessors. */
  187. STATIC hs_service_t *find_service(hs_service_ht *map,
  188. const ed25519_public_key_t *pk);
  189. STATIC void remove_service(hs_service_ht *map, hs_service_t *service);
  190. STATIC int register_service(hs_service_ht *map, hs_service_t *service);
  191. #endif /* TOR_UNIT_TESTS */
  192. #endif /* HS_SERVICE_PRIVATE */
  193. #endif /* TOR_HS_SERVICE_H */