1
//!
2
//! A "router descriptor" is a signed statement that a relay makes
3
//! about itself, explaining its keys, its capabilities, its location,
4
//! and its status.
5
//!
6
//! Relays upload their router descriptors to authorities, which use
7
//! them to build consensus documents.  Old clients and relays used to
8
//! fetch and use router descriptors for all the relays, but nowadays they use
9
//! microdescriptors instead.
10
//!
11
//! Clients still use router descriptors when communicating with
12
//! bridges: since bridges are not passed through an authority,
13
//! clients accept their descriptors directly.
14
//!
15
//! For full information about the router descriptor format, see
16
//! [dir-spec.txt](https://spec.torproject.org/dir-spec).
17
//!
18
//! # Limitations
19
//!
20
//! TODO: This needs to get tested much more!
21
//!
22
//! TODO: This implementation can be memory-inefficient.  In practice,
23
//! it gets really expensive storing policy entries, family
24
//! descriptions, parsed keys, and things like that.  We will probably want to
25
//! de-duplicate those.
26
//!
27
//! TODO: There should be accessor functions for some or all of the
28
//! fields in RouterDesc.  I'm deferring those until I know what they
29
//! should be.
30
//!
31
//! # Availability
32
//!
33
//! Most of this module is only available when this crate is built with the
34
//! `routerdesc` feature enabled.
35
use crate::encode::{ItemEncoder, ItemValueEncodable};
36
use crate::parse::keyword::Keyword;
37
use crate::parse::parser::{Section, SectionRules};
38
use crate::parse::tokenize::{ItemResult, NetDocReader};
39
use crate::parse2::{ArgumentError, ErrorProblem, ItemValueParseable, UnparsedItem};
40
use crate::types::family::{RelayFamily, RelayFamilyIds};
41
use crate::types::policy::*;
42
use crate::types::routerdesc::*;
43
use crate::types::version::TorVersion;
44
use crate::types::{EmbeddedCert, misc::*};
45
use crate::util::PeekableIterator;
46
use crate::{AllowAnnotations, Error, KeywordEncodable, NetdocErrorKind as EK, Result};
47

            
48
use derive_deftly::Deftly;
49
use ll::pk::ed25519::Ed25519Identity;
50
use saturating_time::SaturatingTime;
51
use std::fmt::Display;
52
use std::sync::Arc;
53
use std::sync::LazyLock;
54
use std::{iter, net, time};
55
use tor_cert::{CertType, KeyUnknownCert};
56
use tor_checkable::{Timebound, signed, timed};
57
use tor_error::{internal, into_internal};
58
use tor_llcrypto as ll;
59
use tor_llcrypto::pk::rsa::RsaIdentity;
60

            
61
use digest::Digest;
62

            
63
/// Length of a router descriptor digest
64
pub const DOC_DIGEST_LEN: usize = 20;
65

            
66
/// The digest of a RouterDesc document, as reported in a NS consensus.
67
pub type RdDigest = [u8; DOC_DIGEST_LEN];
68

            
69
/// The digest of an ExtraInfo document, as reported in a RouterDesc.
70
pub type ExtraInfoDigest = [u8; DOC_DIGEST_LEN];
71

            
72
/// A router descriptor, with possible annotations.
73
#[non_exhaustive]
74
pub struct AnnotatedRouterDesc {
75
    /// Annotation for this router descriptor; possibly empty.
76
    pub ann: RouterAnnotation,
77
    /// Underlying router descriptor; signatures not checked yet.
78
    pub router: UncheckedRouterDesc,
79
}
80

            
81
/// Annotations about a router descriptor, as stored on disc.
82
#[derive(Default)]
83
#[non_exhaustive]
84
pub struct RouterAnnotation {
85
    /// Description of where we got this router descriptor
86
    pub source: Option<String>,
87
    /// When this descriptor was first downloaded.
88
    pub downloaded: Option<time::SystemTime>,
89
    /// Description of what we're willing to use this descriptor for.
90
    pub purpose: Option<String>,
91
}
92

            
93
/// Information about a relay, parsed from a router descriptor.
94
///
95
/// This type does not hold all the information in the router descriptor
96
///
97
/// # Limitations
98
///
99
/// See module documentation.
100
///
101
/// Additionally, some fields that from router descriptors are not yet
102
/// parsed: see the comments in ROUTER_BODY_RULES for information about those.
103
///
104
/// Before using this type to connect to a relay, you MUST check that
105
/// it is valid, using is_expired_at().
106
///
107
/// # Specification
108
///
109
/// <https://spec.torproject.org/dir-spec/server-descriptor-format.html>
110
#[derive(Clone, Debug)]
111
#[non_exhaustive]
112
pub struct RouterDesc {
113
    /// `router` --- Introduce a router descriptor.
114
    /// * `router <nickname> <address> <orport> <socksport> <dirport>`
115
    /// * At start, exactly once.
116
    pub router: RouterDescIntroItem,
117

            
118
    /// `identity-ed25519` --- Specify the router's ed25519 identity.
119
    ///
120
    /// <https://spec.torproject.org/dir-spec/server-descriptor-format.html#item:identity-ed25519>
121
    pub identity_ed25519: EmbeddedCert<Ed25519IdentityCert, KeyUnknownCert>,
122

            
123
    /// `master-key-ed25519` --- Redundantly specify the router's ed25519 identity.
124
    ///
125
    /// * `master-key-ed25519 <master key>`
126
    /// * Exactly once.
127
    // TODO DIRAUTH when implementing verification, don't forget to check this!
128
    pub master_key_ed25519: Ed25519Public,
129

            
130
    /// `bandwidth` --- Report router's network bandwidth.
131
    ///
132
    /// * `bandwidth <average> <burst> <observed>`
133
    /// * Exactly once.
134
    pub bandwidth: Bandwidth,
135

            
136
    /// `platform` --- Describe the platform on which this relay is running.
137
    ///
138
    /// * `platform <rest of line>`
139
    /// * At most once.
140
    pub platform: Option<RelayPlatform>,
141

            
142
    /// `published` --- Time this descriptor (and extra-info) was generated.
143
    ///
144
    /// * `published <date> <time>`
145
    /// * Exactly once.
146
    pub published: Iso8601TimeSp,
147

            
148
    /// `fingerprint` --- Redundant hash of ASN-1 encoding of router identity key.
149
    ///
150
    /// * `fingerprint <spaced fingerprint>`
151
    /// * At most once.
152
    pub fingerprint: Option<SpFingerprint>,
153

            
154
    /// `hibernating` --- Whether the relay is hibernating.
155
    ///
156
    /// <https://spec.torproject.org/dir-spec/server-descriptor-format.html#item:hibernating>
157
    // TODO: Mark this as `netdoc(default)` and skip during encoding if false.
158
    pub hibernating: NumericBoolean,
159

            
160
    /// `uptime` --- How long this relay has been continously running
161
    ///
162
    /// * `uptime <number>`
163
    /// * At most once.
164
    pub uptime: Option<u64>,
165

            
166
    /// `onion-key` --- Relay's obsolete RSA tap key.
167
    ///
168
    /// * `onion-key\n<rsa public key>`
169
    /// * At most once.
170
    /// * No extra arguments.
171
    pub onion_key: Option<ll::pk::rsa::PublicKey>,
172

            
173
    /// `ntor-onion-key` --- The circuit extension key.
174
    ///
175
    /// * `ntor-onion-key <base64 padded key>`
176
    /// * Exactly once.
177
    pub ntor_onion_key: Curve25519Public,
178

            
179
    /// `ntor-onion-key-crosscert` --- Reverse cert by K_ntor on KP_relayid_ed
180
    ///
181
    /// <https://spec.torproject.org/dir-spec/server-descriptor-format.html#item:ntor-onion-key-crosscert>
182
    pub ntor_onion_key_crosscert: NtorOnionKeyCrossCert,
183

            
184
    /// `signing-key` --- Obsolete RSA identity key.
185
    ///
186
    /// * `signing-key\n<rsa public key>`
187
    pub signing_key: ll::pk::rsa::PublicKey,
188

            
189
    /// `accept, reject` --- Exit policy.
190
    ///
191
    /// * `accept exitpattern`
192
    /// * `reject exitpattern`
193
    /// * Any number of times.
194
    // TODO: these polices can get bulky too. Perhaps we should
195
    // de-duplicate them too.
196
    pub ipv4_policy: AddrPolicy,
197

            
198
    /// `ipv6-policy` --- Exit plicy summary for IPv6
199
    ///
200
    /// * `ipv6-policy <accept/reject> PortList`
201
    /// * At most once.
202
    pub ipv6_policy: Arc<PortPolicy>,
203

            
204
    /// `overload-general` --- Relay is overloaded.
205
    ///
206
    /// * `overload-general 1 <time>`
207
    /// * At most once.
208
    // TODO in OverloadGeneral use ConstantString (from !3985) for version
209
    pub overload_general: Option<OverloadGeneral>,
210

            
211
    /// `contact` --- Server administrator contact information.
212
    ///
213
    /// <https://spec.torproject.org/dir-spec/server-descriptor-format.html#item:contact>
214
    pub contact: Option<ContactInfo>,
215

            
216
    /// `family` --- Group relays for the purpose of path selection.
217
    ///
218
    /// * `family <LongIdent> ...`
219
    /// * One or more `LongIdent` arguments.
220
    /// * At most once.
221
    pub family: Arc<RelayFamily>,
222

            
223
    /// `family-cert` --- Prove membership in a relay family.
224
    ///
225
    /// * `family-cert\n<object>`
226
    /// * Any number of times.
227
    pub family_cert: RetainedOrderVec<EmbeddedCert<Ed25519FamilyCert, KeyUnknownCert>>,
228

            
229
    /// `caches-extra-info` --- Router provides extra-info as a dirmirror.
230
    ///
231
    /// * `caches-extra-info`
232
    /// * At most once.
233
    /// * No extra arguments.
234
    pub caches_extra_info: Option<ItemPresent<CachesExtraInfoToken>>,
235

            
236
    /// `extra-info-digest` --- Hash of the extra-info document.
237
    ///
238
    /// <https://spec.torproject.org/dir-spec/server-descriptor-format.html#item:extra-info-digest>
239
    pub extra_info_digest: Option<ExtraInfoDigests>,
240

            
241
    /// `hidden-service-dir` --- Declares this router to be a hidden service directory
242
    ///
243
    /// <https://spec.torproject.org/dir-spec/server-descriptor-format.html#item:hidden-service-dir>
244
    pub hidden_service_dir: Option<ItemPresent<HiddenServiceDirToken>>,
245

            
246
    /// `or-address` --- Alternative ORport address and port
247
    ///
248
    /// <https://spec.torproject.org/dir-spec/server-descriptor-format.html#item:or-address>
249
    pub or_address: Vec<net::SocketAddr>,
250

            
251
    /// `tunnelled-dir-server` --- Accepts a `BEGIN_DIR` relay message.
252
    ///
253
    /// * `tunnelled-dir-server`
254
    /// * At most once.
255
    /// * No extra arguments.
256
    pub tunnelled_dir_server: Option<ItemPresent<TunnelledDirServerToken>>,
257

            
258
    /// `proto` --- Subprotocol capabilities supported.
259
    ///
260
    /// * `proto <entries>`
261
    /// * Exactly once.
262
    pub proto: tor_protover::Protocols,
263
}
264

            
265
/// Signatures of a [`RouterDesc`].
266
///
267
/// <https://spec.torproject.org/dir-spec/server-descriptor-format.html#item:router-sig-ed25519>
268
#[derive(Clone, Debug, Deftly)]
269
#[derive_deftly(NetdocParseableSignatures)]
270
#[deftly(netdoc(signatures(hashes_accu = "RouterHashAccu")))]
271
#[non_exhaustive]
272
pub struct RouterDescSignatures {
273
    /// `router-sig-ed25519` --- Ed25519 signature
274
    ///
275
    /// Ed25519 signature by the Ed25519 signing key on the SHA-256 digest of
276
    /// the document prefixed by a magic up until and including the
277
    /// `router-sig-ed25519` keyword plus space.
278
    pub router_sig_ed25519: RouterSigEd25519,
279

            
280
    /// `router-signature` --- RSA signature
281
    ///
282
    /// * At end, exactly once.
283
    /// * RSA signature of the document, including `router-sig-ed25519`.
284
    pub router_signature: RouterSignature,
285
}
286

            
287
/// Description of the software a relay is running.
288
///
289
/// `platform` line in a routerstatus.
290
/// <https://spec.torproject.org/dir-spec/server-descriptor-format.html#item:platform>
291
// TODO: Move this to types/misc.rs.
292
#[derive(Debug, Clone, PartialEq, Eq)]
293
#[non_exhaustive]
294
pub enum RelayPlatform {
295
    /// Software advertised to be some version of Tor, on some platform.
296
    Tor(TorVersion, Option<String>),
297
    /// Software not advertised to be Tor.
298
    Other(String),
299
}
300

            
301
/// Zero-sized token type for use in [`RouterDesc::caches_extra_info`].
302
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
303
#[non_exhaustive]
304
pub struct CachesExtraInfoToken;
305

            
306
/// Zero-sized token type for use in [`RouterDesc::hidden_service_dir`].
307
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
308
#[non_exhaustive]
309
pub struct HiddenServiceDirToken;
310

            
311
/// Zero-sized token type for use in [`RouterDesc::tunnelled_dir_server`].
312
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
313
#[non_exhaustive]
314
pub struct TunnelledDirServerToken;
315

            
316
impl std::str::FromStr for RelayPlatform {
317
    type Err = Error;
318
2288
    fn from_str(args: &str) -> Result<Self> {
319
2288
        if args.starts_with("Tor ") {
320
2286
            let v: Vec<_> = args.splitn(4, ' ').collect();
321
2286
            match &v[..] {
322
2280
                ["Tor", ver, "on", p] => {
323
2280
                    Ok(RelayPlatform::Tor(ver.parse()?, Some((*p).to_string())))
324
                }
325
6
                ["Tor", ver, ..] => Ok(RelayPlatform::Tor(ver.parse()?, None)),
326
                _ => unreachable!(),
327
            }
328
        } else {
329
2
            Ok(RelayPlatform::Other(args.to_string()))
330
        }
331
2288
    }
332
}
333

            
334
impl Display for RelayPlatform {
335
10
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
336
8
        match &self {
337
2
            Self::Tor(v, Some(p)) => write!(f, "Tor {v} on {p}"),
338
6
            Self::Tor(v, None) => write!(f, "Tor {v}"),
339
2
            Self::Other(s) => write!(f, "{s}"),
340
        }
341
10
    }
