test_hs_client.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. /* Copyright (c) 2016-2019, 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 CONFIG_PRIVATE
  8. #define CRYPTO_PRIVATE
  9. #define MAINLOOP_PRIVATE
  10. #define HS_CLIENT_PRIVATE
  11. #define TOR_CHANNEL_INTERNAL_
  12. #define CIRCUITBUILD_PRIVATE
  13. #define CIRCUITLIST_PRIVATE
  14. #define CONNECTION_PRIVATE
  15. #include "test/test.h"
  16. #include "test/test_helpers.h"
  17. #include "test/log_test_helpers.h"
  18. #include "test/rend_test_helpers.h"
  19. #include "test/hs_test_helpers.h"
  20. #include "app/config/config.h"
  21. #include "lib/crypt_ops/crypto_cipher.h"
  22. #include "lib/crypt_ops/crypto_dh.h"
  23. #include "core/or/channeltls.h"
  24. #include "feature/dircommon/directory.h"
  25. #include "core/mainloop/mainloop.h"
  26. #include "feature/nodelist/nodelist.h"
  27. #include "feature/nodelist/routerset.h"
  28. #include "feature/hs/hs_circuit.h"
  29. #include "feature/hs/hs_circuitmap.h"
  30. #include "feature/hs/hs_client.h"
  31. #include "feature/hs/hs_config.h"
  32. #include "feature/hs/hs_ident.h"
  33. #include "feature/hs/hs_cache.h"
  34. #include "core/or/circuitlist.h"
  35. #include "core/or/circuitbuild.h"
  36. #include "core/mainloop/connection.h"
  37. #include "core/or/connection_edge.h"
  38. #include "feature/nodelist/networkstatus.h"
  39. #include "core/or/cpath_build_state_st.h"
  40. #include "core/or/crypt_path_st.h"
  41. #include "feature/dircommon/dir_connection_st.h"
  42. #include "core/or/entry_connection_st.h"
  43. #include "core/or/extend_info_st.h"
  44. #include "feature/nodelist/networkstatus_st.h"
  45. #include "core/or/origin_circuit_st.h"
  46. #include "core/or/socks_request_st.h"
  47. static int
  48. mock_connection_ap_handshake_send_begin(entry_connection_t *ap_conn)
  49. {
  50. (void) ap_conn;
  51. return 0;
  52. }
  53. static networkstatus_t mock_ns;
  54. /* Always return NULL. */
  55. static networkstatus_t *
  56. mock_networkstatus_get_live_consensus_false(time_t now)
  57. {
  58. (void) now;
  59. return NULL;
  60. }
  61. static networkstatus_t *
  62. mock_networkstatus_get_live_consensus(time_t now)
  63. {
  64. (void) now;
  65. return &mock_ns;
  66. }
  67. static int
  68. helper_config_client(const char *conf, int validate_only)
  69. {
  70. int ret = 0;
  71. or_options_t *options = NULL;
  72. tt_assert(conf);
  73. options = helper_parse_options(conf);
  74. tt_assert(options);
  75. ret = hs_config_client_auth_all(options, validate_only);
  76. done:
  77. or_options_free(options);
  78. return ret;
  79. }
  80. /* Test helper function: Setup a circuit and a stream with the same hidden
  81. * service destination, and put them in <b>circ_out</b> and
  82. * <b>conn_out</b>. Make the stream wait for circuits to be established to the
  83. * hidden service. */
  84. static int
  85. helper_get_circ_and_stream_for_test(origin_circuit_t **circ_out,
  86. connection_t **conn_out,
  87. int is_legacy)
  88. {
  89. int retval;
  90. channel_tls_t *n_chan=NULL;
  91. rend_data_t *conn_rend_data = NULL;
  92. origin_circuit_t *or_circ = NULL;
  93. connection_t *conn = NULL;
  94. ed25519_public_key_t service_pk;
  95. /* Make a dummy connection stream and make it wait for our circuit */
  96. conn = test_conn_get_connection(AP_CONN_STATE_CIRCUIT_WAIT,
  97. CONN_TYPE_AP /* ??? */,
  98. 0);
  99. if (is_legacy) {
  100. /* Legacy: Setup rend_data of stream */
  101. char service_id[REND_SERVICE_ID_LEN_BASE32+1] = {0};
  102. TO_EDGE_CONN(conn)->rend_data = mock_rend_data(service_id);
  103. conn_rend_data = TO_EDGE_CONN(conn)->rend_data;
  104. } else {
  105. /* prop224: Setup hs conn identifier on the stream */
  106. ed25519_secret_key_t sk;
  107. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
  108. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
  109. /* Setup hs_conn_identifier of stream */
  110. TO_EDGE_CONN(conn)->hs_ident = hs_ident_edge_conn_new(&service_pk);
  111. }
  112. /* Make it wait for circuit */
  113. connection_ap_mark_as_pending_circuit(TO_ENTRY_CONN(conn));
  114. /* This is needed to silence a BUG warning from
  115. connection_edge_update_circuit_isolation() */
  116. TO_ENTRY_CONN(conn)->original_dest_address =
  117. tor_strdup(TO_ENTRY_CONN(conn)->socks_request->address);
  118. /****************************************************/
  119. /* Now make dummy circuit */
  120. or_circ = origin_circuit_new();
  121. or_circ->base_.purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
  122. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  123. or_circ->build_state->is_internal = 1;
  124. if (is_legacy) {
  125. /* Legacy: Setup rend data and final cpath */
  126. or_circ->build_state->pending_final_cpath =
  127. tor_malloc_zero(sizeof(crypt_path_t));
  128. or_circ->build_state->pending_final_cpath->magic = CRYPT_PATH_MAGIC;
  129. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state =
  130. crypto_dh_new(DH_TYPE_REND);
  131. tt_assert(
  132. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state);
  133. retval = crypto_dh_generate_public(
  134. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state);
  135. tt_int_op(retval, OP_EQ, 0);
  136. or_circ->rend_data = rend_data_dup(conn_rend_data);
  137. } else {
  138. /* prop224: Setup hs ident on the circuit */
  139. or_circ->hs_ident = hs_ident_circuit_new(&service_pk,
  140. HS_IDENT_CIRCUIT_RENDEZVOUS);
  141. }
  142. TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
  143. /* fake n_chan */
  144. n_chan = tor_malloc_zero(sizeof(channel_tls_t));
  145. n_chan->base_.global_identifier = 1;
  146. or_circ->base_.n_chan = &(n_chan->base_);
  147. *circ_out = or_circ;
  148. *conn_out = conn;
  149. return 0;
  150. done:
  151. /* something failed */
  152. return -1;
  153. }
  154. /* Test: Ensure that setting up legacy e2e rendezvous circuits works
  155. * correctly. */
  156. static void
  157. test_e2e_rend_circuit_setup_legacy(void *arg)
  158. {
  159. ssize_t retval;
  160. origin_circuit_t *or_circ = NULL;
  161. connection_t *conn = NULL;
  162. (void) arg;
  163. /** In this test we create a v2 legacy HS stream and a circuit with the same
  164. * hidden service destination. We make the stream wait for circuits to be
  165. * established to the hidden service, and then we complete the circuit using
  166. * the hs_circuit_setup_e2e_rend_circ_legacy_client() function. We then
  167. * check that the end-to-end cpath was setup correctly and that the stream
  168. * was attached to the circuit as expected. */
  169. MOCK(connection_ap_handshake_send_begin,
  170. mock_connection_ap_handshake_send_begin);
  171. /* Setup */
  172. retval = helper_get_circ_and_stream_for_test( &or_circ, &conn, 1);
  173. tt_int_op(retval, OP_EQ, 0);
  174. tt_assert(or_circ);
  175. tt_assert(conn);
  176. /* Check number of hops */
  177. retval = cpath_get_n_hops(&or_circ->cpath);
  178. tt_int_op(retval, OP_EQ, 0);
  179. /* Check that our stream is not attached on any circuits */
  180. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, NULL);
  181. /********************************************** */
  182. /* Make a good RENDEZVOUS1 cell body because it needs to pass key exchange
  183. * digest verification... */
  184. uint8_t rend_cell_body[DH1024_KEY_LEN+DIGEST_LEN] = {2};
  185. {
  186. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  187. crypto_dh_t *dh_state =
  188. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state;
  189. /* compute and overwrite digest of cell body with the right value */
  190. retval = crypto_dh_compute_secret(LOG_PROTOCOL_WARN, dh_state,
  191. (char*)rend_cell_body, DH1024_KEY_LEN,
  192. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN);
  193. tt_int_op(retval, OP_GT, 0);
  194. memcpy(rend_cell_body+DH1024_KEY_LEN, keys, DIGEST_LEN);
  195. }
  196. /* Setup the circuit */
  197. retval = hs_circuit_setup_e2e_rend_circ_legacy_client(or_circ,
  198. rend_cell_body);
  199. tt_int_op(retval, OP_EQ, 0);
  200. /**********************************************/
  201. /* See that a hop was added to the circuit's cpath */
  202. retval = cpath_get_n_hops(&or_circ->cpath);
  203. tt_int_op(retval, OP_EQ, 1);
  204. /* Check the digest algo */
  205. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.f_digest),
  206. OP_EQ, DIGEST_SHA1);
  207. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.b_digest),
  208. OP_EQ, DIGEST_SHA1);
  209. tt_assert(or_circ->cpath->crypto.f_crypto);
  210. tt_assert(or_circ->cpath->crypto.b_crypto);
  211. /* Ensure that circ purpose was changed */
  212. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_C_REND_JOINED);
  213. /* Test that stream got attached */
  214. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, TO_CIRCUIT(or_circ));
  215. done:
  216. connection_free_minimal(conn);
  217. if (or_circ)
  218. tor_free(TO_CIRCUIT(or_circ)->n_chan);
  219. circuit_free_(TO_CIRCUIT(or_circ));
  220. }
  221. /* Test: Ensure that setting up v3 rendezvous circuits works correctly. */
  222. static void
  223. test_e2e_rend_circuit_setup(void *arg)
  224. {
  225. uint8_t ntor_key_seed[DIGEST256_LEN] = {0};
  226. origin_circuit_t *or_circ = NULL;
  227. int retval;
  228. connection_t *conn = NULL;
  229. (void) arg;
  230. /** In this test we create a prop224 v3 HS stream and a circuit with the same
  231. * hidden service destination. We make the stream wait for circuits to be
  232. * established to the hidden service, and then we complete the circuit using
  233. * the hs_circuit_setup_e2e_rend_circ() function. We then check that the
  234. * end-to-end cpath was setup correctly and that the stream was attached to
  235. * the circuit as expected. */
  236. MOCK(connection_ap_handshake_send_begin,
  237. mock_connection_ap_handshake_send_begin);
  238. /* Setup */
  239. retval = helper_get_circ_and_stream_for_test( &or_circ, &conn, 0);
  240. tt_int_op(retval, OP_EQ, 0);
  241. tt_assert(or_circ);
  242. tt_assert(conn);
  243. /* Check number of hops: There should be no hops yet to this circ */
  244. retval = cpath_get_n_hops(&or_circ->cpath);
  245. tt_int_op(retval, OP_EQ, 0);
  246. tt_ptr_op(or_circ->cpath, OP_EQ, NULL);
  247. /* Check that our stream is not attached on any circuits */
  248. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, NULL);
  249. /**********************************************/
  250. /* Setup the circuit */
  251. retval = hs_circuit_setup_e2e_rend_circ(or_circ,
  252. ntor_key_seed, sizeof(ntor_key_seed),
  253. 0);
  254. tt_int_op(retval, OP_EQ, 0);
  255. /**********************************************/
  256. /* See that a hop was added to the circuit's cpath */
  257. retval = cpath_get_n_hops(&or_circ->cpath);
  258. tt_int_op(retval, OP_EQ, 1);
  259. /* Check that the crypt path has prop224 algorithm parameters */
  260. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.f_digest),
  261. OP_EQ, DIGEST_SHA3_256);
  262. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.b_digest),
  263. OP_EQ, DIGEST_SHA3_256);
  264. tt_assert(or_circ->cpath->crypto.f_crypto);
  265. tt_assert(or_circ->cpath->crypto.b_crypto);
  266. /* Ensure that circ purpose was changed */
  267. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_C_REND_JOINED);
  268. /* Test that stream got attached */
  269. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, TO_CIRCUIT(or_circ));
  270. done:
  271. connection_free_minimal(conn);
  272. if (or_circ)
  273. tor_free(TO_CIRCUIT(or_circ)->n_chan);
  274. circuit_free_(TO_CIRCUIT(or_circ));
  275. }
  276. /** Test client logic for picking intro points from a descriptor. Also test how
  277. * ExcludeNodes and intro point failures affect picking intro points. */
  278. static void
  279. test_client_pick_intro(void *arg)
  280. {
  281. int ret;
  282. ed25519_keypair_t service_kp;
  283. hs_descriptor_t *desc = NULL;
  284. MOCK(networkstatus_get_live_consensus,
  285. mock_networkstatus_get_live_consensus);
  286. (void) arg;
  287. hs_init();
  288. /* Generate service keypair */
  289. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&service_kp, 0));
  290. /* Set time */
  291. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  292. &mock_ns.valid_after);
  293. tt_int_op(ret, OP_EQ, 0);
  294. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  295. &mock_ns.fresh_until);
  296. tt_int_op(ret, OP_EQ, 0);
  297. update_approx_time(mock_ns.fresh_until-10);
  298. time_t now = approx_time();
  299. /* Test logic:
  300. *
  301. * 1) Add our desc with intro points to the HS cache.
  302. *
  303. * 2) Mark all descriptor intro points except _the chosen one_ as
  304. * failed. Then query the desc to get a random intro: check that we got
  305. * _the chosen one_. Then fail the chosen one as well, and see that no
  306. * intros are returned.
  307. *
  308. * 3) Then clean the intro state cache and get an intro point.
  309. *
  310. * 4) Try fetching an intro with the wrong service key: shouldn't work
  311. *
  312. * 5) Set StrictNodes and put all our intro points in ExcludeNodes: see that
  313. * nothing is returned.
  314. */
  315. /* 1) Add desc to HS cache */
  316. {
  317. char *encoded = NULL;
  318. desc = hs_helper_build_hs_desc_with_ip(&service_kp);
  319. ret = hs_desc_encode_descriptor(desc, &service_kp, NULL, &encoded);
  320. tt_int_op(ret, OP_EQ, 0);
  321. tt_assert(encoded);
  322. /* store it */
  323. hs_cache_store_as_client(encoded, &service_kp.pubkey);
  324. /* fetch it to make sure it works */
  325. const hs_descriptor_t *fetched_desc =
  326. hs_cache_lookup_as_client(&service_kp.pubkey);
  327. tt_assert(fetched_desc);
  328. tt_mem_op(fetched_desc->subcredential, OP_EQ, desc->subcredential,
  329. DIGEST256_LEN);
  330. tt_assert(!fast_mem_is_zero((char*)fetched_desc->subcredential,
  331. DIGEST256_LEN));
  332. tor_free(encoded);
  333. }
  334. /* 2) Mark all intro points except _the chosen one_ as failed. Then query the
  335. * desc and get a random intro: check that we got _the chosen one_. */
  336. {
  337. /* Tell hs_get_extend_info_from_lspecs() to skip the private address check.
  338. */
  339. get_options_mutable()->ExtendAllowPrivateAddresses = 1;
  340. /* Pick the chosen intro point and get its ei */
  341. hs_desc_intro_point_t *chosen_intro_point =
  342. smartlist_get(desc->encrypted_data.intro_points, 0);
  343. extend_info_t *chosen_intro_ei =
  344. desc_intro_point_to_extend_info(chosen_intro_point);
  345. tt_assert(chosen_intro_point);
  346. tt_assert(chosen_intro_ei);
  347. /* Now mark all other intro points as failed */
  348. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  349. hs_desc_intro_point_t *, ip) {
  350. /* Skip the chosen intro point */
  351. if (ip == chosen_intro_point) {
  352. continue;
  353. }
  354. ed25519_public_key_t *intro_auth_key = &ip->auth_key_cert->signed_key;
  355. hs_cache_client_intro_state_note(&service_kp.pubkey,
  356. intro_auth_key,
  357. INTRO_POINT_FAILURE_GENERIC);
  358. } SMARTLIST_FOREACH_END(ip);
  359. /* Try to get a random intro: Should return the chosen one! */
  360. /* (We try several times, to make sure this behavior is consistent, and to
  361. * cover the different cases of client_get_random_intro().) */
  362. for (int i = 0; i < 64; ++i) {
  363. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  364. tor_assert(ip);
  365. tt_assert(!fast_mem_is_zero((char*)ip->identity_digest, DIGEST_LEN));
  366. tt_mem_op(ip->identity_digest, OP_EQ, chosen_intro_ei->identity_digest,
  367. DIGEST_LEN);
  368. extend_info_free(ip);
  369. }
  370. extend_info_free(chosen_intro_ei);
  371. /* Now also mark the chosen one as failed: See that we can't get any intro
  372. points anymore. */
  373. hs_cache_client_intro_state_note(&service_kp.pubkey,
  374. &chosen_intro_point->auth_key_cert->signed_key,
  375. INTRO_POINT_FAILURE_TIMEOUT);
  376. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  377. tor_assert(!ip);
  378. }
  379. /* 3) Clean the intro state cache and get an intro point */
  380. {
  381. /* Pretend we are 5 mins in the future and order a cleanup of the intro
  382. * state. This should clean up the intro point failures and allow us to get
  383. * an intro. */
  384. hs_cache_client_intro_state_clean(now + 5*60);
  385. /* Get an intro. It should work! */
  386. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  387. tor_assert(ip);
  388. extend_info_free(ip);
  389. }
  390. /* 4) Try fetching an intro with the wrong service key: shouldn't work */
  391. {
  392. ed25519_keypair_t dummy_kp;
  393. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&dummy_kp, 0));
  394. extend_info_t *ip = client_get_random_intro(&dummy_kp.pubkey);
  395. tor_assert(!ip);
  396. }
  397. /* 5) Set StrictNodes and put all our intro points in ExcludeNodes: see that
  398. * nothing is returned. */
  399. {
  400. get_options_mutable()->ExcludeNodes = routerset_new();
  401. get_options_mutable()->StrictNodes = 1;
  402. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  403. hs_desc_intro_point_t *, ip) {
  404. extend_info_t *intro_ei = desc_intro_point_to_extend_info(ip);
  405. tt_assert(intro_ei);
  406. if (intro_ei) {
  407. const char *ptr;
  408. char ip_addr[TOR_ADDR_BUF_LEN];
  409. /* We need to decorate in case it is an IPv6 else routerset_parse()
  410. * doesn't like it. */
  411. ptr = tor_addr_to_str(ip_addr, &intro_ei->addr, sizeof(ip_addr), 1);
  412. tt_assert(ptr == ip_addr);
  413. ret = routerset_parse(get_options_mutable()->ExcludeNodes,
  414. ip_addr, "");
  415. tt_int_op(ret, OP_EQ, 0);
  416. extend_info_free(intro_ei);
  417. }
  418. } SMARTLIST_FOREACH_END(ip);
  419. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  420. tt_assert(!ip);
  421. }
  422. done:
  423. hs_descriptor_free(desc);
  424. }
  425. static int
  426. mock_router_have_minimum_dir_info_false(void)
  427. {
  428. return 0;
  429. }
  430. static int
  431. mock_router_have_minimum_dir_info_true(void)
  432. {
  433. return 1;
  434. }
  435. static hs_client_fetch_status_t
  436. mock_fetch_v3_desc_error(const ed25519_public_key_t *key)
  437. {
  438. (void) key;
  439. return HS_CLIENT_FETCH_ERROR;
  440. }
  441. static void
  442. mock_connection_mark_unattached_ap_(entry_connection_t *conn, int endreason,
  443. int line, const char *file)
  444. {
  445. (void) line;
  446. (void) file;
  447. conn->edge_.end_reason = endreason;
  448. /* This function ultimately will flag this so make sure we do also in the
  449. * MOCK one so we can assess closed connections vs open ones. */
  450. conn->edge_.base_.marked_for_close = 1;
  451. }
  452. static void
  453. test_descriptor_fetch(void *arg)
  454. {
  455. int ret;
  456. entry_connection_t *ec = NULL;
  457. ed25519_public_key_t service_pk;
  458. ed25519_secret_key_t service_sk;
  459. (void) arg;
  460. hs_init();
  461. memset(&service_sk, 'A', sizeof(service_sk));
  462. ret = ed25519_public_key_generate(&service_pk, &service_sk);
  463. tt_int_op(ret, OP_EQ, 0);
  464. /* Initialize this so get_voting_interval() doesn't freak out. */
  465. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  466. &mock_ns.valid_after);
  467. tt_int_op(ret, OP_EQ, 0);
  468. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  469. &mock_ns.fresh_until);
  470. tt_int_op(ret, OP_EQ, 0);
  471. ec = entry_connection_new(CONN_TYPE_AP, AF_INET);
  472. tt_assert(ec);
  473. ENTRY_TO_EDGE_CONN(ec)->hs_ident = hs_ident_edge_conn_new(&service_pk);
  474. tt_assert(ENTRY_TO_EDGE_CONN(ec)->hs_ident);
  475. TO_CONN(ENTRY_TO_EDGE_CONN(ec))->state = AP_CONN_STATE_RENDDESC_WAIT;
  476. smartlist_add(get_connection_array(), &ec->edge_.base_);
  477. /* 1. FetchHidServDescriptors is false so we shouldn't be able to fetch. */
  478. get_options_mutable()->FetchHidServDescriptors = 0;
  479. ret = hs_client_refetch_hsdesc(&service_pk);
  480. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_NOT_ALLOWED);
  481. get_options_mutable()->FetchHidServDescriptors = 1;
  482. /* 2. We don't have a live consensus. */
  483. MOCK(networkstatus_get_live_consensus,
  484. mock_networkstatus_get_live_consensus_false);
  485. ret = hs_client_refetch_hsdesc(&service_pk);
  486. UNMOCK(networkstatus_get_live_consensus);
  487. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_MISSING_INFO);
  488. /* From now on, return a live consensus. */
  489. MOCK(networkstatus_get_live_consensus,
  490. mock_networkstatus_get_live_consensus);
  491. /* 3. Not enough dir information. */
  492. MOCK(router_have_minimum_dir_info,
  493. mock_router_have_minimum_dir_info_false);
  494. ret = hs_client_refetch_hsdesc(&service_pk);
  495. UNMOCK(router_have_minimum_dir_info);
  496. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_MISSING_INFO);
  497. /* From now on, we do have enough directory information. */
  498. MOCK(router_have_minimum_dir_info,
  499. mock_router_have_minimum_dir_info_true);
  500. /* 4. We do have a pending directory request. */
  501. {
  502. dir_connection_t *dir_conn = dir_connection_new(AF_INET);
  503. dir_conn->hs_ident = tor_malloc_zero(sizeof(hs_ident_dir_conn_t));
  504. TO_CONN(dir_conn)->purpose = DIR_PURPOSE_FETCH_HSDESC;
  505. ed25519_pubkey_copy(&dir_conn->hs_ident->identity_pk, &service_pk);
  506. smartlist_add(get_connection_array(), TO_CONN(dir_conn));
  507. ret = hs_client_refetch_hsdesc(&service_pk);
  508. smartlist_remove(get_connection_array(), TO_CONN(dir_conn));
  509. connection_free_minimal(TO_CONN(dir_conn));
  510. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_PENDING);
  511. }
  512. /* 5. We'll trigger an error on the fetch_desc_v3 and force to close all
  513. * pending SOCKS request. */
  514. MOCK(router_have_minimum_dir_info,
  515. mock_router_have_minimum_dir_info_true);
  516. MOCK(fetch_v3_desc, mock_fetch_v3_desc_error);
  517. MOCK(connection_mark_unattached_ap_,
  518. mock_connection_mark_unattached_ap_);
  519. ret = hs_client_refetch_hsdesc(&service_pk);
  520. UNMOCK(fetch_v3_desc);
  521. UNMOCK(connection_mark_unattached_ap_);
  522. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_ERROR);
  523. /* The close waiting for descriptor function has been called. */
  524. tt_int_op(ec->edge_.end_reason, OP_EQ, END_STREAM_REASON_RESOLVEFAILED);
  525. done:
  526. connection_free_minimal(ENTRY_TO_CONN(ec));
  527. UNMOCK(networkstatus_get_live_consensus);
  528. UNMOCK(router_have_minimum_dir_info);
  529. hs_free_all();
  530. }
  531. static void
  532. test_auth_key_filename_is_valid(void *arg)
  533. {
  534. (void) arg;
  535. /* Valid file name. */
  536. tt_assert(auth_key_filename_is_valid("a.auth_private"));
  537. /* Valid file name with special character. */
  538. tt_assert(auth_key_filename_is_valid("a-.auth_private"));
  539. /* Invalid extension. */
  540. tt_assert(!auth_key_filename_is_valid("a.ath_private"));
  541. /* Nothing before the extension. */
  542. tt_assert(!auth_key_filename_is_valid(".auth_private"));
  543. done:
  544. ;
  545. }
  546. static void
  547. test_parse_auth_file_content(void *arg)
  548. {
  549. hs_client_service_authorization_t *auth = NULL;
  550. (void) arg;
  551. /* Valid authorized client. */
  552. auth = parse_auth_file_content(
  553. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad:descriptor:"
  554. "x25519:zdsyvn2jq534ugyiuzgjy4267jbtzcjbsgedhshzx5mforyxtryq");
  555. tt_assert(auth);
  556. /* Wrong number of fields. */
  557. tt_assert(!parse_auth_file_content("a:b"));
  558. /* Wrong auth type. */
  559. tt_assert(!parse_auth_file_content(
  560. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad:x:"
  561. "x25519:zdsyvn2jq534ugyiuzgjy4267jbtzcjbsgedhshzx5mforyxtryq"));
  562. /* Wrong key type. */
  563. tt_assert(!parse_auth_file_content(
  564. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad:descriptor:"
  565. "x:zdsyvn2jq534ugyiuzgjy4267jbtzcjbsgedhshzx5mforyxtryq"));
  566. /* Some malformed string. */
  567. tt_assert(!parse_auth_file_content("xx:descriptor:x25519:aa=="));
  568. /* Bigger key than it should be */
  569. tt_assert(!parse_auth_file_content("xx:descriptor:x25519:"
  570. "vjqea4jbhwwc4hto7ekyvqfbeodghbaq6nxi45hz4wr3qvhqv3yqa"));
  571. done:
  572. tor_free(auth);
  573. }
  574. static char *
  575. mock_read_file_to_str(const char *filename, int flags, struct stat *stat_out)
  576. {
  577. char *ret = NULL;
  578. (void) flags;
  579. (void) stat_out;
  580. if (!strcmp(filename, get_fname("auth_keys" PATH_SEPARATOR
  581. "client1.auth_private"))) {
  582. ret = tor_strdup(
  583. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad:descriptor:"
  584. "x25519:zdsyvn2jq534ugyiuzgjy4267jbtzcjbsgedhshzx5mforyxtryq");
  585. goto done;
  586. }
  587. if (!strcmp(filename, get_fname("auth_keys" PATH_SEPARATOR "dummy.xxx"))) {
  588. ret = tor_strdup(
  589. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:descriptor:"
  590. "x25519:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  591. goto done;
  592. }
  593. if (!strcmp(filename, get_fname("auth_keys" PATH_SEPARATOR
  594. "client2.auth_private"))) {
  595. ret = tor_strdup(
  596. "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid:descriptor:"
  597. "x25519:fdreqzjqso7d2ac7qscrxfl5qfpamdvgy5d6cxejcgzc3hvhurmq");
  598. goto done;
  599. }
  600. done:
  601. return ret;
  602. }
  603. static int
  604. mock_check_private_dir(const char *dirname, cpd_check_t check,
  605. const char *effective_user)
  606. {
  607. (void) dirname;
  608. (void) check;
  609. (void) effective_user;
  610. return 0;
  611. }
  612. static smartlist_t *
  613. mock_tor_listdir(const char *dirname)
  614. {
  615. smartlist_t *file_list = smartlist_new();
  616. (void) dirname;
  617. smartlist_add(file_list, tor_strdup("client1.auth_private"));
  618. smartlist_add(file_list, tor_strdup("dummy.xxx"));
  619. smartlist_add(file_list, tor_strdup("client2.auth_private"));
  620. return file_list;
  621. }
  622. static void
  623. test_config_client_authorization(void *arg)
  624. {
  625. int ret;
  626. char *conf = NULL;
  627. ed25519_public_key_t pk1, pk2;
  628. digest256map_t *global_map = NULL;
  629. char *key_dir = tor_strdup(get_fname("auth_keys"));
  630. (void) arg;
  631. MOCK(read_file_to_str, mock_read_file_to_str);
  632. MOCK(tor_listdir, mock_tor_listdir);
  633. MOCK(check_private_dir, mock_check_private_dir);
  634. #define conf_fmt \
  635. "ClientOnionAuthDir %s\n"
  636. tor_asprintf(&conf, conf_fmt, key_dir);
  637. ret = helper_config_client(conf, 0);
  638. tor_free(conf);
  639. tt_int_op(ret, OP_EQ, 0);
  640. #undef conf_fmt
  641. global_map = get_hs_client_auths_map();
  642. tt_int_op(digest256map_size(global_map), OP_EQ, 2);
  643. hs_parse_address("4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad",
  644. &pk1, NULL, NULL);
  645. hs_parse_address("25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid",
  646. &pk2, NULL, NULL);
  647. tt_assert(digest256map_get(global_map, pk1.pubkey));
  648. tt_assert(digest256map_get(global_map, pk2.pubkey));
  649. done:
  650. tor_free(key_dir);
  651. hs_free_all();
  652. UNMOCK(read_file_to_str);
  653. UNMOCK(tor_listdir);
  654. UNMOCK(check_private_dir);
  655. }
  656. static entry_connection_t *
  657. helper_build_socks_connection(const ed25519_public_key_t *service_pk,
  658. int conn_state)
  659. {
  660. entry_connection_t *socks = entry_connection_new(CONN_TYPE_AP, AF_INET);
  661. ENTRY_TO_EDGE_CONN(socks)->hs_ident = hs_ident_edge_conn_new(service_pk);
  662. TO_CONN(ENTRY_TO_EDGE_CONN(socks))->state = conn_state;
  663. smartlist_add(get_connection_array(), &socks->edge_.base_);
  664. return socks;
  665. }
  666. static void
  667. test_desc_has_arrived_cleanup(void *arg)
  668. {
  669. /* The goal of this test is to make sure we clean up everything in between
  670. * two descriptors from the same .onion. Because intro points can change
  671. * from one descriptor to another, once we received a new descriptor, we
  672. * need to cleanup the remaining circuits so they aren't used or selected
  673. * when establishing a connection with the newly stored descriptor.
  674. *
  675. * This test was created because of #27410. */
  676. int ret;
  677. char *desc_str = NULL;
  678. hs_descriptor_t *desc = NULL;
  679. const hs_descriptor_t *cached_desc;
  680. ed25519_keypair_t signing_kp;
  681. entry_connection_t *socks1 = NULL, *socks2 = NULL;
  682. hs_ident_dir_conn_t hs_dir_ident;
  683. (void) arg;
  684. hs_init();
  685. MOCK(networkstatus_get_live_consensus,
  686. mock_networkstatus_get_live_consensus);
  687. MOCK(connection_mark_unattached_ap_,
  688. mock_connection_mark_unattached_ap_);
  689. MOCK(router_have_minimum_dir_info,
  690. mock_router_have_minimum_dir_info_true);
  691. /* Set consensus time before our time so the cache lookup can always
  692. * validate that the entry is not expired. */
  693. parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC", &mock_ns.valid_after);
  694. parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC", &mock_ns.fresh_until);
  695. parse_rfc1123_time("Sat, 26 Oct 1985 16:00:00 UTC", &mock_ns.valid_until);
  696. /* Build a descriptor for a specific .onion. */
  697. ret = ed25519_keypair_generate(&signing_kp, 0);
  698. tt_int_op(ret, OP_EQ, 0);
  699. desc = hs_helper_build_hs_desc_with_ip(&signing_kp);
  700. tt_assert(desc);
  701. ret = hs_desc_encode_descriptor(desc, &signing_kp, NULL, &desc_str);
  702. tt_int_op(ret, OP_EQ, 0);
  703. /* Store in the client cache. */
  704. ret = hs_cache_store_as_client(desc_str, &signing_kp.pubkey);
  705. tt_int_op(ret, OP_EQ, 0);
  706. cached_desc = hs_cache_lookup_as_client(&signing_kp.pubkey);
  707. tt_assert(cached_desc);
  708. hs_helper_desc_equal(desc, cached_desc);
  709. /* Create two SOCKS connection for the same .onion both in the waiting for a
  710. * descriptor state. */
  711. socks1 = helper_build_socks_connection(&signing_kp.pubkey,
  712. AP_CONN_STATE_RENDDESC_WAIT);
  713. tt_assert(socks1);
  714. socks2 = helper_build_socks_connection(&signing_kp.pubkey,
  715. AP_CONN_STATE_RENDDESC_WAIT);
  716. tt_assert(socks2);
  717. /* Now, we'll make the intro points in the current descriptor unusable so
  718. * the hs_client_desc_has_arrived() will take the right code path that we
  719. * want to test that is the fetched descriptor has bad intro points. */
  720. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  721. hs_desc_intro_point_t *, ip) {
  722. hs_cache_client_intro_state_note(&signing_kp.pubkey,
  723. &ip->auth_key_cert->signed_key,
  724. INTRO_POINT_FAILURE_GENERIC);
  725. } SMARTLIST_FOREACH_END(ip);
  726. /* Simulate that a new descriptor just arrived. We should have both of our
  727. * SOCKS connection to be ended with a resolved failed. */
  728. hs_ident_dir_conn_init(&signing_kp.pubkey,
  729. &desc->plaintext_data.blinded_pubkey, &hs_dir_ident);
  730. hs_client_desc_has_arrived(&hs_dir_ident);
  731. tt_int_op(socks1->edge_.end_reason, OP_EQ, END_STREAM_REASON_RESOLVEFAILED);
  732. /* XXX: MUST work with OP_EQ. */
  733. tt_int_op(socks2->edge_.end_reason, OP_EQ, END_STREAM_REASON_RESOLVEFAILED);
  734. /* Now let say tor cleans up the intro state cache which resets all intro
  735. * point failure count. */
  736. hs_cache_client_intro_state_purge();
  737. /* Retrying all SOCKS which should basically do nothing since we don't have
  738. * any pending SOCKS connection in AP_CONN_STATE_RENDDESC_WAIT state. */
  739. /* XXX: BUG() is triggered here, shouldn't if socks2 wasn't alive. */
  740. retry_all_socks_conn_waiting_for_desc();
  741. done:
  742. connection_free_minimal(ENTRY_TO_CONN(socks1));
  743. connection_free_minimal(ENTRY_TO_CONN(socks2));
  744. hs_descriptor_free(desc);
  745. tor_free(desc_str);
  746. hs_free_all();
  747. UNMOCK(networkstatus_get_live_consensus);
  748. UNMOCK(connection_mark_unattached_ap_);
  749. UNMOCK(router_have_minimum_dir_info);
  750. }
  751. static void
  752. test_close_intro_circuits_new_desc(void *arg)
  753. {
  754. int ret;
  755. ed25519_keypair_t service_kp;
  756. circuit_t *circ = NULL;
  757. origin_circuit_t *ocirc = NULL;
  758. hs_descriptor_t *desc1 = NULL, *desc2 = NULL;
  759. (void) arg;
  760. hs_init();
  761. /* This is needed because of the client cache expiration timestamp is based
  762. * on having a consensus. See cached_client_descriptor_has_expired(). */
  763. MOCK(networkstatus_get_live_consensus,
  764. mock_networkstatus_get_live_consensus);
  765. /* Set consensus time */
  766. parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  767. &mock_ns.valid_after);
  768. parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  769. &mock_ns.fresh_until);
  770. parse_rfc1123_time("Sat, 26 Oct 1985 16:00:00 UTC",
  771. &mock_ns.valid_until);
  772. /* Generate service keypair */
  773. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&service_kp, 0));
  774. /* Create and add to the global list a dummy client introduction circuits.
  775. * We'll then make sure the hs_ident is attached to a dummy descriptor. */
  776. circ = dummy_origin_circuit_new(0);
  777. tt_assert(circ);
  778. circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  779. ocirc = TO_ORIGIN_CIRCUIT(circ);
  780. /* Build the first descriptor and cache it. */
  781. {
  782. char *encoded;
  783. desc1 = hs_helper_build_hs_desc_with_ip(&service_kp);
  784. tt_assert(desc1);
  785. ret = hs_desc_encode_descriptor(desc1, &service_kp, NULL, &encoded);
  786. tt_int_op(ret, OP_EQ, 0);
  787. tt_assert(encoded);
  788. /* Store it */
  789. ret = hs_cache_store_as_client(encoded, &service_kp.pubkey);
  790. tt_int_op(ret, OP_EQ, 0);
  791. tor_free(encoded);
  792. tt_assert(hs_cache_lookup_as_client(&service_kp.pubkey));
  793. }
  794. /* We'll pick one introduction point and associate it with the circuit. */
  795. {
  796. const hs_desc_intro_point_t *ip =
  797. smartlist_get(desc1->encrypted_data.intro_points, 0);
  798. tt_assert(ip);
  799. ocirc->hs_ident = hs_ident_circuit_new(&service_kp.pubkey,
  800. HS_IDENT_CIRCUIT_INTRO);
  801. ed25519_pubkey_copy(&ocirc->hs_ident->intro_auth_pk,
  802. &ip->auth_key_cert->signed_key);
  803. }
  804. /* Before we are about to clean up the intro circuits, make sure it is
  805. * actually there. */
  806. tt_assert(circuit_get_next_intro_circ(NULL, true));
  807. /* Build the second descriptor for the same service and cache it. */
  808. {
  809. char *encoded;
  810. desc2 = hs_helper_build_hs_desc_with_ip(&service_kp);
  811. tt_assert(desc2);
  812. tt_mem_op(&desc1->plaintext_data.signing_pubkey, OP_EQ,
  813. &desc2->plaintext_data.signing_pubkey, ED25519_PUBKEY_LEN);
  814. /* To replace the existing descriptor, the revision counter needs to be
  815. * bigger. */
  816. desc2->plaintext_data.revision_counter =
  817. desc1->plaintext_data.revision_counter + 1;
  818. ret = hs_desc_encode_descriptor(desc2, &service_kp, NULL, &encoded);
  819. tt_int_op(ret, OP_EQ, 0);
  820. tt_assert(encoded);
  821. hs_cache_store_as_client(encoded, &service_kp.pubkey);
  822. tt_int_op(ret, OP_EQ, 0);
  823. tor_free(encoded);
  824. tt_assert(hs_cache_lookup_as_client(&service_kp.pubkey));
  825. }
  826. /* Once stored, our intro circuit should be closed because it is related to
  827. * an old introduction point that doesn't exists anymore. */
  828. tt_assert(!circuit_get_next_intro_circ(NULL, true));
  829. done:
  830. circuit_free(circ);
  831. hs_descriptor_free(desc1);
  832. hs_descriptor_free(desc2);
  833. hs_free_all();
  834. UNMOCK(networkstatus_get_live_consensus);
  835. }
  836. struct testcase_t hs_client_tests[] = {
  837. { "e2e_rend_circuit_setup_legacy", test_e2e_rend_circuit_setup_legacy,
  838. TT_FORK, NULL, NULL },
  839. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup,
  840. TT_FORK, NULL, NULL },
  841. { "client_pick_intro", test_client_pick_intro,
  842. TT_FORK, NULL, NULL },
  843. { "descriptor_fetch", test_descriptor_fetch,
  844. TT_FORK, NULL, NULL },
  845. { "auth_key_filename_is_valid", test_auth_key_filename_is_valid, TT_FORK,
  846. NULL, NULL },
  847. { "parse_auth_file_content", test_parse_auth_file_content, TT_FORK,
  848. NULL, NULL },
  849. { "config_client_authorization", test_config_client_authorization,
  850. TT_FORK, NULL, NULL },
  851. { "desc_has_arrived_cleanup", test_desc_has_arrived_cleanup,
  852. TT_FORK, NULL, NULL },
  853. { "close_intro_circuits_new_desc", test_close_intro_circuits_new_desc,
  854. TT_FORK, NULL, NULL },
  855. END_OF_TESTCASES
  856. };