test_hs_service.c 57 KB

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