hs_service.h 9.0 KB

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