shared_random_client.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Copyright (c) 2018-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file shared_random_client.c
  5. * \brief This file contains functions that are from the shared random
  6. * subsystem but used by many part of tor. The full feature is built
  7. * as part of the dirauth module.
  8. **/
  9. #define SHARED_RANDOM_CLIENT_PRIVATE
  10. #include "feature/hs_common/shared_random_client.h"
  11. #include "app/config/config.h"
  12. #include "feature/dircommon/voting_schedule.h"
  13. #include "feature/nodelist/networkstatus.h"
  14. #include "lib/encoding/binascii.h"
  15. #include "feature/nodelist/networkstatus_st.h"
  16. /* Convert a given srv object to a string for the control port. This doesn't
  17. * fail and the srv object MUST be valid. */
  18. static char *
  19. srv_to_control_string(const sr_srv_t *srv)
  20. {
  21. char *srv_str;
  22. char srv_hash_encoded[SR_SRV_VALUE_BASE64_LEN + 1];
  23. tor_assert(srv);
  24. sr_srv_encode(srv_hash_encoded, sizeof(srv_hash_encoded), srv);
  25. tor_asprintf(&srv_str, "%s", srv_hash_encoded);
  26. return srv_str;
  27. }
  28. /* Return the voting interval of the tor vote subsystem. */
  29. int
  30. get_voting_interval(void)
  31. {
  32. int interval;
  33. networkstatus_t *consensus = networkstatus_get_live_consensus(time(NULL));
  34. if (consensus) {
  35. interval = (int)(consensus->fresh_until - consensus->valid_after);
  36. } else {
  37. /* Same for both a testing and real network. We voluntarily ignore the
  38. * InitialVotingInterval since it complexifies things and it doesn't
  39. * affect the SR protocol. */
  40. interval = get_options()->V3AuthVotingInterval;
  41. }
  42. tor_assert(interval > 0);
  43. return interval;
  44. }
  45. /* Given the time <b>now</b>, return the start time of the current round of
  46. * the SR protocol. For example, if it's 23:47:08, the current round thus
  47. * started at 23:47:00 for a voting interval of 10 seconds. */
  48. time_t
  49. get_start_time_of_current_round(void)
  50. {
  51. const or_options_t *options = get_options();
  52. int voting_interval = get_voting_interval();
  53. /* First, get the start time of the next round */
  54. time_t next_start = voting_schedule_get_next_valid_after_time();
  55. /* Now roll back next_start by a voting interval to find the start time of
  56. the current round. */
  57. time_t curr_start = voting_schedule_get_start_of_next_interval(
  58. next_start - voting_interval - 1,
  59. voting_interval,
  60. options->TestingV3AuthVotingStartOffset);
  61. return curr_start;
  62. }
  63. /*
  64. * Public API
  65. */
  66. /* Encode the given shared random value and put it in dst. Destination
  67. * buffer must be at least SR_SRV_VALUE_BASE64_LEN plus the NULL byte. */
  68. void
  69. sr_srv_encode(char *dst, size_t dst_len, const sr_srv_t *srv)
  70. {
  71. int ret;
  72. /* Extra byte for the NULL terminated char. */
  73. char buf[SR_SRV_VALUE_BASE64_LEN + 1];
  74. tor_assert(dst);
  75. tor_assert(srv);
  76. tor_assert(dst_len >= sizeof(buf));
  77. ret = base64_encode(buf, sizeof(buf), (const char *) srv->value,
  78. sizeof(srv->value), 0);
  79. /* Always expect the full length without the NULL byte. */
  80. tor_assert(ret == (sizeof(buf) - 1));
  81. tor_assert(ret <= (int) dst_len);
  82. strlcpy(dst, buf, dst_len);
  83. }
  84. /* Return the current SRV string representation for the control port. Return a
  85. * newly allocated string on success containing the value else "" if not found
  86. * or if we don't have a valid consensus yet. */
  87. char *
  88. sr_get_current_for_control(void)
  89. {
  90. char *srv_str;
  91. const networkstatus_t *c = networkstatus_get_latest_consensus();
  92. if (c && c->sr_info.current_srv) {
  93. srv_str = srv_to_control_string(c->sr_info.current_srv);
  94. } else {
  95. srv_str = tor_strdup("");
  96. }
  97. return srv_str;
  98. }
  99. /* Return the previous SRV string representation for the control port. Return
  100. * a newly allocated string on success containing the value else "" if not
  101. * found or if we don't have a valid consensus yet. */
  102. char *
  103. sr_get_previous_for_control(void)
  104. {
  105. char *srv_str;
  106. const networkstatus_t *c = networkstatus_get_latest_consensus();
  107. if (c && c->sr_info.previous_srv) {
  108. srv_str = srv_to_control_string(c->sr_info.previous_srv);
  109. } else {
  110. srv_str = tor_strdup("");
  111. }
  112. return srv_str;
  113. }
  114. /* Return current shared random value from the latest consensus. Caller can
  115. * NOT keep a reference to the returned pointer. Return NULL if none. */
  116. const sr_srv_t *
  117. sr_get_current(const networkstatus_t *ns)
  118. {
  119. const networkstatus_t *consensus;
  120. /* Use provided ns else get a live one */
  121. if (ns) {
  122. consensus = ns;
  123. } else {
  124. consensus = networkstatus_get_live_consensus(approx_time());
  125. }
  126. /* Ideally we would never be asked for an SRV without a live consensus. Make
  127. * sure this assumption is correct. */
  128. tor_assert_nonfatal(consensus);
  129. if (consensus) {
  130. return consensus->sr_info.current_srv;
  131. }
  132. return NULL;
  133. }
  134. /* Return previous shared random value from the latest consensus. Caller can
  135. * NOT keep a reference to the returned pointer. Return NULL if none. */
  136. const sr_srv_t *
  137. sr_get_previous(const networkstatus_t *ns)
  138. {
  139. const networkstatus_t *consensus;
  140. /* Use provided ns else get a live one */
  141. if (ns) {
  142. consensus = ns;
  143. } else {
  144. consensus = networkstatus_get_live_consensus(approx_time());
  145. }
  146. /* Ideally we would never be asked for an SRV without a live consensus. Make
  147. * sure this assumption is correct. */
  148. tor_assert_nonfatal(consensus);
  149. if (consensus) {
  150. return consensus->sr_info.previous_srv;
  151. }
  152. return NULL;
  153. }
  154. /* Parse a list of arguments from a SRV value either from a vote, consensus
  155. * or from our disk state and return a newly allocated srv object. NULL is
  156. * returned on error.
  157. *
  158. * The arguments' order:
  159. * num_reveals, value
  160. */
  161. sr_srv_t *
  162. sr_parse_srv(const smartlist_t *args)
  163. {
  164. char *value;
  165. int ok, ret;
  166. uint64_t num_reveals;
  167. sr_srv_t *srv = NULL;
  168. tor_assert(args);
  169. if (smartlist_len(args) < 2) {
  170. goto end;
  171. }
  172. /* First argument is the number of reveal values */
  173. num_reveals = tor_parse_uint64(smartlist_get(args, 0),
  174. 10, 0, UINT64_MAX, &ok, NULL);
  175. if (!ok) {
  176. goto end;
  177. }
  178. /* Second and last argument is the shared random value it self. */
  179. value = smartlist_get(args, 1);
  180. if (strlen(value) != SR_SRV_VALUE_BASE64_LEN) {
  181. goto end;
  182. }
  183. srv = tor_malloc_zero(sizeof(*srv));
  184. srv->num_reveals = num_reveals;
  185. /* We subtract one byte from the srclen because the function ignores the
  186. * '=' character in the given buffer. This is broken but it's a documented
  187. * behavior of the implementation. */
  188. ret = base64_decode((char *) srv->value, sizeof(srv->value), value,
  189. SR_SRV_VALUE_BASE64_LEN - 1);
  190. if (ret != sizeof(srv->value)) {
  191. tor_free(srv);
  192. srv = NULL;
  193. goto end;
  194. }
  195. end:
  196. return srv;
  197. }
  198. /** Return the start time of the current SR protocol run. For example, if the
  199. * time is 23/06/2017 23:47:08 and a full SR protocol run is 24 hours, this
  200. * function should return 23/06/2017 00:00:00. */
  201. time_t
  202. sr_state_get_start_time_of_current_protocol_run(time_t now)
  203. {
  204. int total_rounds = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
  205. int voting_interval = get_voting_interval();
  206. /* Find the time the current round started. */
  207. time_t beginning_of_current_round = get_start_time_of_current_round();
  208. /* Get current SR protocol round */
  209. int current_round = (now / voting_interval) % total_rounds;
  210. /* Get start time by subtracting the time elapsed from the beginning of the
  211. protocol run */
  212. time_t time_elapsed_since_start_of_run = current_round * voting_interval;
  213. return beginning_of_current_round - time_elapsed_since_start_of_run;
  214. }
  215. /** Return the time (in seconds) it takes to complete a full SR protocol phase
  216. * (e.g. the commit phase). */
  217. unsigned int
  218. sr_state_get_phase_duration(void)
  219. {
  220. return SHARED_RANDOM_N_ROUNDS * get_voting_interval();
  221. }
  222. /** Return the time (in seconds) it takes to complete a full SR protocol run */
  223. unsigned int
  224. sr_state_get_protocol_run_duration(void)
  225. {
  226. int total_protocol_rounds = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
  227. return total_protocol_rounds * get_voting_interval();
  228. }