1
//! Main implementation of the connection functionality
2

            
3
use std::time::Duration;
4

            
5
use std::collections::HashMap;
6
use std::fmt::Debug;
7
use std::marker::PhantomData;
8
use std::sync::Arc;
9
use std::time::Instant;
10

            
11
use async_trait::async_trait;
12
use educe::Educe;
13
use futures::{AsyncRead, AsyncWrite};
14
use itertools::Itertools;
15
use rand::Rng;
16
use tor_bytes::Writeable;
17
use tor_cell::relaycell::hs::intro_payload::{self, IntroduceHandshakePayload};
18
use tor_cell::relaycell::hs::pow::ProofOfWork;
19
use tor_cell::relaycell::msg::{AnyRelayMsg, Introduce1, Rendezvous2};
20
use tor_circmgr::build::onion_circparams_from_netparams;
21
use tor_circmgr::{
22
    ClientOnionServiceDataTunnel, ClientOnionServiceDirTunnel, ClientOnionServiceIntroTunnel,
23
};
24
use tor_dirclient::SourceInfo;
25
use tor_error::{Bug, debug_report, warn_report};
26
use tor_hscrypto::Subcredential;
27
use tor_proto::TargetHop;
28
use tor_proto::client::circuit::handshake::hs_ntor;
29
use tracing::{debug, instrument, trace};
30

            
31
use retry_error::RetryError;
32
use safelog::{DispRedacted, Sensitive};
33
use tor_cell::relaycell::RelayMsg;
34
use tor_cell::relaycell::hs::{
35
    AuthKeyType, EstablishRendezvous, IntroduceAck, RendezvousEstablished,
36
};
37
use tor_checkable::{Timebound, timed::TimerangeBound};
38
use tor_circmgr::hspool::HsCircPool;
39
use tor_circmgr::timeouts::Action as TimeoutsAction;
40
use tor_dirclient::request::Requestable as _;
41
use tor_error::{HasRetryTime as _, RetryTime};
42
use tor_error::{internal, into_internal};
43
use tor_hscrypto::RendCookie;
44
use tor_hscrypto::pk::{HsBlindId, HsId, HsIdKey};
45
use tor_linkspec::{CircTarget, HasRelayIds, OwnedCircTarget, RelayId};
46
use tor_llcrypto::pk::ed25519::Ed25519Identity;
47
use tor_netdir::{NetDir, Relay};
48
use tor_netdoc::doc::hsdesc::{HsDesc, IntroPointDesc};
49
use tor_proto::client::circuit::CircParameters;
50
use tor_proto::{MetaCellDisposition, MsgHandler};
51
use tor_rtcompat::{Runtime, SleepProviderExt as _, TimeoutError};
52

            
53
use crate::Config;
54
use crate::pow::HsPowClient;
55
use crate::proto_oneshot;
56
use crate::relay_info::ipt_to_circtarget;
57
use crate::state::MockableConnectorData;
58
use crate::{ConnError, DescriptorError, DescriptorErrorDetail};
59
use crate::{FailedAttemptError, IntroPtIndex, RendPtIdentityForError, rend_pt_identity_for_error};
60
use crate::{HsClientConnector, HsClientSecretKeys};
61

            
62
use ConnError as CE;
63
use FailedAttemptError as FAE;
64

            
65
/// Number of hops in our hsdir, introduction, and rendezvous circuits
66
///
67
/// Required by `tor_circmgr`'s timeout estimation API
68
/// ([`tor_circmgr::CircMgr::estimate_timeout`], [`HsCircPool::estimate_timeout`]).
69
///
70
/// TODO HS hardcoding the number of hops to 3 seems wrong.
71
/// This is really something that HsCircPool knows.  And some setups might want to make
72
/// shorter circuits for some reason.  And it will become wrong with vanguards?
73
/// But right now I think this is what HsCircPool does.
74
//
75
// Some commentary from
76
//   https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/1342#note_2918050
77
// Possibilities:
78
//  * Look at n_hops() on the circuits we get, if we don't need this estimate
79
//    till after we have the circuit.
80
//  * Add a function to HsCircPool to tell us what length of circuit to expect
81
//    for each given type of circuit.
82
const HOPS: usize = 3;
83

            
84
/// Given `R, M` where `M: MocksForConnect<M>`, expand to the mockable `ClientCirc`
85
// This is quite annoying.  But the alternative is to write out `<... as // ...>`
86
// each time, since otherwise the compile complains about ambiguous associated types.
87
macro_rules! DataTunnel{ { $R:ty, $M:ty } => {
88
    <<$M as MocksForConnect<$R>>::HsCircPool as MockableCircPool<$R>>::DataTunnel
89
} }
90

            
91
/// Information about a hidden service, including our connection history
92
#[derive(Default, Educe)]
93
#[educe(Debug)]
94
// This type is actually crate-private, since it isn't re-exported, but it must
95
// be `pub` because it appears as a default for a type parameter in HsClientConnector.
96
pub struct Data {
97
    /// The latest known onion service descriptor for this service.
98
    desc: DataHsDesc,
99
    /// Information about the latest status of trying to connect to this service
100
    /// through each of its introduction points.
101
    ipts: DataIpts,
102
}
103

            
104
/// Part of `Data` that relates to the HS descriptor
105
type DataHsDesc = Option<TimerangeBound<HsDesc>>;
106

            
107
/// Part of `Data` that relates to our information about introduction points
108
type DataIpts = HashMap<RelayIdForExperience, IptExperience>;
109

            
110
/// How things went last time we tried to use this introduction point
111
///
112
/// Neither this data structure, nor [`Data`], is responsible for arranging that we expire this
113
/// information eventually.  If we keep reconnecting to the service, we'll retain information
114
/// about each IPT indefinitely, at least so long as they remain listed in the descriptors we
115
/// receive.
116
///
117
/// Expiry of unused data is handled by `state.rs`, according to `last_used` in `ServiceState`.
118
///
119
/// Choosing which IPT to prefer is done by obtaining an `IptSortKey`
120
/// (from this and other information).
121
//
122
// Don't impl Ord for IptExperience.  We obtain `Option<&IptExperience>` from our
123
// data structure, and if IptExperience were Ord then Option<&IptExperience> would be Ord
124
// but it would be the wrong sort order: it would always prefer None, ie untried IPTs.
125
#[derive(Debug)]
126
struct IptExperience {
127
    /// How long it took us to get whatever outcome occurred
128
    ///
129
    /// We prefer fast successes to slow ones.
130
    /// Then, we prefer failures with earlier `RetryTime`,
131
    /// and, lastly, faster failures to slower ones.
132
    duration: Duration,
133

            
134
    /// What happened and when we might try again
135
    ///
136
    /// Note that we don't actually *enforce* the `RetryTime` here, just sort by it
137
    /// using `RetryTime::loose_cmp`.
138
    ///
139
    /// We *do* return an error that is itself `HasRetryTime` and expect our callers
140
    /// to honour that.
141
    outcome: Result<(), RetryTime>,
142
}
143

            
144
/// Actually make a HS connection, updating our recorded state as necessary
145
///
146
/// `connector` is provided only for obtaining the runtime and netdir (and `mock_for_state`).
147
/// Obviously, `connect` is not supposed to go looking in `services`.
148
///
149
/// This function handles all necessary retrying of fallible operations,
150
/// (and, therefore, must also limit the total work done for a particular call).
151
///
152
/// This function has a minimum of functionality, since it is the boundary
153
/// between "mock connection, used for testing `state.rs`" and
154
/// "mock circuit and netdir, used for testing `connect.rs`",
155
/// so it is not, itself, unit-testable.
156
#[instrument(level = "trace", skip_all)]
157
pub(crate) async fn connect<R: Runtime>(
158
    connector: &HsClientConnector<R>,
159
    netdir: Arc<NetDir>,
160
    config: Arc<Config>,
161
    hsid: HsId,
162
    data: &mut Data,
163
    secret_keys: HsClientSecretKeys,
164
) -> Result<ClientOnionServiceDataTunnel, ConnError> {
165
    Context::new(
166
        &connector.runtime,
167
        &*connector.circpool,
168
        netdir,
169
        config,
170
        hsid,
171
        secret_keys,
172
        (),
173
    )?
174
    .connect(data)
175
    .await
176
}
177

            
178
/// Common context for a single request to connect to a hidden service
179
///
180
/// This saves on passing this same set of (immutable) values (or subsets thereof)
181
/// to each method in the principal functional code, everywhere.
182
/// It also provides a convenient type to be `Self`.
183
///
184
/// Its lifetime is one request to make a new client circuit to a hidden service,
185
/// including all the retries and timeouts.
186
struct Context<'c, R: Runtime, M: MocksForConnect<R>> {
187
    /// Runtime
188
    runtime: &'c R,
189
    /// Circpool
190
    circpool: &'c M::HsCircPool,
191
    /// Netdir
192
    //
193
    // TODO holding onto the netdir for the duration of our attempts is not ideal
194
    // but doing better is fairly complicated.  See discussions here:
195
    //   https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/1228#note_2910545
196
    //   https://gitlab.torproject.org/tpo/core/arti/-/issues/884
197
    netdir: Arc<NetDir>,
198
    /// Configuration
199
    config: Arc<Config>,
200
    /// Secret keys to use
201
    secret_keys: HsClientSecretKeys,
202
    /// HS ID
203
    hsid: DispRedacted<HsId>,
204
    /// Blinded HS ID
205
    hs_blind_id: HsBlindId,
206
    /// The subcredential to use during this time period
207
    subcredential: Subcredential,
208
    /// Mock data
209
    mocks: M,
210
}
211

            
212
/// Details of an established rendezvous point
213
///
214
/// Intermediate value for progress during a connection attempt.
215
struct Rendezvous<'r, R: Runtime, M: MocksForConnect<R>> {
216
    /// RPT as a `Relay`
