test_hs_service.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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 MAIN_PRIVATE
  16. #define NETWORKSTATUS_PRIVATE
  17. #define STATEFILE_PRIVATE
  18. #define TOR_CHANNEL_INTERNAL_
  19. #include "test.h"
  20. #include "test_helpers.h"
  21. #include "log_test_helpers.h"
  22. #include "rend_test_helpers.h"
  23. #include "hs_test_helpers.h"
  24. #include "or.h"
  25. #include "config.h"
  26. #include "circuitbuild.h"
  27. #include "circuitlist.h"
  28. #include "circuituse.h"
  29. #include "crypto.h"
  30. #include "networkstatus.h"
  31. #include "nodelist.h"
  32. #include "relay.h"
  33. #include "hs_common.h"
  34. #include "hs_config.h"
  35. #include "hs_circuit.h"
  36. #include "hs_ident.h"
  37. #include "hs_intropoint.h"
  38. #include "hs_ntor.h"
  39. #include "hs_service.h"
  40. #include "main.h"
  41. #include "rendservice.h"
  42. #include "statefile.h"
  43. /* Trunnel */
  44. #include "hs/cell_establish_intro.h"
  45. static networkstatus_t mock_ns;
  46. static networkstatus_t *
  47. mock_networkstatus_get_live_consensus(time_t now)
  48. {
  49. (void) now;
  50. return &mock_ns;
  51. }
  52. static or_state_t *dummy_state = NULL;
  53. /* Mock function to get fake or state (used for rev counters) */
  54. static or_state_t *
  55. get_or_state_replacement(void)
  56. {
  57. return dummy_state;
  58. }
  59. /* Mock function because we are not trying to test the close circuit that does
  60. * an awful lot of checks on the circuit object. */
  61. static void
  62. mock_circuit_mark_for_close(circuit_t *circ, int reason, int line,
  63. const char *file)
  64. {
  65. (void) circ;
  66. (void) reason;
  67. (void) line;
  68. (void) file;
  69. return;
  70. }
  71. static int
  72. mock_relay_send_command_from_edge(streamid_t stream_id, circuit_t *circ,
  73. uint8_t relay_command, const char *payload,
  74. size_t payload_len,
  75. crypt_path_t *cpath_layer,
  76. const char *filename, int lineno)
  77. {
  78. (void) stream_id;
  79. (void) circ;
  80. (void) relay_command;
  81. (void) payload;
  82. (void) payload_len;
  83. (void) cpath_layer;
  84. (void) filename;
  85. (void) lineno;
  86. return 0;
  87. }
  88. /* Mock function that always return true so we can test the descriptor
  89. * creation of the next time period deterministically. */
  90. static int
  91. mock_hs_overlap_mode_is_active_true(const networkstatus_t *consensus,
  92. time_t now)
  93. {
  94. (void) consensus;
  95. (void) now;
  96. return 1;
  97. }
  98. /* Helper: from a set of options in conf, configure a service which will add
  99. * it to the staging list of the HS subsytem. */
  100. static int
  101. helper_config_service(const char *conf)
  102. {
  103. int ret = 0;
  104. or_options_t *options = NULL;
  105. tt_assert(conf);
  106. options = helper_parse_options(conf);
  107. tt_assert(options);
  108. ret = hs_config_service_all(options, 0);
  109. done:
  110. or_options_free(options);
  111. return ret;
  112. }
  113. /* Test: Ensure that setting up rendezvous circuits works correctly. */
  114. static void
  115. test_e2e_rend_circuit_setup(void *arg)
  116. {
  117. ed25519_public_key_t service_pk;
  118. origin_circuit_t *or_circ;
  119. int retval;
  120. /** In this test we create a v3 prop224 service-side rendezvous circuit.
  121. * We simulate an HS ntor key exchange with a client, and check that
  122. * the circuit was setup correctly and is ready to accept rendezvous data */
  123. (void) arg;
  124. /* Now make dummy circuit */
  125. {
  126. or_circ = origin_circuit_new();
  127. or_circ->base_.purpose = CIRCUIT_PURPOSE_S_CONNECT_REND;
  128. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  129. or_circ->build_state->is_internal = 1;
  130. /* prop224: Setup hs conn identifier on the stream */
  131. ed25519_secret_key_t sk;
  132. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
  133. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
  134. or_circ->hs_ident = hs_ident_circuit_new(&service_pk,
  135. HS_IDENT_CIRCUIT_RENDEZVOUS);
  136. TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
  137. }
  138. /* Check number of hops */
  139. retval = cpath_get_n_hops(&or_circ->cpath);
  140. tt_int_op(retval, OP_EQ, 0);
  141. /* Setup the circuit: do the ntor key exchange */
  142. {
  143. uint8_t ntor_key_seed[DIGEST256_LEN] = {2};
  144. retval = hs_circuit_setup_e2e_rend_circ(or_circ,
  145. ntor_key_seed, sizeof(ntor_key_seed),
  146. 1);
  147. tt_int_op(retval, OP_EQ, 0);
  148. }
  149. /* See that a hop was added to the circuit's cpath */
  150. retval = cpath_get_n_hops(&or_circ->cpath);
  151. tt_int_op(retval, OP_EQ, 1);
  152. /* Check the digest algo */
  153. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->f_digest),
  154. OP_EQ, DIGEST_SHA3_256);
  155. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->b_digest),
  156. OP_EQ, DIGEST_SHA3_256);
  157. tt_assert(or_circ->cpath->f_crypto);
  158. tt_assert(or_circ->cpath->b_crypto);
  159. /* Ensure that circ purpose was changed */
  160. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  161. done:
  162. circuit_free(TO_CIRCUIT(or_circ));
  163. }
  164. /* Helper: Return a newly allocated and initialized origin circuit with
  165. * purpose and flags. A default HS identifier is set to an ed25519
  166. * authentication key for introduction point. */
  167. static origin_circuit_t *
  168. helper_create_origin_circuit(int purpose, int flags)
  169. {
  170. origin_circuit_t *circ = NULL;
  171. circ = origin_circuit_init(purpose, flags);
  172. tt_assert(circ);
  173. circ->cpath = tor_malloc_zero(sizeof(crypt_path_t));
  174. circ->cpath->magic = CRYPT_PATH_MAGIC;
  175. circ->cpath->state = CPATH_STATE_OPEN;
  176. circ->cpath->package_window = circuit_initial_package_window();
  177. circ->cpath->deliver_window = CIRCWINDOW_START;
  178. circ->cpath->prev = circ->cpath;
  179. /* Random nonce. */
  180. crypto_rand(circ->cpath->prev->rend_circ_nonce, DIGEST_LEN);
  181. /* Create a default HS identifier. */
  182. circ->hs_ident = tor_malloc_zero(sizeof(hs_ident_circuit_t));
  183. done:
  184. return circ;
  185. }
  186. /* Helper: Return a newly allocated service object with the identity keypair
  187. * sets and the current descriptor. Then register it to the global map.
  188. * Caller should us hs_free_all() to free this service or remove it from the
  189. * global map before freeing. */
  190. static hs_service_t *
  191. helper_create_service(void)
  192. {
  193. /* Set a service for this circuit. */
  194. hs_service_t *service = hs_service_new(get_options());
  195. tt_assert(service);
  196. service->config.version = HS_VERSION_THREE;
  197. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  198. ed25519_public_key_generate(&service->keys.identity_pk,
  199. &service->keys.identity_sk);
  200. service->desc_current = service_descriptor_new();
  201. tt_assert(service->desc_current);
  202. /* Register service to global map. */
  203. int ret = register_service(get_hs_service_map(), service);
  204. tt_int_op(ret, OP_EQ, 0);
  205. done:
  206. return service;
  207. }
  208. /* Helper: Return a newly allocated service intro point with two link
  209. * specifiers, one IPv4 and one legacy ID set to As. */
  210. static hs_service_intro_point_t *
  211. helper_create_service_ip(void)
  212. {
  213. hs_desc_link_specifier_t *ls;
  214. hs_service_intro_point_t *ip = service_intro_point_new(NULL, 0);
  215. tt_assert(ip);
  216. /* Add a first unused link specifier. */
  217. ls = tor_malloc_zero(sizeof(*ls));
  218. ls->type = LS_IPV4;
  219. smartlist_add(ip->base.link_specifiers, ls);
  220. /* Add a second link specifier used by a test. */
  221. ls = tor_malloc_zero(sizeof(*ls));
  222. ls->type = LS_LEGACY_ID;
  223. memset(ls->u.legacy_id, 'A', sizeof(ls->u.legacy_id));
  224. smartlist_add(ip->base.link_specifiers, ls);
  225. done:
  226. return ip;
  227. }
  228. static void
  229. test_load_keys(void *arg)
  230. {
  231. int ret;
  232. char *conf = NULL;
  233. char *hsdir_v2 = tor_strdup(get_fname("hs2"));
  234. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  235. char addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  236. (void) arg;
  237. /* We'll register two services, a v2 and a v3, then we'll load keys and
  238. * validate that both are in a correct state. */
  239. hs_init();
  240. #define conf_fmt \
  241. "HiddenServiceDir %s\n" \
  242. "HiddenServiceVersion %d\n" \
  243. "HiddenServicePort 65535\n"
  244. /* v2 service. */
  245. tor_asprintf(&conf, conf_fmt, hsdir_v2, HS_VERSION_TWO);
  246. ret = helper_config_service(conf);
  247. tor_free(conf);
  248. tt_int_op(ret, OP_EQ, 0);
  249. /* This one should now be registered into the v2 list. */
  250. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 0);
  251. tt_int_op(rend_num_services(), OP_EQ, 1);
  252. /* v3 service. */
  253. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  254. ret = helper_config_service(conf);
  255. tor_free(conf);
  256. tt_int_op(ret, OP_EQ, 0);
  257. /* It's in staging? */
  258. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  259. /* Load the keys for these. After that, the v3 service should be registered
  260. * in the global map. */
  261. hs_service_load_all_keys();
  262. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  263. hs_service_t *s = get_first_service();
  264. tt_assert(s);
  265. /* Ok we have the service object. Validate few things. */
  266. tt_assert(!tor_mem_is_zero(s->onion_address, sizeof(s->onion_address)));
  267. tt_int_op(hs_address_is_valid(s->onion_address), OP_EQ, 1);
  268. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_sk.seckey,
  269. ED25519_SECKEY_LEN));
  270. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_pk.pubkey,
  271. ED25519_PUBKEY_LEN));
  272. /* Check onion address from identity key. */
  273. hs_build_address(&s->keys.identity_pk, s->config.version, addr);
  274. tt_int_op(hs_address_is_valid(addr), OP_EQ, 1);
  275. tt_str_op(addr, OP_EQ, s->onion_address);
  276. done:
  277. tor_free(hsdir_v2);
  278. tor_free(hsdir_v3);
  279. hs_free_all();
  280. }
  281. static void
  282. test_access_service(void *arg)
  283. {
  284. int ret;
  285. char *conf = NULL;
  286. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  287. hs_service_ht *global_map;
  288. hs_service_t *s = NULL;
  289. (void) arg;
  290. /* We'll register two services, a v2 and a v3, then we'll load keys and
  291. * validate that both are in a correct state. */
  292. hs_init();
  293. #define conf_fmt \
  294. "HiddenServiceDir %s\n" \
  295. "HiddenServiceVersion %d\n" \
  296. "HiddenServicePort 65535\n"
  297. /* v3 service. */
  298. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  299. ret = helper_config_service(conf);
  300. tor_free(conf);
  301. tt_int_op(ret, OP_EQ, 0);
  302. /* It's in staging? */
  303. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  304. /* Load the keys for these. After that, the v3 service should be registered
  305. * in the global map. */
  306. hs_service_load_all_keys();
  307. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  308. s = get_first_service();
  309. tt_assert(s);
  310. global_map = get_hs_service_map();
  311. tt_assert(global_map);
  312. /* From here, we'll try the service accessors. */
  313. hs_service_t *query = find_service(global_map, &s->keys.identity_pk);
  314. tt_assert(query);
  315. tt_mem_op(query, OP_EQ, s, sizeof(hs_service_t));
  316. /* Remove service, check if it actually works and then put it back. */
  317. remove_service(global_map, s);
  318. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  319. query = find_service(global_map, &s->keys.identity_pk);
  320. tt_ptr_op(query, OP_EQ, NULL);
  321. /* Register back the service in the map. */
  322. ret = register_service(global_map, s);
  323. tt_int_op(ret, OP_EQ, 0);
  324. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  325. /* Twice should fail. */
  326. ret = register_service(global_map, s);
  327. tt_int_op(ret, OP_EQ, -1);
  328. /* Remove service from map so we don't double free on cleanup. */
  329. remove_service(global_map, s);
  330. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  331. query = find_service(global_map, &s->keys.identity_pk);
  332. tt_ptr_op(query, OP_EQ, NULL);
  333. /* Let's try to remove twice for fun. */
  334. setup_full_capture_of_logs(LOG_WARN);
  335. remove_service(global_map, s);
  336. expect_log_msg_containing("Could not find service in the global map");
  337. teardown_capture_of_logs();
  338. done:
  339. hs_service_free(s);
  340. tor_free(hsdir_v3);
  341. hs_free_all();
  342. }
  343. /** Test that we can create intro point objects, index them and find them */
  344. static void
  345. test_service_intro_point(void *arg)
  346. {
  347. hs_service_t *service = NULL;
  348. hs_service_intro_point_t *ip = NULL;
  349. (void) arg;
  350. /* Test simple creation of an object. */
  351. {
  352. time_t now = time(NULL);
  353. ip = helper_create_service_ip();
  354. tt_assert(ip);
  355. /* Make sure the authentication keypair is not zeroes. */
  356. tt_int_op(tor_mem_is_zero((const char *) &ip->auth_key_kp,
  357. sizeof(ed25519_keypair_t)), OP_EQ, 0);
  358. /* The introduce2_max MUST be in that range. */
  359. tt_u64_op(ip->introduce2_max, OP_GE,
  360. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  361. tt_u64_op(ip->introduce2_max, OP_LE,
  362. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  363. /* Time to expire MUST also be in that range. We add 5 seconds because
  364. * there could be a gap between setting now and the time taken in
  365. * service_intro_point_new. On ARM, it can be surprisingly slow... */
  366. tt_u64_op(ip->time_to_expire, OP_GE,
  367. now + INTRO_POINT_LIFETIME_MIN_SECONDS + 5);
  368. tt_u64_op(ip->time_to_expire, OP_LE,
  369. now + INTRO_POINT_LIFETIME_MAX_SECONDS + 5);
  370. tt_assert(ip->replay_cache);
  371. tt_assert(ip->base.link_specifiers);
  372. /* By default, this is NOT a legacy object. */
  373. tt_int_op(ip->base.is_only_legacy, OP_EQ, 0);
  374. }
  375. /* Test functions that uses a service intropoints map with that previously
  376. * created object (non legacy). */
  377. {
  378. uint8_t garbage[DIGEST256_LEN] = {0};
  379. hs_service_intro_point_t *query;
  380. service = hs_service_new(get_options());
  381. tt_assert(service);
  382. service->desc_current = service_descriptor_new();
  383. tt_assert(service->desc_current);
  384. /* Add intropoint to descriptor map. */
  385. service_intro_point_add(service->desc_current->intro_points.map, ip);
  386. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  387. tt_mem_op(query, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  388. query = service_intro_point_find(service,
  389. (const ed25519_public_key_t *) garbage);
  390. tt_ptr_op(query, OP_EQ, NULL);
  391. /* While at it, can I find the descriptor with the intro point? */
  392. hs_service_descriptor_t *desc_lookup =
  393. service_desc_find_by_intro(service, ip);
  394. tt_mem_op(service->desc_current, OP_EQ, desc_lookup,
  395. sizeof(hs_service_descriptor_t));
  396. /* Remove object from service descriptor and make sure it is out. */
  397. service_intro_point_remove(service, ip);
  398. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  399. tt_ptr_op(query, OP_EQ, NULL);
  400. }
  401. done:
  402. /* If the test succeed, this object is no longer referenced in the service
  403. * so we can free it without use after free. Else, it might explode because
  404. * it's still in the service descriptor map. */
  405. service_intro_point_free(ip);
  406. hs_service_free(service);
  407. }
  408. static node_t mock_node;
  409. static const node_t *
  410. mock_node_get_by_id(const char *digest)
  411. {
  412. (void) digest;
  413. memset(mock_node.identity, 'A', DIGEST_LEN);
  414. /* Only return the matchin identity of As */
  415. if (!tor_memcmp(mock_node.identity, digest, DIGEST_LEN)) {
  416. return &mock_node;
  417. }
  418. return NULL;
  419. }
  420. static void
  421. test_helper_functions(void *arg)
  422. {
  423. int ret;
  424. hs_service_t *service = NULL;
  425. hs_service_intro_point_t *ip = NULL;
  426. hs_ident_circuit_t ident;
  427. (void) arg;
  428. MOCK(node_get_by_id, mock_node_get_by_id);
  429. hs_service_init();
  430. service = helper_create_service();
  431. ip = helper_create_service_ip();
  432. /* Immediately add the intro point to the service so the free service at the
  433. * end cleans it as well. */
  434. service_intro_point_add(service->desc_current->intro_points.map, ip);
  435. /* Setup the circuit identifier. */
  436. ed25519_pubkey_copy(&ident.intro_auth_pk, &ip->auth_key_kp.pubkey);
  437. ed25519_pubkey_copy(&ident.identity_pk, &service->keys.identity_pk);
  438. /* Testing get_objects_from_ident(). */
  439. {
  440. hs_service_t *s_lookup = NULL;
  441. hs_service_intro_point_t *ip_lookup = NULL;
  442. hs_service_descriptor_t *desc_lookup = NULL;
  443. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  444. tt_mem_op(s_lookup, OP_EQ, service, sizeof(hs_service_t));
  445. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  446. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  447. sizeof(hs_service_descriptor_t));
  448. /* Reset */
  449. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  450. /* NULL parameter should work. */
  451. get_objects_from_ident(&ident, NULL, &ip_lookup, &desc_lookup);
  452. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  453. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  454. sizeof(hs_service_descriptor_t));
  455. /* Reset. */
  456. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  457. /* Break the ident and we should find nothing. */
  458. memset(&ident, 0, sizeof(ident));
  459. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  460. tt_ptr_op(s_lookup, OP_EQ, NULL);
  461. tt_ptr_op(ip_lookup, OP_EQ, NULL);
  462. tt_ptr_op(desc_lookup, OP_EQ, NULL);
  463. }
  464. /* Testing get_node_from_intro_point() */
  465. {
  466. const node_t *node = get_node_from_intro_point(ip);
  467. tt_ptr_op(node, OP_EQ, &mock_node);
  468. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  469. hs_desc_link_specifier_t *, ls) {
  470. if (ls->type == LS_LEGACY_ID) {
  471. /* Change legacy id in link specifier which is not the mock node. */
  472. memset(ls->u.legacy_id, 'B', sizeof(ls->u.legacy_id));
  473. }
  474. } SMARTLIST_FOREACH_END(ls);
  475. node = get_node_from_intro_point(ip);
  476. tt_ptr_op(node, OP_EQ, NULL);
  477. }
  478. /* Testing can_service_launch_intro_circuit() */
  479. {
  480. time_t now = time(NULL);
  481. /* Put the start of the retry period back in time, we should be allowed.
  482. * to launch intro circuit. */
  483. service->state.num_intro_circ_launched = 2;
  484. service->state.intro_circ_retry_started_time =
  485. (now - INTRO_CIRC_RETRY_PERIOD - 1);
  486. ret = can_service_launch_intro_circuit(service, now);
  487. tt_int_op(ret, OP_EQ, 1);
  488. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  489. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  490. /* Call it again, we should still be allowed because we are under
  491. * MAX_INTRO_CIRCS_PER_PERIOD which been set to 0 previously. */
  492. ret = can_service_launch_intro_circuit(service, now);
  493. tt_int_op(ret, OP_EQ, 1);
  494. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  495. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  496. /* Too many intro circuit launched means we are not allowed. */
  497. service->state.num_intro_circ_launched = 20;
  498. ret = can_service_launch_intro_circuit(service, now);
  499. tt_int_op(ret, OP_EQ, 0);
  500. }
  501. /* Testing intro_point_should_expire(). */
  502. {
  503. time_t now = time(NULL);
  504. /* Just some basic test of the current state. */
  505. tt_u64_op(ip->introduce2_max, OP_GE,
  506. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  507. tt_u64_op(ip->introduce2_max, OP_LE,
  508. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  509. tt_u64_op(ip->time_to_expire, OP_GE,
  510. now + INTRO_POINT_LIFETIME_MIN_SECONDS);
  511. tt_u64_op(ip->time_to_expire, OP_LE,
  512. now + INTRO_POINT_LIFETIME_MAX_SECONDS);
  513. /* This newly created IP from above shouldn't expire now. */
  514. ret = intro_point_should_expire(ip, now);
  515. tt_int_op(ret, OP_EQ, 0);
  516. /* Maximum number of INTRODUCE2 cell reached, it should expire. */
  517. ip->introduce2_count = INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS + 1;
  518. ret = intro_point_should_expire(ip, now);
  519. tt_int_op(ret, OP_EQ, 1);
  520. ip->introduce2_count = 0;
  521. /* It should expire if time to expire has been reached. */
  522. ip->time_to_expire = now - 1000;
  523. ret = intro_point_should_expire(ip, now);
  524. tt_int_op(ret, OP_EQ, 1);
  525. }
  526. done:
  527. /* This will free the service and all objects associated to it. */
  528. hs_service_free_all();
  529. UNMOCK(node_get_by_id);
  530. }
  531. /** Test that we do the right operations when an intro circuit opens */
  532. static void
  533. test_intro_circuit_opened(void *arg)
  534. {
  535. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  536. hs_service_t *service;
  537. origin_circuit_t *circ = NULL;
  538. (void) arg;
  539. hs_init();
  540. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  541. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  542. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  543. flags);
  544. /* No service associated with this circuit. */
  545. setup_full_capture_of_logs(LOG_WARN);
  546. hs_service_circuit_has_opened(circ);
  547. expect_log_msg_containing("Unknown service identity key");
  548. teardown_capture_of_logs();
  549. /* Set a service for this circuit. */
  550. {
  551. service = helper_create_service();
  552. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  553. &service->keys.identity_pk);
  554. /* No intro point associated with this circuit. */
  555. setup_full_capture_of_logs(LOG_WARN);
  556. hs_service_circuit_has_opened(circ);
  557. expect_log_msg_containing("Unknown introduction point auth key");
  558. teardown_capture_of_logs();
  559. }
  560. /* Set an IP object now for this circuit. */
  561. {
  562. hs_service_intro_point_t *ip = helper_create_service_ip();
  563. service_intro_point_add(service->desc_current->intro_points.map, ip);
  564. /* Update ident to contain the intro point auth key. */
  565. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  566. &ip->auth_key_kp.pubkey);
  567. }
  568. /* This one should go all the way. */
  569. setup_full_capture_of_logs(LOG_INFO);
  570. hs_service_circuit_has_opened(circ);
  571. expect_log_msg_containing("Introduction circuit 0 established for service");
  572. teardown_capture_of_logs();
  573. done:
  574. circuit_free(TO_CIRCUIT(circ));
  575. hs_free_all();
  576. UNMOCK(circuit_mark_for_close_);
  577. UNMOCK(relay_send_command_from_edge_);
  578. }
  579. /** Test the operations we do on a circuit after we learn that we successfuly
  580. * established an intro point on it */
  581. static void
  582. test_intro_established(void *arg)
  583. {
  584. int ret;
  585. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  586. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  587. origin_circuit_t *circ = NULL;
  588. hs_service_t *service;
  589. hs_service_intro_point_t *ip = NULL;
  590. (void) arg;
  591. hs_init();
  592. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  593. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  594. flags);
  595. /* Test a wrong purpose. */
  596. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  597. setup_full_capture_of_logs(LOG_WARN);
  598. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  599. tt_int_op(ret, OP_EQ, -1);
  600. expect_log_msg_containing("Received an INTRO_ESTABLISHED cell on a "
  601. "non introduction circuit of purpose");
  602. teardown_capture_of_logs();
  603. /* Back to normal. */
  604. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  605. /* No service associated to it. */
  606. setup_full_capture_of_logs(LOG_WARN);
  607. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  608. tt_int_op(ret, OP_EQ, -1);
  609. expect_log_msg_containing("Unknown service identity key");
  610. teardown_capture_of_logs();
  611. /* Set a service for this circuit. */
  612. service = helper_create_service();
  613. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  614. &service->keys.identity_pk);
  615. /* No introduction point associated to it. */
  616. setup_full_capture_of_logs(LOG_WARN);
  617. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  618. tt_int_op(ret, OP_EQ, -1);
  619. expect_log_msg_containing("Introduction circuit established without an "
  620. "intro point object on circuit");
  621. teardown_capture_of_logs();
  622. /* Set an IP object now for this circuit. */
  623. {
  624. ip = helper_create_service_ip();
  625. service_intro_point_add(service->desc_current->intro_points.map, ip);
  626. /* Update ident to contain the intro point auth key. */
  627. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  628. &ip->auth_key_kp.pubkey);
  629. }
  630. /* Send an empty payload. INTRO_ESTABLISHED cells are basically zeroes. */
  631. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  632. tt_int_op(ret, OP_EQ, 0);
  633. tt_u64_op(ip->circuit_established, OP_EQ, 1);
  634. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_INTRO);
  635. done:
  636. circuit_free(TO_CIRCUIT(circ));
  637. hs_free_all();
  638. UNMOCK(circuit_mark_for_close_);
  639. }
  640. /** Check the operations we do on a rendezvous circuit after we learn it's
  641. * open */
  642. static void
  643. test_rdv_circuit_opened(void *arg)
  644. {
  645. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  646. origin_circuit_t *circ = NULL;
  647. hs_service_t *service;
  648. (void) arg;
  649. hs_init();
  650. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  651. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  652. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND, flags);
  653. crypto_rand((char *) circ->hs_ident->rendezvous_cookie, REND_COOKIE_LEN);
  654. crypto_rand((char *) circ->hs_ident->rendezvous_handshake_info,
  655. sizeof(circ->hs_ident->rendezvous_handshake_info));
  656. /* No service associated with this circuit. */
  657. setup_full_capture_of_logs(LOG_WARN);
  658. hs_service_circuit_has_opened(circ);
  659. expect_log_msg_containing("Unknown service identity key");
  660. teardown_capture_of_logs();
  661. /* This should be set to a non zero timestamp. */
  662. tt_u64_op(TO_CIRCUIT(circ)->timestamp_dirty, OP_NE, 0);
  663. /* Set a service for this circuit. */
  664. service = helper_create_service();
  665. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  666. &service->keys.identity_pk);
  667. /* Should be all good. */
  668. hs_service_circuit_has_opened(circ);
  669. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  670. done:
  671. circuit_free(TO_CIRCUIT(circ));
  672. hs_free_all();
  673. UNMOCK(circuit_mark_for_close_);
  674. UNMOCK(relay_send_command_from_edge_);
  675. }
  676. /** Test sending and receiving introduce2 cells */
  677. static void
  678. test_introduce2(void *arg)
  679. {
  680. int ret;
  681. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  682. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  683. origin_circuit_t *circ = NULL;
  684. hs_service_t *service;
  685. hs_service_intro_point_t *ip = NULL;
  686. (void) arg;
  687. hs_init();
  688. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  689. MOCK(get_or_state,
  690. get_or_state_replacement);
  691. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  692. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  693. /* Test a wrong purpose. */
  694. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  695. setup_full_capture_of_logs(LOG_WARN);
  696. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  697. tt_int_op(ret, OP_EQ, -1);
  698. expect_log_msg_containing("Received an INTRODUCE2 cell on a "
  699. "non introduction circuit of purpose");
  700. teardown_capture_of_logs();
  701. /* Back to normal. */
  702. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  703. /* No service associated to it. */
  704. setup_full_capture_of_logs(LOG_WARN);
  705. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  706. tt_int_op(ret, OP_EQ, -1);
  707. expect_log_msg_containing("Unknown service identity key");
  708. teardown_capture_of_logs();
  709. /* Set a service for this circuit. */
  710. service = helper_create_service();
  711. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  712. &service->keys.identity_pk);
  713. /* No introduction point associated to it. */
  714. setup_full_capture_of_logs(LOG_WARN);
  715. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  716. tt_int_op(ret, OP_EQ, -1);
  717. expect_log_msg_containing("Unknown introduction auth key when handling "
  718. "an INTRODUCE2 cell on circuit");
  719. teardown_capture_of_logs();
  720. /* Set an IP object now for this circuit. */
  721. {
  722. ip = helper_create_service_ip();
  723. service_intro_point_add(service->desc_current->intro_points.map, ip);
  724. /* Update ident to contain the intro point auth key. */
  725. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  726. &ip->auth_key_kp.pubkey);
  727. }
  728. /* This will fail because receiving an INTRODUCE2 cell implies a valid cell
  729. * and then launching circuits so let's not do that and instead test that
  730. * behaviour differently. */
  731. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  732. tt_int_op(ret, OP_EQ, -1);
  733. tt_u64_op(ip->introduce2_count, OP_EQ, 0);
  734. done:
  735. or_state_free(dummy_state);
  736. dummy_state = NULL;
  737. circuit_free(TO_CIRCUIT(circ));
  738. hs_free_all();
  739. UNMOCK(circuit_mark_for_close_);
  740. }
  741. /** Test basic hidden service housekeeping operations (maintaining intro
  742. * points, etc) */
  743. static void
  744. test_service_event(void *arg)
  745. {
  746. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  747. time_t now = time(NULL);
  748. hs_service_t *service;
  749. origin_circuit_t *circ = NULL;
  750. (void) arg;
  751. hs_init();
  752. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  753. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  754. /* Set a service for this circuit. */
  755. service = helper_create_service();
  756. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  757. &service->keys.identity_pk);
  758. /* Currently this consists of cleaning invalid intro points. So adding IPs
  759. * here that should get cleaned up. */
  760. {
  761. hs_service_intro_point_t *ip = helper_create_service_ip();
  762. service_intro_point_add(service->desc_current->intro_points.map, ip);
  763. /* This run will remove the IP because we have no circuits nor node_t
  764. * associated with it. */
  765. run_housekeeping_event(now);
  766. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  767. OP_EQ, 0);
  768. /* We'll trigger a removal because we've reached our maximum amount of
  769. * times we should retry a circuit. For this, we need to have a node_t
  770. * that matches the identity of this IP. */
  771. routerinfo_t ri;
  772. ip = helper_create_service_ip();
  773. service_intro_point_add(service->desc_current->intro_points.map, ip);
  774. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  775. /* This triggers a node_t creation. */
  776. tt_assert(nodelist_set_routerinfo(&ri, NULL));
  777. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  778. run_housekeeping_event(now);
  779. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  780. OP_EQ, 0);
  781. /* No removal but no circuit so this means the IP object will stay in the
  782. * descriptor map so we can retry it. */
  783. ip = helper_create_service_ip();
  784. service_intro_point_add(service->desc_current->intro_points.map, ip);
  785. ip->circuit_established = 1; /* We'll test that, it MUST be 0 after. */
  786. run_housekeeping_event(now);
  787. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  788. OP_EQ, 1);
  789. /* Remove the IP object at once for the next test. */
  790. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  791. run_housekeeping_event(now);
  792. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  793. OP_EQ, 0);
  794. /* Now, we'll create an IP with a registered circuit. The IP object
  795. * shouldn't go away. */
  796. ip = helper_create_service_ip();
  797. service_intro_point_add(service->desc_current->intro_points.map, ip);
  798. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  799. &ip->auth_key_kp.pubkey);
  800. hs_circuitmap_register_intro_circ_v3_service_side(
  801. circ, &ip->auth_key_kp.pubkey);
  802. run_housekeeping_event(now);
  803. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  804. OP_EQ, 1);
  805. /* We'll mangle the IP object to expire. */
  806. ip->time_to_expire = now;
  807. run_housekeeping_event(now);
  808. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  809. OP_EQ, 0);
  810. }
  811. done:
  812. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  813. circuit_free(TO_CIRCUIT(circ));
  814. hs_free_all();
  815. UNMOCK(circuit_mark_for_close_);
  816. }
  817. /** Test that we rotate descriptors correctly in overlap period */
  818. static void
  819. test_rotate_descriptors(void *arg)
  820. {
  821. int ret;
  822. time_t now = time(NULL);
  823. hs_service_t *service;
  824. hs_service_descriptor_t *desc_next;
  825. hs_service_intro_point_t *ip;
  826. (void) arg;
  827. hs_init();
  828. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  829. MOCK(networkstatus_get_live_consensus,
  830. mock_networkstatus_get_live_consensus);
  831. /* Setup the valid_after time to be 13:00 UTC, not in overlap period. The
  832. * overlap check doesn't care about the year. */
  833. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  834. &mock_ns.valid_after);
  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. /* Create a service with a default descriptor and state. It's added to the
  839. * global map. */
  840. service = helper_create_service();
  841. ip = helper_create_service_ip();
  842. service_intro_point_add(service->desc_current->intro_points.map, ip);
  843. /* Nothing should happen because we are not in the overlap period. */
  844. rotate_all_descriptors(now);
  845. tt_int_op(service->state.in_overlap_period, OP_EQ, 0);
  846. tt_assert(service->desc_current);
  847. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  848. OP_EQ, 1);
  849. /* Entering an overlap period. */
  850. ret = parse_rfc1123_time("Sat, 26 Oct 1985 01:00:00 UTC",
  851. &mock_ns.valid_after);
  852. ret = parse_rfc1123_time("Sat, 26 Oct 1985 02:00:00 UTC",
  853. &mock_ns.fresh_until);
  854. tt_int_op(ret, OP_EQ, 0);
  855. desc_next = service_descriptor_new();
  856. desc_next->next_upload_time = 42; /* Our marker to recognize it. */
  857. service->desc_next = desc_next;
  858. /* We should have our state flagged to be in the overlap period, our current
  859. * descriptor cleaned up and the next descriptor becoming the current. */
  860. rotate_all_descriptors(now);
  861. tt_int_op(service->state.in_overlap_period, OP_EQ, 1);
  862. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  863. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  864. OP_EQ, 0);
  865. tt_assert(service->desc_next == NULL);
  866. /* A second time should do nothing. */
  867. rotate_all_descriptors(now);
  868. tt_int_op(service->state.in_overlap_period, OP_EQ, 1);
  869. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  870. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  871. OP_EQ, 0);
  872. tt_assert(service->desc_next == NULL);
  873. /* Now let's re-create desc_next and get out of overlap period. We should
  874. test that desc_current gets replaced by desc_next, and desc_next becomes
  875. NULL. */
  876. desc_next = service_descriptor_new();
  877. desc_next->next_upload_time = 240; /* Our marker to recognize it. */
  878. service->desc_next = desc_next;
  879. /* Going out of the overlap period. */
  880. ret = parse_rfc1123_time("Sat, 26 Oct 1985 12:00:00 UTC",
  881. &mock_ns.valid_after);
  882. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  883. &mock_ns.fresh_until);
  884. /* This should reset the state and not touch the current descriptor. */
  885. tt_int_op(ret, OP_EQ, 0);
  886. rotate_all_descriptors(now);
  887. tt_int_op(service->state.in_overlap_period, OP_EQ, 0);
  888. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  889. tt_assert(service->desc_next == NULL);
  890. /* Calling rotate_all_descriptors() another time should do nothing */
  891. rotate_all_descriptors(now);
  892. tt_int_op(service->state.in_overlap_period, OP_EQ, 0);
  893. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  894. tt_assert(service->desc_next == NULL);
  895. done:
  896. hs_free_all();
  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. (void) arg;
  911. hs_init();
  912. MOCK(hs_overlap_mode_is_active, mock_hs_overlap_mode_is_active_true);
  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 13:00:00 UTC",
  919. &mock_ns.valid_after);
  920. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  921. &mock_ns.fresh_until);
  922. tt_int_op(ret, OP_EQ, 0);
  923. uint64_t period_num = hs_get_time_period_num(0);
  924. uint64_t next_period_num = hs_get_next_time_period_num(0);
  925. /* Create a service without a current descriptor to trigger a build. */
  926. service = hs_service_new(get_options());
  927. tt_assert(service);
  928. service->config.version = HS_VERSION_THREE;
  929. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  930. ed25519_public_key_generate(&service->keys.identity_pk,
  931. &service->keys.identity_sk);
  932. /* Register service to global map. */
  933. ret = register_service(get_hs_service_map(), service);
  934. tt_int_op(ret, OP_EQ, 0);
  935. build_all_descriptors(now);
  936. /* Check *current* descriptor. */
  937. tt_assert(service->desc_current);
  938. tt_assert(service->desc_current->desc);
  939. tt_assert(service->desc_current->intro_points.map);
  940. tt_u64_op(service->desc_current->time_period_num, OP_EQ, period_num);
  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, next_period_num);
  949. /* This should be untouched, the update descriptor process changes it. */
  950. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  951. /* Time to test the update of those descriptors. At first, we have no node
  952. * in the routerlist so this will find NO suitable node for the IPs. */
  953. setup_full_capture_of_logs(LOG_INFO);
  954. update_all_descriptors(now);
  955. expect_log_msg_containing("Unable to find a suitable node to be an "
  956. "introduction point for service");
  957. teardown_capture_of_logs();
  958. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  959. OP_EQ, 0);
  960. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  961. OP_EQ, 0);
  962. /* Now, we'll setup a node_t. */
  963. {
  964. routerinfo_t ri;
  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. It "
  1002. "currently has 0 intro points. Launching "
  1003. "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. done:
  1042. hs_free_all();
  1043. nodelist_free_all();
  1044. UNMOCK(hs_overlap_mode_is_active);
  1045. }
  1046. static void
  1047. test_upload_descriptors(void *arg)
  1048. {
  1049. int ret;
  1050. time_t now = time(NULL);
  1051. hs_service_t *service;
  1052. (void) arg;
  1053. hs_init();
  1054. MOCK(hs_overlap_mode_is_active, mock_hs_overlap_mode_is_active_true);
  1055. MOCK(get_or_state,
  1056. get_or_state_replacement);
  1057. MOCK(networkstatus_get_live_consensus,
  1058. mock_networkstatus_get_live_consensus);
  1059. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1060. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1061. &mock_ns.valid_after);
  1062. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1063. &mock_ns.fresh_until);
  1064. tt_int_op(ret, OP_EQ, 0);
  1065. /* Create a service with no descriptor. It's added to the global map. */
  1066. service = hs_service_new(get_options());
  1067. tt_assert(service);
  1068. service->config.version = HS_VERSION_THREE;
  1069. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1070. ed25519_public_key_generate(&service->keys.identity_pk,
  1071. &service->keys.identity_sk);
  1072. /* Register service to global map. */
  1073. ret = register_service(get_hs_service_map(), service);
  1074. tt_int_op(ret, OP_EQ, 0);
  1075. /* But first, build our descriptor. */
  1076. build_all_descriptors(now);
  1077. /* Nothing should happen because we have 0 introduction circuit established
  1078. * and we want (by default) 3 intro points. */
  1079. run_upload_descriptor_event(now);
  1080. /* If no upload happened, this should be untouched. */
  1081. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1082. /* We'll simulate that we've opened our intro point circuit and that we only
  1083. * want one intro point. */
  1084. service->config.num_intro_points = 1;
  1085. /* Set our next upload time after now which will skip the upload. */
  1086. service->desc_current->next_upload_time = now + 1000;
  1087. run_upload_descriptor_event(now);
  1088. /* If no upload happened, this should be untouched. */
  1089. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, now + 1000);
  1090. done:
  1091. hs_free_all();
  1092. UNMOCK(hs_overlap_mode_is_active);
  1093. UNMOCK(get_or_state);
  1094. }
  1095. /** Test the functions that save and load HS revision counters to state. */
  1096. static void
  1097. test_revision_counter_state(void *arg)
  1098. {
  1099. char *state_line_one = NULL;
  1100. char *state_line_two = NULL;
  1101. hs_service_descriptor_t *desc_one = service_descriptor_new();
  1102. hs_service_descriptor_t *desc_two = service_descriptor_new();
  1103. (void) arg;
  1104. /* Prepare both descriptors */
  1105. desc_one->desc->plaintext_data.revision_counter = 42;
  1106. desc_two->desc->plaintext_data.revision_counter = 240;
  1107. memset(&desc_one->blinded_kp.pubkey.pubkey, 66,
  1108. sizeof(desc_one->blinded_kp.pubkey.pubkey));
  1109. memset(&desc_two->blinded_kp.pubkey.pubkey, 240,
  1110. sizeof(desc_one->blinded_kp.pubkey.pubkey));
  1111. /* Turn the descriptor rev counters into state lines */
  1112. state_line_one = encode_desc_rev_counter_for_state(desc_one);
  1113. tt_str_op(state_line_one, OP_EQ,
  1114. "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkI 42");
  1115. state_line_two = encode_desc_rev_counter_for_state(desc_two);
  1116. tt_str_op(state_line_two, OP_EQ,
  1117. "8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PA 240");
  1118. /* Now let's test our state parsing function: */
  1119. int service_found;
  1120. uint64_t cached_rev_counter;
  1121. /* First's try with wrong pubkey and check that no service was found */
  1122. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_one,
  1123. &desc_two->blinded_kp.pubkey,
  1124. &service_found);
  1125. tt_int_op(service_found, OP_EQ, 0);
  1126. /* Now let's try with the right pubkeys */
  1127. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_one,
  1128. &desc_one->blinded_kp.pubkey,
  1129. &service_found);
  1130. tt_int_op(service_found, OP_EQ, 1);
  1131. tt_u64_op(cached_rev_counter, OP_EQ, 42);
  1132. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_two,
  1133. &desc_two->blinded_kp.pubkey,
  1134. &service_found);
  1135. tt_int_op(service_found, OP_EQ, 1);
  1136. tt_u64_op(cached_rev_counter, OP_EQ, 240);
  1137. done:
  1138. tor_free(state_line_one);
  1139. tor_free(state_line_two);
  1140. service_descriptor_free(desc_one);
  1141. service_descriptor_free(desc_two);
  1142. }
  1143. struct testcase_t hs_service_tests[] = {
  1144. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  1145. NULL, NULL },
  1146. { "load_keys", test_load_keys, TT_FORK,
  1147. NULL, NULL },
  1148. { "access_service", test_access_service, TT_FORK,
  1149. NULL, NULL },
  1150. { "service_intro_point", test_service_intro_point, TT_FORK,
  1151. NULL, NULL },
  1152. { "helper_functions", test_helper_functions, TT_FORK,
  1153. NULL, NULL },
  1154. { "intro_circuit_opened", test_intro_circuit_opened, TT_FORK,
  1155. NULL, NULL },
  1156. { "intro_established", test_intro_established, TT_FORK,
  1157. NULL, NULL },
  1158. { "rdv_circuit_opened", test_rdv_circuit_opened, TT_FORK,
  1159. NULL, NULL },
  1160. { "introduce2", test_introduce2, TT_FORK,
  1161. NULL, NULL },
  1162. { "service_event", test_service_event, TT_FORK,
  1163. NULL, NULL },
  1164. { "rotate_descriptors", test_rotate_descriptors, TT_FORK,
  1165. NULL, NULL },
  1166. { "build_update_descriptors", test_build_update_descriptors, TT_FORK,
  1167. NULL, NULL },
  1168. { "upload_descriptors", test_upload_descriptors, TT_FORK,
  1169. NULL, NULL },
  1170. { "revision_counter_state", test_revision_counter_state, TT_FORK,
  1171. NULL, NULL },
  1172. END_OF_TESTCASES
  1173. };