1
//! Module providing [`CircuitExtender`].
2

            
3
use super::{Circuit, ReactorResultChannel};
4
use crate::circuit::circhop::HopSettings;
5
use crate::client::circuit::handshake::HandshakeRole;
6
use crate::client::reactor::MetaCellDisposition;
7
use crate::crypto::cell::HopNum;
8
use crate::crypto::handshake::fast::CreateFastClient;
9
use crate::crypto::handshake::ntor_v3::NtorV3Client;
10
use crate::tunnel::TunnelScopedCircId;
11
use crate::{Error, Result};
12
use crate::{HopLocation, congestion};
13
use oneshot_fused_workaround as oneshot;
14
use std::borrow::Borrow;
15
use tor_cell::chancell::msg::HandshakeType;
16
use tor_cell::relaycell::msg::{Extend2, Extended2};
17
use tor_cell::relaycell::{AnyRelayMsgOuter, UnparsedRelayMsg};
18
use tor_error::internal;
19

            
20
use crate::circuit::circhop::SendRelayCell;
21
use crate::client::circuit::path;
22
use crate::client::reactor::MetaCellHandler;
23
use crate::crypto::handshake::ntor::NtorClient;
24
use crate::crypto::handshake::{ClientHandshake, KeyGenerator};
25
use tor_cell::relaycell::extend::CircResponseExt;
26
use tor_linkspec::{EncodedLinkSpec, OwnedChanTarget};
27
use tracing::trace;
28

            
29
/// An object that can extend a circuit by one hop, using the `MetaCellHandler` trait.
30
///
31
/// Yes, I know having trait bounds on structs is bad, but in this case it's necessary
32
/// since we want to be able to use `H::KeyType`.
33
pub(crate) struct CircuitExtender<H>
34
where
35
    H: ClientHandshake,
36
{
37
    /// The peer that we're extending to.
38
    ///
39
    /// Used to extend our record of the circuit's path.
40
    peer_id: OwnedChanTarget,
41
    /// Handshake state.
42
    state: Option<H::StateType>,
43
    /// In-progress settings that we're negotiating for this hop.
44
    settings: HopSettings,
45
    /// An identifier for logging about this reactor's circuit.
46
    unique_id: TunnelScopedCircId,
47
    /// The hop we're expecting the EXTENDED2 cell to come back from.
48
    expected_hop: HopNum,
49
    /// A oneshot channel that we should inform when we are done with this extend operation.
50
    operation_finished: Option<oneshot::Sender<Result<()>>>,
51
}
52
impl<H> CircuitExtender<H>
53
where
54
    H: ClientHandshake + HandshakeAuxDataHandler,
55
    H::KeyGen: KeyGenerator,
