2017-03-15 16:13:17 -04:00
|
|
|
/* Copyright (c) 2016-2017, The Tor Project, Inc. */
|
2016-09-05 19:03:30 +03:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file test_hs_service.c
|
|
|
|
* \brief Test hidden service functionality.
|
|
|
|
*/
|
|
|
|
|
2017-05-02 16:37:17 +03:00
|
|
|
#define CIRCUITBUILD_PRIVATE
|
|
|
|
#define CIRCUITLIST_PRIVATE
|
2017-02-02 15:26:04 -05:00
|
|
|
#define CONFIG_PRIVATE
|
2017-05-02 16:37:17 +03:00
|
|
|
#define CONNECTION_PRIVATE
|
|
|
|
#define CRYPTO_PRIVATE
|
2017-02-13 15:31:34 +02:00
|
|
|
#define HS_COMMON_PRIVATE
|
2017-02-02 15:26:04 -05:00
|
|
|
#define HS_SERVICE_PRIVATE
|
2016-09-05 19:03:30 +03:00
|
|
|
#define HS_INTROPOINT_PRIVATE
|
2017-05-02 16:37:17 +03:00
|
|
|
#define MAIN_PRIVATE
|
|
|
|
#define TOR_CHANNEL_INTERNAL_
|
2016-09-05 19:03:30 +03:00
|
|
|
|
|
|
|
#include "test.h"
|
2017-02-02 15:26:04 -05:00
|
|
|
#include "test_helpers.h"
|
2016-09-05 19:03:30 +03:00
|
|
|
#include "log_test_helpers.h"
|
2017-05-02 16:37:17 +03:00
|
|
|
#include "rend_test_helpers.h"
|
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
#include "channeltls.h"
|
|
|
|
#include "circuitbuild.h"
|
|
|
|
#include "circuitlist.h"
|
|
|
|
#include "circuituse.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "connection.h"
|
2017-02-02 15:26:04 -05:00
|
|
|
#include "crypto.h"
|
2017-05-02 16:37:17 +03:00
|
|
|
#include "hs_circuit.h"
|
2017-02-13 15:31:34 +02:00
|
|
|
#include "hs_common.h"
|
2017-02-02 15:26:04 -05:00
|
|
|
#include "hs_config.h"
|
2017-05-02 16:37:17 +03:00
|
|
|
#include "hs_ident.h"
|
2016-09-05 19:03:30 +03:00
|
|
|
#include "hs_intropoint.h"
|
2017-02-28 13:50:17 +02:00
|
|
|
#include "hs_ntor.h"
|
2017-05-02 16:37:17 +03:00
|
|
|
#include "hs_service.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "rendservice.h"
|
2017-02-28 13:50:17 +02:00
|
|
|
|
2017-02-02 15:26:04 -05:00
|
|
|
/* Trunnel */
|
|
|
|
#include "hs/cell_establish_intro.h"
|
|
|
|
|
|
|
|
/* Helper: from a set of options in conf, configure a service which will add
|
|
|
|
* it to the staging list of the HS subsytem. */
|
|
|
|
static int
|
|
|
|
helper_config_service(const char *conf)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
or_options_t *options = NULL;
|
|
|
|
tt_assert(conf);
|
|
|
|
options = helper_parse_options(conf);
|
|
|
|
tt_assert(options);
|
|
|
|
ret = hs_config_service_all(options, 0);
|
|
|
|
done:
|
|
|
|
or_options_free(options);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-02 16:37:17 +03:00
|
|
|
/* Test: Ensure that setting up rendezvous circuits works correctly. */
|
|
|
|
static void
|
|
|
|
test_e2e_rend_circuit_setup(void *arg)
|
|
|
|
{
|
|
|
|
ed25519_public_key_t service_pk;
|
|
|
|
origin_circuit_t *or_circ;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
/** In this test we create a v3 prop224 service-side rendezvous circuit.
|
|
|
|
* We simulate an HS ntor key exchange with a client, and check that
|
|
|
|
* the circuit was setup correctly and is ready to accept rendezvous data */
|
|
|
|
|
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
/* Now make dummy circuit */
|
|
|
|
{
|
|
|
|
or_circ = origin_circuit_new();
|
|
|
|
|
|
|
|
or_circ->base_.purpose = CIRCUIT_PURPOSE_S_CONNECT_REND;
|
|
|
|
|
|
|
|
or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
|
|
|
|
or_circ->build_state->is_internal = 1;
|
|
|
|
|
|
|
|
/* prop224: Setup hs conn identifier on the stream */
|
|
|
|
ed25519_secret_key_t sk;
|
|
|
|
tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
|
|
|
|
tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
|
|
|
|
|
|
|
|
or_circ->hs_ident = hs_ident_circuit_new(&service_pk,
|
|
|
|
HS_IDENT_CIRCUIT_RENDEZVOUS);
|
|
|
|
|
|
|
|
TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check number of hops */
|
|
|
|
retval = cpath_get_n_hops(&or_circ->cpath);
|
|
|
|
tt_int_op(retval, OP_EQ, 0);
|
|
|
|
|
|
|
|
/* Setup the circuit: do the ntor key exchange */
|
|
|
|
{
|
|
|
|
uint8_t ntor_key_seed[DIGEST256_LEN] = {2};
|
2017-07-06 16:23:30 +03:00
|
|
|
retval = hs_circuit_setup_e2e_rend_circ(or_circ,
|
2017-07-07 11:15:27 -04:00
|
|
|
ntor_key_seed, sizeof(ntor_key_seed),
|
|
|
|
1);
|
2017-05-02 16:37:17 +03:00
|
|
|
tt_int_op(retval, OP_EQ, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See that a hop was added to the circuit's cpath */
|
|
|
|
retval = cpath_get_n_hops(&or_circ->cpath);
|
|
|
|
tt_int_op(retval, OP_EQ, 1);
|
|
|
|
|
|
|
|
/* Check the digest algo */
|
2017-07-06 16:39:48 +03:00
|
|
|
tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->f_digest),
|
|
|
|
OP_EQ, DIGEST_SHA3_256);
|
|
|
|
tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->b_digest),
|
|
|
|
OP_EQ, DIGEST_SHA3_256);
|
2017-05-02 16:37:17 +03:00
|
|
|
tt_assert(or_circ->cpath->f_crypto);
|
|
|
|
tt_assert(or_circ->cpath->b_crypto);
|
|
|
|
|
|
|
|
/* Ensure that circ purpose was changed */
|
|
|
|
tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
|
|
|
|
|
|
|
|
done:
|
|
|
|
circuit_free(TO_CIRCUIT(or_circ));
|
|
|
|
}
|
|
|
|
|
2017-02-02 15:26:04 -05:00
|
|
|
static void
|
|
|
|
test_load_keys(void *arg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *conf = NULL;
|
|
|
|
char *hsdir_v2 = tor_strdup(get_fname("hs2"));
|
|
|
|
char *hsdir_v3 = tor_strdup(get_fname("hs3"));
|
|
|
|
char addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
|
|
|
|
|
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
/* We'll register two services, a v2 and a v3, then we'll load keys and
|
|
|
|
* validate that both are in a correct state. */
|
|
|
|
|
|
|
|
hs_init();
|
|
|
|
|
|
|
|
#define conf_fmt \
|
|
|
|
"HiddenServiceDir %s\n" \
|
|
|
|
"HiddenServiceVersion %d\n" \
|
|
|
|
"HiddenServicePort 65535\n"
|
|
|
|
|
|
|
|
/* v2 service. */
|
|
|
|
tor_asprintf(&conf, conf_fmt, hsdir_v2, HS_VERSION_TWO);
|
|
|
|
ret = helper_config_service(conf);
|
|
|
|
tor_free(conf);
|
|
|
|
tt_int_op(ret, OP_EQ, 0);
|
|
|
|
/* This one should now be registered into the v2 list. */
|
|
|
|
tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 0);
|
|
|
|
tt_int_op(num_rend_services(), OP_EQ, 1);
|
|
|
|
|
|
|
|
/* v3 service. */
|
|
|
|
tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
|
|
|
|
ret = helper_config_service(conf);
|
|
|
|
tor_free(conf);
|
|
|
|
tt_int_op(ret, OP_EQ, 0);
|
|
|
|
/* It's in staging? */
|
|
|
|
tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
|
|
|
|
|
|
|
|
/* Load the keys for these. After that, the v3 service should be registered
|
|
|
|
* in the global map. */
|
|
|
|
hs_service_load_all_keys();
|
|
|
|
tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
|
|
|
|
hs_service_t *s = get_first_service();
|
|
|
|
tt_assert(s);
|
|
|
|
|
|
|
|
/* Ok we have the service object. Validate few things. */
|
|
|
|
tt_assert(!tor_mem_is_zero(s->onion_address, sizeof(s->onion_address)));
|
|
|
|
tt_int_op(hs_address_is_valid(s->onion_address), OP_EQ, 1);
|
|
|
|
tt_assert(!tor_mem_is_zero((char *) s->keys.identity_sk.seckey,
|
|
|
|
ED25519_SECKEY_LEN));
|
|
|
|
tt_assert(!tor_mem_is_zero((char *) s->keys.identity_pk.pubkey,
|
|
|
|
ED25519_PUBKEY_LEN));
|
|
|
|
/* Check onion address from identity key. */
|
2017-07-13 08:51:14 -04:00
|
|
|
hs_build_address(&s->keys.identity_pk, s->config.version, addr);
|
2017-02-02 15:26:04 -05:00
|
|
|
tt_int_op(hs_address_is_valid(addr), OP_EQ, 1);
|
|
|
|
tt_str_op(addr, OP_EQ, s->onion_address);
|
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(hsdir_v2);
|
|
|
|
tor_free(hsdir_v3);
|
|
|
|
hs_free_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_access_service(void *arg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *conf = NULL;
|
|
|
|
char *hsdir_v3 = tor_strdup(get_fname("hs3"));
|
|
|
|
hs_service_ht *global_map;
|
2017-07-24 11:31:54 -04:00
|
|
|
hs_service_t *s = NULL;
|
2017-02-02 15:26:04 -05:00
|
|
|
|
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
/* We'll register two services, a v2 and a v3, then we'll load keys and
|
|
|
|
* validate that both are in a correct state. */
|
|
|
|
|
|
|
|
hs_init();
|
|
|
|
|
|
|
|
#define conf_fmt \
|
|
|
|
"HiddenServiceDir %s\n" \
|
|
|
|
"HiddenServiceVersion %d\n" \
|
|
|
|
"HiddenServicePort 65535\n"
|
|
|
|
|
|
|
|
/* v3 service. */
|
|
|
|
tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
|
|
|
|
ret = helper_config_service(conf);
|
|
|
|
tor_free(conf);
|
|
|
|
tt_int_op(ret, OP_EQ, 0);
|
|
|
|
/* It's in staging? */
|
|
|
|
tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
|
|
|
|
|
|
|
|
/* Load the keys for these. After that, the v3 service should be registered
|
|
|
|
* in the global map. */
|
|
|
|
hs_service_load_all_keys();
|
|
|
|
tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
|
2017-07-24 11:31:54 -04:00
|
|
|
s = get_first_service();
|
2017-02-02 15:26:04 -05:00
|
|
|
tt_assert(s);
|
|
|
|
global_map = get_hs_service_map();
|
|
|
|
tt_assert(global_map);
|
|
|
|
|
|
|
|
/* From here, we'll try the service accessors. */
|
|
|
|
hs_service_t *query = find_service(global_map, &s->keys.identity_pk);
|
|
|
|
tt_assert(query);
|
|
|
|
tt_mem_op(query, OP_EQ, s, sizeof(hs_service_t));
|
|
|
|
/* Remove service, check if it actually works and then put it back. */
|
|
|
|
remove_service(global_map, s);
|
|
|
|
tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
|
|
|
|
query = find_service(global_map, &s->keys.identity_pk);
|
|
|
|
tt_assert(!query);
|
|
|
|
|
|
|
|
/* Register back the service in the map. */
|
|
|
|
ret = register_service(global_map, s);
|
|
|
|
tt_int_op(ret, OP_EQ, 0);
|
|
|
|
tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
|
|
|
|
/* Twice should fail. */
|
|
|
|
ret = register_service(global_map, s);
|
|
|
|
tt_int_op(ret, OP_EQ, -1);
|
|
|
|
/* Remove service from map so we don't double free on cleanup. */
|
|
|
|
remove_service(global_map, s);
|
2017-07-24 11:31:54 -04:00
|
|
|
tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
|
2017-02-02 15:26:04 -05:00
|
|
|
query = find_service(global_map, &s->keys.identity_pk);
|
|
|
|
tt_assert(!query);
|
|
|
|
/* Let's try to remove twice for fun. */
|
|
|
|
setup_full_capture_of_logs(LOG_WARN);
|
|
|
|
remove_service(global_map, s);
|
|
|
|
expect_log_msg_containing("Could not find service in the global map");
|
|
|
|
teardown_capture_of_logs();
|
|
|
|
|
|
|
|
done:
|
2017-07-24 11:31:54 -04:00
|
|
|
hs_service_free(s);
|
2017-02-02 15:26:04 -05:00
|
|
|
tor_free(hsdir_v3);
|
|
|
|
hs_free_all();
|
|
|
|
}
|
|
|
|
|
2016-09-05 19:03:30 +03:00
|
|
|
struct testcase_t hs_service_tests[] = {
|
2017-05-02 16:37:17 +03:00
|
|
|
{ "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
|
|
|
|
NULL, NULL },
|
2017-02-02 15:26:04 -05:00
|
|
|
{ "load_keys", test_load_keys, TT_FORK,
|
|
|
|
NULL, NULL },
|
|
|
|
{ "access_service", test_access_service, TT_FORK,
|
|
|
|
NULL, NULL },
|
2016-09-05 19:03:30 +03:00
|
|
|
|
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|
|
|
|
|