123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503 |
- const char dirvote_c_id[] =
- "$Id$";
- #define DIRVOTE_PRIVATE
- #include "or.h"
- static int dirvote_add_signatures_to_pending_consensus(
- const char *detached_signatures_body,
- const char **msg_out);
- void
- networkstatus_vote_free(networkstatus_vote_t *ns)
- {
- if (!ns)
- return;
- tor_free(ns->client_versions);
- tor_free(ns->server_versions);
- if (ns->known_flags) {
- SMARTLIST_FOREACH(ns->known_flags, char *, c, tor_free(c));
- smartlist_free(ns->known_flags);
- }
- if (ns->voters) {
- SMARTLIST_FOREACH(ns->voters, networkstatus_voter_info_t *, voter,
- {
- tor_free(voter->nickname);
- tor_free(voter->address);
- tor_free(voter->contact);
- });
- smartlist_free(ns->voters);
- }
- if (ns->cert)
- authority_cert_free(ns->cert);
- if (ns->routerstatus_list) {
- if (ns->is_vote) {
- SMARTLIST_FOREACH(ns->routerstatus_list, vote_routerstatus_t *, rs,
- {
- tor_free(rs->version);
- tor_free(rs);
- });
- } else {
- SMARTLIST_FOREACH(ns->routerstatus_list, routerstatus_t *, rs,
- tor_free(rs));
- }
- smartlist_free(ns->routerstatus_list);
- }
- memset(ns, 11, sizeof(*ns));
- tor_free(ns);
- }
- networkstatus_voter_info_t *
- networkstatus_get_voter_by_id(networkstatus_vote_t *vote,
- const char *identity)
- {
- if (!vote || !vote->voters)
- return NULL;
- SMARTLIST_FOREACH(vote->voters, networkstatus_voter_info_t *, voter,
- if (!memcmp(voter->identity_digest, identity, DIGEST_LEN))
- return voter);
- return NULL;
- }
- static int
- _compare_times(const void **_a, const void **_b)
- {
- const time_t *a = *_a, *b = *_b;
- if (*a<*b)
- return -1;
- else if (*a>*b)
- return 1;
- else
- return 0;
- }
- static int
- _compare_ints(const void **_a, const void **_b)
- {
- const int *a = *_a, *b = *_b;
- if (*a<*b)
- return -1;
- else if (*a>*b)
- return 1;
- else
- return 0;
- }
- static time_t
- median_time(smartlist_t *times)
- {
- int idx;
- tor_assert(smartlist_len(times));
- smartlist_sort(times, _compare_times);
- idx = (smartlist_len(times)-1)/2;
- return *(time_t*)smartlist_get(times, idx);
- }
- static int
- median_int(smartlist_t *ints)
- {
- int idx;
- tor_assert(smartlist_len(ints));
- smartlist_sort(ints, _compare_ints);
- idx = (smartlist_len(ints)-1)/2;
- return *(int*)smartlist_get(ints, idx);
- }
- static networkstatus_voter_info_t *
- get_voter(const networkstatus_vote_t *vote)
- {
- tor_assert(vote);
- tor_assert(vote->is_vote);
- tor_assert(vote->voters);
- tor_assert(smartlist_len(vote->voters) == 1);
- return smartlist_get(vote->voters, 0);
- }
- static int
- _compare_votes_by_authority_id(const void **_a, const void **_b)
- {
- const networkstatus_vote_t *a = *_a, *b = *_b;
- return memcmp(get_voter(a)->identity_digest,
- get_voter(b)->identity_digest, DIGEST_LEN);
- }
- static void
- get_frequent_members(smartlist_t *out, smartlist_t *in, int min)
- {
- char *cur = NULL;
- int count = 0;
- SMARTLIST_FOREACH(in, char *, cp,
- {
- if (cur && !strcmp(cp, cur)) {
- ++count;
- } else {
- if (count > min)
- smartlist_add(out, cur);
- cur = cp;
- count = 1;
- }
- });
- if (count > min)
- smartlist_add(out, cur);
- }
- static const char *
- get_most_frequent_member(smartlist_t *lst)
- {
- const char *most_frequent = NULL;
- int most_frequent_count = 0;
- const char *cur = NULL;
- int count = 0;
- SMARTLIST_FOREACH(lst, const char *, s,
- {
- if (cur && !strcmp(s, cur)) {
- ++count;
- } else {
- if (count >= most_frequent_count) {
- most_frequent = cur;
- most_frequent_count = count;
- }
- cur = s;
- count = 1;
- }
- });
- if (count >= most_frequent_count) {
- most_frequent = cur;
- most_frequent_count = count;
- }
- return most_frequent;
- }
- static int
- compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
- {
- int r;
- if ((r = memcmp(a->status.identity_digest, b->status.identity_digest,
- DIGEST_LEN)))
- return r;
- if ((r = memcmp(a->status.descriptor_digest, b->status.descriptor_digest,
- DIGEST_LEN)))
- return r;
- if ((r = (b->status.published_on - a->status.published_on)))
- return r;
- if ((r = strcmp(b->status.nickname, a->status.nickname)))
- return r;
- if ((r = (((int)b->status.addr) - ((int)a->status.addr))))
- return r;
- if ((r = (((int)b->status.or_port) - ((int)a->status.or_port))))
- return r;
- if ((r = (((int)b->status.dir_port) - ((int)a->status.dir_port))))
- return r;
- return 0;
- }
- static int
- _compare_vote_rs(const void **_a, const void **_b)
- {
- const vote_routerstatus_t *a = *_a, *b = *_b;
- return compare_vote_rs(a,b);
- }
- static vote_routerstatus_t *
- compute_routerstatus_consensus(smartlist_t *votes)
- {
- vote_routerstatus_t *most = NULL, *cur = NULL;
- int most_n = 0, cur_n = 0;
- time_t most_published = 0;
- smartlist_sort(votes, _compare_vote_rs);
- SMARTLIST_FOREACH(votes, vote_routerstatus_t *, rs,
- {
- if (cur && !compare_vote_rs(cur, rs)) {
- ++cur_n;
- } else {
- if (cur_n > most_n ||
- (cur && cur_n == most_n &&
- cur->status.published_on > most_published)) {
- most = cur;
- most_n = cur_n;
- most_published = cur->status.published_on;
- }
- cur_n = 1;
- cur = rs;
- }
- });
- if (cur_n > most_n ||
- (cur && cur_n == most_n && cur->status.published_on > most_published)) {
- most = cur;
- most_n = cur_n;
- most_published = cur->status.published_on;
- }
- tor_assert(most);
- return most;
- }
- static void
- hash_list_members(char *digest_out, smartlist_t *lst)
- {
- crypto_digest_env_t *d = crypto_new_digest_env();
- SMARTLIST_FOREACH(lst, const char *, cp,
- crypto_digest_add_bytes(d, cp, strlen(cp)));
- crypto_digest_get_digest(d, digest_out, DIGEST_LEN);
- crypto_free_digest_env(d);
- }
- char *
- networkstatus_compute_consensus(smartlist_t *votes,
- int total_authorities,
- crypto_pk_env_t *identity_key,
- crypto_pk_env_t *signing_key)
- {
- smartlist_t *chunks;
- char *result = NULL;
- time_t valid_after, fresh_until, valid_until;
- int vote_seconds, dist_seconds;
- char *client_versions = NULL, *server_versions = NULL;
- smartlist_t *flags;
- tor_assert(total_authorities >= smartlist_len(votes));
- if (!smartlist_len(votes)) {
- log_warn(LD_DIR, "Can't compute a consensus from no votes.");
- return NULL;
- }
- flags = smartlist_create();
-
- {
- smartlist_t *va_times = smartlist_create();
- smartlist_t *fu_times = smartlist_create();
- smartlist_t *vu_times = smartlist_create();
- smartlist_t *votesec_list = smartlist_create();
- smartlist_t *distsec_list = smartlist_create();
- int n_versioning_clients = 0, n_versioning_servers = 0;
- smartlist_t *combined_client_versions = smartlist_create();
- smartlist_t *combined_server_versions = smartlist_create();
- int j;
- SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
- {
- tor_assert(v->is_vote);
- smartlist_add(va_times, &v->valid_after);
- smartlist_add(fu_times, &v->fresh_until);
- smartlist_add(vu_times, &v->valid_until);
- smartlist_add(votesec_list, &v->vote_seconds);
- smartlist_add(distsec_list, &v->dist_seconds);
- if (v->client_versions) {
- smartlist_t *cv = smartlist_create();
- ++n_versioning_clients;
- smartlist_split_string(cv, v->client_versions, ",",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- sort_version_list(cv, 1);
- smartlist_add_all(combined_client_versions, cv);
- smartlist_free(cv);
- }
- if (v->server_versions) {
- smartlist_t *sv = smartlist_create();
- ++n_versioning_servers;
- smartlist_split_string(sv, v->server_versions, ",",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- sort_version_list(sv, 1);
- smartlist_add_all(combined_server_versions, sv);
- smartlist_free(sv);
- }
- SMARTLIST_FOREACH(v->known_flags, const char *, cp,
- smartlist_add(flags, tor_strdup(cp)));
- });
- valid_after = median_time(va_times);
- fresh_until = median_time(fu_times);
- valid_until = median_time(vu_times);
- vote_seconds = median_int(votesec_list);
- dist_seconds = median_int(distsec_list);
- for (j = 0; j < 2; ++j) {
- smartlist_t *lst =
- j ? combined_server_versions : combined_client_versions;
- int min = (j ? n_versioning_servers : n_versioning_clients) / 2;
- smartlist_t *good = smartlist_create();
- char *res;
- sort_version_list(lst, 0);
- get_frequent_members(good, lst, min);
- res = smartlist_join_strings(good, ",", 0, NULL);
- if (j)
- server_versions = res;
- else
- client_versions = res;
- SMARTLIST_FOREACH(lst, char *, cp, tor_free(cp));
- smartlist_free(good);
- smartlist_free(lst);
- }
- smartlist_sort_strings(flags);
- smartlist_uniq_strings(flags);
- smartlist_free(va_times);
- smartlist_free(fu_times);
- smartlist_free(vu_times);
- smartlist_free(votesec_list);
- smartlist_free(distsec_list);
- }
- chunks = smartlist_create();
- {
- char buf[1024];
- char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
- vu_buf[ISO_TIME_LEN+1];
- char *flaglist;
- format_iso_time(va_buf, valid_after);
- format_iso_time(fu_buf, fresh_until);
- format_iso_time(vu_buf, valid_until);
- flaglist = smartlist_join_strings(flags, " ", 0, NULL);
- tor_snprintf(buf, sizeof(buf),
- "network-status-version 3\n"
- "vote-status consensus\n"
- "valid-after %s\n"
- "fresh-until %s\n"
- "valid-until %s\n"
- "voting-delay %d %d\n"
- "client-versions %s\n"
- "server-versions %s\n"
- "known-flags %s\n",
- va_buf, fu_buf, vu_buf,
- vote_seconds, dist_seconds,
- client_versions, server_versions, flaglist);
- smartlist_add(chunks, tor_strdup(buf));
- tor_free(flaglist);
- }
-
- smartlist_sort(votes, _compare_votes_by_authority_id);
-
- SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
- {
- char buf[1024];
- struct in_addr in;
- char ip[INET_NTOA_BUF_LEN];
- char fingerprint[HEX_DIGEST_LEN+1];
- char votedigest[HEX_DIGEST_LEN+1];
- networkstatus_voter_info_t *voter = get_voter(v);
- in.s_addr = htonl(voter->addr);
- tor_inet_ntoa(&in, ip, sizeof(ip));
- base16_encode(fingerprint, sizeof(fingerprint), voter->identity_digest,
- DIGEST_LEN);
- base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
- DIGEST_LEN);
- tor_snprintf(buf, sizeof(buf),
- "dir-source %s %s %s %s %d %d\n"
- "contact %s\n"
- "vote-digest %s\n",
- voter->nickname, fingerprint, voter->address, ip,
- voter->dir_port,
- voter->or_port,
- voter->contact,
- votedigest);
- smartlist_add(chunks, tor_strdup(buf));
- });
-
- {
- int *index;
- int *size;
- int *flag_counts;
- int i;
- smartlist_t *matching_descs = smartlist_create();
- smartlist_t *chosen_flags = smartlist_create();
- smartlist_t *versions = smartlist_create();
- int *n_voter_flags;
- int *n_flag_voters;
- int **flag_map;
- int *named_flag;
- index = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
- size = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
- n_voter_flags = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
- n_flag_voters = tor_malloc_zero(sizeof(int) * smartlist_len(flags));
- flag_map = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
- named_flag = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
- for (i = 0; i < smartlist_len(votes); ++i)
- named_flag[i] = -1;
- SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v,
- {
- flag_map[v_sl_idx] = tor_malloc_zero(
- sizeof(int)*smartlist_len(v->known_flags));
- SMARTLIST_FOREACH(v->known_flags, const char *, fl,
- {
- int p = smartlist_string_pos(flags, fl);
- tor_assert(p >= 0);
- flag_map[v_sl_idx][fl_sl_idx] = p;
- ++n_flag_voters[p];
- if (!strcmp(fl, "Named"))
- named_flag[v_sl_idx] = fl_sl_idx;
- });
- n_voter_flags[v_sl_idx] = smartlist_len(v->known_flags);
- size[v_sl_idx] = smartlist_len(v->routerstatus_list);
- });
-
- flag_counts = tor_malloc(sizeof(int) * smartlist_len(flags));
- while (1) {
- vote_routerstatus_t *rs;
- routerstatus_t rs_out;
- const char *lowest_id = NULL;
- const char *chosen_version;
- const char *chosen_name = NULL;
- int naming_conflict = 0;
- int n_listing = 0;
- int i;
- char buf[256];
-
- SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
- if (index[v_sl_idx] < size[v_sl_idx]) {
- rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
- if (!lowest_id ||
- memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN) < 0)
- lowest_id = rs->status.identity_digest;
- }
- });
- if (!lowest_id)
- break;
- memset(flag_counts, 0, sizeof(int)*smartlist_len(flags));
- smartlist_clear(matching_descs);
- smartlist_clear(chosen_flags);
- smartlist_clear(versions);
-
- SMARTLIST_FOREACH(votes, networkstatus_vote_t *, v, {
- if (index[v_sl_idx] >= size[v_sl_idx])
- continue;
- rs = smartlist_get(v->routerstatus_list, index[v_sl_idx]);
- if (memcmp(rs->status.identity_digest, lowest_id, DIGEST_LEN))
- continue;
-
- ++index[v_sl_idx];
- ++n_listing;
- smartlist_add(matching_descs, rs);
- if (rs->version && rs->version[0])
- smartlist_add(versions, rs->version);
-
- for (i = 0; i < n_voter_flags[v_sl_idx]; ++i) {
- if (rs->flags & (U64_LITERAL(1) << i))
- ++flag_counts[flag_map[v_sl_idx][i]];
- }
- if (rs->flags & (U64_LITERAL(1) << named_flag[v_sl_idx])) {
- if (chosen_name && strcmp(chosen_name, rs->status.nickname)) {
- log_notice(LD_DIR, "Conflict on naming for router: %s vs %s",
- chosen_name, rs->status.nickname);
- naming_conflict = 1;
- }
- chosen_name = rs->status.nickname;
- }
- });
-
- if (n_listing <= total_authorities/2)
- continue;
-
- rs = compute_routerstatus_consensus(matching_descs);
-
- tor_assert(!memcmp(lowest_id, rs->status.identity_digest, DIGEST_LEN));
- memcpy(rs_out.identity_digest, lowest_id, DIGEST_LEN);
- memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
- DIGEST_LEN);
- rs_out.addr = rs->status.addr;
- rs_out.published_on = rs->status.published_on;
- rs_out.dir_port = rs->status.dir_port;
- rs_out.or_port = rs->status.or_port;
- if (chosen_name && !naming_conflict) {
- strlcpy(rs_out.nickname, chosen_name, sizeof(rs_out.nickname));
- } else {
- strlcpy(rs_out.nickname, rs->status.nickname, sizeof(rs_out.nickname));
- }
-
- smartlist_add(chosen_flags, (char*)"s");
- SMARTLIST_FOREACH(flags, const char *, fl,
- {
- if (strcmp(fl, "Named")) {
- if (flag_counts[fl_sl_idx] > n_flag_voters[fl_sl_idx]/2)
- smartlist_add(chosen_flags, (char*)fl);
- } else {
- if (!naming_conflict && flag_counts[fl_sl_idx])
- smartlist_add(chosen_flags, (char*)"Named");
- }
- });
-
- if (smartlist_len(versions)) {
- sort_version_list(versions, 0);
- chosen_version = get_most_frequent_member(versions);
- } else {
- chosen_version = NULL;
- }
-
-
- routerstatus_format_entry(buf, sizeof(buf), &rs_out, NULL, 1);
- smartlist_add(chunks, tor_strdup(buf));
-
- smartlist_add(chunks,
- smartlist_join_strings(chosen_flags, " ", 0, NULL));
-
- if (chosen_version) {
- smartlist_add(chunks, tor_strdup("\nv "));
- smartlist_add(chunks, tor_strdup(chosen_version));
- }
- smartlist_add(chunks, tor_strdup("\n"));
-
- }
- tor_free(index);
- tor_free(size);
- tor_free(n_voter_flags);
- tor_free(n_flag_voters);
- for (i = 0; i < smartlist_len(votes); ++i)
- tor_free(flag_map[i]);
- tor_free(flag_map);
- tor_free(flag_counts);
- smartlist_free(matching_descs);
- smartlist_free(chosen_flags);
- smartlist_free(versions);
- }
-
- {
- char digest[DIGEST_LEN];
- char fingerprint[HEX_DIGEST_LEN+1];
- char signing_key_fingerprint[HEX_DIGEST_LEN+1];
- char buf[4096];
- smartlist_add(chunks, tor_strdup("directory-signature "));
-
- hash_list_members(digest, chunks);
-
- crypto_pk_get_fingerprint(identity_key, fingerprint, 0);
- crypto_pk_get_fingerprint(signing_key, signing_key_fingerprint, 0);
-
- tor_snprintf(buf, sizeof(buf), "%s %s\n", fingerprint,
- signing_key_fingerprint);
-
- if (router_append_dirobj_signature(buf, sizeof(buf), digest,
- signing_key)) {
- log_warn(LD_BUG, "Couldn't sign consensus networkstatus.");
- return NULL;
- }
- smartlist_add(chunks, tor_strdup(buf));
- }
- result = smartlist_join_strings(chunks, "", 0, NULL);
- tor_free(client_versions);
- tor_free(server_versions);
- smartlist_free(flags);
- SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
- smartlist_free(chunks);
- {
- networkstatus_vote_t *c;
- if (!(c = networkstatus_parse_vote_from_string(result, 0))) {
- log_err(LD_BUG,"Generated a networkstatus consensus we couldn't "
- "parse.");
- tor_free(result);
- return NULL;
- }
- networkstatus_vote_free(c);
- }
- return result;
- }
- int
- networkstatus_check_voter_signature(networkstatus_vote_t *consensus,
- networkstatus_voter_info_t *voter,
- authority_cert_t *cert)
- {
- char d[DIGEST_LEN];
- char *signed_digest;
- size_t signed_digest_len;
- if (crypto_pk_get_digest(cert->signing_key, d)<0)
- return -1;
- if (memcmp(voter->signing_key_digest, d, DIGEST_LEN))
- return -1;
- signed_digest_len = crypto_pk_keysize(cert->signing_key);
- signed_digest = tor_malloc(signed_digest_len);
- if (crypto_pk_public_checksig(cert->signing_key,
- signed_digest,
- voter->signature,
- voter->signature_len) != DIGEST_LEN ||
- memcmp(signed_digest, consensus->networkstatus_digest, DIGEST_LEN)) {
- log_warn(LD_DIR, "Got a bad signature on a networkstatus vote");
- voter->bad_signature = 1;
- } else {
- voter->good_signature = 1;
- }
- return 0;
- }
- int
- networkstatus_check_consensus_signature(networkstatus_vote_t *consensus)
- {
- int n_good = 0;
- int n_missing_key = 0;
- int n_bad = 0;
- int n_unknown = 0;
- int n_no_signature = 0;
- int n_required = get_n_authorities(V3_AUTHORITY)/2 + 1;
- tor_assert(! consensus->is_vote);
- SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, voter,
- {
- if (!voter->good_signature && !voter->bad_signature && voter->signature) {
-
- authority_cert_t *cert =
- authority_cert_get_by_digests(voter->identity_digest,
- voter->signing_key_digest);
- if (! cert) {
- ++n_unknown;
- continue;
- }
- if (networkstatus_check_voter_signature(consensus, voter, cert) < 0) {
- ++n_missing_key;
- continue;
- }
- }
- if (voter->good_signature)
- ++n_good;
- else if (voter->bad_signature)
- ++n_bad;
- else
- ++n_no_signature;
- });
- log_notice(LD_DIR,
- "%d unknown, %d missing key, %d good, %d bad, %d no signature, "
- "%d required", n_unknown, n_missing_key, n_good, n_bad,
- n_no_signature, n_required);
- if (n_good >= n_required)
- return 0;
- else
- return -1;
- }
- static int
- networkstatus_add_signatures_impl(networkstatus_vote_t *target,
- smartlist_t *src_voter_list,
- char **new_signatures_out)
- {
- smartlist_t *added_signatures, *sigs;
- int r;
- tor_assert(target);
- tor_assert(!target->is_vote);
- tor_assert(new_signatures_out);
- added_signatures = smartlist_create();
-
- SMARTLIST_FOREACH(src_voter_list, networkstatus_voter_info_t *, src_voter,
- {
- networkstatus_voter_info_t *target_voter =
- networkstatus_get_voter_by_id(target, src_voter->identity_digest);
- authority_cert_t *cert;
-
- if (!target_voter)
- continue;
-
- if (target_voter->good_signature)
- continue;
-
- if (!src_voter->good_signature && !src_voter->bad_signature) {
- cert = authority_cert_get_by_digests(src_voter->identity_digest,
- src_voter->signing_key_digest);
- if (cert) {
- networkstatus_check_voter_signature(target, src_voter, cert);
- }
- }
-
-
- if (src_voter->good_signature) {
- tor_free(target_voter->signature);
- target_voter->signature =
- tor_memdup(src_voter->signature, src_voter->signature_len);
- memcpy(target_voter->signing_key_digest, src_voter->signing_key_digest,
- DIGEST_LEN);
- target_voter->signature_len = src_voter->signature_len;
- target_voter->good_signature = 1;
- target_voter->bad_signature = 0;
- smartlist_add(added_signatures, target_voter);
- }
- });
- sigs = smartlist_create();
- SMARTLIST_FOREACH(added_signatures, networkstatus_voter_info_t *, v,
- {
- char buf[4096];
- char sk[HEX_DIGEST_LEN+1];
- char ik[HEX_DIGEST_LEN+1];
- tor_assert(v->signature);
- base16_encode(sk, sizeof(sk), v->signing_key_digest, DIGEST_LEN);
- base16_encode(ik, sizeof(ik), v->identity_digest, DIGEST_LEN);
- tor_snprintf(buf, sizeof(buf), "directory-signature %s %s\n"
- "-----BEGIN SIGNATURE-----\n", ik, sk);
- smartlist_add(sigs, tor_strdup(buf));
- base64_encode(buf, sizeof(buf), v->signature, v->signature_len);
- strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
- smartlist_add(sigs, tor_strdup(buf));
- });
- *new_signatures_out = smartlist_join_strings(sigs, "", 0, NULL);
- SMARTLIST_FOREACH(sigs, char *, cp, tor_free(cp));
- smartlist_free(sigs);
- r = smartlist_len(added_signatures);
- smartlist_free(added_signatures);
- return r;
- }
- int
- networkstatus_add_consensus_signatures(networkstatus_vote_t *target,
- networkstatus_vote_t *src,
- char **new_signatures_out)
- {
- tor_assert(src);
- tor_assert(! src->is_vote);
- *new_signatures_out = NULL;
-
- if (memcmp(target->networkstatus_digest, src->networkstatus_digest,
- DIGEST_LEN))
- return -1;
- if (target == src)
- return 0;
- return networkstatus_add_signatures_impl(target, src->voters,
- new_signatures_out);
- }
- int
- networkstatus_add_detached_signatures(networkstatus_vote_t *target,
- ns_detached_signatures_t *sigs,
- char **new_signatures_out)
- {
- tor_assert(sigs);
- *new_signatures_out = NULL;
-
- if (memcmp(target->networkstatus_digest, sigs->networkstatus_digest,
- DIGEST_LEN))
- return -1;
- return networkstatus_add_signatures_impl(target, sigs->signatures,
- new_signatures_out);
- }
- char *
- networkstatus_get_detached_signatures(networkstatus_vote_t *consensus)
- {
- smartlist_t *elements;
- char buf[4096];
- char *result = NULL;
- int n_sigs = 0;
- tor_assert(consensus);
- tor_assert(! consensus->is_vote);
- elements = smartlist_create();
- {
- char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
- vu_buf[ISO_TIME_LEN+1];
- char d[HEX_DIGEST_LEN+1];
- base16_encode(d, sizeof(d), consensus->networkstatus_digest, DIGEST_LEN);
- format_iso_time(va_buf, consensus->valid_after);
- format_iso_time(fu_buf, consensus->fresh_until);
- format_iso_time(vu_buf, consensus->valid_until);
- tor_snprintf(buf, sizeof(buf),
- "consensus-digest %s\n"
- "valid-after %s\n"
- "fresh-until %s\n"
- "valid-until %s\n", d, va_buf, fu_buf, vu_buf);
- smartlist_add(elements, tor_strdup(buf));
- }
- SMARTLIST_FOREACH(consensus->voters, networkstatus_voter_info_t *, v,
- {
- char sk[HEX_DIGEST_LEN+1];
- char id[HEX_DIGEST_LEN+1];
- if (!v->signature)
- continue;
- ++n_sigs;
- base16_encode(sk, sizeof(sk), v->signing_key_digest, DIGEST_LEN);
- base16_encode(id, sizeof(id), v->identity_digest, DIGEST_LEN);
- tor_snprintf(buf, sizeof(buf),
- "directory-signature %s %s\n-----BEGIN SIGNATURE-----\n",
- id, sk);
- smartlist_add(elements, tor_strdup(buf));
- base64_encode(buf, sizeof(buf), v->signature, v->signature_len);
- strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
- smartlist_add(elements, tor_strdup(buf));
- });
- result = smartlist_join_strings(elements, "", 0, NULL);
- SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
- smartlist_free(elements);
- if (!n_sigs)
- tor_free(result);
- return result;
- }
- void
- ns_detached_signatures_free(ns_detached_signatures_t *s)
- {
- if (s->signatures) {
- SMARTLIST_FOREACH(s->signatures, networkstatus_voter_info_t *, v,
- {
- tor_free(v->signature);
- tor_free(v);
- });
- smartlist_free(s->signatures);
- }
- tor_free(s);
- }
- void
- authority_cert_free(authority_cert_t *cert)
- {
- if (!cert)
- return;
- tor_free(cert->cache_info.signed_descriptor_body);
- if (cert->signing_key)
- crypto_free_pk_env(cert->signing_key);
- if (cert->identity_key)
- crypto_free_pk_env(cert->identity_key);
- tor_free(cert);
- }
- authority_cert_t *
- authority_cert_dup(authority_cert_t *cert)
- {
- authority_cert_t *out = tor_malloc(sizeof(authority_cert_t));
- tor_assert(cert);
- memcpy(out, cert, sizeof(authority_cert_t));
-
- out->cache_info.signed_descriptor_body =
- tor_strndup(cert->cache_info.signed_descriptor_body,
- cert->cache_info.signed_descriptor_len);
- out->cache_info.saved_location = SAVED_NOWHERE;
- out->identity_key = crypto_pk_dup_key(cert->identity_key);
- out->signing_key = crypto_pk_dup_key(cert->signing_key);
- return out;
- }
- void
- dirvote_get_preferred_voting_intervals(vote_timing_t *timing_out)
- {
- or_options_t *options = get_options();
- tor_assert(timing_out);
- timing_out->vote_interval = options->V3AuthVotingInterval;
- timing_out->n_intervals_valid = options->V3AuthNIntervalsValid;
- timing_out->vote_delay = options->V3AuthVoteDelay;
- timing_out->dist_delay = options->V3AuthDistDelay;
- }
- time_t
- dirvote_get_start_of_next_interval(time_t now, int interval)
- {
- struct tm tm;
- time_t midnight_today;
- time_t midnight_tomorrow;
- time_t next;
- tor_gmtime_r(&now, &tm);
- tm.tm_hour = 0;
- tm.tm_min = 0;
- tm.tm_sec = 0;
- midnight_today = tor_timegm(&tm);
- midnight_tomorrow = midnight_today + (24*60*60);
- next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
- if (next > midnight_tomorrow)
- next = midnight_tomorrow;
- return next;
- }
- static struct {
- time_t voting_starts;
- time_t voting_ends;
- time_t interval_starts;
- time_t discard_old_votes;
- int have_voted;
- int have_built_consensus;
- int have_published_consensus;
- } voting_schedule = {0,0,0,0,0,0,0};
- void
- dirvote_recalculate_timing(time_t now)
- {
-
- int interval, vote_delay, dist_delay;
- time_t start;
- networkstatus_vote_t *consensus = networkstatus_get_latest_consensus();
- memset(&voting_schedule, 0, sizeof(voting_schedule));
- if (consensus) {
-
- interval = consensus->fresh_until - consensus->valid_after;
- vote_delay = consensus->vote_seconds;
- dist_delay = consensus->dist_seconds;
- } else {
-
-
- interval = 1200;
- vote_delay = dist_delay = 300;
- }
- start = voting_schedule.interval_starts =
- dirvote_get_start_of_next_interval(now,interval);
- voting_schedule.voting_ends = start - vote_delay;
- voting_schedule.voting_starts = start - vote_delay - dist_delay;
- voting_schedule.discard_old_votes = start + 300;
- }
- void
- dirvote_act(time_t now)
- {
- if (!voting_schedule.voting_starts)
- dirvote_recalculate_timing(now);
- if (voting_schedule.voting_starts < now && !voting_schedule.have_voted) {
- log_notice(LD_DIR, "Time to vote.");
- dirvote_perform_vote();
- voting_schedule.have_voted = 1;
- }
-
- if (voting_schedule.voting_ends < now &&
- !voting_schedule.have_built_consensus) {
- log_notice(LD_DIR, "Time to compute a consensus.");
- dirvote_compute_consensus();
-
- voting_schedule.have_built_consensus = 1;
- }
- if (voting_schedule.interval_starts < now &&
- !voting_schedule.have_published_consensus) {
- log_notice(LD_DIR, "Time to publish the consensus.");
- dirvote_publish_consensus();
-
- voting_schedule.have_published_consensus = 1;
- }
- if (voting_schedule.discard_old_votes < now) {
- log_notice(LD_DIR, "Time to discard old votes consensus.");
- dirvote_clear_pending_votes();
- dirvote_recalculate_timing(now);
- }
- }
- typedef struct pending_vote_t {
- cached_dir_t *vote_body;
- networkstatus_vote_t *vote;
- } pending_vote_t;
- static smartlist_t *pending_vote_list = NULL;
- static char *pending_consensus_body = NULL;
- static char *pending_consensus_signatures = NULL;
- static networkstatus_vote_t *pending_consensus = NULL;
- static smartlist_t *pending_consensus_signature_list = NULL;
- void
- dirvote_perform_vote(void)
- {
- cached_dir_t *new_vote = generate_v3_networkstatus();
- pending_vote_t *pending_vote;
- int status;
- const char *msg = "";
- if (!new_vote)
- return;
- if (!(pending_vote = dirvote_add_vote(new_vote->dir, &msg, &status))) {
- log_warn(LD_DIR, "Couldn't store my own vote! (I told myself, '%s'.)",
- msg);
- return;
- }
- directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE,
- ROUTER_PURPOSE_GENERAL,
- V3_AUTHORITY,
- pending_vote->vote_body->dir,
- pending_vote->vote_body->dir_len, 0);
- log_notice(LD_DIR, "Vote posted.");
- }
- void
- dirvote_clear_pending_votes(void)
- {
- if (pending_vote_list) {
- SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
- cached_dir_decref(v->vote_body);
- v->vote_body = NULL;
- networkstatus_vote_free(v->vote);
- tor_free(v);
- });
- smartlist_clear(pending_vote_list);
- }
- if (pending_consensus_signature_list) {
- SMARTLIST_FOREACH(pending_consensus_signature_list, char *, cp,
- tor_free(cp));
- smartlist_clear(pending_consensus_signature_list);
- }
- tor_free(pending_consensus_body);
- tor_free(pending_consensus_signatures);
- if (pending_consensus) {
- networkstatus_vote_free(pending_consensus);
- pending_consensus = NULL;
- }
- log_notice(LD_DIR, "Pending votes cleared.");
- }
- pending_vote_t *
- dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
- {
- networkstatus_vote_t *vote;
- networkstatus_voter_info_t *vi;
- trusted_dir_server_t *ds;
- pending_vote_t *pending_vote = NULL;
- tor_assert(vote_body);
- tor_assert(msg_out);
- tor_assert(status_out);
- *status_out = 0;
- if (!pending_vote_list)
- pending_vote_list = smartlist_create();
- *msg_out = NULL;
- vote = networkstatus_parse_vote_from_string(vote_body, 1);
- if (!vote) {
- *msg_out = "Unable to parse vote";
- goto err;
- }
- tor_assert(smartlist_len(vote->voters) == 1);
- vi = smartlist_get(vote->voters, 0);
- tor_assert(vi->good_signature == 1);
- ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
- if (!ds || !(ds->type & V3_AUTHORITY)) {
- *msg_out = "Vote not from a recognized v3 authority";
- goto err;
- }
- tor_assert(vote->cert);
- if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
- vote->cert->signing_key_digest)) {
-
- trusted_dirs_load_certs_from_string(
- vote->cert->cache_info.signed_descriptor_body,
- 0 );
- if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
- vote->cert->signing_key_digest)) {
- log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
- }
- }
-
- SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v, {
- if (! memcmp(v->vote->cert->cache_info.identity_digest,
- vote->cert->cache_info.identity_digest,
- DIGEST_LEN)) {
- networkstatus_voter_info_t *vi_old = smartlist_get(v->vote->voters, 0);
- if (!memcmp(vi_old->vote_digest, vi->vote_digest, DIGEST_LEN)) {
-
- log_info(LD_DIR, "Discarding a vote we already have.");
- *status_out = 200;
- *msg_out = "ok";
- goto err;
- } else if (v->vote->published < vote->published) {
- log_notice(LD_DIR, "Replacing an older pending vote from this "
- "directory.");
- cached_dir_decref(v->vote_body);
- networkstatus_vote_free(v->vote);
- v->vote_body = new_cached_dir(tor_strdup(vote_body),
- vote->published);
- v->vote = vote;
- *msg_out = "ok";
- return v;
- } else {
- *msg_out = "Already have a newer pending vote";
- goto err;
- }
- }
- });
- pending_vote = tor_malloc_zero(sizeof(pending_vote_t));
- pending_vote->vote_body = new_cached_dir(tor_strdup(vote_body),
- vote->published);
- pending_vote->vote = vote;
- smartlist_add(pending_vote_list, pending_vote);
- if (!*status_out)
- *status_out = 200;
- *msg_out = "ok";
- return pending_vote;
- err:
- if (vote)
- networkstatus_vote_free(vote);
- if (!*msg_out)
- *msg_out = "Error adding vote";
- if (!*status_out)
- *status_out = 400;
- return NULL;
- }
- int
- dirvote_compute_consensus(void)
- {
-
- int n_votes, n_voters;
- smartlist_t *votes = NULL;
- char *consensus_body = NULL, *signatures = NULL;
- networkstatus_vote_t *consensus = NULL;
- authority_cert_t *my_cert;
- if (!pending_vote_list)
- pending_vote_list = smartlist_create();
- n_voters = get_n_authorities(V3_AUTHORITY);
- n_votes = smartlist_len(pending_vote_list);
- if (n_votes <= n_voters/2) {
- log_warn(LD_DIR, "We don't have enough votes to generate a consensus.");
- goto err;
- }
- if (!(my_cert = get_my_v3_authority_cert())) {
- log_warn(LD_DIR, "Can't generate consensus without a certificate.");
- goto err;
- }
- votes = smartlist_create();
- SMARTLIST_FOREACH(pending_vote_list, pending_vote_t *, v,
- smartlist_add(votes, v->vote));
- consensus_body = networkstatus_compute_consensus(
- votes, n_voters,
- my_cert->identity_key,
- get_my_v3_authority_signing_key());
- if (!consensus_body) {
- log_warn(LD_DIR, "Couldn't generate a consensus at all!");
- goto err;
- }
- consensus = networkstatus_parse_vote_from_string(consensus_body, 0);
- if (!consensus) {
- log_warn(LD_DIR, "Couldn't parse consensus we generated!");
- goto err;
- }
-
- networkstatus_check_consensus_signature(consensus);
- signatures = networkstatus_get_detached_signatures(consensus);
- if (!signatures) {
- log_warn(LD_DIR, "Couldn't extract signatures.");
- goto err;
- }
- tor_free(pending_consensus_body);
- pending_consensus_body = consensus_body;
- tor_free(pending_consensus_signatures);
- pending_consensus_signatures = signatures;
- if (pending_consensus)
- networkstatus_vote_free(pending_consensus);
- pending_consensus = consensus;
- if (pending_consensus_signature_list) {
- int n_sigs = 0;
-
- SMARTLIST_FOREACH(pending_consensus_signature_list, char *, sig,
- {
- const char *msg = NULL;
- n_sigs += dirvote_add_signatures_to_pending_consensus(sig, &msg);
- tor_free(sig);
- });
- if (n_sigs)
- log_notice(LD_DIR, "Added %d pending signatures while building "
- "consensus.", n_sigs);
- smartlist_clear(pending_consensus_signature_list);
- }
- log_notice(LD_DIR, "Consensus computed; uploading signature(s)");
- directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES,
- ROUTER_PURPOSE_GENERAL,
- V3_AUTHORITY,
- pending_consensus_signatures,
- strlen(pending_consensus_signatures), 0);
- log_notice(LD_DIR, "Signature(s) posted.");
- return 0;
- err:
- if (votes)
- smartlist_free(votes);
- tor_free(consensus_body);
- tor_free(signatures);
- networkstatus_vote_free(consensus);
- return -1;
- }
- static int
- dirvote_add_signatures_to_pending_consensus(
- const char *detached_signatures_body,
- const char **msg_out)
- {
- ns_detached_signatures_t *sigs = NULL;
- int r = -1;
- char *new_signatures = NULL;
- size_t siglen;
- tor_assert(detached_signatures_body);
- tor_assert(msg_out);
-
- tor_assert(pending_consensus);
- tor_assert(pending_consensus_body);
- tor_assert(pending_consensus_signatures);
- *msg_out = NULL;
- if (!(sigs = networkstatus_parse_detached_signatures(
- detached_signatures_body, NULL))) {
- *msg_out = "Couldn't parse detached signatures.";
- goto err;
- }
- r = networkstatus_add_detached_signatures(pending_consensus,
- sigs,
- &new_signatures);
- if (new_signatures && (siglen = strlen(new_signatures)) && r >= 0) {
- size_t siglen = strlen(new_signatures);
- size_t len = strlen(pending_consensus_body);
- pending_consensus_body = tor_realloc(pending_consensus_body,
- len+siglen+1);
- memcpy(pending_consensus_body+len, new_signatures, siglen+1);
- len = strlen(pending_consensus_signatures);
- pending_consensus_signatures = tor_realloc(pending_consensus_signatures,
- len+siglen+1);
- memcpy(pending_consensus_signatures+len, new_signatures, siglen+1);
- log_info(LD_DIR, "Added %d new signatures to the pending consensus.", r);
- }
- *msg_out = "ok";
- goto done;
- err:
- if (!msg_out)
- *msg_out = "Unrecognized error while adding detached signatures.";
- done:
- tor_free(new_signatures);
- if (sigs)
- ns_detached_signatures_free(sigs);
- return r;
- }
- int
- dirvote_add_signatures(const char *detached_signatures_body)
- {
- if (pending_consensus) {
- const char *msg=NULL;
- log_notice(LD_DIR, "Got a signature. Adding it to the pending consensus.");
- return dirvote_add_signatures_to_pending_consensus(
- detached_signatures_body, &msg);
- } else {
- log_notice(LD_DIR, "Got a signature. Queueing it for the next consensus.");
- if (!pending_consensus_signature_list)
- pending_consensus_signature_list = smartlist_create();
- smartlist_add(pending_consensus_signature_list,
- tor_strdup(detached_signatures_body));
- return 0;
- }
- }
- int
- dirvote_publish_consensus(void)
- {
-
- if (!pending_consensus ||
- networkstatus_check_consensus_signature(pending_consensus)<0) {
- log_warn(LD_DIR, "Not enough info to publish pending consensus");
- return -1;
- }
- if (networkstatus_set_current_consensus(pending_consensus_body, 0))
- log_warn(LD_DIR, "Error publishing consensus");
- else
- log_warn(LD_DIR, "Consensus published.");
- return 0;
- }
- void
- dirvote_free_all(void)
- {
- dirvote_clear_pending_votes();
- if (pending_vote_list) {
-
- smartlist_free(pending_vote_list);
- pending_vote_list = NULL;
- }
- tor_free(pending_consensus_body);
- tor_free(pending_consensus_signatures);
- if (pending_consensus) {
- networkstatus_vote_free(pending_consensus);
- pending_consensus = NULL;
- }
- if (pending_consensus_signature_list) {
-
- smartlist_free(pending_consensus_signature_list);
- pending_consensus_signature_list = NULL;
- }
- }
|