test_hs_service.c 57 KB

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