217
    rend_relay: Relay<'r>,
218
    /// Rendezvous circuit
219
    rend_tunnel: DataTunnel!(R, M),
220
    /// Rendezvous cookie
221
    rend_cookie: RendCookie,
222

            
223
    /// Receiver that will give us the RENDEZVOUS2 message.
224
    ///
225
    /// The sending ended is owned by the handler
226
    /// which receives control messages on the rendezvous circuit,
227
    /// and which was installed when we sent `ESTABLISH_RENDEZVOUS`.
228
    ///
229
    /// (`RENDEZVOUS2` is the message containing the onion service's side of the handshake.)
230
    rend2_rx: proto_oneshot::Receiver<Rendezvous2>,
231

            
232
    /// Dummy, to placate compiler
233
    ///
234
    /// Covariant without dropck or interfering with Send/Sync will do fine.
235
    marker: PhantomData<fn() -> (R, M)>,
236
}
237

            
238
/// Random value used as part of IPT selection
239
type IptSortRand = u32;
240

            
241
/// Details of an apparently-useable introduction point
242
///
243
/// Intermediate value for progress during a connection attempt.
244
struct UsableIntroPt<'i> {
245
    /// Index in HS descriptor
246
    intro_index: IntroPtIndex,
247
    /// IPT descriptor
248
    intro_desc: &'i IntroPointDesc,
249
    /// IPT `CircTarget`
250
    intro_target: OwnedCircTarget,
251
    /// Random value used as part of IPT selection
252
    sort_rand: IptSortRand,
253
}
254

            
255
/// Lookup key for looking up and recording our IPT use experiences
256
///
257
/// Used to identify a relay when looking to see what happened last time we used it,
258
/// and storing that information after we tried it.
259
///
260
/// We store the experience information under an arbitrary one of the relay's identities,
261
/// as returned by the `HasRelayIds::identities().next()`.
262
/// When we do lookups, we check all the relay's identities to see if we find
263
/// anything relevant.
264
/// If relay identities permute in strange ways, whether we find our previous
265
/// knowledge about them is not particularly well defined, but that's fine.
266
///
267
/// While this is, structurally, a relay identity, it is not suitable for other purposes.
268
#[derive(Hash, Eq, PartialEq, Ord, PartialOrd, Debug)]
269
struct RelayIdForExperience(RelayId);
270

            
271
/// Details of an apparently-successful INTRODUCE exchange
272
///
273
/// Intermediate value for progress during a connection attempt.
274
struct Introduced<R: Runtime, M: MocksForConnect<R>> {
275
    /// End-to-end crypto NTORv3 handshake with the service
276
    ///
277
    /// Created as part of generating our `INTRODUCE1`,
278
    /// and then used when processing `RENDEZVOUS2`.
279
    handshake_state: hs_ntor::HsNtorClientState,
280

            
281
    /// Dummy, to placate compiler
282
    ///
283
    /// `R` and `M` only used for getting to mocks.
284
    /// Covariant without dropck or interfering with Send/Sync will do fine.
285
    marker: PhantomData<fn() -> (R, M)>,
286
}
287

            
288
impl RelayIdForExperience {
289
    /// Identities to use to try to find previous experience information about this IPT
290
8
    fn for_lookup(intro_target: &OwnedCircTarget) -> impl Iterator<Item = Self> + '_ {
291
8
        intro_target
292
8
            .identities()
293
20
            .map(|id| RelayIdForExperience(id.to_owned()))
294
8
    }
295

            
296
    /// Identity to use to store previous experience information about this IPT
297
    fn for_store(intro_target: &OwnedCircTarget) -> Result<Self, Bug> {
298
        let id = intro_target
299
            .identities()
300
            .next()
301
            .ok_or_else(|| internal!("introduction point relay with no identities"))?
302
            .to_owned();
303
        Ok(RelayIdForExperience(id))
304
    }
305
}
306

            
307
/// Sort key for an introduction point, for selecting the best IPTs to try first
308
///
309
/// Ordering is most preferable first.
310
///
311
/// We use this to sort our `UsableIpt`s using `.sort_by_key`.
312
/// (This implementation approach ensures that we obey all the usual ordering invariants.)
313
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug)]
314
struct IptSortKey {
315
    /// Sort by how preferable the experience was
316
    outcome: IptSortKeyOutcome,
317
    /// Failing that, choose randomly
318
    sort_rand: IptSortRand,
319
}
320

            
321
/// Component of the [`IptSortKey`] representing outcome of our last attempt, if any
322
///
323
/// This is the main thing we use to decide which IPTs to try first.
324
/// It is calculated for each IPT
325
/// (via `.sort_by_key`, so repeatedly - it should therefore be cheap to make.)
326
///
327
/// Ordering is most preferable first.
328
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug)]
329
enum IptSortKeyOutcome {
330
    /// Prefer successes
331
    Success {
332
        /// Prefer quick ones
333
        duration: Duration,
334
    },
335
    /// Failing that, try one we don't know to have failed
336
    Untried,
337
    /// Failing that, it'll have to be ones that didn't work last time
338
    Failed {
339
        /// Prefer failures with an earlier retry time
340
        retry_time: tor_error::LooseCmpRetryTime,
341
        /// Failing that, prefer quick failures (rather than slow ones eg timeouts)
342
        duration: Duration,
343
    },
344
}
345

            
346
impl From<Option<&IptExperience>> for IptSortKeyOutcome {
347
8
    fn from(experience: Option<&IptExperience>) -> IptSortKeyOutcome {
348
        use IptSortKeyOutcome as O;
349
8
        match experience {
350
8
            None => O::Untried,
351
            Some(IptExperience { duration, outcome }) => match outcome {
352
                Ok(()) => O::Success {
353
                    duration: *duration,
354
                },
355
                Err(retry_time) => O::Failed {
356
                    retry_time: (*retry_time).into(),
357
                    duration: *duration,
358
                },
359
            },
360
        }
361
8
    }
362
}
363

            
364
impl<'c, R: Runtime, M: MocksForConnect<R>> Context<'c, R, M> {
365
    /// Make a new `Context` from the input data
366
2
    fn new(
367
2
        runtime: &'c R,
368
2
        circpool: &'c M::HsCircPool,
369
2
        netdir: Arc<NetDir>,
370
2
        config: Arc<Config>,
371
2
        hsid: HsId,
372
2
        secret_keys: HsClientSecretKeys,
373
2
        mocks: M,
374
2
    ) -> Result<Self, ConnError> {
375
2
        let time_period = netdir.hs_time_period();
376
2
        let (hs_blind_id_key, subcredential) = HsIdKey::try_from(hsid)
377
2
            .map_err(|_| CE::InvalidHsId)?
378
2
            .compute_blinded_key(time_period)
379
2
            .map_err(
380
                // TODO HS what on earth do these errors mean, in practical terms ?
381
                // In particular, we'll want to convert them to a ConnError variant,
382
                // but what ErrorKind should they have ?
383
2
                into_internal!("key blinding error, don't know how to handle"),
384
            )?;
385
2
        let hs_blind_id = hs_blind_id_key.id();
386

            
387
2
        Ok(Context {
388
2
            netdir,
389
2
            config,
390
2
            hsid: DispRedacted(hsid),
391
2
            hs_blind_id,
392
2
            subcredential,
393
2
            circpool,
394
2
            runtime,
395
2
            secret_keys,
396
2
            mocks,
397
2
        })
398
2
    }
399

            
400
    /// Actually make a HS connection, updating our recorded state as necessary
401
    ///
402
    /// Called by the `connect` function in this module.
403
    ///
404
    /// This function handles all necessary retrying of fallible operations,
405
    /// (and, therefore, must also limit the total work done for a particular call).
406
    #[instrument(level = "trace", skip_all)]
407
2
    async fn connect(&self, data: &mut Data) -> Result<DataTunnel!(R, M), ConnError> {
408
        // This function must do the following, retrying as appropriate.
409
        //  - Look up the onion descriptor in the state.
410
        //  - Download the onion descriptor if one isn't there.
411
        //  - In parallel:
412
        //    - Pick a rendezvous point from the netdirprovider and launch a
413
        //      rendezvous circuit to it. Then send ESTABLISH_INTRO.
414
        //    - Pick a number of introduction points (1 or more) and try to
415
        //      launch circuits to them.
416
        //  - On a circuit to an introduction point, send an INTRODUCE1 cell.
417
        //  - Wait for a RENDEZVOUS2 cell on the rendezvous circuit
418
        //  - Add a virtual hop to the rendezvous circuit.
419
        //  - Return the rendezvous circuit.
420

            
421
        let mocks = self.mocks.clone();
422

            
423
        let desc = self.descriptor_ensure(&mut data.desc).await?;
424

            
425
        mocks.test_got_desc(desc);
426

            
427
        let tunnel = self.intro_rend_connect(desc, &mut data.ipts).await?;
428
        mocks.test_got_tunnel(&tunnel);
429

            
430
        Ok(tunnel)
431
    }
432

            
433
    /// Ensure that `Data.desc` contains the HS descriptor
434
    ///
435
    /// If we have a previously-downloaded descriptor, which is still valid,
436
    /// just returns a reference to it.
437
    ///
438
    /// Otherwise, tries to obtain the descriptor by downloading it from hsdir(s).
439
    ///
440
    /// Does all necessary retries and timeouts.
441
    /// Returns an error if no valid descriptor could be found.
442
    #[allow(clippy::cognitive_complexity)] // TODO: Refactor
443
    #[instrument(level = "trace", skip_all)]
444
2
    async fn descriptor_ensure<'d>(&self, data: &'d mut DataHsDesc) -> Result<&'d HsDesc, CE> {
