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