test_hs_service.c 46 KB

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