445
        // Maximum number of hsdir connection and retrieval attempts we'll make
446
        let max_total_attempts = self
447
            .config
448
            .retry
449
            .hs_desc_fetch_attempts()
450
            .try_into()
451
            // User specified a very large u32.  We must be downcasting it to 16bit!
452
            // let's give them as many retries as we can manage.
453
            .unwrap_or(usize::MAX);
454

            
455
        // Limit on the duration of each retrieval attempt
456
        let each_timeout = self.estimate_timeout(&[
457
            (1, TimeoutsAction::BuildCircuit { length: HOPS }), // build circuit
458
            (1, TimeoutsAction::RoundTrip { length: HOPS }),    // One HTTP query/response
459
        ]);
460

            
461
        // We retain a previously obtained descriptor precisely until its lifetime expires,
462
        // and pay no attention to the descriptor's revision counter.
463
        // When it expires, we discard it completely and try to obtain a new one.
464
        //   https://gitlab.torproject.org/tpo/core/arti/-/issues/913#note_2914448
465
        // TODO SPEC: Discuss HS descriptor lifetime and expiry client behaviour
466
2
        if let Some(previously) = data {
467
            let now = self.runtime.wallclock();
468
            if let Ok(_desc) = previously.as_ref().check_valid_at(&now) {
469
                // Ideally we would just return desc but that confuses borrowck.
470
                // https://github.com/rust-lang/rust/issues/51545
471
                return Ok(data
472
                    .as_ref()
473
                    .expect("Some but now None")
474
                    .as_ref()
475
                    .check_valid_at(&now)
476
                    .expect("Ok but now Err"));
477
            }
478
            // Seems to be not valid now.  Try to fetch a fresh one.
479
        }
480

            
481
        let hs_dirs = self.netdir.hs_dirs_download(
482
            self.hs_blind_id,
483
            self.netdir.hs_time_period(),
484
            &mut self.mocks.thread_rng(),
485
        )?;
486

            
487
        trace!(
488
            "HS desc fetch for {}, using {} hsdirs",
489
            &self.hsid,
490
            hs_dirs.len()
491
        );
492

            
493
        // We might consider launching requests to multiple HsDirs in parallel.
494
        //   https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/1118#note_2894463
495
        // But C Tor doesn't and our HS experts don't consider that important:
496
        //   https://gitlab.torproject.org/tpo/core/arti/-/issues/913#note_2914436
497
        // (Additionally, making multiple HSDir requests at once may make us
498
        // more vulnerable to traffic analysis.)
499
        let mut attempts = hs_dirs.iter().cycle().take(max_total_attempts);
500
        let mut errors = RetryError::in_attempt_to("retrieve hidden service descriptor");
501
        let desc = loop {
502
            let relay = match attempts.next() {
503
                Some(relay) => relay,
504
                None => {
505
                    return Err(if errors.is_empty() {
506
                        CE::NoHsDirs
507
                    } else {
508
                        CE::DescriptorDownload(errors)
509
                    });
510
                }
511
            };
512
            let hsdir_for_error: Sensitive<Ed25519Identity> = (*relay.id()).into();
513
            match self
514
                .runtime
515
                .timeout(each_timeout, self.descriptor_fetch_attempt(relay))
516
                .await
517
                .unwrap_or(Err(DescriptorErrorDetail::Timeout))
518
            {
519
                Ok(desc) => break desc,
520
                Err(error) => {
521
                    if error.should_report_as_suspicious() {
522
                        // Note that not every protocol violation is suspicious:
523
                        // we only warn on the protocol violations that look like attempts
524
                        // to do a traffic tagging attack via hsdir inflation.
525
                        // (See proposal 360.)
526
                        warn_report!(
527
                            &error,
528
                            "Suspicious failure while downloading hsdesc for {} from relay {}",
529
                            &self.hsid,
530
                            relay.display_relay_ids(),
531
                        );
532
                    } else {
533
                        debug_report!(
534
                            &error,
535
                            "failed hsdir desc fetch for {} from {}/{}",
536
                            &self.hsid,
537
                            &relay.id(),
538
                            &relay.rsa_id()
539
                        );
540
                    }
541
                    errors.push_timed(
542
                        tor_error::Report(DescriptorError {
543
                            hsdir: hsdir_for_error,
544
                            error,
545
                        }),
546
                        self.runtime.now(),
547
                        Some(self.runtime.wallclock()),
548
                    );
549
                }
550
            }
551
        };
552

            
553
        // Store the bounded value in the cache for reuse,
554
        // but return a reference to the unwrapped `HsDesc`.
555
        //
556
        // The `HsDesc` must be owned by `data.desc`,
557
        // so first add it to `data.desc`,
558
        // and then dangerously_assume_timely to get a reference out again.
559
        //
560
        // It is safe to dangerously_assume_timely,
561
        // as descriptor_fetch_attempt has already checked the timeliness of the descriptor.
562
        let ret = data.insert(desc);
563
        Ok(ret.as_ref().dangerously_assume_timely())
564
2
    }
565

            
566
    /// Make one attempt to fetch the descriptor from a specific hsdir
567
    ///
568
    /// No timeout
569
    ///
570
    /// On success, returns the descriptor.
571
    ///
572
    /// While the returned descriptor is `TimerangeBound`, its validity at the current time *has*
573
    /// been checked.
574
    #[instrument(level = "trace", skip_all)]
575
2
    async fn descriptor_fetch_attempt(
576
2
        &self,
577
2
        hsdir: &Relay<'_>,
578
2
    ) -> Result<TimerangeBound<HsDesc>, DescriptorErrorDetail> {
579
        let max_len: usize = self
580
            .netdir
581
            .params()
582
            .hsdir_max_desc_size
583
            .get()
584
            .try_into()
585
            .map_err(into_internal!("BoundedInt was not truly bounded!"))?;
586
        let request = {
587
            let mut r = tor_dirclient::request::HsDescDownloadRequest::new(self.hs_blind_id);
588
            r.set_max_len(max_len);
589
            r
590
        };
591
        trace!(
592
            "hsdir for {}, trying {}/{}, request {:?} (http request {:?})",
593
            &self.hsid,
594
            &hsdir.id(),
595
            &hsdir.rsa_id(),
596
            &request,
597
            request.debug_request()
598
        );
599

            
600
        let circuit = self
601
            .circpool
602
            .m_get_or_launch_dir(&self.netdir, OwnedCircTarget::from_circ_target(hsdir))
603
            .await?;
604
        let source: Option<SourceInfo> = circuit
605
            .m_source_info()
606
            .map_err(into_internal!("Couldn't get SourceInfo for circuit"))?;
607
        let mut stream = circuit
608
            .m_begin_dir_stream()
609
            .await
610
            .map_err(DescriptorErrorDetail::Circuit)?;
611

            
612
        let response = tor_dirclient::send_request(self.runtime, &request, &mut stream, source)
613
            .await
614
            .map_err(|dir_error| match dir_error {
615
                tor_dirclient::Error::RequestFailed(rfe) => DescriptorErrorDetail::from(rfe.error),
616
                tor_dirclient::Error::CircMgr(ce) => into_internal!(
617
                    "tor-dirclient complains about circmgr going wrong but we gave it a stream"
618
                )(ce)
619
                .into(),
620
                other => into_internal!(
621
                    "tor-dirclient gave unexpected error, tor-hsclient code needs updating"
622
                )(other)
623
                .into(),
624
            })?;
625

            
626
        let desc_text = response.into_output_string().map_err(|rfe| rfe.error)?;
627
        let hsc_desc_enc = self.secret_keys.keys.ks_hsc_desc_enc.as_ref();
628

            
629
        let now = self.runtime.wallclock();
630

            
631
        HsDesc::parse_decrypt_validate(
632
            &desc_text,
633
            &self.hs_blind_id,
634
            now,
635
            &self.subcredential,
636
            hsc_desc_enc,
637
        )
638
        .map_err(DescriptorErrorDetail::from)
639
2
    }
640

            
641
    /// Given the descriptor, try to connect to service
642
    ///
643
    /// Does all necessary retries, timeouts, etc.
