test_hs_service.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  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. tt_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. done:
  179. return circ;
  180. }
  181. /* Helper: Return a newly allocated service object with the identity keypair
  182. * sets and the current descriptor. Then register it to the global map.
  183. * Caller should us hs_free_all() to free this service or remove it from the
  184. * global map before freeing. */
  185. static hs_service_t *
  186. helper_create_service(void)
  187. {
  188. /* Set a service for this circuit. */
  189. hs_service_t *service = hs_service_new(get_options());
  190. tt_assert(service);
  191. service->config.version = HS_VERSION_THREE;
  192. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  193. ed25519_public_key_generate(&service->keys.identity_pk,
  194. &service->keys.identity_sk);
  195. service->desc_current = service_descriptor_new();
  196. tt_assert(service->desc_current);
  197. /* Register service to global map. */
  198. int ret = register_service(get_hs_service_map(), service);
  199. tt_int_op(ret, OP_EQ, 0);
  200. done:
  201. return service;
  202. }
  203. /* Helper: Return a newly allocated service intro point with two link
  204. * specifiers, one IPv4 and one legacy ID set to As. */
  205. static hs_service_intro_point_t *
  206. helper_create_service_ip(void)
  207. {
  208. hs_desc_link_specifier_t *ls;
  209. hs_service_intro_point_t *ip = service_intro_point_new(NULL, 0);
  210. tt_assert(ip);
  211. /* Add a first unused link specifier. */
  212. ls = tor_malloc_zero(sizeof(*ls));
  213. ls->type = LS_IPV4;
  214. smartlist_add(ip->base.link_specifiers, ls);
  215. /* Add a second link specifier used by a test. */
  216. ls = tor_malloc_zero(sizeof(*ls));
  217. ls->type = LS_LEGACY_ID;
  218. memset(ls->u.legacy_id, 'A', sizeof(ls->u.legacy_id));
  219. smartlist_add(ip->base.link_specifiers, ls);
  220. done:
  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 successfuly
  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. ret = curve25519_secret_key_generate(&curve25519_secret_key, 0);
  1051. tt_int_op(ret, OP_EQ, 0);
  1052. ri.onion_curve25519_pkey =
  1053. tor_malloc_zero(sizeof(curve25519_public_key_t));
  1054. ri.onion_pkey = crypto_pk_new();
  1055. curve25519_public_key_generate(ri.onion_curve25519_pkey,
  1056. &curve25519_secret_key);
  1057. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1058. /* Setup ed25519 identity */
  1059. ed25519_keypair_t kp1;
  1060. ed25519_keypair_generate(&kp1, 0);
  1061. ri.cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  1062. tt_assert(ri.cache_info.signing_key_cert);
  1063. ed25519_pubkey_copy(&ri.cache_info.signing_key_cert->signing_key,
  1064. &kp1.pubkey);
  1065. nodelist_set_routerinfo(&ri, NULL);
  1066. node = node_get_mutable_by_id(ri.cache_info.identity_digest);
  1067. tt_assert(node);
  1068. node->is_running = node->is_valid = node->is_fast = node->is_stable = 1;
  1069. }
  1070. /* We expect to pick only one intro point from the node above. */
  1071. setup_full_capture_of_logs(LOG_INFO);
  1072. update_all_descriptors(now);
  1073. tor_free(node->ri->onion_curve25519_pkey); /* Avoid memleak. */
  1074. tor_free(node->ri->cache_info.signing_key_cert);
  1075. crypto_pk_free(node->ri->onion_pkey);
  1076. expect_log_msg_containing("just picked 1 intro points and wanted 3 for next "
  1077. "descriptor. It currently has 0 intro points. "
  1078. "Launching ESTABLISH_INTRO circuit shortly.");
  1079. teardown_capture_of_logs();
  1080. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1081. OP_EQ, 1);
  1082. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1083. OP_EQ, 1);
  1084. /* Get the IP object. Because we don't have the auth key of the IP, we can't
  1085. * query it so get the first element in the map. */
  1086. {
  1087. void *obj = NULL;
  1088. const uint8_t *key;
  1089. digest256map_iter_t *iter =
  1090. digest256map_iter_init(service->desc_current->intro_points.map);
  1091. digest256map_iter_get(iter, &key, &obj);
  1092. tt_assert(obj);
  1093. ip_cur = obj;
  1094. /* Get also the IP from the next descriptor. We'll make sure it's not the
  1095. * same object as in the current descriptor. */
  1096. iter = digest256map_iter_init(service->desc_next->intro_points.map);
  1097. digest256map_iter_get(iter, &key, &obj);
  1098. tt_assert(obj);
  1099. ip_next = obj;
  1100. }
  1101. tt_mem_op(ip_cur, OP_NE, ip_next, sizeof(hs_desc_intro_point_t));
  1102. /* We won't test the service IP object because there is a specific test
  1103. * already for this but we'll make sure that the state is coherent.*/
  1104. /* Three link specifiers are mandatoy so make sure we do have them. */
  1105. tt_int_op(smartlist_len(ip_cur->base.link_specifiers), OP_EQ, 3);
  1106. /* Make sure we have a valid encryption keypair generated when we pick an
  1107. * intro point in the update process. */
  1108. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.seckey.secret_key,
  1109. CURVE25519_SECKEY_LEN));
  1110. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.pubkey.public_key,
  1111. CURVE25519_PUBKEY_LEN));
  1112. tt_u64_op(ip_cur->time_to_expire, OP_GE, now +
  1113. INTRO_POINT_LIFETIME_MIN_SECONDS);
  1114. tt_u64_op(ip_cur->time_to_expire, OP_LE, now +
  1115. INTRO_POINT_LIFETIME_MAX_SECONDS);
  1116. /* Now, we will try to set up a service after a new time period has started
  1117. * and see if it behaves as expected. */
  1118. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1119. &mock_ns.valid_after);
  1120. tt_int_op(ret, OP_EQ, 0);
  1121. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1122. &mock_ns.fresh_until);
  1123. tt_int_op(ret, OP_EQ, 0);
  1124. /* Create a service without a current descriptor to trigger a build. */
  1125. service = helper_create_service();
  1126. tt_assert(service);
  1127. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1128. service_descriptor_free(service->desc_current);
  1129. service->desc_current = NULL;
  1130. /* We have a fresh service so this should trigger a build for both
  1131. * descriptors for specific time period that we'll test. */
  1132. build_all_descriptors(now);
  1133. /* Check *current* descriptor. */
  1134. tt_assert(service->desc_current);
  1135. tt_assert(service->desc_current->desc);
  1136. tt_assert(service->desc_current->intro_points.map);
  1137. /* This should be for the previous time period. */
  1138. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1139. hs_get_previous_time_period_num(0));
  1140. /* This should be untouched, the update descriptor process changes it. */
  1141. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1142. /* Check *next* descriptor. */
  1143. tt_assert(service->desc_next);
  1144. tt_assert(service->desc_next->desc);
  1145. tt_assert(service->desc_next->intro_points.map);
  1146. tt_assert(service->desc_current != service->desc_next);
  1147. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1148. hs_get_time_period_num(0));
  1149. /* This should be untouched, the update descriptor process changes it. */
  1150. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1151. /* Let's remove the next descriptor to simulate a rotation. */
  1152. service_descriptor_free(service->desc_next);
  1153. service->desc_next = NULL;
  1154. build_all_descriptors(now);
  1155. /* Check *next* descriptor. */
  1156. tt_assert(service->desc_next);
  1157. tt_assert(service->desc_next->desc);
  1158. tt_assert(service->desc_next->intro_points.map);
  1159. tt_assert(service->desc_current != service->desc_next);
  1160. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1161. hs_get_next_time_period_num(0));
  1162. /* This should be untouched, the update descriptor process changes it. */
  1163. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1164. done:
  1165. hs_free_all();
  1166. nodelist_free_all();
  1167. }
  1168. static void
  1169. test_upload_descriptors(void *arg)
  1170. {
  1171. int ret;
  1172. time_t now = time(NULL);
  1173. hs_service_t *service;
  1174. (void) arg;
  1175. hs_init();
  1176. MOCK(get_or_state,
  1177. get_or_state_replacement);
  1178. MOCK(networkstatus_get_live_consensus,
  1179. mock_networkstatus_get_live_consensus);
  1180. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1181. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1182. &mock_ns.valid_after);
  1183. tt_int_op(ret, OP_EQ, 0);
  1184. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1185. &mock_ns.fresh_until);
  1186. tt_int_op(ret, OP_EQ, 0);
  1187. /* Create a service with no descriptor. It's added to the global map. */
  1188. service = hs_service_new(get_options());
  1189. tt_assert(service);
  1190. service->config.version = HS_VERSION_THREE;
  1191. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1192. ed25519_public_key_generate(&service->keys.identity_pk,
  1193. &service->keys.identity_sk);
  1194. /* Register service to global map. */
  1195. ret = register_service(get_hs_service_map(), service);
  1196. tt_int_op(ret, OP_EQ, 0);
  1197. /* But first, build our descriptor. */
  1198. build_all_descriptors(now);
  1199. /* Nothing should happen because we have 0 introduction circuit established
  1200. * and we want (by default) 3 intro points. */
  1201. run_upload_descriptor_event(now);
  1202. /* If no upload happened, this should be untouched. */
  1203. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1204. /* We'll simulate that we've opened our intro point circuit and that we only
  1205. * want one intro point. */
  1206. service->config.num_intro_points = 1;
  1207. /* Set our next upload time after now which will skip the upload. */
  1208. service->desc_current->next_upload_time = now + 1000;
  1209. run_upload_descriptor_event(now);
  1210. /* If no upload happened, this should be untouched. */
  1211. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, now + 1000);
  1212. done:
  1213. hs_free_all();
  1214. UNMOCK(get_or_state);
  1215. }
  1216. /** Test the functions that save and load HS revision counters to state. */
  1217. static void
  1218. test_revision_counter_state(void *arg)
  1219. {
  1220. char *state_line_one = NULL;
  1221. char *state_line_two = NULL;
  1222. hs_service_descriptor_t *desc_one = service_descriptor_new();
  1223. hs_service_descriptor_t *desc_two = service_descriptor_new();
  1224. (void) arg;
  1225. /* Prepare both descriptors */
  1226. desc_one->desc->plaintext_data.revision_counter = 42;
  1227. desc_two->desc->plaintext_data.revision_counter = 240;
  1228. memset(&desc_one->blinded_kp.pubkey.pubkey, 66,
  1229. sizeof(desc_one->blinded_kp.pubkey.pubkey));
  1230. memset(&desc_two->blinded_kp.pubkey.pubkey, 240,
  1231. sizeof(desc_one->blinded_kp.pubkey.pubkey));
  1232. /* Turn the descriptor rev counters into state lines */
  1233. state_line_one = encode_desc_rev_counter_for_state(desc_one);
  1234. tt_str_op(state_line_one, OP_EQ,
  1235. "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkI 42");
  1236. state_line_two = encode_desc_rev_counter_for_state(desc_two);
  1237. tt_str_op(state_line_two, OP_EQ,
  1238. "8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PA 240");
  1239. /* Now let's test our state parsing function: */
  1240. int service_found;
  1241. uint64_t cached_rev_counter;
  1242. /* First's try with wrong pubkey and check that no service was found */
  1243. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_one,
  1244. &desc_two->blinded_kp.pubkey,
  1245. &service_found);
  1246. tt_int_op(service_found, OP_EQ, 0);
  1247. tt_u64_op(cached_rev_counter, OP_EQ, 0);
  1248. /* Now let's try with the right pubkeys */
  1249. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_one,
  1250. &desc_one->blinded_kp.pubkey,
  1251. &service_found);
  1252. tt_int_op(service_found, OP_EQ, 1);
  1253. tt_u64_op(cached_rev_counter, OP_EQ, 42);
  1254. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_two,
  1255. &desc_two->blinded_kp.pubkey,
  1256. &service_found);
  1257. tt_int_op(service_found, OP_EQ, 1);
  1258. tt_u64_op(cached_rev_counter, OP_EQ, 240);
  1259. done:
  1260. tor_free(state_line_one);
  1261. tor_free(state_line_two);
  1262. service_descriptor_free(desc_one);
  1263. service_descriptor_free(desc_two);
  1264. }
  1265. /** Global vars used by test_rendezvous1_parsing() */
  1266. static char rend1_payload[RELAY_PAYLOAD_SIZE];
  1267. static size_t rend1_payload_len = 0;
  1268. /** Mock for relay_send_command_from_edge() to send a RENDEZVOUS1 cell. Instead
  1269. * of sending it to the network, instead save it to the global `rend1_payload`
  1270. * variable so that we can inspect it in the test_rendezvous1_parsing()
  1271. * test. */
  1272. static int
  1273. mock_relay_send_rendezvous1(streamid_t stream_id, circuit_t *circ,
  1274. uint8_t relay_command, const char *payload,
  1275. size_t payload_len,
  1276. crypt_path_t *cpath_layer,
  1277. const char *filename, int lineno)
  1278. {
  1279. (void) stream_id;
  1280. (void) circ;
  1281. (void) relay_command;
  1282. (void) cpath_layer;
  1283. (void) filename;
  1284. (void) lineno;
  1285. memcpy(rend1_payload, payload, payload_len);
  1286. rend1_payload_len = payload_len;
  1287. return 0;
  1288. }
  1289. /** Send a RENDEZVOUS1 as a service, and parse it as a client. */
  1290. static void
  1291. test_rendezvous1_parsing(void *arg)
  1292. {
  1293. int retval;
  1294. static const char *test_addr =
  1295. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad.onion";
  1296. hs_service_t *service = NULL;
  1297. origin_circuit_t *service_circ = NULL;
  1298. origin_circuit_t *client_circ = NULL;
  1299. ed25519_keypair_t ip_auth_kp;
  1300. curve25519_keypair_t ephemeral_kp;
  1301. curve25519_keypair_t client_kp;
  1302. curve25519_keypair_t ip_enc_kp;
  1303. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1304. (void) arg;
  1305. MOCK(relay_send_command_from_edge_, mock_relay_send_rendezvous1);
  1306. {
  1307. /* Let's start by setting up the service that will start the rend */
  1308. service = tor_malloc_zero(sizeof(hs_service_t));
  1309. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1310. ed25519_public_key_generate(&service->keys.identity_pk,
  1311. &service->keys.identity_sk);
  1312. memcpy(service->onion_address, test_addr, sizeof(service->onion_address));
  1313. tt_assert(service);
  1314. }
  1315. {
  1316. /* Now let's set up the service rendezvous circuit and its keys. */
  1317. service_circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND,
  1318. flags);
  1319. tor_free(service_circ->hs_ident);
  1320. hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys;
  1321. uint8_t rendezvous_cookie[HS_REND_COOKIE_LEN];
  1322. curve25519_keypair_generate(&ip_enc_kp, 0);
  1323. curve25519_keypair_generate(&ephemeral_kp, 0);
  1324. curve25519_keypair_generate(&client_kp, 0);
  1325. ed25519_keypair_generate(&ip_auth_kp, 0);
  1326. retval = hs_ntor_service_get_rendezvous1_keys(&ip_auth_kp.pubkey,
  1327. &ip_enc_kp,
  1328. &ephemeral_kp,
  1329. &client_kp.pubkey,
  1330. &hs_ntor_rend_cell_keys);
  1331. tt_int_op(retval, OP_EQ, 0);
  1332. memset(rendezvous_cookie, 2, sizeof(rendezvous_cookie));
  1333. service_circ->hs_ident =
  1334. create_rp_circuit_identifier(service, rendezvous_cookie,
  1335. &ephemeral_kp.pubkey,
  1336. &hs_ntor_rend_cell_keys);
  1337. }
  1338. /* Send out the RENDEZVOUS1 and make sure that our mock func worked */
  1339. tt_assert(tor_mem_is_zero(rend1_payload, 32));
  1340. hs_circ_service_rp_has_opened(service, service_circ);
  1341. tt_assert(!tor_mem_is_zero(rend1_payload, 32));
  1342. tt_int_op(rend1_payload_len, OP_EQ, HS_LEGACY_RENDEZVOUS_CELL_SIZE);
  1343. /******************************/
  1344. /** Now let's create the client rendezvous circuit */
  1345. client_circ =
  1346. helper_create_origin_circuit(CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED,
  1347. flags);
  1348. /* fix up its circ ident */
  1349. ed25519_pubkey_copy(&client_circ->hs_ident->intro_auth_pk,
  1350. &ip_auth_kp.pubkey);
  1351. memcpy(&client_circ->hs_ident->rendezvous_client_kp,
  1352. &client_kp, sizeof(client_circ->hs_ident->rendezvous_client_kp));
  1353. memcpy(&client_circ->hs_ident->intro_enc_pk.public_key,
  1354. &ip_enc_kp.pubkey.public_key,
  1355. sizeof(client_circ->hs_ident->intro_enc_pk.public_key));
  1356. /* Now parse the rendezvous2 circuit and make sure it was fine. We are
  1357. * skipping 20 bytes off its payload, since that's the rendezvous cookie
  1358. * which is only present in REND1. */
  1359. retval = handle_rendezvous2(client_circ,
  1360. (uint8_t*)rend1_payload+20,
  1361. rend1_payload_len-20);
  1362. tt_int_op(retval, OP_EQ, 0);
  1363. /* TODO: We are only simulating client/service here. We could also simulate
  1364. * the rendezvous point by plugging in rend_mid_establish_rendezvous(). We
  1365. * would need an extra circuit and some more stuff but it's doable. */
  1366. done:
  1367. circuit_free(TO_CIRCUIT(service_circ));
  1368. circuit_free(TO_CIRCUIT(client_circ));
  1369. hs_service_free(service);
  1370. hs_free_all();
  1371. UNMOCK(relay_send_command_from_edge_);
  1372. }
  1373. struct testcase_t hs_service_tests[] = {
  1374. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  1375. NULL, NULL },
  1376. { "load_keys", test_load_keys, TT_FORK,
  1377. NULL, NULL },
  1378. { "access_service", test_access_service, TT_FORK,
  1379. NULL, NULL },
  1380. { "service_intro_point", test_service_intro_point, TT_FORK,
  1381. NULL, NULL },
  1382. { "helper_functions", test_helper_functions, TT_FORK,
  1383. NULL, NULL },
  1384. { "intro_circuit_opened", test_intro_circuit_opened, TT_FORK,
  1385. NULL, NULL },
  1386. { "intro_established", test_intro_established, TT_FORK,
  1387. NULL, NULL },
  1388. { "closing_intro_circs", test_closing_intro_circs, TT_FORK,
  1389. NULL, NULL },
  1390. { "rdv_circuit_opened", test_rdv_circuit_opened, TT_FORK,
  1391. NULL, NULL },
  1392. { "introduce2", test_introduce2, TT_FORK,
  1393. NULL, NULL },
  1394. { "service_event", test_service_event, TT_FORK,
  1395. NULL, NULL },
  1396. { "rotate_descriptors", test_rotate_descriptors, TT_FORK,
  1397. NULL, NULL },
  1398. { "build_update_descriptors", test_build_update_descriptors, TT_FORK,
  1399. NULL, NULL },
  1400. { "upload_descriptors", test_upload_descriptors, TT_FORK,
  1401. NULL, NULL },
  1402. { "revision_counter_state", test_revision_counter_state, TT_FORK,
  1403. NULL, NULL },
  1404. { "rendezvous1_parsing", test_rendezvous1_parsing, TT_FORK,
  1405. NULL, NULL },
  1406. END_OF_TESTCASES
  1407. };