test_hs_service.c 58 KB

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