342
}
343

            
344
impl ItemValueParseable for RelayPlatform {
345
    fn from_unparsed(item: UnparsedItem<'_>) -> std::result::Result<Self, ErrorProblem> {
346
        let mut args = item.args_copy();
347
        item.check_no_object()?;
348
        args.into_remaining()
349
            .parse()
350
            .map_err(|_| args.handle_error("platform", ArgumentError::Invalid))
351
    }
352
}
353

            
354
impl ItemValueEncodable for RelayPlatform {
355
    fn write_item_value_onto(
356
        &self,
357
        mut out: ItemEncoder,
358
    ) -> std::result::Result<(), tor_error::Bug> {
359
        // Adding a raw string is fine because this is effectively a free form
360
        // field.
361
        out.args_raw_string(&self);
362
        Ok(())
363
    }
364
}
365

            
366
decl_keyword! {
367
    /// RouterKwd is an instance of Keyword, used to denote the different
368
    /// Items that are recognized as appearing in a router descriptor.
369
    RouterKwd {
370
        annotation "@source" => ANN_SOURCE,
371
        annotation "@downloaded-at" => ANN_DOWNLOADED_AT,
372
        annotation "@purpose" => ANN_PURPOSE,
373
        "accept" | "reject" => POLICY,
374
        "bandwidth" => BANDWIDTH,
375
        "bridge-distribution-request" => BRIDGE_DISTRIBUTION_REQUEST,
376
        "caches-extra-info" => CACHES_EXTRA_INFO,
377
        "contact" => CONTACT,
378
        "extra-info-digest" => EXTRA_INFO_DIGEST,
379
        "family" => FAMILY,
380
        "family-cert" => FAMILY_CERT,
381
        "fingerprint" => FINGERPRINT,
382
        "hibernating" => HIBERNATING,
383
        "identity-ed25519" => IDENTITY_ED25519,
384
        "ipv6-policy" => IPV6_POLICY,
385
        "master-key-ed25519" => MASTER_KEY_ED25519,
386
        "ntor-onion-key" => NTOR_ONION_KEY,
387
        "ntor-onion-key-crosscert" => NTOR_ONION_KEY_CROSSCERT,
388
        "onion-key" => ONION_KEY,
389
        "onion-key-crosscert" => ONION_KEY_CROSSCERT,
390
        "or-address" => OR_ADDRESS,
391
        "platform" => PLATFORM,
392
        "proto" => PROTO,
393
        "published" => PUBLISHED,
394
        "router" => ROUTER,
395
        "router-sig-ed25519" => ROUTER_SIG_ED25519,
396
        "router-signature" => ROUTER_SIGNATURE,
397
        "signing-key" => SIGNING_KEY,
398
        "tunnelled_dir_server" => TUNNELLED_DIR_SERVER,
399
        "uptime" => UPTIME,
400
        // "protocols" once existed, but is obsolete
401
        // "eventdns" once existed, but is obsolete
402
        // "allow-single-hop-exits" is also obsolete.
403
    }
404
}
405

            
406
/// Rules for parsing a set of router descriptor annotations.
407
2
static ROUTER_ANNOTATIONS: LazyLock<SectionRules<RouterKwd>> = LazyLock::new(|| {
408
    use RouterKwd::*;
409

            
410
2
    let mut rules = SectionRules::builder();
411
2
    rules.add(ANN_SOURCE.rule());
412
2
    rules.add(ANN_DOWNLOADED_AT.rule().args(1..));
413
2
    rules.add(ANN_PURPOSE.rule().args(1..));
414
2
    rules.add(ANN_UNRECOGNIZED.rule().may_repeat().obj_optional());
415
    // Unrecognized annotations are fine; anything else is an error in this
416
    // context.
417
2
    rules.reject_unrecognized();
418
2
    rules.build()
419
2
});
420
/// Rules for tokens that are allowed in the first part of a
421
/// router descriptor.
422
56
static ROUTER_HEADER_RULES: LazyLock<SectionRules<RouterKwd>> = LazyLock::new(|| {
423
    use RouterKwd::*;
424

            
425
56
    let mut rules = SectionRules::builder();
426
56
    rules.add(ROUTER.rule().required().args(5..));
427
56
    rules.add(IDENTITY_ED25519.rule().required().no_args().obj_required());
428
    // No other intervening tokens are permitted in the header.
429
56
    rules.reject_unrecognized();
430
56
    rules.build()
431
56
});
432
/// Rules for  tokens that are allowed in the first part of a
433
/// router descriptor.
434
56
static ROUTER_BODY_RULES: LazyLock<SectionRules<RouterKwd>> = LazyLock::new(|| {
435
    use RouterKwd::*;
436

            
437
56
    let mut rules = SectionRules::builder();
438
56
    rules.add(MASTER_KEY_ED25519.rule().required().args(1..));
439
56
    rules.add(PLATFORM.rule());
440
56
    rules.add(PUBLISHED.rule().required());
441
56
    rules.add(FINGERPRINT.rule());
442
56
    rules.add(UPTIME.rule().args(1..));
443
56
    rules.add(ONION_KEY.rule().no_args().obj_required());
444
56
    rules.add(ONION_KEY_CROSSCERT.rule().no_args().obj_required());
445
56
    rules.add(NTOR_ONION_KEY.rule().required().args(1..));
446
56
    rules.add(
447
56
        NTOR_ONION_KEY_CROSSCERT
448
56
            .rule()
449
56
            .required()
450
56
            .args(1..=1)
451
56
            .obj_required(),
452
    );
453
56
    rules.add(SIGNING_KEY.rule().no_args().required().obj_required());
454
56
    rules.add(POLICY.rule().may_repeat().args(1..));
455
56
    rules.add(IPV6_POLICY.rule().args(2..));
456
56
    rules.add(FAMILY.rule().args(1..));
457
56
    rules.add(FAMILY_CERT.rule().obj_required().may_repeat());
458
56
    rules.add(CACHES_EXTRA_INFO.rule().no_args());
459
56
    rules.add(OR_ADDRESS.rule().may_repeat().args(1..));
460
56
    rules.add(TUNNELLED_DIR_SERVER.rule());
461
56
    rules.add(PROTO.rule().required().args(1..));
462
56
    rules.add(UNRECOGNIZED.rule().may_repeat().obj_optional());
463
    // TODO: these aren't parsed yet.  Only authorities use them.
464
56
    {
465
56
        rules.add(BANDWIDTH.rule().required().args(3..));
466
56
        rules.add(BRIDGE_DISTRIBUTION_REQUEST.rule().args(1..));
467
56
        rules.add(HIBERNATING.rule().args(1..));
468
56
        rules.add(CONTACT.rule());
469
56
    }
470
    // TODO: this is ignored for now.
471
56
    {
472
56
        rules.add(EXTRA_INFO_DIGEST.rule().args(1..));
473
56
    }
474
56
    rules.build()
475
56
});
476

            
477
/// Rules for items that appear at the end of a router descriptor.
478
56
static ROUTER_SIG_RULES: LazyLock<SectionRules<RouterKwd>> = LazyLock::new(|| {
479
    use RouterKwd::*;
480

            
481
56
    let mut rules = SectionRules::builder();
482
56
    rules.add(ROUTER_SIG_ED25519.rule().required().args(1..));
483
56
    rules.add(ROUTER_SIGNATURE.rule().required().no_args().obj_required());
484
    // No intervening tokens are allowed in the footer.
485
56
    rules.reject_unrecognized();
486
56
    rules.build()
487
56
});
488

            
489
impl RouterAnnotation {
490
    /// Extract a single RouterAnnotation (possibly empty) from a reader.
491
8
    fn take_from_reader(reader: &mut NetDocReader<'_, RouterKwd>) -> Result<RouterAnnotation> {
492
        use RouterKwd::*;
493
20
        let mut items = reader.pause_at(|item| item.is_ok_with_non_annotation());
494

            
495
8
        let body = ROUTER_ANNOTATIONS.parse(&mut items)?;
496

            
497
6
        let source = body.maybe(ANN_SOURCE).args_as_str().map(String::from);
498
6
        let purpose = body.maybe(ANN_PURPOSE).args_as_str().map(String::from);
499
6
        let downloaded = body
500
6
            .maybe(ANN_DOWNLOADED_AT)
501
6
            .parse_args_as_str::<Iso8601TimeSp>()?
502
6
            .map(|t| t.into());
503
6
        Ok(RouterAnnotation {
504
6
            source,
505
6
            downloaded,
506
6
            purpose,
507
6
        })
508
8
    }
509
}
510

            
511
/// A parsed router descriptor whose signatures and/or validity times
512
/// may or may not be invalid.
513
pub type UncheckedRouterDesc = signed::SignatureGated<timed::TimerangeBound<RouterDesc>>;
514

            
515
/// How long after its published time is a router descriptor officially
516
/// supposed to be usable?
517
const ROUTER_EXPIRY_SECONDS: u64 = 5 * 86400;
518

            
519
/// How long before its published time is a router descriptor usable?
520
// TODO(nickm): This valid doesn't match C tor, which only enforces this rule
521
// ("routers should not some from the future") at directory authorities, and
522
// there only enforces a 12-hour limit (`ROUTER_ALLOW_SKEW`).  Eventually we
523
// should probably harmonize these cutoffs.
524
const ROUTER_PRE_VALIDITY_SECONDS: u64 = 86400;
525

            
526
impl RouterDesc {
527
    /// Return a reference to this relay's RSA identity.
528
1298
    pub fn rsa_identity(&self) -> RsaIdentity {
529
1298
        self.signing_key.to_rsa_identity()
530
1298
    }
531

            
532
    /// Return a reference to this relay's Ed25519 identity.
533
2
    pub fn ed_identity(&self) -> &Ed25519Identity {
534
2
        &self
535
2
            .identity_ed25519
536
2
            .get()
537
2
            .expect("ed25519 identity cert should be verified")
538
2
            .id_ed25519
539
2
    }
540

            
541
    /// Return a reference to the list of subprotocol versions supported by this
542
    /// relay.
543
2
    pub fn protocols(&self) -> &tor_protover::Protocols {
544
2
        &self.proto
545
2
    }
546

            
547
    /// Return a reference to this relay's Ntor onion key.
548
2
    pub fn ntor_onion_key(&self) -> &ll::pk::curve25519::PublicKey {
549
2
        &self.ntor_onion_key.0
550
2
    }
551

            
552
    /// Return the publication
553
326
    pub fn published(&self) -> time::SystemTime {
554
326
        self.published.0
555
326
    }
556

            
557
    /// Return an iterator of every `SocketAddr` at which this descriptor says
558
    /// its relay can be reached.
559
2
    pub fn or_ports(&self) -> impl Iterator<Item = net::SocketAddr> + '_ {
560
2
        iter::once(net::SocketAddr::new(
561
2
            self.router.address.into(),
562
2
            self.router.orport,
563
        ))
564
2
        .chain(self.or_address.iter().copied())
565
2
    }
