hs_service.h 9.4 KB

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