test_hs_service.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049
  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 MAINLOOP_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/mainloop.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->config.clients);
  453. tt_int_op(smartlist_len(service->config.clients), OP_EQ,
  454. smartlist_len(pubkey_b32_list));
  455. /* Test that the is_client_auth_enabled flag is set. */
  456. tt_assert(service->config.is_client_auth_enabled);
  457. /* Test that the keys in clients are correct. */
  458. SMARTLIST_FOREACH_BEGIN(pubkey_b32_list, char *, pubkey_b32) {
  459. curve25519_public_key_t pubkey;
  460. /* This flag will be set if the key is found in clients. */
  461. int is_found = 0;
  462. base32_decode((char *) pubkey.public_key, sizeof(pubkey.public_key),
  463. pubkey_b32, strlen(pubkey_b32));
  464. SMARTLIST_FOREACH_BEGIN(service->config.clients,
  465. hs_service_authorized_client_t *, client) {
  466. if (tor_memeq(&pubkey, &client->client_pk, sizeof(pubkey))) {
  467. is_found = 1;
  468. break;
  469. }
  470. } SMARTLIST_FOREACH_END(client);
  471. tt_assert(is_found);
  472. } SMARTLIST_FOREACH_END(pubkey_b32);
  473. done:
  474. SMARTLIST_FOREACH(pubkey_b32_list, char *, s, tor_free(s));
  475. smartlist_free(pubkey_b32_list);
  476. tor_free(hsdir_v3);
  477. hs_free_all();
  478. UNMOCK(read_file_to_str);
  479. UNMOCK(tor_listdir);
  480. }
  481. static void
  482. test_access_service(void *arg)
  483. {
  484. int ret;
  485. char *conf = NULL;
  486. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  487. hs_service_ht *global_map;
  488. hs_service_t *s = NULL;
  489. (void) arg;
  490. /* We'll register two services, a v2 and a v3, then we'll load keys and
  491. * validate that both are in a correct state. */
  492. hs_init();
  493. #define conf_fmt \
  494. "HiddenServiceDir %s\n" \
  495. "HiddenServiceVersion %d\n" \
  496. "HiddenServicePort 65535\n"
  497. /* v3 service. */
  498. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  499. ret = helper_config_service(conf);
  500. tor_free(conf);
  501. tt_int_op(ret, OP_EQ, 0);
  502. /* It's in staging? */
  503. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  504. /* Load the keys for these. After that, the v3 service should be registered
  505. * in the global map. */
  506. hs_service_load_all_keys();
  507. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  508. s = get_first_service();
  509. tt_assert(s);
  510. global_map = get_hs_service_map();
  511. tt_assert(global_map);
  512. /* From here, we'll try the service accessors. */
  513. hs_service_t *query = find_service(global_map, &s->keys.identity_pk);
  514. tt_assert(query);
  515. tt_mem_op(query, OP_EQ, s, sizeof(hs_service_t));
  516. /* Remove service, check if it actually works and then put it back. */
  517. remove_service(global_map, s);
  518. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  519. query = find_service(global_map, &s->keys.identity_pk);
  520. tt_ptr_op(query, OP_EQ, NULL);
  521. /* Register back the service in the map. */
  522. ret = register_service(global_map, s);
  523. tt_int_op(ret, OP_EQ, 0);
  524. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  525. /* Twice should fail. */
  526. ret = register_service(global_map, s);
  527. tt_int_op(ret, OP_EQ, -1);
  528. /* Remove service from map so we don't double free on cleanup. */
  529. remove_service(global_map, s);
  530. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  531. query = find_service(global_map, &s->keys.identity_pk);
  532. tt_ptr_op(query, OP_EQ, NULL);
  533. /* Let's try to remove twice for fun. */
  534. setup_full_capture_of_logs(LOG_WARN);
  535. remove_service(global_map, s);
  536. expect_log_msg_containing("Could not find service in the global map");
  537. teardown_capture_of_logs();
  538. done:
  539. hs_service_free(s);
  540. tor_free(hsdir_v3);
  541. hs_free_all();
  542. }
  543. /** Test that we can create intro point objects, index them and find them */
  544. static void
  545. test_service_intro_point(void *arg)
  546. {
  547. hs_service_t *service = NULL;
  548. hs_service_intro_point_t *ip = NULL;
  549. (void) arg;
  550. /* Test simple creation of an object. */
  551. {
  552. time_t now = time(NULL);
  553. ip = helper_create_service_ip();
  554. tt_assert(ip);
  555. /* Make sure the authentication keypair is not zeroes. */
  556. tt_int_op(tor_mem_is_zero((const char *) &ip->auth_key_kp,
  557. sizeof(ed25519_keypair_t)), OP_EQ, 0);
  558. /* The introduce2_max MUST be in that range. */
  559. tt_u64_op(ip->introduce2_max, OP_GE,
  560. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  561. tt_u64_op(ip->introduce2_max, OP_LE,
  562. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  563. /* Time to expire MUST also be in that range. We subtract 500 seconds
  564. * because there could be a gap between setting now and the time taken in
  565. * service_intro_point_new. On ARM and other older CPUs, it can be
  566. * surprisingly slow... */
  567. tt_u64_op(ip->time_to_expire, OP_GE,
  568. now + INTRO_POINT_LIFETIME_MIN_SECONDS - 500);
  569. /* We add 500 seconds, because this time we're testing against the
  570. * maximum allowed time. */
  571. tt_u64_op(ip->time_to_expire, OP_LE,
  572. now + INTRO_POINT_LIFETIME_MAX_SECONDS + 500);
  573. tt_assert(ip->replay_cache);
  574. tt_assert(ip->base.link_specifiers);
  575. /* By default, this is NOT a legacy object. */
  576. tt_int_op(ip->base.is_only_legacy, OP_EQ, 0);
  577. }
  578. /* Test functions that uses a service intropoints map with that previously
  579. * created object (non legacy). */
  580. {
  581. ed25519_public_key_t garbage = { {0} };
  582. hs_service_intro_point_t *query;
  583. service = hs_service_new(get_options());
  584. tt_assert(service);
  585. service->desc_current = service_descriptor_new();
  586. tt_assert(service->desc_current);
  587. /* Add intropoint to descriptor map. */
  588. service_intro_point_add(service->desc_current->intro_points.map, ip);
  589. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  590. tt_mem_op(query, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  591. query = service_intro_point_find(service, &garbage);
  592. tt_ptr_op(query, OP_EQ, NULL);
  593. /* While at it, can I find the descriptor with the intro point? */
  594. hs_service_descriptor_t *desc_lookup =
  595. service_desc_find_by_intro(service, ip);
  596. tt_mem_op(service->desc_current, OP_EQ, desc_lookup,
  597. sizeof(hs_service_descriptor_t));
  598. /* Remove object from service descriptor and make sure it is out. */
  599. service_intro_point_remove(service, ip);
  600. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  601. tt_ptr_op(query, OP_EQ, NULL);
  602. }
  603. done:
  604. /* If the test succeed, this object is no longer referenced in the service
  605. * so we can free it without use after free. Else, it might explode because
  606. * it's still in the service descriptor map. */
  607. service_intro_point_free(ip);
  608. hs_service_free(service);
  609. }
  610. static node_t mock_node;
  611. static const node_t *
  612. mock_node_get_by_id(const char *digest)
  613. {
  614. (void) digest;
  615. memset(mock_node.identity, 'A', DIGEST_LEN);
  616. /* Only return the matchin identity of As */
  617. if (!tor_memcmp(mock_node.identity, digest, DIGEST_LEN)) {
  618. return &mock_node;
  619. }
  620. return NULL;
  621. }
  622. static void
  623. test_helper_functions(void *arg)
  624. {
  625. int ret;
  626. hs_service_t *service = NULL;
  627. hs_service_intro_point_t *ip = NULL;
  628. hs_ident_circuit_t ident;
  629. (void) arg;
  630. MOCK(node_get_by_id, mock_node_get_by_id);
  631. hs_service_init();
  632. service = helper_create_service();
  633. ip = helper_create_service_ip();
  634. /* Immediately add the intro point to the service so the free service at the
  635. * end cleans it as well. */
  636. service_intro_point_add(service->desc_current->intro_points.map, ip);
  637. /* Setup the circuit identifier. */
  638. ed25519_pubkey_copy(&ident.intro_auth_pk, &ip->auth_key_kp.pubkey);
  639. ed25519_pubkey_copy(&ident.identity_pk, &service->keys.identity_pk);
  640. /* Testing get_objects_from_ident(). */
  641. {
  642. hs_service_t *s_lookup = NULL;
  643. hs_service_intro_point_t *ip_lookup = NULL;
  644. hs_service_descriptor_t *desc_lookup = NULL;
  645. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  646. tt_mem_op(s_lookup, OP_EQ, service, sizeof(hs_service_t));
  647. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  648. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  649. sizeof(hs_service_descriptor_t));
  650. /* Reset */
  651. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  652. /* NULL parameter should work. */
  653. get_objects_from_ident(&ident, NULL, &ip_lookup, &desc_lookup);
  654. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  655. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  656. sizeof(hs_service_descriptor_t));
  657. /* Reset. */
  658. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  659. /* Break the ident and we should find nothing. */
  660. memset(&ident, 0, sizeof(ident));
  661. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  662. tt_ptr_op(s_lookup, OP_EQ, NULL);
  663. tt_ptr_op(ip_lookup, OP_EQ, NULL);
  664. tt_ptr_op(desc_lookup, OP_EQ, NULL);
  665. }
  666. /* Testing get_node_from_intro_point() */
  667. {
  668. const node_t *node = get_node_from_intro_point(ip);
  669. tt_ptr_op(node, OP_EQ, &mock_node);
  670. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  671. hs_desc_link_specifier_t *, ls) {
  672. if (ls->type == LS_LEGACY_ID) {
  673. /* Change legacy id in link specifier which is not the mock node. */
  674. memset(ls->u.legacy_id, 'B', sizeof(ls->u.legacy_id));
  675. }
  676. } SMARTLIST_FOREACH_END(ls);
  677. node = get_node_from_intro_point(ip);
  678. tt_ptr_op(node, OP_EQ, NULL);
  679. }
  680. /* Testing can_service_launch_intro_circuit() */
  681. {
  682. time_t now = time(NULL);
  683. /* Put the start of the retry period back in time, we should be allowed.
  684. * to launch intro circuit. */
  685. service->state.num_intro_circ_launched = 2;
  686. service->state.intro_circ_retry_started_time =
  687. (now - INTRO_CIRC_RETRY_PERIOD - 1);
  688. ret = can_service_launch_intro_circuit(service, now);
  689. tt_int_op(ret, OP_EQ, 1);
  690. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  691. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  692. /* Call it again, we should still be allowed because we are under
  693. * MAX_INTRO_CIRCS_PER_PERIOD which been set to 0 previously. */
  694. ret = can_service_launch_intro_circuit(service, now);
  695. tt_int_op(ret, OP_EQ, 1);
  696. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  697. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  698. /* Too many intro circuit launched means we are not allowed. */
  699. service->state.num_intro_circ_launched = 20;
  700. ret = can_service_launch_intro_circuit(service, now);
  701. tt_int_op(ret, OP_EQ, 0);
  702. }
  703. /* Testing intro_point_should_expire(). */
  704. {
  705. time_t now = time(NULL);
  706. /* Just some basic test of the current state. */
  707. tt_u64_op(ip->introduce2_max, OP_GE,
  708. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  709. tt_u64_op(ip->introduce2_max, OP_LE,
  710. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  711. tt_u64_op(ip->time_to_expire, OP_GE,
  712. now + INTRO_POINT_LIFETIME_MIN_SECONDS);
  713. tt_u64_op(ip->time_to_expire, OP_LE,
  714. now + INTRO_POINT_LIFETIME_MAX_SECONDS);
  715. /* This newly created IP from above shouldn't expire now. */
  716. ret = intro_point_should_expire(ip, now);
  717. tt_int_op(ret, OP_EQ, 0);
  718. /* Maximum number of INTRODUCE2 cell reached, it should expire. */
  719. ip->introduce2_count = INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS + 1;
  720. ret = intro_point_should_expire(ip, now);
  721. tt_int_op(ret, OP_EQ, 1);
  722. ip->introduce2_count = 0;
  723. /* It should expire if time to expire has been reached. */
  724. ip->time_to_expire = now - 1000;
  725. ret = intro_point_should_expire(ip, now);
  726. tt_int_op(ret, OP_EQ, 1);
  727. }
  728. done:
  729. /* This will free the service and all objects associated to it. */
  730. hs_service_free_all();
  731. UNMOCK(node_get_by_id);
  732. }
  733. /** Test that we do the right operations when an intro circuit opens */
  734. static void
  735. test_intro_circuit_opened(void *arg)
  736. {
  737. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  738. hs_service_t *service;
  739. origin_circuit_t *circ = NULL;
  740. (void) arg;
  741. hs_init();
  742. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  743. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  744. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  745. flags);
  746. /* No service associated with this circuit. */
  747. setup_full_capture_of_logs(LOG_WARN);
  748. hs_service_circuit_has_opened(circ);
  749. expect_log_msg_containing("Unknown service identity key");
  750. teardown_capture_of_logs();
  751. /* Set a service for this circuit. */
  752. {
  753. service = helper_create_service();
  754. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  755. &service->keys.identity_pk);
  756. /* No intro point associated with this circuit. */
  757. setup_full_capture_of_logs(LOG_WARN);
  758. hs_service_circuit_has_opened(circ);
  759. expect_log_msg_containing("Unknown introduction point auth key");
  760. teardown_capture_of_logs();
  761. }
  762. /* Set an IP object now for this circuit. */
  763. {
  764. hs_service_intro_point_t *ip = helper_create_service_ip();
  765. service_intro_point_add(service->desc_current->intro_points.map, ip);
  766. /* Update ident to contain the intro point auth key. */
  767. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  768. &ip->auth_key_kp.pubkey);
  769. }
  770. /* This one should go all the way. */
  771. setup_full_capture_of_logs(LOG_INFO);
  772. hs_service_circuit_has_opened(circ);
  773. expect_log_msg_containing("Introduction circuit 0 established for service");
  774. teardown_capture_of_logs();
  775. done:
  776. circuit_free_(TO_CIRCUIT(circ));
  777. hs_free_all();
  778. UNMOCK(circuit_mark_for_close_);
  779. UNMOCK(relay_send_command_from_edge_);
  780. }
  781. /** Test the operations we do on a circuit after we learn that we successfully
  782. * established an intro point on it */
  783. static void
  784. test_intro_established(void *arg)
  785. {
  786. int ret;
  787. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  788. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  789. origin_circuit_t *circ = NULL;
  790. hs_service_t *service;
  791. hs_service_intro_point_t *ip = NULL;
  792. (void) arg;
  793. hs_init();
  794. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  795. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  796. flags);
  797. tt_assert(circ);
  798. /* Test a wrong purpose. */
  799. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  800. setup_full_capture_of_logs(LOG_WARN);
  801. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  802. tt_int_op(ret, OP_EQ, -1);
  803. expect_log_msg_containing("Received an INTRO_ESTABLISHED cell on a "
  804. "non introduction circuit of purpose");
  805. teardown_capture_of_logs();
  806. /* Back to normal. */
  807. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  808. /* No service associated to it. */
  809. setup_full_capture_of_logs(LOG_WARN);
  810. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  811. tt_int_op(ret, OP_EQ, -1);
  812. expect_log_msg_containing("Unknown service identity key");
  813. teardown_capture_of_logs();
  814. /* Set a service for this circuit. */
  815. service = helper_create_service();
  816. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  817. &service->keys.identity_pk);
  818. /* No introduction point associated to it. */
  819. setup_full_capture_of_logs(LOG_WARN);
  820. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  821. tt_int_op(ret, OP_EQ, -1);
  822. expect_log_msg_containing("Introduction circuit established without an "
  823. "intro point object on circuit");
  824. teardown_capture_of_logs();
  825. /* Set an IP object now for this circuit. */
  826. {
  827. ip = helper_create_service_ip();
  828. service_intro_point_add(service->desc_current->intro_points.map, ip);
  829. /* Update ident to contain the intro point auth key. */
  830. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  831. &ip->auth_key_kp.pubkey);
  832. }
  833. /* Send an empty payload. INTRO_ESTABLISHED cells are basically zeroes. */
  834. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  835. tt_int_op(ret, OP_EQ, 0);
  836. tt_u64_op(ip->circuit_established, OP_EQ, 1);
  837. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_INTRO);
  838. done:
  839. if (circ)
  840. circuit_free_(TO_CIRCUIT(circ));
  841. hs_free_all();
  842. UNMOCK(circuit_mark_for_close_);
  843. }
  844. /** Check the operations we do on a rendezvous circuit after we learn it's
  845. * open */
  846. static void
  847. test_rdv_circuit_opened(void *arg)
  848. {
  849. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  850. origin_circuit_t *circ = NULL;
  851. hs_service_t *service;
  852. (void) arg;
  853. hs_init();
  854. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  855. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  856. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND, flags);
  857. crypto_rand((char *) circ->hs_ident->rendezvous_cookie, REND_COOKIE_LEN);
  858. crypto_rand((char *) circ->hs_ident->rendezvous_handshake_info,
  859. sizeof(circ->hs_ident->rendezvous_handshake_info));
  860. /* No service associated with this circuit. */
  861. setup_full_capture_of_logs(LOG_WARN);
  862. hs_service_circuit_has_opened(circ);
  863. expect_log_msg_containing("Unknown service identity key");
  864. teardown_capture_of_logs();
  865. /* This should be set to a non zero timestamp. */
  866. tt_u64_op(TO_CIRCUIT(circ)->timestamp_dirty, OP_NE, 0);
  867. /* Set a service for this circuit. */
  868. service = helper_create_service();
  869. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  870. &service->keys.identity_pk);
  871. /* Should be all good. */
  872. hs_service_circuit_has_opened(circ);
  873. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  874. done:
  875. circuit_free_(TO_CIRCUIT(circ));
  876. hs_free_all();
  877. UNMOCK(circuit_mark_for_close_);
  878. UNMOCK(relay_send_command_from_edge_);
  879. }
  880. static void
  881. mock_assert_circuit_ok(const circuit_t *c)
  882. {
  883. (void) c;
  884. return;
  885. }
  886. /** Test for the general mechanism for closing intro circs.
  887. * Also a way to identify that #23603 has been fixed. */
  888. static void
  889. test_closing_intro_circs(void *arg)
  890. {
  891. hs_service_t *service = NULL;
  892. hs_service_intro_point_t *ip = NULL, *entry = NULL;
  893. origin_circuit_t *intro_circ = NULL, *tmp_circ;
  894. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  895. (void) arg;
  896. MOCK(assert_circuit_ok, mock_assert_circuit_ok);
  897. hs_init();
  898. /* Initialize service */
  899. service = helper_create_service();
  900. /* Initialize intro point */
  901. ip = helper_create_service_ip();
  902. tt_assert(ip);
  903. service_intro_point_add(service->desc_current->intro_points.map, ip);
  904. /* Initialize intro circuit */
  905. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  906. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk,
  907. HS_IDENT_CIRCUIT_INTRO);
  908. /* Register circuit in the circuitmap . */
  909. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  910. &ip->auth_key_kp.pubkey);
  911. tmp_circ =
  912. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  913. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  914. /* Pretend that intro point has failed too much */
  915. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES+1;
  916. /* Now pretend we are freeing this intro circuit. We want to see that our
  917. * destructor is not gonna kill our intro point structure since that's the
  918. * job of the cleanup routine. */
  919. circuit_free_(TO_CIRCUIT(intro_circ));
  920. intro_circ = NULL;
  921. entry = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  922. tt_assert(entry);
  923. /* The free should also remove the circuit from the circuitmap. */
  924. tmp_circ =
  925. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  926. tt_assert(!tmp_circ);
  927. /* Now pretend that a new intro point circ was launched and opened. Check
  928. * that the intro point will be established correctly. */
  929. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  930. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk,
  931. HS_IDENT_CIRCUIT_INTRO);
  932. ed25519_pubkey_copy(&intro_circ->hs_ident->intro_auth_pk,
  933. &ip->auth_key_kp.pubkey);
  934. /* Register circuit in the circuitmap . */
  935. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  936. &ip->auth_key_kp.pubkey);
  937. tmp_circ =
  938. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  939. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  940. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_EQ, 0);
  941. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  942. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_NE, 0);
  943. /* At this point, we should not be able to find it in the circuitmap. */
  944. tmp_circ =
  945. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  946. tt_assert(!tmp_circ);
  947. done:
  948. if (intro_circ) {
  949. circuit_free_(TO_CIRCUIT(intro_circ));
  950. }
  951. /* Frees the service object. */
  952. hs_free_all();
  953. UNMOCK(assert_circuit_ok);
  954. }
  955. /** Test sending and receiving introduce2 cells */
  956. static void
  957. test_introduce2(void *arg)
  958. {
  959. int ret;
  960. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  961. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  962. origin_circuit_t *circ = NULL;
  963. hs_service_t *service;
  964. hs_service_intro_point_t *ip = NULL;
  965. (void) arg;
  966. hs_init();
  967. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  968. MOCK(get_or_state,
  969. get_or_state_replacement);
  970. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  971. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  972. tt_assert(circ);
  973. /* Test a wrong purpose. */
  974. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  975. setup_full_capture_of_logs(LOG_WARN);
  976. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  977. tt_int_op(ret, OP_EQ, -1);
  978. expect_log_msg_containing("Received an INTRODUCE2 cell on a "
  979. "non introduction circuit of purpose");
  980. teardown_capture_of_logs();
  981. /* Back to normal. */
  982. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  983. /* No service associated to it. */
  984. setup_full_capture_of_logs(LOG_WARN);
  985. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  986. tt_int_op(ret, OP_EQ, -1);
  987. expect_log_msg_containing("Unknown service identity key");
  988. teardown_capture_of_logs();
  989. /* Set a service for this circuit. */
  990. service = helper_create_service();
  991. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  992. &service->keys.identity_pk);
  993. /* No introduction point associated to it. */
  994. setup_full_capture_of_logs(LOG_WARN);
  995. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  996. tt_int_op(ret, OP_EQ, -1);
  997. expect_log_msg_containing("Unknown introduction auth key when handling "
  998. "an INTRODUCE2 cell on circuit");
  999. teardown_capture_of_logs();
  1000. /* Set an IP object now for this circuit. */
  1001. {
  1002. ip = helper_create_service_ip();
  1003. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1004. /* Update ident to contain the intro point auth key. */
  1005. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  1006. &ip->auth_key_kp.pubkey);
  1007. }
  1008. /* This will fail because receiving an INTRODUCE2 cell implies a valid cell
  1009. * and then launching circuits so let's not do that and instead test that
  1010. * behaviour differently. */
  1011. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1012. tt_int_op(ret, OP_EQ, -1);
  1013. tt_u64_op(ip->introduce2_count, OP_EQ, 0);
  1014. done:
  1015. or_state_free(dummy_state);
  1016. dummy_state = NULL;
  1017. if (circ)
  1018. circuit_free_(TO_CIRCUIT(circ));
  1019. hs_free_all();
  1020. UNMOCK(circuit_mark_for_close_);
  1021. }
  1022. /** Test basic hidden service housekeeping operations (maintaining intro
  1023. * points, etc) */
  1024. static void
  1025. test_service_event(void *arg)
  1026. {
  1027. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1028. time_t now = time(NULL);
  1029. hs_service_t *service;
  1030. origin_circuit_t *circ = NULL;
  1031. (void) arg;
  1032. hs_init();
  1033. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  1034. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  1035. /* Set a service for this circuit. */
  1036. service = helper_create_service();
  1037. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  1038. &service->keys.identity_pk);
  1039. /* Currently this consists of cleaning invalid intro points. So adding IPs
  1040. * here that should get cleaned up. */
  1041. {
  1042. hs_service_intro_point_t *ip = helper_create_service_ip();
  1043. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1044. /* This run will remove the IP because we have no circuits nor node_t
  1045. * associated with it. */
  1046. run_housekeeping_event(now);
  1047. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1048. OP_EQ, 0);
  1049. /* We'll trigger a removal because we've reached our maximum amount of
  1050. * times we should retry a circuit. For this, we need to have a node_t
  1051. * that matches the identity of this IP. */
  1052. routerinfo_t ri;
  1053. memset(&ri, 0, sizeof(ri));
  1054. ip = helper_create_service_ip();
  1055. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1056. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1057. /* This triggers a node_t creation. */
  1058. tt_assert(nodelist_set_routerinfo(&ri, NULL));
  1059. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  1060. run_housekeeping_event(now);
  1061. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1062. OP_EQ, 0);
  1063. /* No removal but no circuit so this means the IP object will stay in the
  1064. * descriptor map so we can retry it. */
  1065. ip = helper_create_service_ip();
  1066. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1067. ip->circuit_established = 1; /* We'll test that, it MUST be 0 after. */
  1068. run_housekeeping_event(now);
  1069. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1070. OP_EQ, 1);
  1071. /* Remove the IP object at once for the next test. */
  1072. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  1073. run_housekeeping_event(now);
  1074. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1075. OP_EQ, 0);
  1076. /* Now, we'll create an IP with a registered circuit. The IP object
  1077. * shouldn't go away. */
  1078. ip = helper_create_service_ip();
  1079. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1080. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  1081. &ip->auth_key_kp.pubkey);
  1082. hs_circuitmap_register_intro_circ_v3_service_side(
  1083. circ, &ip->auth_key_kp.pubkey);
  1084. run_housekeeping_event(now);
  1085. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1086. OP_EQ, 1);
  1087. /* We'll mangle the IP object to expire. */
  1088. ip->time_to_expire = now;
  1089. run_housekeeping_event(now);
  1090. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1091. OP_EQ, 0);
  1092. }
  1093. done:
  1094. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  1095. circuit_free_(TO_CIRCUIT(circ));
  1096. hs_free_all();
  1097. UNMOCK(circuit_mark_for_close_);
  1098. }
  1099. /** Test that we rotate descriptors correctly. */
  1100. static void
  1101. test_rotate_descriptors(void *arg)
  1102. {
  1103. int ret;
  1104. time_t next_rotation_time, now;
  1105. hs_service_t *service;
  1106. hs_service_descriptor_t *desc_next;
  1107. (void) arg;
  1108. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1109. hs_init();
  1110. MOCK(get_or_state, get_or_state_replacement);
  1111. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  1112. MOCK(networkstatus_get_live_consensus,
  1113. mock_networkstatus_get_live_consensus);
  1114. /* Descriptor rotation happens with a consensus with a new SRV. */
  1115. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1116. &mock_ns.valid_after);
  1117. tt_int_op(ret, OP_EQ, 0);
  1118. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1119. &mock_ns.fresh_until);
  1120. tt_int_op(ret, OP_EQ, 0);
  1121. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1122. update_approx_time(mock_ns.valid_after+1);
  1123. now = mock_ns.valid_after+1;
  1124. /* Create a service with a default descriptor and state. It's added to the
  1125. * global map. */
  1126. service = helper_create_service();
  1127. service_descriptor_free(service->desc_current);
  1128. service->desc_current = NULL;
  1129. /* This triggers a build for both descriptors. The time now is only used in
  1130. * the descriptor certificate which is important to be now else the decoding
  1131. * will complain that the cert has expired if we use valid_after. */
  1132. build_all_descriptors(now);
  1133. tt_assert(service->desc_current);
  1134. tt_assert(service->desc_next);
  1135. /* Tweak our service next rotation time so we can use a custom time. */
  1136. service->state.next_rotation_time = next_rotation_time =
  1137. mock_ns.valid_after + (11 * 60 * 60);
  1138. /* Nothing should happen, we are not at a new SRV. Our next rotation time
  1139. * should be untouched. */
  1140. rotate_all_descriptors(mock_ns.valid_after);
  1141. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1142. tt_assert(service->desc_current);
  1143. tt_assert(service->desc_next);
  1144. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1145. hs_get_previous_time_period_num(0));
  1146. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1147. hs_get_time_period_num(0));
  1148. /* Keep a reference so we can compare it after rotation to the current. */
  1149. desc_next = service->desc_next;
  1150. /* Going right after a new SRV. */
  1151. ret = parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC",
  1152. &mock_ns.valid_after);
  1153. tt_int_op(ret, OP_EQ, 0);
  1154. ret = parse_rfc1123_time("Sat, 27 Oct 1985 02:00:00 UTC",
  1155. &mock_ns.fresh_until);
  1156. tt_int_op(ret, OP_EQ, 0);
  1157. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1158. update_approx_time(mock_ns.valid_after+1);
  1159. now = mock_ns.valid_after+1;
  1160. /* Note down what to expect for the next rotation time which is 01:00 + 23h
  1161. * meaning 00:00:00. */
  1162. next_rotation_time = mock_ns.valid_after + (23 * 60 * 60);
  1163. /* We should have our next rotation time modified, our current descriptor
  1164. * cleaned up and the next descriptor becoming the current. */
  1165. rotate_all_descriptors(mock_ns.valid_after);
  1166. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1167. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1168. tt_assert(service->desc_next == NULL);
  1169. /* A second time should do nothing. */
  1170. rotate_all_descriptors(mock_ns.valid_after);
  1171. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1172. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1173. tt_assert(service->desc_next == NULL);
  1174. build_all_descriptors(now);
  1175. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1176. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1177. hs_get_time_period_num(0));
  1178. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1179. hs_get_next_time_period_num(0));
  1180. tt_assert(service->desc_next);
  1181. done:
  1182. hs_free_all();
  1183. UNMOCK(get_or_state);
  1184. UNMOCK(circuit_mark_for_close_);
  1185. UNMOCK(networkstatus_get_live_consensus);
  1186. }
  1187. /** Test building descriptors: picking intro points, setting up their link
  1188. * specifiers, etc. */
  1189. static void
  1190. test_build_update_descriptors(void *arg)
  1191. {
  1192. int ret;
  1193. time_t now = time(NULL);
  1194. node_t *node;
  1195. hs_service_t *service;
  1196. hs_service_intro_point_t *ip_cur, *ip_next;
  1197. routerinfo_t ri;
  1198. (void) arg;
  1199. hs_init();
  1200. MOCK(get_or_state,
  1201. get_or_state_replacement);
  1202. MOCK(networkstatus_get_live_consensus,
  1203. mock_networkstatus_get_live_consensus);
  1204. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1205. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  1206. &mock_ns.valid_after);
  1207. tt_int_op(ret, OP_EQ, 0);
  1208. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  1209. &mock_ns.fresh_until);
  1210. tt_int_op(ret, OP_EQ, 0);
  1211. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1212. update_approx_time(mock_ns.valid_after+1);
  1213. now = mock_ns.valid_after+1;
  1214. /* Create a service without a current descriptor to trigger a build. */
  1215. service = helper_create_service();
  1216. tt_assert(service);
  1217. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1218. service_descriptor_free(service->desc_current);
  1219. service->desc_current = NULL;
  1220. /* We have a fresh service so this should trigger a build for both
  1221. * descriptors for specific time period that we'll test. */
  1222. build_all_descriptors(now);
  1223. /* Check *current* descriptor. */
  1224. tt_assert(service->desc_current);
  1225. tt_assert(service->desc_current->desc);
  1226. tt_assert(service->desc_current->intro_points.map);
  1227. /* The current time period is the one expected when starting at 03:00. */
  1228. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1229. hs_get_time_period_num(0));
  1230. /* This should be untouched, the update descriptor process changes it. */
  1231. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1232. /* Check *next* descriptor. */
  1233. tt_assert(service->desc_next);
  1234. tt_assert(service->desc_next->desc);
  1235. tt_assert(service->desc_next->intro_points.map);
  1236. tt_assert(service->desc_current != service->desc_next);
  1237. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1238. hs_get_next_time_period_num(0));
  1239. /* This should be untouched, the update descriptor process changes it. */
  1240. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1241. /* Time to test the update of those descriptors. At first, we have no node
  1242. * in the routerlist so this will find NO suitable node for the IPs. */
  1243. setup_full_capture_of_logs(LOG_INFO);
  1244. update_all_descriptors(now);
  1245. expect_log_msg_containing("Unable to find a suitable node to be an "
  1246. "introduction point for service");
  1247. teardown_capture_of_logs();
  1248. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1249. OP_EQ, 0);
  1250. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1251. OP_EQ, 0);
  1252. /* Now, we'll setup a node_t. */
  1253. {
  1254. tor_addr_t ipv4_addr;
  1255. curve25519_secret_key_t curve25519_secret_key;
  1256. memset(&ri, 0, sizeof(routerinfo_t));
  1257. tor_addr_parse(&ipv4_addr, "127.0.0.1");
  1258. ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  1259. ri.or_port = 1337;
  1260. ri.purpose = ROUTER_PURPOSE_GENERAL;
  1261. /* Ugly yes but we never free the "ri" object so this just makes things
  1262. * easier. */
  1263. ri.protocol_list = (char *) "HSDir=1-2 LinkAuth=3";
  1264. summarize_protover_flags(&ri.pv, ri.protocol_list, NULL);
  1265. ret = curve25519_secret_key_generate(&curve25519_secret_key, 0);
  1266. tt_int_op(ret, OP_EQ, 0);
  1267. ri.onion_curve25519_pkey =
  1268. tor_malloc_zero(sizeof(curve25519_public_key_t));
  1269. ri.onion_pkey = tor_malloc_zero(140);
  1270. curve25519_public_key_generate(ri.onion_curve25519_pkey,
  1271. &curve25519_secret_key);
  1272. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1273. /* Setup ed25519 identity */
  1274. ed25519_keypair_t kp1;
  1275. ed25519_keypair_generate(&kp1, 0);
  1276. ri.cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  1277. tt_assert(ri.cache_info.signing_key_cert);
  1278. ed25519_pubkey_copy(&ri.cache_info.signing_key_cert->signing_key,
  1279. &kp1.pubkey);
  1280. nodelist_set_routerinfo(&ri, NULL);
  1281. node = node_get_mutable_by_id(ri.cache_info.identity_digest);
  1282. tt_assert(node);
  1283. node->is_running = node->is_valid = node->is_fast = node->is_stable = 1;
  1284. }
  1285. /* We have to set this, or the lack of microdescriptors for these
  1286. * nodes will make them unusable. */
  1287. get_options_mutable()->UseMicrodescriptors = 0;
  1288. /* We expect to pick only one intro point from the node above. */
  1289. setup_full_capture_of_logs(LOG_INFO);
  1290. update_all_descriptors(now);
  1291. tor_free(node->ri->onion_curve25519_pkey); /* Avoid memleak. */
  1292. tor_free(node->ri->cache_info.signing_key_cert);
  1293. tor_free(node->ri->onion_pkey);
  1294. expect_log_msg_containing("just picked 1 intro points and wanted 3 for next "
  1295. "descriptor. It currently has 0 intro points. "
  1296. "Launching ESTABLISH_INTRO circuit shortly.");
  1297. teardown_capture_of_logs();
  1298. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1299. OP_EQ, 1);
  1300. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1301. OP_EQ, 1);
  1302. /* Get the IP object. Because we don't have the auth key of the IP, we can't
  1303. * query it so get the first element in the map. */
  1304. {
  1305. void *obj = NULL;
  1306. const uint8_t *key;
  1307. digest256map_iter_t *iter =
  1308. digest256map_iter_init(service->desc_current->intro_points.map);
  1309. digest256map_iter_get(iter, &key, &obj);
  1310. tt_assert(obj);
  1311. ip_cur = obj;
  1312. /* Get also the IP from the next descriptor. We'll make sure it's not the
  1313. * same object as in the current descriptor. */
  1314. iter = digest256map_iter_init(service->desc_next->intro_points.map);
  1315. digest256map_iter_get(iter, &key, &obj);
  1316. tt_assert(obj);
  1317. ip_next = obj;
  1318. }
  1319. tt_mem_op(ip_cur, OP_NE, ip_next, sizeof(hs_desc_intro_point_t));
  1320. /* We won't test the service IP object because there is a specific test
  1321. * already for this but we'll make sure that the state is coherent.*/
  1322. /* Three link specifiers are mandatoy so make sure we do have them. */
  1323. tt_int_op(smartlist_len(ip_cur->base.link_specifiers), OP_EQ, 3);
  1324. /* Make sure we have a valid encryption keypair generated when we pick an
  1325. * intro point in the update process. */
  1326. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.seckey.secret_key,
  1327. CURVE25519_SECKEY_LEN));
  1328. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.pubkey.public_key,
  1329. CURVE25519_PUBKEY_LEN));
  1330. tt_u64_op(ip_cur->time_to_expire, OP_GE, now +
  1331. INTRO_POINT_LIFETIME_MIN_SECONDS);
  1332. tt_u64_op(ip_cur->time_to_expire, OP_LE, now +
  1333. INTRO_POINT_LIFETIME_MAX_SECONDS);
  1334. /* Now, we will try to set up a service after a new time period has started
  1335. * and see if it behaves as expected. */
  1336. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1337. &mock_ns.valid_after);
  1338. tt_int_op(ret, OP_EQ, 0);
  1339. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1340. &mock_ns.fresh_until);
  1341. tt_int_op(ret, OP_EQ, 0);
  1342. update_approx_time(mock_ns.valid_after+1);
  1343. now = mock_ns.valid_after+1;
  1344. /* Create a service without a current descriptor to trigger a build. */
  1345. service = helper_create_service();
  1346. tt_assert(service);
  1347. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1348. service_descriptor_free(service->desc_current);
  1349. service->desc_current = NULL;
  1350. /* We have a fresh service so this should trigger a build for both
  1351. * descriptors for specific time period that we'll test. */
  1352. build_all_descriptors(now);
  1353. /* Check *current* descriptor. */
  1354. tt_assert(service->desc_current);
  1355. tt_assert(service->desc_current->desc);
  1356. tt_assert(service->desc_current->intro_points.map);
  1357. /* This should be for the previous time period. */
  1358. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1359. hs_get_previous_time_period_num(0));
  1360. /* This should be untouched, the update descriptor process changes it. */
  1361. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1362. /* Check *next* descriptor. */
  1363. tt_assert(service->desc_next);
  1364. tt_assert(service->desc_next->desc);
  1365. tt_assert(service->desc_next->intro_points.map);
  1366. tt_assert(service->desc_current != service->desc_next);
  1367. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1368. hs_get_time_period_num(0));
  1369. /* This should be untouched, the update descriptor process changes it. */
  1370. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1371. /* Let's remove the next descriptor to simulate a rotation. */
  1372. service_descriptor_free(service->desc_next);
  1373. service->desc_next = NULL;
  1374. build_all_descriptors(now);
  1375. /* Check *next* descriptor. */
  1376. tt_assert(service->desc_next);
  1377. tt_assert(service->desc_next->desc);
  1378. tt_assert(service->desc_next->intro_points.map);
  1379. tt_assert(service->desc_current != service->desc_next);
  1380. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1381. hs_get_next_time_period_num(0));
  1382. /* This should be untouched, the update descriptor process changes it. */
  1383. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1384. done:
  1385. hs_free_all();
  1386. nodelist_free_all();
  1387. }
  1388. /** Test building descriptors. We use this separate function instead of
  1389. * using test_build_update_descriptors because that function is too complex
  1390. * and also too interactive. */
  1391. static void
  1392. test_build_descriptors(void *arg)
  1393. {
  1394. int ret;
  1395. time_t now = time(NULL);
  1396. (void) arg;
  1397. hs_init();
  1398. MOCK(get_or_state,
  1399. get_or_state_replacement);
  1400. MOCK(networkstatus_get_live_consensus,
  1401. mock_networkstatus_get_live_consensus);
  1402. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1403. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  1404. &mock_ns.valid_after);
  1405. tt_int_op(ret, OP_EQ, 0);
  1406. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  1407. &mock_ns.fresh_until);
  1408. tt_int_op(ret, OP_EQ, 0);
  1409. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1410. /* Generate a valid number of fake auth clients when a client authorization
  1411. * is disabled. */
  1412. {
  1413. hs_service_t *service = helper_create_service();
  1414. service_descriptor_free(service->desc_current);
  1415. service->desc_current = NULL;
  1416. build_all_descriptors(now);
  1417. hs_desc_superencrypted_data_t *superencrypted;
  1418. superencrypted = &service->desc_current->desc->superencrypted_data;
  1419. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 16);
  1420. }
  1421. /* Generate a valid number of fake auth clients when the number of
  1422. * clients is zero. */
  1423. {
  1424. hs_service_t *service = helper_create_service_with_clients(0);
  1425. service_descriptor_free(service->desc_current);
  1426. service->desc_current = NULL;
  1427. build_all_descriptors(now);
  1428. hs_desc_superencrypted_data_t *superencrypted;
  1429. superencrypted = &service->desc_current->desc->superencrypted_data;
  1430. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 16);
  1431. }
  1432. /* Generate a valid number of fake auth clients when the number of
  1433. * clients is not a multiple of 16. */
  1434. {
  1435. hs_service_t *service = helper_create_service_with_clients(20);
  1436. service_descriptor_free(service->desc_current);
  1437. service->desc_current = NULL;
  1438. build_all_descriptors(now);
  1439. hs_desc_superencrypted_data_t *superencrypted;
  1440. superencrypted = &service->desc_current->desc->superencrypted_data;
  1441. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 32);
  1442. }
  1443. /* Do not generate any fake desc client when the number of clients is
  1444. * a multiple of 16 but not zero. */
  1445. {
  1446. hs_service_t *service = helper_create_service_with_clients(32);
  1447. service_descriptor_free(service->desc_current);
  1448. service->desc_current = NULL;
  1449. build_all_descriptors(now);
  1450. hs_desc_superencrypted_data_t *superencrypted;
  1451. superencrypted = &service->desc_current->desc->superencrypted_data;
  1452. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 32);
  1453. }
  1454. done:
  1455. hs_free_all();
  1456. }
  1457. static void
  1458. test_upload_descriptors(void *arg)
  1459. {
  1460. int ret;
  1461. time_t now;
  1462. hs_service_t *service;
  1463. (void) arg;
  1464. hs_init();
  1465. MOCK(get_or_state,
  1466. get_or_state_replacement);
  1467. MOCK(networkstatus_get_live_consensus,
  1468. mock_networkstatus_get_live_consensus);
  1469. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1470. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1471. &mock_ns.valid_after);
  1472. tt_int_op(ret, OP_EQ, 0);
  1473. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1474. &mock_ns.fresh_until);
  1475. tt_int_op(ret, OP_EQ, 0);
  1476. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1477. update_approx_time(mock_ns.valid_after+1);
  1478. now = mock_ns.valid_after+1;
  1479. /* Create a service with no descriptor. It's added to the global map. */
  1480. service = hs_service_new(get_options());
  1481. tt_assert(service);
  1482. service->config.version = HS_VERSION_THREE;
  1483. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1484. ed25519_public_key_generate(&service->keys.identity_pk,
  1485. &service->keys.identity_sk);
  1486. /* Register service to global map. */
  1487. ret = register_service(get_hs_service_map(), service);
  1488. tt_int_op(ret, OP_EQ, 0);
  1489. /* But first, build our descriptor. */
  1490. build_all_descriptors(now);
  1491. /* Nothing should happen because we have 0 introduction circuit established
  1492. * and we want (by default) 3 intro points. */
  1493. run_upload_descriptor_event(now);
  1494. /* If no upload happened, this should be untouched. */
  1495. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1496. /* We'll simulate that we've opened our intro point circuit and that we only
  1497. * want one intro point. */
  1498. service->config.num_intro_points = 1;
  1499. /* Set our next upload time after now which will skip the upload. */
  1500. service->desc_current->next_upload_time = now + 1000;
  1501. run_upload_descriptor_event(now);
  1502. /* If no upload happened, this should be untouched. */
  1503. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, now + 1000);
  1504. done:
  1505. hs_free_all();
  1506. UNMOCK(get_or_state);
  1507. }
  1508. /** Global vars used by test_rendezvous1_parsing() */
  1509. static char rend1_payload[RELAY_PAYLOAD_SIZE];
  1510. static size_t rend1_payload_len = 0;
  1511. /** Mock for relay_send_command_from_edge() to send a RENDEZVOUS1 cell. Instead
  1512. * of sending it to the network, instead save it to the global `rend1_payload`
  1513. * variable so that we can inspect it in the test_rendezvous1_parsing()
  1514. * test. */
  1515. static int
  1516. mock_relay_send_rendezvous1(streamid_t stream_id, circuit_t *circ,
  1517. uint8_t relay_command, const char *payload,
  1518. size_t payload_len,
  1519. crypt_path_t *cpath_layer,
  1520. const char *filename, int lineno)
  1521. {
  1522. (void) stream_id;
  1523. (void) circ;
  1524. (void) relay_command;
  1525. (void) cpath_layer;
  1526. (void) filename;
  1527. (void) lineno;
  1528. memcpy(rend1_payload, payload, payload_len);
  1529. rend1_payload_len = payload_len;
  1530. return 0;
  1531. }
  1532. /** Send a RENDEZVOUS1 as a service, and parse it as a client. */
  1533. static void
  1534. test_rendezvous1_parsing(void *arg)
  1535. {
  1536. int retval;
  1537. static const char *test_addr =
  1538. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad.onion";
  1539. hs_service_t *service = NULL;
  1540. origin_circuit_t *service_circ = NULL;
  1541. origin_circuit_t *client_circ = NULL;
  1542. ed25519_keypair_t ip_auth_kp;
  1543. curve25519_keypair_t ephemeral_kp;
  1544. curve25519_keypair_t client_kp;
  1545. curve25519_keypair_t ip_enc_kp;
  1546. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1547. (void) arg;
  1548. MOCK(relay_send_command_from_edge_, mock_relay_send_rendezvous1);
  1549. {
  1550. /* Let's start by setting up the service that will start the rend */
  1551. service = tor_malloc_zero(sizeof(hs_service_t));
  1552. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1553. ed25519_public_key_generate(&service->keys.identity_pk,
  1554. &service->keys.identity_sk);
  1555. memcpy(service->onion_address, test_addr, sizeof(service->onion_address));
  1556. tt_assert(service);
  1557. }
  1558. {
  1559. /* Now let's set up the service rendezvous circuit and its keys. */
  1560. service_circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND,
  1561. flags);
  1562. tor_free(service_circ->hs_ident);
  1563. hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys;
  1564. uint8_t rendezvous_cookie[HS_REND_COOKIE_LEN];
  1565. curve25519_keypair_generate(&ip_enc_kp, 0);
  1566. curve25519_keypair_generate(&ephemeral_kp, 0);
  1567. curve25519_keypair_generate(&client_kp, 0);
  1568. ed25519_keypair_generate(&ip_auth_kp, 0);
  1569. retval = hs_ntor_service_get_rendezvous1_keys(&ip_auth_kp.pubkey,
  1570. &ip_enc_kp,
  1571. &ephemeral_kp,
  1572. &client_kp.pubkey,
  1573. &hs_ntor_rend_cell_keys);
  1574. tt_int_op(retval, OP_EQ, 0);
  1575. memset(rendezvous_cookie, 2, sizeof(rendezvous_cookie));
  1576. service_circ->hs_ident =
  1577. create_rp_circuit_identifier(service, rendezvous_cookie,
  1578. &ephemeral_kp.pubkey,
  1579. &hs_ntor_rend_cell_keys);
  1580. }
  1581. /* Send out the RENDEZVOUS1 and make sure that our mock func worked */
  1582. tt_assert(tor_mem_is_zero(rend1_payload, 32));
  1583. hs_circ_service_rp_has_opened(service, service_circ);
  1584. tt_assert(!tor_mem_is_zero(rend1_payload, 32));
  1585. tt_int_op(rend1_payload_len, OP_EQ, HS_LEGACY_RENDEZVOUS_CELL_SIZE);
  1586. /******************************/
  1587. /** Now let's create the client rendezvous circuit */
  1588. client_circ =
  1589. helper_create_origin_circuit(CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED,
  1590. flags);
  1591. /* fix up its circ ident */
  1592. ed25519_pubkey_copy(&client_circ->hs_ident->intro_auth_pk,
  1593. &ip_auth_kp.pubkey);
  1594. memcpy(&client_circ->hs_ident->rendezvous_client_kp,
  1595. &client_kp, sizeof(client_circ->hs_ident->rendezvous_client_kp));
  1596. memcpy(&client_circ->hs_ident->intro_enc_pk.public_key,
  1597. &ip_enc_kp.pubkey.public_key,
  1598. sizeof(client_circ->hs_ident->intro_enc_pk.public_key));
  1599. /* Now parse the rendezvous2 circuit and make sure it was fine. We are
  1600. * skipping 20 bytes off its payload, since that's the rendezvous cookie
  1601. * which is only present in REND1. */
  1602. retval = handle_rendezvous2(client_circ,
  1603. (uint8_t*)rend1_payload+20,
  1604. rend1_payload_len-20);
  1605. tt_int_op(retval, OP_EQ, 0);
  1606. /* TODO: We are only simulating client/service here. We could also simulate
  1607. * the rendezvous point by plugging in rend_mid_establish_rendezvous(). We
  1608. * would need an extra circuit and some more stuff but it's doable. */
  1609. done:
  1610. circuit_free_(TO_CIRCUIT(service_circ));
  1611. circuit_free_(TO_CIRCUIT(client_circ));
  1612. hs_service_free(service);
  1613. hs_free_all();
  1614. UNMOCK(relay_send_command_from_edge_);
  1615. }
  1616. static void
  1617. test_authorized_client_config_equal(void *arg)
  1618. {
  1619. int ret;
  1620. hs_service_config_t *config1, *config2;
  1621. (void) arg;
  1622. config1 = tor_malloc_zero(sizeof(*config1));
  1623. config2 = tor_malloc_zero(sizeof(*config2));
  1624. /* Both configs are empty. */
  1625. {
  1626. config1->clients = smartlist_new();
  1627. config2->clients = smartlist_new();
  1628. ret = service_authorized_client_config_equal(config1, config2);
  1629. tt_int_op(ret, OP_EQ, 1);
  1630. service_clear_config(config1);
  1631. service_clear_config(config2);
  1632. }
  1633. /* Both configs have exactly the same client config. */
  1634. {
  1635. config1->clients = smartlist_new();
  1636. config2->clients = smartlist_new();
  1637. hs_service_authorized_client_t *client1, *client2;
  1638. client1 = helper_create_authorized_client();
  1639. client2 = helper_create_authorized_client();
  1640. smartlist_add(config1->clients, client1);
  1641. smartlist_add(config1->clients, client2);
  1642. /* We should swap the order of clients here to test that the order
  1643. * does not matter. */
  1644. smartlist_add(config2->clients, helper_clone_authorized_client(client2));
  1645. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1646. ret = service_authorized_client_config_equal(config1, config2);
  1647. tt_int_op(ret, OP_EQ, 1);
  1648. service_clear_config(config1);
  1649. service_clear_config(config2);
  1650. }
  1651. /* The numbers of clients in both configs are not equal. */
  1652. {
  1653. config1->clients = smartlist_new();
  1654. config2->clients = smartlist_new();
  1655. hs_service_authorized_client_t *client1, *client2;
  1656. client1 = helper_create_authorized_client();
  1657. client2 = helper_create_authorized_client();
  1658. smartlist_add(config1->clients, client1);
  1659. smartlist_add(config1->clients, client2);
  1660. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1661. ret = service_authorized_client_config_equal(config1, config2);
  1662. tt_int_op(ret, OP_EQ, 0);
  1663. service_clear_config(config1);
  1664. service_clear_config(config2);
  1665. }
  1666. /* The first config has two distinct clients while the second config
  1667. * has two clients but they are duplicate. */
  1668. {
  1669. config1->clients = smartlist_new();
  1670. config2->clients = smartlist_new();
  1671. hs_service_authorized_client_t *client1, *client2;
  1672. client1 = helper_create_authorized_client();
  1673. client2 = helper_create_authorized_client();
  1674. smartlist_add(config1->clients, client1);
  1675. smartlist_add(config1->clients, client2);
  1676. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1677. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1678. ret = service_authorized_client_config_equal(config1, config2);
  1679. tt_int_op(ret, OP_EQ, 0);
  1680. service_clear_config(config1);
  1681. service_clear_config(config2);
  1682. }
  1683. /* Both configs have totally distinct clients. */
  1684. {
  1685. config1->clients = smartlist_new();
  1686. config2->clients = smartlist_new();
  1687. hs_service_authorized_client_t *client1, *client2, *client3, *client4;
  1688. client1 = helper_create_authorized_client();
  1689. client2 = helper_create_authorized_client();
  1690. client3 = helper_create_authorized_client();
  1691. client4 = helper_create_authorized_client();
  1692. smartlist_add(config1->clients, client1);
  1693. smartlist_add(config1->clients, client2);
  1694. smartlist_add(config2->clients, client3);
  1695. smartlist_add(config2->clients, client4);
  1696. ret = service_authorized_client_config_equal(config1, config2);
  1697. tt_int_op(ret, OP_EQ, 0);
  1698. service_clear_config(config1);
  1699. service_clear_config(config2);
  1700. }
  1701. done:
  1702. tor_free(config1);
  1703. tor_free(config2);
  1704. }
  1705. struct testcase_t hs_service_tests[] = {
  1706. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  1707. NULL, NULL },
  1708. { "load_keys", test_load_keys, TT_FORK,
  1709. NULL, NULL },
  1710. { "client_filename_is_valid", test_client_filename_is_valid, TT_FORK,
  1711. NULL, NULL },
  1712. { "parse_authorized_client", test_parse_authorized_client, TT_FORK,
  1713. NULL, NULL },
  1714. { "load_keys_with_client_auth", test_load_keys_with_client_auth, TT_FORK,
  1715. NULL, NULL },
  1716. { "access_service", test_access_service, TT_FORK,
  1717. NULL, NULL },
  1718. { "service_intro_point", test_service_intro_point, TT_FORK,
  1719. NULL, NULL },
  1720. { "helper_functions", test_helper_functions, TT_FORK,
  1721. NULL, NULL },
  1722. { "intro_circuit_opened", test_intro_circuit_opened, TT_FORK,
  1723. NULL, NULL },
  1724. { "intro_established", test_intro_established, TT_FORK,
  1725. NULL, NULL },
  1726. { "closing_intro_circs", test_closing_intro_circs, TT_FORK,
  1727. NULL, NULL },
  1728. { "rdv_circuit_opened", test_rdv_circuit_opened, TT_FORK,
  1729. NULL, NULL },
  1730. { "introduce2", test_introduce2, TT_FORK,
  1731. NULL, NULL },
  1732. { "service_event", test_service_event, TT_FORK,
  1733. NULL, NULL },
  1734. { "rotate_descriptors", test_rotate_descriptors, TT_FORK,
  1735. NULL, NULL },
  1736. { "build_update_descriptors", test_build_update_descriptors, TT_FORK,
  1737. NULL, NULL },
  1738. { "build_descriptors", test_build_descriptors, TT_FORK,
  1739. NULL, NULL },
  1740. { "upload_descriptors", test_upload_descriptors, TT_FORK,
  1741. NULL, NULL },
  1742. { "rendezvous1_parsing", test_rendezvous1_parsing, TT_FORK,
  1743. NULL, NULL },
  1744. { "authorized_client_config_equal", test_authorized_client_config_equal,
  1745. TT_FORK, NULL, NULL },
  1746. END_OF_TESTCASES
  1747. };