test_hs_service.c 58 KB

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