summaryrefslogtreecommitdiff
path: root/crates/tor-cell/src/relaycell/extend.rs
blob: cacf7ad709743a273a630c5bf029d6df09b853d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! Types and encodings used during circuit extension.

use super::extlist::{Ext, ExtList, ExtListRef, decl_extension_group};
#[cfg(feature = "hs")]
use super::hs::pow::ProofOfWork;
use caret::caret_int;
use itertools::Itertools as _;
use tor_bytes::{EncodeResult, Reader, Writeable as _, Writer};
use tor_protover::NumberedSubver;

caret_int! {
    /// A type of circuit request extension data (`EXT_FIELD_TYPE`).
    #[derive(PartialOrd,Ord)]
    pub struct CircRequestExtType(u8) {
        /// Request congestion control be enabled for a circuit.
        CC_REQUEST = 1,
        /// HS only: provide a completed proof-of-work solution for denial of service
        /// mitigation
        PROOF_OF_WORK = 2,
        /// Request that certain subprotocol features be enabled.
        SUBPROTOCOL_REQUEST = 3,
    }
}

caret_int! {
    /// A type of circuit response extension data (`EXT_FIELD_TYPE`).
    #[derive(PartialOrd,Ord)]
    pub struct CircResponseExtType(u8) {
        /// Acknowledge a congestion control request.
        CC_RESPONSE = 2
    }
}

/// Request congestion control be enabled for this circuit (client → exit node).
///
/// (`EXT_FIELD_TYPE` = 01)
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct CcRequest {}

impl Ext for CcRequest {
    type Id = CircRequestExtType;
    fn type_id(&self) -> Self::Id {
        CircRequestExtType::CC_REQUEST
    }
    fn take_body_from(_b: &mut Reader<'_>) -> tor_bytes::Result<Self> {
        Ok(Self {})
    }
    fn write_body_onto<B: Writer + ?Sized>(&self, _b: &mut B) -> EncodeResult<()> {
        Ok(())
    }
}

/// Acknowledge a congestion control request (exit node → client).
///
/// (`EXT_FIELD_TYPE` = 02)
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CcResponse {
    /// The exit's current view of the `cc_sendme_inc` consensus parameter.
    sendme_inc: u8,
}

impl CcResponse {
    /// Create a new AckCongestionControl with a given value for the
    /// `sendme_inc` parameter.
    pub fn new(sendme_inc: u8) -> Self {
        CcResponse { sendme_inc }
    }

    /// Return the value of the `sendme_inc` parameter for this extension.
    pub fn sendme_inc(&self) -> u8 {
        self.sendme_inc
    }
}

impl Ext for CcResponse {
    type Id = CircResponseExtType;
    fn type_id(&self) -> Self::Id {
        CircResponseExtType::CC_RESPONSE
    }

    fn take_body_from(b: &mut Reader<'_>) -> tor_bytes::Result<Self> {
        let sendme_inc = b.take_u8()?;
        Ok(Self { sendme_inc })
    }

    fn write_body_onto<B: Writer + ?Sized>(&self, b: &mut B) -> EncodeResult<()> {
        b.write_u8(self.sendme_inc);
        Ok(())
    }
}

/// A request that a certain set of protocols should be enabled. (client to server)
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SubprotocolRequest {
    /// The protocols to enable.
    protocols: Vec<tor_protover::NumberedSubver>,
}

impl<A> FromIterator<A> for SubprotocolRequest
where
    A: Into<tor_protover::NumberedSubver>,
{
    fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
        let mut protocols: Vec<_> = iter.into_iter().map(Into::into).collect();
        protocols.sort();
        protocols.dedup();
        Self { protocols }
    }
}

impl Ext for SubprotocolRequest {
    type Id = CircRequestExtType;

    fn type_id(&self) -> Self::Id {
        CircRequestExtType::SUBPROTOCOL_REQUEST
    }

    fn take_body_from(b: &mut Reader<'_>) -> tor_bytes::Result<Self> {
        let mut protocols = Vec::new();
        while b.remaining() != 0 {
            protocols.push(b.extract()?);
        }

        if !is_strictly_ascending(&protocols) {
            return Err(tor_bytes::Error::InvalidMessage(
                "SubprotocolRequest not sorted and deduplicated.".into(),
            ));
        }

        Ok(Self { protocols })
    }

    fn write_body_onto<B: Writer + ?Sized>(&self, b: &mut B) -> EncodeResult<()> {
        for p in self.protocols.iter() {
            b.write(p)?;
        }
        Ok(())
    }
}
impl SubprotocolRequest {
    /// Return true if this [`SubprotocolRequest`] contains the listed capability.
    pub fn contains(&self, cap: tor_protover::NamedSubver) -> bool {
        self.protocols.binary_search(&cap.into()).is_ok()
    }

    /// Return true if this [`SubprotocolRequest`] contains no other
    /// capabilities except those listed in `list`.
    pub fn contains_only(&self, list: &tor_protover::Protocols) -> bool {
        self.protocols
            .iter()
            .all(|p| list.supports_numbered_subver(*p))
    }
}

