test_hs_client.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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(!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. HS_IDENT_CIRCUIT_INTRO);
  818. ed25519_pubkey_copy(&ocirc->hs_ident->intro_auth_pk,
  819. &ip->auth_key_cert->signed_key);
  820. }
  821. /* Before we are about to clean up the intro circuits, make sure it is
  822. * actually there. */
  823. tt_assert(circuit_get_next_intro_circ(NULL, true));
  824. /* Build the second descriptor for the same service and cache it. */
  825. {
  826. char *encoded;
  827. desc2 = hs_helper_build_hs_desc_with_ip(&service_kp);
  828. tt_assert(desc2);
  829. tt_mem_op(&desc1->plaintext_data.signing_pubkey, OP_EQ,
  830. &desc2->plaintext_data.signing_pubkey, ED25519_PUBKEY_LEN);
  831. /* To replace the existing descriptor, the revision counter needs to be
  832. * bigger. */
  833. desc2->plaintext_data.revision_counter =
  834. desc1->plaintext_data.revision_counter + 1;
  835. ret = hs_desc_encode_descriptor(desc2, &service_kp, NULL, &encoded);
  836. tt_int_op(ret, OP_EQ, 0);
  837. tt_assert(encoded);
  838. hs_cache_store_as_client(encoded, &service_kp.pubkey);
  839. tt_int_op(ret, OP_EQ, 0);
  840. tor_free(encoded);
  841. tt_assert(hs_cache_lookup_as_client(&service_kp.pubkey));
  842. }
  843. /* Once stored, our intro circuit should be closed because it is related to
  844. * an old introduction point that doesn't exists anymore. */
  845. tt_assert(!circuit_get_next_intro_circ(NULL, true));
  846. done:
  847. circuit_free(circ);
  848. hs_descriptor_free(desc1);
  849. hs_descriptor_free(desc2);
  850. hs_free_all();
  851. UNMOCK(networkstatus_get_live_consensus);
  852. }
  853. struct testcase_t hs_client_tests[] = {
  854. { "e2e_rend_circuit_setup_legacy", test_e2e_rend_circuit_setup_legacy,
  855. TT_FORK, NULL, NULL },
  856. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup,
  857. TT_FORK, NULL, NULL },
  858. { "client_pick_intro", test_client_pick_intro,
  859. TT_FORK, NULL, NULL },
  860. { "descriptor_fetch", test_descriptor_fetch,
  861. TT_FORK, NULL, NULL },
  862. { "auth_key_filename_is_valid", test_auth_key_filename_is_valid, TT_FORK,
  863. NULL, NULL },
  864. { "parse_auth_file_content", test_parse_auth_file_content, TT_FORK,
  865. NULL, NULL },
  866. { "config_client_authorization", test_config_client_authorization,
  867. TT_FORK, NULL, NULL },
  868. { "desc_has_arrived_cleanup", test_desc_has_arrived_cleanup,
  869. TT_FORK, NULL, NULL },
  870. { "close_intro_circuits_new_desc", test_close_intro_circuits_new_desc,
  871. TT_FORK, NULL, NULL },
  872. END_OF_TESTCASES
  873. };