hs_cache.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. int hs_cache_store_as_client(const char *desc_str,
  66. const ed25519_public_key_t *identity_pk);
  67. void hs_cache_clean_as_client(time_t now);
  68. void hs_cache_purge_as_client(void);
  69. /* Client failure cache. */
  70. void hs_cache_client_intro_state_note(const ed25519_public_key_t *service_pk,
  71. const ed25519_public_key_t *auth_key,
  72. rend_intro_point_failure_t failure);
  73. const hs_cache_intro_state_t *hs_cache_client_intro_state_find(
  74. const ed25519_public_key_t *service_pk,
  75. const ed25519_public_key_t *auth_key);
  76. void hs_cache_client_intro_state_clean(time_t now);
  77. void hs_cache_client_intro_state_purge(void);
  78. #ifdef HS_CACHE_PRIVATE
  79. /** Represents a locally cached HS descriptor on a hidden service client. */
  80. typedef struct hs_cache_client_descriptor_t {
  81. /* This object is indexed using the service identity public key */
  82. ed25519_public_key_t key;
  83. /* When will this entry expire? We expire cached client descriptors in the
  84. * start of the next time period, since that's when clients need to start
  85. * using the next blinded key of the service. */
  86. time_t expiration_ts;
  87. /* The cached descriptor, this object is the owner. It can't be NULL. A
  88. * cache object without a valid descriptor is not possible. */
  89. hs_descriptor_t *desc;
  90. /* Encoded descriptor in string form. Can't be NULL. */
  91. char *encoded_desc;
  92. } hs_cache_client_descriptor_t;
  93. STATIC size_t cache_clean_v3_as_dir(time_t now, time_t global_cutoff);
  94. STATIC hs_cache_client_descriptor_t *
  95. lookup_v3_desc_as_client(const uint8_t *key);
  96. #endif /* defined(HS_CACHE_PRIVATE) */
  97. #endif /* !defined(TOR_HS_CACHE_H) */