1
//! Implementations for the channel handshake
2

            
3
use asynchronous_codec;
4
use futures::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
5
use futures::sink::SinkExt;
6
use futures::stream::StreamExt;
7
use tor_cell::restricted_msg;
8
use tor_error::internal;
9

            
10
use crate::channel::codec::{self, ChannelCodec, CodecError};
11
use crate::channel::UniqId;
12
use crate::memquota::ChannelAccount;
13
use crate::util::skew::ClockSkew;
14
use crate::{Error, Result};
15
use tor_cell::chancell::{msg, ChanCmd, ChanMsg};
16
use tor_rtcompat::{CoarseTimeProvider, SleepProvider, StreamOps};
17

            
18
use std::net::SocketAddr;
19
use std::sync::Arc;
20
use std::time::SystemTime;
21

            
22
use tor_bytes::Reader;
23
use tor_linkspec::{ChanTarget, ChannelMethod, OwnedChanTargetBuilder, RelayIds};
24
use tor_llcrypto as ll;
25
use tor_llcrypto::pk::ed25519::Ed25519Identity;
26
use tor_llcrypto::pk::rsa::RsaIdentity;
27

            
28
use digest::Digest;
29

            
30
use super::CellFrame;
31

            
32
use tracing::{debug, trace};
33

            
34
/// A list of the link protocols that we support.
35
static LINK_PROTOCOLS: &[u16] = &[4, 5];
36

            
37
/// A raw client channel on which nothing has been done.
38
pub 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.
64
pub 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.
99
pub 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

            
123
restricted_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.
145
4
fn codec_err_to_handshake(err: CodecError) -> Error {
146
4
    match err {
147
        CodecError::Io(e) => Error::HandshakeIoErr(Arc::new(e)),
148
4
        CodecError::DecCell(e) => {
149
4
            Error::HandshakeProto(format!("Invalid cell on handshake: {}", e))
150
        }
151
        CodecError::EncCell(e) => Error::from_cell_enc(e, "cell on handshake"),
152
    }
153
4
}
154

            
155
impl<
156
        T: AsyncRead + AsyncWrite + StreamOps + Send + Unpin + 'static,
157
        S: CoarseTimeProvider + SleepProvider,
158
    > OutboundClientHandshake<T, S>
