test_hs_service.c 59 KB

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