test_hs_client.c 34 KB

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