1
//! Types to describe information about other downloaded directory
2
//! documents, without necessarily having the full document.
3
//!
4
//! These types are all local within tor-dirmgr.  They're used so that
5
//! the storage code doesn't need to know about all of the parsed
6
//! types from tor-netdoc.
7

            
8
use digest::Digest;
9
use tor_llcrypto as ll;
10
use tor_netdoc::doc::{
11
    authcert::{AuthCert, AuthCertKeyIds},
12
    netstatus::{Lifetime, MdConsensus},
13
};
14

            
15
use std::time::SystemTime;
16

            
17
/// Information about a consensus that we have in storage.
18
///
19
/// This information is ordinarily derived from the consensus, but doesn't
20
/// have to be.
21
#[derive(Debug, Clone)]
22
pub(crate) struct ConsensusMeta {
23
    /// The time over which the consensus is valid.
24
    lifetime: Lifetime,
25
    /// A sha3-256 digest of the signed portion of the consensus: used for
26
    /// fetching diffs.
27
    sha3_256_of_signed: [u8; 32],
28
    /// A sha3-256 digest of the entirety of the consensus: used for
29
    /// naming the file.
30
    sha3_256_of_whole: [u8; 32],
31
}
32

            
33
impl ConsensusMeta {
34
    /// Create a new ConsensusMeta
35
20
    pub(crate) fn new(
36
20
        lifetime: Lifetime,
37
20
        sha3_256_of_signed: [u8; 32],
38
20
        sha3_256_of_whole: [u8; 32],
39
20
    ) -> Self {
40
20
        ConsensusMeta {
41
20
            lifetime,
42
20
            sha3_256_of_signed,
43
20
            sha3_256_of_whole,
44
20
        }
45
20
    }
46

            
47
    /// Derive a new ConsensusMeta from a [`Lifetime`] and the
48
    /// text of its signed  and unsigned portions
49
7
    pub(crate) fn from_text_and_lifetime(
50
7
        signed_part: &str,
51
7
        remainder: &str,
52
7
        lifetime: Lifetime,
53
7
    ) -> Self {
54
7
        let (sd, wd) = sha3_dual(signed_part, remainder);
55
7
        ConsensusMeta::new(lifetime, sd, wd)
56
7
    }
57

            
58
    /// Derive a new ConsensusMeta from a MdConsensus and the text of its
59
    /// signed portion.
60
    #[allow(unused)]
61
    pub(crate) fn from_consensus(signed_part: &str, remainder: &str, con: &MdConsensus) -> Self {
62
        let lifetime = con.lifetime().clone();
63
        let (sd, wd) = sha3_dual(signed_part, remainder);
64
        ConsensusMeta::new(lifetime, sd, wd)
65
    }
66
    /// Return the lifetime of this ConsensusMeta
67
19
    pub(crate) fn lifetime(&self) -> &Lifetime {
68
19
        &self.lifetime
69
19
    }
70
    /// Return the sha3-256 of the signed portion of this consensus.
71
11
    pub(crate) fn sha3_256_of_signed(&self) -> &[u8; 32] {
72
11
        &self.sha3_256_of_signed
73
11
    }
74
    /// Return the sha3-256 of the entirety of this consensus.
75
7
    pub(crate) fn sha3_256_of_whole(&self) -> &[u8; 32] {
76
7
        &self.sha3_256_of_whole
77
7
    }
78
}
79

            
80
/// Compute the sha3-256 digests of signed_part on its own, and of
81
/// signed_part concatenated with remainder.
82
356
fn sha3_dual(signed_part: impl AsRef<[u8]>, remainder: impl AsRef<[u8]>) -> ([u8; 32], [u8; 32]) {
83
356
    let mut d = ll::d::Sha3_256::new();
84
356
    d.update(signed_part.as_ref());
85
356
    let sha3_of_signed = d.clone().finalize().into();
86
356
    d.update(remainder.as_ref());
87
356
    let sha3_of_whole = d.finalize().into();
88
356
    (sha3_of_signed, sha3_of_whole)
89
356
}
90

            
91
/// Information about an authority certificate that we have in storage.
92
///
93
/// This information is ordinarily derived from the authority cert, but it
94
/// doesn't have to be.
95
#[derive(Clone, Debug)]
96
pub(crate) struct AuthCertMeta {
97
    /// Key IDs (identity and signing) for the certificate.
98
    ids: AuthCertKeyIds,
99
    /// Time of publication.
100
    published: SystemTime,
101
    /// Expiration time.
102
    expires: SystemTime,
103
}
104

            
105
impl AuthCertMeta {
106
    /// Construct a new AuthCertMeta from its components
107
4
    pub(crate) fn new(ids: AuthCertKeyIds, published: SystemTime, expires: SystemTime) -> Self {
108
4
        AuthCertMeta {
109
4
            ids,
110
4
            published,
111
4
            expires,
112
4
        }
113
4
    }
114

            
115
    /// Construct a new AuthCertMeta from a certificate.
116
1
    pub(crate) fn from_authcert(cert: &AuthCert) -> Self {
117
1
        AuthCertMeta::new(cert.key_ids(), cert.published(), cert.expires())
118
1
    }
119

            
120
    /// Return the key IDs for this certificate
121
4
    pub(crate) fn key_ids(&self) -> &AuthCertKeyIds {
122
4
        &self.ids
123
4
    }
124
    /// Return the published time for this certificate
125
4
    pub(crate) fn published(&self) -> SystemTime {
126
4
        self.published
127
4
    }
128
    /// Return the expiration time for this certificate
129
4
    pub(crate) fn expires(&self) -> SystemTime {
130
4
        self.expires
131
4
    }
132
}
133

            
134
#[cfg(test)]
135
mod test {
136
    // @@ begin test lint list maintained by maint/add_warning @@
137
    #![allow(clippy::bool_assert_comparison)]
138
    #![allow(clippy::clone_on_copy)]
139
    #![allow(clippy::dbg_macro)]
140
    #![allow(clippy::mixed_attributes_style)]
141
    #![allow(clippy::print_stderr)]
142
    #![allow(clippy::print_stdout)]
143
    #![allow(clippy::single_char_pattern)]
144
    #![allow(clippy::unwrap_used)]
145
    #![allow(clippy::unchecked_time_subtraction)]
146
    #![allow(clippy::useless_vec)]
147
    #![allow(clippy::needless_pass_by_value)]
148
    #![allow(clippy::string_slice)] // See arti#2571
149
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
150
    use super::*;
151

            
152
    #[test]
153
    fn t_sha3_dual() {
154
        let s = b"Loarax ipsum gruvvulus thneed amet, snergelly once-ler lerkim, sed do barbaloot tempor gluppitus ut labore et truffula magna aliqua. Ut enim ad grickle-grass veniam, quis miff-muffered ga-zumpco laboris nisi ut cruffulus ex ea schloppity consequat. Duis aute snarggle in swomeeswans in voluptate axe-hacker esse rippulus crummii eu moof nulla snuvv.";
155

            
156
        let sha3_of_whole: [u8; 32] = ll::d::Sha3_256::digest(s).into();
157

            
158
        for idx in 0..s.len() {
159
            let sha3_of_part: [u8; 32] = ll::d::Sha3_256::digest(&s[..idx]).into();
160
            let (a, b) = sha3_dual(&s[..idx], &s[idx..]);
161
            assert_eq!(a, sha3_of_part);
162
            assert_eq!(b, sha3_of_whole);
163
        }
164
    }
165
}