dirvote.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char dirvote_c_id[] =
  6. "$Id$";
  7. #define DIRVOTE_PRIVATE
  8. #include "or.h"
  9. /**
  10. * \file dirvote.c
  11. * \brief Functions to compute directory consensus, and schedule voting.
  12. **/
  13. static int dirvote_add_signatures_to_pending_consensus(
  14. const char *detached_signatures_body,
  15. const char **msg_out);
  16. /* =====
  17. * Voting and consensus generation
  18. * ===== */
  19. /** Clear all storage held in <b>ns</b>. */
  20. void
  21. networkstatus_vote_free(networkstatus_vote_t *ns)
  22. {
  23. if (!ns)
  24. return;
  25. tor_free(ns->client_versions);
  26. tor_free(ns->server_versions);
  27. if (ns->known_flags) {
  28. SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
  29. smartlist_free(ns->known_flags);
  30. }
  31. if (ns->voters) {
  32. SMARTLIST_FOREACH(ns->voters, networkstatus_voter_info_t *, voter,
  33. {
  34. tor_free(voter->nickname);
  35. tor_free(voter->address);
  36. tor_free(voter->contact);
  37. });
  38. smartlist_free(ns->voters);
  39. }
  40. if (ns->cert)
  41. authority_cert_free(ns->cert);
  42. if (ns->routerstatus_list) {
  43. if (ns->is_vote) {
  44. SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
  45. {
  46. tor_free(rs->version);
  47. tor_free(rs);
  48. });
  49. } else {
  50. SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
  51. tor_free(rs));
  52. }
  53. smartlist_free(ns->routerstatus_list);
  54. }
  55. memset(ns, 11, sizeof(*ns));
  56. tor_free(ns);
  57. }
  58. /** Return the voter info from <b>vote</b> for the voter whose identity digest
  59. * is <b>identity</b>, or NULL if no such voter is associated with
  60. * <b>vote</b>. */
  61. networkstatus_voter_info_t *
  62. networkstatus_get_voter_by_id(networkstatus_vote_t *vote,
  63. const char *identity)
  64. {
  65. if (!vote || !vote->voters)
  66. return NULL;
  67. SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
  68. if (!memcmp(voter->identity_digest, identity, DIGEST_LEN))
  69. return voter);
  70. return NULL;
  71. }
  72. /** Helper for sorting a list of time_t*. */
  73. static int
  74. _compare_times(const void **_a, const void **_b)
  75. {
  76. const time_t *a = *_a, *b = *_b;
  77. if (*a<*b)
  78. return -1;
  79. else if (*a>*b)
  80. return 1;
  81. else
  82. return 0;
  83. }
  84. /** Helper for sorting a list of int*. */
  85. static int
  86. _compare_ints(const void **_a, const void **_b)
  87. {
  88. const int *a = *_a, *b = *_b;
  89. if (*a<*b)
  90. return -1;
  91. else if (*a>*b)
  92. return 1;
  93. else
  94. return 0;
  95. }
  96. /** Given a list of one or more time_t*, return the (low) median. */
  97. static time_t
  98. median_time(smartlist_t *times)
  99. {
  100. int idx;
  101. tor_assert(smartlist_len(times));
  102. smartlist_sort(times, _compare_times);
  103. idx = (smartlist_len(times)-1)/2;
  104. return *(time_t*)smartlist_get(times, idx);
  105. }
  106. /** Given a list of one or more int*, return the (low) median. */
  107. static int
  108. median_int(smartlist_t *ints)
  109. {
  110. int idx;
  111. tor_assert(smartlist_len(ints));
  112. smartlist_sort(ints, _compare_ints);
  113. idx = (smartlist_len(ints)-1)/2;
  114. return *(int*)smartlist_get(ints, idx);
  115. }
  116. /** Given a vote <b>vote</b> (not a consensus!), return its associated
  117. * networkstatus_voter_info_t.*/
  118. static networkstatus_voter_info_t *
  119. get_voter(const networkstatus_vote_t *vote)
  120. {
  121. tor_assert(vote);
  122. tor_assert(vote->is_vote);
  123. tor_assert(vote->voters);
  124. tor_assert(smartlist_len(vote->voters) == 1);
  125. return smartlist_get(vote->voters, 0);
  126. }
  127. /** Helper for sorting networkstatus_vote_t votes (not consensuses) by the
  128. * hash of their voters' identity digests. */
  129. static int
  130. _compare_votes_by_authority_id(const void **_a, const void **_b)
  131. {
  132. const networkstatus_vote_t *a = *_a, *b = *_b;
  133. return memcmp(get_voter(a)->identity_digest,
  134. get_voter(b)->identity_digest, DIGEST_LEN);
  135. }
  136. /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
  137. * that occurs more than <b>min</b> times. */
  138. static void
  139. get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
  140. {
  141. char *cur = NULL;
  142. int count = 0;
  143. SMARTLIST_FOREACH(in, char *, cp,
  144. {
  145. if (cur && !strcmp(cp, cur)) {
  146. ++count;
  147. } else {
  148. if (count > min)
  149. smartlist_add(out, cur);
  150. cur = cp;
  151. count = 1;
  152. }
  153. });
  154. if (count > min)
  155. smartlist_add(out, cur);
  156. }
  157. /** Given a sorted list of strings <b>lst</b>, return the member that appears
  158. * most. Break ties in favor of later-occurring members. */
  159. static const char *
  160. get_most_frequent_member(smartlist_t *lst)
  161. {
  162. const char *most_frequent = NULL;
  163. int most_frequent_count = 0;
  164. const char *cur = NULL;
  165. int count = 0;
  166. SMARTLIST_FOREACH(lst, const char *, s,
  167. {
  168. if (cur && !strcmp(s, cur)) {
  169. ++count;
  170. } else {
  171. if (count >= most_frequent_count) {
  172. most_frequent = cur;
  173. most_frequent_count = count;
  174. }
  175. cur = s;
  176. count = 1;
  177. }
  178. });
  179. if (count >= most_frequent_count) {
  180. most_frequent = cur;
  181. most_frequent_count = count;
  182. }
  183. return most_frequent;
  184. }
  185. /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
  186. * that come from the same routerinfo, with the same derived elements.
  187. */
  188. static int
  189. compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
  190. {
  191. int r;
  192. if ((r = memcmp(a->status.identity_digest, b->status.identity_digest,
  193. DIGEST_LEN)))
  194. return r;
  195. if ((r = memcmp(a->status.descriptor_digest, b->status.descriptor_digest,
  196. DIGEST_LEN)))
  197. return r;
  198. if ((r = (b->status.published_on - a->status.published_on)))
  199. return r;
  200. if ((r = strcmp(b->status.nickname, a->status.nickname)))
  201. return r;
  202. if ((r = (((int)b->status.addr) - ((int)a->status.addr))))
  203. return r;
  204. if ((r = (((int)b->status.or_port) - ((int)a->status.or_port))))
  205. return r;
  206. if ((r = (((int)b->status.dir_port) - ((int)a->status.dir_port))))
  207. return r;
  208. return 0;
  209. }
  210. /** Helper for sorting routerlists based on compare_vote_rs. */
  211. static int
  212. _compare_vote_rs(const void **_a, const void **_b)
  213. {
  214. const vote_routerstatus_t *a = *_a, *b = *_b;
  215. return compare_vote_rs(a,b);
  216. }
  217. /** Given a list of vote_routerstatus_t, all for the same router identity,
  218. * return whichever is most frequent, breaking ties in favor of more
  219. * recently published vote_routerstatus_t.
  220. */
  221. static vote_routerstatus_t *
  222. compute_routerstatus_consensus(smartlist_t *votes)
  223. {
  224. vote_routerstatus_t *most = NULL, *cur = NULL;
  225. int most_n = 0, cur_n = 0;
  226. time_t most_published = 0;
  227. smartlist_sort(votes, _compare_vote_rs);
  228. SMARTLIST_FOREACH(votes, vote_routerstatus_t *, rs,
  229. {
  230. if (cur && !compare_vote_rs(cur, rs)) {
  231. ++cur_n;
  232. } else {
  233. if (cur_n > most_n ||
  234. (cur && cur_n == most_n &&
  235. cur->status.published_on > most_published)) {
  236. most = cur;
  237. most_n = cur_n;
  238. most_published = cur->status.published_on;
  239. }
  240. cur_n = 1;
  241. cur = rs;
  242. }
  243. });
  244. if (cur_n > most_n ||
  245. (cur && cur_n == most_n && cur->status.published_on > most_published)) {
  246. most = cur;
  247. most_n = cur_n;
  248. most_published = cur->status.published_on;
  249. }
  250. tor_assert(most);
  251. return most;
  252. }
  253. /** Given a list of strings in <b>lst</b>, set the DIGEST_LEN-byte digest at
  254. * <b>digest_out</b> to the hash of the concatenation of those strings. */
  255. static void
  256. hash_list_members(char *digest_out, smartlist_t *lst)
  257. {
  258. crypto_digest_env_t *d = crypto_new_digest_env();
  259. SMARTLIST_FOREACH(lst, const char *, cp,
  260. crypto_digest_add_bytes(d, cp, strlen(cp)));
  261. crypto_digest_get_digest(d, digest_out, DIGEST_LEN);
  262. crypto_free_digest_env(d);
  263. }
  264. /** Given a list of vote networkstatus_vote_t in <b>votes</b>, our public
  265. * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
  266. * and the number of <b>total_authorities</b> that we believe exist in our
  267. * voting quorum, generate the text of a new v3 consensus vote, and return the
  268. * value in a newly allocated string. */
  269. char *
  270. networkstatus_compute_consensus(smartlist_t *votes,
  271. int total_authorities,
  272. crypto_pk_env_t *identity_key,
  273. crypto_pk_env_t *signing_key)
  274. {
  275. smartlist_t *chunks;
  276. char *result = NULL;
  277. time_t valid_after, fresh_until, valid_until;
  278. int vote_seconds, dist_seconds;
  279. char *client_versions = NULL, *server_versions = NULL;
  280. smartlist_t *flags;
  281. tor_assert(total_authorities >= smartlist_len(votes));
  282. if (!smartlist_len(votes)) {
  283. log_warn(LD_DIR, "Can't compute a consensus from no votes.");
  284. return NULL;
  285. }
  286. /* XXXX020 somebody needs to check vote authority. It could be this
  287. * function, it could be somebody else. */
  288. flags = smartlist_create();
  289. /* Compute medians of time-related things, and figure out how many
  290. * routers we might need to talk about. */
  291. {
  292. smartlist_t *va_times = smartlist_create();
  293. smartlist_t *fu_times = smartlist_create();
  294. smartlist_t *vu_times = smartlist_create();
  295. smartlist_t *votesec_list = smartlist_create();
  296. smartlist_t *distsec_list = smartlist_create();
  297. int n_versioning_clients = 0, n_versioning_servers = 0;
  298. smartlist_t *combined_client_versions = smartlist_create();
  299. smartlist_t *combined_server_versions = smartlist_create();
  300. int j;
  301. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  302. {
  303. tor_assert(v->is_vote);
  304. smartlist_add(va_times, &v->valid_after);
  305. smartlist_add(fu_times, &v->fresh_until);
  306. smartlist_add(vu_times, &v->valid_until);
  307. smartlist_add(votesec_list, &v->vote_seconds);
  308. smartlist_add(distsec_list, &v->dist_seconds);
  309. if (v->client_versions) {
  310. smartlist_t *cv = smartlist_create();
  311. ++n_versioning_clients;
  312. smartlist_split_string(cv, v->client_versions, ",",
  313. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  314. sort_version_list(cv, 1);
  315. smartlist_add_all(combined_client_versions, cv);
  316. smartlist_free(cv); /* elements get freed later. */
  317. }
  318. if (v->server_versions) {
  319. smartlist_t *sv = smartlist_create();
  320. ++n_versioning_servers;
  321. smartlist_split_string(sv, v->server_versions, ",",
  322. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  323. sort_version_list(sv, 1);
  324. smartlist_add_all(combined_server_versions, sv);
  325. smartlist_free(sv); /* elements get freed later. */
  326. }
  327. SMARTLIST_FOREACH(v->known_flags, const char *, cp,
  328. smartlist_add(flags, tor_strdup(cp)));
  329. });
  330. valid_after = median_time(va_times);
  331. fresh_until = median_time(fu_times);
  332. valid_until = median_time(vu_times);
  333. vote_seconds = median_int(votesec_list);
  334. dist_seconds = median_int(distsec_list);
  335. for (j = 0; j < 2; ++j) {
  336. smartlist_t *lst =
  337. j ? combined_server_versions : combined_client_versions;
  338. int min = (j ? n_versioning_servers : n_versioning_clients) / 2;
  339. smartlist_t *good = smartlist_create();
  340. char *res;
  341. sort_version_list(lst, 0);
  342. get_frequent_members(good, lst, min);
  343. res = smartlist_join_strings(good, ",", 0, NULL);
  344. if (j)
  345. server_versions = res;
  346. else
  347. client_versions = res;
  348. SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
  349. smartlist_free(good);
  350. smartlist_free(lst);
  351. }
  352. smartlist_sort_strings(flags);
  353. smartlist_uniq_strings(flags);
  354. smartlist_free(va_times);
  355. smartlist_free(fu_times);
  356. smartlist_free(vu_times);
  357. smartlist_free(votesec_list);
  358. smartlist_free(distsec_list);
  359. }
  360. chunks = smartlist_create();
  361. {
  362. char buf[1024];
  363. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  364. vu_buf[ISO_TIME_LEN+1];
  365. char *flaglist;
  366. format_iso_time(va_buf, valid_after);
  367. format_iso_time(fu_buf, fresh_until);
  368. format_iso_time(vu_buf, valid_until);
  369. flaglist = smartlist_join_strings(flags, " ", 0, NULL);
  370. tor_snprintf(buf, sizeof(buf),
  371. "network-status-version 3\n"
  372. "vote-status consensus\n"
  373. "valid-after %s\n"
  374. "fresh-until %s\n"
  375. "valid-until %s\n"
  376. "voting-delay %d %d\n"
  377. "client-versions %s\n"
  378. "server-versions %s\n"
  379. "known-flags %s\n",
  380. va_buf, fu_buf, vu_buf,
  381. vote_seconds, dist_seconds,
  382. client_versions, server_versions, flaglist);
  383. smartlist_add(chunks, tor_strdup(buf));
  384. tor_free(flaglist);
  385. }
  386. /* Sort the votes. */
  387. smartlist_sort(votes, _compare_votes_by_authority_id);
  388. /* Add the authority sections. */
  389. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  390. {
  391. char buf[1024];
  392. struct in_addr in;
  393. char ip[INET_NTOA_BUF_LEN];
  394. char fingerprint[HEX_DIGEST_LEN+1];
  395. char votedigest[HEX_DIGEST_LEN+1];
  396. networkstatus_voter_info_t *voter = get_voter(v);
  397. in.s_addr = htonl(voter->addr);
  398. tor_inet_ntoa(&in, ip, sizeof(ip));
  399. base16_encode(fingerprint, sizeof(fingerprint), voter->identity_digest,
  400. DIGEST_LEN);
  401. base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
  402. DIGEST_LEN);
  403. tor_snprintf(buf, sizeof(buf),
  404. "dir-source %s %s %s %s %d %d\n"
  405. "contact %s\n"
  406. "vote-digest %s\n",
  407. voter->nickname, fingerprint, voter->address, ip,
  408. voter->dir_port,
  409. voter->or_port,
  410. voter->contact,
  411. votedigest);
  412. smartlist_add(chunks, tor_strdup(buf));
  413. });
  414. /* Add the actual router entries. */
  415. {
  416. /* document these XXXX020 */
  417. int *index;
  418. int *size;
  419. int *flag_counts;
  420. int i;
  421. smartlist_t *matching_descs = smartlist_create();
  422. smartlist_t *chosen_flags = smartlist_create();
  423. smartlist_t *versions = smartlist_create();
  424. int *n_voter_flags; /* n_voter_flags[j] is the number of flags that
  425. * votes[j] knows about. */
  426. int *n_flag_voters; /* n_flag_voters[f] is the number of votes that care
  427. * about flags[f]. */
  428. int **flag_map; /* flag_map[j][b] is an index f such that flag_map[f]
  429. * is the same flag as votes[j]->known_flags[b]. */
  430. int *named_flag;
  431. index = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  432. size = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  433. n_voter_flags = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  434. n_flag_voters = tor_malloc_zero(sizeof(int) * smartlist_len(flags));
  435. flag_map = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  436. named_flag = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  437. for (i = 0; i < smartlist_len(votes); ++i)
  438. named_flag[i] = -1;
  439. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  440. {
  441. flag_map[v_sl_idx] = tor_malloc_zero(
  442. sizeof(int)*smartlist_len(v->known_flags));
  443. SMARTLIST_FOREACH(v->known_flags, const char *, fl,
  444. {
  445. int p = smartlist_string_pos(flags, fl);
  446. tor_assert(p >= 0);
  447. flag_map[v_sl_idx][fl_sl_idx] = p;
  448. ++n_flag_voters[p];
  449. if (!strcmp(fl, "Named"))
  450. named_flag[v_sl_idx] = fl_sl_idx;
  451. });
  452. n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
  453. size[v_sl_idx] = smartlist_len(v->routerstatus_list);
  454. });
  455. /* Now go through all the votes */
  456. flag_counts = tor_malloc(sizeof(int) * smartlist_len(flags));
  457. while (1) {
  458. vote_routerstatus_t *rs;
  459. routerstatus_t rs_out;
  460. const char *lowest_id = NULL;
  461. const char *chosen_version;
  462. const char *chosen_name = NULL;
  463. int naming_conflict = 0;
  464. int n_listing = 0;
  465. int i;
  466. char buf[256];
  467. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
  468. if (index[v_sl_idx] < size[v_sl_idx]) {
  469. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  470. if (!lowest_id ||
  471. memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN) < 0)
  472. lowest_id = rs->status.identity_digest;
  473. }
  474. });
  475. if (!lowest_id) /* we're out of routers. */
  476. break;
  477. memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
  478. smartlist_clear(matching_descs);
  479. smartlist_clear(chosen_flags);
  480. smartlist_clear(versions);
  481. /* Okay, go through all the entries for this digest. */
  482. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
  483. if (index[v_sl_idx] >= size[v_sl_idx])
  484. continue; /* out of entries. */
  485. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  486. if (memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN))
  487. continue; /* doesn't include this router. */
  488. /* At this point, we know that we're looking at a routersatus with
  489. * identity "lowest".
  490. */
  491. ++index[v_sl_idx];
  492. ++n_listing;
  493. smartlist_add(matching_descs, rs);
  494. if (rs->version && rs->version[0])
  495. smartlist_add(versions, rs->version);
  496. /* Tally up all the flags. */
  497. for (i = 0; i < n_voter_flags[v_sl_idx]; ++i) {
  498. if (rs->flags & (U64_LITERAL(1) << i))
  499. ++flag_counts[flag_map[v_sl_idx][i]];
  500. }
  501. if (rs->flags & (U64_LITERAL(1) << named_flag[v_sl_idx])) {
  502. if (chosen_name && strcmp(chosen_name, rs->status.nickname))
  503. naming_conflict = 1; /* XXXX020 warn? */
  504. chosen_name = rs->status.nickname;
  505. }
  506. });
  507. /* We don't include this router at all unless more than half of
  508. * the authorities we believe in list it. */
  509. if (n_listing <= total_authorities/2)
  510. continue;
  511. /* Figure out the most popular opinion of what the most recent
  512. * routerinfo and its contents are. */
  513. rs = compute_routerstatus_consensus(matching_descs);
  514. /* Copy bits of that into rs_out. */
  515. tor_assert(!memcmp(lowest_id, rs->status.identity_digest, DIGEST_LEN));
  516. memcpy(rs_out.identity_digest, lowest_id, DIGEST_LEN);
  517. memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
  518. DIGEST_LEN);
  519. rs_out.addr = rs->status.addr;
  520. rs_out.published_on = rs->status.published_on;
  521. rs_out.dir_port = rs->status.dir_port;
  522. rs_out.or_port = rs->status.or_port;
  523. if (chosen_name && !naming_conflict) {
  524. strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
  525. } else {
  526. strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
  527. }
  528. /* Set the flags. */
  529. smartlist_add(chosen_flags, (char*)"s"); /* for the start of the line. */
  530. SMARTLIST_FOREACH(flags, const char *, fl,
  531. {
  532. if (strcmp(fl, "Named")) {
  533. if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2)
  534. smartlist_add(chosen_flags, (char*)fl);
  535. } else {
  536. if (!naming_conflict && flag_counts[fl_sl_idx])
  537. smartlist_add(chosen_flags, (char*)"Named");
  538. }
  539. });
  540. /* Pick the version. */
  541. if (smartlist_len(versions)) {
  542. sort_version_list(versions, 0);
  543. chosen_version = get_most_frequent_member(versions);
  544. } else {
  545. chosen_version = NULL;
  546. }
  547. /* Okay!! Now we can write the descriptor... */
  548. /* First line goes into "buf". */
  549. routerstatus_format_entry(buf, sizeof(buf), &rs_out, NULL, 1);
  550. smartlist_add(chunks, tor_strdup(buf));
  551. /* Second line is all flags. The "\n" is missing. */
  552. smartlist_add(chunks,
  553. smartlist_join_strings(chosen_flags, " ", 0, NULL));
  554. /* Now the version line. */
  555. if (chosen_version) {
  556. /* XXXX020 fails on very long version string */
  557. tor_snprintf(buf, sizeof(buf), "\nv %s\n", chosen_version);
  558. smartlist_add(chunks, tor_strdup(buf));
  559. } else {
  560. smartlist_add(chunks, tor_strdup("\n"));
  561. }
  562. /* And the loop is over and we move on to the next router */
  563. }
  564. tor_free(index);
  565. tor_free(size);
  566. tor_free(n_voter_flags);
  567. tor_free(n_flag_voters);
  568. for (i = 0; i < smartlist_len(votes); ++i)
  569. tor_free(flag_map[i]);
  570. tor_free(flag_map);
  571. tor_free(flag_counts);
  572. smartlist_free(matching_descs);
  573. smartlist_free(chosen_flags);
  574. smartlist_free(versions);
  575. }
  576. /* Add a signature. */
  577. {
  578. char digest[DIGEST_LEN];
  579. char fingerprint[HEX_DIGEST_LEN+1];
  580. char signing_key_fingerprint[HEX_DIGEST_LEN+1];
  581. char buf[4096];
  582. smartlist_add(chunks, tor_strdup("directory-signature "));
  583. /* Compute the hash of the chunks. */
  584. hash_list_members(digest, chunks);
  585. /* Get the fingerprints */
  586. crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
  587. crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
  588. /* add the junk that will go at the end of the line. */
  589. tor_snprintf(buf, sizeof(buf), "%s %s\n", fingerprint,
  590. signing_key_fingerprint);
  591. /* And the signature. */
  592. /* XXXX020 check return */
  593. router_append_dirobj_signature(buf, sizeof(buf), digest, signing_key);
  594. smartlist_add(chunks, tor_strdup(buf));
  595. }
  596. result = smartlist_join_strings(chunks, "", 0, NULL);
  597. tor_free(client_versions);
  598. tor_free(server_versions);
  599. smartlist_free(flags);
  600. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  601. smartlist_free(chunks);
  602. {
  603. networkstatus_vote_t *c;
  604. if (!(c = networkstatus_parse_vote_from_string(result, 0))) {
  605. log_err(LD_BUG,"Generated a networkstatus consensus we couldn't "
  606. "parse.");
  607. tor_free(result);
  608. return NULL;
  609. }
  610. networkstatus_vote_free(c);
  611. }
  612. return result;
  613. }
  614. /** Check whether the pending_signature on <b>voter</b> is correctly signed by
  615. * the signing key of <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
  616. * signing key; otherwise set the good_signature or bad_signature flag on
  617. * <b>voter</b>, and return 0. */
  618. /* (private; exposed for testing.) */
  619. int
  620. networkstatus_check_voter_signature(networkstatus_vote_t *consensus,
  621. networkstatus_voter_info_t *voter,
  622. authority_cert_t *cert)
  623. {
  624. char d[DIGEST_LEN];
  625. char *signed_digest;
  626. size_t signed_digest_len;
  627. /*XXXX020 check return*/
  628. crypto_pk_get_digest(cert->signing_key, d);
  629. if (memcmp(voter->signing_key_digest, d, DIGEST_LEN)) {
  630. return -1;
  631. }
  632. signed_digest_len = crypto_pk_keysize(cert->signing_key);
  633. signed_digest = tor_malloc(signed_digest_len);
  634. if (crypto_pk_public_checksig(cert->signing_key,
  635. signed_digest,
  636. voter->signature,
  637. voter->signature_len) != DIGEST_LEN ||
  638. memcmp(signed_digest, consensus->networkstatus_digest, DIGEST_LEN)) {
  639. log_warn(LD_DIR, "Got a bad signature."); /*XXXX020 say more*/
  640. voter->bad_signature = 1;
  641. } else {
  642. voter->good_signature = 1;
  643. }
  644. /* XXXX020 did anything rely on this?
  645. * also, rename so it's no longer called "pending". */
  646. // tor_free(voter->signature);
  647. return 0;
  648. }
  649. /** DOCDOC */
  650. int
  651. networkstatus_check_consensus_signature(networkstatus_vote_t *consensus)
  652. {
  653. int n_good = 0;
  654. int n_missing_key = 0;
  655. int n_bad = 0;
  656. int n_unknown = 0;
  657. int n_no_signature = 0;
  658. int n_required = 1; /* XXXX020 This is completely wrong. */
  659. tor_assert(! consensus->is_vote);
  660. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, voter,
  661. {
  662. if (!voter->good_signature && !voter->bad_signature && voter->signature) {
  663. /* we can try to check the signature. */
  664. authority_cert_t *cert =
  665. authority_cert_get_by_digests(voter->identity_digest,
  666. voter->signing_key_digest);
  667. if (! cert) {
  668. ++n_unknown;
  669. continue;
  670. }
  671. if (networkstatus_check_voter_signature(consensus, voter, cert) < 0) {
  672. ++n_missing_key; /* XXXX020 what, really? */
  673. continue;
  674. }
  675. }
  676. if (voter->good_signature)
  677. ++n_good;
  678. else if (voter->bad_signature)
  679. ++n_bad;
  680. else
  681. ++n_no_signature;
  682. });
  683. if (n_good > n_required)
  684. return 0;
  685. else
  686. return -1;
  687. }
  688. /** DOCDOC */
  689. static int
  690. networkstatus_add_signatures_impl(networkstatus_vote_t *target,
  691. smartlist_t *src_voter_list,
  692. char **new_signatures_out)
  693. {
  694. smartlist_t *added_signatures, *sigs;
  695. int r;
  696. tor_assert(target);
  697. tor_assert(!target->is_vote);
  698. tor_assert(new_signatures_out);
  699. added_signatures = smartlist_create();
  700. /* For each voter in src... */
  701. SMARTLIST_FOREACH(src_voter_list, networkstatus_voter_info_t *, src_voter,
  702. {
  703. networkstatus_voter_info_t *target_voter =
  704. networkstatus_get_voter_by_id(target, src_voter->identity_digest);
  705. authority_cert_t *cert;
  706. /* If the target a doesn't know about this voter, then forget it. */
  707. if (!target_voter)
  708. continue;
  709. /* If the target already has a good signature from this voter, then skip
  710. * this one. */
  711. if (target_voter->good_signature)
  712. continue;
  713. /* Try checking the signature if we haven't already. */
  714. if (!src_voter->good_signature && !src_voter->bad_signature) {
  715. cert = authority_cert_get_by_digests(src_voter->identity_digest,
  716. src_voter->signing_key_digest);
  717. if (cert) {
  718. networkstatus_check_voter_signature(target, src_voter, cert);
  719. }
  720. }
  721. /* If this signature is good, then replace and add. */
  722. if (src_voter->good_signature || !target_voter->signature) {
  723. tor_free(target_voter->signature);
  724. target_voter->signature =
  725. tor_memdup(src_voter->signature, src_voter->signature_len);
  726. memcpy(target_voter->signing_key_digest, src_voter->signing_key_digest,
  727. DIGEST_LEN);
  728. target_voter->signature_len = src_voter->signature_len;
  729. target_voter->good_signature = 1;
  730. target_voter->bad_signature = 0;
  731. smartlist_add(added_signatures, target_voter);
  732. }
  733. });
  734. sigs = smartlist_create();
  735. SMARTLIST_FOREACH(added_signatures, networkstatus_voter_info_t *, v,
  736. {
  737. char buf[4096];
  738. char sk[HEX_DIGEST_LEN+1];
  739. char ik[HEX_DIGEST_LEN+1];
  740. tor_assert(v->signature);
  741. base16_encode(sk, sizeof(sk), v->signing_key_digest, DIGEST_LEN);
  742. base16_encode(ik, sizeof(ik), v->identity_digest, DIGEST_LEN);
  743. tor_snprintf(buf, sizeof(buf), "directory-signature %s %s\n"
  744. "-----BEGIN SIGNATURE-----\n", ik, sk);
  745. smartlist_add(sigs, tor_strdup(buf));
  746. base64_encode(buf, sizeof(buf), v->signature, v->signature_len);
  747. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  748. smartlist_add(sigs, tor_strdup(buf));
  749. });
  750. *new_signatures_out = smartlist_join_strings(sigs, "", 0, NULL);
  751. SMARTLIST_FOREACH(sigs, char *, cp, tor_free(cp));
  752. smartlist_free(sigs);
  753. r = smartlist_len(added_signatures);
  754. smartlist_free(added_signatures);
  755. return r;
  756. }
  757. /** DOCDOC */
  758. int
  759. networkstatus_add_consensus_signatures(networkstatus_vote_t *target,
  760. networkstatus_vote_t *src,
  761. char **new_signatures_out)
  762. {
  763. tor_assert(src);
  764. tor_assert(! src->is_vote);
  765. *new_signatures_out = NULL;
  766. /* Are they the same consensus? */
  767. if (memcmp(target->networkstatus_digest, src->networkstatus_digest,
  768. DIGEST_LEN))
  769. return -1;
  770. if (target == src)
  771. return 0;
  772. return networkstatus_add_signatures_impl(target, src->voters,
  773. new_signatures_out);
  774. }
  775. /** DOCDOC */
  776. int
  777. networkstatus_add_detached_signatures(networkstatus_vote_t *target,
  778. ns_detached_signatures_t *sigs,
  779. char **new_signatures_out)
  780. {
  781. tor_assert(sigs);
  782. *new_signatures_out = NULL;
  783. /* Are they the same consensus? */
  784. if (memcmp(target->networkstatus_digest, sigs->networkstatus_digest,
  785. DIGEST_LEN))
  786. return -1;
  787. return networkstatus_add_signatures_impl(target, sigs->signatures,
  788. new_signatures_out);
  789. }
  790. /** DOCDOC */
  791. char *
  792. networkstatus_get_detached_signatures(networkstatus_vote_t *consensus)
  793. {
  794. smartlist_t *elements;
  795. char buf[4096];
  796. char *result = NULL;
  797. int n_sigs = 0;
  798. tor_assert(consensus);
  799. tor_assert(! consensus->is_vote);
  800. elements = smartlist_create();
  801. {
  802. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  803. vu_buf[ISO_TIME_LEN+1];
  804. char d[HEX_DIGEST_LEN+1];
  805. base16_encode(d, sizeof(d), consensus->networkstatus_digest, DIGEST_LEN);
  806. format_iso_time(va_buf, consensus->valid_after);
  807. format_iso_time(fu_buf, consensus->fresh_until);
  808. format_iso_time(vu_buf, consensus->valid_until);
  809. tor_snprintf(buf, sizeof(buf),
  810. "consensus-digest %s\n"
  811. "valid-after %s\n"
  812. "fresh-until %s\n"
  813. "valid-until %s\n", d, va_buf, fu_buf, vu_buf);
  814. smartlist_add(elements, tor_strdup(buf));
  815. }
  816. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, v,
  817. {
  818. char sk[HEX_DIGEST_LEN+1];
  819. char id[HEX_DIGEST_LEN+1];
  820. if (!v->signature) // XXXX020 || !v->good_signature)
  821. continue;
  822. ++n_sigs;
  823. base16_encode(sk, sizeof(sk), v->signing_key_digest, DIGEST_LEN);
  824. base16_encode(id, sizeof(id), v->identity_digest, DIGEST_LEN);
  825. tor_snprintf(buf, sizeof(buf),
  826. "directory-signature %s %s\n-----BEGIN SIGNATURE-----\n",
  827. id, sk);
  828. smartlist_add(elements, tor_strdup(buf));
  829. base64_encode(buf, sizeof(buf), v->signature, v->signature_len);
  830. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  831. smartlist_add(elements, tor_strdup(buf));
  832. });
  833. result = smartlist_join_strings(elements, "", 0, NULL);
  834. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  835. smartlist_free(elements);
  836. if (!n_sigs)
  837. tor_free(result);
  838. return result;
  839. }
  840. /** DOCDOC */
  841. void
  842. ns_detached_signatures_free(ns_detached_signatures_t *s)
  843. {
  844. if (s->signatures) {
  845. SMARTLIST_FOREACH(s->signatures, networkstatus_voter_info_t *, v,
  846. {
  847. tor_free(v->signature);
  848. tor_free(v);
  849. });
  850. smartlist_free(s->signatures);
  851. }
  852. tor_free(s);
  853. }
  854. /* =====
  855. * Certificate functions
  856. * ===== */
  857. /** Free storage held in <b>cert</b>. */
  858. void
  859. authority_cert_free(authority_cert_t *cert)
  860. {
  861. if (!cert)
  862. return;
  863. tor_free(cert->cache_info.signed_descriptor_body);
  864. if (cert->signing_key)
  865. crypto_free_pk_env(cert->signing_key);
  866. if (cert->identity_key)
  867. crypto_free_pk_env(cert->identity_key);
  868. tor_free(cert);
  869. }
  870. /** Allocate and return a new authority_cert_t with the same contents as
  871. * <b>cert</b>. */
  872. authority_cert_t *
  873. authority_cert_dup(authority_cert_t *cert)
  874. {
  875. authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
  876. tor_assert(cert);
  877. memcpy(out, cert, sizeof(authority_cert_t));
  878. /* Now copy pointed-to things. */
  879. out->cache_info.signed_descriptor_body =
  880. tor_strndup(cert->cache_info.signed_descriptor_body,
  881. cert->cache_info.signed_descriptor_len);
  882. out->cache_info.saved_location = SAVED_NOWHERE;
  883. out->identity_key = crypto_pk_dup_key(cert->identity_key);
  884. out->signing_key = crypto_pk_dup_key(cert->signing_key);
  885. return out;
  886. }
  887. /* =====
  888. * Vote scheduling
  889. * ===== */
  890. /** DOCDOC */
  891. void
  892. dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
  893. {
  894. or_options_t *options = get_options();
  895. tor_assert(timing_out);
  896. timing_out->vote_interval = options->V3AuthVotingInterval;
  897. /* XXXX020 make these configurable. */
  898. timing_out->n_intervals_valid = 3;
  899. timing_out->vote_delay = 300;
  900. timing_out->dist_delay = 300;
  901. }
  902. /** DOCDOC */
  903. time_t
  904. dirvote_get_start_of_next_interval(time_t now, int interval)
  905. {
  906. struct tm tm;
  907. time_t midnight_today;
  908. time_t midnight_tomorrow;
  909. time_t next;
  910. tor_gmtime_r(&now, &tm);
  911. tm.tm_hour = 0;
  912. tm.tm_min = 0;
  913. tm.tm_sec = 0;
  914. midnight_today = tor_timegm(&tm);
  915. midnight_tomorrow = midnight_today + (24*60*60);
  916. next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
  917. if (next > midnight_tomorrow)
  918. next = midnight_tomorrow;
  919. return next;
  920. }
  921. /** DOCDOC */
  922. static struct {
  923. time_t voting_starts;
  924. time_t voting_ends;
  925. time_t interval_starts;
  926. time_t discard_old_votes;
  927. int have_voted;
  928. int have_built_consensus;
  929. int have_published_consensus;
  930. } voting_schedule = {0,0,0,0,0,0,0};
  931. /** DOCDOC */
  932. void
  933. dirvote_recalculate_timing(time_t now)
  934. {
  935. int interval, vote_delay, dist_delay;
  936. time_t start;
  937. networkstatus_vote_t *consensus = networkstatus_get_latest_consensus();
  938. memset(&voting_schedule, 0, sizeof(voting_schedule));
  939. if (consensus) {
  940. /* XXXX020 sanity-check these somewhere! */
  941. interval = consensus->fresh_until - consensus->valid_after;
  942. vote_delay = consensus->vote_seconds;
  943. dist_delay = consensus->dist_seconds;
  944. } else {
  945. /* XXXX020 is this correct according the the spec? */
  946. /* XXXX020 drop this back down to 60 minutes, or whatever the spec says. */
  947. interval = 1200;
  948. vote_delay = dist_delay = 300;
  949. }
  950. start = voting_schedule.interval_starts =
  951. dirvote_get_start_of_next_interval(now,interval);
  952. voting_schedule.voting_ends = start - vote_delay;
  953. voting_schedule.voting_starts = start - vote_delay - dist_delay;
  954. voting_schedule.discard_old_votes = start + 600; /* XXXX020 */
  955. }
  956. /** DOCDOC */
  957. void
  958. dirvote_act(time_t now)
  959. {
  960. if (!voting_schedule.voting_starts)
  961. dirvote_recalculate_timing(now);
  962. if (voting_schedule.voting_starts < now && !voting_schedule.have_voted) {
  963. dirvote_perform_vote();
  964. voting_schedule.have_voted = 1;
  965. }
  966. /* XXXX020 after a couple minutes here, start trying to fetch votes. */
  967. if (voting_schedule.voting_ends < now &&
  968. !voting_schedule.have_built_consensus) {
  969. dirvote_compute_consensus();
  970. /* XXXX020 we will want to try again later if we haven't got enough
  971. * votes yet. */
  972. voting_schedule.have_built_consensus = 1;
  973. }
  974. if (voting_schedule.interval_starts < now &&
  975. !voting_schedule.have_published_consensus) {
  976. dirvote_publish_consensus();
  977. /* XXXX020 we will want to try again later if we haven't got enough
  978. * signatures yet. */
  979. voting_schedule.have_published_consensus = 1;
  980. }
  981. if (voting_schedule.discard_old_votes < now) {
  982. dirvote_clear_pending_votes();
  983. dirvote_recalculate_timing(now);
  984. }
  985. }
  986. /** DOCDOC */
  987. typedef struct pending_vote_t {
  988. cached_dir_t *vote_body;
  989. networkstatus_vote_t *vote;
  990. } pending_vote_t;
  991. /** DOCDOC */
  992. static smartlist_t *pending_vote_list = NULL;
  993. /** DOCDOC */
  994. static char *pending_consensus_body = NULL;
  995. /** DOCDOC */
  996. static char *pending_consensus_signatures = NULL;
  997. /** DOCDOC */
  998. static networkstatus_vote_t *pending_consensus = NULL;
  999. /** DOCDOC */
  1000. static smartlist_t *pending_consensus_signature_list = NULL;
  1001. /** DOCDOC */
  1002. void
  1003. dirvote_perform_vote(void)
  1004. {
  1005. cached_dir_t *new_vote = generate_v3_networkstatus();
  1006. pending_vote_t *pending_vote;
  1007. const char *msg = "";
  1008. if (!new_vote)
  1009. return;
  1010. if (!(pending_vote = dirvote_add_vote(new_vote->dir, &msg))) {
  1011. log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
  1012. msg);
  1013. return;
  1014. }
  1015. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
  1016. ROUTER_PURPOSE_GENERAL,
  1017. V3_AUTHORITY,
  1018. pending_vote->vote_body->dir,
  1019. pending_vote->vote_body->dir_len, 0);
  1020. }
  1021. /** DOCDOC */
  1022. void
  1023. dirvote_clear_pending_votes(void)
  1024. {
  1025. if (pending_vote_list) {
  1026. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1027. cached_dir_decref(v->vote_body);
  1028. v->vote_body = NULL;
  1029. networkstatus_vote_free(v->vote);
  1030. tor_free(v);
  1031. });
  1032. smartlist_clear(pending_vote_list);
  1033. }
  1034. if (pending_consensus_signature_list) {
  1035. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
  1036. tor_free(cp));
  1037. smartlist_clear(pending_consensus_signature_list);
  1038. }
  1039. }
  1040. /** DOCDOC */
  1041. pending_vote_t *
  1042. dirvote_add_vote(const char *vote_body, const char **msg_out)
  1043. {
  1044. networkstatus_vote_t *vote;
  1045. networkstatus_voter_info_t *vi;
  1046. trusted_dir_server_t *ds;
  1047. pending_vote_t *pending_vote = NULL;
  1048. tor_assert(vote_body);
  1049. tor_assert(msg_out);
  1050. if (!pending_vote_list)
  1051. pending_vote_list = smartlist_create();
  1052. *msg_out = NULL;
  1053. vote = networkstatus_parse_vote_from_string(vote_body, 1);
  1054. if (!vote) {
  1055. *msg_out = "Unable to parse vote";
  1056. goto err;
  1057. }
  1058. tor_assert(smartlist_len(vote->voters) == 1);
  1059. vi = smartlist_get(vote->voters, 0);
  1060. tor_assert(vi->good_signature == 1);
  1061. ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
  1062. if (!ds || !(ds->type & V3_AUTHORITY)) {
  1063. *msg_out = "Vote not from a recognized v3 authority";
  1064. goto err;
  1065. }
  1066. tor_assert(vote->cert);
  1067. if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
  1068. vote->cert->signing_key_digest)) {
  1069. /* Hey, it's a new cert! */
  1070. trusted_dirs_load_certs_from_string(
  1071. vote->cert->cache_info.signed_descriptor_body,
  1072. 0 /* from_store */);
  1073. }
  1074. /* XXXX020 check times; make sure epochs match. */
  1075. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
  1076. if (! memcmp(v->vote->cert->cache_info.identity_digest,
  1077. vote->cert->cache_info.identity_digest,
  1078. DIGEST_LEN)) {
  1079. log_notice(LD_DIR, "We already have a pending vote from this dir");
  1080. if (v->vote->published < vote->published) {
  1081. cached_dir_decref(v->vote_body);
  1082. networkstatus_vote_free(v->vote);
  1083. v->vote_body = new_cached_dir(tor_strdup(vote_body),
  1084. vote->published);
  1085. v->vote = vote;
  1086. *msg_out = "ok";
  1087. return v;
  1088. } else {
  1089. *msg_out = "Already have a newer pending vote";
  1090. goto err;
  1091. }
  1092. }
  1093. });
  1094. pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
  1095. pending_vote->vote_body = new_cached_dir(tor_strdup(vote_body),
  1096. vote->published);
  1097. pending_vote->vote = vote;
  1098. smartlist_add(pending_vote_list, pending_vote);
  1099. *msg_out = "ok";
  1100. return pending_vote;
  1101. err:
  1102. if (vote)
  1103. networkstatus_vote_free(vote);
  1104. if (!*msg_out)
  1105. *msg_out = "Error adding vote";
  1106. /*XXXX020 free other fields */
  1107. return NULL;
  1108. }
  1109. /** DOCDOC */
  1110. int
  1111. dirvote_compute_consensus(void)
  1112. {
  1113. /* Have we got enough votes to try? */
  1114. int n_votes, n_voters;
  1115. smartlist_t *votes = NULL;
  1116. char *consensus_body = NULL, *signatures = NULL;
  1117. networkstatus_vote_t *consensus = NULL;
  1118. authority_cert_t *my_cert;
  1119. if (!pending_vote_list)
  1120. pending_vote_list = smartlist_create();
  1121. n_voters = get_n_authorities(V3_AUTHORITY);
  1122. n_votes = smartlist_len(pending_vote_list);
  1123. /* XXXX020 see if there are enough to go ahead. */
  1124. if (!(my_cert = get_my_v3_authority_cert())) {
  1125. log_warn(LD_DIR, "Can't generate consensus without a certificate.");
  1126. goto err;
  1127. }
  1128. votes = smartlist_create();
  1129. SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
  1130. smartlist_add(votes, v->vote));
  1131. consensus_body = networkstatus_compute_consensus(
  1132. votes, n_voters,
  1133. my_cert->identity_key,
  1134. get_my_v3_authority_signing_key());
  1135. if (!consensus_body) {
  1136. log_warn(LD_DIR, "Couldn't generate a consensus at all!");
  1137. goto err;
  1138. }
  1139. consensus = networkstatus_parse_vote_from_string(consensus_body, 0);
  1140. if (!consensus) {
  1141. log_warn(LD_DIR, "Couldn't parse consensus we generated!");
  1142. goto err;
  1143. }
  1144. signatures = networkstatus_get_detached_signatures(consensus);
  1145. if (!signatures) {
  1146. log_warn(LD_DIR, "Couldn't extract signatures.");
  1147. goto err;
  1148. }
  1149. tor_free(pending_consensus_body);
  1150. pending_consensus_body = consensus_body;
  1151. tor_free(pending_consensus_signatures);
  1152. pending_consensus_signatures = signatures;
  1153. if (pending_consensus)
  1154. networkstatus_vote_free(pending_consensus);
  1155. pending_consensus = consensus;
  1156. if (pending_consensus_signature_list) {
  1157. /* we may have gotten signatures for this consensus before we built
  1158. * it ourself. Add them now. */
  1159. SMARTLIST_FOREACH(pending_consensus_signature_list, char *, sig,
  1160. {
  1161. const char *msg = NULL;
  1162. dirvote_add_signatures_to_pending_consensus(sig, &msg);
  1163. /* XXXX020 log result. */
  1164. tor_free(sig);
  1165. });
  1166. smartlist_clear(pending_consensus_signature_list);
  1167. }
  1168. return 0;
  1169. err:
  1170. if (votes)
  1171. smartlist_free(votes);
  1172. tor_free(consensus_body);
  1173. tor_free(signatures);
  1174. networkstatus_vote_free(consensus);
  1175. return -1;
  1176. }
  1177. /** DOCDOC */
  1178. static int
  1179. dirvote_add_signatures_to_pending_consensus(
  1180. const char *detached_signatures_body,
  1181. const char **msg_out)
  1182. {
  1183. ns_detached_signatures_t *sigs = NULL;
  1184. int r = -1;
  1185. char *new_signatures = NULL;
  1186. size_t siglen;
  1187. tor_assert(detached_signatures_body);
  1188. tor_assert(msg_out);
  1189. /* Only call if we have a pending consensus right now. */
  1190. tor_assert(pending_consensus);
  1191. tor_assert(pending_consensus_body);
  1192. tor_assert(pending_consensus_signatures);
  1193. *msg_out = NULL;
  1194. if (!(sigs = networkstatus_parse_detached_signatures(
  1195. detached_signatures_body, NULL))) {
  1196. *msg_out = "Couldn't parse detached signatures.";
  1197. goto err;
  1198. }
  1199. r = networkstatus_add_detached_signatures(pending_consensus,
  1200. sigs,
  1201. &new_signatures);
  1202. if (new_signatures && (siglen = strlen(new_signatures)) && r >= 0) {
  1203. size_t siglen = strlen(new_signatures);
  1204. size_t len = strlen(pending_consensus_body);
  1205. pending_consensus_body = tor_realloc(pending_consensus_body,
  1206. len+siglen+1);
  1207. memcpy(pending_consensus_body+len, new_signatures, siglen+1);
  1208. len = strlen(pending_consensus_signatures);
  1209. pending_consensus_signatures = tor_realloc(pending_consensus_signatures,
  1210. len+siglen+1);
  1211. memcpy(pending_consensus_signatures+len, new_signatures, siglen+1);
  1212. log_info(LD_DIR, "Added %d new signatures to the pending consensus.", r);
  1213. }
  1214. *msg_out = "ok";
  1215. goto done;
  1216. err:
  1217. if (!msg_out)
  1218. *msg_out = "Unrecognized error while adding detached signatures.";
  1219. done:
  1220. tor_free(new_signatures);
  1221. if (sigs)
  1222. ns_detached_signatures_free(sigs);
  1223. return r;
  1224. }
  1225. /** DOCDOC */
  1226. int
  1227. dirvote_add_signatures(const char *detached_signatures_body)
  1228. {
  1229. if (pending_consensus) {
  1230. const char *msg=NULL;
  1231. return dirvote_add_signatures_to_pending_consensus(
  1232. detached_signatures_body, &msg);
  1233. } else {
  1234. if (!pending_consensus_signature_list)
  1235. pending_consensus_signature_list = smartlist_create();
  1236. smartlist_add(pending_consensus_signature_list,
  1237. tor_strdup(detached_signatures_body));
  1238. return 0;
  1239. }
  1240. }
  1241. /** DOCDOC */
  1242. int
  1243. dirvote_publish_consensus(void)
  1244. {
  1245. /* Can we actually publish it yet? */
  1246. if (!pending_consensus ||
  1247. networkstatus_check_consensus_signature(pending_consensus)<0)
  1248. return -1;
  1249. networkstatus_set_current_consensus(pending_consensus_body, 0);
  1250. return 0;
  1251. }
  1252. /** Release all static storage held in dirvote.c */
  1253. void
  1254. dirvote_free_all(void)
  1255. {
  1256. dirvote_clear_pending_votes();
  1257. if (pending_vote_list) {
  1258. /* now empty as a result of clear_pending_votes. */
  1259. smartlist_free(pending_vote_list);
  1260. pending_vote_list = NULL;
  1261. }
  1262. tor_free(pending_consensus_body);
  1263. tor_free(pending_consensus_signatures);
  1264. if (pending_consensus) {
  1265. networkstatus_vote_free(pending_consensus);
  1266. pending_consensus = NULL;
  1267. }
  1268. if (pending_consensus_signature_list) {
  1269. /* now empty as a result of clear_pending_votes. */
  1270. smartlist_free(pending_consensus_signature_list);
  1271. pending_consensus_signature_list = NULL;
  1272. }
  1273. }