644
2
    async fn intro_rend_connect(
645
2
        &self,
646
2
        desc: &HsDesc,
647
2
        data: &mut DataIpts,
648
2
    ) -> Result<DataTunnel!(R, M), CE> {
649
        // Maximum number of rendezvous/introduction attempts we'll make
650
2
        let max_total_attempts = self
651
2
            .config
652
2
            .retry
653
2
            .hs_intro_rend_attempts()
654
2
            .try_into()
655
            // User specified a very large u32.  We must be downcasting it to 16bit!
656
            // let's give them as many retries as we can manage.
657
2
            .unwrap_or(usize::MAX);
658

            
659
        // Limit on the duration of each attempt to establish a rendezvous point
660
        //
661
        // This *might* include establishing a fresh circuit,
662
        // if the HsCircPool's pool is empty.
663
2
        let rend_timeout = self.estimate_timeout(&[
664
2
            (1, TimeoutsAction::BuildCircuit { length: HOPS }), // build circuit
665
2
            (1, TimeoutsAction::RoundTrip { length: HOPS }),    // One ESTABLISH_RENDEZVOUS
666
2
        ]);
667

            
668
        // Limit on the duration of each attempt to negotiate with an introduction point
669
        //
670
        // *Does* include establishing the circuit.
671
2
        let intro_timeout = self.estimate_timeout(&[
672
2
            (1, TimeoutsAction::BuildCircuit { length: HOPS }), // build circuit
673
2
            // This does some crypto too, but we don't account for that.
674
2
            (1, TimeoutsAction::RoundTrip { length: HOPS }), // One INTRODUCE1/INTRODUCE_ACK
675
2
        ]);
676

            
677
        // Timeout estimator for the action that the HS will take in building
678
        // its circuit to the RPT.
679
2
        let hs_build_action = TimeoutsAction::BuildCircuit {
680
2
            length: if desc.is_single_onion_service() {
681
                1
682
            } else {
683
2
                HOPS
684
            },
685
        };
686
        // Limit on the duration of each attempt for activities involving both
687
        // RPT and IPT.
688
2
        let rpt_ipt_timeout = self.estimate_timeout(&[
689
2
            // The API requires us to specify a number of circuit builds and round trips.
690
2
            // So what we tell the estimator is a rather imprecise description.
691
2
            // (TODO it would be nice if the circmgr offered us a one-way trip Action).
692
2
            //
693
2
            // What we are timing here is:
694
2
            //
695
2
            //    INTRODUCE2 goes from IPT to HS
696
2
            //    but that happens in parallel with us waiting for INTRODUCE_ACK,
697
2
            //    which is controlled by `intro_timeout` so not pat of `ipt_rpt_timeout`.
698
2
            //    and which has to come HOPS hops.  So don't count INTRODUCE2 here.
699
2
            //
700
2
            //    HS builds to our RPT
701
2
            (1, hs_build_action),
702
2
            //
703
2
            //    RENDEZVOUS1 goes from HS to RPT.  `hs_hops`, one-way.
704
2
            //    RENDEZVOUS2 goes from RPT to us.  HOPS, one-way.
705
2
            //    Together, we squint a bit and call this a HOPS round trip:
706
2
            (1, TimeoutsAction::RoundTrip { length: HOPS }),
707
2
        ]);
708

            
709
        // We can't reliably distinguish IPT failure from RPT failure, so we iterate over IPTs
710
        // (best first) and each time use a random RPT.
711

            
712
        // We limit the number of rendezvous establishment attempts, separately, since we don't
713
        // try to talk to the intro pt until we've established the rendezvous circuit.
714
2
        let mut rend_attempts = 0..max_total_attempts;
715

            
716
        // But, we put all the errors into the same bucket, since we might have a mixture.
717
2
        let mut errors = RetryError::in_attempt_to("make circuit to hidden service");
718

            
719
        // Note that IntroPtIndex is *not* the index into this Vec.
720
        // It is the index into the original list of introduction points in the descriptor.
721
2
        let mut usable_intros: Vec<UsableIntroPt> = desc
722
2
            .intro_points()
723
2
            .iter()
724
2
            .enumerate()
725
6
            .map(|(intro_index, intro_desc)| {
726
6
                let intro_index = intro_index.into();
727
6
                let intro_target = ipt_to_circtarget(intro_desc, &self.netdir)
728
6
                    .map_err(|error| FAE::UnusableIntro { error, intro_index })?;
729
                // Lack of TAIT means this clone
730
6
                let intro_target = OwnedCircTarget::from_circ_target(&intro_target);
731
6
                Ok::<_, FailedAttemptError>(UsableIntroPt {
732
6
                    intro_index,
733
6
                    intro_desc,
734
6
                    intro_target,
735
6
                    sort_rand: self.mocks.thread_rng().random(),
736
6
                })
737
6
            })
738
6
            .filter_map(|entry| match entry {
739
6
                Ok(y) => Some(y),
740
                Err(e) => {
741
                    errors.push_timed(e, self.runtime.now(), Some(self.runtime.wallclock()));
742
                    None
743
                }
744
6
            })
745
2
            .collect_vec();
746

            
747
        // Delete experience information for now-unlisted intro points
748
        // Otherwise, as the IPTs change `Data` might grow without bound,
749
        // if we keep reconnecting to the same HS.
750
2
        data.retain(|k, _v| {
751
            usable_intros
752
                .iter()
753
                .any(|ipt| RelayIdForExperience::for_lookup(&ipt.intro_target).any(|id| &id == k))
754
        });
755

            
756
        // Join with existing state recording our experiences,
757
        // sort by descending goodness, and then randomly
758
        // (so clients without any experience don't all pile onto the same, first, IPT)
759
8
        usable_intros.sort_by_key(|ipt: &UsableIntroPt| {
760
8
            let experience =
761
16
                RelayIdForExperience::for_lookup(&ipt.intro_target).find_map(|id| data.get(&id));
762
8
            IptSortKey {
763
8
                outcome: experience.into(),
764
8
                sort_rand: ipt.sort_rand,
765
8
            }
766
8
        });
767
2
        self.mocks.test_got_ipts(&usable_intros);
768

            
769
2
        let mut intro_attempts = usable_intros.iter().cycle().take(max_total_attempts);
770

            
771
        // We retain a rendezvous we managed to set up in here.  That way if we created it, and
772
        // then failed before we actually needed it, we can reuse it.
773
        // If we exit with an error, we will waste it - but because we isolate things we do
774
        // for different services, it wouldn't be reusable anyway.
775
2
        let mut saved_rendezvous = None;
776

            
777
        // If we are using proof-of-work DoS mitigation, this chooses an
778
        // algorithm and initial effort, and adjusts that effort when we retry.
779
2
        let mut pow_client = HsPowClient::new(&self.hs_blind_id, desc);
780

            
781
        // We might consider making multiple INTRODUCE attempts to different
782
        // IPTs in in parallel, and somehow aggregating the errors and
783
        // experiences.
784
        // However our HS experts don't consider that important:
785
        //   https://gitlab.torproject.org/tpo/core/arti/-/issues/913#note_2914438
786
        // Parallelizing our HsCircPool circuit building would likely have
787
        // greater impact. (See #1149.)
788
        loop {
789
            // When did we start doing things that depended on the IPT?
790
            //
791
            // Used for recording our experience with the selected IPT
792
2
            let mut ipt_use_started = None::<Instant>;
793

            
794
            // Error handling inner async block (analogous to an IEFE):
795
            //  * Ok(Some()) means this attempt succeeded
796
            //  * Ok(None) means all attempts exhausted
797
            //  * Err(error) means this attempt failed
798
            //
799
            // Error handling is rather complex here.  It's the primary job of *this* code to
800
            // make sure that it's done right for timeouts.  (The individual component
801
            // functions handle non-timeout errors.)  The different timeout errors have
802
            // different amounts of information about the identity of the RPT and IPT: in each
803
            // case, the error only mentions the RPT or IPT if that node is implicated in the
804
            // timeout.
805
2
            let outcome = async {
806
                // We establish a rendezvous point first.  Although it appears from reading
807
                // this code that this means we serialise establishment of the rendezvous and
808
                // introduction circuits, this isn't actually the case.  The circmgr maintains
809
                // a pool of circuits.  What actually happens in the "standing start" case is
810
                // that we obtain a circuit for rendezvous from the circmgr's pool, expecting
811
                // one to be available immediately; the circmgr will then start to build a new
812
                // one to replenish its pool, and that happens in parallel with the work we do
813
                // here - but in arrears.  If the circmgr pool is empty, then we must wait.
814
                //
815
                // Perhaps this should be parallelised here.  But that's really what the pool
816
                // is for, since we expect building the rendezvous circuit and building the
817
                // introduction circuit to take about the same length of time.
818
                //
819
                // We *do* serialise the ESTABLISH_RENDEZVOUS exchange, with the
820
                // building of the introduction circuit.  That could be improved, at the cost
821
                // of some additional complexity here.
822
                //
823
                // Our HS experts don't consider it important to increase the parallelism:
824
                //   https://gitlab.torproject.org/tpo/core/arti/-/issues/913#note_2914444
825
                //   https://gitlab.torproject.org/tpo/core/arti/-/issues/913#note_2914445
826
2
                if saved_rendezvous.is_none() {
827
2
                    debug!("hs conn to {}: setting up rendezvous point", &self.hsid);
828
                    // Establish a rendezvous circuit.
829
2
                    let Some(_): Option<usize> = rend_attempts.next() else {
830
                        return Ok(None);
831
                    };
832

            
833
2
                    let mut using_rend_pt = None;
834
                    saved_rendezvous = Some(
835
2
                        self.runtime
836
2
                            .timeout(rend_timeout, self.establish_rendezvous(&mut using_rend_pt))
837
2
                            .await
838
                            .map_err(|_: TimeoutError| match using_rend_pt {
839
                                None => FAE::RendezvousCircuitObtain {
840
                                    error: tor_circmgr::Error::CircTimeout(None),
841
                                },
842
                                Some(rend_pt) => FAE::RendezvousEstablishTimeout { rend_pt },
843
                            })??,
844
                    );
845
                }
846

            
847
                let Some(ipt) = intro_attempts.next() else {
848
                    return Ok(None);
849
                };
850
                let intro_index = ipt.intro_index;
851

            
852
                let proof_of_work = match pow_client.solve().await {
853
                    Ok(solution) => solution,
854
                    Err(e) => {
855
                        debug!(
856
                            "failing to compute proof-of-work, trying without. ({:?})",
857
                            e
858
                        );
859
                        None
860
                    }
861
                };
862

            
863
                // We record how long things take, starting from here, as
864
                // as a statistic we'll use for the IPT in future.
865
                // This is stored in a variable outside this async block,
866
                // so that the outcome handling can use it.
867
                ipt_use_started = Some(self.runtime.now());
868

            
869
                // No `Option::get_or_try_insert_with`, or we'd avoid this expect()
870
                let rend_pt_for_error = rend_pt_identity_for_error(
871
                    &saved_rendezvous
872
                        .as_ref()
873
                        .expect("just made Some")
874
                        .rend_relay,
875
                );
876
                debug!(
877
                    "hs conn to {}: RPT {}",
878
                    &self.hsid,
879
                    rend_pt_for_error.as_inner()
880
                );
881

            
882
                let (rendezvous, introduced) = self
883
                    .runtime
884
                    .timeout(
885
                        intro_timeout,
886
                        self.exchange_introduce(ipt, &mut saved_rendezvous,
887
                            proof_of_work),
888
                    )
889
                    .await
890
                    .map_err(|_: TimeoutError| {
891
                        // The intro point ought to give us a prompt ACK regardless of HS
892
                        // behaviour or whatever is happening at the RPT, so blame the IPT.
893
                        FAE::IntroductionTimeout { intro_index }
894
                    })?
895
                    // TODO: Maybe try, once, to extend-and-reuse the intro circuit.
896
                    //
897
                    // If the introduction fails, the introduction circuit is in principle
898
                    // still usable.  We believe that in this case, C Tor extends the intro
899
                    // circuit by one hop to the next IPT to try.  That saves on building a
900
                    // whole new 3-hop intro circuit.  However, our HS experts tell us that
901
                    // if introduction fails at one IPT it is likely to fail at the others too,
902
                    // so that optimisation might reduce our network impact and time to failure,
903
                    // but isn't likely to improve our chances of success.
904
                    //
905
                    // However, it's not clear whether this approach risks contaminating
906
                    // the 2nd attempt with some fault relating to the introduction point.
907
                    // The 1st ipt might also gain more knowledge about which HS we're talking to.
908
                    //
909
                    // TODO SPEC: Discuss extend-and-reuse HS intro circuit after nack
910
                    ?;
911
                #[allow(unused_variables)] // it's *supposed* to be unused
912
                let saved_rendezvous = (); // don't use `saved_rendezvous` any more, use rendezvous
913

            
914
                let rend_pt = rend_pt_identity_for_error(&rendezvous.rend_relay);
915
                let circ = self
916
                    .runtime
917
                    .timeout(
918
                        rpt_ipt_timeout,
919
                        self.complete_rendezvous(ipt, rendezvous, introduced),
920
                    )
921
                    .await
922
                    .map_err(|_: TimeoutError| FAE::RendezvousCompletionTimeout {
923
                        intro_index,
924
                        rend_pt: rend_pt.clone(),
925
                    })??;
926

            
927
                debug!(
928
                    "hs conn to {}: RPT {} IPT {}: success",
929
                    &self.hsid,
930
                    rend_pt.as_inner(),
931
                    intro_index,
932
                );
933
                Ok::<_, FAE>(Some((intro_index, circ)))
934
            }
935
2
            .await;
936

            
937
            // Store the experience `outcome` we had with IPT `intro_index`, in `data`
938
            #[allow(clippy::unused_unit)] // -> () is here for error handling clarity
939
            let mut store_experience = |intro_index, outcome| -> () {
940
                (|| {
941
                    let ipt = usable_intros
942
                        .iter()
943
                        .find(|ipt| ipt.intro_index == intro_index)
944
                        .ok_or_else(|| internal!("IPT not found by index"))?;
945
                    let id = RelayIdForExperience::for_store(&ipt.intro_target)?;
946
                    let started = ipt_use_started.ok_or_else(|| {
947
                        internal!("trying to record IPT use but no IPT start time noted")
948
                    })?;
949
                    let duration = self
950
                        .runtime
951
                        .now()
952
                        .checked_duration_since(started)
953
                        .ok_or_else(|| internal!("clock overflow calculating IPT use duration"))?;
954
                    data.insert(id, IptExperience { duration, outcome });
955
                    Ok::<_, Bug>(())
956
                })()
957
                .unwrap_or_else(|e| warn_report!(e, "error recording HS IPT use experience"));
958
            };
959

            
960
            match outcome {
961
                Ok(Some((intro_index, y))) => {
962
                    // Record successful outcome in Data
963
                    store_experience(intro_index, Ok(()));
964
                    return Ok(y);
965
                }
966
                Ok(None) => return Err(CE::Failed(errors)),
967
                Err(error) => {
968
                    debug_report!(&error, "hs conn to {}: attempt failed", &self.hsid);
969
                    // Record error outcome in Data, if in fact we involved the IPT
970
                    // at all.  The IPT information is be retrieved from `error`,
971
                    // since only some of the errors implicate the introduction point.
972
                    if let Some(intro_index) = error.intro_index() {
973
                        store_experience(intro_index, Err(error.retry_time()));
974
                    }
975
                    errors.push_timed(error, self.runtime.now(), Some(self.runtime.wallclock()));
976

            
977
                    // If we are using proof-of-work DoS mitigation, try harder next time
978
                    pow_client.increase_effort();
979
                }
980
            }
981
        }
982
    }
