1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2008, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 const char dirvote_c_id
[] =
9 #define DIRVOTE_PRIVATE
14 * \brief Functions to compute directory consensus, and schedule voting.
17 static int dirvote_add_signatures_to_pending_consensus(
18 const char *detached_signatures_body
,
19 const char **msg_out
);
20 static char *list_v3_auth_ids(void);
21 static void dirvote_fetch_missing_votes(void);
22 static void dirvote_fetch_missing_signatures(void);
23 static int dirvote_perform_vote(void);
24 static void dirvote_clear_votes(int all_votes
);
25 static int dirvote_compute_consensus(void);
26 static int dirvote_publish_consensus(void);
32 /** Return a new string containing the string representation of the vote in
33 * <b>v3_ns</b>, signed with our v3 signing key <b>private_signing_key</b>.
34 * For v3 authorities. */
36 format_networkstatus_vote(crypto_pk_env_t
*private_signing_key
,
37 networkstatus_t
*v3_ns
)
41 const char *client_versions
= NULL
, *server_versions
= NULL
;
43 char fingerprint
[FINGERPRINT_LEN
+1];
44 char ipaddr
[INET_NTOA_BUF_LEN
];
45 char digest
[DIGEST_LEN
];
48 routerlist_t
*rl
= router_get_routerlist();
49 char *version_lines
= NULL
;
50 networkstatus_voter_info_t
*voter
;
52 tor_assert(private_signing_key
);
54 voter
= smartlist_get(v3_ns
->voters
, 0);
57 in
.s_addr
= htonl(addr
);
58 tor_inet_ntoa(&in
, ipaddr
, sizeof(ipaddr
));
60 base16_encode(fingerprint
, sizeof(fingerprint
),
61 v3_ns
->cert
->cache_info
.identity_digest
, DIGEST_LEN
);
62 client_versions
= v3_ns
->client_versions
;
63 server_versions
= v3_ns
->server_versions
;
65 if (client_versions
|| server_versions
) {
69 v_len
+= strlen(client_versions
);
71 v_len
+= strlen(server_versions
);
72 version_lines
= tor_malloc(v_len
);
74 if (client_versions
) {
75 tor_snprintf(cp
, v_len
-(cp
-version_lines
),
76 "client-versions %s\n", client_versions
);
80 tor_snprintf(cp
, v_len
-(cp
-version_lines
),
81 "server-versions %s\n", server_versions
);
83 version_lines
= tor_strdup("");
87 len
+= strlen(version_lines
);
88 len
+= (RS_ENTRY_LEN
)*smartlist_len(rl
->routers
);
89 len
+= v3_ns
->cert
->cache_info
.signed_descriptor_len
;
91 status
= tor_malloc(len
);
93 char published
[ISO_TIME_LEN
+1];
94 char va
[ISO_TIME_LEN
+1];
95 char fu
[ISO_TIME_LEN
+1];
96 char vu
[ISO_TIME_LEN
+1];
97 char *flags
= smartlist_join_strings(v3_ns
->known_flags
, " ", 0, NULL
);
98 authority_cert_t
*cert
= v3_ns
->cert
;
99 format_iso_time(published
, v3_ns
->published
);
100 format_iso_time(va
, v3_ns
->valid_after
);
101 format_iso_time(fu
, v3_ns
->fresh_until
);
102 format_iso_time(vu
, v3_ns
->valid_until
);
105 tor_snprintf(status
, len
,
106 "network-status-version 3\n"
108 "consensus-methods 1 2\n"
113 "voting-delay %d %d\n"
116 "dir-source %s %s %s %s %d %d\n"
118 published
, va
, fu
, vu
,
119 v3_ns
->vote_seconds
, v3_ns
->dist_seconds
,
122 voter
->nickname
, fingerprint
, voter
->address
,
123 ipaddr
, voter
->dir_port
, voter
->or_port
, voter
->contact
);
126 outp
= status
+ strlen(status
);
128 tor_assert(outp
+ cert
->cache_info
.signed_descriptor_len
< endp
);
129 memcpy(outp
, cert
->cache_info
.signed_descriptor_body
,
130 cert
->cache_info
.signed_descriptor_len
);
132 outp
+= cert
->cache_info
.signed_descriptor_len
;
135 SMARTLIST_FOREACH(v3_ns
->routerstatus_list
, vote_routerstatus_t
*, vrs
,
137 if (routerstatus_format_entry(outp
, endp
-outp
, &vrs
->status
,
138 vrs
->version
, 0) < 0) {
139 log_warn(LD_BUG
, "Unable to print router status.");
142 outp
+= strlen(outp
);
146 char signing_key_fingerprint
[FINGERPRINT_LEN
+1];
147 if (tor_snprintf(outp
, endp
-outp
, "directory-signature ")<0) {
148 log_warn(LD_BUG
, "Unable to start signature line.");
151 outp
+= strlen(outp
);
153 if (crypto_pk_get_fingerprint(private_signing_key
,
154 signing_key_fingerprint
, 0)<0) {
155 log_warn(LD_BUG
, "Unable to get fingerprint for signing key");
158 if (tor_snprintf(outp
, endp
-outp
, "%s %s\n", fingerprint
,
159 signing_key_fingerprint
)<0) {
160 log_warn(LD_BUG
, "Unable to end signature line.");
163 outp
+= strlen(outp
);
166 if (router_get_networkstatus_v3_hash(status
, digest
)<0)
168 note_crypto_pk_op(SIGN_DIR
);
169 if (router_append_dirobj_signature(outp
,endp
-outp
,digest
,
170 private_signing_key
)<0) {
171 log_warn(LD_BUG
, "Unable to sign networkstatus vote.");
177 if (!(v
= networkstatus_parse_vote_from_string(status
, NULL
, 1))) {
178 log_err(LD_BUG
,"Generated a networkstatus vote we couldn't parse: "
182 networkstatus_vote_free(v
);
190 tor_free(version_lines
);
195 * Consensus generation
198 /** Given a vote <b>vote</b> (not a consensus!), return its associated
199 * networkstatus_voter_info_t. */
200 static networkstatus_voter_info_t
*
201 get_voter(const networkstatus_t
*vote
)
204 tor_assert(vote
->is_vote
);
205 tor_assert(vote
->voters
);
206 tor_assert(smartlist_len(vote
->voters
) == 1);
207 return smartlist_get(vote
->voters
, 0);
210 /** Helper for sorting networkstatus_t votes (not consensuses) by the
211 * hash of their voters' identity digests. */
213 _compare_votes_by_authority_id(const void **_a
, const void **_b
)
215 const networkstatus_t
*a
= *_a
, *b
= *_b
;
216 return memcmp(get_voter(a
)->identity_digest
,
217 get_voter(b
)->identity_digest
, DIGEST_LEN
);
220 /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
221 * that occurs more than <b>min</b> times. */
223 get_frequent_members(smartlist_t
*out
, smartlist_t
*in
, int min
)
227 SMARTLIST_FOREACH(in
, char *, cp
,
229 if (cur
&& !strcmp(cp
, cur
)) {
233 smartlist_add(out
, cur
);
239 smartlist_add(out
, cur
);
242 /** Given a sorted list of strings <b>lst</b>, return the member that appears
243 * most. Break ties in favor of later-occurring members. */
245 get_most_frequent_member(smartlist_t
*lst
)
247 const char *most_frequent
= NULL
;
248 int most_frequent_count
= 0;
250 const char *cur
= NULL
;
253 SMARTLIST_FOREACH(lst
, const char *, s
,
255 if (cur
&& !strcmp(s
, cur
)) {
258 if (count
>= most_frequent_count
) {
260 most_frequent_count
= count
;
266 if (count
>= most_frequent_count
) {
268 most_frequent_count
= count
;
270 return most_frequent
;
273 /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
274 * that come from the same routerinfo, with the same derived elements.
277 compare_vote_rs(const vote_routerstatus_t
*a
, const vote_routerstatus_t
*b
)
280 if ((r
= memcmp(a
->status
.identity_digest
, b
->status
.identity_digest
,
283 if ((r
= memcmp(a
->status
.descriptor_digest
, b
->status
.descriptor_digest
,
286 if ((r
= (int)(b
->status
.published_on
- a
->status
.published_on
)))
288 if ((r
= strcmp(b
->status
.nickname
, a
->status
.nickname
)))
290 if ((r
= (((int)b
->status
.addr
) - ((int)a
->status
.addr
))))
292 if ((r
= (((int)b
->status
.or_port
) - ((int)a
->status
.or_port
))))
294 if ((r
= (((int)b
->status
.dir_port
) - ((int)a
->status
.dir_port
))))
299 /** Helper for sorting routerlists based on compare_vote_rs. */
301 _compare_vote_rs(const void **_a
, const void **_b
)
303 const vote_routerstatus_t
*a
= *_a
, *b
= *_b
;
304 return compare_vote_rs(a
,b
);
307 /** Given a list of vote_routerstatus_t, all for the same router identity,
308 * return whichever is most frequent, breaking ties in favor of more
309 * recently published vote_routerstatus_t.
311 static vote_routerstatus_t
*
312 compute_routerstatus_consensus(smartlist_t
*votes
)
314 vote_routerstatus_t
*most
= NULL
, *cur
= NULL
;
315 int most_n
= 0, cur_n
= 0;
316 time_t most_published
= 0;
318 smartlist_sort(votes
, _compare_vote_rs
);
319 SMARTLIST_FOREACH(votes
, vote_routerstatus_t
*, rs
,
321 if (cur
&& !compare_vote_rs(cur
, rs
)) {
324 if (cur_n
> most_n
||
325 (cur
&& cur_n
== most_n
&&
326 cur
->status
.published_on
> most_published
)) {
329 most_published
= cur
->status
.published_on
;
336 if (cur_n
> most_n
||
337 (cur
&& cur_n
== most_n
&& cur
->status
.published_on
> most_published
)) {
340 most_published
= cur
->status
.published_on
;
347 /** Given a list of strings in <b>lst</b>, set the DIGEST_LEN-byte digest at
348 * <b>digest_out</b> to the hash of the concatenation of those strings. */
350 hash_list_members(char *digest_out
, smartlist_t
*lst
)
352 crypto_digest_env_t
*d
= crypto_new_digest_env();
353 SMARTLIST_FOREACH(lst
, const char *, cp
,
354 crypto_digest_add_bytes(d
, cp
, strlen(cp
)));
355 crypto_digest_get_digest(d
, digest_out
, DIGEST_LEN
);
356 crypto_free_digest_env(d
);
359 /** Sorting helper: compare two strings based on their values as base-ten
360 * positive integers. (Non-integers are treated as prior to all integers, and
361 * compared lexically.) */
363 _cmp_int_strings(const void **_a
, const void **_b
)
365 const char *a
= *_a
, *b
= *_b
;
366 int ai
= (int)tor_parse_long(a
, 10, 1, INT_MAX
, NULL
, NULL
);
367 int bi
= (int)tor_parse_long(b
, 10, 1, INT_MAX
, NULL
, NULL
);
371 if (ai
== 0) /* Parsing failed. */
379 /** Given a list of networkstatus_t votes, determine and return the number of
380 * the highest consensus method that is supported by 2/3 of the voters. */
382 compute_consensus_method(smartlist_t
*votes
)
384 smartlist_t
*all_methods
= smartlist_create();
385 smartlist_t
*acceptable_methods
= smartlist_create();
386 smartlist_t
*tmp
= smartlist_create();
387 int min
= (smartlist_len(votes
) * 2) / 3;
390 SMARTLIST_FOREACH(votes
, networkstatus_t
*, vote
,
392 tor_assert(vote
->supported_methods
);
393 smartlist_add_all(tmp
, vote
->supported_methods
);
394 smartlist_sort(tmp
, _cmp_int_strings
);
395 smartlist_uniq(tmp
, _cmp_int_strings
, NULL
);
396 smartlist_add_all(all_methods
, tmp
);
397 smartlist_clear(tmp
);
400 smartlist_sort(all_methods
, _cmp_int_strings
);
401 get_frequent_members(acceptable_methods
, all_methods
, min
);
402 n_ok
= smartlist_len(acceptable_methods
);
404 const char *best
= smartlist_get(acceptable_methods
, n_ok
-1);
405 result
= (int)tor_parse_long(best
, 10, 1, INT_MAX
, NULL
, NULL
);
410 smartlist_free(all_methods
);
411 smartlist_free(acceptable_methods
);
415 /** Return true iff <b>method</b> is a consensus method that we support. */
417 consensus_method_is_supported(int method
)
419 return (method
>= 1) && (method
<= 2);
422 /** Given a list of vote networkstatus_t in <b>votes</b>, our public
423 * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
424 * and the number of <b>total_authorities</b> that we believe exist in our
425 * voting quorum, generate the text of a new v3 consensus vote, and return the
426 * value in a newly allocated string.
428 * Note: this function DOES NOT check whether the votes are from
429 * recognized authorities. (dirvote_add_vote does that.) */
431 networkstatus_compute_consensus(smartlist_t
*votes
,
432 int total_authorities
,
433 crypto_pk_env_t
*identity_key
,
434 crypto_pk_env_t
*signing_key
)
438 int consensus_method
;
440 time_t valid_after
, fresh_until
, valid_until
;
441 int vote_seconds
, dist_seconds
;
442 char *client_versions
= NULL
, *server_versions
= NULL
;
444 tor_assert(total_authorities
>= smartlist_len(votes
));
446 if (!smartlist_len(votes
)) {
447 log_warn(LD_DIR
, "Can't compute a consensus from no votes.");
450 flags
= smartlist_create();
452 consensus_method
= compute_consensus_method(votes
);
453 if (consensus_method_is_supported(consensus_method
)) {
454 log_info(LD_DIR
, "Generating consensus using method %d.",
457 log_warn(LD_DIR
, "The other authorities will use consensus method %d, "
458 "which I don't support. Maybe I should upgrade!",
460 consensus_method
= 1;
463 /* Compute medians of time-related things, and figure out how many
464 * routers we might need to talk about. */
466 int n_votes
= smartlist_len(votes
);
467 time_t *va_times
= tor_malloc(n_votes
* sizeof(time_t));
468 time_t *fu_times
= tor_malloc(n_votes
* sizeof(time_t));
469 time_t *vu_times
= tor_malloc(n_votes
* sizeof(time_t));
470 int *votesec_list
= tor_malloc(n_votes
* sizeof(int));
471 int *distsec_list
= tor_malloc(n_votes
* sizeof(int));
472 int n_versioning_clients
= 0, n_versioning_servers
= 0;
473 smartlist_t
*combined_client_versions
= smartlist_create();
474 smartlist_t
*combined_server_versions
= smartlist_create();
476 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
,
478 tor_assert(v
->is_vote
);
479 va_times
[v_sl_idx
] = v
->valid_after
;
480 fu_times
[v_sl_idx
] = v
->fresh_until
;
481 vu_times
[v_sl_idx
] = v
->valid_until
;
482 votesec_list
[v_sl_idx
] = v
->vote_seconds
;
483 distsec_list
[v_sl_idx
] = v
->dist_seconds
;
484 if (v
->client_versions
) {
485 smartlist_t
*cv
= smartlist_create();
486 ++n_versioning_clients
;
487 smartlist_split_string(cv
, v
->client_versions
, ",",
488 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
489 sort_version_list(cv
, 1);
490 smartlist_add_all(combined_client_versions
, cv
);
491 smartlist_free(cv
); /* elements get freed later. */
493 if (v
->server_versions
) {
494 smartlist_t
*sv
= smartlist_create();
495 ++n_versioning_servers
;
496 smartlist_split_string(sv
, v
->server_versions
, ",",
497 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
498 sort_version_list(sv
, 1);
499 smartlist_add_all(combined_server_versions
, sv
);
500 smartlist_free(sv
); /* elements get freed later. */
502 SMARTLIST_FOREACH(v
->known_flags
, const char *, cp
,
503 smartlist_add(flags
, tor_strdup(cp
)));
505 valid_after
= median_time(va_times
, n_votes
);
506 fresh_until
= median_time(fu_times
, n_votes
);
507 valid_until
= median_time(vu_times
, n_votes
);
508 vote_seconds
= median_int(votesec_list
, n_votes
);
509 dist_seconds
= median_int(distsec_list
, n_votes
);
511 tor_assert(valid_after
+MIN_VOTE_INTERVAL
<= fresh_until
);
512 tor_assert(fresh_until
+MIN_VOTE_INTERVAL
<= valid_until
);
513 tor_assert(vote_seconds
>= MIN_VOTE_SECONDS
);
514 tor_assert(dist_seconds
>= MIN_DIST_SECONDS
);
516 for (j
= 0; j
< 2; ++j
) {
518 j
? combined_server_versions
: combined_client_versions
;
519 int min
= (j
? n_versioning_servers
: n_versioning_clients
) / 2;
520 smartlist_t
*good
= smartlist_create();
522 sort_version_list(lst
, 0);
523 get_frequent_members(good
, lst
, min
);
524 res
= smartlist_join_strings(good
, ",", 0, NULL
);
526 server_versions
= res
;
528 client_versions
= res
;
529 SMARTLIST_FOREACH(lst
, char *, cp
, tor_free(cp
));
530 smartlist_free(good
);
534 smartlist_sort_strings(flags
);
535 smartlist_uniq_strings(flags
);
540 tor_free(votesec_list
);
541 tor_free(distsec_list
);
544 chunks
= smartlist_create();
548 char va_buf
[ISO_TIME_LEN
+1], fu_buf
[ISO_TIME_LEN
+1],
549 vu_buf
[ISO_TIME_LEN
+1];
551 format_iso_time(va_buf
, valid_after
);
552 format_iso_time(fu_buf
, fresh_until
);
553 format_iso_time(vu_buf
, valid_until
);
554 flaglist
= smartlist_join_strings(flags
, " ", 0, NULL
);
556 smartlist_add(chunks
, tor_strdup("network-status-version 3\n"
557 "vote-status consensus\n"));
559 if (consensus_method
>= 2) {
560 tor_snprintf(buf
, sizeof(buf
), "consensus-method %d\n",
562 smartlist_add(chunks
, tor_strdup(buf
));
565 tor_snprintf(buf
, sizeof(buf
),
569 "voting-delay %d %d\n"
570 "client-versions %s\n"
571 "server-versions %s\n"
573 va_buf
, fu_buf
, vu_buf
,
574 vote_seconds
, dist_seconds
,
575 client_versions
, server_versions
, flaglist
);
576 smartlist_add(chunks
, tor_strdup(buf
));
581 /* Sort the votes. */
582 smartlist_sort(votes
, _compare_votes_by_authority_id
);
583 /* Add the authority sections. */
584 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
,
588 char ip
[INET_NTOA_BUF_LEN
];
589 char fingerprint
[HEX_DIGEST_LEN
+1];
590 char votedigest
[HEX_DIGEST_LEN
+1];
591 networkstatus_voter_info_t
*voter
= get_voter(v
);
593 in
.s_addr
= htonl(voter
->addr
);
594 tor_inet_ntoa(&in
, ip
, sizeof(ip
));
595 base16_encode(fingerprint
, sizeof(fingerprint
), voter
->identity_digest
,
597 base16_encode(votedigest
, sizeof(votedigest
), voter
->vote_digest
,
600 tor_snprintf(buf
, sizeof(buf
),
601 "dir-source %s %s %s %s %d %d\n"
604 voter
->nickname
, fingerprint
, voter
->address
, ip
,
609 smartlist_add(chunks
, tor_strdup(buf
));
612 /* Add the actual router entries. */
614 int *index
; /* index[j] is the current index into votes[j]. */
615 int *size
; /* size[j] is the number of routerstatuses in votes[j]. */
616 int *flag_counts
; /* The number of voters that list flag[j] for the
617 * currently considered router. */
619 smartlist_t
*matching_descs
= smartlist_create();
620 smartlist_t
*chosen_flags
= smartlist_create();
621 smartlist_t
*versions
= smartlist_create();
623 int *n_voter_flags
; /* n_voter_flags[j] is the number of flags that
624 * votes[j] knows about. */
625 int *n_flag_voters
; /* n_flag_voters[f] is the number of votes that care
627 int **flag_map
; /* flag_map[j][b] is an index f such that flag_map[f]
628 * is the same flag as votes[j]->known_flags[b]. */
629 int *named_flag
; /* Index of the flag "Named" for votes[j] */
630 int *unnamed_flag
; /* Index of the flag "Unnamed" for votes[j] */
631 int chosen_named_idx
, chosen_unnamed_idx
;
633 strmap_t
*name_to_id_map
= strmap_new();
634 char conflict
[DIGEST_LEN
];
635 char unknown
[DIGEST_LEN
];
636 memset(conflict
, 0, sizeof(conflict
));
637 memset(unknown
, 0xff, sizeof(conflict
));
639 index
= tor_malloc_zero(sizeof(int)*smartlist_len(votes
));
640 size
= tor_malloc_zero(sizeof(int)*smartlist_len(votes
));
641 n_voter_flags
= tor_malloc_zero(sizeof(int) * smartlist_len(votes
));
642 n_flag_voters
= tor_malloc_zero(sizeof(int) * smartlist_len(flags
));
643 flag_map
= tor_malloc_zero(sizeof(int*) * smartlist_len(votes
));
644 named_flag
= tor_malloc_zero(sizeof(int*) * smartlist_len(votes
));
645 unnamed_flag
= tor_malloc_zero(sizeof(int*) * smartlist_len(votes
));
646 for (i
= 0; i
< smartlist_len(votes
); ++i
)
647 unnamed_flag
[i
] = named_flag
[i
] = -1;
648 chosen_named_idx
= smartlist_string_pos(flags
, "Named");
649 chosen_unnamed_idx
= smartlist_string_pos(flags
, "Unnamed");
651 /* Build the flag index. */
652 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
,
654 flag_map
[v_sl_idx
] = tor_malloc_zero(
655 sizeof(int)*smartlist_len(v
->known_flags
));
656 SMARTLIST_FOREACH(v
->known_flags
, const char *, fl
,
658 int p
= smartlist_string_pos(flags
, fl
);
660 flag_map
[v_sl_idx
][fl_sl_idx
] = p
;
662 if (!strcmp(fl
, "Named"))
663 named_flag
[v_sl_idx
] = fl_sl_idx
;
664 if (!strcmp(fl
, "Unnamed"))
665 unnamed_flag
[v_sl_idx
] = fl_sl_idx
;
667 n_voter_flags
[v_sl_idx
] = smartlist_len(v
->known_flags
);
668 size
[v_sl_idx
] = smartlist_len(v
->routerstatus_list
);
671 /* Named and Unnamed get treated specially */
672 if (consensus_method
>= 2) {
673 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
,
676 if (named_flag
[v_sl_idx
]<0)
678 nf
= U64_LITERAL(1) << named_flag
[v_sl_idx
];
679 SMARTLIST_FOREACH(v
->routerstatus_list
, vote_routerstatus_t
*, rs
,
681 if ((rs
->flags
& nf
) != 0) {
682 const char *d
= strmap_get_lc(name_to_id_map
, rs
->status
.nickname
);
684 /* We have no name officially mapped to this digest. */
685 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
,
686 rs
->status
.identity_digest
);
687 } else if (d
!= conflict
&&
688 memcmp(d
, rs
->status
.identity_digest
, DIGEST_LEN
)) {
689 /* Authorities disagree about this nickname. */
690 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, conflict
);
692 /* It's already a conflict, or it's already this ID. */
697 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
,
700 if (unnamed_flag
[v_sl_idx
]<0)
702 uf
= U64_LITERAL(1) << unnamed_flag
[v_sl_idx
];
703 SMARTLIST_FOREACH(v
->routerstatus_list
, vote_routerstatus_t
*, rs
,
705 if ((rs
->flags
& uf
) != 0) {
706 const char *d
= strmap_get_lc(name_to_id_map
, rs
->status
.nickname
);
707 if (d
== conflict
|| d
== unknown
) {
708 /* Leave it alone; we know what it is. */
710 /* We have no name officially mapped to this digest. */
711 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, unknown
);
712 } else if (!memcmp(d
, rs
->status
.identity_digest
, DIGEST_LEN
)) {
713 /* Authorities disagree about this nickname. */
714 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, conflict
);
716 /* It's mapped to a different name. */
723 /* Now go through all the votes */
724 flag_counts
= tor_malloc(sizeof(int) * smartlist_len(flags
));
726 vote_routerstatus_t
*rs
;
727 routerstatus_t rs_out
;
728 const char *lowest_id
= NULL
;
729 const char *chosen_version
;
730 const char *chosen_name
= NULL
;
731 int is_named
= 0, is_unnamed
= 0;
732 int naming_conflict
= 0;
737 /* Of the next-to-be-considered digest in each voter, which is first? */
738 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
, {
739 if (index
[v_sl_idx
] < size
[v_sl_idx
]) {
740 rs
= smartlist_get(v
->routerstatus_list
, index
[v_sl_idx
]);
742 memcmp(rs
->status
.identity_digest
, lowest_id
, DIGEST_LEN
) < 0)
743 lowest_id
= rs
->status
.identity_digest
;
746 if (!lowest_id
) /* we're out of routers. */
749 memset(flag_counts
, 0, sizeof(int)*smartlist_len(flags
));
750 smartlist_clear(matching_descs
);
751 smartlist_clear(chosen_flags
);
752 smartlist_clear(versions
);
754 /* Okay, go through all the entries for this digest. */
755 SMARTLIST_FOREACH(votes
, networkstatus_t
*, v
, {
756 if (index
[v_sl_idx
] >= size
[v_sl_idx
])
757 continue; /* out of entries. */
758 rs
= smartlist_get(v
->routerstatus_list
, index
[v_sl_idx
]);
759 if (memcmp(rs
->status
.identity_digest
, lowest_id
, DIGEST_LEN
))
760 continue; /* doesn't include this router. */
761 /* At this point, we know that we're looking at a routersatus with
767 smartlist_add(matching_descs
, rs
);
768 if (rs
->version
&& rs
->version
[0])
769 smartlist_add(versions
, rs
->version
);
771 /* Tally up all the flags. */
772 for (i
= 0; i
< n_voter_flags
[v_sl_idx
]; ++i
) {
773 if (rs
->flags
& (U64_LITERAL(1) << i
))
774 ++flag_counts
[flag_map
[v_sl_idx
][i
]];
776 if (rs
->flags
& (U64_LITERAL(1) << named_flag
[v_sl_idx
])) {
777 if (chosen_name
&& strcmp(chosen_name
, rs
->status
.nickname
)) {
778 log_notice(LD_DIR
, "Conflict on naming for router: %s vs %s",
779 chosen_name
, rs
->status
.nickname
);
782 chosen_name
= rs
->status
.nickname
;
786 /* We don't include this router at all unless more than half of
787 * the authorities we believe in list it. */
788 if (n_listing
<= total_authorities
/2)
791 /* Figure out the most popular opinion of what the most recent
792 * routerinfo and its contents are. */
793 rs
= compute_routerstatus_consensus(matching_descs
);
794 /* Copy bits of that into rs_out. */
795 tor_assert(!memcmp(lowest_id
, rs
->status
.identity_digest
, DIGEST_LEN
));
796 memcpy(rs_out
.identity_digest
, lowest_id
, DIGEST_LEN
);
797 memcpy(rs_out
.descriptor_digest
, rs
->status
.descriptor_digest
,
799 rs_out
.addr
= rs
->status
.addr
;
800 rs_out
.published_on
= rs
->status
.published_on
;
801 rs_out
.dir_port
= rs
->status
.dir_port
;
802 rs_out
.or_port
= rs
->status
.or_port
;
804 if (chosen_name
&& !naming_conflict
) {
805 strlcpy(rs_out
.nickname
, chosen_name
, sizeof(rs_out
.nickname
));
807 strlcpy(rs_out
.nickname
, rs
->status
.nickname
, sizeof(rs_out
.nickname
));
810 if (consensus_method
== 1) {
811 is_named
= chosen_named_idx
>= 0 &&
812 (!naming_conflict
&& flag_counts
[chosen_named_idx
]);
814 const char *d
= strmap_get_lc(name_to_id_map
, rs_out
.nickname
);
816 is_named
= is_unnamed
= 0;
817 } else if (!memcmp(d
, lowest_id
, DIGEST_LEN
)) {
818 is_named
= 1; is_unnamed
= 0;
820 is_named
= 0; is_unnamed
= 1;
825 smartlist_add(chosen_flags
, (char*)"s"); /* for the start of the line. */
826 SMARTLIST_FOREACH(flags
, const char *, fl
,
828 if (!strcmp(fl
, "Named")) {
830 smartlist_add(chosen_flags
, (char*)fl
);
831 } else if (!strcmp(fl
, "Unnamed") && consensus_method
>= 2) {
833 smartlist_add(chosen_flags
, (char*)fl
);
835 if (flag_counts
[fl_sl_idx
] > n_flag_voters
[fl_sl_idx
]/2)
836 smartlist_add(chosen_flags
, (char*)fl
);
840 /* Pick the version. */
841 if (smartlist_len(versions
)) {
842 sort_version_list(versions
, 0);
843 chosen_version
= get_most_frequent_member(versions
);
845 chosen_version
= NULL
;
848 /* Okay!! Now we can write the descriptor... */
849 /* First line goes into "buf". */
850 routerstatus_format_entry(buf
, sizeof(buf
), &rs_out
, NULL
, 1);
851 smartlist_add(chunks
, tor_strdup(buf
));
852 /* Second line is all flags. The "\n" is missing. */
853 smartlist_add(chunks
,
854 smartlist_join_strings(chosen_flags
, " ", 0, NULL
));
855 /* Now the version line. */
856 if (chosen_version
) {
857 smartlist_add(chunks
, tor_strdup("\nv "));
858 smartlist_add(chunks
, tor_strdup(chosen_version
));
860 smartlist_add(chunks
, tor_strdup("\n"));
862 /* And the loop is over and we move on to the next router */
867 tor_free(n_voter_flags
);
868 tor_free(n_flag_voters
);
869 for (i
= 0; i
< smartlist_len(votes
); ++i
)
870 tor_free(flag_map
[i
]);
872 tor_free(flag_counts
);
873 tor_free(named_flag
);
874 tor_free(unnamed_flag
);
875 strmap_free(name_to_id_map
, NULL
);
876 smartlist_free(matching_descs
);
877 smartlist_free(chosen_flags
);
878 smartlist_free(versions
);
881 /* Add a signature. */
883 char digest
[DIGEST_LEN
];
884 char fingerprint
[HEX_DIGEST_LEN
+1];
885 char signing_key_fingerprint
[HEX_DIGEST_LEN
+1];
888 smartlist_add(chunks
, tor_strdup("directory-signature "));
890 /* Compute the hash of the chunks. */
891 hash_list_members(digest
, chunks
);
893 /* Get the fingerprints */
894 crypto_pk_get_fingerprint(identity_key
, fingerprint
, 0);
895 crypto_pk_get_fingerprint(signing_key
, signing_key_fingerprint
, 0);
897 /* add the junk that will go at the end of the line. */
898 tor_snprintf(buf
, sizeof(buf
), "%s %s\n", fingerprint
,
899 signing_key_fingerprint
);
900 /* And the signature. */
901 if (router_append_dirobj_signature(buf
, sizeof(buf
), digest
,
903 log_warn(LD_BUG
, "Couldn't sign consensus networkstatus.");
904 return NULL
; /* This leaks, but it should never happen. */
906 smartlist_add(chunks
, tor_strdup(buf
));
909 result
= smartlist_join_strings(chunks
, "", 0, NULL
);
911 tor_free(client_versions
);
912 tor_free(server_versions
);
913 smartlist_free(flags
);
914 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
915 smartlist_free(chunks
);
919 if (!(c
= networkstatus_parse_vote_from_string(result
, NULL
, 0))) {
920 log_err(LD_BUG
,"Generated a networkstatus consensus we couldn't "
925 networkstatus_vote_free(c
);
931 /** Given a consensus vote <b>target</b> and a set of detached signatures in
932 * <b>sigs</b> that correspond to the same consensus, check whether there are
933 * any new signatures in <b>src_voter_list</b> that should be added to
934 * <b>target. (A signature should be added if we have no signature for that
935 * voter in <b>target</b> yet, or if we have no verifiable signature and the
936 * new signature is verifiable.) Return the number of signatures added or
937 * changed, or -1 if the document signed by <b>sigs</b> isn't the same
938 * document as <b>target</b>. */
940 networkstatus_add_detached_signatures(networkstatus_t
*target
,
941 ns_detached_signatures_t
*sigs
,
942 const char **msg_out
)
947 tor_assert(!target
->is_vote
);
949 /* Do the times seem right? */
950 if (target
->valid_after
!= sigs
->valid_after
) {
951 *msg_out
= "Valid-After times do not match "
952 "when adding detached signatures to consensus";
955 if (target
->fresh_until
!= sigs
->fresh_until
) {
956 *msg_out
= "Fresh-until times do not match "
957 "when adding detached signatures to consensus";
960 if (target
->valid_until
!= sigs
->valid_until
) {
961 *msg_out
= "Valid-until times do not match "
962 "when adding detached signatures to consensus";
965 /* Are they the same consensus? */
966 if (memcmp(target
->networkstatus_digest
, sigs
->networkstatus_digest
,
968 *msg_out
= "Digest mismatch when adding detached signatures to consensus";
972 /* For each voter in src... */
973 SMARTLIST_FOREACH(sigs
->signatures
, networkstatus_voter_info_t
*, src_voter
,
975 char voter_identity
[HEX_DIGEST_LEN
+1];
976 networkstatus_voter_info_t
*target_voter
=
977 networkstatus_get_voter_by_id(target
, src_voter
->identity_digest
);
978 authority_cert_t
*cert
;
980 base16_encode(voter_identity
, sizeof(voter_identity
),
981 src_voter
->identity_digest
, DIGEST_LEN
);
982 log_info(LD_DIR
, "Looking at signature from %s", voter_identity
);
983 /* If the target doesn't know about this voter, then forget it. */
985 log_info(LD_DIR
, "We do not know about %s", voter_identity
);
989 /* If the target already has a good signature from this voter, then skip
991 if (target_voter
->good_signature
) {
992 log_info(LD_DIR
, "We already have a good signature from %s",
997 /* Try checking the signature if we haven't already. */
998 if (!src_voter
->good_signature
&& !src_voter
->bad_signature
) {
999 cert
= authority_cert_get_by_digests(src_voter
->identity_digest
,
1000 src_voter
->signing_key_digest
);
1002 networkstatus_check_voter_signature(target
, src_voter
, cert
);
1005 /* If this signature is good, or we don't have any signature yet,
1007 if (src_voter
->good_signature
|| !target_voter
->signature
) {
1008 log_info(LD_DIR
, "Adding signature from %s", voter_identity
);
1010 tor_free(target_voter
->signature
);
1011 target_voter
->signature
=
1012 tor_memdup(src_voter
->signature
, src_voter
->signature_len
);
1013 memcpy(target_voter
->signing_key_digest
, src_voter
->signing_key_digest
,
1015 target_voter
->signature_len
= src_voter
->signature_len
;
1016 target_voter
->good_signature
= 1;
1017 target_voter
->bad_signature
= 0;
1019 log_info(LD_DIR
, "Not adding signature from %s", voter_identity
);
1026 /** Return a newly allocated string holding the detached-signatures document
1027 * corresponding to the signatures on <b>consensus</b>. */
1029 networkstatus_get_detached_signatures(networkstatus_t
*consensus
)
1031 smartlist_t
*elements
;
1033 char *result
= NULL
;
1035 tor_assert(consensus
);
1036 tor_assert(! consensus
->is_vote
);
1038 elements
= smartlist_create();
1041 char va_buf
[ISO_TIME_LEN
+1], fu_buf
[ISO_TIME_LEN
+1],
1042 vu_buf
[ISO_TIME_LEN
+1];
1043 char d
[HEX_DIGEST_LEN
+1];
1045 base16_encode(d
, sizeof(d
), consensus
->networkstatus_digest
, DIGEST_LEN
);
1046 format_iso_time(va_buf
, consensus
->valid_after
);
1047 format_iso_time(fu_buf
, consensus
->fresh_until
);
1048 format_iso_time(vu_buf
, consensus
->valid_until
);
1050 tor_snprintf(buf
, sizeof(buf
),
1051 "consensus-digest %s\n"
1054 "valid-until %s\n", d
, va_buf
, fu_buf
, vu_buf
);
1055 smartlist_add(elements
, tor_strdup(buf
));
1058 SMARTLIST_FOREACH(consensus
->voters
, networkstatus_voter_info_t
*, v
,
1060 char sk
[HEX_DIGEST_LEN
+1];
1061 char id
[HEX_DIGEST_LEN
+1];
1062 if (!v
->signature
|| v
->bad_signature
)
1065 base16_encode(sk
, sizeof(sk
), v
->signing_key_digest
, DIGEST_LEN
);
1066 base16_encode(id
, sizeof(id
), v
->identity_digest
, DIGEST_LEN
);
1067 tor_snprintf(buf
, sizeof(buf
),
1068 "directory-signature %s %s\n-----BEGIN SIGNATURE-----\n",
1070 smartlist_add(elements
, tor_strdup(buf
));
1071 base64_encode(buf
, sizeof(buf
), v
->signature
, v
->signature_len
);
1072 strlcat(buf
, "-----END SIGNATURE-----\n", sizeof(buf
));
1073 smartlist_add(elements
, tor_strdup(buf
));
1076 result
= smartlist_join_strings(elements
, "", 0, NULL
);
1078 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
1079 smartlist_free(elements
);
1085 /** Release all storage held in <b>s</b>. */
1087 ns_detached_signatures_free(ns_detached_signatures_t
*s
)
1089 if (s
->signatures
) {
1090 SMARTLIST_FOREACH(s
->signatures
, networkstatus_voter_info_t
*, v
,
1092 tor_free(v
->signature
);
1095 smartlist_free(s
->signatures
);
1101 * Certificate functions
1104 /** Allocate and return a new authority_cert_t with the same contents as
1107 authority_cert_dup(authority_cert_t
*cert
)
1109 authority_cert_t
*out
= tor_malloc(sizeof(authority_cert_t
));
1112 memcpy(out
, cert
, sizeof(authority_cert_t
));
1113 /* Now copy pointed-to things. */
1114 out
->cache_info
.signed_descriptor_body
=
1115 tor_strndup(cert
->cache_info
.signed_descriptor_body
,
1116 cert
->cache_info
.signed_descriptor_len
);
1117 out
->cache_info
.saved_location
= SAVED_NOWHERE
;
1118 out
->identity_key
= crypto_pk_dup_key(cert
->identity_key
);
1119 out
->signing_key
= crypto_pk_dup_key(cert
->signing_key
);
1128 /** Set *<b>timing_out</b> to the intervals at which we would like to vote.
1129 * Note that these aren't the intervals we'll use to vote; they're the ones
1130 * that we'll vote to use. */
1132 dirvote_get_preferred_voting_intervals(vote_timing_t
*timing_out
)
1134 or_options_t
*options
= get_options();
1136 tor_assert(timing_out
);
1138 timing_out
->vote_interval
= options
->V3AuthVotingInterval
;
1139 timing_out
->n_intervals_valid
= options
->V3AuthNIntervalsValid
;
1140 timing_out
->vote_delay
= options
->V3AuthVoteDelay
;
1141 timing_out
->dist_delay
= options
->V3AuthDistDelay
;
1144 /** Return the start of the next interval of size <b>interval</b> (in seconds)
1145 * after <b>now</b>. Midnight always starts a fresh interval, and if the last
1146 * interval of a day would be truncated to less than half its size, it is
1147 * rolled into the previous interval. */
1149 dirvote_get_start_of_next_interval(time_t now
, int interval
)
1152 time_t midnight_today
;
1153 time_t midnight_tomorrow
;
1156 tor_gmtime_r(&now
, &tm
);
1161 midnight_today
= tor_timegm(&tm
);
1162 midnight_tomorrow
= midnight_today
+ (24*60*60);
1164 next
= midnight_today
+ ((now
-midnight_today
)/interval
+ 1)*interval
;
1166 /* Intervals never cross midnight. */
1167 if (next
> midnight_tomorrow
)
1168 next
= midnight_tomorrow
;
1170 /* If the interval would only last half as long as it's supposed to, then
1171 * skip over to the next day. */
1172 if (next
+ interval
/2 > midnight_tomorrow
)
1173 next
= midnight_tomorrow
;
1178 /** Scheduling information for a voting interval. */
1180 /** When do we generate and distribute our vote for this interval? */
1181 time_t voting_starts
;
1182 /** When do we send an HTTP request for any votes that we haven't
1183 * been posted yet?*/
1184 time_t fetch_missing_votes
;
1185 /** When do we give up on getting more votes and generate a consensus? */
1187 /** When do we send an HTTP request for any signatures we're expecting to
1188 * see on the consensus? */
1189 time_t fetch_missing_signatures
;
1190 /** When do we publish the consensus? */
1191 time_t interval_starts
;
1193 /* True iff we have generated and distributed our vote. */
1195 /* True iff we've requested missing votes. */
1196 int have_fetched_missing_votes
;
1197 /* True iff we have built a consensus and sent the signatures around. */
1198 int have_built_consensus
;
1199 /* True iff we've fetched missing signatures. */
1200 int have_fetched_missing_signatures
;
1201 /* True iff we have published our consensus. */
1202 int have_published_consensus
;
1203 } voting_schedule
= {0,0,0,0,0,0,0,0,0,0};
1205 /** Set voting_schedule to hold the timing for the next vote we should be
1208 dirvote_recalculate_timing(or_options_t
*options
, time_t now
)
1210 int interval
, vote_delay
, dist_delay
;
1213 networkstatus_t
*consensus
;
1215 if (!authdir_mode_v3(options
))
1218 consensus
= networkstatus_get_live_consensus(now
);
1220 memset(&voting_schedule
, 0, sizeof(voting_schedule
));
1223 interval
= (int)( consensus
->fresh_until
- consensus
->valid_after
);
1224 vote_delay
= consensus
->vote_seconds
;
1225 dist_delay
= consensus
->dist_seconds
;
1227 interval
= DEFAULT_VOTING_INTERVAL_WHEN_NO_CONSENSUS
;
1228 vote_delay
= dist_delay
= 300;
1231 tor_assert(interval
> 0);
1233 if (vote_delay
+ dist_delay
> interval
/2)
1234 vote_delay
= dist_delay
= interval
/ 4;
1236 start
= voting_schedule
.interval_starts
=
1237 dirvote_get_start_of_next_interval(now
,interval
);
1238 end
= dirvote_get_start_of_next_interval(start
+1, interval
);
1240 tor_assert(end
> start
);
1242 voting_schedule
.fetch_missing_signatures
= start
- (dist_delay
/2);
1243 voting_schedule
.voting_ends
= start
- dist_delay
;
1244 voting_schedule
.fetch_missing_votes
= start
- dist_delay
- (vote_delay
/2);
1245 voting_schedule
.voting_starts
= start
- dist_delay
- vote_delay
;
1248 char tbuf
[ISO_TIME_LEN
+1];
1249 format_iso_time(tbuf
, voting_schedule
.interval_starts
);
1250 log_notice(LD_DIR
,"Choosing expected valid-after time as %s: "
1251 "consensus_set=%d, interval=%d",
1252 tbuf
, consensus
?1:0, interval
);
1256 /** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
1258 dirvote_act(or_options_t
*options
, time_t now
)
1260 if (!authdir_mode_v3(options
))
1262 if (!voting_schedule
.voting_starts
) {
1263 char *keys
= list_v3_auth_ids();
1264 authority_cert_t
*c
= get_my_v3_authority_cert();
1265 log_notice(LD_DIR
, "Scheduling voting. Known authority IDs are %s. "
1267 keys
, hex_str(c
->cache_info
.identity_digest
, DIGEST_LEN
));
1269 dirvote_recalculate_timing(options
, now
);
1271 if (voting_schedule
.voting_starts
< now
&& !voting_schedule
.have_voted
) {
1272 log_notice(LD_DIR
, "Time to vote.");
1273 dirvote_perform_vote();
1274 voting_schedule
.have_voted
= 1;
1276 if (voting_schedule
.fetch_missing_votes
< now
&&
1277 !voting_schedule
.have_fetched_missing_votes
) {
1278 log_notice(LD_DIR
, "Time to fetch any votes that we're missing.");
1279 dirvote_fetch_missing_votes();
1280 voting_schedule
.have_fetched_missing_votes
= 1;
1282 if (voting_schedule
.voting_ends
< now
&&
1283 !voting_schedule
.have_built_consensus
) {
1284 log_notice(LD_DIR
, "Time to compute a consensus.");
1285 dirvote_compute_consensus();
1286 /* XXXX We will want to try again later if we haven't got enough
1287 * votes yet. Implement this if it turns out to ever happen. */
1288 voting_schedule
.have_built_consensus
= 1;
1290 if (voting_schedule
.fetch_missing_signatures
< now
&&
1291 !voting_schedule
.have_fetched_missing_signatures
) {
1292 log_notice(LD_DIR
, "Time to fetch any signatures that we're missing.");
1293 dirvote_fetch_missing_signatures();
1294 voting_schedule
.have_fetched_missing_signatures
= 1;
1296 if (voting_schedule
.interval_starts
< now
&&
1297 !voting_schedule
.have_published_consensus
) {
1298 log_notice(LD_DIR
, "Time to publish the consensus and discard old votes");
1299 dirvote_publish_consensus();
1300 dirvote_clear_votes(0);
1301 voting_schedule
.have_published_consensus
= 1;
1302 /* XXXX We will want to try again later if we haven't got enough
1303 * signatures yet. Implement this if it turns out to ever happen. */
1304 dirvote_recalculate_timing(options
, now
);
1308 /** A vote networkstatus_t and its unparsed body: held around so we can
1309 * use it to generate a consensus (at voting_ends) and so we can serve it to
1310 * other authorities that might want it. */
1311 typedef struct pending_vote_t
{
1312 cached_dir_t
*vote_body
;
1313 networkstatus_t
*vote
;
1316 /** List of pending_vote_t for the current vote. Before we've used them to
1317 * build a consensus, the votes go here. */
1318 static smartlist_t
*pending_vote_list
= NULL
;
1319 /** List of pending_vote_t for the previous vote. After we've used them to
1320 * build a consensus, the votes go here for the next period. */
1321 static smartlist_t
*previous_vote_list
= NULL
;
1322 /** The body of the consensus that we're currently building. Once we
1323 * have it built, it goes into dirserv.c */
1324 static char *pending_consensus_body
= NULL
;
1325 /** The detached signatures for the consensus that we're currently
1327 static char *pending_consensus_signatures
= NULL
;
1328 /** The parsed in-progress consensus document. */
1329 static networkstatus_t
*pending_consensus
= NULL
;
1330 /** List of ns_detached_signatures_t: hold signatures that get posted to us
1331 * before we have generated the consensus on our own. */
1332 static smartlist_t
*pending_consensus_signature_list
= NULL
;
1334 /** Generate a networkstatus vote and post it to all the v3 authorities.
1335 * (V3 Authority only) */
1337 dirvote_perform_vote(void)
1339 crypto_pk_env_t
*key
= get_my_v3_authority_signing_key();
1340 authority_cert_t
*cert
= get_my_v3_authority_cert();
1341 networkstatus_t
*ns
;
1343 pending_vote_t
*pending_vote
;
1346 const char *msg
= "";
1348 if (!cert
|| !key
) {
1349 log_warn(LD_NET
, "Didn't find key/certificate to generate v3 vote");
1352 if (!(ns
= dirserv_generate_networkstatus_vote_obj(key
, cert
)))
1355 contents
= format_networkstatus_vote(key
, ns
);
1356 networkstatus_vote_free(ns
);
1360 pending_vote
= dirvote_add_vote(contents
, &msg
, &status
);
1362 if (!pending_vote
) {
1363 log_warn(LD_DIR
, "Couldn't store my own vote! (I told myself, '%s'.)",
1368 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE
,
1369 ROUTER_PURPOSE_GENERAL
,
1371 pending_vote
->vote_body
->dir
,
1372 pending_vote
->vote_body
->dir_len
, 0);
1373 log_notice(LD_DIR
, "Vote posted.");
1377 /** Send an HTTP request to every other v3 authority, for the votes of every
1378 * authority for which we haven't received a vote yet in this period. (V3
1379 * authority only) */
1381 dirvote_fetch_missing_votes(void)
1383 smartlist_t
*missing_fps
= smartlist_create();
1386 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
1387 trusted_dir_server_t
*, ds
,
1389 if (!(ds
->type
& V3_AUTHORITY
))
1391 if (!dirvote_get_vote(ds
->v3_identity_digest
,
1392 DGV_BY_ID
|DGV_INCLUDE_PENDING
)) {
1393 char *cp
= tor_malloc(HEX_DIGEST_LEN
+1);
1394 base16_encode(cp
, HEX_DIGEST_LEN
+1, ds
->v3_identity_digest
,
1396 smartlist_add(missing_fps
, cp
);
1400 if (!smartlist_len(missing_fps
)) {
1401 smartlist_free(missing_fps
);
1404 log_notice(LOG_NOTICE
, "We're missing votes from %d authorities. Asking "
1405 "every other authority for a copy.", smartlist_len(missing_fps
));
1406 resource
= smartlist_join_strings(missing_fps
, "+", 0, NULL
);
1407 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE
,
1410 SMARTLIST_FOREACH(missing_fps
, char *, cp
, tor_free(cp
));
1411 smartlist_free(missing_fps
);
1414 /** Send a request to every other authority for its detached signatures,
1415 * unless we have signatures from all other v3 authorities already. */
1417 dirvote_fetch_missing_signatures(void)
1419 if (!pending_consensus
)
1421 if (networkstatus_check_consensus_signature(pending_consensus
, -1) == 1)
1422 return; /* we have a signature from everybody. */
1424 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
,
1428 /** Drop all currently pending votes, consensus, and detached signatures. */
1430 dirvote_clear_votes(int all_votes
)
1432 if (!previous_vote_list
)
1433 previous_vote_list
= smartlist_create();
1434 if (!pending_vote_list
)
1435 pending_vote_list
= smartlist_create();
1437 /* All "previous" votes are now junk. */
1438 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, v
, {
1439 cached_dir_decref(v
->vote_body
);
1440 v
->vote_body
= NULL
;
1441 networkstatus_vote_free(v
->vote
);
1444 smartlist_clear(previous_vote_list
);
1447 /* If we're dumping all the votes, we delete the pending ones. */
1448 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
, {
1449 cached_dir_decref(v
->vote_body
);
1450 v
->vote_body
= NULL
;
1451 networkstatus_vote_free(v
->vote
);
1455 /* Otherwise, we move them into "previous". */
1456 smartlist_add_all(previous_vote_list
, pending_vote_list
);
1458 smartlist_clear(pending_vote_list
);
1460 if (pending_consensus_signature_list
) {
1461 SMARTLIST_FOREACH(pending_consensus_signature_list
, char *, cp
,
1463 smartlist_clear(pending_consensus_signature_list
);
1465 tor_free(pending_consensus_body
);
1466 tor_free(pending_consensus_signatures
);
1467 if (pending_consensus
) {
1468 networkstatus_vote_free(pending_consensus
);
1469 pending_consensus
= NULL
;
1473 /** Return a newly allocated string containing the hex-encoded v3 authority
1474 identity digest of every recognized v3 authority. */
1476 list_v3_auth_ids(void)
1478 smartlist_t
*known_v3_keys
= smartlist_create();
1480 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
1481 trusted_dir_server_t
*, ds
,
1482 if ((ds
->type
& V3_AUTHORITY
) &&
1483 !tor_digest_is_zero(ds
->v3_identity_digest
))
1484 smartlist_add(known_v3_keys
,
1485 tor_strdup(hex_str(ds
->v3_identity_digest
, DIGEST_LEN
))));
1486 keys
= smartlist_join_strings(known_v3_keys
, ", ", 0, NULL
);
1487 SMARTLIST_FOREACH(known_v3_keys
, char *, cp
, tor_free(cp
));
1488 smartlist_free(known_v3_keys
);
1492 /** Called when we have received a networkstatus vote in <b>vote_body</b>.
1493 * Parse and validate it, and on success store it as a pending vote (which we
1494 * then return). Return NULL on failure. Sets *<b>msg_out</b> and
1495 * *<b>status_out</b> to an HTTP response and status code. (V3 authority
1498 dirvote_add_vote(const char *vote_body
, const char **msg_out
, int *status_out
)
1500 networkstatus_t
*vote
;
1501 networkstatus_voter_info_t
*vi
;
1502 trusted_dir_server_t
*ds
;
1503 pending_vote_t
*pending_vote
= NULL
;
1504 const char *end_of_vote
= NULL
;
1506 tor_assert(vote_body
);
1507 tor_assert(msg_out
);
1508 tor_assert(status_out
);
1510 if (!pending_vote_list
)
1511 pending_vote_list
= smartlist_create();
1516 vote
= networkstatus_parse_vote_from_string(vote_body
, &end_of_vote
, 1);
1518 end_of_vote
= vote_body
+ strlen(vote_body
);
1520 log_warn(LD_DIR
, "Couldn't parse vote: length was %d",
1521 (int)strlen(vote_body
));
1522 *msg_out
= "Unable to parse vote";
1525 tor_assert(smartlist_len(vote
->voters
) == 1);
1526 vi
= get_voter(vote
);
1527 tor_assert(vi
->good_signature
== 1);
1528 ds
= trusteddirserver_get_by_v3_auth_digest(vi
->identity_digest
);
1530 char *keys
= list_v3_auth_ids();
1531 log_warn(LD_DIR
, "Got a vote from an authority (nickname %s, address %s) "
1532 "with authority key ID %s. "
1533 "This key ID is not recognized. Known v3 key IDs are: %s",
1534 vi
->nickname
, vi
->address
,
1535 hex_str(vi
->identity_digest
, DIGEST_LEN
), keys
);
1537 *msg_out
= "Vote not from a recognized v3 authority";
1540 tor_assert(vote
->cert
);
1541 if (!authority_cert_get_by_digests(vote
->cert
->cache_info
.identity_digest
,
1542 vote
->cert
->signing_key_digest
)) {
1543 /* Hey, it's a new cert! */
1544 trusted_dirs_load_certs_from_string(
1545 vote
->cert
->cache_info
.signed_descriptor_body
,
1546 0 /* from_store */, 1 /*flush*/);
1547 if (!authority_cert_get_by_digests(vote
->cert
->cache_info
.identity_digest
,
1548 vote
->cert
->signing_key_digest
)) {
1549 log_warn(LD_BUG
, "We added a cert, but still couldn't find it.");
1553 /* Is it for the right period? */
1554 if (vote
->valid_after
!= voting_schedule
.interval_starts
) {
1555 char tbuf1
[ISO_TIME_LEN
+1], tbuf2
[ISO_TIME_LEN
+1];
1556 format_iso_time(tbuf1
, vote
->valid_after
);
1557 format_iso_time(tbuf2
, voting_schedule
.interval_starts
);
1558 log_warn(LD_DIR
, "Rejecting vote from %s with valid-after time of %s; "
1559 "we were expecting %s", vi
->address
, tbuf1
, tbuf2
);
1560 *msg_out
= "Bad valid-after time";
1564 /* Now see whether we already h<ave a vote from this authority.*/
1565 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
, {
1566 if (! memcmp(v
->vote
->cert
->cache_info
.identity_digest
,
1567 vote
->cert
->cache_info
.identity_digest
,
1569 networkstatus_voter_info_t
*vi_old
= get_voter(v
->vote
);
1570 if (!memcmp(vi_old
->vote_digest
, vi
->vote_digest
, DIGEST_LEN
)) {
1571 /* Ah, it's the same vote. Not a problem. */
1572 log_info(LD_DIR
, "Discarding a vote we already have.");
1573 if (*status_out
< 200)
1576 } else if (v
->vote
->published
< vote
->published
) {
1577 log_notice(LD_DIR
, "Replacing an older pending vote from this "
1579 cached_dir_decref(v
->vote_body
);
1580 networkstatus_vote_free(v
->vote
);
1581 v
->vote_body
= new_cached_dir(tor_strndup(vote_body
,
1582 end_of_vote
-vote_body
),
1586 !strcmpstart(end_of_vote
, "network-status-version"))
1589 if (*status_out
< 200)
1595 *msg_out
= "Already have a newer pending vote";
1601 pending_vote
= tor_malloc_zero(sizeof(pending_vote_t
));
1602 pending_vote
->vote_body
= new_cached_dir(tor_strndup(vote_body
,
1603 end_of_vote
-vote_body
),
1605 pending_vote
->vote
= vote
;
1606 smartlist_add(pending_vote_list
, pending_vote
);
1608 if (!strcmpstart(end_of_vote
, "network-status-version ")) {
1609 vote_body
= end_of_vote
;
1618 *msg_out
= "Error adding vote";
1619 if (*status_out
< 400)
1624 networkstatus_vote_free(vote
);
1626 if (end_of_vote
&& !strcmpstart(end_of_vote
, "network-status-version ")) {
1627 vote_body
= end_of_vote
;
1633 if (*status_out
< 200)
1636 if (!any_failed
&& !pending_vote
) {
1637 *msg_out
= "Duplicate discarded";
1643 return any_failed
? NULL
: pending_vote
;
1646 /** Try to compute a v3 networkstatus consensus from the currently pending
1647 * votes. Return 0 on success, -1 on failure. Store the consensus in
1648 * pending_consensus: it won't be ready to be published until we have
1649 * everybody else's signatures collected too. (V3 Authoritity only) */
1651 dirvote_compute_consensus(void)
1653 /* Have we got enough votes to try? */
1654 int n_votes
, n_voters
;
1655 smartlist_t
*votes
= NULL
;
1656 char *consensus_body
= NULL
, *signatures
= NULL
;
1657 networkstatus_t
*consensus
= NULL
;
1658 authority_cert_t
*my_cert
;
1660 if (!pending_vote_list
)
1661 pending_vote_list
= smartlist_create();
1663 n_voters
= get_n_authorities(V3_AUTHORITY
);
1664 n_votes
= smartlist_len(pending_vote_list
);
1665 if (n_votes
<= n_voters
/2) {
1666 log_warn(LD_DIR
, "We don't have enough votes to generate a consensus: "
1667 "%d of %d", n_votes
, n_voters
/2);
1671 if (!(my_cert
= get_my_v3_authority_cert())) {
1672 log_warn(LD_DIR
, "Can't generate consensus without a certificate.");
1676 votes
= smartlist_create();
1677 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
,
1678 smartlist_add(votes
, v
->vote
));
1680 consensus_body
= networkstatus_compute_consensus(
1682 my_cert
->identity_key
,
1683 get_my_v3_authority_signing_key());
1684 if (!consensus_body
) {
1685 log_warn(LD_DIR
, "Couldn't generate a consensus at all!");
1688 consensus
= networkstatus_parse_vote_from_string(consensus_body
, NULL
, 0);
1690 log_warn(LD_DIR
, "Couldn't parse consensus we generated!");
1693 /* 'Check' our own signature, to mark it valid. */
1694 networkstatus_check_consensus_signature(consensus
, -1);
1696 signatures
= networkstatus_get_detached_signatures(consensus
);
1698 log_warn(LD_DIR
, "Couldn't extract signatures.");
1702 tor_free(pending_consensus_body
);
1703 pending_consensus_body
= consensus_body
;
1704 tor_free(pending_consensus_signatures
);
1705 pending_consensus_signatures
= signatures
;
1707 if (pending_consensus
)
1708 networkstatus_vote_free(pending_consensus
);
1709 pending_consensus
= consensus
;
1711 if (pending_consensus_signature_list
) {
1713 /* we may have gotten signatures for this consensus before we built
1714 * it ourself. Add them now. */
1715 SMARTLIST_FOREACH(pending_consensus_signature_list
, char *, sig
,
1717 const char *msg
= NULL
;
1718 int r
= dirvote_add_signatures_to_pending_consensus(sig
, &msg
);
1723 "Could not add queued signature to new consensus: %s",
1728 log_notice(LD_DIR
, "Added %d pending signatures while building "
1729 "consensus.", n_sigs
);
1730 smartlist_clear(pending_consensus_signature_list
);
1733 log_notice(LD_DIR
, "Consensus computed; uploading signature(s)");
1735 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES
,
1736 ROUTER_PURPOSE_GENERAL
,
1738 pending_consensus_signatures
,
1739 strlen(pending_consensus_signatures
), 0);
1740 log_notice(LD_DIR
, "Signature(s) posted.");
1745 smartlist_free(votes
);
1746 tor_free(consensus_body
);
1747 tor_free(signatures
);
1748 networkstatus_vote_free(consensus
);
1753 /** Helper: we just got the <b>detached_signatures_body</b> sent to us as
1754 * signatures on the currently pending consensus. Add them to the consensus
1755 * as appropriate. Return the number of signatures added. (?) */
1757 dirvote_add_signatures_to_pending_consensus(
1758 const char *detached_signatures_body
,
1759 const char **msg_out
)
1761 ns_detached_signatures_t
*sigs
= NULL
;
1764 tor_assert(detached_signatures_body
);
1765 tor_assert(msg_out
);
1767 /* Only call if we have a pending consensus right now. */
1768 tor_assert(pending_consensus
);
1769 tor_assert(pending_consensus_body
);
1770 tor_assert(pending_consensus_signatures
);
1774 if (!(sigs
= networkstatus_parse_detached_signatures(
1775 detached_signatures_body
, NULL
))) {
1776 *msg_out
= "Couldn't parse detached signatures.";
1780 log_info(LD_DIR
, "Have %d signatures for adding to consensus.",
1781 smartlist_len(sigs
->signatures
));
1782 r
= networkstatus_add_detached_signatures(pending_consensus
,
1784 log_info(LD_DIR
,"Added %d signatures to consensus.", r
);
1787 char *new_detached
=
1788 networkstatus_get_detached_signatures(pending_consensus
);
1790 char *dst
, *dst_end
;
1791 size_t new_consensus_len
=
1792 strlen(pending_consensus_body
) + strlen(new_detached
) + 1;
1793 pending_consensus_body
= tor_realloc(pending_consensus_body
,
1795 dst_end
= pending_consensus_body
+ new_consensus_len
;
1796 dst
= strstr(pending_consensus_body
, "directory-signature ");
1798 src
= strstr(new_detached
, "directory-signature ");
1800 strlcpy(dst
, src
, dst_end
-dst
);
1802 /* We remove this block once it has failed to crash for a while. But
1803 * unless it shows up in profiles, we're probably better leaving it in,
1804 * just in case we break detached signature processing at some point. */
1806 ns_detached_signatures_t
*sigs
=
1807 networkstatus_parse_detached_signatures(new_detached
, NULL
);
1808 networkstatus_t
*v
= networkstatus_parse_vote_from_string(
1809 pending_consensus_body
, NULL
, 0);
1811 ns_detached_signatures_free(sigs
);
1813 networkstatus_vote_free(v
);
1815 tor_free(pending_consensus_signatures
);
1816 pending_consensus_signatures
= new_detached
;
1817 *msg_out
= "Signatures added";
1825 *msg_out
= "Unrecognized error while adding detached signatures.";
1828 ns_detached_signatures_free(sigs
);
1832 /** Helper: we just got the <b>deteached_signatures_body</b> sent to us as
1833 * signatures on the currently pending consensus. Add them to the pending
1834 * consensus (if we have one); otherwise queue them until we have a
1835 * consensus. Return negative on failure, nonnegative on success. */
1837 dirvote_add_signatures(const char *detached_signatures_body
,
1841 if (pending_consensus
) {
1842 log_notice(LD_DIR
, "Got a signature from %s. "
1843 "Adding it to the pending consensus.", source
);
1844 return dirvote_add_signatures_to_pending_consensus(
1845 detached_signatures_body
, msg
);
1847 log_notice(LD_DIR
, "Got a signature from %s. "
1848 "Queueing it for the next consensus.", source
);
1849 if (!pending_consensus_signature_list
)
1850 pending_consensus_signature_list
= smartlist_create();
1851 smartlist_add(pending_consensus_signature_list
,
1852 tor_strdup(detached_signatures_body
));
1853 *msg
= "Signature queued";
1858 /** Replace the consensus that we're currently serving with the one that we've
1859 * been building. (V3 Authority only) */
1861 dirvote_publish_consensus(void)
1863 /* Can we actually publish it yet? */
1864 if (!pending_consensus
||
1865 networkstatus_check_consensus_signature(pending_consensus
, 1)<0) {
1866 log_warn(LD_DIR
, "Not enough info to publish pending consensus");
1870 if (networkstatus_set_current_consensus(pending_consensus_body
, 0))
1871 log_warn(LD_DIR
, "Error publishing consensus");
1873 log_notice(LD_DIR
, "Consensus published.");
1878 /** Release all static storage held in dirvote.c */
1880 dirvote_free_all(void)
1882 dirvote_clear_votes(1);
1883 /* now empty as a result of clear_pending_votes. */
1884 smartlist_free(pending_vote_list
);
1885 pending_vote_list
= NULL
;
1886 smartlist_free(previous_vote_list
);
1887 previous_vote_list
= NULL
;
1889 tor_free(pending_consensus_body
);
1890 tor_free(pending_consensus_signatures
);
1891 if (pending_consensus
) {
1892 networkstatus_vote_free(pending_consensus
);
1893 pending_consensus
= NULL
;
1895 if (pending_consensus_signature_list
) {
1896 /* now empty as a result of clear_pending_votes. */
1897 smartlist_free(pending_consensus_signature_list
);
1898 pending_consensus_signature_list
= NULL
;
1903 * Access to pending items.
1906 /** Return the body of the consensus that we're currently trying to build. */
1908 dirvote_get_pending_consensus(void)
1910 return pending_consensus_body
;
1913 /** Return the signatures that we know for the consensus that we're currently
1914 * trying to build */
1916 dirvote_get_pending_detached_signatures(void)
1918 return pending_consensus_signatures
;
1921 /** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
1922 * vote for the authority with the v3 authority identity key digest <b>fp</b>;
1923 * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
1924 * <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
1925 * false, do not consider any votes for a consensus that's already been built.
1926 * If <b>include_pending</b> is false, do not consider any votes for the
1927 * consensus that's in progress. May return NULL if we have no vote for the
1928 * authority in question. */
1929 const cached_dir_t
*
1930 dirvote_get_vote(const char *fp
, int flags
)
1932 int by_id
= flags
& DGV_BY_ID
;
1933 const int include_pending
= flags
& DGV_INCLUDE_PENDING
;
1934 const int include_previous
= flags
& DGV_INCLUDE_PREVIOUS
;
1936 if (!pending_vote_list
&& !previous_vote_list
)
1939 authority_cert_t
*c
= get_my_v3_authority_cert();
1941 fp
= c
->cache_info
.identity_digest
;
1947 if (pending_vote_list
&& include_pending
) {
1948 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, pv
,
1949 if (!memcmp(get_voter(pv
->vote
)->identity_digest
, fp
, DIGEST_LEN
))
1950 return pv
->vote_body
);
1952 if (previous_vote_list
&& include_previous
) {
1953 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, pv
,
1954 if (!memcmp(get_voter(pv
->vote
)->identity_digest
, fp
, DIGEST_LEN
))
1955 return pv
->vote_body
);
1958 if (pending_vote_list
&& include_pending
) {
1959 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, pv
,
1960 if (!memcmp(pv
->vote
->networkstatus_digest
, fp
, DIGEST_LEN
))
1961 return pv
->vote_body
);
1963 if (previous_vote_list
&& include_previous
) {
1964 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, pv
,
1965 if (!memcmp(pv
->vote
->networkstatus_digest
, fp
, DIGEST_LEN
))
1966 return pv
->vote_body
);