test_hs_service.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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. #include "shared_random_state.h"
  44. /* Trunnel */
  45. #include "hs/cell_establish_intro.h"
  46. static networkstatus_t mock_ns;
  47. static networkstatus_t *
  48. mock_networkstatus_get_live_consensus(time_t now)
  49. {
  50. (void) now;
  51. return &mock_ns;
  52. }
  53. static or_state_t *dummy_state = NULL;
  54. /* Mock function to get fake or state (used for rev counters) */
  55. static or_state_t *
  56. get_or_state_replacement(void)
  57. {
  58. return dummy_state;
  59. }
  60. /* Mock function because we are not trying to test the close circuit that does
  61. * an awful lot of checks on the circuit object. */
  62. static void
  63. mock_circuit_mark_for_close(circuit_t *circ, int reason, int line,
  64. const char *file)
  65. {
  66. (void) circ;
  67. (void) reason;
  68. (void) line;
  69. (void) file;
  70. return;
  71. }
  72. static int
  73. mock_relay_send_command_from_edge(streamid_t stream_id, circuit_t *circ,
  74. uint8_t relay_command, const char *payload,
  75. size_t payload_len,
  76. crypt_path_t *cpath_layer,
  77. const char *filename, int lineno)
  78. {
  79. (void) stream_id;
  80. (void) circ;
  81. (void) relay_command;
  82. (void) payload;
  83. (void) payload_len;
  84. (void) cpath_layer;
  85. (void) filename;
  86. (void) lineno;
  87. return 0;
  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_ptr_op(query, OP_EQ, NULL);
  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_ptr_op(query, OP_EQ, NULL);
  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. /** Test that we can create intro point objects, index them and find them */
  335. static void
  336. test_service_intro_point(void *arg)
  337. {
  338. hs_service_t *service = NULL;
  339. hs_service_intro_point_t *ip = NULL;
  340. (void) arg;
  341. /* Test simple creation of an object. */
  342. {
  343. time_t now = time(NULL);
  344. ip = helper_create_service_ip();
  345. tt_assert(ip);
  346. /* Make sure the authentication keypair is not zeroes. */
  347. tt_int_op(tor_mem_is_zero((const char *) &ip->auth_key_kp,
  348. sizeof(ed25519_keypair_t)), OP_EQ, 0);
  349. /* The introduce2_max MUST be in that range. */
  350. tt_u64_op(ip->introduce2_max, OP_GE,
  351. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  352. tt_u64_op(ip->introduce2_max, OP_LE,
  353. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  354. /* Time to expire MUST also be in that range. We add 5 seconds because
  355. * there could be a gap between setting now and the time taken in
  356. * service_intro_point_new. On ARM, it can be surprisingly slow... */
  357. tt_u64_op(ip->time_to_expire, OP_GE,
  358. now + INTRO_POINT_LIFETIME_MIN_SECONDS + 5);
  359. tt_u64_op(ip->time_to_expire, OP_LE,
  360. now + INTRO_POINT_LIFETIME_MAX_SECONDS + 5);
  361. tt_assert(ip->replay_cache);
  362. tt_assert(ip->base.link_specifiers);
  363. /* By default, this is NOT a legacy object. */
  364. tt_int_op(ip->base.is_only_legacy, OP_EQ, 0);
  365. }
  366. /* Test functions that uses a service intropoints map with that previously
  367. * created object (non legacy). */
  368. {
  369. ed25519_public_key_t garbage = { {0} };
  370. hs_service_intro_point_t *query;
  371. service = hs_service_new(get_options());
  372. tt_assert(service);
  373. service->desc_current = service_descriptor_new();
  374. tt_assert(service->desc_current);
  375. /* Add intropoint to descriptor map. */
  376. service_intro_point_add(service->desc_current->intro_points.map, ip);
  377. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  378. tt_mem_op(query, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  379. query = service_intro_point_find(service, &garbage);
  380. tt_ptr_op(query, OP_EQ, 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_ptr_op(query, OP_EQ, 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_ptr_op(s_lookup, OP_EQ, NULL);
  451. tt_ptr_op(ip_lookup, OP_EQ, NULL);
  452. tt_ptr_op(desc_lookup, OP_EQ, NULL);
  453. }
  454. /* Testing get_node_from_intro_point() */
  455. {
  456. const node_t *node = get_node_from_intro_point(ip);
  457. tt_ptr_op(node, OP_EQ, &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_ptr_op(node, OP_EQ, 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. /** Test that we do the right operations when an intro circuit opens */
  522. static void
  523. test_intro_circuit_opened(void *arg)
  524. {
  525. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  526. hs_service_t *service;
  527. origin_circuit_t *circ = NULL;
  528. (void) arg;
  529. hs_init();
  530. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  531. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  532. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  533. flags);
  534. /* No service associated with this circuit. */
  535. setup_full_capture_of_logs(LOG_WARN);
  536. hs_service_circuit_has_opened(circ);
  537. expect_log_msg_containing("Unknown service identity key");
  538. teardown_capture_of_logs();
  539. /* Set a service for this circuit. */
  540. {
  541. service = helper_create_service();
  542. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  543. &service->keys.identity_pk);
  544. /* No intro point 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 introduction point auth key");
  548. teardown_capture_of_logs();
  549. }
  550. /* Set an IP object now for this circuit. */
  551. {
  552. hs_service_intro_point_t *ip = helper_create_service_ip();
  553. service_intro_point_add(service->desc_current->intro_points.map, ip);
  554. /* Update ident to contain the intro point auth key. */
  555. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  556. &ip->auth_key_kp.pubkey);
  557. }
  558. /* This one should go all the way. */
  559. setup_full_capture_of_logs(LOG_INFO);
  560. hs_service_circuit_has_opened(circ);
  561. expect_log_msg_containing("Introduction circuit 0 established for service");
  562. teardown_capture_of_logs();
  563. done:
  564. circuit_free(TO_CIRCUIT(circ));
  565. hs_free_all();
  566. UNMOCK(circuit_mark_for_close_);
  567. UNMOCK(relay_send_command_from_edge_);
  568. }
  569. /** Test the operations we do on a circuit after we learn that we successfuly
  570. * established an intro point on it */
  571. static void
  572. test_intro_established(void *arg)
  573. {
  574. int ret;
  575. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  576. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  577. origin_circuit_t *circ = NULL;
  578. hs_service_t *service;
  579. hs_service_intro_point_t *ip = NULL;
  580. (void) arg;
  581. hs_init();
  582. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  583. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  584. flags);
  585. tt_assert(circ);
  586. /* Test a wrong purpose. */
  587. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  588. setup_full_capture_of_logs(LOG_WARN);
  589. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  590. tt_int_op(ret, OP_EQ, -1);
  591. expect_log_msg_containing("Received an INTRO_ESTABLISHED cell on a "
  592. "non introduction circuit of purpose");
  593. teardown_capture_of_logs();
  594. /* Back to normal. */
  595. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  596. /* No service associated to it. */
  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("Unknown service identity key");
  601. teardown_capture_of_logs();
  602. /* Set a service for this circuit. */
  603. service = helper_create_service();
  604. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  605. &service->keys.identity_pk);
  606. /* No introduction point associated to it. */
  607. setup_full_capture_of_logs(LOG_WARN);
  608. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  609. tt_int_op(ret, OP_EQ, -1);
  610. expect_log_msg_containing("Introduction circuit established without an "
  611. "intro point object on circuit");
  612. teardown_capture_of_logs();
  613. /* Set an IP object now for this circuit. */
  614. {
  615. ip = helper_create_service_ip();
  616. service_intro_point_add(service->desc_current->intro_points.map, ip);
  617. /* Update ident to contain the intro point auth key. */
  618. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  619. &ip->auth_key_kp.pubkey);
  620. }
  621. /* Send an empty payload. INTRO_ESTABLISHED cells are basically zeroes. */
  622. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  623. tt_int_op(ret, OP_EQ, 0);
  624. tt_u64_op(ip->circuit_established, OP_EQ, 1);
  625. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_INTRO);
  626. done:
  627. if (circ)
  628. circuit_free(TO_CIRCUIT(circ));
  629. hs_free_all();
  630. UNMOCK(circuit_mark_for_close_);
  631. }
  632. /** Check the operations we do on a rendezvous circuit after we learn it's
  633. * open */
  634. static void
  635. test_rdv_circuit_opened(void *arg)
  636. {
  637. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  638. origin_circuit_t *circ = NULL;
  639. hs_service_t *service;
  640. (void) arg;
  641. hs_init();
  642. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  643. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  644. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND, flags);
  645. crypto_rand((char *) circ->hs_ident->rendezvous_cookie, REND_COOKIE_LEN);
  646. crypto_rand((char *) circ->hs_ident->rendezvous_handshake_info,
  647. sizeof(circ->hs_ident->rendezvous_handshake_info));
  648. /* No service associated with this circuit. */
  649. setup_full_capture_of_logs(LOG_WARN);
  650. hs_service_circuit_has_opened(circ);
  651. expect_log_msg_containing("Unknown service identity key");
  652. teardown_capture_of_logs();
  653. /* This should be set to a non zero timestamp. */
  654. tt_u64_op(TO_CIRCUIT(circ)->timestamp_dirty, OP_NE, 0);
  655. /* Set a service for this circuit. */
  656. service = helper_create_service();
  657. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  658. &service->keys.identity_pk);
  659. /* Should be all good. */
  660. hs_service_circuit_has_opened(circ);
  661. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  662. done:
  663. circuit_free(TO_CIRCUIT(circ));
  664. hs_free_all();
  665. UNMOCK(circuit_mark_for_close_);
  666. UNMOCK(relay_send_command_from_edge_);
  667. }
  668. /** Test sending and receiving introduce2 cells */
  669. static void
  670. test_introduce2(void *arg)
  671. {
  672. int ret;
  673. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  674. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  675. origin_circuit_t *circ = NULL;
  676. hs_service_t *service;
  677. hs_service_intro_point_t *ip = NULL;
  678. (void) arg;
  679. hs_init();
  680. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  681. MOCK(get_or_state,
  682. get_or_state_replacement);
  683. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  684. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  685. tt_assert(circ);
  686. /* Test a wrong purpose. */
  687. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  688. setup_full_capture_of_logs(LOG_WARN);
  689. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  690. tt_int_op(ret, OP_EQ, -1);
  691. expect_log_msg_containing("Received an INTRODUCE2 cell on a "
  692. "non introduction circuit of purpose");
  693. teardown_capture_of_logs();
  694. /* Back to normal. */
  695. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  696. /* No service associated to it. */
  697. setup_full_capture_of_logs(LOG_WARN);
  698. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  699. tt_int_op(ret, OP_EQ, -1);
  700. expect_log_msg_containing("Unknown service identity key");
  701. teardown_capture_of_logs();
  702. /* Set a service for this circuit. */
  703. service = helper_create_service();
  704. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  705. &service->keys.identity_pk);
  706. /* No introduction point associated to it. */
  707. setup_full_capture_of_logs(LOG_WARN);
  708. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  709. tt_int_op(ret, OP_EQ, -1);
  710. expect_log_msg_containing("Unknown introduction auth key when handling "
  711. "an INTRODUCE2 cell on circuit");
  712. teardown_capture_of_logs();
  713. /* Set an IP object now for this circuit. */
  714. {
  715. ip = helper_create_service_ip();
  716. service_intro_point_add(service->desc_current->intro_points.map, ip);
  717. /* Update ident to contain the intro point auth key. */
  718. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  719. &ip->auth_key_kp.pubkey);
  720. }
  721. /* This will fail because receiving an INTRODUCE2 cell implies a valid cell
  722. * and then launching circuits so let's not do that and instead test that
  723. * behaviour differently. */
  724. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  725. tt_int_op(ret, OP_EQ, -1);
  726. tt_u64_op(ip->introduce2_count, OP_EQ, 0);
  727. done:
  728. or_state_free(dummy_state);
  729. dummy_state = NULL;
  730. if (circ)
  731. circuit_free(TO_CIRCUIT(circ));
  732. hs_free_all();
  733. UNMOCK(circuit_mark_for_close_);
  734. }
  735. /** Test basic hidden service housekeeping operations (maintaining intro
  736. * points, etc) */
  737. static void
  738. test_service_event(void *arg)
  739. {
  740. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  741. time_t now = time(NULL);
  742. hs_service_t *service;
  743. origin_circuit_t *circ = NULL;
  744. (void) arg;
  745. hs_init();
  746. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  747. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  748. /* Set a service for this circuit. */
  749. service = helper_create_service();
  750. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  751. &service->keys.identity_pk);
  752. /* Currently this consists of cleaning invalid intro points. So adding IPs
  753. * here that should get cleaned up. */
  754. {
  755. hs_service_intro_point_t *ip = helper_create_service_ip();
  756. service_intro_point_add(service->desc_current->intro_points.map, ip);
  757. /* This run will remove the IP because we have no circuits nor node_t
  758. * associated with it. */
  759. run_housekeeping_event(now);
  760. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  761. OP_EQ, 0);
  762. /* We'll trigger a removal because we've reached our maximum amount of
  763. * times we should retry a circuit. For this, we need to have a node_t
  764. * that matches the identity of this IP. */
  765. routerinfo_t ri;
  766. memset(&ri, 0, sizeof(ri));
  767. ip = helper_create_service_ip();
  768. service_intro_point_add(service->desc_current->intro_points.map, ip);
  769. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  770. /* This triggers a node_t creation. */
  771. tt_assert(nodelist_set_routerinfo(&ri, NULL));
  772. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  773. run_housekeeping_event(now);
  774. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  775. OP_EQ, 0);
  776. /* No removal but no circuit so this means the IP object will stay in the
  777. * descriptor map so we can retry it. */
  778. ip = helper_create_service_ip();
  779. service_intro_point_add(service->desc_current->intro_points.map, ip);
  780. ip->circuit_established = 1; /* We'll test that, it MUST be 0 after. */
  781. run_housekeeping_event(now);
  782. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  783. OP_EQ, 1);
  784. /* Remove the IP object at once for the next test. */
  785. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  786. run_housekeeping_event(now);
  787. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  788. OP_EQ, 0);
  789. /* Now, we'll create an IP with a registered circuit. The IP object
  790. * shouldn't go away. */
  791. ip = helper_create_service_ip();
  792. service_intro_point_add(service->desc_current->intro_points.map, ip);
  793. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  794. &ip->auth_key_kp.pubkey);
  795. hs_circuitmap_register_intro_circ_v3_service_side(
  796. circ, &ip->auth_key_kp.pubkey);
  797. run_housekeeping_event(now);
  798. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  799. OP_EQ, 1);
  800. /* We'll mangle the IP object to expire. */
  801. ip->time_to_expire = now;
  802. run_housekeeping_event(now);
  803. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  804. OP_EQ, 0);
  805. }
  806. done:
  807. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  808. circuit_free(TO_CIRCUIT(circ));
  809. hs_free_all();
  810. UNMOCK(circuit_mark_for_close_);
  811. }
  812. /** Test that we rotate descriptors correctly. */
  813. static void
  814. test_rotate_descriptors(void *arg)
  815. {
  816. int ret;
  817. time_t next_rotation_time, now = time(NULL);
  818. hs_service_t *service;
  819. hs_service_descriptor_t *desc_next;
  820. (void) arg;
  821. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  822. hs_init();
  823. MOCK(get_or_state, get_or_state_replacement);
  824. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  825. MOCK(networkstatus_get_live_consensus,
  826. mock_networkstatus_get_live_consensus);
  827. /* Descriptor rotation happens with a consensus with a new SRV. */
  828. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  829. &mock_ns.valid_after);
  830. tt_int_op(ret, OP_EQ, 0);
  831. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  832. &mock_ns.fresh_until);
  833. tt_int_op(ret, OP_EQ, 0);
  834. /* Create a service with a default descriptor and state. It's added to the
  835. * global map. */
  836. service = helper_create_service();
  837. service_descriptor_free(service->desc_current);
  838. service->desc_current = NULL;
  839. /* This triggers a build for both descriptors. The time now is only used in
  840. * the descriptor certificate which is important to be now else the decoding
  841. * will complain that the cert has expired if we use valid_after. */
  842. build_all_descriptors(now);
  843. tt_assert(service->desc_current);
  844. tt_assert(service->desc_next);
  845. /* Tweak our service next rotation time so we can use a custom time. */
  846. service->state.next_rotation_time = next_rotation_time =
  847. mock_ns.valid_after + (11 * 60 * 60);
  848. /* Nothing should happen, we are not at a new SRV. Our next rotation time
  849. * should be untouched. */
  850. rotate_all_descriptors(mock_ns.valid_after);
  851. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  852. tt_assert(service->desc_current);
  853. tt_assert(service->desc_next);
  854. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  855. hs_get_previous_time_period_num(0));
  856. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  857. hs_get_time_period_num(0));
  858. /* Keep a reference so we can compare it after rotation to the current. */
  859. desc_next = service->desc_next;
  860. /* Going right after a new SRV. */
  861. ret = parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC",
  862. &mock_ns.valid_after);
  863. tt_int_op(ret, OP_EQ, 0);
  864. ret = parse_rfc1123_time("Sat, 27 Oct 1985 02:00:00 UTC",
  865. &mock_ns.fresh_until);
  866. tt_int_op(ret, OP_EQ, 0);
  867. /* Note down what to expect for the next rotation time which is 01:00 + 23h
  868. * meaning 00:00:00. */
  869. next_rotation_time = mock_ns.valid_after + (23 * 60 * 60);
  870. /* We should have our next rotation time modified, our current descriptor
  871. * cleaned up and the next descriptor becoming the current. */
  872. rotate_all_descriptors(mock_ns.valid_after);
  873. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  874. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  875. tt_assert(service->desc_next == NULL);
  876. /* A second time should do nothing. */
  877. rotate_all_descriptors(mock_ns.valid_after);
  878. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  879. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  880. tt_assert(service->desc_next == NULL);
  881. build_all_descriptors(now);
  882. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  883. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  884. hs_get_time_period_num(0));
  885. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  886. hs_get_next_time_period_num(0));
  887. tt_assert(service->desc_next);
  888. done:
  889. hs_free_all();
  890. UNMOCK(get_or_state);
  891. UNMOCK(circuit_mark_for_close_);
  892. UNMOCK(networkstatus_get_live_consensus);
  893. }
  894. /** Test building descriptors: picking intro points, setting up their link
  895. * specifiers, etc. */
  896. static void
  897. test_build_update_descriptors(void *arg)
  898. {
  899. int ret;
  900. time_t now = time(NULL);
  901. node_t *node;
  902. hs_service_t *service;
  903. hs_service_intro_point_t *ip_cur, *ip_next;
  904. routerinfo_t ri;
  905. (void) arg;
  906. hs_init();
  907. MOCK(get_or_state,
  908. get_or_state_replacement);
  909. MOCK(networkstatus_get_live_consensus,
  910. mock_networkstatus_get_live_consensus);
  911. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  912. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  913. &mock_ns.valid_after);
  914. tt_int_op(ret, OP_EQ, 0);
  915. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  916. &mock_ns.fresh_until);
  917. tt_int_op(ret, OP_EQ, 0);
  918. /* Create a service without a current descriptor to trigger a build. */
  919. service = helper_create_service();
  920. tt_assert(service);
  921. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  922. service_descriptor_free(service->desc_current);
  923. service->desc_current = NULL;
  924. /* We have a fresh service so this should trigger a build for both
  925. * descriptors for specific time period that we'll test. */
  926. build_all_descriptors(now);
  927. /* Check *current* descriptor. */
  928. tt_assert(service->desc_current);
  929. tt_assert(service->desc_current->desc);
  930. tt_assert(service->desc_current->intro_points.map);
  931. /* The current time period is the one expected when starting at 03:00. */
  932. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  933. hs_get_time_period_num(0));
  934. /* This should be untouched, the update descriptor process changes it. */
  935. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  936. /* Check *next* descriptor. */
  937. tt_assert(service->desc_next);
  938. tt_assert(service->desc_next->desc);
  939. tt_assert(service->desc_next->intro_points.map);
  940. tt_assert(service->desc_current != service->desc_next);
  941. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  942. hs_get_next_time_period_num(0));
  943. /* This should be untouched, the update descriptor process changes it. */
  944. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  945. /* Time to test the update of those descriptors. At first, we have no node
  946. * in the routerlist so this will find NO suitable node for the IPs. */
  947. setup_full_capture_of_logs(LOG_INFO);
  948. update_all_descriptors(now);
  949. expect_log_msg_containing("Unable to find a suitable node to be an "
  950. "introduction point for service");
  951. teardown_capture_of_logs();
  952. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  953. OP_EQ, 0);
  954. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  955. OP_EQ, 0);
  956. /* Now, we'll setup a node_t. */
  957. {
  958. tor_addr_t ipv4_addr;
  959. curve25519_secret_key_t curve25519_secret_key;
  960. memset(&ri, 0, sizeof(routerinfo_t));
  961. tor_addr_parse(&ipv4_addr, "127.0.0.1");
  962. ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  963. ri.or_port = 1337;
  964. ri.purpose = ROUTER_PURPOSE_GENERAL;
  965. /* Ugly yes but we never free the "ri" object so this just makes things
  966. * easier. */
  967. ri.protocol_list = (char *) "HSDir=1-2 LinkAuth=3";
  968. ret = curve25519_secret_key_generate(&curve25519_secret_key, 0);
  969. tt_int_op(ret, OP_EQ, 0);
  970. ri.onion_curve25519_pkey =
  971. tor_malloc_zero(sizeof(curve25519_public_key_t));
  972. ri.onion_pkey = crypto_pk_new();
  973. curve25519_public_key_generate(ri.onion_curve25519_pkey,
  974. &curve25519_secret_key);
  975. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  976. /* Setup ed25519 identity */
  977. ed25519_keypair_t kp1;
  978. ed25519_keypair_generate(&kp1, 0);
  979. ri.cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  980. tt_assert(ri.cache_info.signing_key_cert);
  981. ed25519_pubkey_copy(&ri.cache_info.signing_key_cert->signing_key,
  982. &kp1.pubkey);
  983. nodelist_set_routerinfo(&ri, NULL);
  984. node = node_get_mutable_by_id(ri.cache_info.identity_digest);
  985. tt_assert(node);
  986. node->is_running = node->is_valid = node->is_fast = node->is_stable = 1;
  987. }
  988. /* We expect to pick only one intro point from the node above. */
  989. setup_full_capture_of_logs(LOG_INFO);
  990. update_all_descriptors(now);
  991. tor_free(node->ri->onion_curve25519_pkey); /* Avoid memleak. */
  992. tor_free(node->ri->cache_info.signing_key_cert);
  993. crypto_pk_free(node->ri->onion_pkey);
  994. expect_log_msg_containing("just picked 1 intro points and wanted 3. It "
  995. "currently has 0 intro points. Launching "
  996. "ESTABLISH_INTRO circuit shortly.");
  997. teardown_capture_of_logs();
  998. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  999. OP_EQ, 1);
  1000. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1001. OP_EQ, 1);
  1002. /* Get the IP object. Because we don't have the auth key of the IP, we can't
  1003. * query it so get the first element in the map. */
  1004. {
  1005. void *obj = NULL;
  1006. const uint8_t *key;
  1007. digest256map_iter_t *iter =
  1008. digest256map_iter_init(service->desc_current->intro_points.map);
  1009. digest256map_iter_get(iter, &key, &obj);
  1010. tt_assert(obj);
  1011. ip_cur = obj;
  1012. /* Get also the IP from the next descriptor. We'll make sure it's not the
  1013. * same object as in the current descriptor. */
  1014. iter = digest256map_iter_init(service->desc_next->intro_points.map);
  1015. digest256map_iter_get(iter, &key, &obj);
  1016. tt_assert(obj);
  1017. ip_next = obj;
  1018. }
  1019. tt_mem_op(ip_cur, OP_NE, ip_next, sizeof(hs_desc_intro_point_t));
  1020. /* We won't test the service IP object because there is a specific test
  1021. * already for this but we'll make sure that the state is coherent.*/
  1022. /* Three link specifiers are mandatoy so make sure we do have them. */
  1023. tt_int_op(smartlist_len(ip_cur->base.link_specifiers), OP_EQ, 3);
  1024. /* Make sure we have a valid encryption keypair generated when we pick an
  1025. * intro point in the update process. */
  1026. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.seckey.secret_key,
  1027. CURVE25519_SECKEY_LEN));
  1028. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.pubkey.public_key,
  1029. CURVE25519_PUBKEY_LEN));
  1030. tt_u64_op(ip_cur->time_to_expire, OP_GE, now +
  1031. INTRO_POINT_LIFETIME_MIN_SECONDS);
  1032. tt_u64_op(ip_cur->time_to_expire, OP_LE, now +
  1033. INTRO_POINT_LIFETIME_MAX_SECONDS);
  1034. /* Now, we will try to set up a service after a new time period has started
  1035. * and see if it behaves as expected. */
  1036. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1037. &mock_ns.valid_after);
  1038. tt_int_op(ret, OP_EQ, 0);
  1039. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1040. &mock_ns.fresh_until);
  1041. tt_int_op(ret, OP_EQ, 0);
  1042. /* Create a service without a current descriptor to trigger a build. */
  1043. service = helper_create_service();
  1044. tt_assert(service);
  1045. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1046. service_descriptor_free(service->desc_current);
  1047. service->desc_current = NULL;
  1048. /* We have a fresh service so this should trigger a build for both
  1049. * descriptors for specific time period that we'll test. */
  1050. build_all_descriptors(now);
  1051. /* Check *current* descriptor. */
  1052. tt_assert(service->desc_current);
  1053. tt_assert(service->desc_current->desc);
  1054. tt_assert(service->desc_current->intro_points.map);
  1055. /* This should be for the previous time period. */
  1056. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1057. hs_get_previous_time_period_num(0));
  1058. /* This should be untouched, the update descriptor process changes it. */
  1059. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1060. /* Check *next* descriptor. */
  1061. tt_assert(service->desc_next);
  1062. tt_assert(service->desc_next->desc);
  1063. tt_assert(service->desc_next->intro_points.map);
  1064. tt_assert(service->desc_current != service->desc_next);
  1065. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1066. hs_get_time_period_num(0));
  1067. /* This should be untouched, the update descriptor process changes it. */
  1068. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1069. /* Let's remove the next descriptor to simulate a rotation. */
  1070. service_descriptor_free(service->desc_next);
  1071. service->desc_next = NULL;
  1072. build_all_descriptors(now);
  1073. /* Check *next* descriptor. */
  1074. tt_assert(service->desc_next);
  1075. tt_assert(service->desc_next->desc);
  1076. tt_assert(service->desc_next->intro_points.map);
  1077. tt_assert(service->desc_current != service->desc_next);
  1078. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1079. hs_get_next_time_period_num(0));
  1080. /* This should be untouched, the update descriptor process changes it. */
  1081. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1082. done:
  1083. hs_free_all();
  1084. nodelist_free_all();
  1085. }
  1086. static void
  1087. test_upload_descriptors(void *arg)
  1088. {
  1089. int ret;
  1090. time_t now = time(NULL);
  1091. hs_service_t *service;
  1092. (void) arg;
  1093. hs_init();
  1094. MOCK(get_or_state,
  1095. get_or_state_replacement);
  1096. MOCK(networkstatus_get_live_consensus,
  1097. mock_networkstatus_get_live_consensus);
  1098. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1099. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1100. &mock_ns.valid_after);
  1101. tt_int_op(ret, OP_EQ, 0);
  1102. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1103. &mock_ns.fresh_until);
  1104. tt_int_op(ret, OP_EQ, 0);
  1105. /* Create a service with no descriptor. It's added to the global map. */
  1106. service = hs_service_new(get_options());
  1107. tt_assert(service);
  1108. service->config.version = HS_VERSION_THREE;
  1109. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1110. ed25519_public_key_generate(&service->keys.identity_pk,
  1111. &service->keys.identity_sk);
  1112. /* Register service to global map. */
  1113. ret = register_service(get_hs_service_map(), service);
  1114. tt_int_op(ret, OP_EQ, 0);
  1115. /* But first, build our descriptor. */
  1116. build_all_descriptors(now);
  1117. /* Nothing should happen because we have 0 introduction circuit established
  1118. * and we want (by default) 3 intro points. */
  1119. run_upload_descriptor_event(now);
  1120. /* If no upload happened, this should be untouched. */
  1121. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1122. /* We'll simulate that we've opened our intro point circuit and that we only
  1123. * want one intro point. */
  1124. service->config.num_intro_points = 1;
  1125. /* Set our next upload time after now which will skip the upload. */
  1126. service->desc_current->next_upload_time = now + 1000;
  1127. run_upload_descriptor_event(now);
  1128. /* If no upload happened, this should be untouched. */
  1129. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, now + 1000);
  1130. done:
  1131. hs_free_all();
  1132. UNMOCK(get_or_state);
  1133. }
  1134. /** Test the functions that save and load HS revision counters to state. */
  1135. static void
  1136. test_revision_counter_state(void *arg)
  1137. {
  1138. char *state_line_one = NULL;
  1139. char *state_line_two = NULL;
  1140. hs_service_descriptor_t *desc_one = service_descriptor_new();
  1141. hs_service_descriptor_t *desc_two = service_descriptor_new();
  1142. (void) arg;
  1143. /* Prepare both descriptors */
  1144. desc_one->desc->plaintext_data.revision_counter = 42;
  1145. desc_two->desc->plaintext_data.revision_counter = 240;
  1146. memset(&desc_one->blinded_kp.pubkey.pubkey, 66,
  1147. sizeof(desc_one->blinded_kp.pubkey.pubkey));
  1148. memset(&desc_two->blinded_kp.pubkey.pubkey, 240,
  1149. sizeof(desc_one->blinded_kp.pubkey.pubkey));
  1150. /* Turn the descriptor rev counters into state lines */
  1151. state_line_one = encode_desc_rev_counter_for_state(desc_one);
  1152. tt_str_op(state_line_one, OP_EQ,
  1153. "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkI 42");
  1154. state_line_two = encode_desc_rev_counter_for_state(desc_two);
  1155. tt_str_op(state_line_two, OP_EQ,
  1156. "8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PA 240");
  1157. /* Now let's test our state parsing function: */
  1158. int service_found;
  1159. uint64_t cached_rev_counter;
  1160. /* First's try with wrong pubkey and check that no service was found */
  1161. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_one,
  1162. &desc_two->blinded_kp.pubkey,
  1163. &service_found);
  1164. tt_int_op(service_found, OP_EQ, 0);
  1165. tt_u64_op(cached_rev_counter, OP_EQ, 0);
  1166. /* Now let's try with the right pubkeys */
  1167. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_one,
  1168. &desc_one->blinded_kp.pubkey,
  1169. &service_found);
  1170. tt_int_op(service_found, OP_EQ, 1);
  1171. tt_u64_op(cached_rev_counter, OP_EQ, 42);
  1172. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_two,
  1173. &desc_two->blinded_kp.pubkey,
  1174. &service_found);
  1175. tt_int_op(service_found, OP_EQ, 1);
  1176. tt_u64_op(cached_rev_counter, OP_EQ, 240);
  1177. done:
  1178. tor_free(state_line_one);
  1179. tor_free(state_line_two);
  1180. service_descriptor_free(desc_one);
  1181. service_descriptor_free(desc_two);
  1182. }
  1183. struct testcase_t hs_service_tests[] = {
  1184. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  1185. NULL, NULL },
  1186. { "load_keys", test_load_keys, TT_FORK,
  1187. NULL, NULL },
  1188. { "access_service", test_access_service, TT_FORK,
  1189. NULL, NULL },
  1190. { "service_intro_point", test_service_intro_point, TT_FORK,
  1191. NULL, NULL },
  1192. { "helper_functions", test_helper_functions, TT_FORK,
  1193. NULL, NULL },
  1194. { "intro_circuit_opened", test_intro_circuit_opened, TT_FORK,
  1195. NULL, NULL },
  1196. { "intro_established", test_intro_established, TT_FORK,
  1197. NULL, NULL },
  1198. { "rdv_circuit_opened", test_rdv_circuit_opened, TT_FORK,
  1199. NULL, NULL },
  1200. { "introduce2", test_introduce2, TT_FORK,
  1201. NULL, NULL },
  1202. { "service_event", test_service_event, TT_FORK,
  1203. NULL, NULL },
  1204. { "rotate_descriptors", test_rotate_descriptors, TT_FORK,
  1205. NULL, NULL },
  1206. { "build_update_descriptors", test_build_update_descriptors, TT_FORK,
  1207. NULL, NULL },
  1208. { "upload_descriptors", test_upload_descriptors, TT_FORK,
  1209. NULL, NULL },
  1210. { "revision_counter_state", test_revision_counter_state, TT_FORK,
  1211. NULL, NULL },
  1212. END_OF_TESTCASES
  1213. };