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::parse::keyword::Keyword;
36
use crate::parse::parser::{Section, SectionRules};
37
use crate::parse::tokenize::{ItemResult, NetDocReader};
38
use crate::types::family::{RelayFamily, RelayFamilyId};
39
use crate::types::misc::*;
40
use crate::types::policy::*;
41
use crate::types::version::TorVersion;
42
use crate::util::PeekableIterator;
43
use crate::{AllowAnnotations, Error, KeywordEncodable, NetdocErrorKind as EK, Result};
44

            
45
use ll::pk::ed25519::Ed25519Identity;
46
use std::sync::Arc;
47
use std::sync::LazyLock;
48
use std::{net, time};
49
use tor_cert::CertType;
50
use tor_checkable::{Timebound, signed, timed};
51
use tor_error::{internal, into_internal};
52
use tor_llcrypto as ll;
53
use tor_llcrypto::pk::rsa::RsaIdentity;
54

            
55
use digest::Digest;
56

            
57
/// Length of a router descriptor digest
58
pub const DOC_DIGEST_LEN: usize = 20;
59

            
60
/// The digest of a RouterDesc document, as reported in a NS consensus.
61
pub type RdDigest = [u8; DOC_DIGEST_LEN];
62

            
63
/// The digest of an ExtraInfo document, as reported in a RouterDesc.
64
pub type ExtraInfoDigest = [u8; DOC_DIGEST_LEN];
65

            
66
/// A router descriptor, with possible annotations.
67
#[non_exhaustive]
68
pub struct AnnotatedRouterDesc {
69
    /// Annotation for this router descriptor; possibly empty.
70
    pub ann: RouterAnnotation,
71
    /// Underlying router descriptor; signatures not checked yet.
72
    pub router: UncheckedRouterDesc,
73
}
74

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

            
87
/// Information about a relay, parsed from a router descriptor.
88
///
89
/// This type does not hold all the information in the router descriptor
90
///
91
/// # Limitations
92
///
93
/// See module documentation.
94
///
95
/// Additionally, some fields that from router descriptors are not yet
96
/// parsed: see the comments in ROUTER_BODY_RULES for information about those.
97
///
98
/// Before using this type to connect to a relay, you MUST check that
99
/// it is valid, using is_expired_at().
100
#[derive(Clone, Debug)]
101
#[non_exhaustive]
102
pub struct RouterDesc {
103
    /// Human-readable nickname for this relay.
104
    ///
105
    /// This is not secure, and not guaranteed to be unique.
106
    pub nickname: Nickname,
107
    /// IPv4 address for this relay.
108
    pub ipv4addr: Option<net::Ipv4Addr>,
109
    /// IPv4 ORPort for this relay.
110
    pub orport: u16,
111
    /// IPv6 address and port for this relay.
112
    // TODO: we don't use a socketaddrv6 because we don't care about
113
    // the flow and scope fields.  We should decide whether that's a
114
    // good idea.
115
    pub ipv6addr: Option<(net::Ipv6Addr, u16)>,
116
    /// Directory port for contacting this relay for direct HTTP
117
    /// directory downloads.
118
    pub dirport: u16,
119
    /// Declared uptime for this relay, in seconds.
120
    pub uptime: Option<u64>,
121
    /// Time when this router descriptor was published.
122
    pub published: time::SystemTime,
123
    /// Ed25519 identity certificate (identity key authenticating a
124
    /// signing key)
125
    pub identity_cert: tor_cert::Ed25519Cert,
126
    /// RSA identity key for this relay. (Deprecated; never use this without
127
    /// the ed25519 identity as well).
128
    pub rsa_identity_key: ll::pk::rsa::PublicKey,
129
    /// RSA identity key for this relay. (Deprecated; never use this without
130
    /// the ed25519 identity as well).
131
    pub rsa_identity: ll::pk::rsa::RsaIdentity,
132
    /// Key for extending a circuit to this relay using the ntor protocol.
133
    pub ntor_onion_key: ll::pk::curve25519::PublicKey,
134
    /// Key for extending a circuit to this relay using the
135
    /// (deprecated) TAP protocol.
136
    pub tap_onion_key: Option<ll::pk::rsa::PublicKey>,
137
    /// List of subprotocol versions supported by this relay.
138
    pub proto: tor_protover::Protocols,
139
    /// True if this relay says it's a directory cache.
140
    pub is_dircache: bool,
141
    /// True if this relay says that it caches extrainfo documents.
142
    pub is_extrainfo_cache: bool,
143
    /// Declared family members for this relay.  If two relays are in the
144
    /// same family, they shouldn't be used in the same circuit.
145
    pub family: Arc<RelayFamily>,
146
    /// Declared (and proven) family IDs for this relay. If two relays
147
    /// share a family ID, they shouldn't be used in the same circuit.
148
    family_ids: Vec<RelayFamilyId>,
149
    /// Software and version that this relay says it's running.
150
    pub platform: Option<RelayPlatform>,
151
    /// A complete address-level policy for which IPv4 addresses this relay
152
    /// says it supports.
153
    // TODO: these polices can get bulky too. Perhaps we should
154
    // de-duplicate them too.
155
    pub ipv4_policy: AddrPolicy,
156
    /// A summary of which ports this relay is willing to connect to
157
    /// on IPv6.
158
    pub ipv6_policy: Arc<PortPolicy>,
159
}
160

            
161
/// Description of the software a relay is running.
162
#[derive(Debug, Clone, PartialEq, Eq)]
163
#[non_exhaustive]
164
pub enum RelayPlatform {
165
    /// Software advertised to be some version of Tor, on some platform.
166
    Tor(TorVersion, String),
167
    /// Software not advertised to be Tor.
168
    Other(String),
169
}
170

            
171
impl std::str::FromStr for RelayPlatform {
172
    type Err = Error;
173
2246
    fn from_str(args: &str) -> Result<Self> {
174
2246
        if args.starts_with("Tor ") {
175
2244
            let v: Vec<_> = args.splitn(4, ' ').collect();
176
2244
            match &v[..] {
177
2238
                ["Tor", ver, "on", p] => Ok(RelayPlatform::Tor(ver.parse()?, (*p).to_string())),
178
6
                ["Tor", ver, ..] => Ok(RelayPlatform::Tor(ver.parse()?, "".to_string())),
179
                _ => unreachable!(),
180
            }
181
        } else {
182
2
            Ok(RelayPlatform::Other(args.to_string()))
183
        }
184
2246
    }
185
}
186

            
187
decl_keyword! {
188
    /// RouterKwd is an instance of Keyword, used to denote the different
189
    /// Items that are recognized as appearing in a router descriptor.
190
    RouterKwd {
191
        annotation "@source" => ANN_SOURCE,
192
        annotation "@downloaded-at" => ANN_DOWNLOADED_AT,
193
        annotation "@purpose" => ANN_PURPOSE,
194
        "accept" | "reject" => POLICY,
195
        "bandwidth" => BANDWIDTH,
196
        "bridge-distribution-request" => BRIDGE_DISTRIBUTION_REQUEST,
197
        "caches-extra-info" => CACHES_EXTRA_INFO,
198
        "contact" => CONTACT,
199
        "extra-info-digest" => EXTRA_INFO_DIGEST,
200
        "family" => FAMILY,
201
        "family-cert" => FAMILY_CERT,
202
        "fingerprint" => FINGERPRINT,
203
        "hibernating" => HIBERNATING,
204
        "identity-ed25519" => IDENTITY_ED25519,
205
        "ipv6-policy" => IPV6_POLICY,
206
        "master-key-ed25519" => MASTER_KEY_ED25519,
207
        "ntor-onion-key" => NTOR_ONION_KEY,
208
        "ntor-onion-key-crosscert" => NTOR_ONION_KEY_CROSSCERT,
209
        "onion-key" => ONION_KEY,
210
        "onion-key-crosscert" => ONION_KEY_CROSSCERT,
211
        "or-address" => OR_ADDRESS,
212
        "platform" => PLATFORM,
213
        "proto" => PROTO,
214
        "published" => PUBLISHED,
215
        "router" => ROUTER,
216
        "router-sig-ed25519" => ROUTER_SIG_ED25519,
217
        "router-signature" => ROUTER_SIGNATURE,
218
        "signing-key" => SIGNING_KEY,
219
        "tunnelled_dir_server" => TUNNELLED_DIR_SERVER,
220
        "uptime" => UPTIME,
221
        // "protocols" once existed, but is obsolete
222
        // "eventdns" once existed, but is obsolete
223
        // "allow-single-hop-exits" is also obsolete.
224
    }
225
}
226

            
227
/// Rules for parsing a set of router descriptor annotations.
228
2
static ROUTER_ANNOTATIONS: LazyLock<SectionRules<RouterKwd>> = LazyLock::new(|| {
229
    use RouterKwd::*;
230

            
231
2
    let mut rules = SectionRules::builder();
232
2
    rules.add(ANN_SOURCE.rule());
233
2
    rules.add(ANN_DOWNLOADED_AT.rule().args(1..));
234
2
    rules.add(ANN_PURPOSE.rule().args(1..));
235
2
    rules.add(ANN_UNRECOGNIZED.rule().may_repeat().obj_optional());
236
    // Unrecognized annotations are fine; anything else is an error in this
237
    // context.
238
2
    rules.reject_unrecognized();
239
2
    rules.build()
240
2
});
241
/// Rules for tokens that are allowed in the first part of a
242
/// router descriptor.
243
55
static ROUTER_HEADER_RULES: LazyLock<SectionRules<RouterKwd>> = LazyLock::new(|| {
244
    use RouterKwd::*;
245

            
246
55
    let mut rules = SectionRules::builder();
247
55
    rules.add(ROUTER.rule().required().args(5..));
248
55
    rules.add(IDENTITY_ED25519.rule().required().no_args().obj_required());
249
    // No other intervening tokens are permitted in the header.
250
55
    rules.reject_unrecognized();
251
55
    rules.build()
252
55
});
253
/// Rules for  tokens that are allowed in the first part of a
254
/// router descriptor.
255
55
static ROUTER_BODY_RULES: LazyLock<SectionRules<RouterKwd>> = LazyLock::new(|| {
256
    use RouterKwd::*;
257

            
258
55
    let mut rules = SectionRules::builder();
259
55
    rules.add(MASTER_KEY_ED25519.rule().required().args(1..));
260
55
    rules.add(PLATFORM.rule());
261
55
    rules.add(PUBLISHED.rule().required());
262
55
    rules.add(FINGERPRINT.rule());
263
55
    rules.add(UPTIME.rule().args(1..));
264
55
    rules.add(ONION_KEY.rule().no_args().obj_required());
265
55
    rules.add(ONION_KEY_CROSSCERT.rule().no_args().obj_required());
266
55
    rules.add(NTOR_ONION_KEY.rule().required().args(1..));
267
55
    rules.add(
268
55
        NTOR_ONION_KEY_CROSSCERT
269
55
            .rule()
270
55
            .required()
271
55
            .args(1..=1)
272
55
            .obj_required(),
273
    );
274
55
    rules.add(SIGNING_KEY.rule().no_args().required().obj_required());
275
55
    rules.add(POLICY.rule().may_repeat().args(1..));
276
55
    rules.add(IPV6_POLICY.rule().args(2..));
277
55
    rules.add(FAMILY.rule().args(1..));
278
55
    rules.add(FAMILY_CERT.rule().obj_required().may_repeat());
279
55
    rules.add(CACHES_EXTRA_INFO.rule().no_args());
280
55
    rules.add(OR_ADDRESS.rule().may_repeat().args(1..));
281
55
    rules.add(TUNNELLED_DIR_SERVER.rule());
282
55
    rules.add(PROTO.rule().required().args(1..));
283
55
    rules.add(UNRECOGNIZED.rule().may_repeat().obj_optional());
284
    // TODO: these aren't parsed yet.  Only authorities use them.
285
55
    {
286
55
        rules.add(BANDWIDTH.rule().required().args(3..));
287
55
        rules.add(BRIDGE_DISTRIBUTION_REQUEST.rule().args(1..));
288
55
        rules.add(HIBERNATING.rule().args(1..));
289
55
        rules.add(CONTACT.rule());
290
55
    }
291
    // TODO: this is ignored for now.
292
55
    {
293
55
        rules.add(EXTRA_INFO_DIGEST.rule().args(1..));
294
55
    }
295
55
    rules.build()
296
55
});
297

            
298
/// Rules for items that appear at the end of a router descriptor.
299
55
static ROUTER_SIG_RULES: LazyLock<SectionRules<RouterKwd>> = LazyLock::new(|| {
300
    use RouterKwd::*;
301

            
302
55
    let mut rules = SectionRules::builder();
303
55
    rules.add(ROUTER_SIG_ED25519.rule().required().args(1..));
304
55
    rules.add(ROUTER_SIGNATURE.rule().required().no_args().obj_required());
305
    // No intervening tokens are allowed in the footer.
306
55
    rules.reject_unrecognized();
307
55
    rules.build()
308
55
});
309

            
310
impl RouterAnnotation {
311
    /// Extract a single RouterAnnotation (possibly empty) from a reader.
312
8
    fn take_from_reader(reader: &mut NetDocReader<'_, RouterKwd>) -> Result<RouterAnnotation> {
313
        use RouterKwd::*;
314
20
        let mut items = reader.pause_at(|item| item.is_ok_with_non_annotation());
315

            
316
8
        let body = ROUTER_ANNOTATIONS.parse(&mut items)?;
317

            
318
6
        let source = body.maybe(ANN_SOURCE).args_as_str().map(String::from);
319
6
        let purpose = body.maybe(ANN_PURPOSE).args_as_str().map(String::from);
320
6
        let downloaded = body
321
6
            .maybe(ANN_DOWNLOADED_AT)
322
6
            .parse_args_as_str::<Iso8601TimeSp>()?
323
6
            .map(|t| t.into());
324
6
        Ok(RouterAnnotation {
325
6
            source,
326
6
            downloaded,
327
6
            purpose,
328
6
        })
329
8
    }
330
}
331

            
332
/// A parsed router descriptor whose signatures and/or validity times
333
/// may or may not be invalid.
334
pub type UncheckedRouterDesc = signed::SignatureGated<timed::TimerangeBound<RouterDesc>>;
335

            
336
/// How long after its published time is a router descriptor officially
337
/// supposed to be usable?
338
const ROUTER_EXPIRY_SECONDS: u64 = 5 * 86400;
339

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

            
347
impl RouterDesc {
348
    /// Return a reference to this relay's RSA identity.
349
2
    pub fn rsa_identity(&self) -> &RsaIdentity {
350
2
        &self.rsa_identity
351
2
    }
352

            
353
    /// Return a reference to this relay's Ed25519 identity.
354
2
    pub fn ed_identity(&self) -> &Ed25519Identity {
355
2
        self.identity_cert
356
2
            .signing_key()
357
2
            .expect("No ed25519 identity key on identity cert")
358
2
    }
359

            
360
    /// Return a reference to the list of subprotocol versions supported by this
361
    /// relay.
362
2
    pub fn protocols(&self) -> &tor_protover::Protocols {
363
2
        &self.proto
364
2
    }
365

            
366
    /// Return a reference to this relay's Ntor onion key.
367
2
    pub fn ntor_onion_key(&self) -> &ll::pk::curve25519::PublicKey {
368
2
        &self.ntor_onion_key
369
2
    }
370

            
371
    /// Return the publication
372
320
    pub fn published(&self) -> time::SystemTime {
373
320
        self.published
374
320
    }
375

            
376
    /// Return an iterator of every `SocketAddr` at which this descriptor says
377
    /// its relay can be reached.
378
2
    pub fn or_ports(&self) -> impl Iterator<Item = net::SocketAddr> + '_ {
379
2
        self.ipv4addr
380
3
            .map(|a| net::SocketAddr::new(a.into(), self.orport))
381
2
            .into_iter()
382
2
            .chain(self.ipv6addr.map(net::SocketAddr::from))
383
2
    }
