hs_cache.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (c) 2016-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file hs_cache.h
  5. * \brief Header file for hs_cache.c
  6. **/
  7. #ifndef TOR_HS_CACHE_H
  8. #define TOR_HS_CACHE_H
  9. #include <stdint.h>
  10. #include "feature/hs/hs_common.h"
  11. #include "feature/hs/hs_descriptor.h"
  12. #include "feature/rend/rendcommon.h"
  13. #include "feature/nodelist/torcert.h"
  14. struct ed25519_public_key_t;
  15. /* This is the maximum time an introduction point state object can stay in the
  16. * client cache in seconds (2 mins or 120 seconds). */
  17. #define HS_CACHE_CLIENT_INTRO_STATE_MAX_AGE (2 * 60)
  18. /* Introduction point state. */
  19. typedef struct hs_cache_intro_state_t {
  20. /* When this entry was created and put in the cache. */
  21. time_t created_ts;
  22. /* Did it suffered a generic error? */
  23. unsigned int error : 1;
  24. /* Did it timed out? */
  25. unsigned int timed_out : 1;
  26. /* How many times we tried to reached it and it was unreachable. */
  27. uint32_t unreachable_count;
  28. } hs_cache_intro_state_t;
  29. typedef struct hs_cache_client_intro_state_t {
  30. /* Contains hs_cache_intro_state_t object indexed by introduction point
  31. * authentication key. */
  32. digest256map_t *intro_points;
  33. } hs_cache_client_intro_state_t;
  34. /* Descriptor representation on the directory side which is a subset of
  35. * information that the HSDir can decode and serve it. */
  36. typedef struct hs_cache_dir_descriptor_t {
  37. /* This object is indexed using the blinded pubkey located in the plaintext
  38. * data which is populated only once the descriptor has been successfully
  39. * decoded and validated. This simply points to that pubkey. */
  40. const uint8_t *key;
  41. /* When does this entry has been created. Used to expire entries. */
  42. time_t created_ts;
  43. /* Descriptor plaintext information. Obviously, we can't decrypt the
  44. * encrypted part of the descriptor. */
  45. hs_desc_plaintext_data_t *plaintext_data;
  46. /* Encoded descriptor which is basically in text form. It's a NUL terminated
  47. * string thus safe to strlen(). */
  48. char *encoded_desc;
  49. } hs_cache_dir_descriptor_t;
  50. /* Public API */
  51. void hs_cache_init(void);
  52. void hs_cache_free_all(void);
  53. void hs_cache_clean_as_dir(time_t now);
  54. size_t hs_cache_handle_oom(time_t now, size_t min_remove_bytes);
  55. unsigned int hs_cache_get_max_descriptor_size(void);
  56. /* Store and Lookup function. They are version agnostic that is depending on
  57. * the requested version of the descriptor, it will be re-routed to the
  58. * right function. */
  59. int hs_cache_store_as_dir(const char *desc);
  60. int hs_cache_lookup_as_dir(uint32_t version, const char *query,
  61. const char **desc_out);
  62. const hs_descriptor_t *
  63. hs_cache_lookup_as_client(const struct ed25519_public_key_t *key);
  64. const char *
  65. hs_cache_lookup_encoded_as_client(const struct ed25519_public_key_t *key);
  66. int hs_cache_store_as_client(const char *desc_str,
  67. const struct ed25519_public_key_t *identity_pk);
  68. void hs_cache_clean_as_client(time_t now);
  69. void hs_cache_purge_as_client(void);
  70. /* Client failure cache. */
  71. void hs_cache_client_intro_state_note(
  72. const struct ed25519_public_key_t *service_pk,
  73. const struct ed25519_public_key_t *auth_key,
  74. rend_intro_point_failure_t failure);
  75. const hs_cache_intro_state_t *hs_cache_client_intro_state_find(
  76. const struct ed25519_public_key_t *service_pk,
  77. const struct ed25519_public_key_t *auth_key);
  78. void hs_cache_client_intro_state_clean(time_t now);
  79. void hs_cache_client_intro_state_purge(void);
  80. int hs_cache_pirserver_get_params(dir_connection_t *conn);
  81. #ifdef HS_CACHE_PRIVATE
  82. #include "lib/crypt_ops/crypto_ed25519.h"
  83. /** Represents a locally cached HS descriptor on a hidden service client. */
  84. typedef struct hs_cache_client_descriptor_t {
  85. /* This object is indexed using the service identity public key */
  86. struct ed25519_public_key_t key;
  87. /* When will this entry expire? We expire cached client descriptors in the
  88. * start of the next time period, since that's when clients need to start
  89. * using the next blinded key of the service. */
  90. time_t expiration_ts;
  91. /* The cached descriptor, this object is the owner. It can't be NULL. A
  92. * cache object without a valid descriptor is not possible. */
  93. hs_descriptor_t *desc;
  94. /* Encoded descriptor in string form. Can't be NULL. */
  95. char *encoded_desc;
  96. } hs_cache_client_descriptor_t;
  97. STATIC size_t cache_clean_v3_as_dir(time_t now, time_t global_cutoff);
  98. STATIC hs_cache_client_descriptor_t *
  99. lookup_v3_desc_as_client(const uint8_t *key);
  100. #endif /* defined(HS_CACHE_PRIVATE) */
  101. #endif /* !defined(TOR_HS_CACHE_H) */