tor  master
hs_service.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_SERVICE_H
10 #define TOR_HS_SERVICE_H
11 
12 #include "crypto_curve25519.h"
13 #include "crypto_ed25519.h"
14 #include "replaycache.h"
15 
16 #include "hs_common.h"
17 #include "hs_descriptor.h"
18 #include "hs_ident.h"
19 #include "hs_intropoint.h"
20 
21 /* Trunnel */
22 #include "hs/cell_establish_intro.h"
23 
24 /* When loading and configuring a service, this is the default version it will
25  * be configured for as it is possible that no HiddenServiceVersion is
26  * present. */
27 #define HS_SERVICE_DEFAULT_VERSION HS_VERSION_TWO
28 
29 /* As described in the specification, service publishes their next descriptor
30  * at a random time between those two values (in seconds). */
31 #define HS_SERVICE_NEXT_UPLOAD_TIME_MIN (60 * 60)
32 #define HS_SERVICE_NEXT_UPLOAD_TIME_MAX (120 * 60)
33 
34 /* Service side introduction point. */
35 typedef struct hs_service_intro_point_t {
36  /* Top level intropoint "shared" data between client/service. */
38 
39  /* Onion key of the introduction point used to extend to it for the ntor
40  * handshake. */
41  curve25519_public_key_t onion_key;
42 
43  /* Authentication keypair used to create the authentication certificate
44  * which is published in the descriptor. */
45  ed25519_keypair_t auth_key_kp;
46 
47  /* Encryption keypair for the "ntor" type. */
48  curve25519_keypair_t enc_key_kp;
49 
50  /* Legacy key if that intro point doesn't support v3. This should be used if
51  * the base object legacy flag is set. */
52  crypto_pk_t *legacy_key;
53  /* Legacy key SHA1 public key digest. This should be used only if the base
54  * object legacy flag is set. */
55  uint8_t legacy_key_digest[DIGEST_LEN];
56 
57  /* Amount of INTRODUCE2 cell accepted from this intro point. */
58  uint64_t introduce2_count;
59 
60  /* Maximum number of INTRODUCE2 cell this intro point should accept. */
61  uint64_t introduce2_max;
62 
63  /* The time at which this intro point should expire and stop being used. */
64  time_t time_to_expire;
65 
66  /* The amount of circuit creation we've made to this intro point. This is
67  * incremented every time we do a circuit relaunch on this intro point which
68  * is triggered when the circuit dies but the node is still in the
69  * consensus. After MAX_INTRO_POINT_CIRCUIT_RETRIES, we give up on it. */
70  uint32_t circuit_retries;
71 
72  /* Set if this intro point has an established circuit. */
73  unsigned int circuit_established : 1;
74 
75  /* Replay cache recording the encrypted part of an INTRODUCE2 cell that the
76  * circuit associated with this intro point has received. This is used to
77  * prevent replay attacks. */
78  replaycache_t *replay_cache;
80 
81 /* Object handling introduction points of a service. */
82 typedef struct hs_service_intropoints_t {
83  /* The time at which we've started our retry period to build circuits. We
84  * don't want to stress circuit creation so we can only retry for a certain
85  * time and then after we stop and wait. */
86  time_t retry_period_started;
87 
88  /* Number of circuit we've launched during a single retry period. */
89  unsigned int num_circuits_launched;
90 
91  /* Contains the current hs_service_intro_point_t objects indexed by
92  * authentication public key. */
93  digest256map_t *map;
94 
95  /* Contains node's identity key digest that were introduction point for this
96  * descriptor but were retried to many times. We keep those so we avoid
97  * re-picking them over and over for a circuit retry period.
98  * XXX: Once we have #22173, change this to only use ed25519 identity. */
99  digestmap_t *failed_id;
101 
102 /* Representation of a service descriptor. */
103 typedef struct hs_service_descriptor_t {
104  /* Decoded descriptor. This object is used for encoding when the service
105  * publishes the descriptor. */
106  hs_descriptor_t *desc;
107 
108  /* Descriptor signing keypair. */
109  ed25519_keypair_t signing_kp;
110 
111  /* Blinded keypair derived from the master identity public key. */
112  ed25519_keypair_t blinded_kp;
113 
114  /* When is the next time when we should upload the descriptor. */
115  time_t next_upload_time;
116 
117  /* Introduction points assign to this descriptor which contains
118  * hs_service_intropoints_t object indexed by authentication key (the RSA
119  * key if the node is legacy). */
120  hs_service_intropoints_t intro_points;
121 
122  /* The time period number this descriptor has been created for. */
123  uint64_t time_period_num;
124 
125  /* True iff we have missing intro points for this descriptor because we
126  * couldn't pick any nodes. */
127  unsigned int missing_intro_points : 1;
128 
135 
136 /* Service key material. */
137 typedef struct hs_service_keys_t {
138  /* Master identify public key. */
139  ed25519_public_key_t identity_pk;
140  /* Master identity private key. */
141  ed25519_secret_key_t identity_sk;
142  /* True iff the key is kept offline which means the identity_sk MUST not be
143  * used in that case. */
144  unsigned int is_identify_key_offline : 1;
146 
147 /* Service configuration. The following are set from the torrc options either
148  * set by the configuration file or by the control port. Nothing else should
149  * change those values. */
150 typedef struct hs_service_config_t {
151  /* Protocol version of the service. Specified by HiddenServiceVersion
152  * option. */
153  uint32_t version;
154 
155  /* List of rend_service_port_config_t */
156  smartlist_t *ports;
157 
158  /* Path on the filesystem where the service persistent data is stored. NULL
159  * if the service is ephemeral. Specified by HiddenServiceDir option. */
160  char *directory_path;
161 
162  /* The maximum number of simultaneous streams per rendezvous circuit that
163  * are allowed to be created. No limit if 0. Specified by
164  * HiddenServiceMaxStreams option. */
165  uint64_t max_streams_per_rdv_circuit;
166 
167  /* If true, we close circuits that exceed the max_streams_per_rdv_circuit
168  * limit. Specified by HiddenServiceMaxStreamsCloseCircuit option. */
169  unsigned int max_streams_close_circuit : 1;
170 
171  /* How many introduction points this service has. Specified by
172  * HiddenServiceNumIntroductionPoints option. */
173  unsigned int num_intro_points;
174 
175  /* True iff we allow request made on unknown ports. Specified by
176  * HiddenServiceAllowUnknownPorts option. */
177  unsigned int allow_unknown_ports : 1;
178 
179  /* If true, this service is a Single Onion Service. Specified by
180  * HiddenServiceSingleHopMode and HiddenServiceNonAnonymousMode options. */
181  unsigned int is_single_onion : 1;
182 
183  /* If true, allow group read permissions on the directory_path. Specified by
184  * HiddenServiceDirGroupReadable option. */
185  unsigned int dir_group_readable : 1;
186 
187  /* Is this service ephemeral? */
188  unsigned int is_ephemeral : 1;
190 
191 /* Service state. */
192 typedef struct hs_service_state_t {
193  /* The time at which we've started our retry period to build circuits. We
194  * don't want to stress circuit creation so we can only retry for a certain
195  * time and then after we stop and wait. */
196  time_t intro_circ_retry_started_time;
197 
198  /* Number of circuit we've launched during a single retry period. This
199  * should never go over MAX_INTRO_CIRCS_PER_PERIOD. */
200  unsigned int num_intro_circ_launched;
201 
202  /* Replay cache tracking the REND_COOKIE found in INTRODUCE2 cell to detect
203  * repeats. Clients may send INTRODUCE1 cells for the same rendezvous point
204  * through two or more different introduction points; when they do, this
205  * keeps us from launching multiple simultaneous attempts to connect to the
206  * same rend point. */
207  replaycache_t *replay_cache_rend_cookie;
208 
209  /* When is the next time we should rotate our descriptors. This is has to be
210  * done at the start time of the next SRV protocol run. */
211  time_t next_rotation_time;
213 
214 /* Representation of a service running on this tor instance. */
215 typedef struct hs_service_t {
216  /* Onion address base32 encoded and NUL terminated. We keep it for logging
217  * purposes so we don't have to build it everytime. */
218  char onion_address[HS_SERVICE_ADDR_LEN_BASE32 + 1];
219 
220  /* Hashtable node: use to look up the service by its master public identity
221  * key in the service global map. */
222  HT_ENTRY(hs_service_t) hs_service_node;
223 
224  /* Service state which contains various flags and counters. */
225  hs_service_state_t state;
226 
227  /* Key material of the service. */
228  hs_service_keys_t keys;
229 
230  /* Configuration of the service. */
231  hs_service_config_t config;
232 
233  /* Current descriptor. */
234  hs_service_descriptor_t *desc_current;
235  /* Next descriptor. */
236  hs_service_descriptor_t *desc_next;
237 
238  /* XXX: Credential (client auth.) #20700. */
239 
240 } hs_service_t;
241 
242 /* For the service global hash map, we define a specific type for it which
243  * will make it safe to use and specific to some controlled parameters such as
244  * the hashing function and how to compare services. */
245 typedef HT_HEAD(hs_service_ht, hs_service_t) hs_service_ht;
246 
247 /* API */
248 
249 /* Global initializer and cleanup function. */
250 void hs_service_init(void);
251 void hs_service_free_all(void);
252 
253 /* Service new/free functions. */
254 hs_service_t *hs_service_new(const or_options_t *options);
255 void hs_service_free_(hs_service_t *service);
256 #define hs_service_free(s) FREE_AND_NULL(hs_service_t, hs_service_free_, (s))
257 
258 unsigned int hs_service_get_num_services(void);
259 void hs_service_stage_services(const smartlist_t *service_list);
260 int hs_service_load_all_keys(void);
261 void hs_service_lists_fnames_for_sandbox(smartlist_t *file_list,
262  smartlist_t *dir_list);
263 int hs_service_set_conn_addr_port(const origin_circuit_t *circ,
264  edge_connection_t *conn);
265 
266 void hs_service_map_has_changed(void);
267 void hs_service_dir_info_changed(void);
268 void hs_service_run_scheduled_events(time_t now);
269 void hs_service_circuit_has_opened(origin_circuit_t *circ);
270 int hs_service_receive_intro_established(origin_circuit_t *circ,
271  const uint8_t *payload,
272  size_t payload_len);
273 int hs_service_receive_introduce2(origin_circuit_t *circ,
274  const uint8_t *payload,
275  size_t payload_len);
276 
277 void hs_service_intro_circ_has_closed(origin_circuit_t *circ);
278 
279 char *hs_service_lookup_current_desc(const ed25519_public_key_t *pk);
280 
282 hs_service_add_ephemeral(ed25519_secret_key_t *sk, smartlist_t *ports,
283  int max_streams_per_rdv_circuit,
284  int max_streams_close_circuit, char **address_out);
285 int hs_service_del_ephemeral(const char *address);
286 
287 /* Used outside of the HS subsystem by the control port command HSPOST. */
288 void hs_service_upload_desc_to_dir(const char *encoded_desc,
289  const uint8_t version,
290  const ed25519_public_key_t *identity_pk,
291  const ed25519_public_key_t *blinded_pk,
292  const routerstatus_t *hsdir_rs);
293 
294 #ifdef HS_SERVICE_PRIVATE
295 
296 #ifdef TOR_UNIT_TESTS
297 /* Useful getters for unit tests. */
298 STATIC unsigned int get_hs_service_map_size(void);
299 STATIC int get_hs_service_staging_list_size(void);
300 STATIC hs_service_ht *get_hs_service_map(void);
301 STATIC hs_service_t *get_first_service(void);
302 STATIC hs_service_intro_point_t *service_intro_point_find_by_ident(
303  const hs_service_t *service,
304  const hs_ident_circuit_t *ident);
305 #endif
306 
307 /* Service accessors. */
308 STATIC hs_service_t *find_service(hs_service_ht *map,
309  const ed25519_public_key_t *pk);
310 STATIC void remove_service(hs_service_ht *map, hs_service_t *service);
311 STATIC int register_service(hs_service_ht *map, hs_service_t *service);
312 /* Service introduction point functions. */
313 STATIC hs_service_intro_point_t *service_intro_point_new(
314  const extend_info_t *ei,
315  unsigned int is_legacy);
316 STATIC void service_intro_point_free_(hs_service_intro_point_t *ip);
317 #define service_intro_point_free(ip) \
318  FREE_AND_NULL(hs_service_intro_point_t, \
319  service_intro_point_free_, (ip))
320 STATIC void service_intro_point_add(digest256map_t *map,
322 STATIC void service_intro_point_remove(const hs_service_t *service,
323  const hs_service_intro_point_t *ip);
324 STATIC hs_service_intro_point_t *service_intro_point_find(
325  const hs_service_t *service,
326  const ed25519_public_key_t *auth_key);
327 /* Service descriptor functions. */
328 STATIC hs_service_descriptor_t *service_descriptor_new(void);
329 STATIC hs_service_descriptor_t *service_desc_find_by_intro(
330  const hs_service_t *service,
331  const hs_service_intro_point_t *ip);
332 /* Helper functions. */
333 STATIC void get_objects_from_ident(const hs_ident_circuit_t *ident,
334  hs_service_t **service,
336  hs_service_descriptor_t **desc);
337 STATIC const node_t *
338 get_node_from_intro_point(const hs_service_intro_point_t *ip);
339 STATIC int can_service_launch_intro_circuit(hs_service_t *service,
340  time_t now);
341 STATIC int intro_point_should_expire(const hs_service_intro_point_t *ip,
342  time_t now);
343 STATIC void run_housekeeping_event(time_t now);
344 STATIC void rotate_all_descriptors(time_t now);
345 STATIC void build_all_descriptors(time_t now);
346 STATIC void update_all_descriptors(time_t now);
347 STATIC void run_upload_descriptor_event(time_t now);
348 
349 STATIC char *
351 
352 STATIC void service_descriptor_free_(hs_service_descriptor_t *desc);
353 #define service_descriptor_free(d) \
354  FREE_AND_NULL(hs_service_descriptor_t, \
355  service_descriptor_free_, (d))
356 
357 STATIC uint64_t
358 check_state_line_for_service_rev_counter(const char *state_line,
359  const ed25519_public_key_t *blinded_pubkey,
360  int *service_found_out);
361 
362 STATIC int
363 write_address_to_file(const hs_service_t *service, const char *fname_);
364 
365 STATIC void upload_descriptor_to_all(const hs_service_t *service,
367 
369  time_t now,
370  int descriptor_changed);
371 
372 STATIC int service_desc_hsdirs_changed(const hs_service_t *service,
373  const hs_service_descriptor_t *desc);
374 
375 #endif /* defined(HS_SERVICE_PRIVATE) */
376 
377 #endif /* !defined(TOR_HS_SERVICE_H) */
378 
smartlist_t * previous_hsdirs
Definition: hs_service.h:133
Definition: crypto_ed25519.h:39
Definition: hs_service.h:35
Definition: or.h:3657
Definition: hs_intropoint.h:32
Header file containing common data for the whole HS subsytem.
#define DIGEST_LEN
Definition: crypto_digest.h:22
Definition: or.h:2504
Definition: crypto_ed25519.h:23
Definition: hs_service.h:137
Definition: hs_descriptor.h:185
STATIC int service_desc_hsdirs_changed(const hs_service_t *service, const hs_service_descriptor_t *desc)
Definition: hs_service.c:2586
Header file for replaycache.c.
Definition: hs_service.h:103
Definition: container.h:18
Definition: hs_ident.h:42
STATIC void upload_descriptor_to_all(const hs_service_t *service, hs_service_descriptor_t *desc)
Definition: hs_service.c:2526
Header file containing circuit and connection identifier data for the whole HS subsytem.
hs_service_add_ephemeral_status_t
Definition: hs_common.h:135
Definition: crypto_curve25519.h:38
Header file for hs_descriptor.c.
Header file for hs_intropoint.c.
Definition: base.py:1
STATIC char * encode_desc_rev_counter_for_state(const hs_service_descriptor_t *desc)
Definition: hs_service.c:2334
STATIC void service_desc_schedule_upload(hs_service_descriptor_t *desc, time_t now, int descriptor_changed)
Definition: hs_service.c:1736
Definition: crypto_curve25519.h:24
Definition: crypto_ed25519.h:28
Definition: crypto_rsa.c:41
Definition: hs_service.h:215
STATIC uint64_t check_state_line_for_service_rev_counter(const char *state_line, const ed25519_public_key_t *blinded_pubkey, int *service_found_out)
Definition: hs_service.c:2396
Definition: hs_service.h:192
Definition: hs_service.h:82
Definition: or.h:2344
Definition: or.h:2838
Definition: hs_service.h:150
Definition: or.h:1687
Definition: or.h:3256