test_hs_service.c 75 KB

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