hs_cache.h 4.4 KB

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