384

            
385
    /// Return the declared family of this descriptor.
386
    pub fn family(&self) -> Arc<RelayFamily> {
387
        Arc::clone(&self.family)
388
    }
389

            
390
    /// Return the authenticated family IDs of this descriptor.
391
2
    pub fn family_ids(&self) -> &[RelayFamilyId] {
392
2
        &self.family_ids[..]
393
2
    }
394

            
395
    /// Helper: tokenize `s`, and divide it into three validated sections.
396
2366
    fn parse_sections<'a>(
397
2366
        reader: &mut NetDocReader<'a, RouterKwd>,
398
2366
    ) -> Result<(
399
2366
        Section<'a, RouterKwd>,
400
2366
        Section<'a, RouterKwd>,
401
2366
        Section<'a, RouterKwd>,
402
2366
    )> {
403
        use RouterKwd::*;
404

            
405
        // Parse everything up through the header.
406
2366
        let header = ROUTER_HEADER_RULES.parse(
407
6941
            reader.pause_at(|item| item.is_ok_with_kwd_not_in(&[ROUTER, IDENTITY_ED25519])),
408
110
        )?;
409

            
410
        // Parse everything up to but not including the signature.
411
2256
        let body =
412
38453
            ROUTER_BODY_RULES.parse(reader.pause_at(|item| {
413
38396
                item.is_ok_with_kwd_in(&[ROUTER_SIGNATURE, ROUTER_SIG_ED25519])
414
38396
            }))?;
415

            
416
        // Parse the signature.
417
4583
        let sig = ROUTER_SIG_RULES.parse(reader.pause_at(|item| {
418
4526
            item.is_ok_with_annotation() || item.is_ok_with_kwd(ROUTER) || item.is_empty_line()
419
4526
        }))?;
420

            
421
2256
        Ok((header, body, sig))
422
2366
    }