56
{
57
    /// Start extending a circuit, sending the necessary EXTEND cell and returning a
58
    /// new `CircuitExtender` to be called when the reply arrives.
59
    ///
60
    /// The `handshake_id` is the numeric identifier for what kind of
61
    /// handshake we're doing.  The `key` is the relay's onion key that
62
    /// goes along with the handshake, and the `linkspecs` are the
63
    /// link specifiers to include in the EXTEND cell to tell the
64
    /// current last hop which relay to connect to.
65
    #[allow(clippy::too_many_arguments)]
66
    #[allow(clippy::blocks_in_conditions)]
67
72
    pub(crate) fn begin(
68
72
        peer_id: OwnedChanTarget,
69
72
        handshake_id: HandshakeType,
70
72
        key: &H::KeyType,
71
72
        linkspecs: Vec<EncodedLinkSpec>,
72
72
        settings: HopSettings,
73
72
        client_aux_data: &impl Borrow<H::ClientAuxData>,
74
72
        circ: &mut Circuit,
75
72
        done: ReactorResultChannel<()>,
76
72
    ) -> Result<(Self, SendRelayCell)> {
77
72
        match (|| {
78
72
            let mut rng = rand::rng();
79
72
            let unique_id = circ.unique_id;
80

            
81
72
            let (state, msg) = H::client1(&mut rng, key, client_aux_data)?;
82
72
            let n_hops = circ.crypto_out.n_layers();
83
72
            let hop = ((n_hops - 1) as u8).into();
84
72
            trace!(
85
                circ_id = %unique_id,
86
                target_hop = n_hops + 1,
87
                linkspecs = ?linkspecs,
88
                "Extending circuit",
89
            );
90
72
            let extend_msg = Extend2::new(linkspecs, handshake_id, msg);
91
72
            let cell = AnyRelayMsgOuter::new(None, extend_msg.into());
92
            // Prepare a message to send message to the last hop...
93
72
            let cell = SendRelayCell {
94
72
                hop: Some(hop),
95
72
                early: true, // use a RELAY_EARLY cel
96
72
                cell,
97
72
            };
98

            
99
72
            trace!(circ_id = %unique_id, "waiting for EXTENDED2 cell");
100
            // ... and now we wait for a response.
101
72
            let extender = Self {
102
72
                peer_id,
103
72
                state: Some(state),
104
72
                settings,
105
72
                unique_id,
106
72
                expected_hop: hop,
107
72
                operation_finished: None,
108
72
            };
109

            
110
72
            Ok::<(CircuitExtender<_>, SendRelayCell), Error>((extender, cell))
111
        })() {
112
72
            Ok(mut result) => {
113
72
                result.0.operation_finished = Some(done);
114
72
                Ok(result)
115
            }
116
            Err(e) => {
117
                // It's okay if the receiver went away.
118
                let _ = done.send(Err(e.clone()));
119
                Err(e)
120
            }
121
        }
122
72
    }
123

            
124
    /// Perform the work of extending the circuit another hop.
125
    ///
126
    /// This is a separate function to simplify the error-handling work of handle_msg().
127
48
    fn extend_circuit(
128
48
        &mut self,
129
48
        msg: UnparsedRelayMsg,
130
48
        circ: &mut Circuit,
131
48
    ) -> Result<MetaCellDisposition> {
132
48
        let msg = msg
133
48
            .decode::<Extended2>()
134
48
            .map_err(|e| Error::from_bytes_err(e, "extended2 message"))?
135
36
            .into_msg();
136

            
137
36
        let relay_handshake = msg.into_body();
138

            
139
36
        trace!(
140
            circ_id = %self.unique_id,
141
            "Received EXTENDED2 cell; completing handshake.",
142
        );
143
        // Now perform the second part of the handshake, and see if it
144
        // succeeded.
145
36
        let (server_aux_data, keygen) = H::client2(
146
36
            self.state
147
36
                .take()
148
36
                .expect("CircuitExtender::finish() called twice"),
149
36
            relay_handshake,
150
12
        )?;
151

            
152
        // Handle auxiliary data returned from the server, e.g. validating that
153
        // requested extensions have been acknowledged.
154
24
        H::handle_server_aux_data(&mut self.settings, &server_aux_data)?;
155

            
156
24
        let layer = self
157
24
            .settings
158
24
            .relay_crypt_protocol()
159
24
            .construct_client_layers(HandshakeRole::Initiator, keygen)?;
160

            
161
24
        trace!(circ_id = %self.unique_id, "Handshake complete; circuit extended.");
162

            
163
        // If we get here, it succeeded.  Add a new hop to the circuit.
164
24
        circ.add_hop(
165
24
            path::HopDetail::Relay(self.peer_id.clone()),
166
24
            layer.fwd,
167
24
            layer.back,
168
24
            layer.binding,
169
24
            &self.settings,
170
        )?;
171
24
        Ok(MetaCellDisposition::ConversationFinished)
172
48
    }
173
}
174

            
175
impl<H> MetaCellHandler for CircuitExtender<H>
176
where
177
    H: ClientHandshake + HandshakeAuxDataHandler,
178
    H::StateType: Send,
179
    H::KeyGen: KeyGenerator,
