tor  0.4.2.1-alpha-dev
shared_random_state.h
1 /* Copyright (c) 2016-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 #ifndef TOR_SHARED_RANDOM_STATE_H
5 #define TOR_SHARED_RANDOM_STATE_H
6 
7 #include "feature/dirauth/shared_random.h"
8 
9 /* Action that can be performed on the state for any objects. */
10 typedef enum {
11  SR_STATE_ACTION_GET = 1,
12  SR_STATE_ACTION_PUT = 2,
13  SR_STATE_ACTION_DEL = 3,
14  SR_STATE_ACTION_DEL_ALL = 4,
15  SR_STATE_ACTION_SAVE = 5,
16 } sr_state_action_t;
17 
18 /* Object in the state that can be queried through the state API. */
19 typedef enum {
20  /* Will return a single commit using an authority identity key. */
21  SR_STATE_OBJ_COMMIT,
22  /* Returns the entire list of commits from the state. */
23  SR_STATE_OBJ_COMMITS,
24  /* Return the current SRV object pointer. */
25  SR_STATE_OBJ_CURSRV,
26  /* Return the previous SRV object pointer. */
27  SR_STATE_OBJ_PREVSRV,
28  /* Return the phase. */
29  SR_STATE_OBJ_PHASE,
30  /* Get or Put the valid after time. */
31  SR_STATE_OBJ_VALID_AFTER,
32 } sr_state_object_t;
33 
34 /* State of the protocol. It's also saved on disk in fname. This data
35  * structure MUST be synchronized at all time with the one on disk. */
36 typedef struct sr_state_t {
37  /* Filename of the state file on disk. */
38  char *fname;
39  /* Version of the protocol. */
40  uint32_t version;
41  /* The valid-after of the voting period we have prepared the state for. */
42  time_t valid_after;
43  /* Until when is this state valid? */
44  time_t valid_until;
45  /* Protocol phase. */
46  sr_phase_t phase;
47 
48  /* Number of runs completed. */
49  uint64_t n_protocol_runs;
50  /* The number of commitment rounds we've performed in this protocol run. */
51  unsigned int n_commit_rounds;
52  /* The number of reveal rounds we've performed in this protocol run. */
53  unsigned int n_reveal_rounds;
54 
55  /* A map of all the received commitments for this protocol run. This is
56  * indexed by authority RSA identity digest. */
57  digestmap_t *commits;
58 
59  /* Current and previous shared random value. */
60  sr_srv_t *previous_srv;
61  sr_srv_t *current_srv;
62 
63  /* Indicate if the state contains an SRV that was _just_ generated. This is
64  * used during voting so that we know whether to use the super majority rule
65  * or not when deciding on keeping it for the consensus. It is _always_ set
66  * to 0 post consensus.
67  *
68  * EDGE CASE: if an authority computes a new SRV then immediately reboots
69  * and, once back up, votes for the current round, it won't know if the
70  * SRV is fresh or not ultimately making it _NOT_ use the super majority
71  * when deciding to put or not the SRV in the consensus. This is for now
72  * an acceptable very rare edge case. */
73  unsigned int is_srv_fresh:1;
74 } sr_state_t;
75 
76 /* Persistent state of the protocol, as saved to disk. */
77 typedef struct sr_disk_state_t {
78  uint32_t magic_;
79  /* Version of the protocol. */
80  int Version;
81  /* Version of our running tor. */
82  char *TorVersion;
83  /* Creation time of this state */
84  time_t ValidAfter;
85  /* State valid until? */
86  time_t ValidUntil;
87  /* All commits seen that are valid. */
88  struct config_line_t *Commit;
89  /* Previous and current shared random value. */
90  struct config_line_t *SharedRandValues;
91  /* Extra Lines for configuration we might not know. */
92  struct config_line_t *ExtraLines;
94 
95 /* API */
96 
97 /* Public methods: */
98 
99 void sr_state_update(time_t valid_after);
100 
101 /* Private methods (only used by shared-random.c): */
102 
103 void sr_state_set_valid_after(time_t valid_after);
104 sr_phase_t sr_state_get_phase(void);
105 const sr_srv_t *sr_state_get_previous_srv(void);
106 const sr_srv_t *sr_state_get_current_srv(void);
107 void sr_state_set_previous_srv(const sr_srv_t *srv);
108 void sr_state_set_current_srv(const sr_srv_t *srv);
109 void sr_state_clean_srvs(void);
110 digestmap_t *sr_state_get_commits(void);
111 sr_commit_t *sr_state_get_commit(const char *rsa_fpr);
112 void sr_state_add_commit(sr_commit_t *commit);
113 void sr_state_delete_commits(void);
114 void sr_state_copy_reveal_info(sr_commit_t *saved_commit,
115  const sr_commit_t *commit);
116 unsigned int sr_state_srv_is_fresh(void);
117 void sr_state_set_fresh_srv(void);
118 void sr_state_unset_fresh_srv(void);
119 int sr_state_init(int save_to_disk, int read_from_disk);
120 int sr_state_is_initialized(void);
121 void sr_state_save(void);
122 void sr_state_free_all(void);
123 
124 #ifdef SHARED_RANDOM_STATE_PRIVATE
125 
126 STATIC int disk_state_load_from_disk_impl(const char *fname);
127 
128 STATIC sr_phase_t get_sr_protocol_phase(time_t valid_after);
129 
130 STATIC time_t get_state_valid_until_time(time_t now);
131 STATIC const char *get_phase_str(sr_phase_t phase);
132 STATIC void reset_state_for_new_protocol_run(time_t valid_after);
133 STATIC void new_protocol_run(time_t valid_after);
134 STATIC void state_rotate_srv(void);
135 STATIC int is_phase_transition(sr_phase_t next_phase);
136 
137 #endif /* defined(SHARED_RANDOM_STATE_PRIVATE) */
138 
139 #ifdef TOR_UNIT_TESTS
140 
141 STATIC void set_sr_phase(sr_phase_t phase);
142 STATIC sr_state_t *get_sr_state(void);
143 STATIC void state_del_previous_srv(void);
144 STATIC void state_del_current_srv(void);
145 
146 #endif /* defined(TOR_UNIT_TESTS) */
147 
148 #endif /* !defined(TOR_SHARED_RANDOM_STATE_H) */