test_hs_service.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_service.c
  5. * \brief Test hidden service functionality.
  6. */
  7. #define CIRCUITBUILD_PRIVATE
  8. #define CIRCUITLIST_PRIVATE
  9. #define CONFIG_PRIVATE
  10. #define CONNECTION_PRIVATE
  11. #define CRYPTO_PRIVATE
  12. #define HS_COMMON_PRIVATE
  13. #define HS_SERVICE_PRIVATE
  14. #define HS_INTROPOINT_PRIVATE
  15. #define MAIN_PRIVATE
  16. #define TOR_CHANNEL_INTERNAL_
  17. #include "test.h"
  18. #include "test_helpers.h"
  19. #include "log_test_helpers.h"
  20. #include "rend_test_helpers.h"
  21. #include "or.h"
  22. #include "channeltls.h"
  23. #include "circuitbuild.h"
  24. #include "circuitlist.h"
  25. #include "circuituse.h"
  26. #include "config.h"
  27. #include "connection.h"
  28. #include "crypto.h"
  29. #include "hs_circuit.h"
  30. #include "hs_common.h"
  31. #include "hs_config.h"
  32. #include "hs_ident.h"
  33. #include "hs_intropoint.h"
  34. #include "hs_ntor.h"
  35. #include "hs_service.h"
  36. #include "main.h"
  37. #include "rendservice.h"
  38. /* Trunnel */
  39. #include "hs/cell_establish_intro.h"
  40. /* Helper: from a set of options in conf, configure a service which will add
  41. * it to the staging list of the HS subsytem. */
  42. static int
  43. helper_config_service(const char *conf)
  44. {
  45. int ret = 0;
  46. or_options_t *options = NULL;
  47. tt_assert(conf);
  48. options = helper_parse_options(conf);
  49. tt_assert(options);
  50. ret = hs_config_service_all(options, 0);
  51. done:
  52. or_options_free(options);
  53. return ret;
  54. }
  55. /* Test: Ensure that setting up rendezvous circuits works correctly. */
  56. static void
  57. test_e2e_rend_circuit_setup(void *arg)
  58. {
  59. ed25519_public_key_t service_pk;
  60. origin_circuit_t *or_circ;
  61. int retval;
  62. /** In this test we create a v3 prop224 service-side rendezvous circuit.
  63. * We simulate an HS ntor key exchange with a client, and check that
  64. * the circuit was setup correctly and is ready to accept rendezvous data */
  65. (void) arg;
  66. /* Now make dummy circuit */
  67. {
  68. or_circ = origin_circuit_new();
  69. or_circ->base_.purpose = CIRCUIT_PURPOSE_S_CONNECT_REND;
  70. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  71. or_circ->build_state->is_internal = 1;
  72. /* prop224: Setup hs conn identifier on the stream */
  73. ed25519_secret_key_t sk;
  74. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
  75. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
  76. or_circ->hs_ident = hs_ident_circuit_new(&service_pk,
  77. HS_IDENT_CIRCUIT_RENDEZVOUS);
  78. TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
  79. }
  80. /* Check number of hops */
  81. retval = cpath_get_n_hops(&or_circ->cpath);
  82. tt_int_op(retval, OP_EQ, 0);
  83. /* Setup the circuit: do the ntor key exchange */
  84. {
  85. uint8_t ntor_key_seed[DIGEST256_LEN] = {2};
  86. retval = hs_circuit_setup_e2e_rend_circ(or_circ,
  87. ntor_key_seed, sizeof(ntor_key_seed),
  88. 1);
  89. tt_int_op(retval, OP_EQ, 0);
  90. }
  91. /* See that a hop was added to the circuit's cpath */
  92. retval = cpath_get_n_hops(&or_circ->cpath);
  93. tt_int_op(retval, OP_EQ, 1);
  94. /* Check the digest algo */
  95. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->f_digest),
  96. OP_EQ, DIGEST_SHA3_256);
  97. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->b_digest),
  98. OP_EQ, DIGEST_SHA3_256);
  99. tt_assert(or_circ->cpath->f_crypto);
  100. tt_assert(or_circ->cpath->b_crypto);
  101. /* Ensure that circ purpose was changed */
  102. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  103. done:
  104. circuit_free(TO_CIRCUIT(or_circ));
  105. }
  106. static void
  107. test_load_keys(void *arg)
  108. {
  109. int ret;
  110. char *conf = NULL;
  111. char *hsdir_v2 = tor_strdup(get_fname("hs2"));
  112. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  113. char addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  114. (void) arg;
  115. /* We'll register two services, a v2 and a v3, then we'll load keys and
  116. * validate that both are in a correct state. */
  117. hs_init();
  118. #define conf_fmt \
  119. "HiddenServiceDir %s\n" \
  120. "HiddenServiceVersion %d\n" \
  121. "HiddenServicePort 65535\n"
  122. /* v2 service. */
  123. tor_asprintf(&conf, conf_fmt, hsdir_v2, HS_VERSION_TWO);
  124. ret = helper_config_service(conf);
  125. tor_free(conf);
  126. tt_int_op(ret, OP_EQ, 0);
  127. /* This one should now be registered into the v2 list. */
  128. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 0);
  129. tt_int_op(num_rend_services(), OP_EQ, 1);
  130. /* v3 service. */
  131. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  132. ret = helper_config_service(conf);
  133. tor_free(conf);
  134. tt_int_op(ret, OP_EQ, 0);
  135. /* It's in staging? */
  136. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  137. /* Load the keys for these. After that, the v3 service should be registered
  138. * in the global map. */
  139. hs_service_load_all_keys();
  140. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  141. hs_service_t *s = get_first_service();
  142. tt_assert(s);
  143. /* Ok we have the service object. Validate few things. */
  144. tt_assert(!tor_mem_is_zero(s->onion_address, sizeof(s->onion_address)));
  145. tt_int_op(hs_address_is_valid(s->onion_address), OP_EQ, 1);
  146. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_sk.seckey,
  147. ED25519_SECKEY_LEN));
  148. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_pk.pubkey,
  149. ED25519_PUBKEY_LEN));
  150. /* Check onion address from identity key. */
  151. hs_build_address(&s->keys.identity_pk, s->config.version, addr);
  152. tt_int_op(hs_address_is_valid(addr), OP_EQ, 1);
  153. tt_str_op(addr, OP_EQ, s->onion_address);
  154. done:
  155. tor_free(hsdir_v2);
  156. tor_free(hsdir_v3);
  157. hs_free_all();
  158. }
  159. static void
  160. test_access_service(void *arg)
  161. {
  162. int ret;
  163. char *conf = NULL;
  164. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  165. hs_service_ht *global_map;
  166. hs_service_t *s = NULL;
  167. (void) arg;
  168. /* We'll register two services, a v2 and a v3, then we'll load keys and
  169. * validate that both are in a correct state. */
  170. hs_init();
  171. #define conf_fmt \
  172. "HiddenServiceDir %s\n" \
  173. "HiddenServiceVersion %d\n" \
  174. "HiddenServicePort 65535\n"
  175. /* v3 service. */
  176. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  177. ret = helper_config_service(conf);
  178. tor_free(conf);
  179. tt_int_op(ret, OP_EQ, 0);
  180. /* It's in staging? */
  181. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  182. /* Load the keys for these. After that, the v3 service should be registered
  183. * in the global map. */
  184. hs_service_load_all_keys();
  185. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  186. s = get_first_service();
  187. tt_assert(s);
  188. global_map = get_hs_service_map();
  189. tt_assert(global_map);
  190. /* From here, we'll try the service accessors. */
  191. hs_service_t *query = find_service(global_map, &s->keys.identity_pk);
  192. tt_assert(query);
  193. tt_mem_op(query, OP_EQ, s, sizeof(hs_service_t));
  194. /* Remove service, check if it actually works and then put it back. */
  195. remove_service(global_map, s);
  196. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  197. query = find_service(global_map, &s->keys.identity_pk);
  198. tt_assert(!query);
  199. /* Register back the service in the map. */
  200. ret = register_service(global_map, s);
  201. tt_int_op(ret, OP_EQ, 0);
  202. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  203. /* Twice should fail. */
  204. ret = register_service(global_map, s);
  205. tt_int_op(ret, OP_EQ, -1);
  206. /* Remove service from map so we don't double free on cleanup. */
  207. remove_service(global_map, s);
  208. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  209. query = find_service(global_map, &s->keys.identity_pk);
  210. tt_assert(!query);
  211. /* Let's try to remove twice for fun. */
  212. setup_full_capture_of_logs(LOG_WARN);
  213. remove_service(global_map, s);
  214. expect_log_msg_containing("Could not find service in the global map");
  215. teardown_capture_of_logs();
  216. done:
  217. hs_service_free(s);
  218. tor_free(hsdir_v3);
  219. hs_free_all();
  220. }
  221. struct testcase_t hs_service_tests[] = {
  222. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  223. NULL, NULL },
  224. { "load_keys", test_load_keys, TT_FORK,
  225. NULL, NULL },
  226. { "access_service", test_access_service, TT_FORK,
  227. NULL, NULL },
  228. END_OF_TESTCASES
  229. };