test_hs_service.c 56 KB

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