decl_extension_group! {
    /// An extension to be sent along with a circuit extension request
    /// (CREATE2, EXTEND2, or INTRODUCE.)
    #[derive(Debug,Clone,PartialEq)]
    #[non_exhaustive]
    pub enum CircRequestExt [ CircRequestExtType ] {
        /// Request to enable congestion control.
        CcRequest,
        /// HS-only: Provide a proof-of-work solution.
        [ feature: #[cfg(feature = "hs")] ]
        ProofOfWork,
        /// Request to enable one or more subprotocol capabilities.
        SubprotocolRequest,
    }
}

decl_extension_group! {
    /// An extension to be sent along with a circuit extension response
    /// (CREATED2 or EXTENDED2.)
    ///
    /// RENDEZVOUS is not currently supported, but once we replace hs-ntor
    /// with something better, extensions will be possible there too.
    #[derive(Debug,Clone,PartialEq)]
    #[non_exhaustive]
    pub enum CircResponseExt [ CircResponseExtType ] {
        /// Response indicating that congestion control is enabled.
        CcResponse,
    }
}

/// Helper for generating encoding and decoding functions
/// for [`CircRequestExt`] and [`CircResponseExt`].
macro_rules! impl_encode_decode {
    ($extgroup:ty, $name:expr) => {
        impl $extgroup {
            /// Encode a set of extensions into a "message" for a circuit handshake.
            pub fn write_many_onto<W: Writer>(exts: &[Self], out: &mut W) -> EncodeResult<()> {
                ExtListRef::from(exts).write_onto(out)?;
                Ok(())
            }
            /// Decode a slice of bytes representing the "message" of a circuit handshake into a set of
            /// extensions.
            pub fn decode(message: &[u8]) -> crate::Result<Vec<Self>> {
                let err_cvt = |err| crate::Error::BytesErr { err, parsed: $name };
                let mut r = tor_bytes::Reader::from_slice(message);
                let list: ExtList<_> = r.extract().map_err(err_cvt)?;
                r.should_be_exhausted().map_err(err_cvt)?;
                Ok(list.into_vec())
            }
        }
    };
}

impl_encode_decode!(CircRequestExt, "CREATE2 extension list");
impl_encode_decode!(CircResponseExt, "CREATED2 extension list");

/// Return true iff the list of protocol capabilities is strictly ascending.
fn is_strictly_ascending(vers: &[NumberedSubver]) -> bool {
    // We don't use is_sorted, since that doesn't detect duplicates.
    vers.iter().tuple_windows().all(|(a, b)| a < b)
}

#[cfg(test)]
mod test {
    // @@ begin test lint list maintained by maint/add_warning @@
    #![allow(clippy::bool_assert_comparison)]
    #![allow(clippy::clone_on_copy)]
    #![allow(clippy::dbg_macro)]
    #![allow(clippy::mixed_attributes_style)]
    #![allow(clippy::print_stderr)]
    #![allow(clippy::print_stdout)]
    #![allow(clippy::single_char_pattern)]
    #![allow(clippy::unwrap_used)]
    #![allow(clippy::unchecked_time_subtraction)]
    #![allow(clippy::useless_vec)]
    #![allow(clippy::needless_pass_by_value)]
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
    use super::*;

    #[test]
    fn subproto_ext_valid() {
        use tor_protover::named::*;
        let sp: SubprotocolRequest = [RELAY_NTORV3, RELAY_NTORV3, LINK_V4].into_iter().collect();
        let mut v = Vec::new();
        sp.write_body_onto(&mut v).unwrap();
        assert_eq!(&v[..], [0, 4, 2, 4]);

        let mut r = Reader::from_slice(&v[..]);
        let sp2: SubprotocolRequest = SubprotocolRequest::take_body_from(&mut r).unwrap();
        assert_eq!(sp, sp2);
    }

    #[test]
    fn subproto_invalid() {
        // Odd length.
        let mut r = Reader::from_slice(&[0, 4, 2]);
        let e = SubprotocolRequest::take_body_from(&mut r).unwrap_err();
        dbg!(e.to_string());
        assert!(e.to_string().contains("too short"));

        // Duplicate protocols.
        let mut r = Reader::from_slice(&[0, 4, 0, 4]);
        let e = SubprotocolRequest::take_body_from(&mut r).unwrap_err();
        dbg!(e.to_string());
        assert!(e.to_string().contains("deduplicated"));

        // not-sorted protocols.
        let mut r = Reader::from_slice(&[2, 4, 0, 4]);
        let e = SubprotocolRequest::take_body_from(&mut r).unwrap_err();
        dbg!(e.to_string());
        assert!(e.to_string().contains("sorted"));
    }

    #[test]
    fn subproto_supported() {
        use tor_protover::named::*;
        let sp: SubprotocolRequest = [RELAY_NTORV3, RELAY_NTORV3, LINK_V4].into_iter().collect();
        // "contains" tells us if a subprotocol capability is a member of the request.
        assert!(sp.contains(LINK_V4));
        assert!(!sp.contains(LINK_V2));

        // contains_only tells us if there are any subprotocol capabilities in the request
        // other than those listed.
        assert!(sp.contains_only(&[RELAY_NTORV3, LINK_V4, CONFLUX_BASE].into_iter().collect()));
        assert!(sp.contains_only(&[RELAY_NTORV3, LINK_V4].into_iter().collect()));
        assert!(!sp.contains_only(&[LINK_V4].into_iter().collect()));
        assert!(!sp.contains_only(&[LINK_V4, CONFLUX_BASE].into_iter().collect()));
        assert!(!sp.contains_only(&[CONFLUX_BASE].into_iter().collect()));
    }
}