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