tor_proto/channel/
handshake.rs

1//! Implementations for the channel handshake
2
3use asynchronous_codec;
4use futures::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
5use futures::sink::SinkExt;
6use futures::stream::StreamExt;
7use tor_cell::restricted_msg;
8use tor_error::internal;
9
10use crate::channel::codec::{self, ChannelCodec, CodecError};
11use crate::channel::UniqId;
12use crate::memquota::ChannelAccount;
13use crate::util::skew::ClockSkew;
14use crate::{Error, Result};
15use tor_cell::chancell::{msg, ChanCmd, ChanMsg};
16use tor_rtcompat::{CoarseTimeProvider, SleepProvider, StreamOps};
17
18use std::net::SocketAddr;
19use std::sync::Arc;
20use std::time::SystemTime;
21
22use tor_bytes::Reader;
23use tor_linkspec::{ChanTarget, ChannelMethod, OwnedChanTargetBuilder, RelayIds};
24use tor_llcrypto as ll;
25use tor_llcrypto::pk::ed25519::Ed25519Identity;
26use tor_llcrypto::pk::rsa::RsaIdentity;
27
28use digest::Digest;
29
30use super::CellFrame;
31
32use tracing::{debug, trace};
33
34/// A list of the link protocols that we support.
35static LINK_PROTOCOLS: &[u16] = &[4, 5];
36
37/// A raw client channel on which nothing has been done.
38pub struct OutboundClientHandshake<
39    T: AsyncRead + AsyncWrite + StreamOps + Send + Unpin + 'static,
40    S: CoarseTimeProvider + SleepProvider,
41> {
42    /// Runtime handle (insofar as we need it)
43    sleep_prov: S,
44
45    /// Memory quota account
46    memquota: ChannelAccount,
47
48    /// Underlying TLS stream.
49    ///
50    /// (We don't enforce that this is actually TLS, but if it isn't, the
51    /// connection won't be secure.)
52    tls: T,
53
54    /// Declared target method for this channel, if any.
55    target_method: Option<ChannelMethod>,
56
57    /// Logging identifier for this stream.  (Used for logging only.)
58    unique_id: UniqId,
59}
60
61/// A client channel on which versions have been negotiated and the
62/// relay's handshake has been read, but where the certs have not
63/// been checked.
64pub struct UnverifiedChannel<
65    T: AsyncRead + AsyncWrite + StreamOps + Send + Unpin + 'static,
66    S: CoarseTimeProvider + SleepProvider,
67> {
68    /// Runtime handle (insofar as we need it)
69    sleep_prov: S,
70    /// Memory quota account
71    memquota: ChannelAccount,
72    /// The negotiated link protocol.  Must be a member of LINK_PROTOCOLS
73    link_protocol: u16,
74    /// The Source+Sink on which we're reading and writing cells.
75    tls: CellFrame<T>,
76    /// The certs cell that we got from the relay.
77    certs_cell: msg::Certs,
78    /// Declared target method for this channel, if any.
79    target_method: Option<ChannelMethod>,
80    /// The netinfo cell that we got from the relay.
81    #[allow(dead_code)] // Relays will need this.
82    netinfo_cell: msg::Netinfo,
83    /// How much clock skew did we detect in this handshake?
84    ///
85    /// This value is _unauthenticated_, since we have not yet checked whether
86    /// the keys in the handshake are the ones we expected.
87    clock_skew: ClockSkew,
88    /// Logging identifier for this stream.  (Used for logging only.)
89    unique_id: UniqId,
90}
91
92/// A client channel on which versions have been negotiated,
93/// relay's handshake has been read, but the client has not yet
94/// finished the handshake.
95///
96/// This type is separate from UnverifiedChannel, since finishing the
97/// handshake requires a bunch of CPU, and you might want to do it as
98/// a separate task or after a yield.
99pub struct VerifiedChannel<
100    T: AsyncRead + AsyncWrite + StreamOps + Send + Unpin + 'static,
101    S: CoarseTimeProvider + SleepProvider,
102> {
103    /// Runtime handle (insofar as we need it)
104    sleep_prov: S,
105    /// Memory quota account
106    memquota: ChannelAccount,
107    /// The negotiated link protocol.
108    link_protocol: u16,
109    /// The Source+Sink on which we're reading and writing cells.
110    tls: CellFrame<T>,
111    /// Declared target method for this stream, if any.
112    target_method: Option<ChannelMethod>,
113    /// Logging identifier for this stream.  (Used for logging only.)
114    unique_id: UniqId,
115    /// Validated Ed25519 identity for this peer.
116    ed25519_id: Ed25519Identity,
117    /// Validated RSA identity for this peer.
118    rsa_id: RsaIdentity,
119    /// Authenticated clock skew for this peer.
120    clock_skew: ClockSkew,
121}
122
123restricted_msg! {
124    /// A restricted subset of ChanMsg that can arrive during a handshake.
125    ///
126    /// (These are messages that come after the VERSIONS cell, up to and
127    /// including the NETINFO.)
128    ///
129    /// Note that unrecognized message types (ones not yet implemented in Arti)
130    /// cause an error, rather than getting ignored.  That's intentional: if we
131    /// start to allow them in the future, we should negotiate a new Channel
132    /// protocol for the VERSIONS cell.
133    #[derive(Clone,Debug)]
134    enum HandshakeMsg : ChanMsg {
135        Padding,
136        Vpadding,
137        AuthChallenge,
138        Certs,
139        Netinfo
140    }
141}
142
143/// Convert a CodecError to an Error, under the context that it occurs while
144/// doing a channel handshake.
145fn codec_err_to_handshake(err: CodecError) -> Error {
146    match err {
147        CodecError::Io(e) => Error::HandshakeIoErr(Arc::new(e)),
148        CodecError::DecCell(e) => {
149            Error::HandshakeProto(format!("Invalid cell on handshake: {}", e))
150        }
151        CodecError::EncCell(e) => Error::from_cell_enc(e, "cell on handshake"),
152    }
153}
154
155impl<
156        T: AsyncRead + AsyncWrite + StreamOps + Send + Unpin + 'static,
157        S: CoarseTimeProvider + SleepProvider,
158    > OutboundClientHandshake<T, S>
159{
160    /// Construct a new OutboundClientHandshake.
161    pub(crate) fn new(
162        tls: T,
163        target_method: Option<ChannelMethod>,
164        sleep_prov: S,
165        memquota: ChannelAccount,
166    ) -> Self {
167        Self {
168            tls,
169            target_method,
170            unique_id: UniqId::new(),
171            sleep_prov,
172            memquota,
173        }
174    }
175
176    /// Negotiate a link protocol version with the relay, and read
177    /// the relay's handshake information.
178    ///
179    /// Takes a function that reports the current time.  In theory, this can just be
180    /// `SystemTime::now()`.
181    pub async fn connect<F>(mut self, now_fn: F) -> Result<UnverifiedChannel<T, S>>
182    where
183        F: FnOnce() -> SystemTime,
184    {
185        /// Helper: wrap an IoError as a HandshakeIoErr.
186        fn io_err_to_handshake(err: std::io::Error) -> Error {
187            Error::HandshakeIoErr(Arc::new(err))
188        }
189
190        match &self.target_method {
191            Some(method) => debug!(
192                "{}: starting Tor handshake with {:?}",
193                self.unique_id, method
194            ),
195            None => debug!("{}: starting Tor handshake", self.unique_id),
196        }
197        trace!("{}: sending versions", self.unique_id);
198        // Send versions cell
199        {
200            let my_versions = msg::Versions::new(LINK_PROTOCOLS)
201                .map_err(|e| Error::from_cell_enc(e, "versions message"))?;
202            self.tls
203                .write_all(
204                    &my_versions
205                        .encode_for_handshake()
206                        .map_err(|e| Error::from_cell_enc(e.into(), "versions message"))?,
207                )
208                .await
209                .map_err(io_err_to_handshake)?;
210            self.tls.flush().await.map_err(io_err_to_handshake)?;
211        }
212        let versions_flushed_at = coarsetime::Instant::now();
213        let versions_flushed_wallclock = now_fn();
214
215        // Get versions cell.
216        trace!("{}: waiting for versions", self.unique_id);
217        let their_versions: msg::Versions = {
218            // TODO: this could be turned into another function, I suppose.
219            let mut hdr = [0_u8; 5];
220            let not_relay = || {
221                Err(Error::HandshakeProto(
222                    "Doesn't seem to be a tor relay".into(),
223                ))
224            };
225            match self.tls.read_exact(&mut hdr).await {
226                Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return not_relay(),
227                otherwise => otherwise,
228            }
229            .map_err(io_err_to_handshake)?;
230            if hdr[0..3] != [0, 0, ChanCmd::VERSIONS.into()] {
231                return not_relay();
232            }
233            let msglen = u16::from_be_bytes(
234                hdr[3..5]
235                    .try_into()
236                    .expect("Two-byte field was not two bytes!?"),
237            );
238            let mut msg = vec![0; msglen as usize];
239            self.tls
240                .read_exact(&mut msg)
241                .await
242                .map_err(io_err_to_handshake)?;
243            let mut reader = Reader::from_slice(&msg);
244            reader
245                .extract()
246                .map_err(|e| Error::from_bytes_err(e, "versions cell"))?
247        };
248        trace!("{}: received {:?}", self.unique_id, their_versions);
249
250        // Determine which link protocol we negotiated.
251        let link_protocol = their_versions
252            .best_shared_link_protocol(LINK_PROTOCOLS)
253            .ok_or_else(|| Error::HandshakeProto("No shared link protocols".into()))?;
254        trace!("{}: negotiated version {}", self.unique_id, link_protocol);
255
256        // Now we can switch to using a "Framed". We can ignore the
257        // AsyncRead/AsyncWrite aspects of the tls, and just treat it
258        // as a stream and a sink for cells.
259        let codec = ChannelCodec::<HandshakeMsg, HandshakeMsg>::new(link_protocol);
260        let mut tls = asynchronous_codec::Framed::new(self.tls, codec);
261
262        // Read until we have the netinfo cells.
263        let mut certs: Option<msg::Certs> = None;
264        let mut netinfo: Option<(msg::Netinfo, coarsetime::Instant)> = None;
265        let mut seen_authchallenge = false;
266
267        // Loop: reject duplicate and unexpected cells
268        trace!("{}: waiting for rest of handshake.", self.unique_id);
269        while let Some(m) = tls.next().await {
270            use HandshakeMsg::*;
271            let (_, m) = m.map_err(codec_err_to_handshake)?.into_circid_and_msg();
272            trace!("{}: received a {} cell.", self.unique_id, m.cmd());
273            match m {
274                // Are these technically allowed?
275                Padding(_) | Vpadding(_) => (),
276                // Clients don't care about AuthChallenge
277                AuthChallenge(_) => {
278                    if seen_authchallenge {
279                        return Err(Error::HandshakeProto("Duplicate authchallenge cell".into()));
280                    }
281                    seen_authchallenge = true;
282                }
283                Certs(c) => {
284                    if certs.is_some() {
285                        return Err(Error::HandshakeProto("Duplicate certs cell".into()));
286                    }
287                    certs = Some(c);
288                }
289                Netinfo(n) => {
290                    if netinfo.is_some() {
291                        // This should be impossible, since we would
292                        // exit this loop on the first netinfo cell.
293                        return Err(Error::from(internal!(
294                            "Somehow tried to record a duplicate NETINFO cell"
295                        )));
296                    }
297                    netinfo = Some((n, coarsetime::Instant::now()));
298                    break;
299                }
300            }
301        }
302
303        // If we have certs and netinfo, we can finish authenticating.
304        match (certs, netinfo) {
305            (Some(_), None) => Err(Error::HandshakeProto(
306                "Missing netinfo or closed stream".into(),
307            )),
308            (None, _) => Err(Error::HandshakeProto("Missing certs cell".into())),
309            (Some(certs_cell), Some((netinfo_cell, netinfo_rcvd_at))) => {
310                trace!("{}: received handshake, ready to verify.", self.unique_id);
311                // Try to compute our clock skew.  It won't be authenticated
312                // yet, since we haven't checked the certificates.
313                let clock_skew = if let Some(netinfo_timestamp) = netinfo_cell.timestamp() {
314                    let delay = netinfo_rcvd_at - versions_flushed_at;
315                    ClockSkew::from_handshake_timestamps(
316                        versions_flushed_wallclock,
317                        netinfo_timestamp,
318                        delay.into(),
319                    )
320                } else {
321                    ClockSkew::None
322                };
323                Ok(UnverifiedChannel {
324                    link_protocol,
325                    tls: codec::change_message_types(tls),
326                    certs_cell,
327                    netinfo_cell,
328                    clock_skew,
329                    target_method: self.target_method.take(),
330                    unique_id: self.unique_id,
331                    sleep_prov: self.sleep_prov.clone(),
332                    memquota: self.memquota.clone(),
333                })
334            }
335        }
336    }
337}
338
339impl<
340        T: AsyncRead + AsyncWrite + StreamOps + Send + Unpin + 'static,
341        S: CoarseTimeProvider + SleepProvider,
342    > UnverifiedChannel<T, S>
343{
344    /// Return the reported clock skew from this handshake.
345    ///
346    /// Note that the skew reported by this function might not be "true": the
347    /// relay might have its clock set wrong, or it might be lying to us.
348    ///
349    /// The clock skew reported here is not yet authenticated; if you need to
350    /// make sure that the skew is authenticated, use
351    /// [`Channel::clock_skew`](super::Channel::clock_skew) instead.
352    pub fn clock_skew(&self) -> ClockSkew {
353        self.clock_skew
354    }
355
356    /// Validate the certificates and keys in the relay's handshake.
357    ///
358    /// 'peer' is the peer that we want to make sure we're connecting to.
359    ///
360    /// 'peer_cert' is the x.509 certificate that the peer presented during
361    /// its TLS handshake (ServerHello).
362    ///
363    /// 'now' is the time at which to check that certificates are
364    /// valid.  `None` means to use the current time. It can be used
365    /// for testing to override the current view of the time.
366    ///
367    /// This is a separate function because it's likely to be somewhat
368    /// CPU-intensive.
369    pub fn check<U: ChanTarget + ?Sized>(
370        self,
371        peer: &U,
372        peer_cert: &[u8],
373        now: Option<std::time::SystemTime>,
374    ) -> Result<VerifiedChannel<T, S>> {
375        let peer_cert_sha256 = ll::d::Sha256::digest(peer_cert);
376        self.check_internal(peer, &peer_cert_sha256[..], now)
377    }
378
379    /// Same as `check`, but takes the SHA256 hash of the peer certificate,
380    /// since that is all we use.
381    fn check_internal<U: ChanTarget + ?Sized>(
382        self,
383        peer: &U,
384        peer_cert_sha256: &[u8],
385        now: Option<SystemTime>,
386    ) -> Result<VerifiedChannel<T, S>> {
387        use tor_cert::CertType;
388        use tor_checkable::*;
389
390        /// Helper: given a time-bound input, give a result reflecting its
391        /// validity at `now`, and the inner object.
392        ///
393        /// We use this here because we want to validate the whole handshake
394        /// regardless of whether the certs are expired, so we can determine
395        /// whether we got a plausible handshake with a skewed partner, or
396        /// whether the handshake is definitely bad.
397        fn check_timeliness<C, T>(checkable: C, now: SystemTime, skew: ClockSkew) -> (Result<()>, T)
398        where
399            C: Timebound<T, Error = TimeValidityError>,
400        {
401            let status = checkable.is_valid_at(&now).map_err(|e| match (e, skew) {
402                (TimeValidityError::Expired(expired_by), ClockSkew::Fast(skew))
403                    if expired_by < skew =>
404                {
405                    Error::HandshakeCertsExpired { expired_by }
406                }
407                // As it so happens, we don't need to check for this case, since the certs in use
408                // here only have an expiration time in them.
409                // (TimeValidityError::NotYetValid(_), ClockSkew::Slow(_)) => todo!(),
410                (_, _) => Error::HandshakeProto("Certificate expired or not yet valid".into()),
411            });
412            let cert = checkable.dangerously_assume_timely();
413            (status, cert)
414        }
415        // Replace 'now' with the real time to use.
416        let now = now.unwrap_or_else(SystemTime::now);
417
418        // We need to check the following lines of authentication:
419        //
420        // First, to bind the ed identity to the channel.
421        //    peer.ed_identity() matches the key in...
422        //    IDENTITY_V_SIGNING cert, which signs...
423        //    SIGNING_V_TLS_CERT cert, which signs peer_cert.
424        //
425        // Second, to bind the rsa identity to the ed identity:
426        //    peer.rsa_identity() matches the key in...
427        //    the x.509 RSA identity certificate (type 2), which signs...
428        //    the RSA->Ed25519 crosscert (type 7), which signs...
429        //    peer.ed_identity().
430
431        let c = &self.certs_cell;
432        /// Helper: get a cert from a Certs cell, and convert errors appropriately.
433        fn get_cert(
434            certs: &tor_cell::chancell::msg::Certs,
435            tp: CertType,
436        ) -> Result<tor_cert::KeyUnknownCert> {
437            match certs.parse_ed_cert(tp) {
438                Ok(c) => Ok(c),
439                Err(tor_cell::Error::ChanProto(e)) => Err(Error::HandshakeProto(e)),
440                Err(e) => Err(Error::HandshakeProto(e.to_string())),
441            }
442        }
443
444        let id_sk = get_cert(c, CertType::IDENTITY_V_SIGNING)?;
445        let sk_tls = get_cert(c, CertType::SIGNING_V_TLS_CERT)?;
446
447        let mut sigs = Vec::new();
448
449        // Part 1: validate ed25519 stuff.
450        //
451        // (We are performing our timeliness checks now, but not inspecting them
452        // until later in the function, so that we can distinguish failures that
453        // might be caused by clock skew from failures that are definitely not
454        // clock skew.)
455
456        // Check the identity->signing cert
457        let (id_sk, id_sk_sig) = id_sk
458            .should_have_signing_key()
459            .map_err(Error::HandshakeCertErr)?
460            .dangerously_split()
461            .map_err(Error::HandshakeCertErr)?;
462        sigs.push(&id_sk_sig);
463        let (id_sk_timeliness, id_sk) = check_timeliness(id_sk, now, self.clock_skew);
464
465        // Take the identity key from the identity->signing cert
466        let identity_key = id_sk.signing_key().ok_or_else(|| {
467            Error::HandshakeProto("Missing identity key in identity->signing cert".into())
468        })?;
469
470        // Take the signing key from the identity->signing cert
471        let signing_key = id_sk.subject_key().as_ed25519().ok_or_else(|| {
472            Error::HandshakeProto("Bad key type in identity->signing cert".into())
473        })?;
474
475        // Now look at the signing->TLS cert and check it against the
476        // peer certificate.
477        let (sk_tls, sk_tls_sig) = sk_tls
478            .should_be_signed_with(signing_key)
479            .map_err(Error::HandshakeCertErr)?
480            .dangerously_split()
481            .map_err(Error::HandshakeCertErr)?;
482        sigs.push(&sk_tls_sig);
483        let (sk_tls_timeliness, sk_tls) = check_timeliness(sk_tls, now, self.clock_skew);
484
485        if peer_cert_sha256 != sk_tls.subject_key().as_bytes() {
486            return Err(Error::HandshakeProto(
487                "Peer cert did not authenticate TLS cert".into(),
488            ));
489        }
490
491        // Batch-verify the ed25519 certificates in this handshake.
492        //
493        // In theory we could build a list of _all_ the certificates here
494        // and call pk::validate_all_sigs() instead, but that doesn't gain
495        // any performance.
496        if !ll::pk::ed25519::validate_batch(&sigs[..]) {
497            return Err(Error::HandshakeProto(
498                "Invalid ed25519 signature in handshake".into(),
499            ));
500        }
501
502        // Part 2: validate rsa stuff.
503
504        // What is the RSA identity key, according to the X.509 certificate
505        // in which it is self-signed?
506        //
507        // (We don't actually check this self-signed certificate, and we use
508        // a kludge to extract the RSA key)
509        let pkrsa = c
510            .cert_body(CertType::RSA_ID_X509)
511            .and_then(ll::util::x509_extract_rsa_subject_kludge)
512            .ok_or_else(|| Error::HandshakeProto("Couldn't find RSA identity key".into()))?;
513
514        // Now verify the RSA identity -> Ed Identity crosscert.
515        //
516        // This proves that the RSA key vouches for the Ed key.  Note that
517        // the Ed key does not vouch for the RSA key: The RSA key is too
518        // weak.
519        let rsa_cert = c
520            .cert_body(CertType::RSA_ID_V_IDENTITY)
521            .ok_or_else(|| Error::HandshakeProto("No RSA->Ed crosscert".into()))?;
522        let rsa_cert = tor_cert::rsa::RsaCrosscert::decode(rsa_cert)
523            .map_err(|e| Error::from_bytes_err(e, "RSA identity cross-certificate"))?
524            .check_signature(&pkrsa)
525            .map_err(|_| Error::HandshakeProto("Bad RSA->Ed crosscert signature".into()))?;
526        let (rsa_cert_timeliness, rsa_cert) = check_timeliness(rsa_cert, now, self.clock_skew);
527
528        if !rsa_cert.subject_key_matches(identity_key) {
529            return Err(Error::HandshakeProto(
530                "RSA->Ed crosscert certifies incorrect key".into(),
531            ));
532        }
533
534        let rsa_id = pkrsa.to_rsa_identity();
535
536        trace!(
537            "{}: Validated identity as {} [{}]",
538            self.unique_id,
539            identity_key,
540            rsa_id
541        );
542
543        // Now that we've done all the verification steps on the
544        // certificates, we know who we are talking to.  It's time to
545        // make sure that the peer we are talking to is the peer we
546        // actually wanted.
547        //
548        // We do this _last_, since "this is the wrong peer" is
549        // usually a different situation than "this peer couldn't even
550        // identify itself right."
551
552        let actual_identity = RelayIds::builder()
553            .ed_identity(*identity_key)
554            .rsa_identity(rsa_id)
555            .build()
556            .expect("Unable to build RelayIds");
557
558        // We enforce that the relay proved that it has every ID that we wanted:
559        // it may also have additional IDs that we didn't ask for.
560        match super::check_id_match_helper(&actual_identity, peer) {
561            Err(Error::ChanMismatch(msg)) => Err(Error::HandshakeProto(msg)),
562            other => other,
563        }?;
564
565        // If we reach this point, the clock skew might be may now be considered
566        // authenticated: The certificates are what we wanted, and everything
567        // was well signed.
568        //
569        // The only remaining concern is certificate timeliness.  If the
570        // certificates are expired by an amount that is too large for the
571        // declared clock skew to explain, then  we'll return
572        // `Error::HandshakeProto`: in that case the clock skew is _not_
573        // authenticated.  But if the certs are only expired by a little bit,
574        // we'll reject the handshake with `Error::HandshakeCertsExpired`, and
575        // the caller can trust the clock skew.
576        //
577        // We note expired certs last, since we only want to return
578        // `HandshakeCertsExpired` when there are no other errors.
579        id_sk_timeliness?;
580        sk_tls_timeliness?;
581        rsa_cert_timeliness?;
582
583        Ok(VerifiedChannel {
584            link_protocol: self.link_protocol,
585            tls: self.tls,
586            unique_id: self.unique_id,
587            target_method: self.target_method,
588            ed25519_id: *identity_key,
589            rsa_id,
590            clock_skew: self.clock_skew,
591            sleep_prov: self.sleep_prov,
592            memquota: self.memquota,
593        })
594    }
595}
596
597impl<
598        T: AsyncRead + AsyncWrite + StreamOps + Send + Unpin + 'static,
599        S: CoarseTimeProvider + SleepProvider,
600    > VerifiedChannel<T, S>
601{
602    /// Send a 'Netinfo' message to the relay to finish the handshake,
603    /// and create an open channel and reactor.
604    ///
605    /// The channel is used to send cells, and to create outgoing circuits.
606    /// The reactor is used to route incoming messages to their appropriate
607    /// circuit.
608    pub async fn finish(mut self) -> Result<(Arc<super::Channel>, super::reactor::Reactor<S>)> {
609        // We treat a completed channel -- that is to say, one where the
610        // authentication is finished -- as incoming traffic.
611        //
612        // TODO: conceivably we should remember the time when we _got_ the
613        // final cell on the handshake, and update the channel completion
614        // time to be no earlier than _that_ timestamp.
615        crate::note_incoming_traffic();
616        trace!("{}: Sending netinfo cell.", self.unique_id);
617
618        // We do indeed want the real IP here, regardless of whether the
619        // ChannelMethod is Direct connection or not.  The role of the IP in a
620        // NETINFO cell is to tell our peer what address we believe they had, so
621        // that they can better notice MITM attacks and such.
622        let peer_ip = self
623            .target_method
624            .as_ref()
625            .and_then(ChannelMethod::socket_addrs)
626            .and_then(|addrs| addrs.first())
627            .map(SocketAddr::ip);
628        let netinfo = msg::Netinfo::from_client(peer_ip);
629        self.tls
630            .send(netinfo.into())
631            .await
632            .map_err(codec_err_to_handshake)?;
633
634        debug!(
635            "{}: Completed handshake with {} [{}]",
636            self.unique_id, self.ed25519_id, self.rsa_id
637        );
638
639        // Grab a new handle on which we can apply StreamOps (needed for KIST).
640        // On Unix platforms, this handle is a wrapper over the fd of the socket.
641        //
642        // Note: this is necessary because after `StreamExit::split()`,
643        // we no longer have access to the underlying stream
644        // or its StreamOps implementation.
645        let stream_ops = self.tls.new_handle();
646        let (tls_sink, tls_stream) = self.tls.split();
647
648        let mut peer_builder = OwnedChanTargetBuilder::default();
649        if let Some(target_method) = self.target_method {
650            if let Some(addrs) = target_method.socket_addrs() {
651                peer_builder.addrs(addrs.to_owned());
652            }
653            peer_builder.method(target_method);
654        }
655        let peer_id = peer_builder
656            .ed_identity(self.ed25519_id)
657            .rsa_identity(self.rsa_id)
658            .build()
659            .expect("OwnedChanTarget builder failed");
660
661        super::Channel::new(
662            self.link_protocol,
663            Box::new(tls_sink),
664            Box::new(tls_stream),
665            stream_ops,
666            self.unique_id,
667            peer_id,
668            self.clock_skew,
669            self.sleep_prov,
670            self.memquota,
671        )
672    }
673}
674
675#[cfg(test)]
676pub(super) mod test {
677    #![allow(clippy::unwrap_used)]
678    use hex_literal::hex;
679    use regex::Regex;
680    use std::time::{Duration, SystemTime};
681
682    use super::*;
683    use crate::channel::codec::test::MsgBuf;
684    use crate::util::fake_mq;
685    use crate::Result;
686    use tor_cell::chancell::msg;
687    use tor_linkspec::OwnedChanTarget;
688    use tor_rtcompat::{PreferredRuntime, Runtime};
689
690    const VERSIONS: &[u8] = &hex!("0000 07 0006 0003 0004 0005");
691    // no certificates in this cell, but connect() doesn't care.
692    const NOCERTS: &[u8] = &hex!("00000000 81 0001 00");
693    const NETINFO_PREFIX: &[u8] = &hex!(
694        "00000000 08 00000000
695         04 04 7f 00 00 02
696         01
697         04 04 7f 00 00 03"
698    );
699    const NETINFO_PREFIX_WITH_TIME: &[u8] = &hex!(
700        "00000000 08 48949290
701         04 04 7f 00 00 02
702         01
703         04 04 7f 00 00 03"
704    );
705    const AUTHCHALLENGE: &[u8] = &hex!(
706        "00000000 82 0026
707         FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
708         FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
709         0002 0003 00ff"
710    );
711
712    const VPADDING: &[u8] = &hex!("00000000 80 0003 FF FF FF");
713
714    fn add_padded(buf: &mut Vec<u8>, cell: &[u8]) {
715        let len_prev = buf.len();
716        buf.extend_from_slice(cell);
717        buf.resize(len_prev + 514, 0);
718    }
719    fn add_netinfo(buf: &mut Vec<u8>) {
720        add_padded(buf, NETINFO_PREFIX);
721    }
722
723    #[test]
724    fn connect_ok() -> Result<()> {
725        tor_rtcompat::test_with_one_runtime!(|rt| async move {
726            let now = humantime::parse_rfc3339("2008-08-02T17:00:00Z").unwrap();
727            let mut buf = Vec::new();
728            // versions cell
729            buf.extend_from_slice(VERSIONS);
730            // certs cell -- no certs in it, but this function doesn't care.
731            buf.extend_from_slice(NOCERTS);
732            // netinfo cell -- quite minimal.
733            add_padded(&mut buf, NETINFO_PREFIX);
734            let mb = MsgBuf::new(&buf[..]);
735            let handshake = OutboundClientHandshake::new(mb, None, rt.clone(), fake_mq());
736            let unverified = handshake.connect(|| now).await?;
737
738            assert_eq!(unverified.link_protocol, 5);
739            // No timestamp in the NETINFO, so no skew.
740            assert_eq!(unverified.clock_skew(), ClockSkew::None);
741
742            // Try again with an authchallenge cell and some padding.
743            let mut buf = Vec::new();
744            buf.extend_from_slice(VERSIONS);
745            buf.extend_from_slice(NOCERTS);
746            buf.extend_from_slice(VPADDING);
747            buf.extend_from_slice(AUTHCHALLENGE);
748            buf.extend_from_slice(VPADDING);
749            add_padded(&mut buf, NETINFO_PREFIX_WITH_TIME);
750            let mb = MsgBuf::new(&buf[..]);
751            let handshake = OutboundClientHandshake::new(mb, None, rt.clone(), fake_mq());
752            let unverified = handshake.connect(|| now).await?;
753            // Correct timestamp in the NETINFO, so no skew.
754            assert_eq!(unverified.clock_skew(), ClockSkew::None);
755
756            // Now pretend our clock is fast.
757            let now2 = now + Duration::from_secs(3600);
758            let mb = MsgBuf::new(&buf[..]);
759            let handshake = OutboundClientHandshake::new(mb, None, rt.clone(), fake_mq());
760            let unverified = handshake.connect(|| now2).await?;
761            assert_eq!(
762                unverified.clock_skew(),
763                ClockSkew::Fast(Duration::from_secs(3600))
764            );
765
766            Ok(())
767        })
768    }
769
770    async fn connect_err<T: Into<Vec<u8>>, S>(input: T, sleep_prov: S) -> Error
771    where
772        S: CoarseTimeProvider + SleepProvider,
773    {
774        let mb = MsgBuf::new(input);
775        let handshake = OutboundClientHandshake::new(mb, None, sleep_prov, fake_mq());
776        handshake.connect(SystemTime::now).await.err().unwrap()
777    }
778
779    #[test]
780    fn connect_badver() {
781        tor_rtcompat::test_with_one_runtime!(|rt| async move {
782            let err = connect_err(&b"HTTP://"[..], rt.clone()).await;
783            assert!(matches!(err, Error::HandshakeProto(_)));
784            assert_eq!(
785                format!("{}", err),
786                "Handshake protocol violation: Doesn't seem to be a tor relay"
787            );
788
789            let err = connect_err(&hex!("0000 07 0004 1234 ffff")[..], rt.clone()).await;
790            assert!(matches!(err, Error::HandshakeProto(_)));
791            assert_eq!(
792                format!("{}", err),
793                "Handshake protocol violation: No shared link protocols"
794            );
795        });
796    }
797
798    #[test]
799    fn connect_cellparse() {
800        tor_rtcompat::test_with_one_runtime!(|rt| async move {
801            let mut buf = Vec::new();
802            buf.extend_from_slice(VERSIONS);
803            // Here's a certs cell that will fail.
804            buf.extend_from_slice(&hex!("00000000 81 0001 01")[..]);
805            let err = connect_err(buf, rt.clone()).await;
806            assert!(matches!(err, Error::HandshakeProto(_)));
807        });
808    }
809
810    #[test]
811    fn connect_duplicates() {
812        tor_rtcompat::test_with_one_runtime!(|rt| async move {
813            let mut buf = Vec::new();
814            buf.extend_from_slice(VERSIONS);
815            buf.extend_from_slice(NOCERTS);
816            buf.extend_from_slice(NOCERTS);
817            add_netinfo(&mut buf);
818            let err = connect_err(buf, rt.clone()).await;
819            assert!(matches!(err, Error::HandshakeProto(_)));
820            assert_eq!(
821                format!("{}", err),
822                "Handshake protocol violation: Duplicate certs cell"
823            );
824
825            let mut buf = Vec::new();
826            buf.extend_from_slice(VERSIONS);
827            buf.extend_from_slice(NOCERTS);
828            buf.extend_from_slice(AUTHCHALLENGE);
829            buf.extend_from_slice(AUTHCHALLENGE);
830            add_netinfo(&mut buf);
831            let err = connect_err(buf, rt.clone()).await;
832            assert!(matches!(err, Error::HandshakeProto(_)));
833            assert_eq!(
834                format!("{}", err),
835                "Handshake protocol violation: Duplicate authchallenge cell"
836            );
837        });
838    }
839
840    #[test]
841    fn connect_missing_certs() {
842        tor_rtcompat::test_with_one_runtime!(|rt| async move {
843            let mut buf = Vec::new();
844            buf.extend_from_slice(VERSIONS);
845            add_netinfo(&mut buf);
846            let err = connect_err(buf, rt.clone()).await;
847            assert!(matches!(err, Error::HandshakeProto(_)));
848            assert_eq!(
849                format!("{}", err),
850                "Handshake protocol violation: Missing certs cell"
851            );
852        });
853    }
854
855    #[test]
856    fn connect_missing_netinfo() {
857        tor_rtcompat::test_with_one_runtime!(|rt| async move {
858            let mut buf = Vec::new();
859            buf.extend_from_slice(VERSIONS);
860            buf.extend_from_slice(NOCERTS);
861            let err = connect_err(buf, rt.clone()).await;
862            assert!(matches!(err, Error::HandshakeProto(_)));
863            assert_eq!(
864                format!("{}", err),
865                "Handshake protocol violation: Missing netinfo or closed stream"
866            );
867        });
868    }
869
870    #[test]
871    fn connect_misplaced_cell() {
872        tor_rtcompat::test_with_one_runtime!(|rt| async move {
873            let mut buf = Vec::new();
874            buf.extend_from_slice(VERSIONS);
875            // here's a create cell.
876            add_padded(&mut buf, &hex!("00000001 01")[..]);
877            let err = connect_err(buf, rt.clone()).await;
878            assert!(matches!(err, Error::HandshakeProto(_)));
879            assert_eq!(
880                format!("{}", err),
881                "Handshake protocol violation: Invalid cell on handshake: Error while parsing channel cell"
882            );
883        });
884    }
885
886    fn make_unverified<R>(certs: msg::Certs, runtime: R) -> UnverifiedChannel<MsgBuf, R>
887    where
888        R: Runtime,
889    {
890        let localhost = std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST);
891        let netinfo_cell = msg::Netinfo::from_client(Some(localhost));
892        let clock_skew = ClockSkew::None;
893        UnverifiedChannel {
894            link_protocol: 4,
895            tls: asynchronous_codec::Framed::new(MsgBuf::new(&b""[..]), ChannelCodec::new(4)),
896            certs_cell: certs,
897            netinfo_cell,
898            clock_skew,
899            target_method: None,
900            unique_id: UniqId::new(),
901            sleep_prov: runtime,
902            memquota: fake_mq(),
903        }
904    }
905
906    // Timestamp when the example certificates were all valid.
907    fn cert_timestamp() -> SystemTime {
908        use humantime::parse_rfc3339;
909        parse_rfc3339("2020-09-26T18:01:20Z").unwrap()
910    }
911
912    fn certs_test<R>(
913        certs: msg::Certs,
914        when: Option<SystemTime>,
915        peer_ed: &[u8],
916        peer_rsa: &[u8],
917        peer_cert_sha256: &[u8],
918        runtime: &R,
919    ) -> Result<VerifiedChannel<MsgBuf, R>>
920    where
921        R: Runtime,
922    {
923        let unver = make_unverified(certs, runtime.clone());
924        let ed = Ed25519Identity::from_bytes(peer_ed).unwrap();
925        let rsa = RsaIdentity::from_bytes(peer_rsa).unwrap();
926        let chan = OwnedChanTarget::builder()
927            .ed_identity(ed)
928            .rsa_identity(rsa)
929            .build()
930            .unwrap();
931        unver.check_internal(&chan, peer_cert_sha256, when)
932    }
933
934    // no certs at all!
935    #[test]
936    fn certs_none() {
937        let rt = PreferredRuntime::create().unwrap();
938        let err = certs_test(
939            msg::Certs::new_empty(),
940            None,
941            &[0_u8; 32],
942            &[0_u8; 20],
943            &[0_u8; 128],
944            &rt,
945        )
946        .err()
947        .unwrap();
948        assert_eq!(
949            format!("{}", err),
950            "Handshake protocol violation: Missing IDENTITY_V_SIGNING certificate"
951        );
952    }
953
954    #[test]
955    fn certs_good() {
956        let rt = PreferredRuntime::create().unwrap();
957        let mut certs = msg::Certs::new_empty();
958
959        certs.push_cert_body(2.into(), certs::CERT_T2);
960        certs.push_cert_body(5.into(), certs::CERT_T5);
961        certs.push_cert_body(7.into(), certs::CERT_T7);
962        certs.push_cert_body(4.into(), certs::CERT_T4);
963        let res = certs_test(
964            certs,
965            Some(cert_timestamp()),
966            certs::PEER_ED,
967            certs::PEER_RSA,
968            certs::PEER_CERT_DIGEST,
969            &rt,
970        );
971        let _ = res.unwrap();
972    }
973
974    #[test]
975    fn certs_missing() {
976        let rt = PreferredRuntime::create().unwrap();
977        let all_certs = [
978            (2, certs::CERT_T2, "Couldn't find RSA identity key"),
979            (7, certs::CERT_T7, "No RSA->Ed crosscert"),
980            (4, certs::CERT_T4, "Missing IDENTITY_V_SIGNING certificate"),
981            (5, certs::CERT_T5, "Missing SIGNING_V_TLS_CERT certificate"),
982        ];
983
984        for omit_idx in 0..4 {
985            // build a certs cell with all but one certificate
986            let mut certs = msg::Certs::new_empty();
987            let mut expect_err = None;
988            for (idx, (ctype, cert, err)) in all_certs.iter().enumerate() {
989                if idx == omit_idx {
990                    expect_err = Some(err);
991                    continue;
992                }
993
994                certs.push_cert_body((*ctype).into(), &cert[..]);
995            }
996            let res = certs_test(
997                certs,
998                Some(cert_timestamp()),
999                certs::PEER_ED,
1000                certs::PEER_RSA,
1001                certs::PEER_CERT_DIGEST,
1002                &rt,
1003            )
1004            .err()
1005            .unwrap();
1006
1007            assert_eq!(
1008                format!("{}", res),
1009                format!("Handshake protocol violation: {}", expect_err.unwrap())
1010            );
1011        }
1012    }
1013
1014    #[test]
1015    fn certs_wrongtarget() {
1016        let rt = PreferredRuntime::create().unwrap();
1017        let mut certs = msg::Certs::new_empty();
1018        certs.push_cert_body(2.into(), certs::CERT_T2);
1019        certs.push_cert_body(5.into(), certs::CERT_T5);
1020        certs.push_cert_body(7.into(), certs::CERT_T7);
1021        certs.push_cert_body(4.into(), certs::CERT_T4);
1022        let err = certs_test(
1023            certs.clone(),
1024            Some(cert_timestamp()),
1025            &[0x10; 32],
1026            certs::PEER_RSA,
1027            certs::PEER_CERT_DIGEST,
1028            &rt,
1029        )
1030        .err()
1031        .unwrap();
1032
1033        let re = Regex::new(
1034            // identities might be scrubbed by safelog
1035            r"Identity .* does not match target .*",
1036        )
1037        .unwrap();
1038        assert!(re.is_match(&format!("{}", err)));
1039
1040        let err = certs_test(
1041            certs.clone(),
1042            Some(cert_timestamp()),
1043            certs::PEER_ED,
1044            &[0x99; 20],
1045            certs::PEER_CERT_DIGEST,
1046            &rt,
1047        )
1048        .err()
1049        .unwrap();
1050
1051        let re = Regex::new(
1052            // identities might be scrubbed by safelog
1053            r"Identity .* does not match target .*",
1054        )
1055        .unwrap();
1056        assert!(re.is_match(&format!("{}", err)));
1057
1058        let err = certs_test(
1059            certs,
1060            Some(cert_timestamp()),
1061            certs::PEER_ED,
1062            certs::PEER_RSA,
1063            &[0; 32],
1064            &rt,
1065        )
1066        .err()
1067        .unwrap();
1068
1069        assert_eq!(
1070            format!("{}", err),
1071            "Handshake protocol violation: Peer cert did not authenticate TLS cert"
1072        );
1073    }
1074
1075    #[test]
1076    fn certs_badsig() {
1077        let rt = PreferredRuntime::create().unwrap();
1078        fn munge(inp: &[u8]) -> Vec<u8> {
1079            let mut v: Vec<u8> = inp.into();
1080            v[inp.len() - 1] ^= 0x10;
1081            v
1082        }
1083        let mut certs = msg::Certs::new_empty();
1084        certs.push_cert_body(2.into(), certs::CERT_T2);
1085        certs.push_cert_body(5.into(), munge(certs::CERT_T5)); // munge an ed signature
1086        certs.push_cert_body(7.into(), certs::CERT_T7);
1087        certs.push_cert_body(4.into(), certs::CERT_T4);
1088        let res = certs_test(
1089            certs,
1090            Some(cert_timestamp()),
1091            certs::PEER_ED,
1092            certs::PEER_RSA,
1093            certs::PEER_CERT_DIGEST,
1094            &rt,
1095        )
1096        .err()
1097        .unwrap();
1098
1099        assert_eq!(
1100            format!("{}", res),
1101            "Handshake protocol violation: Invalid ed25519 signature in handshake"
1102        );
1103
1104        let mut certs = msg::Certs::new_empty();
1105        certs.push_cert_body(2.into(), certs::CERT_T2);
1106        certs.push_cert_body(5.into(), certs::CERT_T5);
1107        certs.push_cert_body(7.into(), munge(certs::CERT_T7)); // munge an RSA signature
1108        certs.push_cert_body(4.into(), certs::CERT_T4);
1109        let res = certs_test(
1110            certs,
1111            Some(cert_timestamp()),
1112            certs::PEER_ED,
1113            certs::PEER_RSA,
1114            certs::PEER_CERT_DIGEST,
1115            &rt,
1116        )
1117        .err()
1118        .unwrap();
1119
1120        assert_eq!(
1121            format!("{}", res),
1122            "Handshake protocol violation: Bad RSA->Ed crosscert signature"
1123        );
1124    }
1125
1126    /// This module has a few certificates to play with. They're taken
1127    /// from a chutney network. They match those used in the CERTS
1128    /// cell test vector in the tor-cell crate.
1129    ///
1130    /// The names are taken from the type of the certificate.
1131    mod certs {
1132        use hex_literal::hex;
1133
1134        pub(crate) const CERT_T2: &[u8] = &hex!("308201B930820122A0030201020208607C28BE6C390943300D06092A864886F70D01010B0500301F311D301B06035504030C147777772E74636A76356B766A646472322E636F6D301E170D3230303831303030303030305A170D3231303831303030303030305A301F311D301B06035504030C147777772E74636A76356B766A646472322E636F6D30819F300D06092A864886F70D010101050003818D0030818902818100D38B1E6CEB946E0DB0751F4CBACE3DCB9688B6C25304227B4710C35AFB73627E50500F5913E158B621802612D1C75827003703338375237552EB3CD3C12F6AB3604E60C1A2D26BB1FBAD206FF023969A90909D6A65A5458A5312C26EBD3A3DAD30302D4515CDCD264146AC18E6FC60A04BD3EC327F04294D96BA5AA25B464C3F0203010001300D06092A864886F70D01010B0500038181003BCE561EA7F95CC00B78AAB5D69573FF301C282A751D4A651921D042F1BECDBA24D918A6D8A5E138DC07BBA0B335478AE37ABD2C93A93932442AE9084329E846170FE0FC4A50AAFC804F311CC3CA4F41D845A7BA5901CBBC3E021E9794AAC70CE1F37B0A951592DB1B64F2B4AFB81AE52DBD9B6FEDE96A5FB8125EB6251EE50A");
1135
1136        pub(crate) const CERT_T4: &[u8] = &hex!("01040006CC2A01F82294B866A31F01FC5D0DA8572850A9B929545C3266558D7D2316E3B74172B00100200400DCB604DB2034B00FD16986D4ADB9D16B21CB4E4457A33DEC0F538903683E96E9FF1A5203FA27F86EF7528D89A0845D2520166E340754FFEA2AAE0F612B7CE5DA094A0236CDAC45034B0B6842C18E7F6B51B93A3CF7E60663B8AD061C30A62602");
1137        pub(crate) const CERT_T5: &[u8] = &hex!("01050006C98A03B4FD606B64E4CBD466B8D76CB131069BAE6F3AA1878857C9F624E31D77A799B8007173E5F8068431D0D3F5EE16B4C9FFD59DF373E152A87281BAE744AA5FCF72171BF4B27C4E8FC1C6A9FC5CA11058BC49647063D7903CFD9F512F89099B27BC0C");
1138
1139        pub(crate) const CERT_T7: &[u8] = &hex!("DCB604DB2034B00FD16986D4ADB9D16B21CB4E4457A33DEC0F538903683E96E90006DA3A805CF6006F9179066534DE6B45AD47A5C469063EE462762723396DC9F25452A0A52DA3F5087DD239F2A311F6B0D4DFEFF4ABD089DC3D0237A0ABAB19EB2045B91CDCAF04BE0A72D548A27BF2E77BD876ECFE5E1BE622350DA6BF31F6E306ED896488DD5B39409B23FC3EB7B2C9F7328EB18DA36D54D80575899EA6507CCBFCDF1F");
1140
1141        pub(crate) const PEER_CERT_DIGEST: &[u8] =
1142            &hex!("b4fd606b64e4cbd466b8d76cb131069bae6f3aa1878857c9f624e31d77a799b8");
1143
1144        pub(crate) const PEER_ED: &[u8] =
1145            &hex!("dcb604db2034b00fd16986d4adb9d16b21cb4e4457a33dec0f538903683e96e9");
1146        pub(crate) const PEER_RSA: &[u8] = &hex!("2f1fb49bb332a9eec617e41e911c33fb3890aef3");
1147    }
1148
1149    #[test]
1150    fn test_finish() {
1151        tor_rtcompat::test_with_one_runtime!(|rt| async move {
1152            let ed25519_id = [3_u8; 32].into();
1153            let rsa_id = [4_u8; 20].into();
1154            let peer_addr = "127.1.1.2:443".parse().unwrap();
1155            let ver = VerifiedChannel {
1156                link_protocol: 4,
1157                tls: asynchronous_codec::Framed::new(MsgBuf::new(&b""[..]), ChannelCodec::new(4)),
1158                unique_id: UniqId::new(),
1159                target_method: Some(ChannelMethod::Direct(vec![peer_addr])),
1160                ed25519_id,
1161                rsa_id,
1162                clock_skew: ClockSkew::None,
1163                sleep_prov: rt,
1164                memquota: fake_mq(),
1165            };
1166
1167            let (_chan, _reactor) = ver.finish().await.unwrap();
1168
1169            // TODO: check contents of netinfo cell
1170        });
1171    }
1172}