test_hs_service.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221
  1. /* Copyright (c) 2016-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_service.c
  5. * \brief Test hidden service functionality.
  6. */
  7. #define CIRCUITBUILD_PRIVATE
  8. #define CIRCUITLIST_PRIVATE
  9. #define CONFIG_PRIVATE
  10. #define CONNECTION_PRIVATE
  11. #define CONNECTION_EDGE_PRIVATE
  12. #define CRYPTO_PRIVATE
  13. #define HS_COMMON_PRIVATE
  14. #define HS_SERVICE_PRIVATE
  15. #define HS_INTROPOINT_PRIVATE
  16. #define HS_CIRCUIT_PRIVATE
  17. #define MAINLOOP_PRIVATE
  18. #define NETWORKSTATUS_PRIVATE
  19. #define STATEFILE_PRIVATE
  20. #define TOR_CHANNEL_INTERNAL_
  21. #define HS_CLIENT_PRIVATE
  22. #define CRYPT_PATH_PRIVATE
  23. #include "test/test.h"
  24. #include "test/test_helpers.h"
  25. #include "test/log_test_helpers.h"
  26. #include "test/rend_test_helpers.h"
  27. #include "test/hs_test_helpers.h"
  28. #include "core/or/or.h"
  29. #include "app/config/config.h"
  30. #include "app/config/statefile.h"
  31. #include "core/crypto/hs_ntor.h"
  32. #include "core/mainloop/connection.h"
  33. #include "core/mainloop/mainloop.h"
  34. #include "core/or/circuitbuild.h"
  35. #include "core/or/circuitlist.h"
  36. #include "core/or/circuituse.h"
  37. #include "core/or/connection_edge.h"
  38. #include "core/or/edge_connection_st.h"
  39. #include "core/or/relay.h"
  40. #include "core/or/versions.h"
  41. #include "feature/dirauth/dirvote.h"
  42. #include "feature/dirauth/shared_random_state.h"
  43. #include "feature/dircommon/voting_schedule.h"
  44. #include "feature/hs/hs_circuit.h"
  45. #include "feature/hs/hs_circuitmap.h"
  46. #include "feature/hs/hs_client.h"
  47. #include "feature/hs/hs_common.h"
  48. #include "feature/hs/hs_config.h"
  49. #include "feature/hs/hs_ident.h"
  50. #include "feature/hs/hs_intropoint.h"
  51. #include "feature/hs/hs_service.h"
  52. #include "feature/nodelist/networkstatus.h"
  53. #include "feature/nodelist/nodelist.h"
  54. #include "feature/rend/rendservice.h"
  55. #include "lib/crypt_ops/crypto_rand.h"
  56. #include "lib/fs/dir.h"
  57. #include "core/or/cpath_build_state_st.h"
  58. #include "core/or/crypt_path_st.h"
  59. #include "core/or/crypt_path.h"
  60. #include "feature/nodelist/networkstatus_st.h"
  61. #include "feature/nodelist/node_st.h"
  62. #include "core/or/origin_circuit_st.h"
  63. #include "app/config/or_state_st.h"
  64. #include "feature/nodelist/routerinfo_st.h"
  65. /* Trunnel */
  66. #include "trunnel/hs/cell_establish_intro.h"
  67. #ifdef HAVE_SYS_STAT_H
  68. #include <sys/stat.h>
  69. #endif
  70. #ifdef HAVE_UNISTD_H
  71. #include <unistd.h>
  72. #endif
  73. static networkstatus_t mock_ns;
  74. static networkstatus_t *
  75. mock_networkstatus_get_live_consensus(time_t now)
  76. {
  77. (void) now;
  78. return &mock_ns;
  79. }
  80. static or_state_t *dummy_state = NULL;
  81. /* Mock function to get fake or state (used for rev counters) */
  82. static or_state_t *
  83. get_or_state_replacement(void)
  84. {
  85. return dummy_state;
  86. }
  87. /* Mock function because we are not trying to test the close circuit that does
  88. * an awful lot of checks on the circuit object. */
  89. static void
  90. mock_circuit_mark_for_close(circuit_t *circ, int reason, int line,
  91. const char *file)
  92. {
  93. (void) circ;
  94. (void) reason;
  95. (void) line;
  96. (void) file;
  97. return;
  98. }
  99. static int
  100. mock_relay_send_command_from_edge(streamid_t stream_id, circuit_t *circ,
  101. uint8_t relay_command, const char *payload,
  102. size_t payload_len,
  103. crypt_path_t *cpath_layer,
  104. const char *filename, int lineno)
  105. {
  106. (void) stream_id;
  107. (void) circ;
  108. (void) relay_command;
  109. (void) payload;
  110. (void) payload_len;
  111. (void) cpath_layer;
  112. (void) filename;
  113. (void) lineno;
  114. return 0;
  115. }
  116. /* Helper: from a set of options in conf, configure a service which will add
  117. * it to the staging list of the HS subsytem. */
  118. static int
  119. helper_config_service(const char *conf)
  120. {
  121. int ret = 0;
  122. or_options_t *options = NULL;
  123. tt_assert(conf);
  124. options = helper_parse_options(conf);
  125. tt_assert(options);
  126. ret = hs_config_service_all(options, 0);
  127. done:
  128. or_options_free(options);
  129. return ret;
  130. }
  131. /* Test: Ensure that setting up rendezvous circuits works correctly. */
  132. static void
  133. test_e2e_rend_circuit_setup(void *arg)
  134. {
  135. ed25519_public_key_t service_pk;
  136. origin_circuit_t *or_circ;
  137. int retval;
  138. /** In this test we create a v3 prop224 service-side rendezvous circuit.
  139. * We simulate an HS ntor key exchange with a client, and check that
  140. * the circuit was setup correctly and is ready to accept rendezvous data */
  141. (void) arg;
  142. /* Now make dummy circuit */
  143. {
  144. or_circ = origin_circuit_new();
  145. or_circ->base_.purpose = CIRCUIT_PURPOSE_S_CONNECT_REND;
  146. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  147. or_circ->build_state->is_internal = 1;
  148. /* prop224: Setup hs conn identifier on the stream */
  149. ed25519_secret_key_t sk;
  150. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
  151. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
  152. or_circ->hs_ident = hs_ident_circuit_new(&service_pk);
  153. TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
  154. }
  155. /* Check number of hops */
  156. retval = cpath_get_n_hops(&or_circ->cpath);
  157. tt_int_op(retval, OP_EQ, 0);
  158. /* Setup the circuit: do the ntor key exchange */
  159. {
  160. uint8_t ntor_key_seed[DIGEST256_LEN] = {2};
  161. retval = hs_circuit_setup_e2e_rend_circ(or_circ,
  162. ntor_key_seed, sizeof(ntor_key_seed),
  163. 1);
  164. tt_int_op(retval, OP_EQ, 0);
  165. }
  166. /* See that a hop was added to the circuit's cpath */
  167. retval = cpath_get_n_hops(&or_circ->cpath);
  168. tt_int_op(retval, OP_EQ, 1);
  169. /* Check the digest algo */
  170. tt_int_op(
  171. crypto_digest_get_algorithm(or_circ->cpath->pvt_crypto.f_digest),
  172. OP_EQ, DIGEST_SHA3_256);
  173. tt_int_op(
  174. crypto_digest_get_algorithm(or_circ->cpath->pvt_crypto.b_digest),
  175. OP_EQ, DIGEST_SHA3_256);
  176. tt_assert(or_circ->cpath->pvt_crypto.f_crypto);
  177. tt_assert(or_circ->cpath->pvt_crypto.b_crypto);
  178. /* Ensure that circ purpose was changed */
  179. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  180. done:
  181. circuit_free_(TO_CIRCUIT(or_circ));
  182. }
  183. /* Helper: Return a newly allocated and initialized origin circuit with
  184. * purpose and flags. A default HS identifier is set to an ed25519
  185. * authentication key for introduction point. */
  186. static origin_circuit_t *
  187. helper_create_origin_circuit(int purpose, int flags)
  188. {
  189. origin_circuit_t *circ = NULL;
  190. circ = origin_circuit_init(purpose, flags);
  191. tor_assert(circ);
  192. circ->cpath = tor_malloc_zero(sizeof(crypt_path_t));
  193. circ->cpath->magic = CRYPT_PATH_MAGIC;
  194. circ->cpath->state = CPATH_STATE_OPEN;
  195. circ->cpath->package_window = circuit_initial_package_window();
  196. circ->cpath->deliver_window = CIRCWINDOW_START;
  197. circ->cpath->prev = circ->cpath;
  198. /* Random nonce. */
  199. crypto_rand(circ->cpath->prev->rend_circ_nonce, DIGEST_LEN);
  200. /* Create a default HS identifier. */
  201. circ->hs_ident = tor_malloc_zero(sizeof(hs_ident_circuit_t));
  202. return circ;
  203. }
  204. /* Helper: Return a newly allocated authorized client object with
  205. * and a newly generated public key. */
  206. static hs_service_authorized_client_t *
  207. helper_create_authorized_client(void)
  208. {
  209. int ret;
  210. hs_service_authorized_client_t *client;
  211. curve25519_secret_key_t seckey;
  212. client = tor_malloc_zero(sizeof(hs_service_authorized_client_t));
  213. ret = curve25519_secret_key_generate(&seckey, 0);
  214. tt_int_op(ret, OP_EQ, 0);
  215. curve25519_public_key_generate(&client->client_pk, &seckey);
  216. done:
  217. return client;
  218. }
  219. /* Helper: Return a newly allocated authorized client object with the
  220. * same client name and the same public key as the given client. */
  221. static hs_service_authorized_client_t *
  222. helper_clone_authorized_client(const hs_service_authorized_client_t *client)
  223. {
  224. hs_service_authorized_client_t *client_out;
  225. tor_assert(client);
  226. client_out = tor_malloc_zero(sizeof(hs_service_authorized_client_t));
  227. memcpy(client_out->client_pk.public_key,
  228. client->client_pk.public_key, CURVE25519_PUBKEY_LEN);
  229. return client_out;
  230. }
  231. /* Helper: Return a newly allocated service object with the identity keypair
  232. * sets and the current descriptor. Then register it to the global map.
  233. * Caller should use hs_free_all() to free this service or remove it from the
  234. * global map before freeing. */
  235. static hs_service_t *
  236. helper_create_service(void)
  237. {
  238. /* Set a service for this circuit. */
  239. hs_service_t *service = hs_service_new(get_options());
  240. tor_assert(service);
  241. service->config.version = HS_VERSION_THREE;
  242. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  243. ed25519_public_key_generate(&service->keys.identity_pk,
  244. &service->keys.identity_sk);
  245. service->desc_current = service_descriptor_new();
  246. tt_assert(service->desc_current);
  247. /* Register service to global map. */
  248. int ret = register_service(get_hs_service_map(), service);
  249. tt_int_op(ret, OP_EQ, 0);
  250. done:
  251. return service;
  252. }
  253. /* Helper: Deallocate a given service object, its child objects and
  254. * remove it from onion service map.
  255. * */
  256. static void
  257. helper_destroy_service(hs_service_t *service)
  258. {
  259. if (!service)
  260. return;
  261. remove_service(get_hs_service_map(), service);
  262. hs_service_free(service);
  263. }
  264. /* Helper: Return a newly allocated service object with clients. */
  265. static hs_service_t *
  266. helper_create_service_with_clients(int num_clients)
  267. {
  268. int i;
  269. hs_service_t *service = helper_create_service();
  270. tt_assert(service);
  271. service->config.is_client_auth_enabled = 1;
  272. service->config.clients = smartlist_new();
  273. for (i = 0; i < num_clients; i++) {
  274. hs_service_authorized_client_t *client;
  275. client = helper_create_authorized_client();
  276. smartlist_add(service->config.clients, client);
  277. }
  278. done:
  279. return service;
  280. }
  281. /* Helper: Return a newly allocated service intro point with two link
  282. * specifiers, one IPv4 and one legacy ID set to As. */
  283. static hs_service_intro_point_t *
  284. helper_create_service_ip(void)
  285. {
  286. link_specifier_t *ls;
  287. hs_service_intro_point_t *ip = service_intro_point_new(NULL);
  288. tor_assert(ip);
  289. /* Add a first unused link specifier. */
  290. ls = link_specifier_new();
  291. link_specifier_set_ls_type(ls, LS_IPV4);
  292. smartlist_add(ip->base.link_specifiers, ls);
  293. /* Add a second link specifier used by a test. */
  294. ls = link_specifier_new();
  295. link_specifier_set_ls_type(ls, LS_LEGACY_ID);
  296. memset(link_specifier_getarray_un_legacy_id(ls), 'A',
  297. link_specifier_getlen_un_legacy_id(ls));
  298. smartlist_add(ip->base.link_specifiers, ls);
  299. return ip;
  300. }
  301. static void
  302. test_load_keys(void *arg)
  303. {
  304. int ret;
  305. char *conf = NULL;
  306. char *hsdir_v2 = tor_strdup(get_fname("hs2"));
  307. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  308. char addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  309. (void) arg;
  310. /* We'll register two services, a v2 and a v3, then we'll load keys and
  311. * validate that both are in a correct state. */
  312. hs_init();
  313. #define conf_fmt \
  314. "HiddenServiceDir %s\n" \
  315. "HiddenServiceVersion %d\n" \
  316. "HiddenServicePort 65535\n"
  317. /* v2 service. */
  318. tor_asprintf(&conf, conf_fmt, hsdir_v2, HS_VERSION_TWO);
  319. ret = helper_config_service(conf);
  320. tor_free(conf);
  321. tt_int_op(ret, OP_EQ, 0);
  322. /* This one should now be registered into the v2 list. */
  323. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 0);
  324. tt_int_op(rend_num_services(), OP_EQ, 1);
  325. /* v3 service. */
  326. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  327. ret = helper_config_service(conf);
  328. tor_free(conf);
  329. tt_int_op(ret, OP_EQ, 0);
  330. /* It's in staging? */
  331. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  332. #undef conf_fmt
  333. /* Load the keys for these. After that, the v3 service should be registered
  334. * in the global map. */
  335. hs_service_load_all_keys();
  336. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  337. hs_service_t *s = get_first_service();
  338. tt_assert(s);
  339. /* Ok we have the service object. Validate few things. */
  340. tt_assert(!fast_mem_is_zero(s->onion_address, sizeof(s->onion_address)));
  341. tt_int_op(hs_address_is_valid(s->onion_address), OP_EQ, 1);
  342. tt_assert(!fast_mem_is_zero((char *) s->keys.identity_sk.seckey,
  343. ED25519_SECKEY_LEN));
  344. tt_assert(!fast_mem_is_zero((char *) s->keys.identity_pk.pubkey,
  345. ED25519_PUBKEY_LEN));
  346. /* Check onion address from identity key. */
  347. hs_build_address(&s->keys.identity_pk, s->config.version, addr);
  348. tt_int_op(hs_address_is_valid(addr), OP_EQ, 1);
  349. tt_str_op(addr, OP_EQ, s->onion_address);
  350. /* Check that the is_client_auth_enabled is not set. */
  351. tt_assert(!s->config.is_client_auth_enabled);
  352. done:
  353. tor_free(hsdir_v2);
  354. tor_free(hsdir_v3);
  355. hs_free_all();
  356. }
  357. static void
  358. test_client_filename_is_valid(void *arg)
  359. {
  360. (void) arg;
  361. /* Valid file name. */
  362. tt_assert(client_filename_is_valid("a.auth"));
  363. /* Valid file name with special character. */
  364. tt_assert(client_filename_is_valid("a-.auth"));
  365. /* Invalid extension. */
  366. tt_assert(!client_filename_is_valid("a.ath"));
  367. /* Nothing before the extension. */
  368. tt_assert(!client_filename_is_valid(".auth"));
  369. done:
  370. ;
  371. }
  372. static void
  373. test_parse_authorized_client(void *arg)
  374. {
  375. hs_service_authorized_client_t *client = NULL;
  376. (void) arg;
  377. /* Valid authorized client. */
  378. client = parse_authorized_client(
  379. "descriptor:x25519:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja");
  380. tt_assert(client);
  381. /* Wrong number of fields. */
  382. tt_assert(!parse_authorized_client("a:b:c:d:e"));
  383. /* Wrong auth type. */
  384. tt_assert(!parse_authorized_client(
  385. "x:x25519:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  386. /* Wrong key type. */
  387. tt_assert(!parse_authorized_client(
  388. "descriptor:x:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  389. /* Some malformed string. */
  390. tt_assert(!parse_authorized_client("descriptor:x25519:aa=="));
  391. tt_assert(!parse_authorized_client("descriptor:"));
  392. tt_assert(!parse_authorized_client("descriptor:x25519"));
  393. tt_assert(!parse_authorized_client("descriptor:x25519:"));
  394. tt_assert(!parse_authorized_client(""));
  395. done:
  396. service_authorized_client_free(client);
  397. }
  398. static char *
  399. mock_read_file_to_str(const char *filename, int flags, struct stat *stat_out)
  400. {
  401. char *ret = NULL;
  402. (void) flags;
  403. (void) stat_out;
  404. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  405. "authorized_clients" PATH_SEPARATOR
  406. "client1.auth"))) {
  407. ret = tor_strdup("descriptor:x25519:"
  408. "dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja");
  409. goto done;
  410. }
  411. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  412. "authorized_clients" PATH_SEPARATOR
  413. "dummy.xxx"))) {
  414. ret = tor_strdup("descriptor:x25519:"
  415. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  416. goto done;
  417. }
  418. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  419. "authorized_clients" PATH_SEPARATOR
  420. "client2.auth"))) {
  421. ret = tor_strdup("descriptor:x25519:"
  422. "okoi2gml3wd6x7jganlk5d66xxyjgg24sxw4y7javx4giqr66zta");
  423. goto done;
  424. }
  425. done:
  426. return ret;
  427. }
  428. static smartlist_t *
  429. mock_tor_listdir(const char *dirname)
  430. {
  431. smartlist_t *file_list = smartlist_new();
  432. (void) dirname;
  433. smartlist_add(file_list, tor_strdup("client1.auth"));
  434. smartlist_add(file_list, tor_strdup("dummy.xxx"));
  435. smartlist_add(file_list, tor_strdup("client2.auth"));
  436. return file_list;
  437. }
  438. static void
  439. test_load_keys_with_client_auth(void *arg)
  440. {
  441. int ret;
  442. char *conf = NULL;
  443. smartlist_t *pubkey_b32_list = smartlist_new();
  444. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  445. hs_service_t *service;
  446. (void) arg;
  447. hs_init();
  448. smartlist_add(pubkey_b32_list, tor_strdup(
  449. "dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  450. smartlist_add(pubkey_b32_list, tor_strdup(
  451. "okoi2gml3wd6x7jganlk5d66xxyjgg24sxw4y7javx4giqr66zta"));
  452. #define conf_fmt \
  453. "HiddenServiceDir %s\n" \
  454. "HiddenServiceVersion %d\n" \
  455. "HiddenServicePort 65534\n"
  456. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  457. ret = helper_config_service(conf);
  458. tor_free(conf);
  459. tt_int_op(ret, OP_EQ, 0);
  460. /* It's in staging? */
  461. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  462. #undef conf_fmt
  463. MOCK(read_file_to_str, mock_read_file_to_str);
  464. MOCK(tor_listdir, mock_tor_listdir);
  465. /* Load the keys for these. After that, the v3 service should be registered
  466. * in the global map. */
  467. hs_service_load_all_keys();
  468. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  469. service = get_first_service();
  470. tt_assert(service);
  471. tt_assert(service->config.clients);
  472. tt_int_op(smartlist_len(service->config.clients), OP_EQ,
  473. smartlist_len(pubkey_b32_list));
  474. /* Test that the is_client_auth_enabled flag is set. */
  475. tt_assert(service->config.is_client_auth_enabled);
  476. /* Test that the keys in clients are correct. */
  477. SMARTLIST_FOREACH_BEGIN(pubkey_b32_list, char *, pubkey_b32) {
  478. curve25519_public_key_t pubkey;
  479. /* This flag will be set if the key is found in clients. */
  480. int is_found = 0;
  481. base32_decode((char *) pubkey.public_key, sizeof(pubkey.public_key),
  482. pubkey_b32, strlen(pubkey_b32));
  483. SMARTLIST_FOREACH_BEGIN(service->config.clients,
  484. hs_service_authorized_client_t *, client) {
  485. if (tor_memeq(&pubkey, &client->client_pk, sizeof(pubkey))) {
  486. is_found = 1;
  487. break;
  488. }
  489. } SMARTLIST_FOREACH_END(client);
  490. tt_assert(is_found);
  491. } SMARTLIST_FOREACH_END(pubkey_b32);
  492. done:
  493. SMARTLIST_FOREACH(pubkey_b32_list, char *, s, tor_free(s));
  494. smartlist_free(pubkey_b32_list);
  495. tor_free(hsdir_v3);
  496. hs_free_all();
  497. UNMOCK(read_file_to_str);
  498. UNMOCK(tor_listdir);
  499. }
  500. static void
  501. test_access_service(void *arg)
  502. {
  503. int ret;
  504. char *conf = NULL;
  505. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  506. hs_service_ht *global_map;
  507. hs_service_t *s = NULL;
  508. (void) arg;
  509. /* We'll register two services, a v2 and a v3, then we'll load keys and
  510. * validate that both are in a correct state. */
  511. hs_init();
  512. #define conf_fmt \
  513. "HiddenServiceDir %s\n" \
  514. "HiddenServiceVersion %d\n" \
  515. "HiddenServicePort 65535\n"
  516. /* v3 service. */
  517. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  518. ret = helper_config_service(conf);
  519. tor_free(conf);
  520. tt_int_op(ret, OP_EQ, 0);
  521. /* It's in staging? */
  522. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  523. /* Load the keys for these. After that, the v3 service should be registered
  524. * in the global map. */
  525. hs_service_load_all_keys();
  526. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  527. s = get_first_service();
  528. tt_assert(s);
  529. global_map = get_hs_service_map();
  530. tt_assert(global_map);
  531. /* From here, we'll try the service accessors. */
  532. hs_service_t *query = find_service(global_map, &s->keys.identity_pk);
  533. tt_assert(query);
  534. tt_mem_op(query, OP_EQ, s, sizeof(hs_service_t));
  535. /* Remove service, check if it actually works and then put it back. */
  536. remove_service(global_map, s);
  537. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  538. query = find_service(global_map, &s->keys.identity_pk);
  539. tt_ptr_op(query, OP_EQ, NULL);
  540. /* Register back the service in the map. */
  541. ret = register_service(global_map, s);
  542. tt_int_op(ret, OP_EQ, 0);
  543. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  544. /* Twice should fail. */
  545. ret = register_service(global_map, s);
  546. tt_int_op(ret, OP_EQ, -1);
  547. /* Remove service from map so we don't double free on cleanup. */
  548. remove_service(global_map, s);
  549. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  550. query = find_service(global_map, &s->keys.identity_pk);
  551. tt_ptr_op(query, OP_EQ, NULL);
  552. /* Let's try to remove twice for fun. */
  553. setup_full_capture_of_logs(LOG_WARN);
  554. remove_service(global_map, s);
  555. expect_log_msg_containing("Could not find service in the global map");
  556. teardown_capture_of_logs();
  557. done:
  558. hs_service_free(s);
  559. tor_free(hsdir_v3);
  560. hs_free_all();
  561. }
  562. /** Test that we can create intro point objects, index them and find them */
  563. static void
  564. test_service_intro_point(void *arg)
  565. {
  566. hs_service_t *service = NULL;
  567. hs_service_intro_point_t *ip = NULL;
  568. (void) arg;
  569. /* Test simple creation of an object. */
  570. {
  571. time_t now = time(NULL);
  572. ip = helper_create_service_ip();
  573. tt_assert(ip);
  574. /* Make sure the authentication keypair is not zeroes. */
  575. tt_int_op(fast_mem_is_zero((const char *) &ip->auth_key_kp,
  576. sizeof(ed25519_keypair_t)), OP_EQ, 0);
  577. /* The introduce2_max MUST be in that range. */
  578. tt_u64_op(ip->introduce2_max, OP_GE,
  579. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  580. tt_u64_op(ip->introduce2_max, OP_LE,
  581. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  582. /* Time to expire MUST also be in that range. We subtract 500 seconds
  583. * because there could be a gap between setting now and the time taken in
  584. * service_intro_point_new. On ARM and other older CPUs, it can be
  585. * surprisingly slow... */
  586. tt_u64_op(ip->time_to_expire, OP_GE,
  587. now + INTRO_POINT_LIFETIME_MIN_SECONDS - 500);
  588. /* We add 500 seconds, because this time we're testing against the
  589. * maximum allowed time. */
  590. tt_u64_op(ip->time_to_expire, OP_LE,
  591. now + INTRO_POINT_LIFETIME_MAX_SECONDS + 500);
  592. tt_assert(ip->replay_cache);
  593. tt_assert(ip->base.link_specifiers);
  594. /* By default, this is NOT a legacy object. */
  595. tt_int_op(ip->base.is_only_legacy, OP_EQ, 0);
  596. }
  597. /* Test functions that uses a service intropoints map with that previously
  598. * created object (non legacy). */
  599. {
  600. ed25519_public_key_t garbage = { {0} };
  601. hs_service_intro_point_t *query;
  602. service = hs_service_new(get_options());
  603. tt_assert(service);
  604. service->desc_current = service_descriptor_new();
  605. tt_assert(service->desc_current);
  606. /* Add intropoint to descriptor map. */
  607. service_intro_point_add(service->desc_current->intro_points.map, ip);
  608. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  609. tt_mem_op(query, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  610. query = service_intro_point_find(service, &garbage);
  611. tt_ptr_op(query, OP_EQ, NULL);
  612. /* While at it, can I find the descriptor with the intro point? */
  613. hs_service_descriptor_t *desc_lookup =
  614. service_desc_find_by_intro(service, ip);
  615. tt_mem_op(service->desc_current, OP_EQ, desc_lookup,
  616. sizeof(hs_service_descriptor_t));
  617. /* Remove object from service descriptor and make sure it is out. */
  618. service_intro_point_remove(service, ip);
  619. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  620. tt_ptr_op(query, OP_EQ, NULL);
  621. }
  622. done:
  623. /* If the test succeed, this object is no longer referenced in the service
  624. * so we can free it without use after free. Else, it might explode because
  625. * it's still in the service descriptor map. */
  626. service_intro_point_free(ip);
  627. hs_service_free(service);
  628. }
  629. static node_t mock_node;
  630. static const node_t *
  631. mock_node_get_by_id(const char *digest)
  632. {
  633. (void) digest;
  634. memset(mock_node.identity, 'A', DIGEST_LEN);
  635. /* Only return the matchin identity of As */
  636. if (!tor_memcmp(mock_node.identity, digest, DIGEST_LEN)) {
  637. return &mock_node;
  638. }
  639. return NULL;
  640. }
  641. static void
  642. test_helper_functions(void *arg)
  643. {
  644. int ret;
  645. hs_service_t *service = NULL;
  646. hs_service_intro_point_t *ip = NULL;
  647. hs_ident_circuit_t ident;
  648. (void) arg;
  649. MOCK(node_get_by_id, mock_node_get_by_id);
  650. hs_service_init();
  651. time_t now = time(NULL);
  652. update_approx_time(now);
  653. service = helper_create_service();
  654. ip = helper_create_service_ip();
  655. /* Immediately add the intro point to the service so the free service at the
  656. * end cleans it as well. */
  657. service_intro_point_add(service->desc_current->intro_points.map, ip);
  658. /* Setup the circuit identifier. */
  659. ed25519_pubkey_copy(&ident.intro_auth_pk, &ip->auth_key_kp.pubkey);
  660. ed25519_pubkey_copy(&ident.identity_pk, &service->keys.identity_pk);
  661. /* Testing get_objects_from_ident(). */
  662. {
  663. hs_service_t *s_lookup = NULL;
  664. hs_service_intro_point_t *ip_lookup = NULL;
  665. hs_service_descriptor_t *desc_lookup = NULL;
  666. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  667. tt_mem_op(s_lookup, OP_EQ, service, sizeof(hs_service_t));
  668. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  669. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  670. sizeof(hs_service_descriptor_t));
  671. /* Reset */
  672. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  673. /* NULL parameter should work. */
  674. get_objects_from_ident(&ident, NULL, &ip_lookup, &desc_lookup);
  675. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  676. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  677. sizeof(hs_service_descriptor_t));
  678. /* Reset. */
  679. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  680. /* Break the ident and we should find nothing. */
  681. memset(&ident, 0, sizeof(ident));
  682. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  683. tt_ptr_op(s_lookup, OP_EQ, NULL);
  684. tt_ptr_op(ip_lookup, OP_EQ, NULL);
  685. tt_ptr_op(desc_lookup, OP_EQ, NULL);
  686. }
  687. /* Testing get_node_from_intro_point() */
  688. {
  689. const node_t *node = get_node_from_intro_point(ip);
  690. tt_ptr_op(node, OP_EQ, &mock_node);
  691. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  692. link_specifier_t *, ls) {
  693. if (link_specifier_get_ls_type(ls) == LS_LEGACY_ID) {
  694. /* Change legacy id in link specifier which is not the mock node. */
  695. memset(link_specifier_getarray_un_legacy_id(ls), 'B',
  696. link_specifier_getlen_un_legacy_id(ls));
  697. }
  698. } SMARTLIST_FOREACH_END(ls);
  699. node = get_node_from_intro_point(ip);
  700. tt_ptr_op(node, OP_EQ, NULL);
  701. }
  702. /* Testing can_service_launch_intro_circuit() */
  703. {
  704. /* Put the start of the retry period back in time, we should be allowed.
  705. * to launch intro circuit. */
  706. service->state.num_intro_circ_launched = 2;
  707. service->state.intro_circ_retry_started_time =
  708. (now - INTRO_CIRC_RETRY_PERIOD - 1);
  709. ret = can_service_launch_intro_circuit(service, now);
  710. tt_int_op(ret, OP_EQ, 1);
  711. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  712. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  713. /* Call it again, we should still be allowed because we are under
  714. * MAX_INTRO_CIRCS_PER_PERIOD which been set to 0 previously. */
  715. ret = can_service_launch_intro_circuit(service, now);
  716. tt_int_op(ret, OP_EQ, 1);
  717. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  718. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  719. /* Too many intro circuit launched means we are not allowed. */
  720. service->state.num_intro_circ_launched = 20;
  721. ret = can_service_launch_intro_circuit(service, now);
  722. tt_int_op(ret, OP_EQ, 0);
  723. }
  724. /* Testing intro_point_should_expire(). */
  725. {
  726. /* Just some basic test of the current state. */
  727. tt_u64_op(ip->introduce2_max, OP_GE,
  728. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  729. tt_u64_op(ip->introduce2_max, OP_LE,
  730. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  731. tt_u64_op(ip->time_to_expire, OP_GE,
  732. now + INTRO_POINT_LIFETIME_MIN_SECONDS);
  733. tt_u64_op(ip->time_to_expire, OP_LE,
  734. now + INTRO_POINT_LIFETIME_MAX_SECONDS);
  735. /* This newly created IP from above shouldn't expire now. */
  736. ret = intro_point_should_expire(ip, now);
  737. tt_int_op(ret, OP_EQ, 0);
  738. /* Maximum number of INTRODUCE2 cell reached, it should expire. */
  739. ip->introduce2_count = INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS + 1;
  740. ret = intro_point_should_expire(ip, now);
  741. tt_int_op(ret, OP_EQ, 1);
  742. ip->introduce2_count = 0;
  743. /* It should expire if time to expire has been reached. */
  744. ip->time_to_expire = now - 1000;
  745. ret = intro_point_should_expire(ip, now);
  746. tt_int_op(ret, OP_EQ, 1);
  747. }
  748. done:
  749. /* This will free the service and all objects associated to it. */
  750. if (service) {
  751. remove_service(get_hs_service_map(), service);
  752. hs_service_free(service);
  753. }
  754. hs_service_free_all();
  755. UNMOCK(node_get_by_id);
  756. }
  757. /** Test that we do the right operations when an intro circuit opens */
  758. static void
  759. test_intro_circuit_opened(void *arg)
  760. {
  761. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  762. hs_service_t *service = NULL;
  763. origin_circuit_t *circ = NULL;
  764. (void) arg;
  765. hs_init();
  766. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  767. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  768. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  769. flags);
  770. /* No service associated with this circuit. */
  771. setup_full_capture_of_logs(LOG_WARN);
  772. hs_service_circuit_has_opened(circ);
  773. expect_log_msg_containing("Unknown service identity key");
  774. teardown_capture_of_logs();
  775. /* Set a service for this circuit. */
  776. {
  777. service = helper_create_service();
  778. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  779. &service->keys.identity_pk);
  780. /* No intro point associated with this circuit. */
  781. setup_full_capture_of_logs(LOG_WARN);
  782. hs_service_circuit_has_opened(circ);
  783. expect_log_msg_containing("Unknown introduction point auth key");
  784. teardown_capture_of_logs();
  785. }
  786. /* Set an IP object now for this circuit. */
  787. {
  788. hs_service_intro_point_t *ip = helper_create_service_ip();
  789. service_intro_point_add(service->desc_current->intro_points.map, ip);
  790. /* Update ident to contain the intro point auth key. */
  791. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  792. &ip->auth_key_kp.pubkey);
  793. }
  794. /* This one should go all the way. */
  795. setup_full_capture_of_logs(LOG_INFO);
  796. hs_service_circuit_has_opened(circ);
  797. expect_log_msg_containing("Introduction circuit 0 established for service");
  798. teardown_capture_of_logs();
  799. done:
  800. circuit_free_(TO_CIRCUIT(circ));
  801. if (service) {
  802. remove_service(get_hs_service_map(), service);
  803. hs_service_free(service);
  804. }
  805. hs_free_all();
  806. UNMOCK(circuit_mark_for_close_);
  807. UNMOCK(relay_send_command_from_edge_);
  808. }
  809. /** Test the operations we do on a circuit after we learn that we successfully
  810. * established an intro point on it */
  811. static void
  812. test_intro_established(void *arg)
  813. {
  814. int ret;
  815. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  816. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  817. origin_circuit_t *circ = NULL;
  818. hs_service_t *service = NULL;
  819. hs_service_intro_point_t *ip = NULL;
  820. (void) arg;
  821. hs_init();
  822. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  823. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  824. flags);
  825. tt_assert(circ);
  826. /* Test a wrong purpose. */
  827. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  828. setup_full_capture_of_logs(LOG_WARN);
  829. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  830. tt_int_op(ret, OP_EQ, -1);
  831. expect_log_msg_containing("Received an INTRO_ESTABLISHED cell on a "
  832. "non introduction circuit of purpose");
  833. teardown_capture_of_logs();
  834. /* Back to normal. */
  835. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  836. /* No service associated to it. */
  837. setup_full_capture_of_logs(LOG_WARN);
  838. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  839. tt_int_op(ret, OP_EQ, -1);
  840. expect_log_msg_containing("Unknown service identity key");
  841. teardown_capture_of_logs();
  842. /* Set a service for this circuit. */
  843. service = helper_create_service();
  844. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  845. &service->keys.identity_pk);
  846. /* No introduction point associated to it. */
  847. setup_full_capture_of_logs(LOG_WARN);
  848. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  849. tt_int_op(ret, OP_EQ, -1);
  850. expect_log_msg_containing("Introduction circuit established without an "
  851. "intro point object on circuit");
  852. teardown_capture_of_logs();
  853. /* Set an IP object now for this circuit. */
  854. {
  855. ip = helper_create_service_ip();
  856. service_intro_point_add(service->desc_current->intro_points.map, ip);
  857. /* Update ident to contain the intro point auth key. */
  858. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  859. &ip->auth_key_kp.pubkey);
  860. }
  861. /* Send an empty payload. INTRO_ESTABLISHED cells are basically zeroes. */
  862. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  863. tt_int_op(ret, OP_EQ, 0);
  864. tt_u64_op(ip->circuit_established, OP_EQ, 1);
  865. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_INTRO);
  866. done:
  867. if (circ)
  868. circuit_free_(TO_CIRCUIT(circ));
  869. if (service) {
  870. remove_service(get_hs_service_map(), service);
  871. hs_service_free(service);
  872. }
  873. hs_free_all();
  874. UNMOCK(circuit_mark_for_close_);
  875. }
  876. /** Check the operations we do on a rendezvous circuit after we learn it's
  877. * open */
  878. static void
  879. test_rdv_circuit_opened(void *arg)
  880. {
  881. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  882. origin_circuit_t *circ = NULL;
  883. hs_service_t *service = NULL;
  884. (void) arg;
  885. hs_init();
  886. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  887. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  888. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND, flags);
  889. crypto_rand((char *) circ->hs_ident->rendezvous_cookie, REND_COOKIE_LEN);
  890. crypto_rand((char *) circ->hs_ident->rendezvous_handshake_info,
  891. sizeof(circ->hs_ident->rendezvous_handshake_info));
  892. /* No service associated with this circuit. */
  893. setup_full_capture_of_logs(LOG_WARN);
  894. hs_service_circuit_has_opened(circ);
  895. expect_log_msg_containing("Unknown service identity key");
  896. teardown_capture_of_logs();
  897. /* This should be set to a non zero timestamp. */
  898. tt_u64_op(TO_CIRCUIT(circ)->timestamp_dirty, OP_NE, 0);
  899. /* Set a service for this circuit. */
  900. service = helper_create_service();
  901. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  902. &service->keys.identity_pk);
  903. /* Should be all good. */
  904. hs_service_circuit_has_opened(circ);
  905. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  906. done:
  907. circuit_free_(TO_CIRCUIT(circ));
  908. if (service) {
  909. remove_service(get_hs_service_map(), service);
  910. hs_service_free(service);
  911. }
  912. hs_free_all();
  913. UNMOCK(circuit_mark_for_close_);
  914. UNMOCK(relay_send_command_from_edge_);
  915. }
  916. static void
  917. mock_assert_circuit_ok(const circuit_t *c)
  918. {
  919. (void) c;
  920. return;
  921. }
  922. /** Test for the general mechanism for closing intro circs.
  923. * Also a way to identify that #23603 has been fixed. */
  924. static void
  925. test_closing_intro_circs(void *arg)
  926. {
  927. hs_service_t *service = NULL;
  928. hs_service_intro_point_t *ip = NULL, *entry = NULL;
  929. origin_circuit_t *intro_circ = NULL, *tmp_circ;
  930. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  931. (void) arg;
  932. MOCK(assert_circuit_ok, mock_assert_circuit_ok);
  933. hs_init();
  934. /* Initialize service */
  935. service = helper_create_service();
  936. /* Initialize intro point */
  937. ip = helper_create_service_ip();
  938. tt_assert(ip);
  939. service_intro_point_add(service->desc_current->intro_points.map, ip);
  940. /* Initialize intro circuit */
  941. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  942. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk);
  943. /* Register circuit in the circuitmap . */
  944. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  945. &ip->auth_key_kp.pubkey);
  946. tmp_circ =
  947. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  948. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  949. /* Pretend that intro point has failed too much */
  950. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES+1;
  951. /* Now pretend we are freeing this intro circuit. We want to see that our
  952. * destructor is not gonna kill our intro point structure since that's the
  953. * job of the cleanup routine. */
  954. circuit_free_(TO_CIRCUIT(intro_circ));
  955. intro_circ = NULL;
  956. entry = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  957. tt_assert(entry);
  958. /* The free should also remove the circuit from the circuitmap. */
  959. tmp_circ =
  960. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  961. tt_assert(!tmp_circ);
  962. /* Now pretend that a new intro point circ was launched and opened. Check
  963. * that the intro point will be established correctly. */
  964. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  965. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk);
  966. ed25519_pubkey_copy(&intro_circ->hs_ident->intro_auth_pk,
  967. &ip->auth_key_kp.pubkey);
  968. /* Register circuit in the circuitmap . */
  969. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  970. &ip->auth_key_kp.pubkey);
  971. tmp_circ =
  972. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  973. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  974. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_EQ, 0);
  975. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  976. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_NE, 0);
  977. /* At this point, we should not be able to find it in the circuitmap. */
  978. tmp_circ =
  979. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  980. tt_assert(!tmp_circ);
  981. done:
  982. if (intro_circ) {
  983. circuit_free_(TO_CIRCUIT(intro_circ));
  984. }
  985. /* Frees the service object. */
  986. if (service) {
  987. remove_service(get_hs_service_map(), service);
  988. hs_service_free(service);
  989. }
  990. hs_free_all();
  991. UNMOCK(assert_circuit_ok);
  992. }
  993. /** Test sending and receiving introduce2 cells */
  994. static void
  995. test_introduce2(void *arg)
  996. {
  997. int ret;
  998. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  999. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  1000. origin_circuit_t *circ = NULL;
  1001. hs_service_t *service = NULL;
  1002. hs_service_intro_point_t *ip = NULL;
  1003. (void) arg;
  1004. hs_init();
  1005. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  1006. MOCK(get_or_state,
  1007. get_or_state_replacement);
  1008. dummy_state = or_state_new();
  1009. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  1010. tt_assert(circ);
  1011. /* Test a wrong purpose. */
  1012. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  1013. setup_full_capture_of_logs(LOG_WARN);
  1014. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1015. tt_int_op(ret, OP_EQ, -1);
  1016. expect_log_msg_containing("Received an INTRODUCE2 cell on a "
  1017. "non introduction circuit of purpose");
  1018. teardown_capture_of_logs();
  1019. /* Back to normal. */
  1020. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  1021. /* No service associated to it. */
  1022. setup_full_capture_of_logs(LOG_WARN);
  1023. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1024. tt_int_op(ret, OP_EQ, -1);
  1025. expect_log_msg_containing("Unknown service identity key");
  1026. teardown_capture_of_logs();
  1027. /* Set a service for this circuit. */
  1028. service = helper_create_service();
  1029. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  1030. &service->keys.identity_pk);
  1031. /* No introduction point associated to it. */
  1032. setup_full_capture_of_logs(LOG_WARN);
  1033. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1034. tt_int_op(ret, OP_EQ, -1);
  1035. expect_log_msg_containing("Unknown introduction auth key when handling "
  1036. "an INTRODUCE2 cell on circuit");
  1037. teardown_capture_of_logs();
  1038. /* Set an IP object now for this circuit. */
  1039. {
  1040. ip = helper_create_service_ip();
  1041. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1042. /* Update ident to contain the intro point auth key. */
  1043. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  1044. &ip->auth_key_kp.pubkey);
  1045. }
  1046. /* This will fail because receiving an INTRODUCE2 cell implies a valid cell
  1047. * and then launching circuits so let's not do that and instead test that
  1048. * behaviour differently. */
  1049. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1050. tt_int_op(ret, OP_EQ, -1);
  1051. tt_u64_op(ip->introduce2_count, OP_EQ, 0);
  1052. done:
  1053. or_state_free(dummy_state);
  1054. dummy_state = NULL;
  1055. if (circ)
  1056. circuit_free_(TO_CIRCUIT(circ));
  1057. if (service) {
  1058. remove_service(get_hs_service_map(), service);
  1059. hs_service_free(service);
  1060. }
  1061. hs_free_all();
  1062. UNMOCK(circuit_mark_for_close_);
  1063. }
  1064. /** Test basic hidden service housekeeping operations (maintaining intro
  1065. * points, etc) */
  1066. static void
  1067. test_service_event(void *arg)
  1068. {
  1069. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1070. time_t now = time(NULL);
  1071. hs_service_t *service;
  1072. origin_circuit_t *circ = NULL;
  1073. (void) arg;
  1074. hs_init();
  1075. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  1076. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  1077. /* Set a service for this circuit. */
  1078. service = helper_create_service();
  1079. tt_assert(service);
  1080. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  1081. &service->keys.identity_pk);
  1082. /* Currently this consists of cleaning invalid intro points. So adding IPs
  1083. * here that should get cleaned up. */
  1084. {
  1085. hs_service_intro_point_t *ip = helper_create_service_ip();
  1086. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1087. /* This run will remove the IP because we have no circuits nor node_t
  1088. * associated with it. */
  1089. run_housekeeping_event(now);
  1090. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1091. OP_EQ, 0);
  1092. /* We'll trigger a removal because we've reached our maximum amount of
  1093. * times we should retry a circuit. For this, we need to have a node_t
  1094. * that matches the identity of this IP. */
  1095. routerinfo_t ri;
  1096. memset(&ri, 0, sizeof(ri));
  1097. ip = helper_create_service_ip();
  1098. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1099. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1100. /* This triggers a node_t creation. */
  1101. tt_assert(nodelist_set_routerinfo(&ri, NULL));
  1102. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  1103. run_housekeeping_event(now);
  1104. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1105. OP_EQ, 0);
  1106. /* No removal but no circuit so this means the IP object will stay in the
  1107. * descriptor map so we can retry it. */
  1108. ip = helper_create_service_ip();
  1109. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1110. ip->circuit_established = 1; /* We'll test that, it MUST be 0 after. */
  1111. run_housekeeping_event(now);
  1112. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1113. OP_EQ, 1);
  1114. /* Remove the IP object at once for the next test. */
  1115. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  1116. ip->circuit_established = 0;
  1117. run_housekeeping_event(now);
  1118. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1119. OP_EQ, 0);
  1120. /* Now, we'll create an IP with a registered circuit. The IP object
  1121. * shouldn't go away. */
  1122. ip = helper_create_service_ip();
  1123. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1124. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  1125. &ip->auth_key_kp.pubkey);
  1126. hs_circuitmap_register_intro_circ_v3_service_side(
  1127. circ, &ip->auth_key_kp.pubkey);
  1128. run_housekeeping_event(now);
  1129. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1130. OP_EQ, 1);
  1131. /* We'll mangle the IP object to expire. */
  1132. ip->time_to_expire = now;
  1133. run_housekeeping_event(now);
  1134. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1135. OP_EQ, 0);
  1136. }
  1137. done:
  1138. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  1139. circuit_free_(TO_CIRCUIT(circ));
  1140. if (service) {
  1141. remove_service(get_hs_service_map(), service);
  1142. hs_service_free(service);
  1143. }
  1144. hs_free_all();
  1145. UNMOCK(circuit_mark_for_close_);
  1146. }
  1147. /** Test that we rotate descriptors correctly. */
  1148. static void
  1149. test_rotate_descriptors(void *arg)
  1150. {
  1151. int ret;
  1152. time_t next_rotation_time, now;
  1153. hs_service_t *service = NULL;
  1154. hs_service_descriptor_t *desc_next;
  1155. (void) arg;
  1156. dummy_state = or_state_new();
  1157. hs_init();
  1158. MOCK(get_or_state, get_or_state_replacement);
  1159. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  1160. MOCK(networkstatus_get_live_consensus,
  1161. mock_networkstatus_get_live_consensus);
  1162. /* Descriptor rotation happens with a consensus with a new SRV. */
  1163. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1164. &mock_ns.valid_after);
  1165. tt_int_op(ret, OP_EQ, 0);
  1166. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1167. &mock_ns.fresh_until);
  1168. tt_int_op(ret, OP_EQ, 0);
  1169. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1170. update_approx_time(mock_ns.valid_after+1);
  1171. now = mock_ns.valid_after+1;
  1172. /* Create a service with a default descriptor and state. It's added to the
  1173. * global map. */
  1174. service = helper_create_service();
  1175. service_descriptor_free(service->desc_current);
  1176. service->desc_current = NULL;
  1177. /* This triggers a build for both descriptors. The time now is only used in
  1178. * the descriptor certificate which is important to be now else the decoding
  1179. * will complain that the cert has expired if we use valid_after. */
  1180. build_all_descriptors(now);
  1181. tt_assert(service->desc_current);
  1182. tt_assert(service->desc_next);
  1183. /* Tweak our service next rotation time so we can use a custom time. */
  1184. service->state.next_rotation_time = next_rotation_time =
  1185. mock_ns.valid_after + (11 * 60 * 60);
  1186. /* Nothing should happen, we are not at a new SRV. Our next rotation time
  1187. * should be untouched. */
  1188. rotate_all_descriptors(mock_ns.valid_after);
  1189. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1190. tt_assert(service->desc_current);
  1191. tt_assert(service->desc_next);
  1192. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1193. hs_get_previous_time_period_num(0));
  1194. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1195. hs_get_time_period_num(0));
  1196. /* Keep a reference so we can compare it after rotation to the current. */
  1197. desc_next = service->desc_next;
  1198. /* Going right after a new SRV. */
  1199. ret = parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC",
  1200. &mock_ns.valid_after);
  1201. tt_int_op(ret, OP_EQ, 0);
  1202. ret = parse_rfc1123_time("Sat, 27 Oct 1985 02:00:00 UTC",
  1203. &mock_ns.fresh_until);
  1204. tt_int_op(ret, OP_EQ, 0);
  1205. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1206. update_approx_time(mock_ns.valid_after+1);
  1207. now = mock_ns.valid_after+1;
  1208. /* Note down what to expect for the next rotation time which is 01:00 + 23h
  1209. * meaning 00:00:00. */
  1210. next_rotation_time = mock_ns.valid_after + (23 * 60 * 60);
  1211. /* We should have our next rotation time modified, our current descriptor
  1212. * cleaned up and the next descriptor becoming the current. */
  1213. rotate_all_descriptors(mock_ns.valid_after);
  1214. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1215. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1216. tt_assert(service->desc_next == NULL);
  1217. /* A second time should do nothing. */
  1218. rotate_all_descriptors(mock_ns.valid_after);
  1219. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1220. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1221. tt_assert(service->desc_next == NULL);
  1222. build_all_descriptors(now);
  1223. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1224. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1225. hs_get_time_period_num(0));
  1226. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1227. hs_get_next_time_period_num(0));
  1228. tt_assert(service->desc_next);
  1229. done:
  1230. if (service) {
  1231. remove_service(get_hs_service_map(), service);
  1232. hs_service_free(service);
  1233. }
  1234. hs_free_all();
  1235. UNMOCK(get_or_state);
  1236. UNMOCK(circuit_mark_for_close_);
  1237. UNMOCK(networkstatus_get_live_consensus);
  1238. }
  1239. /** Test building descriptors: picking intro points, setting up their link
  1240. * specifiers, etc. */
  1241. static void
  1242. test_build_update_descriptors(void *arg)
  1243. {
  1244. int ret;
  1245. node_t *node;
  1246. hs_service_t *service = NULL;
  1247. hs_service_intro_point_t *ip_cur, *ip_next;
  1248. routerinfo_t ri;
  1249. (void) arg;
  1250. hs_init();
  1251. MOCK(get_or_state,
  1252. get_or_state_replacement);
  1253. MOCK(networkstatus_get_live_consensus,
  1254. mock_networkstatus_get_live_consensus);
  1255. dummy_state = or_state_new();
  1256. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  1257. &mock_ns.valid_after);
  1258. tt_int_op(ret, OP_EQ, 0);
  1259. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  1260. &mock_ns.fresh_until);
  1261. tt_int_op(ret, OP_EQ, 0);
  1262. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1263. update_approx_time(mock_ns.valid_after+1);
  1264. time_t now = mock_ns.valid_after+1;
  1265. /* Create a service without a current descriptor to trigger a build. */
  1266. service = helper_create_service();
  1267. tt_assert(service);
  1268. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1269. service_descriptor_free(service->desc_current);
  1270. service->desc_current = NULL;
  1271. /* We have a fresh service so this should trigger a build for both
  1272. * descriptors for specific time period that we'll test. */
  1273. build_all_descriptors(now);
  1274. /* Check *current* descriptor. */
  1275. tt_assert(service->desc_current);
  1276. tt_assert(service->desc_current->desc);
  1277. tt_assert(service->desc_current->intro_points.map);
  1278. /* The current time period is the one expected when starting at 03:00. */
  1279. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1280. hs_get_time_period_num(0));
  1281. /* This should be untouched, the update descriptor process changes it. */
  1282. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1283. /* Check *next* descriptor. */
  1284. tt_assert(service->desc_next);
  1285. tt_assert(service->desc_next->desc);
  1286. tt_assert(service->desc_next->intro_points.map);
  1287. tt_assert(service->desc_current != service->desc_next);
  1288. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1289. hs_get_next_time_period_num(0));
  1290. /* This should be untouched, the update descriptor process changes it. */
  1291. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1292. /* Time to test the update of those descriptors. At first, we have no node
  1293. * in the routerlist so this will find NO suitable node for the IPs. */
  1294. setup_full_capture_of_logs(LOG_INFO);
  1295. update_all_descriptors_intro_points(now);
  1296. expect_log_msg_containing("Unable to find a suitable node to be an "
  1297. "introduction point for service");
  1298. teardown_capture_of_logs();
  1299. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1300. OP_EQ, 0);
  1301. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1302. OP_EQ, 0);
  1303. /* Now, we'll setup a node_t. */
  1304. {
  1305. tor_addr_t ipv4_addr;
  1306. curve25519_secret_key_t curve25519_secret_key;
  1307. memset(&ri, 0, sizeof(routerinfo_t));
  1308. tor_addr_parse(&ipv4_addr, "127.0.0.1");
  1309. ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  1310. ri.or_port = 1337;
  1311. ri.purpose = ROUTER_PURPOSE_GENERAL;
  1312. /* Ugly yes but we never free the "ri" object so this just makes things
  1313. * easier. */
  1314. ri.protocol_list = (char *) "HSDir=1-2 LinkAuth=3";
  1315. summarize_protover_flags(&ri.pv, ri.protocol_list, NULL);
  1316. ret = curve25519_secret_key_generate(&curve25519_secret_key, 0);
  1317. tt_int_op(ret, OP_EQ, 0);
  1318. ri.onion_curve25519_pkey =
  1319. tor_malloc_zero(sizeof(curve25519_public_key_t));
  1320. ri.onion_pkey = tor_malloc_zero(140);
  1321. curve25519_public_key_generate(ri.onion_curve25519_pkey,
  1322. &curve25519_secret_key);
  1323. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1324. /* Setup ed25519 identity */
  1325. ed25519_keypair_t kp1;
  1326. ed25519_keypair_generate(&kp1, 0);
  1327. ri.cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  1328. tt_assert(ri.cache_info.signing_key_cert);
  1329. ed25519_pubkey_copy(&ri.cache_info.signing_key_cert->signing_key,
  1330. &kp1.pubkey);
  1331. nodelist_set_routerinfo(&ri, NULL);
  1332. node = node_get_mutable_by_id(ri.cache_info.identity_digest);
  1333. tt_assert(node);
  1334. node->is_running = node->is_valid = node->is_fast = node->is_stable = 1;
  1335. }
  1336. /* We have to set this, or the lack of microdescriptors for these
  1337. * nodes will make them unusable. */
  1338. get_options_mutable()->UseMicrodescriptors = 0;
  1339. /* We expect to pick only one intro point from the node above. */
  1340. setup_full_capture_of_logs(LOG_INFO);
  1341. update_all_descriptors_intro_points(now);
  1342. tor_free(node->ri->onion_curve25519_pkey); /* Avoid memleak. */
  1343. tor_free(node->ri->cache_info.signing_key_cert);
  1344. tor_free(node->ri->onion_pkey);
  1345. expect_log_msg_containing("just picked 1 intro points and wanted 3 for next "
  1346. "descriptor. It currently has 0 intro points. "
  1347. "Launching ESTABLISH_INTRO circuit shortly.");
  1348. teardown_capture_of_logs();
  1349. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1350. OP_EQ, 1);
  1351. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1352. OP_EQ, 1);
  1353. /* Get the IP object. Because we don't have the auth key of the IP, we can't
  1354. * query it so get the first element in the map. */
  1355. {
  1356. void *obj = NULL;
  1357. const uint8_t *key;
  1358. digest256map_iter_t *iter =
  1359. digest256map_iter_init(service->desc_current->intro_points.map);
  1360. digest256map_iter_get(iter, &key, &obj);
  1361. tt_assert(obj);
  1362. ip_cur = obj;
  1363. /* Get also the IP from the next descriptor. We'll make sure it's not the
  1364. * same object as in the current descriptor. */
  1365. iter = digest256map_iter_init(service->desc_next->intro_points.map);
  1366. digest256map_iter_get(iter, &key, &obj);
  1367. tt_assert(obj);
  1368. ip_next = obj;
  1369. }
  1370. tt_mem_op(ip_cur, OP_NE, ip_next, sizeof(hs_desc_intro_point_t));
  1371. /* We won't test the service IP object because there is a specific test
  1372. * already for this but we'll make sure that the state is coherent.*/
  1373. /* Three link specifiers are mandatoy so make sure we do have them. */
  1374. tt_int_op(smartlist_len(ip_cur->base.link_specifiers), OP_EQ, 3);
  1375. /* Make sure we have a valid encryption keypair generated when we pick an
  1376. * intro point in the update process. */
  1377. tt_assert(!fast_mem_is_zero((char *) ip_cur->enc_key_kp.seckey.secret_key,
  1378. CURVE25519_SECKEY_LEN));
  1379. tt_assert(!fast_mem_is_zero((char *) ip_cur->enc_key_kp.pubkey.public_key,
  1380. CURVE25519_PUBKEY_LEN));
  1381. tt_u64_op(ip_cur->time_to_expire, OP_GE, now +
  1382. INTRO_POINT_LIFETIME_MIN_SECONDS);
  1383. tt_u64_op(ip_cur->time_to_expire, OP_LE, now +
  1384. INTRO_POINT_LIFETIME_MAX_SECONDS);
  1385. /* Now, we will try to set up a service after a new time period has started
  1386. * and see if it behaves as expected. */
  1387. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1388. &mock_ns.valid_after);
  1389. tt_int_op(ret, OP_EQ, 0);
  1390. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1391. &mock_ns.fresh_until);
  1392. tt_int_op(ret, OP_EQ, 0);
  1393. update_approx_time(mock_ns.valid_after+1);
  1394. now = mock_ns.valid_after+1;
  1395. /* Create a service without a current descriptor to trigger a build. */
  1396. service = helper_create_service();
  1397. tt_assert(service);
  1398. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1399. service_descriptor_free(service->desc_current);
  1400. service->desc_current = NULL;
  1401. /* We have a fresh service so this should trigger a build for both
  1402. * descriptors for specific time period that we'll test. */
  1403. build_all_descriptors(now);
  1404. /* Check *current* descriptor. */
  1405. tt_assert(service->desc_current);
  1406. tt_assert(service->desc_current->desc);
  1407. tt_assert(service->desc_current->intro_points.map);
  1408. /* This should be for the previous time period. */
  1409. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1410. hs_get_previous_time_period_num(0));
  1411. /* This should be untouched, the update descriptor process changes it. */
  1412. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1413. /* Check *next* descriptor. */
  1414. tt_assert(service->desc_next);
  1415. tt_assert(service->desc_next->desc);
  1416. tt_assert(service->desc_next->intro_points.map);
  1417. tt_assert(service->desc_current != service->desc_next);
  1418. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1419. hs_get_time_period_num(0));
  1420. /* This should be untouched, the update descriptor process changes it. */
  1421. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1422. /* Let's remove the next descriptor to simulate a rotation. */
  1423. service_descriptor_free(service->desc_next);
  1424. service->desc_next = NULL;
  1425. build_all_descriptors(now);
  1426. /* Check *next* descriptor. */
  1427. tt_assert(service->desc_next);
  1428. tt_assert(service->desc_next->desc);
  1429. tt_assert(service->desc_next->intro_points.map);
  1430. tt_assert(service->desc_current != service->desc_next);
  1431. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1432. hs_get_next_time_period_num(0));
  1433. /* This should be untouched, the update descriptor process changes it. */
  1434. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1435. done:
  1436. if (service) {
  1437. remove_service(get_hs_service_map(), service);
  1438. hs_service_free(service);
  1439. }
  1440. hs_free_all();
  1441. nodelist_free_all();
  1442. }
  1443. /** Test building descriptors. We use this separate function instead of
  1444. * using test_build_update_descriptors because that function is too complex
  1445. * and also too interactive. */
  1446. static void
  1447. test_build_descriptors(void *arg)
  1448. {
  1449. int ret;
  1450. time_t now = time(NULL);
  1451. hs_service_t *last_service = NULL;
  1452. (void) arg;
  1453. hs_init();
  1454. MOCK(get_or_state,
  1455. get_or_state_replacement);
  1456. MOCK(networkstatus_get_live_consensus,
  1457. mock_networkstatus_get_live_consensus);
  1458. dummy_state = or_state_new();
  1459. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  1460. &mock_ns.valid_after);
  1461. tt_int_op(ret, OP_EQ, 0);
  1462. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  1463. &mock_ns.fresh_until);
  1464. tt_int_op(ret, OP_EQ, 0);
  1465. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1466. /* Generate a valid number of fake auth clients when a client authorization
  1467. * is disabled. */
  1468. {
  1469. hs_service_t *service = helper_create_service();
  1470. last_service = service;
  1471. service_descriptor_free(service->desc_current);
  1472. service->desc_current = NULL;
  1473. build_all_descriptors(now);
  1474. tt_assert(service->desc_current);
  1475. tt_assert(service->desc_current->desc);
  1476. hs_desc_superencrypted_data_t *superencrypted;
  1477. superencrypted = &service->desc_current->desc->superencrypted_data;
  1478. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 16);
  1479. helper_destroy_service(service);
  1480. last_service = NULL;
  1481. }
  1482. /* Generate a valid number of fake auth clients when the number of
  1483. * clients is zero. */
  1484. {
  1485. hs_service_t *service = helper_create_service_with_clients(0);
  1486. last_service = service;
  1487. service_descriptor_free(service->desc_current);
  1488. service->desc_current = NULL;
  1489. build_all_descriptors(now);
  1490. hs_desc_superencrypted_data_t *superencrypted;
  1491. superencrypted = &service->desc_current->desc->superencrypted_data;
  1492. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 16);
  1493. helper_destroy_service(service);
  1494. last_service = NULL;
  1495. }
  1496. /* Generate a valid number of fake auth clients when the number of
  1497. * clients is not a multiple of 16. */
  1498. {
  1499. hs_service_t *service = helper_create_service_with_clients(20);
  1500. last_service = service;
  1501. service_descriptor_free(service->desc_current);
  1502. service->desc_current = NULL;
  1503. build_all_descriptors(now);
  1504. hs_desc_superencrypted_data_t *superencrypted;
  1505. superencrypted = &service->desc_current->desc->superencrypted_data;
  1506. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 32);
  1507. helper_destroy_service(service);
  1508. last_service = NULL;
  1509. }
  1510. /* Do not generate any fake desc client when the number of clients is
  1511. * a multiple of 16 but not zero. */
  1512. {
  1513. hs_service_t *service = helper_create_service_with_clients(32);
  1514. last_service = service;
  1515. service_descriptor_free(service->desc_current);
  1516. service->desc_current = NULL;
  1517. build_all_descriptors(now);
  1518. hs_desc_superencrypted_data_t *superencrypted;
  1519. superencrypted = &service->desc_current->desc->superencrypted_data;
  1520. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 32);
  1521. helper_destroy_service(service);
  1522. last_service = NULL;
  1523. }
  1524. done:
  1525. helper_destroy_service(last_service);
  1526. hs_free_all();
  1527. }
  1528. static void
  1529. test_upload_descriptors(void *arg)
  1530. {
  1531. int ret;
  1532. time_t now;
  1533. hs_service_t *service;
  1534. (void) arg;
  1535. hs_init();
  1536. MOCK(get_or_state,
  1537. get_or_state_replacement);
  1538. MOCK(networkstatus_get_live_consensus,
  1539. mock_networkstatus_get_live_consensus);
  1540. dummy_state = or_state_new();
  1541. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1542. &mock_ns.valid_after);
  1543. tt_int_op(ret, OP_EQ, 0);
  1544. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1545. &mock_ns.fresh_until);
  1546. tt_int_op(ret, OP_EQ, 0);
  1547. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1548. update_approx_time(mock_ns.valid_after+1);
  1549. now = mock_ns.valid_after+1;
  1550. /* Create a service with no descriptor. It's added to the global map. */
  1551. service = hs_service_new(get_options());
  1552. tt_assert(service);
  1553. service->config.version = HS_VERSION_THREE;
  1554. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1555. ed25519_public_key_generate(&service->keys.identity_pk,
  1556. &service->keys.identity_sk);
  1557. /* Register service to global map. */
  1558. ret = register_service(get_hs_service_map(), service);
  1559. tt_int_op(ret, OP_EQ, 0);
  1560. /* But first, build our descriptor. */
  1561. build_all_descriptors(now);
  1562. /* Nothing should happen because we have 0 introduction circuit established
  1563. * and we want (by default) 3 intro points. */
  1564. run_upload_descriptor_event(now);
  1565. /* If no upload happened, this should be untouched. */
  1566. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1567. /* We'll simulate that we've opened our intro point circuit and that we only
  1568. * want one intro point. */
  1569. service->config.num_intro_points = 1;
  1570. /* Set our next upload time after now which will skip the upload. */
  1571. service->desc_current->next_upload_time = now + 1000;
  1572. run_upload_descriptor_event(now);
  1573. /* If no upload happened, this should be untouched. */
  1574. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, now + 1000);
  1575. done:
  1576. hs_free_all();
  1577. UNMOCK(get_or_state);
  1578. }
  1579. /** Global vars used by test_rendezvous1_parsing() */
  1580. static char rend1_payload[RELAY_PAYLOAD_SIZE];
  1581. static size_t rend1_payload_len = 0;
  1582. /** Mock for relay_send_command_from_edge() to send a RENDEZVOUS1 cell. Instead
  1583. * of sending it to the network, instead save it to the global `rend1_payload`
  1584. * variable so that we can inspect it in the test_rendezvous1_parsing()
  1585. * test. */
  1586. static int
  1587. mock_relay_send_rendezvous1(streamid_t stream_id, circuit_t *circ,
  1588. uint8_t relay_command, const char *payload,
  1589. size_t payload_len,
  1590. crypt_path_t *cpath_layer,
  1591. const char *filename, int lineno)
  1592. {
  1593. (void) stream_id;
  1594. (void) circ;
  1595. (void) relay_command;
  1596. (void) cpath_layer;
  1597. (void) filename;
  1598. (void) lineno;
  1599. memcpy(rend1_payload, payload, payload_len);
  1600. rend1_payload_len = payload_len;
  1601. return 0;
  1602. }
  1603. /** Send a RENDEZVOUS1 as a service, and parse it as a client. */
  1604. static void
  1605. test_rendezvous1_parsing(void *arg)
  1606. {
  1607. int retval;
  1608. static const char *test_addr =
  1609. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad.onion";
  1610. hs_service_t *service = NULL;
  1611. origin_circuit_t *service_circ = NULL;
  1612. origin_circuit_t *client_circ = NULL;
  1613. ed25519_keypair_t ip_auth_kp;
  1614. curve25519_keypair_t ephemeral_kp;
  1615. curve25519_keypair_t client_kp;
  1616. curve25519_keypair_t ip_enc_kp;
  1617. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1618. (void) arg;
  1619. MOCK(relay_send_command_from_edge_, mock_relay_send_rendezvous1);
  1620. {
  1621. /* Let's start by setting up the service that will start the rend */
  1622. service = tor_malloc_zero(sizeof(hs_service_t));
  1623. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1624. ed25519_public_key_generate(&service->keys.identity_pk,
  1625. &service->keys.identity_sk);
  1626. memcpy(service->onion_address, test_addr, sizeof(service->onion_address));
  1627. tt_assert(service);
  1628. }
  1629. {
  1630. /* Now let's set up the service rendezvous circuit and its keys. */
  1631. service_circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND,
  1632. flags);
  1633. tor_free(service_circ->hs_ident);
  1634. hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys;
  1635. uint8_t rendezvous_cookie[HS_REND_COOKIE_LEN];
  1636. curve25519_keypair_generate(&ip_enc_kp, 0);
  1637. curve25519_keypair_generate(&ephemeral_kp, 0);
  1638. curve25519_keypair_generate(&client_kp, 0);
  1639. ed25519_keypair_generate(&ip_auth_kp, 0);
  1640. retval = hs_ntor_service_get_rendezvous1_keys(&ip_auth_kp.pubkey,
  1641. &ip_enc_kp,
  1642. &ephemeral_kp,
  1643. &client_kp.pubkey,
  1644. &hs_ntor_rend_cell_keys);
  1645. tt_int_op(retval, OP_EQ, 0);
  1646. memset(rendezvous_cookie, 2, sizeof(rendezvous_cookie));
  1647. service_circ->hs_ident =
  1648. create_rp_circuit_identifier(service, rendezvous_cookie,
  1649. &ephemeral_kp.pubkey,
  1650. &hs_ntor_rend_cell_keys);
  1651. }
  1652. /* Send out the RENDEZVOUS1 and make sure that our mock func worked */
  1653. tt_assert(fast_mem_is_zero(rend1_payload, 32));
  1654. hs_circ_service_rp_has_opened(service, service_circ);
  1655. tt_assert(!fast_mem_is_zero(rend1_payload, 32));
  1656. tt_int_op(rend1_payload_len, OP_EQ, HS_LEGACY_RENDEZVOUS_CELL_SIZE);
  1657. /******************************/
  1658. /** Now let's create the client rendezvous circuit */
  1659. client_circ =
  1660. helper_create_origin_circuit(CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED,
  1661. flags);
  1662. /* fix up its circ ident */
  1663. ed25519_pubkey_copy(&client_circ->hs_ident->intro_auth_pk,
  1664. &ip_auth_kp.pubkey);
  1665. memcpy(&client_circ->hs_ident->rendezvous_client_kp,
  1666. &client_kp, sizeof(client_circ->hs_ident->rendezvous_client_kp));
  1667. memcpy(&client_circ->hs_ident->intro_enc_pk.public_key,
  1668. &ip_enc_kp.pubkey.public_key,
  1669. sizeof(client_circ->hs_ident->intro_enc_pk.public_key));
  1670. /* Now parse the rendezvous2 circuit and make sure it was fine. We are
  1671. * skipping 20 bytes off its payload, since that's the rendezvous cookie
  1672. * which is only present in REND1. */
  1673. retval = handle_rendezvous2(client_circ,
  1674. (uint8_t*)rend1_payload+20,
  1675. rend1_payload_len-20);
  1676. tt_int_op(retval, OP_EQ, 0);
  1677. /* TODO: We are only simulating client/service here. We could also simulate
  1678. * the rendezvous point by plugging in rend_mid_establish_rendezvous(). We
  1679. * would need an extra circuit and some more stuff but it's doable. */
  1680. done:
  1681. circuit_free_(TO_CIRCUIT(service_circ));
  1682. circuit_free_(TO_CIRCUIT(client_circ));
  1683. hs_service_free(service);
  1684. hs_free_all();
  1685. UNMOCK(relay_send_command_from_edge_);
  1686. }
  1687. static void
  1688. test_authorized_client_config_equal(void *arg)
  1689. {
  1690. int ret;
  1691. hs_service_config_t *config1, *config2;
  1692. (void) arg;
  1693. config1 = tor_malloc_zero(sizeof(*config1));
  1694. config2 = tor_malloc_zero(sizeof(*config2));
  1695. /* Both configs are empty. */
  1696. {
  1697. config1->clients = smartlist_new();
  1698. config2->clients = smartlist_new();
  1699. ret = service_authorized_client_config_equal(config1, config2);
  1700. tt_int_op(ret, OP_EQ, 1);
  1701. service_clear_config(config1);
  1702. service_clear_config(config2);
  1703. }
  1704. /* Both configs have exactly the same client config. */
  1705. {
  1706. config1->clients = smartlist_new();
  1707. config2->clients = smartlist_new();
  1708. hs_service_authorized_client_t *client1, *client2;
  1709. client1 = helper_create_authorized_client();
  1710. client2 = helper_create_authorized_client();
  1711. smartlist_add(config1->clients, client1);
  1712. smartlist_add(config1->clients, client2);
  1713. /* We should swap the order of clients here to test that the order
  1714. * does not matter. */
  1715. smartlist_add(config2->clients, helper_clone_authorized_client(client2));
  1716. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1717. ret = service_authorized_client_config_equal(config1, config2);
  1718. tt_int_op(ret, OP_EQ, 1);
  1719. service_clear_config(config1);
  1720. service_clear_config(config2);
  1721. }
  1722. /* The numbers of clients in both configs are not equal. */
  1723. {
  1724. config1->clients = smartlist_new();
  1725. config2->clients = smartlist_new();
  1726. hs_service_authorized_client_t *client1, *client2;
  1727. client1 = helper_create_authorized_client();
  1728. client2 = helper_create_authorized_client();
  1729. smartlist_add(config1->clients, client1);
  1730. smartlist_add(config1->clients, client2);
  1731. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1732. ret = service_authorized_client_config_equal(config1, config2);
  1733. tt_int_op(ret, OP_EQ, 0);
  1734. service_clear_config(config1);
  1735. service_clear_config(config2);
  1736. }
  1737. /* The first config has two distinct clients while the second config
  1738. * has two clients but they are duplicate. */
  1739. {
  1740. config1->clients = smartlist_new();
  1741. config2->clients = smartlist_new();
  1742. hs_service_authorized_client_t *client1, *client2;
  1743. client1 = helper_create_authorized_client();
  1744. client2 = helper_create_authorized_client();
  1745. smartlist_add(config1->clients, client1);
  1746. smartlist_add(config1->clients, client2);
  1747. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1748. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1749. ret = service_authorized_client_config_equal(config1, config2);
  1750. tt_int_op(ret, OP_EQ, 0);
  1751. service_clear_config(config1);
  1752. service_clear_config(config2);
  1753. }
  1754. /* Both configs have totally distinct clients. */
  1755. {
  1756. config1->clients = smartlist_new();
  1757. config2->clients = smartlist_new();
  1758. hs_service_authorized_client_t *client1, *client2, *client3, *client4;
  1759. client1 = helper_create_authorized_client();
  1760. client2 = helper_create_authorized_client();
  1761. client3 = helper_create_authorized_client();
  1762. client4 = helper_create_authorized_client();
  1763. smartlist_add(config1->clients, client1);
  1764. smartlist_add(config1->clients, client2);
  1765. smartlist_add(config2->clients, client3);
  1766. smartlist_add(config2->clients, client4);
  1767. ret = service_authorized_client_config_equal(config1, config2);
  1768. tt_int_op(ret, OP_EQ, 0);
  1769. service_clear_config(config1);
  1770. service_clear_config(config2);
  1771. }
  1772. done:
  1773. tor_free(config1);
  1774. tor_free(config2);
  1775. }
  1776. /** Test that client circuit ID gets correctly exported */
  1777. static void
  1778. test_export_client_circuit_id(void *arg)
  1779. {
  1780. origin_circuit_t *or_circ = NULL;
  1781. size_t sz;
  1782. char *cp1=NULL, *cp2=NULL;
  1783. connection_t *conn = NULL;
  1784. (void) arg;
  1785. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1786. hs_service_init();
  1787. /* Create service */
  1788. hs_service_t *service = helper_create_service();
  1789. /* Check that export circuit ID detection works */
  1790. service->config.circuit_id_protocol = HS_CIRCUIT_ID_PROTOCOL_NONE;
  1791. tt_int_op(0, OP_EQ,
  1792. hs_service_exports_circuit_id(&service->keys.identity_pk));
  1793. service->config.circuit_id_protocol = HS_CIRCUIT_ID_PROTOCOL_HAPROXY;
  1794. tt_int_op(1, OP_EQ,
  1795. hs_service_exports_circuit_id(&service->keys.identity_pk));
  1796. /* Create client connection */
  1797. conn = test_conn_get_connection(AP_CONN_STATE_CIRCUIT_WAIT, CONN_TYPE_AP, 0);
  1798. /* Create client edge conn hs_ident */
  1799. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  1800. edge_conn->hs_ident = hs_ident_edge_conn_new(&service->keys.identity_pk);
  1801. edge_conn->hs_ident->orig_virtual_port = 42;
  1802. /* Create rend circuit */
  1803. or_circ = origin_circuit_new();
  1804. or_circ->base_.purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  1805. edge_conn->on_circuit = TO_CIRCUIT(or_circ);
  1806. or_circ->global_identifier = 666;
  1807. /* Export circuit ID */
  1808. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1809. /* Check contents */
  1810. cp1 = buf_get_contents(conn->outbuf, &sz);
  1811. tt_str_op(cp1, OP_EQ,
  1812. "PROXY TCP6 fc00:dead:beef:4dad::0:29a ::1 666 42\r\n");
  1813. /* Change circ GID and see that the reported circuit ID also changes */
  1814. or_circ->global_identifier = 22;
  1815. /* check changes */
  1816. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1817. cp2 = buf_get_contents(conn->outbuf, &sz);
  1818. tt_str_op(cp1, OP_NE, cp2);
  1819. tor_free(cp1);
  1820. /* Check that GID with UINT32_MAX works. */
  1821. or_circ->global_identifier = UINT32_MAX;
  1822. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1823. cp1 = buf_get_contents(conn->outbuf, &sz);
  1824. tt_str_op(cp1, OP_EQ,
  1825. "PROXY TCP6 fc00:dead:beef:4dad::ffff:ffff ::1 65535 42\r\n");
  1826. tor_free(cp1);
  1827. /* Check that GID with UINT16_MAX works. */
  1828. or_circ->global_identifier = UINT16_MAX;
  1829. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1830. cp1 = buf_get_contents(conn->outbuf, &sz);
  1831. tt_str_op(cp1, OP_EQ,
  1832. "PROXY TCP6 fc00:dead:beef:4dad::0:ffff ::1 65535 42\r\n");
  1833. tor_free(cp1);
  1834. /* Check that GID with UINT16_MAX + 7 works. */
  1835. or_circ->global_identifier = UINT16_MAX + 7;
  1836. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1837. cp1 = buf_get_contents(conn->outbuf, &sz);
  1838. tt_str_op(cp1, OP_EQ, "PROXY TCP6 fc00:dead:beef:4dad::1:6 ::1 6 42\r\n");
  1839. done:
  1840. UNMOCK(connection_write_to_buf_impl_);
  1841. circuit_free_(TO_CIRCUIT(or_circ));
  1842. connection_free_minimal(conn);
  1843. hs_service_free(service);
  1844. tor_free(cp1);
  1845. tor_free(cp2);
  1846. }
  1847. struct testcase_t hs_service_tests[] = {
  1848. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  1849. NULL, NULL },
  1850. { "load_keys", test_load_keys, TT_FORK,
  1851. NULL, NULL },
  1852. { "client_filename_is_valid", test_client_filename_is_valid, TT_FORK,
  1853. NULL, NULL },
  1854. { "parse_authorized_client", test_parse_authorized_client, TT_FORK,
  1855. NULL, NULL },
  1856. { "load_keys_with_client_auth", test_load_keys_with_client_auth, TT_FORK,
  1857. NULL, NULL },
  1858. { "access_service", test_access_service, TT_FORK,
  1859. NULL, NULL },
  1860. { "service_intro_point", test_service_intro_point, TT_FORK,
  1861. NULL, NULL },
  1862. { "helper_functions", test_helper_functions, TT_FORK,
  1863. NULL, NULL },
  1864. { "intro_circuit_opened", test_intro_circuit_opened, TT_FORK,
  1865. NULL, NULL },
  1866. { "intro_established", test_intro_established, TT_FORK,
  1867. NULL, NULL },
  1868. { "closing_intro_circs", test_closing_intro_circs, TT_FORK,
  1869. NULL, NULL },
  1870. { "rdv_circuit_opened", test_rdv_circuit_opened, TT_FORK,
  1871. NULL, NULL },
  1872. { "introduce2", test_introduce2, TT_FORK,
  1873. NULL, NULL },
  1874. { "service_event", test_service_event, TT_FORK,
  1875. NULL, NULL },
  1876. { "rotate_descriptors", test_rotate_descriptors, TT_FORK,
  1877. NULL, NULL },
  1878. { "build_update_descriptors", test_build_update_descriptors, TT_FORK,
  1879. NULL, NULL },
  1880. { "build_descriptors", test_build_descriptors, TT_FORK,
  1881. NULL, NULL },
  1882. { "upload_descriptors", test_upload_descriptors, TT_FORK,
  1883. NULL, NULL },
  1884. { "rendezvous1_parsing", test_rendezvous1_parsing, TT_FORK,
  1885. NULL, NULL },
  1886. { "authorized_client_config_equal", test_authorized_client_config_equal,
  1887. TT_FORK, NULL, NULL },
  1888. { "export_client_circuit_id", test_export_client_circuit_id, TT_FORK,
  1889. NULL, NULL },
  1890. END_OF_TESTCASES
  1891. };