test_hs_client.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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 "feature/rend/rendcache.h"
  36. #include "core/or/circuitlist.h"
  37. #include "core/or/circuitbuild.h"
  38. #include "core/mainloop/connection.h"
  39. #include "core/or/connection_edge.h"
  40. #include "feature/nodelist/networkstatus.h"
  41. #include "core/or/cpath_build_state_st.h"
  42. #include "core/or/crypt_path_st.h"
  43. #include "core/or/crypt_path.h"
  44. #include "feature/dircommon/dir_connection_st.h"
  45. #include "core/or/entry_connection_st.h"
  46. #include "core/or/extend_info_st.h"
  47. #include "feature/nodelist/networkstatus_st.h"
  48. #include "core/or/origin_circuit_st.h"
  49. #include "core/or/socks_request_st.h"
  50. static int
  51. mock_connection_ap_handshake_send_begin(entry_connection_t *ap_conn)
  52. {
  53. (void) ap_conn;
  54. return 0;
  55. }
  56. static networkstatus_t mock_ns;
  57. /* Always return NULL. */
  58. static networkstatus_t *
  59. mock_networkstatus_get_live_consensus_false(time_t now)
  60. {
  61. (void) now;
  62. return NULL;
  63. }
  64. static networkstatus_t *
  65. mock_networkstatus_get_live_consensus(time_t now)
  66. {
  67. (void) now;
  68. return &mock_ns;
  69. }
  70. static int
  71. helper_config_client(const char *conf, int validate_only)
  72. {
  73. int ret = 0;
  74. or_options_t *options = NULL;
  75. tt_assert(conf);
  76. options = helper_parse_options(conf);
  77. tt_assert(options);
  78. ret = hs_config_client_auth_all(options, validate_only);
  79. done:
  80. or_options_free(options);
  81. return ret;
  82. }
  83. /* Test helper function: Setup a circuit and a stream with the same hidden
  84. * service destination, and put them in <b>circ_out</b> and
  85. * <b>conn_out</b>. Make the stream wait for circuits to be established to the
  86. * hidden service. */
  87. static int
  88. helper_get_circ_and_stream_for_test(origin_circuit_t **circ_out,
  89. connection_t **conn_out,
  90. int is_legacy)
  91. {
  92. int retval;
  93. channel_tls_t *n_chan=NULL;
  94. rend_data_t *conn_rend_data = NULL;
  95. origin_circuit_t *or_circ = NULL;
  96. connection_t *conn = NULL;
  97. ed25519_public_key_t service_pk;
  98. /* Make a dummy connection stream and make it wait for our circuit */
  99. conn = test_conn_get_connection(AP_CONN_STATE_CIRCUIT_WAIT,
  100. CONN_TYPE_AP /* ??? */,
  101. 0);
  102. if (is_legacy) {
  103. /* Legacy: Setup rend_data of stream */
  104. char service_id[REND_SERVICE_ID_LEN_BASE32+1] = {0};
  105. TO_EDGE_CONN(conn)->rend_data = mock_rend_data(service_id);
  106. conn_rend_data = TO_EDGE_CONN(conn)->rend_data;
  107. } else {
  108. /* prop224: Setup hs conn identifier on the stream */
  109. ed25519_secret_key_t sk;
  110. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
  111. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
  112. /* Setup hs_conn_identifier of stream */
  113. TO_EDGE_CONN(conn)->hs_ident = hs_ident_edge_conn_new(&service_pk);
  114. }
  115. /* Make it wait for circuit */
  116. connection_ap_mark_as_pending_circuit(TO_ENTRY_CONN(conn));
  117. /* This is needed to silence a BUG warning from
  118. connection_edge_update_circuit_isolation() */
  119. TO_ENTRY_CONN(conn)->original_dest_address =
  120. tor_strdup(TO_ENTRY_CONN(conn)->socks_request->address);
  121. /****************************************************/
  122. /* Now make dummy circuit */
  123. or_circ = origin_circuit_new();
  124. or_circ->base_.purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
  125. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  126. or_circ->build_state->is_internal = 1;
  127. if (is_legacy) {
  128. /* Legacy: Setup rend data and final cpath */
  129. or_circ->build_state->pending_final_cpath =
  130. tor_malloc_zero(sizeof(crypt_path_t));
  131. or_circ->build_state->pending_final_cpath->magic = CRYPT_PATH_MAGIC;
  132. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state =
  133. crypto_dh_new(DH_TYPE_REND);
  134. tt_assert(
  135. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state);
  136. retval = crypto_dh_generate_public(
  137. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state);
  138. tt_int_op(retval, OP_EQ, 0);
  139. or_circ->rend_data = rend_data_dup(conn_rend_data);
  140. } else {
  141. /* prop224: Setup hs ident on the circuit */
  142. or_circ->hs_ident = hs_ident_circuit_new(&service_pk);
  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(!fast_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(!fast_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. /* desc_intro_point_to_extend_info() doesn't return IPv6 intro points
  412. * yet, because we can't extend to them. See #24404, #24451, and #24181.
  413. */
  414. if (intro_ei == NULL) {
  415. /* Pretend we're making a direct connection, and that we can use IPv6
  416. */
  417. get_options_mutable()->ClientUseIPv6 = 1;
  418. intro_ei = hs_get_extend_info_from_lspecs(ip->link_specifiers,
  419. &ip->onion_key, 1);
  420. tt_assert(tor_addr_family(&intro_ei->addr) == AF_INET6);
  421. }
  422. tt_assert(intro_ei);
  423. if (intro_ei) {
  424. const char *ptr;
  425. char ip_addr[TOR_ADDR_BUF_LEN];
  426. /* We need to decorate in case it is an IPv6 else routerset_parse()
  427. * doesn't like it. */
  428. ptr = tor_addr_to_str(ip_addr, &intro_ei->addr, sizeof(ip_addr), 1);
  429. tt_assert(ptr == ip_addr);
  430. ret = routerset_parse(get_options_mutable()->ExcludeNodes,
  431. ip_addr, "");
  432. tt_int_op(ret, OP_EQ, 0);
  433. extend_info_free(intro_ei);
  434. }
  435. } SMARTLIST_FOREACH_END(ip);
  436. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  437. tt_assert(!ip);
  438. }
  439. done:
  440. hs_descriptor_free(desc);
  441. }
  442. static int
  443. mock_router_have_minimum_dir_info_false(void)
  444. {
  445. return 0;
  446. }
  447. static int
  448. mock_router_have_minimum_dir_info_true(void)
  449. {
  450. return 1;
  451. }
  452. static hs_client_fetch_status_t
  453. mock_fetch_v3_desc_error(const ed25519_public_key_t *key)
  454. {
  455. (void) key;
  456. return HS_CLIENT_FETCH_ERROR;
  457. }
  458. static void
  459. mock_connection_mark_unattached_ap_(entry_connection_t *conn, int endreason,
  460. int line, const char *file)
  461. {
  462. (void) line;
  463. (void) file;
  464. conn->edge_.end_reason = endreason;
  465. /* This function ultimately will flag this so make sure we do also in the
  466. * MOCK one so we can assess closed connections vs open ones. */
  467. conn->edge_.base_.marked_for_close = 1;
  468. }
  469. static void
  470. test_descriptor_fetch(void *arg)
  471. {
  472. int ret;
  473. entry_connection_t *ec = NULL;
  474. ed25519_public_key_t service_pk;
  475. ed25519_secret_key_t service_sk;
  476. (void) arg;
  477. hs_init();
  478. memset(&service_sk, 'A', sizeof(service_sk));
  479. ret = ed25519_public_key_generate(&service_pk, &service_sk);
  480. tt_int_op(ret, OP_EQ, 0);
  481. /* Initialize this so get_voting_interval() doesn't freak out. */
  482. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  483. &mock_ns.valid_after);
  484. tt_int_op(ret, OP_EQ, 0);
  485. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  486. &mock_ns.fresh_until);
  487. tt_int_op(ret, OP_EQ, 0);
  488. ec = entry_connection_new(CONN_TYPE_AP, AF_INET);
  489. tt_assert(ec);
  490. ENTRY_TO_EDGE_CONN(ec)->hs_ident = hs_ident_edge_conn_new(&service_pk);
  491. tt_assert(ENTRY_TO_EDGE_CONN(ec)->hs_ident);
  492. TO_CONN(ENTRY_TO_EDGE_CONN(ec))->state = AP_CONN_STATE_RENDDESC_WAIT;
  493. smartlist_add(get_connection_array(), &ec->edge_.base_);
  494. /* 1. FetchHidServDescriptors is false so we shouldn't be able to fetch. */
  495. get_options_mutable()->FetchHidServDescriptors = 0;
  496. ret = hs_client_refetch_hsdesc(&service_pk);
  497. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_NOT_ALLOWED);
  498. get_options_mutable()->FetchHidServDescriptors = 1;
  499. /* 2. We don't have a live consensus. */
  500. MOCK(networkstatus_get_live_consensus,
  501. mock_networkstatus_get_live_consensus_false);
  502. ret = hs_client_refetch_hsdesc(&service_pk);
  503. UNMOCK(networkstatus_get_live_consensus);
  504. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_MISSING_INFO);
  505. /* From now on, return a live consensus. */
  506. MOCK(networkstatus_get_live_consensus,
  507. mock_networkstatus_get_live_consensus);
  508. /* 3. Not enough dir information. */
  509. MOCK(router_have_minimum_dir_info,
  510. mock_router_have_minimum_dir_info_false);
  511. ret = hs_client_refetch_hsdesc(&service_pk);
  512. UNMOCK(router_have_minimum_dir_info);
  513. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_MISSING_INFO);
  514. /* From now on, we do have enough directory information. */
  515. MOCK(router_have_minimum_dir_info,
  516. mock_router_have_minimum_dir_info_true);
  517. /* 4. We do have a pending directory request. */
  518. {
  519. dir_connection_t *dir_conn = dir_connection_new(AF_INET);
  520. dir_conn->hs_ident = tor_malloc_zero(sizeof(hs_ident_dir_conn_t));
  521. TO_CONN(dir_conn)->purpose = DIR_PURPOSE_FETCH_HSDESC;
  522. ed25519_pubkey_copy(&dir_conn->hs_ident->identity_pk, &service_pk);
  523. smartlist_add(get_connection_array(), TO_CONN(dir_conn));
  524. ret = hs_client_refetch_hsdesc(&service_pk);
  525. smartlist_remove(get_connection_array(), TO_CONN(dir_conn));
  526. connection_free_minimal(TO_CONN(dir_conn));
  527. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_PENDING);
  528. }
  529. /* 5. We'll trigger an error on the fetch_desc_v3 and force to close all
  530. * pending SOCKS request. */
  531. MOCK(router_have_minimum_dir_info,
  532. mock_router_have_minimum_dir_info_true);
  533. MOCK(fetch_v3_desc, mock_fetch_v3_desc_error);
  534. MOCK(connection_mark_unattached_ap_,
  535. mock_connection_mark_unattached_ap_);
  536. ret = hs_client_refetch_hsdesc(&service_pk);
  537. UNMOCK(fetch_v3_desc);
  538. UNMOCK(connection_mark_unattached_ap_);
  539. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_ERROR);
  540. /* The close waiting for descriptor function has been called. */
  541. tt_int_op(ec->edge_.end_reason, OP_EQ, END_STREAM_REASON_RESOLVEFAILED);
  542. done:
  543. connection_free_minimal(ENTRY_TO_CONN(ec));
  544. UNMOCK(networkstatus_get_live_consensus);
  545. UNMOCK(router_have_minimum_dir_info);
  546. hs_free_all();
  547. }
  548. static void
  549. test_auth_key_filename_is_valid(void *arg)
  550. {
  551. (void) arg;
  552. /* Valid file name. */
  553. tt_assert(auth_key_filename_is_valid("a.auth_private"));
  554. /* Valid file name with special character. */
  555. tt_assert(auth_key_filename_is_valid("a-.auth_private"));
  556. /* Invalid extension. */
  557. tt_assert(!auth_key_filename_is_valid("a.ath_private"));
  558. /* Nothing before the extension. */
  559. tt_assert(!auth_key_filename_is_valid(".auth_private"));
  560. done:
  561. ;
  562. }
  563. static void
  564. test_parse_auth_file_content(void *arg)
  565. {
  566. hs_client_service_authorization_t *auth = NULL;
  567. (void) arg;
  568. /* Valid authorized client. */
  569. auth = parse_auth_file_content(
  570. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad:descriptor:"
  571. "x25519:zdsyvn2jq534ugyiuzgjy4267jbtzcjbsgedhshzx5mforyxtryq");
  572. tt_assert(auth);
  573. /* Wrong number of fields. */
  574. tt_assert(!parse_auth_file_content("a:b"));
  575. /* Wrong auth type. */
  576. tt_assert(!parse_auth_file_content(
  577. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad:x:"
  578. "x25519:zdsyvn2jq534ugyiuzgjy4267jbtzcjbsgedhshzx5mforyxtryq"));
  579. /* Wrong key type. */
  580. tt_assert(!parse_auth_file_content(
  581. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad:descriptor:"
  582. "x:zdsyvn2jq534ugyiuzgjy4267jbtzcjbsgedhshzx5mforyxtryq"));
  583. /* Some malformed string. */
  584. tt_assert(!parse_auth_file_content("xx:descriptor:x25519:aa=="));
  585. /* Bigger key than it should be */
  586. tt_assert(!parse_auth_file_content("xx:descriptor:x25519:"
  587. "vjqea4jbhwwc4hto7ekyvqfbeodghbaq6nxi45hz4wr3qvhqv3yqa"));
  588. done:
  589. tor_free(auth);
  590. }
  591. static char *
  592. mock_read_file_to_str(const char *filename, int flags, struct stat *stat_out)
  593. {
  594. char *ret = NULL;
  595. (void) flags;
  596. (void) stat_out;
  597. if (!strcmp(filename, get_fname("auth_keys" PATH_SEPARATOR
  598. "client1.auth_private"))) {
  599. ret = tor_strdup(
  600. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad:descriptor:"
  601. "x25519:zdsyvn2jq534ugyiuzgjy4267jbtzcjbsgedhshzx5mforyxtryq");
  602. goto done;
  603. }
  604. if (!strcmp(filename, get_fname("auth_keys" PATH_SEPARATOR "dummy.xxx"))) {
  605. ret = tor_strdup(
  606. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:descriptor:"
  607. "x25519:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  608. goto done;
  609. }
  610. if (!strcmp(filename, get_fname("auth_keys" PATH_SEPARATOR
  611. "client2.auth_private"))) {
  612. ret = tor_strdup(
  613. "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid:descriptor:"
  614. "x25519:fdreqzjqso7d2ac7qscrxfl5qfpamdvgy5d6cxejcgzc3hvhurmq");
  615. goto done;
  616. }
  617. done:
  618. return ret;
  619. }
  620. static int
  621. mock_check_private_dir(const char *dirname, cpd_check_t check,
  622. const char *effective_user)
  623. {
  624. (void) dirname;
  625. (void) check;
  626. (void) effective_user;
  627. return 0;
  628. }
  629. static smartlist_t *
  630. mock_tor_listdir(const char *dirname)
  631. {
  632. smartlist_t *file_list = smartlist_new();
  633. (void) dirname;
  634. smartlist_add(file_list, tor_strdup("client1.auth_private"));
  635. smartlist_add(file_list, tor_strdup("dummy.xxx"));
  636. smartlist_add(file_list, tor_strdup("client2.auth_private"));
  637. return file_list;
  638. }
  639. static void
  640. test_config_client_authorization(void *arg)
  641. {
  642. int ret;
  643. char *conf = NULL;
  644. ed25519_public_key_t pk1, pk2;
  645. digest256map_t *global_map = NULL;
  646. char *key_dir = tor_strdup(get_fname("auth_keys"));
  647. (void) arg;
  648. MOCK(read_file_to_str, mock_read_file_to_str);
  649. MOCK(tor_listdir, mock_tor_listdir);
  650. MOCK(check_private_dir, mock_check_private_dir);
  651. #define conf_fmt \
  652. "ClientOnionAuthDir %s\n"
  653. tor_asprintf(&conf, conf_fmt, key_dir);
  654. ret = helper_config_client(conf, 0);
  655. tor_free(conf);
  656. tt_int_op(ret, OP_EQ, 0);
  657. #undef conf_fmt
  658. global_map = get_hs_client_auths_map();
  659. tt_int_op(digest256map_size(global_map), OP_EQ, 2);
  660. hs_parse_address("4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad",
  661. &pk1, NULL, NULL);
  662. hs_parse_address("25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid",
  663. &pk2, NULL, NULL);
  664. tt_assert(digest256map_get(global_map, pk1.pubkey));
  665. tt_assert(digest256map_get(global_map, pk2.pubkey));
  666. done:
  667. tor_free(key_dir);
  668. hs_free_all();
  669. UNMOCK(read_file_to_str);
  670. UNMOCK(tor_listdir);
  671. UNMOCK(check_private_dir);
  672. }
  673. static entry_connection_t *
  674. helper_build_socks_connection(const ed25519_public_key_t *service_pk,
  675. int conn_state)
  676. {
  677. entry_connection_t *socks = entry_connection_new(CONN_TYPE_AP, AF_INET);
  678. ENTRY_TO_EDGE_CONN(socks)->hs_ident = hs_ident_edge_conn_new(service_pk);
  679. TO_CONN(ENTRY_TO_EDGE_CONN(socks))->state = conn_state;
  680. smartlist_add(get_connection_array(), &socks->edge_.base_);
  681. return socks;
  682. }
  683. static void
  684. test_desc_has_arrived_cleanup(void *arg)
  685. {
  686. /* The goal of this test is to make sure we clean up everything in between
  687. * two descriptors from the same .onion. Because intro points can change
  688. * from one descriptor to another, once we received a new descriptor, we
  689. * need to cleanup the remaining circuits so they aren't used or selected
  690. * when establishing a connection with the newly stored descriptor.
  691. *
  692. * This test was created because of #27410. */
  693. int ret;
  694. char *desc_str = NULL;
  695. hs_descriptor_t *desc = NULL;
  696. const hs_descriptor_t *cached_desc;
  697. ed25519_keypair_t signing_kp;
  698. entry_connection_t *socks1 = NULL, *socks2 = NULL;
  699. hs_ident_dir_conn_t hs_dir_ident;
  700. (void) arg;
  701. hs_init();
  702. MOCK(networkstatus_get_live_consensus,
  703. mock_networkstatus_get_live_consensus);
  704. MOCK(connection_mark_unattached_ap_,
  705. mock_connection_mark_unattached_ap_);
  706. MOCK(router_have_minimum_dir_info,
  707. mock_router_have_minimum_dir_info_true);
  708. /* Set consensus time before our time so the cache lookup can always
  709. * validate that the entry is not expired. */
  710. parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC", &mock_ns.valid_after);
  711. parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC", &mock_ns.fresh_until);
  712. parse_rfc1123_time("Sat, 26 Oct 1985 16:00:00 UTC", &mock_ns.valid_until);
  713. /* Build a descriptor for a specific .onion. */
  714. ret = ed25519_keypair_generate(&signing_kp, 0);
  715. tt_int_op(ret, OP_EQ, 0);
  716. desc = hs_helper_build_hs_desc_with_ip(&signing_kp);
  717. tt_assert(desc);
  718. ret = hs_desc_encode_descriptor(desc, &signing_kp, NULL, &desc_str);
  719. tt_int_op(ret, OP_EQ, 0);
  720. /* Store in the client cache. */
  721. ret = hs_cache_store_as_client(desc_str, &signing_kp.pubkey);
  722. tt_int_op(ret, OP_EQ, 0);
  723. cached_desc = hs_cache_lookup_as_client(&signing_kp.pubkey);
  724. tt_assert(cached_desc);
  725. hs_helper_desc_equal(desc, cached_desc);
  726. /* Create two SOCKS connection for the same .onion both in the waiting for a
  727. * descriptor state. */
  728. socks1 = helper_build_socks_connection(&signing_kp.pubkey,
  729. AP_CONN_STATE_RENDDESC_WAIT);
  730. tt_assert(socks1);
  731. socks2 = helper_build_socks_connection(&signing_kp.pubkey,
  732. AP_CONN_STATE_RENDDESC_WAIT);
  733. tt_assert(socks2);
  734. /* Now, we'll make the intro points in the current descriptor unusable so
  735. * the hs_client_desc_has_arrived() will take the right code path that we
  736. * want to test that is the fetched descriptor has bad intro points. */
  737. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  738. hs_desc_intro_point_t *, ip) {
  739. hs_cache_client_intro_state_note(&signing_kp.pubkey,
  740. &ip->auth_key_cert->signed_key,
  741. INTRO_POINT_FAILURE_GENERIC);
  742. } SMARTLIST_FOREACH_END(ip);
  743. /* Simulate that a new descriptor just arrived. We should have both of our
  744. * SOCKS connection to be ended with a resolved failed. */
  745. hs_ident_dir_conn_init(&signing_kp.pubkey,
  746. &desc->plaintext_data.blinded_pubkey, &hs_dir_ident);
  747. hs_client_desc_has_arrived(&hs_dir_ident);
  748. tt_int_op(socks1->edge_.end_reason, OP_EQ, END_STREAM_REASON_RESOLVEFAILED);
  749. /* XXX: MUST work with OP_EQ. */
  750. tt_int_op(socks2->edge_.end_reason, OP_EQ, END_STREAM_REASON_RESOLVEFAILED);
  751. /* Now let say tor cleans up the intro state cache which resets all intro
  752. * point failure count. */
  753. hs_cache_client_intro_state_purge();
  754. /* Retrying all SOCKS which should basically do nothing since we don't have
  755. * any pending SOCKS connection in AP_CONN_STATE_RENDDESC_WAIT state. */
  756. /* XXX: BUG() is triggered here, shouldn't if socks2 wasn't alive. */
  757. retry_all_socks_conn_waiting_for_desc();
  758. done:
  759. connection_free_minimal(ENTRY_TO_CONN(socks1));
  760. connection_free_minimal(ENTRY_TO_CONN(socks2));
  761. hs_descriptor_free(desc);
  762. tor_free(desc_str);
  763. hs_free_all();
  764. UNMOCK(networkstatus_get_live_consensus);
  765. UNMOCK(connection_mark_unattached_ap_);
  766. UNMOCK(router_have_minimum_dir_info);
  767. }
  768. static void
  769. test_close_intro_circuits_new_desc(void *arg)
  770. {
  771. int ret;
  772. ed25519_keypair_t service_kp;
  773. circuit_t *circ = NULL;
  774. origin_circuit_t *ocirc = NULL;
  775. hs_descriptor_t *desc1 = NULL, *desc2 = NULL;
  776. (void) arg;
  777. hs_init();
  778. /* This is needed because of the client cache expiration timestamp is based
  779. * on having a consensus. See cached_client_descriptor_has_expired(). */
  780. MOCK(networkstatus_get_live_consensus,
  781. mock_networkstatus_get_live_consensus);
  782. /* Set consensus time */
  783. parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  784. &mock_ns.valid_after);
  785. parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  786. &mock_ns.fresh_until);
  787. parse_rfc1123_time("Sat, 26 Oct 1985 16:00:00 UTC",
  788. &mock_ns.valid_until);
  789. /* Generate service keypair */
  790. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&service_kp, 0));
  791. /* Create and add to the global list a dummy client introduction circuits.
  792. * We'll then make sure the hs_ident is attached to a dummy descriptor. */
  793. circ = dummy_origin_circuit_new(0);
  794. tt_assert(circ);
  795. circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  796. ocirc = TO_ORIGIN_CIRCUIT(circ);
  797. /* Build the first descriptor and cache it. */
  798. {
  799. char *encoded;
  800. desc1 = hs_helper_build_hs_desc_with_ip(&service_kp);
  801. tt_assert(desc1);
  802. ret = hs_desc_encode_descriptor(desc1, &service_kp, NULL, &encoded);
  803. tt_int_op(ret, OP_EQ, 0);
  804. tt_assert(encoded);
  805. /* Store it */
  806. ret = hs_cache_store_as_client(encoded, &service_kp.pubkey);
  807. tt_int_op(ret, OP_EQ, 0);
  808. tor_free(encoded);
  809. tt_assert(hs_cache_lookup_as_client(&service_kp.pubkey));
  810. }
  811. /* We'll pick one introduction point and associate it with the circuit. */
  812. {
  813. const hs_desc_intro_point_t *ip =
  814. smartlist_get(desc1->encrypted_data.intro_points, 0);
  815. tt_assert(ip);
  816. ocirc->hs_ident = hs_ident_circuit_new(&service_kp.pubkey);
  817. ed25519_pubkey_copy(&ocirc->hs_ident->intro_auth_pk,
  818. &ip->auth_key_cert->signed_key);
  819. }
  820. /* Before we are about to clean up the intro circuits, make sure it is
  821. * actually there. */
  822. tt_assert(circuit_get_next_intro_circ(NULL, true));
  823. /* Build the second descriptor for the same service and cache it. */
  824. {
  825. char *encoded;
  826. desc2 = hs_helper_build_hs_desc_with_ip(&service_kp);
  827. tt_assert(desc2);
  828. tt_mem_op(&desc1->plaintext_data.signing_pubkey, OP_EQ,
  829. &desc2->plaintext_data.signing_pubkey, ED25519_PUBKEY_LEN);
  830. /* To replace the existing descriptor, the revision counter needs to be
  831. * bigger. */
  832. desc2->plaintext_data.revision_counter =
  833. desc1->plaintext_data.revision_counter + 1;
  834. ret = hs_desc_encode_descriptor(desc2, &service_kp, NULL, &encoded);
  835. tt_int_op(ret, OP_EQ, 0);
  836. tt_assert(encoded);
  837. hs_cache_store_as_client(encoded, &service_kp.pubkey);
  838. tt_int_op(ret, OP_EQ, 0);
  839. tor_free(encoded);
  840. tt_assert(hs_cache_lookup_as_client(&service_kp.pubkey));
  841. }
  842. /* Once stored, our intro circuit should be closed because it is related to
  843. * an old introduction point that doesn't exists anymore. */
  844. tt_assert(!circuit_get_next_intro_circ(NULL, true));
  845. done:
  846. circuit_free(circ);
  847. hs_descriptor_free(desc1);
  848. hs_descriptor_free(desc2);
  849. hs_free_all();
  850. UNMOCK(networkstatus_get_live_consensus);
  851. }
  852. static void
  853. test_close_intro_circuits_cache_clean(void *arg)
  854. {
  855. int ret;
  856. ed25519_keypair_t service_kp;
  857. circuit_t *circ = NULL;
  858. origin_circuit_t *ocirc = NULL;
  859. hs_descriptor_t *desc1 = NULL;
  860. (void) arg;
  861. hs_init();
  862. rend_cache_init();
  863. /* This is needed because of the client cache expiration timestamp is based
  864. * on having a consensus. See cached_client_descriptor_has_expired(). */
  865. MOCK(networkstatus_get_live_consensus,
  866. mock_networkstatus_get_live_consensus);
  867. /* Set consensus time */
  868. parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  869. &mock_ns.valid_after);
  870. parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  871. &mock_ns.fresh_until);
  872. parse_rfc1123_time("Sat, 26 Oct 1985 16:00:00 UTC",
  873. &mock_ns.valid_until);
  874. /* Generate service keypair */
  875. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&service_kp, 0));
  876. /* Create and add to the global list a dummy client introduction circuits.
  877. * We'll then make sure the hs_ident is attached to a dummy descriptor. */
  878. circ = dummy_origin_circuit_new(0);
  879. tt_assert(circ);
  880. circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  881. ocirc = TO_ORIGIN_CIRCUIT(circ);
  882. /* Build the first descriptor and cache it. */
  883. {
  884. char *encoded;
  885. desc1 = hs_helper_build_hs_desc_with_ip(&service_kp);
  886. tt_assert(desc1);
  887. ret = hs_desc_encode_descriptor(desc1, &service_kp, NULL, &encoded);
  888. tt_int_op(ret, OP_EQ, 0);
  889. tt_assert(encoded);
  890. /* Store it */
  891. ret = hs_cache_store_as_client(encoded, &service_kp.pubkey);
  892. tt_int_op(ret, OP_EQ, 0);
  893. tor_free(encoded);
  894. tt_assert(hs_cache_lookup_as_client(&service_kp.pubkey));
  895. }
  896. /* We'll pick one introduction point and associate it with the circuit. */
  897. {
  898. const hs_desc_intro_point_t *ip =
  899. smartlist_get(desc1->encrypted_data.intro_points, 0);
  900. tt_assert(ip);
  901. ocirc->hs_ident = hs_ident_circuit_new(&service_kp.pubkey);
  902. ed25519_pubkey_copy(&ocirc->hs_ident->intro_auth_pk,
  903. &ip->auth_key_cert->signed_key);
  904. }
  905. /* Before we are about to clean up the intro circuits, make sure it is
  906. * actually there. */
  907. tt_assert(circuit_get_next_intro_circ(NULL, true));
  908. /* Cleanup the client cache. The ns valid after time is what decides if the
  909. * descriptor has expired so put it in the future enough (72h) so we are
  910. * sure to always expire. */
  911. mock_ns.valid_after = approx_time() + (72 * 24 * 60 * 60);
  912. hs_cache_clean_as_client(0);
  913. /* Once stored, our intro circuit should be closed because it is related to
  914. * an old introduction point that doesn't exists anymore. */
  915. tt_assert(!circuit_get_next_intro_circ(NULL, true));
  916. done:
  917. circuit_free(circ);
  918. hs_descriptor_free(desc1);
  919. hs_free_all();
  920. rend_cache_free_all();
  921. UNMOCK(networkstatus_get_live_consensus);
  922. }
  923. struct testcase_t hs_client_tests[] = {
  924. { "e2e_rend_circuit_setup_legacy", test_e2e_rend_circuit_setup_legacy,
  925. TT_FORK, NULL, NULL },
  926. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup,
  927. TT_FORK, NULL, NULL },
  928. { "client_pick_intro", test_client_pick_intro,
  929. TT_FORK, NULL, NULL },
  930. { "descriptor_fetch", test_descriptor_fetch,
  931. TT_FORK, NULL, NULL },
  932. { "auth_key_filename_is_valid", test_auth_key_filename_is_valid, TT_FORK,
  933. NULL, NULL },
  934. { "parse_auth_file_content", test_parse_auth_file_content, TT_FORK,
  935. NULL, NULL },
  936. { "config_client_authorization", test_config_client_authorization,
  937. TT_FORK, NULL, NULL },
  938. { "desc_has_arrived_cleanup", test_desc_has_arrived_cleanup,
  939. TT_FORK, NULL, NULL },
  940. { "close_intro_circuits_new_desc", test_close_intro_circuits_new_desc,
  941. TT_FORK, NULL, NULL },
  942. { "close_intro_circuits_cache_clean", test_close_intro_circuits_cache_clean,
  943. TT_FORK, NULL, NULL },
  944. END_OF_TESTCASES
  945. };