test_hs_client.c 38 KB

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