983

            
984
    /// Make one attempt to establish a rendezvous circuit
985
    ///
986
    /// This doesn't really depend on anything,
987
    /// other than (obviously) the isolation implied by our circuit pool.
988
    /// In particular it doesn't depend on the introduction point.
989
    ///
990
    /// Does not apply a timeout.
991
    ///
992
    /// On entry `using_rend_pt` is `None`.
993
    /// This function will store `Some` when it finds out which relay
994
    /// it is talking to and starts to converse with it.
995
    /// That way, if a timeout occurs, the caller can add that information to the error.
996
    #[instrument(level = "trace", skip_all)]
997
2
    async fn establish_rendezvous(
998
2
        &'c self,
999
2
        using_rend_pt: &mut Option<RendPtIdentityForError>,
2
    ) -> Result<Rendezvous<'c, R, M>, FAE> {
        let (rend_tunnel, rend_relay) = self
            .circpool
            .m_get_or_launch_client_rend(&self.netdir)
            .await
            .map_err(|error| FAE::RendezvousCircuitObtain { error })?;
        let rend_pt = rend_pt_identity_for_error(&rend_relay);
        *using_rend_pt = Some(rend_pt.clone());
        let rend_cookie: RendCookie = self.mocks.thread_rng().random();
        let message = EstablishRendezvous::new(rend_cookie);
        let (rend_established_tx, rend_established_rx) = proto_oneshot::channel();
        let (rend2_tx, rend2_rx) = proto_oneshot::channel();
        /// Handler which expects `RENDEZVOUS_ESTABLISHED` and then
        /// `RENDEZVOUS2`.   Returns each message via the corresponding `oneshot`.
        struct Handler {
            /// Sender for a RENDEZVOUS_ESTABLISHED message.
            rend_established_tx: proto_oneshot::Sender<RendezvousEstablished>,
            /// Sender for a RENDEZVOUS2 message.
            rend2_tx: proto_oneshot::Sender<Rendezvous2>,
        }
        impl MsgHandler for Handler {
            fn handle_msg(
                &mut self,
                msg: AnyRelayMsg,
            ) -> Result<MetaCellDisposition, tor_proto::Error> {
                // The first message we expect is a RENDEZVOUS_ESTABALISHED.
                if self.rend_established_tx.still_expected() {
                    self.rend_established_tx
                        .deliver_expected_message(msg, MetaCellDisposition::Consumed)
                } else {
                    self.rend2_tx
                        .deliver_expected_message(msg, MetaCellDisposition::ConversationFinished)
                }
            }
        }
        debug!(
            "hs conn to {}: RPT {}: sending ESTABLISH_RENDEZVOUS",
            &self.hsid,
            rend_pt.as_inner(),
        );
        let failed_map_err = |error| FAE::RendezvousEstablish {
            error,
            rend_pt: rend_pt.clone(),
        };
        let handler = Handler {
            rend_established_tx,
            rend2_tx,
        };
        // TODO(conflux) This error handling is horrible. Problem is that this Mock system requires
        // to send back a tor_circmgr::Error while our reply handler requires a tor_proto::Error.
        // And unifying both is hard here considering it needs to be converted to yet another Error
        // type "FAE" so we have to do these hoops and jumps.
        rend_tunnel
            .m_start_conversation_last_hop(Some(message.into()), handler)
            .await
            .map_err(|e| {
                let proto_error = match e {
                    tor_circmgr::Error::Protocol { error, .. } => error,
                    _ => tor_proto::Error::CircuitClosed,
                };
                FAE::RendezvousEstablish {
                    error: proto_error,
                    rend_pt: rend_pt.clone(),
                }
            })?;
        // `start_conversation` returns as soon as the control message has been sent.
        // We need to obtain the RENDEZVOUS_ESTABLISHED message, which is "returned" via the oneshot.
        let _: RendezvousEstablished = rend_established_rx.recv(failed_map_err).await?;
        debug!(
            "hs conn to {}: RPT {}: got RENDEZVOUS_ESTABLISHED",
            &self.hsid,
            rend_pt.as_inner(),
        );
        Ok(Rendezvous {
            rend_tunnel,
            rend_cookie,
            rend_relay,
            rend2_rx,
            marker: PhantomData,
        })
    }
    /// Attempt (once) to send an INTRODUCE1 and wait for the INTRODUCE_ACK
    ///
    /// `take`s the input `rendezvous` (but only takes it if it gets that far)
    /// and, if successful, returns it.
    /// (This arranges that the rendezvous is "used up" precisely if
    /// we sent its secret somewhere.)
    ///
    /// Although this function handles the `Rendezvous`,
    /// nothing in it actually involves the rendezvous point.
    /// So if there's a failure, it's purely to do with the introduction point.
    ///
    /// Does not apply a timeout.
    #[allow(clippy::cognitive_complexity, clippy::type_complexity)] // TODO: Refactor
    #[instrument(level = "trace", skip_all)]
    async fn exchange_introduce(
        &'c self,
        ipt: &UsableIntroPt<'_>,
        rendezvous: &mut Option<Rendezvous<'c, R, M>>,
        proof_of_work: Option<ProofOfWork>,
    ) -> Result<(Rendezvous<'c, R, M>, Introduced<R, M>), FAE> {
        let intro_index = ipt.intro_index;
        debug!(
            "hs conn to {}: IPT {}: obtaining intro circuit",
            &self.hsid, intro_index,
        );
        let intro_circ = self
            .circpool
            .m_get_or_launch_intro(
                &self.netdir,
                ipt.intro_target.clone(), // &OwnedCircTarget isn't CircTarget apparently
            )
            .await
            .map_err(|error| FAE::IntroductionCircuitObtain { error, intro_index })?;
        let rendezvous = rendezvous.take().ok_or_else(|| internal!("no rend"))?;
        let rend_pt = rend_pt_identity_for_error(&rendezvous.rend_relay);
        debug!(
            "hs conn to {}: RPT {} IPT {}: making introduction",
            &self.hsid,
            rend_pt.as_inner(),
            intro_index,
        );
        // Now we construct an introduce1 message and perform the first part of the
        // rendezvous handshake.
        //
        // This process is tricky because the header of the INTRODUCE1 message
        // -- which depends on the IntroPt configuration -- is authenticated as
        // part of the HsDesc handshake.
        // Construct the header, since we need it as input to our encryption.
        let intro_header = {
            let ipt_sid_key = ipt.intro_desc.ipt_sid_key();
            let intro1 = Introduce1::new(
                AuthKeyType::ED25519_SHA3_256,
                ipt_sid_key.as_bytes().to_vec(),
                vec![],
            );
            let mut header = vec![];
            intro1
                .encode_onto(&mut header)
                .map_err(into_internal!("couldn't encode intro1 header"))?;
            header
        };
        // Construct the introduce payload, which tells the onion service how to find
        // our rendezvous point.  (We could do this earlier if we wanted.)
        let intro_payload = {
            let onion_key =
                intro_payload::OnionKey::NtorOnionKey(*rendezvous.rend_relay.ntor_onion_key());
            let linkspecs = rendezvous
                .rend_relay
                .linkspecs()
                .map_err(into_internal!("Couldn't encode link specifiers"))?;
            let payload = IntroduceHandshakePayload::new(
                rendezvous.rend_cookie,
                onion_key,
                linkspecs,
                proof_of_work,
            );
            let mut encoded = vec![];
            payload
                .write_onto(&mut encoded)
                .map_err(into_internal!("Couldn't encode introduce1 payload"))?;
            encoded
        };
        // Perform the cryptographic handshake with the onion service.
        let service_info = hs_ntor::HsNtorServiceInfo::new(
            ipt.intro_desc.svc_ntor_key().clone(),
            ipt.intro_desc.ipt_sid_key().clone(),
            self.subcredential,
        );
        let handshake_state =
            hs_ntor::HsNtorClientState::new(&mut self.mocks.thread_rng(), service_info);
        let encrypted_body = handshake_state
            .client_send_intro(&intro_header, &intro_payload)
            .map_err(into_internal!("can't begin hs-ntor handshake"))?;
        // Build our actual INTRODUCE1 message.
        let intro1_real = Introduce1::new(
            AuthKeyType::ED25519_SHA3_256,
            ipt.intro_desc.ipt_sid_key().as_bytes().to_vec(),
            encrypted_body,
        );
        /// Handler which expects just `INTRODUCE_ACK`
        struct Handler {
            /// Sender for `INTRODUCE_ACK`
            intro_ack_tx: proto_oneshot::Sender<IntroduceAck>,
        }
        impl MsgHandler for Handler {
            fn handle_msg(
                &mut self,
                msg: AnyRelayMsg,
            ) -> Result<MetaCellDisposition, tor_proto::Error> {
                self.intro_ack_tx
                    .deliver_expected_message(msg, MetaCellDisposition::ConversationFinished)
            }
        }
        let failed_map_err = |error| FAE::IntroductionExchange { error, intro_index };
        let (intro_ack_tx, intro_ack_rx) = proto_oneshot::channel();
        let handler = Handler { intro_ack_tx };
        debug!(
            "hs conn to {}: RPT {} IPT {}: making introduction - sending INTRODUCE1",
            &self.hsid,
            rend_pt.as_inner(),
            intro_index,
        );
        // TODO(conflux) This error handling is horrible. Problem is that this Mock system requires
        // to send back a tor_circmgr::Error while our reply handler requires a tor_proto::Error.
        // And unifying both is hard here considering it needs to be converted to yet another Error
        // type "FAE" so we have to do these hoops and jumps.
        intro_circ
            .m_start_conversation_last_hop(Some(intro1_real.into()), handler)
            .await
            .map_err(|e| {
                let proto_error = match e {
                    tor_circmgr::Error::Protocol { error, .. } => error,
                    _ => tor_proto::Error::CircuitClosed,
                };
                FAE::IntroductionExchange {
                    error: proto_error,
                    intro_index,
                }
            })?;
        // Status is checked by `.success()`, and we don't look at the extensions;
        // just discard the known-successful `IntroduceAck`
        let _: IntroduceAck =
            intro_ack_rx
                .recv(failed_map_err)
                .await?
                .success()
                .map_err(|status| FAE::IntroductionFailed {
                    status,
                    intro_index,
                })?;
        debug!(
            "hs conn to {}: RPT {} IPT {}: making introduction - success",
            &self.hsid,
            rend_pt.as_inner(),
            intro_index,
        );
        // Having received INTRODUCE_ACK. we can forget about this circuit
        // (and potentially tear it down).
        drop(intro_circ);
        Ok((
            rendezvous,
            Introduced {
                handshake_state,
                marker: PhantomData,
            },
        ))
    }
    /// Attempt (once) to connect a rendezvous circuit using the given intro pt
    ///
    /// Timeouts here might be due to the IPT, RPT, service,
    /// or any of the intermediate relays.
    ///
    /// If, rather than a timeout, we actually encounter some kind of error,
    /// we'll return the appropriate `FailedAttemptError`.
    /// (Who is responsible may vary, so the `FailedAttemptError` variant will reflect that.)
    ///
    /// Does not apply a timeout
    async fn complete_rendezvous(
        &'c self,
        ipt: &UsableIntroPt<'_>,
        rendezvous: Rendezvous<'c, R, M>,
        introduced: Introduced<R, M>,
    ) -> Result<DataTunnel!(R, M), FAE> {
        use tor_proto::client::circuit::handshake;
        let rend_pt = rend_pt_identity_for_error(&rendezvous.rend_relay);
        let intro_index = ipt.intro_index;
        let failed_map_err = |error| FAE::RendezvousCompletionCircuitError {
            error,
            intro_index,
            rend_pt: rend_pt.clone(),
        };
        debug!(
            "hs conn to {}: RPT {} IPT {}: awaiting rendezvous completion",
            &self.hsid,
            rend_pt.as_inner(),
            intro_index,
        );
        let rend2_msg: Rendezvous2 = rendezvous.rend2_rx.recv(failed_map_err).await?;
        debug!(
            "hs conn to {}: RPT {} IPT {}: received RENDEZVOUS2",
            &self.hsid,
            rend_pt.as_inner(),
            intro_index,
        );
        // In theory would be great if we could have multiple introduction attempts in parallel
        // with similar x,X values but different IPTs.  However, our HS experts don't
        // think increasing parallelism here is important:
        //   https://gitlab.torproject.org/tpo/core/arti/-/issues/913#note_2914438
        let handshake_state = introduced.handshake_state;
        // Try to complete the cryptographic handshake.
        let keygen = handshake_state
            .client_receive_rend(rend2_msg.handshake_info())
            // If this goes wrong. either the onion service has mangled the crypto,
            // or the rendezvous point has misbehaved (that that is possible is a protocol bug),
            // or we have used the wrong handshake_state (let's assume that's not true).
            //
            // If this happens we'll go and try another RPT.
            .map_err(|error| FAE::RendezvousCompletionHandshake {
                error,
                intro_index,
                rend_pt: rend_pt.clone(),
            })?;
        let params = onion_circparams_from_netparams(self.netdir.params())
            .map_err(into_internal!("Failed to build CircParameters"))?;
        // TODO: We may be able to infer more about the supported protocols of the other side from our
        // handshake, and from its descriptors.
        //
        // TODO CC: This is relevant for congestion control!
        let protocols = self.netdir.client_protocol_status().required_protocols();
        rendezvous
            .rend_tunnel
            .m_extend_virtual(
                handshake::RelayProtocol::HsV3,
                handshake::HandshakeRole::Initiator,
                keygen,
                params,
                protocols,
            )
            .await
            .map_err(into_internal!(
                "actually this is probably a 'circuit closed' error" // TODO HS
            ))?;
        debug!(
            "hs conn to {}: RPT {} IPT {}: HS circuit established",
            &self.hsid,
            rend_pt.as_inner(),
            intro_index,
        );
        Ok(rendezvous.rend_tunnel)
    }
    /// Helper to estimate a timeout for a complicated operation
    ///
    /// `actions` is a list of `(count, action)`, where each entry
    /// represents doing `action`, `count` times sequentially.
    ///
    /// Combines the timeout estimates and returns an overall timeout.
