1
//! Hidden service descriptor encoding.
2

            
3
mod inner;
4
mod middle;
5
mod outer;
6

            
7
use crate::NetdocBuilder;
8
use crate::doc::hsdesc::{IntroAuthType, IntroPointDesc};
9
use rand::{CryptoRng, RngCore};
10
use tor_bytes::EncodeError;
11
use tor_cell::chancell::msg::HandshakeType;
12
use tor_cert::{CertEncodeError, CertType, CertifiedKey, Ed25519Cert, EncodedEd25519Cert};
13
use tor_error::into_bad_api_usage;
14
use tor_hscrypto::pk::{HsBlindIdKey, HsBlindIdKeypair, HsSvcDescEncKeypair};
15
use tor_hscrypto::{RevisionCounter, Subcredential};
16
use tor_llcrypto::pk::curve25519;
17
use tor_llcrypto::pk::ed25519;
18
use tor_units::IntegerMinutes;
19

            
20
use derive_builder::Builder;
21
use smallvec::SmallVec;
22

            
23
use std::borrow::{Borrow, Cow};
24
use std::time::SystemTime;
25

            
26
use self::inner::HsDescInner;
27
use self::middle::HsDescMiddle;
28
use self::outer::HsDescOuter;
29

            
30
use super::desc_enc::{HS_DESC_ENC_NONCE_LEN, HsDescEncNonce, HsDescEncryption};
31
use super::pow::PowParams;
32

            
33
/// An intermediary type for encoding hidden service descriptors.
34
///
35
/// This object is constructed via [`HsDescBuilder`], and then turned into a
36
/// signed document using [`HsDescBuilder::build_sign()`].
37
///
38
/// TODO: Add an example for using this API.
39
88
#[derive(Builder)]
40
#[builder(public, derive(Debug, Clone), pattern = "owned", build_fn(vis = ""))]
41
struct HsDesc<'a> {
42
    /// The blinded hidden service public key used for the first half of the "SECRET_DATA" field.
43
    ///
44
    /// (See rend-spec v3 2.5.1.1 and 2.5.2.1.)
45
    blinded_id: &'a HsBlindIdKey,
46
    /// The short-term descriptor signing key (KP_hs_desc_sign, KS_hs_desc_sign).
47
    hs_desc_sign: &'a ed25519::Keypair,
48
    /// The descriptor signing key certificate.
49
    ///
50
    /// This certificate can be created using [`create_desc_sign_key_cert`].
51
    hs_desc_sign_cert: EncodedEd25519Cert,
52
    /// A list of recognized CREATE handshakes that this onion service supports.
53
    create2_formats: &'a [HandshakeType],
54
    /// A list of authentication types that this onion service supports.
55
    auth_required: Option<SmallVec<[IntroAuthType; 2]>>,
56
    /// If true, this a "single onion service" and is not trying to keep its own location private.
57
    is_single_onion_service: bool,
58
    /// One or more introduction points used to contact the onion service.
59
    intro_points: &'a [IntroPointDesc],
60
    /// The expiration time of an introduction point authentication key certificate.
61
    intro_auth_key_cert_expiry: SystemTime,
62
    /// The expiration time of an introduction point encryption key certificate.
63
    intro_enc_key_cert_expiry: SystemTime,
64
    /// Proof-of-work parameters.
65
    #[builder(default)]
66
    #[cfg(feature = "hs-pow-full")]
67
    pow_params: Option<&'a PowParams>,
68

            
69
    /// The list of clients authorized to discover the hidden service.
70
    ///
71
    /// If `None`, restricted discovery is disabled.
72
    /// If `Some(&[])`, restricted discovery is enabled,
73
    /// but there will be no authorized clients.
74
    ///
75
    /// If restricted discovery is disabled, the resulting middle document will contain a single
76
    /// `auth-client` line populated with random values.
77
    ///
78
    /// Restricted discovery is disabled by default.
79
    #[builder(default)]
80
    auth_clients: Option<&'a [curve25519::PublicKey]>,
81
    /// The lifetime of this descriptor, in minutes.
82
    ///
83
    /// This doesn't actually list the starting time or the end time for the
84
    /// descriptor: presumably, because we didn't want to leak the onion
85
    /// service's view of the wallclock.
