test_hs_service.c 58 KB

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