8
    fn estimate_timeout(&self, actions: &[(u32, TimeoutsAction)]) -> Duration {
        // This algorithm is, perhaps, wrong.  For uncorrelated variables, a particular
        // percentile estimate for a sum of random variables, is not calculated by adding the
        // percentile estimates of the individual variables.
        //
        // But the actual lengths of times of the operations aren't uncorrelated.
        // If they were *perfectly* correlated, then this addition would be correct.
        // It will do for now; it just might be rather longer than it ought to be.
8
        actions
8
            .iter()
16
            .map(|(count, action)| {
16
                self.circpool
16
                    .m_estimate_timeout(action)
16
                    .saturating_mul(*count)
16
            })
8
            .fold(Duration::ZERO, Duration::saturating_add)
8
    }
}
/// Mocks used for testing `connect.rs`
///
/// This is different to `MockableConnectorData`,
/// which is used to *replace* this file, when testing `state.rs`.
///
/// `MocksForConnect` provides mock facilities for *testing* this file.
//
// TODO this should probably live somewhere else, maybe tor-circmgr even?
// TODO this really ought to be made by macros or something
trait MocksForConnect<R>: Clone {
    /// HS circuit pool
    type HsCircPool: MockableCircPool<R>;
    /// A random number generator
    type Rng: rand::Rng + rand::CryptoRng;
    /// Tell tests we got this descriptor text
    fn test_got_desc(&self, _: &HsDesc) {}
    /// Tell tests we got this data tunnel.
    fn test_got_tunnel(&self, _: &DataTunnel!(R, Self)) {}
    /// Tell tests we have obtained and sorted the intros like this
    fn test_got_ipts(&self, _: &[UsableIntroPt]) {}
    /// Return a random number generator
    fn thread_rng(&self) -> Self::Rng;
}
/// Mock for `HsCircPool`
///
/// Methods start with `m_` to avoid the following problem:
/// `ClientCirc::start_conversation` (say) means
/// to use the inherent method if one exists,
/// but will use a trait method if there isn't an inherent method.
///
/// So if the inherent method is renamed, the call in the impl here
/// turns into an always-recursive call.
/// This is not detected by the compiler due to the situation being
/// complicated by futures, `#[async_trait]` etc.
/// <https://github.com/rust-lang/rust/issues/111177>
#[async_trait]
trait MockableCircPool<R> {
    /// Directory tunnel.
    type DirTunnel: MockableClientDir;
    /// Data tunnel.
    type DataTunnel: MockableClientData;
    /// Intro tunnel.
    type IntroTunnel: MockableClientIntro;
    async fn m_get_or_launch_dir(
        &self,
        netdir: &NetDir,
        target: impl CircTarget + Send + Sync + 'async_trait,
    ) -> tor_circmgr::Result<Self::DirTunnel>;
    async fn m_get_or_launch_intro(
        &self,
        netdir: &NetDir,
        target: impl CircTarget + Send + Sync + 'async_trait,
    ) -> tor_circmgr::Result<Self::IntroTunnel>;
    /// Client circuit
    async fn m_get_or_launch_client_rend<'a>(
        &self,
        netdir: &'a NetDir,
    ) -> tor_circmgr::Result<(Self::DataTunnel, Relay<'a>)>;
    /// Estimate timeout
    fn m_estimate_timeout(&self, action: &TimeoutsAction) -> Duration;
}
/// Mock for onion service client directory tunnel.
#[async_trait]
trait MockableClientDir: Debug {
    /// Client circuit
    type DirStream: AsyncRead + AsyncWrite + Send + Unpin;
    async fn m_begin_dir_stream(&self) -> tor_circmgr::Result<Self::DirStream>;
    /// Get a tor_dirclient::SourceInfo for this circuit, if possible.
    fn m_source_info(&self) -> tor_proto::Result<Option<SourceInfo>>;
}
/// Mock for onion service client data tunnel.
#[async_trait]
trait MockableClientData: Debug {
    /// Conversation
    type Conversation<'r>
    where
        Self: 'r;
    /// Converse
    async fn m_start_conversation_last_hop(
        &self,
        msg: Option<AnyRelayMsg>,
        reply_handler: impl MsgHandler + Send + 'static,
    ) -> tor_circmgr::Result<Self::Conversation<'_>>;
    /// Add a virtual hop to the circuit.
    async fn m_extend_virtual(
        &self,
        protocol: tor_proto::client::circuit::handshake::RelayProtocol,
        role: tor_proto::client::circuit::handshake::HandshakeRole,
        handshake: impl tor_proto::client::circuit::handshake::KeyGenerator + Send,
        params: CircParameters,
        capabilities: &tor_protover::Protocols,
    ) -> tor_circmgr::Result<()>;
}
/// Mock for onion service client introduction tunnel.
#[async_trait]
trait MockableClientIntro: Debug {
    /// Conversation
    type Conversation<'r>
    where
        Self: 'r;
    /// Converse
    async fn m_start_conversation_last_hop(
        &self,
        msg: Option<AnyRelayMsg>,
        reply_handler: impl MsgHandler + Send + 'static,
    ) -> tor_circmgr::Result<Self::Conversation<'_>>;
}
impl<R: Runtime> MocksForConnect<R> for () {
    type HsCircPool = HsCircPool<R>;
    type Rng = rand::rngs::ThreadRng;
    fn thread_rng(&self) -> Self::Rng {
        rand::rng()
    }
}
#[async_trait]
impl<R: Runtime> MockableCircPool<R> for HsCircPool<R> {
    type DirTunnel = ClientOnionServiceDirTunnel;
    type DataTunnel = ClientOnionServiceDataTunnel;
    type IntroTunnel = ClientOnionServiceIntroTunnel;
    #[instrument(level = "trace", skip_all)]
    async fn m_get_or_launch_dir(
        &self,
        netdir: &NetDir,
        target: impl CircTarget + Send + Sync + 'async_trait,
    ) -> tor_circmgr::Result<Self::DirTunnel> {
        Ok(HsCircPool::get_or_launch_client_dir(self, netdir, target).await?)
    }
    #[instrument(level = "trace", skip_all)]
    async fn m_get_or_launch_intro(
        &self,
        netdir: &NetDir,
        target: impl CircTarget + Send + Sync + 'async_trait,
    ) -> tor_circmgr::Result<Self::IntroTunnel> {
        Ok(HsCircPool::get_or_launch_client_intro(self, netdir, target).await?)
    }
    #[instrument(level = "trace", skip_all)]
    async fn m_get_or_launch_client_rend<'a>(
        &self,
        netdir: &'a NetDir,
    ) -> tor_circmgr::Result<(Self::DataTunnel, Relay<'a>)> {
        HsCircPool::get_or_launch_client_rend(self, netdir).await
    }
    fn m_estimate_timeout(&self, action: &TimeoutsAction) -> Duration {
        HsCircPool::estimate_timeout(self, action)
    }
}
#[async_trait]
impl MockableClientDir for ClientOnionServiceDirTunnel {
    /// Client circuit
    type DirStream = tor_proto::client::stream::DataStream;
    async fn m_begin_dir_stream(&self) -> tor_circmgr::Result<Self::DirStream> {
        Self::begin_dir_stream(self).await
    }
    /// Get a tor_dirclient::SourceInfo for this circuit, if possible.
    fn m_source_info(&self) -> tor_proto::Result<Option<SourceInfo>> {
        SourceInfo::from_tunnel(self)
    }
}
#[async_trait]
impl MockableClientData for ClientOnionServiceDataTunnel {
    type Conversation<'r> = tor_proto::Conversation<'r>;
    async fn m_start_conversation_last_hop(
        &self,
        msg: Option<AnyRelayMsg>,
        reply_handler: impl MsgHandler + Send + 'static,
    ) -> tor_circmgr::Result<Self::Conversation<'_>> {
        Self::start_conversation(self, msg, reply_handler, TargetHop::LastHop).await
    }
    async fn m_extend_virtual(
        &self,
        protocol: tor_proto::client::circuit::handshake::RelayProtocol,
        role: tor_proto::client::circuit::handshake::HandshakeRole,
        handshake: impl tor_proto::client::circuit::handshake::KeyGenerator + Send,
        params: CircParameters,
        capabilities: &tor_protover::Protocols,
    ) -> tor_circmgr::Result<()> {
        Self::extend_virtual(self, protocol, role, handshake, params, capabilities).await
    }
}
#[async_trait]
impl MockableClientIntro for ClientOnionServiceIntroTunnel {
    type Conversation<'r> = tor_proto::Conversation<'r>;
    async fn m_start_conversation_last_hop(
        &self,
        msg: Option<AnyRelayMsg>,
        reply_handler: impl MsgHandler + Send + 'static,
    ) -> tor_circmgr::Result<Self::Conversation<'_>> {
        Self::start_conversation(self, msg, reply_handler, TargetHop::LastHop).await
    }
}
#[async_trait]
impl MockableConnectorData for Data {
    type DataTunnel = ClientOnionServiceDataTunnel;
    type MockGlobalState = ();
    async fn connect<R: Runtime>(
        connector: &HsClientConnector<R>,
        netdir: Arc<NetDir>,
        config: Arc<Config>,
        hsid: HsId,
        data: &mut Self,
        secret_keys: HsClientSecretKeys,
    ) -> Result<Self::DataTunnel, ConnError> {
        connect(connector, netdir, config, hsid, data, secret_keys).await
    }
    fn tunnel_is_ok(tunnel: &Self::DataTunnel) -> bool {
        !tunnel.is_closed()
    }
}
#[cfg(test)]
mod test {
    // @@ begin test lint list maintained by maint/add_warning @@
    #![allow(clippy::bool_assert_comparison)]
    #![allow(clippy::clone_on_copy)]
    #![allow(clippy::dbg_macro)]
    #![allow(clippy::mixed_attributes_style)]
    #![allow(clippy::print_stderr)]
    #![allow(clippy::print_stdout)]
    #![allow(clippy::single_char_pattern)]
    #![allow(clippy::unwrap_used)]
    #![allow(clippy::unchecked_time_subtraction)]
    #![allow(clippy::useless_vec)]
    #![allow(clippy::needless_pass_by_value)]
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
    #![allow(dead_code, unused_variables)] // TODO HS TESTS delete, after tests are completed
    use super::*;
    use crate::*;
    use futures::FutureExt as _;
    use std::{iter, panic::AssertUnwindSafe};
    use tokio_crate as tokio;
    use tor_async_utils::JoinReadWrite;
    use tor_basic_utils::test_rng::{TestingRng, testing_rng};
    use tor_hscrypto::pk::{HsClientDescEncKey, HsClientDescEncKeypair};
    use tor_llcrypto::pk::curve25519;
    use tor_netdoc::doc::{hsdesc::test_data, netstatus::Lifetime};
    use tor_rtcompat::RuntimeSubstExt as _;
    use tor_rtcompat::tokio::TokioNativeTlsRuntime;
    use tor_rtmock::simple_time::SimpleMockTimeProvider;
    use tracing_test::traced_test;
    #[derive(Debug, Default)]
    struct MocksGlobal {
        hsdirs_asked: Vec<OwnedCircTarget>,
        got_desc: Option<HsDesc>,
    }
    #[derive(Clone, Debug)]
    struct Mocks<I> {
        mglobal: Arc<Mutex<MocksGlobal>>,
        id: I,
    }
    impl<I> Mocks<I> {
        fn map_id<J>(&self, f: impl FnOnce(&I) -> J) -> Mocks<J> {
            Mocks {
                mglobal: self.mglobal.clone(),
                id: f(&self.id),
            }
        }
    }
    impl<R: Runtime> MocksForConnect<R> for Mocks<()> {
        type HsCircPool = Mocks<()>;
        type Rng = TestingRng;
        fn test_got_desc(&self, desc: &HsDesc) {
            self.mglobal.lock().unwrap().got_desc = Some(desc.clone());
        }
        fn test_got_ipts(&self, desc: &[UsableIntroPt]) {}
        fn thread_rng(&self) -> Self::Rng {
            testing_rng()
        }
    }
    #[allow(clippy::diverging_sub_expression)] // async_trait + todo!()
    #[async_trait]
    impl<R: Runtime> MockableCircPool<R> for Mocks<()> {
        type DataTunnel = Mocks<()>;
        type DirTunnel = Mocks<()>;
        type IntroTunnel = Mocks<()>;
        async fn m_get_or_launch_dir(
            &self,
            _netdir: &NetDir,
            target: impl CircTarget + Send + Sync + 'async_trait,
        ) -> tor_circmgr::Result<Self::DirTunnel> {
            let target = OwnedCircTarget::from_circ_target(&target);
            self.mglobal.lock().unwrap().hsdirs_asked.push(target);
            // Adding the `Arc` here is a little ugly, but that's what we get
            // for using the same Mocks for everything.
            Ok(self.clone())
        }
        async fn m_get_or_launch_intro(
            &self,
            _netdir: &NetDir,
            target: impl CircTarget + Send + Sync + 'async_trait,
        ) -> tor_circmgr::Result<Self::IntroTunnel> {
            todo!()
        }
        /// Client circuit
        async fn m_get_or_launch_client_rend<'a>(
            &self,
            netdir: &'a NetDir,
        ) -> tor_circmgr::Result<(Self::DataTunnel, Relay<'a>)> {
            todo!()
        }
        fn m_estimate_timeout(&self, action: &TimeoutsAction) -> Duration {
            Duration::from_secs(10)
        }
    }
    #[allow(clippy::diverging_sub_expression)] // async_trait + todo!()
    #[async_trait]
    impl MockableClientDir for Mocks<()> {
        type DirStream = JoinReadWrite<futures::io::Cursor<Box<[u8]>>, futures::io::Sink>;
        async fn m_begin_dir_stream(&self) -> tor_circmgr::Result<Self::DirStream> {
            let response = format!(
                r#"HTTP/1.1 200 OK
{}"#,
                test_data::TEST_DATA_2
            )
            .into_bytes()
            .into_boxed_slice();
            Ok(JoinReadWrite::new(
                futures::io::Cursor::new(response),
                futures::io::sink(),
            ))
        }
        fn m_source_info(&self) -> tor_proto::Result<Option<SourceInfo>> {
            Ok(None)
        }
    }
    #[allow(clippy::diverging_sub_expression)] // async_trait + todo!()
    #[async_trait]
    impl MockableClientData for Mocks<()> {
        type Conversation<'r> = &'r ();
        async fn m_start_conversation_last_hop(
            &self,
            msg: Option<AnyRelayMsg>,
            reply_handler: impl MsgHandler + Send + 'static,
        ) -> tor_circmgr::Result<Self::Conversation<'_>> {
            todo!()
        }
        async fn m_extend_virtual(
            &self,
            protocol: tor_proto::client::circuit::handshake::RelayProtocol,
            role: tor_proto::client::circuit::handshake::HandshakeRole,
            handshake: impl tor_proto::client::circuit::handshake::KeyGenerator + Send,
            params: CircParameters,
            capabilities: &tor_protover::Protocols,
        ) -> tor_circmgr::Result<()> {
            todo!()
        }
    }
    #[allow(clippy::diverging_sub_expression)] // async_trait + todo!()
    #[async_trait]
    impl MockableClientIntro for Mocks<()> {
        type Conversation<'r> = &'r ();
        async fn m_start_conversation_last_hop(
            &self,
            msg: Option<AnyRelayMsg>,
            reply_handler: impl MsgHandler + Send + 'static,
        ) -> tor_circmgr::Result<Self::Conversation<'_>> {
            todo!()
        }
    }
    #[traced_test]
    #[tokio::test]
    async fn test_connect() {
        let valid_after = humantime::parse_rfc3339("2023-02-09T12:00:00Z").unwrap();
        let fresh_until = valid_after + humantime::parse_duration("1 hours").unwrap();
        let valid_until = valid_after + humantime::parse_duration("24 hours").unwrap();
        let lifetime = Lifetime::new(valid_after, fresh_until, valid_until).unwrap();
        let netdir = tor_netdir::testnet::construct_custom_netdir_with_params(
            tor_netdir::testnet::simple_net_func,
            iter::empty::<(&str, _)>(),
            Some(lifetime),
        )
        .expect("failed to build default testing netdir");
        let netdir = Arc::new(netdir.unwrap_if_sufficient().unwrap());
        let runtime = TokioNativeTlsRuntime::current().unwrap();
        let now = humantime::parse_rfc3339("2023-02-09T12:00:00Z").unwrap();
        let mock_sp = SimpleMockTimeProvider::from_wallclock(now);
        let runtime = runtime
            .with_sleep_provider(mock_sp.clone())
            .with_coarse_time_provider(mock_sp);
        let time_period = netdir.hs_time_period();
        let mglobal = Arc::new(Mutex::new(MocksGlobal::default()));
        let mocks = Mocks { mglobal, id: () };
        // From C Tor src/test/test_hs_common.c test_build_address
        let hsid = test_data::TEST_HSID_2.into();
        let mut data = Data::default();
        let pk: HsClientDescEncKey = curve25519::PublicKey::from(test_data::TEST_PUBKEY_2).into();
        let sk = curve25519::StaticSecret::from(test_data::TEST_SECKEY_2).into();
        let mut secret_keys_builder = HsClientSecretKeysBuilder::default();
        secret_keys_builder.ks_hsc_desc_enc(HsClientDescEncKeypair::new(pk.clone(), sk));
        let secret_keys = secret_keys_builder.build().unwrap();
        let ctx = Context::new(
            &runtime,
            &mocks,
            netdir,
            Default::default(),
            hsid,
            secret_keys,
            mocks.clone(),
        )
        .unwrap();
        let _got = AssertUnwindSafe(ctx.connect(&mut data))
            .catch_unwind() // TODO HS TESTS: remove this and the AssertUnwindSafe
            .await;
        let (hs_blind_id_key, subcredential) = HsIdKey::try_from(hsid)
            .unwrap()
            .compute_blinded_key(time_period)
            .unwrap();
        let hs_blind_id = hs_blind_id_key.id();
        let sk = curve25519::StaticSecret::from(test_data::TEST_SECKEY_2).into();
        let hsdesc = HsDesc::parse_decrypt_validate(
            test_data::TEST_DATA_2,
            &hs_blind_id,
            now,
            &subcredential,
            Some(&HsClientDescEncKeypair::new(pk, sk)),
        )
        .unwrap()
        .dangerously_assume_timely();
        let mglobal = mocks.mglobal.lock().unwrap();
        assert_eq!(mglobal.hsdirs_asked.len(), 1);
        // TODO hs: here and in other places, consider implementing PartialEq instead, or creating
        // an assert_dbg_eq macro (which would be part of a test_helpers crate or something)
        assert_eq!(
            format!("{:?}", mglobal.got_desc),
            format!("{:?}", Some(hsdesc))
        );
        // Check how long the descriptor is valid for
        let (start_time, end_time) = data.desc.as_ref().unwrap().bounds();
        assert_eq!(start_time, None);
        let desc_valid_until = humantime::parse_rfc3339("2023-02-11T20:00:00Z").unwrap();
        assert_eq!(end_time, Some(desc_valid_until));
        // TODO HS TESTS: check the circuit in got is the one we gave out
        // TODO HS TESTS: continue with this
    }
    // TODO HS TESTS: Test IPT state management and expiry:
    //   - obtain a test descriptor with only a broken ipt
    //     (broken in the sense that intro can be attempted, but will fail somehow)
    //   - try to make a connection and expect it to fail
    //   - assert that the ipt data isn't empty
    //   - cause the descriptor to expire (advance clock)
    //   - start using a mocked RNG if we weren't already and pin its seed here
    //   - make a new descriptor with two IPTs: the broken one from earlier, and a new one
    //   - make a new connection
    //   - use test_got_ipts to check that the random numbers
    //     would sort the bad intro first, *and* that the good one is appears first
    //   - assert that connection succeeded
    //   - cause the circuit and descriptor to expire (advance clock)
    //   - go back to the previous descriptor contents, but with a new validity period
    //   - try to make a connection
    //   - use test_got_ipts to check that only the broken ipt is present
    // TODO HS TESTS: test retries (of every retry loop we have here)
    // TODO HS TESTS: test error paths
}