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