tor  master
hs_common.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_COMMON_H
10 #define TOR_HS_COMMON_H
11 
12 #include "or.h"
13 
14 /* Trunnel */
15 #include "ed25519_cert.h"
16 
17 /* Protocol version 2. Use this instead of hardcoding "2" in the code base,
18  * this adds a clearer semantic to the value when used. */
19 #define HS_VERSION_TWO 2
20 /* Version 3 of the protocol (prop224). */
21 #define HS_VERSION_THREE 3
22 /* Earliest and latest version we support. */
23 #define HS_VERSION_MIN HS_VERSION_TWO
24 #define HS_VERSION_MAX HS_VERSION_THREE
25 
27 #define NUM_INTRO_POINTS_DEFAULT 3
28 
29 #define NUM_INTRO_POINTS_MAX 10
30 
32 #define NUM_INTRO_POINTS_EXTRA 2
33 
35 #define INTRO_CIRC_RETRY_PERIOD (60*5)
36 
38 #define MAX_INTRO_CIRCS_PER_PERIOD 10
39 
41 #define MAX_REND_FAILURES 1
42 
44 #define MAX_REND_TIMEOUT 30
45 
46 /* String prefix for the signature of ESTABLISH_INTRO */
47 #define ESTABLISH_INTRO_SIG_PREFIX "Tor establish-intro cell v1"
48 
49 /* The default HS time period length */
50 #define HS_TIME_PERIOD_LENGTH_DEFAULT 1440 /* 1440 minutes == one day */
51 /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
52 #define HS_TIME_PERIOD_LENGTH_MIN 30 /* minutes */
53 /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
54 #define HS_TIME_PERIOD_LENGTH_MAX (60 * 24 * 10) /* 10 days or 14400 minutes */
55 
56 /* Prefix of the onion address checksum. */
57 #define HS_SERVICE_ADDR_CHECKSUM_PREFIX ".onion checksum"
58 /* Length of the checksum prefix minus the NUL terminated byte. */
59 #define HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN \
60  (sizeof(HS_SERVICE_ADDR_CHECKSUM_PREFIX) - 1)
61 /* Length of the resulting checksum of the address. The construction of this
62  * checksum looks like:
63  * CHECKSUM = ".onion checksum" || PUBKEY || VERSION
64  * where VERSION is 1 byte. This is pre-hashing. */
65 #define HS_SERVICE_ADDR_CHECKSUM_INPUT_LEN \
66  (HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN + ED25519_PUBKEY_LEN + sizeof(uint8_t))
67 /* The amount of bytes we use from the address checksum. */
68 #define HS_SERVICE_ADDR_CHECKSUM_LEN_USED 2
69 /* Length of the binary encoded service address which is of course before the
70  * base32 encoding. Construction is:
71  * PUBKEY || CHECKSUM || VERSION
72  * with 1 byte VERSION and 2 bytes CHECKSUM. The following is 35 bytes. */
73 #define HS_SERVICE_ADDR_LEN \
74  (ED25519_PUBKEY_LEN + HS_SERVICE_ADDR_CHECKSUM_LEN_USED + sizeof(uint8_t))
75 /* Length of 'y' portion of 'y.onion' URL. This is base32 encoded and the
76  * length ends up to 56 bytes (not counting the terminated NUL byte.) */
77 #define HS_SERVICE_ADDR_LEN_BASE32 \
78  (CEIL_DIV(HS_SERVICE_ADDR_LEN * 8, 5))
79 
80 /* The default HS time period length */
81 #define HS_TIME_PERIOD_LENGTH_DEFAULT 1440 /* 1440 minutes == one day */
82 /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
83 #define HS_TIME_PERIOD_LENGTH_MIN 30 /* minutes */
84 /* The minimum time period length as seen in prop224 section [TIME-PERIODS] */
85 #define HS_TIME_PERIOD_LENGTH_MAX (60 * 24 * 10) /* 10 days or 14400 minutes */
86 /* The time period rotation offset as seen in prop224 section [TIME-PERIODS] */
87 #define HS_TIME_PERIOD_ROTATION_OFFSET (12 * 60) /* minutes */
88 
89 /* Keyblinding parameter construction is as follow:
90  * "key-blind" || INT_8(period_num) || INT_8(start_period_sec) */
91 #define HS_KEYBLIND_NONCE_PREFIX "key-blind"
92 #define HS_KEYBLIND_NONCE_PREFIX_LEN (sizeof(HS_KEYBLIND_NONCE_PREFIX) - 1)
93 #define HS_KEYBLIND_NONCE_LEN \
94  (HS_KEYBLIND_NONCE_PREFIX_LEN + sizeof(uint64_t) + sizeof(uint64_t))
95 
96 /* Credential and subcredential prefix value. */
97 #define HS_CREDENTIAL_PREFIX "credential"
98 #define HS_CREDENTIAL_PREFIX_LEN (sizeof(HS_CREDENTIAL_PREFIX) - 1)
99 #define HS_SUBCREDENTIAL_PREFIX "subcredential"
100 #define HS_SUBCREDENTIAL_PREFIX_LEN (sizeof(HS_SUBCREDENTIAL_PREFIX) - 1)
101 
102 /* Node hidden service stored at index prefix value. */
103 #define HS_INDEX_PREFIX "store-at-idx"
104 #define HS_INDEX_PREFIX_LEN (sizeof(HS_INDEX_PREFIX) - 1)
105 
106 /* Node hidden service directory index prefix value. */
107 #define HSDIR_INDEX_PREFIX "node-idx"
108 #define HSDIR_INDEX_PREFIX_LEN (sizeof(HSDIR_INDEX_PREFIX) - 1)
109 
110 /* Prefix of the shared random value disaster mode. */
111 #define HS_SRV_DISASTER_PREFIX "shared-random-disaster"
112 #define HS_SRV_DISASTER_PREFIX_LEN (sizeof(HS_SRV_DISASTER_PREFIX) - 1)
113 
114 /* Default value of number of hsdir replicas (hsdir_n_replicas). */
115 #define HS_DEFAULT_HSDIR_N_REPLICAS 2
116 /* Default value of hsdir spread store (hsdir_spread_store). */
117 #define HS_DEFAULT_HSDIR_SPREAD_STORE 4
118 /* Default value of hsdir spread fetch (hsdir_spread_fetch). */
119 #define HS_DEFAULT_HSDIR_SPREAD_FETCH 3
120 
121 /* The size of a legacy RENDEZVOUS1 cell which adds up to 168 bytes. It is
122  * bigger than the 84 bytes needed for version 3 so we need to pad up to that
123  * length so it is indistinguishable between versions. */
124 #define HS_LEGACY_RENDEZVOUS_CELL_SIZE \
125  (REND_COOKIE_LEN + DH_KEY_LEN + DIGEST_LEN)
126 
127 /* Type of authentication key used by an introduction point. */
128 typedef enum {
129  HS_AUTH_KEY_TYPE_LEGACY = 1,
130  HS_AUTH_KEY_TYPE_ED25519 = 2,
131 } hs_auth_key_type_t;
132 
133 /* Return value when adding an ephemeral service through the ADD_ONION
134  * control port command. Both v2 and v3 share these. */
135 typedef enum {
143 
144 /* Represents the mapping from a virtual port of a rendezvous service to a
145  * real port on some IP. */
147  /* The incoming HS virtual port we're mapping */
148  uint16_t virtual_port;
149  /* Is this an AF_UNIX port? */
150  unsigned int is_unix_addr:1;
151  /* The outgoing TCP port to use, if !is_unix_addr */
152  uint16_t real_port;
153  /* The outgoing IPv4 or IPv6 address to use, if !is_unix_addr */
154  tor_addr_t real_addr;
155  /* The socket path to connect to, if is_unix_addr */
156  char unix_addr[FLEXIBLE_ARRAY_MEMBER];
158 
159 void hs_init(void);
160 void hs_free_all(void);
161 
162 void hs_cleanup_circ(circuit_t *circ);
163 
164 int hs_check_service_private_dir(const char *username, const char *path,
165  unsigned int dir_group_readable,
166  unsigned int create);
168 
169 char *hs_path_from_filename(const char *directory, const char *filename);
170 void hs_build_address(const ed25519_public_key_t *key, uint8_t version,
171  char *addr_out);
172 int hs_address_is_valid(const char *address);
173 int hs_parse_address(const char *address, ed25519_public_key_t *key_out,
174  uint8_t *checksum_out, uint8_t *version_out);
175 
176 void hs_build_blinded_pubkey(const ed25519_public_key_t *pubkey,
177  const uint8_t *secret, size_t secret_len,
178  uint64_t time_period_num,
179  ed25519_public_key_t *pubkey_out);
180 void hs_build_blinded_keypair(const ed25519_keypair_t *kp,
181  const uint8_t *secret, size_t secret_len,
182  uint64_t time_period_num,
183  ed25519_keypair_t *kp_out);
184 int hs_service_requires_uptime_circ(const smartlist_t *ports);
185 
186 void rend_data_free_(rend_data_t *data);
187 #define rend_data_free(data) \
188  FREE_AND_NULL(rend_data_t, rend_data_free_, (data))
189 rend_data_t *rend_data_dup(const rend_data_t *data);
190 rend_data_t *rend_data_client_create(const char *onion_address,
191  const char *desc_id,
192  const char *cookie,
193  rend_auth_type_t auth_type);
194 rend_data_t *rend_data_service_create(const char *onion_address,
195  const char *pk_digest,
196  const uint8_t *cookie,
197  rend_auth_type_t auth_type);
198 const char *rend_data_get_address(const rend_data_t *rend_data);
199 const char *rend_data_get_desc_id(const rend_data_t *rend_data,
200  uint8_t replica, size_t *len_out);
201 const uint8_t *rend_data_get_pk_digest(const rend_data_t *rend_data,
202  size_t *len_out);
203 
204 routerstatus_t *pick_hsdir(const char *desc_id, const char *desc_id_base32);
205 
206 void hs_get_subcredential(const ed25519_public_key_t *identity_pk,
207  const ed25519_public_key_t *blinded_pk,
208  uint8_t *subcred_out);
209 
210 uint64_t hs_get_previous_time_period_num(time_t now);
211 uint64_t hs_get_time_period_num(time_t now);
212 uint64_t hs_get_next_time_period_num(time_t now);
213 time_t hs_get_start_time_of_next_time_period(time_t now);
214 
215 link_specifier_t *hs_link_specifier_dup(const link_specifier_t *lspec);
216 
217 MOCK_DECL(int, hs_in_period_between_tp_and_srv,
218  (const networkstatus_t *consensus, time_t now));
219 
220 uint8_t *hs_get_current_srv(uint64_t time_period_num,
221  const networkstatus_t *ns);
222 uint8_t *hs_get_previous_srv(uint64_t time_period_num,
223  const networkstatus_t *ns);
224 
225 void hs_build_hsdir_index(const ed25519_public_key_t *identity_pk,
226  const uint8_t *srv, uint64_t period_num,
227  uint8_t *hsdir_index_out);
228 void hs_build_hs_index(uint64_t replica,
229  const ed25519_public_key_t *blinded_pk,
230  uint64_t period_num, uint8_t *hs_index_out);
231 
232 int32_t hs_get_hsdir_n_replicas(void);
233 int32_t hs_get_hsdir_spread_fetch(void);
234 int32_t hs_get_hsdir_spread_store(void);
235 
236 void hs_get_responsible_hsdirs(const ed25519_public_key_t *blinded_pk,
237  uint64_t time_period_num,
238  int use_second_hsdir_index,
239  int for_fetching, smartlist_t *responsible_dirs);
240 routerstatus_t *hs_pick_hsdir(smartlist_t *responsible_dirs,
241  const char *req_key_str);
242 
243 time_t hs_hsdir_requery_period(const or_options_t *options);
245  const char *desc_id_base32,
246  time_t now, int set);
247 void hs_clean_last_hid_serv_requests(time_t now);
248 void hs_purge_hid_serv_from_last_hid_serv_requests(const char *desc_id);
250 
251 int hs_set_conn_addr_port(const smartlist_t *ports, edge_connection_t *conn);
252 
253 void hs_inc_rdv_stream_counter(origin_circuit_t *circ);
254 void hs_dec_rdv_stream_counter(origin_circuit_t *circ);
255 
256 extend_info_t *hs_get_extend_info_from_lspecs(const smartlist_t *lspecs,
257  const curve25519_public_key_t *onion_key,
258  int direct_conn);
259 
260 #ifdef HS_COMMON_PRIVATE
261 
262 STATIC void get_disaster_srv(uint64_t time_period_num, uint8_t *srv_out);
263 
266 #define REND_HID_SERV_DIR_REQUERY_PERIOD (15 * 60)
267 
269 #define REND_HID_SERV_DIR_REQUERY_PERIOD_TESTING (5)
270 
271 #ifdef TOR_UNIT_TESTS
272 
273 STATIC strmap_t *get_last_hid_serv_requests(void);
274 STATIC uint64_t get_time_period_length(void);
275 
276 STATIC uint8_t *get_first_cached_disaster_srv(void);
277 STATIC uint8_t *get_second_cached_disaster_srv(void);
278 
279 #endif /* defined(TOR_UNIT_TESTS) */
280 
281 #endif /* defined(HS_COMMON_PRIVATE) */
282 
283 #endif /* !defined(TOR_HS_COMMON_H) */
284 
Definition: or.h:842
Definition: crypto_ed25519.h:39
Definition: or.h:3657
uint64_t hs_get_time_period_num(time_t now)
Definition: hs_common.c:259
Definition: crypto_ed25519.h:23
STATIC strmap_t * get_last_hid_serv_requests(void)
Definition: hs_common.c:1457
Definition: address.h:56
void rend_data_free_(rend_data_t *data)
Definition: hs_common.c:351
MOCK_DECL(int, router_have_minimum_dir_info,(void))
Definition: container.h:18
Definition: or.h:2687
rend_auth_type_t
Definition: or.h:826
Definition: hs_common.h:146
Definition: hs_common.h:139
Master header file for Tor-specific functionality.
STATIC uint64_t get_time_period_length(void)
Definition: hs_common.c:233
time_t hs_hsdir_requery_period(const or_options_t *options)
Definition: hs_common.c:1427
hs_service_add_ephemeral_status_t
Definition: hs_common.h:135
int hs_get_service_max_rend_failures(void)
Definition: hs_common.c:223
uint64_t hs_get_next_time_period_num(time_t now)
Definition: hs_common.c:294
Definition: hs_build_address.py:1
Definition: crypto_curve25519.h:24
Definition: hs_common.h:140
Definition: or.h:3059
Definition: hs_common.h:138
Definition: hs_common.h:137
time_t hs_lookup_last_hid_serv_request(routerstatus_t *hs_dir, const char *desc_id_base32, time_t now, int set)
Definition: hs_common.c:1470
Definition: hs_common.h:141
Definition: or.h:2344
STATIC void get_disaster_srv(uint64_t time_period_num, uint8_t *srv_out)
Definition: hs_common.c:624
Definition: or.h:2838
routerstatus_t * hs_pick_hsdir(smartlist_t *responsible_dirs, const char *req_key_str)
Definition: hs_common.c:1601
Definition: hs_common.h:136
Definition: or.h:1687
void hs_purge_last_hid_serv_requests(void)
Definition: hs_common.c:1574
void hs_purge_hid_serv_from_last_hid_serv_requests(const char *desc_id)
Definition: hs_common.c:1533
Definition: or.h:3256
void hs_clean_last_hid_serv_requests(time_t now)
Definition: hs_common.c:1504