test_hs_service.c 49 KB

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