159
{
160
    /// Construct a new OutboundClientHandshake.
161
26
    pub(crate) fn new(
162
26
        tls: T,
163
26
        target_method: Option<ChannelMethod>,
164
26
        sleep_prov: S,
165
26
        memquota: ChannelAccount,
166
26
    ) -> Self {
167
26
        Self {
168
26
            tls,
169
26
            target_method,
170
26
            unique_id: UniqId::new(),
171
26
            sleep_prov,
172
26
            memquota,
173
26
        }
174
26
    }
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
24
    pub async fn connect<F>(mut self, now_fn: F) -> Result<UnverifiedChannel<T, S>>
182
24
    where
183
24
        F: FnOnce() -> SystemTime,
184
24
    {
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
24
        match &self.target_method {
191
2
            Some(method) => debug!(
192
                "{}: starting Tor handshake with {:?}",
193
                self.unique_id, method
194
            ),
195
22
            None => debug!("{}: starting Tor handshake", self.unique_id),
196
        }
197
24
        trace!("{}: sending versions", self.unique_id);
198
        // Send versions cell
199
        {
200
24
            let my_versions = msg::Versions::new(LINK_PROTOCOLS)
201
24
                .map_err(|e| Error::from_cell_enc(e, "versions message"))?;
202
24
            self.tls
203
24
                .write_all(
204
24
                    &my_versions
205
24
                        .encode_for_handshake()
206
24
                        .map_err(|e| Error::from_cell_enc(e.into(), "versions message"))?,
207
                )
208
24
                .await
209
24
                .map_err(io_err_to_handshake)?;
210
24
            self.tls.flush().await.map_err(io_err_to_handshake)?;
211
        }
212
24
        let versions_flushed_at = coarsetime::Instant::now();
213
24
        let versions_flushed_wallclock = now_fn();
214
24

            
215
24
        // Get versions cell.
216
24
        trace!("{}: waiting for versions", self.unique_id);
217
22
        let their_versions: msg::Versions = {
218
            // TODO: this could be turned into another function, I suppose.
219
24
            let mut hdr = [0_u8; 5];
220
24
            let not_relay = || {
221
2
                Err(Error::HandshakeProto(
222
2
                    "Doesn't seem to be a tor relay".into(),
223
2
                ))
224
2
            };
225
24
            match self.tls.read_exact(&mut hdr).await {
226
                Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return not_relay(),
227
24
                otherwise => otherwise,
228
24
            }
229
24
            .map_err(io_err_to_handshake)?;
230
24
            if hdr[0..3] != [0, 0, ChanCmd::VERSIONS.into()] {
231
2
                return not_relay();
232
22
            }
233
22
            let msglen = u16::from_be_bytes(
234
22
                hdr[3..5]
235
22
                    .try_into()
236
22
                    .expect("Two-byte field was not two bytes!?"),
237
22
            );
238
22
            let mut msg = vec![0; msglen as usize];
239
22
            self.tls
240
22
                .read_exact(&mut msg)
241
22
                .await
242
22
                .map_err(io_err_to_handshake)?;
243
22
            let mut reader = Reader::from_slice(&msg);
244
22
            reader
245
22
                .extract()
246
22
                .map_err(|e| Error::from_bytes_err(e, "versions cell"))?
247
        };
248
22
        trace!("{}: received {:?}", self.unique_id, their_versions);
249

            
250
        // Determine which link protocol we negotiated.
251
22
        let link_protocol = their_versions
252
22
            .best_shared_link_protocol(LINK_PROTOCOLS)
253
22
            .ok_or_else(|| Error::HandshakeProto("No shared link protocols".into()))?;
254
20
        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
20
        let codec = ChannelCodec::<HandshakeMsg, HandshakeMsg>::new(link_protocol);
260
20
        let mut tls = asynchronous_codec::Framed::new(self.tls, codec);
261
20

            
262
20
        // Read until we have the netinfo cells.
263
20
        let mut certs: Option<msg::Certs> = None;
264
20
        let mut netinfo: Option<(msg::Netinfo, coarsetime::Instant)> = None;
265
20
        let mut seen_authchallenge = false;
266
20

            
267
20
        // Loop: reject duplicate and unexpected cells
268
20
        trace!("{}: waiting for rest of handshake.", self.unique_id);
269
48
        while let Some(m) = tls.next().await {
270
            use HandshakeMsg::*;
271
46
            let (_, m) = m.map_err(codec_err_to_handshake)?.into_circid_and_msg();
272
42
            trace!("{}: received a {} cell.", self.unique_id, m.cmd());
273
42
            match m {
274
                // Are these technically allowed?
275
8
                Padding(_) | Vpadding(_) => (),
276
                // Clients don't care about AuthChallenge
277
                AuthChallenge(_) => {
278
8
                    if seen_authchallenge {
279
2
                        return Err(Error::HandshakeProto("Duplicate authchallenge cell".into()));
280
6
                    }
281
6
                    seen_authchallenge = true;
282
                }
283
16
                Certs(c) => {
284
16
                    if certs.is_some() {
285
2
                        return Err(Error::HandshakeProto("Duplicate certs cell".into()));
286
14
                    }
287
14
                    certs = Some(c);
288
                }
289
10
                Netinfo(n) => {
290
10
                    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
10
                    }
297
10
                    netinfo = Some((n, coarsetime::Instant::now()));
298
10
                    break;
299
                }
300
            }
301
        }
302

            
303
        // If we have certs and netinfo, we can finish authenticating.
304
12
        match (certs, netinfo) {
305
2
            (Some(_), None) => Err(Error::HandshakeProto(
306
2
                "Missing netinfo or closed stream".into(),
307
2
            )),
308
2
            (None, _) => Err(Error::HandshakeProto("Missing certs cell".into())),
309
8
            (Some(certs_cell), Some((netinfo_cell, netinfo_rcvd_at))) => {
310
8
                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
8
                let clock_skew = if let Some(netinfo_timestamp) = netinfo_cell.timestamp() {
314
6
                    let delay = netinfo_rcvd_at - versions_flushed_at;
315
6
                    ClockSkew::from_handshake_timestamps(
316
6
                        versions_flushed_wallclock,
317
6
                        netinfo_timestamp,
318
6
                        delay.into(),
319
6
                    )
320
                } else {
321
2
                    ClockSkew::None
322
                };
323
8
                Ok(UnverifiedChannel {
324
8
                    link_protocol,
325
8
                    tls: codec::change_message_types(tls),
326
8
                    certs_cell,
327
8
                    netinfo_cell,
328
8
                    clock_skew,
329
8
                    target_method: self.target_method.take(),
330
8
                    unique_id: self.unique_id,
331
8
                    sleep_prov: self.sleep_prov.clone(),
332
8
                    memquota: self.memquota.clone(),
333
8
                })
334
            }
335
        }
336
24
    }
