1 /* Copyright (c) 2016-2018, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
5 * \file test_hs_service.c
6 * \brief Test hidden service functionality.
9 #define CIRCUITBUILD_PRIVATE
10 #define CIRCUITLIST_PRIVATE
11 #define CONFIG_PRIVATE
12 #define CONNECTION_PRIVATE
13 #define CRYPTO_PRIVATE
14 #define HS_COMMON_PRIVATE
15 #define HS_SERVICE_PRIVATE
16 #define HS_INTROPOINT_PRIVATE
17 #define HS_CIRCUIT_PRIVATE
19 #define NETWORKSTATUS_PRIVATE
20 #define STATEFILE_PRIVATE
21 #define TOR_CHANNEL_INTERNAL_
22 #define HS_CLIENT_PRIVATE
23 #define ROUTERPARSE_PRIVATE
25 #include "test/test.h"
26 #include "test/test_helpers.h"
27 #include "test/log_test_helpers.h"
28 #include "test/rend_test_helpers.h"
29 #include "test/hs_test_helpers.h"
32 #include "or/config.h"
33 #include "or/circuitbuild.h"
34 #include "or/circuitlist.h"
35 #include "or/circuituse.h"
36 #include "lib/crypt_ops/crypto_rand.h"
37 #include "or/dirauth/dirvote.h"
38 #include "or/networkstatus.h"
39 #include "or/nodelist.h"
41 #include "or/routerparse.h"
42 #include "or/hs_common.h"
43 #include "or/hs_config.h"
44 #include "or/hs_ident.h"
45 #include "or/hs_intropoint.h"
46 #include "or/hs_ntor.h"
47 #include "or/hs_circuit.h"
48 #include "or/hs_service.h"
49 #include "or/hs_client.h"
51 #include "or/rendservice.h"
52 #include "or/statefile.h"
53 #include "or/dirauth/shared_random_state.h"
54 #include "or/voting_schedule.h"
56 #include "or/cpath_build_state_st.h"
57 #include "or/crypt_path_st.h"
58 #include "or/networkstatus_st.h"
59 #include "or/node_st.h"
60 #include "or/origin_circuit_st.h"
61 #include "or/routerinfo_st.h"
64 #include "trunnel/hs/cell_establish_intro.h"
66 static networkstatus_t mock_ns
;
68 static networkstatus_t
*
69 mock_networkstatus_get_live_consensus(time_t now
)
75 static or_state_t
*dummy_state
= NULL
;
77 /* Mock function to get fake or state (used for rev counters) */
79 get_or_state_replacement(void)
84 /* Mock function because we are not trying to test the close circuit that does
85 * an awful lot of checks on the circuit object. */
87 mock_circuit_mark_for_close(circuit_t
*circ
, int reason
, int line
,
98 mock_relay_send_command_from_edge(streamid_t stream_id
, circuit_t
*circ
,
99 uint8_t relay_command
, const char *payload
,
101 crypt_path_t
*cpath_layer
,
102 const char *filename
, int lineno
)
106 (void) relay_command
;
115 /* Helper: from a set of options in conf, configure a service which will add
116 * it to the staging list of the HS subsytem. */
118 helper_config_service(const char *conf
)
121 or_options_t
*options
= NULL
;
123 options
= helper_parse_options(conf
);
125 ret
= hs_config_service_all(options
, 0);
127 or_options_free(options
);
131 /* Test: Ensure that setting up rendezvous circuits works correctly. */
133 test_e2e_rend_circuit_setup(void *arg
)
135 ed25519_public_key_t service_pk
;
136 origin_circuit_t
*or_circ
;
139 /** In this test we create a v3 prop224 service-side rendezvous circuit.
140 * We simulate an HS ntor key exchange with a client, and check that
141 * the circuit was setup correctly and is ready to accept rendezvous data */
145 /* Now make dummy circuit */
147 or_circ
= origin_circuit_new();
149 or_circ
->base_
.purpose
= CIRCUIT_PURPOSE_S_CONNECT_REND
;
151 or_circ
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
152 or_circ
->build_state
->is_internal
= 1;
154 /* prop224: Setup hs conn identifier on the stream */
155 ed25519_secret_key_t sk
;
156 tt_int_op(0, OP_EQ
, ed25519_secret_key_generate(&sk
, 0));
157 tt_int_op(0, OP_EQ
, ed25519_public_key_generate(&service_pk
, &sk
));
159 or_circ
->hs_ident
= hs_ident_circuit_new(&service_pk
,
160 HS_IDENT_CIRCUIT_RENDEZVOUS
);
162 TO_CIRCUIT(or_circ
)->state
= CIRCUIT_STATE_OPEN
;
165 /* Check number of hops */
166 retval
= cpath_get_n_hops(&or_circ
->cpath
);
167 tt_int_op(retval
, OP_EQ
, 0);
169 /* Setup the circuit: do the ntor key exchange */
171 uint8_t ntor_key_seed
[DIGEST256_LEN
] = {2};
172 retval
= hs_circuit_setup_e2e_rend_circ(or_circ
,
173 ntor_key_seed
, sizeof(ntor_key_seed
),
175 tt_int_op(retval
, OP_EQ
, 0);
178 /* See that a hop was added to the circuit's cpath */
179 retval
= cpath_get_n_hops(&or_circ
->cpath
);
180 tt_int_op(retval
, OP_EQ
, 1);
182 /* Check the digest algo */
183 tt_int_op(crypto_digest_get_algorithm(or_circ
->cpath
->crypto
.f_digest
),
184 OP_EQ
, DIGEST_SHA3_256
);
185 tt_int_op(crypto_digest_get_algorithm(or_circ
->cpath
->crypto
.b_digest
),
186 OP_EQ
, DIGEST_SHA3_256
);
187 tt_assert(or_circ
->cpath
->crypto
.f_crypto
);
188 tt_assert(or_circ
->cpath
->crypto
.b_crypto
);
190 /* Ensure that circ purpose was changed */
191 tt_int_op(or_circ
->base_
.purpose
, OP_EQ
, CIRCUIT_PURPOSE_S_REND_JOINED
);
194 circuit_free_(TO_CIRCUIT(or_circ
));
197 /* Helper: Return a newly allocated and initialized origin circuit with
198 * purpose and flags. A default HS identifier is set to an ed25519
199 * authentication key for introduction point. */
200 static origin_circuit_t
*
201 helper_create_origin_circuit(int purpose
, int flags
)
203 origin_circuit_t
*circ
= NULL
;
205 circ
= origin_circuit_init(purpose
, flags
);
207 circ
->cpath
= tor_malloc_zero(sizeof(crypt_path_t
));
208 circ
->cpath
->magic
= CRYPT_PATH_MAGIC
;
209 circ
->cpath
->state
= CPATH_STATE_OPEN
;
210 circ
->cpath
->package_window
= circuit_initial_package_window();
211 circ
->cpath
->deliver_window
= CIRCWINDOW_START
;
212 circ
->cpath
->prev
= circ
->cpath
;
214 crypto_rand(circ
->cpath
->prev
->rend_circ_nonce
, DIGEST_LEN
);
215 /* Create a default HS identifier. */
216 circ
->hs_ident
= tor_malloc_zero(sizeof(hs_ident_circuit_t
));
221 /* Helper: Return a newly allocated service object with the identity keypair
222 * sets and the current descriptor. Then register it to the global map.
223 * Caller should us hs_free_all() to free this service or remove it from the
224 * global map before freeing. */
225 static hs_service_t
*
226 helper_create_service(void)
228 /* Set a service for this circuit. */
229 hs_service_t
*service
= hs_service_new(get_options());
231 service
->config
.version
= HS_VERSION_THREE
;
232 ed25519_secret_key_generate(&service
->keys
.identity_sk
, 0);
233 ed25519_public_key_generate(&service
->keys
.identity_pk
,
234 &service
->keys
.identity_sk
);
235 service
->desc_current
= service_descriptor_new();
236 tt_assert(service
->desc_current
);
237 /* Register service to global map. */
238 int ret
= register_service(get_hs_service_map(), service
);
239 tt_int_op(ret
, OP_EQ
, 0);
245 /* Helper: Return a newly allocated service intro point with two link
246 * specifiers, one IPv4 and one legacy ID set to As. */
247 static hs_service_intro_point_t
*
248 helper_create_service_ip(void)
250 hs_desc_link_specifier_t
*ls
;
251 hs_service_intro_point_t
*ip
= service_intro_point_new(NULL
, 0);
253 /* Add a first unused link specifier. */
254 ls
= tor_malloc_zero(sizeof(*ls
));
256 smartlist_add(ip
->base
.link_specifiers
, ls
);
257 /* Add a second link specifier used by a test. */
258 ls
= tor_malloc_zero(sizeof(*ls
));
259 ls
->type
= LS_LEGACY_ID
;
260 memset(ls
->u
.legacy_id
, 'A', sizeof(ls
->u
.legacy_id
));
261 smartlist_add(ip
->base
.link_specifiers
, ls
);
267 test_load_keys(void *arg
)
271 char *hsdir_v2
= tor_strdup(get_fname("hs2"));
272 char *hsdir_v3
= tor_strdup(get_fname("hs3"));
273 char addr
[HS_SERVICE_ADDR_LEN_BASE32
+ 1];
277 /* We'll register two services, a v2 and a v3, then we'll load keys and
278 * validate that both are in a correct state. */
283 "HiddenServiceDir %s\n" \
284 "HiddenServiceVersion %d\n" \
285 "HiddenServicePort 65535\n"
288 tor_asprintf(&conf
, conf_fmt
, hsdir_v2
, HS_VERSION_TWO
);
289 ret
= helper_config_service(conf
);
291 tt_int_op(ret
, OP_EQ
, 0);
292 /* This one should now be registered into the v2 list. */
293 tt_int_op(get_hs_service_staging_list_size(), OP_EQ
, 0);
294 tt_int_op(rend_num_services(), OP_EQ
, 1);
297 tor_asprintf(&conf
, conf_fmt
, hsdir_v3
, HS_VERSION_THREE
);
298 ret
= helper_config_service(conf
);
300 tt_int_op(ret
, OP_EQ
, 0);
301 /* It's in staging? */
302 tt_int_op(get_hs_service_staging_list_size(), OP_EQ
, 1);
304 /* Load the keys for these. After that, the v3 service should be registered
305 * in the global map. */
306 hs_service_load_all_keys();
307 tt_int_op(get_hs_service_map_size(), OP_EQ
, 1);
308 hs_service_t
*s
= get_first_service();
311 /* Ok we have the service object. Validate few things. */
312 tt_assert(!tor_mem_is_zero(s
->onion_address
, sizeof(s
->onion_address
)));
313 tt_int_op(hs_address_is_valid(s
->onion_address
), OP_EQ
, 1);
314 tt_assert(!tor_mem_is_zero((char *) s
->keys
.identity_sk
.seckey
,
315 ED25519_SECKEY_LEN
));
316 tt_assert(!tor_mem_is_zero((char *) s
->keys
.identity_pk
.pubkey
,
317 ED25519_PUBKEY_LEN
));
318 /* Check onion address from identity key. */
319 hs_build_address(&s
->keys
.identity_pk
, s
->config
.version
, addr
);
320 tt_int_op(hs_address_is_valid(addr
), OP_EQ
, 1);
321 tt_str_op(addr
, OP_EQ
, s
->onion_address
);
330 test_access_service(void *arg
)
334 char *hsdir_v3
= tor_strdup(get_fname("hs3"));
335 hs_service_ht
*global_map
;
336 hs_service_t
*s
= NULL
;
340 /* We'll register two services, a v2 and a v3, then we'll load keys and
341 * validate that both are in a correct state. */
346 "HiddenServiceDir %s\n" \
347 "HiddenServiceVersion %d\n" \
348 "HiddenServicePort 65535\n"
351 tor_asprintf(&conf
, conf_fmt
, hsdir_v3
, HS_VERSION_THREE
);
352 ret
= helper_config_service(conf
);
354 tt_int_op(ret
, OP_EQ
, 0);
355 /* It's in staging? */
356 tt_int_op(get_hs_service_staging_list_size(), OP_EQ
, 1);
358 /* Load the keys for these. After that, the v3 service should be registered
359 * in the global map. */
360 hs_service_load_all_keys();
361 tt_int_op(get_hs_service_map_size(), OP_EQ
, 1);
362 s
= get_first_service();
364 global_map
= get_hs_service_map();
365 tt_assert(global_map
);
367 /* From here, we'll try the service accessors. */
368 hs_service_t
*query
= find_service(global_map
, &s
->keys
.identity_pk
);
370 tt_mem_op(query
, OP_EQ
, s
, sizeof(hs_service_t
));
371 /* Remove service, check if it actually works and then put it back. */
372 remove_service(global_map
, s
);
373 tt_int_op(get_hs_service_map_size(), OP_EQ
, 0);
374 query
= find_service(global_map
, &s
->keys
.identity_pk
);
375 tt_ptr_op(query
, OP_EQ
, NULL
);
377 /* Register back the service in the map. */
378 ret
= register_service(global_map
, s
);
379 tt_int_op(ret
, OP_EQ
, 0);
380 tt_int_op(get_hs_service_map_size(), OP_EQ
, 1);
381 /* Twice should fail. */
382 ret
= register_service(global_map
, s
);
383 tt_int_op(ret
, OP_EQ
, -1);
384 /* Remove service from map so we don't double free on cleanup. */
385 remove_service(global_map
, s
);
386 tt_int_op(get_hs_service_map_size(), OP_EQ
, 0);
387 query
= find_service(global_map
, &s
->keys
.identity_pk
);
388 tt_ptr_op(query
, OP_EQ
, NULL
);
389 /* Let's try to remove twice for fun. */
390 setup_full_capture_of_logs(LOG_WARN
);
391 remove_service(global_map
, s
);
392 expect_log_msg_containing("Could not find service in the global map");
393 teardown_capture_of_logs();
401 /** Test that we can create intro point objects, index them and find them */
403 test_service_intro_point(void *arg
)
405 hs_service_t
*service
= NULL
;
406 hs_service_intro_point_t
*ip
= NULL
;
410 /* Test simple creation of an object. */
412 time_t now
= time(NULL
);
413 ip
= helper_create_service_ip();
415 /* Make sure the authentication keypair is not zeroes. */
416 tt_int_op(tor_mem_is_zero((const char *) &ip
->auth_key_kp
,
417 sizeof(ed25519_keypair_t
)), OP_EQ
, 0);
418 /* The introduce2_max MUST be in that range. */
419 tt_u64_op(ip
->introduce2_max
, OP_GE
,
420 INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS
);
421 tt_u64_op(ip
->introduce2_max
, OP_LE
,
422 INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS
);
423 /* Time to expire MUST also be in that range. We subtract 500 seconds
424 * because there could be a gap between setting now and the time taken in
425 * service_intro_point_new. On ARM and other older CPUs, it can be
426 * surprisingly slow... */
427 tt_u64_op(ip
->time_to_expire
, OP_GE
,
428 now
+ INTRO_POINT_LIFETIME_MIN_SECONDS
- 500);
429 /* We add 500 seconds, because this time we're testing against the
430 * maximum allowed time. */
431 tt_u64_op(ip
->time_to_expire
, OP_LE
,
432 now
+ INTRO_POINT_LIFETIME_MAX_SECONDS
+ 500);
433 tt_assert(ip
->replay_cache
);
434 tt_assert(ip
->base
.link_specifiers
);
435 /* By default, this is NOT a legacy object. */
436 tt_int_op(ip
->base
.is_only_legacy
, OP_EQ
, 0);
439 /* Test functions that uses a service intropoints map with that previously
440 * created object (non legacy). */
442 ed25519_public_key_t garbage
= { {0} };
443 hs_service_intro_point_t
*query
;
445 service
= hs_service_new(get_options());
447 service
->desc_current
= service_descriptor_new();
448 tt_assert(service
->desc_current
);
449 /* Add intropoint to descriptor map. */
450 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
451 query
= service_intro_point_find(service
, &ip
->auth_key_kp
.pubkey
);
452 tt_mem_op(query
, OP_EQ
, ip
, sizeof(hs_service_intro_point_t
));
453 query
= service_intro_point_find(service
, &garbage
);
454 tt_ptr_op(query
, OP_EQ
, NULL
);
456 /* While at it, can I find the descriptor with the intro point? */
457 hs_service_descriptor_t
*desc_lookup
=
458 service_desc_find_by_intro(service
, ip
);
459 tt_mem_op(service
->desc_current
, OP_EQ
, desc_lookup
,
460 sizeof(hs_service_descriptor_t
));
462 /* Remove object from service descriptor and make sure it is out. */
463 service_intro_point_remove(service
, ip
);
464 query
= service_intro_point_find(service
, &ip
->auth_key_kp
.pubkey
);
465 tt_ptr_op(query
, OP_EQ
, NULL
);
469 /* If the test succeed, this object is no longer referenced in the service
470 * so we can free it without use after free. Else, it might explode because
471 * it's still in the service descriptor map. */
472 service_intro_point_free(ip
);
473 hs_service_free(service
);
476 static node_t mock_node
;
477 static const node_t
*
478 mock_node_get_by_id(const char *digest
)
481 memset(mock_node
.identity
, 'A', DIGEST_LEN
);
482 /* Only return the matchin identity of As */
483 if (!tor_memcmp(mock_node
.identity
, digest
, DIGEST_LEN
)) {
490 test_helper_functions(void *arg
)
493 hs_service_t
*service
= NULL
;
494 hs_service_intro_point_t
*ip
= NULL
;
495 hs_ident_circuit_t ident
;
499 MOCK(node_get_by_id
, mock_node_get_by_id
);
503 service
= helper_create_service();
505 ip
= helper_create_service_ip();
506 /* Immediately add the intro point to the service so the free service at the
507 * end cleans it as well. */
508 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
510 /* Setup the circuit identifier. */
511 ed25519_pubkey_copy(&ident
.intro_auth_pk
, &ip
->auth_key_kp
.pubkey
);
512 ed25519_pubkey_copy(&ident
.identity_pk
, &service
->keys
.identity_pk
);
514 /* Testing get_objects_from_ident(). */
516 hs_service_t
*s_lookup
= NULL
;
517 hs_service_intro_point_t
*ip_lookup
= NULL
;
518 hs_service_descriptor_t
*desc_lookup
= NULL
;
520 get_objects_from_ident(&ident
, &s_lookup
, &ip_lookup
, &desc_lookup
);
521 tt_mem_op(s_lookup
, OP_EQ
, service
, sizeof(hs_service_t
));
522 tt_mem_op(ip_lookup
, OP_EQ
, ip
, sizeof(hs_service_intro_point_t
));
523 tt_mem_op(desc_lookup
, OP_EQ
, service
->desc_current
,
524 sizeof(hs_service_descriptor_t
));
526 s_lookup
= NULL
; ip_lookup
= NULL
; desc_lookup
= NULL
;
528 /* NULL parameter should work. */
529 get_objects_from_ident(&ident
, NULL
, &ip_lookup
, &desc_lookup
);
530 tt_mem_op(ip_lookup
, OP_EQ
, ip
, sizeof(hs_service_intro_point_t
));
531 tt_mem_op(desc_lookup
, OP_EQ
, service
->desc_current
,
532 sizeof(hs_service_descriptor_t
));
534 s_lookup
= NULL
; ip_lookup
= NULL
; desc_lookup
= NULL
;
536 /* Break the ident and we should find nothing. */
537 memset(&ident
, 0, sizeof(ident
));
538 get_objects_from_ident(&ident
, &s_lookup
, &ip_lookup
, &desc_lookup
);
539 tt_ptr_op(s_lookup
, OP_EQ
, NULL
);
540 tt_ptr_op(ip_lookup
, OP_EQ
, NULL
);
541 tt_ptr_op(desc_lookup
, OP_EQ
, NULL
);
544 /* Testing get_node_from_intro_point() */
546 const node_t
*node
= get_node_from_intro_point(ip
);
547 tt_ptr_op(node
, OP_EQ
, &mock_node
);
548 SMARTLIST_FOREACH_BEGIN(ip
->base
.link_specifiers
,
549 hs_desc_link_specifier_t
*, ls
) {
550 if (ls
->type
== LS_LEGACY_ID
) {
551 /* Change legacy id in link specifier which is not the mock node. */
552 memset(ls
->u
.legacy_id
, 'B', sizeof(ls
->u
.legacy_id
));
554 } SMARTLIST_FOREACH_END(ls
);
555 node
= get_node_from_intro_point(ip
);
556 tt_ptr_op(node
, OP_EQ
, NULL
);
559 /* Testing can_service_launch_intro_circuit() */
561 time_t now
= time(NULL
);
562 /* Put the start of the retry period back in time, we should be allowed.
563 * to launch intro circuit. */
564 service
->state
.num_intro_circ_launched
= 2;
565 service
->state
.intro_circ_retry_started_time
=
566 (now
- INTRO_CIRC_RETRY_PERIOD
- 1);
567 ret
= can_service_launch_intro_circuit(service
, now
);
568 tt_int_op(ret
, OP_EQ
, 1);
569 tt_u64_op(service
->state
.intro_circ_retry_started_time
, OP_EQ
, now
);
570 tt_u64_op(service
->state
.num_intro_circ_launched
, OP_EQ
, 0);
571 /* Call it again, we should still be allowed because we are under
572 * MAX_INTRO_CIRCS_PER_PERIOD which been set to 0 previously. */
573 ret
= can_service_launch_intro_circuit(service
, now
);
574 tt_int_op(ret
, OP_EQ
, 1);
575 tt_u64_op(service
->state
.intro_circ_retry_started_time
, OP_EQ
, now
);
576 tt_u64_op(service
->state
.num_intro_circ_launched
, OP_EQ
, 0);
577 /* Too many intro circuit launched means we are not allowed. */
578 service
->state
.num_intro_circ_launched
= 20;
579 ret
= can_service_launch_intro_circuit(service
, now
);
580 tt_int_op(ret
, OP_EQ
, 0);
583 /* Testing intro_point_should_expire(). */
585 time_t now
= time(NULL
);
586 /* Just some basic test of the current state. */
587 tt_u64_op(ip
->introduce2_max
, OP_GE
,
588 INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS
);
589 tt_u64_op(ip
->introduce2_max
, OP_LE
,
590 INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS
);
591 tt_u64_op(ip
->time_to_expire
, OP_GE
,
592 now
+ INTRO_POINT_LIFETIME_MIN_SECONDS
);
593 tt_u64_op(ip
->time_to_expire
, OP_LE
,
594 now
+ INTRO_POINT_LIFETIME_MAX_SECONDS
);
596 /* This newly created IP from above shouldn't expire now. */
597 ret
= intro_point_should_expire(ip
, now
);
598 tt_int_op(ret
, OP_EQ
, 0);
599 /* Maximum number of INTRODUCE2 cell reached, it should expire. */
600 ip
->introduce2_count
= INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS
+ 1;
601 ret
= intro_point_should_expire(ip
, now
);
602 tt_int_op(ret
, OP_EQ
, 1);
603 ip
->introduce2_count
= 0;
604 /* It should expire if time to expire has been reached. */
605 ip
->time_to_expire
= now
- 1000;
606 ret
= intro_point_should_expire(ip
, now
);
607 tt_int_op(ret
, OP_EQ
, 1);
611 /* This will free the service and all objects associated to it. */
612 hs_service_free_all();
613 UNMOCK(node_get_by_id
);
616 /** Test that we do the right operations when an intro circuit opens */
618 test_intro_circuit_opened(void *arg
)
620 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
621 hs_service_t
*service
;
622 origin_circuit_t
*circ
= NULL
;
627 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
628 MOCK(relay_send_command_from_edge_
, mock_relay_send_command_from_edge
);
630 circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
,
633 /* No service associated with this circuit. */
634 setup_full_capture_of_logs(LOG_WARN
);
635 hs_service_circuit_has_opened(circ
);
636 expect_log_msg_containing("Unknown service identity key");
637 teardown_capture_of_logs();
639 /* Set a service for this circuit. */
641 service
= helper_create_service();
642 ed25519_pubkey_copy(&circ
->hs_ident
->identity_pk
,
643 &service
->keys
.identity_pk
);
645 /* No intro point associated with this circuit. */
646 setup_full_capture_of_logs(LOG_WARN
);
647 hs_service_circuit_has_opened(circ
);
648 expect_log_msg_containing("Unknown introduction point auth key");
649 teardown_capture_of_logs();
652 /* Set an IP object now for this circuit. */
654 hs_service_intro_point_t
*ip
= helper_create_service_ip();
655 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
656 /* Update ident to contain the intro point auth key. */
657 ed25519_pubkey_copy(&circ
->hs_ident
->intro_auth_pk
,
658 &ip
->auth_key_kp
.pubkey
);
661 /* This one should go all the way. */
662 setup_full_capture_of_logs(LOG_INFO
);
663 hs_service_circuit_has_opened(circ
);
664 expect_log_msg_containing("Introduction circuit 0 established for service");
665 teardown_capture_of_logs();
668 circuit_free_(TO_CIRCUIT(circ
));
670 UNMOCK(circuit_mark_for_close_
);
671 UNMOCK(relay_send_command_from_edge_
);
674 /** Test the operations we do on a circuit after we learn that we successfully
675 * established an intro point on it */
677 test_intro_established(void *arg
)
680 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
681 uint8_t payload
[RELAY_PAYLOAD_SIZE
] = {0};
682 origin_circuit_t
*circ
= NULL
;
683 hs_service_t
*service
;
684 hs_service_intro_point_t
*ip
= NULL
;
689 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
691 circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
,
695 /* Test a wrong purpose. */
696 TO_CIRCUIT(circ
)->purpose
= CIRCUIT_PURPOSE_S_INTRO
;
697 setup_full_capture_of_logs(LOG_WARN
);
698 ret
= hs_service_receive_intro_established(circ
, payload
, sizeof(payload
));
699 tt_int_op(ret
, OP_EQ
, -1);
700 expect_log_msg_containing("Received an INTRO_ESTABLISHED cell on a "
701 "non introduction circuit of purpose");
702 teardown_capture_of_logs();
704 /* Back to normal. */
705 TO_CIRCUIT(circ
)->purpose
= CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
;
707 /* No service associated to it. */
708 setup_full_capture_of_logs(LOG_WARN
);
709 ret
= hs_service_receive_intro_established(circ
, payload
, sizeof(payload
));
710 tt_int_op(ret
, OP_EQ
, -1);
711 expect_log_msg_containing("Unknown service identity key");
712 teardown_capture_of_logs();
714 /* Set a service for this circuit. */
715 service
= helper_create_service();
716 ed25519_pubkey_copy(&circ
->hs_ident
->identity_pk
,
717 &service
->keys
.identity_pk
);
718 /* No introduction point associated to it. */
719 setup_full_capture_of_logs(LOG_WARN
);
720 ret
= hs_service_receive_intro_established(circ
, payload
, sizeof(payload
));
721 tt_int_op(ret
, OP_EQ
, -1);
722 expect_log_msg_containing("Introduction circuit established without an "
723 "intro point object on circuit");
724 teardown_capture_of_logs();
726 /* Set an IP object now for this circuit. */
728 ip
= helper_create_service_ip();
729 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
730 /* Update ident to contain the intro point auth key. */
731 ed25519_pubkey_copy(&circ
->hs_ident
->intro_auth_pk
,
732 &ip
->auth_key_kp
.pubkey
);
735 /* Send an empty payload. INTRO_ESTABLISHED cells are basically zeroes. */
736 ret
= hs_service_receive_intro_established(circ
, payload
, sizeof(payload
));
737 tt_int_op(ret
, OP_EQ
, 0);
738 tt_u64_op(ip
->circuit_established
, OP_EQ
, 1);
739 tt_int_op(TO_CIRCUIT(circ
)->purpose
, OP_EQ
, CIRCUIT_PURPOSE_S_INTRO
);
743 circuit_free_(TO_CIRCUIT(circ
));
745 UNMOCK(circuit_mark_for_close_
);
748 /** Check the operations we do on a rendezvous circuit after we learn it's
751 test_rdv_circuit_opened(void *arg
)
753 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
754 origin_circuit_t
*circ
= NULL
;
755 hs_service_t
*service
;
760 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
761 MOCK(relay_send_command_from_edge_
, mock_relay_send_command_from_edge
);
763 circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND
, flags
);
764 crypto_rand((char *) circ
->hs_ident
->rendezvous_cookie
, REND_COOKIE_LEN
);
765 crypto_rand((char *) circ
->hs_ident
->rendezvous_handshake_info
,
766 sizeof(circ
->hs_ident
->rendezvous_handshake_info
));
768 /* No service associated with this circuit. */
769 setup_full_capture_of_logs(LOG_WARN
);
770 hs_service_circuit_has_opened(circ
);
771 expect_log_msg_containing("Unknown service identity key");
772 teardown_capture_of_logs();
773 /* This should be set to a non zero timestamp. */
774 tt_u64_op(TO_CIRCUIT(circ
)->timestamp_dirty
, OP_NE
, 0);
776 /* Set a service for this circuit. */
777 service
= helper_create_service();
778 ed25519_pubkey_copy(&circ
->hs_ident
->identity_pk
,
779 &service
->keys
.identity_pk
);
780 /* Should be all good. */
781 hs_service_circuit_has_opened(circ
);
782 tt_int_op(TO_CIRCUIT(circ
)->purpose
, OP_EQ
, CIRCUIT_PURPOSE_S_REND_JOINED
);
785 circuit_free_(TO_CIRCUIT(circ
));
787 UNMOCK(circuit_mark_for_close_
);
788 UNMOCK(relay_send_command_from_edge_
);
792 mock_assert_circuit_ok(const circuit_t
*c
)
798 /** Test for the general mechanism for closing intro circs.
799 * Also a way to identify that #23603 has been fixed. */
801 test_closing_intro_circs(void *arg
)
803 hs_service_t
*service
= NULL
;
804 hs_service_intro_point_t
*ip
= NULL
, *entry
= NULL
;
805 origin_circuit_t
*intro_circ
= NULL
, *tmp_circ
;
806 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
810 MOCK(assert_circuit_ok
, mock_assert_circuit_ok
);
814 /* Initialize service */
815 service
= helper_create_service();
816 /* Initialize intro point */
817 ip
= helper_create_service_ip();
819 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
821 /* Initialize intro circuit */
822 intro_circ
= origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
, flags
);
823 intro_circ
->hs_ident
= hs_ident_circuit_new(&service
->keys
.identity_pk
,
824 HS_IDENT_CIRCUIT_INTRO
);
825 /* Register circuit in the circuitmap . */
826 hs_circuitmap_register_intro_circ_v3_service_side(intro_circ
,
827 &ip
->auth_key_kp
.pubkey
);
829 hs_circuitmap_get_intro_circ_v3_service_side(&ip
->auth_key_kp
.pubkey
);
830 tt_ptr_op(tmp_circ
, OP_EQ
, intro_circ
);
832 /* Pretend that intro point has failed too much */
833 ip
->circuit_retries
= MAX_INTRO_POINT_CIRCUIT_RETRIES
+1;
835 /* Now pretend we are freeing this intro circuit. We want to see that our
836 * destructor is not gonna kill our intro point structure since that's the
837 * job of the cleanup routine. */
838 circuit_free_(TO_CIRCUIT(intro_circ
));
840 entry
= service_intro_point_find(service
, &ip
->auth_key_kp
.pubkey
);
842 /* The free should also remove the circuit from the circuitmap. */
844 hs_circuitmap_get_intro_circ_v3_service_side(&ip
->auth_key_kp
.pubkey
);
845 tt_assert(!tmp_circ
);
847 /* Now pretend that a new intro point circ was launched and opened. Check
848 * that the intro point will be established correctly. */
849 intro_circ
= origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
, flags
);
850 intro_circ
->hs_ident
= hs_ident_circuit_new(&service
->keys
.identity_pk
,
851 HS_IDENT_CIRCUIT_INTRO
);
852 ed25519_pubkey_copy(&intro_circ
->hs_ident
->intro_auth_pk
,
853 &ip
->auth_key_kp
.pubkey
);
854 /* Register circuit in the circuitmap . */
855 hs_circuitmap_register_intro_circ_v3_service_side(intro_circ
,
856 &ip
->auth_key_kp
.pubkey
);
858 hs_circuitmap_get_intro_circ_v3_service_side(&ip
->auth_key_kp
.pubkey
);
859 tt_ptr_op(tmp_circ
, OP_EQ
, intro_circ
);
860 tt_int_op(TO_CIRCUIT(intro_circ
)->marked_for_close
, OP_EQ
, 0);
861 circuit_mark_for_close(TO_CIRCUIT(intro_circ
), END_CIRC_REASON_INTERNAL
);
862 tt_int_op(TO_CIRCUIT(intro_circ
)->marked_for_close
, OP_NE
, 0);
863 /* At this point, we should not be able to find it in the circuitmap. */
865 hs_circuitmap_get_intro_circ_v3_service_side(&ip
->auth_key_kp
.pubkey
);
866 tt_assert(!tmp_circ
);
870 circuit_free_(TO_CIRCUIT(intro_circ
));
872 /* Frees the service object. */
874 UNMOCK(assert_circuit_ok
);
877 /** Test sending and receiving introduce2 cells */
879 test_introduce2(void *arg
)
882 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
883 uint8_t payload
[RELAY_PAYLOAD_SIZE
] = {0};
884 origin_circuit_t
*circ
= NULL
;
885 hs_service_t
*service
;
886 hs_service_intro_point_t
*ip
= NULL
;
891 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
893 get_or_state_replacement
);
895 dummy_state
= tor_malloc_zero(sizeof(or_state_t
));
897 circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO
, flags
);
900 /* Test a wrong purpose. */
901 TO_CIRCUIT(circ
)->purpose
= CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
;
902 setup_full_capture_of_logs(LOG_WARN
);
903 ret
= hs_service_receive_introduce2(circ
, payload
, sizeof(payload
));
904 tt_int_op(ret
, OP_EQ
, -1);
905 expect_log_msg_containing("Received an INTRODUCE2 cell on a "
906 "non introduction circuit of purpose");
907 teardown_capture_of_logs();
909 /* Back to normal. */
910 TO_CIRCUIT(circ
)->purpose
= CIRCUIT_PURPOSE_S_INTRO
;
912 /* No service associated to it. */
913 setup_full_capture_of_logs(LOG_WARN
);
914 ret
= hs_service_receive_introduce2(circ
, payload
, sizeof(payload
));
915 tt_int_op(ret
, OP_EQ
, -1);
916 expect_log_msg_containing("Unknown service identity key");
917 teardown_capture_of_logs();
919 /* Set a service for this circuit. */
920 service
= helper_create_service();
921 ed25519_pubkey_copy(&circ
->hs_ident
->identity_pk
,
922 &service
->keys
.identity_pk
);
923 /* No introduction point associated to it. */
924 setup_full_capture_of_logs(LOG_WARN
);
925 ret
= hs_service_receive_introduce2(circ
, payload
, sizeof(payload
));
926 tt_int_op(ret
, OP_EQ
, -1);
927 expect_log_msg_containing("Unknown introduction auth key when handling "
928 "an INTRODUCE2 cell on circuit");
929 teardown_capture_of_logs();
931 /* Set an IP object now for this circuit. */
933 ip
= helper_create_service_ip();
934 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
935 /* Update ident to contain the intro point auth key. */
936 ed25519_pubkey_copy(&circ
->hs_ident
->intro_auth_pk
,
937 &ip
->auth_key_kp
.pubkey
);
940 /* This will fail because receiving an INTRODUCE2 cell implies a valid cell
941 * and then launching circuits so let's not do that and instead test that
942 * behaviour differently. */
943 ret
= hs_service_receive_introduce2(circ
, payload
, sizeof(payload
));
944 tt_int_op(ret
, OP_EQ
, -1);
945 tt_u64_op(ip
->introduce2_count
, OP_EQ
, 0);
948 or_state_free(dummy_state
);
951 circuit_free_(TO_CIRCUIT(circ
));
953 UNMOCK(circuit_mark_for_close_
);
956 /** Test basic hidden service housekeeping operations (maintaining intro
959 test_service_event(void *arg
)
961 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
962 time_t now
= time(NULL
);
963 hs_service_t
*service
;
964 origin_circuit_t
*circ
= NULL
;
969 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
971 circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO
, flags
);
973 /* Set a service for this circuit. */
974 service
= helper_create_service();
975 ed25519_pubkey_copy(&circ
->hs_ident
->identity_pk
,
976 &service
->keys
.identity_pk
);
978 /* Currently this consists of cleaning invalid intro points. So adding IPs
979 * here that should get cleaned up. */
981 hs_service_intro_point_t
*ip
= helper_create_service_ip();
982 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
983 /* This run will remove the IP because we have no circuits nor node_t
984 * associated with it. */
985 run_housekeeping_event(now
);
986 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
988 /* We'll trigger a removal because we've reached our maximum amount of
989 * times we should retry a circuit. For this, we need to have a node_t
990 * that matches the identity of this IP. */
992 memset(&ri
, 0, sizeof(ri
));
993 ip
= helper_create_service_ip();
994 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
995 memset(ri
.cache_info
.identity_digest
, 'A', DIGEST_LEN
);
996 /* This triggers a node_t creation. */
997 tt_assert(nodelist_set_routerinfo(&ri
, NULL
));
998 ip
->circuit_retries
= MAX_INTRO_POINT_CIRCUIT_RETRIES
+ 1;
999 run_housekeeping_event(now
);
1000 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1002 /* No removal but no circuit so this means the IP object will stay in the
1003 * descriptor map so we can retry it. */
1004 ip
= helper_create_service_ip();
1005 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
1006 ip
->circuit_established
= 1; /* We'll test that, it MUST be 0 after. */
1007 run_housekeeping_event(now
);
1008 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1010 /* Remove the IP object at once for the next test. */
1011 ip
->circuit_retries
= MAX_INTRO_POINT_CIRCUIT_RETRIES
+ 1;
1012 run_housekeeping_event(now
);
1013 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1015 /* Now, we'll create an IP with a registered circuit. The IP object
1016 * shouldn't go away. */
1017 ip
= helper_create_service_ip();
1018 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
1019 ed25519_pubkey_copy(&circ
->hs_ident
->intro_auth_pk
,
1020 &ip
->auth_key_kp
.pubkey
);
1021 hs_circuitmap_register_intro_circ_v3_service_side(
1022 circ
, &ip
->auth_key_kp
.pubkey
);
1023 run_housekeeping_event(now
);
1024 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1026 /* We'll mangle the IP object to expire. */
1027 ip
->time_to_expire
= now
;
1028 run_housekeeping_event(now
);
1029 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1034 hs_circuitmap_remove_circuit(TO_CIRCUIT(circ
));
1035 circuit_free_(TO_CIRCUIT(circ
));
1037 UNMOCK(circuit_mark_for_close_
);
1040 /** Test that we rotate descriptors correctly. */
1042 test_rotate_descriptors(void *arg
)
1045 time_t next_rotation_time
, now
;
1046 hs_service_t
*service
;
1047 hs_service_descriptor_t
*desc_next
;
1051 dummy_state
= tor_malloc_zero(sizeof(or_state_t
));
1054 MOCK(get_or_state
, get_or_state_replacement
);
1055 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
1056 MOCK(networkstatus_get_live_consensus
,
1057 mock_networkstatus_get_live_consensus
);
1059 /* Descriptor rotation happens with a consensus with a new SRV. */
1061 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
1062 &mock_ns
.valid_after
);
1063 tt_int_op(ret
, OP_EQ
, 0);
1064 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
1065 &mock_ns
.fresh_until
);
1066 tt_int_op(ret
, OP_EQ
, 0);
1067 voting_schedule_recalculate_timing(get_options(), mock_ns
.valid_after
);
1069 update_approx_time(mock_ns
.valid_after
+1);
1070 now
= mock_ns
.valid_after
+1;
1072 /* Create a service with a default descriptor and state. It's added to the
1074 service
= helper_create_service();
1075 service_descriptor_free(service
->desc_current
);
1076 service
->desc_current
= NULL
;
1077 /* This triggers a build for both descriptors. The time now is only used in
1078 * the descriptor certificate which is important to be now else the decoding
1079 * will complain that the cert has expired if we use valid_after. */
1080 build_all_descriptors(now
);
1081 tt_assert(service
->desc_current
);
1082 tt_assert(service
->desc_next
);
1084 /* Tweak our service next rotation time so we can use a custom time. */
1085 service
->state
.next_rotation_time
= next_rotation_time
=
1086 mock_ns
.valid_after
+ (11 * 60 * 60);
1088 /* Nothing should happen, we are not at a new SRV. Our next rotation time
1089 * should be untouched. */
1090 rotate_all_descriptors(mock_ns
.valid_after
);
1091 tt_u64_op(service
->state
.next_rotation_time
, OP_EQ
, next_rotation_time
);
1092 tt_assert(service
->desc_current
);
1093 tt_assert(service
->desc_next
);
1094 tt_u64_op(service
->desc_current
->time_period_num
, OP_EQ
,
1095 hs_get_previous_time_period_num(0));
1096 tt_u64_op(service
->desc_next
->time_period_num
, OP_EQ
,
1097 hs_get_time_period_num(0));
1098 /* Keep a reference so we can compare it after rotation to the current. */
1099 desc_next
= service
->desc_next
;
1101 /* Going right after a new SRV. */
1102 ret
= parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC",
1103 &mock_ns
.valid_after
);
1104 tt_int_op(ret
, OP_EQ
, 0);
1105 ret
= parse_rfc1123_time("Sat, 27 Oct 1985 02:00:00 UTC",
1106 &mock_ns
.fresh_until
);
1107 tt_int_op(ret
, OP_EQ
, 0);
1108 voting_schedule_recalculate_timing(get_options(), mock_ns
.valid_after
);
1110 update_approx_time(mock_ns
.valid_after
+1);
1111 now
= mock_ns
.valid_after
+1;
1113 /* Note down what to expect for the next rotation time which is 01:00 + 23h
1114 * meaning 00:00:00. */
1115 next_rotation_time
= mock_ns
.valid_after
+ (23 * 60 * 60);
1116 /* We should have our next rotation time modified, our current descriptor
1117 * cleaned up and the next descriptor becoming the current. */
1118 rotate_all_descriptors(mock_ns
.valid_after
);
1119 tt_u64_op(service
->state
.next_rotation_time
, OP_EQ
, next_rotation_time
);
1120 tt_mem_op(service
->desc_current
, OP_EQ
, desc_next
, sizeof(*desc_next
));
1121 tt_assert(service
->desc_next
== NULL
);
1123 /* A second time should do nothing. */
1124 rotate_all_descriptors(mock_ns
.valid_after
);
1125 tt_u64_op(service
->state
.next_rotation_time
, OP_EQ
, next_rotation_time
);
1126 tt_mem_op(service
->desc_current
, OP_EQ
, desc_next
, sizeof(*desc_next
));
1127 tt_assert(service
->desc_next
== NULL
);
1129 build_all_descriptors(now
);
1130 tt_mem_op(service
->desc_current
, OP_EQ
, desc_next
, sizeof(*desc_next
));
1131 tt_u64_op(service
->desc_current
->time_period_num
, OP_EQ
,
1132 hs_get_time_period_num(0));
1133 tt_u64_op(service
->desc_next
->time_period_num
, OP_EQ
,
1134 hs_get_next_time_period_num(0));
1135 tt_assert(service
->desc_next
);
1139 UNMOCK(get_or_state
);
1140 UNMOCK(circuit_mark_for_close_
);
1141 UNMOCK(networkstatus_get_live_consensus
);
1144 /** Test building descriptors: picking intro points, setting up their link
1145 * specifiers, etc. */
1147 test_build_update_descriptors(void *arg
)
1150 time_t now
= time(NULL
);
1152 hs_service_t
*service
;
1153 hs_service_intro_point_t
*ip_cur
, *ip_next
;
1161 get_or_state_replacement
);
1162 MOCK(networkstatus_get_live_consensus
,
1163 mock_networkstatus_get_live_consensus
);
1165 dummy_state
= tor_malloc_zero(sizeof(or_state_t
));
1167 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
1168 &mock_ns
.valid_after
);
1169 tt_int_op(ret
, OP_EQ
, 0);
1170 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
1171 &mock_ns
.fresh_until
);
1172 tt_int_op(ret
, OP_EQ
, 0);
1173 voting_schedule_recalculate_timing(get_options(), mock_ns
.valid_after
);
1175 update_approx_time(mock_ns
.valid_after
+1);
1176 now
= mock_ns
.valid_after
+1;
1178 /* Create a service without a current descriptor to trigger a build. */
1179 service
= helper_create_service();
1181 /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
1182 service_descriptor_free(service
->desc_current
);
1183 service
->desc_current
= NULL
;
1185 /* We have a fresh service so this should trigger a build for both
1186 * descriptors for specific time period that we'll test. */
1187 build_all_descriptors(now
);
1188 /* Check *current* descriptor. */
1189 tt_assert(service
->desc_current
);
1190 tt_assert(service
->desc_current
->desc
);
1191 tt_assert(service
->desc_current
->intro_points
.map
);
1192 /* The current time period is the one expected when starting at 03:00. */
1193 tt_u64_op(service
->desc_current
->time_period_num
, OP_EQ
,
1194 hs_get_time_period_num(0));
1195 /* This should be untouched, the update descriptor process changes it. */
1196 tt_u64_op(service
->desc_current
->next_upload_time
, OP_EQ
, 0);
1198 /* Check *next* descriptor. */
1199 tt_assert(service
->desc_next
);
1200 tt_assert(service
->desc_next
->desc
);
1201 tt_assert(service
->desc_next
->intro_points
.map
);
1202 tt_assert(service
->desc_current
!= service
->desc_next
);
1203 tt_u64_op(service
->desc_next
->time_period_num
, OP_EQ
,
1204 hs_get_next_time_period_num(0));
1205 /* This should be untouched, the update descriptor process changes it. */
1206 tt_u64_op(service
->desc_next
->next_upload_time
, OP_EQ
, 0);
1208 /* Time to test the update of those descriptors. At first, we have no node
1209 * in the routerlist so this will find NO suitable node for the IPs. */
1210 setup_full_capture_of_logs(LOG_INFO
);
1211 update_all_descriptors(now
);
1212 expect_log_msg_containing("Unable to find a suitable node to be an "
1213 "introduction point for service");
1214 teardown_capture_of_logs();
1215 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1217 tt_int_op(digest256map_size(service
->desc_next
->intro_points
.map
),
1220 /* Now, we'll setup a node_t. */
1222 tor_addr_t ipv4_addr
;
1223 curve25519_secret_key_t curve25519_secret_key
;
1225 memset(&ri
, 0, sizeof(routerinfo_t
));
1227 tor_addr_parse(&ipv4_addr
, "127.0.0.1");
1228 ri
.addr
= tor_addr_to_ipv4h(&ipv4_addr
);
1230 ri
.purpose
= ROUTER_PURPOSE_GENERAL
;
1231 /* Ugly yes but we never free the "ri" object so this just makes things
1233 ri
.protocol_list
= (char *) "HSDir=1-2 LinkAuth=3";
1234 summarize_protover_flags(&ri
.pv
, ri
.protocol_list
, NULL
);
1235 ret
= curve25519_secret_key_generate(&curve25519_secret_key
, 0);
1236 tt_int_op(ret
, OP_EQ
, 0);
1237 ri
.onion_curve25519_pkey
=
1238 tor_malloc_zero(sizeof(curve25519_public_key_t
));
1239 ri
.onion_pkey
= crypto_pk_new();
1240 curve25519_public_key_generate(ri
.onion_curve25519_pkey
,
1241 &curve25519_secret_key
);
1242 memset(ri
.cache_info
.identity_digest
, 'A', DIGEST_LEN
);
1243 /* Setup ed25519 identity */
1244 ed25519_keypair_t kp1
;
1245 ed25519_keypair_generate(&kp1
, 0);
1246 ri
.cache_info
.signing_key_cert
= tor_malloc_zero(sizeof(tor_cert_t
));
1247 tt_assert(ri
.cache_info
.signing_key_cert
);
1248 ed25519_pubkey_copy(&ri
.cache_info
.signing_key_cert
->signing_key
,
1250 nodelist_set_routerinfo(&ri
, NULL
);
1251 node
= node_get_mutable_by_id(ri
.cache_info
.identity_digest
);
1253 node
->is_running
= node
->is_valid
= node
->is_fast
= node
->is_stable
= 1;
1256 /* We have to set this, or the lack of microdescriptors for these
1257 * nodes will make them unusable. */
1258 get_options_mutable()->UseMicrodescriptors
= 0;
1260 /* We expect to pick only one intro point from the node above. */
1261 setup_full_capture_of_logs(LOG_INFO
);
1262 update_all_descriptors(now
);
1263 tor_free(node
->ri
->onion_curve25519_pkey
); /* Avoid memleak. */
1264 tor_free(node
->ri
->cache_info
.signing_key_cert
);
1265 crypto_pk_free(node
->ri
->onion_pkey
);
1266 expect_log_msg_containing("just picked 1 intro points and wanted 3 for next "
1267 "descriptor. It currently has 0 intro points. "
1268 "Launching ESTABLISH_INTRO circuit shortly.");
1269 teardown_capture_of_logs();
1270 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1272 tt_int_op(digest256map_size(service
->desc_next
->intro_points
.map
),
1274 /* Get the IP object. Because we don't have the auth key of the IP, we can't
1275 * query it so get the first element in the map. */
1279 digest256map_iter_t
*iter
=
1280 digest256map_iter_init(service
->desc_current
->intro_points
.map
);
1281 digest256map_iter_get(iter
, &key
, &obj
);
1284 /* Get also the IP from the next descriptor. We'll make sure it's not the
1285 * same object as in the current descriptor. */
1286 iter
= digest256map_iter_init(service
->desc_next
->intro_points
.map
);
1287 digest256map_iter_get(iter
, &key
, &obj
);
1291 tt_mem_op(ip_cur
, OP_NE
, ip_next
, sizeof(hs_desc_intro_point_t
));
1293 /* We won't test the service IP object because there is a specific test
1294 * already for this but we'll make sure that the state is coherent.*/
1296 /* Three link specifiers are mandatoy so make sure we do have them. */
1297 tt_int_op(smartlist_len(ip_cur
->base
.link_specifiers
), OP_EQ
, 3);
1298 /* Make sure we have a valid encryption keypair generated when we pick an
1299 * intro point in the update process. */
1300 tt_assert(!tor_mem_is_zero((char *) ip_cur
->enc_key_kp
.seckey
.secret_key
,
1301 CURVE25519_SECKEY_LEN
));
1302 tt_assert(!tor_mem_is_zero((char *) ip_cur
->enc_key_kp
.pubkey
.public_key
,
1303 CURVE25519_PUBKEY_LEN
));
1304 tt_u64_op(ip_cur
->time_to_expire
, OP_GE
, now
+
1305 INTRO_POINT_LIFETIME_MIN_SECONDS
);
1306 tt_u64_op(ip_cur
->time_to_expire
, OP_LE
, now
+
1307 INTRO_POINT_LIFETIME_MAX_SECONDS
);
1309 /* Now, we will try to set up a service after a new time period has started
1310 * and see if it behaves as expected. */
1312 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
1313 &mock_ns
.valid_after
);
1314 tt_int_op(ret
, OP_EQ
, 0);
1315 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
1316 &mock_ns
.fresh_until
);
1317 tt_int_op(ret
, OP_EQ
, 0);
1319 update_approx_time(mock_ns
.valid_after
+1);
1320 now
= mock_ns
.valid_after
+1;
1322 /* Create a service without a current descriptor to trigger a build. */
1323 service
= helper_create_service();
1325 /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
1326 service_descriptor_free(service
->desc_current
);
1327 service
->desc_current
= NULL
;
1329 /* We have a fresh service so this should trigger a build for both
1330 * descriptors for specific time period that we'll test. */
1331 build_all_descriptors(now
);
1332 /* Check *current* descriptor. */
1333 tt_assert(service
->desc_current
);
1334 tt_assert(service
->desc_current
->desc
);
1335 tt_assert(service
->desc_current
->intro_points
.map
);
1336 /* This should be for the previous time period. */
1337 tt_u64_op(service
->desc_current
->time_period_num
, OP_EQ
,
1338 hs_get_previous_time_period_num(0));
1339 /* This should be untouched, the update descriptor process changes it. */
1340 tt_u64_op(service
->desc_current
->next_upload_time
, OP_EQ
, 0);
1342 /* Check *next* descriptor. */
1343 tt_assert(service
->desc_next
);
1344 tt_assert(service
->desc_next
->desc
);
1345 tt_assert(service
->desc_next
->intro_points
.map
);
1346 tt_assert(service
->desc_current
!= service
->desc_next
);
1347 tt_u64_op(service
->desc_next
->time_period_num
, OP_EQ
,
1348 hs_get_time_period_num(0));
1349 /* This should be untouched, the update descriptor process changes it. */
1350 tt_u64_op(service
->desc_next
->next_upload_time
, OP_EQ
, 0);
1352 /* Let's remove the next descriptor to simulate a rotation. */
1353 service_descriptor_free(service
->desc_next
);
1354 service
->desc_next
= NULL
;
1356 build_all_descriptors(now
);
1357 /* Check *next* descriptor. */
1358 tt_assert(service
->desc_next
);
1359 tt_assert(service
->desc_next
->desc
);
1360 tt_assert(service
->desc_next
->intro_points
.map
);
1361 tt_assert(service
->desc_current
!= service
->desc_next
);
1362 tt_u64_op(service
->desc_next
->time_period_num
, OP_EQ
,
1363 hs_get_next_time_period_num(0));
1364 /* This should be untouched, the update descriptor process changes it. */
1365 tt_u64_op(service
->desc_next
->next_upload_time
, OP_EQ
, 0);
1369 nodelist_free_all();
1373 test_upload_descriptors(void *arg
)
1377 hs_service_t
*service
;
1383 get_or_state_replacement
);
1384 MOCK(networkstatus_get_live_consensus
,
1385 mock_networkstatus_get_live_consensus
);
1387 dummy_state
= tor_malloc_zero(sizeof(or_state_t
));
1389 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
1390 &mock_ns
.valid_after
);
1391 tt_int_op(ret
, OP_EQ
, 0);
1392 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
1393 &mock_ns
.fresh_until
);
1394 tt_int_op(ret
, OP_EQ
, 0);
1395 voting_schedule_recalculate_timing(get_options(), mock_ns
.valid_after
);
1397 update_approx_time(mock_ns
.valid_after
+1);
1398 now
= mock_ns
.valid_after
+1;
1400 /* Create a service with no descriptor. It's added to the global map. */
1401 service
= hs_service_new(get_options());
1403 service
->config
.version
= HS_VERSION_THREE
;
1404 ed25519_secret_key_generate(&service
->keys
.identity_sk
, 0);
1405 ed25519_public_key_generate(&service
->keys
.identity_pk
,
1406 &service
->keys
.identity_sk
);
1407 /* Register service to global map. */
1408 ret
= register_service(get_hs_service_map(), service
);
1409 tt_int_op(ret
, OP_EQ
, 0);
1410 /* But first, build our descriptor. */
1411 build_all_descriptors(now
);
1413 /* Nothing should happen because we have 0 introduction circuit established
1414 * and we want (by default) 3 intro points. */
1415 run_upload_descriptor_event(now
);
1416 /* If no upload happened, this should be untouched. */
1417 tt_u64_op(service
->desc_current
->next_upload_time
, OP_EQ
, 0);
1418 /* We'll simulate that we've opened our intro point circuit and that we only
1419 * want one intro point. */
1420 service
->config
.num_intro_points
= 1;
1422 /* Set our next upload time after now which will skip the upload. */
1423 service
->desc_current
->next_upload_time
= now
+ 1000;
1424 run_upload_descriptor_event(now
);
1425 /* If no upload happened, this should be untouched. */
1426 tt_u64_op(service
->desc_current
->next_upload_time
, OP_EQ
, now
+ 1000);
1430 UNMOCK(get_or_state
);
1433 /** Global vars used by test_rendezvous1_parsing() */
1434 static char rend1_payload
[RELAY_PAYLOAD_SIZE
];
1435 static size_t rend1_payload_len
= 0;
1437 /** Mock for relay_send_command_from_edge() to send a RENDEZVOUS1 cell. Instead
1438 * of sending it to the network, instead save it to the global `rend1_payload`
1439 * variable so that we can inspect it in the test_rendezvous1_parsing()
1442 mock_relay_send_rendezvous1(streamid_t stream_id
, circuit_t
*circ
,
1443 uint8_t relay_command
, const char *payload
,
1445 crypt_path_t
*cpath_layer
,
1446 const char *filename
, int lineno
)
1450 (void) relay_command
;
1455 memcpy(rend1_payload
, payload
, payload_len
);
1456 rend1_payload_len
= payload_len
;
1461 /** Send a RENDEZVOUS1 as a service, and parse it as a client. */
1463 test_rendezvous1_parsing(void *arg
)
1466 static const char *test_addr
=
1467 "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad.onion";
1468 hs_service_t
*service
= NULL
;
1469 origin_circuit_t
*service_circ
= NULL
;
1470 origin_circuit_t
*client_circ
= NULL
;
1471 ed25519_keypair_t ip_auth_kp
;
1472 curve25519_keypair_t ephemeral_kp
;
1473 curve25519_keypair_t client_kp
;
1474 curve25519_keypair_t ip_enc_kp
;
1475 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
1479 MOCK(relay_send_command_from_edge_
, mock_relay_send_rendezvous1
);
1482 /* Let's start by setting up the service that will start the rend */
1483 service
= tor_malloc_zero(sizeof(hs_service_t
));
1484 ed25519_secret_key_generate(&service
->keys
.identity_sk
, 0);
1485 ed25519_public_key_generate(&service
->keys
.identity_pk
,
1486 &service
->keys
.identity_sk
);
1487 memcpy(service
->onion_address
, test_addr
, sizeof(service
->onion_address
));
1492 /* Now let's set up the service rendezvous circuit and its keys. */
1493 service_circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND
,
1495 tor_free(service_circ
->hs_ident
);
1496 hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys
;
1497 uint8_t rendezvous_cookie
[HS_REND_COOKIE_LEN
];
1498 curve25519_keypair_generate(&ip_enc_kp
, 0);
1499 curve25519_keypair_generate(&ephemeral_kp
, 0);
1500 curve25519_keypair_generate(&client_kp
, 0);
1501 ed25519_keypair_generate(&ip_auth_kp
, 0);
1502 retval
= hs_ntor_service_get_rendezvous1_keys(&ip_auth_kp
.pubkey
,
1506 &hs_ntor_rend_cell_keys
);
1507 tt_int_op(retval
, OP_EQ
, 0);
1509 memset(rendezvous_cookie
, 2, sizeof(rendezvous_cookie
));
1510 service_circ
->hs_ident
=
1511 create_rp_circuit_identifier(service
, rendezvous_cookie
,
1512 &ephemeral_kp
.pubkey
,
1513 &hs_ntor_rend_cell_keys
);
1516 /* Send out the RENDEZVOUS1 and make sure that our mock func worked */
1517 tt_assert(tor_mem_is_zero(rend1_payload
, 32));
1518 hs_circ_service_rp_has_opened(service
, service_circ
);
1519 tt_assert(!tor_mem_is_zero(rend1_payload
, 32));
1520 tt_int_op(rend1_payload_len
, OP_EQ
, HS_LEGACY_RENDEZVOUS_CELL_SIZE
);
1522 /******************************/
1524 /** Now let's create the client rendezvous circuit */
1526 helper_create_origin_circuit(CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
,
1528 /* fix up its circ ident */
1529 ed25519_pubkey_copy(&client_circ
->hs_ident
->intro_auth_pk
,
1530 &ip_auth_kp
.pubkey
);
1531 memcpy(&client_circ
->hs_ident
->rendezvous_client_kp
,
1532 &client_kp
, sizeof(client_circ
->hs_ident
->rendezvous_client_kp
));
1533 memcpy(&client_circ
->hs_ident
->intro_enc_pk
.public_key
,
1534 &ip_enc_kp
.pubkey
.public_key
,
1535 sizeof(client_circ
->hs_ident
->intro_enc_pk
.public_key
));
1537 /* Now parse the rendezvous2 circuit and make sure it was fine. We are
1538 * skipping 20 bytes off its payload, since that's the rendezvous cookie
1539 * which is only present in REND1. */
1540 retval
= handle_rendezvous2(client_circ
,
1541 (uint8_t*)rend1_payload
+20,
1542 rend1_payload_len
-20);
1543 tt_int_op(retval
, OP_EQ
, 0);
1545 /* TODO: We are only simulating client/service here. We could also simulate
1546 * the rendezvous point by plugging in rend_mid_establish_rendezvous(). We
1547 * would need an extra circuit and some more stuff but it's doable. */
1550 circuit_free_(TO_CIRCUIT(service_circ
));
1551 circuit_free_(TO_CIRCUIT(client_circ
));
1552 hs_service_free(service
);
1554 UNMOCK(relay_send_command_from_edge_
);
1557 struct testcase_t hs_service_tests
[] = {
1558 { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup
, TT_FORK
,
1560 { "load_keys", test_load_keys
, TT_FORK
,
1562 { "access_service", test_access_service
, TT_FORK
,
1564 { "service_intro_point", test_service_intro_point
, TT_FORK
,
1566 { "helper_functions", test_helper_functions
, TT_FORK
,
1568 { "intro_circuit_opened", test_intro_circuit_opened
, TT_FORK
,
1570 { "intro_established", test_intro_established
, TT_FORK
,
1572 { "closing_intro_circs", test_closing_intro_circs
, TT_FORK
,
1574 { "rdv_circuit_opened", test_rdv_circuit_opened
, TT_FORK
,
1576 { "introduce2", test_introduce2
, TT_FORK
,
1578 { "service_event", test_service_event
, TT_FORK
,
1580 { "rotate_descriptors", test_rotate_descriptors
, TT_FORK
,
1582 { "build_update_descriptors", test_build_update_descriptors
, TT_FORK
,
1584 { "upload_descriptors", test_upload_descriptors
, TT_FORK
,
1586 { "rendezvous1_parsing", test_rendezvous1_parsing
, TT_FORK
,