test_hs_service.c 66 KB

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