test_hs_service.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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 subtract 500 seconds
  359. * because there could be a gap between setting now and the time taken in
  360. * service_intro_point_new. On ARM and other older CPUs, it can be
  361. * surprisingly slow... */
  362. tt_u64_op(ip->time_to_expire, OP_GE,
  363. now + INTRO_POINT_LIFETIME_MIN_SECONDS - 500);
  364. /* We add 500 seconds, because this time we're testing against the
  365. * maximum allowed time. */
  366. tt_u64_op(ip->time_to_expire, OP_LE,
  367. now + INTRO_POINT_LIFETIME_MAX_SECONDS + 500);
  368. tt_assert(ip->replay_cache);
  369. tt_assert(ip->base.link_specifiers);
  370. /* By default, this is NOT a legacy object. */
  371. tt_int_op(ip->base.is_only_legacy, OP_EQ, 0);
  372. }
  373. /* Test functions that uses a service intropoints map with that previously
  374. * created object (non legacy). */
  375. {
  376. ed25519_public_key_t garbage = { {0} };
  377. hs_service_intro_point_t *query;
  378. service = hs_service_new(get_options());
  379. tt_assert(service);
  380. service->desc_current = service_descriptor_new();
  381. tt_assert(service->desc_current);
  382. /* Add intropoint to descriptor map. */
  383. service_intro_point_add(service->desc_current->intro_points.map, ip);
  384. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  385. tt_mem_op(query, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  386. query = service_intro_point_find(service, &garbage);
  387. tt_ptr_op(query, OP_EQ, NULL);
  388. /* While at it, can I find the descriptor with the intro point? */
  389. hs_service_descriptor_t *desc_lookup =
  390. service_desc_find_by_intro(service, ip);
  391. tt_mem_op(service->desc_current, OP_EQ, desc_lookup,
  392. sizeof(hs_service_descriptor_t));
  393. /* Remove object from service descriptor and make sure it is out. */
  394. service_intro_point_remove(service, ip);
  395. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  396. tt_ptr_op(query, OP_EQ, NULL);
  397. }
  398. done:
  399. /* If the test succeed, this object is no longer referenced in the service
  400. * so we can free it without use after free. Else, it might explode because
  401. * it's still in the service descriptor map. */
  402. service_intro_point_free(ip);
  403. hs_service_free(service);
  404. }
  405. static node_t mock_node;
  406. static const node_t *
  407. mock_node_get_by_id(const char *digest)
  408. {
  409. (void) digest;
  410. memset(mock_node.identity, 'A', DIGEST_LEN);
  411. /* Only return the matchin identity of As */
  412. if (!tor_memcmp(mock_node.identity, digest, DIGEST_LEN)) {
  413. return &mock_node;
  414. }
  415. return NULL;
  416. }
  417. static void
  418. test_helper_functions(void *arg)
  419. {
  420. int ret;
  421. hs_service_t *service = NULL;
  422. hs_service_intro_point_t *ip = NULL;
  423. hs_ident_circuit_t ident;
  424. (void) arg;
  425. MOCK(node_get_by_id, mock_node_get_by_id);
  426. hs_service_init();
  427. service = helper_create_service();
  428. ip = helper_create_service_ip();
  429. /* Immediately add the intro point to the service so the free service at the
  430. * end cleans it as well. */
  431. service_intro_point_add(service->desc_current->intro_points.map, ip);
  432. /* Setup the circuit identifier. */
  433. ed25519_pubkey_copy(&ident.intro_auth_pk, &ip->auth_key_kp.pubkey);
  434. ed25519_pubkey_copy(&ident.identity_pk, &service->keys.identity_pk);
  435. /* Testing get_objects_from_ident(). */
  436. {
  437. hs_service_t *s_lookup = NULL;
  438. hs_service_intro_point_t *ip_lookup = NULL;
  439. hs_service_descriptor_t *desc_lookup = NULL;
  440. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  441. tt_mem_op(s_lookup, OP_EQ, service, sizeof(hs_service_t));
  442. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  443. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  444. sizeof(hs_service_descriptor_t));
  445. /* Reset */
  446. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  447. /* NULL parameter should work. */
  448. get_objects_from_ident(&ident, NULL, &ip_lookup, &desc_lookup);
  449. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  450. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  451. sizeof(hs_service_descriptor_t));
  452. /* Reset. */
  453. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  454. /* Break the ident and we should find nothing. */
  455. memset(&ident, 0, sizeof(ident));
  456. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  457. tt_ptr_op(s_lookup, OP_EQ, NULL);
  458. tt_ptr_op(ip_lookup, OP_EQ, NULL);
  459. tt_ptr_op(desc_lookup, OP_EQ, NULL);
  460. }
  461. /* Testing get_node_from_intro_point() */
  462. {
  463. const node_t *node = get_node_from_intro_point(ip);
  464. tt_ptr_op(node, OP_EQ, &mock_node);
  465. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  466. hs_desc_link_specifier_t *, ls) {
  467. if (ls->type == LS_LEGACY_ID) {
  468. /* Change legacy id in link specifier which is not the mock node. */
  469. memset(ls->u.legacy_id, 'B', sizeof(ls->u.legacy_id));
  470. }
  471. } SMARTLIST_FOREACH_END(ls);
  472. node = get_node_from_intro_point(ip);
  473. tt_ptr_op(node, OP_EQ, NULL);
  474. }
  475. /* Testing can_service_launch_intro_circuit() */
  476. {
  477. time_t now = time(NULL);
  478. /* Put the start of the retry period back in time, we should be allowed.
  479. * to launch intro circuit. */
  480. service->state.num_intro_circ_launched = 2;
  481. service->state.intro_circ_retry_started_time =
  482. (now - INTRO_CIRC_RETRY_PERIOD - 1);
  483. ret = can_service_launch_intro_circuit(service, now);
  484. tt_int_op(ret, OP_EQ, 1);
  485. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  486. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  487. /* Call it again, we should still be allowed because we are under
  488. * MAX_INTRO_CIRCS_PER_PERIOD which been set to 0 previously. */
  489. ret = can_service_launch_intro_circuit(service, now);
  490. tt_int_op(ret, OP_EQ, 1);
  491. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  492. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  493. /* Too many intro circuit launched means we are not allowed. */
  494. service->state.num_intro_circ_launched = 20;
  495. ret = can_service_launch_intro_circuit(service, now);
  496. tt_int_op(ret, OP_EQ, 0);
  497. }
  498. /* Testing intro_point_should_expire(). */
  499. {
  500. time_t now = time(NULL);
  501. /* Just some basic test of the current state. */
  502. tt_u64_op(ip->introduce2_max, OP_GE,
  503. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  504. tt_u64_op(ip->introduce2_max, OP_LE,
  505. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  506. tt_u64_op(ip->time_to_expire, OP_GE,
  507. now + INTRO_POINT_LIFETIME_MIN_SECONDS);
  508. tt_u64_op(ip->time_to_expire, OP_LE,
  509. now + INTRO_POINT_LIFETIME_MAX_SECONDS);
  510. /* This newly created IP from above shouldn't expire now. */
  511. ret = intro_point_should_expire(ip, now);
  512. tt_int_op(ret, OP_EQ, 0);
  513. /* Maximum number of INTRODUCE2 cell reached, it should expire. */
  514. ip->introduce2_count = INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS + 1;
  515. ret = intro_point_should_expire(ip, now);
  516. tt_int_op(ret, OP_EQ, 1);
  517. ip->introduce2_count = 0;
  518. /* It should expire if time to expire has been reached. */
  519. ip->time_to_expire = now - 1000;
  520. ret = intro_point_should_expire(ip, now);
  521. tt_int_op(ret, OP_EQ, 1);
  522. }
  523. done:
  524. /* This will free the service and all objects associated to it. */
  525. hs_service_free_all();
  526. UNMOCK(node_get_by_id);
  527. }
  528. /** Test that we do the right operations when an intro circuit opens */
  529. static void
  530. test_intro_circuit_opened(void *arg)
  531. {
  532. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  533. hs_service_t *service;
  534. origin_circuit_t *circ = NULL;
  535. (void) arg;
  536. hs_init();
  537. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  538. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  539. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  540. flags);
  541. /* No service associated with this circuit. */
  542. setup_full_capture_of_logs(LOG_WARN);
  543. hs_service_circuit_has_opened(circ);
  544. expect_log_msg_containing("Unknown service identity key");
  545. teardown_capture_of_logs();
  546. /* Set a service for this circuit. */
  547. {
  548. service = helper_create_service();
  549. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  550. &service->keys.identity_pk);
  551. /* No intro point associated with this circuit. */
  552. setup_full_capture_of_logs(LOG_WARN);
  553. hs_service_circuit_has_opened(circ);
  554. expect_log_msg_containing("Unknown introduction point auth key");
  555. teardown_capture_of_logs();
  556. }
  557. /* Set an IP object now for this circuit. */
  558. {
  559. hs_service_intro_point_t *ip = helper_create_service_ip();
  560. service_intro_point_add(service->desc_current->intro_points.map, ip);
  561. /* Update ident to contain the intro point auth key. */
  562. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  563. &ip->auth_key_kp.pubkey);
  564. }
  565. /* This one should go all the way. */
  566. setup_full_capture_of_logs(LOG_INFO);
  567. hs_service_circuit_has_opened(circ);
  568. expect_log_msg_containing("Introduction circuit 0 established for service");
  569. teardown_capture_of_logs();
  570. done:
  571. circuit_free(TO_CIRCUIT(circ));
  572. hs_free_all();
  573. UNMOCK(circuit_mark_for_close_);
  574. UNMOCK(relay_send_command_from_edge_);
  575. }
  576. /** Test the operations we do on a circuit after we learn that we successfuly
  577. * established an intro point on it */
  578. static void
  579. test_intro_established(void *arg)
  580. {
  581. int ret;
  582. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  583. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  584. origin_circuit_t *circ = NULL;
  585. hs_service_t *service;
  586. hs_service_intro_point_t *ip = NULL;
  587. (void) arg;
  588. hs_init();
  589. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  590. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  591. flags);
  592. tt_assert(circ);
  593. /* Test a wrong purpose. */
  594. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  595. setup_full_capture_of_logs(LOG_WARN);
  596. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  597. tt_int_op(ret, OP_EQ, -1);
  598. expect_log_msg_containing("Received an INTRO_ESTABLISHED cell on a "
  599. "non introduction circuit of purpose");
  600. teardown_capture_of_logs();
  601. /* Back to normal. */
  602. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  603. /* No service associated to it. */
  604. setup_full_capture_of_logs(LOG_WARN);
  605. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  606. tt_int_op(ret, OP_EQ, -1);
  607. expect_log_msg_containing("Unknown service identity key");
  608. teardown_capture_of_logs();
  609. /* Set a service for this circuit. */
  610. service = helper_create_service();
  611. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  612. &service->keys.identity_pk);
  613. /* No introduction point associated to it. */
  614. setup_full_capture_of_logs(LOG_WARN);
  615. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  616. tt_int_op(ret, OP_EQ, -1);
  617. expect_log_msg_containing("Introduction circuit established without an "
  618. "intro point object on circuit");
  619. teardown_capture_of_logs();
  620. /* Set an IP object now for this circuit. */
  621. {
  622. ip = helper_create_service_ip();
  623. service_intro_point_add(service->desc_current->intro_points.map, ip);
  624. /* Update ident to contain the intro point auth key. */
  625. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  626. &ip->auth_key_kp.pubkey);
  627. }
  628. /* Send an empty payload. INTRO_ESTABLISHED cells are basically zeroes. */
  629. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  630. tt_int_op(ret, OP_EQ, 0);
  631. tt_u64_op(ip->circuit_established, OP_EQ, 1);
  632. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_INTRO);
  633. done:
  634. if (circ)
  635. circuit_free(TO_CIRCUIT(circ));
  636. hs_free_all();
  637. UNMOCK(circuit_mark_for_close_);
  638. }
  639. /** Check the operations we do on a rendezvous circuit after we learn it's
  640. * open */
  641. static void
  642. test_rdv_circuit_opened(void *arg)
  643. {
  644. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  645. origin_circuit_t *circ = NULL;
  646. hs_service_t *service;
  647. (void) arg;
  648. hs_init();
  649. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  650. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  651. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND, flags);
  652. crypto_rand((char *) circ->hs_ident->rendezvous_cookie, REND_COOKIE_LEN);
  653. crypto_rand((char *) circ->hs_ident->rendezvous_handshake_info,
  654. sizeof(circ->hs_ident->rendezvous_handshake_info));
  655. /* No service associated with this circuit. */
  656. setup_full_capture_of_logs(LOG_WARN);
  657. hs_service_circuit_has_opened(circ);
  658. expect_log_msg_containing("Unknown service identity key");
  659. teardown_capture_of_logs();
  660. /* This should be set to a non zero timestamp. */
  661. tt_u64_op(TO_CIRCUIT(circ)->timestamp_dirty, OP_NE, 0);
  662. /* Set a service for this circuit. */
  663. service = helper_create_service();
  664. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  665. &service->keys.identity_pk);
  666. /* Should be all good. */
  667. hs_service_circuit_has_opened(circ);
  668. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  669. done:
  670. circuit_free(TO_CIRCUIT(circ));
  671. hs_free_all();
  672. UNMOCK(circuit_mark_for_close_);
  673. UNMOCK(relay_send_command_from_edge_);
  674. }
  675. static void
  676. mock_assert_circuit_ok(const circuit_t *c)
  677. {
  678. (void) c;
  679. return;
  680. }
  681. /** Test for the general mechanism for closing intro circs.
  682. * Also a way to identify that #23603 has been fixed. */
  683. static void
  684. test_closing_intro_circs(void *arg)
  685. {
  686. hs_service_t *service = NULL;
  687. hs_service_intro_point_t *ip = NULL, *entry = NULL;
  688. origin_circuit_t *intro_circ = NULL, *tmp_circ;
  689. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  690. (void) arg;
  691. MOCK(assert_circuit_ok, mock_assert_circuit_ok);
  692. hs_init();
  693. /* Initialize service */
  694. service = helper_create_service();
  695. /* Initialize intro point */
  696. ip = helper_create_service_ip();
  697. tt_assert(ip);
  698. service_intro_point_add(service->desc_current->intro_points.map, ip);
  699. /* Initialize intro circuit */
  700. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  701. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk,
  702. HS_IDENT_CIRCUIT_INTRO);
  703. /* Register circuit in the circuitmap . */
  704. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  705. &ip->auth_key_kp.pubkey);
  706. tmp_circ =
  707. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  708. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  709. /* Pretend that intro point has failed too much */
  710. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES+1;
  711. /* Now pretend we are freeing this intro circuit. We want to see that our
  712. * destructor is not gonna kill our intro point structure since that's the
  713. * job of the cleanup routine. */
  714. circuit_free(TO_CIRCUIT(intro_circ));
  715. intro_circ = NULL;
  716. entry = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  717. tt_assert(entry);
  718. /* The free should also remove the circuit from the circuitmap. */
  719. tmp_circ =
  720. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  721. tt_assert(!tmp_circ);
  722. /* Now pretend that a new intro point circ was launched and opened. Check
  723. * that the intro point will be established correctly. */
  724. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  725. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk,
  726. HS_IDENT_CIRCUIT_INTRO);
  727. ed25519_pubkey_copy(&intro_circ->hs_ident->intro_auth_pk,
  728. &ip->auth_key_kp.pubkey);
  729. /* Register circuit in the circuitmap . */
  730. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  731. &ip->auth_key_kp.pubkey);
  732. tmp_circ =
  733. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  734. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  735. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_EQ, 0);
  736. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  737. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_NE, 0);
  738. /* At this point, we should not be able to find it in the circuitmap. */
  739. tmp_circ =
  740. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  741. tt_assert(!tmp_circ);
  742. done:
  743. if (intro_circ) {
  744. circuit_free(TO_CIRCUIT(intro_circ));
  745. }
  746. /* Frees the service object. */
  747. hs_free_all();
  748. UNMOCK(assert_circuit_ok);
  749. }
  750. /** Test sending and receiving introduce2 cells */
  751. static void
  752. test_introduce2(void *arg)
  753. {
  754. int ret;
  755. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  756. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  757. origin_circuit_t *circ = NULL;
  758. hs_service_t *service;
  759. hs_service_intro_point_t *ip = NULL;
  760. (void) arg;
  761. hs_init();
  762. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  763. MOCK(get_or_state,
  764. get_or_state_replacement);
  765. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  766. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  767. tt_assert(circ);
  768. /* Test a wrong purpose. */
  769. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  770. setup_full_capture_of_logs(LOG_WARN);
  771. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  772. tt_int_op(ret, OP_EQ, -1);
  773. expect_log_msg_containing("Received an INTRODUCE2 cell on a "
  774. "non introduction circuit of purpose");
  775. teardown_capture_of_logs();
  776. /* Back to normal. */
  777. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  778. /* No service associated to it. */
  779. setup_full_capture_of_logs(LOG_WARN);
  780. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  781. tt_int_op(ret, OP_EQ, -1);
  782. expect_log_msg_containing("Unknown service identity key");
  783. teardown_capture_of_logs();
  784. /* Set a service for this circuit. */
  785. service = helper_create_service();
  786. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  787. &service->keys.identity_pk);
  788. /* No introduction point associated to it. */
  789. setup_full_capture_of_logs(LOG_WARN);
  790. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  791. tt_int_op(ret, OP_EQ, -1);
  792. expect_log_msg_containing("Unknown introduction auth key when handling "
  793. "an INTRODUCE2 cell on circuit");
  794. teardown_capture_of_logs();
  795. /* Set an IP object now for this circuit. */
  796. {
  797. ip = helper_create_service_ip();
  798. service_intro_point_add(service->desc_current->intro_points.map, ip);
  799. /* Update ident to contain the intro point auth key. */
  800. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  801. &ip->auth_key_kp.pubkey);
  802. }
  803. /* This will fail because receiving an INTRODUCE2 cell implies a valid cell
  804. * and then launching circuits so let's not do that and instead test that
  805. * behaviour differently. */
  806. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  807. tt_int_op(ret, OP_EQ, -1);
  808. tt_u64_op(ip->introduce2_count, OP_EQ, 0);
  809. done:
  810. or_state_free(dummy_state);
  811. dummy_state = NULL;
  812. if (circ)
  813. circuit_free(TO_CIRCUIT(circ));
  814. hs_free_all();
  815. UNMOCK(circuit_mark_for_close_);
  816. }
  817. /** Test basic hidden service housekeeping operations (maintaining intro
  818. * points, etc) */
  819. static void
  820. test_service_event(void *arg)
  821. {
  822. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  823. time_t now = time(NULL);
  824. hs_service_t *service;
  825. origin_circuit_t *circ = NULL;
  826. (void) arg;
  827. hs_init();
  828. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  829. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  830. /* Set a service for this circuit. */
  831. service = helper_create_service();
  832. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  833. &service->keys.identity_pk);
  834. /* Currently this consists of cleaning invalid intro points. So adding IPs
  835. * here that should get cleaned up. */
  836. {
  837. hs_service_intro_point_t *ip = helper_create_service_ip();
  838. service_intro_point_add(service->desc_current->intro_points.map, ip);
  839. /* This run will remove the IP because we have no circuits nor node_t
  840. * associated with it. */
  841. run_housekeeping_event(now);
  842. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  843. OP_EQ, 0);
  844. /* We'll trigger a removal because we've reached our maximum amount of
  845. * times we should retry a circuit. For this, we need to have a node_t
  846. * that matches the identity of this IP. */
  847. routerinfo_t ri;
  848. memset(&ri, 0, sizeof(ri));
  849. ip = helper_create_service_ip();
  850. service_intro_point_add(service->desc_current->intro_points.map, ip);
  851. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  852. /* This triggers a node_t creation. */
  853. tt_assert(nodelist_set_routerinfo(&ri, NULL));
  854. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  855. run_housekeeping_event(now);
  856. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  857. OP_EQ, 0);
  858. /* No removal but no circuit so this means the IP object will stay in the
  859. * descriptor map so we can retry it. */
  860. ip = helper_create_service_ip();
  861. service_intro_point_add(service->desc_current->intro_points.map, ip);
  862. ip->circuit_established = 1; /* We'll test that, it MUST be 0 after. */
  863. run_housekeeping_event(now);
  864. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  865. OP_EQ, 1);
  866. /* Remove the IP object at once for the next test. */
  867. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  868. run_housekeeping_event(now);
  869. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  870. OP_EQ, 0);
  871. /* Now, we'll create an IP with a registered circuit. The IP object
  872. * shouldn't go away. */
  873. ip = helper_create_service_ip();
  874. service_intro_point_add(service->desc_current->intro_points.map, ip);
  875. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  876. &ip->auth_key_kp.pubkey);
  877. hs_circuitmap_register_intro_circ_v3_service_side(
  878. circ, &ip->auth_key_kp.pubkey);
  879. run_housekeeping_event(now);
  880. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  881. OP_EQ, 1);
  882. /* We'll mangle the IP object to expire. */
  883. ip->time_to_expire = now;
  884. run_housekeeping_event(now);
  885. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  886. OP_EQ, 0);
  887. }
  888. done:
  889. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  890. circuit_free(TO_CIRCUIT(circ));
  891. hs_free_all();
  892. UNMOCK(circuit_mark_for_close_);
  893. }
  894. /** Test that we rotate descriptors correctly. */
  895. static void
  896. test_rotate_descriptors(void *arg)
  897. {
  898. int ret;
  899. time_t next_rotation_time, now = time(NULL);
  900. hs_service_t *service;
  901. hs_service_descriptor_t *desc_next;
  902. (void) arg;
  903. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  904. hs_init();
  905. MOCK(get_or_state, get_or_state_replacement);
  906. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  907. MOCK(networkstatus_get_live_consensus,
  908. mock_networkstatus_get_live_consensus);
  909. /* Descriptor rotation happens with a consensus with a new SRV. */
  910. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  911. &mock_ns.valid_after);
  912. tt_int_op(ret, OP_EQ, 0);
  913. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  914. &mock_ns.fresh_until);
  915. tt_int_op(ret, OP_EQ, 0);
  916. dirvote_recalculate_timing(get_options(), mock_ns.valid_after);
  917. /* Create a service with a default descriptor and state. It's added to the
  918. * global map. */
  919. service = helper_create_service();
  920. service_descriptor_free(service->desc_current);
  921. service->desc_current = NULL;
  922. /* This triggers a build for both descriptors. The time now is only used in
  923. * the descriptor certificate which is important to be now else the decoding
  924. * will complain that the cert has expired if we use valid_after. */
  925. build_all_descriptors(now);
  926. tt_assert(service->desc_current);
  927. tt_assert(service->desc_next);
  928. /* Tweak our service next rotation time so we can use a custom time. */
  929. service->state.next_rotation_time = next_rotation_time =
  930. mock_ns.valid_after + (11 * 60 * 60);
  931. /* Nothing should happen, we are not at a new SRV. Our next rotation time
  932. * should be untouched. */
  933. rotate_all_descriptors(mock_ns.valid_after);
  934. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  935. tt_assert(service->desc_current);
  936. tt_assert(service->desc_next);
  937. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  938. hs_get_previous_time_period_num(0));
  939. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  940. hs_get_time_period_num(0));
  941. /* Keep a reference so we can compare it after rotation to the current. */
  942. desc_next = service->desc_next;
  943. /* Going right after a new SRV. */
  944. ret = parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC",
  945. &mock_ns.valid_after);
  946. tt_int_op(ret, OP_EQ, 0);
  947. ret = parse_rfc1123_time("Sat, 27 Oct 1985 02:00:00 UTC",
  948. &mock_ns.fresh_until);
  949. tt_int_op(ret, OP_EQ, 0);
  950. dirvote_recalculate_timing(get_options(), mock_ns.valid_after);
  951. /* Note down what to expect for the next rotation time which is 01:00 + 23h
  952. * meaning 00:00:00. */
  953. next_rotation_time = mock_ns.valid_after + (23 * 60 * 60);
  954. /* We should have our next rotation time modified, our current descriptor
  955. * cleaned up and the next descriptor becoming the current. */
  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. /* A second time should do nothing. */
  961. rotate_all_descriptors(mock_ns.valid_after);
  962. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  963. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  964. tt_assert(service->desc_next == NULL);
  965. build_all_descriptors(now);
  966. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  967. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  968. hs_get_time_period_num(0));
  969. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  970. hs_get_next_time_period_num(0));
  971. tt_assert(service->desc_next);
  972. done:
  973. hs_free_all();
  974. UNMOCK(get_or_state);
  975. UNMOCK(circuit_mark_for_close_);
  976. UNMOCK(networkstatus_get_live_consensus);
  977. }
  978. /** Test building descriptors: picking intro points, setting up their link
  979. * specifiers, etc. */
  980. static void
  981. test_build_update_descriptors(void *arg)
  982. {
  983. int ret;
  984. time_t now = time(NULL);
  985. node_t *node;
  986. hs_service_t *service;
  987. hs_service_intro_point_t *ip_cur, *ip_next;
  988. routerinfo_t ri;
  989. (void) arg;
  990. hs_init();
  991. MOCK(get_or_state,
  992. get_or_state_replacement);
  993. MOCK(networkstatus_get_live_consensus,
  994. mock_networkstatus_get_live_consensus);
  995. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  996. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  997. &mock_ns.valid_after);
  998. tt_int_op(ret, OP_EQ, 0);
  999. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  1000. &mock_ns.fresh_until);
  1001. tt_int_op(ret, OP_EQ, 0);
  1002. dirvote_recalculate_timing(get_options(), mock_ns.valid_after);
  1003. /* Create a service without a current descriptor to trigger a build. */
  1004. service = helper_create_service();
  1005. tt_assert(service);
  1006. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1007. service_descriptor_free(service->desc_current);
  1008. service->desc_current = NULL;
  1009. /* We have a fresh service so this should trigger a build for both
  1010. * descriptors for specific time period that we'll test. */
  1011. build_all_descriptors(now);
  1012. /* Check *current* descriptor. */
  1013. tt_assert(service->desc_current);
  1014. tt_assert(service->desc_current->desc);
  1015. tt_assert(service->desc_current->intro_points.map);
  1016. /* The current time period is the one expected when starting at 03:00. */
  1017. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1018. hs_get_time_period_num(0));
  1019. /* This should be untouched, the update descriptor process changes it. */
  1020. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1021. /* Check *next* descriptor. */
  1022. tt_assert(service->desc_next);
  1023. tt_assert(service->desc_next->desc);
  1024. tt_assert(service->desc_next->intro_points.map);
  1025. tt_assert(service->desc_current != service->desc_next);
  1026. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1027. hs_get_next_time_period_num(0));
  1028. /* This should be untouched, the update descriptor process changes it. */
  1029. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1030. /* Time to test the update of those descriptors. At first, we have no node
  1031. * in the routerlist so this will find NO suitable node for the IPs. */
  1032. setup_full_capture_of_logs(LOG_INFO);
  1033. update_all_descriptors(now);
  1034. expect_log_msg_containing("Unable to find a suitable node to be an "
  1035. "introduction point for service");
  1036. teardown_capture_of_logs();
  1037. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1038. OP_EQ, 0);
  1039. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1040. OP_EQ, 0);
  1041. /* Now, we'll setup a node_t. */
  1042. {
  1043. tor_addr_t ipv4_addr;
  1044. curve25519_secret_key_t curve25519_secret_key;
  1045. memset(&ri, 0, sizeof(routerinfo_t));
  1046. tor_addr_parse(&ipv4_addr, "127.0.0.1");
  1047. ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  1048. ri.or_port = 1337;
  1049. ri.purpose = ROUTER_PURPOSE_GENERAL;
  1050. /* Ugly yes but we never free the "ri" object so this just makes things
  1051. * easier. */
  1052. ri.protocol_list = (char *) "HSDir=1-2 LinkAuth=3";
  1053. ret = curve25519_secret_key_generate(&curve25519_secret_key, 0);
  1054. tt_int_op(ret, OP_EQ, 0);
  1055. ri.onion_curve25519_pkey =
  1056. tor_malloc_zero(sizeof(curve25519_public_key_t));
  1057. ri.onion_pkey = crypto_pk_new();
  1058. curve25519_public_key_generate(ri.onion_curve25519_pkey,
  1059. &curve25519_secret_key);
  1060. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1061. /* Setup ed25519 identity */
  1062. ed25519_keypair_t kp1;
  1063. ed25519_keypair_generate(&kp1, 0);
  1064. ri.cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  1065. tt_assert(ri.cache_info.signing_key_cert);
  1066. ed25519_pubkey_copy(&ri.cache_info.signing_key_cert->signing_key,
  1067. &kp1.pubkey);
  1068. nodelist_set_routerinfo(&ri, NULL);
  1069. node = node_get_mutable_by_id(ri.cache_info.identity_digest);
  1070. tt_assert(node);
  1071. node->is_running = node->is_valid = node->is_fast = node->is_stable = 1;
  1072. }
  1073. /* We expect to pick only one intro point from the node above. */
  1074. setup_full_capture_of_logs(LOG_INFO);
  1075. update_all_descriptors(now);
  1076. tor_free(node->ri->onion_curve25519_pkey); /* Avoid memleak. */
  1077. tor_free(node->ri->cache_info.signing_key_cert);
  1078. crypto_pk_free(node->ri->onion_pkey);
  1079. expect_log_msg_containing("just picked 1 intro points and wanted 3 for next "
  1080. "descriptor. It currently has 0 intro points. "
  1081. "Launching ESTABLISH_INTRO circuit shortly.");
  1082. teardown_capture_of_logs();
  1083. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1084. OP_EQ, 1);
  1085. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1086. OP_EQ, 1);
  1087. /* Get the IP object. Because we don't have the auth key of the IP, we can't
  1088. * query it so get the first element in the map. */
  1089. {
  1090. void *obj = NULL;
  1091. const uint8_t *key;
  1092. digest256map_iter_t *iter =
  1093. digest256map_iter_init(service->desc_current->intro_points.map);
  1094. digest256map_iter_get(iter, &key, &obj);
  1095. tt_assert(obj);
  1096. ip_cur = obj;
  1097. /* Get also the IP from the next descriptor. We'll make sure it's not the
  1098. * same object as in the current descriptor. */
  1099. iter = digest256map_iter_init(service->desc_next->intro_points.map);
  1100. digest256map_iter_get(iter, &key, &obj);
  1101. tt_assert(obj);
  1102. ip_next = obj;
  1103. }
  1104. tt_mem_op(ip_cur, OP_NE, ip_next, sizeof(hs_desc_intro_point_t));
  1105. /* We won't test the service IP object because there is a specific test
  1106. * already for this but we'll make sure that the state is coherent.*/
  1107. /* Three link specifiers are mandatoy so make sure we do have them. */
  1108. tt_int_op(smartlist_len(ip_cur->base.link_specifiers), OP_EQ, 3);
  1109. /* Make sure we have a valid encryption keypair generated when we pick an
  1110. * intro point in the update process. */
  1111. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.seckey.secret_key,
  1112. CURVE25519_SECKEY_LEN));
  1113. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.pubkey.public_key,
  1114. CURVE25519_PUBKEY_LEN));
  1115. tt_u64_op(ip_cur->time_to_expire, OP_GE, now +
  1116. INTRO_POINT_LIFETIME_MIN_SECONDS);
  1117. tt_u64_op(ip_cur->time_to_expire, OP_LE, now +
  1118. INTRO_POINT_LIFETIME_MAX_SECONDS);
  1119. /* Now, we will try to set up a service after a new time period has started
  1120. * and see if it behaves as expected. */
  1121. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1122. &mock_ns.valid_after);
  1123. tt_int_op(ret, OP_EQ, 0);
  1124. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1125. &mock_ns.fresh_until);
  1126. tt_int_op(ret, OP_EQ, 0);
  1127. /* Create a service without a current descriptor to trigger a build. */
  1128. service = helper_create_service();
  1129. tt_assert(service);
  1130. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1131. service_descriptor_free(service->desc_current);
  1132. service->desc_current = NULL;
  1133. /* We have a fresh service so this should trigger a build for both
  1134. * descriptors for specific time period that we'll test. */
  1135. build_all_descriptors(now);
  1136. /* Check *current* descriptor. */
  1137. tt_assert(service->desc_current);
  1138. tt_assert(service->desc_current->desc);
  1139. tt_assert(service->desc_current->intro_points.map);
  1140. /* This should be for the previous time period. */
  1141. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1142. hs_get_previous_time_period_num(0));
  1143. /* This should be untouched, the update descriptor process changes it. */
  1144. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1145. /* Check *next* descriptor. */
  1146. tt_assert(service->desc_next);
  1147. tt_assert(service->desc_next->desc);
  1148. tt_assert(service->desc_next->intro_points.map);
  1149. tt_assert(service->desc_current != service->desc_next);
  1150. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1151. hs_get_time_period_num(0));
  1152. /* This should be untouched, the update descriptor process changes it. */
  1153. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1154. /* Let's remove the next descriptor to simulate a rotation. */
  1155. service_descriptor_free(service->desc_next);
  1156. service->desc_next = NULL;
  1157. build_all_descriptors(now);
  1158. /* Check *next* descriptor. */
  1159. tt_assert(service->desc_next);
  1160. tt_assert(service->desc_next->desc);
  1161. tt_assert(service->desc_next->intro_points.map);
  1162. tt_assert(service->desc_current != service->desc_next);
  1163. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1164. hs_get_next_time_period_num(0));
  1165. /* This should be untouched, the update descriptor process changes it. */
  1166. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1167. done:
  1168. hs_free_all();
  1169. nodelist_free_all();
  1170. }
  1171. static void
  1172. test_upload_descriptors(void *arg)
  1173. {
  1174. int ret;
  1175. time_t now = time(NULL);
  1176. hs_service_t *service;
  1177. (void) arg;
  1178. hs_init();
  1179. MOCK(get_or_state,
  1180. get_or_state_replacement);
  1181. MOCK(networkstatus_get_live_consensus,
  1182. mock_networkstatus_get_live_consensus);
  1183. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1184. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1185. &mock_ns.valid_after);
  1186. tt_int_op(ret, OP_EQ, 0);
  1187. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1188. &mock_ns.fresh_until);
  1189. tt_int_op(ret, OP_EQ, 0);
  1190. /* Create a service with no descriptor. It's added to the global map. */
  1191. service = hs_service_new(get_options());
  1192. tt_assert(service);
  1193. service->config.version = HS_VERSION_THREE;
  1194. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1195. ed25519_public_key_generate(&service->keys.identity_pk,
  1196. &service->keys.identity_sk);
  1197. /* Register service to global map. */
  1198. ret = register_service(get_hs_service_map(), service);
  1199. tt_int_op(ret, OP_EQ, 0);
  1200. /* But first, build our descriptor. */
  1201. build_all_descriptors(now);
  1202. /* Nothing should happen because we have 0 introduction circuit established
  1203. * and we want (by default) 3 intro points. */
  1204. run_upload_descriptor_event(now);
  1205. /* If no upload happened, this should be untouched. */
  1206. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1207. /* We'll simulate that we've opened our intro point circuit and that we only
  1208. * want one intro point. */
  1209. service->config.num_intro_points = 1;
  1210. /* Set our next upload time after now which will skip the upload. */
  1211. service->desc_current->next_upload_time = now + 1000;
  1212. run_upload_descriptor_event(now);
  1213. /* If no upload happened, this should be untouched. */
  1214. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, now + 1000);
  1215. done:
  1216. hs_free_all();
  1217. UNMOCK(get_or_state);
  1218. }
  1219. /** Test the functions that save and load HS revision counters to state. */
  1220. static void
  1221. test_revision_counter_state(void *arg)
  1222. {
  1223. char *state_line_one = NULL;
  1224. char *state_line_two = NULL;
  1225. hs_service_descriptor_t *desc_one = service_descriptor_new();
  1226. hs_service_descriptor_t *desc_two = service_descriptor_new();
  1227. (void) arg;
  1228. /* Prepare both descriptors */
  1229. desc_one->desc->plaintext_data.revision_counter = 42;
  1230. desc_two->desc->plaintext_data.revision_counter = 240;
  1231. memset(&desc_one->blinded_kp.pubkey.pubkey, 66,
  1232. sizeof(desc_one->blinded_kp.pubkey.pubkey));
  1233. memset(&desc_two->blinded_kp.pubkey.pubkey, 240,
  1234. sizeof(desc_one->blinded_kp.pubkey.pubkey));
  1235. /* Turn the descriptor rev counters into state lines */
  1236. state_line_one = encode_desc_rev_counter_for_state(desc_one);
  1237. tt_str_op(state_line_one, OP_EQ,
  1238. "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkI 42");
  1239. state_line_two = encode_desc_rev_counter_for_state(desc_two);
  1240. tt_str_op(state_line_two, OP_EQ,
  1241. "8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PA 240");
  1242. /* Now let's test our state parsing function: */
  1243. int service_found;
  1244. uint64_t cached_rev_counter;
  1245. /* First's try with wrong pubkey and check that no service was found */
  1246. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_one,
  1247. &desc_two->blinded_kp.pubkey,
  1248. &service_found);
  1249. tt_int_op(service_found, OP_EQ, 0);
  1250. tt_u64_op(cached_rev_counter, OP_EQ, 0);
  1251. /* Now let's try with the right pubkeys */
  1252. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_one,
  1253. &desc_one->blinded_kp.pubkey,
  1254. &service_found);
  1255. tt_int_op(service_found, OP_EQ, 1);
  1256. tt_u64_op(cached_rev_counter, OP_EQ, 42);
  1257. cached_rev_counter =check_state_line_for_service_rev_counter(state_line_two,
  1258. &desc_two->blinded_kp.pubkey,
  1259. &service_found);
  1260. tt_int_op(service_found, OP_EQ, 1);
  1261. tt_u64_op(cached_rev_counter, OP_EQ, 240);
  1262. done:
  1263. tor_free(state_line_one);
  1264. tor_free(state_line_two);
  1265. service_descriptor_free(desc_one);
  1266. service_descriptor_free(desc_two);
  1267. }
  1268. /** Global vars used by test_rendezvous1_parsing() */
  1269. static char rend1_payload[RELAY_PAYLOAD_SIZE];
  1270. static size_t rend1_payload_len = 0;
  1271. /** Mock for relay_send_command_from_edge() to send a RENDEZVOUS1 cell. Instead
  1272. * of sending it to the network, instead save it to the global `rend1_payload`
  1273. * variable so that we can inspect it in the test_rendezvous1_parsing()
  1274. * test. */
  1275. static int
  1276. mock_relay_send_rendezvous1(streamid_t stream_id, circuit_t *circ,
  1277. uint8_t relay_command, const char *payload,
  1278. size_t payload_len,
  1279. crypt_path_t *cpath_layer,
  1280. const char *filename, int lineno)
  1281. {
  1282. (void) stream_id;
  1283. (void) circ;
  1284. (void) relay_command;
  1285. (void) cpath_layer;
  1286. (void) filename;
  1287. (void) lineno;
  1288. memcpy(rend1_payload, payload, payload_len);
  1289. rend1_payload_len = payload_len;
  1290. return 0;
  1291. }
  1292. /** Send a RENDEZVOUS1 as a service, and parse it as a client. */
  1293. static void
  1294. test_rendezvous1_parsing(void *arg)
  1295. {
  1296. int retval;
  1297. static const char *test_addr =
  1298. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad.onion";
  1299. hs_service_t *service = NULL;
  1300. origin_circuit_t *service_circ = NULL;
  1301. origin_circuit_t *client_circ = NULL;
  1302. ed25519_keypair_t ip_auth_kp;
  1303. curve25519_keypair_t ephemeral_kp;
  1304. curve25519_keypair_t client_kp;
  1305. curve25519_keypair_t ip_enc_kp;
  1306. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1307. (void) arg;
  1308. MOCK(relay_send_command_from_edge_, mock_relay_send_rendezvous1);
  1309. {
  1310. /* Let's start by setting up the service that will start the rend */
  1311. service = tor_malloc_zero(sizeof(hs_service_t));
  1312. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1313. ed25519_public_key_generate(&service->keys.identity_pk,
  1314. &service->keys.identity_sk);
  1315. memcpy(service->onion_address, test_addr, sizeof(service->onion_address));
  1316. tt_assert(service);
  1317. }
  1318. {
  1319. /* Now let's set up the service rendezvous circuit and its keys. */
  1320. service_circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND,
  1321. flags);
  1322. tor_free(service_circ->hs_ident);
  1323. hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys;
  1324. uint8_t rendezvous_cookie[HS_REND_COOKIE_LEN];
  1325. curve25519_keypair_generate(&ip_enc_kp, 0);
  1326. curve25519_keypair_generate(&ephemeral_kp, 0);
  1327. curve25519_keypair_generate(&client_kp, 0);
  1328. ed25519_keypair_generate(&ip_auth_kp, 0);
  1329. retval = hs_ntor_service_get_rendezvous1_keys(&ip_auth_kp.pubkey,
  1330. &ip_enc_kp,
  1331. &ephemeral_kp,
  1332. &client_kp.pubkey,
  1333. &hs_ntor_rend_cell_keys);
  1334. tt_int_op(retval, OP_EQ, 0);
  1335. memset(rendezvous_cookie, 2, sizeof(rendezvous_cookie));
  1336. service_circ->hs_ident =
  1337. create_rp_circuit_identifier(service, rendezvous_cookie,
  1338. &ephemeral_kp.pubkey,
  1339. &hs_ntor_rend_cell_keys);
  1340. }
  1341. /* Send out the RENDEZVOUS1 and make sure that our mock func worked */
  1342. tt_assert(tor_mem_is_zero(rend1_payload, 32));
  1343. hs_circ_service_rp_has_opened(service, service_circ);
  1344. tt_assert(!tor_mem_is_zero(rend1_payload, 32));
  1345. tt_int_op(rend1_payload_len, OP_EQ, HS_LEGACY_RENDEZVOUS_CELL_SIZE);
  1346. /******************************/
  1347. /** Now let's create the client rendezvous circuit */
  1348. client_circ =
  1349. helper_create_origin_circuit(CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED,
  1350. flags);
  1351. /* fix up its circ ident */
  1352. ed25519_pubkey_copy(&client_circ->hs_ident->intro_auth_pk,
  1353. &ip_auth_kp.pubkey);
  1354. memcpy(&client_circ->hs_ident->rendezvous_client_kp,
  1355. &client_kp, sizeof(client_circ->hs_ident->rendezvous_client_kp));
  1356. memcpy(&client_circ->hs_ident->intro_enc_pk.public_key,
  1357. &ip_enc_kp.pubkey.public_key,
  1358. sizeof(client_circ->hs_ident->intro_enc_pk.public_key));
  1359. /* Now parse the rendezvous2 circuit and make sure it was fine. We are
  1360. * skipping 20 bytes off its payload, since that's the rendezvous cookie
  1361. * which is only present in REND1. */
  1362. retval = handle_rendezvous2(client_circ,
  1363. (uint8_t*)rend1_payload+20,
  1364. rend1_payload_len-20);
  1365. tt_int_op(retval, OP_EQ, 0);
  1366. /* TODO: We are only simulating client/service here. We could also simulate
  1367. * the rendezvous point by plugging in rend_mid_establish_rendezvous(). We
  1368. * would need an extra circuit and some more stuff but it's doable. */
  1369. done:
  1370. circuit_free(TO_CIRCUIT(service_circ));
  1371. circuit_free(TO_CIRCUIT(client_circ));
  1372. hs_service_free(service);
  1373. hs_free_all();
  1374. UNMOCK(relay_send_command_from_edge_);
  1375. }
  1376. struct testcase_t hs_service_tests[] = {
  1377. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  1378. NULL, NULL },
  1379. { "load_keys", test_load_keys, TT_FORK,
  1380. NULL, NULL },
  1381. { "access_service", test_access_service, TT_FORK,
  1382. NULL, NULL },
  1383. { "service_intro_point", test_service_intro_point, TT_FORK,
  1384. NULL, NULL },
  1385. { "helper_functions", test_helper_functions, TT_FORK,
  1386. NULL, NULL },
  1387. { "intro_circuit_opened", test_intro_circuit_opened, TT_FORK,
  1388. NULL, NULL },
  1389. { "intro_established", test_intro_established, TT_FORK,
  1390. NULL, NULL },
  1391. { "closing_intro_circs", test_closing_intro_circs, TT_FORK,
  1392. NULL, NULL },
  1393. { "rdv_circuit_opened", test_rdv_circuit_opened, TT_FORK,
  1394. NULL, NULL },
  1395. { "introduce2", test_introduce2, TT_FORK,
  1396. NULL, NULL },
  1397. { "service_event", test_service_event, TT_FORK,
  1398. NULL, NULL },
  1399. { "rotate_descriptors", test_rotate_descriptors, TT_FORK,
  1400. NULL, NULL },
  1401. { "build_update_descriptors", test_build_update_descriptors, TT_FORK,
  1402. NULL, NULL },
  1403. { "upload_descriptors", test_upload_descriptors, TT_FORK,
  1404. NULL, NULL },
  1405. { "revision_counter_state", test_revision_counter_state, TT_FORK,
  1406. NULL, NULL },
  1407. { "rendezvous1_parsing", test_rendezvous1_parsing, TT_FORK,
  1408. NULL, NULL },
  1409. END_OF_TESTCASES
  1410. };