hs_common.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_common.h
  5. * \brief Header file containing common data for the whole HS subsytem.
  6. **/
  7. #ifndef TOR_HS_COMMON_H
  8. #define TOR_HS_COMMON_H
  9. #include "or.h"
  10. /* Trunnel */
  11. #include "ed25519_cert.h"
  12. /* Protocol version 2. Use this instead of hardcoding "2" in the code base,
  13. * this adds a clearer semantic to the value when used. */
  14. #define HS_VERSION_TWO 2
  15. /* Version 3 of the protocol (prop224). */
  16. #define HS_VERSION_THREE 3
  17. /* Earliest and latest version we support. */
  18. #define HS_VERSION_MIN HS_VERSION_TWO
  19. #define HS_VERSION_MAX HS_VERSION_THREE
  20. /** Try to maintain this many intro points per service by default. */
  21. #define NUM_INTRO_POINTS_DEFAULT 3
  22. /** Maximum number of intro points per generic and version 2 service. */
  23. #define NUM_INTRO_POINTS_MAX 10
  24. /** Number of extra intro points we launch if our set of intro nodes is empty.
  25. * See proposal 155, section 4. */
  26. #define NUM_INTRO_POINTS_EXTRA 2
  27. /** If we can't build our intro circuits, don't retry for this long. */
  28. #define INTRO_CIRC_RETRY_PERIOD (60*5)
  29. /** Don't try to build more than this many circuits before giving up for a
  30. * while.*/
  31. #define MAX_INTRO_CIRCS_PER_PERIOD 10
  32. /** How many times will a hidden service operator attempt to connect to a
  33. * requested rendezvous point before giving up? */
  34. #define MAX_REND_FAILURES 1
  35. /** How many seconds should we spend trying to connect to a requested
  36. * rendezvous point before giving up? */
  37. #define MAX_REND_TIMEOUT 30
  38. /* String prefix for the signature of ESTABLISH_INTRO */
  39. #define ESTABLISH_INTRO_SIG_PREFIX "Tor establish-intro cell v1"
  40. /* The default HS time period length */
  41. #define HS_TIME_PERIOD_LENGTH_DEFAULT 1440 /* 1440 minutes == one day */
  42. /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
  43. #define HS_TIME_PERIOD_LENGTH_MIN 30 /* minutes */
  44. /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
  45. #define HS_TIME_PERIOD_LENGTH_MAX (60 * 24 * 10) /* 10 days or 14400 minutes */
  46. /* The time period rotation offset as seen in prop224 section [TIME-PERIODS] */
  47. #define HS_TIME_PERIOD_ROTATION_OFFSET (12 * 60) /* minutes */
  48. /* Prefix of the onion address checksum. */
  49. #define HS_SERVICE_ADDR_CHECKSUM_PREFIX ".onion checksum"
  50. /* Length of the checksum prefix minus the NUL terminated byte. */
  51. #define HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN \
  52. (sizeof(HS_SERVICE_ADDR_CHECKSUM_PREFIX) - 1)
  53. /* Length of the resulting checksum of the address. The construction of this
  54. * checksum looks like:
  55. * CHECKSUM = ".onion checksum" || PUBKEY || VERSION
  56. * where VERSION is 1 byte. This is pre-hashing. */
  57. #define HS_SERVICE_ADDR_CHECKSUM_INPUT_LEN \
  58. (HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN + ED25519_PUBKEY_LEN + sizeof(uint8_t))
  59. /* The amount of bytes we use from the address checksum. */
  60. #define HS_SERVICE_ADDR_CHECKSUM_LEN_USED 2
  61. /* Length of the binary encoded service address which is of course before the
  62. * base32 encoding. Construction is:
  63. * PUBKEY || CHECKSUM || VERSION
  64. * with 1 byte VERSION and 2 bytes CHECKSUM. The following is 35 bytes. */
  65. #define HS_SERVICE_ADDR_LEN \
  66. (ED25519_PUBKEY_LEN + HS_SERVICE_ADDR_CHECKSUM_LEN_USED + sizeof(uint8_t))
  67. /* Length of 'y' portion of 'y.onion' URL. This is base32 encoded and the
  68. * length ends up to 56 bytes (not counting the terminated NUL byte.) */
  69. #define HS_SERVICE_ADDR_LEN_BASE32 \
  70. (CEIL_DIV(HS_SERVICE_ADDR_LEN * 8, 5))
  71. /* The default HS time period length */
  72. #define HS_TIME_PERIOD_LENGTH_DEFAULT 1440 /* 1440 minutes == one day */
  73. /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
  74. #define HS_TIME_PERIOD_LENGTH_MIN 30 /* minutes */
  75. /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
  76. #define HS_TIME_PERIOD_LENGTH_MAX (60 * 24 * 10) /* 10 days or 14400 minutes */
  77. /* The time period rotation offset as seen in prop224 section [TIME-PERIODS] */
  78. #define HS_TIME_PERIOD_ROTATION_OFFSET (12 * 60) /* minutes */
  79. /* Keyblinding parameter construction is as follow:
  80. * "key-blind" || INT_8(period_num) || INT_8(start_period_sec) */
  81. #define HS_KEYBLIND_NONCE_PREFIX "key-blind"
  82. #define HS_KEYBLIND_NONCE_PREFIX_LEN (sizeof(HS_KEYBLIND_NONCE_PREFIX) - 1)
  83. #define HS_KEYBLIND_NONCE_LEN \
  84. (HS_KEYBLIND_NONCE_PREFIX_LEN + sizeof(uint64_t) + sizeof(uint64_t))
  85. /* Credential and subcredential prefix value. */
  86. #define HS_CREDENTIAL_PREFIX "credential"
  87. #define HS_CREDENTIAL_PREFIX_LEN (sizeof(HS_CREDENTIAL_PREFIX) - 1)
  88. #define HS_SUBCREDENTIAL_PREFIX "subcredential"
  89. #define HS_SUBCREDENTIAL_PREFIX_LEN (sizeof(HS_SUBCREDENTIAL_PREFIX) - 1)
  90. /* Type of authentication key used by an introduction point. */
  91. typedef enum {
  92. HS_AUTH_KEY_TYPE_LEGACY = 1,
  93. HS_AUTH_KEY_TYPE_ED25519 = 2,
  94. } hs_auth_key_type_t;
  95. /* Represents the mapping from a virtual port of a rendezvous service to a
  96. * real port on some IP. */
  97. typedef struct rend_service_port_config_t {
  98. /* The incoming HS virtual port we're mapping */
  99. uint16_t virtual_port;
  100. /* Is this an AF_UNIX port? */
  101. unsigned int is_unix_addr:1;
  102. /* The outgoing TCP port to use, if !is_unix_addr */
  103. uint16_t real_port;
  104. /* The outgoing IPv4 or IPv6 address to use, if !is_unix_addr */
  105. tor_addr_t real_addr;
  106. /* The socket path to connect to, if is_unix_addr */
  107. char unix_addr[FLEXIBLE_ARRAY_MEMBER];
  108. } rend_service_port_config_t;
  109. void hs_init(void);
  110. void hs_free_all(void);
  111. int hs_check_service_private_dir(const char *username, const char *path,
  112. unsigned int dir_group_readable,
  113. unsigned int create);
  114. char *hs_path_from_filename(const char *directory, const char *filename);
  115. void hs_build_address(const ed25519_public_key_t *key, uint8_t version,
  116. char *addr_out);
  117. int hs_address_is_valid(const char *address);
  118. int hs_parse_address(const char *address, ed25519_public_key_t *key_out,
  119. uint8_t *checksum_out, uint8_t *version_out);
  120. void hs_build_blinded_pubkey(const ed25519_public_key_t *pubkey,
  121. const uint8_t *secret, size_t secret_len,
  122. uint64_t time_period_num,
  123. ed25519_public_key_t *pubkey_out);
  124. void hs_build_blinded_keypair(const ed25519_keypair_t *kp,
  125. const uint8_t *secret, size_t secret_len,
  126. uint64_t time_period_num,
  127. ed25519_keypair_t *kp_out);
  128. int hs_service_requires_uptime_circ(const smartlist_t *ports);
  129. void rend_data_free(rend_data_t *data);
  130. rend_data_t *rend_data_dup(const rend_data_t *data);
  131. rend_data_t *rend_data_client_create(const char *onion_address,
  132. const char *desc_id,
  133. const char *cookie,
  134. rend_auth_type_t auth_type);
  135. rend_data_t *rend_data_service_create(const char *onion_address,
  136. const char *pk_digest,
  137. const uint8_t *cookie,
  138. rend_auth_type_t auth_type);
  139. const char *rend_data_get_address(const rend_data_t *rend_data);
  140. const char *rend_data_get_desc_id(const rend_data_t *rend_data,
  141. uint8_t replica, size_t *len_out);
  142. const uint8_t *rend_data_get_pk_digest(const rend_data_t *rend_data,
  143. size_t *len_out);
  144. void hs_get_subcredential(const ed25519_public_key_t *identity_pk,
  145. const ed25519_public_key_t *blinded_pk,
  146. uint8_t *subcred_out);
  147. uint64_t hs_get_time_period_num(time_t now);
  148. uint64_t hs_get_next_time_period_num(time_t now);
  149. link_specifier_t *hs_link_specifier_dup(const link_specifier_t *lspec);
  150. int hs_overlap_mode_is_active(const networkstatus_t *consensus, time_t now);
  151. #ifdef HS_COMMON_PRIVATE
  152. #ifdef TOR_UNIT_TESTS
  153. STATIC uint64_t get_time_period_length(void);
  154. #endif /* TOR_UNIT_TESTS */
  155. #endif /* HS_COMMON_PRIVATE */
  156. #endif /* TOR_HS_COMMON_H */