test_hs_service.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  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 service object with the identity keypair
  199. * sets and the current descriptor. Then register it to the global map.
  200. * Caller should us hs_free_all() to free this service or remove it from the
  201. * global map before freeing. */
  202. static hs_service_t *
  203. helper_create_service(void)
  204. {
  205. /* Set a service for this circuit. */
  206. hs_service_t *service = hs_service_new(get_options());
  207. tor_assert(service);
  208. service->config.version = HS_VERSION_THREE;
  209. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  210. ed25519_public_key_generate(&service->keys.identity_pk,
  211. &service->keys.identity_sk);
  212. service->desc_current = service_descriptor_new();
  213. tt_assert(service->desc_current);
  214. /* Register service to global map. */
  215. int ret = register_service(get_hs_service_map(), service);
  216. tt_int_op(ret, OP_EQ, 0);
  217. done:
  218. return service;
  219. }
  220. /* Helper: Return a newly allocated service intro point with two link
  221. * specifiers, one IPv4 and one legacy ID set to As. */
  222. static hs_service_intro_point_t *
  223. helper_create_service_ip(void)
  224. {
  225. hs_desc_link_specifier_t *ls;
  226. hs_service_intro_point_t *ip = service_intro_point_new(NULL, 0, 0);
  227. tor_assert(ip);
  228. /* Add a first unused link specifier. */
  229. ls = tor_malloc_zero(sizeof(*ls));
  230. ls->type = LS_IPV4;
  231. smartlist_add(ip->base.link_specifiers, ls);
  232. /* Add a second link specifier used by a test. */
  233. ls = tor_malloc_zero(sizeof(*ls));
  234. ls->type = LS_LEGACY_ID;
  235. memset(ls->u.legacy_id, 'A', sizeof(ls->u.legacy_id));
  236. smartlist_add(ip->base.link_specifiers, ls);
  237. return ip;
  238. }
  239. static void
  240. test_load_keys(void *arg)
  241. {
  242. int ret;
  243. char *conf = NULL;
  244. char *hsdir_v2 = tor_strdup(get_fname("hs2"));
  245. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  246. char addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  247. (void) arg;
  248. /* We'll register two services, a v2 and a v3, then we'll load keys and
  249. * validate that both are in a correct state. */
  250. hs_init();
  251. #define conf_fmt \
  252. "HiddenServiceDir %s\n" \
  253. "HiddenServiceVersion %d\n" \
  254. "HiddenServicePort 65535\n"
  255. /* v2 service. */
  256. tor_asprintf(&conf, conf_fmt, hsdir_v2, HS_VERSION_TWO);
  257. ret = helper_config_service(conf);
  258. tor_free(conf);
  259. tt_int_op(ret, OP_EQ, 0);
  260. /* This one should now be registered into the v2 list. */
  261. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 0);
  262. tt_int_op(rend_num_services(), OP_EQ, 1);
  263. /* v3 service. */
  264. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  265. ret = helper_config_service(conf);
  266. tor_free(conf);
  267. tt_int_op(ret, OP_EQ, 0);
  268. /* It's in staging? */
  269. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  270. #undef conf_fmt
  271. /* Load the keys for these. After that, the v3 service should be registered
  272. * in the global map. */
  273. hs_service_load_all_keys();
  274. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  275. hs_service_t *s = get_first_service();
  276. tt_assert(s);
  277. /* Ok we have the service object. Validate few things. */
  278. tt_assert(!tor_mem_is_zero(s->onion_address, sizeof(s->onion_address)));
  279. tt_int_op(hs_address_is_valid(s->onion_address), OP_EQ, 1);
  280. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_sk.seckey,
  281. ED25519_SECKEY_LEN));
  282. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_pk.pubkey,
  283. ED25519_PUBKEY_LEN));
  284. /* Check onion address from identity key. */
  285. hs_build_address(&s->keys.identity_pk, s->config.version, addr);
  286. tt_int_op(hs_address_is_valid(addr), OP_EQ, 1);
  287. tt_str_op(addr, OP_EQ, s->onion_address);
  288. /* Check that the is_client_auth_enabled is not set. */
  289. tt_assert(!s->config.is_client_auth_enabled);
  290. done:
  291. tor_free(hsdir_v2);
  292. tor_free(hsdir_v3);
  293. hs_free_all();
  294. }
  295. static void
  296. test_client_filename_is_valid(void *arg)
  297. {
  298. (void) arg;
  299. /* Valid file name. */
  300. tt_assert(client_filename_is_valid("a.auth"));
  301. /* Valid file name with special character. */
  302. tt_assert(client_filename_is_valid("a-.auth"));
  303. /* Invalid extension. */
  304. tt_assert(!client_filename_is_valid("a.ath"));
  305. /* Nothing before the extension. */
  306. tt_assert(!client_filename_is_valid(".auth"));
  307. done:
  308. ;
  309. }
  310. static void
  311. test_parse_authorized_client(void *arg)
  312. {
  313. hs_service_authorized_client_t *client = NULL;
  314. (void) arg;
  315. /* Valid authorized client. */
  316. client = parse_authorized_client(
  317. "descriptor:x25519:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja");
  318. tt_assert(client);
  319. /* Wrong number of fields. */
  320. tt_assert(!parse_authorized_client("a:b:c:d:e"));
  321. /* Wrong auth type. */
  322. tt_assert(!parse_authorized_client(
  323. "x:x25519:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  324. /* Wrong key type. */
  325. tt_assert(!parse_authorized_client(
  326. "descriptor:x:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  327. /* Some malformed string. */
  328. tt_assert(!parse_authorized_client("descriptor:x25519:aa=="));
  329. tt_assert(!parse_authorized_client("descriptor:"));
  330. tt_assert(!parse_authorized_client("descriptor:x25519"));
  331. tt_assert(!parse_authorized_client("descriptor:x25519:"));
  332. tt_assert(!parse_authorized_client(""));
  333. done:
  334. service_authorized_client_free(client);
  335. }
  336. static char *
  337. mock_read_file_to_str(const char *filename, int flags, struct stat *stat_out)
  338. {
  339. char *ret = NULL;
  340. (void) flags;
  341. (void) stat_out;
  342. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  343. "authorized_clients" PATH_SEPARATOR
  344. "client1.auth"))) {
  345. ret = tor_strdup("descriptor:x25519:"
  346. "dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja");
  347. goto done;
  348. }
  349. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  350. "authorized_clients" PATH_SEPARATOR
  351. "dummy.xxx"))) {
  352. ret = tor_strdup("descriptor:x25519:"
  353. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  354. goto done;
  355. }
  356. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  357. "authorized_clients" PATH_SEPARATOR
  358. "client2.auth"))) {
  359. ret = tor_strdup("descriptor:x25519:"
  360. "okoi2gml3wd6x7jganlk5d66xxyjgg24sxw4y7javx4giqr66zta");
  361. goto done;
  362. }
  363. done:
  364. return ret;
  365. }
  366. static smartlist_t *
  367. mock_tor_listdir(const char *dirname)
  368. {
  369. smartlist_t *file_list = smartlist_new();
  370. (void) dirname;
  371. smartlist_add(file_list, tor_strdup("client1.auth"));
  372. smartlist_add(file_list, tor_strdup("dummy.xxx"));
  373. smartlist_add(file_list, tor_strdup("client2.auth"));
  374. return file_list;
  375. }
  376. static void
  377. test_load_keys_with_client_auth(void *arg)
  378. {
  379. int ret;
  380. char *conf = NULL;
  381. smartlist_t *pubkey_b32_list = smartlist_new();
  382. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  383. hs_service_t *service;
  384. (void) arg;
  385. hs_init();
  386. smartlist_add(pubkey_b32_list, tor_strdup(
  387. "dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  388. smartlist_add(pubkey_b32_list, tor_strdup(
  389. "okoi2gml3wd6x7jganlk5d66xxyjgg24sxw4y7javx4giqr66zta"));
  390. #define conf_fmt \
  391. "HiddenServiceDir %s\n" \
  392. "HiddenServiceVersion %d\n" \
  393. "HiddenServicePort 65534\n"
  394. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  395. ret = helper_config_service(conf);
  396. tor_free(conf);
  397. tt_int_op(ret, OP_EQ, 0);
  398. /* It's in staging? */
  399. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  400. #undef conf_fmt
  401. MOCK(read_file_to_str, mock_read_file_to_str);
  402. MOCK(tor_listdir, mock_tor_listdir);
  403. /* Load the keys for these. After that, the v3 service should be registered
  404. * in the global map. */
  405. hs_service_load_all_keys();
  406. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  407. service = get_first_service();
  408. tt_assert(service->config.clients);
  409. tt_int_op(smartlist_len(service->config.clients), OP_EQ,
  410. smartlist_len(pubkey_b32_list));
  411. /* Test that the is_client_auth_enabled flag is set. */
  412. tt_assert(service->config.is_client_auth_enabled);
  413. /* Test that the keys in clients are correct. */
  414. SMARTLIST_FOREACH_BEGIN(pubkey_b32_list, char *, pubkey_b32) {
  415. curve25519_public_key_t pubkey;
  416. /* This flag will be set if the key is found in clients. */
  417. int is_found = 0;
  418. base32_decode((char *) pubkey.public_key, sizeof(pubkey.public_key),
  419. pubkey_b32, strlen(pubkey_b32));
  420. SMARTLIST_FOREACH_BEGIN(service->config.clients,
  421. hs_service_authorized_client_t *, client) {
  422. if (tor_memeq(&pubkey, &client->client_pk, sizeof(pubkey))) {
  423. is_found = 1;
  424. break;
  425. }
  426. } SMARTLIST_FOREACH_END(client);
  427. tt_assert(is_found);
  428. } SMARTLIST_FOREACH_END(pubkey_b32);
  429. done:
  430. if (pubkey_b32_list) {
  431. SMARTLIST_FOREACH(pubkey_b32_list, char *, s, tor_free(s));
  432. }
  433. smartlist_free(pubkey_b32_list);
  434. tor_free(hsdir_v3);
  435. hs_free_all();
  436. UNMOCK(read_file_to_str);
  437. UNMOCK(tor_listdir);
  438. }
  439. static void
  440. test_access_service(void *arg)
  441. {
  442. int ret;
  443. char *conf = NULL;
  444. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  445. hs_service_ht *global_map;
  446. hs_service_t *s = NULL;
  447. (void) arg;
  448. /* We'll register two services, a v2 and a v3, then we'll load keys and
  449. * validate that both are in a correct state. */
  450. hs_init();
  451. #define conf_fmt \
  452. "HiddenServiceDir %s\n" \
  453. "HiddenServiceVersion %d\n" \
  454. "HiddenServicePort 65535\n"
  455. /* v3 service. */
  456. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  457. ret = helper_config_service(conf);
  458. tor_free(conf);
  459. tt_int_op(ret, OP_EQ, 0);
  460. /* It's in staging? */
  461. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  462. /* 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. s = get_first_service();
  467. tt_assert(s);
  468. global_map = get_hs_service_map();
  469. tt_assert(global_map);
  470. /* From here, we'll try the service accessors. */
  471. hs_service_t *query = find_service(global_map, &s->keys.identity_pk);
  472. tt_assert(query);
  473. tt_mem_op(query, OP_EQ, s, sizeof(hs_service_t));
  474. /* Remove service, check if it actually works and then put it back. */
  475. remove_service(global_map, s);
  476. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  477. query = find_service(global_map, &s->keys.identity_pk);
  478. tt_ptr_op(query, OP_EQ, NULL);
  479. /* Register back the service in the map. */
  480. ret = register_service(global_map, s);
  481. tt_int_op(ret, OP_EQ, 0);
  482. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  483. /* Twice should fail. */
  484. ret = register_service(global_map, s);
  485. tt_int_op(ret, OP_EQ, -1);
  486. /* Remove service from map so we don't double free on cleanup. */
  487. remove_service(global_map, s);
  488. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  489. query = find_service(global_map, &s->keys.identity_pk);
  490. tt_ptr_op(query, OP_EQ, NULL);
  491. /* Let's try to remove twice for fun. */
  492. setup_full_capture_of_logs(LOG_WARN);
  493. remove_service(global_map, s);
  494. expect_log_msg_containing("Could not find service in the global map");
  495. teardown_capture_of_logs();
  496. done:
  497. hs_service_free(s);
  498. tor_free(hsdir_v3);
  499. hs_free_all();
  500. }
  501. /** Test that we can create intro point objects, index them and find them */
  502. static void
  503. test_service_intro_point(void *arg)
  504. {
  505. hs_service_t *service = NULL;
  506. hs_service_intro_point_t *ip = NULL;
  507. (void) arg;
  508. /* Test simple creation of an object. */
  509. {
  510. time_t now = time(NULL);
  511. ip = helper_create_service_ip();
  512. tt_assert(ip);
  513. /* Make sure the authentication keypair is not zeroes. */
  514. tt_int_op(tor_mem_is_zero((const char *) &ip->auth_key_kp,
  515. sizeof(ed25519_keypair_t)), OP_EQ, 0);
  516. /* The introduce2_max MUST be in that range. */
  517. tt_u64_op(ip->introduce2_max, OP_GE,
  518. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  519. tt_u64_op(ip->introduce2_max, OP_LE,
  520. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  521. /* Time to expire MUST also be in that range. We subtract 500 seconds
  522. * because there could be a gap between setting now and the time taken in
  523. * service_intro_point_new. On ARM and other older CPUs, it can be
  524. * surprisingly slow... */
  525. tt_u64_op(ip->time_to_expire, OP_GE,
  526. now + INTRO_POINT_LIFETIME_MIN_SECONDS - 500);
  527. /* We add 500 seconds, because this time we're testing against the
  528. * maximum allowed time. */
  529. tt_u64_op(ip->time_to_expire, OP_LE,
  530. now + INTRO_POINT_LIFETIME_MAX_SECONDS + 500);
  531. tt_assert(ip->replay_cache);
  532. tt_assert(ip->base.link_specifiers);
  533. /* By default, this is NOT a legacy object. */
  534. tt_int_op(ip->base.is_only_legacy, OP_EQ, 0);
  535. }
  536. /* Test functions that uses a service intropoints map with that previously
  537. * created object (non legacy). */
  538. {
  539. ed25519_public_key_t garbage = { {0} };
  540. hs_service_intro_point_t *query;
  541. service = hs_service_new(get_options());
  542. tt_assert(service);
  543. service->desc_current = service_descriptor_new();
  544. tt_assert(service->desc_current);
  545. /* Add intropoint to descriptor map. */
  546. service_intro_point_add(service->desc_current->intro_points.map, ip);
  547. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  548. tt_mem_op(query, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  549. query = service_intro_point_find(service, &garbage);
  550. tt_ptr_op(query, OP_EQ, NULL);
  551. /* While at it, can I find the descriptor with the intro point? */
  552. hs_service_descriptor_t *desc_lookup =
  553. service_desc_find_by_intro(service, ip);
  554. tt_mem_op(service->desc_current, OP_EQ, desc_lookup,
  555. sizeof(hs_service_descriptor_t));
  556. /* Remove object from service descriptor and make sure it is out. */
  557. service_intro_point_remove(service, ip);
  558. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  559. tt_ptr_op(query, OP_EQ, NULL);
  560. }
  561. done:
  562. /* If the test succeed, this object is no longer referenced in the service
  563. * so we can free it without use after free. Else, it might explode because
  564. * it's still in the service descriptor map. */
  565. service_intro_point_free(ip);
  566. hs_service_free(service);
  567. }
  568. static node_t mock_node;
  569. static const node_t *
  570. mock_node_get_by_id(const char *digest)
  571. {
  572. (void) digest;
  573. memset(mock_node.identity, 'A', DIGEST_LEN);
  574. /* Only return the matchin identity of As */
  575. if (!tor_memcmp(mock_node.identity, digest, DIGEST_LEN)) {
  576. return &mock_node;
  577. }
  578. return NULL;
  579. }
  580. static void
  581. test_helper_functions(void *arg)
  582. {
  583. int ret;
  584. hs_service_t *service = NULL;
  585. hs_service_intro_point_t *ip = NULL;
  586. hs_ident_circuit_t ident;
  587. (void) arg;
  588. MOCK(node_get_by_id, mock_node_get_by_id);
  589. hs_service_init();
  590. service = helper_create_service();
  591. ip = helper_create_service_ip();
  592. /* Immediately add the intro point to the service so the free service at the
  593. * end cleans it as well. */
  594. service_intro_point_add(service->desc_current->intro_points.map, ip);
  595. /* Setup the circuit identifier. */
  596. ed25519_pubkey_copy(&ident.intro_auth_pk, &ip->auth_key_kp.pubkey);
  597. ed25519_pubkey_copy(&ident.identity_pk, &service->keys.identity_pk);
  598. /* Testing get_objects_from_ident(). */
  599. {
  600. hs_service_t *s_lookup = NULL;
  601. hs_service_intro_point_t *ip_lookup = NULL;
  602. hs_service_descriptor_t *desc_lookup = NULL;
  603. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  604. tt_mem_op(s_lookup, OP_EQ, service, sizeof(hs_service_t));
  605. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  606. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  607. sizeof(hs_service_descriptor_t));
  608. /* Reset */
  609. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  610. /* NULL parameter should work. */
  611. get_objects_from_ident(&ident, NULL, &ip_lookup, &desc_lookup);
  612. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  613. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  614. sizeof(hs_service_descriptor_t));
  615. /* Reset. */
  616. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  617. /* Break the ident and we should find nothing. */
  618. memset(&ident, 0, sizeof(ident));
  619. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  620. tt_ptr_op(s_lookup, OP_EQ, NULL);
  621. tt_ptr_op(ip_lookup, OP_EQ, NULL);
  622. tt_ptr_op(desc_lookup, OP_EQ, NULL);
  623. }
  624. /* Testing get_node_from_intro_point() */
  625. {
  626. const node_t *node = get_node_from_intro_point(ip);
  627. tt_ptr_op(node, OP_EQ, &mock_node);
  628. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  629. hs_desc_link_specifier_t *, ls) {
  630. if (ls->type == LS_LEGACY_ID) {
  631. /* Change legacy id in link specifier which is not the mock node. */
  632. memset(ls->u.legacy_id, 'B', sizeof(ls->u.legacy_id));
  633. }
  634. } SMARTLIST_FOREACH_END(ls);
  635. node = get_node_from_intro_point(ip);
  636. tt_ptr_op(node, OP_EQ, NULL);
  637. }
  638. /* Testing can_service_launch_intro_circuit() */
  639. {
  640. time_t now = time(NULL);
  641. /* Put the start of the retry period back in time, we should be allowed.
  642. * to launch intro circuit. */
  643. service->state.num_intro_circ_launched = 2;
  644. service->state.intro_circ_retry_started_time =
  645. (now - INTRO_CIRC_RETRY_PERIOD - 1);
  646. ret = can_service_launch_intro_circuit(service, now);
  647. tt_int_op(ret, OP_EQ, 1);
  648. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  649. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  650. /* Call it again, we should still be allowed because we are under
  651. * MAX_INTRO_CIRCS_PER_PERIOD which been set to 0 previously. */
  652. ret = can_service_launch_intro_circuit(service, now);
  653. tt_int_op(ret, OP_EQ, 1);
  654. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  655. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  656. /* Too many intro circuit launched means we are not allowed. */
  657. service->state.num_intro_circ_launched = 20;
  658. ret = can_service_launch_intro_circuit(service, now);
  659. tt_int_op(ret, OP_EQ, 0);
  660. }
  661. /* Testing intro_point_should_expire(). */
  662. {
  663. time_t now = time(NULL);
  664. /* Just some basic test of the current state. */
  665. tt_u64_op(ip->introduce2_max, OP_GE,
  666. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  667. tt_u64_op(ip->introduce2_max, OP_LE,
  668. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  669. tt_u64_op(ip->time_to_expire, OP_GE,
  670. now + INTRO_POINT_LIFETIME_MIN_SECONDS);
  671. tt_u64_op(ip->time_to_expire, OP_LE,
  672. now + INTRO_POINT_LIFETIME_MAX_SECONDS);
  673. /* This newly created IP from above shouldn't expire now. */
  674. ret = intro_point_should_expire(ip, now);
  675. tt_int_op(ret, OP_EQ, 0);
  676. /* Maximum number of INTRODUCE2 cell reached, it should expire. */
  677. ip->introduce2_count = INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS + 1;
  678. ret = intro_point_should_expire(ip, now);
  679. tt_int_op(ret, OP_EQ, 1);
  680. ip->introduce2_count = 0;
  681. /* It should expire if time to expire has been reached. */
  682. ip->time_to_expire = now - 1000;
  683. ret = intro_point_should_expire(ip, now);
  684. tt_int_op(ret, OP_EQ, 1);
  685. }
  686. done:
  687. /* This will free the service and all objects associated to it. */
  688. hs_service_free_all();
  689. UNMOCK(node_get_by_id);
  690. }
  691. /** Test that we do the right operations when an intro circuit opens */
  692. static void
  693. test_intro_circuit_opened(void *arg)
  694. {
  695. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  696. hs_service_t *service;
  697. origin_circuit_t *circ = NULL;
  698. (void) arg;
  699. hs_init();
  700. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  701. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  702. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  703. flags);
  704. /* No service associated with this circuit. */
  705. setup_full_capture_of_logs(LOG_WARN);
  706. hs_service_circuit_has_opened(circ);
  707. expect_log_msg_containing("Unknown service identity key");
  708. teardown_capture_of_logs();
  709. /* Set a service for this circuit. */
  710. {
  711. service = helper_create_service();
  712. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  713. &service->keys.identity_pk);
  714. /* No intro point associated with this circuit. */
  715. setup_full_capture_of_logs(LOG_WARN);
  716. hs_service_circuit_has_opened(circ);
  717. expect_log_msg_containing("Unknown introduction point auth key");
  718. teardown_capture_of_logs();
  719. }
  720. /* Set an IP object now for this circuit. */
  721. {
  722. hs_service_intro_point_t *ip = helper_create_service_ip();
  723. service_intro_point_add(service->desc_current->intro_points.map, ip);
  724. /* Update ident to contain the intro point auth key. */
  725. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  726. &ip->auth_key_kp.pubkey);
  727. }
  728. /* This one should go all the way. */
  729. setup_full_capture_of_logs(LOG_INFO);
  730. hs_service_circuit_has_opened(circ);
  731. expect_log_msg_containing("Introduction circuit 0 established for service");
  732. teardown_capture_of_logs();
  733. done:
  734. circuit_free_(TO_CIRCUIT(circ));
  735. hs_free_all();
  736. UNMOCK(circuit_mark_for_close_);
  737. UNMOCK(relay_send_command_from_edge_);
  738. }
  739. /** Test the operations we do on a circuit after we learn that we successfully
  740. * established an intro point on it */
  741. static void
  742. test_intro_established(void *arg)
  743. {
  744. int ret;
  745. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  746. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  747. origin_circuit_t *circ = NULL;
  748. hs_service_t *service;
  749. hs_service_intro_point_t *ip = NULL;
  750. (void) arg;
  751. hs_init();
  752. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  753. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  754. flags);
  755. tt_assert(circ);
  756. /* Test a wrong purpose. */
  757. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  758. setup_full_capture_of_logs(LOG_WARN);
  759. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  760. tt_int_op(ret, OP_EQ, -1);
  761. expect_log_msg_containing("Received an INTRO_ESTABLISHED cell on a "
  762. "non introduction circuit of purpose");
  763. teardown_capture_of_logs();
  764. /* Back to normal. */
  765. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  766. /* No service associated to it. */
  767. setup_full_capture_of_logs(LOG_WARN);
  768. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  769. tt_int_op(ret, OP_EQ, -1);
  770. expect_log_msg_containing("Unknown service identity key");
  771. teardown_capture_of_logs();
  772. /* Set a service for this circuit. */
  773. service = helper_create_service();
  774. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  775. &service->keys.identity_pk);
  776. /* No introduction point associated to it. */
  777. setup_full_capture_of_logs(LOG_WARN);
  778. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  779. tt_int_op(ret, OP_EQ, -1);
  780. expect_log_msg_containing("Introduction circuit established without an "
  781. "intro point object on circuit");
  782. teardown_capture_of_logs();
  783. /* Set an IP object now for this circuit. */
  784. {
  785. ip = helper_create_service_ip();
  786. service_intro_point_add(service->desc_current->intro_points.map, ip);
  787. /* Update ident to contain the intro point auth key. */
  788. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  789. &ip->auth_key_kp.pubkey);
  790. }
  791. /* Send an empty payload. INTRO_ESTABLISHED cells are basically zeroes. */
  792. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  793. tt_int_op(ret, OP_EQ, 0);
  794. tt_u64_op(ip->circuit_established, OP_EQ, 1);
  795. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_INTRO);
  796. done:
  797. if (circ)
  798. circuit_free_(TO_CIRCUIT(circ));
  799. hs_free_all();
  800. UNMOCK(circuit_mark_for_close_);
  801. }
  802. /** Check the operations we do on a rendezvous circuit after we learn it's
  803. * open */
  804. static void
  805. test_rdv_circuit_opened(void *arg)
  806. {
  807. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  808. origin_circuit_t *circ = NULL;
  809. hs_service_t *service;
  810. (void) arg;
  811. hs_init();
  812. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  813. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  814. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND, flags);
  815. crypto_rand((char *) circ->hs_ident->rendezvous_cookie, REND_COOKIE_LEN);
  816. crypto_rand((char *) circ->hs_ident->rendezvous_handshake_info,
  817. sizeof(circ->hs_ident->rendezvous_handshake_info));
  818. /* No service associated with this circuit. */
  819. setup_full_capture_of_logs(LOG_WARN);
  820. hs_service_circuit_has_opened(circ);
  821. expect_log_msg_containing("Unknown service identity key");
  822. teardown_capture_of_logs();
  823. /* This should be set to a non zero timestamp. */
  824. tt_u64_op(TO_CIRCUIT(circ)->timestamp_dirty, OP_NE, 0);
  825. /* Set a service for this circuit. */
  826. service = helper_create_service();
  827. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  828. &service->keys.identity_pk);
  829. /* Should be all good. */
  830. hs_service_circuit_has_opened(circ);
  831. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  832. done:
  833. circuit_free_(TO_CIRCUIT(circ));
  834. hs_free_all();
  835. UNMOCK(circuit_mark_for_close_);
  836. UNMOCK(relay_send_command_from_edge_);
  837. }
  838. static void
  839. mock_assert_circuit_ok(const circuit_t *c)
  840. {
  841. (void) c;
  842. return;
  843. }
  844. /** Test for the general mechanism for closing intro circs.
  845. * Also a way to identify that #23603 has been fixed. */
  846. static void
  847. test_closing_intro_circs(void *arg)
  848. {
  849. hs_service_t *service = NULL;
  850. hs_service_intro_point_t *ip = NULL, *entry = NULL;
  851. origin_circuit_t *intro_circ = NULL, *tmp_circ;
  852. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  853. (void) arg;
  854. MOCK(assert_circuit_ok, mock_assert_circuit_ok);
  855. hs_init();
  856. /* Initialize service */
  857. service = helper_create_service();
  858. /* Initialize intro point */
  859. ip = helper_create_service_ip();
  860. tt_assert(ip);
  861. service_intro_point_add(service->desc_current->intro_points.map, ip);
  862. /* Initialize intro circuit */
  863. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  864. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk,
  865. HS_IDENT_CIRCUIT_INTRO);
  866. /* Register circuit in the circuitmap . */
  867. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  868. &ip->auth_key_kp.pubkey);
  869. tmp_circ =
  870. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  871. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  872. /* Pretend that intro point has failed too much */
  873. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES+1;
  874. /* Now pretend we are freeing this intro circuit. We want to see that our
  875. * destructor is not gonna kill our intro point structure since that's the
  876. * job of the cleanup routine. */
  877. circuit_free_(TO_CIRCUIT(intro_circ));
  878. intro_circ = NULL;
  879. entry = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  880. tt_assert(entry);
  881. /* The free should also remove the circuit from the circuitmap. */
  882. tmp_circ =
  883. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  884. tt_assert(!tmp_circ);
  885. /* Now pretend that a new intro point circ was launched and opened. Check
  886. * that the intro point will be established correctly. */
  887. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  888. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk,
  889. HS_IDENT_CIRCUIT_INTRO);
  890. ed25519_pubkey_copy(&intro_circ->hs_ident->intro_auth_pk,
  891. &ip->auth_key_kp.pubkey);
  892. /* Register circuit in the circuitmap . */
  893. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  894. &ip->auth_key_kp.pubkey);
  895. tmp_circ =
  896. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  897. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  898. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_EQ, 0);
  899. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  900. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_NE, 0);
  901. /* At this point, we should not be able to find it in the circuitmap. */
  902. tmp_circ =
  903. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  904. tt_assert(!tmp_circ);
  905. done:
  906. if (intro_circ) {
  907. circuit_free_(TO_CIRCUIT(intro_circ));
  908. }
  909. /* Frees the service object. */
  910. hs_free_all();
  911. UNMOCK(assert_circuit_ok);
  912. }
  913. /** Test sending and receiving introduce2 cells */
  914. static void
  915. test_introduce2(void *arg)
  916. {
  917. int ret;
  918. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  919. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  920. origin_circuit_t *circ = NULL;
  921. hs_service_t *service;
  922. hs_service_intro_point_t *ip = NULL;
  923. (void) arg;
  924. hs_init();
  925. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  926. MOCK(get_or_state,
  927. get_or_state_replacement);
  928. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  929. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  930. tt_assert(circ);
  931. /* Test a wrong purpose. */
  932. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  933. setup_full_capture_of_logs(LOG_WARN);
  934. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  935. tt_int_op(ret, OP_EQ, -1);
  936. expect_log_msg_containing("Received an INTRODUCE2 cell on a "
  937. "non introduction circuit of purpose");
  938. teardown_capture_of_logs();
  939. /* Back to normal. */
  940. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  941. /* No service associated to it. */
  942. setup_full_capture_of_logs(LOG_WARN);
  943. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  944. tt_int_op(ret, OP_EQ, -1);
  945. expect_log_msg_containing("Unknown service identity key");
  946. teardown_capture_of_logs();
  947. /* Set a service for this circuit. */
  948. service = helper_create_service();
  949. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  950. &service->keys.identity_pk);
  951. /* No introduction point associated to it. */
  952. setup_full_capture_of_logs(LOG_WARN);
  953. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  954. tt_int_op(ret, OP_EQ, -1);
  955. expect_log_msg_containing("Unknown introduction auth key when handling "
  956. "an INTRODUCE2 cell on circuit");
  957. teardown_capture_of_logs();
  958. /* Set an IP object now for this circuit. */
  959. {
  960. ip = helper_create_service_ip();
  961. service_intro_point_add(service->desc_current->intro_points.map, ip);
  962. /* Update ident to contain the intro point auth key. */
  963. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  964. &ip->auth_key_kp.pubkey);
  965. }
  966. /* This will fail because receiving an INTRODUCE2 cell implies a valid cell
  967. * and then launching circuits so let's not do that and instead test that
  968. * behaviour differently. */
  969. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  970. tt_int_op(ret, OP_EQ, -1);
  971. tt_u64_op(ip->introduce2_count, OP_EQ, 0);
  972. done:
  973. or_state_free(dummy_state);
  974. dummy_state = NULL;
  975. if (circ)
  976. circuit_free_(TO_CIRCUIT(circ));
  977. hs_free_all();
  978. UNMOCK(circuit_mark_for_close_);
  979. }
  980. /** Test basic hidden service housekeeping operations (maintaining intro
  981. * points, etc) */
  982. static void
  983. test_service_event(void *arg)
  984. {
  985. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  986. time_t now = time(NULL);
  987. hs_service_t *service;
  988. origin_circuit_t *circ = NULL;
  989. (void) arg;
  990. hs_init();
  991. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  992. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  993. /* Set a service for this circuit. */
  994. service = helper_create_service();
  995. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  996. &service->keys.identity_pk);
  997. /* Currently this consists of cleaning invalid intro points. So adding IPs
  998. * here that should get cleaned up. */
  999. {
  1000. hs_service_intro_point_t *ip = helper_create_service_ip();
  1001. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1002. /* This run will remove the IP because we have no circuits nor node_t
  1003. * associated with it. */
  1004. run_housekeeping_event(now);
  1005. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1006. OP_EQ, 0);
  1007. /* We'll trigger a removal because we've reached our maximum amount of
  1008. * times we should retry a circuit. For this, we need to have a node_t
  1009. * that matches the identity of this IP. */
  1010. routerinfo_t ri;
  1011. memset(&ri, 0, sizeof(ri));
  1012. ip = helper_create_service_ip();
  1013. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1014. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1015. /* This triggers a node_t creation. */
  1016. tt_assert(nodelist_set_routerinfo(&ri, NULL));
  1017. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  1018. run_housekeeping_event(now);
  1019. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1020. OP_EQ, 0);
  1021. /* No removal but no circuit so this means the IP object will stay in the
  1022. * descriptor map so we can retry it. */
  1023. ip = helper_create_service_ip();
  1024. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1025. ip->circuit_established = 1; /* We'll test that, it MUST be 0 after. */
  1026. run_housekeeping_event(now);
  1027. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1028. OP_EQ, 1);
  1029. /* Remove the IP object at once for the next test. */
  1030. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  1031. run_housekeeping_event(now);
  1032. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1033. OP_EQ, 0);
  1034. /* Now, we'll create an IP with a registered circuit. The IP object
  1035. * shouldn't go away. */
  1036. ip = helper_create_service_ip();
  1037. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1038. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  1039. &ip->auth_key_kp.pubkey);
  1040. hs_circuitmap_register_intro_circ_v3_service_side(
  1041. circ, &ip->auth_key_kp.pubkey);
  1042. run_housekeeping_event(now);
  1043. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1044. OP_EQ, 1);
  1045. /* We'll mangle the IP object to expire. */
  1046. ip->time_to_expire = now;
  1047. run_housekeeping_event(now);
  1048. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1049. OP_EQ, 0);
  1050. }
  1051. done:
  1052. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  1053. circuit_free_(TO_CIRCUIT(circ));
  1054. hs_free_all();
  1055. UNMOCK(circuit_mark_for_close_);
  1056. }
  1057. /** Test that we rotate descriptors correctly. */
  1058. static void
  1059. test_rotate_descriptors(void *arg)
  1060. {
  1061. int ret;
  1062. time_t next_rotation_time, now;
  1063. hs_service_t *service;
  1064. hs_service_descriptor_t *desc_next;
  1065. (void) arg;
  1066. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1067. hs_init();
  1068. MOCK(get_or_state, get_or_state_replacement);
  1069. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  1070. MOCK(networkstatus_get_live_consensus,
  1071. mock_networkstatus_get_live_consensus);
  1072. /* Descriptor rotation happens with a consensus with a new SRV. */
  1073. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1074. &mock_ns.valid_after);
  1075. tt_int_op(ret, OP_EQ, 0);
  1076. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1077. &mock_ns.fresh_until);
  1078. tt_int_op(ret, OP_EQ, 0);
  1079. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1080. update_approx_time(mock_ns.valid_after+1);
  1081. now = mock_ns.valid_after+1;
  1082. /* Create a service with a default descriptor and state. It's added to the
  1083. * global map. */
  1084. service = helper_create_service();
  1085. service_descriptor_free(service->desc_current);
  1086. service->desc_current = NULL;
  1087. /* This triggers a build for both descriptors. The time now is only used in
  1088. * the descriptor certificate which is important to be now else the decoding
  1089. * will complain that the cert has expired if we use valid_after. */
  1090. build_all_descriptors(now);
  1091. tt_assert(service->desc_current);
  1092. tt_assert(service->desc_next);
  1093. /* Tweak our service next rotation time so we can use a custom time. */
  1094. service->state.next_rotation_time = next_rotation_time =
  1095. mock_ns.valid_after + (11 * 60 * 60);
  1096. /* Nothing should happen, we are not at a new SRV. Our next rotation time
  1097. * should be untouched. */
  1098. rotate_all_descriptors(mock_ns.valid_after);
  1099. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1100. tt_assert(service->desc_current);
  1101. tt_assert(service->desc_next);
  1102. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1103. hs_get_previous_time_period_num(0));
  1104. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1105. hs_get_time_period_num(0));
  1106. /* Keep a reference so we can compare it after rotation to the current. */
  1107. desc_next = service->desc_next;
  1108. /* Going right after a new SRV. */
  1109. ret = parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC",
  1110. &mock_ns.valid_after);
  1111. tt_int_op(ret, OP_EQ, 0);
  1112. ret = parse_rfc1123_time("Sat, 27 Oct 1985 02:00:00 UTC",
  1113. &mock_ns.fresh_until);
  1114. tt_int_op(ret, OP_EQ, 0);
  1115. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1116. update_approx_time(mock_ns.valid_after+1);
  1117. now = mock_ns.valid_after+1;
  1118. /* Note down what to expect for the next rotation time which is 01:00 + 23h
  1119. * meaning 00:00:00. */
  1120. next_rotation_time = mock_ns.valid_after + (23 * 60 * 60);
  1121. /* We should have our next rotation time modified, our current descriptor
  1122. * cleaned up and the next descriptor becoming the current. */
  1123. rotate_all_descriptors(mock_ns.valid_after);
  1124. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1125. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1126. tt_assert(service->desc_next == NULL);
  1127. /* A second time should do nothing. */
  1128. rotate_all_descriptors(mock_ns.valid_after);
  1129. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1130. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1131. tt_assert(service->desc_next == NULL);
  1132. build_all_descriptors(now);
  1133. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1134. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1135. hs_get_time_period_num(0));
  1136. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1137. hs_get_next_time_period_num(0));
  1138. tt_assert(service->desc_next);
  1139. done:
  1140. hs_free_all();
  1141. UNMOCK(get_or_state);
  1142. UNMOCK(circuit_mark_for_close_);
  1143. UNMOCK(networkstatus_get_live_consensus);
  1144. }
  1145. /** Test building descriptors: picking intro points, setting up their link
  1146. * specifiers, etc. */
  1147. static void
  1148. test_build_update_descriptors(void *arg)
  1149. {
  1150. int ret;
  1151. time_t now = time(NULL);
  1152. node_t *node;
  1153. hs_service_t *service;
  1154. hs_service_intro_point_t *ip_cur, *ip_next;
  1155. routerinfo_t ri;
  1156. (void) arg;
  1157. hs_init();
  1158. MOCK(get_or_state,
  1159. get_or_state_replacement);
  1160. MOCK(networkstatus_get_live_consensus,
  1161. mock_networkstatus_get_live_consensus);
  1162. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1163. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  1164. &mock_ns.valid_after);
  1165. tt_int_op(ret, OP_EQ, 0);
  1166. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  1167. &mock_ns.fresh_until);
  1168. tt_int_op(ret, OP_EQ, 0);
  1169. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1170. update_approx_time(mock_ns.valid_after+1);
  1171. now = mock_ns.valid_after+1;
  1172. /* Create a service without a current descriptor to trigger a build. */
  1173. service = helper_create_service();
  1174. tt_assert(service);
  1175. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1176. service_descriptor_free(service->desc_current);
  1177. service->desc_current = NULL;
  1178. /* We have a fresh service so this should trigger a build for both
  1179. * descriptors for specific time period that we'll test. */
  1180. build_all_descriptors(now);
  1181. /* Check *current* descriptor. */
  1182. tt_assert(service->desc_current);
  1183. tt_assert(service->desc_current->desc);
  1184. tt_assert(service->desc_current->intro_points.map);
  1185. /* The current time period is the one expected when starting at 03:00. */
  1186. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1187. hs_get_time_period_num(0));
  1188. /* This should be untouched, the update descriptor process changes it. */
  1189. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1190. /* Check *next* descriptor. */
  1191. tt_assert(service->desc_next);
  1192. tt_assert(service->desc_next->desc);
  1193. tt_assert(service->desc_next->intro_points.map);
  1194. tt_assert(service->desc_current != service->desc_next);
  1195. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1196. hs_get_next_time_period_num(0));
  1197. /* This should be untouched, the update descriptor process changes it. */
  1198. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1199. /* Time to test the update of those descriptors. At first, we have no node
  1200. * in the routerlist so this will find NO suitable node for the IPs. */
  1201. setup_full_capture_of_logs(LOG_INFO);
  1202. update_all_descriptors(now);
  1203. expect_log_msg_containing("Unable to find a suitable node to be an "
  1204. "introduction point for service");
  1205. teardown_capture_of_logs();
  1206. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1207. OP_EQ, 0);
  1208. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1209. OP_EQ, 0);
  1210. /* Now, we'll setup a node_t. */
  1211. {
  1212. tor_addr_t ipv4_addr;
  1213. curve25519_secret_key_t curve25519_secret_key;
  1214. memset(&ri, 0, sizeof(routerinfo_t));
  1215. tor_addr_parse(&ipv4_addr, "127.0.0.1");
  1216. ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  1217. ri.or_port = 1337;
  1218. ri.purpose = ROUTER_PURPOSE_GENERAL;
  1219. /* Ugly yes but we never free the "ri" object so this just makes things
  1220. * easier. */
  1221. ri.protocol_list = (char *) "HSDir=1-2 LinkAuth=3";
  1222. summarize_protover_flags(&ri.pv, ri.protocol_list, NULL);
  1223. ret = curve25519_secret_key_generate(&curve25519_secret_key, 0);
  1224. tt_int_op(ret, OP_EQ, 0);
  1225. ri.onion_curve25519_pkey =
  1226. tor_malloc_zero(sizeof(curve25519_public_key_t));
  1227. ri.onion_pkey = tor_malloc_zero(140);
  1228. curve25519_public_key_generate(ri.onion_curve25519_pkey,
  1229. &curve25519_secret_key);
  1230. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1231. /* Setup ed25519 identity */
  1232. ed25519_keypair_t kp1;
  1233. ed25519_keypair_generate(&kp1, 0);
  1234. ri.cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  1235. tt_assert(ri.cache_info.signing_key_cert);
  1236. ed25519_pubkey_copy(&ri.cache_info.signing_key_cert->signing_key,
  1237. &kp1.pubkey);
  1238. nodelist_set_routerinfo(&ri, NULL);
  1239. node = node_get_mutable_by_id(ri.cache_info.identity_digest);
  1240. tt_assert(node);
  1241. node->is_running = node->is_valid = node->is_fast = node->is_stable = 1;
  1242. }
  1243. /* We have to set this, or the lack of microdescriptors for these
  1244. * nodes will make them unusable. */
  1245. get_options_mutable()->UseMicrodescriptors = 0;
  1246. /* We expect to pick only one intro point from the node above. */
  1247. setup_full_capture_of_logs(LOG_INFO);
  1248. update_all_descriptors(now);
  1249. tor_free(node->ri->onion_curve25519_pkey); /* Avoid memleak. */
  1250. tor_free(node->ri->cache_info.signing_key_cert);
  1251. tor_free(node->ri->onion_pkey);
  1252. expect_log_msg_containing("just picked 1 intro points and wanted 3 for next "
  1253. "descriptor. It currently has 0 intro points. "
  1254. "Launching ESTABLISH_INTRO circuit shortly.");
  1255. teardown_capture_of_logs();
  1256. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1257. OP_EQ, 1);
  1258. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1259. OP_EQ, 1);
  1260. /* Get the IP object. Because we don't have the auth key of the IP, we can't
  1261. * query it so get the first element in the map. */
  1262. {
  1263. void *obj = NULL;
  1264. const uint8_t *key;
  1265. digest256map_iter_t *iter =
  1266. digest256map_iter_init(service->desc_current->intro_points.map);
  1267. digest256map_iter_get(iter, &key, &obj);
  1268. tt_assert(obj);
  1269. ip_cur = obj;
  1270. /* Get also the IP from the next descriptor. We'll make sure it's not the
  1271. * same object as in the current descriptor. */
  1272. iter = digest256map_iter_init(service->desc_next->intro_points.map);
  1273. digest256map_iter_get(iter, &key, &obj);
  1274. tt_assert(obj);
  1275. ip_next = obj;
  1276. }
  1277. tt_mem_op(ip_cur, OP_NE, ip_next, sizeof(hs_desc_intro_point_t));
  1278. /* We won't test the service IP object because there is a specific test
  1279. * already for this but we'll make sure that the state is coherent.*/
  1280. /* Three link specifiers are mandatoy so make sure we do have them. */
  1281. tt_int_op(smartlist_len(ip_cur->base.link_specifiers), OP_EQ, 3);
  1282. /* Make sure we have a valid encryption keypair generated when we pick an
  1283. * intro point in the update process. */
  1284. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.seckey.secret_key,
  1285. CURVE25519_SECKEY_LEN));
  1286. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.pubkey.public_key,
  1287. CURVE25519_PUBKEY_LEN));
  1288. tt_u64_op(ip_cur->time_to_expire, OP_GE, now +
  1289. INTRO_POINT_LIFETIME_MIN_SECONDS);
  1290. tt_u64_op(ip_cur->time_to_expire, OP_LE, now +
  1291. INTRO_POINT_LIFETIME_MAX_SECONDS);
  1292. /* Now, we will try to set up a service after a new time period has started
  1293. * and see if it behaves as expected. */
  1294. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1295. &mock_ns.valid_after);
  1296. tt_int_op(ret, OP_EQ, 0);
  1297. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1298. &mock_ns.fresh_until);
  1299. tt_int_op(ret, OP_EQ, 0);
  1300. update_approx_time(mock_ns.valid_after+1);
  1301. now = mock_ns.valid_after+1;
  1302. /* Create a service without a current descriptor to trigger a build. */
  1303. service = helper_create_service();
  1304. tt_assert(service);
  1305. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1306. service_descriptor_free(service->desc_current);
  1307. service->desc_current = NULL;
  1308. /* We have a fresh service so this should trigger a build for both
  1309. * descriptors for specific time period that we'll test. */
  1310. build_all_descriptors(now);
  1311. /* Check *current* descriptor. */
  1312. tt_assert(service->desc_current);
  1313. tt_assert(service->desc_current->desc);
  1314. tt_assert(service->desc_current->intro_points.map);
  1315. /* This should be for the previous time period. */
  1316. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1317. hs_get_previous_time_period_num(0));
  1318. /* This should be untouched, the update descriptor process changes it. */
  1319. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1320. /* Check *next* descriptor. */
  1321. tt_assert(service->desc_next);
  1322. tt_assert(service->desc_next->desc);
  1323. tt_assert(service->desc_next->intro_points.map);
  1324. tt_assert(service->desc_current != service->desc_next);
  1325. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1326. hs_get_time_period_num(0));
  1327. /* This should be untouched, the update descriptor process changes it. */
  1328. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1329. /* Let's remove the next descriptor to simulate a rotation. */
  1330. service_descriptor_free(service->desc_next);
  1331. service->desc_next = NULL;
  1332. build_all_descriptors(now);
  1333. /* Check *next* descriptor. */
  1334. tt_assert(service->desc_next);
  1335. tt_assert(service->desc_next->desc);
  1336. tt_assert(service->desc_next->intro_points.map);
  1337. tt_assert(service->desc_current != service->desc_next);
  1338. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1339. hs_get_next_time_period_num(0));
  1340. /* This should be untouched, the update descriptor process changes it. */
  1341. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1342. done:
  1343. hs_free_all();
  1344. nodelist_free_all();
  1345. }
  1346. static void
  1347. test_upload_descriptors(void *arg)
  1348. {
  1349. int ret;
  1350. time_t now;
  1351. hs_service_t *service;
  1352. (void) arg;
  1353. hs_init();
  1354. MOCK(get_or_state,
  1355. get_or_state_replacement);
  1356. MOCK(networkstatus_get_live_consensus,
  1357. mock_networkstatus_get_live_consensus);
  1358. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1359. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1360. &mock_ns.valid_after);
  1361. tt_int_op(ret, OP_EQ, 0);
  1362. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1363. &mock_ns.fresh_until);
  1364. tt_int_op(ret, OP_EQ, 0);
  1365. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1366. update_approx_time(mock_ns.valid_after+1);
  1367. now = mock_ns.valid_after+1;
  1368. /* Create a service with no descriptor. It's added to the global map. */
  1369. service = hs_service_new(get_options());
  1370. tt_assert(service);
  1371. service->config.version = HS_VERSION_THREE;
  1372. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1373. ed25519_public_key_generate(&service->keys.identity_pk,
  1374. &service->keys.identity_sk);
  1375. /* Register service to global map. */
  1376. ret = register_service(get_hs_service_map(), service);
  1377. tt_int_op(ret, OP_EQ, 0);
  1378. /* But first, build our descriptor. */
  1379. build_all_descriptors(now);
  1380. /* Nothing should happen because we have 0 introduction circuit established
  1381. * and we want (by default) 3 intro points. */
  1382. run_upload_descriptor_event(now);
  1383. /* If no upload happened, this should be untouched. */
  1384. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1385. /* We'll simulate that we've opened our intro point circuit and that we only
  1386. * want one intro point. */
  1387. service->config.num_intro_points = 1;
  1388. /* Set our next upload time after now which will skip the upload. */
  1389. service->desc_current->next_upload_time = now + 1000;
  1390. run_upload_descriptor_event(now);
  1391. /* If no upload happened, this should be untouched. */
  1392. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, now + 1000);
  1393. done:
  1394. hs_free_all();
  1395. UNMOCK(get_or_state);
  1396. }
  1397. /** Global vars used by test_rendezvous1_parsing() */
  1398. static char rend1_payload[RELAY_PAYLOAD_SIZE];
  1399. static size_t rend1_payload_len = 0;
  1400. /** Mock for relay_send_command_from_edge() to send a RENDEZVOUS1 cell. Instead
  1401. * of sending it to the network, instead save it to the global `rend1_payload`
  1402. * variable so that we can inspect it in the test_rendezvous1_parsing()
  1403. * test. */
  1404. static int
  1405. mock_relay_send_rendezvous1(streamid_t stream_id, circuit_t *circ,
  1406. uint8_t relay_command, const char *payload,
  1407. size_t payload_len,
  1408. crypt_path_t *cpath_layer,
  1409. const char *filename, int lineno)
  1410. {
  1411. (void) stream_id;
  1412. (void) circ;
  1413. (void) relay_command;
  1414. (void) cpath_layer;
  1415. (void) filename;
  1416. (void) lineno;
  1417. memcpy(rend1_payload, payload, payload_len);
  1418. rend1_payload_len = payload_len;
  1419. return 0;
  1420. }
  1421. /** Send a RENDEZVOUS1 as a service, and parse it as a client. */
  1422. static void
  1423. test_rendezvous1_parsing(void *arg)
  1424. {
  1425. int retval;
  1426. static const char *test_addr =
  1427. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad.onion";
  1428. hs_service_t *service = NULL;
  1429. origin_circuit_t *service_circ = NULL;
  1430. origin_circuit_t *client_circ = NULL;
  1431. ed25519_keypair_t ip_auth_kp;
  1432. curve25519_keypair_t ephemeral_kp;
  1433. curve25519_keypair_t client_kp;
  1434. curve25519_keypair_t ip_enc_kp;
  1435. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1436. (void) arg;
  1437. MOCK(relay_send_command_from_edge_, mock_relay_send_rendezvous1);
  1438. {
  1439. /* Let's start by setting up the service that will start the rend */
  1440. service = tor_malloc_zero(sizeof(hs_service_t));
  1441. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1442. ed25519_public_key_generate(&service->keys.identity_pk,
  1443. &service->keys.identity_sk);
  1444. memcpy(service->onion_address, test_addr, sizeof(service->onion_address));
  1445. tt_assert(service);
  1446. }
  1447. {
  1448. /* Now let's set up the service rendezvous circuit and its keys. */
  1449. service_circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND,
  1450. flags);
  1451. tor_free(service_circ->hs_ident);
  1452. hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys;
  1453. uint8_t rendezvous_cookie[HS_REND_COOKIE_LEN];
  1454. curve25519_keypair_generate(&ip_enc_kp, 0);
  1455. curve25519_keypair_generate(&ephemeral_kp, 0);
  1456. curve25519_keypair_generate(&client_kp, 0);
  1457. ed25519_keypair_generate(&ip_auth_kp, 0);
  1458. retval = hs_ntor_service_get_rendezvous1_keys(&ip_auth_kp.pubkey,
  1459. &ip_enc_kp,
  1460. &ephemeral_kp,
  1461. &client_kp.pubkey,
  1462. &hs_ntor_rend_cell_keys);
  1463. tt_int_op(retval, OP_EQ, 0);
  1464. memset(rendezvous_cookie, 2, sizeof(rendezvous_cookie));
  1465. service_circ->hs_ident =
  1466. create_rp_circuit_identifier(service, rendezvous_cookie,
  1467. &ephemeral_kp.pubkey,
  1468. &hs_ntor_rend_cell_keys);
  1469. }
  1470. /* Send out the RENDEZVOUS1 and make sure that our mock func worked */
  1471. tt_assert(tor_mem_is_zero(rend1_payload, 32));
  1472. hs_circ_service_rp_has_opened(service, service_circ);
  1473. tt_assert(!tor_mem_is_zero(rend1_payload, 32));
  1474. tt_int_op(rend1_payload_len, OP_EQ, HS_LEGACY_RENDEZVOUS_CELL_SIZE);
  1475. /******************************/
  1476. /** Now let's create the client rendezvous circuit */
  1477. client_circ =
  1478. helper_create_origin_circuit(CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED,
  1479. flags);
  1480. /* fix up its circ ident */
  1481. ed25519_pubkey_copy(&client_circ->hs_ident->intro_auth_pk,
  1482. &ip_auth_kp.pubkey);
  1483. memcpy(&client_circ->hs_ident->rendezvous_client_kp,
  1484. &client_kp, sizeof(client_circ->hs_ident->rendezvous_client_kp));
  1485. memcpy(&client_circ->hs_ident->intro_enc_pk.public_key,
  1486. &ip_enc_kp.pubkey.public_key,
  1487. sizeof(client_circ->hs_ident->intro_enc_pk.public_key));
  1488. /* Now parse the rendezvous2 circuit and make sure it was fine. We are
  1489. * skipping 20 bytes off its payload, since that's the rendezvous cookie
  1490. * which is only present in REND1. */
  1491. retval = handle_rendezvous2(client_circ,
  1492. (uint8_t*)rend1_payload+20,
  1493. rend1_payload_len-20);
  1494. tt_int_op(retval, OP_EQ, 0);
  1495. /* TODO: We are only simulating client/service here. We could also simulate
  1496. * the rendezvous point by plugging in rend_mid_establish_rendezvous(). We
  1497. * would need an extra circuit and some more stuff but it's doable. */
  1498. done:
  1499. circuit_free_(TO_CIRCUIT(service_circ));
  1500. circuit_free_(TO_CIRCUIT(client_circ));
  1501. hs_service_free(service);
  1502. hs_free_all();
  1503. UNMOCK(relay_send_command_from_edge_);
  1504. }
  1505. struct testcase_t hs_service_tests[] = {
  1506. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  1507. NULL, NULL },
  1508. { "load_keys", test_load_keys, TT_FORK,
  1509. NULL, NULL },
  1510. { "client_filename_is_valid", test_client_filename_is_valid, TT_FORK,
  1511. NULL, NULL },
  1512. { "parse_authorized_client", test_parse_authorized_client, TT_FORK,
  1513. NULL, NULL },
  1514. { "load_keys_with_client_auth", test_load_keys_with_client_auth, TT_FORK,
  1515. NULL, NULL },
  1516. { "access_service", test_access_service, TT_FORK,
  1517. NULL, NULL },
  1518. { "service_intro_point", test_service_intro_point, TT_FORK,
  1519. NULL, NULL },
  1520. { "helper_functions", test_helper_functions, TT_FORK,
  1521. NULL, NULL },
  1522. { "intro_circuit_opened", test_intro_circuit_opened, TT_FORK,
  1523. NULL, NULL },
  1524. { "intro_established", test_intro_established, TT_FORK,
  1525. NULL, NULL },
  1526. { "closing_intro_circs", test_closing_intro_circs, TT_FORK,
  1527. NULL, NULL },
  1528. { "rdv_circuit_opened", test_rdv_circuit_opened, TT_FORK,
  1529. NULL, NULL },
  1530. { "introduce2", test_introduce2, TT_FORK,
  1531. NULL, NULL },
  1532. { "service_event", test_service_event, TT_FORK,
  1533. NULL, NULL },
  1534. { "rotate_descriptors", test_rotate_descriptors, TT_FORK,
  1535. NULL, NULL },
  1536. { "build_update_descriptors", test_build_update_descriptors, TT_FORK,
  1537. NULL, NULL },
  1538. { "upload_descriptors", test_upload_descriptors, TT_FORK,
  1539. NULL, NULL },
  1540. { "rendezvous1_parsing", test_rendezvous1_parsing, TT_FORK,
  1541. NULL, NULL },
  1542. END_OF_TESTCASES
  1543. };