test_hs_service.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228
  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. 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(
  171. crypto_digest_get_algorithm(or_circ->cpath->pvt_crypto.f_digest),
  172. OP_EQ, DIGEST_SHA3_256);
  173. tt_int_op(
  174. crypto_digest_get_algorithm(or_circ->cpath->pvt_crypto.b_digest),
  175. OP_EQ, DIGEST_SHA3_256);
  176. tt_assert(or_circ->cpath->pvt_crypto.f_crypto);
  177. tt_assert(or_circ->cpath->pvt_crypto.b_crypto);
  178. /* Ensure that circ purpose was changed */
  179. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  180. done:
  181. circuit_free_(TO_CIRCUIT(or_circ));
  182. }
  183. /* Helper: Return a newly allocated and initialized origin circuit with
  184. * purpose and flags. A default HS identifier is set to an ed25519
  185. * authentication key for introduction point. */
  186. static origin_circuit_t *
  187. helper_create_origin_circuit(int purpose, int flags)
  188. {
  189. origin_circuit_t *circ = NULL;
  190. circ = origin_circuit_init(purpose, flags);
  191. tor_assert(circ);
  192. circ->cpath = tor_malloc_zero(sizeof(crypt_path_t));
  193. circ->cpath->magic = CRYPT_PATH_MAGIC;
  194. circ->cpath->state = CPATH_STATE_OPEN;
  195. circ->cpath->package_window = circuit_initial_package_window();
  196. circ->cpath->deliver_window = CIRCWINDOW_START;
  197. circ->cpath->prev = circ->cpath;
  198. /* Random nonce. */
  199. crypto_rand(circ->cpath->prev->rend_circ_nonce, DIGEST_LEN);
  200. /* Create a default HS identifier. */
  201. circ->hs_ident = tor_malloc_zero(sizeof(hs_ident_circuit_t));
  202. return circ;
  203. }
  204. /* Helper: Return a newly allocated authorized client object with
  205. * and a newly generated public key. */
  206. static hs_service_authorized_client_t *
  207. helper_create_authorized_client(void)
  208. {
  209. int ret;
  210. hs_service_authorized_client_t *client;
  211. curve25519_secret_key_t seckey;
  212. client = tor_malloc_zero(sizeof(hs_service_authorized_client_t));
  213. ret = curve25519_secret_key_generate(&seckey, 0);
  214. tt_int_op(ret, OP_EQ, 0);
  215. curve25519_public_key_generate(&client->client_pk, &seckey);
  216. done:
  217. return client;
  218. }
  219. /* Helper: Return a newly allocated authorized client object with the
  220. * same client name and the same public key as the given client. */
  221. static hs_service_authorized_client_t *
  222. helper_clone_authorized_client(const hs_service_authorized_client_t *client)
  223. {
  224. hs_service_authorized_client_t *client_out;
  225. tor_assert(client);
  226. client_out = tor_malloc_zero(sizeof(hs_service_authorized_client_t));
  227. memcpy(client_out->client_pk.public_key,
  228. client->client_pk.public_key, CURVE25519_PUBKEY_LEN);
  229. return client_out;
  230. }
  231. /* Helper: Return a newly allocated service object with the identity keypair
  232. * sets and the current descriptor. Then register it to the global map.
  233. * Caller should use hs_free_all() to free this service or remove it from the
  234. * global map before freeing. */
  235. static hs_service_t *
  236. helper_create_service(void)
  237. {
  238. /* Set a service for this circuit. */
  239. hs_service_t *service = hs_service_new(get_options());
  240. tor_assert(service);
  241. service->config.version = HS_VERSION_THREE;
  242. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  243. ed25519_public_key_generate(&service->keys.identity_pk,
  244. &service->keys.identity_sk);
  245. service->desc_current = service_descriptor_new();
  246. tt_assert(service->desc_current);
  247. /* Register service to global map. */
  248. int ret = register_service(get_hs_service_map(), service);
  249. tt_int_op(ret, OP_EQ, 0);
  250. done:
  251. return service;
  252. }
  253. /* Helper: Deallocate a given service object, its child objects and
  254. * remove it from onion service map.
  255. * */
  256. static void
  257. helper_destroy_service(hs_service_t *service)
  258. {
  259. if (!service)
  260. return;
  261. remove_service(get_hs_service_map(), service);
  262. hs_service_free(service);
  263. }
  264. /* Helper: Return a newly allocated service object with clients. */
  265. static hs_service_t *
  266. helper_create_service_with_clients(int num_clients)
  267. {
  268. int i;
  269. hs_service_t *service = helper_create_service();
  270. tt_assert(service);
  271. service->config.is_client_auth_enabled = 1;
  272. service->config.clients = smartlist_new();
  273. for (i = 0; i < num_clients; i++) {
  274. hs_service_authorized_client_t *client;
  275. client = helper_create_authorized_client();
  276. smartlist_add(service->config.clients, client);
  277. }
  278. done:
  279. return service;
  280. }
  281. /* Helper: Return a newly allocated service intro point with two link
  282. * specifiers, one IPv4 and one legacy ID set to As. */
  283. static hs_service_intro_point_t *
  284. helper_create_service_ip(void)
  285. {
  286. link_specifier_t *ls;
  287. hs_service_intro_point_t *ip = service_intro_point_new(NULL);
  288. tor_assert(ip);
  289. /* Add a first unused link specifier. */
  290. ls = link_specifier_new();
  291. link_specifier_set_ls_type(ls, LS_IPV4);
  292. smartlist_add(ip->base.link_specifiers, ls);
  293. /* Add a second link specifier used by a test. */
  294. ls = link_specifier_new();
  295. link_specifier_set_ls_type(ls, LS_LEGACY_ID);
  296. memset(link_specifier_getarray_un_legacy_id(ls), 'A',
  297. link_specifier_getlen_un_legacy_id(ls));
  298. smartlist_add(ip->base.link_specifiers, ls);
  299. return ip;
  300. }
  301. static void
  302. test_load_keys(void *arg)
  303. {
  304. int ret;
  305. char *conf = NULL;
  306. char *hsdir_v2 = tor_strdup(get_fname("hs2"));
  307. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  308. char addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  309. (void) arg;
  310. /* We'll register two services, a v2 and a v3, then we'll load keys and
  311. * validate that both are in a correct state. */
  312. hs_init();
  313. #define conf_fmt \
  314. "HiddenServiceDir %s\n" \
  315. "HiddenServiceVersion %d\n" \
  316. "HiddenServicePort 65535\n"
  317. /* v2 service. */
  318. tor_asprintf(&conf, conf_fmt, hsdir_v2, HS_VERSION_TWO);
  319. ret = helper_config_service(conf);
  320. tor_free(conf);
  321. tt_int_op(ret, OP_EQ, 0);
  322. /* This one should now be registered into the v2 list. */
  323. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 0);
  324. tt_int_op(rend_num_services(), OP_EQ, 1);
  325. /* v3 service. */
  326. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  327. ret = helper_config_service(conf);
  328. tor_free(conf);
  329. tt_int_op(ret, OP_EQ, 0);
  330. /* It's in staging? */
  331. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  332. #undef conf_fmt
  333. /* Load the keys for these. After that, the v3 service should be registered
  334. * in the global map. */
  335. hs_service_load_all_keys();
  336. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  337. hs_service_t *s = get_first_service();
  338. tt_assert(s);
  339. /* Ok we have the service object. Validate few things. */
  340. tt_assert(!fast_mem_is_zero(s->onion_address, sizeof(s->onion_address)));
  341. tt_int_op(hs_address_is_valid(s->onion_address), OP_EQ, 1);
  342. tt_assert(!fast_mem_is_zero((char *) s->keys.identity_sk.seckey,
  343. ED25519_SECKEY_LEN));
  344. tt_assert(!fast_mem_is_zero((char *) s->keys.identity_pk.pubkey,
  345. ED25519_PUBKEY_LEN));
  346. /* Check onion address from identity key. */
  347. hs_build_address(&s->keys.identity_pk, s->config.version, addr);
  348. tt_int_op(hs_address_is_valid(addr), OP_EQ, 1);
  349. tt_str_op(addr, OP_EQ, s->onion_address);
  350. /* Check that the is_client_auth_enabled is not set. */
  351. tt_assert(!s->config.is_client_auth_enabled);
  352. done:
  353. tor_free(hsdir_v2);
  354. tor_free(hsdir_v3);
  355. hs_free_all();
  356. }
  357. static void
  358. test_client_filename_is_valid(void *arg)
  359. {
  360. (void) arg;
  361. /* Valid file name. */
  362. tt_assert(client_filename_is_valid("a.auth"));
  363. /* Valid file name with special character. */
  364. tt_assert(client_filename_is_valid("a-.auth"));
  365. /* Invalid extension. */
  366. tt_assert(!client_filename_is_valid("a.ath"));
  367. /* Nothing before the extension. */
  368. tt_assert(!client_filename_is_valid(".auth"));
  369. done:
  370. ;
  371. }
  372. static void
  373. test_parse_authorized_client(void *arg)
  374. {
  375. hs_service_authorized_client_t *client = NULL;
  376. (void) arg;
  377. /* Valid authorized client. */
  378. client = parse_authorized_client(
  379. "descriptor:x25519:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja");
  380. tt_assert(client);
  381. /* Wrong number of fields. */
  382. tt_assert(!parse_authorized_client("a:b:c:d:e"));
  383. /* Wrong auth type. */
  384. tt_assert(!parse_authorized_client(
  385. "x:x25519:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  386. /* Wrong key type. */
  387. tt_assert(!parse_authorized_client(
  388. "descriptor:x:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  389. /* Some malformed string. */
  390. tt_assert(!parse_authorized_client("descriptor:x25519:aa=="));
  391. tt_assert(!parse_authorized_client("descriptor:"));
  392. tt_assert(!parse_authorized_client("descriptor:x25519"));
  393. tt_assert(!parse_authorized_client("descriptor:x25519:"));
  394. tt_assert(!parse_authorized_client(""));
  395. done:
  396. service_authorized_client_free(client);
  397. }
  398. static char *
  399. mock_read_file_to_str(const char *filename, int flags, struct stat *stat_out)
  400. {
  401. char *ret = NULL;
  402. (void) flags;
  403. (void) stat_out;
  404. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  405. "authorized_clients" PATH_SEPARATOR
  406. "client1.auth"))) {
  407. ret = tor_strdup("descriptor:x25519:"
  408. "dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja");
  409. goto done;
  410. }
  411. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  412. "authorized_clients" PATH_SEPARATOR
  413. "dummy.xxx"))) {
  414. ret = tor_strdup("descriptor:x25519:"
  415. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  416. goto done;
  417. }
  418. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  419. "authorized_clients" PATH_SEPARATOR
  420. "client2.auth"))) {
  421. ret = tor_strdup("descriptor:x25519:"
  422. "okoi2gml3wd6x7jganlk5d66xxyjgg24sxw4y7javx4giqr66zta");
  423. goto done;
  424. }
  425. done:
  426. return ret;
  427. }
  428. static smartlist_t *
  429. mock_tor_listdir(const char *dirname)
  430. {
  431. smartlist_t *file_list = smartlist_new();
  432. (void) dirname;
  433. smartlist_add(file_list, tor_strdup("client1.auth"));
  434. smartlist_add(file_list, tor_strdup("dummy.xxx"));
  435. smartlist_add(file_list, tor_strdup("client2.auth"));
  436. return file_list;
  437. }
  438. static void
  439. test_load_keys_with_client_auth(void *arg)
  440. {
  441. int ret;
  442. char *conf = NULL;
  443. smartlist_t *pubkey_b32_list = smartlist_new();
  444. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  445. hs_service_t *service;
  446. (void) arg;
  447. hs_init();
  448. smartlist_add(pubkey_b32_list, tor_strdup(
  449. "dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  450. smartlist_add(pubkey_b32_list, tor_strdup(
  451. "okoi2gml3wd6x7jganlk5d66xxyjgg24sxw4y7javx4giqr66zta"));
  452. #define conf_fmt \
  453. "HiddenServiceDir %s\n" \
  454. "HiddenServiceVersion %d\n" \
  455. "HiddenServicePort 65534\n"
  456. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  457. ret = helper_config_service(conf);
  458. tor_free(conf);
  459. tt_int_op(ret, OP_EQ, 0);
  460. /* It's in staging? */
  461. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  462. #undef conf_fmt
  463. MOCK(read_file_to_str, mock_read_file_to_str);
  464. MOCK(tor_listdir, mock_tor_listdir);
  465. /* Load the keys for these. After that, the v3 service should be registered
  466. * in the global map. */
  467. hs_service_load_all_keys();
  468. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  469. service = get_first_service();
  470. tt_assert(service);
  471. tt_assert(service->config.clients);
  472. tt_int_op(smartlist_len(service->config.clients), OP_EQ,
  473. smartlist_len(pubkey_b32_list));
  474. /* Test that the is_client_auth_enabled flag is set. */
  475. tt_assert(service->config.is_client_auth_enabled);
  476. /* Test that the keys in clients are correct. */
  477. SMARTLIST_FOREACH_BEGIN(pubkey_b32_list, char *, pubkey_b32) {
  478. curve25519_public_key_t pubkey;
  479. /* This flag will be set if the key is found in clients. */
  480. int is_found = 0;
  481. base32_decode((char *) pubkey.public_key, sizeof(pubkey.public_key),
  482. pubkey_b32, strlen(pubkey_b32));
  483. SMARTLIST_FOREACH_BEGIN(service->config.clients,
  484. hs_service_authorized_client_t *, client) {
  485. if (tor_memeq(&pubkey, &client->client_pk, sizeof(pubkey))) {
  486. is_found = 1;
  487. break;
  488. }
  489. } SMARTLIST_FOREACH_END(client);
  490. tt_assert(is_found);
  491. } SMARTLIST_FOREACH_END(pubkey_b32);
  492. done:
  493. SMARTLIST_FOREACH(pubkey_b32_list, char *, s, tor_free(s));
  494. smartlist_free(pubkey_b32_list);
  495. tor_free(hsdir_v3);
  496. hs_free_all();
  497. UNMOCK(read_file_to_str);
  498. UNMOCK(tor_listdir);
  499. }
  500. static void
  501. test_access_service(void *arg)
  502. {
  503. int ret;
  504. char *conf = NULL;
  505. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  506. hs_service_ht *global_map;
  507. hs_service_t *s = NULL;
  508. (void) arg;
  509. /* We'll register two services, a v2 and a v3, then we'll load keys and
  510. * validate that both are in a correct state. */
  511. hs_init();
  512. #define conf_fmt \
  513. "HiddenServiceDir %s\n" \
  514. "HiddenServiceVersion %d\n" \
  515. "HiddenServicePort 65535\n"
  516. /* v3 service. */
  517. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  518. ret = helper_config_service(conf);
  519. tor_free(conf);
  520. tt_int_op(ret, OP_EQ, 0);
  521. /* It's in staging? */
  522. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  523. /* Load the keys for these. After that, the v3 service should be registered
  524. * in the global map. */
  525. hs_service_load_all_keys();
  526. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  527. s = get_first_service();
  528. tt_assert(s);
  529. global_map = get_hs_service_map();
  530. tt_assert(global_map);
  531. /* From here, we'll try the service accessors. */
  532. hs_service_t *query = find_service(global_map, &s->keys.identity_pk);
  533. tt_assert(query);
  534. tt_mem_op(query, OP_EQ, s, sizeof(hs_service_t));
  535. /* Remove service, check if it actually works and then put it back. */
  536. remove_service(global_map, s);
  537. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  538. query = find_service(global_map, &s->keys.identity_pk);
  539. tt_ptr_op(query, OP_EQ, NULL);
  540. /* Register back the service in the map. */
  541. ret = register_service(global_map, s);
  542. tt_int_op(ret, OP_EQ, 0);
  543. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  544. /* Twice should fail. */
  545. ret = register_service(global_map, s);
  546. tt_int_op(ret, OP_EQ, -1);
  547. /* Remove service from map so we don't double free on cleanup. */
  548. remove_service(global_map, s);
  549. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  550. query = find_service(global_map, &s->keys.identity_pk);
  551. tt_ptr_op(query, OP_EQ, NULL);
  552. /* Let's try to remove twice for fun. */
  553. setup_full_capture_of_logs(LOG_WARN);
  554. remove_service(global_map, s);
  555. expect_log_msg_containing("Could not find service in the global map");
  556. teardown_capture_of_logs();
  557. done:
  558. hs_service_free(s);
  559. tor_free(hsdir_v3);
  560. hs_free_all();
  561. }
  562. /** Test that we can create intro point objects, index them and find them */
  563. static void
  564. test_service_intro_point(void *arg)
  565. {
  566. hs_service_t *service = NULL;
  567. hs_service_intro_point_t *ip = NULL;
  568. (void) arg;
  569. update_approx_time(1481621834);
  570. /* Test simple creation of an object. */
  571. {
  572. time_t now = approx_time();
  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. /* Register circuit in the circuitmap . */
  945. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  946. &ip->auth_key_kp.pubkey);
  947. tmp_circ =
  948. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  949. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  950. /* Pretend that intro point has failed too much */
  951. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES+1;
  952. /* Now pretend we are freeing this intro circuit. We want to see that our
  953. * destructor is not gonna kill our intro point structure since that's the
  954. * job of the cleanup routine. */
  955. circuit_free_(TO_CIRCUIT(intro_circ));
  956. intro_circ = NULL;
  957. entry = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  958. tt_assert(entry);
  959. /* The free should also remove the circuit from the circuitmap. */
  960. tmp_circ =
  961. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  962. tt_assert(!tmp_circ);
  963. /* Now pretend that a new intro point circ was launched and opened. Check
  964. * that the intro point will be established correctly. */
  965. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  966. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk);
  967. ed25519_pubkey_copy(&intro_circ->hs_ident->intro_auth_pk,
  968. &ip->auth_key_kp.pubkey);
  969. /* Register circuit in the circuitmap . */
  970. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  971. &ip->auth_key_kp.pubkey);
  972. tmp_circ =
  973. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  974. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  975. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_EQ, 0);
  976. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  977. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_NE, 0);
  978. /* At this point, we should not be able to find it in the circuitmap. */
  979. tmp_circ =
  980. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  981. tt_assert(!tmp_circ);
  982. done:
  983. if (intro_circ) {
  984. circuit_free_(TO_CIRCUIT(intro_circ));
  985. }
  986. /* Frees the service object. */
  987. if (service) {
  988. remove_service(get_hs_service_map(), service);
  989. hs_service_free(service);
  990. }
  991. hs_free_all();
  992. UNMOCK(assert_circuit_ok);
  993. }
  994. /** Test sending and receiving introduce2 cells */
  995. static void
  996. test_introduce2(void *arg)
  997. {
  998. int ret;
  999. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1000. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  1001. origin_circuit_t *circ = NULL;
  1002. hs_service_t *service = NULL;
  1003. hs_service_intro_point_t *ip = NULL;
  1004. (void) arg;
  1005. hs_init();
  1006. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  1007. MOCK(get_or_state,
  1008. get_or_state_replacement);
  1009. dummy_state = or_state_new();
  1010. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  1011. tt_assert(circ);
  1012. /* Test a wrong purpose. */
  1013. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  1014. setup_full_capture_of_logs(LOG_WARN);
  1015. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1016. tt_int_op(ret, OP_EQ, -1);
  1017. expect_log_msg_containing("Received an INTRODUCE2 cell on a "
  1018. "non introduction circuit of purpose");
  1019. teardown_capture_of_logs();
  1020. /* Back to normal. */
  1021. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  1022. /* No service associated to it. */
  1023. setup_full_capture_of_logs(LOG_WARN);
  1024. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1025. tt_int_op(ret, OP_EQ, -1);
  1026. expect_log_msg_containing("Unknown service identity key");
  1027. teardown_capture_of_logs();
  1028. /* Set a service for this circuit. */
  1029. service = helper_create_service();
  1030. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  1031. &service->keys.identity_pk);
  1032. /* No introduction point associated to it. */
  1033. setup_full_capture_of_logs(LOG_WARN);
  1034. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1035. tt_int_op(ret, OP_EQ, -1);
  1036. expect_log_msg_containing("Unknown introduction auth key when handling "
  1037. "an INTRODUCE2 cell on circuit");
  1038. teardown_capture_of_logs();
  1039. /* Set an IP object now for this circuit. */
  1040. {
  1041. ip = helper_create_service_ip();
  1042. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1043. /* Update ident to contain the intro point auth key. */
  1044. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  1045. &ip->auth_key_kp.pubkey);
  1046. }
  1047. /* This will fail because receiving an INTRODUCE2 cell implies a valid cell
  1048. * and then launching circuits so let's not do that and instead test that
  1049. * behaviour differently. */
  1050. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1051. tt_int_op(ret, OP_EQ, -1);
  1052. tt_u64_op(ip->introduce2_count, OP_EQ, 0);
  1053. done:
  1054. or_state_free(dummy_state);
  1055. dummy_state = NULL;
  1056. if (circ)
  1057. circuit_free_(TO_CIRCUIT(circ));
  1058. if (service) {
  1059. remove_service(get_hs_service_map(), service);
  1060. hs_service_free(service);
  1061. }
  1062. hs_free_all();
  1063. UNMOCK(circuit_mark_for_close_);
  1064. }
  1065. /** Test basic hidden service housekeeping operations (maintaining intro
  1066. * points, etc) */
  1067. static void
  1068. test_service_event(void *arg)
  1069. {
  1070. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1071. time_t now = time(NULL);
  1072. hs_service_t *service;
  1073. origin_circuit_t *circ = NULL;
  1074. (void) arg;
  1075. hs_init();
  1076. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  1077. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  1078. /* Set a service for this circuit. */
  1079. service = helper_create_service();
  1080. tt_assert(service);
  1081. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  1082. &service->keys.identity_pk);
  1083. /* Currently this consists of cleaning invalid intro points. So adding IPs
  1084. * here that should get cleaned up. */
  1085. {
  1086. hs_service_intro_point_t *ip = helper_create_service_ip();
  1087. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1088. /* This run will remove the IP because we have no circuits nor node_t
  1089. * associated with it. */
  1090. run_housekeeping_event(now);
  1091. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1092. OP_EQ, 0);
  1093. /* We'll trigger a removal because we've reached our maximum amount of
  1094. * times we should retry a circuit. For this, we need to have a node_t
  1095. * that matches the identity of this IP. */
  1096. routerinfo_t ri;
  1097. memset(&ri, 0, sizeof(ri));
  1098. ip = helper_create_service_ip();
  1099. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1100. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1101. /* This triggers a node_t creation. */
  1102. tt_assert(nodelist_set_routerinfo(&ri, NULL));
  1103. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  1104. run_housekeeping_event(now);
  1105. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1106. OP_EQ, 0);
  1107. /* No removal but no circuit so this means the IP object will stay in the
  1108. * descriptor map so we can retry it. */
  1109. ip = helper_create_service_ip();
  1110. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1111. ip->circuit_established = 1; /* We'll test that, it MUST be 0 after. */
  1112. run_housekeeping_event(now);
  1113. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1114. OP_EQ, 1);
  1115. /* No removal if we have an established circuit after retries. */
  1116. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  1117. run_housekeeping_event(now);
  1118. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1119. OP_EQ, 1);
  1120. /* Remove the IP object at once for the next test. */
  1121. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  1122. ip->circuit_established = 0;
  1123. run_housekeeping_event(now);
  1124. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1125. OP_EQ, 0);
  1126. /* Now, we'll create an IP with a registered circuit. The IP object
  1127. * shouldn't go away. */
  1128. ip = helper_create_service_ip();
  1129. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1130. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  1131. &ip->auth_key_kp.pubkey);
  1132. hs_circuitmap_register_intro_circ_v3_service_side(
  1133. circ, &ip->auth_key_kp.pubkey);
  1134. run_housekeeping_event(now);
  1135. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1136. OP_EQ, 1);
  1137. /* We'll mangle the IP object to expire. */
  1138. ip->time_to_expire = now;
  1139. run_housekeeping_event(now);
  1140. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1141. OP_EQ, 0);
  1142. }
  1143. done:
  1144. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  1145. circuit_free_(TO_CIRCUIT(circ));
  1146. if (service) {
  1147. remove_service(get_hs_service_map(), service);
  1148. hs_service_free(service);
  1149. }
  1150. hs_free_all();
  1151. UNMOCK(circuit_mark_for_close_);
  1152. }
  1153. /** Test that we rotate descriptors correctly. */
  1154. static void
  1155. test_rotate_descriptors(void *arg)
  1156. {
  1157. int ret;
  1158. time_t next_rotation_time, now;
  1159. hs_service_t *service = NULL;
  1160. hs_service_descriptor_t *desc_next;
  1161. (void) arg;
  1162. dummy_state = or_state_new();
  1163. hs_init();
  1164. MOCK(get_or_state, get_or_state_replacement);
  1165. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  1166. MOCK(networkstatus_get_live_consensus,
  1167. mock_networkstatus_get_live_consensus);
  1168. /* Descriptor rotation happens with a consensus with a new SRV. */
  1169. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1170. &mock_ns.valid_after);
  1171. tt_int_op(ret, OP_EQ, 0);
  1172. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1173. &mock_ns.fresh_until);
  1174. tt_int_op(ret, OP_EQ, 0);
  1175. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1176. update_approx_time(mock_ns.valid_after+1);
  1177. now = mock_ns.valid_after+1;
  1178. /* Create a service with a default descriptor and state. It's added to the
  1179. * global map. */
  1180. service = helper_create_service();
  1181. service_descriptor_free(service->desc_current);
  1182. service->desc_current = NULL;
  1183. /* This triggers a build for both descriptors. The time now is only used in
  1184. * the descriptor certificate which is important to be now else the decoding
  1185. * will complain that the cert has expired if we use valid_after. */
  1186. build_all_descriptors(now);
  1187. tt_assert(service->desc_current);
  1188. tt_assert(service->desc_next);
  1189. /* Tweak our service next rotation time so we can use a custom time. */
  1190. service->state.next_rotation_time = next_rotation_time =
  1191. mock_ns.valid_after + (11 * 60 * 60);
  1192. /* Nothing should happen, we are not at a new SRV. Our next rotation time
  1193. * should be untouched. */
  1194. rotate_all_descriptors(mock_ns.valid_after);
  1195. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1196. tt_assert(service->desc_current);
  1197. tt_assert(service->desc_next);
  1198. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1199. hs_get_previous_time_period_num(0));
  1200. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1201. hs_get_time_period_num(0));
  1202. /* Keep a reference so we can compare it after rotation to the current. */
  1203. desc_next = service->desc_next;
  1204. /* Going right after a new SRV. */
  1205. ret = parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC",
  1206. &mock_ns.valid_after);
  1207. tt_int_op(ret, OP_EQ, 0);
  1208. ret = parse_rfc1123_time("Sat, 27 Oct 1985 02:00:00 UTC",
  1209. &mock_ns.fresh_until);
  1210. tt_int_op(ret, OP_EQ, 0);
  1211. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1212. update_approx_time(mock_ns.valid_after+1);
  1213. now = mock_ns.valid_after+1;
  1214. /* Note down what to expect for the next rotation time which is 01:00 + 23h
  1215. * meaning 00:00:00. */
  1216. next_rotation_time = mock_ns.valid_after + (23 * 60 * 60);
  1217. /* We should have our next rotation time modified, our current descriptor
  1218. * cleaned up and the next descriptor becoming the current. */
  1219. rotate_all_descriptors(mock_ns.valid_after);
  1220. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1221. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1222. tt_assert(service->desc_next == NULL);
  1223. /* A second time should do nothing. */
  1224. rotate_all_descriptors(mock_ns.valid_after);
  1225. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1226. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1227. tt_assert(service->desc_next == NULL);
  1228. build_all_descriptors(now);
  1229. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1230. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1231. hs_get_time_period_num(0));
  1232. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1233. hs_get_next_time_period_num(0));
  1234. tt_assert(service->desc_next);
  1235. done:
  1236. if (service) {
  1237. remove_service(get_hs_service_map(), service);
  1238. hs_service_free(service);
  1239. }
  1240. hs_free_all();
  1241. UNMOCK(get_or_state);
  1242. UNMOCK(circuit_mark_for_close_);
  1243. UNMOCK(networkstatus_get_live_consensus);
  1244. }
  1245. /** Test building descriptors: picking intro points, setting up their link
  1246. * specifiers, etc. */
  1247. static void
  1248. test_build_update_descriptors(void *arg)
  1249. {
  1250. int ret;
  1251. node_t *node;
  1252. hs_service_t *service = NULL;
  1253. hs_service_intro_point_t *ip_cur, *ip_next;
  1254. routerinfo_t ri;
  1255. (void) arg;
  1256. hs_init();
  1257. MOCK(get_or_state,
  1258. get_or_state_replacement);
  1259. MOCK(networkstatus_get_live_consensus,
  1260. mock_networkstatus_get_live_consensus);
  1261. dummy_state = or_state_new();
  1262. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  1263. &mock_ns.valid_after);
  1264. tt_int_op(ret, OP_EQ, 0);
  1265. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  1266. &mock_ns.fresh_until);
  1267. tt_int_op(ret, OP_EQ, 0);
  1268. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1269. update_approx_time(mock_ns.valid_after+1);
  1270. time_t now = mock_ns.valid_after+1;
  1271. /* Create a service without a current descriptor to trigger a build. */
  1272. service = helper_create_service();
  1273. tt_assert(service);
  1274. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1275. service_descriptor_free(service->desc_current);
  1276. service->desc_current = NULL;
  1277. /* We have a fresh service so this should trigger a build for both
  1278. * descriptors for specific time period that we'll test. */
  1279. build_all_descriptors(now);
  1280. /* Check *current* descriptor. */
  1281. tt_assert(service->desc_current);
  1282. tt_assert(service->desc_current->desc);
  1283. tt_assert(service->desc_current->intro_points.map);
  1284. /* The current time period is the one expected when starting at 03:00. */
  1285. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1286. hs_get_time_period_num(0));
  1287. /* This should be untouched, the update descriptor process changes it. */
  1288. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1289. /* Check *next* descriptor. */
  1290. tt_assert(service->desc_next);
  1291. tt_assert(service->desc_next->desc);
  1292. tt_assert(service->desc_next->intro_points.map);
  1293. tt_assert(service->desc_current != service->desc_next);
  1294. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1295. hs_get_next_time_period_num(0));
  1296. /* This should be untouched, the update descriptor process changes it. */
  1297. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1298. /* Time to test the update of those descriptors. At first, we have no node
  1299. * in the routerlist so this will find NO suitable node for the IPs. */
  1300. setup_full_capture_of_logs(LOG_INFO);
  1301. update_all_descriptors_intro_points(now);
  1302. expect_log_msg_containing("Unable to find a suitable node to be an "
  1303. "introduction point for service");
  1304. teardown_capture_of_logs();
  1305. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1306. OP_EQ, 0);
  1307. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1308. OP_EQ, 0);
  1309. /* Now, we'll setup a node_t. */
  1310. {
  1311. tor_addr_t ipv4_addr;
  1312. curve25519_secret_key_t curve25519_secret_key;
  1313. memset(&ri, 0, sizeof(routerinfo_t));
  1314. tor_addr_parse(&ipv4_addr, "127.0.0.1");
  1315. ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  1316. ri.or_port = 1337;
  1317. ri.purpose = ROUTER_PURPOSE_GENERAL;
  1318. /* Ugly yes but we never free the "ri" object so this just makes things
  1319. * easier. */
  1320. ri.protocol_list = (char *) "HSDir=1-2 LinkAuth=3";
  1321. summarize_protover_flags(&ri.pv, ri.protocol_list, NULL);
  1322. ret = curve25519_secret_key_generate(&curve25519_secret_key, 0);
  1323. tt_int_op(ret, OP_EQ, 0);
  1324. ri.onion_curve25519_pkey =
  1325. tor_malloc_zero(sizeof(curve25519_public_key_t));
  1326. ri.onion_pkey = tor_malloc_zero(140);
  1327. curve25519_public_key_generate(ri.onion_curve25519_pkey,
  1328. &curve25519_secret_key);
  1329. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1330. /* Setup ed25519 identity */
  1331. ed25519_keypair_t kp1;
  1332. ed25519_keypair_generate(&kp1, 0);
  1333. ri.cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  1334. tt_assert(ri.cache_info.signing_key_cert);
  1335. ed25519_pubkey_copy(&ri.cache_info.signing_key_cert->signing_key,
  1336. &kp1.pubkey);
  1337. nodelist_set_routerinfo(&ri, NULL);
  1338. node = node_get_mutable_by_id(ri.cache_info.identity_digest);
  1339. tt_assert(node);
  1340. node->is_running = node->is_valid = node->is_fast = node->is_stable = 1;
  1341. }
  1342. /* We have to set this, or the lack of microdescriptors for these
  1343. * nodes will make them unusable. */
  1344. get_options_mutable()->UseMicrodescriptors = 0;
  1345. /* We expect to pick only one intro point from the node above. */
  1346. setup_full_capture_of_logs(LOG_INFO);
  1347. update_all_descriptors_intro_points(now);
  1348. tor_free(node->ri->onion_curve25519_pkey); /* Avoid memleak. */
  1349. tor_free(node->ri->cache_info.signing_key_cert);
  1350. tor_free(node->ri->onion_pkey);
  1351. expect_log_msg_containing("just picked 1 intro points and wanted 3 for next "
  1352. "descriptor. It currently has 0 intro points. "
  1353. "Launching ESTABLISH_INTRO circuit shortly.");
  1354. teardown_capture_of_logs();
  1355. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1356. OP_EQ, 1);
  1357. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1358. OP_EQ, 1);
  1359. /* Get the IP object. Because we don't have the auth key of the IP, we can't
  1360. * query it so get the first element in the map. */
  1361. {
  1362. void *obj = NULL;
  1363. const uint8_t *key;
  1364. digest256map_iter_t *iter =
  1365. digest256map_iter_init(service->desc_current->intro_points.map);
  1366. digest256map_iter_get(iter, &key, &obj);
  1367. tt_assert(obj);
  1368. ip_cur = obj;
  1369. /* Get also the IP from the next descriptor. We'll make sure it's not the
  1370. * same object as in the current descriptor. */
  1371. iter = digest256map_iter_init(service->desc_next->intro_points.map);
  1372. digest256map_iter_get(iter, &key, &obj);
  1373. tt_assert(obj);
  1374. ip_next = obj;
  1375. }
  1376. tt_mem_op(ip_cur, OP_NE, ip_next, sizeof(hs_desc_intro_point_t));
  1377. /* We won't test the service IP object because there is a specific test
  1378. * already for this but we'll make sure that the state is coherent.*/
  1379. /* Three link specifiers are mandatoy so make sure we do have them. */
  1380. tt_int_op(smartlist_len(ip_cur->base.link_specifiers), OP_EQ, 3);
  1381. /* Make sure we have a valid encryption keypair generated when we pick an
  1382. * intro point in the update process. */
  1383. tt_assert(!fast_mem_is_zero((char *) ip_cur->enc_key_kp.seckey.secret_key,
  1384. CURVE25519_SECKEY_LEN));
  1385. tt_assert(!fast_mem_is_zero((char *) ip_cur->enc_key_kp.pubkey.public_key,
  1386. CURVE25519_PUBKEY_LEN));
  1387. tt_u64_op(ip_cur->time_to_expire, OP_GE, now +
  1388. INTRO_POINT_LIFETIME_MIN_SECONDS);
  1389. tt_u64_op(ip_cur->time_to_expire, OP_LE, now +
  1390. INTRO_POINT_LIFETIME_MAX_SECONDS);
  1391. /* Now, we will try to set up a service after a new time period has started
  1392. * and see if it behaves as expected. */
  1393. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1394. &mock_ns.valid_after);
  1395. tt_int_op(ret, OP_EQ, 0);
  1396. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1397. &mock_ns.fresh_until);
  1398. tt_int_op(ret, OP_EQ, 0);
  1399. update_approx_time(mock_ns.valid_after+1);
  1400. now = mock_ns.valid_after+1;
  1401. /* Create a service without a current descriptor to trigger a build. */
  1402. service = helper_create_service();
  1403. tt_assert(service);
  1404. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1405. service_descriptor_free(service->desc_current);
  1406. service->desc_current = NULL;
  1407. /* We have a fresh service so this should trigger a build for both
  1408. * descriptors for specific time period that we'll test. */
  1409. build_all_descriptors(now);
  1410. /* Check *current* descriptor. */
  1411. tt_assert(service->desc_current);
  1412. tt_assert(service->desc_current->desc);
  1413. tt_assert(service->desc_current->intro_points.map);
  1414. /* This should be for the previous time period. */
  1415. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1416. hs_get_previous_time_period_num(0));
  1417. /* This should be untouched, the update descriptor process changes it. */
  1418. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1419. /* Check *next* descriptor. */
  1420. tt_assert(service->desc_next);
  1421. tt_assert(service->desc_next->desc);
  1422. tt_assert(service->desc_next->intro_points.map);
  1423. tt_assert(service->desc_current != service->desc_next);
  1424. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1425. hs_get_time_period_num(0));
  1426. /* This should be untouched, the update descriptor process changes it. */
  1427. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1428. /* Let's remove the next descriptor to simulate a rotation. */
  1429. service_descriptor_free(service->desc_next);
  1430. service->desc_next = NULL;
  1431. build_all_descriptors(now);
  1432. /* Check *next* descriptor. */
  1433. tt_assert(service->desc_next);
  1434. tt_assert(service->desc_next->desc);
  1435. tt_assert(service->desc_next->intro_points.map);
  1436. tt_assert(service->desc_current != service->desc_next);
  1437. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1438. hs_get_next_time_period_num(0));
  1439. /* This should be untouched, the update descriptor process changes it. */
  1440. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1441. done:
  1442. if (service) {
  1443. remove_service(get_hs_service_map(), service);
  1444. hs_service_free(service);
  1445. }
  1446. hs_free_all();
  1447. nodelist_free_all();
  1448. }
  1449. /** Test building descriptors. We use this separate function instead of
  1450. * using test_build_update_descriptors because that function is too complex
  1451. * and also too interactive. */
  1452. static void
  1453. test_build_descriptors(void *arg)
  1454. {
  1455. int ret;
  1456. time_t now = time(NULL);
  1457. hs_service_t *last_service = NULL;
  1458. (void) arg;
  1459. hs_init();
  1460. MOCK(get_or_state,
  1461. get_or_state_replacement);
  1462. MOCK(networkstatus_get_live_consensus,
  1463. mock_networkstatus_get_live_consensus);
  1464. dummy_state = or_state_new();
  1465. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  1466. &mock_ns.valid_after);
  1467. tt_int_op(ret, OP_EQ, 0);
  1468. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  1469. &mock_ns.fresh_until);
  1470. tt_int_op(ret, OP_EQ, 0);
  1471. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1472. /* Generate a valid number of fake auth clients when a client authorization
  1473. * is disabled. */
  1474. {
  1475. hs_service_t *service = helper_create_service();
  1476. last_service = service;
  1477. service_descriptor_free(service->desc_current);
  1478. service->desc_current = NULL;
  1479. build_all_descriptors(now);
  1480. tt_assert(service->desc_current);
  1481. tt_assert(service->desc_current->desc);
  1482. hs_desc_superencrypted_data_t *superencrypted;
  1483. superencrypted = &service->desc_current->desc->superencrypted_data;
  1484. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 16);
  1485. helper_destroy_service(service);
  1486. last_service = NULL;
  1487. }
  1488. /* Generate a valid number of fake auth clients when the number of
  1489. * clients is zero. */
  1490. {
  1491. hs_service_t *service = helper_create_service_with_clients(0);
  1492. last_service = service;
  1493. service_descriptor_free(service->desc_current);
  1494. service->desc_current = NULL;
  1495. build_all_descriptors(now);
  1496. hs_desc_superencrypted_data_t *superencrypted;
  1497. superencrypted = &service->desc_current->desc->superencrypted_data;
  1498. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 16);
  1499. helper_destroy_service(service);
  1500. last_service = NULL;
  1501. }
  1502. /* Generate a valid number of fake auth clients when the number of
  1503. * clients is not a multiple of 16. */
  1504. {
  1505. hs_service_t *service = helper_create_service_with_clients(20);
  1506. last_service = service;
  1507. service_descriptor_free(service->desc_current);
  1508. service->desc_current = NULL;
  1509. build_all_descriptors(now);
  1510. hs_desc_superencrypted_data_t *superencrypted;
  1511. superencrypted = &service->desc_current->desc->superencrypted_data;
  1512. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 32);
  1513. helper_destroy_service(service);
  1514. last_service = NULL;
  1515. }
  1516. /* Do not generate any fake desc client when the number of clients is
  1517. * a multiple of 16 but not zero. */
  1518. {
  1519. hs_service_t *service = helper_create_service_with_clients(32);
  1520. last_service = service;
  1521. service_descriptor_free(service->desc_current);
  1522. service->desc_current = NULL;
  1523. build_all_descriptors(now);
  1524. hs_desc_superencrypted_data_t *superencrypted;
  1525. superencrypted = &service->desc_current->desc->superencrypted_data;
  1526. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 32);
  1527. helper_destroy_service(service);
  1528. last_service = NULL;
  1529. }
  1530. done:
  1531. helper_destroy_service(last_service);
  1532. hs_free_all();
  1533. }
  1534. static void
  1535. test_upload_descriptors(void *arg)
  1536. {
  1537. int ret;
  1538. time_t now;
  1539. hs_service_t *service;
  1540. (void) arg;
  1541. hs_init();
  1542. MOCK(get_or_state,
  1543. get_or_state_replacement);
  1544. MOCK(networkstatus_get_live_consensus,
  1545. mock_networkstatus_get_live_consensus);
  1546. dummy_state = or_state_new();
  1547. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1548. &mock_ns.valid_after);
  1549. tt_int_op(ret, OP_EQ, 0);
  1550. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1551. &mock_ns.fresh_until);
  1552. tt_int_op(ret, OP_EQ, 0);
  1553. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1554. update_approx_time(mock_ns.valid_after+1);
  1555. now = mock_ns.valid_after+1;
  1556. /* Create a service with no descriptor. It's added to the global map. */
  1557. service = hs_service_new(get_options());
  1558. tt_assert(service);
  1559. service->config.version = HS_VERSION_THREE;
  1560. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1561. ed25519_public_key_generate(&service->keys.identity_pk,
  1562. &service->keys.identity_sk);
  1563. /* Register service to global map. */
  1564. ret = register_service(get_hs_service_map(), service);
  1565. tt_int_op(ret, OP_EQ, 0);
  1566. /* But first, build our descriptor. */
  1567. build_all_descriptors(now);
  1568. /* Nothing should happen because we have 0 introduction circuit established
  1569. * and we want (by default) 3 intro points. */
  1570. run_upload_descriptor_event(now);
  1571. /* If no upload happened, this should be untouched. */
  1572. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1573. /* We'll simulate that we've opened our intro point circuit and that we only
  1574. * want one intro point. */
  1575. service->config.num_intro_points = 1;
  1576. /* Set our next upload time after now which will skip the upload. */
  1577. service->desc_current->next_upload_time = now + 1000;
  1578. run_upload_descriptor_event(now);
  1579. /* If no upload happened, this should be untouched. */
  1580. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, now + 1000);
  1581. done:
  1582. hs_free_all();
  1583. UNMOCK(get_or_state);
  1584. }
  1585. /** Global vars used by test_rendezvous1_parsing() */
  1586. static char rend1_payload[RELAY_PAYLOAD_SIZE];
  1587. static size_t rend1_payload_len = 0;
  1588. /** Mock for relay_send_command_from_edge() to send a RENDEZVOUS1 cell. Instead
  1589. * of sending it to the network, instead save it to the global `rend1_payload`
  1590. * variable so that we can inspect it in the test_rendezvous1_parsing()
  1591. * test. */
  1592. static int
  1593. mock_relay_send_rendezvous1(streamid_t stream_id, circuit_t *circ,
  1594. uint8_t relay_command, const char *payload,
  1595. size_t payload_len,
  1596. crypt_path_t *cpath_layer,
  1597. const char *filename, int lineno)
  1598. {
  1599. (void) stream_id;
  1600. (void) circ;
  1601. (void) relay_command;
  1602. (void) cpath_layer;
  1603. (void) filename;
  1604. (void) lineno;
  1605. memcpy(rend1_payload, payload, payload_len);
  1606. rend1_payload_len = payload_len;
  1607. return 0;
  1608. }
  1609. /** Send a RENDEZVOUS1 as a service, and parse it as a client. */
  1610. static void
  1611. test_rendezvous1_parsing(void *arg)
  1612. {
  1613. int retval;
  1614. static const char *test_addr =
  1615. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad.onion";
  1616. hs_service_t *service = NULL;
  1617. origin_circuit_t *service_circ = NULL;
  1618. origin_circuit_t *client_circ = NULL;
  1619. ed25519_keypair_t ip_auth_kp;
  1620. curve25519_keypair_t ephemeral_kp;
  1621. curve25519_keypair_t client_kp;
  1622. curve25519_keypair_t ip_enc_kp;
  1623. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1624. (void) arg;
  1625. MOCK(relay_send_command_from_edge_, mock_relay_send_rendezvous1);
  1626. {
  1627. /* Let's start by setting up the service that will start the rend */
  1628. service = tor_malloc_zero(sizeof(hs_service_t));
  1629. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1630. ed25519_public_key_generate(&service->keys.identity_pk,
  1631. &service->keys.identity_sk);
  1632. memcpy(service->onion_address, test_addr, sizeof(service->onion_address));
  1633. tt_assert(service);
  1634. }
  1635. {
  1636. /* Now let's set up the service rendezvous circuit and its keys. */
  1637. service_circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND,
  1638. flags);
  1639. tor_free(service_circ->hs_ident);
  1640. hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys;
  1641. uint8_t rendezvous_cookie[HS_REND_COOKIE_LEN];
  1642. curve25519_keypair_generate(&ip_enc_kp, 0);
  1643. curve25519_keypair_generate(&ephemeral_kp, 0);
  1644. curve25519_keypair_generate(&client_kp, 0);
  1645. ed25519_keypair_generate(&ip_auth_kp, 0);
  1646. retval = hs_ntor_service_get_rendezvous1_keys(&ip_auth_kp.pubkey,
  1647. &ip_enc_kp,
  1648. &ephemeral_kp,
  1649. &client_kp.pubkey,
  1650. &hs_ntor_rend_cell_keys);
  1651. tt_int_op(retval, OP_EQ, 0);
  1652. memset(rendezvous_cookie, 2, sizeof(rendezvous_cookie));
  1653. service_circ->hs_ident =
  1654. create_rp_circuit_identifier(service, rendezvous_cookie,
  1655. &ephemeral_kp.pubkey,
  1656. &hs_ntor_rend_cell_keys);
  1657. }
  1658. /* Send out the RENDEZVOUS1 and make sure that our mock func worked */
  1659. tt_assert(fast_mem_is_zero(rend1_payload, 32));
  1660. hs_circ_service_rp_has_opened(service, service_circ);
  1661. tt_assert(!fast_mem_is_zero(rend1_payload, 32));
  1662. tt_int_op(rend1_payload_len, OP_EQ, HS_LEGACY_RENDEZVOUS_CELL_SIZE);
  1663. /******************************/
  1664. /** Now let's create the client rendezvous circuit */
  1665. client_circ =
  1666. helper_create_origin_circuit(CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED,
  1667. flags);
  1668. /* fix up its circ ident */
  1669. ed25519_pubkey_copy(&client_circ->hs_ident->intro_auth_pk,
  1670. &ip_auth_kp.pubkey);
  1671. memcpy(&client_circ->hs_ident->rendezvous_client_kp,
  1672. &client_kp, sizeof(client_circ->hs_ident->rendezvous_client_kp));
  1673. memcpy(&client_circ->hs_ident->intro_enc_pk.public_key,
  1674. &ip_enc_kp.pubkey.public_key,
  1675. sizeof(client_circ->hs_ident->intro_enc_pk.public_key));
  1676. /* Now parse the rendezvous2 circuit and make sure it was fine. We are
  1677. * skipping 20 bytes off its payload, since that's the rendezvous cookie
  1678. * which is only present in REND1. */
  1679. retval = handle_rendezvous2(client_circ,
  1680. (uint8_t*)rend1_payload+20,
  1681. rend1_payload_len-20);
  1682. tt_int_op(retval, OP_EQ, 0);
  1683. /* TODO: We are only simulating client/service here. We could also simulate
  1684. * the rendezvous point by plugging in rend_mid_establish_rendezvous(). We
  1685. * would need an extra circuit and some more stuff but it's doable. */
  1686. done:
  1687. circuit_free_(TO_CIRCUIT(service_circ));
  1688. circuit_free_(TO_CIRCUIT(client_circ));
  1689. hs_service_free(service);
  1690. hs_free_all();
  1691. UNMOCK(relay_send_command_from_edge_);
  1692. }
  1693. static void
  1694. test_authorized_client_config_equal(void *arg)
  1695. {
  1696. int ret;
  1697. hs_service_config_t *config1, *config2;
  1698. (void) arg;
  1699. config1 = tor_malloc_zero(sizeof(*config1));
  1700. config2 = tor_malloc_zero(sizeof(*config2));
  1701. /* Both configs are empty. */
  1702. {
  1703. config1->clients = smartlist_new();
  1704. config2->clients = smartlist_new();
  1705. ret = service_authorized_client_config_equal(config1, config2);
  1706. tt_int_op(ret, OP_EQ, 1);
  1707. service_clear_config(config1);
  1708. service_clear_config(config2);
  1709. }
  1710. /* Both configs have exactly the same client config. */
  1711. {
  1712. config1->clients = smartlist_new();
  1713. config2->clients = smartlist_new();
  1714. hs_service_authorized_client_t *client1, *client2;
  1715. client1 = helper_create_authorized_client();
  1716. client2 = helper_create_authorized_client();
  1717. smartlist_add(config1->clients, client1);
  1718. smartlist_add(config1->clients, client2);
  1719. /* We should swap the order of clients here to test that the order
  1720. * does not matter. */
  1721. smartlist_add(config2->clients, helper_clone_authorized_client(client2));
  1722. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1723. ret = service_authorized_client_config_equal(config1, config2);
  1724. tt_int_op(ret, OP_EQ, 1);
  1725. service_clear_config(config1);
  1726. service_clear_config(config2);
  1727. }
  1728. /* The numbers of clients in both configs are not equal. */
  1729. {
  1730. config1->clients = smartlist_new();
  1731. config2->clients = smartlist_new();
  1732. hs_service_authorized_client_t *client1, *client2;
  1733. client1 = helper_create_authorized_client();
  1734. client2 = helper_create_authorized_client();
  1735. smartlist_add(config1->clients, client1);
  1736. smartlist_add(config1->clients, client2);
  1737. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1738. ret = service_authorized_client_config_equal(config1, config2);
  1739. tt_int_op(ret, OP_EQ, 0);
  1740. service_clear_config(config1);
  1741. service_clear_config(config2);
  1742. }
  1743. /* The first config has two distinct clients while the second config
  1744. * has two clients but they are duplicate. */
  1745. {
  1746. config1->clients = smartlist_new();
  1747. config2->clients = smartlist_new();
  1748. hs_service_authorized_client_t *client1, *client2;
  1749. client1 = helper_create_authorized_client();
  1750. client2 = helper_create_authorized_client();
  1751. smartlist_add(config1->clients, client1);
  1752. smartlist_add(config1->clients, client2);
  1753. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1754. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1755. ret = service_authorized_client_config_equal(config1, config2);
  1756. tt_int_op(ret, OP_EQ, 0);
  1757. service_clear_config(config1);
  1758. service_clear_config(config2);
  1759. }
  1760. /* Both configs have totally distinct clients. */
  1761. {
  1762. config1->clients = smartlist_new();
  1763. config2->clients = smartlist_new();
  1764. hs_service_authorized_client_t *client1, *client2, *client3, *client4;
  1765. client1 = helper_create_authorized_client();
  1766. client2 = helper_create_authorized_client();
  1767. client3 = helper_create_authorized_client();
  1768. client4 = helper_create_authorized_client();
  1769. smartlist_add(config1->clients, client1);
  1770. smartlist_add(config1->clients, client2);
  1771. smartlist_add(config2->clients, client3);
  1772. smartlist_add(config2->clients, client4);
  1773. ret = service_authorized_client_config_equal(config1, config2);
  1774. tt_int_op(ret, OP_EQ, 0);
  1775. service_clear_config(config1);
  1776. service_clear_config(config2);
  1777. }
  1778. done:
  1779. tor_free(config1);
  1780. tor_free(config2);
  1781. }
  1782. /** Test that client circuit ID gets correctly exported */
  1783. static void
  1784. test_export_client_circuit_id(void *arg)
  1785. {
  1786. origin_circuit_t *or_circ = NULL;
  1787. size_t sz;
  1788. char *cp1=NULL, *cp2=NULL;
  1789. connection_t *conn = NULL;
  1790. (void) arg;
  1791. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1792. hs_service_init();
  1793. /* Create service */
  1794. hs_service_t *service = helper_create_service();
  1795. /* Check that export circuit ID detection works */
  1796. service->config.circuit_id_protocol = HS_CIRCUIT_ID_PROTOCOL_NONE;
  1797. tt_int_op(0, OP_EQ,
  1798. hs_service_exports_circuit_id(&service->keys.identity_pk));
  1799. service->config.circuit_id_protocol = HS_CIRCUIT_ID_PROTOCOL_HAPROXY;
  1800. tt_int_op(1, OP_EQ,
  1801. hs_service_exports_circuit_id(&service->keys.identity_pk));
  1802. /* Create client connection */
  1803. conn = test_conn_get_connection(AP_CONN_STATE_CIRCUIT_WAIT, CONN_TYPE_AP, 0);
  1804. /* Create client edge conn hs_ident */
  1805. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  1806. edge_conn->hs_ident = hs_ident_edge_conn_new(&service->keys.identity_pk);
  1807. edge_conn->hs_ident->orig_virtual_port = 42;
  1808. /* Create rend circuit */
  1809. or_circ = origin_circuit_new();
  1810. or_circ->base_.purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  1811. edge_conn->on_circuit = TO_CIRCUIT(or_circ);
  1812. or_circ->global_identifier = 666;
  1813. /* Export circuit ID */
  1814. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1815. /* Check contents */
  1816. cp1 = buf_get_contents(conn->outbuf, &sz);
  1817. tt_str_op(cp1, OP_EQ,
  1818. "PROXY TCP6 fc00:dead:beef:4dad::0:29a ::1 666 42\r\n");
  1819. /* Change circ GID and see that the reported circuit ID also changes */
  1820. or_circ->global_identifier = 22;
  1821. /* check changes */
  1822. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1823. cp2 = buf_get_contents(conn->outbuf, &sz);
  1824. tt_str_op(cp1, OP_NE, cp2);
  1825. tor_free(cp1);
  1826. /* Check that GID with UINT32_MAX works. */
  1827. or_circ->global_identifier = UINT32_MAX;
  1828. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1829. cp1 = buf_get_contents(conn->outbuf, &sz);
  1830. tt_str_op(cp1, OP_EQ,
  1831. "PROXY TCP6 fc00:dead:beef:4dad::ffff:ffff ::1 65535 42\r\n");
  1832. tor_free(cp1);
  1833. /* Check that GID with UINT16_MAX works. */
  1834. or_circ->global_identifier = UINT16_MAX;
  1835. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1836. cp1 = buf_get_contents(conn->outbuf, &sz);
  1837. tt_str_op(cp1, OP_EQ,
  1838. "PROXY TCP6 fc00:dead:beef:4dad::0:ffff ::1 65535 42\r\n");
  1839. tor_free(cp1);
  1840. /* Check that GID with UINT16_MAX + 7 works. */
  1841. or_circ->global_identifier = UINT16_MAX + 7;
  1842. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1843. cp1 = buf_get_contents(conn->outbuf, &sz);
  1844. tt_str_op(cp1, OP_EQ, "PROXY TCP6 fc00:dead:beef:4dad::1:6 ::1 6 42\r\n");
  1845. done:
  1846. UNMOCK(connection_write_to_buf_impl_);
  1847. circuit_free_(TO_CIRCUIT(or_circ));
  1848. connection_free_minimal(conn);
  1849. hs_service_free(service);
  1850. tor_free(cp1);
  1851. tor_free(cp2);
  1852. }
  1853. struct testcase_t hs_service_tests[] = {
  1854. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  1855. NULL, NULL },
  1856. { "load_keys", test_load_keys, TT_FORK,
  1857. NULL, NULL },
  1858. { "client_filename_is_valid", test_client_filename_is_valid, TT_FORK,
  1859. NULL, NULL },
  1860. { "parse_authorized_client", test_parse_authorized_client, TT_FORK,
  1861. NULL, NULL },
  1862. { "load_keys_with_client_auth", test_load_keys_with_client_auth, TT_FORK,
  1863. NULL, NULL },
  1864. { "access_service", test_access_service, TT_FORK,
  1865. NULL, NULL },
  1866. { "service_intro_point", test_service_intro_point, TT_FORK,
  1867. NULL, NULL },
  1868. { "helper_functions", test_helper_functions, TT_FORK,
  1869. NULL, NULL },
  1870. { "intro_circuit_opened", test_intro_circuit_opened, TT_FORK,
  1871. NULL, NULL },
  1872. { "intro_established", test_intro_established, TT_FORK,
  1873. NULL, NULL },
  1874. { "closing_intro_circs", test_closing_intro_circs, TT_FORK,
  1875. NULL, NULL },
  1876. { "rdv_circuit_opened", test_rdv_circuit_opened, TT_FORK,
  1877. NULL, NULL },
  1878. { "introduce2", test_introduce2, TT_FORK,
  1879. NULL, NULL },
  1880. { "service_event", test_service_event, TT_FORK,
  1881. NULL, NULL },
  1882. { "rotate_descriptors", test_rotate_descriptors, TT_FORK,
  1883. NULL, NULL },
  1884. { "build_update_descriptors", test_build_update_descriptors, TT_FORK,
  1885. NULL, NULL },
  1886. { "build_descriptors", test_build_descriptors, TT_FORK,
  1887. NULL, NULL },
  1888. { "upload_descriptors", test_upload_descriptors, TT_FORK,
  1889. NULL, NULL },
  1890. { "rendezvous1_parsing", test_rendezvous1_parsing, TT_FORK,
  1891. NULL, NULL },
  1892. { "authorized_client_config_equal", test_authorized_client_config_equal,
  1893. TT_FORK, NULL, NULL },
  1894. { "export_client_circuit_id", test_export_client_circuit_id, TT_FORK,
  1895. NULL, NULL },
  1896. END_OF_TESTCASES
  1897. };