test_hs_service.c 58 KB

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