566

            
567
    /// Return the declared family of this descriptor.
568
    pub fn family(&self) -> Arc<RelayFamily> {
569
        Arc::clone(&self.family)
570
    }
571

            
572
    /// Return the authenticated family IDs of this descriptor.
573
2
    pub fn family_ids(&self) -> RelayFamilyIds {
574
2
        RelayFamilyIds::from_iter(
575
2
            self.family_cert
576
2
                .iter()
577
5
                .map(|cert| cert.get().expect("unverified family cert?"))
578
5
                .map(|cert| cert.family_ed25519.into()),
579
        )
580
2
    }
581

            
582
    /// Helper: tokenize `s`, and divide it into three validated sections.
583
2410
    fn parse_sections<'a>(
584
2410
        reader: &mut NetDocReader<'a, RouterKwd>,
585
2410
    ) -> Result<(
586
2410
        Section<'a, RouterKwd>,
587
2410
        Section<'a, RouterKwd>,
588
2410
        Section<'a, RouterKwd>,
589
2410
    )> {
590
        use RouterKwd::*;
591

            
592
        // Parse everything up through the header.
593
2410
        let header = ROUTER_HEADER_RULES.parse(
594
7069
            reader.pause_at(|item| item.is_ok_with_kwd_not_in(&[ROUTER, IDENTITY_ED25519])),
595
112
        )?;
596

            
597
        // Parse everything up to but not including the signature.
598
2298
        let body =
599
39167
            ROUTER_BODY_RULES.parse(reader.pause_at(|item| {
600
39110
                item.is_ok_with_kwd_in(&[ROUTER_SIGNATURE, ROUTER_SIG_ED25519])
601
39110
            }))?;
602

            
603
        // Parse the signature.
604
4667
        let sig = ROUTER_SIG_RULES.parse(reader.pause_at(|item| {
605
4610
            item.is_ok_with_annotation() || item.is_ok_with_kwd(ROUTER) || item.is_empty_line()
606
4610
        }))?;
607

            
608
2298
        Ok((header, body, sig))
609
2410
    }
