test_hs_service.c 17 KB

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