test_hs_service.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. static void
  56. test_validate_address(void *arg)
  57. {
  58. int ret;
  59. (void) arg;
  60. /* Address too short and too long. */
  61. setup_full_capture_of_logs(LOG_WARN);
  62. ret = hs_address_is_valid("blah");
  63. tt_int_op(ret, OP_EQ, 0);
  64. expect_log_msg_containing("has an invalid length");
  65. teardown_capture_of_logs();
  66. setup_full_capture_of_logs(LOG_WARN);
  67. ret = hs_address_is_valid(
  68. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnadb");
  69. tt_int_op(ret, OP_EQ, 0);
  70. expect_log_msg_containing("has an invalid length");
  71. teardown_capture_of_logs();
  72. /* Invalid checksum (taken from prop224) */
  73. setup_full_capture_of_logs(LOG_WARN);
  74. ret = hs_address_is_valid(
  75. "l5satjgud6gucryazcyvyvhuxhr74u6ygigiuyixe3a6ysis67ororad");
  76. tt_int_op(ret, OP_EQ, 0);
  77. expect_log_msg_containing("invalid checksum");
  78. teardown_capture_of_logs();
  79. setup_full_capture_of_logs(LOG_WARN);
  80. ret = hs_address_is_valid(
  81. "btojiu7nu5y5iwut64eufevogqdw4wmqzugnoluw232r4t3ecsfv37ad");
  82. tt_int_op(ret, OP_EQ, 0);
  83. expect_log_msg_containing("invalid checksum");
  84. teardown_capture_of_logs();
  85. /* Non base32 decodable string. */
  86. setup_full_capture_of_logs(LOG_WARN);
  87. ret = hs_address_is_valid(
  88. "????????????????????????????????????????????????????????");
  89. tt_int_op(ret, OP_EQ, 0);
  90. expect_log_msg_containing("can't be decoded");
  91. teardown_capture_of_logs();
  92. /* Valid address. */
  93. ret = hs_address_is_valid(
  94. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnad");
  95. tt_int_op(ret, OP_EQ, 1);
  96. done:
  97. ;
  98. }
  99. static void
  100. test_build_address(void *arg)
  101. {
  102. int ret;
  103. char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  104. ed25519_public_key_t pubkey;
  105. (void) arg;
  106. /* The following has been created with hs_build_address.py script that
  107. * follows proposal 224 specification to build an onion address. */
  108. static const char *test_addr =
  109. "ijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbezhid";
  110. /* Let's try to build the same onion address that the script can do. Key is
  111. * a long set of very random \x42 :). */
  112. memset(&pubkey, '\x42', sizeof(pubkey));
  113. hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  114. tt_str_op(test_addr, OP_EQ, onion_addr);
  115. /* Validate that address. */
  116. ret = hs_address_is_valid(onion_addr);
  117. tt_int_op(ret, OP_EQ, 1);
  118. done:
  119. ;
  120. }
  121. /** Test that our HS time period calculation functions work properly */
  122. static void
  123. test_time_period(void *arg)
  124. {
  125. (void) arg;
  126. uint64_t tn;
  127. int retval;
  128. time_t fake_time;
  129. /* Let's do the example in prop224 section [TIME-PERIODS] */
  130. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  131. &fake_time);
  132. tt_int_op(retval, ==, 0);
  133. /* Check that the time period number is right */
  134. tn = hs_get_time_period_num(fake_time);
  135. tt_u64_op(tn, ==, 16903);
  136. /* Increase current time to 11:59:59 UTC and check that the time period
  137. number is still the same */
  138. fake_time += 3599;
  139. tn = hs_get_time_period_num(fake_time);
  140. tt_u64_op(tn, ==, 16903);
  141. /* Now take time to 12:00:00 UTC and check that the time period rotated */
  142. fake_time += 1;
  143. tn = hs_get_time_period_num(fake_time);
  144. tt_u64_op(tn, ==, 16904);
  145. /* Now also check our hs_get_next_time_period_num() function */
  146. tn = hs_get_next_time_period_num(fake_time);
  147. tt_u64_op(tn, ==, 16905);
  148. done:
  149. ;
  150. }
  151. /* Test: Ensure that setting up rendezvous circuits works correctly. */
  152. static void
  153. test_e2e_rend_circuit_setup(void *arg)
  154. {
  155. ed25519_public_key_t service_pk;
  156. origin_circuit_t *or_circ;
  157. int retval;
  158. /** In this test we create a v3 prop224 service-side rendezvous circuit.
  159. * We simulate an HS ntor key exchange with a client, and check that
  160. * the circuit was setup correctly and is ready to accept rendezvous data */
  161. (void) arg;
  162. /* Now make dummy circuit */
  163. {
  164. or_circ = origin_circuit_new();
  165. or_circ->base_.purpose = CIRCUIT_PURPOSE_S_CONNECT_REND;
  166. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  167. or_circ->build_state->is_internal = 1;
  168. /* prop224: Setup hs conn identifier on the stream */
  169. ed25519_secret_key_t sk;
  170. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
  171. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
  172. or_circ->hs_ident = hs_ident_circuit_new(&service_pk,
  173. HS_IDENT_CIRCUIT_RENDEZVOUS);
  174. TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
  175. }
  176. /* Check number of hops */
  177. retval = cpath_get_n_hops(&or_circ->cpath);
  178. tt_int_op(retval, OP_EQ, 0);
  179. /* Setup the circuit: do the ntor key exchange */
  180. {
  181. uint8_t ntor_key_seed[DIGEST256_LEN] = {2};
  182. retval = hs_circuit_setup_e2e_rend_circ(or_circ,
  183. ntor_key_seed, sizeof(ntor_key_seed),
  184. 1);
  185. tt_int_op(retval, OP_EQ, 0);
  186. }
  187. /* See that a hop was added to the circuit's cpath */
  188. retval = cpath_get_n_hops(&or_circ->cpath);
  189. tt_int_op(retval, OP_EQ, 1);
  190. /* Check the digest algo */
  191. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->f_digest),
  192. OP_EQ, DIGEST_SHA3_256);
  193. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->b_digest),
  194. OP_EQ, DIGEST_SHA3_256);
  195. tt_assert(or_circ->cpath->f_crypto);
  196. tt_assert(or_circ->cpath->b_crypto);
  197. /* Ensure that circ purpose was changed */
  198. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  199. done:
  200. circuit_free(TO_CIRCUIT(or_circ));
  201. }
  202. static void
  203. test_load_keys(void *arg)
  204. {
  205. int ret;
  206. char *conf = NULL;
  207. char *hsdir_v2 = tor_strdup(get_fname("hs2"));
  208. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  209. char addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  210. (void) arg;
  211. /* We'll register two services, a v2 and a v3, then we'll load keys and
  212. * validate that both are in a correct state. */
  213. hs_init();
  214. #define conf_fmt \
  215. "HiddenServiceDir %s\n" \
  216. "HiddenServiceVersion %d\n" \
  217. "HiddenServicePort 65535\n"
  218. /* v2 service. */
  219. tor_asprintf(&conf, conf_fmt, hsdir_v2, HS_VERSION_TWO);
  220. ret = helper_config_service(conf);
  221. tor_free(conf);
  222. tt_int_op(ret, OP_EQ, 0);
  223. /* This one should now be registered into the v2 list. */
  224. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 0);
  225. tt_int_op(num_rend_services(), OP_EQ, 1);
  226. /* v3 service. */
  227. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  228. ret = helper_config_service(conf);
  229. tor_free(conf);
  230. tt_int_op(ret, OP_EQ, 0);
  231. /* It's in staging? */
  232. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  233. /* Load the keys for these. After that, the v3 service should be registered
  234. * in the global map. */
  235. hs_service_load_all_keys();
  236. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  237. hs_service_t *s = get_first_service();
  238. tt_assert(s);
  239. /* Ok we have the service object. Validate few things. */
  240. tt_assert(!tor_mem_is_zero(s->onion_address, sizeof(s->onion_address)));
  241. tt_int_op(hs_address_is_valid(s->onion_address), OP_EQ, 1);
  242. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_sk.seckey,
  243. ED25519_SECKEY_LEN));
  244. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_pk.pubkey,
  245. ED25519_PUBKEY_LEN));
  246. /* Check onion address from identity key. */
  247. hs_build_address(&s->keys.identity_pk, s->config.version, addr);
  248. tt_int_op(hs_address_is_valid(addr), OP_EQ, 1);
  249. tt_str_op(addr, OP_EQ, s->onion_address);
  250. done:
  251. tor_free(hsdir_v2);
  252. tor_free(hsdir_v3);
  253. hs_free_all();
  254. }
  255. static void
  256. test_access_service(void *arg)
  257. {
  258. int ret;
  259. char *conf = NULL;
  260. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  261. hs_service_ht *global_map;
  262. hs_service_t *s = NULL;
  263. (void) arg;
  264. /* We'll register two services, a v2 and a v3, then we'll load keys and
  265. * validate that both are in a correct state. */
  266. hs_init();
  267. #define conf_fmt \
  268. "HiddenServiceDir %s\n" \
  269. "HiddenServiceVersion %d\n" \
  270. "HiddenServicePort 65535\n"
  271. /* v3 service. */
  272. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  273. ret = helper_config_service(conf);
  274. tor_free(conf);
  275. tt_int_op(ret, OP_EQ, 0);
  276. /* It's in staging? */
  277. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  278. /* Load the keys for these. After that, the v3 service should be registered
  279. * in the global map. */
  280. hs_service_load_all_keys();
  281. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  282. s = get_first_service();
  283. tt_assert(s);
  284. global_map = get_hs_service_map();
  285. tt_assert(global_map);
  286. /* From here, we'll try the service accessors. */
  287. hs_service_t *query = find_service(global_map, &s->keys.identity_pk);
  288. tt_assert(query);
  289. tt_mem_op(query, OP_EQ, s, sizeof(hs_service_t));
  290. /* Remove service, check if it actually works and then put it back. */
  291. remove_service(global_map, s);
  292. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  293. query = find_service(global_map, &s->keys.identity_pk);
  294. tt_assert(!query);
  295. /* Register back the service in the map. */
  296. ret = register_service(global_map, s);
  297. tt_int_op(ret, OP_EQ, 0);
  298. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  299. /* Twice should fail. */
  300. ret = register_service(global_map, s);
  301. tt_int_op(ret, OP_EQ, -1);
  302. /* Remove service from map so we don't double free on cleanup. */
  303. remove_service(global_map, s);
  304. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  305. query = find_service(global_map, &s->keys.identity_pk);
  306. tt_assert(!query);
  307. /* Let's try to remove twice for fun. */
  308. setup_full_capture_of_logs(LOG_WARN);
  309. remove_service(global_map, s);
  310. expect_log_msg_containing("Could not find service in the global map");
  311. teardown_capture_of_logs();
  312. done:
  313. hs_service_free(s);
  314. tor_free(hsdir_v3);
  315. hs_free_all();
  316. }
  317. /** Test that our HS overlap period functions work properly. */
  318. static void
  319. test_desc_overlap_period(void *arg)
  320. {
  321. (void) arg;
  322. int retval;
  323. time_t now = time(NULL);
  324. networkstatus_t *dummy_consensus = NULL;
  325. /* First try with a consensus inside the overlap period */
  326. dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  327. retval = parse_rfc1123_time("Wed, 13 Apr 2016 10:00:00 UTC",
  328. &dummy_consensus->valid_after);
  329. tt_int_op(retval, ==, 0);
  330. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  331. tt_int_op(retval, ==, 1);
  332. /* Now increase the valid_after so that it goes to 11:00:00 UTC. Overlap
  333. period is still active. */
  334. dummy_consensus->valid_after += 3600;
  335. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  336. tt_int_op(retval, ==, 1);
  337. /* Now increase the valid_after so that it goes to 11:59:59 UTC. Overlap
  338. period is still active. */
  339. dummy_consensus->valid_after += 3599;
  340. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  341. tt_int_op(retval, ==, 1);
  342. /* Now increase the valid_after so that it drifts to noon, and check that
  343. overlap mode is not active anymore. */
  344. dummy_consensus->valid_after += 1;
  345. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  346. tt_int_op(retval, ==, 0);
  347. /* Check that overlap mode is also inactive at 23:59:59 UTC */
  348. retval = parse_rfc1123_time("Wed, 13 Apr 2016 23:59:59 UTC",
  349. &dummy_consensus->valid_after);
  350. tt_int_op(retval, ==, 0);
  351. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  352. tt_int_op(retval, ==, 0);
  353. done:
  354. tor_free(dummy_consensus);
  355. }
  356. struct testcase_t hs_service_tests[] = {
  357. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  358. NULL, NULL },
  359. { "build_address", test_build_address, TT_FORK,
  360. NULL, NULL },
  361. { "validate_address", test_validate_address, TT_FORK,
  362. NULL, NULL },
  363. { "load_keys", test_load_keys, TT_FORK,
  364. NULL, NULL },
  365. { "access_service", test_access_service, TT_FORK,
  366. NULL, NULL },
  367. { "desc_overlap_period", test_desc_overlap_period, TT_FORK,
  368. NULL, NULL },
  369. { "time_period", test_time_period, TT_FORK,
  370. NULL, NULL },
  371. END_OF_TESTCASES
  372. };