hs_service.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /* As described in the specification, service publishes their next descriptor
  22. * at a random time between those two values (in seconds). */
  23. #define HS_SERVICE_NEXT_UPLOAD_TIME_MIN (60 * 60)
  24. #define HS_SERVICE_NEXT_UPLOAD_TIME_MAX (120 * 60)
  25. /* Service side introduction point. */
  26. typedef struct hs_service_intro_point_t {
  27. /* Top level intropoint "shared" data between client/service. */
  28. hs_intropoint_t base;
  29. /* Onion key of the introduction point used to extend to it for the ntor
  30. * handshake. */
  31. curve25519_public_key_t onion_key;
  32. /* Authentication keypair used to create the authentication certificate
  33. * which is published in the descriptor. */
  34. ed25519_keypair_t auth_key_kp;
  35. /* Encryption keypair for the "ntor" type. */
  36. curve25519_keypair_t enc_key_kp;
  37. /* Legacy key if that intro point doesn't support v3. This should be used if
  38. * the base object legacy flag is set. */
  39. crypto_pk_t *legacy_key;
  40. /* Amount of INTRODUCE2 cell accepted from this intro point. */
  41. uint64_t introduce2_count;
  42. /* Maximum number of INTRODUCE2 cell this intro point should accept. */
  43. uint64_t introduce2_max;
  44. /* The time at which this intro point should expire and stop being used. */
  45. time_t time_to_expire;
  46. /* The amount of circuit creation we've made to this intro point. This is
  47. * incremented every time we do a circuit relaunch on this intro point which
  48. * is triggered when the circuit dies but the node is still in the
  49. * consensus. After MAX_INTRO_POINT_CIRCUIT_RETRIES, we give up on it. */
  50. uint32_t circuit_retries;
  51. /* Set if this intro point has an established circuit. */
  52. unsigned int circuit_established : 1;
  53. /* Replay cache recording the encrypted part of an INTRODUCE2 cell that the
  54. * circuit associated with this intro point has received. This is used to
  55. * prevent replay attacks. */
  56. replaycache_t *replay_cache;
  57. } hs_service_intro_point_t;
  58. /* Object handling introduction points of a service. */
  59. typedef struct hs_service_intropoints_t {
  60. /* The time at which we've started our retry period to build circuits. We
  61. * don't want to stress circuit creation so we can only retry for a certain
  62. * time and then after we stop and wait. */
  63. time_t retry_period_started;
  64. /* Number of circuit we've launched during a single retry period. */
  65. unsigned int num_circuits_launched;
  66. /* Contains the current hs_service_intro_point_t objects indexed by
  67. * authentication public key. */
  68. digest256map_t *map;
  69. /* Contains node's identity key digest that were introduction point for this
  70. * descriptor but were retried to many times. We keep those so we avoid
  71. * re-picking them over and over for a circuit retry period.
  72. * XXX: Once we have #22173, change this to only use ed25519 identity. */
  73. digestmap_t *failed_id;
  74. } hs_service_intropoints_t;
  75. /* Representation of a service descriptor. */
  76. typedef struct hs_service_descriptor_t {
  77. /* Decoded descriptor. This object is used for encoding when the service
  78. * publishes the descriptor. */
  79. hs_descriptor_t *desc;
  80. /* Descriptor signing keypair. */
  81. ed25519_keypair_t signing_kp;
  82. /* Blinded keypair derived from the master identity public key. */
  83. ed25519_keypair_t blinded_kp;
  84. /* When is the next time when we should upload the descriptor. */
  85. time_t next_upload_time;
  86. /* Introduction points assign to this descriptor which contains
  87. * hs_service_intropoints_t object indexed by authentication key (the RSA
  88. * key if the node is legacy). */
  89. hs_service_intropoints_t intro_points;
  90. /* The time period number this descriptor has been created for. */
  91. uint64_t time_period_num;
  92. /* True iff we have missing intro points for this descriptor because we
  93. * couldn't pick any nodes. */
  94. unsigned int missing_intro_points : 1;
  95. /* List of hidden service directories node_t object to which we couldn't
  96. * upload this descriptor because we didn't have its router descriptor at
  97. * the time. If this list is non-empty, only the relays in this list are
  98. * re-tried to upload this descriptor when our directory information have
  99. * been updated. */
  100. smartlist_t *hsdir_missing_info;
  101. } hs_service_descriptor_t;
  102. /* Service key material. */
  103. typedef struct hs_service_keys_t {
  104. /* Master identify public key. */
  105. ed25519_public_key_t identity_pk;
  106. /* Master identity private key. */
  107. ed25519_secret_key_t identity_sk;
  108. /* True iff the key is kept offline which means the identity_sk MUST not be
  109. * used in that case. */
  110. unsigned int is_identify_key_offline : 1;
  111. } hs_service_keys_t;
  112. /* Service configuration. The following are set from the torrc options either
  113. * set by the configuration file or by the control port. Nothing else should
  114. * change those values. */
  115. typedef struct hs_service_config_t {
  116. /* Protocol version of the service. Specified by HiddenServiceVersion
  117. * option. */
  118. uint32_t version;
  119. /* List of rend_service_port_config_t */
  120. smartlist_t *ports;
  121. /* Path on the filesystem where the service persistent data is stored. NULL
  122. * if the service is ephemeral. Specified by HiddenServiceDir option. */
  123. char *directory_path;
  124. /* The maximum number of simultaneous streams per rendezvous circuit that
  125. * are allowed to be created. No limit if 0. Specified by
  126. * HiddenServiceMaxStreams option. */
  127. uint64_t max_streams_per_rdv_circuit;
  128. /* If true, we close circuits that exceed the max_streams_per_rdv_circuit
  129. * limit. Specified by HiddenServiceMaxStreamsCloseCircuit option. */
  130. unsigned int max_streams_close_circuit : 1;
  131. /* How many introduction points this service has. Specified by
  132. * HiddenServiceNumIntroductionPoints option. */
  133. unsigned int num_intro_points;
  134. /* True iff we allow request made on unknown ports. Specified by
  135. * HiddenServiceAllowUnknownPorts option. */
  136. unsigned int allow_unknown_ports : 1;
  137. /* If true, this service is a Single Onion Service. Specified by
  138. * HiddenServiceSingleHopMode and HiddenServiceNonAnonymousMode options. */
  139. unsigned int is_single_onion : 1;
  140. /* If true, allow group read permissions on the directory_path. Specified by
  141. * HiddenServiceDirGroupReadable option. */
  142. unsigned int dir_group_readable : 1;
  143. /* Is this service ephemeral? */
  144. unsigned int is_ephemeral : 1;
  145. } hs_service_config_t;
  146. /* Service state. */
  147. typedef struct hs_service_state_t {
  148. /* The time at which we've started our retry period to build circuits. We
  149. * don't want to stress circuit creation so we can only retry for a certain
  150. * time and then after we stop and wait. */
  151. time_t intro_circ_retry_started_time;
  152. /* Number of circuit we've launched during a single retry period. This
  153. * should never go over MAX_INTRO_CIRCS_PER_PERIOD. */
  154. unsigned int num_intro_circ_launched;
  155. /* Indicate that the service has entered the overlap period. We use this
  156. * flag to check for descriptor rotation. */
  157. unsigned int in_overlap_period : 1;
  158. /* Replay cache tracking the REND_COOKIE found in INTRODUCE2 cell to detect
  159. * repeats. Clients may send INTRODUCE1 cells for the same rendezvous point
  160. * through two or more different introduction points; when they do, this
  161. * keeps us from launching multiple simultaneous attempts to connect to the
  162. * same rend point. */
  163. replaycache_t *replay_cache_rend_cookie;
  164. } hs_service_state_t;
  165. /* Representation of a service running on this tor instance. */
  166. typedef struct hs_service_t {
  167. /* Onion address base32 encoded and NUL terminated. We keep it for logging
  168. * purposes so we don't have to build it everytime. */
  169. char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  170. /* Hashtable node: use to look up the service by its master public identity
  171. * key in the service global map. */
  172. HT_ENTRY(hs_service_t) hs_service_node;
  173. /* Service state which contains various flags and counters. */
  174. hs_service_state_t state;
  175. /* Key material of the service. */
  176. hs_service_keys_t keys;
  177. /* Configuration of the service. */
  178. hs_service_config_t config;
  179. /* Current descriptor. */
  180. hs_service_descriptor_t *desc_current;
  181. /* Next descriptor that we need for the overlap period for which we have to
  182. * keep two sets of opened introduction point circuits. */
  183. hs_service_descriptor_t *desc_next;
  184. /* XXX: Credential (client auth.) #20700. */
  185. } hs_service_t;
  186. /* For the service global hash map, we define a specific type for it which
  187. * will make it safe to use and specific to some controlled parameters such as
  188. * the hashing function and how to compare services. */
  189. typedef HT_HEAD(hs_service_ht, hs_service_t) hs_service_ht;
  190. /* API */
  191. /* Global initializer and cleanup function. */
  192. void hs_service_init(void);
  193. void hs_service_free_all(void);
  194. /* Service new/free functions. */
  195. hs_service_t *hs_service_new(const or_options_t *options);
  196. void hs_service_free(hs_service_t *service);
  197. void hs_service_stage_services(const smartlist_t *service_list);
  198. int hs_service_load_all_keys(void);
  199. void hs_service_lists_fnames_for_sandbox(smartlist_t *file_list,
  200. smartlist_t *dir_list);
  201. int hs_service_set_conn_addr_port(const origin_circuit_t *circ,
  202. edge_connection_t *conn);
  203. void hs_service_dir_info_changed(void);
  204. void hs_service_run_scheduled_events(time_t now);
  205. void hs_service_circuit_has_opened(origin_circuit_t *circ);
  206. int hs_service_receive_intro_established(origin_circuit_t *circ,
  207. const uint8_t *payload,
  208. size_t payload_len);
  209. int hs_service_receive_introduce2(origin_circuit_t *circ,
  210. const uint8_t *payload,
  211. size_t payload_len);
  212. #ifdef HS_SERVICE_PRIVATE
  213. #ifdef TOR_UNIT_TESTS
  214. /* Useful getters for unit tests. */
  215. STATIC unsigned int get_hs_service_map_size(void);
  216. STATIC int get_hs_service_staging_list_size(void);
  217. STATIC hs_service_ht *get_hs_service_map(void);
  218. STATIC hs_service_t *get_first_service(void);
  219. /* Service accessors. */
  220. STATIC hs_service_t *find_service(hs_service_ht *map,
  221. const ed25519_public_key_t *pk);
  222. STATIC void remove_service(hs_service_ht *map, hs_service_t *service);
  223. STATIC int register_service(hs_service_ht *map, hs_service_t *service);
  224. STATIC hs_service_intro_point_t *service_intro_point_new(
  225. const extend_info_t *ei,
  226. unsigned int is_legacy);
  227. STATIC void service_intro_point_free(hs_service_intro_point_t *ip);
  228. #endif /* TOR_UNIT_TESTS */
  229. #endif /* HS_SERVICE_PRIVATE */
  230. #endif /* TOR_HS_SERVICE_H */