test_hs_service.c 70 KB

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