hs_descriptor.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /* Lifetime of certificate in the descriptor. This defines the lifetime of the
  20. * descriptor signing key and the cross certification cert of that key. */
  21. #define HS_DESC_CERT_LIFETIME (24 * 60 * 60)
  22. /* Length of the salt needed for the encrypted section of a descriptor. */
  23. #define HS_DESC_ENCRYPTED_SALT_LEN 16
  24. /* Length of the secret input needed for the KDF construction which derives
  25. * the encryption key for the encrypted data section of the descriptor. This
  26. * adds up to 68 bytes being the blinded key, hashed subcredential and
  27. * revision counter. */
  28. #define HS_DESC_ENCRYPTED_SECRET_INPUT_LEN \
  29. ED25519_PUBKEY_LEN + DIGEST256_LEN + sizeof(uint64_t)
  30. /* Length of the KDF output value which is the length of the secret key,
  31. * the secret IV and MAC key length which is the length of H() output. */
  32. #define HS_DESC_ENCRYPTED_KDF_OUTPUT_LEN \
  33. CIPHER_KEY_LEN + CIPHER_IV_LEN + DIGEST256_LEN
  34. /* We need to pad the plaintext version of the encrypted data section before
  35. * encryption and it has to be a multiple of this value. */
  36. #define HS_DESC_PLAINTEXT_PADDING_MULTIPLE 128
  37. /* XXX: Let's make sure this makes sense as an upper limit for the padded
  38. * plaintext section. Then we should enforce it as now only an assert will be
  39. * triggered if we are above it. */
  40. /* Once padded, this is the maximum length in bytes for the plaintext. */
  41. #define HS_DESC_PADDED_PLAINTEXT_MAX_LEN 8192
  42. /* Type of encryption key in the descriptor. */
  43. typedef enum {
  44. HS_DESC_KEY_TYPE_LEGACY = 1,
  45. HS_DESC_KEY_TYPE_CURVE25519 = 2,
  46. } hs_desc_key_type_t;
  47. /* Link specifier object that contains information on how to extend to the
  48. * relay that is the address, port and handshake type. */
  49. typedef struct hs_desc_link_specifier_t {
  50. /* Indicate the type of link specifier. See trunnel ed25519_cert
  51. * specification. */
  52. uint8_t type;
  53. /* It's either an address/port or a legacy identity fingerprint. */
  54. union {
  55. /* IP address and port of the relay use to extend. */
  56. tor_addr_port_t ap;
  57. /* Legacy identity. A 20-byte SHA1 identity fingerprint. */
  58. uint8_t legacy_id[DIGEST_LEN];
  59. } u;
  60. } hs_desc_link_specifier_t;
  61. /* Introduction point information located in a descriptor. */
  62. typedef struct hs_desc_intro_point_t {
  63. /* Link specifier(s) which details how to extend to the relay. This list
  64. * contains hs_desc_link_specifier_t object. It MUST have at least one. */
  65. smartlist_t *link_specifiers;
  66. /* Authentication key used to establish the introduction point circuit and
  67. * cross-certifies the blinded public key for the replica thus signed by
  68. * the blinded key and in turn signs it. */
  69. tor_cert_t *auth_key_cert;
  70. /* Encryption key type so we know which one to use in the union below. */
  71. hs_desc_key_type_t enc_key_type;
  72. /* Keys are mutually exclusive thus the union. */
  73. union {
  74. /* Encryption key used to encrypt request to hidden service. */
  75. curve25519_keypair_t curve25519;
  76. /* Backward compat: RSA 1024 encryption key for legacy purposes.
  77. * Mutually exclusive with enc_key. */
  78. crypto_pk_t *legacy;
  79. } enc_key;
  80. } hs_desc_intro_point_t;
  81. /* The encrypted data section of a descriptor. Obviously the data in this is
  82. * in plaintext but encrypted once encoded. */
  83. typedef struct hs_desc_encrypted_data_t {
  84. /* Bitfield of CREATE2 cell supported formats. The only currently supported
  85. * format is ntor. */
  86. unsigned int create2_ntor : 1;
  87. /* A list of authentication types that a client must at least support one
  88. * in order to contact the service. Contains NULL terminated strings. */
  89. smartlist_t *auth_types;
  90. /* A list of intro points. Contains hs_desc_intro_point_t objects. */
  91. smartlist_t *intro_points;
  92. } hs_desc_encrypted_data_t;
  93. /* Plaintext data that is unencrypted information of the descriptor. */
  94. typedef struct hs_desc_plaintext_data_t {
  95. /* Version of the descriptor format. Spec specifies this field as a
  96. * positive integer. */
  97. uint32_t version;
  98. /* The lifetime of the descriptor in seconds. */
  99. uint32_t lifetime_sec;
  100. /* Certificate with the short-term ed22519 descriptor signing key for the
  101. * replica which is signed by the blinded public key for that replica. */
  102. tor_cert_t *signing_key_cert;
  103. /* Signing keypair which is used to sign the descriptor. Same public key
  104. * as in the signing key certificate. */
  105. ed25519_keypair_t signing_kp;
  106. /* Blinded keypair used for this descriptor derived from the master
  107. * identity key and generated for a specific replica number. */
  108. ed25519_keypair_t blinded_kp;
  109. /* Revision counter is incremented at each upload, regardless of whether
  110. * the descriptor has changed. This avoids leaking whether the descriptor
  111. * has changed. Spec specifies this as a 8 bytes positive integer. */
  112. uint64_t revision_counter;
  113. } hs_desc_plaintext_data_t;
  114. /* Service descriptor in its decoded form. */
  115. typedef struct hs_descriptor_t {
  116. /* Contains the plaintext part of the descriptor. */
  117. hs_desc_plaintext_data_t plaintext_data;
  118. /* The following contains what's in the encrypted part of the descriptor.
  119. * It's only encrypted in the encoded version of the descriptor thus the
  120. * data contained in that object is in plaintext. */
  121. hs_desc_encrypted_data_t encrypted_data;
  122. /* Subcredentials of a service, used by the client and service to decrypt
  123. * the encrypted data. */
  124. uint8_t subcredential[DIGEST256_LEN];
  125. } hs_descriptor_t;
  126. /* Return true iff the given descriptor format version is supported. */
  127. static inline int
  128. hs_desc_is_supported_version(uint32_t version)
  129. {
  130. if (version < HS_DESC_SUPPORTED_FORMAT_VERSION_MIN ||
  131. version > HS_DESC_SUPPORTED_FORMAT_VERSION_MAX) {
  132. return 0;
  133. }
  134. return 1;
  135. }
  136. /* Public API. */
  137. int hs_desc_encode_descriptor(const hs_descriptor_t *desc,
  138. char **encoded_out);
  139. #endif /* TOR_HS_DESCRIPTOR_H */