337
}
338

            
339
impl<
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
8
    pub fn clock_skew(&self) -> ClockSkew {
353
8
        self.clock_skew
354
8
    }
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
2
    pub fn check<U: ChanTarget + ?Sized>(
370
2
        self,
371
2
        peer: &U,
372
2
        peer_cert: &[u8],
373
2
        now: Option<std::time::SystemTime>,
374
2
    ) -> Result<VerifiedChannel<T, S>> {
375
2
        let peer_cert_sha256 = ll::d::Sha256::digest(peer_cert);
376
2
        self.check_internal(peer, &peer_cert_sha256[..], now)
377
2
    }
378

            
379
    /// Same as `check`, but takes the SHA256 hash of the peer certificate,
380
    /// since that is all we use.
381
24
    fn check_internal<U: ChanTarget + ?Sized>(
382
24
        self,
383
24
        peer: &U,
384
24
        peer_cert_sha256: &[u8],
385
24
        now: Option<SystemTime>,
386
24
    ) -> 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
44
        fn check_timeliness<C, T>(checkable: C, now: SystemTime, skew: ClockSkew) -> (Result<()>, T)
398
44
        where
399
44
            C: Timebound<T, Error = TimeValidityError>,
400
44
        {
401
44
            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
44
            });
412
44
            let cert = checkable.dangerously_assume_timely();
413
44
            (status, cert)
414
44
        }
415
        // Replace 'now' with the real time to use.
416
24
        let now = now.unwrap_or_else(SystemTime::now);
417
24

            
418
24
        // We need to check the following lines of authentication:
419
24
        //
420
24
        // First, to bind the ed identity to the channel.
421
24
        //    peer.ed_identity() matches the key in...
422
24
        //    IDENTITY_V_SIGNING cert, which signs...
423
24
        //    SIGNING_V_TLS_CERT cert, which signs peer_cert.
424
24
        //
425
24
        // Second, to bind the rsa identity to the ed identity:
426
24
        //    peer.rsa_identity() matches the key in...
427
24
        //    the x.509 RSA identity certificate (type 2), which signs...
428
24
        //    the RSA->Ed25519 crosscert (type 7), which signs...
429
24
        //    peer.ed_identity().
430
24

            
431
24
        let c = &self.certs_cell;
432
        /// Helper: get a cert from a Certs cell, and convert errors appropriately.
433
114
        fn get_cert(
434
114
            certs: &tor_cell::chancell::msg::Certs,
435
114
            tp: CertType,
436
114
        ) -> Result<tor_cert::KeyUnknownCert> {
437
114
            match certs.parse_ed_cert(tp) {
438
108
                Ok(c) => Ok(c),
439
6
                Err(tor_cell::Error::ChanProto(e)) => Err(Error::HandshakeProto(e)),
440
                Err(e) => Err(Error::HandshakeProto(e.to_string())),
441
            }
442
114
        }
