hs_descriptor.h 10.0 KB

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