hs_descriptor.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* Copyright (c) 2016-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_descriptor.h
  5. * \brief Header file for hs_descriptor.c
  6. **/
  7. #ifndef TOR_HS_DESCRIPTOR_H
  8. #define TOR_HS_DESCRIPTOR_H
  9. #include <stdint.h>
  10. #include "core/or/or.h"
  11. #include "trunnel/ed25519_cert.h" /* needed for trunnel */
  12. #include "feature/nodelist/torcert.h"
  13. /* Trunnel */
  14. struct link_specifier_t;
  15. /* The earliest descriptor format version we support. */
  16. #define HS_DESC_SUPPORTED_FORMAT_VERSION_MIN 3
  17. /* The latest descriptor format version we support. */
  18. #define HS_DESC_SUPPORTED_FORMAT_VERSION_MAX 3
  19. /* Default lifetime of a descriptor in seconds. The valus is set at 3 hours
  20. * which is 180 minutes or 10800 seconds. */
  21. #define HS_DESC_DEFAULT_LIFETIME (3 * 60 * 60)
  22. /* Maximum lifetime of a descriptor in seconds. The value is set at 12 hours
  23. * which is 720 minutes or 43200 seconds. */
  24. #define HS_DESC_MAX_LIFETIME (12 * 60 * 60)
  25. /* Lifetime of certificate in the descriptor. This defines the lifetime of the
  26. * descriptor signing key and the cross certification cert of that key. It is
  27. * set to 54 hours because a descriptor can be around for 48 hours and because
  28. * consensuses are used after the hour, add an extra 6 hours to give some time
  29. * for the service to stop using it. */
  30. #define HS_DESC_CERT_LIFETIME (54 * 60 * 60)
  31. /* Length of the salt needed for the encrypted section of a descriptor. */
  32. #define HS_DESC_ENCRYPTED_SALT_LEN 16
  33. /* Length of the KDF output value which is the length of the secret key,
  34. * the secret IV and MAC key length which is the length of H() output. */
  35. #define HS_DESC_ENCRYPTED_KDF_OUTPUT_LEN \
  36. CIPHER256_KEY_LEN + CIPHER_IV_LEN + DIGEST256_LEN
  37. /* Pad plaintext of superencrypted data section before encryption so that its
  38. * length is a multiple of this value. */
  39. #define HS_DESC_SUPERENC_PLAINTEXT_PAD_MULTIPLE 10000
  40. /* Maximum length in bytes of a full hidden service descriptor. */
  41. #define HS_DESC_MAX_LEN 50000 /* 50kb max size */
  42. /* Key length for the descriptor symmetric encryption. As specified in the
  43. * protocol, we use AES-256 for the encrypted section of the descriptor. The
  44. * following is the length in bytes and the bit size. */
  45. #define HS_DESC_ENCRYPTED_KEY_LEN CIPHER256_KEY_LEN
  46. #define HS_DESC_ENCRYPTED_BIT_SIZE (HS_DESC_ENCRYPTED_KEY_LEN * 8)
  47. /* Length of each components in the auth client section in the descriptor. */
  48. #define HS_DESC_CLIENT_ID_LEN 8
  49. #define HS_DESC_DESCRIPTOR_COOKIE_LEN 16
  50. #define HS_DESC_COOKIE_KEY_LEN 32
  51. #define HS_DESC_COOKIE_KEY_BIT_SIZE (HS_DESC_COOKIE_KEY_LEN * 8)
  52. #define HS_DESC_ENCRYPED_COOKIE_LEN HS_DESC_DESCRIPTOR_COOKIE_LEN
  53. /* The number of auth client entries in the descriptor must be the multiple
  54. * of this constant. */
  55. #define HS_DESC_AUTH_CLIENT_MULTIPLE 16
  56. /* Type of authentication in the descriptor. */
  57. typedef enum {
  58. HS_DESC_AUTH_ED25519 = 1
  59. } hs_desc_auth_type_t;
  60. /* Introduction point information located in a descriptor. */
  61. typedef struct hs_desc_intro_point_t {
  62. /* Link specifier(s) which details how to extend to the relay. This list
  63. * contains link_specifier_t objects. It MUST have at least one. */
  64. smartlist_t *link_specifiers;
  65. /* Onion key of the introduction point used to extend to it for the ntor
  66. * handshake. */
  67. curve25519_public_key_t onion_key;
  68. /* Authentication key used to establish the introduction point circuit and
  69. * cross-certifies the blinded public key for the replica thus signed by
  70. * the blinded key and in turn signs it. */
  71. tor_cert_t *auth_key_cert;
  72. /* Encryption key for the "ntor" type. */
  73. curve25519_public_key_t enc_key;
  74. /* Certificate cross certifying the descriptor signing key by the encryption
  75. * curve25519 key. This certificate contains the signing key and is of type
  76. * CERT_TYPE_CROSS_HS_IP_KEYS [0B]. */
  77. tor_cert_t *enc_key_cert;
  78. /* (Optional): If this introduction point is a legacy one that is version <=
  79. * 0.2.9.x (HSIntro=3), we use this extra key for the intro point to be able
  80. * to relay the cells to the service correctly. */
  81. struct {
  82. /* RSA public key. */
  83. crypto_pk_t *key;
  84. /* Cross certified cert with the descriptor signing key (RSA->Ed). Because
  85. * of the cross certification API, we need to keep the certificate binary
  86. * blob and its length in order to properly encode it after. */
  87. struct {
  88. uint8_t *encoded;
  89. size_t len;
  90. } cert;
  91. } legacy;
  92. /* True iff the introduction point has passed the cross certification. Upon
  93. * decoding an intro point, this must be true. */
  94. unsigned int cross_certified : 1;
  95. } hs_desc_intro_point_t;
  96. /* Authorized client information located in a descriptor. */
  97. typedef struct hs_desc_authorized_client_t {
  98. /* An identifier that the client will use to identify which auth client
  99. * entry it needs to use. */
  100. uint8_t client_id[HS_DESC_CLIENT_ID_LEN];
  101. /* An IV that is used to decrypt the encrypted descriptor cookie. */
  102. uint8_t iv[CIPHER_IV_LEN];
  103. /* An encrypted descriptor cookie that the client needs to decrypt to use
  104. * it to decrypt the descriptor. */
  105. uint8_t encrypted_cookie[HS_DESC_ENCRYPED_COOKIE_LEN];
  106. } hs_desc_authorized_client_t;
  107. /* The encrypted data section of a descriptor. Obviously the data in this is
  108. * in plaintext but encrypted once encoded. */
  109. typedef struct hs_desc_encrypted_data_t {
  110. /* Bitfield of CREATE2 cell supported formats. The only currently supported
  111. * format is ntor. */
  112. unsigned int create2_ntor : 1;
  113. /* A list of authentication types that a client must at least support one
  114. * in order to contact the service. Contains NULL terminated strings. */
  115. smartlist_t *intro_auth_types;
  116. /* Is this descriptor a single onion service? */
  117. unsigned int single_onion_service : 1;
  118. /* A list of intro points. Contains hs_desc_intro_point_t objects. */
  119. smartlist_t *intro_points;
  120. } hs_desc_encrypted_data_t;
  121. /* The superencrypted data section of a descriptor. Obviously the data in
  122. * this is in plaintext but encrypted once encoded. */
  123. typedef struct hs_desc_superencrypted_data_t {
  124. /* This field contains ephemeral x25519 public key which is used by
  125. * the encryption scheme in the client authorization. */
  126. curve25519_public_key_t auth_ephemeral_pubkey;
  127. /* A list of authorized clients. Contains hs_desc_authorized_client_t
  128. * objects. */
  129. smartlist_t *clients;
  130. /* Decoding only: The b64-decoded encrypted blob from the descriptor */
  131. uint8_t *encrypted_blob;
  132. /* Decoding only: Size of the encrypted_blob */
  133. size_t encrypted_blob_size;
  134. } hs_desc_superencrypted_data_t;
  135. /* Plaintext data that is unencrypted information of the descriptor. */
  136. typedef struct hs_desc_plaintext_data_t {
  137. /* Version of the descriptor format. Spec specifies this field as a
  138. * positive integer. */
  139. uint32_t version;
  140. /* The lifetime of the descriptor in seconds. */
  141. uint32_t lifetime_sec;
  142. /* Certificate with the short-term ed22519 descriptor signing key for the
  143. * replica which is signed by the blinded public key for that replica. */
  144. tor_cert_t *signing_key_cert;
  145. /* Signing public key which is used to sign the descriptor. Same public key
  146. * as in the signing key certificate. */
  147. ed25519_public_key_t signing_pubkey;
  148. /* Blinded public key used for this descriptor derived from the master
  149. * identity key and generated for a specific replica number. */
  150. ed25519_public_key_t blinded_pubkey;
  151. /* Revision counter is incremented at each upload, regardless of whether
  152. * the descriptor has changed. This avoids leaking whether the descriptor
  153. * has changed. Spec specifies this as a 8 bytes positive integer. */
  154. uint64_t revision_counter;
  155. /* Decoding only: The b64-decoded superencrypted blob from the descriptor */
  156. uint8_t *superencrypted_blob;
  157. /* Decoding only: Size of the superencrypted_blob */
  158. size_t superencrypted_blob_size;
  159. } hs_desc_plaintext_data_t;
  160. /* Service descriptor in its decoded form. */
  161. typedef struct hs_descriptor_t {
  162. /* Contains the plaintext part of the descriptor. */
  163. hs_desc_plaintext_data_t plaintext_data;
  164. /* The following contains what's in the superencrypted part of the
  165. * descriptor. It's only encrypted in the encoded version of the descriptor
  166. * thus the data contained in that object is in plaintext. */
  167. hs_desc_superencrypted_data_t superencrypted_data;
  168. /* The following contains what's in the encrypted part of the descriptor.
  169. * It's only encrypted in the encoded version of the descriptor thus the
  170. * data contained in that object is in plaintext. */
  171. hs_desc_encrypted_data_t encrypted_data;
  172. /* Subcredentials of a service, used by the client and service to decrypt
  173. * the encrypted data. */
  174. uint8_t subcredential[DIGEST256_LEN];
  175. } hs_descriptor_t;
  176. /* Return true iff the given descriptor format version is supported. */
  177. static inline int
  178. hs_desc_is_supported_version(uint32_t version)
  179. {
  180. if (version < HS_DESC_SUPPORTED_FORMAT_VERSION_MIN ||
  181. version > HS_DESC_SUPPORTED_FORMAT_VERSION_MAX) {
  182. return 0;
  183. }
  184. return 1;
  185. }
  186. /* Public API. */
  187. void hs_descriptor_free_(hs_descriptor_t *desc);
  188. #define hs_descriptor_free(desc) \
  189. FREE_AND_NULL(hs_descriptor_t, hs_descriptor_free_, (desc))
  190. void hs_desc_plaintext_data_free_(hs_desc_plaintext_data_t *desc);
  191. #define hs_desc_plaintext_data_free(desc) \
  192. FREE_AND_NULL(hs_desc_plaintext_data_t, hs_desc_plaintext_data_free_, (desc))
  193. void hs_desc_superencrypted_data_free_(hs_desc_superencrypted_data_t *desc);
  194. #define hs_desc_superencrypted_data_free(desc) \
  195. FREE_AND_NULL(hs_desc_superencrypted_data_t, \
  196. hs_desc_superencrypted_data_free_, (desc))
  197. void hs_desc_encrypted_data_free_(hs_desc_encrypted_data_t *desc);
  198. #define hs_desc_encrypted_data_free(desc) \
  199. FREE_AND_NULL(hs_desc_encrypted_data_t, hs_desc_encrypted_data_free_, (desc))
  200. void hs_descriptor_clear_intro_points(hs_descriptor_t *desc);
  201. MOCK_DECL(int,
  202. hs_desc_encode_descriptor,(const hs_descriptor_t *desc,
  203. const ed25519_keypair_t *signing_kp,
  204. const uint8_t *descriptor_cookie,
  205. char **encoded_out));
  206. int hs_desc_decode_descriptor(const char *encoded,
  207. const uint8_t *subcredential,
  208. const curve25519_secret_key_t *client_auth_sk,
  209. hs_descriptor_t **desc_out);
  210. int hs_desc_decode_plaintext(const char *encoded,
  211. hs_desc_plaintext_data_t *plaintext);
  212. int hs_desc_decode_superencrypted(const hs_descriptor_t *desc,
  213. hs_desc_superencrypted_data_t *desc_out);
  214. int hs_desc_decode_encrypted(const hs_descriptor_t *desc,
  215. const curve25519_secret_key_t *client_auth_sk,
  216. hs_desc_encrypted_data_t *desc_out);
  217. size_t hs_desc_obj_size(const hs_descriptor_t *data);
  218. size_t hs_desc_plaintext_obj_size(const hs_desc_plaintext_data_t *data);
  219. hs_desc_intro_point_t *hs_desc_intro_point_new(void);
  220. void hs_desc_intro_point_free_(hs_desc_intro_point_t *ip);
  221. #define hs_desc_intro_point_free(ip) \
  222. FREE_AND_NULL(hs_desc_intro_point_t, hs_desc_intro_point_free_, (ip))
  223. void hs_desc_authorized_client_free_(hs_desc_authorized_client_t *client);
  224. #define hs_desc_authorized_client_free(client) \
  225. FREE_AND_NULL(hs_desc_authorized_client_t, \
  226. hs_desc_authorized_client_free_, (client))
  227. hs_desc_authorized_client_t *hs_desc_build_fake_authorized_client(void);
  228. void hs_desc_build_authorized_client(const uint8_t *subcredential,
  229. const curve25519_public_key_t *
  230. client_auth_pk,
  231. const curve25519_secret_key_t *
  232. auth_ephemeral_sk,
  233. const uint8_t *descriptor_cookie,
  234. hs_desc_authorized_client_t *client_out);
  235. void hs_desc_plaintext_data_free_contents(hs_desc_plaintext_data_t *desc);
  236. void hs_desc_superencrypted_data_free_contents(
  237. hs_desc_superencrypted_data_t *desc);
  238. void hs_desc_encrypted_data_free_contents(hs_desc_encrypted_data_t *desc);
  239. #ifdef HS_DESCRIPTOR_PRIVATE
  240. /* Encoding. */
  241. STATIC char *encode_link_specifiers(const smartlist_t *specs);
  242. STATIC size_t build_plaintext_padding(const char *plaintext,
  243. size_t plaintext_len,
  244. uint8_t **padded_out);
  245. /* Decoding. */
  246. STATIC smartlist_t *decode_link_specifiers(const char *encoded);
  247. STATIC hs_desc_intro_point_t *decode_introduction_point(
  248. const hs_descriptor_t *desc,
  249. const char *text);
  250. STATIC int encrypted_data_length_is_valid(size_t len);
  251. STATIC int cert_is_valid(tor_cert_t *cert, uint8_t type,
  252. const char *log_obj_type);
  253. STATIC int desc_sig_is_valid(const char *b64_sig,
  254. const ed25519_public_key_t *signing_pubkey,
  255. const char *encoded_desc, size_t encoded_len);
  256. MOCK_DECL(STATIC size_t, decrypt_desc_layer,(const hs_descriptor_t *desc,
  257. const uint8_t *encrypted_blob,
  258. size_t encrypted_blob_size,
  259. const uint8_t *descriptor_cookie,
  260. int is_superencrypted_layer,
  261. char **decrypted_out));
  262. #endif /* defined(HS_DESCRIPTOR_PRIVATE) */
  263. #endif /* !defined(TOR_HS_DESCRIPTOR_H) */