hs_descriptor.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Copyright (c) 2016, 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 "address.h"
  11. #include "container.h"
  12. #include "crypto.h"
  13. #include "crypto_ed25519.h"
  14. #include "torcert.h"
  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. /* Maximum lifetime of a descriptor in seconds. The value is set at 12 hours
  20. * which is 720 minutes or 43200 seconds. */
  21. #define HS_DESC_MAX_LIFETIME (12 * 60 * 60)
  22. /* Lifetime of certificate in the descriptor. This defines the lifetime of the
  23. * descriptor signing key and the cross certification cert of that key. */
  24. #define HS_DESC_CERT_LIFETIME (24 * 60 * 60)
  25. /* Length of the salt needed for the encrypted section of a descriptor. */
  26. #define HS_DESC_ENCRYPTED_SALT_LEN 16
  27. /* Length of the secret input needed for the KDF construction which derives
  28. * the encryption key for the encrypted data section of the descriptor. This
  29. * adds up to 68 bytes being the blinded key, hashed subcredential and
  30. * revision counter. */
  31. #define HS_DESC_ENCRYPTED_SECRET_INPUT_LEN \
  32. ED25519_PUBKEY_LEN + DIGEST256_LEN + sizeof(uint64_t)
  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. CIPHER_KEY_LEN + CIPHER_IV_LEN + DIGEST256_LEN
  37. /* We need to pad the plaintext version of the encrypted data section before
  38. * encryption and it has to be a multiple of this value. */
  39. #define HS_DESC_PLAINTEXT_PADDING_MULTIPLE 128
  40. /* XXX: Let's make sure this makes sense as an upper limit for the padded
  41. * plaintext section. Then we should enforce it as now only an assert will be
  42. * triggered if we are above it. */
  43. /* Once padded, this is the maximum length in bytes for the plaintext. */
  44. #define HS_DESC_PADDED_PLAINTEXT_MAX_LEN 8192
  45. /* Minimum length in bytes of the encrypted portion of the descriptor. */
  46. #define HS_DESC_ENCRYPTED_MIN_LEN \
  47. HS_DESC_ENCRYPTED_SALT_LEN + \
  48. HS_DESC_PLAINTEXT_PADDING_MULTIPLE + DIGEST256_LEN
  49. /* Maximum length in bytes of a full hidden service descriptor. */
  50. #define HS_DESC_MAX_LEN 32768 // XXX justify
  51. /* The minimum amount of fields a descriptor should contain. The parsing of
  52. * the fields are version specific so the only required field, as a generic
  53. * view of a descriptor, is 1 that is the version field. */
  54. #define HS_DESC_PLAINTEXT_MIN_FIELDS 1
  55. /* Type of authentication in the descriptor. */
  56. typedef enum {
  57. HS_DESC_AUTH_PASSWORD = 1,
  58. HS_DESC_AUTH_ED25519 = 2,
  59. } hs_desc_auth_type_t;
  60. /* Type of encryption key in the descriptor. */
  61. typedef enum {
  62. HS_DESC_KEY_TYPE_LEGACY = 1,
  63. HS_DESC_KEY_TYPE_CURVE25519 = 2,
  64. } hs_desc_key_type_t;
  65. /* Link specifier object that contains information on how to extend to the
  66. * relay that is the address, port and handshake type. */
  67. typedef struct hs_desc_link_specifier_t {
  68. /* Indicate the type of link specifier. See trunnel ed25519_cert
  69. * specification. */
  70. uint8_t type;
  71. /* It's either an address/port or a legacy identity fingerprint. */
  72. union {
  73. /* IP address and port of the relay use to extend. */
  74. tor_addr_port_t ap;
  75. /* Legacy identity. A 20-byte SHA1 identity fingerprint. */
  76. uint8_t legacy_id[DIGEST_LEN];
  77. } u;
  78. } hs_desc_link_specifier_t;
  79. /* Introduction point information located in a descriptor. */
  80. typedef struct hs_desc_intro_point_t {
  81. /* Link specifier(s) which details how to extend to the relay. This list
  82. * contains hs_desc_link_specifier_t object. It MUST have at least one. */
  83. smartlist_t *link_specifiers;
  84. /* Authentication key used to establish the introduction point circuit and
  85. * cross-certifies the blinded public key for the replica thus signed by
  86. * the blinded key and in turn signs it. */
  87. tor_cert_t *auth_key_cert;
  88. /* Encryption key type so we know which one to use in the union below. */
  89. hs_desc_key_type_t enc_key_type;
  90. /* Keys are mutually exclusive thus the union. */
  91. union {
  92. /* Encryption key used to encrypt request to hidden service. */
  93. curve25519_keypair_t curve25519;
  94. /* Backward compat: RSA 1024 encryption key for legacy purposes.
  95. * Mutually exclusive with enc_key. */
  96. crypto_pk_t *legacy;
  97. } enc_key;
  98. /* True iff the introduction point has passed the cross certification. Upon
  99. * decoding an intro point, this must be true. */
  100. unsigned int cross_certified : 1;
  101. } hs_desc_intro_point_t;
  102. /* The encrypted data section of a descriptor. Obviously the data in this is
  103. * in plaintext but encrypted once encoded. */
  104. typedef struct hs_desc_encrypted_data_t {
  105. /* Bitfield of CREATE2 cell supported formats. The only currently supported
  106. * format is ntor. */
  107. unsigned int create2_ntor : 1;
  108. /* A list of authentication types that a client must at least support one
  109. * in order to contact the service. Contains NULL terminated strings. */
  110. smartlist_t *auth_types;
  111. /* A list of intro points. Contains hs_desc_intro_point_t objects. */
  112. smartlist_t *intro_points;
  113. } hs_desc_encrypted_data_t;
  114. /* Plaintext data that is unencrypted information of the descriptor. */
  115. typedef struct hs_desc_plaintext_data_t {
  116. /* Version of the descriptor format. Spec specifies this field as a
  117. * positive integer. */
  118. uint32_t version;
  119. /* The lifetime of the descriptor in seconds. */
  120. uint32_t lifetime_sec;
  121. /* Certificate with the short-term ed22519 descriptor signing key for the
  122. * replica which is signed by the blinded public key for that replica. */
  123. tor_cert_t *signing_key_cert;
  124. /* Signing keypair which is used to sign the descriptor. Same public key
  125. * as in the signing key certificate. */
  126. ed25519_keypair_t signing_kp;
  127. /* Blinded keypair used for this descriptor derived from the master
  128. * identity key and generated for a specific replica number. */
  129. ed25519_keypair_t blinded_kp;
  130. /* Revision counter is incremented at each upload, regardless of whether
  131. * the descriptor has changed. This avoids leaking whether the descriptor
  132. * has changed. Spec specifies this as a 8 bytes positive integer. */
  133. uint64_t revision_counter;
  134. /* Decoding only: The base64-decoded encrypted blob from the descriptor */
  135. uint8_t *encrypted_blob;
  136. /* Decoding only: Size of the encrypted_blob */
  137. size_t encrypted_blob_size;
  138. } hs_desc_plaintext_data_t;
  139. /* Service descriptor in its decoded form. */
  140. typedef struct hs_descriptor_t {
  141. /* Contains the plaintext part of the descriptor. */
  142. hs_desc_plaintext_data_t plaintext_data;
  143. /* The following contains what's in the encrypted part of the descriptor.
  144. * It's only encrypted in the encoded version of the descriptor thus the
  145. * data contained in that object is in plaintext. */
  146. hs_desc_encrypted_data_t encrypted_data;
  147. /* Subcredentials of a service, used by the client and service to decrypt
  148. * the encrypted data. */
  149. uint8_t subcredential[DIGEST256_LEN];
  150. } hs_descriptor_t;
  151. /* Return true iff the given descriptor format version is supported. */
  152. static inline int
  153. hs_desc_is_supported_version(uint32_t version)
  154. {
  155. if (version < HS_DESC_SUPPORTED_FORMAT_VERSION_MIN ||
  156. version > HS_DESC_SUPPORTED_FORMAT_VERSION_MAX) {
  157. return 0;
  158. }
  159. return 1;
  160. }
  161. /* Public API. */
  162. void hs_descriptor_free(hs_descriptor_t *desc);
  163. void hs_desc_plaintext_data_free(hs_desc_plaintext_data_t *desc);
  164. void hs_desc_encrypted_data_free(hs_desc_encrypted_data_t *desc);
  165. int hs_desc_encode_descriptor(const hs_descriptor_t *desc,
  166. char **encoded_out);
  167. int hs_desc_decode_descriptor(const char *encoded,
  168. const uint8_t *subcredential,
  169. hs_descriptor_t **desc_out);
  170. int hs_desc_decode_plaintext(const char *encoded,
  171. hs_desc_plaintext_data_t *plaintext);
  172. int hs_desc_decode_encrypted(const hs_descriptor_t *desc,
  173. hs_desc_encrypted_data_t *desc_out);
  174. size_t hs_desc_plaintext_obj_size(const hs_desc_plaintext_data_t *data);
  175. #ifdef HS_DESCRIPTOR_PRIVATE
  176. /* Encoding. */
  177. STATIC int encode_cert(const tor_cert_t *cert, char **cert_str_out);
  178. STATIC char *encode_link_specifiers(const smartlist_t *specs);
  179. STATIC size_t build_plaintext_padding(const char *plaintext,
  180. size_t plaintext_len,
  181. uint8_t **padded_out);
  182. /* Decoding. */
  183. STATIC smartlist_t *decode_link_specifiers(const char *encoded);
  184. STATIC hs_desc_intro_point_t *decode_introduction_point(
  185. const hs_descriptor_t *desc,
  186. const char *text);
  187. STATIC int decode_intro_points(const hs_descriptor_t *desc,
  188. hs_desc_encrypted_data_t *desc_enc,
  189. const char *data);
  190. STATIC int encrypted_data_length_is_valid(size_t len);
  191. STATIC int cert_is_valid(tor_cert_t *cert, uint8_t type,
  192. const char *log_obj_type);
  193. STATIC int desc_sig_is_valid(const char *b64_sig,
  194. const ed25519_keypair_t *signing_kp,
  195. const char *encoded_desc, size_t encoded_len);
  196. #endif /* HS_DESCRIPTOR_PRIVATE */
  197. #endif /* TOR_HS_DESCRIPTOR_H */