hs_descriptor.h 14 KB

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