Remove now useless rev counter state file code.
[tor.git] / src / test / test_hs_service.c
blob0350face460781bc313dda5ab09e554b55a0c325
1 /* Copyright (c) 2016-2018, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file test_hs_service.c
6 * \brief Test hidden service functionality.
7 */
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
18 #define MAIN_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"
31 #include "or/or.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"
40 #include "or/relay.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"
50 #include "or/main.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"
63 /* Trunnel */
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)
71 (void) now;
72 return &mock_ns;
75 static or_state_t *dummy_state = NULL;
77 /* Mock function to get fake or state (used for rev counters) */
78 static or_state_t *
79 get_or_state_replacement(void)
81 return dummy_state;
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. */
86 static void
87 mock_circuit_mark_for_close(circuit_t *circ, int reason, int line,
88 const char *file)
90 (void) circ;
91 (void) reason;
92 (void) line;
93 (void) file;
94 return;
97 static int
98 mock_relay_send_command_from_edge(streamid_t stream_id, circuit_t *circ,
99 uint8_t relay_command, const char *payload,
100 size_t payload_len,
101 crypt_path_t *cpath_layer,
102 const char *filename, int lineno)
104 (void) stream_id;
105 (void) circ;
106 (void) relay_command;
107 (void) payload;
108 (void) payload_len;
109 (void) cpath_layer;
110 (void) filename;
111 (void) lineno;
112 return 0;
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. */
117 static int
118 helper_config_service(const char *conf)
120 int ret = 0;
121 or_options_t *options = NULL;
122 tt_assert(conf);
123 options = helper_parse_options(conf);
124 tt_assert(options);
125 ret = hs_config_service_all(options, 0);
126 done:
127 or_options_free(options);
128 return ret;
131 /* Test: Ensure that setting up rendezvous circuits works correctly. */
132 static void
133 test_e2e_rend_circuit_setup(void *arg)
135 ed25519_public_key_t service_pk;
136 origin_circuit_t *or_circ;
137 int retval;
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 */
143 (void) arg;
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);
193 done:
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);
206 tor_assert(circ);
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;
213 /* Random nonce. */
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));
218 return circ;
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());
230 tor_assert(service);
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);
241 done:
242 return service;
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);
252 tor_assert(ip);
253 /* Add a first unused link specifier. */
254 ls = tor_malloc_zero(sizeof(*ls));
255 ls->type = LS_IPV4;
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);
263 return ip;
266 static void
267 test_load_keys(void *arg)
269 int ret;
270 char *conf = NULL;
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];
275 (void) arg;
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. */
280 hs_init();
282 #define conf_fmt \
283 "HiddenServiceDir %s\n" \
284 "HiddenServiceVersion %d\n" \
285 "HiddenServicePort 65535\n"
287 /* v2 service. */
288 tor_asprintf(&conf, conf_fmt, hsdir_v2, HS_VERSION_TWO);
289 ret = helper_config_service(conf);
290 tor_free(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);
296 /* v3 service. */
297 tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
298 ret = helper_config_service(conf);
299 tor_free(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();
309 tt_assert(s);
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);
323 done:
324 tor_free(hsdir_v2);
325 tor_free(hsdir_v3);
326 hs_free_all();
329 static void
330 test_access_service(void *arg)
332 int ret;
333 char *conf = NULL;
334 char *hsdir_v3 = tor_strdup(get_fname("hs3"));
335 hs_service_ht *global_map;
336 hs_service_t *s = NULL;
338 (void) arg;
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. */
343 hs_init();
345 #define conf_fmt \
346 "HiddenServiceDir %s\n" \
347 "HiddenServiceVersion %d\n" \
348 "HiddenServicePort 65535\n"
350 /* v3 service. */
351 tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
352 ret = helper_config_service(conf);
353 tor_free(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();
363 tt_assert(s);
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);
369 tt_assert(query);
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();
395 done:
396 hs_service_free(s);
397 tor_free(hsdir_v3);
398 hs_free_all();
401 /** Test that we can create intro point objects, index them and find them */
402 static void
403 test_service_intro_point(void *arg)
405 hs_service_t *service = NULL;
406 hs_service_intro_point_t *ip = NULL;
408 (void) arg;
410 /* Test simple creation of an object. */
412 time_t now = time(NULL);
413 ip = helper_create_service_ip();
414 tt_assert(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());
446 tt_assert(service);
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);
468 done:
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)
480 (void) 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)) {
484 return &mock_node;
486 return NULL;
489 static void
490 test_helper_functions(void *arg)
492 int ret;
493 hs_service_t *service = NULL;
494 hs_service_intro_point_t *ip = NULL;
495 hs_ident_circuit_t ident;
497 (void) arg;
499 MOCK(node_get_by_id, mock_node_get_by_id);
501 hs_service_init();
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));
525 /* Reset */
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));
533 /* Reset. */
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);
610 done:
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 */
617 static void
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;
624 (void) arg;
626 hs_init();
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,
631 flags);
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();
667 done:
668 circuit_free_(TO_CIRCUIT(circ));
669 hs_free_all();
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 */
676 static void
677 test_intro_established(void *arg)
679 int ret;
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;
686 (void) arg;
688 hs_init();
689 MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
691 circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
692 flags);
693 tt_assert(circ);
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);
741 done:
742 if (circ)
743 circuit_free_(TO_CIRCUIT(circ));
744 hs_free_all();
745 UNMOCK(circuit_mark_for_close_);
748 /** Check the operations we do on a rendezvous circuit after we learn it's
749 * open */
750 static void
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;
757 (void) arg;
759 hs_init();
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);
784 done:
785 circuit_free_(TO_CIRCUIT(circ));
786 hs_free_all();
787 UNMOCK(circuit_mark_for_close_);
788 UNMOCK(relay_send_command_from_edge_);
791 static void
792 mock_assert_circuit_ok(const circuit_t *c)
794 (void) c;
795 return;
798 /** Test for the general mechanism for closing intro circs.
799 * Also a way to identify that #23603 has been fixed. */
800 static void
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;
808 (void) arg;
810 MOCK(assert_circuit_ok, mock_assert_circuit_ok);
812 hs_init();
814 /* Initialize service */
815 service = helper_create_service();
816 /* Initialize intro point */
817 ip = helper_create_service_ip();
818 tt_assert(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);
828 tmp_circ =
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));
839 intro_circ = NULL;
840 entry = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
841 tt_assert(entry);
842 /* The free should also remove the circuit from the circuitmap. */
843 tmp_circ =
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);
857 tmp_circ =
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. */
864 tmp_circ =
865 hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
866 tt_assert(!tmp_circ);
868 done:
869 if (intro_circ) {
870 circuit_free_(TO_CIRCUIT(intro_circ));
872 /* Frees the service object. */
873 hs_free_all();
874 UNMOCK(assert_circuit_ok);
877 /** Test sending and receiving introduce2 cells */
878 static void
879 test_introduce2(void *arg)
881 int ret;
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;
888 (void) arg;
890 hs_init();
891 MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
892 MOCK(get_or_state,
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);
898 tt_assert(circ);
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);
947 done:
948 or_state_free(dummy_state);
949 dummy_state = NULL;
950 if (circ)
951 circuit_free_(TO_CIRCUIT(circ));
952 hs_free_all();
953 UNMOCK(circuit_mark_for_close_);
956 /** Test basic hidden service housekeeping operations (maintaining intro
957 * points, etc) */
958 static void
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;
966 (void) arg;
968 hs_init();
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),
987 OP_EQ, 0);
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. */
991 routerinfo_t ri;
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),
1001 OP_EQ, 0);
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),
1009 OP_EQ, 1);
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),
1014 OP_EQ, 0);
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),
1025 OP_EQ, 1);
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),
1030 OP_EQ, 0);
1033 done:
1034 hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
1035 circuit_free_(TO_CIRCUIT(circ));
1036 hs_free_all();
1037 UNMOCK(circuit_mark_for_close_);
1040 /** Test that we rotate descriptors correctly. */
1041 static void
1042 test_rotate_descriptors(void *arg)
1044 int ret;
1045 time_t next_rotation_time, now;
1046 hs_service_t *service;
1047 hs_service_descriptor_t *desc_next;
1049 (void) arg;
1051 dummy_state = tor_malloc_zero(sizeof(or_state_t));
1053 hs_init();
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
1073 * global map. */
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);
1137 done:
1138 hs_free_all();
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. */
1146 static void
1147 test_build_update_descriptors(void *arg)
1149 int ret;
1150 time_t now = time(NULL);
1151 node_t *node;
1152 hs_service_t *service;
1153 hs_service_intro_point_t *ip_cur, *ip_next;
1154 routerinfo_t ri;
1156 (void) arg;
1158 hs_init();
1160 MOCK(get_or_state,
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();
1180 tt_assert(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),
1216 OP_EQ, 0);
1217 tt_int_op(digest256map_size(service->desc_next->intro_points.map),
1218 OP_EQ, 0);
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);
1229 ri.or_port = 1337;
1230 ri.purpose = ROUTER_PURPOSE_GENERAL;
1231 /* Ugly yes but we never free the "ri" object so this just makes things
1232 * easier. */
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,
1249 &kp1.pubkey);
1250 nodelist_set_routerinfo(&ri, NULL);
1251 node = node_get_mutable_by_id(ri.cache_info.identity_digest);
1252 tt_assert(node);
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),
1271 OP_EQ, 1);
1272 tt_int_op(digest256map_size(service->desc_next->intro_points.map),
1273 OP_EQ, 1);
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. */
1277 void *obj = NULL;
1278 const uint8_t *key;
1279 digest256map_iter_t *iter =
1280 digest256map_iter_init(service->desc_current->intro_points.map);
1281 digest256map_iter_get(iter, &key, &obj);
1282 tt_assert(obj);
1283 ip_cur = 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);
1288 tt_assert(obj);
1289 ip_next = 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();
1324 tt_assert(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);
1367 done:
1368 hs_free_all();
1369 nodelist_free_all();
1372 static void
1373 test_upload_descriptors(void *arg)
1375 int ret;
1376 time_t now;
1377 hs_service_t *service;
1379 (void) arg;
1381 hs_init();
1382 MOCK(get_or_state,
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());
1402 tt_assert(service);
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);
1428 done:
1429 hs_free_all();
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()
1440 * test. */
1441 static int
1442 mock_relay_send_rendezvous1(streamid_t stream_id, circuit_t *circ,
1443 uint8_t relay_command, const char *payload,
1444 size_t payload_len,
1445 crypt_path_t *cpath_layer,
1446 const char *filename, int lineno)
1448 (void) stream_id;
1449 (void) circ;
1450 (void) relay_command;
1451 (void) cpath_layer;
1452 (void) filename;
1453 (void) lineno;
1455 memcpy(rend1_payload, payload, payload_len);
1456 rend1_payload_len = payload_len;
1458 return 0;
1461 /** Send a RENDEZVOUS1 as a service, and parse it as a client. */
1462 static void
1463 test_rendezvous1_parsing(void *arg)
1465 int retval;
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;
1477 (void) arg;
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));
1488 tt_assert(service);
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,
1494 flags);
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,
1503 &ip_enc_kp,
1504 &ephemeral_kp,
1505 &client_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 */
1525 client_circ =
1526 helper_create_origin_circuit(CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED,
1527 flags);
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. */
1549 done:
1550 circuit_free_(TO_CIRCUIT(service_circ));
1551 circuit_free_(TO_CIRCUIT(client_circ));
1552 hs_service_free(service);
1553 hs_free_all();
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,
1559 NULL, NULL },
1560 { "load_keys", test_load_keys, TT_FORK,
1561 NULL, NULL },
1562 { "access_service", test_access_service, TT_FORK,
1563 NULL, NULL },
1564 { "service_intro_point", test_service_intro_point, TT_FORK,
1565 NULL, NULL },
1566 { "helper_functions", test_helper_functions, TT_FORK,
1567 NULL, NULL },
1568 { "intro_circuit_opened", test_intro_circuit_opened, TT_FORK,
1569 NULL, NULL },
1570 { "intro_established", test_intro_established, TT_FORK,
1571 NULL, NULL },
1572 { "closing_intro_circs", test_closing_intro_circs, TT_FORK,
1573 NULL, NULL },
1574 { "rdv_circuit_opened", test_rdv_circuit_opened, TT_FORK,
1575 NULL, NULL },
1576 { "introduce2", test_introduce2, TT_FORK,
1577 NULL, NULL },
1578 { "service_event", test_service_event, TT_FORK,
1579 NULL, NULL },
1580 { "rotate_descriptors", test_rotate_descriptors, TT_FORK,
1581 NULL, NULL },
1582 { "build_update_descriptors", test_build_update_descriptors, TT_FORK,
1583 NULL, NULL },
1584 { "upload_descriptors", test_upload_descriptors, TT_FORK,
1585 NULL, NULL },
1586 { "rendezvous1_parsing", test_rendezvous1_parsing, TT_FORK,
1587 NULL, NULL },
1589 END_OF_TESTCASES