86
    lifetime: IntegerMinutes<u16>,
87
    /// A revision counter to tell whether this descriptor is more or less recent
88
    /// than another one for the same blinded ID.
89
    revision_counter: RevisionCounter,
90
    /// The "subcredential" of the onion service.
91
    subcredential: Subcredential,
92

            
93
    /// Maximum length of generated HsDesc.
94
    ///
95
    /// If the generated descriptor is larger than this, `build_sign` will return an error.
96
    #[builder(field(type = "Option<usize>", build = "()"), setter(strip_option))]
97
    #[allow(dead_code)]
98
    max_generated_len: (),
99
}
100

            
101
/// Restricted discovery parameters.
102
#[derive(Debug)]
103
pub(super) struct ClientAuth<'a> {
104
    /// An ephemeral x25519 keypair generated by the hidden service (`KP_hss_desc_enc`).
105
    ///
106
    /// A new keypair MUST be generated every time a descriptor is encoded, or the descriptor
107
    /// encryption will not be secure.
108
    ephemeral_key: HsSvcDescEncKeypair,
109
    /// The list of clients authorized to discover this service.
110
    auth_clients: &'a [curve25519::PublicKey],
111
    /// The `N_hs_desc_enc` descriptor_cookie key generated by the hidden service.
112
    ///
113
    /// A new descriptor cookie is randomly generated for each descriptor.
114
    descriptor_cookie: [u8; HS_DESC_ENC_NONCE_LEN],
115
}
116

            
117
impl<'a> ClientAuth<'a> {
118
    /// Create a new `ClientAuth` using the specified authorized clients.
119
    ///
120
    /// If `auth_clients` is empty list, there will be no authorized clients.
121
    ///
122
    /// This returns `None` if the list of `auth_clients` is `None`.
123
8
    fn new<R: RngCore + CryptoRng>(
124
8
        auth_clients: Option<&'a [curve25519::PublicKey]>,
125
8
        rng: &mut R,
126
8
    ) -> Option<ClientAuth<'a>> {
127
8
        let Some(auth_clients) = auth_clients else {
128
            // Restricted discovery is disabled
129
4
            return None;
130
        };
131

            
132
        // Generate a new `N_hs_desc_enc` descriptor_cookie key for this descriptor.
133
4
        let descriptor_cookie = rand::Rng::random::<[u8; HS_DESC_ENC_NONCE_LEN]>(rng);
134
4

            
135
4
        let secret = curve25519::StaticSecret::random_from_rng(rng);
136
4
        let ephemeral_key = HsSvcDescEncKeypair {
137
4
            public: curve25519::PublicKey::from(&secret).into(),
138
4
            secret: secret.into(),
139
4
        };
140
4

            
141
4
        Some(ClientAuth {
142
4
            ephemeral_key,
143
4
            auth_clients,
144
4
            descriptor_cookie,
145
4
        })
146
8
    }
