dirvote_common.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (c) 2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file dirvote_common.c
  5. * \brief This file contains functions that are from the directory authority
  6. * subsystem related to voting specifically but used by many part of
  7. * tor. The full feature is built as part of the dirauth module.
  8. **/
  9. #define DIRVOTE_COMMON_PRIVATE
  10. #include "dirvote_common.h"
  11. #include "or.h"
  12. #include "config.h"
  13. #include "networkstatus.h"
  14. /* =====
  15. * Vote scheduling
  16. * ===== */
  17. /** Set *<b>timing_out</b> to the intervals at which we would like to vote.
  18. * Note that these aren't the intervals we'll use to vote; they're the ones
  19. * that we'll vote to use. */
  20. void
  21. dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
  22. {
  23. const or_options_t *options = get_options();
  24. tor_assert(timing_out);
  25. timing_out->vote_interval = options->V3AuthVotingInterval;
  26. timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
  27. timing_out->vote_delay = options->V3AuthVoteDelay;
  28. timing_out->dist_delay = options->V3AuthDistDelay;
  29. }
  30. /** Return the start of the next interval of size <b>interval</b> (in
  31. * seconds) after <b>now</b>, plus <b>offset</b>. Midnight always
  32. * starts a fresh interval, and if the last interval of a day would be
  33. * truncated to less than half its size, it is rolled into the
  34. * previous interval. */
  35. time_t
  36. dirvote_get_start_of_next_interval(time_t now, int interval, int offset)
  37. {
  38. struct tm tm;
  39. time_t midnight_today=0;
  40. time_t midnight_tomorrow;
  41. time_t next;
  42. tor_gmtime_r(&now, &tm);
  43. tm.tm_hour = 0;
  44. tm.tm_min = 0;
  45. tm.tm_sec = 0;
  46. if (tor_timegm(&tm, &midnight_today) < 0) {
  47. log_warn(LD_BUG, "Ran into an invalid time when trying to find midnight.");
  48. }
  49. midnight_tomorrow = midnight_today + (24*60*60);
  50. next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
  51. /* Intervals never cross midnight. */
  52. if (next > midnight_tomorrow)
  53. next = midnight_tomorrow;
  54. /* If the interval would only last half as long as it's supposed to, then
  55. * skip over to the next day. */
  56. if (next + interval/2 > midnight_tomorrow)
  57. next = midnight_tomorrow;
  58. next += offset;
  59. if (next - interval > now)
  60. next -= interval;
  61. return next;
  62. }
  63. /* Populate and return a new voting_schedule_t that can be used to schedule
  64. * voting. The object is allocated on the heap and it's the responsibility of
  65. * the caller to free it. Can't fail. */
  66. static voting_schedule_t *
  67. get_voting_schedule(const or_options_t *options, time_t now, int severity)
  68. {
  69. int interval, vote_delay, dist_delay;
  70. time_t start;
  71. time_t end;
  72. networkstatus_t *consensus;
  73. voting_schedule_t *new_voting_schedule;
  74. new_voting_schedule = tor_malloc_zero(sizeof(voting_schedule_t));
  75. consensus = networkstatus_get_live_consensus(now);
  76. if (consensus) {
  77. interval = (int)( consensus->fresh_until - consensus->valid_after );
  78. vote_delay = consensus->vote_seconds;
  79. dist_delay = consensus->dist_seconds;
  80. } else {
  81. interval = options->TestingV3AuthInitialVotingInterval;
  82. vote_delay = options->TestingV3AuthInitialVoteDelay;
  83. dist_delay = options->TestingV3AuthInitialDistDelay;
  84. }
  85. tor_assert(interval > 0);
  86. if (vote_delay + dist_delay > interval/2)
  87. vote_delay = dist_delay = interval / 4;
  88. start = new_voting_schedule->interval_starts =
  89. dirvote_get_start_of_next_interval(now,interval,
  90. options->TestingV3AuthVotingStartOffset);
  91. end = dirvote_get_start_of_next_interval(start+1, interval,
  92. options->TestingV3AuthVotingStartOffset);
  93. tor_assert(end > start);
  94. new_voting_schedule->fetch_missing_signatures = start - (dist_delay/2);
  95. new_voting_schedule->voting_ends = start - dist_delay;
  96. new_voting_schedule->fetch_missing_votes =
  97. start - dist_delay - (vote_delay/2);
  98. new_voting_schedule->voting_starts = start - dist_delay - vote_delay;
  99. {
  100. char tbuf[ISO_TIME_LEN+1];
  101. format_iso_time(tbuf, new_voting_schedule->interval_starts);
  102. tor_log(severity, LD_DIR,"Choosing expected valid-after time as %s: "
  103. "consensus_set=%d, interval=%d",
  104. tbuf, consensus?1:0, interval);
  105. }
  106. return new_voting_schedule;
  107. }
  108. #define voting_schedule_free(s) \
  109. FREE_AND_NULL(voting_schedule_t, voting_schedule_free_, (s))
  110. /** Frees a voting_schedule_t. This should be used instead of the generic
  111. * tor_free. */
  112. static void
  113. voting_schedule_free_(voting_schedule_t *voting_schedule_to_free)
  114. {
  115. if (!voting_schedule_to_free)
  116. return;
  117. tor_free(voting_schedule_to_free);
  118. }
  119. voting_schedule_t voting_schedule;
  120. /* Using the time <b>now</b>, return the next voting valid-after time. */
  121. time_t
  122. dirvote_get_next_valid_after_time(void)
  123. {
  124. /* This is a safe guard in order to make sure that the voting schedule
  125. * static object is at least initialized. Using this function with a zeroed
  126. * voting schedule can lead to bugs. */
  127. if (tor_mem_is_zero((const char *) &voting_schedule,
  128. sizeof(voting_schedule))) {
  129. dirvote_recalculate_timing(get_options(), time(NULL));
  130. voting_schedule.created_on_demand = 1;
  131. }
  132. return voting_schedule.interval_starts;
  133. }
  134. /** Set voting_schedule to hold the timing for the next vote we should be
  135. * doing. All type of tor do that because HS subsystem needs the timing as
  136. * well to function properly. */
  137. void
  138. dirvote_recalculate_timing(const or_options_t *options, time_t now)
  139. {
  140. voting_schedule_t *new_voting_schedule;
  141. /* get the new voting schedule */
  142. new_voting_schedule = get_voting_schedule(options, now, LOG_INFO);
  143. tor_assert(new_voting_schedule);
  144. /* Fill in the global static struct now */
  145. memcpy(&voting_schedule, new_voting_schedule, sizeof(voting_schedule));
  146. voting_schedule_free(new_voting_schedule);
  147. }
  148. /** Return the signature made by <b>voter</b> using the algorithm
  149. * <b>alg</b>, or NULL if none is found. */
  150. document_signature_t *
  151. dirvote_get_voter_sig_by_alg(const networkstatus_voter_info_t *voter,
  152. digest_algorithm_t alg)
  153. {
  154. if (!voter->sigs)
  155. return NULL;
  156. SMARTLIST_FOREACH(voter->sigs, document_signature_t *, sig,
  157. if (sig->alg == alg)
  158. return sig);
  159. return NULL;
  160. }
  161. /* =====
  162. * Certificate functions
  163. * ===== */
  164. /** Allocate and return a new authority_cert_t with the same contents as
  165. * <b>cert</b>. */
  166. authority_cert_t *
  167. dirvote_authority_cert_dup(authority_cert_t *cert)
  168. {
  169. authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
  170. tor_assert(cert);
  171. memcpy(out, cert, sizeof(authority_cert_t));
  172. /* Now copy pointed-to things. */
  173. out->cache_info.signed_descriptor_body =
  174. tor_strndup(cert->cache_info.signed_descriptor_body,
  175. cert->cache_info.signed_descriptor_len);
  176. out->cache_info.saved_location = SAVED_NOWHERE;
  177. out->identity_key = crypto_pk_dup_key(cert->identity_key);
  178. out->signing_key = crypto_pk_dup_key(cert->signing_key);
  179. return out;
  180. }