Skip to main content

tor_dircommon/
authority.rs

1//! Information about directory authorities
2//!
3//! From a client's point of view, an authority's role is to sign the
4//! consensus directory.
5
6use std::net::SocketAddr;
7
8use derive_deftly::Deftly;
9use getset::Getters;
10use tor_config::define_list_builder_accessors;
11use tor_config::derive::prelude::*;
12use tor_llcrypto::pk::rsa::RsaIdentity;
13
14/// The contact information for all directory authorities this implementation is
15/// aware of.
16///
17/// This data structure makes use of proposal 330 in order to distinguish
18/// authorities by their responsibilities, hence why the fields are divided.
19#[derive(Debug, Clone, Deftly, Eq, PartialEq, Getters)]
20#[derive_deftly(TorConfig)]
21pub struct AuthorityContacts {
22    // NOTE: We aren't using list builders here, since these never actually used sub-builders,
23    // and therefore has a slightly different API.
24    // We might later decide to change these to proper list-builders;
25    // if so, it will be an API break.
26    //
27    /// The [`RsaIdentity`] keys that may be used to sign valid consensus documents.
28    #[deftly(tor_config(no_magic, setter(skip), default = "default_v3idents()"))]
29    #[getset(get = "pub")]
30    v3idents: Vec<RsaIdentity>,
31
32    /// The endpoints of authorities where upload of router descriptors and other
33    /// documents is possible.
34    ///
35    /// This section is primarily of interest for relays.
36    ///
37    /// The use of nested a [`Vec`] serves the purpose to assign multiple IPs to
38    /// a single logical authority, such as having an IPv4 and IPv6 address.
39    #[deftly(tor_config(no_magic, setter(skip), default = "default_uploads()"))]
40    #[getset(get = "pub")]
41    uploads: Vec<Vec<SocketAddr>>,
42
43    /// The endpoints of authorities where download of network documents is possible.
44    ///
45    /// This section is primarily of interest for directory mirrors.
46    ///
47    /// The use of nested a [`Vec`] serves the purpose to assign multiple IPs to
48    /// a single logical authority, such as having an IPv4 and IPv6 address.
49    #[deftly(tor_config(no_magic, setter(skip), default = "default_downloads()"))]
50    #[getset(get = "pub")]
51    downloads: Vec<Vec<SocketAddr>>,
52
53    /// The endpoints of authorities where voting for consensus documents is possible.
54    ///
55    /// This section is primarily of interest for other directory authorities.
56    ///
57    /// The use of nested a [`Vec`] serves the purpose to assign multiple IPs to
58    /// a single logical authority, such as having an IPv4 and IPv6 address.
59    #[deftly(tor_config(no_magic, setter(skip), default = "default_votes()"))]
60    #[getset(get = "pub")]
61    votes: Vec<Vec<SocketAddr>>,
62}
63
64define_list_builder_accessors! {
65    struct AuthorityContactsBuilder {
66        pub v3idents: [RsaIdentity],
67        pub uploads: [Vec<SocketAddr>],
68        pub downloads: [Vec<SocketAddr>],
69        pub votes: [Vec<SocketAddr>],
70    }
71}
72
73impl AuthorityContacts {
74    /// Do any identity or IP address entries intersect with `other`?
75    ///
76    /// This compares the flattened lists of entries field-by-field.
77    ///
78    /// This is intended to be used to compare an `AuthorityContacts` as
79    /// configured by the user with the default `AuthorityContacts`,
80    /// to detect if the configured authority contacts use any of
81    /// the authorities from the "live" Tor network.
82    pub fn intersects(&self, other: &Self) -> bool {
83        /// Check for an intersection between two lists of lists of `SocketAddr`s,
84        /// but flatten them once first.
85        fn flatten_addrs_intersect<'a, I>(a: I, b: I) -> bool
86        where
87            I: IntoIterator,
88            I::Item: IntoIterator<Item = &'a SocketAddr>,
89        {
90            let mut a = a.into_iter().flatten();
91            let mut b = b.into_iter().flatten();
92            a.any(|x| b.any(|y| x == y))
93        }
94
95        // Destructure so that we ensure we consider each field.
96        #[deny(unused)]
97        let Self {
98            v3idents,
99            uploads,
100            downloads,
101            votes,
102        } = self;
103
104        if v3idents.iter().any(|id| other.v3idents.contains(id)) {
105            return true;
106        }
107
108        if flatten_addrs_intersect(uploads, &other.uploads) {
109            return true;
110        }
111
112        if flatten_addrs_intersect(downloads, &other.downloads) {
113            return true;
114        }
115
116        if flatten_addrs_intersect(votes, &other.votes) {
117            return true;
118        }
119
120        false
121    }
122}
123
124/// Returns a list of the default [`RsaIdentity`]s for the directory authorities.
125fn default_v3idents() -> Vec<RsaIdentity> {
126    fn rsa(hex: &str) -> RsaIdentity {
127        RsaIdentity::from_hex(hex).expect("invalid hex?!?")
128    }
129
130    vec![
131        rsa("0232AF901C31A04EE9848595AF9BB7620D4C5B2E"), // dannenberg
132        rsa("E8A9C45EDE6D711294FADF8E7951F4DE6CA56B58"), // dizum
133        rsa("70849B868D606BAECFB6128C5E3D782029AA394F"), // faravahar
134        rsa("ED03BB616EB2F60BEC80151114BB25CEF515B226"), // gabelmoo
135        rsa("23D15D965BC35114467363C165C4F724B64B4F66"), // longclaw
136        rsa("49015F787433103580E3B66A1707A00E60F2D15B"), // maatuska
137        rsa("F533C81CEF0BC0267857C99B2F471ADF249FA232"), // moria1
138        rsa("2F3DF9CA0E5D36F2685A2DA67184EB8DCB8CBA8C"), // tor26
139    ]
140}
141
142/// Returns a list of the [`SocketAddr`] for the directory authorities.
143///
144/// The nested [`Vec`] serves dual-stack purposes
145/// (i.e. many IPs mapping to one logical authority).
146fn default_uploads() -> Vec<Vec<SocketAddr>> {
147    /// Converts a [`str`] to a [`SocketAddr`].
148    fn sa(s: &str) -> SocketAddr {
149        s.parse().expect("invalid socket address?!?")
150    }
151
152    vec![
153        // dannenberg
154        vec![sa("193.23.244.244:80"), sa("[2001:678:558:1000::244]:80")],
155        // dizum
156        vec![sa("45.66.35.11:80"), sa("[2a09:61c0::1337]:80")],
157        // faravahar
158        vec![sa("216.218.219.41:80"), sa("[2001:470:164:2::2]:80")],
159        // gabelmoo
160        vec![
161            sa("131.188.40.189:80"),
162            sa("[2001:638:a000:4140::ffff:189]:80"),
163        ],
164        // longclaw
165        vec![sa("199.58.81.140:80")],
166        // maatuska
167        vec![sa("171.25.193.9:443"), sa("[2001:67c:289c::9]:443")],
168        // moria1
169        vec![sa("128.31.0.39:9231")],
170        // tor26
171        vec![sa("217.196.147.77:80"), sa("[2a02:16a8:662:2203::1]:80")],
172    ]
173}
174
175/// For now, an alias to [`default_uploads()`].
176fn default_downloads() -> Vec<Vec<SocketAddr>> {
177    default_uploads()
178}
179
180/// For now, an alias to [`default_uploads()`].
181fn default_votes() -> Vec<Vec<SocketAddr>> {
182    default_uploads()
183}
184
185#[cfg(test)]
186mod test {
187    // @@ begin test lint list maintained by maint/add_warning @@
188    #![allow(clippy::bool_assert_comparison)]
189    #![allow(clippy::clone_on_copy)]
190    #![allow(clippy::dbg_macro)]
191    #![allow(clippy::mixed_attributes_style)]
192    #![allow(clippy::print_stderr)]
193    #![allow(clippy::print_stdout)]
194    #![allow(clippy::single_char_pattern)]
195    #![allow(clippy::unwrap_used)]
196    #![allow(clippy::unchecked_time_subtraction)]
197    #![allow(clippy::useless_vec)]
198    #![allow(clippy::needless_pass_by_value)]
199    #![allow(clippy::string_slice)] // See arti#2571
200    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
201    #![allow(clippy::unnecessary_wraps)]
202
203    use super::*;
204
205    #[test]
206    fn default_auths() {
207        let dflt = AuthorityContacts::builder().build().unwrap();
208        assert_eq!(dflt.v3idents, default_v3idents());
209        assert_eq!(dflt.uploads, default_uploads());
210        assert_eq!(dflt.downloads, default_downloads());
211        assert_eq!(dflt.votes, default_votes());
212        assert_eq!(dflt.v3idents.len(), dflt.uploads.len());
213
214        // Use tor26's entry for this test. Note that this depends on tor26
215        // being listed last in default_v3idents() and default_uploads().
216        let last = dflt.v3idents.len() - 1;
217        assert_eq!(
218            dflt.v3idents[last],
219            RsaIdentity::from_hex("2F3DF9CA0E5D36F2685A2DA67184EB8DCB8CBA8C").unwrap()
220        );
221        assert_eq!(
222            dflt.uploads[last],
223            vec![
224                "217.196.147.77:80".parse().unwrap(),
225                "[2a02:16a8:662:2203::1]:80".parse().unwrap()
226            ]
227        );
228        assert_eq!(dflt.uploads[last], dflt.downloads[last]);
229        assert_eq!(dflt.uploads[last], dflt.votes[last]);
230    }
231}