423

            
424
    /// Try to parse `s` as a router descriptor.
425
    ///
426
    /// Does not actually check liveness or signatures; you need to do that
427
    /// yourself before you can do the output.
428
2360
    pub fn parse(s: &str) -> Result<UncheckedRouterDesc> {
429
2360
        let mut reader = crate::parse::tokenize::NetDocReader::new(s)?;
430
2373
        let result = Self::parse_internal(&mut reader).map_err(|e| e.within(s))?;
431
        // We permit empty lines at the end of router descriptors, since there's
432
        // a known issue in Tor relays that causes them to return them this way.
433
2232
        reader
434
2232
            .should_be_exhausted_but_for_empty_lines()
435
2232
            .map_err(|e| e.within(s))?;
436
2232
        Ok(result)
437
2360
    }
438

            
439
    /// Helper: parse a router descriptor from `s`.
440
    ///
441
    /// This function does the same as parse(), but returns errors based on
442
    /// byte-wise positions.  The parse() function converts such errors
443
    /// into line-and-byte positions.
444
2366
    fn parse_internal(r: &mut NetDocReader<'_, RouterKwd>) -> Result<UncheckedRouterDesc> {
445
        // TODO: This function is too long!  The little "paragraphs" here
446
        // that parse one item at a time should be made into sub-functions.
447
        use RouterKwd::*;
448

            
449
2366
        let s = r.str();
450
2366
        let (header, body, sig) = RouterDesc::parse_sections(r)?;
451

            
452
        // Unwrap should be safe because inline `required` call should return
453
        // `Error::MissingToken` if `ROUTER` is not `Ok`
454
        #[allow(clippy::unwrap_used)]
455
2256
        let start_offset = header.required(ROUTER)?.offset_in(s).unwrap();
456

            
457
        // ed25519 identity and signing key.
458
2248
        let (identity_cert, ed25519_signing_key) = {
459
2256
            let cert_tok = header.required(IDENTITY_ED25519)?;
460
            // Unwrap should be safe because above `required` call should
461
            // return `Error::MissingToken` if `IDENTITY_ED25519` is not `Ok`
462
            #[allow(clippy::unwrap_used)]
463
2256
            if cert_tok.offset_in(s).unwrap() < start_offset {
464
2
                return Err(EK::MisplacedToken
465
2
                    .with_msg("identity-ed25519")
466
2
                    .at_pos(cert_tok.pos()));
467
2254
            }
468
2254
            let cert: tor_cert::UncheckedCert = cert_tok
469
2254
                .parse_obj::<UnvalidatedEdCert>("ED25519 CERT")?
470
2254
                .check_cert_type(tor_cert::CertType::IDENTITY_V_SIGNING)?
471
2254
                .into_unchecked()
472
2254
                .should_have_signing_key()
473
2255
                .map_err(|err| {
474
2
                    EK::BadObjectVal
475
2
                        .err()
476
2
                        .with_source(err)
477
2
                        .at_pos(cert_tok.pos())
478
3
                })?;
479
2253
            let sk = *cert.peek_subject_key().as_ed25519().ok_or_else(|| {
480
2
                EK::BadObjectVal
481
2
                    .at_pos(cert_tok.pos())
482
2
                    .with_msg("wrong type for signing key in cert")
483
3
            })?;
484
2251
            let sk: ll::pk::ed25519::PublicKey = sk.try_into().map_err(|_| {
485
2
                EK::BadObjectVal
486
2
                    .at_pos(cert_tok.pos())
487
2
                    .with_msg("invalid ed25519 signing key")
488
3
            })?;
489
2248
            (cert, sk)
490
        };
491

            
492
        // master-key-ed25519: required, and should match certificate.
493
        #[allow(unexpected_cfgs)]
494
        {
495
2248
            let master_key_tok = body.required(MASTER_KEY_ED25519)?;
496
2248
            let ed_id: Ed25519Public = master_key_tok.parse_arg(0)?;
497
2248
            let ed_id: ll::pk::ed25519::Ed25519Identity = ed_id.into();
498
2248
            if ed_id != *identity_cert.peek_signing_key() {
499
                #[cfg(not(fuzzing))] // No feature here; never omit in production.
500
2
                return Err(EK::BadObjectVal
501
2
                    .at_pos(master_key_tok.pos())
502
2
                    .with_msg("master-key-ed25519 does not match key in identity-ed25519"));
503
2246
            }
504
        }
505

            
506
        // Legacy RSA identity
507
2246
        let rsa_identity_key: ll::pk::rsa::PublicKey = body
508
2246
            .required(SIGNING_KEY)?
509
2246
            .parse_obj::<RsaPublicParse1Helper>("RSA PUBLIC KEY")?
510
2246
            .check_len_eq(1024)?
511
2246
            .check_exponent(65537)?
512
2246
            .into();
513
2246
        let rsa_identity = rsa_identity_key.to_rsa_identity();
514

            
515
2246
        let ed_sig = sig.required(ROUTER_SIG_ED25519)?;
516
2246
        let rsa_sig = sig.required(ROUTER_SIGNATURE)?;
517
        // Unwrap should be safe because above `required` calls should return
518
        // an `Error::MissingToken` if `ROUTER_...` is not `Ok`
519
        #[allow(clippy::unwrap_used)]
520
2246
        let ed_sig_pos = ed_sig.offset_in(s).unwrap();
521
        #[allow(clippy::unwrap_used)]
522
2246
        let rsa_sig_pos = rsa_sig.offset_in(s).unwrap();
523

            
524
2246
        if ed_sig_pos > rsa_sig_pos {
525
2
            return Err(EK::UnexpectedToken
526
2
                .with_msg(ROUTER_SIG_ED25519.to_str())
527
2
                .at_pos(ed_sig.pos()));
528
2244
        }
529

            
530
        // Extract ed25519 signature.
531
2244
        let ed_signature: ll::pk::ed25519::ValidatableEd25519Signature = {
532
2244
            let mut d = ll::d::Sha256::new();
533
2244
            d.update(&b"Tor router descriptor signature v1"[..]);
534
2244
            let signed_end = ed_sig_pos + b"router-sig-ed25519 ".len();
535
2244
            d.update(&s[start_offset..signed_end]);
536
2244
            let d = d.finalize();
537
2244
            let sig: [u8; 64] = ed_sig
538
2244
                .parse_arg::<B64>(0)?
539
2244
                .into_array()
540
2244
                .map_err(|_| EK::BadSignature.at_pos(ed_sig.pos()))?;
541
2244
            let sig = ll::pk::ed25519::Signature::from(sig);
542
2244
            ll::pk::ed25519::ValidatableEd25519Signature::new(ed25519_signing_key, sig, &d)
543
        };
544

            
545
        // Extract legacy RSA signature.
546
2244
        let rsa_signature: ll::pk::rsa::ValidatableRsaSignature = {
547
2244
            let mut d = ll::d::Sha1::new();
548
2244
            let signed_end = rsa_sig_pos + b"router-signature\n".len();
549
2244
            d.update(&s[start_offset..signed_end]);
550
2244
            let d = d.finalize();
551
2244
            let sig = rsa_sig.obj("SIGNATURE")?;
552
            // TODO: we need to accept prefixes here. COMPAT BLOCKER.
553

            
554
2244
            ll::pk::rsa::ValidatableRsaSignature::new(&rsa_identity_key, &sig, &d)
555
        };
556

            
557
        // router nickname ipv4addr orport socksport dirport
558
2244
        let (nickname, ipv4addr, orport, dirport) = {
559
2244
            let rtrline = header.required(ROUTER)?;
560
            (
561
2244
                rtrline.parse_arg::<Nickname>(0)?,
562
2244
                Some(rtrline.parse_arg::<net::Ipv4Addr>(1)?),
563
2244
                rtrline.parse_arg(2)?,
564
                // Skipping socksport.
565
2244
                rtrline.parse_arg(4)?,
566
            )
567
        };
568

            
569
        // uptime
570
2244
        let uptime = body.maybe(UPTIME).parse_arg(0)?;
571

            
572
        // published time.
573
2244
        let published = body
574
2244
            .required(PUBLISHED)?
575
2244
            .args_as_str()
576
2244
            .parse::<Iso8601TimeSp>()?
577
2244
            .into();
578

            
579
        // ntor key
580
2244
        let ntor_onion_key: Curve25519Public = body.required(NTOR_ONION_KEY)?.parse_arg(0)?;
581
2244
        let ntor_onion_key: ll::pk::curve25519::PublicKey = ntor_onion_key.into();
582
        // ntor crosscert
583
2240
        let crosscert_cert: tor_cert::UncheckedCert = {
584
2244
            let cc = body.required(NTOR_ONION_KEY_CROSSCERT)?;
585
2244
            let sign: u8 = cc.parse_arg(0)?;
586
2244
            if sign != 0 && sign != 1 {
587
4
                return Err(EK::BadArgument.at_pos(cc.arg_pos(0)).with_msg("not 0 or 1"));
588
2240
            }
589
2240
            let ntor_as_ed: ll::pk::ed25519::PublicKey =
590
2240
                ll::pk::keymanip::convert_curve25519_to_ed25519_public(&ntor_onion_key, sign)
591
2240
                    .ok_or_else(|| {
592
                        EK::BadArgument
593
                            .at_pos(cc.pos())
594
                            .with_msg("Uncheckable crosscert")
595
                    })?;
596

            
597
2240
            cc.parse_obj::<UnvalidatedEdCert>("ED25519 CERT")?
598
2240
                .check_cert_type(tor_cert::CertType::NTOR_CC_IDENTITY)?
599
2240
                .check_subject_key_is(identity_cert.peek_signing_key())?
600
2240
                .into_unchecked()
601
2240
                .should_be_signed_with(&ntor_as_ed.into())
602
2240
                .map_err(|err| EK::BadSignature.err().with_source(err))?
603
        };
604

            
605
        // TAP key
606
2240
        let tap_onion_key: Option<ll::pk::rsa::PublicKey> = if let Some(tok) = body.get(ONION_KEY) {
607
            Some(
608
2238
                tok.parse_obj::<RsaPublicParse1Helper>("RSA PUBLIC KEY")?
609
2238
                    .check_len_eq(1024)?
610
2238
                    .check_exponent(65537)?
611
2238
                    .into(),
612
            )
613
        } else {
614
2
            None
615
        };
616

            
617
        // TAP crosscert
618
2240
        let tap_crosscert_sig = if let Some(cc_tok) = body.get(ONION_KEY_CROSSCERT) {
619
2238
            let cc_val = cc_tok.obj("CROSSCERT")?;
620
2238
            let mut signed = Vec::new();
621
2238
            signed.extend(rsa_identity.as_bytes());
622
2238
            signed.extend(identity_cert.peek_signing_key().as_bytes());
623
2238
            Some(ll::pk::rsa::ValidatableRsaSignature::new(
624
2238
                tap_onion_key.as_ref().ok_or_else(|| {
625
                    EK::MissingToken.with_msg("onion-key-crosscert without onion-key")
626
                })?,
627
2238
                &cc_val,
628
2238
                &signed,
629
            ))
630
2
        } else if tap_onion_key.is_some() {
631
            return Err(EK::MissingToken.with_msg("onion-key without onion-key-crosscert"));
632
        } else {
633
2
            None
634
        };
635

            
636
        // List of subprotocol versions
637
2240
        let proto = {
638
2240
            let proto_tok = body.required(PROTO)?;
639
2240
            proto_tok
640
2240
                .args_as_str()
641
2240
                .parse::<tor_protover::Protocols>()
642
2240
                .map_err(|e| EK::BadArgument.at_pos(proto_tok.pos()).with_source(e))?
643
        };
644

            
645
        // tunneled-dir-server
646
2240
        let is_dircache = (dirport != 0) || body.get(TUNNELLED_DIR_SERVER).is_some();
647

            
648
        // caches-extra-info
649
2240
        let is_extrainfo_cache = body.get(CACHES_EXTRA_INFO).is_some();
650

            
651
        // fingerprint: check for consistency with RSA identity.
652
2240
        if let Some(fp_tok) = body.get(FINGERPRINT) {
653
2240
            let fp: RsaIdentity = fp_tok.args_as_str().parse::<SpFingerprint>()?.into();
654
2240
            if fp != rsa_identity {
655
4
                return Err(EK::BadArgument
656
4
                    .at_pos(fp_tok.pos())
657
4
                    .with_msg("fingerprint does not match RSA identity"));
658
2236
            }
659
        }
660

            
661
        // Family
662
2236
        let family = {
663
2236
            let mut family = body
664
2236
                .maybe(FAMILY)
665
2236
                .parse_args_as_str::<RelayFamily>()?
666
2236
                .unwrap_or_else(RelayFamily::new);
667
2236
            if !family.is_empty() {
668
4
                // If this family is nonempty, we add our own RSA id to it, on
669
4
                // the theory that doing so will improve the odds of having a
670
4
                // canonical family shared by all of the members of this family.
671
4
                // If the family is empty, there's no point in adding our own ID
672
4
                // to it, and doing so would only waste memory.
673
4
                family.push(rsa_identity);
674
2232
            }
675
2236
            family.intern()
676
        };
677

            
678
        // Family ids (for "happy families")
679
2236
        let family_certs: Vec<tor_cert::UncheckedCert> = body
680
2236
            .slice(FAMILY_CERT)
681
2236
            .iter()
682
2238
            .map(|ent| {
683
4
                ent.parse_obj::<UnvalidatedEdCert>("FAMILY CERT")?
684
4
                    .check_cert_type(CertType::FAMILY_V_IDENTITY)?
685
4
                    .check_subject_key_is(identity_cert.peek_signing_key())?
686
4
                    .into_unchecked()
687
4
                    .should_have_signing_key()
688
4
                    .map_err(|e| {
689
                        EK::BadObjectVal
690
                            .with_msg("missing public key")
691
                            .at_pos(ent.pos())
692
                            .with_source(e)
693
                    })
694
4
            })
695
2236
            .collect::<Result<_>>()?;
696

            
697
2236
        let mut family_ids: Vec<_> = family_certs
698
2236
            .iter()
699
2238
            .map(|cert| RelayFamilyId::Ed25519(*cert.peek_signing_key()))
700
2236
            .collect();
701
2236
        family_ids.sort();
702
2236
        family_ids.dedup();
703

            
704
        // or-address
705
        // Extract at most one ipv6 address from the list.  It's not great,
706
        // but it's what Tor does.
707
2236
        let mut ipv6addr = None;
708
2236
        for tok in body.slice(OR_ADDRESS) {
709
6
            if let Ok(net::SocketAddr::V6(a)) = tok.parse_arg::<net::SocketAddr>(0) {
710
6
                ipv6addr = Some((*a.ip(), a.port()));
711
6
                break;
712
            }
713
            // We skip over unparsable addresses. Is that right?
714
        }
715

            
716
        // platform
717
2236
        let platform = body.maybe(PLATFORM).parse_args_as_str::<RelayPlatform>()?;
718

            
719
        // ipv4_policy
720
2236
        let ipv4_policy = {
721
2236
            let mut pol = AddrPolicy::new();
722
2238
            for ruletok in body.slice(POLICY).iter() {
723
2238
                let accept = match ruletok.kwd_str() {
724
2238
                    "accept" => RuleKind::Accept,
725
2236
                    "reject" => RuleKind::Reject,
726
                    _ => {
727
                        return Err(Error::from(internal!(
728
                            "tried to parse a strange line as a policy"
729
                        ))
730
                        .at_pos(ruletok.pos()));
731
                    }
732
                };
733
2238
                let pat: AddrPortPattern = ruletok
734
2238
                    .args_as_str()
735
2238
                    .parse()
736
2238
                    .map_err(|e| EK::BadPolicy.at_pos(ruletok.pos()).with_source(e))?;
737
2238
                pol.push(accept, pat);
738
            }
739
2236
            pol
740
        };
741

            
742
        // ipv6 policy
743
2236
        let ipv6_policy = match body.get(IPV6_POLICY) {
744
2
            Some(p) => p
745
2
                .args_as_str()
746
2
                .parse()
747
3
                .map_err(|e| EK::BadPolicy.at_pos(p.pos()).with_source(e))?,
748
            // Unwrap is safe here because str is not empty
749
            #[allow(clippy::unwrap_used)]
750
2234
            None => "reject 1-65535".parse::<PortPolicy>().unwrap(),
751
        };
752

            
753
        // Now we're going to collect signatures and expiration times.
754
2234
        let (identity_cert, identity_sig) = identity_cert.dangerously_split().map_err(|err| {
755
            EK::BadObjectVal
756
                .with_msg("missing public key")
757
                .with_source(err)
758
        })?;
759
2234
        let (crosscert_cert, cc_sig) = crosscert_cert.dangerously_split().map_err(|err| {
760
            EK::BadObjectVal
761
                .with_msg("missing public key")
762
                .with_source(err)
763
        })?;
764
2234
        let mut signatures: Vec<Box<dyn ll::pk::ValidatableSignature>> = vec![
765
2234
            Box::new(rsa_signature),
766
2234
            Box::new(ed_signature),
767
2234
            Box::new(identity_sig),
768
2234
            Box::new(cc_sig),
769
        ];
770
2234
        if let Some(s) = tap_crosscert_sig {
771
2232
            signatures.push(Box::new(s));
772
2232
        }
773

            
774
2234
        let identity_cert = identity_cert.dangerously_assume_timely();
775
2234
        let crosscert_cert = crosscert_cert.dangerously_assume_timely();
776
2234
        let mut expirations = vec![
777
2234
            published + time::Duration::new(ROUTER_EXPIRY_SECONDS, 0),
778
2234
            identity_cert.expiry(),
779
2234
            crosscert_cert.expiry(),
780
        ];
781

            
782
2238
        for cert in family_certs {
783
4
            let (inner, sig) = cert.dangerously_split().map_err(into_internal!(
784
                "Missing a public key that was previously there."
785
            ))?;
786
4
            signatures.push(Box::new(sig));
787
4
            expirations.push(inner.dangerously_assume_timely().expiry());
788
        }
789

            
790
        // Unwrap is safe here because `expirations` array is not empty
791
        #[allow(clippy::unwrap_used)]
792
2234
        let expiry = *expirations.iter().min().unwrap();
793

            
794
2234
        let start_time = published - time::Duration::new(ROUTER_PRE_VALIDITY_SECONDS, 0);
795

            
796
2234
        let desc = RouterDesc {
797
2234
            nickname,
798
2234
            ipv4addr,
799
2234
            orport,
800
2234
            ipv6addr,
801
2234
            dirport,
802
2234
            uptime,
803
2234
            published,
804
2234
            identity_cert,
805
2234
            rsa_identity_key,
806
2234
            rsa_identity,
807
2234
            ntor_onion_key,
808
2234
            tap_onion_key,
809
2234
            proto,
810
2234
            is_dircache,
811
2234
            is_extrainfo_cache,
812
2234
            family,
813
2234
            family_ids,
814
2234
            platform,
815
2234
            ipv4_policy,
816
2234
            ipv6_policy: ipv6_policy.intern(),
817
2234
        };
818

            
819
2234
        let time_gated = timed::TimerangeBound::new(desc, start_time..expiry);
820
2234
        let sig_gated = signed::SignatureGated::new(time_gated, signatures);
821

            
822
2234
        Ok(sig_gated)
823
2366
    }