610

            
611
    /// Try to parse `s` as a router descriptor.
612
    ///
613
    /// Does not actually check liveness or signatures; you need to do that
614
    /// yourself before you can do the output.
615
    ///
616
    /// The following fields are not parsed with the legacy parser and their
617
    /// default value is used instead.
618
    /// * [`RouterDescIntroItem::socksport`] in [`RouterDesc::router`]
619
    /// * [`RouterDesc::bandwidth`]
620
    /// * [`RouterDesc::or_address`]
621
    ///     * Extracts only the first IPv6 address.
622
    /// * [`RouterDesc::hibernating`]
623
    /// * [`RouterDesc::overload_general`]
624
    /// * [`RouterDesc::contact`]
625
    /// * [`RouterDesc::extra_info_digest`]
626
    /// * [`RouterDesc::hidden_service_dir`]
627
2404
    pub fn parse(s: &str) -> Result<UncheckedRouterDesc> {
628
2404
        let mut reader = crate::parse::tokenize::NetDocReader::new(s)?;
629
2417
        let result = Self::parse_internal(&mut reader).map_err(|e| e.within(s))?;
630
        // We permit empty lines at the end of router descriptors, since there's
631
        // a known issue in Tor relays that causes them to return them this way.
632
2274
        reader
633
2274
            .should_be_exhausted_but_for_empty_lines()
634
2274
            .map_err(|e| e.within(s))?;
635
2274
        Ok(result)
636
2404
    }
637

            
638
    /// Helper: parse a router descriptor from `s`.
639
    ///
640
    /// This function does the same as parse(), but returns errors based on
641
    /// byte-wise positions.  The parse() function converts such errors
642
    /// into line-and-byte positions.
643
    #[allow(clippy::string_slice)] // TODO
