test_hs_service.c 54 KB

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