hs_descriptor.h 12 KB

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