180
{
181
60
    fn expected_hop(&self) -> HopLocation {
182
60
        (self.unique_id.unique_id(), self.expected_hop).into()
183
60
    }
184
48
    fn handle_msg(
185
48
        &mut self,
186
48
        msg: UnparsedRelayMsg,
187
48
        circ: &mut Circuit,
188
48
    ) -> Result<MetaCellDisposition> {
189
48
        let status = self.extend_circuit(msg, circ);
190

            
191
48
        if let Some(done) = self.operation_finished.take() {
192
            // ignore it if the receiving channel went away.
193
48
            let _ = done.send(status.as_ref().map(|_| ()).map_err(Clone::clone));
194
48
            status
195
        } else {
196
            Err(Error::from(internal!(
197
                "Passed two messages to an CircuitExtender!"
198
            )))
199
        }
200
48
    }
201
}
202

            
203
/// Specifies handling of auxiliary handshake data for a given `ClientHandshake`.
204
//
205
// For simplicity we implement this as a trait of the handshake object itself.
206
// This is currently sufficient because
207
//
208
// 1. We only need or want one handler implementation for a given handshake type.
209
// 2. We currently don't need to keep extra state; i.e. its method doesn't take
210
//    &self.
211
//
212
// If we end up wanting to instantiate objects for one or both of the
213
// `ClientHandshake` object or the `HandshakeAuxDataHandler` object, we could
214
// decouple them by making this something like:
215
//
216
// ```
217
// trait HandshakeAuxDataHandler<H> where H: ClientHandshake
218
// ```
219
pub(crate) trait HandshakeAuxDataHandler: ClientHandshake {
220
    /// Handle auxiliary handshake data returned when creating or extending a
221
    /// circuit.
222
    fn handle_server_aux_data(
223
        settings: &mut HopSettings,
224
        data: &<Self as ClientHandshake>::ServerAuxData,
225
    ) -> Result<()>;
226
}
227

            
228
impl HandshakeAuxDataHandler for NtorV3Client {
229
36
    fn handle_server_aux_data(
230
36
        settings: &mut HopSettings,
231
36
        data: &Vec<CircResponseExt>,
232
36
    ) -> Result<()> {
233
        // Process all extensions.
234
        // If "flowctl-cc" is not enabled, this loop will always return an error, so tell clippy
235
        // that it's okay.
236
        #[cfg_attr(not(feature = "flowctl-cc"), allow(clippy::never_loop))]
237
48
        for ext in data {
238
12
            match ext {
239
12
                CircResponseExt::CcResponse(ack_ext) => {
240
                    cfg_if::cfg_if! {
241
                        if #[cfg(feature = "flowctl-cc")] {
242
                            // Unexpected ACK extension as in if CC is disabled on our side, we would never have
243
                            // requested it. Reject and circuit must be closed.
244
12
                            if !settings.ccontrol.is_enabled() {
245
                                return Err(Error::HandshakeProto(
246
                                    "Received unexpected ntorv3 CC ack extension".into(),
247
                                ));
248
12
                            }
249
12
                            let sendme_inc = ack_ext.sendme_inc();
250
                            // Invalid increment, reject and circuit must be closed.
251
12
                            if !congestion::params::is_sendme_inc_valid(sendme_inc, &settings.ccontrol) {
252
                                return Err(Error::HandshakeProto(
253
                                    "Received invalid sendme increment in CC ntorv3 extension".into(),
254
                                ));
255
12
                            }
256
                            // Excellent, we have a negotiated sendme increment. Set it for this circuit.
257
12
                            settings
258
12
                                .ccontrol
259
12
                                .cwnd_params_mut()
260
12
                                .set_sendme_inc(sendme_inc);
261
                        } else {
262
                            let _ = ack_ext;
263
                            return Err(Error::HandshakeProto(
264
                                "Received unexpected `AckCongestionControl` ntorv3 extension".into(),
265
                            ));
266
                        }
267
                    }
268
                }
269
                // Any other extensions is not expected. Reject and circuit must be closed.
270
                _ => {
271
                    return Err(Error::HandshakeProto(
272
                        "Received unexpected ntorv3 extension".into(),
273
                    ));
274
                }
275
            }
276
        }
277
36
        Ok(())
278
36
    }
279
}
280

            
281
impl HandshakeAuxDataHandler for NtorClient {
282
24
    fn handle_server_aux_data(_settings: &mut HopSettings, _data: &()) -> Result<()> {
283
        // This handshake doesn't have any auxiliary data; nothing to do.
284
24
        Ok(())
285
24
    }
286
}
287

            
288
impl HandshakeAuxDataHandler for CreateFastClient {
289
12
    fn handle_server_aux_data(_settings: &mut HopSettings, _data: &()) -> Result<()> {
290
        // This handshake doesn't have any auxiliary data; nothing to do.
291
12
        Ok(())
292
12
    }
293
}