dirvote.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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. **/
  12. /** Clear all storage held in <b>ns</b>. */
  13. void
  14. networkstatus_vote_free(networkstatus_vote_t *ns)
  15. {
  16. if (!ns)
  17. return;
  18. tor_free(ns->client_versions);
  19. tor_free(ns->server_versions);
  20. if (ns->known_flags) {
  21. SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
  22. smartlist_free(ns->known_flags);
  23. }
  24. if (ns->voters) {
  25. SMARTLIST_FOREACH(ns->voters, networkstatus_voter_info_t *, voter,
  26. {
  27. tor_free(voter->nickname);
  28. tor_free(voter->address);
  29. tor_free(voter->contact);
  30. });
  31. smartlist_free(ns->voters);
  32. }
  33. if (ns->cert)
  34. authority_cert_free(ns->cert);
  35. if (ns->routerstatus_list) {
  36. if (ns->is_vote) {
  37. SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
  38. {
  39. tor_free(rs->version);
  40. tor_free(rs);
  41. });
  42. } else {
  43. SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
  44. tor_free(rs));
  45. }
  46. smartlist_free(ns->routerstatus_list);
  47. }
  48. memset(ns, 11, sizeof(*ns));
  49. tor_free(ns);
  50. }
  51. /** Return the voter info from <b>vote</b> for the voter whose identity digest
  52. * is <b>identity</b>, or NULL if no such voter is associated with
  53. * <b>vote</b>. */
  54. networkstatus_voter_info_t *
  55. networkstatus_get_voter_by_id(networkstatus_vote_t *vote,
  56. const char *identity)
  57. {
  58. if (!vote || !vote->voters)
  59. return NULL;
  60. SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
  61. if (!memcmp(voter->identity_digest, identity, DIGEST_LEN))
  62. return voter);
  63. return NULL;
  64. }
  65. /** Helper for sorting a list of time_t*. */
  66. static int
  67. _compare_times(const void **_a, const void **_b)
  68. {
  69. const time_t *a = *_a, *b = *_b;
  70. if (*a<*b)
  71. return -1;
  72. else if (*a>*b)
  73. return 1;
  74. else
  75. return 0;
  76. }
  77. /** Helper for sorting a list of int*. */
  78. static int
  79. _compare_ints(const void **_a, const void **_b)
  80. {
  81. const int *a = *_a, *b = *_b;
  82. if (*a<*b)
  83. return -1;
  84. else if (*a>*b)
  85. return 1;
  86. else
  87. return 0;
  88. }
  89. /** Given a list of one or more time_t*, return the (low) median. */
  90. static time_t
  91. median_time(smartlist_t *times)
  92. {
  93. int idx;
  94. tor_assert(smartlist_len(times));
  95. smartlist_sort(times, _compare_times);
  96. idx = (smartlist_len(times)-1)/2;
  97. return *(time_t*)smartlist_get(times, idx);
  98. }
  99. /** Given a list of one or more int*, return the (low) median. */
  100. static int
  101. median_int(smartlist_t *ints)
  102. {
  103. int idx;
  104. tor_assert(smartlist_len(ints));
  105. smartlist_sort(ints, _compare_ints);
  106. idx = (smartlist_len(ints)-1)/2;
  107. return *(time_t*)smartlist_get(ints, idx);
  108. }
  109. /** Given a vote <b>vote</b> (not a consensus!), return its associated
  110. * networkstatus_voter_info_t.*/
  111. static networkstatus_voter_info_t *
  112. get_voter(const networkstatus_vote_t *vote)
  113. {
  114. tor_assert(vote);
  115. tor_assert(vote->is_vote);
  116. tor_assert(vote->voters);
  117. tor_assert(smartlist_len(vote->voters) == 1);
  118. return smartlist_get(vote->voters, 0);
  119. }
  120. /** Helper for sorting networkstatus_vote_t votes (not consensuses) by the
  121. * hash of their voters' identity digests. */
  122. static int
  123. _compare_votes_by_authority_id(const void **_a, const void **_b)
  124. {
  125. const networkstatus_vote_t *a = *_a, *b = *_b;
  126. return memcmp(get_voter(a)->identity_digest,
  127. get_voter(b)->identity_digest, DIGEST_LEN);
  128. }
  129. /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
  130. * that occurs more than <b>min</b> times. */
  131. static void
  132. get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
  133. {
  134. char *cur = NULL;
  135. int count = 0;
  136. SMARTLIST_FOREACH(in, char *, cp,
  137. {
  138. if (cur && !strcmp(cp, cur)) {
  139. ++count;
  140. } else {
  141. if (count > min)
  142. smartlist_add(out, cur);
  143. cur = cp;
  144. count = 1;
  145. }
  146. });
  147. if (count > min)
  148. smartlist_add(out, cur);
  149. }
  150. /** Given a sorted list of strings <b>lst</b>, return the member that appears
  151. * most. Break ties in favor of later-occurring members. */
  152. static const char *
  153. get_most_frequent_member(smartlist_t *lst)
  154. {
  155. const char *most_frequent = NULL;
  156. int most_frequent_count = 0;
  157. const char *cur = NULL;
  158. int count = 0;
  159. SMARTLIST_FOREACH(lst, const char *, s,
  160. {
  161. if (cur && !strcmp(s, cur)) {
  162. ++count;
  163. } else {
  164. if (count >= most_frequent_count) {
  165. most_frequent = cur;
  166. most_frequent_count = count;
  167. }
  168. cur = s;
  169. count = 1;
  170. }
  171. });
  172. if (count >= most_frequent_count) {
  173. most_frequent = cur;
  174. most_frequent_count = count;
  175. }
  176. return most_frequent;
  177. }
  178. /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
  179. * that come from the same routerinfo, with the same derived elements.
  180. */
  181. static int
  182. compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
  183. {
  184. int r;
  185. if ((r = memcmp(a->status.identity_digest, b->status.identity_digest,
  186. DIGEST_LEN)))
  187. return r;
  188. if ((r = memcmp(a->status.descriptor_digest, b->status.descriptor_digest,
  189. DIGEST_LEN)))
  190. return r;
  191. if ((r = (b->status.published_on - a->status.published_on)))
  192. return r;
  193. if ((r = strcmp(b->status.nickname, a->status.nickname)))
  194. return r;
  195. if ((r = (((int)b->status.addr) - ((int)a->status.addr))))
  196. return r;
  197. if ((r = (((int)b->status.or_port) - ((int)a->status.or_port))))
  198. return r;
  199. if ((r = (((int)b->status.dir_port) - ((int)a->status.dir_port))))
  200. return r;
  201. return 0;
  202. }
  203. /** Helper for sorting routerlists based on compare_vote_rs. */
  204. static int
  205. _compare_vote_rs(const void **_a, const void **_b)
  206. {
  207. const vote_routerstatus_t *a = *_a, *b = *_b;
  208. return compare_vote_rs(a,b);
  209. }
  210. /** Given a list of vote_routerstatus_t, all for the same router identity,
  211. * return whichever is most frequent, breaking ties in favor of more
  212. * recently published vote_routerstatus_t.
  213. */
  214. static vote_routerstatus_t *
  215. compute_routerstatus_consensus(smartlist_t *votes)
  216. {
  217. vote_routerstatus_t *most = NULL, *cur = NULL;
  218. int most_n = 0, cur_n = 0;
  219. time_t most_published = 0;
  220. smartlist_sort(votes, _compare_vote_rs);
  221. SMARTLIST_FOREACH(votes, vote_routerstatus_t *, rs,
  222. {
  223. if (cur && !compare_vote_rs(cur, rs)) {
  224. ++cur_n;
  225. } else {
  226. if (cur_n > most_n ||
  227. (cur && cur_n == most_n &&
  228. cur->status.published_on > most_published)) {
  229. most = cur;
  230. most_n = cur_n;
  231. most_published = cur->status.published_on;
  232. }
  233. cur_n = 1;
  234. cur = rs;
  235. }
  236. });
  237. if (cur_n > most_n ||
  238. (cur && cur_n == most_n && cur->status.published_on > most_published)) {
  239. most = cur;
  240. most_n = cur_n;
  241. most_published = cur->status.published_on;
  242. }
  243. tor_assert(most);
  244. return most;
  245. }
  246. /** Given a list of strings in <b>lst</b>, set the DIGEST_LEN-byte digest at
  247. * <b>digest_out</b> to the hash of the concatenation of those strings. */
  248. static void
  249. hash_list_members(char *digest_out, smartlist_t *lst)
  250. {
  251. crypto_digest_env_t *d = crypto_new_digest_env();
  252. SMARTLIST_FOREACH(lst, const char *, cp,
  253. crypto_digest_add_bytes(d, cp, strlen(cp)));
  254. crypto_digest_get_digest(d, digest_out, DIGEST_LEN);
  255. crypto_free_digest_env(d);
  256. }
  257. /** Given a list of vote networkstatus_vote_t in <b>votes</b>, our public
  258. * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
  259. * and the number of <b>total_authorities</b> that we believe exist in our
  260. * voting quorum, generate the text of a new v3 consensus vote, and return the
  261. * value in a newly allocated string. */
  262. char *
  263. networkstatus_compute_consensus(smartlist_t *votes,
  264. int total_authorities,
  265. crypto_pk_env_t *identity_key,
  266. crypto_pk_env_t *signing_key)
  267. {
  268. smartlist_t *chunks;
  269. char *result = NULL;
  270. time_t valid_after, fresh_until, valid_until;
  271. int vote_seconds, dist_seconds;
  272. char *client_versions = NULL, *server_versions = NULL;
  273. smartlist_t *flags;
  274. tor_assert(total_authorities >= smartlist_len(votes));
  275. if (!smartlist_len(votes)) {
  276. log_warn(LD_DIR, "Can't compute a consensus from no votes.");
  277. return NULL;
  278. }
  279. /* XXXX020 somebody needs to check vote authority. It could be this
  280. * function, it could be somebody else. */
  281. flags = smartlist_create();
  282. /* Compute medians of time-related things, and figure out how many
  283. * routers we might need to talk about. */
  284. {
  285. smartlist_t *va_times = smartlist_create();
  286. smartlist_t *fu_times = smartlist_create();
  287. smartlist_t *vu_times = smartlist_create();
  288. smartlist_t *votesec_list = smartlist_create();
  289. smartlist_t *distsec_list = smartlist_create();
  290. int n_versioning_clients = 0, n_versioning_servers = 0;
  291. smartlist_t *combined_client_versions = smartlist_create();
  292. smartlist_t *combined_server_versions = smartlist_create();
  293. int j;
  294. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  295. {
  296. tor_assert(v->is_vote);
  297. smartlist_add(va_times, &v->valid_after);
  298. smartlist_add(fu_times, &v->fresh_until);
  299. smartlist_add(vu_times, &v->valid_until);
  300. smartlist_add(votesec_list, &v->vote_seconds);
  301. smartlist_add(distsec_list, &v->dist_seconds);
  302. if (v->client_versions) {
  303. smartlist_t *cv = smartlist_create();
  304. ++n_versioning_clients;
  305. smartlist_split_string(cv, v->client_versions, ",",
  306. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  307. sort_version_list(cv, 1);
  308. smartlist_add_all(combined_client_versions, cv);
  309. smartlist_free(cv); /* elements get freed later. */
  310. }
  311. if (v->server_versions) {
  312. smartlist_t *sv = smartlist_create();
  313. ++n_versioning_servers;
  314. smartlist_split_string(sv, v->server_versions, ",",
  315. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  316. sort_version_list(sv, 1);
  317. smartlist_add_all(combined_server_versions, sv);
  318. smartlist_free(sv); /* elements get freed later. */
  319. }
  320. SMARTLIST_FOREACH(v->known_flags, const char *, cp,
  321. smartlist_add(flags, tor_strdup(cp)));
  322. });
  323. valid_after = median_time(va_times);
  324. fresh_until = median_time(fu_times);
  325. valid_until = median_time(vu_times);
  326. vote_seconds = median_int(votesec_list);
  327. dist_seconds = median_int(distsec_list);
  328. for (j = 0; j < 2; ++j) {
  329. smartlist_t *lst =
  330. j ? combined_server_versions : combined_client_versions;
  331. int min = (j ? n_versioning_servers : n_versioning_clients) / 2;
  332. smartlist_t *good = smartlist_create();
  333. char *res;
  334. sort_version_list(lst, 0);
  335. get_frequent_members(good, lst, min);
  336. res = smartlist_join_strings(good, ",", 0, NULL);
  337. if (j)
  338. server_versions = res;
  339. else
  340. client_versions = res;
  341. SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
  342. smartlist_free(good);
  343. smartlist_free(lst);
  344. }
  345. smartlist_sort_strings(flags);
  346. smartlist_uniq_strings(flags);
  347. smartlist_free(va_times);
  348. smartlist_free(fu_times);
  349. smartlist_free(vu_times);
  350. smartlist_free(votesec_list);
  351. smartlist_free(distsec_list);
  352. }
  353. chunks = smartlist_create();
  354. {
  355. char buf[1024];
  356. char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
  357. vu_buf[ISO_TIME_LEN+1];
  358. char *flaglist;
  359. format_iso_time(va_buf, valid_after);
  360. format_iso_time(fu_buf, fresh_until);
  361. format_iso_time(vu_buf, valid_until);
  362. flaglist = smartlist_join_strings(flags, " ", 0, NULL);
  363. tor_snprintf(buf, sizeof(buf),
  364. "network-status-version 3\n"
  365. "vote-status consensus\n"
  366. "valid-after %s\n"
  367. "fresh-until %s\n"
  368. "valid-until %s\n"
  369. "voting-delay %d %d\n"
  370. "client-versions %s\n"
  371. "server-versions %s\n"
  372. "known-flags %s\n",
  373. va_buf, fu_buf, vu_buf,
  374. vote_seconds, dist_seconds,
  375. client_versions, server_versions, flaglist);
  376. smartlist_add(chunks, tor_strdup(buf));
  377. tor_free(flaglist);
  378. }
  379. /* Sort the votes. */
  380. smartlist_sort(votes, _compare_votes_by_authority_id);
  381. /* Add the authority sections. */
  382. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  383. {
  384. char buf[1024];
  385. struct in_addr in;
  386. char ip[INET_NTOA_BUF_LEN];
  387. char fingerprint[HEX_DIGEST_LEN+1];
  388. char votedigest[HEX_DIGEST_LEN+1];
  389. networkstatus_voter_info_t *voter = get_voter(v);
  390. in.s_addr = htonl(voter->addr);
  391. tor_inet_ntoa(&in, ip, sizeof(ip));
  392. base16_encode(fingerprint, sizeof(fingerprint), voter->identity_digest,
  393. DIGEST_LEN);
  394. base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
  395. DIGEST_LEN);
  396. tor_snprintf(buf, sizeof(buf),
  397. "dir-source %s %s %s %s %d %d\n"
  398. "contact %s\n"
  399. "vote-digest %s\n",
  400. voter->nickname, fingerprint, voter->address, ip,
  401. voter->dir_port,
  402. voter->or_port,
  403. voter->contact,
  404. votedigest);
  405. smartlist_add(chunks, tor_strdup(buf));
  406. });
  407. /* Add the actual router entries. */
  408. {
  409. /* document these XXXX020 */
  410. int *index;
  411. int *size;
  412. int *flag_counts;
  413. int i;
  414. smartlist_t *matching_descs = smartlist_create();
  415. smartlist_t *chosen_flags = smartlist_create();
  416. smartlist_t *versions = smartlist_create();
  417. int *n_voter_flags; /* n_voter_flags[j] is the number of flags that
  418. * votes[j] knows about. */
  419. int *n_flag_voters; /* n_flag_voters[f] is the number of votes that care
  420. * about flags[f]. */
  421. int **flag_map; /* flag_map[j][b] is an index f such that flag_map[f]
  422. * is the same flag as votes[j]->known_flags[b]. */
  423. int *named_flag;
  424. index = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  425. size = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
  426. n_voter_flags = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
  427. n_flag_voters = tor_malloc_zero(sizeof(int) * smartlist_len(flags));
  428. flag_map = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  429. named_flag = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
  430. for (i = 0; i < smartlist_len(votes); ++i)
  431. named_flag[i] = -1;
  432. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
  433. {
  434. flag_map[v_sl_idx] = tor_malloc_zero(
  435. sizeof(int)*smartlist_len(v->known_flags));
  436. SMARTLIST_FOREACH(v->known_flags, const char *, fl,
  437. {
  438. int p = smartlist_string_pos(flags, fl);
  439. tor_assert(p >= 0);
  440. flag_map[v_sl_idx][fl_sl_idx] = p;
  441. ++n_flag_voters[p];
  442. if (!strcmp(fl, "Named"))
  443. named_flag[v_sl_idx] = fl_sl_idx;
  444. });
  445. n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
  446. size[v_sl_idx] = smartlist_len(v->routerstatus_list);
  447. });
  448. /* Now go through all the votes */
  449. flag_counts = tor_malloc(sizeof(int) * smartlist_len(flags));
  450. while (1) {
  451. vote_routerstatus_t *rs;
  452. routerstatus_t rs_out;
  453. const char *lowest_id = NULL;
  454. const char *chosen_version;
  455. const char *chosen_name = NULL;
  456. int naming_conflict = 0;
  457. int n_listing = 0;
  458. int i;
  459. char buf[256];
  460. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
  461. if (index[v_sl_idx] < size[v_sl_idx]) {
  462. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  463. if (!lowest_id ||
  464. memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN) < 0)
  465. lowest_id = rs->status.identity_digest;
  466. }
  467. });
  468. if (!lowest_id) /* we're out of routers. */
  469. break;
  470. memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
  471. smartlist_clear(matching_descs);
  472. smartlist_clear(chosen_flags);
  473. smartlist_clear(versions);
  474. /* Okay, go through all the entries for this digest. */
  475. SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
  476. if (index[v_sl_idx] >= size[v_sl_idx])
  477. continue; /* out of entries. */
  478. rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
  479. if (memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN))
  480. continue; /* doesn't include this router. */
  481. /* At this point, we know that we're looking at a routersatus with
  482. * identity "lowest".
  483. */
  484. ++index[v_sl_idx];
  485. ++n_listing;
  486. smartlist_add(matching_descs, rs);
  487. if (rs->version && rs->version[0])
  488. smartlist_add(versions, rs->version);
  489. /* Tally up all the flags. */
  490. for (i = 0; i < n_voter_flags[v_sl_idx]; ++i) {
  491. if (rs->flags & (U64_LITERAL(1) << i))
  492. ++flag_counts[flag_map[v_sl_idx][i]];
  493. }
  494. if (rs->flags & (U64_LITERAL(1) << named_flag[v_sl_idx])) {
  495. if (chosen_name && strcmp(chosen_name, rs->status.nickname))
  496. naming_conflict = 1; /* XXXX020 warn? */
  497. chosen_name = rs->status.nickname;
  498. }
  499. });
  500. /* We don't include this router at all unless more than half of
  501. * the authorities we believe in list it. */
  502. if (n_listing <= total_authorities/2)
  503. continue;
  504. /* Figure out the most popular opinion of what the most recent
  505. * routerinfo and its contents are. */
  506. rs = compute_routerstatus_consensus(matching_descs);
  507. /* Copy bits of that into rs_out. */
  508. tor_assert(!memcmp(lowest_id, rs->status.identity_digest, DIGEST_LEN));
  509. memcpy(rs_out.identity_digest, lowest_id, DIGEST_LEN);
  510. memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
  511. DIGEST_LEN);
  512. rs_out.addr = rs->status.addr;
  513. rs_out.published_on = rs->status.published_on;
  514. rs_out.dir_port = rs->status.dir_port;
  515. rs_out.or_port = rs->status.or_port;
  516. if (chosen_name && !naming_conflict) {
  517. strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
  518. } else {
  519. strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
  520. }
  521. /* Set the flags. */
  522. smartlist_add(chosen_flags, (char*)"s"); /* for the start of the line. */
  523. SMARTLIST_FOREACH(flags, const char *, fl,
  524. {
  525. if (strcmp(fl, "Named")) {
  526. if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2)
  527. smartlist_add(chosen_flags, (char*)fl);
  528. } else {
  529. if (!naming_conflict && flag_counts[fl_sl_idx])
  530. smartlist_add(chosen_flags, (char*)"Named");
  531. }
  532. });
  533. /* Pick the version. */
  534. if (smartlist_len(versions)) {
  535. sort_version_list(versions, 0);
  536. chosen_version = get_most_frequent_member(versions);
  537. } else {
  538. chosen_version = NULL;
  539. }
  540. /* Okay!! Now we can write the descriptor... */
  541. /* First line goes into "buf". */
  542. routerstatus_format_entry(buf, sizeof(buf), &rs_out, NULL, 1);
  543. smartlist_add(chunks, tor_strdup(buf));
  544. /* Second line is all flags. The "\n" is missing. */
  545. smartlist_add(chunks,
  546. smartlist_join_strings(chosen_flags, " ", 0, NULL));
  547. /* Now the version line. */
  548. if (chosen_version) {
  549. /* XXXX020 fails on very long version string */
  550. tor_snprintf(buf, sizeof(buf), "\nv %s\n", chosen_version);
  551. smartlist_add(chunks, tor_strdup(buf));
  552. } else {
  553. smartlist_add(chunks, tor_strdup("\n"));
  554. }
  555. /* And the loop is over and we move on to the next router */
  556. }
  557. tor_free(index);
  558. tor_free(size);
  559. tor_free(n_voter_flags);
  560. tor_free(n_flag_voters);
  561. for (i = 0; i < smartlist_len(votes); ++i)
  562. tor_free(flag_map[i]);
  563. tor_free(flag_map);
  564. tor_free(flag_counts);
  565. smartlist_free(matching_descs);
  566. smartlist_free(chosen_flags);
  567. smartlist_free(versions);
  568. }
  569. /* Add a signature. */
  570. {
  571. char digest[DIGEST_LEN];
  572. char fingerprint[HEX_DIGEST_LEN+1];
  573. char signing_key_fingerprint[HEX_DIGEST_LEN+1];
  574. char buf[4096];
  575. smartlist_add(chunks, tor_strdup("directory-signature "));
  576. /* Compute the hash of the chunks. */
  577. hash_list_members(digest, chunks);
  578. /* Get the fingerprints */
  579. crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
  580. crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
  581. /* add the junk that will go at the end of the line. */
  582. tor_snprintf(buf, sizeof(buf), "%s %s\n", fingerprint,
  583. signing_key_fingerprint);
  584. /* And the signature. */
  585. /* XXXX020 check return */
  586. router_append_dirobj_signature(buf, sizeof(buf), digest, signing_key);
  587. smartlist_add(chunks, tor_strdup(buf));
  588. }
  589. result = smartlist_join_strings(chunks, "", 0, NULL);
  590. tor_free(client_versions);
  591. tor_free(server_versions);
  592. smartlist_free(flags);
  593. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  594. smartlist_free(chunks);
  595. {
  596. networkstatus_vote_t *c;
  597. if (!(c = networkstatus_parse_vote_from_string(result, 0))) {
  598. log_err(LD_BUG,"Generated a networkstatus consensus we couldn't "
  599. "parse.");
  600. tor_free(result);
  601. return NULL;
  602. }
  603. networkstatus_vote_free(c);
  604. }
  605. return result;
  606. }
  607. /** Check whether the pending_signature on <b>voter</b> is correctly signed by
  608. * the signing key of <b>cert</b>. Return -1 if <b>cert</b> doesn't match the
  609. * signing key; otherwise set the good_signature or bad_signature flag on
  610. * <b>voter</b>, and return 0. */
  611. /* (private; exposed for testing.) */
  612. int
  613. networkstatus_check_voter_signature(networkstatus_vote_t *consensus,
  614. networkstatus_voter_info_t *voter,
  615. authority_cert_t *cert)
  616. {
  617. char d[DIGEST_LEN];
  618. char *signed_digest;
  619. size_t signed_digest_len;
  620. /*XXXX020 check return*/
  621. crypto_pk_get_digest(cert->signing_key, d);
  622. if (memcmp(voter->signing_key_digest, d, DIGEST_LEN)) {
  623. return -1;
  624. }
  625. signed_digest_len = crypto_pk_keysize(cert->signing_key);
  626. signed_digest = tor_malloc(signed_digest_len);
  627. if (crypto_pk_public_checksig(cert->signing_key,
  628. signed_digest,
  629. voter->pending_signature,
  630. voter->pending_signature_len) != DIGEST_LEN ||
  631. memcmp(signed_digest, consensus->networkstatus_digest, DIGEST_LEN)) {
  632. log_warn(LD_DIR, "Got a bad signature."); /*XXXX020 say more*/
  633. voter->bad_signature = 1;
  634. } else {
  635. voter->good_signature = 1;
  636. }
  637. tor_free(voter->pending_signature);
  638. return 0;
  639. }
  640. /** DOCDOC */
  641. int
  642. networkstatus_check_consensus_signature(networkstatus_vote_t *consensus)
  643. {
  644. int n_good = 0;
  645. int n_missing_key = 0;
  646. int n_bad = 0;
  647. int n_unknown = 0;
  648. int n_no_signature = 0;
  649. tor_assert(! consensus->is_vote);
  650. SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, voter,
  651. {
  652. trusted_dir_server_t *ds =
  653. trusteddirserver_get_by_v3_auth_digest(voter->identity_digest);
  654. if (!ds) {
  655. ++n_unknown;
  656. continue;
  657. }
  658. if (voter->pending_signature) {
  659. tor_assert(!voter->good_signature && !voter->bad_signature);
  660. if (!ds->v3_cert ||
  661. networkstatus_check_voter_signature(consensus, voter,
  662. ds->v3_cert) < 0) {
  663. ++n_missing_key;
  664. continue;
  665. }
  666. }
  667. if (voter->good_signature)
  668. ++n_good;
  669. else if (voter->bad_signature)
  670. ++n_bad;
  671. else
  672. ++n_no_signature;
  673. });
  674. /* XXXX020 actually use the result. */
  675. return 0;
  676. }
  677. /** Free storage held in <b>cert</b>. */
  678. void
  679. authority_cert_free(authority_cert_t *cert)
  680. {
  681. if (!cert)
  682. return;
  683. tor_free(cert->cache_info.signed_descriptor_body);
  684. if (cert->signing_key)
  685. crypto_free_pk_env(cert->signing_key);
  686. if (cert->identity_key)
  687. crypto_free_pk_env(cert->identity_key);
  688. tor_free(cert);
  689. }
  690. /** Allocate and return a new authority_cert_t with the same contents as
  691. * <b>cert</b>. */
  692. authority_cert_t *
  693. authority_cert_dup(authority_cert_t *cert)
  694. {
  695. authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
  696. tor_assert(cert);
  697. memcpy(out, cert, sizeof(authority_cert_t));
  698. /* Now copy pointed-to things. */
  699. out->cache_info.signed_descriptor_body =
  700. tor_strndup(cert->cache_info.signed_descriptor_body,
  701. cert->cache_info.signed_descriptor_len);
  702. out->cache_info.saved_location = SAVED_NOWHERE;
  703. out->identity_key = crypto_pk_dup_key(cert->identity_key);
  704. out->signing_key = crypto_pk_dup_key(cert->signing_key);
  705. return out;
  706. }