test_hs_service.c 74 KB

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