hs_common.c 38 KB

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