dirvote.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2008, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char dirvote_c_id[] =
  7. "$Id$";
  8. #define DIRVOTE_PRIVATE
  9. #include "or.h"
  10. /**
  11. * \file dirvote.c
  12. * \brief Functions to compute directory consensus, and schedule voting.
  13. **/
  14. static int dirvote_add_signatures_to_pending_consensus(
  15. const char *detached_signatures_body,
  16. const char **msg_out);
  17. static char *list_v3_auth_ids(void);
  18. static void dirvote_fetch_missing_votes(void);
  19. static void dirvote_fetch_missing_signatures(void);
  20. static int dirvote_perform_vote(void);
  21. static void dirvote_clear_votes(int all_votes);
  22. static int dirvote_compute_consensus(void);
  23. static int dirvote_publish_consensus(void);
  24. /* =====
  25. * Voting
  26. * =====*/
  27. /** Return a new string containing the string representation of the vote in
  28. * <b>v3_ns</b>, signed with our v3 signing key <b>private_signing_key</b>.
  29. * For v3 authorities. */
  30. char *
  31. format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
  32. networkstatus_t *v3_ns)
  33. {
  34. size_t len;
  35. char *status = NULL;
  36. const char *client_versions = NULL, *server_versions = NULL;
  37. char *outp, *endp;
  38. char fingerprint[FINGERPRINT_LEN+1];
  39. char ipaddr[INET_NTOA_BUF_LEN];
  40. char digest[DIGEST_LEN];
  41. struct in_addr in;
  42. uint32_t addr;
  43. routerlist_t *rl = router_get_routerlist();
  44. char *version_lines = NULL;
  45. networkstatus_voter_info_t *voter;
  46. tor_assert(private_signing_key);
  47. voter = smartlist_get(v3_ns->voters, 0);
  48. addr = voter->addr;
  49. in.s_addr = htonl(addr);
  50. tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
  51. base16_encode(fingerprint, sizeof(fingerprint),
  52. v3_ns->cert->cache_info.identity_digest, DIGEST_LEN);
  53. client_versions = v3_ns->client_versions;
  54. server_versions = v3_ns->server_versions;
  55. if (client_versions || server_versions) {
  56. size_t v_len = 64;
  57. char *cp;
  58. if (client_versions)
  59. v_len += strlen(client_versions);
  60. if (server_versions)
  61. v_len += strlen(server_versions);
  62. version_lines = tor_malloc(v_len);
  63. cp = version_lines;
  64. if (client_versions) {
  65. tor_snprintf(cp, v_len-(cp-version_lines),
  66. "client-versions %s\n", client_versions);
  67. cp += strlen(cp);
  68. }
  69. if (server_versions)
  70. tor_snprintf(cp, v_len-(cp-version_lines),
  71. "server-versions %s\n", server_versions);
  72. } else {
  73. version_lines = tor_strdup("");
  74. }
  75. len = 8192;
  76. len += strlen(version_lines);
  77. len += (RS_ENTRY_LEN)*smartlist_len(rl->routers);
  78. len += v3_ns->cert->cache_info.signed_descriptor_len;
  79. status = tor_malloc(len);
  80. {
  81. char published[ISO_TIME_LEN+1];
  82. char va[ISO_TIME_LEN+1];
  83. char fu[ISO_TIME_LEN+1];
  84. char vu[ISO_TIME_LEN+1];
  85. char *flags = smartlist_join_strings(v3_ns->known_flags, " ", 0, NULL);
  86. authority_cert_t *cert = v3_ns->cert;
  87. format_iso_time(published, v3_ns->published);
  88. format_iso_time(va, v3_ns->valid_after);
  89. format_iso_time(fu, v3_ns->fresh_until);
  90. format_iso_time(vu, v3_ns->valid_until);
  91. tor_assert(cert);
  92. tor_snprintf(status, len,
  93. "network-status-version 3\n"
  94. "vote-status vote\n"
  95. "consensus-methods 1 2 3\n"
  96. "published %s\n"
  97. "valid-after %s\n"
  98. "fresh-until %s\n"
  99. "valid-until %s\n"
  100. "voting-delay %d %d\n"
  101. "%s" /* versions */
  102. "known-flags %s\n"
  103. "dir-source %s %s %s %s %d %d\n"
  104. "contact %s\n",
  105. published, va, fu, vu,
  106. v3_ns->vote_seconds, v3_ns->dist_seconds,
  107. version_lines,
  108. flags,
  109. voter->nickname, fingerprint, voter->address,
  110. ipaddr, voter->dir_port, voter->or_port, voter->contact);
  111. tor_free(flags);
  112. outp = status + strlen(status);
  113. endp = status + len;
  114. if (!tor_digest_is_zero(voter->legacy_id_digest)) {
  115. char fpbuf[HEX_DIGEST_LEN+1];
  116. base16_encode(fpbuf, sizeof(fpbuf), voter->legacy_id_digest, DIGEST_LEN);
  117. tor_snprintf(outp, endp-outp, "legacy-dir-key %s\n", fpbuf);
  118. outp += strlen(outp);
  119. }
  120. tor_assert(outp + cert->cache_info.signed_descriptor_len < endp);
  121. memcpy(outp, cert->cache_info.signed_descriptor_body,
  122. cert->cache_info.signed_descriptor_len);
  123. outp += cert->cache_info.signed_descriptor_len;
  124. }
  125. SMARTLIST_FOREACH(v3_ns->routerstatus_list, vote_routerstatus_t *, vrs,
  126. {
  127. if (routerstatus_format_entry(outp, endp-outp, &vrs->status,
  128. vrs->version, 0) < 0) {
  129. log_warn(LD_BUG, "Unable to print router status.");
  130. goto err;
  131. }
  132. outp += strlen(outp);
  133. });
  134. {
  135. char signing_key_fingerprint[FINGERPRINT_LEN+1];
  136. if (tor_snprintf(outp, endp-outp, "directory-signature ")<0) {
  137. log_warn(LD_BUG, "Unable to start signature line.");
  138. goto err;
  139. }
  140. outp += strlen(outp);
  141. if (crypto_pk_get_fingerprint(private_signing_key,
  142. signing_key_fingerprint, 0)<0) {
  143. log_warn(LD_BUG, "Unable to get fingerprint for signing key");
  144. goto err;
  145. }
  146. if (tor_snprintf(outp, endp-outp, "%s %s\n", fingerprint,
  147. signing_key_fingerprint)<0) {
  148. log_warn(LD_BUG, "Unable to end signature line.");
  149. goto err;
  150. }
  151. outp += strlen(outp);
  152. }
  153. if (router_get_networkstatus_v3_hash(status, digest)<0)
  154. goto err;
  155. note_crypto_pk_op(SIGN_DIR);
  156. if (router_append_dirobj_signature(outp,endp-outp,digest,
  157. private_signing_key)<0) {
  158. log_warn(LD_BUG, "Unable to sign networkstatus vote.");
  159. goto err;
  160. }
  161. {
  162. networkstatus_t *v;
  163. if (!(v = networkstatus_parse_vote_from_string(status, NULL, 1))) {
  164. log_err(LD_BUG,"Generated a networkstatus vote we couldn't parse: "
  165. "<<%s>>", status);
  166. goto err;
  167. }
  168. networkstatus_vote_free(v);
  169. }
  170. goto done;
  171. err:
  172. tor_free(status);
  173. done:
  174. tor_free(version_lines);
  175. return status;
  176. }
  177. /* =====
  178. * Consensus generation
  179. * ===== */
  180. /** Given a vote <b>vote</b> (not a consensus!), return its associated
  181. * networkstatus_voter_info_t. */
  182. static networkstatus_voter_info_t *
  183. get_voter(const networkstatus_t *vote)
  184. {
  185. tor_assert(vote);
  186. tor_assert(vote->is_vote);
  187. tor_assert(vote->voters);
  188. tor_assert(smartlist_len(vote->voters) == 1);
  189. return smartlist_get(vote->voters, 0);
  190. }
  191. typedef struct {
  192. networkstatus_t *v;
  193. const char *digest;
  194. int is_legacy;
  195. } dir_src_ent_t;
  196. /** Helper for sorting networkstatus_t votes (not consensuses) by the
  197. * hash of their voters' identity digests. */
  198. static int
  199. _compare_votes_by_authority_id(const void **_a, const void **_b)
  200. {
  201. const networkstatus_t *a = *_a, *b = *_b;
  202. return memcmp(get_voter(a)->identity_digest,
  203. get_voter(b)->identity_digest, DIGEST_LEN);
  204. }
  205. static int
  206. _compare_dir_src_ents_by_authority_id(const void **_a, const void **_b)
  207. {
  208. const dir_src_ent_t *a = *_a, *b = *_b;
  209. const networkstatus_voter_info_t *a_v = get_voter(a->v),
  210. *b_v = get_voter(b->v);
  211. const char *a_id, *b_id;
  212. a_id = a->is_legacy ? a_v->legacy_id_digest : a_v->identity_digest;
  213. b_id = b->is_legacy ? b_v->legacy_id_digest : b_v->identity_digest;
  214. return memcmp(a_id, b_id, DIGEST_LEN);
  215. }
  216. /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
  217. * that occurs more than <b>min</b> times. */
  218. static void
  219. get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
  220. {
  221. char *cur = NULL;
  222. int count = 0;
  223. SMARTLIST_FOREACH(in, char *, cp,
  224. {
  225. if (cur && !strcmp(cp, cur)) {
  226. ++count;
  227. } else {
  228. if (count > min)
  229. smartlist_add(out, cur);
  230. cur = cp;
  231. count = 1;
  232. }
  233. });
  234. if (count > min)
  235. smartlist_add(out, cur);
  236. }
  237. /** Given a sorted list of strings <b>lst</b>, return the member that appears
  238. * most. Break ties in favor of later-occurring members. */
  239. static const char *
  240. get_most_frequent_member(smartlist_t *lst)
  241. {
  242. const char *most_frequent = NULL;
  243. int most_frequent_count = 0;
  244. const char *cur = NULL;
  245. int count = 0;
  246. SMARTLIST_FOREACH(lst, const char *, s,
  247. {
  248. if (cur && !strcmp(s, cur)) {
  249. ++count;
  250. } else {
  251. if (count >= most_frequent_count) {
  252. most_frequent = cur;
  253. most_frequent_count = count;
  254. }
  255. cur = s;
  256. count = 1;
  257. }
  258. });
  259. if (count >= most_frequent_count) {
  260. most_frequent = cur;
  261. most_frequent_count = count;
  262. }
  263. return most_frequent;
  264. }
  265. /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
  266. * that come from the same routerinfo, with the same derived elements.
  267. */
  268. static int
  269. compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
  270. {
  271. int r;
  272. if ((r = memcmp(a->status.identity_digest, b->status.identity_digest,
  273. DIGEST_LEN)))
  274. return r;
  275. if ((r = memcmp(a->status.descriptor_digest, b->status.descriptor_digest,
  276. DIGEST_LEN)))
  277. return r;
  278. if ((r = (int)(b->status.published_on - a->status.published_on)))
  279. return r;
  280. if ((r = strcmp(b->status.nickname, a->status.nickname)))
  281. return r;
  282. if ((r = (((int)b->status.addr) - ((int)a->status.addr))))
  283. return r;
  284. if ((r = (((int)b->status.or_port) - ((int)a->status.or_port))))
  285. return r;
  286. if ((r = (((int)b->status.dir_port) - ((int)a->status.dir_port))))
  287. return r;
  288. return 0;
  289. }
  290. /** Helper for sorting routerlists based on compare_vote_rs. */
  291. static int
  292. _compare_vote_rs(const void **_a, const void **_b)
  293. {
  294. const vote_routerstatus_t *a = *_a, *b = *_b;
  295. return compare_vote_rs(a,b);
  296. }
  297. /** Given a list of vote_routerstatus_t, all for the same router identity,
  298. * return whichever is most frequent, breaking ties in favor of more
  299. * recently published vote_routerstatus_t.
  300. */
  301. static vote_routerstatus_t *
  302. compute_routerstatus_consensus(smartlist_t *votes)
  303. {
  304. vote_routerstatus_t *most = NULL, *cur = NULL;
  305. int most_n = 0, cur_n = 0;
  306. time_t most_published = 0;
  307. smartlist_sort(votes, _compare_vote_rs);
  308. SMARTLIST_FOREACH(votes, vote_routerstatus_t *, rs,
  309. {
  310. if (cur && !compare_vote_rs(cur, rs)) {
  311. ++cur_n;
  312. } else {
  313. if (cur_n > most_n ||
  314. (cur && cur_n == most_n &&
  315. cur->status.published_on > most_published)) {
  316. most = cur;
  317. most_n = cur_n;
  318. most_published = cur->status.published_on;
  319. }
  320. cur_n = 1;
  321. cur = rs;
  322. }
  323. });
  324. if (cur_n > most_n ||
  325. (cur && cur_n == most_n && cur->status.published_on > most_published)) {
  326. most = cur;
  327. most_n = cur_n;
  328. most_published = cur->status.published_on;
  329. }
  330. tor_assert(most);
  331. return most;
  332. }
  333. /** Given a list of strings in <b>lst</b>, set the DIGEST_LEN-byte digest at
  334. * <b>digest_out</b> to the hash of the concatenation of those strings. */
  335. static void
  336. hash_list_members(char *digest_out, smartlist_t *lst)
  337. {
  338. crypto_digest_env_t *d = crypto_new_digest_env();
  339. SMARTLIST_FOREACH(lst, const char *, cp,
  340. crypto_digest_add_bytes(d, cp, strlen(cp)));
  341. crypto_digest_get_digest(d, digest_out, DIGEST_LEN);
  342. crypto_free_digest_env(d);
  343. }
  344. /** Sorting helper: compare two strings based on their values as base-ten
  345. * positive integers. (Non-integers are treated as prior to all integers, and
  346. * compared lexically.) */
  347. static int
  348. _cmp_int_strings(const void **_a, const void **_b)
  349. {
  350. const char *a = *_a, *b = *_b;
  351. int ai = (int)tor_parse_long(a, 10, 1, INT_MAX, NULL, NULL);
  352. int bi = (int)tor_parse_long(b, 10, 1, INT_MAX, NULL, NULL);
  353. if (ai<bi) {
  354. return -1;
  355. } else if (ai==bi) {
  356. if (ai == 0) /* Parsing failed. */
  357. return strcmp(a, b);
  358. return 0;
  359. } else {
  360. return 1;
  361. }
  362. }
  363. /** Given a list of networkstatus_t votes, determine and return the number of
  364. * the highest consensus method that is supported by 2/3 of the voters. */
  365. static int
  366. compute_consensus_method(smartlist_t *votes)
  367. {
  368. smartlist_t *all_methods = smartlist_create();
  369. smartlist_t *acceptable_methods = smartlist_create();
  370. smartlist_t *tmp = smartlist_create();
  371. int min = (smartlist_len(votes) * 2) / 3;
  372. int n_ok;
  373. int result;
  374. SMARTLIST_FOREACH(votes, networkstatus_t *, vote,
  375. {
  376. tor_assert(vote->supported_methods);
  377. smartlist_add_all(tmp, vote->supported_methods);
  378. smartlist_sort(tmp, _cmp_int_strings);
  379. smartlist_uniq(tmp, _cmp_int_strings, NULL);
  380. smartlist_add_all(all_methods, tmp);
  381. smartlist_clear(tmp);
  382. });
  383. smartlist_sort(all_methods, _cmp_int_strings);
  384. get_frequent_members(acceptable_methods, all_methods, min);
  385. n_ok = smartlist_len(acceptable_methods);
  386. if (n_ok) {
  387. const char *best = smartlist_get(acceptable_methods, n_ok-1);
  388. result = (int)tor_parse_long(best, 10, 1, INT_MAX, NULL, NULL);
  389. } else {
  390. result = 1;
  391. }
  392. smartlist_free(tmp);
  393. smartlist_free(all_methods);
  394. smartlist_free(acceptable_methods);
  395. return result;
  396. }
  397. /** Return true iff <b>method</b> is a consensus method that we support. */
  398. static int
  399. consensus_method_is_supported(int method)
  400. {
  401. return (method >= 1) && (method <= 3);
  402. }
  403. /** Given a list of vote networkstatus_t in <b>votes</b>, our public
  404. * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
  405. * and the number of <b>total_authorities</b> that we believe exist in our
  406. * voting quorum, generate the text of a new v3 consensus vote, and return the
  407. * value in a newly allocated string.
  408. *
  409. * Note: this function DOES NOT check whether the votes are from
  410. * recognized authorities. (dirvote_add_vote does that.) */
  411. char *
  412. networkstatus_compute_consensus(smartlist_t *votes,
  413. int total_authorities,
  414. crypto_pk_env_t *identity_key,
  415. crypto_pk_env_t *signing_key,
  416. const char *legacy_id_key_digest,
  417. crypto_pk_env_t *legacy_signing_key)
  418. {
  419. smartlist_t *chunks;
  420. char *result = NULL;
  421. int consensus_method;
  422. time_t valid_after, fresh_until, valid_until;
  423. int vote_seconds, dist_seconds;
  424. char *client_versions = NULL, *server_versions = NULL;
  425. smartlist_t *flags;
  426. tor_assert(total_authorities >= smartlist_len(votes));
  427. if (!smartlist_len(votes)) {
  428. log_warn(LD_DIR, "Can't compute a consensus from no votes.");
  429. return NULL;
  430. }
  431. flags = smartlist_create();
  432. consensus_method = compute_consensus_method(votes);
  433. if (consensus_method_is_supported(consensus_method)) {
  434. log_info(LD_DIR, "Generating consensus using method %d.",
  435. consensus_method);
  436. } else {
  437. log_warn(LD_DIR, "The other authorities will use consensus method %d, "
  438. "which I don't support. Maybe I should upgrade!",
  439. consensus_method);
  440. consensus_method = 1;
  441. }
  442. /* Compute medians of time-related things, and figure out how many
  443. * routers we might need to talk about. */
  444. {
  445. int n_votes = smartlist_len(votes);
  446. time_t *va_times = tor_malloc(n_votes * sizeof(time_t));
  447. time_t *fu_times = tor_malloc(n_votes * sizeof(time_t));
  448. time_t *vu_times = tor_malloc(n_votes * sizeof(time_t));
  449. int *votesec_list = tor_malloc(n_votes * sizeof(int));
  450. int *distsec_list = tor_malloc(n_votes * sizeof(int));
  451. int n_versioning_clients = 0, n_versioning_servers = 0;
  452. smartlist_t *combined_client_versions = smartlist_create();
  453. smartlist_t *combined_server_versions = smartlist_create();
  454. int j;
  455. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  456. {
  457. tor_assert(v->is_vote);
  458. va_times[v_sl_idx] = v->valid_after;
  459. fu_times[v_sl_idx] = v->fresh_until;
  460. vu_times[v_sl_idx] = v->valid_until;
  461. votesec_list[v_sl_idx] = v->vote_seconds;
  462. distsec_list[v_sl_idx] = v->dist_seconds;
  463. if (v->client_versions) {
  464. smartlist_t *cv = smartlist_create();
  465. ++n_versioning_clients;
  466. smartlist_split_string(cv, v->client_versions, ",",
  467. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  468. sort_version_list(cv, 1);
  469. smartlist_add_all(combined_client_versions, cv);
  470. smartlist_free(cv); /* elements get freed later. */
  471. }
  472. if (v->server_versions) {
  473. smartlist_t *sv = smartlist_create();
  474. ++n_versioning_servers;
  475. smartlist_split_string(sv, v->server_versions, ",",
  476. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  477. sort_version_list(sv, 1);
  478. smartlist_add_all(combined_server_versions, sv);
  479. smartlist_free(sv); /* elements get freed later. */
  480. }
  481. SMARTLIST_FOREACH(v->known_flags, const char *, cp,
  482. smartlist_add(flags, tor_strdup(cp)));
  483. });
  484. valid_after = median_time(va_times, n_votes);
  485. fresh_until = median_time(fu_times, n_votes);
  486. valid_until = median_time(vu_times, n_votes);
  487. vote_seconds = median_int(votesec_list, n_votes);
  488. dist_seconds = median_int(distsec_list, n_votes);
  489. tor_assert(valid_after+MIN_VOTE_INTERVAL <= fresh_until);
  490. tor_assert(fresh_until+MIN_VOTE_INTERVAL <= valid_until);
  491. tor_assert(vote_seconds >= MIN_VOTE_SECONDS);
  492. tor_assert(dist_seconds >= MIN_DIST_SECONDS);
  493. for (j = 0; j < 2; ++j) {
  494. smartlist_t *lst =
  495. j ? combined_server_versions : combined_client_versions;
  496. int min = (j ? n_versioning_servers : n_versioning_clients) / 2;
  497. smartlist_t *good = smartlist_create();
  498. char *res;
  499. sort_version_list(lst, 0);
  500. get_frequent_members(good, lst, min);
  501. res = smartlist_join_strings(good, ",", 0, NULL);
  502. if (j)
  503. server_versions = res;
  504. else
  505. client_versions = res;
  506. SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
  507. smartlist_free(good);
  508. smartlist_free(lst);
  509. }
  510. smartlist_sort_strings(flags);
  511. smartlist_uniq_strings(flags);
  512. tor_free(va_times);
  513. tor_free(fu_times);
  514. tor_free(vu_times);
  515. tor_free(votesec_list);
  516. tor_free(distsec_list);
  517. }
  518. chunks = smartlist_create();
  519. {
  520. char buf[1024];
  521. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  522. vu_buf[ISO_TIME_LEN+1];
  523. char *flaglist;
  524. format_iso_time(va_buf, valid_after);
  525. format_iso_time(fu_buf, fresh_until);
  526. format_iso_time(vu_buf, valid_until);
  527. flaglist = smartlist_join_strings(flags, " ", 0, NULL);
  528. smartlist_add(chunks, tor_strdup("network-status-version 3\n"
  529. "vote-status consensus\n"));
  530. if (consensus_method >= 2) {
  531. tor_snprintf(buf, sizeof(buf), "consensus-method %d\n",
  532. consensus_method);
  533. smartlist_add(chunks, tor_strdup(buf));
  534. }
  535. tor_snprintf(buf, sizeof(buf),
  536. "valid-after %s\n"
  537. "fresh-until %s\n"
  538. "valid-until %s\n"
  539. "voting-delay %d %d\n"
  540. "client-versions %s\n"
  541. "server-versions %s\n"
  542. "known-flags %s\n",
  543. va_buf, fu_buf, vu_buf,
  544. vote_seconds, dist_seconds,
  545. client_versions, server_versions, flaglist);
  546. smartlist_add(chunks, tor_strdup(buf));
  547. tor_free(flaglist);
  548. }
  549. /* Sort the votes. */
  550. smartlist_sort(votes, _compare_votes_by_authority_id);
  551. /* Add the authority sections. */
  552. {
  553. smartlist_t *dir_sources = smartlist_create();
  554. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  555. {
  556. dir_src_ent_t *e = tor_malloc_zero(sizeof(dir_src_ent_t));
  557. e->v = v;
  558. e->digest = get_voter(v)->identity_digest;
  559. e->is_legacy = 0;
  560. smartlist_add(dir_sources, e);
  561. if (consensus_method >= 3 &&
  562. !tor_digest_is_zero(get_voter(v)->legacy_id_digest)) {
  563. dir_src_ent_t *e_legacy = tor_malloc_zero(sizeof(dir_src_ent_t));
  564. e_legacy->v = v;
  565. e_legacy->digest = get_voter(v)->legacy_id_digest;
  566. e_legacy->is_legacy = 1;
  567. smartlist_add(dir_sources, e_legacy);
  568. }
  569. });
  570. smartlist_sort(dir_sources, _compare_dir_src_ents_by_authority_id);
  571. SMARTLIST_FOREACH(dir_sources, const dir_src_ent_t *, e,
  572. {
  573. char buf[1024];
  574. struct in_addr in;
  575. char ip[INET_NTOA_BUF_LEN];
  576. char fingerprint[HEX_DIGEST_LEN+1];
  577. char votedigest[HEX_DIGEST_LEN+1];
  578. networkstatus_t *v = e->v;
  579. networkstatus_voter_info_t *voter = get_voter(v);
  580. if (e->is_legacy)
  581. tor_assert(consensus_method >= 2);
  582. in.s_addr = htonl(voter->addr);
  583. tor_inet_ntoa(&in, ip, sizeof(ip));
  584. base16_encode(fingerprint, sizeof(fingerprint), e->digest, DIGEST_LEN);
  585. base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
  586. DIGEST_LEN);
  587. tor_snprintf(buf, sizeof(buf),
  588. "dir-source %s%s %s %s %s %d %d\n",
  589. voter->nickname, e->is_legacy ? "-legacy" : "",
  590. fingerprint, voter->address, ip,
  591. voter->dir_port,
  592. voter->or_port);
  593. smartlist_add(chunks, tor_strdup(buf));
  594. if (! e->is_legacy) {
  595. tor_snprintf(buf, sizeof(buf),
  596. "contact %s\n"
  597. "vote-digest %s\n",
  598. voter->contact,
  599. votedigest);
  600. smartlist_add(chunks, tor_strdup(buf));
  601. }
  602. });
  603. SMARTLIST_FOREACH(dir_sources, dir_src_ent_t *, e, tor_free(e));
  604. smartlist_free(dir_sources);
  605. }
  606. /* Add the actual router entries. */
  607. {
  608. int *index; /* index[j] is the current index into votes[j]. */
  609. int *size; /* size[j] is the number of routerstatuses in votes[j]. */
  610. int *flag_counts; /* The number of voters that list flag[j] for the
  611. * currently considered router. */
  612. int i;
  613. smartlist_t *matching_descs = smartlist_create();
  614. smartlist_t *chosen_flags = smartlist_create();
  615. smartlist_t *versions = smartlist_create();
  616. int *n_voter_flags; /* n_voter_flags[j] is the number of flags that
  617. * votes[j] knows about. */
  618. int *n_flag_voters; /* n_flag_voters[f] is the number of votes that care
  619. * about flags[f]. */
  620. int **flag_map; /* flag_map[j][b] is an index f such that flag_map[f]
  621. * is the same flag as votes[j]->known_flags[b]. */
  622. int *named_flag; /* Index of the flag "Named" for votes[j] */
  623. int *unnamed_flag; /* Index of the flag "Unnamed" for votes[j] */
  624. int chosen_named_idx, chosen_unnamed_idx;
  625. strmap_t *name_to_id_map = strmap_new();
  626. char conflict[DIGEST_LEN];
  627. char unknown[DIGEST_LEN];
  628. memset(conflict, 0, sizeof(conflict));
  629. memset(unknown, 0xff, sizeof(conflict));
  630. index = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  631. size = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  632. n_voter_flags = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  633. n_flag_voters = tor_malloc_zero(sizeof(int) * smartlist_len(flags));
  634. flag_map = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  635. named_flag = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  636. unnamed_flag = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  637. for (i = 0; i < smartlist_len(votes); ++i)
  638. unnamed_flag[i] = named_flag[i] = -1;
  639. chosen_named_idx = smartlist_string_pos(flags, "Named");
  640. chosen_unnamed_idx = smartlist_string_pos(flags, "Unnamed");
  641. /* Build the flag index. */
  642. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  643. {
  644. flag_map[v_sl_idx] = tor_malloc_zero(
  645. sizeof(int)*smartlist_len(v->known_flags));
  646. SMARTLIST_FOREACH(v->known_flags, const char *, fl,
  647. {
  648. int p = smartlist_string_pos(flags, fl);
  649. tor_assert(p >= 0);
  650. flag_map[v_sl_idx][fl_sl_idx] = p;
  651. ++n_flag_voters[p];
  652. if (!strcmp(fl, "Named"))
  653. named_flag[v_sl_idx] = fl_sl_idx;
  654. if (!strcmp(fl, "Unnamed"))
  655. unnamed_flag[v_sl_idx] = fl_sl_idx;
  656. });
  657. n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
  658. size[v_sl_idx] = smartlist_len(v->routerstatus_list);
  659. });
  660. /* Named and Unnamed get treated specially */
  661. if (consensus_method >= 2) {
  662. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  663. {
  664. uint64_t nf;
  665. if (named_flag[v_sl_idx]<0)
  666. continue;
  667. nf = U64_LITERAL(1) << named_flag[v_sl_idx];
  668. SMARTLIST_FOREACH(v->routerstatus_list, vote_routerstatus_t *, rs,
  669. {
  670. if ((rs->flags & nf) != 0) {
  671. const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
  672. if (!d) {
  673. /* We have no name officially mapped to this digest. */
  674. strmap_set_lc(name_to_id_map, rs->status.nickname,
  675. rs->status.identity_digest);
  676. } else if (d != conflict &&
  677. memcmp(d, rs->status.identity_digest, DIGEST_LEN)) {
  678. /* Authorities disagree about this nickname. */
  679. strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
  680. } else {
  681. /* It's already a conflict, or it's already this ID. */
  682. }
  683. }
  684. });
  685. });
  686. SMARTLIST_FOREACH(votes, networkstatus_t *, v,
  687. {
  688. uint64_t uf;
  689. if (unnamed_flag[v_sl_idx]<0)
  690. continue;
  691. uf = U64_LITERAL(1) << unnamed_flag[v_sl_idx];
  692. SMARTLIST_FOREACH(v->routerstatus_list, vote_routerstatus_t *, rs,
  693. {
  694. if ((rs->flags & uf) != 0) {
  695. const char *d = strmap_get_lc(name_to_id_map, rs->status.nickname);
  696. if (d == conflict || d == unknown) {
  697. /* Leave it alone; we know what it is. */
  698. } else if (!d) {
  699. /* We have no name officially mapped to this digest. */
  700. strmap_set_lc(name_to_id_map, rs->status.nickname, unknown);
  701. } else if (!memcmp(d, rs->status.identity_digest, DIGEST_LEN)) {
  702. /* Authorities disagree about this nickname. */
  703. strmap_set_lc(name_to_id_map, rs->status.nickname, conflict);
  704. } else {
  705. /* It's mapped to a different name. */
  706. }
  707. }
  708. });
  709. });
  710. }
  711. /* Now go through all the votes */
  712. flag_counts = tor_malloc(sizeof(int) * smartlist_len(flags));
  713. while (1) {
  714. vote_routerstatus_t *rs;
  715. routerstatus_t rs_out;
  716. const char *lowest_id = NULL;
  717. const char *chosen_version;
  718. const char *chosen_name = NULL;
  719. int is_named = 0, is_unnamed = 0;
  720. int naming_conflict = 0;
  721. int n_listing = 0;
  722. int i;
  723. char buf[256];
  724. /* Of the next-to-be-considered digest in each voter, which is first? */
  725. SMARTLIST_FOREACH(votes, networkstatus_t *, v, {
  726. if (index[v_sl_idx] < size[v_sl_idx]) {
  727. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  728. if (!lowest_id ||
  729. memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN) < 0)
  730. lowest_id = rs->status.identity_digest;
  731. }
  732. });
  733. if (!lowest_id) /* we're out of routers. */
  734. break;
  735. memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
  736. smartlist_clear(matching_descs);
  737. smartlist_clear(chosen_flags);
  738. smartlist_clear(versions);
  739. /* Okay, go through all the entries for this digest. */
  740. SMARTLIST_FOREACH(votes, networkstatus_t *, v, {
  741. if (index[v_sl_idx] >= size[v_sl_idx])
  742. continue; /* out of entries. */
  743. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  744. if (memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN))
  745. continue; /* doesn't include this router. */
  746. /* At this point, we know that we're looking at a routersatus with
  747. * identity "lowest".
  748. */
  749. ++index[v_sl_idx];
  750. ++n_listing;
  751. smartlist_add(matching_descs, rs);
  752. if (rs->version && rs->version[0])
  753. smartlist_add(versions, rs->version);
  754. /* Tally up all the flags. */
  755. for (i = 0; i < n_voter_flags[v_sl_idx]; ++i) {
  756. if (rs->flags & (U64_LITERAL(1) << i))
  757. ++flag_counts[flag_map[v_sl_idx][i]];
  758. }
  759. if (rs->flags & (U64_LITERAL(1) << named_flag[v_sl_idx])) {
  760. if (chosen_name && strcmp(chosen_name, rs->status.nickname)) {
  761. log_notice(LD_DIR, "Conflict on naming for router: %s vs %s",
  762. chosen_name, rs->status.nickname);
  763. naming_conflict = 1;
  764. }
  765. chosen_name = rs->status.nickname;
  766. }
  767. });
  768. /* We don't include this router at all unless more than half of
  769. * the authorities we believe in list it. */
  770. if (n_listing <= total_authorities/2)
  771. continue;
  772. /* Figure out the most popular opinion of what the most recent
  773. * routerinfo and its contents are. */
  774. rs = compute_routerstatus_consensus(matching_descs);
  775. /* Copy bits of that into rs_out. */
  776. tor_assert(!memcmp(lowest_id, rs->status.identity_digest, DIGEST_LEN));
  777. memcpy(rs_out.identity_digest, lowest_id, DIGEST_LEN);
  778. memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
  779. DIGEST_LEN);
  780. rs_out.addr = rs->status.addr;
  781. rs_out.published_on = rs->status.published_on;
  782. rs_out.dir_port = rs->status.dir_port;
  783. rs_out.or_port = rs->status.or_port;
  784. if (chosen_name && !naming_conflict) {
  785. strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
  786. } else {
  787. strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
  788. }
  789. if (consensus_method == 1) {
  790. is_named = chosen_named_idx >= 0 &&
  791. (!naming_conflict && flag_counts[chosen_named_idx]);
  792. } else {
  793. const char *d = strmap_get_lc(name_to_id_map, rs_out.nickname);
  794. if (!d) {
  795. is_named = is_unnamed = 0;
  796. } else if (!memcmp(d, lowest_id, DIGEST_LEN)) {
  797. is_named = 1; is_unnamed = 0;
  798. } else {
  799. is_named = 0; is_unnamed = 1;
  800. }
  801. }
  802. /* Set the flags. */
  803. smartlist_add(chosen_flags, (char*)"s"); /* for the start of the line. */
  804. SMARTLIST_FOREACH(flags, const char *, fl,
  805. {
  806. if (!strcmp(fl, "Named")) {
  807. if (is_named)
  808. smartlist_add(chosen_flags, (char*)fl);
  809. } else if (!strcmp(fl, "Unnamed") && consensus_method >= 2) {
  810. if (is_unnamed)
  811. smartlist_add(chosen_flags, (char*)fl);
  812. } else {
  813. if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2)
  814. smartlist_add(chosen_flags, (char*)fl);
  815. }
  816. });
  817. /* Pick the version. */
  818. if (smartlist_len(versions)) {
  819. sort_version_list(versions, 0);
  820. chosen_version = get_most_frequent_member(versions);
  821. } else {
  822. chosen_version = NULL;
  823. }
  824. /* Okay!! Now we can write the descriptor... */
  825. /* First line goes into "buf". */
  826. routerstatus_format_entry(buf, sizeof(buf), &rs_out, NULL, 1);
  827. smartlist_add(chunks, tor_strdup(buf));
  828. /* Second line is all flags. The "\n" is missing. */
  829. smartlist_add(chunks,
  830. smartlist_join_strings(chosen_flags, " ", 0, NULL));
  831. /* Now the version line. */
  832. if (chosen_version) {
  833. smartlist_add(chunks, tor_strdup("\nv "));
  834. smartlist_add(chunks, tor_strdup(chosen_version));
  835. }
  836. smartlist_add(chunks, tor_strdup("\n"));
  837. /* And the loop is over and we move on to the next router */
  838. }
  839. tor_free(index);
  840. tor_free(size);
  841. tor_free(n_voter_flags);
  842. tor_free(n_flag_voters);
  843. for (i = 0; i < smartlist_len(votes); ++i)
  844. tor_free(flag_map[i]);
  845. tor_free(flag_map);
  846. tor_free(flag_counts);
  847. tor_free(named_flag);
  848. tor_free(unnamed_flag);
  849. strmap_free(name_to_id_map, NULL);
  850. smartlist_free(matching_descs);
  851. smartlist_free(chosen_flags);
  852. smartlist_free(versions);
  853. }
  854. /* Add a signature. */
  855. {
  856. char digest[DIGEST_LEN];
  857. char fingerprint[HEX_DIGEST_LEN+1];
  858. char signing_key_fingerprint[HEX_DIGEST_LEN+1];
  859. char buf[4096];
  860. smartlist_add(chunks, tor_strdup("directory-signature "));
  861. /* Compute the hash of the chunks. */
  862. hash_list_members(digest, chunks);
  863. /* Get the fingerprints */
  864. crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
  865. crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
  866. /* add the junk that will go at the end of the line. */
  867. tor_snprintf(buf, sizeof(buf), "%s %s\n", fingerprint,
  868. signing_key_fingerprint);
  869. /* And the signature. */
  870. if (router_append_dirobj_signature(buf, sizeof(buf), digest,
  871. signing_key)) {
  872. log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
  873. return NULL; /* This leaks, but it should never happen. */
  874. }
  875. smartlist_add(chunks, tor_strdup(buf));
  876. if (legacy_id_key_digest && legacy_signing_key && consensus_method >= 3) {
  877. smartlist_add(chunks, tor_strdup("directory-signature "));
  878. base16_encode(fingerprint, sizeof(fingerprint),
  879. legacy_id_key_digest, DIGEST_LEN);
  880. crypto_pk_get_fingerprint(legacy_signing_key,
  881. signing_key_fingerprint, 0);
  882. tor_snprintf(buf, sizeof(buf), "%s %s\n", fingerprint,
  883. signing_key_fingerprint);
  884. if (router_append_dirobj_signature(buf, sizeof(buf), digest,
  885. legacy_signing_key)) {
  886. log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
  887. return NULL; /* This leaks, but it should never happen. */
  888. }
  889. smartlist_add(chunks, tor_strdup(buf));
  890. }
  891. }
  892. result = smartlist_join_strings(chunks, "", 0, NULL);
  893. tor_free(client_versions);
  894. tor_free(server_versions);
  895. smartlist_free(flags);
  896. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  897. smartlist_free(chunks);
  898. {
  899. networkstatus_t *c;
  900. if (!(c = networkstatus_parse_vote_from_string(result, NULL, 0))) {
  901. log_err(LD_BUG,"Generated a networkstatus consensus we couldn't "
  902. "parse.");
  903. tor_free(result);
  904. return NULL;
  905. }
  906. networkstatus_vote_free(c);
  907. }
  908. return result;
  909. }
  910. /** Given a consensus vote <b>target</b> and a set of detached signatures in
  911. * <b>sigs</b> that correspond to the same consensus, check whether there are
  912. * any new signatures in <b>src_voter_list</b> that should be added to
  913. * <b>target. (A signature should be added if we have no signature for that
  914. * voter in <b>target</b> yet, or if we have no verifiable signature and the
  915. * new signature is verifiable.) Return the number of signatures added or
  916. * changed, or -1 if the document signed by <b>sigs</b> isn't the same
  917. * document as <b>target</b>. */
  918. int
  919. networkstatus_add_detached_signatures(networkstatus_t *target,
  920. ns_detached_signatures_t *sigs,
  921. const char **msg_out)
  922. {
  923. int r = 0;
  924. tor_assert(sigs);
  925. tor_assert(target);
  926. tor_assert(!target->is_vote);
  927. /* Do the times seem right? */
  928. if (target->valid_after != sigs->valid_after) {
  929. *msg_out = "Valid-After times do not match "
  930. "when adding detached signatures to consensus";
  931. return -1;
  932. }
  933. if (target->fresh_until != sigs->fresh_until) {
  934. *msg_out = "Fresh-until times do not match "
  935. "when adding detached signatures to consensus";
  936. return -1;
  937. }
  938. if (target->valid_until != sigs->valid_until) {
  939. *msg_out = "Valid-until times do not match "
  940. "when adding detached signatures to consensus";
  941. return -1;
  942. }
  943. /* Are they the same consensus? */
  944. if (memcmp(target->networkstatus_digest, sigs->networkstatus_digest,
  945. DIGEST_LEN)) {
  946. *msg_out = "Digest mismatch when adding detached signatures to consensus";
  947. return -1;
  948. }
  949. /* For each voter in src... */
  950. SMARTLIST_FOREACH(sigs->signatures, networkstatus_voter_info_t *, src_voter,
  951. {
  952. char voter_identity[HEX_DIGEST_LEN+1];
  953. networkstatus_voter_info_t *target_voter =
  954. networkstatus_get_voter_by_id(target, src_voter->identity_digest);
  955. authority_cert_t *cert;
  956. base16_encode(voter_identity, sizeof(voter_identity),
  957. src_voter->identity_digest, DIGEST_LEN);
  958. log_info(LD_DIR, "Looking at signature from %s", voter_identity);
  959. /* If the target doesn't know about this voter, then forget it. */
  960. if (!target_voter) {
  961. log_info(LD_DIR, "We do not know about %s", voter_identity);
  962. continue;
  963. }
  964. /* If the target already has a good signature from this voter, then skip
  965. * this one. */
  966. if (target_voter->good_signature) {
  967. log_info(LD_DIR, "We already have a good signature from %s",
  968. voter_identity);
  969. continue;
  970. }
  971. /* Try checking the signature if we haven't already. */
  972. if (!src_voter->good_signature && !src_voter->bad_signature) {
  973. cert = authority_cert_get_by_digests(src_voter->identity_digest,
  974. src_voter->signing_key_digest);
  975. if (cert) {
  976. networkstatus_check_voter_signature(target, src_voter, cert);
  977. }
  978. }
  979. /* If this signature is good, or we don't have any signature yet,
  980. * then add it. */
  981. if (src_voter->good_signature || !target_voter->signature) {
  982. log_info(LD_DIR, "Adding signature from %s", voter_identity);
  983. ++r;
  984. tor_free(target_voter->signature);
  985. target_voter->signature =
  986. tor_memdup(src_voter->signature, src_voter->signature_len);
  987. memcpy(target_voter->signing_key_digest, src_voter->signing_key_digest,
  988. DIGEST_LEN);
  989. target_voter->signature_len = src_voter->signature_len;
  990. target_voter->good_signature = 1;
  991. target_voter->bad_signature = 0;
  992. } else {
  993. log_info(LD_DIR, "Not adding signature from %s", voter_identity);
  994. }
  995. });
  996. return r;
  997. }
  998. /** Return a newly allocated string holding the detached-signatures document
  999. * corresponding to the signatures on <b>consensus</b>. */
  1000. char *
  1001. networkstatus_get_detached_signatures(networkstatus_t *consensus)
  1002. {
  1003. smartlist_t *elements;
  1004. char buf[4096];
  1005. char *result = NULL;
  1006. int n_sigs = 0;
  1007. tor_assert(consensus);
  1008. tor_assert(! consensus->is_vote);
  1009. elements = smartlist_create();
  1010. {
  1011. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  1012. vu_buf[ISO_TIME_LEN+1];
  1013. char d[HEX_DIGEST_LEN+1];
  1014. base16_encode(d, sizeof(d), consensus->networkstatus_digest, DIGEST_LEN);
  1015. format_iso_time(va_buf, consensus->valid_after);
  1016. format_iso_time(fu_buf, consensus->fresh_until);
  1017. format_iso_time(vu_buf, consensus->valid_until);
  1018. tor_snprintf(buf, sizeof(buf),
  1019. "consensus-digest %s\n"
  1020. "valid-after %s\n"
  1021. "fresh-until %s\n"
  1022. "valid-until %s\n", d, va_buf, fu_buf, vu_buf);
  1023. smartlist_add(elements, tor_strdup(buf));
  1024. }
  1025. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, v,
  1026. {
  1027. char sk[HEX_DIGEST_LEN+1];
  1028. char id[HEX_DIGEST_LEN+1];
  1029. if (!v->signature || v->bad_signature)
  1030. continue;
  1031. ++n_sigs;
  1032. base16_encode(sk, sizeof(sk), v->signing_key_digest, DIGEST_LEN);
  1033. base16_encode(id, sizeof(id), v->identity_digest, DIGEST_LEN);
  1034. tor_snprintf(buf, sizeof(buf),
  1035. "directory-signature %s %s\n-----BEGIN SIGNATURE-----\n",
  1036. id, sk);
  1037. smartlist_add(elements, tor_strdup(buf));
  1038. base64_encode(buf, sizeof(buf), v->signature, v->signature_len);
  1039. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  1040. smartlist_add(elements, tor_strdup(buf));
  1041. });
  1042. result = smartlist_join_strings(elements, "", 0, NULL);
  1043. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  1044. smartlist_free(elements);
  1045. if (!n_sigs)
  1046. tor_free(result);
  1047. return result;
  1048. }
  1049. /** Release all storage held in <b>s</b>. */
  1050. void
  1051. ns_detached_signatures_free(ns_detached_signatures_t *s)
  1052. {
  1053. if (s->signatures) {
  1054. SMARTLIST_FOREACH(s->signatures, networkstatus_voter_info_t *, v,
  1055. {
  1056. tor_free(v->signature);
  1057. tor_free(v);
  1058. });
  1059. smartlist_free(s->signatures);
  1060. }
  1061. tor_free(s);
  1062. }
  1063. /* =====
  1064. * Certificate functions
  1065. * ===== */
  1066. /** Allocate and return a new authority_cert_t with the same contents as
  1067. * <b>cert</b>. */
  1068. authority_cert_t *
  1069. authority_cert_dup(authority_cert_t *cert)
  1070. {
  1071. authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
  1072. tor_assert(cert);
  1073. memcpy(out, cert, sizeof(authority_cert_t));
  1074. /* Now copy pointed-to things. */
  1075. out->cache_info.signed_descriptor_body =
  1076. tor_strndup(cert->cache_info.signed_descriptor_body,
  1077. cert->cache_info.signed_descriptor_len);
  1078. out->cache_info.saved_location = SAVED_NOWHERE;
  1079. out->identity_key = crypto_pk_dup_key(cert->identity_key);
  1080. out->signing_key = crypto_pk_dup_key(cert->signing_key);
  1081. return out;
  1082. }
  1083. /* =====
  1084. * Vote scheduling
  1085. * ===== */
  1086. /** Set *<b>timing_out</b> to the intervals at which we would like to vote.
  1087. * Note that these aren't the intervals we'll use to vote; they're the ones
  1088. * that we'll vote to use. */
  1089. void
  1090. dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
  1091. {
  1092. or_options_t *options = get_options();
  1093. tor_assert(timing_out);
  1094. timing_out->vote_interval = options->V3AuthVotingInterval;
  1095. timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
  1096. timing_out->vote_delay = options->V3AuthVoteDelay;
  1097. timing_out->dist_delay = options->V3AuthDistDelay;
  1098. }
  1099. /** Return the start of the next interval of size <b>interval</b> (in seconds)
  1100. * after <b>now</b>. Midnight always starts a fresh interval, and if the last
  1101. * interval of a day would be truncated to less than half its size, it is
  1102. * rolled into the previous interval. */
  1103. time_t
  1104. dirvote_get_start_of_next_interval(time_t now, int interval)
  1105. {
  1106. struct tm tm;
  1107. time_t midnight_today;
  1108. time_t midnight_tomorrow;
  1109. time_t next;
  1110. tor_gmtime_r(&now, &tm);
  1111. tm.tm_hour = 0;
  1112. tm.tm_min = 0;
  1113. tm.tm_sec = 0;
  1114. midnight_today = tor_timegm(&tm);
  1115. midnight_tomorrow = midnight_today + (24*60*60);
  1116. next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
  1117. /* Intervals never cross midnight. */
  1118. if (next > midnight_tomorrow)
  1119. next = midnight_tomorrow;
  1120. /* If the interval would only last half as long as it's supposed to, then
  1121. * skip over to the next day. */
  1122. if (next + interval/2 > midnight_tomorrow)
  1123. next = midnight_tomorrow;
  1124. return next;
  1125. }
  1126. /** Scheduling information for a voting interval. */
  1127. static struct {
  1128. /** When do we generate and distribute our vote for this interval? */
  1129. time_t voting_starts;
  1130. /** When do we send an HTTP request for any votes that we haven't
  1131. * been posted yet?*/
  1132. time_t fetch_missing_votes;
  1133. /** When do we give up on getting more votes and generate a consensus? */
  1134. time_t voting_ends;
  1135. /** When do we send an HTTP request for any signatures we're expecting to
  1136. * see on the consensus? */
  1137. time_t fetch_missing_signatures;
  1138. /** When do we publish the consensus? */
  1139. time_t interval_starts;
  1140. /* True iff we have generated and distributed our vote. */
  1141. int have_voted;
  1142. /* True iff we've requested missing votes. */
  1143. int have_fetched_missing_votes;
  1144. /* True iff we have built a consensus and sent the signatures around. */
  1145. int have_built_consensus;
  1146. /* True iff we've fetched missing signatures. */
  1147. int have_fetched_missing_signatures;
  1148. /* True iff we have published our consensus. */
  1149. int have_published_consensus;
  1150. } voting_schedule = {0,0,0,0,0,0,0,0,0,0};
  1151. /** Set voting_schedule to hold the timing for the next vote we should be
  1152. * doing. */
  1153. void
  1154. dirvote_recalculate_timing(or_options_t *options, time_t now)
  1155. {
  1156. int interval, vote_delay, dist_delay;
  1157. time_t start;
  1158. time_t end;
  1159. networkstatus_t *consensus;
  1160. if (!authdir_mode_v3(options))
  1161. return;
  1162. consensus = networkstatus_get_live_consensus(now);
  1163. memset(&voting_schedule, 0, sizeof(voting_schedule));
  1164. if (consensus) {
  1165. interval = (int)( consensus->fresh_until - consensus->valid_after );
  1166. vote_delay = consensus->vote_seconds;
  1167. dist_delay = consensus->dist_seconds;
  1168. } else {
  1169. interval = DEFAULT_VOTING_INTERVAL_WHEN_NO_CONSENSUS;
  1170. vote_delay = dist_delay = 300;
  1171. }
  1172. tor_assert(interval > 0);
  1173. if (vote_delay + dist_delay > interval/2)
  1174. vote_delay = dist_delay = interval / 4;
  1175. start = voting_schedule.interval_starts =
  1176. dirvote_get_start_of_next_interval(now,interval);
  1177. end = dirvote_get_start_of_next_interval(start+1, interval);
  1178. tor_assert(end > start);
  1179. voting_schedule.fetch_missing_signatures = start - (dist_delay/2);
  1180. voting_schedule.voting_ends = start - dist_delay;
  1181. voting_schedule.fetch_missing_votes = start - dist_delay - (vote_delay/2);
  1182. voting_schedule.voting_starts = start - dist_delay - vote_delay;
  1183. {
  1184. char tbuf[ISO_TIME_LEN+1];
  1185. format_iso_time(tbuf, voting_schedule.interval_starts);
  1186. log_notice(LD_DIR,"Choosing expected valid-after time as %s: "
  1187. "consensus_set=%d, interval=%d",
  1188. tbuf, consensus?1:0, interval);
  1189. }
  1190. }
  1191. /** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
  1192. void
  1193. dirvote_act(or_options_t *options, time_t now)
  1194. {
  1195. if (!authdir_mode_v3(options))
  1196. return;
  1197. if (!voting_schedule.voting_starts) {
  1198. char *keys = list_v3_auth_ids();
  1199. authority_cert_t *c = get_my_v3_authority_cert();
  1200. log_notice(LD_DIR, "Scheduling voting. Known authority IDs are %s. "
  1201. "Mine is %s.",
  1202. keys, hex_str(c->cache_info.identity_digest, DIGEST_LEN));
  1203. tor_free(keys);
  1204. dirvote_recalculate_timing(options, now);
  1205. }
  1206. if (voting_schedule.voting_starts < now && !voting_schedule.have_voted) {
  1207. log_notice(LD_DIR, "Time to vote.");
  1208. dirvote_perform_vote();
  1209. voting_schedule.have_voted = 1;
  1210. }
  1211. if (voting_schedule.fetch_missing_votes < now &&
  1212. !voting_schedule.have_fetched_missing_votes) {
  1213. log_notice(LD_DIR, "Time to fetch any votes that we're missing.");
  1214. dirvote_fetch_missing_votes();
  1215. voting_schedule.have_fetched_missing_votes = 1;
  1216. }
  1217. if (voting_schedule.voting_ends < now &&
  1218. !voting_schedule.have_built_consensus) {
  1219. log_notice(LD_DIR, "Time to compute a consensus.");
  1220. dirvote_compute_consensus();
  1221. /* XXXX We will want to try again later if we haven't got enough
  1222. * votes yet. Implement this if it turns out to ever happen. */
  1223. voting_schedule.have_built_consensus = 1;
  1224. }
  1225. if (voting_schedule.fetch_missing_signatures < now &&
  1226. !voting_schedule.have_fetched_missing_signatures) {
  1227. log_notice(LD_DIR, "Time to fetch any signatures that we're missing.");
  1228. dirvote_fetch_missing_signatures();
  1229. voting_schedule.have_fetched_missing_signatures = 1;
  1230. }
  1231. if (voting_schedule.interval_starts < now &&
  1232. !voting_schedule.have_published_consensus) {
  1233. log_notice(LD_DIR, "Time to publish the consensus and discard old votes");
  1234. dirvote_publish_consensus();
  1235. dirvote_clear_votes(0);
  1236. voting_schedule.have_published_consensus = 1;
  1237. /* XXXX We will want to try again later if we haven't got enough
  1238. * signatures yet. Implement this if it turns out to ever happen. */
  1239. dirvote_recalculate_timing(options, now);
  1240. }
  1241. }
  1242. /** A vote networkstatus_t and its unparsed body: held around so we can
  1243. * use it to generate a consensus (at voting_ends) and so we can serve it to
  1244. * other authorities that might want it. */
  1245. typedef struct pending_vote_t {
  1246. cached_dir_t *vote_body;
  1247. networkstatus_t *vote;
  1248. } pending_vote_t;
  1249. /** List of pending_vote_t for the current vote. Before we've used them to
  1250. * build a consensus, the votes go here. */
  1251. static smartlist_t *pending_vote_list = NULL;
  1252. /** List of pending_vote_t for the previous vote. After we've used them to
  1253. * build a consensus, the votes go here for the next period. */
  1254. static smartlist_t *previous_vote_list = NULL;
  1255. /** The body of the consensus that we're currently building. Once we
  1256. * have it built, it goes into dirserv.c */
  1257. static char *pending_consensus_body = NULL;
  1258. /** The detached signatures for the consensus that we're currently
  1259. * building. */
  1260. static char *pending_consensus_signatures = NULL;
  1261. /** The parsed in-progress consensus document. */
  1262. static networkstatus_t *pending_consensus = NULL;
  1263. /** List of ns_detached_signatures_t: hold signatures that get posted to us
  1264. * before we have generated the consensus on our own. */
  1265. static smartlist_t *pending_consensus_signature_list = NULL;
  1266. /** Generate a networkstatus vote and post it to all the v3 authorities.
  1267. * (V3 Authority only) */
  1268. static int
  1269. dirvote_perform_vote(void)
  1270. {
  1271. crypto_pk_env_t *key = get_my_v3_authority_signing_key();
  1272. authority_cert_t *cert = get_my_v3_authority_cert();
  1273. networkstatus_t *ns;
  1274. char *contents;
  1275. pending_vote_t *pending_vote;
  1276. int status;
  1277. const char *msg = "";
  1278. if (!cert || !key) {
  1279. log_warn(LD_NET, "Didn't find key/certificate to generate v3 vote");
  1280. return -1;
  1281. }
  1282. if (!(ns = dirserv_generate_networkstatus_vote_obj(key, cert)))
  1283. return -1;
  1284. contents = format_networkstatus_vote(key, ns);
  1285. networkstatus_vote_free(ns);
  1286. if (!contents)
  1287. return -1;
  1288. pending_vote = dirvote_add_vote(contents, &msg, &status);
  1289. tor_free(contents);
  1290. if (!pending_vote) {
  1291. log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
  1292. msg);
  1293. return -1;
  1294. }
  1295. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
  1296. ROUTER_PURPOSE_GENERAL,
  1297. V3_AUTHORITY,
  1298. pending_vote->vote_body->dir,
  1299. pending_vote->vote_body->dir_len, 0);
  1300. log_notice(LD_DIR, "Vote posted.");
  1301. return 0;
  1302. }
  1303. /** Send an HTTP request to every other v3 authority, for the votes of every
  1304. * authority for which we haven't received a vote yet in this period. (V3
  1305. * authority only) */
  1306. static void
  1307. dirvote_fetch_missing_votes(void)
  1308. {
  1309. smartlist_t *missing_fps = smartlist_create();
  1310. char *resource;
  1311. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1312. trusted_dir_server_t *, ds,
  1313. {
  1314. if (!(ds->type & V3_AUTHORITY))
  1315. continue;
  1316. if (!dirvote_get_vote(ds->v3_identity_digest,
  1317. DGV_BY_ID|DGV_INCLUDE_PENDING)) {
  1318. char *cp = tor_malloc(HEX_DIGEST_LEN+1);
  1319. base16_encode(cp, HEX_DIGEST_LEN+1, ds->v3_identity_digest,
  1320. DIGEST_LEN);
  1321. smartlist_add(missing_fps, cp);
  1322. }
  1323. });
  1324. if (!smartlist_len(missing_fps)) {
  1325. smartlist_free(missing_fps);
  1326. return;
  1327. }
  1328. log_notice(LOG_NOTICE, "We're missing votes from %d authorities. Asking "
  1329. "every other authority for a copy.", smartlist_len(missing_fps));
  1330. resource = smartlist_join_strings(missing_fps, "+", 0, NULL);
  1331. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE,
  1332. 0, resource);
  1333. tor_free(resource);
  1334. SMARTLIST_FOREACH(missing_fps, char *, cp, tor_free(cp));
  1335. smartlist_free(missing_fps);
  1336. }
  1337. /** Send a request to every other authority for its detached signatures,
  1338. * unless we have signatures from all other v3 authorities already. */
  1339. static void
  1340. dirvote_fetch_missing_signatures(void)
  1341. {
  1342. if (!pending_consensus)
  1343. return;
  1344. if (networkstatus_check_consensus_signature(pending_consensus, -1) == 1)
  1345. return; /* we have a signature from everybody. */
  1346. directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES,
  1347. 0, NULL);
  1348. }
  1349. /** Drop all currently pending votes, consensus, and detached signatures. */
  1350. static void
  1351. dirvote_clear_votes(int all_votes)
  1352. {
  1353. if (!previous_vote_list)
  1354. previous_vote_list = smartlist_create();
  1355. if (!pending_vote_list)
  1356. pending_vote_list = smartlist_create();
  1357. /* All "previous" votes are now junk. */
  1358. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, v, {
  1359. cached_dir_decref(v->vote_body);
  1360. v->vote_body = NULL;
  1361. networkstatus_vote_free(v->vote);
  1362. tor_free(v);
  1363. });
  1364. smartlist_clear(previous_vote_list);
  1365. if (all_votes) {
  1366. /* If we're dumping all the votes, we delete the pending ones. */
  1367. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1368. cached_dir_decref(v->vote_body);
  1369. v->vote_body = NULL;
  1370. networkstatus_vote_free(v->vote);
  1371. tor_free(v);
  1372. });
  1373. } else {
  1374. /* Otherwise, we move them into "previous". */
  1375. smartlist_add_all(previous_vote_list, pending_vote_list);
  1376. }
  1377. smartlist_clear(pending_vote_list);
  1378. if (pending_consensus_signature_list) {
  1379. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
  1380. tor_free(cp));
  1381. smartlist_clear(pending_consensus_signature_list);
  1382. }
  1383. tor_free(pending_consensus_body);
  1384. tor_free(pending_consensus_signatures);
  1385. if (pending_consensus) {
  1386. networkstatus_vote_free(pending_consensus);
  1387. pending_consensus = NULL;
  1388. }
  1389. }
  1390. /** Return a newly allocated string containing the hex-encoded v3 authority
  1391. identity digest of every recognized v3 authority. */
  1392. static char *
  1393. list_v3_auth_ids(void)
  1394. {
  1395. smartlist_t *known_v3_keys = smartlist_create();
  1396. char *keys;
  1397. SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
  1398. trusted_dir_server_t *, ds,
  1399. if ((ds->type & V3_AUTHORITY) &&
  1400. !tor_digest_is_zero(ds->v3_identity_digest))
  1401. smartlist_add(known_v3_keys,
  1402. tor_strdup(hex_str(ds->v3_identity_digest, DIGEST_LEN))));
  1403. keys = smartlist_join_strings(known_v3_keys, ", ", 0, NULL);
  1404. SMARTLIST_FOREACH(known_v3_keys, char *, cp, tor_free(cp));
  1405. smartlist_free(known_v3_keys);
  1406. return keys;
  1407. }
  1408. /** Called when we have received a networkstatus vote in <b>vote_body</b>.
  1409. * Parse and validate it, and on success store it as a pending vote (which we
  1410. * then return). Return NULL on failure. Sets *<b>msg_out</b> and
  1411. * *<b>status_out</b> to an HTTP response and status code. (V3 authority
  1412. * only) */
  1413. pending_vote_t *
  1414. dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
  1415. {
  1416. networkstatus_t *vote;
  1417. networkstatus_voter_info_t *vi;
  1418. trusted_dir_server_t *ds;
  1419. pending_vote_t *pending_vote = NULL;
  1420. const char *end_of_vote = NULL;
  1421. int any_failed = 0;
  1422. tor_assert(vote_body);
  1423. tor_assert(msg_out);
  1424. tor_assert(status_out);
  1425. if (!pending_vote_list)
  1426. pending_vote_list = smartlist_create();
  1427. *status_out = 0;
  1428. *msg_out = NULL;
  1429. again:
  1430. vote = networkstatus_parse_vote_from_string(vote_body, &end_of_vote, 1);
  1431. if (!end_of_vote)
  1432. end_of_vote = vote_body + strlen(vote_body);
  1433. if (!vote) {
  1434. log_warn(LD_DIR, "Couldn't parse vote: length was %d",
  1435. (int)strlen(vote_body));
  1436. *msg_out = "Unable to parse vote";
  1437. goto err;
  1438. }
  1439. tor_assert(smartlist_len(vote->voters) == 1);
  1440. vi = get_voter(vote);
  1441. tor_assert(vi->good_signature == 1);
  1442. ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
  1443. if (!ds) {
  1444. char *keys = list_v3_auth_ids();
  1445. log_warn(LD_DIR, "Got a vote from an authority (nickname %s, address %s) "
  1446. "with authority key ID %s. "
  1447. "This key ID is not recognized. Known v3 key IDs are: %s",
  1448. vi->nickname, vi->address,
  1449. hex_str(vi->identity_digest, DIGEST_LEN), keys);
  1450. tor_free(keys);
  1451. *msg_out = "Vote not from a recognized v3 authority";
  1452. goto err;
  1453. }
  1454. tor_assert(vote->cert);
  1455. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1456. vote->cert->signing_key_digest)) {
  1457. /* Hey, it's a new cert! */
  1458. trusted_dirs_load_certs_from_string(
  1459. vote->cert->cache_info.signed_descriptor_body,
  1460. 0 /* from_store */, 1 /*flush*/);
  1461. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1462. vote->cert->signing_key_digest)) {
  1463. log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
  1464. }
  1465. }
  1466. /* Is it for the right period? */
  1467. if (vote->valid_after != voting_schedule.interval_starts) {
  1468. char tbuf1[ISO_TIME_LEN+1], tbuf2[ISO_TIME_LEN+1];
  1469. format_iso_time(tbuf1, vote->valid_after);
  1470. format_iso_time(tbuf2, voting_schedule.interval_starts);
  1471. log_warn(LD_DIR, "Rejecting vote from %s with valid-after time of %s; "
  1472. "we were expecting %s", vi->address, tbuf1, tbuf2);
  1473. *msg_out = "Bad valid-after time";
  1474. goto err;
  1475. }
  1476. /* Now see whether we already h<ave a vote from this authority.*/
  1477. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1478. if (! memcmp(v->vote->cert->cache_info.identity_digest,
  1479. vote->cert->cache_info.identity_digest,
  1480. DIGEST_LEN)) {
  1481. networkstatus_voter_info_t *vi_old = get_voter(v->vote);
  1482. if (!memcmp(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
  1483. /* Ah, it's the same vote. Not a problem. */
  1484. log_info(LD_DIR, "Discarding a vote we already have.");
  1485. if (*status_out < 200)
  1486. *status_out = 200;
  1487. goto discard;
  1488. } else if (v->vote->published < vote->published) {
  1489. log_notice(LD_DIR, "Replacing an older pending vote from this "
  1490. "directory.");
  1491. cached_dir_decref(v->vote_body);
  1492. networkstatus_vote_free(v->vote);
  1493. v->vote_body = new_cached_dir(tor_strndup(vote_body,
  1494. end_of_vote-vote_body),
  1495. vote->published);
  1496. v->vote = vote;
  1497. if (end_of_vote &&
  1498. !strcmpstart(end_of_vote, "network-status-version"))
  1499. goto again;
  1500. if (*status_out < 200)
  1501. *status_out = 200;
  1502. if (!*msg_out)
  1503. *msg_out = "OK";
  1504. return v;
  1505. } else {
  1506. *msg_out = "Already have a newer pending vote";
  1507. goto err;
  1508. }
  1509. }
  1510. });
  1511. pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
  1512. pending_vote->vote_body = new_cached_dir(tor_strndup(vote_body,
  1513. end_of_vote-vote_body),
  1514. vote->published);
  1515. pending_vote->vote = vote;
  1516. smartlist_add(pending_vote_list, pending_vote);
  1517. if (!strcmpstart(end_of_vote, "network-status-version ")) {
  1518. vote_body = end_of_vote;
  1519. goto again;
  1520. }
  1521. goto done;
  1522. err:
  1523. any_failed = 1;
  1524. if (!*msg_out)
  1525. *msg_out = "Error adding vote";
  1526. if (*status_out < 400)
  1527. *status_out = 400;
  1528. discard:
  1529. if (vote)
  1530. networkstatus_vote_free(vote);
  1531. if (end_of_vote && !strcmpstart(end_of_vote, "network-status-version ")) {
  1532. vote_body = end_of_vote;
  1533. goto again;
  1534. }
  1535. done:
  1536. if (*status_out < 200)
  1537. *status_out = 200;
  1538. if (!*msg_out) {
  1539. if (!any_failed && !pending_vote) {
  1540. *msg_out = "Duplicate discarded";
  1541. } else {
  1542. *msg_out = "ok";
  1543. }
  1544. }
  1545. return any_failed ? NULL : pending_vote;
  1546. }
  1547. /** Try to compute a v3 networkstatus consensus from the currently pending
  1548. * votes. Return 0 on success, -1 on failure. Store the consensus in
  1549. * pending_consensus: it won't be ready to be published until we have
  1550. * everybody else's signatures collected too. (V3 Authoritity only) */
  1551. static int
  1552. dirvote_compute_consensus(void)
  1553. {
  1554. /* Have we got enough votes to try? */
  1555. int n_votes, n_voters;
  1556. smartlist_t *votes = NULL;
  1557. char *consensus_body = NULL, *signatures = NULL;
  1558. networkstatus_t *consensus = NULL;
  1559. authority_cert_t *my_cert;
  1560. if (!pending_vote_list)
  1561. pending_vote_list = smartlist_create();
  1562. n_voters = get_n_authorities(V3_AUTHORITY);
  1563. n_votes = smartlist_len(pending_vote_list);
  1564. if (n_votes <= n_voters/2) {
  1565. log_warn(LD_DIR, "We don't have enough votes to generate a consensus: "
  1566. "%d of %d", n_votes, n_voters/2);
  1567. goto err;
  1568. }
  1569. if (!(my_cert = get_my_v3_authority_cert())) {
  1570. log_warn(LD_DIR, "Can't generate consensus without a certificate.");
  1571. goto err;
  1572. }
  1573. votes = smartlist_create();
  1574. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
  1575. smartlist_add(votes, v->vote));
  1576. {
  1577. char legacy_dbuf[DIGEST_LEN];
  1578. crypto_pk_env_t *legacy_sign=NULL;
  1579. char *legacy_id_digest = NULL;
  1580. if (get_options()->V3AuthUseLegacyKey) {
  1581. authority_cert_t *cert = get_my_v3_legacy_cert();
  1582. legacy_sign = get_my_v3_legacy_signing_key();
  1583. if (cert) {
  1584. crypto_pk_get_digest(cert->identity_key, legacy_dbuf);
  1585. legacy_id_digest = legacy_dbuf;
  1586. }
  1587. }
  1588. consensus_body = networkstatus_compute_consensus(
  1589. votes, n_voters,
  1590. my_cert->identity_key,
  1591. get_my_v3_authority_signing_key(), legacy_id_digest, legacy_sign);
  1592. }
  1593. if (!consensus_body) {
  1594. log_warn(LD_DIR, "Couldn't generate a consensus at all!");
  1595. goto err;
  1596. }
  1597. consensus = networkstatus_parse_vote_from_string(consensus_body, NULL, 0);
  1598. if (!consensus) {
  1599. log_warn(LD_DIR, "Couldn't parse consensus we generated!");
  1600. goto err;
  1601. }
  1602. /* 'Check' our own signature, to mark it valid. */
  1603. networkstatus_check_consensus_signature(consensus, -1);
  1604. signatures = networkstatus_get_detached_signatures(consensus);
  1605. if (!signatures) {
  1606. log_warn(LD_DIR, "Couldn't extract signatures.");
  1607. goto err;
  1608. }
  1609. tor_free(pending_consensus_body);
  1610. pending_consensus_body = consensus_body;
  1611. tor_free(pending_consensus_signatures);
  1612. pending_consensus_signatures = signatures;
  1613. if (pending_consensus)
  1614. networkstatus_vote_free(pending_consensus);
  1615. pending_consensus = consensus;
  1616. if (pending_consensus_signature_list) {
  1617. int n_sigs = 0;
  1618. /* we may have gotten signatures for this consensus before we built
  1619. * it ourself. Add them now. */
  1620. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, sig,
  1621. {
  1622. const char *msg = NULL;
  1623. int r = dirvote_add_signatures_to_pending_consensus(sig, &msg);
  1624. if (r >= 0)
  1625. n_sigs += r;
  1626. else
  1627. log_warn(LD_DIR,
  1628. "Could not add queued signature to new consensus: %s",
  1629. msg);
  1630. tor_free(sig);
  1631. });
  1632. if (n_sigs)
  1633. log_notice(LD_DIR, "Added %d pending signatures while building "
  1634. "consensus.", n_sigs);
  1635. smartlist_clear(pending_consensus_signature_list);
  1636. }
  1637. log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
  1638. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES,
  1639. ROUTER_PURPOSE_GENERAL,
  1640. V3_AUTHORITY,
  1641. pending_consensus_signatures,
  1642. strlen(pending_consensus_signatures), 0);
  1643. log_notice(LD_DIR, "Signature(s) posted.");
  1644. return 0;
  1645. err:
  1646. if (votes)
  1647. smartlist_free(votes);
  1648. tor_free(consensus_body);
  1649. tor_free(signatures);
  1650. networkstatus_vote_free(consensus);
  1651. return -1;
  1652. }
  1653. /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
  1654. * signatures on the currently pending consensus. Add them to the consensus
  1655. * as appropriate. Return the number of signatures added. (?) */
  1656. static int
  1657. dirvote_add_signatures_to_pending_consensus(
  1658. const char *detached_signatures_body,
  1659. const char **msg_out)
  1660. {
  1661. ns_detached_signatures_t *sigs = NULL;
  1662. int r = -1;
  1663. tor_assert(detached_signatures_body);
  1664. tor_assert(msg_out);
  1665. /* Only call if we have a pending consensus right now. */
  1666. tor_assert(pending_consensus);
  1667. tor_assert(pending_consensus_body);
  1668. tor_assert(pending_consensus_signatures);
  1669. *msg_out = NULL;
  1670. if (!(sigs = networkstatus_parse_detached_signatures(
  1671. detached_signatures_body, NULL))) {
  1672. *msg_out = "Couldn't parse detached signatures.";
  1673. goto err;
  1674. }
  1675. log_info(LD_DIR, "Have %d signatures for adding to consensus.",
  1676. smartlist_len(sigs->signatures));
  1677. r = networkstatus_add_detached_signatures(pending_consensus,
  1678. sigs, msg_out);
  1679. log_info(LD_DIR,"Added %d signatures to consensus.", r);
  1680. if (r >= 0) {
  1681. char *new_detached =
  1682. networkstatus_get_detached_signatures(pending_consensus);
  1683. const char *src;
  1684. char *dst, *dst_end;
  1685. size_t new_consensus_len =
  1686. strlen(pending_consensus_body) + strlen(new_detached) + 1;
  1687. pending_consensus_body = tor_realloc(pending_consensus_body,
  1688. new_consensus_len);
  1689. dst_end = pending_consensus_body + new_consensus_len;
  1690. dst = strstr(pending_consensus_body, "directory-signature ");
  1691. tor_assert(dst);
  1692. src = strstr(new_detached, "directory-signature ");
  1693. tor_assert(src);
  1694. strlcpy(dst, src, dst_end-dst);
  1695. /* We remove this block once it has failed to crash for a while. But
  1696. * unless it shows up in profiles, we're probably better leaving it in,
  1697. * just in case we break detached signature processing at some point. */
  1698. {
  1699. ns_detached_signatures_t *sigs =
  1700. networkstatus_parse_detached_signatures(new_detached, NULL);
  1701. networkstatus_t *v = networkstatus_parse_vote_from_string(
  1702. pending_consensus_body, NULL, 0);
  1703. tor_assert(sigs);
  1704. ns_detached_signatures_free(sigs);
  1705. tor_assert(v);
  1706. networkstatus_vote_free(v);
  1707. }
  1708. tor_free(pending_consensus_signatures);
  1709. pending_consensus_signatures = new_detached;
  1710. *msg_out = "Signatures added";
  1711. } else {
  1712. goto err;
  1713. }
  1714. goto done;
  1715. err:
  1716. if (!msg_out)
  1717. *msg_out = "Unrecognized error while adding detached signatures.";
  1718. done:
  1719. if (sigs)
  1720. ns_detached_signatures_free(sigs);
  1721. return r;
  1722. }
  1723. /** Helper: we just got the <b>deteached_signatures_body</b> sent to us as
  1724. * signatures on the currently pending consensus. Add them to the pending
  1725. * consensus (if we have one); otherwise queue them until we have a
  1726. * consensus. Return negative on failure, nonnegative on success. */
  1727. int
  1728. dirvote_add_signatures(const char *detached_signatures_body,
  1729. const char *source,
  1730. const char **msg)
  1731. {
  1732. if (pending_consensus) {
  1733. log_notice(LD_DIR, "Got a signature from %s. "
  1734. "Adding it to the pending consensus.", source);
  1735. return dirvote_add_signatures_to_pending_consensus(
  1736. detached_signatures_body, msg);
  1737. } else {
  1738. log_notice(LD_DIR, "Got a signature from %s. "
  1739. "Queueing it for the next consensus.", source);
  1740. if (!pending_consensus_signature_list)
  1741. pending_consensus_signature_list = smartlist_create();
  1742. smartlist_add(pending_consensus_signature_list,
  1743. tor_strdup(detached_signatures_body));
  1744. *msg = "Signature queued";
  1745. return 0;
  1746. }
  1747. }
  1748. /** Replace the consensus that we're currently serving with the one that we've
  1749. * been building. (V3 Authority only) */
  1750. static int
  1751. dirvote_publish_consensus(void)
  1752. {
  1753. /* Can we actually publish it yet? */
  1754. if (!pending_consensus ||
  1755. networkstatus_check_consensus_signature(pending_consensus, 1)<0) {
  1756. log_warn(LD_DIR, "Not enough info to publish pending consensus");
  1757. return -1;
  1758. }
  1759. if (networkstatus_set_current_consensus(pending_consensus_body, 0))
  1760. log_warn(LD_DIR, "Error publishing consensus");
  1761. else
  1762. log_notice(LD_DIR, "Consensus published.");
  1763. return 0;
  1764. }
  1765. /** Release all static storage held in dirvote.c */
  1766. void
  1767. dirvote_free_all(void)
  1768. {
  1769. dirvote_clear_votes(1);
  1770. /* now empty as a result of clear_pending_votes. */
  1771. smartlist_free(pending_vote_list);
  1772. pending_vote_list = NULL;
  1773. smartlist_free(previous_vote_list);
  1774. previous_vote_list = NULL;
  1775. tor_free(pending_consensus_body);
  1776. tor_free(pending_consensus_signatures);
  1777. if (pending_consensus) {
  1778. networkstatus_vote_free(pending_consensus);
  1779. pending_consensus = NULL;
  1780. }
  1781. if (pending_consensus_signature_list) {
  1782. /* now empty as a result of clear_pending_votes. */
  1783. smartlist_free(pending_consensus_signature_list);
  1784. pending_consensus_signature_list = NULL;
  1785. }
  1786. }
  1787. /* ====
  1788. * Access to pending items.
  1789. * ==== */
  1790. /** Return the body of the consensus that we're currently trying to build. */
  1791. const char *
  1792. dirvote_get_pending_consensus(void)
  1793. {
  1794. return pending_consensus_body;
  1795. }
  1796. /** Return the signatures that we know for the consensus that we're currently
  1797. * trying to build */
  1798. const char *
  1799. dirvote_get_pending_detached_signatures(void)
  1800. {
  1801. return pending_consensus_signatures;
  1802. }
  1803. /** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
  1804. * vote for the authority with the v3 authority identity key digest <b>fp</b>;
  1805. * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
  1806. * <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
  1807. * false, do not consider any votes for a consensus that's already been built.
  1808. * If <b>include_pending</b> is false, do not consider any votes for the
  1809. * consensus that's in progress. May return NULL if we have no vote for the
  1810. * authority in question. */
  1811. const cached_dir_t *
  1812. dirvote_get_vote(const char *fp, int flags)
  1813. {
  1814. int by_id = flags & DGV_BY_ID;
  1815. const int include_pending = flags & DGV_INCLUDE_PENDING;
  1816. const int include_previous = flags & DGV_INCLUDE_PREVIOUS;
  1817. if (!pending_vote_list && !previous_vote_list)
  1818. return NULL;
  1819. if (fp == NULL) {
  1820. authority_cert_t *c = get_my_v3_authority_cert();
  1821. if (c) {
  1822. fp = c->cache_info.identity_digest;
  1823. by_id = 1;
  1824. } else
  1825. return NULL;
  1826. }
  1827. if (by_id) {
  1828. if (pending_vote_list && include_pending) {
  1829. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  1830. if (!memcmp(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
  1831. return pv->vote_body);
  1832. }
  1833. if (previous_vote_list && include_previous) {
  1834. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  1835. if (!memcmp(get_voter(pv->vote)->identity_digest, fp, DIGEST_LEN))
  1836. return pv->vote_body);
  1837. }
  1838. } else {
  1839. if (pending_vote_list && include_pending) {
  1840. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, pv,
  1841. if (!memcmp(pv->vote->networkstatus_digest, fp, DIGEST_LEN))
  1842. return pv->vote_body);
  1843. }
  1844. if (previous_vote_list && include_previous) {
  1845. SMARTLIST_FOREACH(previous_vote_list, pending_vote_t *, pv,
  1846. if (!memcmp(pv->vote->networkstatus_digest, fp, DIGEST_LEN))
  1847. return pv->vote_body);
  1848. }
  1849. }
  1850. return NULL;
  1851. }