1 /* Copyright 2001-2004 Roger Dingledine.
2 * Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
3 /* See LICENSE for licensing information */
5 const char dirvote_c_id
[] =
8 #define DIRVOTE_PRIVATE
13 * \brief Functions to compute directory consensus, and schedule voting.
16 static int dirvote_add_signatures_to_pending_consensus(
17 const char *detached_signatures_body
,
18 const char **msg_out
);
19 static char *list_v3_auth_ids(void);
20 static void dirvote_fetch_missing_votes(void);
21 static void dirvote_fetch_missing_signatures(void);
22 static int dirvote_perform_vote(void);
23 static void dirvote_clear_votes(int all_votes
);
24 static int dirvote_compute_consensus(void);
25 static int dirvote_publish_consensus(void);
31 /** Return a new string containing teh string representation of the vote in
32 * <b>v3_ns</b>, signed with our v3 signing key <b>private_signing_key</b>.
33 * For v3 authorities. */
35 format_networkstatus_vote(crypto_pk_env_t
*private_signing_key
,
36 networkstatus_vote_t
*v3_ns
)
38 /** Longest status flag name that we generate. */
39 #define LONGEST_STATUS_FLAG_NAME_LEN 9
40 /** Maximum number of status flags we'll apply to one router. */
41 #define N_STATUS_FLAGS 10
42 /** Amount of space to allocate for each entry. (r line and s line.) */
43 #define RS_ENTRY_LEN \
45 MAX_NICKNAME_LEN+BASE64_DIGEST_LEN*2+ISO_TIME_LEN+INET_NTOA_BUF_LEN+ \
46 5*2 /* ports */ + 10 /* punctuation */ + \
48 (LONGEST_STATUS_FLAG_NAME_LEN+1)*N_STATUS_FLAGS + 2)
52 const char *client_versions
= NULL
, *server_versions
= NULL
;
54 char fingerprint
[FINGERPRINT_LEN
+1];
55 char ipaddr
[INET_NTOA_BUF_LEN
];
56 char digest
[DIGEST_LEN
];
59 routerlist_t
*rl
= router_get_routerlist();
60 char *version_lines
= NULL
;
61 networkstatus_voter_info_t
*voter
;
63 tor_assert(private_signing_key
);
65 voter
= smartlist_get(v3_ns
->voters
, 0);
68 in
.s_addr
= htonl(addr
);
69 tor_inet_ntoa(&in
, ipaddr
, sizeof(ipaddr
));
71 base16_encode(fingerprint
, sizeof(fingerprint
),
72 v3_ns
->cert
->cache_info
.identity_digest
, DIGEST_LEN
);
73 client_versions
= v3_ns
->client_versions
;
74 server_versions
= v3_ns
->server_versions
;
76 if (client_versions
|| server_versions
) {
80 v_len
+= strlen(client_versions
);
82 v_len
+= strlen(server_versions
);
83 version_lines
= tor_malloc(v_len
);
85 if (client_versions
) {
86 tor_snprintf(cp
, v_len
-(cp
-version_lines
),
87 "client-versions %s\n", client_versions
);
91 tor_snprintf(cp
, v_len
-(cp
-version_lines
),
92 "server-versions %s\n", server_versions
);
94 version_lines
= tor_strdup("");
98 len
+= strlen(version_lines
);
99 len
+= (RS_ENTRY_LEN
)*smartlist_len(rl
->routers
);
100 len
+= v3_ns
->cert
->cache_info
.signed_descriptor_len
;
102 status
= tor_malloc(len
);
104 char published
[ISO_TIME_LEN
+1];
105 char va
[ISO_TIME_LEN
+1];
106 char fu
[ISO_TIME_LEN
+1];
107 char vu
[ISO_TIME_LEN
+1];
108 char *flags
= smartlist_join_strings(v3_ns
->known_flags
, " ", 0, NULL
);
109 authority_cert_t
*cert
= v3_ns
->cert
;
110 format_iso_time(published
, v3_ns
->published
);
111 format_iso_time(va
, v3_ns
->valid_after
);
112 format_iso_time(fu
, v3_ns
->fresh_until
);
113 format_iso_time(vu
, v3_ns
->valid_until
);
116 tor_snprintf(status
, len
,
117 "network-status-version 3\n"
119 "consensus-methods 1 2\n"
124 "voting-delay %d %d\n"
127 "dir-source %s %s %s %s %d %d\n"
129 published
, va
, fu
, vu
,
130 v3_ns
->vote_seconds
, v3_ns
->dist_seconds
,
133 voter
->nickname
, fingerprint
, voter
->address
,
134 ipaddr
, voter
->dir_port
, voter
->or_port
, voter
->contact
);
137 outp
= status
+ strlen(status
);
139 tor_assert(outp
+ cert
->cache_info
.signed_descriptor_len
< endp
);
140 memcpy(outp
, cert
->cache_info
.signed_descriptor_body
,
141 cert
->cache_info
.signed_descriptor_len
);
143 outp
+= cert
->cache_info
.signed_descriptor_len
;
146 SMARTLIST_FOREACH(v3_ns
->routerstatus_list
, vote_routerstatus_t
*, vrs
,
148 if (routerstatus_format_entry(outp
, endp
-outp
, &vrs
->status
,
149 vrs
->version
, 0) < 0) {
150 log_warn(LD_BUG
, "Unable to print router status.");
153 outp
+= strlen(outp
);
157 char signing_key_fingerprint
[FINGERPRINT_LEN
+1];
158 if (tor_snprintf(outp
, endp
-outp
, "directory-signature ")<0) {
159 log_warn(LD_BUG
, "Unable to start signature line.");
162 outp
+= strlen(outp
);
164 if (crypto_pk_get_fingerprint(private_signing_key
,
165 signing_key_fingerprint
, 0)<0) {
166 log_warn(LD_BUG
, "Unable to get fingerprint for signing key");
169 if (tor_snprintf(outp
, endp
-outp
, "%s %s\n", fingerprint
,
170 signing_key_fingerprint
)<0) {
171 log_warn(LD_BUG
, "Unable to end signature line.");
174 outp
+= strlen(outp
);
177 if (router_get_networkstatus_v3_hash(status
, digest
)<0)
179 note_crypto_pk_op(SIGN_DIR
);
180 if (router_append_dirobj_signature(outp
,endp
-outp
,digest
,
181 private_signing_key
)<0) {
182 log_warn(LD_BUG
, "Unable to sign networkstatus vote.");
187 networkstatus_vote_t
*v
;
188 if (!(v
= networkstatus_parse_vote_from_string(status
, NULL
, 1))) {
189 log_err(LD_BUG
,"Generated a networkstatus vote we couldn't parse: "
193 networkstatus_vote_free(v
);
201 tor_free(version_lines
);
206 * Consensus generation
209 /** Given a vote <b>vote</b> (not a consensus!), return its associated
210 * networkstatus_voter_info_t.*/
211 static networkstatus_voter_info_t
*
212 get_voter(const networkstatus_vote_t
*vote
)
215 tor_assert(vote
->is_vote
);
216 tor_assert(vote
->voters
);
217 tor_assert(smartlist_len(vote
->voters
) == 1);
218 return smartlist_get(vote
->voters
, 0);
221 /** Helper for sorting networkstatus_vote_t votes (not consensuses) by the
222 * hash of their voters' identity digests. */
224 _compare_votes_by_authority_id(const void **_a
, const void **_b
)
226 const networkstatus_vote_t
*a
= *_a
, *b
= *_b
;
227 return memcmp(get_voter(a
)->identity_digest
,
228 get_voter(b
)->identity_digest
, DIGEST_LEN
);
231 /** Given a sorted list of strings <b>in</b>, add every member to <b>out</b>
232 * that occurs more than <b>min</b> times. */
234 get_frequent_members(smartlist_t
*out
, smartlist_t
*in
, int min
)
238 SMARTLIST_FOREACH(in
, char *, cp
,
240 if (cur
&& !strcmp(cp
, cur
)) {
244 smartlist_add(out
, cur
);
250 smartlist_add(out
, cur
);
253 /** Given a sorted list of strings <b>lst</b>, return the member that appears
254 * most. Break ties in favor of later-occurring members. */
256 get_most_frequent_member(smartlist_t
*lst
)
258 const char *most_frequent
= NULL
;
259 int most_frequent_count
= 0;
261 const char *cur
= NULL
;
264 SMARTLIST_FOREACH(lst
, const char *, s
,
266 if (cur
&& !strcmp(s
, cur
)) {
269 if (count
>= most_frequent_count
) {
271 most_frequent_count
= count
;
277 if (count
>= most_frequent_count
) {
279 most_frequent_count
= count
;
281 return most_frequent
;
284 /** Return 0 if and only if <b>a</b> and <b>b</b> are routerstatuses
285 * that come from the same routerinfo, with the same derived elements.
288 compare_vote_rs(const vote_routerstatus_t
*a
, const vote_routerstatus_t
*b
)
291 if ((r
= memcmp(a
->status
.identity_digest
, b
->status
.identity_digest
,
294 if ((r
= memcmp(a
->status
.descriptor_digest
, b
->status
.descriptor_digest
,
297 if ((r
= (b
->status
.published_on
- a
->status
.published_on
)))
299 if ((r
= strcmp(b
->status
.nickname
, a
->status
.nickname
)))
301 if ((r
= (((int)b
->status
.addr
) - ((int)a
->status
.addr
))))
303 if ((r
= (((int)b
->status
.or_port
) - ((int)a
->status
.or_port
))))
305 if ((r
= (((int)b
->status
.dir_port
) - ((int)a
->status
.dir_port
))))
310 /** Helper for sorting routerlists based on compare_vote_rs. */
312 _compare_vote_rs(const void **_a
, const void **_b
)
314 const vote_routerstatus_t
*a
= *_a
, *b
= *_b
;
315 return compare_vote_rs(a
,b
);
318 /** Given a list of vote_routerstatus_t, all for the same router identity,
319 * return whichever is most frequent, breaking ties in favor of more
320 * recently published vote_routerstatus_t.
322 static vote_routerstatus_t
*
323 compute_routerstatus_consensus(smartlist_t
*votes
)
325 vote_routerstatus_t
*most
= NULL
, *cur
= NULL
;
326 int most_n
= 0, cur_n
= 0;
327 time_t most_published
= 0;
329 smartlist_sort(votes
, _compare_vote_rs
);
330 SMARTLIST_FOREACH(votes
, vote_routerstatus_t
*, rs
,
332 if (cur
&& !compare_vote_rs(cur
, rs
)) {
335 if (cur_n
> most_n
||
336 (cur
&& cur_n
== most_n
&&
337 cur
->status
.published_on
> most_published
)) {
340 most_published
= cur
->status
.published_on
;
347 if (cur_n
> most_n
||
348 (cur
&& cur_n
== most_n
&& cur
->status
.published_on
> most_published
)) {
351 most_published
= cur
->status
.published_on
;
358 /** Given a list of strings in <b>lst</b>, set the DIGEST_LEN-byte digest at
359 * <b>digest_out</b> to the hash of the concatenation of those strings. */
361 hash_list_members(char *digest_out
, smartlist_t
*lst
)
363 crypto_digest_env_t
*d
= crypto_new_digest_env();
364 SMARTLIST_FOREACH(lst
, const char *, cp
,
365 crypto_digest_add_bytes(d
, cp
, strlen(cp
)));
366 crypto_digest_get_digest(d
, digest_out
, DIGEST_LEN
);
367 crypto_free_digest_env(d
);
372 _cmp_int_strings(const void **_a
, const void **_b
)
374 const char *a
= *_a
, *b
= *_b
;
375 int ai
= (int)tor_parse_long(a
, 10, 1, INT_MAX
, NULL
, NULL
);
376 int bi
= (int)tor_parse_long(b
, 10, 1, INT_MAX
, NULL
, NULL
);
387 compute_consensus_method(smartlist_t
*votes
)
389 smartlist_t
*all_methods
= smartlist_create();
390 smartlist_t
*acceptable_methods
= smartlist_create();
391 smartlist_t
*tmp
= smartlist_create();
392 int min
= (smartlist_len(votes
) * 2) / 3;
395 SMARTLIST_FOREACH(votes
, networkstatus_vote_t
*, vote
,
397 tor_assert(vote
->supported_methods
);
398 smartlist_add_all(tmp
, vote
->supported_methods
);
399 smartlist_sort(tmp
, _cmp_int_strings
);
400 smartlist_uniq(tmp
, _cmp_int_strings
, NULL
);
401 smartlist_add_all(all_methods
, tmp
);
402 smartlist_clear(tmp
);
405 smartlist_sort(all_methods
, _cmp_int_strings
);
406 get_frequent_members(acceptable_methods
, all_methods
, min
);
407 n_ok
= smartlist_len(acceptable_methods
);
409 const char *best
= smartlist_get(acceptable_methods
, n_ok
-1);
410 result
= (int)tor_parse_long(best
, 10, 1, INT_MAX
, NULL
, NULL
);
415 smartlist_free(all_methods
);
416 smartlist_free(acceptable_methods
);
422 consensus_method_is_supported(int method
)
424 return (method
>= 1) && (method
<= 2);
427 /** Given a list of vote networkstatus_vote_t in <b>votes</b>, our public
428 * authority <b>identity_key</b>, our private authority <b>signing_key</b>,
429 * and the number of <b>total_authorities</b> that we believe exist in our
430 * voting quorum, generate the text of a new v3 consensus vote, and return the
431 * value in a newly allocated string.
433 * Note: this function DOES NOT check whether the votes are from
434 * recognized authorities. (dirvote_add_vote does that.) */
436 networkstatus_compute_consensus(smartlist_t
*votes
,
437 int total_authorities
,
438 crypto_pk_env_t
*identity_key
,
439 crypto_pk_env_t
*signing_key
)
443 int consensus_method
;
445 time_t valid_after
, fresh_until
, valid_until
;
446 int vote_seconds
, dist_seconds
;
447 char *client_versions
= NULL
, *server_versions
= NULL
;
449 tor_assert(total_authorities
>= smartlist_len(votes
));
451 if (!smartlist_len(votes
)) {
452 log_warn(LD_DIR
, "Can't compute a consensus from no votes.");
455 flags
= smartlist_create();
457 consensus_method
= compute_consensus_method(votes
);
458 if (consensus_method_is_supported(consensus_method
)) {
459 log_info(LD_DIR
, "Generating consensus using method %d.",
462 log_warn(LD_DIR
, "The other authorities will use consensus method %d, "
463 "which I don't support. Maybe I should upgrade!",
465 consensus_method
= 1;
468 /* Compute medians of time-related things, and figure out how many
469 * routers we might need to talk about. */
471 int n_votes
= smartlist_len(votes
);
472 time_t *va_times
= tor_malloc(n_votes
* sizeof(time_t));
473 time_t *fu_times
= tor_malloc(n_votes
* sizeof(time_t));
474 time_t *vu_times
= tor_malloc(n_votes
* sizeof(time_t));
475 int *votesec_list
= tor_malloc(n_votes
* sizeof(int));
476 int *distsec_list
= tor_malloc(n_votes
* sizeof(int));
477 int n_versioning_clients
= 0, n_versioning_servers
= 0;
478 smartlist_t
*combined_client_versions
= smartlist_create();
479 smartlist_t
*combined_server_versions
= smartlist_create();
481 SMARTLIST_FOREACH(votes
, networkstatus_vote_t
*, v
,
483 tor_assert(v
->is_vote
);
484 va_times
[v_sl_idx
] = v
->valid_after
;
485 fu_times
[v_sl_idx
] = v
->fresh_until
;
486 vu_times
[v_sl_idx
] = v
->valid_until
;
487 votesec_list
[v_sl_idx
] = v
->vote_seconds
;
488 distsec_list
[v_sl_idx
] = v
->dist_seconds
;
489 if (v
->client_versions
) {
490 smartlist_t
*cv
= smartlist_create();
491 ++n_versioning_clients
;
492 smartlist_split_string(cv
, v
->client_versions
, ",",
493 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
494 sort_version_list(cv
, 1);
495 smartlist_add_all(combined_client_versions
, cv
);
496 smartlist_free(cv
); /* elements get freed later. */
498 if (v
->server_versions
) {
499 smartlist_t
*sv
= smartlist_create();
500 ++n_versioning_servers
;
501 smartlist_split_string(sv
, v
->server_versions
, ",",
502 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
503 sort_version_list(sv
, 1);
504 smartlist_add_all(combined_server_versions
, sv
);
505 smartlist_free(sv
); /* elements get freed later. */
507 SMARTLIST_FOREACH(v
->known_flags
, const char *, cp
,
508 smartlist_add(flags
, tor_strdup(cp
)));
510 valid_after
= median_time(va_times
, n_votes
);
511 fresh_until
= median_time(fu_times
, n_votes
);
512 valid_until
= median_time(vu_times
, n_votes
);
513 vote_seconds
= median_int(votesec_list
, n_votes
);
514 dist_seconds
= median_int(distsec_list
, n_votes
);
516 tor_assert(valid_after
+MIN_VOTE_INTERVAL
<= fresh_until
);
517 tor_assert(fresh_until
+MIN_VOTE_INTERVAL
<= valid_until
);
518 tor_assert(vote_seconds
>= MIN_VOTE_SECONDS
);
519 tor_assert(dist_seconds
>= MIN_DIST_SECONDS
);
521 for (j
= 0; j
< 2; ++j
) {
523 j
? combined_server_versions
: combined_client_versions
;
524 int min
= (j
? n_versioning_servers
: n_versioning_clients
) / 2;
525 smartlist_t
*good
= smartlist_create();
527 sort_version_list(lst
, 0);
528 get_frequent_members(good
, lst
, min
);
529 res
= smartlist_join_strings(good
, ",", 0, NULL
);
531 server_versions
= res
;
533 client_versions
= res
;
534 SMARTLIST_FOREACH(lst
, char *, cp
, tor_free(cp
));
535 smartlist_free(good
);
539 smartlist_sort_strings(flags
);
540 smartlist_uniq_strings(flags
);
545 tor_free(votesec_list
);
546 tor_free(distsec_list
);
549 chunks
= smartlist_create();
553 char va_buf
[ISO_TIME_LEN
+1], fu_buf
[ISO_TIME_LEN
+1],
554 vu_buf
[ISO_TIME_LEN
+1];
556 format_iso_time(va_buf
, valid_after
);
557 format_iso_time(fu_buf
, fresh_until
);
558 format_iso_time(vu_buf
, valid_until
);
559 flaglist
= smartlist_join_strings(flags
, " ", 0, NULL
);
561 smartlist_add(chunks
, tor_strdup("network-status-version 3\n"
562 "vote-status consensus\n"));
564 if (consensus_method
>= 2) {
565 tor_snprintf(buf
, sizeof(buf
), "consensus-method %d\n",
567 smartlist_add(chunks
, tor_strdup(buf
));
570 tor_snprintf(buf
, sizeof(buf
),
574 "voting-delay %d %d\n"
575 "client-versions %s\n"
576 "server-versions %s\n"
578 va_buf
, fu_buf
, vu_buf
,
579 vote_seconds
, dist_seconds
,
580 client_versions
, server_versions
, flaglist
);
581 smartlist_add(chunks
, tor_strdup(buf
));
586 /* Sort the votes. */
587 smartlist_sort(votes
, _compare_votes_by_authority_id
);
588 /* Add the authority sections. */
589 SMARTLIST_FOREACH(votes
, networkstatus_vote_t
*, v
,
593 char ip
[INET_NTOA_BUF_LEN
];
594 char fingerprint
[HEX_DIGEST_LEN
+1];
595 char votedigest
[HEX_DIGEST_LEN
+1];
596 networkstatus_voter_info_t
*voter
= get_voter(v
);
598 in
.s_addr
= htonl(voter
->addr
);
599 tor_inet_ntoa(&in
, ip
, sizeof(ip
));
600 base16_encode(fingerprint
, sizeof(fingerprint
), voter
->identity_digest
,
602 base16_encode(votedigest
, sizeof(votedigest
), voter
->vote_digest
,
605 tor_snprintf(buf
, sizeof(buf
),
606 "dir-source %s %s %s %s %d %d\n"
609 voter
->nickname
, fingerprint
, voter
->address
, ip
,
614 smartlist_add(chunks
, tor_strdup(buf
));
617 /* Add the actual router entries. */
619 int *index
; /* index[j] is the current index into votes[j]. */
620 int *size
; /* size[j] is the number of routerstatuses in votes[j]. */
621 int *flag_counts
; /* The number of voters that list flag[j] for the
622 * currently considered router. */
624 smartlist_t
*matching_descs
= smartlist_create();
625 smartlist_t
*chosen_flags
= smartlist_create();
626 smartlist_t
*versions
= smartlist_create();
628 int *n_voter_flags
; /* n_voter_flags[j] is the number of flags that
629 * votes[j] knows about. */
630 int *n_flag_voters
; /* n_flag_voters[f] is the number of votes that care
632 int **flag_map
; /* flag_map[j][b] is an index f such that flag_map[f]
633 * is the same flag as votes[j]->known_flags[b]. */
634 int *named_flag
; /* Index of the flag "Named" for votes[j] */
635 int *unnamed_flag
; /* Index of the flag "Unnamed" for votes[j] */
636 int chosen_named_idx
, chosen_unnamed_idx
;
638 strmap_t
*name_to_id_map
= strmap_new();
639 char conflict
[DIGEST_LEN
];
640 char unknown
[DIGEST_LEN
];
641 memset(conflict
, 0, sizeof(conflict
));
642 memset(unknown
, 0xff, sizeof(conflict
));
644 index
= tor_malloc_zero(sizeof(int)*smartlist_len(votes
));
645 size
= tor_malloc_zero(sizeof(int)*smartlist_len(votes
));
646 n_voter_flags
= tor_malloc_zero(sizeof(int) * smartlist_len(votes
));
647 n_flag_voters
= tor_malloc_zero(sizeof(int) * smartlist_len(flags
));
648 flag_map
= tor_malloc_zero(sizeof(int*) * smartlist_len(votes
));
649 named_flag
= tor_malloc_zero(sizeof(int*) * smartlist_len(votes
));
650 unnamed_flag
= tor_malloc_zero(sizeof(int*) * smartlist_len(votes
));
651 for (i
= 0; i
< smartlist_len(votes
); ++i
)
652 unnamed_flag
[i
] = named_flag
[i
] = -1;
653 chosen_named_idx
= smartlist_string_pos(flags
, "Named");
654 chosen_unnamed_idx
= smartlist_string_pos(flags
, "Unnamed");
656 /* Build the flag index. */
657 SMARTLIST_FOREACH(votes
, networkstatus_vote_t
*, v
,
659 flag_map
[v_sl_idx
] = tor_malloc_zero(
660 sizeof(int)*smartlist_len(v
->known_flags
));
661 SMARTLIST_FOREACH(v
->known_flags
, const char *, fl
,
663 int p
= smartlist_string_pos(flags
, fl
);
665 flag_map
[v_sl_idx
][fl_sl_idx
] = p
;
667 if (!strcmp(fl
, "Named"))
668 named_flag
[v_sl_idx
] = fl_sl_idx
;
669 if (!strcmp(fl
, "Unnamed"))
670 unnamed_flag
[v_sl_idx
] = fl_sl_idx
;
672 n_voter_flags
[v_sl_idx
] = smartlist_len(v
->known_flags
);
673 size
[v_sl_idx
] = smartlist_len(v
->routerstatus_list
);
676 /* Named and Unnamed get treated specially */
677 if (consensus_method
>= 2) {
678 SMARTLIST_FOREACH(votes
, networkstatus_vote_t
*, v
,
681 if (named_flag
[v_sl_idx
]<0)
683 nf
= U64_LITERAL(1) << named_flag
[v_sl_idx
];
684 SMARTLIST_FOREACH(v
->routerstatus_list
, vote_routerstatus_t
*, rs
,
686 if ((rs
->flags
& nf
) != 0) {
687 const char *d
= strmap_get_lc(name_to_id_map
, rs
->status
.nickname
);
689 /* We have no name officially mapped to this digest. */
690 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
,
691 rs
->status
.identity_digest
);
692 } else if (d
!= conflict
&&
693 memcmp(d
, rs
->status
.identity_digest
, DIGEST_LEN
)) {
694 /* Authorities disagree about this nickname. */
695 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, conflict
);
697 /* It's already a conflict, or it's already this ID. */
702 SMARTLIST_FOREACH(votes
, networkstatus_vote_t
*, v
,
705 if (unnamed_flag
[v_sl_idx
]<0)
707 uf
= U64_LITERAL(1) << unnamed_flag
[v_sl_idx
];
708 SMARTLIST_FOREACH(v
->routerstatus_list
, vote_routerstatus_t
*, rs
,
710 if ((rs
->flags
& uf
) != 0) {
711 const char *d
= strmap_get_lc(name_to_id_map
, rs
->status
.nickname
);
712 if (d
== conflict
|| d
== unknown
) {
713 /* Leave it alone; we know what it is. */
715 /* We have no name officially mapped to this digest. */
716 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, unknown
);
717 } else if (!memcmp(d
, rs
->status
.identity_digest
, DIGEST_LEN
)) {
718 /* Authorities disagree about this nickname. */
719 strmap_set_lc(name_to_id_map
, rs
->status
.nickname
, conflict
);
721 /* It's mapped to a different name. */
728 /* Now go through all the votes */
729 flag_counts
= tor_malloc(sizeof(int) * smartlist_len(flags
));
731 vote_routerstatus_t
*rs
;
732 routerstatus_t rs_out
;
733 const char *lowest_id
= NULL
;
734 const char *chosen_version
;
735 const char *chosen_name
= NULL
;
736 int is_named
= 0, is_unnamed
= 0;
737 int naming_conflict
= 0;
742 /* Of the next-to-be-considered digest in each voter, which is first? */
743 SMARTLIST_FOREACH(votes
, networkstatus_vote_t
*, v
, {
744 if (index
[v_sl_idx
] < size
[v_sl_idx
]) {
745 rs
= smartlist_get(v
->routerstatus_list
, index
[v_sl_idx
]);
747 memcmp(rs
->status
.identity_digest
, lowest_id
, DIGEST_LEN
) < 0)
748 lowest_id
= rs
->status
.identity_digest
;
751 if (!lowest_id
) /* we're out of routers. */
754 memset(flag_counts
, 0, sizeof(int)*smartlist_len(flags
));
755 smartlist_clear(matching_descs
);
756 smartlist_clear(chosen_flags
);
757 smartlist_clear(versions
);
759 /* Okay, go through all the entries for this digest. */
760 SMARTLIST_FOREACH(votes
, networkstatus_vote_t
*, v
, {
761 if (index
[v_sl_idx
] >= size
[v_sl_idx
])
762 continue; /* out of entries. */
763 rs
= smartlist_get(v
->routerstatus_list
, index
[v_sl_idx
]);
764 if (memcmp(rs
->status
.identity_digest
, lowest_id
, DIGEST_LEN
))
765 continue; /* doesn't include this router. */
766 /* At this point, we know that we're looking at a routersatus with
772 smartlist_add(matching_descs
, rs
);
773 if (rs
->version
&& rs
->version
[0])
774 smartlist_add(versions
, rs
->version
);
776 /* Tally up all the flags. */
777 for (i
= 0; i
< n_voter_flags
[v_sl_idx
]; ++i
) {
778 if (rs
->flags
& (U64_LITERAL(1) << i
))
779 ++flag_counts
[flag_map
[v_sl_idx
][i
]];
781 if (rs
->flags
& (U64_LITERAL(1) << named_flag
[v_sl_idx
])) {
782 if (chosen_name
&& strcmp(chosen_name
, rs
->status
.nickname
)) {
783 log_notice(LD_DIR
, "Conflict on naming for router: %s vs %s",
784 chosen_name
, rs
->status
.nickname
);
787 chosen_name
= rs
->status
.nickname
;
791 /* We don't include this router at all unless more than half of
792 * the authorities we believe in list it. */
793 if (n_listing
<= total_authorities
/2)
796 /* Figure out the most popular opinion of what the most recent
797 * routerinfo and its contents are. */
798 rs
= compute_routerstatus_consensus(matching_descs
);
799 /* Copy bits of that into rs_out. */
800 tor_assert(!memcmp(lowest_id
, rs
->status
.identity_digest
, DIGEST_LEN
));
801 memcpy(rs_out
.identity_digest
, lowest_id
, DIGEST_LEN
);
802 memcpy(rs_out
.descriptor_digest
, rs
->status
.descriptor_digest
,
804 rs_out
.addr
= rs
->status
.addr
;
805 rs_out
.published_on
= rs
->status
.published_on
;
806 rs_out
.dir_port
= rs
->status
.dir_port
;
807 rs_out
.or_port
= rs
->status
.or_port
;
809 if (chosen_name
&& !naming_conflict
) {
810 strlcpy(rs_out
.nickname
, chosen_name
, sizeof(rs_out
.nickname
));
812 strlcpy(rs_out
.nickname
, rs
->status
.nickname
, sizeof(rs_out
.nickname
));
815 if (consensus_method
== 1) {
816 is_named
= chosen_named_idx
>= 0 &&
817 (!naming_conflict
&& flag_counts
[chosen_named_idx
]);
819 const char *d
= strmap_get_lc(name_to_id_map
, rs_out
.nickname
);
821 is_named
= is_unnamed
= 0;
822 } else if (!memcmp(d
, lowest_id
, DIGEST_LEN
)) {
823 is_named
= 1; is_unnamed
= 0;
825 is_named
= 0; is_unnamed
= 1;
830 smartlist_add(chosen_flags
, (char*)"s"); /* for the start of the line. */
831 SMARTLIST_FOREACH(flags
, const char *, fl
,
833 if (!strcmp(fl
, "Named")) {
835 smartlist_add(chosen_flags
, (char*)fl
);
836 } else if (!strcmp(fl
, "Unnamed") && consensus_method
>= 2) {
838 smartlist_add(chosen_flags
, (char*)fl
);
840 if (flag_counts
[fl_sl_idx
] > n_flag_voters
[fl_sl_idx
]/2)
841 smartlist_add(chosen_flags
, (char*)fl
);
845 /* Pick the version. */
846 if (smartlist_len(versions
)) {
847 sort_version_list(versions
, 0);
848 chosen_version
= get_most_frequent_member(versions
);
850 chosen_version
= NULL
;
853 /* Okay!! Now we can write the descriptor... */
854 /* First line goes into "buf". */
855 routerstatus_format_entry(buf
, sizeof(buf
), &rs_out
, NULL
, 1);
856 smartlist_add(chunks
, tor_strdup(buf
));
857 /* Second line is all flags. The "\n" is missing. */
858 smartlist_add(chunks
,
859 smartlist_join_strings(chosen_flags
, " ", 0, NULL
));
860 /* Now the version line. */
861 if (chosen_version
) {
862 smartlist_add(chunks
, tor_strdup("\nv "));
863 smartlist_add(chunks
, tor_strdup(chosen_version
));
865 smartlist_add(chunks
, tor_strdup("\n"));
867 /* And the loop is over and we move on to the next router */
872 tor_free(n_voter_flags
);
873 tor_free(n_flag_voters
);
874 for (i
= 0; i
< smartlist_len(votes
); ++i
)
875 tor_free(flag_map
[i
]);
877 tor_free(flag_counts
);
878 tor_free(named_flag
);
879 tor_free(unnamed_flag
);
880 strmap_free(name_to_id_map
, NULL
);
881 smartlist_free(matching_descs
);
882 smartlist_free(chosen_flags
);
883 smartlist_free(versions
);
886 /* Add a signature. */
888 char digest
[DIGEST_LEN
];
889 char fingerprint
[HEX_DIGEST_LEN
+1];
890 char signing_key_fingerprint
[HEX_DIGEST_LEN
+1];
893 smartlist_add(chunks
, tor_strdup("directory-signature "));
895 /* Compute the hash of the chunks. */
896 hash_list_members(digest
, chunks
);
898 /* Get the fingerprints */
899 crypto_pk_get_fingerprint(identity_key
, fingerprint
, 0);
900 crypto_pk_get_fingerprint(signing_key
, signing_key_fingerprint
, 0);
902 /* add the junk that will go at the end of the line. */
903 tor_snprintf(buf
, sizeof(buf
), "%s %s\n", fingerprint
,
904 signing_key_fingerprint
);
905 /* And the signature. */
906 if (router_append_dirobj_signature(buf
, sizeof(buf
), digest
,
908 log_warn(LD_BUG
, "Couldn't sign consensus networkstatus.");
909 return NULL
; /* This leaks, but it should never happen. */
911 smartlist_add(chunks
, tor_strdup(buf
));
914 result
= smartlist_join_strings(chunks
, "", 0, NULL
);
916 tor_free(client_versions
);
917 tor_free(server_versions
);
918 smartlist_free(flags
);
919 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
920 smartlist_free(chunks
);
923 networkstatus_vote_t
*c
;
924 if (!(c
= networkstatus_parse_vote_from_string(result
, NULL
, 0))) {
925 log_err(LD_BUG
,"Generated a networkstatus consensus we couldn't "
930 networkstatus_vote_free(c
);
936 /** Given a consensus vote <b>target</b> and a set of detached signatures in
937 * <b>sigs</b> that correspond to the same consensus, check whether there are
938 * any new signatures in <b>src_voter_list</b> that should be added to
939 * <b>target. (A signature should be added if we have no signature for that
940 * voter in <b>target</b> yet, or if we have no verifiable signature and the
941 * new signature is verifiable.) Return the number of signatures added or
942 * changed, or -1 if the document signed by <b>sigs</b> isn't the same
943 * document as <b>target</b>. */
945 networkstatus_add_detached_signatures(networkstatus_vote_t
*target
,
946 ns_detached_signatures_t
*sigs
,
947 const char **msg_out
)
952 tor_assert(!target
->is_vote
);
954 /* Do the times seem right? */
955 if (target
->valid_after
!= sigs
->valid_after
) {
956 *msg_out
= "Valid-After times do not match "
957 "when adding detached signatures to consensus";
960 if (target
->fresh_until
!= sigs
->fresh_until
) {
961 *msg_out
= "Fresh-until times do not match "
962 "when adding detached signatures to consensus";
965 if (target
->valid_until
!= sigs
->valid_until
) {
966 *msg_out
= "Valid-until times do not match "
967 "when adding detached signatures to consensus";
970 /* Are they the same consensus? */
971 if (memcmp(target
->networkstatus_digest
, sigs
->networkstatus_digest
,
973 *msg_out
= "Digest mismatch when adding detached signatures to consensus";
977 /* For each voter in src... */
978 SMARTLIST_FOREACH(sigs
->signatures
, networkstatus_voter_info_t
*, src_voter
,
980 networkstatus_voter_info_t
*target_voter
=
981 networkstatus_get_voter_by_id(target
, src_voter
->identity_digest
);
982 authority_cert_t
*cert
;
983 /* If the target doesn't know about this voter, then forget it. */
987 /* If the target already has a good signature from this voter, then skip
989 if (target_voter
->good_signature
)
992 /* Try checking the signature if we haven't already. */
993 if (!src_voter
->good_signature
&& !src_voter
->bad_signature
) {
994 cert
= authority_cert_get_by_digests(src_voter
->identity_digest
,
995 src_voter
->signing_key_digest
);
997 networkstatus_check_voter_signature(target
, src_voter
, cert
);
1000 /* If this signature is good, or we don't have any signature yet,
1002 if (src_voter
->good_signature
|| !target_voter
->signature
) {
1004 tor_free(target_voter
->signature
);
1005 target_voter
->signature
=
1006 tor_memdup(src_voter
->signature
, src_voter
->signature_len
);
1007 memcpy(target_voter
->signing_key_digest
, src_voter
->signing_key_digest
,
1009 target_voter
->signature_len
= src_voter
->signature_len
;
1010 target_voter
->good_signature
= 1;
1011 target_voter
->bad_signature
= 0;
1018 /** Return a newly allocated string holding the detached-signatures document
1019 * corresponding to the signatures on <b>consensus</b>. */
1021 networkstatus_get_detached_signatures(networkstatus_vote_t
*consensus
)
1023 smartlist_t
*elements
;
1025 char *result
= NULL
;
1027 tor_assert(consensus
);
1028 tor_assert(! consensus
->is_vote
);
1030 elements
= smartlist_create();
1033 char va_buf
[ISO_TIME_LEN
+1], fu_buf
[ISO_TIME_LEN
+1],
1034 vu_buf
[ISO_TIME_LEN
+1];
1035 char d
[HEX_DIGEST_LEN
+1];
1037 base16_encode(d
, sizeof(d
), consensus
->networkstatus_digest
, DIGEST_LEN
);
1038 format_iso_time(va_buf
, consensus
->valid_after
);
1039 format_iso_time(fu_buf
, consensus
->fresh_until
);
1040 format_iso_time(vu_buf
, consensus
->valid_until
);
1042 tor_snprintf(buf
, sizeof(buf
),
1043 "consensus-digest %s\n"
1046 "valid-until %s\n", d
, va_buf
, fu_buf
, vu_buf
);
1047 smartlist_add(elements
, tor_strdup(buf
));
1050 SMARTLIST_FOREACH(consensus
->voters
, networkstatus_voter_info_t
*, v
,
1052 char sk
[HEX_DIGEST_LEN
+1];
1053 char id
[HEX_DIGEST_LEN
+1];
1054 if (!v
->signature
|| v
->bad_signature
)
1057 base16_encode(sk
, sizeof(sk
), v
->signing_key_digest
, DIGEST_LEN
);
1058 base16_encode(id
, sizeof(id
), v
->identity_digest
, DIGEST_LEN
);
1059 tor_snprintf(buf
, sizeof(buf
),
1060 "directory-signature %s %s\n-----BEGIN SIGNATURE-----\n",
1062 smartlist_add(elements
, tor_strdup(buf
));
1063 base64_encode(buf
, sizeof(buf
), v
->signature
, v
->signature_len
);
1064 strlcat(buf
, "-----END SIGNATURE-----\n", sizeof(buf
));
1065 smartlist_add(elements
, tor_strdup(buf
));
1068 result
= smartlist_join_strings(elements
, "", 0, NULL
);
1070 SMARTLIST_FOREACH(elements
, char *, cp
, tor_free(cp
));
1071 smartlist_free(elements
);
1077 /** Release all storage held in <b>s</b>. */
1079 ns_detached_signatures_free(ns_detached_signatures_t
*s
)
1081 if (s
->signatures
) {
1082 SMARTLIST_FOREACH(s
->signatures
, networkstatus_voter_info_t
*, v
,
1084 tor_free(v
->signature
);
1087 smartlist_free(s
->signatures
);
1093 * Certificate functions
1096 /*XXXX020 make this static? */
1097 /** Allocate and return a new authority_cert_t with the same contents as
1100 authority_cert_dup(authority_cert_t
*cert
)
1102 authority_cert_t
*out
= tor_malloc(sizeof(authority_cert_t
));
1105 memcpy(out
, cert
, sizeof(authority_cert_t
));
1106 /* Now copy pointed-to things. */
1107 out
->cache_info
.signed_descriptor_body
=
1108 tor_strndup(cert
->cache_info
.signed_descriptor_body
,
1109 cert
->cache_info
.signed_descriptor_len
);
1110 out
->cache_info
.saved_location
= SAVED_NOWHERE
;
1111 out
->identity_key
= crypto_pk_dup_key(cert
->identity_key
);
1112 out
->signing_key
= crypto_pk_dup_key(cert
->signing_key
);
1121 /** Set *<b>timing_out</b> to the intervals at which we would like to vote.
1122 * Note that these aren't the intervals we'll use to vote; they're the ones
1123 * that we'll vote to use. */
1125 dirvote_get_preferred_voting_intervals(vote_timing_t
*timing_out
)
1127 or_options_t
*options
= get_options();
1129 tor_assert(timing_out
);
1131 timing_out
->vote_interval
= options
->V3AuthVotingInterval
;
1132 timing_out
->n_intervals_valid
= options
->V3AuthNIntervalsValid
;
1133 timing_out
->vote_delay
= options
->V3AuthVoteDelay
;
1134 timing_out
->dist_delay
= options
->V3AuthDistDelay
;
1137 /** Return the start of the next interval of size <b>interval</b> (in seconds)
1138 * after <b>now</b>. Midnight always starts a fresh interval, and if the last
1139 * interval of a day would be truncated to less than half its size, it is
1140 * rolled into the previous interval. */
1142 dirvote_get_start_of_next_interval(time_t now
, int interval
)
1145 time_t midnight_today
;
1146 time_t midnight_tomorrow
;
1149 tor_gmtime_r(&now
, &tm
);
1154 midnight_today
= tor_timegm(&tm
);
1155 midnight_tomorrow
= midnight_today
+ (24*60*60);
1157 next
= midnight_today
+ ((now
-midnight_today
)/interval
+ 1)*interval
;
1159 /* Intervals never cross midnight. */
1160 if (next
> midnight_tomorrow
)
1161 next
= midnight_tomorrow
;
1163 /* If the interval would only last half as long as it's supposed to, then
1164 * skip over to the next day. */
1165 if (next
+ interval
/2 > midnight_tomorrow
)
1166 next
= midnight_tomorrow
;
1171 /** Scheduling information for a voting interval. */
1173 /** When do we generate and distribute our vote for this interval? */
1174 time_t voting_starts
;
1175 /** When do we send an HTTP request for any votes that we haven't
1176 * been posted yet?*/
1177 time_t fetch_missing_votes
;
1178 /** When do we give up on getting more votes and generate a consensus? */
1180 /** When do we send an HTTP request for any signatures we're expecting to
1181 * see on the consensus? */
1182 time_t fetch_missing_signatures
;
1183 /** When do we publish the consensus? */
1184 time_t interval_starts
;
1186 /* True iff we have generated and distributed our vote. */
1188 /* True iff we've requested missing votes. */
1189 int have_fetched_missing_votes
;
1190 /* True iff we have built a consensus and sent the signatures around. */
1191 int have_built_consensus
;
1192 /* True iff we've fetched missing signatures. */
1193 int have_fetched_missing_signatures
;
1194 /* True iff we have published our consensus. */
1195 int have_published_consensus
;
1196 } voting_schedule
= {0,0,0,0,0,0,0,0,0,0};
1198 /** Set voting_schedule to hold the timing for the next vote we should be
1201 dirvote_recalculate_timing(or_options_t
*options
, time_t now
)
1203 int interval
, vote_delay
, dist_delay
;
1206 networkstatus_vote_t
*consensus
;
1208 if (!authdir_mode_v3(options
))
1211 consensus
= networkstatus_get_live_consensus(now
);
1213 memset(&voting_schedule
, 0, sizeof(voting_schedule
));
1216 interval
= consensus
->fresh_until
- consensus
->valid_after
;
1217 vote_delay
= consensus
->vote_seconds
;
1218 dist_delay
= consensus
->dist_seconds
;
1220 interval
= DEFAULT_VOTING_INTERVAL_WHEN_NO_CONSENSUS
;
1221 vote_delay
= dist_delay
= 300;
1224 tor_assert(interval
> 0);
1226 if (vote_delay
+ dist_delay
> interval
/2)
1227 vote_delay
= dist_delay
= interval
/ 4;
1229 start
= voting_schedule
.interval_starts
=
1230 dirvote_get_start_of_next_interval(now
,interval
);
1231 end
= dirvote_get_start_of_next_interval(start
+1, interval
);
1233 tor_assert(end
> start
);
1235 voting_schedule
.fetch_missing_signatures
= start
- (dist_delay
/2);
1236 voting_schedule
.voting_ends
= start
- dist_delay
;
1237 voting_schedule
.fetch_missing_votes
= start
- dist_delay
- (vote_delay
/2);
1238 voting_schedule
.voting_starts
= start
- dist_delay
- vote_delay
;
1241 char tbuf
[ISO_TIME_LEN
+1];
1242 format_iso_time(tbuf
, voting_schedule
.interval_starts
);
1243 log_notice(LD_DIR
,"Choosing expected valid-after time as %s: "
1244 "consensus_set=%d, interval=%d",
1245 tbuf
, consensus
?1:0, interval
);
1249 /** Entry point: Take whatever voting actions are pending as of <b>now</b>. */
1251 dirvote_act(or_options_t
*options
, time_t now
)
1253 if (!authdir_mode_v3(options
))
1255 if (!voting_schedule
.voting_starts
) {
1256 char *keys
= list_v3_auth_ids();
1257 authority_cert_t
*c
= get_my_v3_authority_cert();
1258 log_notice(LD_DIR
, "Scheduling voting. Known authority IDs are %s. "
1260 keys
, hex_str(c
->cache_info
.identity_digest
, DIGEST_LEN
));
1262 dirvote_recalculate_timing(options
, now
);
1264 if (voting_schedule
.voting_starts
< now
&& !voting_schedule
.have_voted
) {
1265 log_notice(LD_DIR
, "Time to vote.");
1266 dirvote_perform_vote();
1267 voting_schedule
.have_voted
= 1;
1269 if (voting_schedule
.fetch_missing_votes
< now
&&
1270 !voting_schedule
.have_fetched_missing_votes
) {
1271 log_notice(LD_DIR
, "Time to fetch any votes that we're missing.");
1272 dirvote_fetch_missing_votes();
1273 voting_schedule
.have_fetched_missing_votes
= 1;
1275 if (voting_schedule
.voting_ends
< now
&&
1276 !voting_schedule
.have_built_consensus
) {
1277 log_notice(LD_DIR
, "Time to compute a consensus.");
1278 dirvote_compute_consensus();
1279 /* XXXX We will want to try again later if we haven't got enough
1280 * votes yet. Implement this if it turns out to ever happen. */
1281 voting_schedule
.have_built_consensus
= 1;
1283 if (voting_schedule
.fetch_missing_signatures
< now
&&
1284 !voting_schedule
.have_fetched_missing_signatures
) {
1285 log_notice(LD_DIR
, "Time to fetch any signatures that we're missing.");
1286 dirvote_fetch_missing_signatures();
1287 voting_schedule
.have_fetched_missing_signatures
= 1;
1289 if (voting_schedule
.interval_starts
< now
&&
1290 !voting_schedule
.have_published_consensus
) {
1291 log_notice(LD_DIR
, "Time to publish the consensus and discard old votes");
1292 dirvote_publish_consensus();
1293 dirvote_clear_votes(0);
1294 voting_schedule
.have_published_consensus
= 1;
1295 /* XXXX We will want to try again later if we haven't got enough
1296 * signatures yet. Implement this if it turns out to ever happen. */
1297 dirvote_recalculate_timing(options
, now
);
1301 /** A vote networkstatus_vote_t and its unparsed body: held around so we can
1302 * use it to generate a consensus (at voting_ends) and so we can serve it to
1303 * other authorities that might want it. */
1304 typedef struct pending_vote_t
{
1305 cached_dir_t
*vote_body
;
1306 networkstatus_vote_t
*vote
;
1309 /** List of pending_vote_t for the current vote. Before we've used them to
1310 * build a consensus, the votes go here. */
1311 static smartlist_t
*pending_vote_list
= NULL
;
1312 /** List of pending_vote_t for the previous vote. After we've used them to
1313 * build a consensus, the votes go here for the next period. */
1314 static smartlist_t
*previous_vote_list
= NULL
;
1315 /** The body of the consensus that we're currently building. Once we
1316 * have it built, it goes into dirserv.c */
1317 static char *pending_consensus_body
= NULL
;
1318 /** The detached signatures for the consensus that we're currently
1320 static char *pending_consensus_signatures
= NULL
;
1321 /** The parsed in-progress consensus document. */
1322 static networkstatus_vote_t
*pending_consensus
= NULL
;
1323 /** List of ns_detached_signatures_t: hold signatures that get posted to us
1324 * before we have generated the consensus on our own. */
1325 static smartlist_t
*pending_consensus_signature_list
= NULL
;
1327 /** Generate a networkstatus vote and post it to all the v3 authorities.
1328 * (V3 Authority only) */
1330 dirvote_perform_vote(void)
1332 crypto_pk_env_t
*key
= get_my_v3_authority_signing_key();
1333 authority_cert_t
*cert
= get_my_v3_authority_cert();
1334 networkstatus_vote_t
*ns
;
1336 pending_vote_t
*pending_vote
;
1339 const char *msg
= "";
1341 if (!cert
|| !key
) {
1342 log_warn(LD_NET
, "Didn't find key/certificate to generate v3 vote");
1345 if (!(ns
= dirserv_generate_networkstatus_vote_obj(key
, cert
)))
1348 contents
= format_networkstatus_vote(key
, ns
);
1349 networkstatus_vote_free(ns
);
1353 pending_vote
= dirvote_add_vote(contents
, &msg
, &status
);
1355 if (!pending_vote
) {
1356 log_warn(LD_DIR
, "Couldn't store my own vote! (I told myself, '%s'.)",
1361 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_VOTE
,
1362 ROUTER_PURPOSE_GENERAL
,
1364 pending_vote
->vote_body
->dir
,
1365 pending_vote
->vote_body
->dir_len
, 0);
1366 log_notice(LD_DIR
, "Vote posted.");
1370 /** Send an HTTP request to every other v3 authority, for the votes of every
1371 * authority for which we haven't received a vote yet in this period. (V3
1372 * authority only) */
1374 dirvote_fetch_missing_votes(void)
1376 smartlist_t
*missing_fps
= smartlist_create();
1379 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
1380 trusted_dir_server_t
*, ds
,
1382 if (!(ds
->type
& V3_AUTHORITY
))
1384 if (!dirvote_get_vote(ds
->v3_identity_digest
,
1385 DGV_BY_ID
|DGV_INCLUDE_PENDING
)) {
1386 char *cp
= tor_malloc(HEX_DIGEST_LEN
+1);
1387 base16_encode(cp
, HEX_DIGEST_LEN
+1, ds
->v3_identity_digest
,
1389 smartlist_add(missing_fps
, cp
);
1393 if (!smartlist_len(missing_fps
)) {
1394 smartlist_free(missing_fps
);
1397 log_notice(LOG_NOTICE
, "We're missing votes from %d authorities. Asking "
1398 "every other authority for a copy.", smartlist_len(missing_fps
));
1399 resource
= smartlist_join_strings(missing_fps
, "+", 0, NULL
);
1400 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_STATUS_VOTE
,
1403 SMARTLIST_FOREACH(missing_fps
, char *, cp
, tor_free(cp
));
1404 smartlist_free(missing_fps
);
1407 /** Send a request to every other authority for its detached signatures,
1408 * unless we have signatures from all other v3 authorities already. */
1410 dirvote_fetch_missing_signatures(void)
1412 if (!pending_consensus
)
1414 if (networkstatus_check_consensus_signature(pending_consensus
, -1) == 1)
1415 return; /* we have a signature from everybody. */
1417 directory_get_from_all_authorities(DIR_PURPOSE_FETCH_DETACHED_SIGNATURES
,
1421 /** Drop all currently pending votes, consensus, and detached signatures. */
1423 dirvote_clear_votes(int all_votes
)
1425 if (!previous_vote_list
)
1426 previous_vote_list
= smartlist_create();
1427 if (!pending_vote_list
)
1428 pending_vote_list
= smartlist_create();
1430 /* All "previous" votes are now junk. */
1431 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, v
, {
1432 cached_dir_decref(v
->vote_body
);
1433 v
->vote_body
= NULL
;
1434 networkstatus_vote_free(v
->vote
);
1437 smartlist_clear(previous_vote_list
);
1440 /* If we're dumping all the votes, we delete the pending ones. */
1441 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
, {
1442 cached_dir_decref(v
->vote_body
);
1443 v
->vote_body
= NULL
;
1444 networkstatus_vote_free(v
->vote
);
1448 /* Otherwise, we move them into "previous". */
1449 smartlist_add_all(previous_vote_list
, pending_vote_list
);
1451 smartlist_clear(pending_vote_list
);
1453 if (pending_consensus_signature_list
) {
1454 SMARTLIST_FOREACH(pending_consensus_signature_list
, char *, cp
,
1456 smartlist_clear(pending_consensus_signature_list
);
1458 tor_free(pending_consensus_body
);
1459 tor_free(pending_consensus_signatures
);
1460 if (pending_consensus
) {
1461 networkstatus_vote_free(pending_consensus
);
1462 pending_consensus
= NULL
;
1466 /** Return a newly allocated string containing the hex-encoded v3 authority
1467 identity digest of every recognized v3 authority. */
1469 list_v3_auth_ids(void)
1471 smartlist_t
*known_v3_keys
= smartlist_create();
1473 SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
1474 trusted_dir_server_t
*, ds
,
1475 if ((ds
->type
& V3_AUTHORITY
) &&
1476 !tor_digest_is_zero(ds
->v3_identity_digest
))
1477 smartlist_add(known_v3_keys
,
1478 tor_strdup(hex_str(ds
->v3_identity_digest
, DIGEST_LEN
))));
1479 keys
= smartlist_join_strings(known_v3_keys
, ", ", 0, NULL
);
1480 SMARTLIST_FOREACH(known_v3_keys
, char *, cp
, tor_free(cp
));
1481 smartlist_free(known_v3_keys
);
1485 /** Called when we have received a networkstatus vote in <b>vote_body</b>.
1486 * Parse and validate it, and on success store it as a pending vote (which we
1487 * then return). Return NULL on failure. Sets *<b>msg_out</b> and
1488 * *<b>status_out</b> to an HTTP response and status code. (V3 authority
1491 dirvote_add_vote(const char *vote_body
, const char **msg_out
, int *status_out
)
1493 networkstatus_vote_t
*vote
;
1494 networkstatus_voter_info_t
*vi
;
1495 trusted_dir_server_t
*ds
;
1496 pending_vote_t
*pending_vote
= NULL
;
1497 const char *end_of_vote
= NULL
;
1499 tor_assert(vote_body
);
1500 tor_assert(msg_out
);
1501 tor_assert(status_out
);
1503 if (!pending_vote_list
)
1504 pending_vote_list
= smartlist_create();
1509 vote
= networkstatus_parse_vote_from_string(vote_body
, &end_of_vote
, 1);
1511 end_of_vote
= vote_body
+ strlen(vote_body
);
1513 log_warn(LD_DIR
, "Couldn't parse vote: length was %d",
1514 (int)strlen(vote_body
));
1515 *msg_out
= "Unable to parse vote";
1518 tor_assert(smartlist_len(vote
->voters
) == 1);
1519 vi
= get_voter(vote
);
1520 tor_assert(vi
->good_signature
== 1);
1521 ds
= trusteddirserver_get_by_v3_auth_digest(vi
->identity_digest
);
1522 if (!ds
|| !(ds
->type
& V3_AUTHORITY
)) {
1523 char *keys
= list_v3_auth_ids();
1524 log_warn(LD_DIR
, "Got a vote from an authority with authority key ID %s. "
1525 "This authority %s. Known v3 key IDs are: %s",
1526 hex_str(vi
->identity_digest
, DIGEST_LEN
),
1527 ds
?"is not recognized":"is recognized, but is not listed as v3",
1531 *msg_out
= "Vote not from a recognized v3 authority";
1534 tor_assert(vote
->cert
);
1535 if (!authority_cert_get_by_digests(vote
->cert
->cache_info
.identity_digest
,
1536 vote
->cert
->signing_key_digest
)) {
1537 /* Hey, it's a new cert! */
1538 trusted_dirs_load_certs_from_string(
1539 vote
->cert
->cache_info
.signed_descriptor_body
,
1540 0 /* from_store */);
1541 if (!authority_cert_get_by_digests(vote
->cert
->cache_info
.identity_digest
,
1542 vote
->cert
->signing_key_digest
)) {
1543 log_warn(LD_BUG
, "We added a cert, but still couldn't find it.");
1547 /* Is it for the right period? */
1548 if (vote
->valid_after
!= voting_schedule
.interval_starts
) {
1549 char tbuf1
[ISO_TIME_LEN
+1], tbuf2
[ISO_TIME_LEN
+1];
1550 format_iso_time(tbuf1
, vote
->valid_after
);
1551 format_iso_time(tbuf2
, voting_schedule
.interval_starts
);
1552 log_warn(LD_DIR
, "Rejecting vote with valid-after time of %s; we were "
1553 "expecting %s", tbuf1
, tbuf2
);
1554 *msg_out
= "Bad valid-after time";
1558 /* Now see whether we already h<ave a vote from this authority.*/
1559 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
, {
1560 if (! memcmp(v
->vote
->cert
->cache_info
.identity_digest
,
1561 vote
->cert
->cache_info
.identity_digest
,
1563 networkstatus_voter_info_t
*vi_old
= get_voter(v
->vote
);
1564 if (!memcmp(vi_old
->vote_digest
, vi
->vote_digest
, DIGEST_LEN
)) {
1565 /* Ah, it's the same vote. Not a problem. */
1566 log_info(LD_DIR
, "Discarding a vote we already have.");
1567 if (*status_out
< 200)
1570 } else if (v
->vote
->published
< vote
->published
) {
1571 log_notice(LD_DIR
, "Replacing an older pending vote from this "
1573 cached_dir_decref(v
->vote_body
);
1574 networkstatus_vote_free(v
->vote
);
1575 v
->vote_body
= new_cached_dir(tor_strndup(vote_body
,
1576 end_of_vote
-vote_body
),
1580 !strcmpstart(end_of_vote
, "network-status-version"))
1583 if (*status_out
< 200)
1589 *msg_out
= "Already have a newer pending vote";
1595 pending_vote
= tor_malloc_zero(sizeof(pending_vote_t
));
1596 pending_vote
->vote_body
= new_cached_dir(tor_strndup(vote_body
,
1597 end_of_vote
-vote_body
),
1599 pending_vote
->vote
= vote
;
1600 smartlist_add(pending_vote_list
, pending_vote
);
1602 if (!strcmpstart(end_of_vote
, "network-status-version ")) {
1603 vote_body
= end_of_vote
;
1612 *msg_out
= "Error adding vote";
1613 if (*status_out
< 400)
1618 networkstatus_vote_free(vote
);
1620 if (end_of_vote
&& !strcmpstart(end_of_vote
, "network-status-version ")) {
1621 vote_body
= end_of_vote
;
1627 if (*status_out
< 200)
1630 if (!any_failed
&& !pending_vote
) {
1631 *msg_out
= "Duplicate discarded";
1637 return any_failed
? NULL
: pending_vote
;
1640 /** Try to compute a v3 networkstatus consensus from the currently pending
1641 * votes. Return 0 on success, -1 on failure. Store the consensus in
1642 * pending_consensus: it won't be ready to be published until we have
1643 * everybody else's signatures collected too. (V3 Authoritity only) */
1645 dirvote_compute_consensus(void)
1647 /* Have we got enough votes to try? */
1648 int n_votes
, n_voters
;
1649 smartlist_t
*votes
= NULL
;
1650 char *consensus_body
= NULL
, *signatures
= NULL
;
1651 networkstatus_vote_t
*consensus
= NULL
;
1652 authority_cert_t
*my_cert
;
1654 if (!pending_vote_list
)
1655 pending_vote_list
= smartlist_create();
1657 n_voters
= get_n_authorities(V3_AUTHORITY
);
1658 n_votes
= smartlist_len(pending_vote_list
);
1659 if (n_votes
<= n_voters
/2) {
1660 log_warn(LD_DIR
, "We don't have enough votes to generate a consensus.");
1664 if (!(my_cert
= get_my_v3_authority_cert())) {
1665 log_warn(LD_DIR
, "Can't generate consensus without a certificate.");
1669 votes
= smartlist_create();
1670 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, v
,
1671 smartlist_add(votes
, v
->vote
));
1673 consensus_body
= networkstatus_compute_consensus(
1675 my_cert
->identity_key
,
1676 get_my_v3_authority_signing_key());
1677 if (!consensus_body
) {
1678 log_warn(LD_DIR
, "Couldn't generate a consensus at all!");
1681 consensus
= networkstatus_parse_vote_from_string(consensus_body
, NULL
, 0);
1683 log_warn(LD_DIR
, "Couldn't parse consensus we generated!");
1686 /* 'Check' our own signature, to mark it valid. */
1687 networkstatus_check_consensus_signature(consensus
, -1);
1689 signatures
= networkstatus_get_detached_signatures(consensus
);
1691 log_warn(LD_DIR
, "Couldn't extract signatures.");
1695 tor_free(pending_consensus_body
);
1696 pending_consensus_body
= consensus_body
;
1697 tor_free(pending_consensus_signatures
);
1698 pending_consensus_signatures
= signatures
;
1700 if (pending_consensus
)
1701 networkstatus_vote_free(pending_consensus
);
1702 pending_consensus
= consensus
;
1704 if (pending_consensus_signature_list
) {
1706 /* we may have gotten signatures for this consensus before we built
1707 * it ourself. Add them now. */
1708 SMARTLIST_FOREACH(pending_consensus_signature_list
, char *, sig
,
1710 const char *msg
= NULL
;
1711 n_sigs
+= dirvote_add_signatures_to_pending_consensus(sig
, &msg
);
1715 log_notice(LD_DIR
, "Added %d pending signatures while building "
1716 "consensus.", n_sigs
);
1717 smartlist_clear(pending_consensus_signature_list
);
1720 log_notice(LD_DIR
, "Consensus computed; uploading signature(s)");
1722 directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_SIGNATURES
,
1723 ROUTER_PURPOSE_GENERAL
,
1725 pending_consensus_signatures
,
1726 strlen(pending_consensus_signatures
), 0);
1727 log_notice(LD_DIR
, "Signature(s) posted.");
1732 smartlist_free(votes
);
1733 tor_free(consensus_body
);
1734 tor_free(signatures
);
1735 networkstatus_vote_free(consensus
);
1740 /** Helper: we just got the <b>deteached_signatures_body</b> sent to us as
1741 * signatures on the currently pending consensus. Add them to the consensus
1742 * as appropriate. Return the number of signatures added. (?) */
1744 dirvote_add_signatures_to_pending_consensus(
1745 const char *detached_signatures_body
,
1746 const char **msg_out
)
1748 ns_detached_signatures_t
*sigs
= NULL
;
1751 tor_assert(detached_signatures_body
);
1752 tor_assert(msg_out
);
1754 /* Only call if we have a pending consensus right now. */
1755 tor_assert(pending_consensus
);
1756 tor_assert(pending_consensus_body
);
1757 tor_assert(pending_consensus_signatures
);
1761 if (!(sigs
= networkstatus_parse_detached_signatures(
1762 detached_signatures_body
, NULL
))) {
1763 *msg_out
= "Couldn't parse detached signatures.";
1767 r
= networkstatus_add_detached_signatures(pending_consensus
,
1771 char *new_detached
=
1772 networkstatus_get_detached_signatures(pending_consensus
);
1775 size_t new_consensus_len
=
1776 strlen(pending_consensus_body
) + strlen(new_detached
) + 1;
1777 pending_consensus_body
= tor_realloc(pending_consensus_body
,
1779 dst
= strstr(pending_consensus_body
, "directory-signature ");
1781 src
= strstr(new_detached
, "directory-signature ");
1783 strlcpy(dst
, src
, new_consensus_len
- (dst
-pending_consensus_body
));
1785 /* We remove this block once it has failed to crash for a while. But
1786 * unless it shows up in profiles, we're probably better leaving it in,
1787 * just in case we break detached signature processing at some point. */
1789 ns_detached_signatures_t
*sigs
=
1790 networkstatus_parse_detached_signatures(new_detached
, NULL
);
1791 networkstatus_vote_t
*v
= networkstatus_parse_vote_from_string(
1792 pending_consensus_body
, NULL
, 0);
1794 ns_detached_signatures_free(sigs
);
1796 networkstatus_vote_free(v
);
1798 tor_free(pending_consensus_signatures
);
1799 pending_consensus_signatures
= new_detached
;
1800 *msg_out
= "Signatures added";
1808 *msg_out
= "Unrecognized error while adding detached signatures.";
1811 ns_detached_signatures_free(sigs
);
1815 /** Helper: we just got the <b>deteached_signatures_body</b> sent to us as
1816 * signatures on the currently pending consensus. Add them to the pending
1817 * consensus (if we have one); otherwise queue them until we have a
1818 * consensus. Return negative on failure, nonnegative on success. */
1820 dirvote_add_signatures(const char *detached_signatures_body
,
1823 if (pending_consensus
) {
1824 log_notice(LD_DIR
, "Got a signature. Adding it to the pending consensus.");
1825 return dirvote_add_signatures_to_pending_consensus(
1826 detached_signatures_body
, msg
);
1828 log_notice(LD_DIR
, "Got a signature. Queueing it for the next consensus.");
1829 if (!pending_consensus_signature_list
)
1830 pending_consensus_signature_list
= smartlist_create();
1831 smartlist_add(pending_consensus_signature_list
,
1832 tor_strdup(detached_signatures_body
));
1833 *msg
= "Signature queued";
1838 /** Replace the consensus that we're currently serving with the one that we've
1839 * been building. (V3 Authority only) */
1841 dirvote_publish_consensus(void)
1843 /* Can we actually publish it yet? */
1844 if (!pending_consensus
||
1845 networkstatus_check_consensus_signature(pending_consensus
, 1)<0) {
1846 log_warn(LD_DIR
, "Not enough info to publish pending consensus");
1850 if (networkstatus_set_current_consensus(pending_consensus_body
, 0, 0))
1851 log_warn(LD_DIR
, "Error publishing consensus");
1853 log_notice(LD_DIR
, "Consensus published.");
1858 /** Release all static storage held in dirvote.c */
1860 dirvote_free_all(void)
1862 dirvote_clear_votes(1);
1863 /* now empty as a result of clear_pending_votes. */
1864 smartlist_free(pending_vote_list
);
1865 pending_vote_list
= NULL
;
1866 smartlist_free(previous_vote_list
);
1867 previous_vote_list
= NULL
;
1869 tor_free(pending_consensus_body
);
1870 tor_free(pending_consensus_signatures
);
1871 if (pending_consensus
) {
1872 networkstatus_vote_free(pending_consensus
);
1873 pending_consensus
= NULL
;
1875 if (pending_consensus_signature_list
) {
1876 /* now empty as a result of clear_pending_votes. */
1877 smartlist_free(pending_consensus_signature_list
);
1878 pending_consensus_signature_list
= NULL
;
1883 * Access to pending items.
1886 /** Return the body of the consensus that we're currently trying to build. */
1888 dirvote_get_pending_consensus(void)
1890 return pending_consensus_body
;
1893 /** Return the signatures that we know for the consensus that we're currently
1894 * trying to build */
1896 dirvote_get_pending_detached_signatures(void)
1898 return pending_consensus_signatures
;
1901 /** Return a given vote specified by <b>fp</b>. If <b>by_id</b>, return the
1902 * vote for the authority with the v3 authority identity key digest <b>fp</b>;
1903 * if <b>by_id</b> is false, return the vote whose digest is <b>fp</b>. If
1904 * <b>fp</b> is NULL, return our own vote. If <b>include_previous</b> is
1905 * false, do not consider any votes for a consensus that's already been built.
1906 * If <b>include_pending</b> is false, do not consider any votes for the
1907 * consensus that's in progress. May return NULL if we have no vote for the
1908 * authority in question. */
1909 const cached_dir_t
*
1910 dirvote_get_vote(const char *fp
, int flags
)
1912 int by_id
= flags
& DGV_BY_ID
;
1913 const int include_pending
= flags
& DGV_INCLUDE_PENDING
;
1914 const int include_previous
= flags
& DGV_INCLUDE_PREVIOUS
;
1916 if (!pending_vote_list
&& !previous_vote_list
)
1919 authority_cert_t
*c
= get_my_v3_authority_cert();
1921 fp
= c
->cache_info
.identity_digest
;
1927 if (pending_vote_list
&& include_pending
) {
1928 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, pv
,
1929 if (!memcmp(get_voter(pv
->vote
)->identity_digest
, fp
, DIGEST_LEN
))
1930 return pv
->vote_body
);
1932 if (previous_vote_list
&& include_previous
) {
1933 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, pv
,
1934 if (!memcmp(get_voter(pv
->vote
)->identity_digest
, fp
, DIGEST_LEN
))
1935 return pv
->vote_body
);
1938 if (pending_vote_list
&& include_pending
) {
1939 SMARTLIST_FOREACH(pending_vote_list
, pending_vote_t
*, pv
,
1940 if (!memcmp(pv
->vote
->networkstatus_digest
, fp
, DIGEST_LEN
))
1941 return pv
->vote_body
);
1943 if (previous_vote_list
&& include_previous
) {
1944 SMARTLIST_FOREACH(previous_vote_list
, pending_vote_t
*, pv
,
1945 if (!memcmp(pv
->vote
->networkstatus_digest
, fp
, DIGEST_LEN
))
1946 return pv
->vote_body
);