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

            
6
use super::*;
7

            
8
// Import `each_variety.rs`, appropriately variegated
9
ns_do_variety_vote! {}
10

            
11
/// Used for reporting errors when parsing this document type
12
const 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.
24
pub type VarietyKeyword = NoMoreArguments;
25

            
26
impl 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
12
    pub fn verify(
33
12
        self,
34
12
        trusted: &mut VoteVerifier,
35
12
    ) -> Result<TimeRangeBound<NetworkStatus>, VoteVerifyFailed> {
36
        use VoteVerifyFailed as VVF;
37

            
38
12
        let (mut body, sigs) = self.unwrap_unverified();
39

            
40
        let authcert = {
41
12
            let input = parse2::ParseInput::new(
42
12
                body.authority.cert.raw_unverified().as_ref(),
43
12
                "<authcert>",
44
12
            );
45
12
            let authcert = parse2::parse_netdoc::<AuthCertUnverified>(&input)
46
12
                .map_err(VVF::AuthCertParseError)?;
47
11
            let authcert = authcert
48
11
                .verify(&trusted.trusted)
49
11
                .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
27
            let test_validity_at = |t| {
54
27
                authcert
55
27
                    .check_valid_at(&t)
56
27
                    .map_err(VVF::AuthCertWrongValidity)
57
27
            };
58

            
59
            // test at all relevant times, in a uniform way so we can break out check
60
10
            test_validity_at(*body.preamble.lifetime.valid_after)?;
61
9
            test_validity_at(*body.preamble.lifetime.fresh_until)?;
62
8
            test_validity_at(*body.preamble.lifetime.valid_until)?;
63
7
            authcert.dangerously_assume_timely() // we just checked it ^ there
64
        };
65

            
66
7
        let authority_id = body.authority.authority.dir_source.identity;
67
7
        if authority_id != authcert.fingerprint {
68
1
            return Err(VVF::AuthCertWrongAuthority);
69
6
        }
70

            
71
6
        SignatureGroup {
72
6
            hashes: sigs.hashes,
73
6
            signatures: vec![sigs.sigs.directory_signature],
74
6
        }
75
        .verify_general(
76
6
            VerifyGeneralTrustedAuthorities::AnyOneOfThese {
77
6
                trusted: &trusted.trusted,
78
6
            },
79
6
            slice::from_ref(&authcert),
80
6
            |tv| tv.verify().map_err(VVF::InvalidSignature),
81
1
        )?;
82

            
83
5
        if !trusted.remaining_good.remove(&*authority_id) {
84
1
            return Err(VVF::DuplicateAuthority(*authority_id));
85
4
        }
86

            
87
4
        body.authority.cert.set_verified(authcert);
88

            
89
4
        let time_range = body.preamble.validity_time_range();
90
4
        Ok(TimeRangeBound::new(body, time_range))
91
12
    }
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
7
    pub fn peek_alleged_authority(&self) -> RsaIdentity {
99
7
        *self
100
7
            .inspect_unverified()
101
7
            .0
102
7
            .authority
103
7
            .authority
104
7
            .dir_source
105
7
            .identity
106
7
    }
107
}
108

            
109
impl 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
}