147
}
148

            
149
impl<'a> NetdocBuilder for HsDescBuilder<'a> {
150
8
    fn build_sign<R: RngCore + CryptoRng>(self, rng: &mut R) -> Result<String, EncodeError> {
151
        /// The superencrypted field must be padded to the nearest multiple of 10k bytes
152
        ///
153
        /// rend-spec-v3 2.5.1.1
154
        const SUPERENCRYPTED_ALIGN: usize = 10 * (1 << 10);
155

            
156
8
        let max_generated_len = self.max_generated_len.unwrap_or(usize::MAX);
157

            
158
8
        let hs_desc = self
159
8
            .build()
160
8
            .map_err(into_bad_api_usage!("the HsDesc could not be built"))?;
161

            
162
8
        let client_auth = ClientAuth::new(hs_desc.auth_clients, rng);
163

            
164
        // Construct the inner (second layer) plaintext. This is the unencrypted value of the
165
        // "encrypted" field.
166
8
        let inner_plaintext = HsDescInner {
167
8
            hs_desc_sign: hs_desc.hs_desc_sign,
168
8
            create2_formats: hs_desc.create2_formats,
169
8
            auth_required: hs_desc.auth_required.as_ref(),
170
8
            is_single_onion_service: hs_desc.is_single_onion_service,
171
8
            intro_points: hs_desc.intro_points,
172
8
            intro_auth_key_cert_expiry: hs_desc.intro_auth_key_cert_expiry,
173
8
            intro_enc_key_cert_expiry: hs_desc.intro_enc_key_cert_expiry,
174
8
            #[cfg(feature = "hs-pow-full")]
175
8
            pow_params: hs_desc.pow_params,
176
8
        }
177
8
        .build_sign(rng)?;
178

            
179
8
        let desc_enc_nonce = client_auth
180
8
            .as_ref()
181
8
            .map(|client_auth| client_auth.descriptor_cookie.into());
182
8

            
183
8
        // Encrypt the inner document. The encrypted blob is the ciphertext contained in the
184
8
        // "encrypted" field described in section 2.5.1.2. of rend-spec-v3.
185
8
        let inner_encrypted = hs_desc.encrypt_field(
186
8
            rng,
187
8
            inner_plaintext.as_bytes(),
188
8
            desc_enc_nonce.as_ref(),
189
8
            b"hsdir-encrypted-data",
190
8
        );
191

            
192
        // Construct the middle (first player) plaintext. This is the unencrypted value of the
193
        // "superencrypted" field.
194
8
        let middle_plaintext = HsDescMiddle {
195
8
            client_auth: client_auth.as_ref(),
196
8
            subcredential: hs_desc.subcredential,
197
8
            encrypted: inner_encrypted,
198
8
        }
199
8
        .build_sign(rng)?;
200

            
201
        // Section 2.5.1.1. of rend-spec-v3: before encryption, pad the plaintext to the nearest
202
        // multiple of 10k bytes
203
8
        let middle_plaintext =
204
8
            pad_with_zero_to_align(middle_plaintext.as_bytes(), SUPERENCRYPTED_ALIGN);
205
8

            
206
8
        // Encrypt the middle document. The encrypted blob is the ciphertext contained in the
207
8
        // "superencrypted" field described in section 2.5.1.1. of rend-spec-v3.
208
8
        let middle_encrypted = hs_desc.encrypt_field(
209
8
            rng,
210
8
            middle_plaintext.borrow(),
211
8
            // desc_enc_nonce is absent when handling the superencryption layer (2.5.1.1).
212
8
            None,
213
8
            b"hsdir-superencrypted-data",
214
8
        );
215

            
216
        // Finally, build the hidden service descriptor.
217
8
        let hsdesc = HsDescOuter {
218
8
            hs_desc_sign: hs_desc.hs_desc_sign,
219
8
            hs_desc_sign_cert: hs_desc.hs_desc_sign_cert,
220
8
            lifetime: hs_desc.lifetime,
221
8
            revision_counter: hs_desc.revision_counter,
222
8
            superencrypted: middle_encrypted,
223
8
        }
224
8
        .build_sign(rng)?;
225

            
226
8
        if hsdesc.len() > max_generated_len {
227
            return Err(EncodeError::BadLengthValue);
228
8
        }
229
8
        Ok(hsdesc)
230
8
    }
231
}
232

            
233
/// Create the descriptor signing key certificate.
234
///
235
/// Returns the encoded representation of the certificate
236
/// obtained by signing the descriptor signing key `hs_desc_sign`
237
/// with the blinded id key `blind_id`.
238
///
239
/// This certificate is meant to be passed to [`HsDescBuilder::hs_desc_sign_cert`].
240
8
pub fn create_desc_sign_key_cert(
241
8
    hs_desc_sign: &ed25519::PublicKey,
242
8
    blind_id: &HsBlindIdKeypair,
243
8
    expiry: SystemTime,
244
8
) -> Result<EncodedEd25519Cert, CertEncodeError> {
245
8
    // "The certificate cross-certifies the short-term descriptor signing key with the blinded
246
8
    // public key.  The certificate type must be [08], and the blinded public key must be
247
8
    // present as the signing-key extension."
248
8
    Ed25519Cert::constructor()
249
8
        .cert_type(CertType::HS_BLINDED_ID_V_SIGNING)
250
8
        .expiration(expiry)
251
8
        .signing_key(ed25519::Ed25519Identity::from(blind_id.as_ref().public()))
252
8
        .cert_key(CertifiedKey::Ed25519(hs_desc_sign.into()))
253
8
        .encode_and_sign(blind_id)
254
8
}
255

            
256
impl<'a> HsDesc<'a> {
257
    /// Encrypt the specified plaintext using the algorithm described in section
258
    /// `[HS-DESC-ENCRYPTION-KEYS]` of rend-spec-v3.txt.
259
16
    fn encrypt_field<R: RngCore + CryptoRng>(
260
16
        &self,
261
16
        rng: &mut R,
262
16
        plaintext: &[u8],
263
16
        desc_enc_nonce: Option<&HsDescEncNonce>,
264
16
        string_const: &[u8],
265
16
    ) -> Vec<u8> {
266
16
        let encrypt = HsDescEncryption {
267
16
            blinded_id: &ed25519::Ed25519Identity::from(self.blinded_id.as_ref()).into(),
268
16
            desc_enc_nonce,
269
16
            subcredential: &self.subcredential,
270
16
            revision: self.revision_counter,
271
16
            string_const,
272
16
        };
273
16

            
274
16
        encrypt.encrypt(rng, plaintext)
275
16
    }
276
}
277

            
278
/// Pad `v` with zeroes to the next multiple of `alignment`.
279
8
fn pad_with_zero_to_align(v: &[u8], alignment: usize) -> Cow<[u8]> {
280
8
    let padding = (alignment - (v.len() % alignment)) % alignment;
281
8

            
282
8
    if padding > 0 {
283
8
        let padded = v
284
8
            .iter()
285
8
            .copied()
286
8
            .chain(std::iter::repeat_n(0, padding))
287
8
            .collect::<Vec<_>>();
288
8

            
289
8
        Cow::Owned(padded)
290
    } else {
291
        // No need to pad.
292
        Cow::Borrowed(v)
293
    }
294
8
}
295

            
296
#[cfg(test)]
297
mod test {
298
    // @@ begin test lint list maintained by maint/add_warning @@
299
    #![allow(clippy::bool_assert_comparison)]
300
    #![allow(clippy::clone_on_copy)]
301
    #![allow(clippy::dbg_macro)]
302
    #![allow(clippy::mixed_attributes_style)]
303
    #![allow(clippy::print_stderr)]
304
    #![allow(clippy::print_stdout)]
305
    #![allow(clippy::single_char_pattern)]
306
    #![allow(clippy::unwrap_used)]
307
    #![allow(clippy::unchecked_duration_subtraction)]
308
    #![allow(clippy::useless_vec)]
309
    #![allow(clippy::needless_pass_by_value)]
310
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
311

            
312
    use std::net::Ipv4Addr;
313
    use std::time::Duration;
314

            
315
    use super::*;
316
    use crate::doc::hsdesc::{EncryptedHsDesc, HsDesc as ParsedHsDesc};
317
    use tor_basic_utils::test_rng::Config;
318
    use tor_checkable::{SelfSigned, Timebound};
319
    use tor_hscrypto::pk::{HsClientDescEncKeypair, HsIdKeypair};
320
    use tor_hscrypto::time::TimePeriod;
321
    use tor_linkspec::LinkSpec;
322
    use tor_llcrypto::pk::{curve25519, ed25519::ExpandedKeypair};
323

            
324
    // TODO: move the test helpers to a separate module and make them more broadly available if
325
    // necessary.
326

            
327
    /// Expect `err` to be a `Bug`, and return its string representation.
328
    ///
329
    /// # Panics
330
    ///
331
    /// Panics if `err` is not a `Bug`.
332
    pub(super) fn expect_bug(err: EncodeError) -> String {
333
        match err {
334
            EncodeError::Bug(b) => b.to_string(),
335
            EncodeError::BadLengthValue => panic!("expected Bug, got BadLengthValue"),
336
            _ => panic!("expected Bug, got unknown error"),
337
        }
338
    }
339

            
340
    pub(super) fn create_intro_point_descriptor<R: RngCore + CryptoRng>(
341
        rng: &mut R,
342
        link_specifiers: &[LinkSpec],
343
    ) -> IntroPointDesc {
344
        let link_specifiers = link_specifiers
345
            .iter()
346
            .map(|link_spec| link_spec.encode())
347
            .collect::<Result<Vec<_>, _>>()
348
            .unwrap();
349

            
350
        IntroPointDesc {
351
            link_specifiers,
352
            ipt_ntor_key: create_curve25519_pk(rng),
353
            ipt_sid_key: ed25519::Keypair::generate(rng).verifying_key().into(),
354
            svc_ntor_key: create_curve25519_pk(rng).into(),
355
        }
356
    }
357

            
358
    /// Create a new curve25519 public key.
359
    pub(super) fn create_curve25519_pk<R: RngCore + CryptoRng>(
360
        rng: &mut R,
361
    ) -> curve25519::PublicKey {
362
        let ephemeral_key = curve25519::EphemeralSecret::random_from_rng(rng);
363
        (&ephemeral_key).into()
364
    }
365

            
366
    /// Parse the specified hidden service descriptor.
367
    fn parse_hsdesc(
368
        unparsed_desc: &str,
369
        blinded_pk: ed25519::PublicKey,
370
        subcredential: &Subcredential,
371
        hsc_desc_enc: Option<&HsClientDescEncKeypair>,
372
    ) -> ParsedHsDesc {
373
        const TIMESTAMP: &str = "2023-01-23T15:00:00Z";
374

            
375
        let id = ed25519::Ed25519Identity::from(blinded_pk);
376
        let enc_desc: EncryptedHsDesc = ParsedHsDesc::parse(unparsed_desc, &id.into())
377
            .unwrap()
378
            .check_signature()
379
            .unwrap()
380
            .check_valid_at(&humantime::parse_rfc3339(TIMESTAMP).unwrap())
381
            .unwrap();
382

            
383
        enc_desc
384
            .decrypt(subcredential, hsc_desc_enc)
385
            .unwrap()
386
            .check_valid_at(&humantime::parse_rfc3339(TIMESTAMP).unwrap())
387
            .unwrap()
388
            .check_signature()
389
            .unwrap()
390
    }
391

            
392
    #[test]
393
    fn encode_decode() {
394
        const CREATE2_FORMATS: &[HandshakeType] = &[HandshakeType::TAP, HandshakeType::NTOR];
395
        const LIFETIME_MINS: u16 = 100;
396
        const REVISION_COUNT: u64 = 2;
397
        const CERT_EXPIRY_SECS: u64 = 60 * 60;
398

            
399
        let mut rng = Config::Deterministic.into_rng();
400
        // The identity keypair of the hidden service.
401
        let hs_id = ed25519::Keypair::generate(&mut rng);
402
        let hs_desc_sign = ed25519::Keypair::generate(&mut rng);
403
        let period = TimePeriod::new(
404
            humantime::parse_duration("24 hours").unwrap(),
405
            humantime::parse_rfc3339("2023-02-09T12:00:00Z").unwrap(),
406
            humantime::parse_duration("12 hours").unwrap(),
407
        )
408
        .unwrap();
409
        let (_, blinded_id, subcredential) = HsIdKeypair::from(ExpandedKeypair::from(&hs_id))
410
            .compute_blinded_key(period)
411
            .unwrap();
412

            
413
        let expiry = SystemTime::now() + Duration::from_secs(CERT_EXPIRY_SECS);
414
        let mut rng = Config::Deterministic.into_rng();
415
        let intro_points = vec![IntroPointDesc {
416
            link_specifiers: vec![
417
                LinkSpec::OrPort(Ipv4Addr::LOCALHOST.into(), 9999)
418
                    .encode()
419
                    .unwrap(),
420
            ],
421
            ipt_ntor_key: create_curve25519_pk(&mut rng),
422
            ipt_sid_key: ed25519::Keypair::generate(&mut rng).verifying_key().into(),
423
            svc_ntor_key: create_curve25519_pk(&mut rng).into(),
424
        }];
425

            
426
        let hs_desc_sign_cert =
427
            create_desc_sign_key_cert(&hs_desc_sign.verifying_key(), &blinded_id, expiry).unwrap();
428
        let blinded_pk = (&blinded_id).into();
429
        let builder = HsDescBuilder::default()
430
            .blinded_id(&blinded_pk)
431
            .hs_desc_sign(&hs_desc_sign)
432
            .hs_desc_sign_cert(hs_desc_sign_cert)
433
            .create2_formats(CREATE2_FORMATS)
434
            .auth_required(None)
435
            .is_single_onion_service(true)
436
            .intro_points(&intro_points)
437
            .intro_auth_key_cert_expiry(expiry)
438
            .intro_enc_key_cert_expiry(expiry)
439
            .lifetime(LIFETIME_MINS.into())
440
            .revision_counter(REVISION_COUNT.into())
441
            .subcredential(subcredential);
442

            
443
        // Build and encode a new descriptor (cloning `builder` because it's needed later, when we
444
        // test if restricted discovery works):
445
        let encoded_desc = builder
446
            .clone()
447
            .build_sign(&mut Config::Deterministic.into_rng())
448
            .unwrap();
449

            
450
        // Now decode it...
451
        let desc = parse_hsdesc(
452
            encoded_desc.as_str(),
453
            *blinded_id.as_ref().public(),
454
            &subcredential,
455
            None, /* No restricted discovery */
456
        );
457

            
458
        let hs_desc_sign_cert =
459
            create_desc_sign_key_cert(&hs_desc_sign.verifying_key(), &blinded_id, expiry).unwrap();
460
        // ...and build a new descriptor using the information from the parsed descriptor,
461
        // asserting that the resulting descriptor is identical to the original.
462
        let reencoded_desc = HsDescBuilder::default()
463
            .blinded_id(&(&blinded_id).into())
464
            .hs_desc_sign(&hs_desc_sign)
465
            .hs_desc_sign_cert(hs_desc_sign_cert)
466
            // create2_formats is hard-coded rather than extracted from desc, because
467
            // create2_formats is ignored while parsing
468
            .create2_formats(CREATE2_FORMATS)
469
            .auth_required(None)
470
            .is_single_onion_service(desc.is_single_onion_service)
471
            .intro_points(&intro_points)
472
            .intro_auth_key_cert_expiry(expiry)
473
            .intro_enc_key_cert_expiry(expiry)
474
            .lifetime(desc.idx_info.lifetime)
475
            .revision_counter(desc.idx_info.revision)
476
            .subcredential(subcredential)
477
            .build_sign(&mut Config::Deterministic.into_rng())
478
            .unwrap();
479

            
480
        assert_eq!(&*encoded_desc, &*reencoded_desc);
481

            
482
        // The same test, this time with restricted discovery enabled
483
        // (with a single authorized client):
484
        let client_kp: HsClientDescEncKeypair = HsClientDescEncKeypair::generate(&mut rng);
485
        let client_pkey = client_kp.public().as_ref();
486
        let auth_clients = vec![*client_pkey];
487

            
488
        let encoded_desc = builder
489
            .auth_clients(Some(&auth_clients[..]))
490
            .build_sign(&mut Config::Deterministic.into_rng())
491
            .unwrap();
492

            
493
        // Now decode it...
494
        let desc = parse_hsdesc(
495
            encoded_desc.as_str(),
496
            *blinded_id.as_ref().public(),
497
            &subcredential,
498
            Some(&client_kp), /* With restricted discovery */
499
        );
500

            
501
        let hs_desc_sign_cert =
502
            create_desc_sign_key_cert(&hs_desc_sign.verifying_key(), &blinded_id, expiry).unwrap();
503
        // ...and build a new descriptor using the information from the parsed descriptor,
504
        // asserting that the resulting descriptor is identical to the original.
505
        let reencoded_desc = HsDescBuilder::default()
506
            .blinded_id(&(&blinded_id).into())
507
            .hs_desc_sign(&hs_desc_sign)
508
            .hs_desc_sign_cert(hs_desc_sign_cert)
509
            // create2_formats is hard-coded rather than extracted from desc, because
510
            // create2_formats is ignored while parsing
511
            .create2_formats(CREATE2_FORMATS)
512
            .auth_required(None)
513
            .is_single_onion_service(desc.is_single_onion_service)
514
            .intro_points(&intro_points)
515
            .intro_auth_key_cert_expiry(expiry)
516
            .intro_enc_key_cert_expiry(expiry)
517
            .auth_clients(Some(&auth_clients))
518
            .lifetime(desc.idx_info.lifetime)
519
            .revision_counter(desc.idx_info.revision)
520
            .subcredential(subcredential)
521
            .build_sign(&mut Config::Deterministic.into_rng())
522
            .unwrap();
523

            
524
        assert_eq!(&*encoded_desc, &*reencoded_desc);
525
    }
526
}