1
//! Implementation for onion service descriptors.
2
//!
3
//! An onion service descriptor is a document generated by an onion service and
4
//! uploaded to one or more HsDir nodes for clients to later download.  It tells
5
//! the onion service client where to find the current introduction points for
6
//! the onion service, and how to connect to them.
7
//!
8
//! An onion service descriptor is more complicated than most other
9
//! documentation types, because it is partially encrypted.
10

            
11
mod desc_enc;
12

            
13
#[cfg(feature = "hs-service")]
14
mod build;
15
mod inner;
16
mod middle;
17
mod outer;
18
pub mod pow;
19

            
20
pub use desc_enc::DecryptionError;
21
use tor_basic_utils::rangebounds::RangeBoundsExt;
22
use tor_error::internal;
23

            
24
use crate::{NetdocErrorKind as EK, Result};
25

            
26
use tor_checkable::signed::{self, SignatureGated};
27
use tor_checkable::timed::{self, TimerangeBound};
28
use tor_checkable::{SelfSigned, Timebound};
29
use tor_hscrypto::pk::{HsBlindId, HsClientDescEncKeypair, HsIntroPtSessionIdKey, HsSvcNtorKey};
30
use tor_hscrypto::{RevisionCounter, Subcredential};
31
use tor_linkspec::EncodedLinkSpec;
32
use tor_llcrypto::pk::curve25519;
33
use tor_units::IntegerMinutes;
34

            
35
use derive_builder::Builder;
36
use smallvec::SmallVec;
37

            
38
use std::result::Result as StdResult;
39
use std::time::SystemTime;
40

            
41
#[cfg(feature = "hsdesc-inner-docs")]
42
pub use {inner::HsDescInner, middle::HsDescMiddle, outer::HsDescOuter};
43

            
44
#[cfg(feature = "hs-service")]
45
pub use build::{HsDescBuilder, create_desc_sign_key_cert};
46

            
47
/// Metadata about an onion service descriptor, as stored at an HsDir.
48
///
49
/// This object is parsed from the outermost document of an onion service
50
/// descriptor, and used on the HsDir to maintain its index.  It does not
51
/// include the inner documents' information about introduction points, since the
52
/// HsDir cannot decrypt those without knowing the onion service's un-blinded
53
/// identity.
54
///
55
/// The HsDir caches this value, along with the original text of the descriptor.
56
#[cfg(feature = "hs-dir")]
57
#[allow(dead_code)] // TODO RELAY: Remove this.
58
pub struct StoredHsDescMeta {
59
    /// The blinded onion identity for this descriptor.  (This is the only
60
    /// identity that the HsDir knows.)
61
    blinded_id: HsBlindId,
62

            
63
    /// Information about the expiration and revision counter for this
64
    /// descriptor.
65
    idx_info: IndexInfo,
66
}
67

            
68
/// An unchecked StoredHsDescMeta: parsed, but not checked for liveness or validity.
69
#[cfg(feature = "hs-dir")]
70
pub type UncheckedStoredHsDescMeta =
71
    signed::SignatureGated<timed::TimerangeBound<StoredHsDescMeta>>;