443

            
444
24
        let id_sk = get_cert(c, CertType::IDENTITY_V_SIGNING)?;
445
20
        let sk_tls = get_cert(c, CertType::SIGNING_V_TLS_CERT)?;
446

            
447
18
        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
18
        let (id_sk, id_sk_sig) = id_sk
458
18
            .should_have_signing_key()
459
18
            .map_err(Error::HandshakeCertErr)?
460
18
            .dangerously_split()
461
18
            .map_err(Error::HandshakeCertErr)?;
462
18
        sigs.push(&id_sk_sig);
463
18
        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
18
        let identity_key = id_sk.signing_key().ok_or_else(|| {
467
            Error::HandshakeProto("Missing identity key in identity->signing cert".into())
468
18
        })?;
469

            
470
        // Take the signing key from the identity->signing cert
471
18
        let signing_key = id_sk.subject_key().as_ed25519().ok_or_else(|| {
472
            Error::HandshakeProto("Bad key type in identity->signing cert".into())
473
18
        })?;
474

            
475
        // Now look at the signing->TLS cert and check it against the
476
        // peer certificate.
477
18
        let (sk_tls, sk_tls_sig) = sk_tls
478
18
            .should_be_signed_with(signing_key)
479
18
            .map_err(Error::HandshakeCertErr)?
480
18
            .dangerously_split()
481
18
            .map_err(Error::HandshakeCertErr)?;
482
18
        sigs.push(&sk_tls_sig);
483
18
        let (sk_tls_timeliness, sk_tls) = check_timeliness(sk_tls, now, self.clock_skew);
484
18

            
485
18
        if peer_cert_sha256 != sk_tls.subject_key().as_bytes() {
486
2
            return Err(Error::HandshakeProto(
487
2
                "Peer cert did not authenticate TLS cert".into(),
488
2
            ));
489
16
        }
490
16

            
491
16
        // Batch-verify the ed25519 certificates in this handshake.
492
16
        //
493
16
        // In theory we could build a list of _all_ the certificates here
494
16
        // and call pk::validate_all_sigs() instead, but that doesn't gain
495
16
        // any performance.
496
16
        if !ll::pk::ed25519::validate_batch(&sigs[..]) {
497
2
            return Err(Error::HandshakeProto(
498
2
                "Invalid ed25519 signature in handshake".into(),
499
2
            ));
500
14
        }
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
14
        let pkrsa = c
510
14
            .cert_body(CertType::RSA_ID_X509)
511
14
            .and_then(ll::util::x509_extract_rsa_subject_kludge)
512
14
            .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
12
        let rsa_cert = c
520
12
            .cert_body(CertType::RSA_ID_V_IDENTITY)
521
12
            .ok_or_else(|| Error::HandshakeProto("No RSA->Ed crosscert".into()))?;
522
10
        let rsa_cert = tor_cert::rsa::RsaCrosscert::decode(rsa_cert)
523
10
            .map_err(|e| Error::from_bytes_err(e, "RSA identity cross-certificate"))?
524
10
            .check_signature(&pkrsa)
525
10
            .map_err(|_| Error::HandshakeProto("Bad RSA->Ed crosscert signature".into()))?;
526
8
        let (rsa_cert_timeliness, rsa_cert) = check_timeliness(rsa_cert, now, self.clock_skew);
527
8

            
528
8
        if !rsa_cert.subject_key_matches(identity_key) {
529
            return Err(Error::HandshakeProto(
530
                "RSA->Ed crosscert certifies incorrect key".into(),
531
            ));
532
8
        }
533
8

            
534
8
        let rsa_id = pkrsa.to_rsa_identity();
535
8

            
536
8
        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
