test_hs_service.c 58 KB

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