1
//! Errors relating to being a hidden service client
2
use std::sync::Arc;
3

            
4
use derive_more::{From, Into};
5
use futures::task::SpawnError;
6

            
7
use thiserror::Error;
8
use tracing::error;
9

            
10
use retry_error::RetryError;
11
use safelog::{Redacted, Sensitive};
12
use tor_cell::relaycell::hs::IntroduceAckStatus;
13
use tor_error::define_asref_dyn_std_error;
14
use tor_error::{Bug, ErrorKind, ErrorReport as _, HasKind, HasRetryTime, RetryTime, internal};
15
use tor_linkspec::RelayIds;
16
use tor_llcrypto::pk::ed25519::Ed25519Identity;
17
use tor_netdir::Relay;
18

            
19
/// Identity of a rendezvous point, for use in error reports
20
pub(crate) type RendPtIdentityForError = Redacted<RelayIds>;
21

            
22
/// Given a `Relay` for a rendezvous pt, provides its identify for use in error reports
23
518
pub(crate) fn rend_pt_identity_for_error(relay: &Relay<'_>) -> RendPtIdentityForError {
24
518
    RelayIds::from_relay_ids(relay).into()
25
518
}
26

            
27
/// Index of an introduction point in the descriptor
28
///
29
/// Principally used in error reporting.
30
///
31
/// Formats as `#<n+1>`.
32
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, From, Into)]
33
#[allow(clippy::exhaustive_structs)]
34
#[derive(derive_more::Display)]
35
#[display("#{}", self.0 + 1)]
36
pub struct IntroPtIndex(pub usize);
37

            
38
/// Error that occurred attempting to reach a hidden service
39
#[derive(Error, Clone, Debug)]
40
#[non_exhaustive]
41
pub enum ConnError {
42
    /// Invalid hidden service identity (`.onion` address)
43
    #[error("Invalid hidden service identity (`.onion` address)")]
44
    InvalidHsId,
45

            
46
    /// Unable to download hidden service descriptor
47
    #[error("Unable to download hidden service descriptor")]
48
    DescriptorDownload(RetryError<tor_error::Report<DescriptorError>>),
49

            
50
    /// Obtained descriptor but unable to connect to hidden service due to problem with IPT or RPT
51
    // TODO HS is this the right name for this variant?
52
    #[error("Unable to connect to hidden service using any Rendezvous Point / Introduction Point")]
53
    Failed(#[source] RetryError<FailedAttemptError>),
54

            
55
    /// The consensus network contains no suitable hidden service directories!
56
    #[error("consensus contains no suitable hidden service directories")]
57
    NoHsDirs,
58

            
59
    /// The descriptor contained only unusable introduction points!
60
    ///
61
    /// This is the fault of the service, or shows incompatibility between us and them.
62
    #[error("hidden service has no introduction points usable by us")]
63
    NoUsableIntroPoints,
64

            
65
    /// Cannot fetch the descriptor because we are rate-limited
66
    ///
67
    /// Returned if we do not have a descriptor for the service,
68
    /// and we cannot download a new one because a rate-limit
69
    /// is in place for all HsDirs.
70
    #[error("cannot fetch descriptor (we are rate-limited)")]
71
    NoUsableHsDirs,
72

            
73
    /// Unable to spawn
74
    #[error("Unable to spawn {spawning}")]
75
    Spawn {
76
        /// What we were trying to spawn
77
        spawning: &'static str,
78
        /// What happened when we tried to spawn it
79
        #[source]
80
        cause: Arc<SpawnError>,
81
    },
82

            
83
    /// Internal error
84
    #[error("{0}")]
85
    Bug(#[from] Bug),
86
}
87

            
88
/// Error that occurred attempting to download a descriptor
89
#[derive(Error, Clone, Debug)]
90
#[non_exhaustive]
91
#[error("tried hsdir {hsdir}: {error}")]
92
pub struct DescriptorError {
93
    /// Which hsdir we were trying
94
    // TODO #813 this should be Redacted<RelayDescription> or something
95
    // It seems likely that the set of redacted hsdir ids could identify the service,
96
    // so use Sensitive rather than Redacted.
97
    pub hsdir: Sensitive<Ed25519Identity>,
98

            
99
    /// What happened
100
    #[source]
101
    pub error: DescriptorErrorDetail,
102
}
103
define_asref_dyn_std_error!(DescriptorError);
104

            
105
/// Error that occurred attempting to download a descriptor
106
#[derive(Error, Clone, Debug)]
107
#[non_exhaustive]
108
//
109
// NOTE! These are in an order!  "Most interesting" errors come last.
110
// Specifically, after various attempts, the ErrorKind of the overall error
111
// will be that of the error which is latest in this enum.
112
//
113
#[derive(strum::EnumDiscriminants)]
114
#[strum_discriminants(derive(PartialOrd, Ord))]
115
pub enum DescriptorErrorDetail {
116
    /// Timed out
117
    #[error("timed out")]
118
    Timeout,
119

            
120
    /// Failed to establish circuit to hidden service directory
121
    #[error("circuit failed")]
122
    Circuit(#[from] tor_circmgr::Error),
123

            
124
    /// Failed to establish stream to hidden service directory
125
    #[error("stream failed")]
126
    Stream(#[source] tor_proto::Error),
127

            
128
    /// Failed to make directory request
129
    #[error("directory error")]
130
    Directory(#[from] tor_dirclient::RequestError),
131

            
132
    /// Failed to parse or validate descriptor
133
    #[error("problem with descriptor")]
134
    Descriptor(#[from] tor_netdoc::doc::hsdesc::HsDescError),
135

            
136
    /// Internal error
137
    #[error("{0}")]
138
    Bug(#[from] Bug),
139
}
140

            
141
impl From<tor_rtcompat::TimeoutError> for DescriptorErrorDetail {
142
    fn from(_: tor_rtcompat::TimeoutError) -> Self {
143
        Self::Timeout
144
    }
145
}
146

            
147
/// Error that occurred making one attempt to connect to a hidden service using an IP and RP
148
#[derive(Error, Clone, Debug)]
149
#[non_exhaustive]
150
//
151
// NOTE! These are in an order!  "Most interesting" errors come last.
152
// Specifically, after various attempts, the ErrorKind of the overall error
153
// will be that of the error which is latest in this enum.
154
//
155
#[derive(strum::EnumDiscriminants)]
156
#[strum_discriminants(derive(PartialOrd, Ord))]
157
// TODO HS is this the right name for this type?  It's a very mixed bag, so maybe it is.
158
pub enum FailedAttemptError {
159
    /// Introduction point unusable because it couldn't be used as a circuit target
160
    #[error("Unusable introduction point #{intro_index}")]
161
    UnusableIntro {
162
        /// Why it's not use able
163
        #[source]
164
        error: crate::relay_info::InvalidTarget,
165

            
166
        /// The index of the IPT in the list of IPTs in the descriptor
167
        intro_index: IntroPtIndex,
168
    },
169

            
170
    /// Failed to obtain any circuit to use as a rendezvous circuit
171
    #[error("Failed to obtain any circuit to use as a rendezvous circuit")]
172
    RendezvousCircuitObtain {
173
        /// Why it's not use able
174
        #[source]
175
        error: tor_circmgr::Error,
176
    },
177

            
178
    /// Creating a rendezvous circuit and rendezvous point took too long
179
    #[error("Creating a rendezvous circuit and rendezvous point took too long")]
180
    RendezvousEstablishTimeout {
181
        /// Which relay did we choose for rendezvous point
182
        // TODO #813 this should be Redacted<RelayDescription> or something
183
        rend_pt: RendPtIdentityForError,
184
    },
185

            
186
    /// Failed to establish rendezvous point
187
    #[error("Failed to establish rendezvous point at {rend_pt}")]
188
    RendezvousEstablish {
189
        /// What happened
190
        #[source]
191
        error: tor_proto::Error,
192

            
193
        /// Which relay did we choose for rendezvous point
194
        // TODO #813 this should be Redacted<RelayDescription> or something
195
        rend_pt: RendPtIdentityForError,
196
    },
197

            
198
    /// Failed to obtain circuit to introduction point
199
    #[error("Failed to obtain circuit to introduction point {intro_index}")]
200
    IntroductionCircuitObtain {
201
        /// What happened
202
        #[source]
203
        error: tor_circmgr::Error,
204

            
205
        /// The index of the IPT in the list of IPTs in the descriptor
206
        intro_index: IntroPtIndex,
207
    },
208

            
209
    /// Introduction exchange (with the introduction point) failed
210
    #[error("Introduction exchange (with the introduction point) failed")]
211
    IntroductionExchange {
212
        /// What happened
213
        #[source]
214
        error: tor_proto::Error,
215

            
216
        /// The index of the IPT in the list of IPTs in the descriptor
217
        intro_index: IntroPtIndex,
218
    },
219

            
220
    /// Introduction point reported error in its INTRODUCE_ACK
221
    #[error("Introduction point {intro_index} reported error in its INTRODUCE_ACK: {status}")]
222
    IntroductionFailed {
223
        /// The status code provided by the introduction point
224
        status: IntroduceAckStatus,
225

            
226
        /// The index of the IPT in the list of IPTs in the descriptor
227
        intro_index: IntroPtIndex,
228
    },
229

            
230
    /// Communication with introduction point {intro_index} took too long
231
    ///
232
    /// This might mean it took too long to establish a circuit to the IPT,
233
    /// or that the INTRODUCE exchange took too long.
234
    #[error("Communication with introduction point {intro_index} took too long")]
235
    IntroductionTimeout {
236
        /// The index of the IPT in the list of IPTs in the descriptor
237
        intro_index: IntroPtIndex,
238
    },
239

            
240
    /// It took too long for the rendezvous to be completed
241
    ///
242
    /// This might be the fault of almost anyone.  All we know is that we got
243
    /// a successful `INTRODUCE_ACK` but the `RENDEZVOUS2` never arrived.
244
    #[error("Rendezvous at {rend_pt} using introduction point {intro_index} took too long")]
245
    RendezvousCompletionTimeout {
246
        /// The index of the IPT in the list of IPTs in the descriptor
247
        intro_index: IntroPtIndex,
248

            
249
        /// Which relay did we choose for rendezvous point
250
        // TODO #813 this should be Redacted<RelayDescription> or something
251
        rend_pt: RendPtIdentityForError,
252
    },
253

            
254
    /// Error on rendezvous circuit when expecting rendezvous completion (`RENDEZVOUS2`)
255
    #[error(
256
        "Error on rendezvous circuit when expecting rendezvous completion (RENDEZVOUS2 message)"
257
    )]
258
    RendezvousCompletionCircuitError {
259
        /// What happened
260
        #[source]
261
        error: tor_proto::Error,
262

            
263
        /// The index of the IPT in the list of IPTs in the descriptor
264
        intro_index: IntroPtIndex,
265

            
266
        /// Which relay did we choose for rendezvous point
267
        // TODO #813 this should be Redacted<RelayDescription> or something
268
        rend_pt: RendPtIdentityForError,
269
    },
270

            
271
    /// Error processing rendezvous completion (`RENDEZVOUS2`)
272
    ///
273
    /// This is might be the fault of the hidden service or the rendezvous point.
274
    #[error("Rendezvous completion end-to-end crypto handshake failed (bad RENDEZVOUS2 message)")]
275
    RendezvousCompletionHandshake {
276
        /// What happened
277
        #[source]
278
        error: tor_proto::Error,
279

            
280
        /// The index of the IPT in the list of IPTs in the descriptor
281
        intro_index: IntroPtIndex,
282

            
283
        /// Which relay did we choose for rendezvous point
284
        // TODO #813 this should be Redacted<RelayDescription> or something
285
        rend_pt: RendPtIdentityForError,
286
    },
287

            
288
    /// Internal error
289
    #[error("{0}")]
290
    Bug(#[from] Bug),
291
}
292
define_asref_dyn_std_error!(FailedAttemptError);
293

            
294
impl FailedAttemptError {
295
    /// Which introduction point did this error involve (or implicate), if any?
296
    ///
297
    /// This is an index into the table in the HS descriptor,
298
    /// so it can be less-than-useful outside the context where this error was generated.
299
    // TODO derive this, too much human error possibility
300
166
    pub(crate) fn intro_index(&self) -> Option<IntroPtIndex> {
301
        use FailedAttemptError as FAE;
302
166
        match self {
303
            FAE::UnusableIntro { intro_index, .. }
304
            | FAE::RendezvousCompletionCircuitError { intro_index, .. }
305
            | FAE::RendezvousCompletionHandshake { intro_index, .. }
306
            | FAE::RendezvousCompletionTimeout { intro_index, .. }
307
            | FAE::IntroductionCircuitObtain { intro_index, .. }
308
            | FAE::IntroductionExchange { intro_index, .. }
309
166
            | FAE::IntroductionFailed { intro_index, .. }
310
166
            | FAE::IntroductionTimeout { intro_index, .. } => Some(*intro_index),
311
            FAE::RendezvousCircuitObtain { .. }
312
            | FAE::RendezvousEstablish { .. }
313
            | FAE::RendezvousEstablishTimeout { .. }
314
            | FAE::Bug(_) => None,
315
        }
316
166
    }
317
}
318

            
319
/// When *an attempt like this* should be retried.
320
///
321
/// For error variants with an introduction point index
322
/// (`FailedAttemptError::intro_index` returns `Some`)
323
/// that's when we might retry *with that introduction point*.
324
///
325
/// For error variants with a rendezvous point,
326
/// that's when we might retry *with that rendezvous point*.
327
///
328
/// For variants with both, we don't know
329
/// which of the introduction point or rendezvous point is implicated.
330
/// Retrying earlier with *one* different relay out of the two relays would be reasonable,
331
/// as would delaying retrying with *either* of the same relays.
332
//
333
// Our current code doesn't keep history about rendezvous points.
334
// We use this to choose what order to try the service's introduction points.
335
// See `IptSortKey` in connect.rs.
336
impl HasRetryTime for FailedAttemptError {
337
166
    fn retry_time(&self) -> RetryTime {
338
        use FailedAttemptError as FAE;
339
        use RetryTime as RT;
340
166
        match self {
341
            // Delegate to the cause
342
            FAE::UnusableIntro { error, .. } => error.retry_time(),
343
            FAE::RendezvousCircuitObtain { error } => error.retry_time(),
344
            FAE::IntroductionCircuitObtain { error, .. } => error.retry_time(),
345
166
            FAE::IntroductionFailed { status, .. } => status.retry_time(),
346
            // tor_proto::Error doesn't impl HasRetryTime, so we guess
347
            FAE::RendezvousCompletionCircuitError { error: _e, .. }
348
            | FAE::IntroductionExchange { error: _e, .. }
349
            | FAE::RendezvousEstablish { error: _e, .. } => RT::AfterWaiting,
350
            // Timeouts
351
            FAE::RendezvousEstablishTimeout { .. }
352
            | FAE::RendezvousCompletionTimeout { .. }
353
            | FAE::IntroductionTimeout { .. } => RT::AfterWaiting,
354
            // Other cases where we define the ErrorKind ourselves
355
            // If service didn't cause this, it was the RPT, so prefer to try another RPT
356
            FAE::RendezvousCompletionHandshake { error: _e, .. } => RT::Never,
357
            FAE::Bug(_) => RT::Never,
358
        }
359
166
    }
360
}
361

            
362
impl HasKind for ConnError {
363
    fn kind(&self) -> ErrorKind {
364
        use ConnError as CE;
365
        use ErrorKind as EK;
366
        match self {
367
            CE::InvalidHsId => EK::InvalidStreamTarget,
368
            CE::NoHsDirs => EK::TorDirectoryUnusable,
369
            CE::NoUsableIntroPoints => EK::OnionServiceProtocolViolation,
370
            CE::Spawn { cause, .. } => cause.kind(),
371
            CE::Bug(e) => e.kind(),
372

            
373
            CE::DescriptorDownload(attempts) => attempts
374
                .sources()
375
                .max_by_key(|attempt| DescriptorErrorDetailDiscriminants::from(&attempt.0.error))
376
                .map(|attempt| attempt.0.kind())
377
                .unwrap_or_else(|| {
378
                    let bug = internal!("internal error, empty CE::DescriptorDownload");
379
                    error!("bug: {}", bug.report());
380
                    bug.kind()
381
                }),
382

            
383
            CE::NoUsableHsDirs => EK::OnionServiceConnectionFailed,
384

            
385
            CE::Failed(attempts) => attempts
386
                .sources()
387
                .max_by_key(|attempt| FailedAttemptErrorDiscriminants::from(*attempt))
388
                .map(|attempt| attempt.kind())
389
                .unwrap_or_else(|| {
390
                    let bug = internal!("internal error, empty CE::DescriptorDownload");
391
                    error!("bug: {}", bug.report());
392
                    bug.kind()
393
                }),
394
        }
395
    }
396
}
397

            
398
impl HasKind for DescriptorError {
399
    fn kind(&self) -> ErrorKind {
400
        self.error.kind()
401
    }
402
}
403

            
404
impl HasKind for DescriptorErrorDetail {
405
    fn kind(&self) -> ErrorKind {
406
        use DescriptorErrorDetail as DED;
407
        use ErrorKind as EK;
408
        use tor_dirclient::RequestError as RE;
409
        match self {
410
            DED::Timeout => EK::TorNetworkTimeout,
411
            DED::Circuit(e) => e.kind(),
412
            DED::Stream(e) => e.kind(),
413
            DED::Directory(RE::HttpStatus(st, _)) if *st == 404 => EK::OnionServiceNotFound,
414
            DED::Directory(RE::ResponseTooLong(_)) => EK::OnionServiceProtocolViolation,
415
            DED::Directory(RE::HeadersTooLong(_)) => EK::OnionServiceProtocolViolation,
416
            DED::Directory(RE::Utf8Encoding(_)) => EK::OnionServiceProtocolViolation,
417
            DED::Directory(other_re) => other_re.kind(),
418
            DED::Descriptor(e) => e.kind(),
419
            DED::Bug(e) => e.kind(),
420
        }
421
    }
422
}
423

            
424
impl HasKind for FailedAttemptError {
425
166
    fn kind(&self) -> ErrorKind {
426
        /*use tor_dirclient::RequestError as RE;
427
        use tor_netdoc::NetdocErrorKind as NEK;
428
        use DescriptorErrorDetail as DED;*/
429
        use ErrorKind as EK;
430
        use FailedAttemptError as FAE;
431
166
        match self {
432
            FAE::UnusableIntro { .. } => EK::OnionServiceProtocolViolation,
433
            FAE::RendezvousCircuitObtain { error, .. } => error.kind(),
434
            FAE::RendezvousEstablish { error, .. } => error.kind(),
435
            FAE::RendezvousCompletionCircuitError { error, .. } => error.kind(),
436
            FAE::RendezvousCompletionHandshake { error, .. } => error.kind(),
437
            FAE::RendezvousEstablishTimeout { .. } => EK::TorNetworkTimeout,
438
            FAE::IntroductionCircuitObtain { error, .. } => error.kind(),
439
            FAE::IntroductionExchange { error, .. } => error.kind(),
440
166
            FAE::IntroductionFailed { .. } => EK::OnionServiceConnectionFailed,
441
            FAE::IntroductionTimeout { .. } => EK::TorNetworkTimeout,
442
            FAE::RendezvousCompletionTimeout { .. } => EK::RemoteNetworkTimeout,
443
            FAE::Bug(e) => e.kind(),
444
        }
445
166
    }
446
}
447

            
448
/// Error that occurred attempting to start up a hidden service client connector
449
#[derive(Error, Clone, Debug)]
450
#[non_exhaustive]
451
pub enum StartupError {
452
    /// Unable to spawn
453
    #[error("Unable to spawn {spawning}")]
454
    Spawn {
455
        /// What we were trying to spawn
456
        spawning: &'static str,
457
        /// What happened when we tried to spawn it
458
        #[source]
459
        cause: Arc<SpawnError>,
460
    },
461

            
462
    /// Internal error
463
    #[error("{0}")]
464
    Bug(#[from] Bug),
465
}
466

            
467
impl HasKind for StartupError {
468
    fn kind(&self) -> ErrorKind {
469
        use StartupError as SE;
470
        match self {
471
            SE::Spawn { cause, .. } => cause.kind(),
472
            SE::Bug(e) => e.kind(),
473
        }
474
    }
475
}
476

            
477
/// Error that occurred while trying to solve a proof of work puzzle
478
///
479
/// These errors will not prevent a connection from proceeding.
480
/// We may try a different proof of work scheme or none at all.
481
///
482
#[derive(Error, Clone, Debug)]
483
#[non_exhaustive]
484
pub(crate) enum ProofOfWorkError {
485
    /// Runtime error from a specific proof of work scheme
486
    #[error("Runtime error from client puzzle solver, #{0}")]
487
    Runtime(#[from] tor_hscrypto::pow::RuntimeError),
488

            
489
    /// Time-limited parameters are not valid
490
    #[error("Client puzzle parameters are not valid at this time")]
491
    TimeValidity(#[from] tor_checkable::TimeValidityError),
492

            
493
    /// Unexpectedly lost contact with solver
494
    #[error("Unexpectedly lost contact with solver task")]
495
    #[allow(dead_code)]
496
    SolverDisconnected,
497
}
498

            
499
impl DescriptorErrorDetail {
500
    /// Return true if this error is one that we should report as a suspicious event,
501
    /// along with the dirserver and description of the relevant document.
502
    pub(crate) fn should_report_as_suspicious(&self) -> bool {
503
        use DescriptorErrorDetail as E;
504
        match self {
505
            E::Timeout => false,
506
            E::Circuit(_) => false,
507
            E::Stream(_) => false, // TODO prop360
508
            E::Directory(e) => e.should_report_as_suspicious_if_anon(),
509
            E::Descriptor(e) => e.should_report_as_suspicious(),
510
            E::Bug(_) => false,
511
        }
512
    }
513
}