test_hs_service.c 58 KB

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