test_hs_service.c 58 KB

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