test_hs_service.c 73 KB

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