Skip to main content

tor_netdoc/doc/netstatus/
vote.rs

1//! Implementation for plain consensus documents.
2//
3// Read this file in conjunction with `each_variety.rs`.
4// See "module scope" ns_variety_definition_macros.rs.
5
6use super::*;
7
8// Import `each_variety.rs`, appropriately variegated
9ns_do_variety_vote! {}
10
11/// Used for reporting errors when parsing this document type
12const NETSTATUS_DOCTYPE_FOR_ERROR: &str = "network status vote";
13
14/// The forbidden flavor keyword in a vote consensus heading line
15///
16/// This type is one of the fields in `NetworkStatusVersionItem`.
17///
18/// Votes start with `network-status-version 3`
19/// and aren't allowed to have a variety.
20///
21/// So in *this* variety, we insist that there are no more arguments.
22///
23/// See also torspec#359.
24pub type VarietyKeyword = NoMoreArguments;
25
26impl NetworkStatusUnverified {
27    /// Verify the signatures
28    ///
29    /// Doesn't check the validity period:
30    /// the document is wrapped in [`TimeRangeBound`],
31    /// ensuring that the caller does that check.
32    pub fn verify(
33        self,
34        trusted: &mut VoteVerifier,
35    ) -> Result<TimeRangeBound<NetworkStatus>, VoteVerifyFailed> {
36        use VoteVerifyFailed as VVF;
37
38        let (mut body, sigs) = self.unwrap_unverified();
39
40        let authcert = {
41            let input = parse2::ParseInput::new(
42                body.authority.cert.raw_unverified().as_ref(),
43                "<authcert>",
44            );
45            let authcert = parse2::parse_netdoc::<AuthCertUnverified>(&input)
46                .map_err(VVF::AuthCertParseError)?;
47            let authcert = authcert
48                .verify(&trusted.trusted)
49                .map_err(VVF::InvalidSignature)?;
50
51            // We do the authcert validity time check here, with reference to
52            // the vote's declared validity period, not the current time or whatever.
53            let test_validity_at = |t| {
54                authcert
55                    .check_valid_at(&t)
56                    .map_err(VVF::AuthCertWrongValidity)
57            };
58
59            // test at all relevant times, in a uniform way so we can break out check
60            test_validity_at(*body.preamble.lifetime.valid_after)?;
61            test_validity_at(*body.preamble.lifetime.fresh_until)?;
62            test_validity_at(*body.preamble.lifetime.valid_until)?;
63            authcert.dangerously_assume_timely() // we just checked it ^ there
64        };
65
66        let authority_id = body.authority.authority.dir_source.identity;
67        if authority_id != authcert.fingerprint {
68            return Err(VVF::AuthCertWrongAuthority);
69        }
70
71        SignatureGroup {
72            hashes: sigs.hashes,
73            signatures: vec![sigs.sigs.directory_signature],
74        }
75        .verify_general(
76            VerifyGeneralTrustedAuthorities::AnyOneOfThese {
77                trusted: &trusted.trusted,
78            },
79            slice::from_ref(&authcert),
80            |tv| tv.verify().map_err(VVF::InvalidSignature),
81        )?;
82
83        if !trusted.remaining_good.remove(&*authority_id) {
84            return Err(VVF::DuplicateAuthority(*authority_id));
85        }
86
87        body.authority.cert.set_verified(authcert);
88
89        let time_range = body.preamble.validity_time_range();
90        Ok(TimeRangeBound::new(body, time_range))
91    }
92
93    /// Look at the declared directory authority identity KHP_auth_id_rsa
94    ///
95    /// This tells you what the vote says the issuing authority is,
96    /// but note that the signatures haven't been checked,
97    /// so this information should be used with care.
98    pub fn peek_alleged_authority(&self) -> RsaIdentity {
99        *self
100            .inspect_unverified()
101            .0
102            .authority
103            .authority
104            .dir_source
105            .identity
106    }
107}
108
109impl From<ConsensusVerifiabilityError> for VoteVerifyFailed {
110    fn from(cve: ConsensusVerifiabilityError) -> VoteVerifyFailed {
111        use ConsensusVerifiabilityError as CVE;
112        use VerifyFailed as VF;
113        use VoteVerifyFailed as VVF;
114
115        match cve {
116            CVE::InsufficientTrustedSigners => {
117                VVF::InvalidSignature(VF::InsufficientTrustedSigners)
118            }
119            CVE::MissingAuthCerts { .. } => {
120                // this should be impossible, because we checked the authcert was right
121                VVF::AuthCertWrongAuthority
122            }
123        }
124    }
125}