test_hs_service.c 54 KB

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