test_hs_client.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_client.c
  5. * \brief Test prop224 HS client functionality.
  6. */
  7. #define CRYPTO_PRIVATE
  8. #define MAIN_PRIVATE
  9. #define HS_CLIENT_PRIVATE
  10. #define TOR_CHANNEL_INTERNAL_
  11. #define CIRCUITBUILD_PRIVATE
  12. #define CIRCUITLIST_PRIVATE
  13. #define CONNECTION_PRIVATE
  14. #include "test.h"
  15. #include "test_helpers.h"
  16. #include "log_test_helpers.h"
  17. #include "rend_test_helpers.h"
  18. #include "hs_test_helpers.h"
  19. #include "config.h"
  20. #include "crypto.h"
  21. #include "channeltls.h"
  22. #include "routerset.h"
  23. #include "hs_circuit.h"
  24. #include "hs_client.h"
  25. #include "hs_ident.h"
  26. #include "hs_cache.h"
  27. #include "circuitlist.h"
  28. #include "circuitbuild.h"
  29. #include "connection.h"
  30. #include "connection_edge.h"
  31. #include "networkstatus.h"
  32. static int
  33. mock_connection_ap_handshake_send_begin(entry_connection_t *ap_conn)
  34. {
  35. (void) ap_conn;
  36. return 0;
  37. }
  38. static networkstatus_t mock_ns;
  39. static networkstatus_t *
  40. mock_networkstatus_get_live_consensus(time_t now)
  41. {
  42. (void) now;
  43. return &mock_ns;
  44. }
  45. /* Test helper function: Setup a circuit and a stream with the same hidden
  46. * service destination, and put them in <b>circ_out</b> and
  47. * <b>conn_out</b>. Make the stream wait for circuits to be established to the
  48. * hidden service. */
  49. static int
  50. helper_get_circ_and_stream_for_test(origin_circuit_t **circ_out,
  51. connection_t **conn_out,
  52. int is_legacy)
  53. {
  54. int retval;
  55. channel_tls_t *n_chan=NULL;
  56. rend_data_t *conn_rend_data = NULL;
  57. origin_circuit_t *or_circ = NULL;
  58. connection_t *conn = NULL;
  59. ed25519_public_key_t service_pk;
  60. /* Make a dummy connection stream and make it wait for our circuit */
  61. conn = test_conn_get_connection(AP_CONN_STATE_CIRCUIT_WAIT,
  62. CONN_TYPE_AP /* ??? */,
  63. 0);
  64. if (is_legacy) {
  65. /* Legacy: Setup rend_data of stream */
  66. char service_id[REND_SERVICE_ID_LEN_BASE32+1] = {0};
  67. TO_EDGE_CONN(conn)->rend_data = mock_rend_data(service_id);
  68. conn_rend_data = TO_EDGE_CONN(conn)->rend_data;
  69. } else {
  70. /* prop224: Setup hs conn identifier on the stream */
  71. ed25519_secret_key_t sk;
  72. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
  73. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
  74. /* Setup hs_conn_identifier of stream */
  75. TO_EDGE_CONN(conn)->hs_ident = hs_ident_edge_conn_new(&service_pk);
  76. }
  77. /* Make it wait for circuit */
  78. connection_ap_mark_as_pending_circuit(TO_ENTRY_CONN(conn));
  79. /* This is needed to silence a BUG warning from
  80. connection_edge_update_circuit_isolation() */
  81. TO_ENTRY_CONN(conn)->original_dest_address =
  82. tor_strdup(TO_ENTRY_CONN(conn)->socks_request->address);
  83. /****************************************************/
  84. /* Now make dummy circuit */
  85. or_circ = origin_circuit_new();
  86. or_circ->base_.purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
  87. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  88. or_circ->build_state->is_internal = 1;
  89. if (is_legacy) {
  90. /* Legacy: Setup rend data and final cpath */
  91. or_circ->build_state->pending_final_cpath =
  92. tor_malloc_zero(sizeof(crypt_path_t));
  93. or_circ->build_state->pending_final_cpath->magic = CRYPT_PATH_MAGIC;
  94. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state =
  95. crypto_dh_new(DH_TYPE_REND);
  96. tt_assert(
  97. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state);
  98. retval = crypto_dh_generate_public(
  99. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state);
  100. tt_int_op(retval, OP_EQ, 0);
  101. or_circ->rend_data = rend_data_dup(conn_rend_data);
  102. } else {
  103. /* prop224: Setup hs ident on the circuit */
  104. or_circ->hs_ident = hs_ident_circuit_new(&service_pk,
  105. HS_IDENT_CIRCUIT_RENDEZVOUS);
  106. }
  107. TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
  108. /* fake n_chan */
  109. n_chan = tor_malloc_zero(sizeof(channel_tls_t));
  110. n_chan->base_.global_identifier = 1;
  111. or_circ->base_.n_chan = &(n_chan->base_);
  112. *circ_out = or_circ;
  113. *conn_out = conn;
  114. return 0;
  115. done:
  116. /* something failed */
  117. return -1;
  118. }
  119. /* Test: Ensure that setting up legacy e2e rendezvous circuits works
  120. * correctly. */
  121. static void
  122. test_e2e_rend_circuit_setup_legacy(void *arg)
  123. {
  124. ssize_t retval;
  125. origin_circuit_t *or_circ = NULL;
  126. connection_t *conn = NULL;
  127. (void) arg;
  128. /** In this test we create a v2 legacy HS stream and a circuit with the same
  129. * hidden service destination. We make the stream wait for circuits to be
  130. * established to the hidden service, and then we complete the circuit using
  131. * the hs_circuit_setup_e2e_rend_circ_legacy_client() function. We then
  132. * check that the end-to-end cpath was setup correctly and that the stream
  133. * was attached to the circuit as expected. */
  134. MOCK(connection_ap_handshake_send_begin,
  135. mock_connection_ap_handshake_send_begin);
  136. /* Setup */
  137. retval = helper_get_circ_and_stream_for_test( &or_circ, &conn, 1);
  138. tt_int_op(retval, OP_EQ, 0);
  139. tt_assert(or_circ);
  140. tt_assert(conn);
  141. /* Check number of hops */
  142. retval = cpath_get_n_hops(&or_circ->cpath);
  143. tt_int_op(retval, OP_EQ, 0);
  144. /* Check that our stream is not attached on any circuits */
  145. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, NULL);
  146. /********************************************** */
  147. /* Make a good RENDEZVOUS1 cell body because it needs to pass key exchange
  148. * digest verification... */
  149. uint8_t rend_cell_body[DH_KEY_LEN+DIGEST_LEN] = {2};
  150. {
  151. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  152. crypto_dh_t *dh_state =
  153. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state;
  154. /* compute and overwrite digest of cell body with the right value */
  155. retval = crypto_dh_compute_secret(LOG_PROTOCOL_WARN, dh_state,
  156. (char*)rend_cell_body, DH_KEY_LEN,
  157. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN);
  158. tt_int_op(retval, OP_GT, 0);
  159. memcpy(rend_cell_body+DH_KEY_LEN, keys, DIGEST_LEN);
  160. }
  161. /* Setup the circuit */
  162. retval = hs_circuit_setup_e2e_rend_circ_legacy_client(or_circ,
  163. rend_cell_body);
  164. tt_int_op(retval, OP_EQ, 0);
  165. /**********************************************/
  166. /* See that a hop was added to the circuit's cpath */
  167. retval = cpath_get_n_hops(&or_circ->cpath);
  168. tt_int_op(retval, OP_EQ, 1);
  169. /* Check the digest algo */
  170. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->f_digest),
  171. OP_EQ, DIGEST_SHA1);
  172. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->b_digest),
  173. OP_EQ, DIGEST_SHA1);
  174. tt_assert(or_circ->cpath->f_crypto);
  175. tt_assert(or_circ->cpath->b_crypto);
  176. /* Ensure that circ purpose was changed */
  177. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_C_REND_JOINED);
  178. /* Test that stream got attached */
  179. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, TO_CIRCUIT(or_circ));
  180. done:
  181. connection_free_(conn);
  182. tor_free(TO_CIRCUIT(or_circ)->n_chan);
  183. circuit_free(TO_CIRCUIT(or_circ));
  184. }
  185. /* Test: Ensure that setting up v3 rendezvous circuits works correctly. */
  186. static void
  187. test_e2e_rend_circuit_setup(void *arg)
  188. {
  189. uint8_t ntor_key_seed[DIGEST256_LEN] = {0};
  190. origin_circuit_t *or_circ;
  191. int retval;
  192. connection_t *conn = NULL;
  193. (void) arg;
  194. /** In this test we create a prop224 v3 HS stream and a circuit with the same
  195. * hidden service destination. We make the stream wait for circuits to be
  196. * established to the hidden service, and then we complete the circuit using
  197. * the hs_circuit_setup_e2e_rend_circ() function. We then check that the
  198. * end-to-end cpath was setup correctly and that the stream was attached to
  199. * the circuit as expected. */
  200. MOCK(connection_ap_handshake_send_begin,
  201. mock_connection_ap_handshake_send_begin);
  202. /* Setup */
  203. retval = helper_get_circ_and_stream_for_test( &or_circ, &conn, 0);
  204. tt_int_op(retval, OP_EQ, 0);
  205. tt_assert(or_circ);
  206. tt_assert(conn);
  207. /* Check number of hops: There should be no hops yet to this circ */
  208. retval = cpath_get_n_hops(&or_circ->cpath);
  209. tt_int_op(retval, OP_EQ, 0);
  210. tt_ptr_op(or_circ->cpath, OP_EQ, NULL);
  211. /* Check that our stream is not attached on any circuits */
  212. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, NULL);
  213. /**********************************************/
  214. /* Setup the circuit */
  215. retval = hs_circuit_setup_e2e_rend_circ(or_circ,
  216. ntor_key_seed, sizeof(ntor_key_seed),
  217. 0);
  218. tt_int_op(retval, OP_EQ, 0);
  219. /**********************************************/
  220. /* See that a hop was added to the circuit's cpath */
  221. retval = cpath_get_n_hops(&or_circ->cpath);
  222. tt_int_op(retval, OP_EQ, 1);
  223. /* Check that the crypt path has prop224 algorithm parameters */
  224. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->f_digest),
  225. OP_EQ, DIGEST_SHA3_256);
  226. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->b_digest),
  227. OP_EQ, DIGEST_SHA3_256);
  228. tt_assert(or_circ->cpath->f_crypto);
  229. tt_assert(or_circ->cpath->b_crypto);
  230. /* Ensure that circ purpose was changed */
  231. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_C_REND_JOINED);
  232. /* Test that stream got attached */
  233. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, TO_CIRCUIT(or_circ));
  234. done:
  235. connection_free_(conn);
  236. tor_free(TO_CIRCUIT(or_circ)->n_chan);
  237. circuit_free(TO_CIRCUIT(or_circ));
  238. }
  239. /** Test client logic for picking intro points from a descriptor. Also test how
  240. * ExcludeNodes and intro point failures affect picking intro points. */
  241. static void
  242. test_client_pick_intro(void *arg)
  243. {
  244. int ret;
  245. ed25519_keypair_t service_kp;
  246. hs_descriptor_t *desc = NULL;
  247. MOCK(networkstatus_get_live_consensus,
  248. mock_networkstatus_get_live_consensus);
  249. (void) arg;
  250. hs_init();
  251. /* Generate service keypair */
  252. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&service_kp, 0));
  253. /* Set time */
  254. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  255. &mock_ns.valid_after);
  256. tt_int_op(ret, OP_EQ, 0);
  257. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  258. &mock_ns.fresh_until);
  259. tt_int_op(ret, OP_EQ, 0);
  260. update_approx_time(mock_ns.fresh_until-10);
  261. time_t now = approx_time();
  262. /* Test logic:
  263. *
  264. * 1) Add our desc with intro points to the HS cache.
  265. *
  266. * 2) Mark all descriptor intro points except _the chosen one_ as
  267. * failed. Then query the desc to get a random intro: check that we got
  268. * _the chosen one_. Then fail the chosen one as well, and see that no
  269. * intros are returned.
  270. *
  271. * 3) Then clean the intro state cache and get an intro point.
  272. *
  273. * 4) Try fetching an intro with the wrong service key: shouldn't work
  274. *
  275. * 5) Set StrictNodes and put all our intro points in ExcludeNodes: see that
  276. * nothing is returned.
  277. */
  278. /* 1) Add desc to HS cache */
  279. {
  280. char *encoded = NULL;
  281. desc = hs_helper_build_hs_desc_with_ip(&service_kp);
  282. ret = hs_desc_encode_descriptor(desc, &service_kp, &encoded);
  283. tt_int_op(ret, OP_EQ, 0);
  284. tt_assert(encoded);
  285. /* store it */
  286. hs_cache_store_as_client(encoded, &service_kp.pubkey);
  287. /* fetch it to make sure it works */
  288. const hs_descriptor_t *fetched_desc =
  289. hs_cache_lookup_as_client(&service_kp.pubkey);
  290. tt_assert(fetched_desc);
  291. tt_mem_op(fetched_desc->subcredential, OP_EQ, desc->subcredential,
  292. DIGEST256_LEN);
  293. tt_assert(!tor_mem_is_zero((char*)fetched_desc->subcredential,
  294. DIGEST256_LEN));
  295. tor_free(encoded);
  296. }
  297. /* 2) Mark all intro points except _the chosen one_ as failed. Then query the
  298. * desc and get a random intro: check that we got _the chosen one_. */
  299. {
  300. /* Pick the chosen intro point and get its ei */
  301. hs_desc_intro_point_t *chosen_intro_point =
  302. smartlist_get(desc->encrypted_data.intro_points, 0);
  303. extend_info_t *chosen_intro_ei =
  304. desc_intro_point_to_extend_info(chosen_intro_point);
  305. tt_assert(chosen_intro_point);
  306. tt_assert(chosen_intro_ei);
  307. /* Now mark all other intro points as failed */
  308. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  309. hs_desc_intro_point_t *, ip) {
  310. /* Skip the chosen intro point */
  311. if (ip == chosen_intro_point) {
  312. continue;
  313. }
  314. ed25519_public_key_t *intro_auth_key = &ip->auth_key_cert->signed_key;
  315. hs_cache_client_intro_state_note(&service_kp.pubkey,
  316. intro_auth_key,
  317. INTRO_POINT_FAILURE_GENERIC);
  318. } SMARTLIST_FOREACH_END(ip);
  319. /* Try to get a random intro: Should return the chosen one! */
  320. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  321. tor_assert(ip);
  322. tt_assert(!tor_mem_is_zero((char*)ip->identity_digest, DIGEST_LEN));
  323. tt_mem_op(ip->identity_digest, OP_EQ, chosen_intro_ei->identity_digest,
  324. DIGEST_LEN);
  325. extend_info_free(chosen_intro_ei);
  326. extend_info_free(ip);
  327. /* Now also mark the chosen one as failed: See that we can't get any intro
  328. points anymore. */
  329. hs_cache_client_intro_state_note(&service_kp.pubkey,
  330. &chosen_intro_point->auth_key_cert->signed_key,
  331. INTRO_POINT_FAILURE_TIMEOUT);
  332. ip = client_get_random_intro(&service_kp.pubkey);
  333. tor_assert(!ip);
  334. }
  335. /* 3) Clean the intro state cache and get an intro point */
  336. {
  337. /* Pretend we are 5 mins in the future and order a cleanup of the intro
  338. * state. This should clean up the intro point failures and allow us to get
  339. * an intro. */
  340. hs_cache_client_intro_state_clean(now + 5*60);
  341. /* Get an intro. It should work! */
  342. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  343. tor_assert(ip);
  344. extend_info_free(ip);
  345. }
  346. /* 4) Try fetching an intro with the wrong service key: shouldn't work */
  347. {
  348. ed25519_keypair_t dummy_kp;
  349. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&dummy_kp, 0));
  350. extend_info_t *ip = client_get_random_intro(&dummy_kp.pubkey);
  351. tor_assert(!ip);
  352. }
  353. /* 5) Set StrictNodes and put all our intro points in ExcludeNodes: see that
  354. * nothing is returned. */
  355. {
  356. get_options_mutable()->ExcludeNodes = routerset_new();
  357. get_options_mutable()->StrictNodes = 1;
  358. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  359. hs_desc_intro_point_t *, ip) {
  360. extend_info_t *intro_ei = desc_intro_point_to_extend_info(ip);
  361. if (intro_ei) {
  362. const char *ptr;
  363. char ip_addr[TOR_ADDR_BUF_LEN];
  364. /* We need to decorate in case it is an IPv6 else routerset_parse()
  365. * doesn't like it. */
  366. ptr = tor_addr_to_str(ip_addr, &intro_ei->addr, sizeof(ip_addr), 1);
  367. tt_assert(ptr == ip_addr);
  368. ret = routerset_parse(get_options_mutable()->ExcludeNodes,
  369. ip_addr, "");
  370. tt_int_op(ret, OP_EQ, 0);
  371. extend_info_free(intro_ei);
  372. }
  373. } SMARTLIST_FOREACH_END(ip);
  374. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  375. tt_assert(!ip);
  376. }
  377. done:
  378. hs_descriptor_free(desc);
  379. }
  380. struct testcase_t hs_client_tests[] = {
  381. { "e2e_rend_circuit_setup_legacy", test_e2e_rend_circuit_setup_legacy,
  382. TT_FORK, NULL, NULL },
  383. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup,
  384. TT_FORK, NULL, NULL },
  385. { "client_pick_intro", test_client_pick_intro,
  386. TT_FORK, NULL, NULL },
  387. END_OF_TESTCASES
  388. };