644
2410
    fn parse_internal(r: &mut NetDocReader<'_, RouterKwd>) -> Result<UncheckedRouterDesc> {
645
        // TODO: This function is too long!  The little "paragraphs" here
646
        // that parse one item at a time should be made into sub-functions.
647
        use RouterKwd::*;
648

            
649
2410
        let s = r.str();
650
2410
        let (header, body, sig) = RouterDesc::parse_sections(r)?;
651

            
652
        // Unwrap should be safe because inline `required` call should return
653
        // `Error::MissingToken` if `ROUTER` is not `Ok`
654
        #[allow(clippy::unwrap_used)]
655
2298
        let start_offset = header.required(ROUTER)?.offset_in(s).unwrap();
656

            
657
        // ed25519 identity and signing key.
658
        //
659
        // Small digression: This is terrible.  We return a tuple containing
660
        // a KeyUnknownCert and an UncheckedCert.  This is because of a parse2
661
        // and legacy incongruence.  For parse2, we need the KeyUnknownCert
662
        // to properly include it into EmbeddedCert, whereas the legacy parser
663
        // will need an UncheckedCert because the verification chain is
664
        // performed at the end.  Because tor-cert's method all consume self,
665
        // we can not go backwards, meaning we have to store two separate
666
        // copies.  It is also not possible to do the conversion to
667
        // UncheckedCert later, because then we lose the error context returned
668
        // in EK::BadObjectVal if the signed-by extension is missing.
669
        //
670
2290
        let (ku_identity_cert, identity_cert, ed25519_signing_key) = {
671
2298
            let cert_tok = header.required(IDENTITY_ED25519)?;
672
            // Unwrap should be safe because above `required` call should
673
            // return `Error::MissingToken` if `IDENTITY_ED25519` is not `Ok`
674
            #[allow(clippy::unwrap_used)]
675
2298
            if cert_tok.offset_in(s).unwrap() < start_offset {
676
2
                return Err(EK::MisplacedToken
677
2
                    .with_msg("identity-ed25519")
678
2
                    .at_pos(cert_tok.pos()));
679
2296
            }
680
2296
            let ku_cert = cert_tok
681
2296
                .parse_obj::<UnvalidatedEdCert>("ED25519 CERT")?
682
2296
                .check_cert_type(tor_cert::CertType::IDENTITY_V_SIGNING)?
683
2296
                .into_unchecked();
684
2297
            let cert = ku_cert.clone().should_have_signing_key().map_err(|err| {
685
2
                EK::BadObjectVal
686
2
                    .err()
687
2
                    .with_source(err)
688
2
                    .at_pos(cert_tok.pos())
689
3
            })?;
690
2295
            let sk = *cert.peek_subject_key().as_ed25519().ok_or_else(|| {
691
2
                EK::BadObjectVal
692
2
                    .at_pos(cert_tok.pos())
693
2
                    .with_msg("wrong type for signing key in cert")
694
3
            })?;
695
2293
            let sk: ll::pk::ed25519::PublicKey = sk.try_into().map_err(|_| {
696
2
                EK::BadObjectVal
697
2
                    .at_pos(cert_tok.pos())
698
2
                    .with_msg("invalid ed25519 signing key")
699
3
            })?;
700
2290
            (ku_cert, cert, sk)
701
        };
702

            
703
        // master-key-ed25519: required, and should match certificate.
704
        #[allow(unexpected_cfgs)]
705
2288
        let ed25519_identity_key = {
706
2290
            let master_key_tok = body.required(MASTER_KEY_ED25519)?;
707
2290
            let ed_id: Ed25519Public = master_key_tok.parse_arg(0)?;
708
2290
            let ed_id: ll::pk::ed25519::Ed25519Identity = ed_id.into();
709
2290
            if ed_id != *identity_cert.peek_signing_key() {
710
                #[cfg(not(fuzzing))] // No feature here; never omit in production.
711
2
                return Err(EK::BadObjectVal
712
2
                    .at_pos(master_key_tok.pos())
713
2
                    .with_msg("master-key-ed25519 does not match key in identity-ed25519"));
714
2288
            }
715
2288
            ed_id
716
        };
717

            
718
        // Legacy RSA identity
719
2288
        let rsa_identity_key: ll::pk::rsa::PublicKey = body
720
2288
            .required(SIGNING_KEY)?
721
2288
            .parse_obj::<RsaPublicParse1Helper>("RSA PUBLIC KEY")?
722
2288
            .check_len_eq(1024)?
723
2288
            .check_exponent(65537)?
724
2288
            .into();
725
2288
        let rsa_identity = rsa_identity_key.to_rsa_identity();
726

            
727
2288
        let ed_sig = sig.required(ROUTER_SIG_ED25519)?;
728
2288
        let rsa_sig = sig.required(ROUTER_SIGNATURE)?;
729
        // Unwrap should be safe because above `required` calls should return
730
        // an `Error::MissingToken` if `ROUTER_...` is not `Ok`
731
        #[allow(clippy::unwrap_used)]
732
2288
        let ed_sig_pos = ed_sig.offset_in(s).unwrap();
733
        #[allow(clippy::unwrap_used)]
734
2288
        let rsa_sig_pos = rsa_sig.offset_in(s).unwrap();
735

            
736
2288
        if ed_sig_pos > rsa_sig_pos {
737
2
            return Err(EK::UnexpectedToken
738
2
                .with_msg(ROUTER_SIG_ED25519.to_str())
739
2
                .at_pos(ed_sig.pos()));
740
2286
        }
741

            
742
        // Extract ed25519 signature.
743
2286
        let ed_signature: ll::pk::ed25519::ValidatableEd25519Signature = {
744
2286
            let mut d = ll::d::Sha256::new();
745
2286
            d.update(&b"Tor router descriptor signature v1"[..]);
746
2286
            let signed_end = ed_sig_pos + b"router-sig-ed25519 ".len();
747
2286
            d.update(&s[start_offset..signed_end]);
748
2286
            let d = d.finalize();
749
2286
            let sig: [u8; 64] = ed_sig
750
2286
                .parse_arg::<B64>(0)?
751
2286
                .into_array()
752
2286
                .map_err(|_| EK::BadSignature.at_pos(ed_sig.pos()))?;
753
2286
            let sig = ll::pk::ed25519::Signature::from(sig);
754
2286
            ll::pk::ed25519::ValidatableEd25519Signature::new(ed25519_signing_key, sig, &d)
755
        };
756

            
757
        // Extract legacy RSA signature.
758
2286
        let rsa_signature: ll::pk::rsa::ValidatableRsaSignature = {
759
2286
            let mut d = ll::d::Sha1::new();
760
2286
            let signed_end = rsa_sig_pos + b"router-signature\n".len();
761
2286
            d.update(&s[start_offset..signed_end]);
762
2286
            let d = d.finalize();
763
2286
            let sig = rsa_sig.obj("SIGNATURE")?;
764
            // TODO: we need to accept prefixes here. COMPAT BLOCKER.
765

            
766
2286
            ll::pk::rsa::ValidatableRsaSignature::new(&rsa_identity_key, &sig, &d)
767
        };
768

            
769
        // router nickname ipv4addr orport socksport dirport
770
2286
        let (nickname, ipv4addr, orport, dirport) = {
771
2286
            let rtrline = header.required(ROUTER)?;
772
            (
773
2286
                rtrline.required_arg(0)?.parse::<Nickname>().map_err(|e| {
774
                    EK::BadArgument
775
                        .with_msg(e.to_string())
776
                        .at_pos(rtrline.pos())
777
                })?,
778
2286
                rtrline.parse_arg::<net::Ipv4Addr>(1)?,
779
2286
                rtrline.parse_arg(2)?,
780
                // Skipping socksport.
781
2286
                rtrline.parse_arg(4)?,
782
            )
783
        };
784

            
785
        // uptime
786
2286
        let uptime = body.maybe(UPTIME).parse_arg(0)?;
787

            
788
        // published time.
789
2286
        let published = body
790
2286
            .required(PUBLISHED)?
791
2286
            .args_as_str()
792
2286
            .parse::<Iso8601TimeSp>()?;
793

            
794
        // ntor key
795
2286
        let ntor_onion_key: Curve25519Public = body.required(NTOR_ONION_KEY)?.parse_arg(0)?;
796
        // ntor crosscert
797
2282
        let (cc_sig, cc_expiry, cc_cert) = {
798
2286
            let cc = body.required(NTOR_ONION_KEY_CROSSCERT)?;
799
2286
            let sign: u8 = cc.parse_arg(0)?;
800
2286
            if sign != 0 && sign != 1 {
801
4
                return Err(EK::BadArgument.at_pos(cc.arg_pos(0)).with_msg("not 0 or 1"));
802
2282
            }
803
2282
            let ntor_as_ed: ll::pk::ed25519::PublicKey =
804
2282
                ll::pk::keymanip::convert_curve25519_to_ed25519_public(&ntor_onion_key.0, sign)
805
2282
                    .ok_or_else(|| {
806
                        EK::BadArgument
807
                            .at_pos(cc.pos())
808
                            .with_msg("Uncheckable crosscert")
809
                    })?;
810

            
811
2282
            let cert = cc
812
2282
                .parse_obj::<UnvalidatedEdCert>("ED25519 CERT")?
813
2282
                .into_unchecked();
814
2282
            let (_, sig, expiry) = Ed25519NtorCrossCert::verify_inner(
815
2282
                ntor_as_ed.into(),
816
2282
                ed25519_identity_key,
817
2282
                cert.clone(),
818
            )
819
2282
            .map_err(|_| EK::BadSignature.err())?;
820

            
821
2282
            let cert = NtorOnionKeyCrossCert {
822
2282
                bit: NumericBoolean(sign != 0),
823
2282
                // Okay to call because we added the signature to the batch.
824
2282
                cert: EmbeddedCert::new(Ed25519NtorCrossCert::dangerous_new_unverified(), cert),
825
2282
            };
826

            
827
2282
            (sig, expiry, cert)
828
        };
829

            
830
        // TAP key
831
2282
        let tap_onion_key: Option<ll::pk::rsa::PublicKey> = if let Some(tok) = body.get(ONION_KEY) {
832
            Some(
833
2280
                tok.parse_obj::<RsaPublicParse1Helper>("RSA PUBLIC KEY")?
834
2280
                    .check_len_eq(1024)?
835
2280
                    .check_exponent(65537)?
836
2280
                    .into(),
837
            )
838
        } else {
839
2
            None
840
        };
841

            
842
        // TAP crosscert
843
2282
        let tap_crosscert_sig = if let Some(cc_tok) = body.get(ONION_KEY_CROSSCERT) {
844
2280
            let cc_val = cc_tok.obj("CROSSCERT")?;
845
2280
            let mut signed = Vec::new();
846
2280
            signed.extend(rsa_identity.as_bytes());
847
2280
            signed.extend(identity_cert.peek_signing_key().as_bytes());
848
2280
            Some(ll::pk::rsa::ValidatableRsaSignature::new(
849
2280
                tap_onion_key.as_ref().ok_or_else(|| {
850
                    EK::MissingToken.with_msg("onion-key-crosscert without onion-key")
851
                })?,
852
2280
                &cc_val,
853
2280
                &signed,
854
            ))
855
2
        } else if tap_onion_key.is_some() {
856
            return Err(EK::MissingToken.with_msg("onion-key without onion-key-crosscert"));
857
        } else {
858
2
            None
859
        };
860

            
861
        // List of subprotocol versions
862
2282
        let proto = {
863
2282
            let proto_tok = body.required(PROTO)?;
864
2282
            proto_tok
865
2282
                .args_as_str()
866
2282
                .parse::<tor_protover::Protocols>()
867
2282
                .map_err(|e| EK::BadArgument.at_pos(proto_tok.pos()).with_source(e))?
868
        };
869

            
870
        // tunneled-dir-server
871
2282
        let is_dircache = ((dirport != 0) || body.get(TUNNELLED_DIR_SERVER).is_some())
872
2282
            .then_some(ItemPresent::default());
873

            
874
        // caches-extra-info
875
2283
        let is_extrainfo_cache = body.get(CACHES_EXTRA_INFO).map(|_| ItemPresent::default());
876

            
877
        // fingerprint: check for consistency with RSA identity.
878
2282
        if let Some(fp_tok) = body.get(FINGERPRINT) {
879
2282
            let fp: RsaIdentity = fp_tok.args_as_str().parse::<SpFingerprint>()?.into();
880
2282
            if fp != rsa_identity {
881
4
                return Err(EK::BadArgument
882
4
                    .at_pos(fp_tok.pos())
883
4
                    .with_msg("fingerprint does not match RSA identity"));
884
2278
            }
885
        }
886

            
887
        // Family
888
2278
        let family = {
889
2278
            let mut family = body
890
2278
                .maybe(FAMILY)
891
2278
                .parse_args_as_str::<RelayFamily>()?
892
2278
                .unwrap_or_else(RelayFamily::new);
893
2278
            if !family.is_empty() {
894
4
                // If this family is nonempty, we add our own RSA id to it, on
895
4
                // the theory that doing so will improve the odds of having a
896
4
                // canonical family shared by all of the members of this family.
897
4
                // If the family is empty, there's no point in adding our own ID
898
4
                // to it, and doing so would only waste memory.
899
4
                family.push(rsa_identity);
900
2274
            }
901
2278
            family.intern()
902
        };
903

            
904
        // Family ids (for "happy families")
905
        //
906
        // Unfortunately we have to store this as a tuple of KeyUnknownCert and
907
        // UncheckedCert due to a parse2/legacy incongruence.  parse2 requires
908
        // KeyUnknownCert for EmbeddedCert whereas the legacy parser needs
909
        // descendants of it obtained by passing it irreversibly through the
910
        // tor_cert verification chain.
911
2278
        let family_certs = body
912
2278
            .slice(FAMILY_CERT)
913
2278
            .iter()
914
2280
            .map(|ent| {
915
4
                let ku = ent
916
4
                    .parse_obj::<UnvalidatedEdCert>("FAMILY CERT")?
917
4
                    .check_cert_type(CertType::FAMILY_V_IDENTITY)?
918
4
                    .check_subject_key_is(identity_cert.peek_signing_key())?
919
4
                    .into_unchecked();
920
4
                let unchecked = ku.clone().should_have_signing_key().map_err(|e| {
921
                    EK::BadObjectVal
922
                        .with_msg("missing public key")
923
                        .at_pos(ent.pos())
924
                        .with_source(e)
925
                })?;
926
4
                Ok((ku, unchecked))
927
4
            })
928
2278
            .collect::<Result<Vec<_>>>()?;
929

            
930
        // or-address
931
        // Extract at most one ipv6 address from the list.  It's not great,
932
        // but it's what the legacy parser does.
933
2278
        let mut ipv6addr = Vec::with_capacity(1);
934
2278
        for tok in body.slice(OR_ADDRESS) {
935
6
            if let Ok(net::SocketAddr::V6(a)) = tok.parse_arg::<net::SocketAddr>(0) {
936
6
                ipv6addr.push(a.into());
937
6
                break;
938
            }
939
            // We skip over unparsable addresses. Is that right?
940
        }
941

            
942
        // platform
943
2278
        let platform = body.maybe(PLATFORM).parse_args_as_str::<RelayPlatform>()?;
944

            
945
        // ipv4_policy
946
2278
        let ipv4_policy = {
947
2278
            let mut pol = AddrPolicy::new();
948
2280
            for ruletok in body.slice(POLICY).iter() {
949
2280
                let accept = match ruletok.kwd_str() {
950
2280
                    "accept" => RuleKind::Accept,
951
2278
                    "reject" => RuleKind::Reject,
952
                    _ => {
953
                        return Err(Error::from(internal!(
954
                            "tried to parse a strange line as a policy"
955
                        ))
956
                        .at_pos(ruletok.pos()));
957
                    }
958
                };
959
2280
                let pat: AddrPortPattern = ruletok
960
2280
                    .args_as_str()
961
2280
                    .parse()
962
2280
                    .map_err(|e| EK::BadPolicy.at_pos(ruletok.pos()).with_source(e))?;
963
2280
                pol.push(accept, pat);
964
            }
965
2278
            pol
966
        };
967

            
968
        // ipv6 policy
969
2278
        let ipv6_policy = match body.get(IPV6_POLICY) {
970
2
            Some(p) => p
971
2
                .args_as_str()
972
2
                .parse()
973
3
                .map_err(|e| EK::BadPolicy.at_pos(p.pos()).with_source(e))?,
974
            // Unwrap is safe here because str is not empty
975
            #[allow(clippy::unwrap_used)]
976
2276
            None => "reject 1-65535".parse::<PortPolicy>().unwrap(),
977
        };
978

            
979
        // Now we're going to collect signatures and expiration times.
980
2276
        let (identity_cert, identity_sig) = identity_cert.dangerously_split().map_err(|err| {
981
            EK::BadObjectVal
982
                .with_msg("missing public key")
983
                .with_source(err)
984
        })?;
985
2276
        let mut signatures: Vec<Box<dyn ll::pk::ValidatableSignature>> = vec![
986
2276
            Box::new(rsa_signature),
987
2276
            Box::new(ed_signature),
988
2276
            Box::new(identity_sig),
989
2276
            Box::new(cc_sig),
990
        ];
991
2276
        if let Some(s) = tap_crosscert_sig {
992
2274
            signatures.push(Box::new(s));
993
2274
        }
994

            
995
2276
        let identity_cert = identity_cert.dangerously_assume_timely();
996
2276
        let mut expirations = vec![
997
2276
            published
998
2276
                .0
999
2276
                .saturating_add(time::Duration::new(ROUTER_EXPIRY_SECONDS, 0)),
2276
            identity_cert.expiry(),
2276
            cc_expiry,
        ];
        // As outlined above, we have to do this ... :/
        //
        // Composing the verified part of the EmbeddedCert by just extracting
        // the key alone is OK because it gets checked at the end anyways
        // due to the push to signatures and expirations.
2276
        let mut embedded_family_certs = Vec::with_capacity(family_certs.len());
2276
        for (ku_cert, cert) in family_certs {
4
            let family_ed25519 = *cert.peek_signing_key();
4
            let (inner, sig) = cert.dangerously_split().map_err(into_internal!(
                "Missing a public key that was previously there."
            ))?;
4
            let embedded_cert = EmbeddedCert::new(Ed25519FamilyCert { family_ed25519 }, ku_cert);
4
            signatures.push(Box::new(sig));
4
            expirations.push(inner.dangerously_assume_timely().expiry());
4
            embedded_family_certs.push(embedded_cert);
        }
        // Unwrap is safe here because `expirations` array is not empty
        #[allow(clippy::unwrap_used)]
2276
        let expiry = *expirations.iter().min().unwrap();
2276
        let start_time = published
2276
            .0
2276
            .saturating_sub(time::Duration::new(ROUTER_PRE_VALIDITY_SECONDS, 0));
2276
        let desc = RouterDesc {
2276
            router: RouterDescIntroItem {
2276
                nickname,
2276
                address: ipv4addr,
2276
                orport,
2276
                socksport: 0,
2276
                dirport,
2276
            },
2276
            identity_ed25519: EmbeddedCert::new(
2276
                Ed25519IdentityCert {
2276
                    id_ed25519: ed25519_identity_key,
2276
                    sign_ed25519: ed25519_signing_key.into(),
2276
                },
2276
                ku_identity_cert,
2276
            ),
2276
            master_key_ed25519: ed25519_identity_key.into(),
2276
            bandwidth: Default::default(),
2276
            platform,
2276
            published,
2276
            fingerprint: Some(rsa_identity.into()),
2276
            hibernating: Default::default(),
2276
            uptime,
2276
            onion_key: tap_onion_key,
2276
            ntor_onion_key,
2276
            ntor_onion_key_crosscert: cc_cert,
2276
            signing_key: rsa_identity_key,
2276
            ipv4_policy,
2276
            ipv6_policy: ipv6_policy.intern(),
2276
            overload_general: Default::default(),
2276
            contact: Default::default(),
2276
            family,
2276
            family_cert: embedded_family_certs.into(),
2276
            caches_extra_info: is_extrainfo_cache,
2276
            extra_info_digest: Default::default(),
2276
            hidden_service_dir: Default::default(),
2276
            or_address: ipv6addr,
2276
            tunnelled_dir_server: is_dircache,
2276
            proto,
2276
        };
2276
        let time_gated = timed::TimerangeBound::new(desc, start_time..expiry);
2276
        let sig_gated = signed::SignatureGated::new(time_gated, signatures);
2276
        Ok(sig_gated)
2410
    }
}
/// An iterator that parses one or more (possibly annotated
/// router descriptors from a string.
//
// TODO: This is largely copy-pasted from MicrodescReader. Can/should they
// be merged?
pub struct RouterReader<'a> {
    /// True iff we accept annotations
    annotated: bool,
    /// Reader that we're extracting items from.
    reader: NetDocReader<'a, RouterKwd>,
}
/// Skip this reader forward until the next thing it reads looks like the
/// start of a router descriptor.
///
/// Used to recover from errors.
6
fn advance_to_next_routerdesc(reader: &mut NetDocReader<'_, RouterKwd>, annotated: bool) {
    use RouterKwd::*;
    loop {
6
        let item = reader.peek();
4
        match item {
4
            Some(Ok(t)) => {
4
                let kwd = t.kwd();
4
                if (annotated && kwd.is_annotation()) || kwd == ROUTER {
4
                    return;
                }
            }
            Some(Err(_)) => {
                // Skip over broken tokens.
            }
            None => {
2
                return;
            }
        }
        let _ = reader.next();
    }
6
}
impl<'a> RouterReader<'a> {
    /// Construct a RouterReader to take router descriptors from a string.
2
    pub fn new(s: &'a str, allow: &AllowAnnotations) -> Result<Self> {
2
        let reader = NetDocReader::new(s)?;
2
        let annotated = allow == &AllowAnnotations::AnnotationsAllowed;
2
        Ok(RouterReader { annotated, reader })
2
    }
    /// Extract an annotation from this reader.
8
    fn take_annotation(&mut self) -> Result<RouterAnnotation> {
8
        if self.annotated {
8
            RouterAnnotation::take_from_reader(&mut self.reader)
        } else {
            Ok(RouterAnnotation::default())
        }
8
    }
    /// Extract an annotated router descriptor from this reader
    ///
    /// (internal helper; does not clean up on failures.)
8
    fn take_annotated_routerdesc_raw(&mut self) -> Result<AnnotatedRouterDesc> {
8
        let ann = self.take_annotation()?;
6
        let router = RouterDesc::parse_internal(&mut self.reader)?;
2
        Ok(AnnotatedRouterDesc { ann, router })
8
    }
    /// Extract an annotated router descriptor from this reader
    ///
    /// Ensure that at least one token is consumed
8
    fn take_annotated_routerdesc(&mut self) -> Result<AnnotatedRouterDesc> {
8
        let pos_orig = self.reader.pos();
8
        let result = self.take_annotated_routerdesc_raw();
8
        if result.is_err() {
6
            if self.reader.pos() == pos_orig {
                // No tokens were consumed from the reader.  We need
                // to drop at least one token to ensure we aren't in
                // an infinite loop.
                //
                // (This might not be able to happen, but it's easier to
                // explicitly catch this case than it is to prove that
                // it's impossible.)
                let _ = self.reader.next();
6
            }
6
            advance_to_next_routerdesc(&mut self.reader, self.annotated);
2
        }
8
        result
8
    }
}
impl<'a> Iterator for RouterReader<'a> {
    type Item = Result<AnnotatedRouterDesc>;
10
    fn next(&mut self) -> Option<Self::Item> {
        // Is there a next token? If not, we're done.
10
        self.reader.peek()?;
        Some(
8
            self.take_annotated_routerdesc()
11
                .map_err(|e| e.within(self.reader.str())),
        )
10
    }
}
#[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)]
    #![allow(clippy::string_slice)] // See arti#2571
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
    use super::*;
    const TESTDATA: &str = include_str!("../../testdata/routerdesc1.txt");
    const TESTDATA2: &str = include_str!("../../testdata/routerdesc2.txt");
    // Generated with a patched C tor to include "happy family" IDs.
    const TESTDATA3: &str = include_str!("../../testdata/routerdesc3.txt");
    fn read_bad(fname: &str) -> String {
        use std::fs;
        use std::path::PathBuf;
        let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        path.push("testdata");
        path.push("bad-routerdesc");
        path.push(fname);
        fs::read_to_string(path).unwrap()
    }
    #[test]
    fn parse_arbitrary() -> Result<()> {
        use std::str::FromStr;
        use tor_checkable::{SelfSigned, Timebound};
        let rd = RouterDesc::parse(TESTDATA)?
            .check_signature()?
            .dangerously_assume_timely();
        assert_eq!(rd.router.nickname.as_str(), "Akka");
        assert_eq!(rd.router.orport, 443);
        assert_eq!(rd.router.dirport, 0);
        assert_eq!(rd.uptime, Some(1036923));
        assert_eq!(
            rd.family.as_ref(),
            &RelayFamily::from_str(
                "$303509ab910ef207b7438c27435c4a2fd579f1b1 \
                 $56927e61b51e6f363fb55498150a6ddfcf7077f2"
            )
            .unwrap()
        );
        assert_eq!(
            rd.rsa_identity().to_string(),
            "$56927e61b51e6f363fb55498150a6ddfcf7077f2"
        );
        assert_eq!(
            rd.ed_identity().to_string(),
            "CVTjf1oeaL616hH+1+UvYZ8OgkwF3z7UMITvJzm5r7A"
        );
        assert_eq!(
            rd.protocols().to_string(),
            "Cons=1-2 Desc=1-2 DirCache=2 FlowCtrl=1-2 HSDir=2 \
             HSIntro=4-5 HSRend=1-2 Link=1-5 LinkAuth=1,3 Microdesc=1-2 \
             Padding=2 Relay=1-4"
        );
        assert_eq!(
            hex::encode(rd.ntor_onion_key().to_bytes()),
            "329b3b52991613392e35d1a821dd6753e1210458ecc3337f7b7d39bfcf5da273"
        );
        assert_eq!(
            rd.published(),
            humantime::parse_rfc3339("2022-11-14T19:58:52Z").unwrap()
        );
        assert_eq!(
            rd.or_ports().collect::<Vec<_>>(),
            vec![
                "95.216.33.58:443".parse().unwrap(),
                "[2a01:4f9:2a:2145::2]:443".parse().unwrap(),
            ]
        );
        assert!(rd.onion_key.is_some());
        Ok(())
    }
    #[test]
    fn parse_no_tap_key() -> Result<()> {
        use tor_checkable::{SelfSigned, Timebound};
        let rd = RouterDesc::parse(TESTDATA2)?
            .check_signature()?
            .dangerously_assume_timely();
        assert!(rd.onion_key.is_none());
        Ok(())
    }
    #[test]
    fn test_bad() {
        use crate::Pos;
        use crate::types::policy::PolicyError;
        fn check(fname: &str, e: &Error) {
            let text = read_bad(fname);
            let rd = RouterDesc::parse(&text);
            assert!(rd.is_err());
            assert_eq!(&rd.err().unwrap(), e);
        }
        check(
            "bad-sig-order",
            &EK::UnexpectedToken
                .with_msg("router-sig-ed25519")
                .at_pos(Pos::from_line(50, 1)),
        );
        check(
            "bad-start1",
            &EK::MisplacedToken
                .with_msg("identity-ed25519")
                .at_pos(Pos::from_line(1, 1)),
        );
        check("bad-start2", &EK::MissingToken.with_msg("identity-ed25519"));
        check(
            "mismatched-fp",
            &EK::BadArgument
                .at_pos(Pos::from_line(12, 1))
                .with_msg("fingerprint does not match RSA identity"),
        );
        check("no-ed-sk", &EK::MissingToken.with_msg("identity-ed25519"));
        check(
            "bad-cc-sign",
            &EK::BadArgument
                .at_pos(Pos::from_line(34, 26))
                .with_msg("not 0 or 1"),
        );
        check(
            "bad-ipv6policy",
            &EK::BadPolicy
                .at_pos(Pos::from_line(43, 1))
                .with_source(PolicyError::InvalidPolicy),
        );
        check(
            "no-ed-id-key-in-cert",
            &EK::BadObjectVal
                .at_pos(Pos::from_line(2, 1))
                .with_source(tor_cert::CertError::MissingPubKey),
        );
        check(
            "non-ed-sk-in-cert",
            &EK::BadObjectVal
                .at_pos(Pos::from_line(2, 1))
                .with_msg("wrong type for signing key in cert"),
        );
        check(
            "bad-ed-sk-in-cert",
            &EK::BadObjectVal
                .at_pos(Pos::from_line(2, 1))
                .with_msg("invalid ed25519 signing key"),
        );
        check(
            "mismatched-ed-sk-in-cert",
            &EK::BadObjectVal
                .at_pos(Pos::from_line(8, 1))
                .with_msg("master-key-ed25519 does not match key in identity-ed25519"),
        );
    }
    #[test]
    fn parse_multiple_annotated() {
        use crate::AllowAnnotations;
        let mut s = read_bad("bad-cc-sign");
        s += "\
@uploaded-at 2020-09-26 18:15:41
@source \"127.0.0.1\"
";
        s += TESTDATA;
        s += "\
@uploaded-at 2020-09-26 18:15:41
@source \"127.0.0.1\"
";
        s += &read_bad("mismatched-fp");
        let rd = RouterReader::new(&s, &AllowAnnotations::AnnotationsAllowed).unwrap();
        let v: Vec<_> = rd.collect();
        assert!(v[0].is_err());
        assert!(v[1].is_ok());
        assert_eq!(
            v[1].as_ref().unwrap().ann.source,
            Some("\"127.0.0.1\"".to_string())
        );
        assert!(v[2].is_err());
    }
    #[test]
    fn test_platform() {
        let tests = [
            // Test with platform.
            (
                "Tor 0.4.4.4-alpha on a flying bison",
                RelayPlatform::Tor(
                    "0.4.4.4-alpha".parse().unwrap(),
                    Some("a flying bison".to_string()),
                ),
            ),
            // Test without platform but potentially weird spacing.
            (
                "Tor 0.4.4.4-alpha on",
                RelayPlatform::Tor("0.4.4.4-alpha".parse().unwrap(), None),
            ),
            (
                "Tor 0.4.4.4-alpha ",
                RelayPlatform::Tor("0.4.4.4-alpha".parse().unwrap(), None),
            ),
            (
                "Tor 0.4.4.4-alpha",
                RelayPlatform::Tor("0.4.4.4-alpha".parse().unwrap(), None),
            ),
            // Test other.
            ("arti 0.0.0", RelayPlatform::Other("arti 0.0.0".to_string())),
        ];
        for (input, output) in tests {
            assert_eq!(input.parse::<RelayPlatform>().unwrap(), output);
            // Round-trip test with input stripped of " on" suffix and trimmed.
            // Otherwise we cannot really make this work because certain inputs
            // contain redundant data on purpose.
            let input = input.strip_suffix(" on").unwrap_or(input);
            let input = input.trim();
            assert_eq!(output.to_string(), input);
        }
    }
    #[test]
    fn test_family_ids() -> Result<()> {
        use tor_checkable::{SelfSigned, Timebound};
        let rd = RouterDesc::parse(TESTDATA3)?
            .check_signature()?
            .dangerously_assume_timely();
        assert_eq!(
            rd.family_ids().as_ref(),
            &[
                "ed25519:7sToQRuge1bU2hS0CG0ViMndc4m82JhO4B4kdrQey80"
                    .parse()
                    .unwrap(),
                "ed25519:szHUS3ItRd9uk85b1UVnOZx1gg4B0266jCpbuIMNjcM"
                    .parse()
                    .unwrap(),
            ]
        );
        Ok(())
    }
}