1 /* Copyright (c) 2016-2017, 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
26 #include "test_helpers.h"
27 #include "log_test_helpers.h"
28 #include "rend_test_helpers.h"
29 #include "hs_test_helpers.h"
33 #include "circuitbuild.h"
34 #include "circuitlist.h"
35 #include "circuituse.h"
37 #include "dirauth/dirvote.h"
38 #include "networkstatus.h"
41 #include "routerparse.h"
43 #include "hs_common.h"
44 #include "hs_config.h"
46 #include "hs_intropoint.h"
48 #include "hs_circuit.h"
49 #include "hs_service.h"
50 #include "hs_client.h"
52 #include "rendservice.h"
53 #include "statefile.h"
54 #include "dirauth/shared_random_state.h"
57 #include "hs/cell_establish_intro.h"
59 static networkstatus_t mock_ns
;
61 static networkstatus_t
*
62 mock_networkstatus_get_live_consensus(time_t now
)
68 static or_state_t
*dummy_state
= NULL
;
70 /* Mock function to get fake or state (used for rev counters) */
72 get_or_state_replacement(void)
77 /* Mock function because we are not trying to test the close circuit that does
78 * an awful lot of checks on the circuit object. */
80 mock_circuit_mark_for_close(circuit_t
*circ
, int reason
, int line
,
91 mock_relay_send_command_from_edge(streamid_t stream_id
, circuit_t
*circ
,
92 uint8_t relay_command
, const char *payload
,
94 crypt_path_t
*cpath_layer
,
95 const char *filename
, int lineno
)
108 /* Helper: from a set of options in conf, configure a service which will add
109 * it to the staging list of the HS subsytem. */
111 helper_config_service(const char *conf
)
114 or_options_t
*options
= NULL
;
116 options
= helper_parse_options(conf
);
118 ret
= hs_config_service_all(options
, 0);
120 or_options_free(options
);
124 /* Test: Ensure that setting up rendezvous circuits works correctly. */
126 test_e2e_rend_circuit_setup(void *arg
)
128 ed25519_public_key_t service_pk
;
129 origin_circuit_t
*or_circ
;
132 /** In this test we create a v3 prop224 service-side rendezvous circuit.
133 * We simulate an HS ntor key exchange with a client, and check that
134 * the circuit was setup correctly and is ready to accept rendezvous data */
138 /* Now make dummy circuit */
140 or_circ
= origin_circuit_new();
142 or_circ
->base_
.purpose
= CIRCUIT_PURPOSE_S_CONNECT_REND
;
144 or_circ
->build_state
= tor_malloc_zero(sizeof(cpath_build_state_t
));
145 or_circ
->build_state
->is_internal
= 1;
147 /* prop224: Setup hs conn identifier on the stream */
148 ed25519_secret_key_t sk
;
149 tt_int_op(0, OP_EQ
, ed25519_secret_key_generate(&sk
, 0));
150 tt_int_op(0, OP_EQ
, ed25519_public_key_generate(&service_pk
, &sk
));
152 or_circ
->hs_ident
= hs_ident_circuit_new(&service_pk
,
153 HS_IDENT_CIRCUIT_RENDEZVOUS
);
155 TO_CIRCUIT(or_circ
)->state
= CIRCUIT_STATE_OPEN
;
158 /* Check number of hops */
159 retval
= cpath_get_n_hops(&or_circ
->cpath
);
160 tt_int_op(retval
, OP_EQ
, 0);
162 /* Setup the circuit: do the ntor key exchange */
164 uint8_t ntor_key_seed
[DIGEST256_LEN
] = {2};
165 retval
= hs_circuit_setup_e2e_rend_circ(or_circ
,
166 ntor_key_seed
, sizeof(ntor_key_seed
),
168 tt_int_op(retval
, OP_EQ
, 0);
171 /* See that a hop was added to the circuit's cpath */
172 retval
= cpath_get_n_hops(&or_circ
->cpath
);
173 tt_int_op(retval
, OP_EQ
, 1);
175 /* Check the digest algo */
176 tt_int_op(crypto_digest_get_algorithm(or_circ
->cpath
->crypto
.f_digest
),
177 OP_EQ
, DIGEST_SHA3_256
);
178 tt_int_op(crypto_digest_get_algorithm(or_circ
->cpath
->crypto
.b_digest
),
179 OP_EQ
, DIGEST_SHA3_256
);
180 tt_assert(or_circ
->cpath
->crypto
.f_crypto
);
181 tt_assert(or_circ
->cpath
->crypto
.b_crypto
);
183 /* Ensure that circ purpose was changed */
184 tt_int_op(or_circ
->base_
.purpose
, OP_EQ
, CIRCUIT_PURPOSE_S_REND_JOINED
);
187 circuit_free_(TO_CIRCUIT(or_circ
));
190 /* Helper: Return a newly allocated and initialized origin circuit with
191 * purpose and flags. A default HS identifier is set to an ed25519
192 * authentication key for introduction point. */
193 static origin_circuit_t
*
194 helper_create_origin_circuit(int purpose
, int flags
)
196 origin_circuit_t
*circ
= NULL
;
198 circ
= origin_circuit_init(purpose
, flags
);
200 circ
->cpath
= tor_malloc_zero(sizeof(crypt_path_t
));
201 circ
->cpath
->magic
= CRYPT_PATH_MAGIC
;
202 circ
->cpath
->state
= CPATH_STATE_OPEN
;
203 circ
->cpath
->package_window
= circuit_initial_package_window();
204 circ
->cpath
->deliver_window
= CIRCWINDOW_START
;
205 circ
->cpath
->prev
= circ
->cpath
;
207 crypto_rand(circ
->cpath
->prev
->rend_circ_nonce
, DIGEST_LEN
);
208 /* Create a default HS identifier. */
209 circ
->hs_ident
= tor_malloc_zero(sizeof(hs_ident_circuit_t
));
214 /* Helper: Return a newly allocated service object with the identity keypair
215 * sets and the current descriptor. Then register it to the global map.
216 * Caller should us hs_free_all() to free this service or remove it from the
217 * global map before freeing. */
218 static hs_service_t
*
219 helper_create_service(void)
221 /* Set a service for this circuit. */
222 hs_service_t
*service
= hs_service_new(get_options());
224 service
->config
.version
= HS_VERSION_THREE
;
225 ed25519_secret_key_generate(&service
->keys
.identity_sk
, 0);
226 ed25519_public_key_generate(&service
->keys
.identity_pk
,
227 &service
->keys
.identity_sk
);
228 service
->desc_current
= service_descriptor_new();
229 tt_assert(service
->desc_current
);
230 /* Register service to global map. */
231 int ret
= register_service(get_hs_service_map(), service
);
232 tt_int_op(ret
, OP_EQ
, 0);
238 /* Helper: Return a newly allocated service intro point with two link
239 * specifiers, one IPv4 and one legacy ID set to As. */
240 static hs_service_intro_point_t
*
241 helper_create_service_ip(void)
243 hs_desc_link_specifier_t
*ls
;
244 hs_service_intro_point_t
*ip
= service_intro_point_new(NULL
, 0);
246 /* Add a first unused link specifier. */
247 ls
= tor_malloc_zero(sizeof(*ls
));
249 smartlist_add(ip
->base
.link_specifiers
, ls
);
250 /* Add a second link specifier used by a test. */
251 ls
= tor_malloc_zero(sizeof(*ls
));
252 ls
->type
= LS_LEGACY_ID
;
253 memset(ls
->u
.legacy_id
, 'A', sizeof(ls
->u
.legacy_id
));
254 smartlist_add(ip
->base
.link_specifiers
, ls
);
260 test_load_keys(void *arg
)
264 char *hsdir_v2
= tor_strdup(get_fname("hs2"));
265 char *hsdir_v3
= tor_strdup(get_fname("hs3"));
266 char addr
[HS_SERVICE_ADDR_LEN_BASE32
+ 1];
270 /* We'll register two services, a v2 and a v3, then we'll load keys and
271 * validate that both are in a correct state. */
276 "HiddenServiceDir %s\n" \
277 "HiddenServiceVersion %d\n" \
278 "HiddenServicePort 65535\n"
281 tor_asprintf(&conf
, conf_fmt
, hsdir_v2
, HS_VERSION_TWO
);
282 ret
= helper_config_service(conf
);
284 tt_int_op(ret
, OP_EQ
, 0);
285 /* This one should now be registered into the v2 list. */
286 tt_int_op(get_hs_service_staging_list_size(), OP_EQ
, 0);
287 tt_int_op(rend_num_services(), OP_EQ
, 1);
290 tor_asprintf(&conf
, conf_fmt
, hsdir_v3
, HS_VERSION_THREE
);
291 ret
= helper_config_service(conf
);
293 tt_int_op(ret
, OP_EQ
, 0);
294 /* It's in staging? */
295 tt_int_op(get_hs_service_staging_list_size(), OP_EQ
, 1);
297 /* Load the keys for these. After that, the v3 service should be registered
298 * in the global map. */
299 hs_service_load_all_keys();
300 tt_int_op(get_hs_service_map_size(), OP_EQ
, 1);
301 hs_service_t
*s
= get_first_service();
304 /* Ok we have the service object. Validate few things. */
305 tt_assert(!tor_mem_is_zero(s
->onion_address
, sizeof(s
->onion_address
)));
306 tt_int_op(hs_address_is_valid(s
->onion_address
), OP_EQ
, 1);
307 tt_assert(!tor_mem_is_zero((char *) s
->keys
.identity_sk
.seckey
,
308 ED25519_SECKEY_LEN
));
309 tt_assert(!tor_mem_is_zero((char *) s
->keys
.identity_pk
.pubkey
,
310 ED25519_PUBKEY_LEN
));
311 /* Check onion address from identity key. */
312 hs_build_address(&s
->keys
.identity_pk
, s
->config
.version
, addr
);
313 tt_int_op(hs_address_is_valid(addr
), OP_EQ
, 1);
314 tt_str_op(addr
, OP_EQ
, s
->onion_address
);
323 test_access_service(void *arg
)
327 char *hsdir_v3
= tor_strdup(get_fname("hs3"));
328 hs_service_ht
*global_map
;
329 hs_service_t
*s
= NULL
;
333 /* We'll register two services, a v2 and a v3, then we'll load keys and
334 * validate that both are in a correct state. */
339 "HiddenServiceDir %s\n" \
340 "HiddenServiceVersion %d\n" \
341 "HiddenServicePort 65535\n"
344 tor_asprintf(&conf
, conf_fmt
, hsdir_v3
, HS_VERSION_THREE
);
345 ret
= helper_config_service(conf
);
347 tt_int_op(ret
, OP_EQ
, 0);
348 /* It's in staging? */
349 tt_int_op(get_hs_service_staging_list_size(), OP_EQ
, 1);
351 /* Load the keys for these. After that, the v3 service should be registered
352 * in the global map. */
353 hs_service_load_all_keys();
354 tt_int_op(get_hs_service_map_size(), OP_EQ
, 1);
355 s
= get_first_service();
357 global_map
= get_hs_service_map();
358 tt_assert(global_map
);
360 /* From here, we'll try the service accessors. */
361 hs_service_t
*query
= find_service(global_map
, &s
->keys
.identity_pk
);
363 tt_mem_op(query
, OP_EQ
, s
, sizeof(hs_service_t
));
364 /* Remove service, check if it actually works and then put it back. */
365 remove_service(global_map
, s
);
366 tt_int_op(get_hs_service_map_size(), OP_EQ
, 0);
367 query
= find_service(global_map
, &s
->keys
.identity_pk
);
368 tt_ptr_op(query
, OP_EQ
, NULL
);
370 /* Register back the service in the map. */
371 ret
= register_service(global_map
, s
);
372 tt_int_op(ret
, OP_EQ
, 0);
373 tt_int_op(get_hs_service_map_size(), OP_EQ
, 1);
374 /* Twice should fail. */
375 ret
= register_service(global_map
, s
);
376 tt_int_op(ret
, OP_EQ
, -1);
377 /* Remove service from map so we don't double free on cleanup. */
378 remove_service(global_map
, s
);
379 tt_int_op(get_hs_service_map_size(), OP_EQ
, 0);
380 query
= find_service(global_map
, &s
->keys
.identity_pk
);
381 tt_ptr_op(query
, OP_EQ
, NULL
);
382 /* Let's try to remove twice for fun. */
383 setup_full_capture_of_logs(LOG_WARN
);
384 remove_service(global_map
, s
);
385 expect_log_msg_containing("Could not find service in the global map");
386 teardown_capture_of_logs();
394 /** Test that we can create intro point objects, index them and find them */
396 test_service_intro_point(void *arg
)
398 hs_service_t
*service
= NULL
;
399 hs_service_intro_point_t
*ip
= NULL
;
403 /* Test simple creation of an object. */
405 time_t now
= time(NULL
);
406 ip
= helper_create_service_ip();
408 /* Make sure the authentication keypair is not zeroes. */
409 tt_int_op(tor_mem_is_zero((const char *) &ip
->auth_key_kp
,
410 sizeof(ed25519_keypair_t
)), OP_EQ
, 0);
411 /* The introduce2_max MUST be in that range. */
412 tt_u64_op(ip
->introduce2_max
, OP_GE
,
413 INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS
);
414 tt_u64_op(ip
->introduce2_max
, OP_LE
,
415 INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS
);
416 /* Time to expire MUST also be in that range. We subtract 500 seconds
417 * because there could be a gap between setting now and the time taken in
418 * service_intro_point_new. On ARM and other older CPUs, it can be
419 * surprisingly slow... */
420 tt_u64_op(ip
->time_to_expire
, OP_GE
,
421 now
+ INTRO_POINT_LIFETIME_MIN_SECONDS
- 500);
422 /* We add 500 seconds, because this time we're testing against the
423 * maximum allowed time. */
424 tt_u64_op(ip
->time_to_expire
, OP_LE
,
425 now
+ INTRO_POINT_LIFETIME_MAX_SECONDS
+ 500);
426 tt_assert(ip
->replay_cache
);
427 tt_assert(ip
->base
.link_specifiers
);
428 /* By default, this is NOT a legacy object. */
429 tt_int_op(ip
->base
.is_only_legacy
, OP_EQ
, 0);
432 /* Test functions that uses a service intropoints map with that previously
433 * created object (non legacy). */
435 ed25519_public_key_t garbage
= { {0} };
436 hs_service_intro_point_t
*query
;
438 service
= hs_service_new(get_options());
440 service
->desc_current
= service_descriptor_new();
441 tt_assert(service
->desc_current
);
442 /* Add intropoint to descriptor map. */
443 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
444 query
= service_intro_point_find(service
, &ip
->auth_key_kp
.pubkey
);
445 tt_mem_op(query
, OP_EQ
, ip
, sizeof(hs_service_intro_point_t
));
446 query
= service_intro_point_find(service
, &garbage
);
447 tt_ptr_op(query
, OP_EQ
, NULL
);
449 /* While at it, can I find the descriptor with the intro point? */
450 hs_service_descriptor_t
*desc_lookup
=
451 service_desc_find_by_intro(service
, ip
);
452 tt_mem_op(service
->desc_current
, OP_EQ
, desc_lookup
,
453 sizeof(hs_service_descriptor_t
));
455 /* Remove object from service descriptor and make sure it is out. */
456 service_intro_point_remove(service
, ip
);
457 query
= service_intro_point_find(service
, &ip
->auth_key_kp
.pubkey
);
458 tt_ptr_op(query
, OP_EQ
, NULL
);
462 /* If the test succeed, this object is no longer referenced in the service
463 * so we can free it without use after free. Else, it might explode because
464 * it's still in the service descriptor map. */
465 service_intro_point_free(ip
);
466 hs_service_free(service
);
469 static node_t mock_node
;
470 static const node_t
*
471 mock_node_get_by_id(const char *digest
)
474 memset(mock_node
.identity
, 'A', DIGEST_LEN
);
475 /* Only return the matchin identity of As */
476 if (!tor_memcmp(mock_node
.identity
, digest
, DIGEST_LEN
)) {
483 test_helper_functions(void *arg
)
486 hs_service_t
*service
= NULL
;
487 hs_service_intro_point_t
*ip
= NULL
;
488 hs_ident_circuit_t ident
;
492 MOCK(node_get_by_id
, mock_node_get_by_id
);
496 service
= helper_create_service();
498 ip
= helper_create_service_ip();
499 /* Immediately add the intro point to the service so the free service at the
500 * end cleans it as well. */
501 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
503 /* Setup the circuit identifier. */
504 ed25519_pubkey_copy(&ident
.intro_auth_pk
, &ip
->auth_key_kp
.pubkey
);
505 ed25519_pubkey_copy(&ident
.identity_pk
, &service
->keys
.identity_pk
);
507 /* Testing get_objects_from_ident(). */
509 hs_service_t
*s_lookup
= NULL
;
510 hs_service_intro_point_t
*ip_lookup
= NULL
;
511 hs_service_descriptor_t
*desc_lookup
= NULL
;
513 get_objects_from_ident(&ident
, &s_lookup
, &ip_lookup
, &desc_lookup
);
514 tt_mem_op(s_lookup
, OP_EQ
, service
, sizeof(hs_service_t
));
515 tt_mem_op(ip_lookup
, OP_EQ
, ip
, sizeof(hs_service_intro_point_t
));
516 tt_mem_op(desc_lookup
, OP_EQ
, service
->desc_current
,
517 sizeof(hs_service_descriptor_t
));
519 s_lookup
= NULL
; ip_lookup
= NULL
; desc_lookup
= NULL
;
521 /* NULL parameter should work. */
522 get_objects_from_ident(&ident
, NULL
, &ip_lookup
, &desc_lookup
);
523 tt_mem_op(ip_lookup
, OP_EQ
, ip
, sizeof(hs_service_intro_point_t
));
524 tt_mem_op(desc_lookup
, OP_EQ
, service
->desc_current
,
525 sizeof(hs_service_descriptor_t
));
527 s_lookup
= NULL
; ip_lookup
= NULL
; desc_lookup
= NULL
;
529 /* Break the ident and we should find nothing. */
530 memset(&ident
, 0, sizeof(ident
));
531 get_objects_from_ident(&ident
, &s_lookup
, &ip_lookup
, &desc_lookup
);
532 tt_ptr_op(s_lookup
, OP_EQ
, NULL
);
533 tt_ptr_op(ip_lookup
, OP_EQ
, NULL
);
534 tt_ptr_op(desc_lookup
, OP_EQ
, NULL
);
537 /* Testing get_node_from_intro_point() */
539 const node_t
*node
= get_node_from_intro_point(ip
);
540 tt_ptr_op(node
, OP_EQ
, &mock_node
);
541 SMARTLIST_FOREACH_BEGIN(ip
->base
.link_specifiers
,
542 hs_desc_link_specifier_t
*, ls
) {
543 if (ls
->type
== LS_LEGACY_ID
) {
544 /* Change legacy id in link specifier which is not the mock node. */
545 memset(ls
->u
.legacy_id
, 'B', sizeof(ls
->u
.legacy_id
));
547 } SMARTLIST_FOREACH_END(ls
);
548 node
= get_node_from_intro_point(ip
);
549 tt_ptr_op(node
, OP_EQ
, NULL
);
552 /* Testing can_service_launch_intro_circuit() */
554 time_t now
= time(NULL
);
555 /* Put the start of the retry period back in time, we should be allowed.
556 * to launch intro circuit. */
557 service
->state
.num_intro_circ_launched
= 2;
558 service
->state
.intro_circ_retry_started_time
=
559 (now
- INTRO_CIRC_RETRY_PERIOD
- 1);
560 ret
= can_service_launch_intro_circuit(service
, now
);
561 tt_int_op(ret
, OP_EQ
, 1);
562 tt_u64_op(service
->state
.intro_circ_retry_started_time
, OP_EQ
, now
);
563 tt_u64_op(service
->state
.num_intro_circ_launched
, OP_EQ
, 0);
564 /* Call it again, we should still be allowed because we are under
565 * MAX_INTRO_CIRCS_PER_PERIOD which been set to 0 previously. */
566 ret
= can_service_launch_intro_circuit(service
, now
);
567 tt_int_op(ret
, OP_EQ
, 1);
568 tt_u64_op(service
->state
.intro_circ_retry_started_time
, OP_EQ
, now
);
569 tt_u64_op(service
->state
.num_intro_circ_launched
, OP_EQ
, 0);
570 /* Too many intro circuit launched means we are not allowed. */
571 service
->state
.num_intro_circ_launched
= 20;
572 ret
= can_service_launch_intro_circuit(service
, now
);
573 tt_int_op(ret
, OP_EQ
, 0);
576 /* Testing intro_point_should_expire(). */
578 time_t now
= time(NULL
);
579 /* Just some basic test of the current state. */
580 tt_u64_op(ip
->introduce2_max
, OP_GE
,
581 INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS
);
582 tt_u64_op(ip
->introduce2_max
, OP_LE
,
583 INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS
);
584 tt_u64_op(ip
->time_to_expire
, OP_GE
,
585 now
+ INTRO_POINT_LIFETIME_MIN_SECONDS
);
586 tt_u64_op(ip
->time_to_expire
, OP_LE
,
587 now
+ INTRO_POINT_LIFETIME_MAX_SECONDS
);
589 /* This newly created IP from above shouldn't expire now. */
590 ret
= intro_point_should_expire(ip
, now
);
591 tt_int_op(ret
, OP_EQ
, 0);
592 /* Maximum number of INTRODUCE2 cell reached, it should expire. */
593 ip
->introduce2_count
= INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS
+ 1;
594 ret
= intro_point_should_expire(ip
, now
);
595 tt_int_op(ret
, OP_EQ
, 1);
596 ip
->introduce2_count
= 0;
597 /* It should expire if time to expire has been reached. */
598 ip
->time_to_expire
= now
- 1000;
599 ret
= intro_point_should_expire(ip
, now
);
600 tt_int_op(ret
, OP_EQ
, 1);
604 /* This will free the service and all objects associated to it. */
605 hs_service_free_all();
606 UNMOCK(node_get_by_id
);
609 /** Test that we do the right operations when an intro circuit opens */
611 test_intro_circuit_opened(void *arg
)
613 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
614 hs_service_t
*service
;
615 origin_circuit_t
*circ
= NULL
;
620 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
621 MOCK(relay_send_command_from_edge_
, mock_relay_send_command_from_edge
);
623 circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
,
626 /* No service associated with this circuit. */
627 setup_full_capture_of_logs(LOG_WARN
);
628 hs_service_circuit_has_opened(circ
);
629 expect_log_msg_containing("Unknown service identity key");
630 teardown_capture_of_logs();
632 /* Set a service for this circuit. */
634 service
= helper_create_service();
635 ed25519_pubkey_copy(&circ
->hs_ident
->identity_pk
,
636 &service
->keys
.identity_pk
);
638 /* No intro point associated with this circuit. */
639 setup_full_capture_of_logs(LOG_WARN
);
640 hs_service_circuit_has_opened(circ
);
641 expect_log_msg_containing("Unknown introduction point auth key");
642 teardown_capture_of_logs();
645 /* Set an IP object now for this circuit. */
647 hs_service_intro_point_t
*ip
= helper_create_service_ip();
648 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
649 /* Update ident to contain the intro point auth key. */
650 ed25519_pubkey_copy(&circ
->hs_ident
->intro_auth_pk
,
651 &ip
->auth_key_kp
.pubkey
);
654 /* This one should go all the way. */
655 setup_full_capture_of_logs(LOG_INFO
);
656 hs_service_circuit_has_opened(circ
);
657 expect_log_msg_containing("Introduction circuit 0 established for service");
658 teardown_capture_of_logs();
661 circuit_free_(TO_CIRCUIT(circ
));
663 UNMOCK(circuit_mark_for_close_
);
664 UNMOCK(relay_send_command_from_edge_
);
667 /** Test the operations we do on a circuit after we learn that we successfully
668 * established an intro point on it */
670 test_intro_established(void *arg
)
673 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
674 uint8_t payload
[RELAY_PAYLOAD_SIZE
] = {0};
675 origin_circuit_t
*circ
= NULL
;
676 hs_service_t
*service
;
677 hs_service_intro_point_t
*ip
= NULL
;
682 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
684 circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
,
688 /* Test a wrong purpose. */
689 TO_CIRCUIT(circ
)->purpose
= CIRCUIT_PURPOSE_S_INTRO
;
690 setup_full_capture_of_logs(LOG_WARN
);
691 ret
= hs_service_receive_intro_established(circ
, payload
, sizeof(payload
));
692 tt_int_op(ret
, OP_EQ
, -1);
693 expect_log_msg_containing("Received an INTRO_ESTABLISHED cell on a "
694 "non introduction circuit of purpose");
695 teardown_capture_of_logs();
697 /* Back to normal. */
698 TO_CIRCUIT(circ
)->purpose
= CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
;
700 /* No service associated to it. */
701 setup_full_capture_of_logs(LOG_WARN
);
702 ret
= hs_service_receive_intro_established(circ
, payload
, sizeof(payload
));
703 tt_int_op(ret
, OP_EQ
, -1);
704 expect_log_msg_containing("Unknown service identity key");
705 teardown_capture_of_logs();
707 /* Set a service for this circuit. */
708 service
= helper_create_service();
709 ed25519_pubkey_copy(&circ
->hs_ident
->identity_pk
,
710 &service
->keys
.identity_pk
);
711 /* No introduction point associated to it. */
712 setup_full_capture_of_logs(LOG_WARN
);
713 ret
= hs_service_receive_intro_established(circ
, payload
, sizeof(payload
));
714 tt_int_op(ret
, OP_EQ
, -1);
715 expect_log_msg_containing("Introduction circuit established without an "
716 "intro point object on circuit");
717 teardown_capture_of_logs();
719 /* Set an IP object now for this circuit. */
721 ip
= helper_create_service_ip();
722 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
723 /* Update ident to contain the intro point auth key. */
724 ed25519_pubkey_copy(&circ
->hs_ident
->intro_auth_pk
,
725 &ip
->auth_key_kp
.pubkey
);
728 /* Send an empty payload. INTRO_ESTABLISHED cells are basically zeroes. */
729 ret
= hs_service_receive_intro_established(circ
, payload
, sizeof(payload
));
730 tt_int_op(ret
, OP_EQ
, 0);
731 tt_u64_op(ip
->circuit_established
, OP_EQ
, 1);
732 tt_int_op(TO_CIRCUIT(circ
)->purpose
, OP_EQ
, CIRCUIT_PURPOSE_S_INTRO
);
736 circuit_free_(TO_CIRCUIT(circ
));
738 UNMOCK(circuit_mark_for_close_
);
741 /** Check the operations we do on a rendezvous circuit after we learn it's
744 test_rdv_circuit_opened(void *arg
)
746 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
747 origin_circuit_t
*circ
= NULL
;
748 hs_service_t
*service
;
753 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
754 MOCK(relay_send_command_from_edge_
, mock_relay_send_command_from_edge
);
756 circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND
, flags
);
757 crypto_rand((char *) circ
->hs_ident
->rendezvous_cookie
, REND_COOKIE_LEN
);
758 crypto_rand((char *) circ
->hs_ident
->rendezvous_handshake_info
,
759 sizeof(circ
->hs_ident
->rendezvous_handshake_info
));
761 /* No service associated with this circuit. */
762 setup_full_capture_of_logs(LOG_WARN
);
763 hs_service_circuit_has_opened(circ
);
764 expect_log_msg_containing("Unknown service identity key");
765 teardown_capture_of_logs();
766 /* This should be set to a non zero timestamp. */
767 tt_u64_op(TO_CIRCUIT(circ
)->timestamp_dirty
, OP_NE
, 0);
769 /* Set a service for this circuit. */
770 service
= helper_create_service();
771 ed25519_pubkey_copy(&circ
->hs_ident
->identity_pk
,
772 &service
->keys
.identity_pk
);
773 /* Should be all good. */
774 hs_service_circuit_has_opened(circ
);
775 tt_int_op(TO_CIRCUIT(circ
)->purpose
, OP_EQ
, CIRCUIT_PURPOSE_S_REND_JOINED
);
778 circuit_free_(TO_CIRCUIT(circ
));
780 UNMOCK(circuit_mark_for_close_
);
781 UNMOCK(relay_send_command_from_edge_
);
785 mock_assert_circuit_ok(const circuit_t
*c
)
791 /** Test for the general mechanism for closing intro circs.
792 * Also a way to identify that #23603 has been fixed. */
794 test_closing_intro_circs(void *arg
)
796 hs_service_t
*service
= NULL
;
797 hs_service_intro_point_t
*ip
= NULL
, *entry
= NULL
;
798 origin_circuit_t
*intro_circ
= NULL
, *tmp_circ
;
799 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
803 MOCK(assert_circuit_ok
, mock_assert_circuit_ok
);
807 /* Initialize service */
808 service
= helper_create_service();
809 /* Initialize intro point */
810 ip
= helper_create_service_ip();
812 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
814 /* Initialize intro circuit */
815 intro_circ
= origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
, flags
);
816 intro_circ
->hs_ident
= hs_ident_circuit_new(&service
->keys
.identity_pk
,
817 HS_IDENT_CIRCUIT_INTRO
);
818 /* Register circuit in the circuitmap . */
819 hs_circuitmap_register_intro_circ_v3_service_side(intro_circ
,
820 &ip
->auth_key_kp
.pubkey
);
822 hs_circuitmap_get_intro_circ_v3_service_side(&ip
->auth_key_kp
.pubkey
);
823 tt_ptr_op(tmp_circ
, OP_EQ
, intro_circ
);
825 /* Pretend that intro point has failed too much */
826 ip
->circuit_retries
= MAX_INTRO_POINT_CIRCUIT_RETRIES
+1;
828 /* Now pretend we are freeing this intro circuit. We want to see that our
829 * destructor is not gonna kill our intro point structure since that's the
830 * job of the cleanup routine. */
831 circuit_free_(TO_CIRCUIT(intro_circ
));
833 entry
= service_intro_point_find(service
, &ip
->auth_key_kp
.pubkey
);
835 /* The free should also remove the circuit from the circuitmap. */
837 hs_circuitmap_get_intro_circ_v3_service_side(&ip
->auth_key_kp
.pubkey
);
838 tt_assert(!tmp_circ
);
840 /* Now pretend that a new intro point circ was launched and opened. Check
841 * that the intro point will be established correctly. */
842 intro_circ
= origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
, flags
);
843 intro_circ
->hs_ident
= hs_ident_circuit_new(&service
->keys
.identity_pk
,
844 HS_IDENT_CIRCUIT_INTRO
);
845 ed25519_pubkey_copy(&intro_circ
->hs_ident
->intro_auth_pk
,
846 &ip
->auth_key_kp
.pubkey
);
847 /* Register circuit in the circuitmap . */
848 hs_circuitmap_register_intro_circ_v3_service_side(intro_circ
,
849 &ip
->auth_key_kp
.pubkey
);
851 hs_circuitmap_get_intro_circ_v3_service_side(&ip
->auth_key_kp
.pubkey
);
852 tt_ptr_op(tmp_circ
, OP_EQ
, intro_circ
);
853 tt_int_op(TO_CIRCUIT(intro_circ
)->marked_for_close
, OP_EQ
, 0);
854 circuit_mark_for_close(TO_CIRCUIT(intro_circ
), END_CIRC_REASON_INTERNAL
);
855 tt_int_op(TO_CIRCUIT(intro_circ
)->marked_for_close
, OP_NE
, 0);
856 /* At this point, we should not be able to find it in the circuitmap. */
858 hs_circuitmap_get_intro_circ_v3_service_side(&ip
->auth_key_kp
.pubkey
);
859 tt_assert(!tmp_circ
);
863 circuit_free_(TO_CIRCUIT(intro_circ
));
865 /* Frees the service object. */
867 UNMOCK(assert_circuit_ok
);
870 /** Test sending and receiving introduce2 cells */
872 test_introduce2(void *arg
)
875 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
876 uint8_t payload
[RELAY_PAYLOAD_SIZE
] = {0};
877 origin_circuit_t
*circ
= NULL
;
878 hs_service_t
*service
;
879 hs_service_intro_point_t
*ip
= NULL
;
884 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
886 get_or_state_replacement
);
888 dummy_state
= tor_malloc_zero(sizeof(or_state_t
));
890 circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO
, flags
);
893 /* Test a wrong purpose. */
894 TO_CIRCUIT(circ
)->purpose
= CIRCUIT_PURPOSE_S_ESTABLISH_INTRO
;
895 setup_full_capture_of_logs(LOG_WARN
);
896 ret
= hs_service_receive_introduce2(circ
, payload
, sizeof(payload
));
897 tt_int_op(ret
, OP_EQ
, -1);
898 expect_log_msg_containing("Received an INTRODUCE2 cell on a "
899 "non introduction circuit of purpose");
900 teardown_capture_of_logs();
902 /* Back to normal. */
903 TO_CIRCUIT(circ
)->purpose
= CIRCUIT_PURPOSE_S_INTRO
;
905 /* No service associated to it. */
906 setup_full_capture_of_logs(LOG_WARN
);
907 ret
= hs_service_receive_introduce2(circ
, payload
, sizeof(payload
));
908 tt_int_op(ret
, OP_EQ
, -1);
909 expect_log_msg_containing("Unknown service identity key");
910 teardown_capture_of_logs();
912 /* Set a service for this circuit. */
913 service
= helper_create_service();
914 ed25519_pubkey_copy(&circ
->hs_ident
->identity_pk
,
915 &service
->keys
.identity_pk
);
916 /* No introduction point associated to it. */
917 setup_full_capture_of_logs(LOG_WARN
);
918 ret
= hs_service_receive_introduce2(circ
, payload
, sizeof(payload
));
919 tt_int_op(ret
, OP_EQ
, -1);
920 expect_log_msg_containing("Unknown introduction auth key when handling "
921 "an INTRODUCE2 cell on circuit");
922 teardown_capture_of_logs();
924 /* Set an IP object now for this circuit. */
926 ip
= helper_create_service_ip();
927 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
928 /* Update ident to contain the intro point auth key. */
929 ed25519_pubkey_copy(&circ
->hs_ident
->intro_auth_pk
,
930 &ip
->auth_key_kp
.pubkey
);
933 /* This will fail because receiving an INTRODUCE2 cell implies a valid cell
934 * and then launching circuits so let's not do that and instead test that
935 * behaviour differently. */
936 ret
= hs_service_receive_introduce2(circ
, payload
, sizeof(payload
));
937 tt_int_op(ret
, OP_EQ
, -1);
938 tt_u64_op(ip
->introduce2_count
, OP_EQ
, 0);
941 or_state_free(dummy_state
);
944 circuit_free_(TO_CIRCUIT(circ
));
946 UNMOCK(circuit_mark_for_close_
);
949 /** Test basic hidden service housekeeping operations (maintaining intro
952 test_service_event(void *arg
)
954 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
955 time_t now
= time(NULL
);
956 hs_service_t
*service
;
957 origin_circuit_t
*circ
= NULL
;
962 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
964 circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO
, flags
);
966 /* Set a service for this circuit. */
967 service
= helper_create_service();
968 ed25519_pubkey_copy(&circ
->hs_ident
->identity_pk
,
969 &service
->keys
.identity_pk
);
971 /* Currently this consists of cleaning invalid intro points. So adding IPs
972 * here that should get cleaned up. */
974 hs_service_intro_point_t
*ip
= helper_create_service_ip();
975 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
976 /* This run will remove the IP because we have no circuits nor node_t
977 * associated with it. */
978 run_housekeeping_event(now
);
979 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
981 /* We'll trigger a removal because we've reached our maximum amount of
982 * times we should retry a circuit. For this, we need to have a node_t
983 * that matches the identity of this IP. */
985 memset(&ri
, 0, sizeof(ri
));
986 ip
= helper_create_service_ip();
987 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
988 memset(ri
.cache_info
.identity_digest
, 'A', DIGEST_LEN
);
989 /* This triggers a node_t creation. */
990 tt_assert(nodelist_set_routerinfo(&ri
, NULL
));
991 ip
->circuit_retries
= MAX_INTRO_POINT_CIRCUIT_RETRIES
+ 1;
992 run_housekeeping_event(now
);
993 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
995 /* No removal but no circuit so this means the IP object will stay in the
996 * descriptor map so we can retry it. */
997 ip
= helper_create_service_ip();
998 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
999 ip
->circuit_established
= 1; /* We'll test that, it MUST be 0 after. */
1000 run_housekeeping_event(now
);
1001 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1003 /* Remove the IP object at once for the next test. */
1004 ip
->circuit_retries
= MAX_INTRO_POINT_CIRCUIT_RETRIES
+ 1;
1005 run_housekeeping_event(now
);
1006 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1008 /* Now, we'll create an IP with a registered circuit. The IP object
1009 * shouldn't go away. */
1010 ip
= helper_create_service_ip();
1011 service_intro_point_add(service
->desc_current
->intro_points
.map
, ip
);
1012 ed25519_pubkey_copy(&circ
->hs_ident
->intro_auth_pk
,
1013 &ip
->auth_key_kp
.pubkey
);
1014 hs_circuitmap_register_intro_circ_v3_service_side(
1015 circ
, &ip
->auth_key_kp
.pubkey
);
1016 run_housekeeping_event(now
);
1017 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1019 /* We'll mangle the IP object to expire. */
1020 ip
->time_to_expire
= now
;
1021 run_housekeeping_event(now
);
1022 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1027 hs_circuitmap_remove_circuit(TO_CIRCUIT(circ
));
1028 circuit_free_(TO_CIRCUIT(circ
));
1030 UNMOCK(circuit_mark_for_close_
);
1033 /** Test that we rotate descriptors correctly. */
1035 test_rotate_descriptors(void *arg
)
1038 time_t next_rotation_time
, now
= time(NULL
);
1039 hs_service_t
*service
;
1040 hs_service_descriptor_t
*desc_next
;
1044 dummy_state
= tor_malloc_zero(sizeof(or_state_t
));
1047 MOCK(get_or_state
, get_or_state_replacement
);
1048 MOCK(circuit_mark_for_close_
, mock_circuit_mark_for_close
);
1049 MOCK(networkstatus_get_live_consensus
,
1050 mock_networkstatus_get_live_consensus
);
1052 /* Descriptor rotation happens with a consensus with a new SRV. */
1054 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
1055 &mock_ns
.valid_after
);
1056 tt_int_op(ret
, OP_EQ
, 0);
1057 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
1058 &mock_ns
.fresh_until
);
1059 tt_int_op(ret
, OP_EQ
, 0);
1060 dirvote_recalculate_timing(get_options(), mock_ns
.valid_after
);
1062 /* Create a service with a default descriptor and state. It's added to the
1064 service
= helper_create_service();
1065 service_descriptor_free(service
->desc_current
);
1066 service
->desc_current
= NULL
;
1067 /* This triggers a build for both descriptors. The time now is only used in
1068 * the descriptor certificate which is important to be now else the decoding
1069 * will complain that the cert has expired if we use valid_after. */
1070 build_all_descriptors(now
);
1071 tt_assert(service
->desc_current
);
1072 tt_assert(service
->desc_next
);
1074 /* Tweak our service next rotation time so we can use a custom time. */
1075 service
->state
.next_rotation_time
= next_rotation_time
=
1076 mock_ns
.valid_after
+ (11 * 60 * 60);
1078 /* Nothing should happen, we are not at a new SRV. Our next rotation time
1079 * should be untouched. */
1080 rotate_all_descriptors(mock_ns
.valid_after
);
1081 tt_u64_op(service
->state
.next_rotation_time
, OP_EQ
, next_rotation_time
);
1082 tt_assert(service
->desc_current
);
1083 tt_assert(service
->desc_next
);
1084 tt_u64_op(service
->desc_current
->time_period_num
, OP_EQ
,
1085 hs_get_previous_time_period_num(0));
1086 tt_u64_op(service
->desc_next
->time_period_num
, OP_EQ
,
1087 hs_get_time_period_num(0));
1088 /* Keep a reference so we can compare it after rotation to the current. */
1089 desc_next
= service
->desc_next
;
1091 /* Going right after a new SRV. */
1092 ret
= parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC",
1093 &mock_ns
.valid_after
);
1094 tt_int_op(ret
, OP_EQ
, 0);
1095 ret
= parse_rfc1123_time("Sat, 27 Oct 1985 02:00:00 UTC",
1096 &mock_ns
.fresh_until
);
1097 tt_int_op(ret
, OP_EQ
, 0);
1098 dirvote_recalculate_timing(get_options(), mock_ns
.valid_after
);
1100 /* Note down what to expect for the next rotation time which is 01:00 + 23h
1101 * meaning 00:00:00. */
1102 next_rotation_time
= mock_ns
.valid_after
+ (23 * 60 * 60);
1103 /* We should have our next rotation time modified, our current descriptor
1104 * cleaned up and the next descriptor becoming the current. */
1105 rotate_all_descriptors(mock_ns
.valid_after
);
1106 tt_u64_op(service
->state
.next_rotation_time
, OP_EQ
, next_rotation_time
);
1107 tt_mem_op(service
->desc_current
, OP_EQ
, desc_next
, sizeof(*desc_next
));
1108 tt_assert(service
->desc_next
== NULL
);
1110 /* A second time should do nothing. */
1111 rotate_all_descriptors(mock_ns
.valid_after
);
1112 tt_u64_op(service
->state
.next_rotation_time
, OP_EQ
, next_rotation_time
);
1113 tt_mem_op(service
->desc_current
, OP_EQ
, desc_next
, sizeof(*desc_next
));
1114 tt_assert(service
->desc_next
== NULL
);
1116 build_all_descriptors(now
);
1117 tt_mem_op(service
->desc_current
, OP_EQ
, desc_next
, sizeof(*desc_next
));
1118 tt_u64_op(service
->desc_current
->time_period_num
, OP_EQ
,
1119 hs_get_time_period_num(0));
1120 tt_u64_op(service
->desc_next
->time_period_num
, OP_EQ
,
1121 hs_get_next_time_period_num(0));
1122 tt_assert(service
->desc_next
);
1126 UNMOCK(get_or_state
);
1127 UNMOCK(circuit_mark_for_close_
);
1128 UNMOCK(networkstatus_get_live_consensus
);
1131 /** Test building descriptors: picking intro points, setting up their link
1132 * specifiers, etc. */
1134 test_build_update_descriptors(void *arg
)
1137 time_t now
= time(NULL
);
1139 hs_service_t
*service
;
1140 hs_service_intro_point_t
*ip_cur
, *ip_next
;
1148 get_or_state_replacement
);
1149 MOCK(networkstatus_get_live_consensus
,
1150 mock_networkstatus_get_live_consensus
);
1152 dummy_state
= tor_malloc_zero(sizeof(or_state_t
));
1154 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
1155 &mock_ns
.valid_after
);
1156 tt_int_op(ret
, OP_EQ
, 0);
1157 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
1158 &mock_ns
.fresh_until
);
1159 tt_int_op(ret
, OP_EQ
, 0);
1160 dirvote_recalculate_timing(get_options(), mock_ns
.valid_after
);
1162 /* Create a service without a current descriptor to trigger a build. */
1163 service
= helper_create_service();
1165 /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
1166 service_descriptor_free(service
->desc_current
);
1167 service
->desc_current
= NULL
;
1169 /* We have a fresh service so this should trigger a build for both
1170 * descriptors for specific time period that we'll test. */
1171 build_all_descriptors(now
);
1172 /* Check *current* descriptor. */
1173 tt_assert(service
->desc_current
);
1174 tt_assert(service
->desc_current
->desc
);
1175 tt_assert(service
->desc_current
->intro_points
.map
);
1176 /* The current time period is the one expected when starting at 03:00. */
1177 tt_u64_op(service
->desc_current
->time_period_num
, OP_EQ
,
1178 hs_get_time_period_num(0));
1179 /* This should be untouched, the update descriptor process changes it. */
1180 tt_u64_op(service
->desc_current
->next_upload_time
, OP_EQ
, 0);
1182 /* Check *next* descriptor. */
1183 tt_assert(service
->desc_next
);
1184 tt_assert(service
->desc_next
->desc
);
1185 tt_assert(service
->desc_next
->intro_points
.map
);
1186 tt_assert(service
->desc_current
!= service
->desc_next
);
1187 tt_u64_op(service
->desc_next
->time_period_num
, OP_EQ
,
1188 hs_get_next_time_period_num(0));
1189 /* This should be untouched, the update descriptor process changes it. */
1190 tt_u64_op(service
->desc_next
->next_upload_time
, OP_EQ
, 0);
1192 /* Time to test the update of those descriptors. At first, we have no node
1193 * in the routerlist so this will find NO suitable node for the IPs. */
1194 setup_full_capture_of_logs(LOG_INFO
);
1195 update_all_descriptors(now
);
1196 expect_log_msg_containing("Unable to find a suitable node to be an "
1197 "introduction point for service");
1198 teardown_capture_of_logs();
1199 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1201 tt_int_op(digest256map_size(service
->desc_next
->intro_points
.map
),
1204 /* Now, we'll setup a node_t. */
1206 tor_addr_t ipv4_addr
;
1207 curve25519_secret_key_t curve25519_secret_key
;
1209 memset(&ri
, 0, sizeof(routerinfo_t
));
1211 tor_addr_parse(&ipv4_addr
, "127.0.0.1");
1212 ri
.addr
= tor_addr_to_ipv4h(&ipv4_addr
);
1214 ri
.purpose
= ROUTER_PURPOSE_GENERAL
;
1215 /* Ugly yes but we never free the "ri" object so this just makes things
1217 ri
.protocol_list
= (char *) "HSDir=1-2 LinkAuth=3";
1218 summarize_protover_flags(&ri
.pv
, ri
.protocol_list
, NULL
);
1219 ret
= curve25519_secret_key_generate(&curve25519_secret_key
, 0);
1220 tt_int_op(ret
, OP_EQ
, 0);
1221 ri
.onion_curve25519_pkey
=
1222 tor_malloc_zero(sizeof(curve25519_public_key_t
));
1223 ri
.onion_pkey
= crypto_pk_new();
1224 curve25519_public_key_generate(ri
.onion_curve25519_pkey
,
1225 &curve25519_secret_key
);
1226 memset(ri
.cache_info
.identity_digest
, 'A', DIGEST_LEN
);
1227 /* Setup ed25519 identity */
1228 ed25519_keypair_t kp1
;
1229 ed25519_keypair_generate(&kp1
, 0);
1230 ri
.cache_info
.signing_key_cert
= tor_malloc_zero(sizeof(tor_cert_t
));
1231 tt_assert(ri
.cache_info
.signing_key_cert
);
1232 ed25519_pubkey_copy(&ri
.cache_info
.signing_key_cert
->signing_key
,
1234 nodelist_set_routerinfo(&ri
, NULL
);
1235 node
= node_get_mutable_by_id(ri
.cache_info
.identity_digest
);
1237 node
->is_running
= node
->is_valid
= node
->is_fast
= node
->is_stable
= 1;
1240 /* We have to set this, or the lack of microdescriptors for these
1241 * nodes will make them unusable. */
1242 get_options_mutable()->UseMicrodescriptors
= 0;
1244 /* We expect to pick only one intro point from the node above. */
1245 setup_full_capture_of_logs(LOG_INFO
);
1246 update_all_descriptors(now
);
1247 tor_free(node
->ri
->onion_curve25519_pkey
); /* Avoid memleak. */
1248 tor_free(node
->ri
->cache_info
.signing_key_cert
);
1249 crypto_pk_free(node
->ri
->onion_pkey
);
1250 expect_log_msg_containing("just picked 1 intro points and wanted 3 for next "
1251 "descriptor. It currently has 0 intro points. "
1252 "Launching ESTABLISH_INTRO circuit shortly.");
1253 teardown_capture_of_logs();
1254 tt_int_op(digest256map_size(service
->desc_current
->intro_points
.map
),
1256 tt_int_op(digest256map_size(service
->desc_next
->intro_points
.map
),
1258 /* Get the IP object. Because we don't have the auth key of the IP, we can't
1259 * query it so get the first element in the map. */
1263 digest256map_iter_t
*iter
=
1264 digest256map_iter_init(service
->desc_current
->intro_points
.map
);
1265 digest256map_iter_get(iter
, &key
, &obj
);
1268 /* Get also the IP from the next descriptor. We'll make sure it's not the
1269 * same object as in the current descriptor. */
1270 iter
= digest256map_iter_init(service
->desc_next
->intro_points
.map
);
1271 digest256map_iter_get(iter
, &key
, &obj
);
1275 tt_mem_op(ip_cur
, OP_NE
, ip_next
, sizeof(hs_desc_intro_point_t
));
1277 /* We won't test the service IP object because there is a specific test
1278 * already for this but we'll make sure that the state is coherent.*/
1280 /* Three link specifiers are mandatoy so make sure we do have them. */
1281 tt_int_op(smartlist_len(ip_cur
->base
.link_specifiers
), OP_EQ
, 3);
1282 /* Make sure we have a valid encryption keypair generated when we pick an
1283 * intro point in the update process. */
1284 tt_assert(!tor_mem_is_zero((char *) ip_cur
->enc_key_kp
.seckey
.secret_key
,
1285 CURVE25519_SECKEY_LEN
));
1286 tt_assert(!tor_mem_is_zero((char *) ip_cur
->enc_key_kp
.pubkey
.public_key
,
1287 CURVE25519_PUBKEY_LEN
));
1288 tt_u64_op(ip_cur
->time_to_expire
, OP_GE
, now
+
1289 INTRO_POINT_LIFETIME_MIN_SECONDS
);
1290 tt_u64_op(ip_cur
->time_to_expire
, OP_LE
, now
+
1291 INTRO_POINT_LIFETIME_MAX_SECONDS
);
1293 /* Now, we will try to set up a service after a new time period has started
1294 * and see if it behaves as expected. */
1296 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
1297 &mock_ns
.valid_after
);
1298 tt_int_op(ret
, OP_EQ
, 0);
1299 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
1300 &mock_ns
.fresh_until
);
1301 tt_int_op(ret
, OP_EQ
, 0);
1303 /* Create a service without a current descriptor to trigger a build. */
1304 service
= helper_create_service();
1306 /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
1307 service_descriptor_free(service
->desc_current
);
1308 service
->desc_current
= NULL
;
1310 /* We have a fresh service so this should trigger a build for both
1311 * descriptors for specific time period that we'll test. */
1312 build_all_descriptors(now
);
1313 /* Check *current* descriptor. */
1314 tt_assert(service
->desc_current
);
1315 tt_assert(service
->desc_current
->desc
);
1316 tt_assert(service
->desc_current
->intro_points
.map
);
1317 /* This should be for the previous time period. */
1318 tt_u64_op(service
->desc_current
->time_period_num
, OP_EQ
,
1319 hs_get_previous_time_period_num(0));
1320 /* This should be untouched, the update descriptor process changes it. */
1321 tt_u64_op(service
->desc_current
->next_upload_time
, OP_EQ
, 0);
1323 /* Check *next* descriptor. */
1324 tt_assert(service
->desc_next
);
1325 tt_assert(service
->desc_next
->desc
);
1326 tt_assert(service
->desc_next
->intro_points
.map
);
1327 tt_assert(service
->desc_current
!= service
->desc_next
);
1328 tt_u64_op(service
->desc_next
->time_period_num
, OP_EQ
,
1329 hs_get_time_period_num(0));
1330 /* This should be untouched, the update descriptor process changes it. */
1331 tt_u64_op(service
->desc_next
->next_upload_time
, OP_EQ
, 0);
1333 /* Let's remove the next descriptor to simulate a rotation. */
1334 service_descriptor_free(service
->desc_next
);
1335 service
->desc_next
= NULL
;
1337 build_all_descriptors(now
);
1338 /* Check *next* descriptor. */
1339 tt_assert(service
->desc_next
);
1340 tt_assert(service
->desc_next
->desc
);
1341 tt_assert(service
->desc_next
->intro_points
.map
);
1342 tt_assert(service
->desc_current
!= service
->desc_next
);
1343 tt_u64_op(service
->desc_next
->time_period_num
, OP_EQ
,
1344 hs_get_next_time_period_num(0));
1345 /* This should be untouched, the update descriptor process changes it. */
1346 tt_u64_op(service
->desc_next
->next_upload_time
, OP_EQ
, 0);
1350 nodelist_free_all();
1354 test_upload_descriptors(void *arg
)
1357 time_t now
= time(NULL
);
1358 hs_service_t
*service
;
1364 get_or_state_replacement
);
1365 MOCK(networkstatus_get_live_consensus
,
1366 mock_networkstatus_get_live_consensus
);
1368 dummy_state
= tor_malloc_zero(sizeof(or_state_t
));
1370 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
1371 &mock_ns
.valid_after
);
1372 tt_int_op(ret
, OP_EQ
, 0);
1373 ret
= parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
1374 &mock_ns
.fresh_until
);
1375 tt_int_op(ret
, OP_EQ
, 0);
1377 /* Create a service with no descriptor. It's added to the global map. */
1378 service
= hs_service_new(get_options());
1380 service
->config
.version
= HS_VERSION_THREE
;
1381 ed25519_secret_key_generate(&service
->keys
.identity_sk
, 0);
1382 ed25519_public_key_generate(&service
->keys
.identity_pk
,
1383 &service
->keys
.identity_sk
);
1384 /* Register service to global map. */
1385 ret
= register_service(get_hs_service_map(), service
);
1386 tt_int_op(ret
, OP_EQ
, 0);
1387 /* But first, build our descriptor. */
1388 build_all_descriptors(now
);
1390 /* Nothing should happen because we have 0 introduction circuit established
1391 * and we want (by default) 3 intro points. */
1392 run_upload_descriptor_event(now
);
1393 /* If no upload happened, this should be untouched. */
1394 tt_u64_op(service
->desc_current
->next_upload_time
, OP_EQ
, 0);
1395 /* We'll simulate that we've opened our intro point circuit and that we only
1396 * want one intro point. */
1397 service
->config
.num_intro_points
= 1;
1399 /* Set our next upload time after now which will skip the upload. */
1400 service
->desc_current
->next_upload_time
= now
+ 1000;
1401 run_upload_descriptor_event(now
);
1402 /* If no upload happened, this should be untouched. */
1403 tt_u64_op(service
->desc_current
->next_upload_time
, OP_EQ
, now
+ 1000);
1407 UNMOCK(get_or_state
);
1410 /** Test the functions that save and load HS revision counters to state. */
1412 test_revision_counter_state(void *arg
)
1414 char *state_line_one
= NULL
;
1415 char *state_line_two
= NULL
;
1417 hs_service_descriptor_t
*desc_one
= service_descriptor_new();
1418 hs_service_descriptor_t
*desc_two
= service_descriptor_new();
1422 /* Prepare both descriptors */
1423 desc_one
->desc
->plaintext_data
.revision_counter
= 42;
1424 desc_two
->desc
->plaintext_data
.revision_counter
= 240;
1425 memset(&desc_one
->blinded_kp
.pubkey
.pubkey
, 66,
1426 sizeof(desc_one
->blinded_kp
.pubkey
.pubkey
));
1427 memset(&desc_two
->blinded_kp
.pubkey
.pubkey
, 240,
1428 sizeof(desc_one
->blinded_kp
.pubkey
.pubkey
));
1430 /* Turn the descriptor rev counters into state lines */
1431 state_line_one
= encode_desc_rev_counter_for_state(desc_one
);
1432 tt_str_op(state_line_one
, OP_EQ
,
1433 "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkI 42");
1435 state_line_two
= encode_desc_rev_counter_for_state(desc_two
);
1436 tt_str_op(state_line_two
, OP_EQ
,
1437 "8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PA 240");
1439 /* Now let's test our state parsing function: */
1441 uint64_t cached_rev_counter
;
1443 /* First's try with wrong pubkey and check that no service was found */
1444 cached_rev_counter
=check_state_line_for_service_rev_counter(state_line_one
,
1445 &desc_two
->blinded_kp
.pubkey
,
1447 tt_int_op(service_found
, OP_EQ
, 0);
1448 tt_u64_op(cached_rev_counter
, OP_EQ
, 0);
1450 /* Now let's try with the right pubkeys */
1451 cached_rev_counter
=check_state_line_for_service_rev_counter(state_line_one
,
1452 &desc_one
->blinded_kp
.pubkey
,
1454 tt_int_op(service_found
, OP_EQ
, 1);
1455 tt_u64_op(cached_rev_counter
, OP_EQ
, 42);
1457 cached_rev_counter
=check_state_line_for_service_rev_counter(state_line_two
,
1458 &desc_two
->blinded_kp
.pubkey
,
1460 tt_int_op(service_found
, OP_EQ
, 1);
1461 tt_u64_op(cached_rev_counter
, OP_EQ
, 240);
1464 tor_free(state_line_one
);
1465 tor_free(state_line_two
);
1466 service_descriptor_free(desc_one
);
1467 service_descriptor_free(desc_two
);
1470 /** Global vars used by test_rendezvous1_parsing() */
1471 static char rend1_payload
[RELAY_PAYLOAD_SIZE
];
1472 static size_t rend1_payload_len
= 0;
1474 /** Mock for relay_send_command_from_edge() to send a RENDEZVOUS1 cell. Instead
1475 * of sending it to the network, instead save it to the global `rend1_payload`
1476 * variable so that we can inspect it in the test_rendezvous1_parsing()
1479 mock_relay_send_rendezvous1(streamid_t stream_id
, circuit_t
*circ
,
1480 uint8_t relay_command
, const char *payload
,
1482 crypt_path_t
*cpath_layer
,
1483 const char *filename
, int lineno
)
1487 (void) relay_command
;
1492 memcpy(rend1_payload
, payload
, payload_len
);
1493 rend1_payload_len
= payload_len
;
1498 /** Send a RENDEZVOUS1 as a service, and parse it as a client. */
1500 test_rendezvous1_parsing(void *arg
)
1503 static const char *test_addr
=
1504 "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad.onion";
1505 hs_service_t
*service
= NULL
;
1506 origin_circuit_t
*service_circ
= NULL
;
1507 origin_circuit_t
*client_circ
= NULL
;
1508 ed25519_keypair_t ip_auth_kp
;
1509 curve25519_keypair_t ephemeral_kp
;
1510 curve25519_keypair_t client_kp
;
1511 curve25519_keypair_t ip_enc_kp
;
1512 int flags
= CIRCLAUNCH_NEED_UPTIME
| CIRCLAUNCH_IS_INTERNAL
;
1516 MOCK(relay_send_command_from_edge_
, mock_relay_send_rendezvous1
);
1519 /* Let's start by setting up the service that will start the rend */
1520 service
= tor_malloc_zero(sizeof(hs_service_t
));
1521 ed25519_secret_key_generate(&service
->keys
.identity_sk
, 0);
1522 ed25519_public_key_generate(&service
->keys
.identity_pk
,
1523 &service
->keys
.identity_sk
);
1524 memcpy(service
->onion_address
, test_addr
, sizeof(service
->onion_address
));
1529 /* Now let's set up the service rendezvous circuit and its keys. */
1530 service_circ
= helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND
,
1532 tor_free(service_circ
->hs_ident
);
1533 hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys
;
1534 uint8_t rendezvous_cookie
[HS_REND_COOKIE_LEN
];
1535 curve25519_keypair_generate(&ip_enc_kp
, 0);
1536 curve25519_keypair_generate(&ephemeral_kp
, 0);
1537 curve25519_keypair_generate(&client_kp
, 0);
1538 ed25519_keypair_generate(&ip_auth_kp
, 0);
1539 retval
= hs_ntor_service_get_rendezvous1_keys(&ip_auth_kp
.pubkey
,
1543 &hs_ntor_rend_cell_keys
);
1544 tt_int_op(retval
, OP_EQ
, 0);
1546 memset(rendezvous_cookie
, 2, sizeof(rendezvous_cookie
));
1547 service_circ
->hs_ident
=
1548 create_rp_circuit_identifier(service
, rendezvous_cookie
,
1549 &ephemeral_kp
.pubkey
,
1550 &hs_ntor_rend_cell_keys
);
1553 /* Send out the RENDEZVOUS1 and make sure that our mock func worked */
1554 tt_assert(tor_mem_is_zero(rend1_payload
, 32));
1555 hs_circ_service_rp_has_opened(service
, service_circ
);
1556 tt_assert(!tor_mem_is_zero(rend1_payload
, 32));
1557 tt_int_op(rend1_payload_len
, OP_EQ
, HS_LEGACY_RENDEZVOUS_CELL_SIZE
);
1559 /******************************/
1561 /** Now let's create the client rendezvous circuit */
1563 helper_create_origin_circuit(CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED
,
1565 /* fix up its circ ident */
1566 ed25519_pubkey_copy(&client_circ
->hs_ident
->intro_auth_pk
,
1567 &ip_auth_kp
.pubkey
);
1568 memcpy(&client_circ
->hs_ident
->rendezvous_client_kp
,
1569 &client_kp
, sizeof(client_circ
->hs_ident
->rendezvous_client_kp
));
1570 memcpy(&client_circ
->hs_ident
->intro_enc_pk
.public_key
,
1571 &ip_enc_kp
.pubkey
.public_key
,
1572 sizeof(client_circ
->hs_ident
->intro_enc_pk
.public_key
));
1574 /* Now parse the rendezvous2 circuit and make sure it was fine. We are
1575 * skipping 20 bytes off its payload, since that's the rendezvous cookie
1576 * which is only present in REND1. */
1577 retval
= handle_rendezvous2(client_circ
,
1578 (uint8_t*)rend1_payload
+20,
1579 rend1_payload_len
-20);
1580 tt_int_op(retval
, OP_EQ
, 0);
1582 /* TODO: We are only simulating client/service here. We could also simulate
1583 * the rendezvous point by plugging in rend_mid_establish_rendezvous(). We
1584 * would need an extra circuit and some more stuff but it's doable. */
1587 circuit_free_(TO_CIRCUIT(service_circ
));
1588 circuit_free_(TO_CIRCUIT(client_circ
));
1589 hs_service_free(service
);
1591 UNMOCK(relay_send_command_from_edge_
);
1594 struct testcase_t hs_service_tests
[] = {
1595 { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup
, TT_FORK
,
1597 { "load_keys", test_load_keys
, TT_FORK
,
1599 { "access_service", test_access_service
, TT_FORK
,
1601 { "service_intro_point", test_service_intro_point
, TT_FORK
,
1603 { "helper_functions", test_helper_functions
, TT_FORK
,
1605 { "intro_circuit_opened", test_intro_circuit_opened
, TT_FORK
,
1607 { "intro_established", test_intro_established
, TT_FORK
,
1609 { "closing_intro_circs", test_closing_intro_circs
, TT_FORK
,
1611 { "rdv_circuit_opened", test_rdv_circuit_opened
, TT_FORK
,
1613 { "introduce2", test_introduce2
, TT_FORK
,
1615 { "service_event", test_service_event
, TT_FORK
,
1617 { "rotate_descriptors", test_rotate_descriptors
, TT_FORK
,
1619 { "build_update_descriptors", test_build_update_descriptors
, TT_FORK
,
1621 { "upload_descriptors", test_upload_descriptors
, TT_FORK
,
1623 { "revision_counter_state", test_revision_counter_state
, TT_FORK
,
1625 { "rendezvous1_parsing", test_rendezvous1_parsing
, TT_FORK
,