test_hs_service.c 70 KB

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