72

            
73
/// Information about how long to hold a given onion service descriptor, and
74
/// when to replace it.
75
#[derive(Debug, Clone)]
76
#[allow(dead_code)] // TODO RELAY: Remove this if there turns out to be no need for it.
77
struct IndexInfo {
78
    /// The lifetime in minutes that this descriptor should be held after it is
79
    /// received.
80
    lifetime: IntegerMinutes<u16>,
81
    /// The expiration time on the `descriptor-signing-key-cert` included in this
82
    /// descriptor.
83
    signing_cert_expires: SystemTime,
84
    /// The revision counter on this descriptor: higher values should replace
85
    /// older ones.
86
    revision: RevisionCounter,
87
}
88

            
89
/// A decrypted, decoded onion service descriptor.
90
///
91
/// This object includes information from both the outer (plaintext) document of
92
/// the descriptor, and the inner (encrypted) documents.  It tells the client the
93
/// information it needs to contact the onion service, including necessary
94
/// introduction points and public keys.
95
#[derive(Debug, Clone)]
96
pub struct HsDesc {
97
    /// Information about the expiration and revision counter for this
98
    /// descriptor.
99
    #[allow(dead_code)] // TODO RELAY: Remove this if there turns out to be no need for it.
100
    idx_info: IndexInfo,
101

            
102
    /// The list of authentication types that this onion service supports.
103
    auth_required: Option<SmallVec<[IntroAuthType; 2]>>,
104

            
105
    /// If true, this a "single onion service" and is not trying to keep its own location private.
106
    is_single_onion_service: bool,
107

            
108
    /// One or more introduction points used to contact the onion service.
109
    intro_points: Vec<IntroPointDesc>,
110

            
111
    /// A list of offered proof-of-work parameters, at most one per type.
112
    pow_params: pow::PowParamSet,
113
    // /// A list of recognized CREATE handshakes that this onion service supports.
114
    //
115
    // TODO:  When someday we add a "create2 format" other than "hs-ntor", we
116
    // should turn this into a caret enum, record this info, and expose it.
117
    // create2_formats: Vec<u32>,
118
}
119

            
120
/// A type of authentication that is required when introducing to an onion
121
/// service.
122
#[non_exhaustive]
123
#[derive(Debug, Clone, Copy, Eq, PartialEq, derive_more::Display)]
124
pub enum IntroAuthType {
125
    /// Ed25519 authentication is required.
126
    #[display("ed25519")]
127
    Ed25519,
128
}
129

            
130
/// Information in an onion service descriptor about a single
131
/// introduction point.
132
#[derive(Debug, Clone, amplify::Getters, Builder)]
133
#[builder(pattern = "owned")] // mirrors HsDescBuilder
134
pub struct IntroPointDesc {
135
    /// The list of link specifiers needed to extend a circuit to the introduction point.
136
    ///
137
    /// These can include public keys and network addresses.
138
    ///
139
    /// Note that we do not enforce the presence of any link specifiers here;
140
    /// this means that you can't assume that an `IntroPointDesc` is a meaningful
141
    /// `ChanTarget` without some processing.
142
    //
143
    // The builder setter takes a `Vec` directly.  This seems fine.
144
    #[getter(skip)]
145
    link_specifiers: Vec<EncodedLinkSpec>,
146

            
147
    /// The key to be used to extend a circuit _to the introduction point_, using the
148
    /// ntor or ntor3 handshakes.  (`KP_ntor`)
149
    #[builder(setter(name = "ipt_kp_ntor"))] // TODO rename the internal variable too
150
    ipt_ntor_key: curve25519::PublicKey,
151

            
152
    /// The key to be used to identify the onion service at this introduction point.
153
    /// (`KP_hs_ipt_sid`)
154
    #[builder(setter(name = "kp_hs_ipt_sid"))] // TODO rename the internal variable too
155
    ipt_sid_key: HsIntroPtSessionIdKey,
156

            
157
    /// `KP_hss_ntor`, the key used to encrypt a handshake _to the onion
158
    /// service_ when using this introduction point.
159
    ///
160
    /// The onion service uses a separate key of this type with each
161
    /// introduction point as part of its strategy for preventing replay
162
    /// attacks.
163
    #[builder(setter(name = "kp_hss_ntor"))] // TODO rename the internal variable too
164
    svc_ntor_key: HsSvcNtorKey,
165
}
166

            
167
/// An onion service after it has been parsed by the client, but not yet decrypted.
168
pub struct EncryptedHsDesc {
169
    /// The un-decoded outer document of our onion service descriptor.
170
    outer_doc: outer::HsDescOuter,
171
}
172

            
173
/// An unchecked HsDesc: parsed, but not checked for liveness or validity.
174
pub type UncheckedEncryptedHsDesc = signed::SignatureGated<timed::TimerangeBound<EncryptedHsDesc>>;
175

            
176
#[cfg(feature = "hs-dir")]
177
impl StoredHsDescMeta {
178
    // TODO relay: needs accessor functions too.  (Let's not use public fields; we
179
    // are likely to want to mess with the repr of these types.)
180

            
181
    /// Parse the outermost layer of the descriptor in `input`, and return the
182
    /// resulting metadata (if possible).
183
2
    pub fn parse(input: &str) -> Result<UncheckedStoredHsDescMeta> {
184
2
        let outer = outer::HsDescOuter::parse(input)?;
185
3
        Ok(outer.dangerously_map(|timebound| {
186
2
            timebound.dangerously_map(|outer| StoredHsDescMeta::from_outer_doc(&outer))
187
2
        }))
188
2
    }
189
}
190

            
191
impl HsDesc {
192
    /// Parse the outermost document of the descriptor in `input`, and validate
193
    /// that its identity is consistent with `blinded_onion_id`.
194
    ///
195
    /// On success, the caller will get a wrapped object which they must
196
    /// validate and then decrypt.
197
    ///
198
    /// Use [`HsDesc::parse_decrypt_validate`] if you just need an [`HsDesc`] and don't want to
199
    /// handle the validation/decryption of the wrapped object yourself.
200
    ///
201
    /// # Example
202
    /// ```
203
    /// # use hex_literal::hex;
204
    /// # use tor_checkable::{SelfSigned, Timebound};
205
    /// # use tor_netdoc::doc::hsdesc::HsDesc;
206
    /// # use tor_netdoc::Error;
207
    /// #
208
    /// # let unparsed_desc: &str = include_str!("../../testdata/hsdesc1.txt");
209
    /// # let blinded_id =
210
    /// #    hex!("43cc0d62fc6252f578705ca645a46109e265290343b1137e90189744b20b3f2d").into();
211
    /// # let subcredential =
212
    /// #    hex!("78210A0D2C72BB7A0CAF606BCD938B9A3696894FDDDBC3B87D424753A7E3DF37").into();
213
    /// # let timestamp = humantime::parse_rfc3339("2023-01-23T15:00:00Z").unwrap();
214
    /// #
215
    /// // Parse the descriptor
216
    /// let unchecked_desc = HsDesc::parse(unparsed_desc, &blinded_id)?;
217
    /// // Validate the signature and timeliness of the outer document
218
    /// let checked_desc = unchecked_desc
219
    ///     .check_signature()?
220
    ///     .check_valid_at(&timestamp)?;
221
    /// // Decrypt the outer and inner layers of the descriptor
222
    /// let unchecked_decrypted_desc = checked_desc.decrypt(&subcredential, None)?;
223
    /// // Validate the signature and timeliness of the inner document
224
    /// let hsdesc = unchecked_decrypted_desc
225
    ///     .check_valid_at(&timestamp)?
226
    ///     .check_signature()?;
227
    /// # Ok::<(), anyhow::Error>(())
228
    /// ```
229
355
    pub fn parse(
230
355
        input: &str,
231
355
        // We don't actually need this to parse the HsDesc, but we _do_ need it to prevent
232
355
        // a nasty pattern where we forget to check that we got the right one.
233
355
        blinded_onion_id: &HsBlindId,
234
355
    ) -> Result<UncheckedEncryptedHsDesc> {
235
355
        let outer = outer::HsDescOuter::parse(input)?;
236
355
        let mut id_matches = false;
237
368
        let result = outer.dangerously_map(|timebound| {
238
355
            timebound.dangerously_map(|outer| {
239
355
                id_matches = blinded_onion_id == &outer.blinded_id();
240
355
                EncryptedHsDesc::from_outer_doc(outer)
241
355
            })
242
355
        });
243
355
        if !id_matches {
244
2
            return Err(
245
2
                EK::BadObjectVal.with_msg("onion service descriptor did not have the expected ID")
246
2
            );
247
353
        }
248

            
249
353
        Ok(result)
250
355
    }
251

            
252
    /// A convenience function for parsing, decrypting and validating HS descriptors.
253
    ///
254
    /// This function:
255
    ///   * parses the outermost document of the descriptor in `input`, and validates that its
256
    ///     identity is consistent with `blinded_onion_id`.
257
    ///   * decrypts both layers of encryption in the onion service descriptor. If `hsc_desc_enc`
258
    ///     is provided, we use it to decrypt the inner encryption layer;
259
    ///     otherwise, we require that
260
    ///     the inner document is encrypted using the "no restricted discovery" method.
261
    ///   * checks if both layers are valid at the `valid_at` timestamp
262
    ///   * validates the signatures on both layers
263
    ///
264
    /// Returns an error if the descriptor cannot be parsed, or if one of the validation steps
265
    /// fails.
266
98
    pub fn parse_decrypt_validate(
267
98
        input: &str,
268
98
        blinded_onion_id: &HsBlindId,
269
98
        valid_at: SystemTime,
270
98
        subcredential: &Subcredential,
271
98
        hsc_desc_enc: Option<&HsClientDescEncKeypair>,
272
98
    ) -> StdResult<TimerangeBound<Self>, HsDescError> {
273
        use HsDescError as E;
274
98
        let unchecked_desc = Self::parse(input, blinded_onion_id)
275
98
            .map_err(E::OuterParsing)?
276
98
            .check_signature()
277
98
            .map_err(|e| E::OuterValidation(e.into()))?;
278

            
279
98
        let (inner_desc, new_bounds) = {
280
            // We use is_valid_at and dangerously_into_parts instead of check_valid_at because we
281
            // need the time bounds of the outer layer (for computing the intersection with the
282
            // time bounds of the inner layer).
283
98
            unchecked_desc
284
98
                .is_valid_at(&valid_at)
285
98
                .map_err(|e| E::OuterValidation(e.into()))?;
286
            // It's safe to use dangerously_peek() as we've just checked if unchecked_desc is
287
            // valid at the current time
288
98
            let inner_timerangebound = unchecked_desc
289
98
                .dangerously_peek()
290
98
                .decrypt(subcredential, hsc_desc_enc)?;
291

            
292
98
            let new_bounds = unchecked_desc
293
98
                .intersect(&inner_timerangebound)
294
100
                .map(|(b1, b2)| (b1.cloned(), b2.cloned()));
295

            
296
98
            (inner_timerangebound, new_bounds)
297
        };
298

            
299
98
        let hsdesc = inner_desc
300
98
            .check_valid_at(&valid_at)
301
98
            .map_err(|e| E::InnerValidation(e.into()))?
302
98
            .check_signature()
303
98
            .map_err(|e| E::InnerValidation(e.into()))?;
304

            
305
        // If we've reached this point, it means the descriptor is valid at specified time. This
306
        // means the time bounds of the two layers definitely intersect, so new_bounds **must** be
307
        // Some. It is a bug if new_bounds is None.
308
98
        let new_bounds = new_bounds
309
98
            .ok_or_else(|| internal!("failed to compute TimerangeBounds for a valid descriptor"))?;
310

            
311
98
        Ok(TimerangeBound::new(hsdesc, new_bounds))
312
98
    }
313

            
314
    /// One or more introduction points used to contact the onion service.
315
    ///
316
    /// Always returns at least one introduction point,
317
    /// and never more than [`NUM_INTRO_POINT_MAX`](tor_hscrypto::NUM_INTRO_POINT_MAX).
318
    /// (Descriptors which have fewer or more are dealt with during parsing.)
319
    ///
320
    /// Accessor function.
321
    //
322
    // TODO: We'd like to derive this, but amplify::Getters  would give us &Vec<>,
323
    // not &[].
324
    //
325
    // Perhaps someday we can use derive_deftly, or add as_ref() support?
326
296
    pub fn intro_points(&self) -> &[IntroPointDesc] {
327
296
        &self.intro_points
328
296
    }
329

            
330
    /// Return true if this onion service claims to be a non-anonymous "single
331
    /// onion service".
332
    ///
333
    /// (We should always anonymize our own connection to an onion service.)
334
49
    pub fn is_single_onion_service(&self) -> bool {
335
49
        self.is_single_onion_service
336
49
    }
337

            
338
    /// Return true if this onion service claims that it needs user authentication
339
    /// of some kind in its INTRODUCE messages.
340
    ///
341
    /// (Arti does not currently support sending this kind of authentication.)
342
    pub fn requires_intro_authentication(&self) -> bool {
343
        self.auth_required.is_some()
344
    }
345

            
346
    /// Get a list of offered proof-of-work parameters, at most one per type.
347
49
    pub fn pow_params(&self) -> &[pow::PowParams] {
348
49
        self.pow_params.slice()
349
49
    }
350
}
351

            
352
/// An error returned by [`HsDesc::parse_decrypt_validate`], indicating what
353
/// kind of failure prevented us from validating an onion service descriptor.
354
///
355
/// This is distinct from [`tor_netdoc::Error`](crate::Error) so that we can
356
/// tell errors that could be the HsDir's fault from those that are definitely
357
/// protocol violations by the onion service.
358
#[derive(Clone, Debug, thiserror::Error)]
359
#[non_exhaustive]
360
pub enum HsDescError {
361
    /// An outer object failed parsing: the HsDir should probably have
362
    /// caught this, and not given us this HsDesc.
363
    ///
364
    /// (This can be an innocent error if we happen to know about restrictions
365
    /// that the HsDir does not).
366
    #[error("Parsing failure on outer layer of an onion service descriptor.")]
367
    OuterParsing(#[source] crate::Error),
368

            
369
    /// An outer object failed validation: the HsDir should probably have
370
    /// caught this, and not given us this HsDesc.
371
    ///
372
    /// (This can happen erroneously if we think that something is untimely but
373
    /// the HSDir's clock is slightly different, or _was_ different when it
374
    /// decided to give us this object.)
375
    #[error("Validation failure on outer layer of an onion service descriptor.")]
376
    OuterValidation(#[source] crate::Error),
377

            
378
    /// Decrypting the inner layer failed because we need to have a decryption key,
379
    /// but we didn't provide one.
380
    ///
381
    /// This is probably our fault.
382
    #[error("Decryption failure on onion service descriptor: missing decryption key")]
383
    MissingDecryptionKey,
384

            
385
    /// Decrypting the inner layer failed because, although we provided a key,
386
    /// we did not provide the key we need to decrypt it.
387
    ///
388
    /// This is probably our fault.
389
    #[error("Decryption failure on onion service descriptor: incorrect decryption key")]
390
    WrongDecryptionKey,
391

            
392
    /// Decrypting the inner or middle layer failed because of an issue with the
393
    /// decryption itself.
394
    ///
395
    /// This is the onion service's fault.
396
    #[error("Decryption failure on onion service descriptor: could not decrypt")]
397
    DecryptionFailed,
398

            
399
    /// We failed to parse something cryptographic in an inner layer of the
400
    /// onion service descriptor.
401
    ///
402
    /// This is definitely the onion service's fault.
403
    #[error("Parsing failure on inner layer of an onion service descriptor")]
404
    InnerParsing(#[source] crate::Error),
405

            
406
    /// We failed to validate something cryptographic in an inner layer of the
407
    /// onion service descriptor.
408
    ///
409
    /// This is definitely the onion service's fault.
410
    #[error("Validation failure on inner layer of an onion service descriptor")]
411
    InnerValidation(#[source] crate::Error),
412

            
413
    /// We encountered an internal error.
414
    #[error("Internal error: {0}")]
415
    Bug(#[from] tor_error::Bug),
416
}
417

            
418
impl tor_error::HasKind for HsDescError {
419
    fn kind(&self) -> tor_error::ErrorKind {
420
        use HsDescError as E;
421
        use tor_error::ErrorKind as EK;
422
        match self {
423
            E::OuterParsing(_) | E::OuterValidation(_) => EK::TorProtocolViolation,
424
            E::MissingDecryptionKey => EK::OnionServiceMissingClientAuth,
425
            E::WrongDecryptionKey => EK::OnionServiceWrongClientAuth,
426
            E::DecryptionFailed | E::InnerParsing(_) | E::InnerValidation(_) => {
427
                EK::OnionServiceProtocolViolation
428
            }
429
            E::Bug(e) => e.kind(),
430
        }
431
    }
432
}
433

            
434
impl HsDescError {
435
    /// Return true if this error is one that we should report as a suspicious event.
436
    ///
437
    /// Note that this is a defense-in-depth check
438
    /// for resisting descriptor-length inflation attacks:
439
    /// Our limits on total download size and/or total cell counts are the defense
440
    /// that really matters.
441
    /// (See prop360 for more information.)
442
    pub fn should_report_as_suspicious(&self) -> bool {
443
        use crate::NetdocErrorKind as EK;
444
        use HsDescError as E;
445
        #[allow(clippy::match_like_matches_macro)]
446
        match self {
447
            E::OuterParsing(e) => match e.netdoc_error_kind() {
448
                EK::ExtraneousSpace => true,
449
                EK::WrongEndingToken => true,
450
                EK::MissingKeyword => true,
451
                _ => false,
452
            },
453
            E::OuterValidation(e) => match e.netdoc_error_kind() {
454
                EK::BadSignature => true,
455
                _ => false,
456
            },
457
            E::MissingDecryptionKey => false,
458
            E::WrongDecryptionKey => false,
459
            E::DecryptionFailed => false,
460
            E::InnerParsing(_) => false,
461
            E::InnerValidation(_) => false,
462
            E::Bug(_) => false,
463
        }
464
    }
465
}
466

            
467
impl IntroPointDesc {
468
    /// Start building a description of an intro point
469
588
    pub fn builder() -> IntroPointDescBuilder {
470
588
        IntroPointDescBuilder::default()
471
588
    }
472

            
473
    /// The list of link specifiers needed to extend a circuit to the introduction point.
474
    ///
475
    /// These can include public keys and network addresses.
476
    ///
477
    /// Accessor function.
478
    //
479
    // TODO: It would be better to derive this too, but this accessor needs to
480
    // return a slice; Getters can only give us a &Vec<> in this case.
481
147
    pub fn link_specifiers(&self) -> &[EncodedLinkSpec] {
482
147
        &self.link_specifiers
483
147
    }
484
}
485

            
486
impl EncryptedHsDesc {
487
    /// Attempt to decrypt both layers of encryption in this onion service
488
    /// descriptor.
489
    ///
490
    /// If `hsc_desc_enc` is provided, we use it to decrypt the inner encryption layer;
491
    /// otherwise, we require that the inner document is encrypted using the "no
492
    /// restricted discovery" method.
493
    //
494
    // TODO: Someday we _might_ want to allow a list of keypairs in place of
495
    // `hs_desc_enc`.  For now, though, we always know a single key that we want
496
    // to try using, and we don't want to leak any extra information by
497
    // providing other keys that _might_ work.  We certainly don't want to
498
    // encourage people to provide every key they know.
499
353
    pub fn decrypt(
500
353
        &self,
501
353
        subcredential: &Subcredential,
502
353
        hsc_desc_enc: Option<&HsClientDescEncKeypair>,
503
353
    ) -> StdResult<TimerangeBound<SignatureGated<HsDesc>>, HsDescError> {
504
        use HsDescError as E;
505
353
        let blinded_id = self.outer_doc.blinded_id();
506
353
        let revision_counter = self.outer_doc.revision_counter();
507
353
        let kp_desc_sign = self.outer_doc.desc_sign_key_id();
508

            
509
        // Decrypt the superencryption layer; parse the middle document.
510
353
        let middle = self
511
353
            .outer_doc
512
353
            .decrypt_body(subcredential)
513
353
            .map_err(|_| E::DecryptionFailed)?;
514
353
        let middle = std::str::from_utf8(&middle[..]).map_err(|_| {
515
            E::InnerParsing(EK::BadObjectVal.with_msg("Bad utf-8 in middle document"))
516
        })?;
517
353
        let middle = middle::HsDescMiddle::parse(middle).map_err(E::InnerParsing)?;
518

            
519
        // Decrypt the encryption layer and parse the inner document.
520
353
        let inner = middle.decrypt_inner(
521
353
            &blinded_id,
522
353
            revision_counter,
523
353
            subcredential,
524
357
            hsc_desc_enc.map(|keys| keys.secret()),
525
2
        )?;
526
351
        let inner = std::str::from_utf8(&inner[..]).map_err(|_| {
527
            E::InnerParsing(EK::BadObjectVal.with_msg("Bad utf-8 in inner document"))
528
        })?;
529
351
        let (cert_signing_key, time_bound) =
530
351
            inner::HsDescInner::parse(inner).map_err(E::InnerParsing)?;
531

            
532
351
        if cert_signing_key.as_ref() != Some(kp_desc_sign) {
533
            return Err(E::InnerValidation(EK::BadObjectVal.with_msg(
534
                "Signing keys in inner document did not match those in outer document",
535
            )));
536
351
        }
537

            
538
        // Construct the HsDesc!
539
362
        let time_bound = time_bound.dangerously_map(|sig_bound| {
540
351
            sig_bound.dangerously_map(|inner| HsDesc {
541
351
                idx_info: IndexInfo::from_outer_doc(&self.outer_doc),
542
351
                auth_required: inner.intro_auth_types,
543
351
                is_single_onion_service: inner.single_onion_service,
544
351
                intro_points: inner.intro_points,
545
351
                pow_params: inner.pow_params,
546
351
            })
547
351
        });
548
351
        Ok(time_bound)
549
353
    }
550

            
551
    /// Create a new `IndexInfo` from the outer part of an onion service descriptor.
552
355
    fn from_outer_doc(outer_layer: outer::HsDescOuter) -> Self {
553
355
        EncryptedHsDesc {
554
355
            outer_doc: outer_layer,
555
355
        }
556
355
    }
557
}
558

            
559
impl IndexInfo {
560
    /// Create a new `IndexInfo` from the outer part of an onion service descriptor.
561
353
    fn from_outer_doc(outer: &outer::HsDescOuter) -> Self {
562
353
        IndexInfo {
563
353
            lifetime: outer.lifetime,
564
353
            signing_cert_expires: outer.desc_signing_key_cert.expiry(),
565
353
            revision: outer.revision_counter(),
566
353
        }
567
353
    }
568
}
569

            
570
#[cfg(feature = "hs-dir")]
571
impl StoredHsDescMeta {
572
    /// Create a new `StoredHsDescMeta` from the outer part of an onion service descriptor.
573
2
    fn from_outer_doc(outer: &outer::HsDescOuter) -> Self {
574
2
        let blinded_id = outer.blinded_id();
575
2
        let idx_info = IndexInfo::from_outer_doc(outer);
576
2
        StoredHsDescMeta {
577
2
            blinded_id,
578
2
            idx_info,
579
2
        }
580
2
    }
581
}
582

            
583
/// Test data
584
#[cfg(any(test, feature = "testing"))]
585
#[allow(missing_docs)]
586
#[allow(clippy::missing_docs_in_private_items)]
587
#[allow(clippy::unwrap_used)]
588
pub mod test_data {
589
    use super::*;
590
    use hex_literal::hex;
591

            
592
    pub const TEST_DATA: &str = include_str!("../../testdata/hsdesc1.txt");
593

            
594
    pub const TEST_SUBCREDENTIAL: [u8; 32] =
595
        hex!("78210A0D2C72BB7A0CAF606BCD938B9A3696894FDDDBC3B87D424753A7E3DF37");
596

            
597
    // This HsDesc uses DescEnc authentication.
598
    pub const TEST_DATA_2: &str = include_str!("../../testdata/hsdesc2.txt");
599
    pub const TEST_DATA_TIMEPERIOD_2: u64 = 19397;
600
    // paozpdhgz2okvc6kgbxvh2bnfsmt4xergrtcl4obkhopyvwxkpjzvoad.onion
601
    pub const TEST_HSID_2: [u8; 32] =
602
        hex!("781D978CE6CE9CAA8BCA306F53E82D2C993E5C91346625F1C151DCFC56D753D3");
603
    pub const TEST_SUBCREDENTIAL_2: [u8; 32] =
604
        hex!("24A133E905102BDA9A6AFE57F901366A1B8281865A91F1FE0853E4B50CC8B070");
605
    // SACGOAEODFGCYY22NYZV45ZESFPFLDGLMBWFACKEO34XGHASSAMQ (base32)
606
    pub const TEST_PUBKEY_2: [u8; 32] =
607
        hex!("900467008E194C2C635A6E335E7724915E558CCB606C50094476F9731C129019");
608
    // SDZNMD4RP4SCH4EYTTUZPFRZINNFWAOPPKZ6BINZAC7LREV24RBQ (base32)
609
    pub const TEST_SECKEY_2: [u8; 32] =
610
        hex!("90F2D60F917F2423F0989CE9979639435A5B01CF7AB3E0A1B900BEB892BAE443");
611

            
612
    /// K_hs_blind_id that can be used to parse [`TEST_DATA`]
613
    ///
614
    /// `pub(crate)` mostly because it's difficult to describe what TP it's for.
615
    pub(crate) const TEST_DATA_HS_BLIND_ID: [u8; 32] =
616
        hex!("43cc0d62fc6252f578705ca645a46109e265290343b1137e90189744b20b3f2d");
617

            
618
    /// Obtain a testing [`HsDesc`]
619
    pub fn test_parsed_hsdesc() -> Result<HsDesc> {
620
        let blinded_id = TEST_DATA_HS_BLIND_ID.into();
621

            
622
        let desc = HsDesc::parse(TEST_DATA, &blinded_id)?
623
            .check_signature()?
624
            .check_valid_at(&humantime::parse_rfc3339("2023-01-23T15:00:00Z").unwrap())
625
            .unwrap()
626
            .decrypt(&TEST_SUBCREDENTIAL.into(), None)
627
            .unwrap();
628
        let desc = desc
629
            .check_valid_at(&humantime::parse_rfc3339("2023-01-24T03:00:00Z").unwrap())
630
            .unwrap();
631
        let desc = desc.check_signature().unwrap();
632
        Ok(desc)
633
    }
634
}
635

            
636
#[cfg(test)]
637
mod test {
638
    // @@ begin test lint list maintained by maint/add_warning @@
639
    #![allow(clippy::bool_assert_comparison)]
640
    #![allow(clippy::clone_on_copy)]
641
    #![allow(clippy::dbg_macro)]
642
    #![allow(clippy::mixed_attributes_style)]
643
    #![allow(clippy::print_stderr)]
644
    #![allow(clippy::print_stdout)]
645
    #![allow(clippy::single_char_pattern)]
646
    #![allow(clippy::unwrap_used)]
647
    #![allow(clippy::unchecked_time_subtraction)]
648
    #![allow(clippy::useless_vec)]
649
    #![allow(clippy::needless_pass_by_value)]
650
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
651
    use std::time::Duration;
652

            
653
    use super::test_data::*;
654
    use super::*;
655
    use hex_literal::hex;
656
    use tor_hscrypto::{pk::HsIdKey, time::TimePeriod};
657
    use tor_llcrypto::pk::ed25519;
658

            
659
    #[test]
660
    #[cfg(feature = "hs-dir")]
661
    fn parse_meta_good() -> Result<()> {
662
        let meta = StoredHsDescMeta::parse(TEST_DATA)?
663
            .check_signature()?
664
            .check_valid_at(&humantime::parse_rfc3339("2023-01-23T15:00:00Z").unwrap())
665
            .unwrap();
666

            
667
        assert_eq!(meta.blinded_id.as_ref(), &TEST_DATA_HS_BLIND_ID);
668
        assert_eq!(
669
            Duration::try_from(meta.idx_info.lifetime).unwrap(),
670
            Duration::from_secs(60 * 180)
671
        );
672
        assert_eq!(
673
            meta.idx_info.signing_cert_expires,
674
            humantime::parse_rfc3339("2023-01-26T03:00:00Z").unwrap()
675
        );
676
        assert_eq!(meta.idx_info.revision, RevisionCounter::from(19655750));
677

            
678
        Ok(())
679
    }
680

            
681
    #[test]
682
    fn parse_desc_good() -> Result<()> {
683
        let wrong_blinded_id = [12; 32].into();
684
        let desc = HsDesc::parse(TEST_DATA, &wrong_blinded_id);
685
        assert!(desc.is_err());
686
        let desc = test_parsed_hsdesc()?;
687

            
688
        assert_eq!(
689
            Duration::try_from(desc.idx_info.lifetime).unwrap(),
690
            Duration::from_secs(60 * 180)
691
        );
692
        assert_eq!(
693
            desc.idx_info.signing_cert_expires,
694
            humantime::parse_rfc3339("2023-01-26T03:00:00Z").unwrap()
695
        );
696
        assert_eq!(desc.idx_info.revision, RevisionCounter::from(19655750));
697
        assert!(desc.auth_required.is_none());
698
        assert_eq!(desc.is_single_onion_service, false);
699
        assert_eq!(desc.intro_points.len(), 3);
700

            
701
        let ipt0 = &desc.intro_points()[0];
702
        assert_eq!(
703
            ipt0.ipt_ntor_key().as_bytes(),
704
            &hex!("553BF9F9E1979D6F5D5D7D20BB3FE7272E32E22B6E86E35C76A7CA8A377E402F")
705
        );
706
        // TODO TEST: Perhaps add tests for other intro point fields.
707

            
708
        Ok(())
709
    }
710

            
711
    /// Get an EncryptedHsDesc corresponding to `TEST_DATA_2`.
712
    fn get_test2_encrypted() -> EncryptedHsDesc {
713
        let id: HsIdKey = ed25519::PublicKey::from_bytes(&TEST_HSID_2).unwrap().into();
714
        let period = TimePeriod::new(
715
            humantime::parse_duration("24 hours").unwrap(),
716
            humantime::parse_rfc3339("2023-02-09T12:00:00Z").unwrap(),
717
            humantime::parse_duration("12 hours").unwrap(),
718
        )
719
        .unwrap();
720
        assert_eq!(period.interval_num(), TEST_DATA_TIMEPERIOD_2);
721
        let (blind_id, subcredential) = id.compute_blinded_key(period).unwrap();
722

            
723
        assert_eq!(
724
            blind_id.as_bytes(),
725
            &hex!("706628758208395D461AA0F460A5E76E7B828C66B5E794768592B451302E961D")
726
        );
727

            
728
        assert_eq!(subcredential.as_ref(), &TEST_SUBCREDENTIAL_2);
729

            
730
        HsDesc::parse(TEST_DATA_2, &blind_id.into())
731
            .unwrap()
732
            .check_signature()
733
            .unwrap()
734
            .check_valid_at(&humantime::parse_rfc3339("2023-02-09T12:00:00Z").unwrap())
735
            .unwrap()
736
    }
737

            
738
    #[test]
739
    fn parse_desc_auth_missing() {
740
        // If we try to decrypt TEST_DATA_2 with no ClientDescEncKey, we get a
741
        // failure.
742
        let encrypted = get_test2_encrypted();
743
        let subcredential = TEST_SUBCREDENTIAL_2.into();
744
        let with_no_auth = encrypted.decrypt(&subcredential, None);
745
        assert!(with_no_auth.is_err());
746
    }
747

            
748
    #[test]
749
    fn parse_desc_auth_good() {
750
        // But if we try to decrypt TEST_DATA_2 with the correct ClientDescEncKey, we get a
751
        // the data inside!
752

            
753
        let encrypted = get_test2_encrypted();
754
        let subcredential = TEST_SUBCREDENTIAL_2.into();
755
        let pk = curve25519::PublicKey::from(TEST_PUBKEY_2).into();
756
        let sk = curve25519::StaticSecret::from(TEST_SECKEY_2).into();
757
        let desc = encrypted
758
            .decrypt(&subcredential, Some(&HsClientDescEncKeypair::new(pk, sk)))
759
            .unwrap();
760
        let desc = desc
761
            .check_valid_at(&humantime::parse_rfc3339("2023-01-24T03:00:00Z").unwrap())
762
            .unwrap();
763
        let desc = desc.check_signature().unwrap();
764
        assert_eq!(desc.intro_points.len(), 3);
765
    }
766
}