8
        let actual_identity = RelayIds::builder()
553
8
            .ed_identity(*identity_key)
554
8
            .rsa_identity(rsa_id)
555
8
            .build()
556
8
            .expect("Unable to build RelayIds");
557
8

            
558
8
        // We enforce that the relay proved that it has every ID that we wanted:
559
8
        // it may also have additional IDs that we didn't ask for.
560
8
        match super::check_id_match_helper(&actual_identity, peer) {
561
4
            Err(Error::ChanMismatch(msg)) => Err(Error::HandshakeProto(msg)),
562
4
            other => other,
563
4
        }?;
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
4
        id_sk_timeliness?;
580
4
        sk_tls_timeliness?;
581
4
        rsa_cert_timeliness?;
582

            
583
4
        Ok(VerifiedChannel {
584
4
            link_protocol: self.link_protocol,
585
4
            tls: self.tls,
586
4
            unique_id: self.unique_id,
587
4
            target_method: self.target_method,
588
4
            ed25519_id: *identity_key,
589
4
            rsa_id,
590
4
            clock_skew: self.clock_skew,
591
4
            sleep_prov: self.sleep_prov,
592
4
            memquota: self.memquota,
593
4
        })
594
24
    }
595
}
596

            
597
impl<
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
4
    pub async fn finish(mut self) -> Result<(Arc<super::Channel>, super::reactor::Reactor<S>)> {
609
4
        // We treat a completed channel -- that is to say, one where the
610
4
        // authentication is finished -- as incoming traffic.
611
4
        //
612
4
        // TODO: conceivably we should remember the time when we _got_ the
613
4
        // final cell on the handshake, and update the channel completion
614
4
        // time to be no earlier than _that_ timestamp.
615
4
        crate::note_incoming_traffic();
616
4
        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
4
        let peer_ip = self
623
4
            .target_method
624
4
            .as_ref()
625
4
            .and_then(ChannelMethod::socket_addrs)
626
4
            .and_then(|addrs| addrs.first())
627
4
            .map(SocketAddr::ip);
628
4
        let netinfo = msg::Netinfo::from_client(peer_ip);
629
4
        self.tls
630
4
            .send(netinfo.into())
631
4
            .await
632
4
            .map_err(codec_err_to_handshake)?;
633

            
634
4
        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
4
        let stream_ops = self.tls.new_handle();
646
4
        let (tls_sink, tls_stream) = self.tls.split();
647
4

            
648
4
        let mut peer_builder = OwnedChanTargetBuilder::default();
649
4
        if let Some(target_method) = self.target_method {
650
4
            if let Some(addrs) = target_method.socket_addrs() {
651
4
                peer_builder.addrs(addrs.to_owned());
652
4
            }
653
4
            peer_builder.method(target_method);
654
        }
655
4
        let peer_id = peer_builder
656
4
            .ed_identity(self.ed25519_id)
657
4
            .rsa_identity(self.rsa_id)
658
4
            .build()
659
4
            .expect("OwnedChanTarget builder failed");
660
4

            
661
4
        super::Channel::new(
662
4
            self.link_protocol,
663
4
            Box::new(tls_sink),
664
4
            Box::new(tls_stream),
665
4
            stream_ops,
666
4
            self.unique_id,
667
4
            peer_id,
668
4
            self.clock_skew,
669
4
            self.sleep_prov,
670
4
            self.memquota,
671
4
        )
672
4
    }