824
}
825

            
826
/// An iterator that parses one or more (possibly annotated
827
/// router descriptors from a string.
828
//
829
// TODO: This is largely copy-pasted from MicrodescReader. Can/should they
830
// be merged?
831
pub struct RouterReader<'a> {
832
    /// True iff we accept annotations
833
    annotated: bool,
834
    /// Reader that we're extracting items from.
835
    reader: NetDocReader<'a, RouterKwd>,
836
}
837

            
838
/// Skip this reader forward until the next thing it reads looks like the
839
/// start of a router descriptor.
840
///
841
/// Used to recover from errors.
842
6
fn advance_to_next_routerdesc(reader: &mut NetDocReader<'_, RouterKwd>, annotated: bool) {
843
    use RouterKwd::*;
844
    loop {
845
6
        let item = reader.peek();
846
4
        match item {
847
4
            Some(Ok(t)) => {
848
4
                let kwd = t.kwd();
849
4
                if (annotated && kwd.is_annotation()) || kwd == ROUTER {
850
4
                    return;
851
                }
852
            }
853
            Some(Err(_)) => {
854
                // Skip over broken tokens.
855
            }
856
            None => {
857
2
                return;
858
            }
859
        }
860
        let _ = reader.next();
861
    }
862
6
}
863

            
864
impl<'a> RouterReader<'a> {
865
    /// Construct a RouterReader to take router descriptors from a string.
866
2
    pub fn new(s: &'a str, allow: &AllowAnnotations) -> Result<Self> {
867
2
        let reader = NetDocReader::new(s)?;
868
2
        let annotated = allow == &AllowAnnotations::AnnotationsAllowed;
869
2
        Ok(RouterReader { annotated, reader })
870
2
    }
871

            
872
    /// Extract an annotation from this reader.
873
8
    fn take_annotation(&mut self) -> Result<RouterAnnotation> {
874
8
        if self.annotated {
875
8
            RouterAnnotation::take_from_reader(&mut self.reader)
876
        } else {
877
            Ok(RouterAnnotation::default())
878
        }
879
8
    }
880

            
881
    /// Extract an annotated router descriptor from this reader
882
    ///
883
    /// (internal helper; does not clean up on failures.)
884
8
    fn take_annotated_routerdesc_raw(&mut self) -> Result<AnnotatedRouterDesc> {
885
8
        let ann = self.take_annotation()?;
886
6
        let router = RouterDesc::parse_internal(&mut self.reader)?;
887
2
        Ok(AnnotatedRouterDesc { ann, router })
888
8
    }
889

            
890
    /// Extract an annotated router descriptor from this reader
891
    ///
892
    /// Ensure that at least one token is consumed
893
8
    fn take_annotated_routerdesc(&mut self) -> Result<AnnotatedRouterDesc> {
894
8
        let pos_orig = self.reader.pos();
895
8
        let result = self.take_annotated_routerdesc_raw();
896
8
        if result.is_err() {
897
6
            if self.reader.pos() == pos_orig {
898
                // No tokens were consumed from the reader.  We need
899
                // to drop at least one token to ensure we aren't in
900
                // an infinite loop.
901
                //
902
                // (This might not be able to happen, but it's easier to
903
                // explicitly catch this case than it is to prove that
904
                // it's impossible.)
905
                let _ = self.reader.next();
906
6
            }
907
6
            advance_to_next_routerdesc(&mut self.reader, self.annotated);
908
2
        }
909
8
        result
910
8
    }
911
}
912

            
913
impl<'a> Iterator for RouterReader<'a> {
914
    type Item = Result<AnnotatedRouterDesc>;
915
10
    fn next(&mut self) -> Option<Self::Item> {
916
        // Is there a next token? If not, we're done.
917
10
        self.reader.peek()?;
918

            
919
        Some(
920
8
            self.take_annotated_routerdesc()
921
11
                .map_err(|e| e.within(self.reader.str())),
922
        )
923
10
    }
924
}
925

            
926
#[cfg(test)]
927
mod test {
928
    // @@ begin test lint list maintained by maint/add_warning @@
929
    #![allow(clippy::bool_assert_comparison)]
930
    #![allow(clippy::clone_on_copy)]
931
    #![allow(clippy::dbg_macro)]
932
    #![allow(clippy::mixed_attributes_style)]
933
    #![allow(clippy::print_stderr)]
934
    #![allow(clippy::print_stdout)]
935
    #![allow(clippy::single_char_pattern)]
936
    #![allow(clippy::unwrap_used)]
937
    #![allow(clippy::unchecked_time_subtraction)]
938
    #![allow(clippy::useless_vec)]
939
    #![allow(clippy::needless_pass_by_value)]
940
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
941
    use super::*;
942
    const TESTDATA: &str = include_str!("../../testdata/routerdesc1.txt");
943
    const TESTDATA2: &str = include_str!("../../testdata/routerdesc2.txt");
944
    // Generated with a patched C tor to include "happy family" IDs.
945
    const TESTDATA3: &str = include_str!("../../testdata/routerdesc3.txt");
946

            
947
    fn read_bad(fname: &str) -> String {
948
        use std::fs;
949
        use std::path::PathBuf;
950
        let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
951
        path.push("testdata");
952
        path.push("bad-routerdesc");
953
        path.push(fname);
954

            
955
        fs::read_to_string(path).unwrap()
956
    }
957

            
958
    #[test]
959
    fn parse_arbitrary() -> Result<()> {
960
        use std::str::FromStr;
961
        use tor_checkable::{SelfSigned, Timebound};
962
        let rd = RouterDesc::parse(TESTDATA)?
963
            .check_signature()?
964
            .dangerously_assume_timely();
965

            
966
        assert_eq!(rd.nickname.as_str(), "Akka");
967
        assert_eq!(rd.orport, 443);
968
        assert_eq!(rd.dirport, 0);
969
        assert_eq!(rd.uptime, Some(1036923));
970
        assert_eq!(
971
            rd.family.as_ref(),
972
            &RelayFamily::from_str(
973
                "$303509ab910ef207b7438c27435c4a2fd579f1b1 \
974
                 $56927e61b51e6f363fb55498150a6ddfcf7077f2"
975
            )
976
            .unwrap()
977
        );
978

            
979
        assert_eq!(
980
            rd.rsa_identity().to_string(),
981
            "$56927e61b51e6f363fb55498150a6ddfcf7077f2"
982
        );
983
        assert_eq!(
984
            rd.ed_identity().to_string(),
985
            "CVTjf1oeaL616hH+1+UvYZ8OgkwF3z7UMITvJzm5r7A"
986
        );
987
        assert_eq!(
988
            rd.protocols().to_string(),
989
            "Cons=1-2 Desc=1-2 DirCache=2 FlowCtrl=1-2 HSDir=2 \
990
             HSIntro=4-5 HSRend=1-2 Link=1-5 LinkAuth=1,3 Microdesc=1-2 \
991
             Padding=2 Relay=1-4"
992
        );
993

            
994
        assert_eq!(
995
            hex::encode(rd.ntor_onion_key().to_bytes()),
996
            "329b3b52991613392e35d1a821dd6753e1210458ecc3337f7b7d39bfcf5da273"
997
        );
998
        assert_eq!(
999
            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.tap_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.tap_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 p = "Tor 0.4.4.4-alpha on a flying bison".parse::<RelayPlatform>();
        assert!(p.is_ok());
        assert_eq!(
            p.unwrap(),
            RelayPlatform::Tor(
                "0.4.4.4-alpha".parse().unwrap(),
                "a flying bison".to_string()
            )
        );
        let p = "Tor 0.4.4.4-alpha on".parse::<RelayPlatform>();
        assert!(p.is_ok());
        let p = "Tor 0.4.4.4-alpha ".parse::<RelayPlatform>();
        assert!(p.is_ok());
        let p = "Tor 0.4.4.4-alpha".parse::<RelayPlatform>();
        assert!(p.is_ok());
        let p = "arti 0.0.0".parse::<RelayPlatform>();
        assert!(p.is_ok());
        assert_eq!(p.unwrap(), RelayPlatform::Other("arti 0.0.0".to_string()));
    }
    #[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(),
            &[
                "ed25519:7sToQRuge1bU2hS0CG0ViMndc4m82JhO4B4kdrQey80"
                    .parse()
                    .unwrap(),
                "ed25519:szHUS3ItRd9uk85b1UVnOZx1gg4B0266jCpbuIMNjcM"
                    .parse()
                    .unwrap(),
            ]
        );
        Ok(())
    }
}