11 #define SHARED_RANDOM_STATE_PRIVATE
33 static const char *
phase_str[] = {
"unknown",
"commit",
"reveal" };
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";
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)
60 #define SR_DISK_STATE_MAGIC 0x98AB1254
65 V(Version, POSINT,
"0"),
66 V(TorVersion, STRING, NULL),
67 V(ValidAfter, ISOTIME, NULL),
68 V(ValidUntil, ISOTIME, NULL),
70 V(Commit, LINELIST, NULL),
72 V(SharedRandValues, LINELIST_V, NULL),
73 VAR(
"SharedRandPreviousValue",LINELIST_S, SharedRandValues, NULL),
74 VAR(
"SharedRandCurrentValue", LINELIST_S, SharedRandValues, NULL),
119 const char *the_string = NULL;
138 int total_rounds = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
139 int current_round, voting_interval, rounds_left;
140 time_t valid_until, beginning_of_current_round;
147 current_round = (now / voting_interval) % total_rounds;
148 rounds_left = total_rounds - current_round;
153 valid_until = beginning_of_current_round + (rounds_left * voting_interval);
156 char tbuf[ISO_TIME_LEN + 1];
158 log_debug(
LD_DIR,
"SR: Valid until time for state set to %s.", tbuf);
170 int total_periods = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
177 if (current_slot < SHARED_RANDOM_N_ROUNDS) {
198 if (saved_commit != NULL) {
202 log_warn(
LD_DIR,
"SR: Commit from %s exists in our state while "
203 "adding it: '%s'", sr_commit_get_rsa_fpr(commit),
204 commit->encoded_commit);
205 sr_commit_free(saved_commit);
217 #define state_free(val) \
218 FREE_AND_NULL(sr_state_t, state_free_, (val))
240 sr_state_t *new_state = tor_malloc_zero(
sizeof(*new_state));
245 new_state->
fname = tor_strdup(fname);
247 new_state->
commits = digestmap_new();
264 #define disk_state_free(val) \
265 FREE_AND_NULL(sr_disk_state_t, disk_state_free_, (val))
322 log_info(
LD_DIR,
"SR: Disk state has expired. Ignoring it.");
329 log_info(
LD_DIR,
"SR: Disk state valid after/until times are invalid.");
351 for (line = disk_state->
Commit; line; line = line->next) {
355 if (strcasecmp(line->key, dstate_commit_key) ||
356 line->value == NULL) {
363 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
364 if (smartlist_len(args) < 3) {
365 log_warn(
LD_BUG,
"SR: Too few arguments in Commit Line: %s",
370 if (commit == NULL) {
373 smartlist_free(args);
383 smartlist_free(args);
390 smartlist_free(args);
408 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
409 if (smartlist_len(args) < 2) {
410 log_warn(
LD_BUG,
"SR: Too few arguments in shared random value. "
425 smartlist_free(args);
437 unsigned int seen_previous = 0, seen_current = 0;
445 if (line->value == NULL) {
448 srv = tor_malloc_zero(
sizeof(*srv));
450 log_warn(
LD_BUG,
"SR: Broken current SRV line in state %s",
454 if (!strcasecmp(line->key, dstate_prev_srv_key)) {
456 log_warn(
LD_DIR,
"SR: Second previous SRV value seen. Bad state");
461 }
else if (!strcasecmp(line->key, dstate_cur_srv_key)) {
463 log_warn(
LD_DIR,
"SR: Second current SRV value seen. Bad state");
510 state_free(new_state);
519 char *reveal_str = NULL;
532 sr_commit_get_rsa_fpr(commit),
533 commit->encoded_commit,
534 reveal_str != NULL ? reveal_str :
"");
535 if (reveal_str != NULL) {
536 memwipe(reveal_str, 0, strlen(reveal_str));
601 line->key = tor_strdup(dstate_prev_srv_key);
604 next = &(line->next);
607 *next = line = tor_malloc_zero(
sizeof(*line));
608 line->key = tor_strdup(dstate_cur_srv_key);
615 *next = line = tor_malloc_zero(
sizeof(*line));
616 line->key = tor_strdup(dstate_commit_key);
618 next = &(line->next);
645 char *content = NULL;
650 if ((content = read_file_to_str(fname, 0, NULL)) == NULL) {
651 log_warn(
LD_FS,
"SR: Unable to read SR state file %s",
664 config_free_lines(lines);
670 config_free_lines(lines);
672 log_warn(
LD_DIR,
"SR: Reading state error: %s", errmsg);
686 if (parsed_state == NULL) {
693 log_info(
LD_DIR,
"SR: State loaded successfully from file %s", fname);
697 disk_state_free(disk_state);
708 char *state, *content = NULL, *fname = NULL;
709 char tbuf[ISO_TIME_LEN + 1];
710 time_t now = time(NULL);
725 "# Tor shared random state file last generated on %s "
727 "# Other times below are in UTC\n"
728 "# Please *do not* edit this file.\n\n%s",
733 log_warn(
LD_FS,
"SR: Unable to write SR state to file %s", fname);
738 log_debug(
LD_DIR,
"SR: Saved state to file %s", fname);
787 log_info(
LD_DIR,
"SR: Protocol run #%" PRIu64
" starting!",
793 if (our_commitment) {
974 void *data,
void **out)
977 case SR_STATE_ACTION_GET:
980 case SR_STATE_ACTION_PUT:
983 case SR_STATE_ACTION_DEL:
986 case SR_STATE_ACTION_DEL_ALL:
989 case SR_STATE_ACTION_SAVE:
998 if (action != SR_STATE_ACTION_GET) {
1037 (
void *) &valid_after, NULL);
1105 digestmap_t *commits;
1107 NULL, (
void *) &commits);
1123 if (valid_after <= sr_state->valid_after) {
1124 log_info(
LD_DIR,
"SR: Asked to update state twice. Ignoring.");
1167 char tbuf[ISO_TIME_LEN + 1];
1169 log_info(
LD_DIR,
"SR: State prepared for upcoming voting period (%s). "
1170 "Upcoming phase is %s (counters: %d commit & %d reveal rounds).",
1186 (
void *) rsa_identity, (
void *) &commit);
1199 (
void *) commit, NULL);
1201 log_debug(
LD_DIR,
"SR: Commit from %s has been added to our state.",
1202 sr_commit_get_rsa_fpr(commit));
1228 log_debug(
LD_DIR,
"SR: Reveal value learned %s (for commit %s) from %s",
1230 sr_commit_get_rsa_fpr(saved_commit));
1296 time_t now = time(NULL);
1303 if (read_from_disk) {
1345 #ifdef TOR_UNIT_TESTS
#define VAR(varname, conftype, member, initvalue)
Header file for config.c.
int config_get_lines(const char *string, config_line_t **result, int extended)
#define END_OF_CONFIG_VARS
void config_init(const config_mgr_t *mgr, void *options)
void config_mgr_freeze(config_mgr_t *mgr)
char * config_dump(const config_mgr_t *mgr, const void *default_options, const void *options, int minimal, int comment_defaults)
void * config_new(const config_mgr_t *mgr)
config_mgr_t * config_mgr_new(const config_format_t *toplevel_fmt)
int config_assign(const config_mgr_t *mgr, void *options, config_line_t *list, unsigned config_assign_flags, char **msg)
const char * crypto_digest_algorithm_get_name(digest_algorithm_t alg)
void memwipe(void *mem, uint8_t byte, size_t sz)
Common functions for cryptographic routines.
Header file for dirvote.c.
const char * escaped(const char *s)
int write_str_to_file(const char *fname, const char *str, int bin)
#define MAP_DEL_CURRENT(keyvar)
#define DIGESTMAP_FOREACH_END
#define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar)
#define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar)
Header file for networkstatus.c.
Master header file for Tor-specific functionality.
The or_state_t structure, which represents Tor's state file.
int tor_asprintf(char **strp, const char *fmt,...)
authority_cert_t * get_my_v3_authority_cert(void)
Header file for router.c.
sr_commit_t * sr_generate_our_commit(time_t timestamp, const authority_cert_t *my_rsa_cert)
sr_commit_t * sr_parse_commit(const smartlist_t *args)
sr_srv_t * sr_srv_dup(const sr_srv_t *orig)
void sr_compute_srv(void)
void sr_commit_free_(sr_commit_t *commit)
This file contains ABI/API of the shared random protocol defined in proposal #250....
#define SR_SRV_VALUE_BASE64_LEN
sr_srv_t * sr_parse_srv(const smartlist_t *args)
int get_voting_interval(void)
void sr_srv_encode(char *dst, size_t dst_len, const sr_srv_t *srv)
Header file for shared_random_client.c.
static void state_query_del_(sr_state_object_t obj_type, void *data)
void sr_state_update(time_t valid_after)
void sr_state_set_fresh_srv(void)
const sr_srv_t * sr_state_get_current_srv(void)
static int disk_state_validate(const sr_disk_state_t *state)
static const config_format_t state_format
static void disk_state_put_srv_line(const sr_srv_t *srv, config_line_t *line)
static sr_disk_state_t * disk_state_new(time_t now)
sr_commit_t * sr_state_get_commit(const char *rsa_identity)
static int disk_state_load_from_disk(void)
void sr_state_free_all(void)
void sr_state_copy_reveal_info(sr_commit_t *saved_commit, const sr_commit_t *commit)
STATIC sr_phase_t get_sr_protocol_phase(time_t valid_after)
void sr_state_set_previous_srv(const sr_srv_t *srv)
STATIC int disk_state_load_from_disk_impl(const char *fname)
const sr_srv_t * sr_state_get_previous_srv(void)
static void state_query_del_all_(sr_state_object_t obj_type)
void sr_state_set_valid_after(time_t valid_after)
STATIC void reset_state_for_new_protocol_run(time_t valid_after)
DUMMY_TYPECHECK_INSTANCE(sr_disk_state_t)
#define SR_DISK_STATE_MAGIC
static const config_var_t state_vars[]
STATIC void state_rotate_srv(void)
static void disk_state_free_(sr_disk_state_t *state)
static void commit_free_(void *p)
static void disk_state_reset(void)
static void state_query_put_(sr_state_object_t obj_type, void *data)
STATIC int is_phase_transition(sr_phase_t next_phase)
static void commit_add_to_state(sr_commit_t *commit, sr_state_t *state)
void sr_state_clean_srvs(void)
static int disk_state_save_to_disk(void)
void sr_state_set_current_srv(const sr_srv_t *srv)
digestmap_t * sr_state_get_commits(void)
int sr_state_init(int save_to_disk, int read_from_disk)
static sr_disk_state_t * sr_disk_state
void sr_state_unset_fresh_srv(void)
static sr_state_t * sr_state
static int disk_state_parse_srv(const char *value, sr_srv_t *dst)
static void state_free_(sr_state_t *state)
void sr_state_delete_commits(void)
static const char * phase_str[]
static void state_query(sr_state_action_t action, sr_state_object_t obj_type, void *data, void **out)
STATIC void state_del_previous_srv(void)
static void disk_state_update(void)
static sr_commit_t * state_query_get_commit(const char *rsa_fpr)
static const config_mgr_t * get_srs_mgr(void)
int sr_state_is_initialized(void)
static const char default_fname[]
STATIC void new_protocol_run(time_t valid_after)
static sr_state_t * disk_state_parse(const sr_disk_state_t *new_disk_state)
static void * state_query_get_(sr_state_object_t obj_type, const void *data)
STATIC void state_del_current_srv(void)
static void disk_state_set(sr_disk_state_t *state)
static const struct_member_t state_extra_var
STATIC const char * get_phase_str(sr_phase_t phase)
unsigned int sr_state_srv_is_fresh(void)
static void disk_state_put_commit_line(const sr_commit_t *commit, config_line_t *line)
STATIC time_t get_state_valid_until_time(time_t now)
static void state_set(sr_state_t *state)
static sr_state_t * state_new(const char *fname, time_t now)
static config_mgr_t * shared_random_state_mgr
static int disk_state_parse_sr_values(sr_state_t *state, const sr_disk_state_t *disk_state)
void sr_state_add_commit(sr_commit_t *commit)
static int disk_state_parse_commits(sr_state_t *state, const sr_disk_state_t *disk_state)
sr_phase_t sr_state_get_phase(void)
Header for shared_random_state.c.
@ SR_STATE_OBJ_VALID_AFTER
smartlist_t * smartlist_new(void)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)
char rsa_identity[DIGEST_LEN]
char encoded_reveal[SR_REVEAL_BASE64_LEN+1]
uint8_t random_number[SR_RANDOM_NUMBER_LEN]
struct config_line_t * Commit
struct config_line_t * SharedRandValues
struct config_line_t * ExtraLines
uint8_t value[DIGEST256_LEN]
unsigned int n_reveal_rounds
unsigned int is_srv_fresh
unsigned int n_commit_rounds
void format_iso_time(char *buf, time_t t)
void format_local_iso_time(char *buf, time_t t)
const char * get_version(void)
#define tor_fragile_assert()
int fast_mem_is_zero(const char *mem, size_t len)
time_t dirauth_sched_get_cur_valid_after_time(void)
time_t dirauth_sched_get_next_valid_after_time(void)
Header file for voting_schedule.c.