hs_service.h 7.5 KB

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