tor  0.4.2.1-alpha-dev
shared_random_state.c
Go to the documentation of this file.
1 /* Copyright (c) 2016-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
11 #define SHARED_RANDOM_STATE_PRIVATE
12 
13 #include "core/or/or.h"
14 #include "app/config/config.h"
15 #include "lib/confmgt/confparse.h"
19 #include "feature/relay/router.h"
20 #include "feature/dirauth/shared_random.h"
22 #include "feature/dirauth/shared_random_state.h"
24 #include "lib/encoding/confline.h"
25 #include "lib/version/torversion.h"
26 
27 #include "app/config/or_state_st.h"
28 
29 /* Default filename of the shared random state on disk. */
30 static const char default_fname[] = "sr-state";
31 
32 /* String representation of a protocol phase. */
33 static const char *phase_str[] = { "unknown", "commit", "reveal" };
34 
35 /* Our shared random protocol state. There is only one possible state per
36  * protocol run so this is the global state which is reset at every run once
37  * the shared random value has been computed. */
38 static sr_state_t *sr_state = NULL;
39 
40 /* Representation of our persistent state on disk. The sr_state above
41  * contains the data parsed from this state. When we save to disk, we
42  * translate the sr_state to this sr_disk_state. */
43 static sr_disk_state_t *sr_disk_state = NULL;
44 
45 /* Disk state file keys. */
46 static const char dstate_commit_key[] = "Commit";
47 static const char dstate_prev_srv_key[] = "SharedRandPreviousValue";
48 static const char dstate_cur_srv_key[] = "SharedRandCurrentValue";
49 
53 
54 #define VAR(varname,conftype,member,initvalue) \
55  CONFIG_VAR_ETYPE(sr_disk_state_t, varname, conftype, member, 0, initvalue)
56 #define V(member,conftype,initvalue) \
57  VAR(#member, conftype, member, initvalue)
58 
59 /* Our persistent state magic number. */
60 #define SR_DISK_STATE_MAGIC 0x98AB1254
61 
62 static int
63 disk_state_validate_cb(void *old_state, void *state, void *default_state,
64  int from_setconf, char **msg);
65 
66 /* Array of variables that are saved to disk as a persistent state. */
67 static const config_var_t state_vars[] = {
68  V(Version, POSINT, "0"),
69  V(TorVersion, STRING, NULL),
70  V(ValidAfter, ISOTIME, NULL),
71  V(ValidUntil, ISOTIME, NULL),
72 
73  V(Commit, LINELIST, NULL),
74 
75  V(SharedRandValues, LINELIST_V, NULL),
76  VAR("SharedRandPreviousValue",LINELIST_S, SharedRandValues, NULL),
77  VAR("SharedRandCurrentValue", LINELIST_S, SharedRandValues, NULL),
79 };
80 
81 /* "Extra" variable in the state that receives lines we can't parse. This
82  * lets us preserve options from versions of Tor newer than us. */
83 static const struct_member_t state_extra_var = {
84  .name = "__extra",
85  .type = CONFIG_TYPE_LINELIST,
86  .offset = offsetof(sr_disk_state_t, ExtraLines),
87 };
88 
89 /* Configuration format of sr_disk_state_t. */
90 static const config_format_t state_format = {
91  sizeof(sr_disk_state_t),
92  {
93  "sr_disk_state_t",
94  SR_DISK_STATE_MAGIC,
95  offsetof(sr_disk_state_t, magic_),
96  },
97  NULL,
98  NULL,
99  state_vars,
100  disk_state_validate_cb,
101  NULL,
103  -1,
104 };
105 
106 /* Global configuration manager for the shared-random state file */
107 static config_mgr_t *shared_random_state_mgr = NULL;
108 
110 static const config_mgr_t *
112 {
113  if (PREDICT_UNLIKELY(shared_random_state_mgr == NULL)) {
114  shared_random_state_mgr = config_mgr_new(&state_format);
115  config_mgr_freeze(shared_random_state_mgr);
116  }
117  return shared_random_state_mgr;
118 }
119 
120 static void state_query_del_(sr_state_object_t obj_type, void *data);
121 
122 /* Return a string representation of a protocol phase. */
123 STATIC const char *
124 get_phase_str(sr_phase_t phase)
125 {
126  const char *the_string = NULL;
127 
128  switch (phase) {
129  case SR_PHASE_COMMIT:
130  case SR_PHASE_REVEAL:
131  the_string = phase_str[phase];
132  break;
133  default:
134  /* Unknown phase shouldn't be possible. */
135  tor_assert(0);
136  }
137 
138  return the_string;
139 }
140 /* Return the time we should expire the state file created at <b>now</b>.
141  * We expire the state file in the beginning of the next protocol run. */
142 STATIC time_t
143 get_state_valid_until_time(time_t now)
144 {
145  int total_rounds = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
146  int current_round, voting_interval, rounds_left;
147  time_t valid_until, beginning_of_current_round;
148 
149  voting_interval = get_voting_interval();
150  /* Find the time the current round started. */
151  beginning_of_current_round = get_start_time_of_current_round();
152 
153  /* Find how many rounds are left till the end of the protocol run */
154  current_round = (now / voting_interval) % total_rounds;
155  rounds_left = total_rounds - current_round;
156 
157  /* To find the valid-until time now, take the start time of the current
158  * round and add to it the time it takes for the leftover rounds to
159  * complete. */
160  valid_until = beginning_of_current_round + (rounds_left * voting_interval);
161 
162  { /* Logging */
163  char tbuf[ISO_TIME_LEN + 1];
164  format_iso_time(tbuf, valid_until);
165  log_debug(LD_DIR, "SR: Valid until time for state set to %s.", tbuf);
166  }
167 
168  return valid_until;
169 }
170 
171 /* Given the consensus 'valid-after' time, return the protocol phase we should
172  * be in. */
173 STATIC sr_phase_t
174 get_sr_protocol_phase(time_t valid_after)
175 {
176  /* Shared random protocol has two phases, commit and reveal. */
177  int total_periods = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
178  int current_slot;
179 
180  /* Split time into slots of size 'voting_interval'. See which slot we are
181  * currently into, and find which phase it corresponds to. */
182  current_slot = (valid_after / get_voting_interval()) % total_periods;
183 
184  if (current_slot < SHARED_RANDOM_N_ROUNDS) {
185  return SR_PHASE_COMMIT;
186  } else {
187  return SR_PHASE_REVEAL;
188  }
189 }
190 
191 /* Add the given <b>commit</b> to <b>state</b>. It MUST be a valid commit
192  * and there shouldn't be a commit from the same authority in the state
193  * already else verification hasn't been done prior. This takes ownership of
194  * the commit once in our state. */
195 static void
196 commit_add_to_state(sr_commit_t *commit, sr_state_t *state)
197 {
198  sr_commit_t *saved_commit;
199 
200  tor_assert(commit);
201  tor_assert(state);
202 
203  saved_commit = digestmap_set(state->commits, commit->rsa_identity,
204  commit);
205  if (saved_commit != NULL) {
206  /* This means we already have that commit in our state so adding twice
207  * the same commit is either a code flow error, a corrupted disk state
208  * or some new unknown issue. */
209  log_warn(LD_DIR, "SR: Commit from %s exists in our state while "
210  "adding it: '%s'", sr_commit_get_rsa_fpr(commit),
211  commit->encoded_commit);
212  sr_commit_free(saved_commit);
213  }
214 }
215 
216 /* Helper: deallocate a commit object. (Used with digestmap_free(), which
217  * requires a function pointer whose argument is void *). */
218 static void
219 commit_free_(void *p)
220 {
221  sr_commit_free_(p);
222 }
223 
224 #define state_free(val) \
225  FREE_AND_NULL(sr_state_t, state_free_, (val))
226 
227 /* Free a state that was allocated with state_new(). */
228 static void
229 state_free_(sr_state_t *state)
230 {
231  if (state == NULL) {
232  return;
233  }
234  tor_free(state->fname);
235  digestmap_free(state->commits, commit_free_);
236  tor_free(state->current_srv);
237  tor_free(state->previous_srv);
238  tor_free(state);
239 }
240 
241 /* Allocate an sr_state_t object and returns it. If no <b>fname</b>, the
242  * default file name is used. This function does NOT initialize the state
243  * timestamp, phase or shared random value. NULL is never returned. */
244 static sr_state_t *
245 state_new(const char *fname, time_t now)
246 {
247  sr_state_t *new_state = tor_malloc_zero(sizeof(*new_state));
248  /* If file name is not provided, use default. */
249  if (fname == NULL) {
250  fname = default_fname;
251  }
252  new_state->fname = tor_strdup(fname);
253  new_state->version = SR_PROTO_VERSION;
254  new_state->commits = digestmap_new();
255  new_state->phase = get_sr_protocol_phase(now);
256  new_state->valid_until = get_state_valid_until_time(now);
257  return new_state;
258 }
259 
260 /* Set our global state pointer with the one given. */
261 static void
262 state_set(sr_state_t *state)
263 {
264  tor_assert(state);
265  if (sr_state != NULL) {
266  state_free(sr_state);
267  }
268  sr_state = state;
269 }
270 
271 #define disk_state_free(val) \
272  FREE_AND_NULL(sr_disk_state_t, disk_state_free_, (val))
273 
274 /* Free an allocated disk state. */
275 static void
276 disk_state_free_(sr_disk_state_t *state)
277 {
278  if (state == NULL) {
279  return;
280  }
281  config_free(get_srs_mgr(), state);
282 }
283 
284 /* Allocate a new disk state, initialize it and return it. */
285 static sr_disk_state_t *
286 disk_state_new(time_t now)
287 {
288  sr_disk_state_t *new_state = config_new(get_srs_mgr());
289 
290  new_state->Version = SR_PROTO_VERSION;
291  new_state->TorVersion = tor_strdup(get_version());
292  new_state->ValidUntil = get_state_valid_until_time(now);
293  new_state->ValidAfter = now;
294 
295  /* Init config format. */
296  config_init(get_srs_mgr(), new_state);
297  return new_state;
298 }
299 
300 /* Set our global disk state with the given state. */
301 static void
302 disk_state_set(sr_disk_state_t *state)
303 {
304  tor_assert(state);
305  if (sr_disk_state != NULL) {
306  disk_state_free(sr_disk_state);
307  }
308  sr_disk_state = state;
309 }
310 
311 /* Return -1 if the disk state is invalid (something in there that we can't or
312  * shouldn't use). Return 0 if everything checks out. */
313 static int
314 disk_state_validate(const sr_disk_state_t *state)
315 {
316  time_t now;
317 
318  tor_assert(state);
319 
320  /* Do we support the protocol version in the state or is it 0 meaning
321  * Version wasn't found in the state file or bad anyway ? */
322  if (state->Version == 0 || state->Version > SR_PROTO_VERSION) {
323  goto invalid;
324  }
325 
326  /* If the valid until time is before now, we shouldn't use that state. */
327  now = time(NULL);
328  if (state->ValidUntil < now) {
329  log_info(LD_DIR, "SR: Disk state has expired. Ignoring it.");
330  goto invalid;
331  }
332 
333  /* Make sure we don't have a valid after time that is earlier than a valid
334  * until time which would make things not work well. */
335  if (state->ValidAfter >= state->ValidUntil) {
336  log_info(LD_DIR, "SR: Disk state valid after/until times are invalid.");
337  goto invalid;
338  }
339 
340  return 0;
341 
342  invalid:
343  return -1;
344 }
345 
346 /* Validate the disk state (NOP for now). */
347 static int
348 disk_state_validate_cb(void *old_state, void *state, void *default_state,
349  int from_setconf, char **msg)
350 {
351  /* We don't use these; only options do. */
352  (void) from_setconf;
353  (void) default_state;
354  (void) old_state;
355 
356  /* This is called by config_dump which is just before we are about to
357  * write it to disk. At that point, our global memory state has been
358  * copied to the disk state so it's fair to assume it's trustable. */
359  (void) state;
360  (void) msg;
361  return 0;
362 }
363 
364 /* Parse the Commit line(s) in the disk state and translate them to the
365  * the memory state. Return 0 on success else -1 on error. */
366 static int
367 disk_state_parse_commits(sr_state_t *state,
368  const sr_disk_state_t *disk_state)
369 {
370  config_line_t *line;
371  smartlist_t *args = NULL;
372 
373  tor_assert(state);
374  tor_assert(disk_state);
375 
376  for (line = disk_state->Commit; line; line = line->next) {
377  sr_commit_t *commit = NULL;
378 
379  /* Extra safety. */
380  if (strcasecmp(line->key, dstate_commit_key) ||
381  line->value == NULL) {
382  /* Ignore any lines that are not commits. */
384  continue;
385  }
386  args = smartlist_new();
387  smartlist_split_string(args, line->value, " ",
388  SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
389  if (smartlist_len(args) < 3) {
390  log_warn(LD_BUG, "SR: Too few arguments in Commit Line: %s",
391  escaped(line->value));
392  goto error;
393  }
394  commit = sr_parse_commit(args);
395  if (commit == NULL) {
396  /* Ignore badly formed commit. It could also be a authority
397  * fingerprint that we don't know about so it shouldn't be used. */
398  smartlist_free(args);
399  continue;
400  }
401  /* We consider parseable commit from our disk state to be valid because
402  * they need to be in the first place to get in there. */
403  commit->valid = 1;
404  /* Add commit to our state pointer. */
405  commit_add_to_state(commit, state);
406 
407  SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
408  smartlist_free(args);
409  }
410 
411  return 0;
412 
413  error:
414  SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
415  smartlist_free(args);
416  return -1;
417 }
418 
419 /* Parse a share random value line from the disk state and save it to dst
420  * which is an allocated srv object. Return 0 on success else -1. */
421 static int
422 disk_state_parse_srv(const char *value, sr_srv_t *dst)
423 {
424  int ret = -1;
425  smartlist_t *args;
426  sr_srv_t *srv;
427 
428  tor_assert(value);
429  tor_assert(dst);
430 
431  args = smartlist_new();
432  smartlist_split_string(args, value, " ",
433  SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
434  if (smartlist_len(args) < 2) {
435  log_warn(LD_BUG, "SR: Too few arguments in shared random value. "
436  "Line: %s", escaped(value));
437  goto error;
438  }
439  srv = sr_parse_srv(args);
440  if (srv == NULL) {
441  goto error;
442  }
443  dst->num_reveals = srv->num_reveals;
444  memcpy(dst->value, srv->value, sizeof(dst->value));
445  tor_free(srv);
446  ret = 0;
447 
448  error:
449  SMARTLIST_FOREACH(args, char *, s, tor_free(s));
450  smartlist_free(args);
451  return ret;
452 }
453 
454 /* Parse both SharedRandCurrentValue and SharedRandPreviousValue line from
455  * the state. Return 0 on success else -1. */
456 static int
457 disk_state_parse_sr_values(sr_state_t *state,
458  const sr_disk_state_t *disk_state)
459 {
460  /* Only one value per type (current or previous) is allowed so we keep
461  * track of it with these flag. */
462  unsigned int seen_previous = 0, seen_current = 0;
463  config_line_t *line;
464  sr_srv_t *srv = NULL;
465 
466  tor_assert(state);
467  tor_assert(disk_state);
468 
469  for (line = disk_state->SharedRandValues; line; line = line->next) {
470  if (line->value == NULL) {
471  continue;
472  }
473  srv = tor_malloc_zero(sizeof(*srv));
474  if (disk_state_parse_srv(line->value, srv) < 0) {
475  log_warn(LD_BUG, "SR: Broken current SRV line in state %s",
476  escaped(line->value));
477  goto bad;
478  }
479  if (!strcasecmp(line->key, dstate_prev_srv_key)) {
480  if (seen_previous) {
481  log_warn(LD_DIR, "SR: Second previous SRV value seen. Bad state");
482  goto bad;
483  }
484  state->previous_srv = srv;
485  seen_previous = 1;
486  } else if (!strcasecmp(line->key, dstate_cur_srv_key)) {
487  if (seen_current) {
488  log_warn(LD_DIR, "SR: Second current SRV value seen. Bad state");
489  goto bad;
490  }
491  state->current_srv = srv;
492  seen_current = 1;
493  } else {
494  /* Unknown key. Ignoring. */
495  tor_free(srv);
496  }
497  }
498 
499  return 0;
500  bad:
501  tor_free(srv);
502  return -1;
503 }
504 
505 /* Parse the given disk state and set a newly allocated state. On success,
506  * return that state else NULL. */
507 static sr_state_t *
508 disk_state_parse(const sr_disk_state_t *new_disk_state)
509 {
510  sr_state_t *new_state = state_new(default_fname, time(NULL));
511 
512  tor_assert(new_disk_state);
513 
514  new_state->version = new_disk_state->Version;
515  new_state->valid_until = new_disk_state->ValidUntil;
516  new_state->valid_after = new_disk_state->ValidAfter;
517 
518  /* Set our current phase according to the valid-after time in our disk
519  * state. The disk state we are parsing contains everything for the phase
520  * starting at valid_after so make sure our phase reflects that. */
521  new_state->phase = get_sr_protocol_phase(new_state->valid_after);
522 
523  /* Parse the shared random values. */
524  if (disk_state_parse_sr_values(new_state, new_disk_state) < 0) {
525  goto error;
526  }
527  /* Parse the commits. */
528  if (disk_state_parse_commits(new_state, new_disk_state) < 0) {
529  goto error;
530  }
531  /* Great! This new state contains everything we had on disk. */
532  return new_state;
533 
534  error:
535  state_free(new_state);
536  return NULL;
537 }
538 
539 /* From a valid commit object and an allocated config line, set the line's
540  * value to the state string representation of a commit. */
541 static void
542 disk_state_put_commit_line(const sr_commit_t *commit, config_line_t *line)
543 {
544  char *reveal_str = NULL;
545 
546  tor_assert(commit);
547  tor_assert(line);
548 
549  if (!fast_mem_is_zero(commit->encoded_reveal,
550  sizeof(commit->encoded_reveal))) {
551  /* Add extra whitespace so we can format the line correctly. */
552  tor_asprintf(&reveal_str, " %s", commit->encoded_reveal);
553  }
554  tor_asprintf(&line->value, "%u %s %s %s%s",
555  SR_PROTO_VERSION,
557  sr_commit_get_rsa_fpr(commit),
558  commit->encoded_commit,
559  reveal_str != NULL ? reveal_str : "");
560  if (reveal_str != NULL) {
561  memwipe(reveal_str, 0, strlen(reveal_str));
562  tor_free(reveal_str);
563  }
564 }
565 
566 /* From a valid srv object and an allocated config line, set the line's
567  * value to the state string representation of a shared random value. */
568 static void
569 disk_state_put_srv_line(const sr_srv_t *srv, config_line_t *line)
570 {
571  char encoded[SR_SRV_VALUE_BASE64_LEN + 1];
572 
573  tor_assert(line);
574 
575  /* No SRV value thus don't add the line. This is possible since we might
576  * not have a current or previous SRV value in our state. */
577  if (srv == NULL) {
578  return;
579  }
580  sr_srv_encode(encoded, sizeof(encoded), srv);
581  tor_asprintf(&line->value, "%" PRIu64 " %s", srv->num_reveals, encoded);
582 }
583 
584 /* Reset disk state that is free allocated memory and zeroed the object. */
585 static void
586 disk_state_reset(void)
587 {
588  /* Free allocated memory */
589  config_free_lines(sr_disk_state->Commit);
590  config_free_lines(sr_disk_state->SharedRandValues);
591  config_free_lines(sr_disk_state->ExtraLines);
592  tor_free(sr_disk_state->TorVersion);
593 
594  /* Clear other fields. */
595  sr_disk_state->ValidAfter = 0;
596  sr_disk_state->ValidUntil = 0;
597  sr_disk_state->Version = 0;
598 
599  /* Reset it with useful data */
600  sr_disk_state->TorVersion = tor_strdup(get_version());
601 }
602 
603 /* Update our disk state based on our global SR state. */
604 static void
605 disk_state_update(void)
606 {
607  config_line_t **next, *line;
608 
609  if (BUG(!sr_disk_state))
610  return;
611  if (BUG(!sr_state))
612  return;
613 
614  /* Reset current disk state. */
615  disk_state_reset();
616 
617  /* First, update elements that we don't need to do a construction. */
618  sr_disk_state->Version = sr_state->version;
619  sr_disk_state->ValidUntil = sr_state->valid_until;
620  sr_disk_state->ValidAfter = sr_state->valid_after;
621 
622  /* Shared random values. */
623  next = &sr_disk_state->SharedRandValues;
624  if (sr_state->previous_srv != NULL) {
625  *next = line = tor_malloc_zero(sizeof(config_line_t));
626  line->key = tor_strdup(dstate_prev_srv_key);
627  disk_state_put_srv_line(sr_state->previous_srv, line);
628  /* Go to the next shared random value. */
629  next = &(line->next);
630  }
631  if (sr_state->current_srv != NULL) {
632  *next = line = tor_malloc_zero(sizeof(*line));
633  line->key = tor_strdup(dstate_cur_srv_key);
634  disk_state_put_srv_line(sr_state->current_srv, line);
635  }
636 
637  /* Parse the commits and construct config line(s). */
638  next = &sr_disk_state->Commit;
639  DIGESTMAP_FOREACH(sr_state->commits, key, sr_commit_t *, commit) {
640  *next = line = tor_malloc_zero(sizeof(*line));
641  line->key = tor_strdup(dstate_commit_key);
642  disk_state_put_commit_line(commit, line);
643  next = &(line->next);
645 }
646 
647 /* Load state from disk and put it into our disk state. If the state passes
648  * validation, our global state will be updated with it. Return 0 on
649  * success. On error, -EINVAL is returned if the state on disk did contained
650  * something malformed or is unreadable. -ENOENT is returned indicating that
651  * the state file is either empty of non existing. */
652 static int
653 disk_state_load_from_disk(void)
654 {
655  int ret;
656  char *fname;
657 
658  fname = get_datadir_fname(default_fname);
659  ret = disk_state_load_from_disk_impl(fname);
660  tor_free(fname);
661 
662  return ret;
663 }
664 
665 /* Helper for disk_state_load_from_disk(). */
666 STATIC int
667 disk_state_load_from_disk_impl(const char *fname)
668 {
669  int ret;
670  char *content = NULL;
671  sr_state_t *parsed_state = NULL;
672  sr_disk_state_t *disk_state = NULL;
673 
674  /* Read content of file so we can parse it. */
675  if ((content = read_file_to_str(fname, 0, NULL)) == NULL) {
676  log_warn(LD_FS, "SR: Unable to read SR state file %s",
677  escaped(fname));
678  ret = -errno;
679  goto error;
680  }
681 
682  {
683  config_line_t *lines = NULL;
684  char *errmsg = NULL;
685 
686  /* Every error in this code path will return EINVAL. */
687  ret = -EINVAL;
688  if (config_get_lines(content, &lines, 0) < 0) {
689  config_free_lines(lines);
690  goto error;
691  }
692 
693  disk_state = disk_state_new(time(NULL));
694  config_assign(get_srs_mgr(), disk_state, lines, 0, &errmsg);
695  config_free_lines(lines);
696  if (errmsg) {
697  log_warn(LD_DIR, "SR: Reading state error: %s", errmsg);
698  tor_free(errmsg);
699  goto error;
700  }
701  }
702 
703  /* So far so good, we've loaded our state file into our disk state. Let's
704  * validate it and then parse it. */
705  if (disk_state_validate(disk_state) < 0) {
706  ret = -EINVAL;
707  goto error;
708  }
709 
710  parsed_state = disk_state_parse(disk_state);
711  if (parsed_state == NULL) {
712  ret = -EINVAL;
713  goto error;
714  }
715  state_set(parsed_state);
716  disk_state_set(disk_state);
717  tor_free(content);
718  log_info(LD_DIR, "SR: State loaded successfully from file %s", fname);
719  return 0;
720 
721  error:
722  disk_state_free(disk_state);
723  tor_free(content);
724  return ret;
725 }
726 
727 /* Save the disk state to disk but before that update it from the current
728  * state so we always have the latest. Return 0 on success else -1. */
729 static int
730 disk_state_save_to_disk(void)
731 {
732  int ret;
733  char *state, *content = NULL, *fname = NULL;
734  char tbuf[ISO_TIME_LEN + 1];
735  time_t now = time(NULL);
736 
737  /* If we didn't have the opportunity to setup an internal disk state,
738  * don't bother saving something to disk. */
739  if (sr_disk_state == NULL) {
740  ret = 0;
741  goto done;
742  }
743 
744  /* Make sure that our disk state is up to date with our memory state
745  * before saving it to disk. */
746  disk_state_update();
747  state = config_dump(get_srs_mgr(), NULL, sr_disk_state, 0, 0);
748  format_local_iso_time(tbuf, now);
749  tor_asprintf(&content,
750  "# Tor shared random state file last generated on %s "
751  "local time\n"
752  "# Other times below are in UTC\n"
753  "# Please *do not* edit this file.\n\n%s",
754  tbuf, state);
755  tor_free(state);
756  fname = get_datadir_fname(default_fname);
757  if (write_str_to_file(fname, content, 0) < 0) {
758  log_warn(LD_FS, "SR: Unable to write SR state to file %s", fname);
759  ret = -1;
760  goto done;
761  }
762  ret = 0;
763  log_debug(LD_DIR, "SR: Saved state to file %s", fname);
764 
765  done:
766  tor_free(fname);
767  tor_free(content);
768  return ret;
769 }
770 
771 /* Reset our state to prepare for a new protocol run. Once this returns, all
772  * commits in the state will be removed and freed. */
773 STATIC void
774 reset_state_for_new_protocol_run(time_t valid_after)
775 {
776  if (BUG(!sr_state))
777  return;
778 
779  /* Keep counters in track */
780  sr_state->n_reveal_rounds = 0;
781  sr_state->n_commit_rounds = 0;
782  sr_state->n_protocol_runs++;
783 
784  /* Reset valid-until */
785  sr_state->valid_until = get_state_valid_until_time(valid_after);
786  sr_state->valid_after = valid_after;
787 
788  /* We are in a new protocol run so cleanup commits. */
789  sr_state_delete_commits();
790 }
791 
792 /* This is the first round of the new protocol run starting at
793  * <b>valid_after</b>. Do the necessary housekeeping. */
794 STATIC void
795 new_protocol_run(time_t valid_after)
796 {
797  sr_commit_t *our_commitment = NULL;
798 
799  /* Only compute the srv at the end of the reveal phase. */
800  if (sr_state->phase == SR_PHASE_REVEAL) {
801  /* We are about to compute a new shared random value that will be set in
802  * our state as the current value so rotate values. */
803  state_rotate_srv();
804  /* Compute the shared randomness value of the day. */
805  sr_compute_srv();
806  }
807 
808  /* Prepare for the new protocol run by reseting the state */
809  reset_state_for_new_protocol_run(valid_after);
810 
811  /* Do some logging */
812  log_info(LD_DIR, "SR: Protocol run #%" PRIu64 " starting!",
813  sr_state->n_protocol_runs);
814 
815  /* Generate fresh commitments for this protocol run */
816  our_commitment = sr_generate_our_commit(valid_after,
817  get_my_v3_authority_cert());
818  if (our_commitment) {
819  /* Add our commitment to our state. In case we are unable to create one
820  * (highly unlikely), we won't vote for this protocol run since our
821  * commitment won't be in our state. */
822  sr_state_add_commit(our_commitment);
823  }
824 }
825 
826 /* Return 1 iff the <b>next_phase</b> is a phase transition from the current
827  * phase that is it's different. */
828 STATIC int
829 is_phase_transition(sr_phase_t next_phase)
830 {
831  return sr_state->phase != next_phase;
832 }
833 
834 /* Helper function: return a commit using the RSA fingerprint of the
835  * authority or NULL if no such commit is known. */
836 static sr_commit_t *
837 state_query_get_commit(const char *rsa_fpr)
838 {
839  tor_assert(rsa_fpr);
840  return digestmap_get(sr_state->commits, rsa_fpr);
841 }
842 
843 /* Helper function: This handles the GET state action using an
844  * <b>obj_type</b> and <b>data</b> needed for the action. */
845 static void *
846 state_query_get_(sr_state_object_t obj_type, const void *data)
847 {
848  if (BUG(!sr_state))
849  return NULL;
850 
851  void *obj = NULL;
852 
853  switch (obj_type) {
854  case SR_STATE_OBJ_COMMIT:
855  {
856  obj = state_query_get_commit(data);
857  break;
858  }
859  case SR_STATE_OBJ_COMMITS:
860  obj = sr_state->commits;
861  break;
862  case SR_STATE_OBJ_CURSRV:
863  obj = sr_state->current_srv;
864  break;
865  case SR_STATE_OBJ_PREVSRV:
866  obj = sr_state->previous_srv;
867  break;
868  case SR_STATE_OBJ_PHASE:
869  obj = &sr_state->phase;
870  break;
871  case SR_STATE_OBJ_VALID_AFTER:
872  default:
873  tor_assert(0);
874  }
875  return obj;
876 }
877 
878 /* Helper function: This handles the PUT state action using an
879  * <b>obj_type</b> and <b>data</b> needed for the action.
880  * PUT frees the previous data before replacing it, if needed. */
881 static void
882 state_query_put_(sr_state_object_t obj_type, void *data)
883 {
884  if (BUG(!sr_state))
885  return;
886 
887  switch (obj_type) {
888  case SR_STATE_OBJ_COMMIT:
889  {
890  sr_commit_t *commit = data;
891  tor_assert(commit);
892  /* commit_add_to_state() frees the old commit, if there is one */
893  commit_add_to_state(commit, sr_state);
894  break;
895  }
896  case SR_STATE_OBJ_CURSRV:
897  /* Check if the new pointer is the same as the old one: if it is, it's
898  * probably a bug. The caller may have confused current and previous,
899  * or they may have forgotten to sr_srv_dup().
900  * Putting NULL multiple times is allowed. */
901  if (!BUG(data && sr_state->current_srv == (sr_srv_t *) data)) {
902  /* We own the old SRV, so we need to free it. */
903  state_query_del_(SR_STATE_OBJ_CURSRV, NULL);
904  sr_state->current_srv = (sr_srv_t *) data;
905  }
906  break;
907  case SR_STATE_OBJ_PREVSRV:
908  /* Check if the new pointer is the same as the old one: if it is, it's
909  * probably a bug. The caller may have confused current and previous,
910  * or they may have forgotten to sr_srv_dup().
911  * Putting NULL multiple times is allowed. */
912  if (!BUG(data && sr_state->previous_srv == (sr_srv_t *) data)) {
913  /* We own the old SRV, so we need to free it. */
914  state_query_del_(SR_STATE_OBJ_PREVSRV, NULL);
915  sr_state->previous_srv = (sr_srv_t *) data;
916  }
917  break;
918  case SR_STATE_OBJ_VALID_AFTER:
919  sr_state->valid_after = *((time_t *) data);
920  break;
921  /* It's not allowed to change the phase nor the full commitments map from
922  * the state. The phase is decided during a strict process post voting and
923  * the commits should be put individually. */
924  case SR_STATE_OBJ_PHASE:
925  case SR_STATE_OBJ_COMMITS:
926  default:
927  tor_assert(0);
928  }
929 }
930 
931 /* Helper function: This handles the DEL_ALL state action using an
932  * <b>obj_type</b> and <b>data</b> needed for the action. */
933 static void
934 state_query_del_all_(sr_state_object_t obj_type)
935 {
936  if (BUG(!sr_state))
937  return;
938 
939  switch (obj_type) {
940  case SR_STATE_OBJ_COMMIT:
941  {
942  /* We are in a new protocol run so cleanup commitments. */
943  DIGESTMAP_FOREACH_MODIFY(sr_state->commits, key, sr_commit_t *, c) {
944  sr_commit_free(c);
945  MAP_DEL_CURRENT(key);
947  break;
948  }
949  /* The following objects are _NOT_ supposed to be removed. */
950  case SR_STATE_OBJ_CURSRV:
951  case SR_STATE_OBJ_PREVSRV:
952  case SR_STATE_OBJ_PHASE:
953  case SR_STATE_OBJ_COMMITS:
954  case SR_STATE_OBJ_VALID_AFTER:
955  default:
956  tor_assert(0);
957  }
958 }
959 
960 /* Helper function: This handles the DEL state action using an
961  * <b>obj_type</b> and <b>data</b> needed for the action. */
962 static void
963 state_query_del_(sr_state_object_t obj_type, void *data)
964 {
965  (void) data;
966 
967  if (BUG(!sr_state))
968  return;
969 
970  switch (obj_type) {
971  case SR_STATE_OBJ_PREVSRV:
972  tor_free(sr_state->previous_srv);
973  break;
974  case SR_STATE_OBJ_CURSRV:
975  tor_free(sr_state->current_srv);
976  break;
977  case SR_STATE_OBJ_COMMIT:
978  case SR_STATE_OBJ_COMMITS:
979  case SR_STATE_OBJ_PHASE:
980  case SR_STATE_OBJ_VALID_AFTER:
981  default:
982  tor_assert(0);
983  }
984 }
985 
986 /* Query state using an <b>action</b> for an object type <b>obj_type</b>.
987  * The <b>data</b> pointer needs to point to an object that the action needs
988  * to use and if anything is required to be returned, it is stored in
989  * <b>out</b>.
990  *
991  * This mechanism exists so we have one single point where we synchronized
992  * our memory state with our disk state for every actions that changes it.
993  * We then trigger a write on disk immediately.
994  *
995  * This should be the only entry point to our memory state. It's used by all
996  * our state accessors and should be in the future. */
997 static void
998 state_query(sr_state_action_t action, sr_state_object_t obj_type,
999  void *data, void **out)
1000 {
1001  switch (action) {
1002  case SR_STATE_ACTION_GET:
1003  *out = state_query_get_(obj_type, data);
1004  break;
1005  case SR_STATE_ACTION_PUT:
1006  state_query_put_(obj_type, data);
1007  break;
1008  case SR_STATE_ACTION_DEL:
1009  state_query_del_(obj_type, data);
1010  break;
1011  case SR_STATE_ACTION_DEL_ALL:
1012  state_query_del_all_(obj_type);
1013  break;
1014  case SR_STATE_ACTION_SAVE:
1015  /* Only trigger a disk state save. */
1016  break;
1017  default:
1018  tor_assert(0);
1019  }
1020 
1021  /* If the action actually changes the state, immediately save it to disk.
1022  * The following will sync the state -> disk state and then save it. */
1023  if (action != SR_STATE_ACTION_GET) {
1024  disk_state_save_to_disk();
1025  }
1026 }
1027 
1028 /* Delete the current SRV value from the state freeing it and the value is set
1029  * to NULL meaning empty. */
1030 STATIC void
1031 state_del_current_srv(void)
1032 {
1033  state_query(SR_STATE_ACTION_DEL, SR_STATE_OBJ_CURSRV, NULL, NULL);
1034 }
1035 
1036 /* Delete the previous SRV value from the state freeing it and the value is
1037  * set to NULL meaning empty. */
1038 STATIC void
1039 state_del_previous_srv(void)
1040 {
1041  state_query(SR_STATE_ACTION_DEL, SR_STATE_OBJ_PREVSRV, NULL, NULL);
1042 }
1043 
1044 /* Rotate SRV value by setting the previous SRV to the current SRV, and
1045  * clearing the current SRV. */
1046 STATIC void
1047 state_rotate_srv(void)
1048 {
1049  /* First delete previous SRV from the state. Object will be freed. */
1050  state_del_previous_srv();
1051  /* Set previous SRV to a copy of the current one. */
1052  sr_state_set_previous_srv(sr_srv_dup(sr_state_get_current_srv()));
1053  /* Free and NULL the current srv. */
1054  sr_state_set_current_srv(NULL);
1055 }
1056 
1057 /* Set valid after time in the our state. */
1058 void
1059 sr_state_set_valid_after(time_t valid_after)
1060 {
1061  state_query(SR_STATE_ACTION_PUT, SR_STATE_OBJ_VALID_AFTER,
1062  (void *) &valid_after, NULL);
1063 }
1064 
1065 /* Return the phase we are currently in according to our state. */
1066 sr_phase_t
1067 sr_state_get_phase(void)
1068 {
1069  void *ptr;
1070  state_query(SR_STATE_ACTION_GET, SR_STATE_OBJ_PHASE, NULL, &ptr);
1071  return *(sr_phase_t *) ptr;
1072 }
1073 
1074 /* Return the previous SRV value from our state. Value CAN be NULL.
1075  * The state object owns the SRV, so the calling code should not free the SRV.
1076  * Use sr_srv_dup() if you want to keep a copy of the SRV. */
1077 const sr_srv_t *
1078 sr_state_get_previous_srv(void)
1079 {
1080  const sr_srv_t *srv;
1081  state_query(SR_STATE_ACTION_GET, SR_STATE_OBJ_PREVSRV, NULL,
1082  (void *) &srv);
1083  return srv;
1084 }
1085 
1086 /* Set the current SRV value from our state. Value CAN be NULL. The srv
1087  * object ownership is transferred to the state object. */
1088 void
1089 sr_state_set_previous_srv(const sr_srv_t *srv)
1090 {
1091  state_query(SR_STATE_ACTION_PUT, SR_STATE_OBJ_PREVSRV, (void *) srv,
1092  NULL);
1093 }
1094 
1095 /* Return the current SRV value from our state. Value CAN be NULL.
1096  * The state object owns the SRV, so the calling code should not free the SRV.
1097  * Use sr_srv_dup() if you want to keep a copy of the SRV. */
1098 const sr_srv_t *
1099 sr_state_get_current_srv(void)
1100 {
1101  const sr_srv_t *srv;
1102  state_query(SR_STATE_ACTION_GET, SR_STATE_OBJ_CURSRV, NULL,
1103  (void *) &srv);
1104  return srv;
1105 }
1106 
1107 /* Set the current SRV value from our state. Value CAN be NULL. The srv
1108  * object ownership is transferred to the state object. */
1109 void
1110 sr_state_set_current_srv(const sr_srv_t *srv)
1111 {
1112  state_query(SR_STATE_ACTION_PUT, SR_STATE_OBJ_CURSRV, (void *) srv,
1113  NULL);
1114 }
1115 
1116 /* Clean all the SRVs in our state. */
1117 void
1118 sr_state_clean_srvs(void)
1119 {
1120  /* Remove SRVs from state. They will be set to NULL as "empty". */
1121  state_del_previous_srv();
1122  state_del_current_srv();
1123 }
1124 
1125 /* Return a pointer to the commits map from our state. CANNOT be NULL. */
1126 digestmap_t *
1127 sr_state_get_commits(void)
1128 {
1129  digestmap_t *commits;
1130  state_query(SR_STATE_ACTION_GET, SR_STATE_OBJ_COMMITS,
1131  NULL, (void *) &commits);
1132  tor_assert(commits);
1133  return commits;
1134 }
1135 
1136 /* Update the current SR state as needed for the upcoming voting round at
1137  * <b>valid_after</b>. */
1138 void
1139 sr_state_update(time_t valid_after)
1140 {
1141  sr_phase_t next_phase;
1142 
1143  if (BUG(!sr_state))
1144  return;
1145 
1146  /* Don't call this function twice in the same voting period. */
1147  if (valid_after <= sr_state->valid_after) {
1148  log_info(LD_DIR, "SR: Asked to update state twice. Ignoring.");
1149  return;
1150  }
1151 
1152  /* Get phase of upcoming round. */
1153  next_phase = get_sr_protocol_phase(valid_after);
1154 
1155  /* If we are transitioning to a new protocol phase, prepare the stage. */
1156  if (is_phase_transition(next_phase)) {
1157  if (next_phase == SR_PHASE_COMMIT) {
1158  /* Going into commit phase means we are starting a new protocol run. */
1159  new_protocol_run(valid_after);
1160  }
1161  /* Set the new phase for this round */
1162  sr_state->phase = next_phase;
1163  } else if (sr_state->phase == SR_PHASE_COMMIT &&
1164  digestmap_size(sr_state->commits) == 0) {
1165  /* We are _NOT_ in a transition phase so if we are in the commit phase
1166  * and have no commit, generate one. Chances are that we are booting up
1167  * so let's have a commit in our state for the next voting period. */
1168  sr_commit_t *our_commit =
1169  sr_generate_our_commit(valid_after, get_my_v3_authority_cert());
1170  if (our_commit) {
1171  /* Add our commitment to our state. In case we are unable to create one
1172  * (highly unlikely), we won't vote for this protocol run since our
1173  * commitment won't be in our state. */
1174  sr_state_add_commit(our_commit);
1175  }
1176  }
1177 
1178  sr_state_set_valid_after(valid_after);
1179 
1180  /* Count the current round */
1181  if (sr_state->phase == SR_PHASE_COMMIT) {
1182  /* invariant check: we've not entered reveal phase yet */
1183  if (BUG(sr_state->n_reveal_rounds != 0))
1184  return;
1185  sr_state->n_commit_rounds++;
1186  } else {
1187  sr_state->n_reveal_rounds++;
1188  }
1189 
1190  { /* Debugging. */
1191  char tbuf[ISO_TIME_LEN + 1];
1192  format_iso_time(tbuf, valid_after);
1193  log_info(LD_DIR, "SR: State prepared for upcoming voting period (%s). "
1194  "Upcoming phase is %s (counters: %d commit & %d reveal rounds).",
1195  tbuf, get_phase_str(sr_state->phase),
1196  sr_state->n_commit_rounds, sr_state->n_reveal_rounds);
1197  }
1198 }
1199 
1200 /* Return commit object from the given authority digest <b>rsa_identity</b>.
1201  * Return NULL if not found. */
1202 sr_commit_t *
1203 sr_state_get_commit(const char *rsa_identity)
1204 {
1205  sr_commit_t *commit;
1206 
1207  tor_assert(rsa_identity);
1208 
1209  state_query(SR_STATE_ACTION_GET, SR_STATE_OBJ_COMMIT,
1210  (void *) rsa_identity, (void *) &commit);
1211  return commit;
1212 }
1213 
1214 /* Add <b>commit</b> to the permanent state. The commit object ownership is
1215  * transferred to the state so the caller MUST not free it. */
1216 void
1217 sr_state_add_commit(sr_commit_t *commit)
1218 {
1219  tor_assert(commit);
1220 
1221  /* Put the commit to the global state. */
1222  state_query(SR_STATE_ACTION_PUT, SR_STATE_OBJ_COMMIT,
1223  (void *) commit, NULL);
1224 
1225  log_debug(LD_DIR, "SR: Commit from %s has been added to our state.",
1226  sr_commit_get_rsa_fpr(commit));
1227 }
1228 
1229 /* Remove all commits from our state. */
1230 void
1231 sr_state_delete_commits(void)
1232 {
1233  state_query(SR_STATE_ACTION_DEL_ALL, SR_STATE_OBJ_COMMIT, NULL, NULL);
1234 }
1235 
1236 /* Copy the reveal information from <b>commit</b> into <b>saved_commit</b>.
1237  * This <b>saved_commit</b> MUST come from our current SR state. Once modified,
1238  * the disk state is updated. */
1239 void
1240 sr_state_copy_reveal_info(sr_commit_t *saved_commit, const sr_commit_t *commit)
1241 {
1242  tor_assert(saved_commit);
1243  tor_assert(commit);
1244 
1245  saved_commit->reveal_ts = commit->reveal_ts;
1246  memcpy(saved_commit->random_number, commit->random_number,
1247  sizeof(saved_commit->random_number));
1248 
1249  strlcpy(saved_commit->encoded_reveal, commit->encoded_reveal,
1250  sizeof(saved_commit->encoded_reveal));
1251  state_query(SR_STATE_ACTION_SAVE, 0, NULL, NULL);
1252  log_debug(LD_DIR, "SR: Reveal value learned %s (for commit %s) from %s",
1253  saved_commit->encoded_reveal, saved_commit->encoded_commit,
1254  sr_commit_get_rsa_fpr(saved_commit));
1255 }
1256 
1257 /* Set the fresh SRV flag from our state. This doesn't need to trigger a
1258  * disk state synchronization so we directly change the state. */
1259 void
1260 sr_state_set_fresh_srv(void)
1261 {
1262  sr_state->is_srv_fresh = 1;
1263 }
1264 
1265 /* Unset the fresh SRV flag from our state. This doesn't need to trigger a
1266  * disk state synchronization so we directly change the state. */
1267 void
1268 sr_state_unset_fresh_srv(void)
1269 {
1270  sr_state->is_srv_fresh = 0;
1271 }
1272 
1273 /* Return the value of the fresh SRV flag. */
1274 unsigned int
1275 sr_state_srv_is_fresh(void)
1276 {
1277  return sr_state->is_srv_fresh;
1278 }
1279 
1280 /* Cleanup and free our disk and memory state. */
1281 void
1282 sr_state_free_all(void)
1283 {
1284  state_free(sr_state);
1285  disk_state_free(sr_disk_state);
1286  /* Nullify our global state. */
1287  sr_state = NULL;
1288  sr_disk_state = NULL;
1289  config_mgr_free(shared_random_state_mgr);
1290 }
1291 
1292 /* Save our current state in memory to disk. */
1293 void
1294 sr_state_save(void)
1295 {
1296  /* Query a SAVE action on our current state so it's synced and saved. */
1297  state_query(SR_STATE_ACTION_SAVE, 0, NULL, NULL);
1298 }
1299 
1300 /* Return 1 iff the state has been initialized that is it exists in memory.
1301  * Return 0 otherwise. */
1302 int
1303 sr_state_is_initialized(void)
1304 {
1305  return sr_state == NULL ? 0 : 1;
1306 }
1307 
1308 /* Initialize the disk and memory state.
1309  *
1310  * If save_to_disk is set to 1, the state is immediately saved to disk after
1311  * creation else it's not thus only kept in memory.
1312  * If read_from_disk is set to 1, we try to load the state from the disk and
1313  * if not found, a new state is created.
1314  *
1315  * Return 0 on success else a negative value on error. */
1316 int
1317 sr_state_init(int save_to_disk, int read_from_disk)
1318 {
1319  int ret = -ENOENT;
1320  time_t now = time(NULL);
1321 
1322  /* We shouldn't have those assigned. */
1323  tor_assert(sr_disk_state == NULL);
1324  tor_assert(sr_state == NULL);
1325 
1326  /* First, try to load the state from disk. */
1327  if (read_from_disk) {
1328  ret = disk_state_load_from_disk();
1329  }
1330 
1331  if (ret < 0) {
1332  switch (-ret) {
1333  case EINVAL:
1334  /* We have a state on disk but it contains something we couldn't parse
1335  * or an invalid entry in the state file. Let's remove it since it's
1336  * obviously unusable and replace it by an new fresh state below. */
1337  case ENOENT:
1338  {
1339  /* No state on disk so allocate our states for the first time. */
1340  sr_state_t *new_state = state_new(default_fname, now);
1341  sr_disk_state_t *new_disk_state = disk_state_new(now);
1342  state_set(new_state);
1343  /* It's important to set our disk state pointer since the save call
1344  * below uses it to synchronized it with our memory state. */
1345  disk_state_set(new_disk_state);
1346  /* No entry, let's save our new state to disk. */
1347  if (save_to_disk && disk_state_save_to_disk() < 0) {
1348  goto error;
1349  }
1350  break;
1351  }
1352  default:
1353  /* Big problem. Not possible. */
1354  tor_assert(0);
1355  }
1356  }
1357  /* We have a state in memory, let's make sure it's updated for the current
1358  * and next voting round. */
1359  {
1360  time_t valid_after = voting_schedule_get_next_valid_after_time();
1361  sr_state_update(valid_after);
1362  }
1363  return 0;
1364 
1365  error:
1366  return -1;
1367 }
1368 
1369 #ifdef TOR_UNIT_TESTS
1370 
1371 /* Set the current phase of the protocol. Used only by unit tests. */
1372 void
1373 set_sr_phase(sr_phase_t phase)
1374 {
1375  if (BUG(!sr_state))
1376  return;
1377  sr_state->phase = phase;
1378 }
1379 
1380 /* Get the SR state. Used only by unit tests */
1381 sr_state_t *
1382 get_sr_state(void)
1383 {
1384  return sr_state;
1385 }
1386 
1387 #endif /* defined(TOR_UNIT_TESTS) */
Header for confline.c.
void config_mgr_freeze(config_mgr_t *mgr)
Definition: confparse.c:280
void config_init(const config_mgr_t *mgr, void *options)
Definition: confparse.c:1134
void format_local_iso_time(char *buf, time_t t)
Definition: time_fmt.c:285
Header file for config.c.
config_mgr_t * config_mgr_new(const config_format_t *toplevel_fmt)
Definition: confparse.c:145
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
#define END_OF_CONFIG_VARS
Definition: confmacros.h:21
#define MAP_DEL_CURRENT(keyvar)
Definition: map.h:139
#define tor_free(p)
Definition: malloc.h:52
#define tor_fragile_assert()
Definition: util_bug.h:241
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:57
DUMMY_TYPECHECK_INSTANCE(sr_disk_state_t)
static const config_format_t state_format
Definition: statefile.c:160
int config_get_lines(const char *string, config_line_t **result, int extended)
Definition: confline.c:201
#define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar)
Definition: map.h:153
Common functions for cryptographic routines.
tor_assert(buffer)
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
Master header file for Tor-specific functionality.
Header file for shared_random_client.c.
char * config_dump(const config_mgr_t *mgr, const void *default_options, const void *options, int minimal, int comment_defaults)
Definition: confparse.c:1150
const char * name
Definition: conftypes.h:87
static const config_mgr_t * get_srs_mgr(void)
Header file for voting_schedule.c.
void * config_new(const config_mgr_t *mgr)
Definition: confparse.c:371
#define DIGESTMAP_FOREACH_END
Definition: map.h:167
#define LD_FS
Definition: log.h:68
#define LD_DIR
Definition: log.h:86
const char * crypto_digest_algorithm_get_name(digest_algorithm_t alg)
Definition: crypto_digest.c:44
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
Header file for dirvote.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
int config_assign(const config_mgr_t *mgr, void *options, config_line_t *list, unsigned config_assign_flags, char **msg)
Definition: confparse.c:913
Header file for router.c.
const char * escaped(const char *s)
Definition: escape.c:126
#define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar)
Definition: map.h:164
static struct_member_t state_extra_var
Definition: statefile.c:153
#define VAR(varname, conftype, member, initvalue)
Definition: config.c:261
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:84
Header for confparse.c.
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)