test_hs_service.c 45 KB

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