test_hs_service.c 58 KB

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