673
}
674

            
675
#[cfg(test)]
676
pub(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,
                certs::PEER_RSA,
                certs::PEER_CERT_DIGEST,
                &rt,
            )
            .err()
            .unwrap();
            assert_eq!(
                format!("{}", res),
                format!("Handshake protocol violation: {}", expect_err.unwrap())
            );
        }
    }
    #[test]
    fn certs_wrongtarget() {
        let rt = PreferredRuntime::create().unwrap();
        let mut certs = msg::Certs::new_empty();
        certs.push_cert_body(2.into(), certs::CERT_T2);
        certs.push_cert_body(5.into(), certs::CERT_T5);
        certs.push_cert_body(7.into(), certs::CERT_T7);
        certs.push_cert_body(4.into(), certs::CERT_T4);
        let err = certs_test(
            certs.clone(),
            Some(cert_timestamp()),
            &[0x10; 32],
            certs::PEER_RSA,
            certs::PEER_CERT_DIGEST,
            &rt,
        )
        .err()
        .unwrap();
        let re = Regex::new(
            // identities might be scrubbed by safelog
            r"Identity .* does not match target .*",
        )
        .unwrap();
        assert!(re.is_match(&format!("{}", err)));
        let err = certs_test(
            certs.clone(),
            Some(cert_timestamp()),
            certs::PEER_ED,
            &[0x99; 20],
            certs::PEER_CERT_DIGEST,
            &rt,
        )
        .err()
        .unwrap();
        let re = Regex::new(
            // identities might be scrubbed by safelog
            r"Identity .* does not match target .*",
        )
        .unwrap();
        assert!(re.is_match(&format!("{}", err)));
        let err = certs_test(
            certs,
            Some(cert_timestamp()),
            certs::PEER_ED,
            certs::PEER_RSA,
            &[0; 32],
            &rt,
        )
        .err()
        .unwrap();
        assert_eq!(
            format!("{}", err),
            "Handshake protocol violation: Peer cert did not authenticate TLS cert"
        );
    }
    #[test]
    fn certs_badsig() {
        let rt = PreferredRuntime::create().unwrap();
        fn munge(inp: &[u8]) -> Vec<u8> {
            let mut v: Vec<u8> = inp.into();
            v[inp.len() - 1] ^= 0x10;
            v
        }
        let mut certs = msg::Certs::new_empty();
        certs.push_cert_body(2.into(), certs::CERT_T2);
        certs.push_cert_body(5.into(), munge(certs::CERT_T5)); // munge an ed signature
        certs.push_cert_body(7.into(), certs::CERT_T7);
        certs.push_cert_body(4.into(), certs::CERT_T4);
        let res = certs_test(
            certs,
            Some(cert_timestamp()),
            certs::PEER_ED,
            certs::PEER_RSA,
            certs::PEER_CERT_DIGEST,
            &rt,
        )
        .err()
        .unwrap();
        assert_eq!(
            format!("{}", res),
            "Handshake protocol violation: Invalid ed25519 signature in handshake"
        );
        let mut certs = msg::Certs::new_empty();
        certs.push_cert_body(2.into(), certs::CERT_T2);
        certs.push_cert_body(5.into(), certs::CERT_T5);
        certs.push_cert_body(7.into(), munge(certs::CERT_T7)); // munge an RSA signature
        certs.push_cert_body(4.into(), certs::CERT_T4);
        let res = certs_test(
            certs,
            Some(cert_timestamp()),
            certs::PEER_ED,
            certs::PEER_RSA,
            certs::PEER_CERT_DIGEST,
            &rt,
        )
        .err()
        .unwrap();
        assert_eq!(
            format!("{}", res),
            "Handshake protocol violation: Bad RSA->Ed crosscert signature"
        );
    }
    /// This module has a few certificates to play with. They're taken
    /// from a chutney network. They match those used in the CERTS
    /// cell test vector in the tor-cell crate.
    ///
    /// The names are taken from the type of the certificate.
    mod certs {
        use hex_literal::hex;
        pub(crate) const CERT_T2: &[u8] = &hex!("308201B930820122A0030201020208607C28BE6C390943300D06092A864886F70D01010B0500301F311D301B06035504030C147777772E74636A76356B766A646472322E636F6D301E170D3230303831303030303030305A170D3231303831303030303030305A301F311D301B06035504030C147777772E74636A76356B766A646472322E636F6D30819F300D06092A864886F70D010101050003818D0030818902818100D38B1E6CEB946E0DB0751F4CBACE3DCB9688B6C25304227B4710C35AFB73627E50500F5913E158B621802612D1C75827003703338375237552EB3CD3C12F6AB3604E60C1A2D26BB1FBAD206FF023969A90909D6A65A5458A5312C26EBD3A3DAD30302D4515CDCD264146AC18E6FC60A04BD3EC327F04294D96BA5AA25B464C3F0203010001300D06092A864886F70D01010B0500038181003BCE561EA7F95CC00B78AAB5D69573FF301C282A751D4A651921D042F1BECDBA24D918A6D8A5E138DC07BBA0B335478AE37ABD2C93A93932442AE9084329E846170FE0FC4A50AAFC804F311CC3CA4F41D845A7BA5901CBBC3E021E9794AAC70CE1F37B0A951592DB1B64F2B4AFB81AE52DBD9B6FEDE96A5FB8125EB6251EE50A");
        pub(crate) const CERT_T4: &[u8] = &hex!("01040006CC2A01F82294B866A31F01FC5D0DA8572850A9B929545C3266558D7D2316E3B74172B00100200400DCB604DB2034B00FD16986D4ADB9D16B21CB4E4457A33DEC0F538903683E96E9FF1A5203FA27F86EF7528D89A0845D2520166E340754FFEA2AAE0F612B7CE5DA094A0236CDAC45034B0B6842C18E7F6B51B93A3CF7E60663B8AD061C30A62602");
        pub(crate) const CERT_T5: &[u8] = &hex!("01050006C98A03B4FD606B64E4CBD466B8D76CB131069BAE6F3AA1878857C9F624E31D77A799B8007173E5F8068431D0D3F5EE16B4C9FFD59DF373E152A87281BAE744AA5FCF72171BF4B27C4E8FC1C6A9FC5CA11058BC49647063D7903CFD9F512F89099B27BC0C");
        pub(crate) const CERT_T7: &[u8] = &hex!("DCB604DB2034B00FD16986D4ADB9D16B21CB4E4457A33DEC0F538903683E96E90006DA3A805CF6006F9179066534DE6B45AD47A5C469063EE462762723396DC9F25452A0A52DA3F5087DD239F2A311F6B0D4DFEFF4ABD089DC3D0237A0ABAB19EB2045B91CDCAF04BE0A72D548A27BF2E77BD876ECFE5E1BE622350DA6BF31F6E306ED896488DD5B39409B23FC3EB7B2C9F7328EB18DA36D54D80575899EA6507CCBFCDF1F");
        pub(crate) const PEER_CERT_DIGEST: &[u8] =
            &hex!("b4fd606b64e4cbd466b8d76cb131069bae6f3aa1878857c9f624e31d77a799b8");
        pub(crate) const PEER_ED: &[u8] =
            &hex!("dcb604db2034b00fd16986d4adb9d16b21cb4e4457a33dec0f538903683e96e9");
        pub(crate) const PEER_RSA: &[u8] = &hex!("2f1fb49bb332a9eec617e41e911c33fb3890aef3");
    }
    #[test]
    fn test_finish() {
        tor_rtcompat::test_with_one_runtime!(|rt| async move {
            let ed25519_id = [3_u8; 32].into();
            let rsa_id = [4_u8; 20].into();
            let peer_addr = "127.1.1.2:443".parse().unwrap();
            let ver = VerifiedChannel {
                link_protocol: 4,
                tls: asynchronous_codec::Framed::new(MsgBuf::new(&b""[..]), ChannelCodec::new(4)),
                unique_id: UniqId::new(),
                target_method: Some(ChannelMethod::Direct(vec![peer_addr])),
                ed25519_id,
                rsa_id,
                clock_skew: ClockSkew::None,
                sleep_prov: rt,
                memquota: fake_mq(),
            };
            let (_chan, _reactor) = ver.finish().await.unwrap();
            // TODO: check contents of netinfo cell
        });
    }
}