hs_common.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_common.c
  5. * \brief Contains code shared between different HS protocol version as well
  6. * as useful data structures and accessors used by other subsystems.
  7. * The rendcommon.c should only contains code relating to the v2
  8. * protocol.
  9. **/
  10. #define HS_COMMON_PRIVATE
  11. #include "or.h"
  12. #include "config.h"
  13. #include "networkstatus.h"
  14. #include "nodelist.h"
  15. #include "hs_cache.h"
  16. #include "hs_common.h"
  17. #include "hs_service.h"
  18. #include "rendcommon.h"
  19. #include "rendservice.h"
  20. #include "router.h"
  21. #include "shared_random.h"
  22. #include "shared_random_state.h"
  23. /* Ed25519 Basepoint value. Taken from section 5 of
  24. * https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-03 */
  25. static const char *str_ed25519_basepoint =
  26. "(15112221349535400772501151409588531511"
  27. "454012693041857206046113283949847762202, "
  28. "463168356949264781694283940034751631413"
  29. "07993866256225615783033603165251855960)";
  30. #ifdef HAVE_SYS_UN_H
  31. /** Given <b>ports</b>, a smarlist containing rend_service_port_config_t,
  32. * add the given <b>p</b>, a AF_UNIX port to the list. Return 0 on success
  33. * else return -ENOSYS if AF_UNIX is not supported (see function in the
  34. * #else statement below). */
  35. static int
  36. add_unix_port(smartlist_t *ports, rend_service_port_config_t *p)
  37. {
  38. tor_assert(ports);
  39. tor_assert(p);
  40. tor_assert(p->is_unix_addr);
  41. smartlist_add(ports, p);
  42. return 0;
  43. }
  44. /** Given <b>conn</b> set it to use the given port <b>p</b> values. Return 0
  45. * on success else return -ENOSYS if AF_UNIX is not supported (see function
  46. * in the #else statement below). */
  47. static int
  48. set_unix_port(edge_connection_t *conn, rend_service_port_config_t *p)
  49. {
  50. tor_assert(conn);
  51. tor_assert(p);
  52. tor_assert(p->is_unix_addr);
  53. conn->base_.socket_family = AF_UNIX;
  54. tor_addr_make_unspec(&conn->base_.addr);
  55. conn->base_.port = 1;
  56. conn->base_.address = tor_strdup(p->unix_addr);
  57. return 0;
  58. }
  59. #else /* defined(HAVE_SYS_UN_H) */
  60. static int
  61. set_unix_port(edge_connection_t *conn, rend_service_port_config_t *p)
  62. {
  63. (void) conn;
  64. (void) p;
  65. return -ENOSYS;
  66. }
  67. static int
  68. add_unix_port(smartlist_t *ports, rend_service_port_config_t *p)
  69. {
  70. (void) ports;
  71. (void) p;
  72. return -ENOSYS;
  73. }
  74. #endif /* HAVE_SYS_UN_H */
  75. /* Helper function: The key is a digest that we compare to a node_t object
  76. * current hsdir_index. */
  77. static int
  78. compare_digest_to_current_hsdir_index(const void *_key, const void **_member)
  79. {
  80. const char *key = _key;
  81. const node_t *node = *_member;
  82. return tor_memcmp(key, node->hsdir_index->current, DIGEST256_LEN);
  83. }
  84. /* Helper function: The key is a digest that we compare to a node_t object
  85. * next hsdir_index. */
  86. static int
  87. compare_digest_to_next_hsdir_index(const void *_key, const void **_member)
  88. {
  89. const char *key = _key;
  90. const node_t *node = *_member;
  91. return tor_memcmp(key, node->hsdir_index->next, DIGEST256_LEN);
  92. }
  93. /* Helper function: Compare two node_t objects current hsdir_index. */
  94. static int
  95. compare_node_current_hsdir_index(const void **a, const void **b)
  96. {
  97. const node_t *node1= *a;
  98. const node_t *node2 = *b;
  99. return tor_memcmp(node1->hsdir_index->current,
  100. node2->hsdir_index->current,
  101. DIGEST256_LEN);
  102. }
  103. /* Helper function: Compare two node_t objects next hsdir_index. */
  104. static int
  105. compare_node_next_hsdir_index(const void **a, const void **b)
  106. {
  107. const node_t *node1= *a;
  108. const node_t *node2 = *b;
  109. return tor_memcmp(node1->hsdir_index->next,
  110. node2->hsdir_index->next,
  111. DIGEST256_LEN);
  112. }
  113. /* Allocate and return a string containing the path to filename in directory.
  114. * This function will never return NULL. The caller must free this path. */
  115. char *
  116. hs_path_from_filename(const char *directory, const char *filename)
  117. {
  118. char *file_path = NULL;
  119. tor_assert(directory);
  120. tor_assert(filename);
  121. tor_asprintf(&file_path, "%s%s%s", directory, PATH_SEPARATOR, filename);
  122. return file_path;
  123. }
  124. /* Make sure that the directory for <b>service</b> is private, using the config
  125. * <b>username</b>.
  126. * If <b>create</b> is true:
  127. * - if the directory exists, change permissions if needed,
  128. * - if the directory does not exist, create it with the correct permissions.
  129. * If <b>create</b> is false:
  130. * - if the directory exists, check permissions,
  131. * - if the directory does not exist, check if we think we can create it.
  132. * Return 0 on success, -1 on failure. */
  133. int
  134. hs_check_service_private_dir(const char *username, const char *path,
  135. unsigned int dir_group_readable,
  136. unsigned int create)
  137. {
  138. cpd_check_t check_opts = CPD_NONE;
  139. tor_assert(path);
  140. if (create) {
  141. check_opts |= CPD_CREATE;
  142. } else {
  143. check_opts |= CPD_CHECK_MODE_ONLY;
  144. check_opts |= CPD_CHECK;
  145. }
  146. if (dir_group_readable) {
  147. check_opts |= CPD_GROUP_READ;
  148. }
  149. /* Check/create directory */
  150. if (check_private_dir(path, check_opts, username) < 0) {
  151. return -1;
  152. }
  153. return 0;
  154. }
  155. /** Get the default HS time period length in minutes from the consensus. */
  156. STATIC uint64_t
  157. get_time_period_length(void)
  158. {
  159. int32_t time_period_length = networkstatus_get_param(NULL, "hsdir-interval",
  160. HS_TIME_PERIOD_LENGTH_DEFAULT,
  161. HS_TIME_PERIOD_LENGTH_MIN,
  162. HS_TIME_PERIOD_LENGTH_MAX);
  163. /* Make sure it's a positive value. */
  164. tor_assert(time_period_length >= 0);
  165. /* uint64_t will always be able to contain a int32_t */
  166. return (uint64_t) time_period_length;
  167. }
  168. /** Get the HS time period number at time <b>now</b> */
  169. uint64_t
  170. hs_get_time_period_num(time_t now)
  171. {
  172. uint64_t time_period_num;
  173. uint64_t time_period_length = get_time_period_length();
  174. uint64_t minutes_since_epoch = now / 60;
  175. /* Now subtract half a day to fit the prop224 time period schedule (see
  176. * section [TIME-PERIODS]). */
  177. tor_assert(minutes_since_epoch > HS_TIME_PERIOD_ROTATION_OFFSET);
  178. minutes_since_epoch -= HS_TIME_PERIOD_ROTATION_OFFSET;
  179. /* Calculate the time period */
  180. time_period_num = minutes_since_epoch / time_period_length;
  181. return time_period_num;
  182. }
  183. /** Get the number of the _upcoming_ HS time period, given that the current
  184. * time is <b>now</b>. */
  185. uint64_t
  186. hs_get_next_time_period_num(time_t now)
  187. {
  188. return hs_get_time_period_num(now) + 1;
  189. }
  190. /* Return the start time of the upcoming time period based on <b>now</b>. */
  191. time_t
  192. hs_get_start_time_of_next_time_period(time_t now)
  193. {
  194. uint64_t time_period_length = get_time_period_length();
  195. /* Get start time of next time period */
  196. uint64_t next_time_period_num = hs_get_next_time_period_num(now);
  197. uint64_t start_of_next_tp_in_mins = next_time_period_num *time_period_length;
  198. /* Apply rotation offset as specified by prop224 section [TIME-PERIODS] */
  199. unsigned int time_period_rotation_offset = sr_state_get_phase_duration();
  200. return start_of_next_tp_in_mins * 60 + time_period_rotation_offset;
  201. }
  202. /* Create a new rend_data_t for a specific given <b>version</b>.
  203. * Return a pointer to the newly allocated data structure. */
  204. static rend_data_t *
  205. rend_data_alloc(uint32_t version)
  206. {
  207. rend_data_t *rend_data = NULL;
  208. switch (version) {
  209. case HS_VERSION_TWO:
  210. {
  211. rend_data_v2_t *v2 = tor_malloc_zero(sizeof(*v2));
  212. v2->base_.version = HS_VERSION_TWO;
  213. v2->base_.hsdirs_fp = smartlist_new();
  214. rend_data = &v2->base_;
  215. break;
  216. }
  217. default:
  218. tor_assert(0);
  219. break;
  220. }
  221. return rend_data;
  222. }
  223. /** Free all storage associated with <b>data</b> */
  224. void
  225. rend_data_free(rend_data_t *data)
  226. {
  227. if (!data) {
  228. return;
  229. }
  230. /* By using our allocation function, this should always be set. */
  231. tor_assert(data->hsdirs_fp);
  232. /* Cleanup the HSDir identity digest. */
  233. SMARTLIST_FOREACH(data->hsdirs_fp, char *, d, tor_free(d));
  234. smartlist_free(data->hsdirs_fp);
  235. /* Depending on the version, cleanup. */
  236. switch (data->version) {
  237. case HS_VERSION_TWO:
  238. {
  239. rend_data_v2_t *v2_data = TO_REND_DATA_V2(data);
  240. tor_free(v2_data);
  241. break;
  242. }
  243. default:
  244. tor_assert(0);
  245. }
  246. }
  247. /* Allocate and return a deep copy of <b>data</b>. */
  248. rend_data_t *
  249. rend_data_dup(const rend_data_t *data)
  250. {
  251. rend_data_t *data_dup = NULL;
  252. smartlist_t *hsdirs_fp = smartlist_new();
  253. tor_assert(data);
  254. tor_assert(data->hsdirs_fp);
  255. SMARTLIST_FOREACH(data->hsdirs_fp, char *, fp,
  256. smartlist_add(hsdirs_fp, tor_memdup(fp, DIGEST_LEN)));
  257. switch (data->version) {
  258. case HS_VERSION_TWO:
  259. {
  260. rend_data_v2_t *v2_data = tor_memdup(TO_REND_DATA_V2(data),
  261. sizeof(*v2_data));
  262. data_dup = &v2_data->base_;
  263. data_dup->hsdirs_fp = hsdirs_fp;
  264. break;
  265. }
  266. default:
  267. tor_assert(0);
  268. break;
  269. }
  270. return data_dup;
  271. }
  272. /* Compute the descriptor ID for each HS descriptor replica and save them. A
  273. * valid onion address must be present in the <b>rend_data</b>.
  274. *
  275. * Return 0 on success else -1. */
  276. static int
  277. compute_desc_id(rend_data_t *rend_data)
  278. {
  279. int ret = 0;
  280. unsigned replica;
  281. time_t now = time(NULL);
  282. tor_assert(rend_data);
  283. switch (rend_data->version) {
  284. case HS_VERSION_TWO:
  285. {
  286. rend_data_v2_t *v2_data = TO_REND_DATA_V2(rend_data);
  287. /* Compute descriptor ID for each replicas. */
  288. for (replica = 0; replica < ARRAY_LENGTH(v2_data->descriptor_id);
  289. replica++) {
  290. ret = rend_compute_v2_desc_id(v2_data->descriptor_id[replica],
  291. v2_data->onion_address,
  292. v2_data->descriptor_cookie,
  293. now, replica);
  294. if (ret < 0) {
  295. goto end;
  296. }
  297. }
  298. break;
  299. }
  300. default:
  301. tor_assert(0);
  302. }
  303. end:
  304. return ret;
  305. }
  306. /* Allocate and initialize a rend_data_t object for a service using the
  307. * provided arguments. All arguments are optional (can be NULL), except from
  308. * <b>onion_address</b> which MUST be set. The <b>pk_digest</b> is the hash of
  309. * the service private key. The <b>cookie</b> is the rendezvous cookie and
  310. * <b>auth_type</b> is which authentiation this service is configured with.
  311. *
  312. * Return a valid rend_data_t pointer. This only returns a version 2 object of
  313. * rend_data_t. */
  314. rend_data_t *
  315. rend_data_service_create(const char *onion_address, const char *pk_digest,
  316. const uint8_t *cookie, rend_auth_type_t auth_type)
  317. {
  318. /* Create a rend_data_t object for version 2. */
  319. rend_data_t *rend_data = rend_data_alloc(HS_VERSION_TWO);
  320. rend_data_v2_t *v2= TO_REND_DATA_V2(rend_data);
  321. /* We need at least one else the call is wrong. */
  322. tor_assert(onion_address != NULL);
  323. if (pk_digest) {
  324. memcpy(v2->rend_pk_digest, pk_digest, sizeof(v2->rend_pk_digest));
  325. }
  326. if (cookie) {
  327. memcpy(rend_data->rend_cookie, cookie, sizeof(rend_data->rend_cookie));
  328. }
  329. strlcpy(v2->onion_address, onion_address, sizeof(v2->onion_address));
  330. v2->auth_type = auth_type;
  331. return rend_data;
  332. }
  333. /* Allocate and initialize a rend_data_t object for a client request using the
  334. * given arguments. Either an onion address or a descriptor ID is needed. Both
  335. * can be given but in this case only the onion address will be used to make
  336. * the descriptor fetch. The <b>cookie</b> is the rendezvous cookie and
  337. * <b>auth_type</b> is which authentiation the service is configured with.
  338. *
  339. * Return a valid rend_data_t pointer or NULL on error meaning the
  340. * descriptor IDs couldn't be computed from the given data. */
  341. rend_data_t *
  342. rend_data_client_create(const char *onion_address, const char *desc_id,
  343. const char *cookie, rend_auth_type_t auth_type)
  344. {
  345. /* Create a rend_data_t object for version 2. */
  346. rend_data_t *rend_data = rend_data_alloc(HS_VERSION_TWO);
  347. rend_data_v2_t *v2= TO_REND_DATA_V2(rend_data);
  348. /* We need at least one else the call is wrong. */
  349. tor_assert(onion_address != NULL || desc_id != NULL);
  350. if (cookie) {
  351. memcpy(v2->descriptor_cookie, cookie, sizeof(v2->descriptor_cookie));
  352. }
  353. if (desc_id) {
  354. memcpy(v2->desc_id_fetch, desc_id, sizeof(v2->desc_id_fetch));
  355. }
  356. if (onion_address) {
  357. strlcpy(v2->onion_address, onion_address, sizeof(v2->onion_address));
  358. if (compute_desc_id(rend_data) < 0) {
  359. goto error;
  360. }
  361. }
  362. v2->auth_type = auth_type;
  363. return rend_data;
  364. error:
  365. rend_data_free(rend_data);
  366. return NULL;
  367. }
  368. /* Return the onion address from the rend data. Depending on the version,
  369. * the size of the address can vary but it's always NUL terminated. */
  370. const char *
  371. rend_data_get_address(const rend_data_t *rend_data)
  372. {
  373. tor_assert(rend_data);
  374. switch (rend_data->version) {
  375. case HS_VERSION_TWO:
  376. return TO_REND_DATA_V2(rend_data)->onion_address;
  377. default:
  378. /* We should always have a supported version. */
  379. tor_assert(0);
  380. }
  381. }
  382. /* Return the descriptor ID for a specific replica number from the rend
  383. * data. The returned data is a binary digest and depending on the version its
  384. * size can vary. The size of the descriptor ID is put in <b>len_out</b> if
  385. * non NULL. */
  386. const char *
  387. rend_data_get_desc_id(const rend_data_t *rend_data, uint8_t replica,
  388. size_t *len_out)
  389. {
  390. tor_assert(rend_data);
  391. switch (rend_data->version) {
  392. case HS_VERSION_TWO:
  393. tor_assert(replica < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS);
  394. if (len_out) {
  395. *len_out = DIGEST_LEN;
  396. }
  397. return TO_REND_DATA_V2(rend_data)->descriptor_id[replica];
  398. default:
  399. /* We should always have a supported version. */
  400. tor_assert(0);
  401. }
  402. }
  403. /* Return the public key digest using the given <b>rend_data</b>. The size of
  404. * the digest is put in <b>len_out</b> (if set) which can differ depending on
  405. * the version. */
  406. const uint8_t *
  407. rend_data_get_pk_digest(const rend_data_t *rend_data, size_t *len_out)
  408. {
  409. tor_assert(rend_data);
  410. switch (rend_data->version) {
  411. case HS_VERSION_TWO:
  412. {
  413. const rend_data_v2_t *v2_data = TO_REND_DATA_V2(rend_data);
  414. if (len_out) {
  415. *len_out = sizeof(v2_data->rend_pk_digest);
  416. }
  417. return (const uint8_t *) v2_data->rend_pk_digest;
  418. }
  419. default:
  420. /* We should always have a supported version. */
  421. tor_assert(0);
  422. }
  423. }
  424. /* Using the given time period number, compute the disaster shared random
  425. * value and put it in srv_out. It MUST be at least DIGEST256_LEN bytes. */
  426. static void
  427. get_disaster_srv(uint64_t time_period_num, uint8_t *srv_out)
  428. {
  429. crypto_digest_t *digest;
  430. tor_assert(srv_out);
  431. digest = crypto_digest256_new(DIGEST_SHA3_256);
  432. /* Setup payload: H("shared-random-disaster" | INT_8(period_num)) */
  433. crypto_digest_add_bytes(digest, HS_SRV_DISASTER_PREFIX,
  434. HS_SRV_DISASTER_PREFIX_LEN);
  435. crypto_digest_add_bytes(digest, (const char *) &time_period_num,
  436. sizeof(time_period_num));
  437. crypto_digest_get_digest(digest, (char *) srv_out, DIGEST256_LEN);
  438. crypto_digest_free(digest);
  439. }
  440. /* When creating a blinded key, we need a parameter which construction is as
  441. * follow: H(pubkey | [secret] | ed25519-basepoint | nonce).
  442. *
  443. * The nonce has a pre-defined format which uses the time period number
  444. * period_num and the start of the period in second start_time_period.
  445. *
  446. * The secret of size secret_len is optional meaning that it can be NULL and
  447. * thus will be ignored for the param construction.
  448. *
  449. * The result is put in param_out. */
  450. static void
  451. build_blinded_key_param(const ed25519_public_key_t *pubkey,
  452. const uint8_t *secret, size_t secret_len,
  453. uint64_t period_num, uint64_t start_time_period,
  454. uint8_t *param_out)
  455. {
  456. size_t offset = 0;
  457. uint8_t nonce[HS_KEYBLIND_NONCE_LEN];
  458. crypto_digest_t *digest;
  459. tor_assert(pubkey);
  460. tor_assert(param_out);
  461. /* Create the nonce N. The construction is as follow:
  462. * N = "key-blind" || INT_8(period_num) || INT_8(start_period_sec) */
  463. memcpy(nonce, HS_KEYBLIND_NONCE_PREFIX, HS_KEYBLIND_NONCE_PREFIX_LEN);
  464. offset += HS_KEYBLIND_NONCE_PREFIX_LEN;
  465. set_uint64(nonce + offset, period_num);
  466. offset += sizeof(uint64_t);
  467. set_uint64(nonce + offset, start_time_period);
  468. offset += sizeof(uint64_t);
  469. tor_assert(offset == HS_KEYBLIND_NONCE_LEN);
  470. /* Generate the parameter h and the construction is as follow:
  471. * h = H(pubkey | [secret] | ed25519-basepoint | nonce) */
  472. digest = crypto_digest256_new(DIGEST_SHA3_256);
  473. crypto_digest_add_bytes(digest, (char *) pubkey, ED25519_PUBKEY_LEN);
  474. /* Optional secret. */
  475. if (secret) {
  476. crypto_digest_add_bytes(digest, (char *) secret, secret_len);
  477. }
  478. crypto_digest_add_bytes(digest, str_ed25519_basepoint,
  479. strlen(str_ed25519_basepoint));
  480. crypto_digest_add_bytes(digest, (char *) nonce, sizeof(nonce));
  481. /* Extract digest and put it in the param. */
  482. crypto_digest_get_digest(digest, (char *) param_out, DIGEST256_LEN);
  483. crypto_digest_free(digest);
  484. }
  485. /* Using an ed25519 public key and version to build the checksum of an
  486. * address. Put in checksum_out. Format is:
  487. * SHA3-256(".onion checksum" || PUBKEY || VERSION)
  488. *
  489. * checksum_out must be large enough to receive 32 bytes (DIGEST256_LEN). */
  490. static void
  491. build_hs_checksum(const ed25519_public_key_t *key, uint8_t version,
  492. uint8_t *checksum_out)
  493. {
  494. size_t offset = 0;
  495. char data[HS_SERVICE_ADDR_CHECKSUM_INPUT_LEN];
  496. /* Build checksum data. */
  497. memcpy(data, HS_SERVICE_ADDR_CHECKSUM_PREFIX,
  498. HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN);
  499. offset += HS_SERVICE_ADDR_CHECKSUM_PREFIX_LEN;
  500. memcpy(data + offset, key->pubkey, ED25519_PUBKEY_LEN);
  501. offset += ED25519_PUBKEY_LEN;
  502. set_uint8(data + offset, version);
  503. offset += sizeof(version);
  504. tor_assert(offset == HS_SERVICE_ADDR_CHECKSUM_INPUT_LEN);
  505. /* Hash the data payload to create the checksum. */
  506. crypto_digest256((char *) checksum_out, data, sizeof(data),
  507. DIGEST_SHA3_256);
  508. }
  509. /* Using an ed25519 public key, checksum and version to build the binary
  510. * representation of a service address. Put in addr_out. Format is:
  511. * addr_out = PUBKEY || CHECKSUM || VERSION
  512. *
  513. * addr_out must be large enough to receive HS_SERVICE_ADDR_LEN bytes. */
  514. static void
  515. build_hs_address(const ed25519_public_key_t *key, const uint8_t *checksum,
  516. uint8_t version, char *addr_out)
  517. {
  518. size_t offset = 0;
  519. tor_assert(key);
  520. tor_assert(checksum);
  521. memcpy(addr_out, key->pubkey, ED25519_PUBKEY_LEN);
  522. offset += ED25519_PUBKEY_LEN;
  523. memcpy(addr_out + offset, checksum, HS_SERVICE_ADDR_CHECKSUM_LEN_USED);
  524. offset += HS_SERVICE_ADDR_CHECKSUM_LEN_USED;
  525. set_uint8(addr_out + offset, version);
  526. offset += sizeof(uint8_t);
  527. tor_assert(offset == HS_SERVICE_ADDR_LEN);
  528. }
  529. /* Helper for hs_parse_address(): Using a binary representation of a service
  530. * address, parse its content into the key_out, checksum_out and version_out.
  531. * Any out variable can be NULL in case the caller would want only one field.
  532. * checksum_out MUST at least be 2 bytes long. address must be at least
  533. * HS_SERVICE_ADDR_LEN bytes but doesn't need to be NUL terminated. */
  534. static void
  535. hs_parse_address_impl(const char *address, ed25519_public_key_t *key_out,
  536. uint8_t *checksum_out, uint8_t *version_out)
  537. {
  538. size_t offset = 0;
  539. tor_assert(address);
  540. if (key_out) {
  541. /* First is the key. */
  542. memcpy(key_out->pubkey, address, ED25519_PUBKEY_LEN);
  543. }
  544. offset += ED25519_PUBKEY_LEN;
  545. if (checksum_out) {
  546. /* Followed by a 2 bytes checksum. */
  547. memcpy(checksum_out, address + offset, HS_SERVICE_ADDR_CHECKSUM_LEN_USED);
  548. }
  549. offset += HS_SERVICE_ADDR_CHECKSUM_LEN_USED;
  550. if (version_out) {
  551. /* Finally, version value is 1 byte. */
  552. *version_out = get_uint8(address + offset);
  553. }
  554. offset += sizeof(uint8_t);
  555. /* Extra safety. */
  556. tor_assert(offset == HS_SERVICE_ADDR_LEN);
  557. }
  558. /* Using the given identity public key and a blinded public key, compute the
  559. * subcredential and put it in subcred_out. This can't fail. */
  560. void
  561. hs_get_subcredential(const ed25519_public_key_t *identity_pk,
  562. const ed25519_public_key_t *blinded_pk,
  563. uint8_t *subcred_out)
  564. {
  565. uint8_t credential[DIGEST256_LEN];
  566. crypto_digest_t *digest;
  567. tor_assert(identity_pk);
  568. tor_assert(blinded_pk);
  569. tor_assert(subcred_out);
  570. /* First, build the credential. Construction is as follow:
  571. * credential = H("credential" | public-identity-key) */
  572. digest = crypto_digest256_new(DIGEST_SHA3_256);
  573. crypto_digest_add_bytes(digest, HS_CREDENTIAL_PREFIX,
  574. HS_CREDENTIAL_PREFIX_LEN);
  575. crypto_digest_add_bytes(digest, (const char *) identity_pk->pubkey,
  576. ED25519_PUBKEY_LEN);
  577. crypto_digest_get_digest(digest, (char *) credential, DIGEST256_LEN);
  578. crypto_digest_free(digest);
  579. /* Now, compute the subcredential. Construction is as follow:
  580. * subcredential = H("subcredential" | credential | blinded-public-key). */
  581. digest = crypto_digest256_new(DIGEST_SHA3_256);
  582. crypto_digest_add_bytes(digest, HS_SUBCREDENTIAL_PREFIX,
  583. HS_SUBCREDENTIAL_PREFIX_LEN);
  584. crypto_digest_add_bytes(digest, (const char *) credential,
  585. sizeof(credential));
  586. crypto_digest_add_bytes(digest, (const char *) blinded_pk->pubkey,
  587. ED25519_PUBKEY_LEN);
  588. crypto_digest_get_digest(digest, (char *) subcred_out, DIGEST256_LEN);
  589. crypto_digest_free(digest);
  590. }
  591. /* From the given list of hidden service ports, find the matching one from the
  592. * given edge connection conn and set the connection address from the service
  593. * port object. Return 0 on success or -1 if none. */
  594. int
  595. hs_set_conn_addr_port(const smartlist_t *ports, edge_connection_t *conn)
  596. {
  597. rend_service_port_config_t *chosen_port;
  598. unsigned int warn_once = 0;
  599. smartlist_t *matching_ports;
  600. tor_assert(ports);
  601. tor_assert(conn);
  602. matching_ports = smartlist_new();
  603. SMARTLIST_FOREACH_BEGIN(ports, rend_service_port_config_t *, p) {
  604. if (TO_CONN(conn)->port != p->virtual_port) {
  605. continue;
  606. }
  607. if (!(p->is_unix_addr)) {
  608. smartlist_add(matching_ports, p);
  609. } else {
  610. if (add_unix_port(matching_ports, p)) {
  611. if (!warn_once) {
  612. /* Unix port not supported so warn only once. */
  613. log_warn(LD_REND, "Saw AF_UNIX virtual port mapping for port %d "
  614. "which is unsupported on this platform. "
  615. "Ignoring it.",
  616. TO_CONN(conn)->port);
  617. }
  618. warn_once++;
  619. }
  620. }
  621. } SMARTLIST_FOREACH_END(p);
  622. chosen_port = smartlist_choose(matching_ports);
  623. smartlist_free(matching_ports);
  624. if (chosen_port) {
  625. if (!(chosen_port->is_unix_addr)) {
  626. /* Get a non-AF_UNIX connection ready for connection_exit_connect() */
  627. tor_addr_copy(&TO_CONN(conn)->addr, &chosen_port->real_addr);
  628. TO_CONN(conn)->port = chosen_port->real_port;
  629. } else {
  630. if (set_unix_port(conn, chosen_port)) {
  631. /* Simply impossible to end up here else we were able to add a Unix
  632. * port without AF_UNIX support... ? */
  633. tor_assert(0);
  634. }
  635. }
  636. }
  637. return (chosen_port) ? 0 : -1;
  638. }
  639. /* Using a base32 representation of a service address, parse its content into
  640. * the key_out, checksum_out and version_out. Any out variable can be NULL in
  641. * case the caller would want only one field. checksum_out MUST at least be 2
  642. * bytes long.
  643. *
  644. * Return 0 if parsing went well; return -1 in case of error. */
  645. int
  646. hs_parse_address(const char *address, ed25519_public_key_t *key_out,
  647. uint8_t *checksum_out, uint8_t *version_out)
  648. {
  649. char decoded[HS_SERVICE_ADDR_LEN];
  650. tor_assert(address);
  651. /* Obvious length check. */
  652. if (strlen(address) != HS_SERVICE_ADDR_LEN_BASE32) {
  653. log_warn(LD_REND, "Service address %s has an invalid length. "
  654. "Expected %lu but got %lu.",
  655. escaped_safe_str(address),
  656. (unsigned long) HS_SERVICE_ADDR_LEN_BASE32,
  657. (unsigned long) strlen(address));
  658. goto invalid;
  659. }
  660. /* Decode address so we can extract needed fields. */
  661. if (base32_decode(decoded, sizeof(decoded), address, strlen(address)) < 0) {
  662. log_warn(LD_REND, "Service address %s can't be decoded.",
  663. escaped_safe_str(address));
  664. goto invalid;
  665. }
  666. /* Parse the decoded address into the fields we need. */
  667. hs_parse_address_impl(decoded, key_out, checksum_out, version_out);
  668. return 0;
  669. invalid:
  670. return -1;
  671. }
  672. /* Validate a given onion address. The length, the base32 decoding and
  673. * checksum are validated. Return 1 if valid else 0. */
  674. int
  675. hs_address_is_valid(const char *address)
  676. {
  677. uint8_t version;
  678. uint8_t checksum[HS_SERVICE_ADDR_CHECKSUM_LEN_USED];
  679. uint8_t target_checksum[DIGEST256_LEN];
  680. ed25519_public_key_t key;
  681. /* Parse the decoded address into the fields we need. */
  682. if (hs_parse_address(address, &key, checksum, &version) < 0) {
  683. goto invalid;
  684. }
  685. /* Get the checksum it's suppose to be and compare it with what we have
  686. * encoded in the address. */
  687. build_hs_checksum(&key, version, target_checksum);
  688. if (tor_memcmp(checksum, target_checksum, sizeof(checksum))) {
  689. log_warn(LD_REND, "Service address %s invalid checksum.",
  690. escaped_safe_str(address));
  691. goto invalid;
  692. }
  693. /* Valid address. */
  694. return 1;
  695. invalid:
  696. return 0;
  697. }
  698. /* Build a service address using an ed25519 public key and a given version.
  699. * The returned address is base32 encoded and put in addr_out. The caller MUST
  700. * make sure the addr_out is at least HS_SERVICE_ADDR_LEN_BASE32 + 1 long.
  701. *
  702. * Format is as follow:
  703. * base32(PUBKEY || CHECKSUM || VERSION)
  704. * CHECKSUM = H(".onion checksum" || PUBKEY || VERSION)
  705. * */
  706. void
  707. hs_build_address(const ed25519_public_key_t *key, uint8_t version,
  708. char *addr_out)
  709. {
  710. uint8_t checksum[DIGEST256_LEN];
  711. char address[HS_SERVICE_ADDR_LEN];
  712. tor_assert(key);
  713. tor_assert(addr_out);
  714. /* Get the checksum of the address. */
  715. build_hs_checksum(key, version, checksum);
  716. /* Get the binary address representation. */
  717. build_hs_address(key, checksum, version, address);
  718. /* Encode the address. addr_out will be NUL terminated after this. */
  719. base32_encode(addr_out, HS_SERVICE_ADDR_LEN_BASE32 + 1, address,
  720. sizeof(address));
  721. /* Validate what we just built. */
  722. tor_assert(hs_address_is_valid(addr_out));
  723. }
  724. /* Return a newly allocated copy of lspec. */
  725. link_specifier_t *
  726. hs_link_specifier_dup(const link_specifier_t *lspec)
  727. {
  728. link_specifier_t *dup = link_specifier_new();
  729. memcpy(dup, lspec, sizeof(*dup));
  730. /* The unrecognized field is a dynamic array so make sure to copy its
  731. * content and not the pointer. */
  732. link_specifier_setlen_un_unrecognized(
  733. dup, link_specifier_getlen_un_unrecognized(lspec));
  734. if (link_specifier_getlen_un_unrecognized(dup)) {
  735. memcpy(link_specifier_getarray_un_unrecognized(dup),
  736. link_specifier_getconstarray_un_unrecognized(lspec),
  737. link_specifier_getlen_un_unrecognized(dup));
  738. }
  739. return dup;
  740. }
  741. /* From a given ed25519 public key pk and an optional secret, compute a
  742. * blinded public key and put it in blinded_pk_out. This is only useful to
  743. * the client side because the client only has access to the identity public
  744. * key of the service. */
  745. void
  746. hs_build_blinded_pubkey(const ed25519_public_key_t *pk,
  747. const uint8_t *secret, size_t secret_len,
  748. uint64_t time_period_num,
  749. ed25519_public_key_t *blinded_pk_out)
  750. {
  751. /* Our blinding key API requires a 32 bytes parameter. */
  752. uint8_t param[DIGEST256_LEN];
  753. tor_assert(pk);
  754. tor_assert(blinded_pk_out);
  755. tor_assert(!tor_mem_is_zero((char *) pk, ED25519_PUBKEY_LEN));
  756. build_blinded_key_param(pk, secret, secret_len,
  757. time_period_num, get_time_period_length(), param);
  758. ed25519_public_blind(blinded_pk_out, pk, param);
  759. }
  760. /* From a given ed25519 keypair kp and an optional secret, compute a blinded
  761. * keypair for the current time period and put it in blinded_kp_out. This is
  762. * only useful by the service side because the client doesn't have access to
  763. * the identity secret key. */
  764. void
  765. hs_build_blinded_keypair(const ed25519_keypair_t *kp,
  766. const uint8_t *secret, size_t secret_len,
  767. uint64_t time_period_num,
  768. ed25519_keypair_t *blinded_kp_out)
  769. {
  770. /* Our blinding key API requires a 32 bytes parameter. */
  771. uint8_t param[DIGEST256_LEN];
  772. tor_assert(kp);
  773. tor_assert(blinded_kp_out);
  774. /* Extra safety. A zeroed key is bad. */
  775. tor_assert(!tor_mem_is_zero((char *) &kp->pubkey, ED25519_PUBKEY_LEN));
  776. tor_assert(!tor_mem_is_zero((char *) &kp->seckey, ED25519_SECKEY_LEN));
  777. build_blinded_key_param(&kp->pubkey, secret, secret_len,
  778. time_period_num, get_time_period_length(), param);
  779. ed25519_keypair_blind(blinded_kp_out, kp, param);
  780. }
  781. /* Return true if overlap mode is active given the date in consensus. If
  782. * consensus is NULL, then we use the latest live consensus we can find. */
  783. MOCK_IMPL(int,
  784. hs_overlap_mode_is_active, (const networkstatus_t *consensus, time_t now))
  785. {
  786. struct tm valid_after_tm;
  787. if (!consensus) {
  788. consensus = networkstatus_get_live_consensus(now);
  789. if (!consensus) {
  790. return 0;
  791. }
  792. }
  793. /* XXX: Futur commits will change this to a slot system so it can be
  794. * fine tuned better for testing networks in terms of timings. */
  795. /* From the spec: "Specifically, when a hidden service fetches a consensus
  796. * with "valid-after" between 00:00UTC and 12:00UTC, it goes into
  797. * "descriptor overlap" mode." */
  798. tor_gmtime_r(&consensus->valid_after, &valid_after_tm);
  799. if (valid_after_tm.tm_hour > 0 && valid_after_tm.tm_hour < 12) {
  800. return 1;
  801. }
  802. return 0;
  803. }
  804. /* Return 1 if any virtual port in ports needs a circuit with good uptime.
  805. * Else return 0. */
  806. int
  807. hs_service_requires_uptime_circ(const smartlist_t *ports)
  808. {
  809. tor_assert(ports);
  810. SMARTLIST_FOREACH_BEGIN(ports, rend_service_port_config_t *, p) {
  811. if (smartlist_contains_int_as_string(get_options()->LongLivedPorts,
  812. p->virtual_port)) {
  813. return 1;
  814. }
  815. } SMARTLIST_FOREACH_END(p);
  816. return 0;
  817. }
  818. /* Build hs_index which is used to find the responsible hsdirs. This index
  819. * value is used to select the responsible HSDir where their hsdir_index is
  820. * closest to this value.
  821. * SHA3-256("store-at-idx" | blinded_public_key |
  822. * INT_8(replicanum) | INT_8(period_num) )
  823. *
  824. * hs_index_out must be large enough to receive DIGEST256_LEN bytes. */
  825. void
  826. hs_build_hs_index(uint64_t replica, const ed25519_public_key_t *blinded_pk,
  827. uint64_t period_num, uint8_t *hs_index_out)
  828. {
  829. crypto_digest_t *digest;
  830. tor_assert(blinded_pk);
  831. tor_assert(hs_index_out);
  832. /* Build hs_index. See construction at top of function comment. */
  833. digest = crypto_digest256_new(DIGEST_SHA3_256);
  834. crypto_digest_add_bytes(digest, HS_INDEX_PREFIX, HS_INDEX_PREFIX_LEN);
  835. crypto_digest_add_bytes(digest, (const char *) blinded_pk->pubkey,
  836. ED25519_PUBKEY_LEN);
  837. crypto_digest_add_bytes(digest, (const char *) &replica, sizeof(replica));
  838. crypto_digest_add_bytes(digest, (const char *) &period_num,
  839. sizeof(period_num));
  840. crypto_digest_get_digest(digest, (char *) hs_index_out, DIGEST256_LEN);
  841. crypto_digest_free(digest);
  842. }
  843. /* Build hsdir_index which is used to find the responsible hsdirs. This is the
  844. * index value that is compare to the hs_index when selecting an HSDir.
  845. * SHA3-256("node-idx" | node_identity |
  846. * shared_random_value | INT_8(period_num) )
  847. *
  848. * hsdir_index_out must be large enough to receive DIGEST256_LEN bytes. */
  849. void
  850. hs_build_hsdir_index(const ed25519_public_key_t *identity_pk,
  851. const uint8_t *srv_value, uint64_t period_num,
  852. uint8_t *hsdir_index_out)
  853. {
  854. crypto_digest_t *digest;
  855. tor_assert(identity_pk);
  856. tor_assert(srv_value);
  857. tor_assert(hsdir_index_out);
  858. /* Build hsdir_index. See construction at top of function comment. */
  859. digest = crypto_digest256_new(DIGEST_SHA3_256);
  860. crypto_digest_add_bytes(digest, HSDIR_INDEX_PREFIX, HSDIR_INDEX_PREFIX_LEN);
  861. crypto_digest_add_bytes(digest, (const char *) identity_pk->pubkey,
  862. ED25519_PUBKEY_LEN);
  863. crypto_digest_add_bytes(digest, (const char *) srv_value, DIGEST256_LEN);
  864. crypto_digest_add_bytes(digest, (const char *) &period_num,
  865. sizeof(period_num));
  866. crypto_digest_get_digest(digest, (char *) hsdir_index_out, DIGEST256_LEN);
  867. crypto_digest_free(digest);
  868. }
  869. /* Return a newly allocated buffer containing the current shared random value
  870. * or if not present, a disaster value is computed using the given time period
  871. * number. This function can't fail. */
  872. uint8_t *
  873. hs_get_current_srv(uint64_t time_period_num)
  874. {
  875. uint8_t *sr_value = tor_malloc_zero(DIGEST256_LEN);
  876. const sr_srv_t *current_srv = sr_get_current();
  877. if (current_srv) {
  878. memcpy(sr_value, current_srv->value, sizeof(current_srv->value));
  879. } else {
  880. /* Disaster mode. */
  881. get_disaster_srv(time_period_num, sr_value);
  882. }
  883. return sr_value;
  884. }
  885. /* Return a newly allocated buffer containing the previous shared random
  886. * value or if not present, a disaster value is computed using the given time
  887. * period number. This function can't fail. */
  888. uint8_t *
  889. hs_get_previous_srv(uint64_t time_period_num)
  890. {
  891. uint8_t *sr_value = tor_malloc_zero(DIGEST256_LEN);
  892. const sr_srv_t *previous_srv = sr_get_previous();
  893. if (previous_srv) {
  894. memcpy(sr_value, previous_srv->value, sizeof(previous_srv->value));
  895. } else {
  896. /* Disaster mode. */
  897. get_disaster_srv(time_period_num, sr_value);
  898. }
  899. return sr_value;
  900. }
  901. /* Return the number of replicas defined by a consensus parameter or the
  902. * default value. */
  903. int32_t
  904. hs_get_hsdir_n_replicas(void)
  905. {
  906. /* The [1,16] range is a specification requirement. */
  907. return networkstatus_get_param(NULL, "hsdir_n_replicas",
  908. HS_DEFAULT_HSDIR_N_REPLICAS, 1, 16);
  909. }
  910. /* Return the spread fetch value defined by a consensus parameter or the
  911. * default value. */
  912. int32_t
  913. hs_get_hsdir_spread_fetch(void)
  914. {
  915. /* The [1,128] range is a specification requirement. */
  916. return networkstatus_get_param(NULL, "hsdir_spread_fetch",
  917. HS_DEFAULT_HSDIR_SPREAD_FETCH, 1, 128);
  918. }
  919. /* Return the spread store value defined by a consensus parameter or the
  920. * default value. */
  921. int32_t
  922. hs_get_hsdir_spread_store(void)
  923. {
  924. /* The [1,128] range is a specification requirement. */
  925. return networkstatus_get_param(NULL, "hsdir_spread_store",
  926. HS_DEFAULT_HSDIR_SPREAD_STORE, 1, 128);
  927. }
  928. /* For a given blinded key and time period number, get the responsible HSDir
  929. * and put their routerstatus_t object in the responsible_dirs list. If
  930. * is_next_period is true, the next hsdir_index of the node_t is used. If
  931. * is_client is true, the spread fetch consensus parameter is used else the
  932. * spread store is used which is only for upload. This function can't fail but
  933. * it is possible that the responsible_dirs list contains fewer nodes than
  934. * expected.
  935. *
  936. * This function goes over the latest consensus routerstatus list and sorts it
  937. * by their node_t hsdir_index then does a binary search to find the closest
  938. * node. All of this makes it a bit CPU intensive so use it wisely. */
  939. void
  940. hs_get_responsible_hsdirs(const ed25519_public_key_t *blinded_pk,
  941. uint64_t time_period_num, int is_next_period,
  942. int is_client, smartlist_t *responsible_dirs)
  943. {
  944. smartlist_t *sorted_nodes;
  945. /* The compare function used for the smartlist bsearch. We have two
  946. * different depending on is_next_period. */
  947. int (*cmp_fct)(const void *, const void **);
  948. tor_assert(blinded_pk);
  949. tor_assert(responsible_dirs);
  950. sorted_nodes = smartlist_new();
  951. /* Add every node_t that support HSDir v3 for which we do have a valid
  952. * hsdir_index already computed for them for this consensus. */
  953. {
  954. networkstatus_t *c = networkstatus_get_latest_consensus();
  955. if (!c || smartlist_len(c->routerstatus_list) == 0) {
  956. log_warn(LD_REND, "No valid consensus so we can't get the responsible "
  957. "hidden service directories.");
  958. goto done;
  959. }
  960. SMARTLIST_FOREACH_BEGIN(c->routerstatus_list, const routerstatus_t *, rs) {
  961. /* Even though this node_t object won't be modified and should be const,
  962. * we can't add const object in a smartlist_t. */
  963. node_t *n = node_get_mutable_by_id(rs->identity_digest);
  964. tor_assert(n);
  965. if (node_supports_v3_hsdir(n) && rs->is_hs_dir) {
  966. if (BUG(n->hsdir_index == NULL)) {
  967. continue;
  968. }
  969. smartlist_add(sorted_nodes, n);
  970. }
  971. } SMARTLIST_FOREACH_END(rs);
  972. }
  973. if (smartlist_len(sorted_nodes) == 0) {
  974. log_warn(LD_REND, "No nodes found to be HSDir or supporting v3.");
  975. goto done;
  976. }
  977. /* First thing we have to do is sort all node_t by hsdir_index. The
  978. * is_next_period tells us if we want the current or the next one. Set the
  979. * bsearch compare function also while we are at it. */
  980. if (is_next_period) {
  981. smartlist_sort(sorted_nodes, compare_node_next_hsdir_index);
  982. cmp_fct = compare_digest_to_next_hsdir_index;
  983. } else {
  984. smartlist_sort(sorted_nodes, compare_node_current_hsdir_index);
  985. cmp_fct = compare_digest_to_current_hsdir_index;
  986. }
  987. /* For all replicas, we'll select a set of HSDirs using the consensus
  988. * parameters and the sorted list. The replica starting at value 1 is
  989. * defined by the specification. */
  990. for (int replica = 1; replica <= hs_get_hsdir_n_replicas(); replica++) {
  991. int idx, start, found, n_added = 0;
  992. uint8_t hs_index[DIGEST256_LEN] = {0};
  993. /* Number of node to add to the responsible dirs list depends on if we are
  994. * trying to fetch or store. A client always fetches. */
  995. int n_to_add = (is_client) ? hs_get_hsdir_spread_fetch() :
  996. hs_get_hsdir_spread_store();
  997. /* Get the index that we should use to select the node. */
  998. hs_build_hs_index(replica, blinded_pk, time_period_num, hs_index);
  999. /* The compare function pointer has been set correctly earlier. */
  1000. start = idx = smartlist_bsearch_idx(sorted_nodes, hs_index, cmp_fct,
  1001. &found);
  1002. /* Getting the length of the list if no member is greater than the key we
  1003. * are looking for so start at the first element. */
  1004. if (idx == smartlist_len(sorted_nodes)) {
  1005. start = idx = 0;
  1006. }
  1007. while (n_added < n_to_add) {
  1008. const node_t *node = smartlist_get(sorted_nodes, idx);
  1009. /* If the node has already been selected which is possible between
  1010. * replicas, the specification says to skip over. */
  1011. if (!smartlist_contains(responsible_dirs, node->rs)) {
  1012. smartlist_add(responsible_dirs, node->rs);
  1013. ++n_added;
  1014. }
  1015. if (++idx == smartlist_len(sorted_nodes)) {
  1016. /* Wrap if we've reached the end of the list. */
  1017. idx = 0;
  1018. }
  1019. if (idx == start) {
  1020. /* We've gone over the whole list, stop and avoid infinite loop. */
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. done:
  1026. smartlist_free(sorted_nodes);
  1027. }
  1028. /* Initialize the entire HS subsytem. This is called in tor_init() before any
  1029. * torrc options are loaded. Only for >= v3. */
  1030. void
  1031. hs_init(void)
  1032. {
  1033. hs_circuitmap_init();
  1034. hs_service_init();
  1035. hs_cache_init();
  1036. }
  1037. /* Release and cleanup all memory of the HS subsystem (all version). This is
  1038. * called by tor_free_all(). */
  1039. void
  1040. hs_free_all(void)
  1041. {
  1042. hs_circuitmap_free_all();
  1043. hs_service_free_all();
  1044. hs_cache_free_all();
  1045. }