1
//! Types used to parse arguments of entries in a directory document.
2
//!
3
//! There are some types that are pretty common, like "ISOTime",
4
//! "base64-encoded data", and so on.
5
//!
6
//! These types shouldn't be exposed outside of the netdoc crate.
7

            
8
pub(crate) use b16impl::*;
9
pub use b64impl::*;
10
pub use contact_info::*;
11
pub use curve25519impl::*;
12
pub use ed25519impl::*;
13
#[cfg(any(feature = "routerdesc", feature = "hs-common"))]
14
pub(crate) use edcert::*;
15
pub(crate) use fingerprint::*;
16
pub use rsa::*;
17
pub use timeimpl::*;
18

            
19
#[cfg(feature = "encode")]
20
use {
21
    crate::encode::{
22
        self,
23
        ItemEncoder,
24
        ItemObjectEncodable,
25
        ItemValueEncodable,
26
        // `E` for "encode`; different from `parse2::MultiplicitySelector`
27
        MultiplicitySelector as EMultiplicitySelector,
28
        NetdocEncoder,
29
    },
30
    digest::Digest as _,
31
    std::iter,
32
    std::result::Result as StdResult,
33
    tor_error::into_internal,
34
};
35
#[cfg(feature = "parse2")]
36
use {
37
    crate::parse2::multiplicity::{
38
        ItemSetMethods,
39
        // `P2` for "parse2`; different from `encode::MultiplicitySelector`
40
        MultiplicitySelector as P2MultiplicitySelector,
41
        ObjectSetMethods,
42
    },
43
    crate::parse2::sig_hashes::Sha1WholeKeywordLine,
44
    crate::parse2::{
45
        self, ArgumentError, ArgumentStream, ItemArgumentParseable, ItemObjectParseable,
46
        ItemValueParseable, SignatureHashInputs, UnparsedItem,
47
    },
48
};
49

            
50
pub use nickname::Nickname;
51

            
52
pub use fingerprint::{Base64Fingerprint, Fingerprint};
53

            
54
pub use identified_digest::{DigestName, IdentifiedDigest};
55

            
56
pub use ignored_impl::{Ignored, IgnoredItemOrObjectValue, NotPresent};
57

            
58
use crate::NormalItemArgument;
59
use derive_deftly::{Deftly, define_derive_deftly, define_derive_deftly_module};
60
use std::cmp::{self, PartialOrd};
61
use std::fmt::{self, Display};
62
use std::marker::PhantomData;
63
use std::ops::{Deref, DerefMut};
64
use std::str::FromStr;
65
use subtle::{Choice, ConstantTimeEq};
66
use tor_error::{Bug, ErrorReport as _, internal};
67
use void::{ResultVoidExt as _, Void};
68

            
69
/// Describes a value that van be decoded from a bunch of bytes.
70
///
71
/// Used for decoding the objects between BEGIN and END tags.
72
pub(crate) trait FromBytes: Sized {
73
    /// Try to parse a value of this type from a byte slice
74
    fn from_bytes(b: &[u8], p: crate::Pos) -> crate::Result<Self>;
75
    /// Try to parse a value of this type from a vector of bytes,
76
    /// and consume that value
77
5300
    fn from_vec(v: Vec<u8>, p: crate::Pos) -> crate::Result<Self> {
78
5300
        Self::from_bytes(&v[..], p)
79
5300
    }
80
}
81

            
82
define_derive_deftly_module! {
83
    /// Implement conversion traits for a transparent newtype around bytes - shared code
84
    ///
85
    /// This is precisely `#[derive_deftly(Transparent)]`, but in the form of a deftly module,
86
    /// so that other derives (eg `BytesTransparent`) can re-use it.
87
    Transparent beta_deftly:
88

            
89
    // Expands to bullet points for "generated code", except omitting
90
    // `AsRef` & `AsMut` because some uses sites have additional impls of those,
91
    // which are best presented together in the docs.
92
  ${define TRANSPARENT_DOCS_IMPLS {
93
    ///  * impls of `Deref`, `DerefMut`
94
    ///  * impls of `From<field>` and "`Into`" (technically, `From<Self> for field`)
95
  }}
96

            
97
    // Expands to the implementations
98
  ${define TRANSPARENT_IMPLS {
99

            
100
  ${for fields {
101
    ${loop_exactly_1 "must be applied to a single-field struct"}
102

            
103
    impl<$tgens> From<$ftype> for $ttype {
104
520715
        fn from($fpatname: $ftype) -> $ttype {
105
            $vpat
106
        }
107
    }
108

            
109
    impl<$tgens> From<$ttype> for $ftype {
110
24028
        fn from(self_: $ttype) -> $ftype {
111
            self_.$fname
112
        }
113
    }
114

            
115
    impl<$tgens> Deref for $ttype {
116
        type Target = $ftype;
117
251139724
        fn deref(&self) -> &$ftype {
118
            &self.$fname
119
        }
120
    }
121

            
122
    impl<$tgens> DerefMut for $ttype {
123
        fn deref_mut(&mut self) -> &mut $ftype {
124
            &mut self.$fname
125
        }
126
    }
127

            
128
    impl<$tgens> AsRef<$ftype> for $ttype {
129
        fn as_ref(&self) -> &$ftype {
130
            &self.$fname
131
        }
132
    }
133

            
134
    impl<$tgens> AsMut<$ftype> for $ttype {
135
        fn as_mut(&mut self) -> &mut $ftype {
136
            &mut self.$fname
137
        }
138
    }
139
  }}
140
  }}
141
}
142

            
143
define_derive_deftly! {
144
    use Transparent;
145

            
146
    /// Implement conversion traits for an arbitrary transparent newtype
147
    ///
148
    /// # Requirements
149
    ///
150
    ///  * Self should be a single-field struct
151
    ///  * Self should have no runtime invariants
152
    ///
153
    /// # Generated code
154
    ///
155
    $TRANSPARENT_DOCS_IMPLS
156
    ///  * impls of `AsMut<field>`, `AsRef<field>`
157
    ///
158
    /// # Guidelines
159
    ///
160
    ///  * the field should be `pub`, with `#[allow(clippy::exhaustive_structs)]`
161
    ///  * derive `Hash`, `Debug` and (usually) `Clone`
162
    ///  * consider deriving `PartialEq` and `Eq`
163
    ///    but for types containing bytes, use [`ConstantTimeEq`],
164
    ///    eg with [`#[derive_deftly(BytesTransparent)]`](derive_deftly_template_BytesTransparent)
165
    ///    (instead of `Transparent`).
166
    ///  * implement `FromStr`, `Display`, `NormalItemArgument`, as required
167
    Transparent for struct, beta_deftly:
168

            
169
    $TRANSPARENT_IMPLS
170
}
171

            
172
define_derive_deftly! {
173
    use Transparent;
174

            
175
    /// Implement `ConstantTimeEq`, `.as_bytes()`, etc., for a transparent newtype around bytes
176
    ///
177
    /// # Requirements
178
    ///
179
    ///  * Self should be a single-field struct
180
    ///  * Self should deref to `&[u8]` (and to `&mut [u8]`).
181
    ///  * (so Self should have no runtime invariants)
182
    ///
183
    /// # Generated code
184
    ///
185
    ///  * impls of `ConstantTimeEq`, `Eq`, `PartialEq`
186
    ///  * `as_bytes()` method
187
    ${TRANSPARENT_DOCS_IMPLS}
188
    ///  * impls of `AsMut<field>`, `AsRef<field>`, `AsRef<[u8]>`, `AsMut<[u8]>`
189
    ///
190
    // We could derive Debug here but then we have to deal with the Fixed's N
191
    // which gets quite fiddly.
192
    //
193
    /// # Guidelines
194
    ///
195
    ///  * derive `Hash` and write `#[allow(clippy::derived_hash_with_manual_eq)]`
196
    ///  * impl `FromStr` and `Display` (if required, which they usually will be)
197
    ///  * derive `derive_more::Debug` eg with `#[debug(r#"B64("{self}")"#)]`
198
    ///  * `impl NormalItemArgument` if appropriate (ie the representation has no spaces)
199
    BytesTransparent for struct, beta_deftly:
200

            
201
    $TRANSPARENT_IMPLS
202

            
203
    impl<$tgens> ConstantTimeEq for $ttype {
204
        fn ct_eq(&self, other: &$ttype) -> Choice {
205
          $(
206
            self.$fname.ct_eq(&other.$fname)
207
          )
208
        }
209
    }
210
    $/// `$tname` is `Eq` via its constant-time implementation.
211
    impl<$tgens> PartialEq for $ttype {
212
        fn eq(&self, other: &$ttype) -> bool {
213
            self.ct_eq(other).into()
214
        }
215
    }
216
    impl<$tgens> Eq for $ttype {}
217

            
218
    impl<$tgens> $ttype {
219
        /// Return the byte array from this object.
220
12401
        pub fn as_bytes(&self) -> &[u8] {
221
          $(
222
12401
            &self.$fname[..]
223
          )
224
12401
        }
225
    }
226

            
227
    impl<$tgens> AsRef<[u8]> for $ttype {
228
        fn as_ref(&self) -> &[u8] {
229
          $(
230
            self.$fname.as_ref()
231
          )
232
        }
233
    }
234

            
235
    impl<$tgens> AsMut<[u8]> for $ttype {
236
        fn as_mut(&mut self) -> &mut [u8] {
237
          $(
238
            self.$fname.as_mut()
239
          )
240
        }
241
    }
242
}
243

            
244
/// Types for decoding base64-encoded values.
245
mod b64impl {
246
    use super::*;
247
    use crate::{Error, NetdocErrorKind as EK, Pos, Result};
248
    use base64ct::{Base64, Base64Unpadded, Encoding};
249
    use std::ops::RangeBounds;
250

            
251
    /// A byte array, encoded in base64 with optional padding.
252
    ///
253
    /// On output (`Display`), output is unpadded.
254
    #[derive(Clone, Hash, Deftly)]
255
    #[derive_deftly(BytesTransparent)]
256
    #[allow(clippy::derived_hash_with_manual_eq)]
257
    #[derive(derive_more::Debug)]
258
    #[debug(r#"B64("{self}")"#)]
259
    #[allow(clippy::exhaustive_structs)]
260
    pub struct B64(pub Vec<u8>);
261

            
262
    impl FromStr for B64 {
263
        type Err = Error;
264
74867
        fn from_str(s: &str) -> Result<Self> {
265
74867
            let v: core::result::Result<Vec<u8>, base64ct::Error> = match s.len() % 4 {
266
11206
                0 => Base64::decode_vec(s),
267
63661
                _ => Base64Unpadded::decode_vec(s),
268
            };
269
92874
            let v = v.map_err(|_| {
270
36014
                EK::BadArgument
271
36014
                    .with_msg("Invalid base64")
272
36014
                    .at_pos(Pos::at(s))
273
54021
            })?;
274
38853
            Ok(B64(v))
275
74867
        }
276
    }
277

            
278
    impl Display for B64 {
279
16
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
280
16
            Display::fmt(&Base64Unpadded::encode_string(&self.0), f)
281
16
        }
282
    }
283

            
284
    impl B64 {
285
        /// Return this object if its length is within the provided bounds
286
        /// object, or an error otherwise.
287
2086
        pub(crate) fn check_len<B: RangeBounds<usize>>(self, bounds: B) -> Result<Self> {
288
2086
            if bounds.contains(&self.0.len()) {
289
2084
                Ok(self)
290
            } else {
291
2
                Err(EK::BadObjectVal.with_msg("Invalid length on base64 data"))
292
            }
293
2086
        }
294

            
295
        /// Try to convert this object into an array of N bytes.
296
        ///
297
        /// Return an error if the length is wrong.
298
24276
        pub(crate) fn into_array<const N: usize>(self) -> Result<[u8; N]> {
299
24276
            self.0
300
24276
                .try_into()
301
24276
                .map_err(|_| EK::BadObjectVal.with_msg("Invalid length on base64 data"))
302
24276
        }
303
    }
304

            
305
    impl FromIterator<u8> for B64 {
306
        fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self {
307
            Self(iter.into_iter().collect())
308
        }
309
    }
310

            
311
    impl NormalItemArgument for B64 {}
312

            
313
    /// A byte array encoded in a hexadecimal with a fixed length.
314
    #[derive(Clone, Hash, Deftly)]
315
    #[derive_deftly(BytesTransparent)]
316
    #[allow(clippy::derived_hash_with_manual_eq)]
317
    #[derive(derive_more::Debug)]
318
    #[debug(r#"FixedB64::<{N}>("{self}")"#)]
319
    pub struct FixedB64<const N: usize>(pub [u8; N]);
320

            
321
    impl<const N: usize> Display for FixedB64<N> {
322
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
323
            Display::fmt(&B64(self.0.to_vec()), f)
324
        }
325
    }
326

            
327
    impl<const N: usize> FromStr for FixedB64<N> {
328
        type Err = Error;
329
5442
        fn from_str(s: &str) -> Result<Self> {
330
5442
            Ok(Self(B64::from_str(s)?.0.try_into().map_err(|_| {
331
2
                EK::BadArgument
332
2
                    .at_pos(Pos::at(s))
333
2
                    .with_msg("invalid length")
334
2
            })?))
335
5442
        }
336
    }
337

            
338
    impl<const N: usize> NormalItemArgument for FixedB64<N> {}
339
}
340

            
341
// ============================================================
342

            
343
/// Types for decoding hex-encoded values.
344
mod b16impl {
345
    use super::*;
346
    use crate::{Error, NetdocErrorKind as EK, Pos, Result};
347

            
348
    /// A byte array encoded in hexadecimal; prints in lowercase
349
    ///
350
    /// Both uppercase and lowercase are tolerated when parsing.
351
    #[derive(Clone, Hash, Deftly)]
352
    #[derive_deftly(BytesTransparent)]
353
    #[allow(clippy::derived_hash_with_manual_eq)]
354
    #[derive(derive_more::Debug)]
355
    #[debug(r#"B16("{self}")"#)]
356
    #[allow(clippy::exhaustive_structs)]
357
    pub struct B16(pub Vec<u8>);
358

            
359
    /// A byte array encoded in hexadecimal; prints in uppercase
360
    ///
361
    /// Both uppercase and lowercase are tolerated when parsing.
362
    #[derive(Clone, Hash, Deftly)]
363
    #[derive_deftly(BytesTransparent)]
364
    #[allow(clippy::derived_hash_with_manual_eq)]
365
    #[derive(derive_more::Debug)]
366
    #[debug(r#"B16("{self}")"#)]
367
    #[allow(clippy::exhaustive_structs)]
368
    pub struct B16U(pub Vec<u8>);
369

            
370
    impl FromStr for B16 {
371
        type Err = Error;
372
1173
        fn from_str(s: &str) -> Result<Self> {
373
1175
            let bytes = hex::decode(s).map_err(|_| {
374
4
                EK::BadArgument
375
4
                    .at_pos(Pos::at(s))
376
4
                    .with_msg("invalid hexadecimal")
377
6
            })?;
378
1169
            Ok(B16(bytes))
379
1173
        }
380
    }
381

            
382
    impl FromStr for B16U {
383
        type Err = Error;
384
6
        fn from_str(s: &str) -> Result<Self> {
385
6
            Ok(B16U(B16::from_str(s)?.0))
386
6
        }
387
    }
388

            
389
    impl Display for B16 {
390
6
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
391
            // `hex` has `hex::encode` but that allocates a `String`, which this approach doesn't
392
28
            for c in self.as_bytes() {
393
28
                write!(f, "{c:02x}")?;
394
            }
395
6
            Ok(())
396
6
        }
397
    }
398

            
399
    impl Display for B16U {
400
6
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
401
            // `hex` has `hex::encode_upper` but that allocates a `String`
402
28
            for c in self.as_bytes() {
403
28
                write!(f, "{c:02X}")?;
404
            }
405
6
            Ok(())
406
6
        }
407
    }
408

            
409
    impl NormalItemArgument for B16 {}
410
    impl NormalItemArgument for B16U {}
411
}
412

            
413
// ============================================================
414

            
415
/// Types for decoding curve25519 keys
416
mod curve25519impl {
417
    use super::*;
418

            
419
    use crate::{Error, NormalItemArgument, Result, types::misc::FixedB64};
420
    use tor_llcrypto::pk::curve25519::PublicKey;
421

            
422
    /// A Curve25519 public key, encoded in base64 with optional padding
423
    #[derive(Debug, Clone, PartialEq, Eq, Deftly)]
424
    #[derive_deftly(Transparent)]
425
    #[allow(clippy::exhaustive_structs)]
426
    pub struct Curve25519Public(pub PublicKey);
427

            
428
    impl FromStr for Curve25519Public {
429
        type Err = Error;
430
2720
        fn from_str(s: &str) -> Result<Self> {
431
2720
            let pk: FixedB64<32> = s.parse()?;
432
2710
            let pk: [u8; 32] = pk.into();
433
2710
            Ok(Curve25519Public(pk.into()))
434
2720
        }
435
    }
436

            
437
    impl Display for Curve25519Public {
438
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
439
            FixedB64::from(self.0.to_bytes()).fmt(f)
440
        }
441
    }
442

            
443
    impl NormalItemArgument for Curve25519Public {}
444
}
445

            
446
// ============================================================
447

            
448
/// Types for decoding ed25519 keys
449
mod ed25519impl {
450
    use super::*;
451

            
452
    use crate::{Error, NormalItemArgument, Result, types::misc::FixedB64};
453
    use derive_deftly::Deftly;
454
    use tor_llcrypto::pk::ed25519::Ed25519Identity;
455

            
456
    /// An alleged ed25519 public key, encoded in base64 with optional
457
    /// padding.
458
    #[derive(Debug, Clone, PartialEq, Eq)]
459
    #[allow(clippy::exhaustive_structs)]
460
    pub struct Ed25519Public(pub Ed25519Identity);
461

            
462
    impl FromStr for Ed25519Public {
463
        type Err = Error;
464
2718
        fn from_str(s: &str) -> Result<Self> {
465
2718
            let pk: FixedB64<32> = s.parse()?;
466
2712
            Ok(Ed25519Public(Ed25519Identity::new(pk.into())))
467
2718
        }
468
    }
469

            
470
    impl Display for Ed25519Public {
471
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
472
            let pk: [u8; 32] = self.0.into();
473
            let pk = FixedB64::from(pk);
474
            pk.fmt(f)
475
        }
476
    }
477

            
478
    impl NormalItemArgument for Ed25519Public {}
479

            
480
    impl From<Ed25519Public> for Ed25519Identity {
481
2254
        fn from(pk: Ed25519Public) -> Ed25519Identity {
482
2254
            pk.0
483
2254
        }
484
    }
485

            
486
    /// Helper that checks for the presence of `ed25519`.
487
    #[derive(Debug, Clone, PartialEq, Eq, derive_more::Display, derive_more::FromStr)]
488
    #[display(rename_all = "lowercase")]
489
    #[from_str(rename_all = "lowercase")]
490
    #[allow(clippy::exhaustive_enums)]
491
    pub enum Ed25519AlgorithmString {
492
        /// Ed25519 encoded as `ed25519`.
493
        Ed25519,
494
    }
495

            
496
    impl NormalItemArgument for Ed25519AlgorithmString {}
497

            
498
    /// An Ed25519 public key found in a micro descriptor `id` line.
499
    #[derive(Debug, Clone, PartialEq, Eq, Deftly)]
500
    #[cfg_attr(feature = "parse2", derive_deftly(ItemValueParseable))]
501
    #[non_exhaustive]
502
    pub struct Ed25519IdentityLine {
503
        /// Fixed magic identifier (`ed25519`) for this line.
504
        pub alg: Ed25519AlgorithmString,
505

            
506
        /// The actual Ed25519 identity.
507
        pub pk: Ed25519Public,
508
    }
509

            
510
    impl From<Ed25519Public> for Ed25519IdentityLine {
511
479126
        fn from(pk: Ed25519Public) -> Self {
512
479126
            Self {
513
479126
                alg: Ed25519AlgorithmString::Ed25519,
514
479126
                pk,
515
479126
            }
516
479126
        }
517
    }
518

            
519
    impl From<Ed25519Identity> for Ed25519IdentityLine {
520
479126
        fn from(pk: Ed25519Identity) -> Self {
521
479126
            Ed25519Public(pk).into()
522
479126
        }
523
    }
524
}
525

            
526
// ============================================================
527

            
528
/// Dummy types like [`Ignored`]
529
mod ignored_impl {
530
    use super::*;
531

            
532
    #[cfg(feature = "parse2")]
533
    use crate::parse2::ErrorProblem as EP;
534

            
535
    /// Part of a network document, that isn't actually there.
536
    ///
537
    /// Used as a standin in `ns_type!` calls in various netstatus `each_variety.rs`.
538
    /// The effect is as if the field were omitted from the containing type.
539
    ///
540
    ///  * When used as item(s) (ie, a field type when deriving `NetdocParseable\[Fields\]`):
541
    ///    **ignores any number** of items with that field's keyword during parsing,
542
    ///    and emits none during encoding.
543
    ///
544
    ///    (To *reject* documents containing this item, use `Option<Void>`,
545
    ///    but note that the spec says unknown items should be ignored,
546
    ///    which would normally include items which are merely missing from one variety.)
547
    ///
548
    ///  * When used as an argument (ie, a field type when deriving `ItemValueParseable`,
549
    ///    or with `netdoc(single_arg)`  when deriving `NetdocParseable\[Fields\]`):
550
    ///    consumes **no arguments** during parsing, and emits none during encoding.
551
    ///
552
    ///  * When used as an object field (ie, `netdoc(object)` when deriving `ItemValueParseable`):
553
    ///    **rejects** an object - failing the parse if one is present.
554
    ///    (Functions similarly to `Option<Void>`, but prefer `NotPresent` as it's clearer.)
555
    ///
556
    /// There are bespoke impls of the multiplicity traits
557
    /// `ItemSetMethods` and `ObjectSetMethods`:
558
    /// don't wrap this type in `Option` or `Vec`.
559
    //
560
    // TODO we'll need to implement ItemArgument etc., for encoding, too.
561
    #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Default)]
562
    #[allow(clippy::exhaustive_structs)]
563
    #[cfg_attr(
564
        feature = "parse2",
565
        derive(Deftly),
566
        derive_deftly(NetdocParseableFields)
567
    )]
568
    pub struct NotPresent;
569

            
570
    /// Ignored part of a network document.
571
    ///
572
    /// With `parse2`, can be used as an item, object, or even flattened-fields.
573
    ///
574
    /// When deriving `parse2` traits, and a field is absent in a particular netstatus variety,
575
    /// use `ns_type!` with [`NotPresent`], rather than `Ignored`.
576
    ///
577
    /// During encoding as an Items or Objects, will be entirely omitted,
578
    /// via the multiplicity arrangements.
579
    ///
580
    /// Cannot be encoded as an Argument: if this is not the last
581
    /// Argument, we need something to put into the output document to avoid generating
582
    /// a document with the arguments out of step.  If it *is* the last argument,
583
    /// it could simply be omitted, since additional arguments are in any case ignored.
584
    #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Default)]
585
    #[cfg_attr(
586
        feature = "parse2",
587
        derive(Deftly),
588
        derive_deftly(ItemValueParseable, NetdocParseableFields)
589
    )]
590
    #[allow(clippy::exhaustive_structs)]
591
    pub struct Ignored;
592

            
593
    /// An Item or Object that would be ignored during parsing and is omitted during encoding
594
    ///
595
    /// This is the "single" item type for encoding multiplicity for Items or Objects,
596
    /// for [`Ignored`].
597
    ///
598
    /// This type is uninhabited.
599
    pub struct IgnoredItemOrObjectValue(Void);
600

            
601
    #[cfg(feature = "parse2")]
602
    impl ItemSetMethods for P2MultiplicitySelector<NotPresent> {
603
        type Each = Ignored;
604
        type Field = NotPresent;
605
        fn can_accumulate(self, _acc: &Option<NotPresent>) -> Result<(), EP> {
606
            Ok(())
607
        }
608
12
        fn accumulate(self, _acc: &mut Option<NotPresent>, _item: Ignored) -> Result<(), EP> {
609
12
            Ok(())
610
12
        }
611
458
        fn finish(self, _acc: Option<NotPresent>, _: &'static str) -> Result<NotPresent, EP> {
612
458
            Ok(NotPresent)
613
458
        }
614
    }
615

            
616
    #[cfg(feature = "parse2")]
617
    impl ItemArgumentParseable for NotPresent {
618
14
        fn from_args(_: &mut ArgumentStream) -> Result<NotPresent, ArgumentError> {
619
14
            Ok(NotPresent)
620
14
        }
621
    }
622

            
623
    #[cfg(feature = "parse2")]
624
    impl ObjectSetMethods for P2MultiplicitySelector<NotPresent> {
625
        type Field = NotPresent;
626
        type Each = Void;
627
6
        fn resolve_option(self, _found: Option<Void>) -> Result<NotPresent, EP> {
628
6
            Ok(NotPresent)
629
6
        }
630
    }
631

            
632
    #[cfg(feature = "encode")]
633
    impl<'f> encode::MultiplicityMethods<'f> for EMultiplicitySelector<NotPresent> {
634
        type Field = NotPresent;
635
        type Each = Void;
636
6
        fn iter_ordered(self, _: &'f Self::Field) -> impl Iterator<Item = &'f Self::Each> {
637
6
            iter::empty()
638
6
        }
639
    }
640

            
641
    #[cfg(feature = "encode")]
642
    impl encode::OptionalityMethods for EMultiplicitySelector<NotPresent> {
643
        type Field = NotPresent;
644
        type Each = Void;
645
2
        fn as_option<'f>(self, _: &'f Self::Field) -> Option<&'f Self::Each> {
646
2
            None
647
2
        }
648
    }
649

            
650
    impl FromStr for Ignored {
651
        type Err = Void;
652
        fn from_str(_s: &str) -> Result<Ignored, Void> {
653
            Ok(Ignored)
654
        }
655
    }
656

            
657
    #[cfg(feature = "parse2")]
658
    impl ItemArgumentParseable for Ignored {
659
        fn from_args(_: &mut ArgumentStream) -> Result<Ignored, ArgumentError> {
660
            Ok(Ignored)
661
        }
662
    }
663

            
664
    #[cfg(feature = "parse2")]
665
    impl ItemObjectParseable for Ignored {
666
6
        fn check_label(_label: &str) -> Result<(), EP> {
667
            // allow any label
668
6
            Ok(())
669
6
        }
670
6
        fn from_bytes(_input: &[u8]) -> Result<Self, EP> {
671
6
            Ok(Ignored)
672
6
        }
673
    }
674

            
675
    #[cfg(feature = "parse2")]
676
    impl ObjectSetMethods for P2MultiplicitySelector<Ignored> {
677
        type Field = Ignored;
678
        type Each = Ignored;
679
6
        fn resolve_option(self, _found: Option<Ignored>) -> Result<Ignored, EP> {
680
6
            Ok(Ignored)
681
6
        }
682
    }
683

            
684
    #[cfg(feature = "encode")]
685
    impl<'f> encode::MultiplicityMethods<'f> for EMultiplicitySelector<Ignored> {
686
        type Field = Ignored;
687
        type Each = IgnoredItemOrObjectValue;
688
        fn iter_ordered(self, _: &'f Self::Field) -> impl Iterator<Item = &'f Self::Each> {
689
            iter::empty()
690
        }
691
    }
692

            
693
    #[cfg(feature = "encode")]
694
    impl encode::OptionalityMethods for EMultiplicitySelector<Ignored> {
695
        type Field = Ignored;
696
        type Each = IgnoredItemOrObjectValue;
697
2
        fn as_option<'f>(self, _: &'f Self::Field) -> Option<&'f Self::Each> {
698
2
            None
699
2
        }
700
    }
701

            
702
    #[cfg(feature = "encode")]
703
    impl ItemValueEncodable for IgnoredItemOrObjectValue {
704
        fn write_item_value_onto(&self, _: ItemEncoder) -> Result<(), Bug> {
705
            void::unreachable(self.0)
706
        }
707
    }
708

            
709
    #[cfg(feature = "encode")]
710
    impl ItemObjectEncodable for IgnoredItemOrObjectValue {
711
        fn label(&self) -> &str {
712
            void::unreachable(self.0)
713
        }
714
        fn write_object_onto(&self, _: &mut Vec<u8>) -> Result<(), Bug> {
715
            void::unreachable(self.0)
716
        }
717
    }
718
}
719

            
720
// ============================================================
721

            
722
/// Information about unknown values, which may have been retained as a `T`
723
///
724
/// Won't grow additional variants - but, `Retained` is only included conditionally.
725
///
726
/// Also used in the form `Unknown<()>` to indicate whether unknown values *should* be retained.
727
///
728
/// ### Example
729
///
730
/// ```
731
/// # {
732
/// #![cfg(feature = "retain-unknown")]
733
///
734
/// use tor_netdoc::types::Unknown;
735
///
736
/// let mut unk: Unknown<Vec<String>> = Unknown::new_retained_default();
737
/// unk.with_mut_unknown(|u| u.push("something-we-found".into()));
738
/// assert_eq!(unk.into_retained().unwrap(), ["something-we-found"]);
739
/// # }
740
/// ```
741
///
742
/// ### Equality comparison, semantics
743
///
744
/// Two `Unknown` are consider equal if both have the same record of unknown values,
745
/// or if neither records unknown values at all.
746
///
747
/// `Unknown` is not `Eq` or `Ord` because we won't want to relate a `Discarded`
748
/// to a `Retained`.  That would be a logic error.  `partial_cmp` gives `None` for this.
749
#[derive(Debug, PartialEq, Clone, Copy, Hash)]
750
#[non_exhaustive]
751
pub enum Unknown<T> {
752
    /// The parsing discarded unknown values and they are no longer available.
753
    Discarded(PhantomData<T>),
754

            
755
    /// The document parsing retained (or should retain) unknown values.
756
    #[cfg(feature = "retain-unknown")]
757
    Retained(T),
758
}
759

            
760
impl<T> Unknown<T> {
761
    /// Create an `Unknown` which specifies that values were discarded (or should be)
762
455171
    pub fn new_discard() -> Self {
763
455171
        Unknown::Discarded(PhantomData)
764
455171
    }
765

            
766
    /// Map the `Retained`, if there is one
767
2544
    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Unknown<U> {
768
2544
        self.try_map(move |t| Ok::<_, Void>(f(t))).void_unwrap()
769
2544
    }
770

            
771
    /// Map the `Retained`, fallibly
772
2544
    pub fn try_map<U, E>(self, f: impl FnOnce(T) -> Result<U, E>) -> Result<Unknown<U>, E> {
773
2544
        Ok(match self {
774
2544
            Unknown::Discarded(_) => Unknown::Discarded(PhantomData),
775
            #[cfg(feature = "retain-unknown")]
776
            Unknown::Retained(t) => Unknown::Retained(f(t)?),
777
        })
778
2544
    }
779

            
780
    /// Obtain an `Unknown` containing (maybe) a reference
781
    pub fn as_ref(&self) -> Option<&T> {
782
        match self {
783
            Unknown::Discarded(_) => None,
784
            #[cfg(feature = "retain-unknown")]
785
            Unknown::Retained(t) => Some(t),
786
        }
787
    }
788

            
789
    /// Obtain the `Retained` data
790
    ///
791
    /// Treats lack of retention as an internal error.
792
    #[cfg(feature = "retain-unknown")]
793
    pub fn into_retained(self) -> Result<T, Bug> {
794
        match self {
795
            Unknown::Discarded(_) => Err(internal!("Unknown::retained but data not collected")),
796
            Unknown::Retained(t) => Ok(t),
797
        }
798
    }
799

            
800
    /// Start recording unknown information, with a default value for `T`
801
    #[cfg(feature = "retain-unknown")]
802
    pub fn new_retained_default() -> Self
803
    where
804
        T: Default,
805
    {
806
        Unknown::Retained(T::default())
807
    }
808

            
809
    /// Update the `Retained`, if there is one
810
    ///
811
    /// Intended for use in parsing, when we encounter an unknown value.
812
    ///
813
    /// Not provided in `try_` form.  If you think you need this, instead, unconditionally
814
    /// parse and verify the unknown value, and then conditionally insert it with this function.
815
    /// Don't parse it conditionally - that would skip some validation.
816
2
    pub fn with_mut_unknown(&mut self, f: impl FnOnce(&mut T)) {
817
2
        match self {
818
2
            Unknown::Discarded(_) => {}
819
            #[cfg(feature = "retain-unknown")]
820
            Unknown::Retained(t) => f(t),
821
        }
822
2
    }
823
}
824

            
825
impl<T: PartialOrd> PartialOrd for Unknown<T> {
826
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
827
        use Unknown::*;
828
        match (self, other) {
829
            (Discarded(_), Discarded(_)) => Some(cmp::Ordering::Equal),
830
            #[cfg(feature = "retain-unknown")]
831
            (Discarded(_), Retained(_)) | (Retained(_), Discarded(_)) => None,
832
            #[cfg(feature = "retain-unknown")]
833
            (Retained(a), Retained(b)) => a.partial_cmp(b),
834
        }
835
    }
836
}
837

            
838
// ============================================================
839

            
840
/// Types for decoding times and dates
841
mod timeimpl {
842
    use super::*;
843
    use crate::{Error, NetdocErrorKind as EK, Pos, Result};
844
    use std::time::SystemTime;
845
    use time::{
846
        OffsetDateTime, PrimitiveDateTime, format_description::FormatItem,
847
        macros::format_description,
848
    };
849

            
850
    /// A wall-clock time, encoded in Iso8601 format with an intervening
851
    /// space between the date and time.
852
    ///
853
    /// (Example: "2020-10-09 17:38:12")
854
    #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Deftly)]
855
    #[derive_deftly(Transparent)]
856
    #[allow(clippy::exhaustive_structs)]
857
    pub struct Iso8601TimeSp(pub SystemTime);
858

            
859
    /// Formatting object for parsing the space-separated Iso8601 format.
860
    const ISO_8601SP_FMT: &[FormatItem] =
861
        format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
862

            
863
    impl FromStr for Iso8601TimeSp {
864
        type Err = Error;
865
8201
        fn from_str(s: &str) -> Result<Iso8601TimeSp> {
866
8205
            let d = PrimitiveDateTime::parse(s, &ISO_8601SP_FMT).map_err(|e| {
867
8
                EK::BadArgument
868
8
                    .at_pos(Pos::at(s))
869
8
                    .with_msg(format!("invalid time: {}", e))
870
12
            })?;
871
8193
            Ok(Iso8601TimeSp(d.assume_utc().into()))
872
8201
        }
873
    }
874

            
875
    /// Formats a SystemTime according to the given format description
876
    ///
877
    /// Also converts any time::error::format to fmt::Error
878
    /// so that it can be unwrapped in the Display trait impl
879
18
    fn fmt_with(
880
18
        t: SystemTime,
881
18
        format_desc: &[FormatItem],
882
18
    ) -> core::result::Result<String, fmt::Error> {
883
18
        OffsetDateTime::from(t)
884
18
            .format(format_desc)
885
18
            .map_err(|_| fmt::Error)
886
18
    }
887

            
888
    impl Display for Iso8601TimeSp {
889
12
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
890
12
            write!(f, "{}", fmt_with(self.0, ISO_8601SP_FMT)?)
891
12
        }
892
    }
893

            
894
    /// A wall-clock time, encoded in ISO8601 format without an intervening
895
    /// space.
896
    ///
897
    /// This represents a specific UTC instant (ie an instant in global civil time).
898
    /// But it may not be able to represent leap seconds.
899
    ///
900
    /// The timezone is not included in the string representation; `+0000` is implicit.
901
    ///
902
    /// (Example: "2020-10-09T17:38:12")
903
    #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Deftly)]
904
    #[derive_deftly(Transparent)]
905
    #[allow(clippy::exhaustive_structs)]
906
    pub struct Iso8601TimeNoSp(pub SystemTime);
907

            
908
    /// Formatting object for parsing the space-separated Iso8601 format.
909
    const ISO_8601NOSP_FMT: &[FormatItem] =
910
        format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
911

            
912
    impl FromStr for Iso8601TimeNoSp {
913
        type Err = Error;
914
24
        fn from_str(s: &str) -> Result<Iso8601TimeNoSp> {
915
28
            let d = PrimitiveDateTime::parse(s, &ISO_8601NOSP_FMT).map_err(|e| {
916
8
                EK::BadArgument
917
8
                    .at_pos(Pos::at(s))
918
8
                    .with_msg(format!("invalid time: {}", e))
919
12
            })?;
920
16
            Ok(Iso8601TimeNoSp(d.assume_utc().into()))
921
24
        }
922
    }
923

            
924
    impl Display for Iso8601TimeNoSp {
925
6
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
926
6
            write!(f, "{}", fmt_with(self.0, ISO_8601NOSP_FMT)?)
927
6
        }
928
    }
929

            
930
    impl crate::NormalItemArgument for Iso8601TimeNoSp {}
931
}
932

            
933
/// Types for decoding RSA keys
934
mod rsa {
935
    use super::*;
936
    use crate::{NetdocErrorKind as EK, Pos, Result};
937
    use std::ops::RangeBounds;
938
    use tor_llcrypto::pk::rsa::PublicKey;
939
    #[cfg(feature = "encode")]
940
    use tor_llcrypto::{d::Sha1, pk::rsa::KeyPair};
941

            
942
    /// The fixed exponent which we require when parsing any RSA key in a netdoc
943
    //
944
    // TODO this value is duplicated a lot in the v1 parser
945
    pub(crate) const RSA_FIXED_EXPONENT: u32 = 65537;
946

            
947
    /// The fixed exponent which we require when parsing any RSA key in a netdoc
948
    //
949
    // TODO this value is duplicated a lot in the v1 parser
950
    pub(crate) const RSA_MIN_BITS: usize = 1024;
951

            
952
    /// RSA public key, partially processed by `crate::paarse`.
953
    ///
954
    /// As parsed from a base64-encoded object.
955
    /// They key's properties (exponent and size) haven't been checked.
956
    #[allow(non_camel_case_types)]
957
    #[derive(Clone, Debug)]
958
    pub(crate) struct RsaPublicParse1Helper(PublicKey, Pos);
959

            
960
    /// RSA signature using SHA-1 as per "Signing documents" in dir-spec
961
    ///
962
    /// <https://spec.torproject.org/dir-spec/netdoc.html#signing>
963
    ///
964
    /// Used for
965
    /// [`AuthCert::dir-key-certification`](crate::doc::authcert::AuthCert::dir-key-certification),
966
    /// for example.
967
    ///
968
    /// # Caveats
969
    ///
970
    /// This type MUST NOT be used for anomalous signatures
971
    /// such as
972
    /// [`AuthCert::dir_key_crosscert`](crate::doc::authcert::AuthCert::dir_key_crosscert);
973
    /// in that case because `dir_key_crosscert`'s
974
    /// set of allowed object labels includes `ID SIGNATURE` whereas this type
975
    /// is always `SIGNATURE`
976
    #[derive(Debug, Clone, PartialEq, Eq, Deftly)]
977
    #[cfg_attr(
978
        feature = "parse2",
979
        derive_deftly(ItemValueParseable),
980
        deftly(netdoc(no_extra_args, signature(hash_accu = Sha1WholeKeywordLine)))
981
    )]
982
    #[cfg_attr(feature = "encode", derive_deftly(ItemValueEncodable))]
983
    // derive_deftly_adhoc disables unused deftly attribute checking, so we needn't cfg_attr them all
984
    #[cfg_attr(not(any(feature = "parse2", feature = "encode")), derive_deftly_adhoc)]
985
    #[non_exhaustive]
986
    pub struct RsaSha1Signature {
987
        /// The bytes of the signature (base64-decoded).
988
        #[deftly(netdoc(object(label = "SIGNATURE"), with = crate::types::raw_data_object))]
989
        pub signature: Vec<u8>,
990
    }
991

            
992
    impl From<RsaPublicParse1Helper> for PublicKey {
993
5298
        fn from(k: RsaPublicParse1Helper) -> PublicKey {
994
5298
            k.0
995
5298
        }
996
    }
997
    impl super::FromBytes for RsaPublicParse1Helper {
998
5300
        fn from_bytes(b: &[u8], pos: Pos) -> Result<Self> {
999
5300
            let key = PublicKey::from_der(b)
5301
                .ok_or_else(|| EK::BadObjectVal.with_msg("unable to decode RSA public key"))?;
5298
            Ok(RsaPublicParse1Helper(key, pos))
5300
        }
    }
    impl RsaPublicParse1Helper {
        /// Give an error if the exponent of this key is not 'e'
5300
        pub(crate) fn check_exponent(self, e: u32) -> Result<Self> {
5300
            if self.0.exponent_is(e) {
5298
                Ok(self)
            } else {
2
                Err(EK::BadObjectVal
2
                    .at_pos(self.1)
2
                    .with_msg("invalid RSA exponent"))
            }
5300
        }
        /// Give an error if the length of this key's modulus, in
        /// bits, is not contained in 'bounds'
5304
        pub(crate) fn check_len<B: RangeBounds<usize>>(self, bounds: B) -> Result<Self> {
5304
            if bounds.contains(&self.0.bits()) {
5300
                Ok(self)
            } else {
4
                Err(EK::BadObjectVal
4
                    .at_pos(self.1)
4
                    .with_msg("invalid RSA length"))
            }
5304
        }
        /// Give an error if the length of this key's modulus, in
        /// bits, is not exactly `n`.
4928
        pub(crate) fn check_len_eq(self, n: usize) -> Result<Self> {
4928
            self.check_len(n..=n)
4928
        }
    }
    #[cfg(feature = "encode")]
    impl RsaSha1Signature {
        /// Make a signature according to "Signing documents" in the netdoc spec
        ///
        /// <https://spec.torproject.org/dir-spec/netdoc.html#signing>
        ///
        /// `NetdocEncoder` should have had the body of the document
        /// (everything except the signatures) already encoded.
        ///
        /// `item_keyword` is the keyword for the signature item.
        /// This is needed because different documents use different keywords,
        /// and the keyword is covered by the signature (an annoying is a layering violation).
        /// See <https://gitlab.torproject.org/tpo/core/torspec/-/issues/322>.
        ///
        /// # Example
        ///
        /// ```
        /// use derive_deftly::Deftly;
        /// use tor_error::Bug;
        /// use tor_llcrypto::pk::rsa;
        /// use tor_netdoc::derive_deftly_template_NetdocEncodable;
        /// use tor_netdoc::encode::{NetdocEncodable, NetdocEncoder};
        /// use tor_netdoc::types::RsaSha1Signature;
        ///
        /// #[derive(Deftly, Default)]
        /// #[derive_deftly(NetdocEncodable)]
        /// pub struct Document {
        ///     pub document_intro_keyword: (),
        /// }
        /// #[derive(Deftly)]
        /// #[derive_deftly(NetdocEncodable)]
        /// pub struct DocumentSignatures {
        ///     pub document_signature: RsaSha1Signature,
        /// }
        /// impl Document {
        ///     pub fn encode_sign(&self, k: &rsa::KeyPair) -> Result<String, Bug> {
        ///         let mut encoder = NetdocEncoder::new();
        ///         self.encode_unsigned(&mut encoder)?;
        ///         let document_signature =
        ///             RsaSha1Signature::new_sign_netdoc(k, &encoder, "document-signature")?;
        ///         let sigs = DocumentSignatures { document_signature };
        ///         sigs.encode_unsigned(&mut encoder)?;
        ///         let encoded = encoder.finish()?;
        ///         Ok(encoded)
        ///     }
        /// }
        ///
        /// # fn main() -> Result<(), anyhow::Error> {
        /// let k = rsa::KeyPair::generate(&mut tor_basic_utils::test_rng::testing_rng())?;
        /// let doc = Document::default();
        /// let encoded = doc.encode_sign(&k)?;
        /// assert!(encoded.starts_with(concat!(
        ///     "document-intro-keyword\n",
        ///     "document-signature\n",
        ///     "-----BEGIN SIGNATURE-----\n",
        /// )));
        /// # Ok(())
        /// # }
        /// ```
2
        pub fn new_sign_netdoc(
2
            private_key: &KeyPair,
2
            encoder: &NetdocEncoder,
2
            item_keyword: &str,
2
        ) -> StdResult<Self, Bug> {
2
            let mut h = Sha1::new();
2
            h.update(encoder.text_sofar()?);
2
            h.update(item_keyword);
2
            h.update("\n");
2
            let h = h.finalize();
2
            let signature = private_key
2
                .sign(&h)
2
                .map_err(into_internal!("RSA signing failed"))?;
2
            Ok(RsaSha1Signature { signature })
2
        }
    }
}
/// Types for decoding Ed25519 certificates
#[cfg(any(feature = "routerdesc", feature = "hs-common"))]
mod edcert {
    use crate::{NetdocErrorKind as EK, Pos, Result};
    use tor_cert::{CertType, Ed25519Cert, KeyUnknownCert};
    #[cfg(feature = "routerdesc")]
    use tor_llcrypto::pk::ed25519;
    /// An ed25519 certificate as parsed from a directory object, with
    /// signature not validated.
    #[derive(Debug, Clone)]
    pub(crate) struct UnvalidatedEdCert(KeyUnknownCert, Pos);
    impl super::FromBytes for UnvalidatedEdCert {
7371
        fn from_bytes(b: &[u8], p: Pos) -> Result<Self> {
7372
            let cert = Ed25519Cert::decode(b).map_err(|e| {
2
                EK::BadObjectVal
2
                    .at_pos(p)
2
                    .with_msg("Bad certificate")
2
                    .with_source(e)
3
            })?;
7369
            Ok(Self(cert, p))
7371
        }
7371
        fn from_vec(v: Vec<u8>, p: Pos) -> Result<Self> {
7371
            Self::from_bytes(&v[..], p)
7371
        }
    }
    impl UnvalidatedEdCert {
        /// Give an error if this certificate's type is not `desired_type`.
7371
        pub(crate) fn check_cert_type(self, desired_type: CertType) -> Result<Self> {
7371
            if self.0.peek_cert_type() != desired_type {
2
                return Err(EK::BadObjectVal.at_pos(self.1).with_msg(format!(
2
                    "bad certificate type {} (wanted {})",
2
                    self.0.peek_cert_type(),
2
                    desired_type
2
                )));
7369
            }
7369
            Ok(self)
7371
        }
        /// Give an error if this certificate's subject_key is not `pk`
        #[cfg(feature = "routerdesc")]
2248
        pub(crate) fn check_subject_key_is(self, pk: &ed25519::Ed25519Identity) -> Result<Self> {
2248
            if self.0.peek_subject_key().as_ed25519() != Some(pk) {
2
                return Err(EK::BadObjectVal
2
                    .at_pos(self.1)
2
                    .with_msg("incorrect subject key"));
2246
            }
2246
            Ok(self)
2248
        }
        /// Consume this object and return the inner Ed25519 certificate.
7367
        pub(crate) fn into_unchecked(self) -> KeyUnknownCert {
7367
            self.0
7367
        }
    }
}
/// Digest identifiers, and digests in the form `ALGORITHM=BASE64U`
///
/// As found in a vote's `m` line.
// TODO Use FixedB64 here.
mod identified_digest {
    use super::*;
    define_derive_deftly! {
        /// impl `FromStr` and `Display` for an enum with unit variants but also "unknown"
        ///
        /// Expected input: an enum whose variants are either
        ///  * unit variants, perhaps with `#[deftly(string_repr = "string")]`
        ///  * singleton tuple variant, containing `String` (or near equivalent)
        ///
        /// If `#[deftly(string_repro)]` is not specified,
        /// the default is snake case of the variant name.
        //
        // This macro may seem overkill, but open-coding these impls gives opportunities
        // for mismatches between FromStr, Display, and the variant name.
        //
        // TODO consider putting this in tor-basic-utils (maybe with a better name),
        // or possibly asking if derive_more want their FromStr to have this.
        StringReprUnitsOrUnknown for enum, expect items, beta_deftly:
        ${define STRING_REPR {
            ${vmeta(string_repr)
              as str,
              default { ${concat ${snake_case $vname}} }
            }
        }}
        impl FromStr for $ttype {
            type Err = Void;
14
            fn from_str(s: &str) -> Result<Self, Void> {
                $(
                    ${when v_is_unit}
                    if s == $STRING_REPR {
                        return Ok($vtype)
                    }
                )
                $(
                    ${when not(v_is_unit)} // anything else had better be Unknown
                    // not using `return ..;` makes this a syntax error if there are several.
                    Ok($vtype { 0: s.into() })
                )
            }
        }
        impl Display for $ttype {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                let s: &str = match self {
                    $(
                        ${when v_is_unit}
                        $vtype => $STRING_REPR,
                    )
                    $(
                        ${when not(v_is_unit)}
                        $vpat => f_0,
                    )
                };
                Display::fmt(s, f)
            }
        }
    }
    /// The name of a digest algorithm.
    ///
    /// Can represent an unrecognised algorithm, so it's parsed and reproduced.
    #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deftly)]
    #[derive_deftly(StringReprUnitsOrUnknown)]
    #[non_exhaustive]
    pub enum DigestName {
        /// SHA-256
        Sha256,
        /// Unknown
        Unknown(String),
    }
    /// A single digest made with a nominated digest algorithm, `ALGORITHM=DIGEST`
    #[derive(Debug, Clone, Eq, PartialEq, Hash, derive_more::Display)]
    #[display("{alg}={value}")]
    #[non_exhaustive]
    pub struct IdentifiedDigest {
        /// The algorithm name.
        alg: DigestName,
        /// The digest value.
        ///
        /// Invariant: length is correct for `alg`, assuming `alg` is known.
        value: B64,
    }
    impl NormalItemArgument for DigestName {}
    impl NormalItemArgument for IdentifiedDigest {}
    /// Invalid syntax parsing an `IdentifiedDigest`
    #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, thiserror::Error)]
    #[error("invalid syntax, expected ALGORITHM=DIGEST: {0}")]
    pub struct IdentifiedDigestParseError(String);
    impl FromStr for IdentifiedDigest {
        type Err = IdentifiedDigestParseError;
14
        fn from_str(s: &str) -> Result<Self, Self::Err> {
14
            (|| {
14
                let (alg, value) = s.split_once('=').ok_or("missing equals sign")?;
14
                let alg = alg.parse().void_unwrap();
14
                let value = value
14
                    .parse::<B64>()
14
                    .map_err(|e| format!("bad value: {}", e.report()))?;
14
                if let Some(exp_len) = (|| {
                    Some({
                        use DigestName::*;
14
                        match alg {
14
                            Sha256 => 32,
                            Unknown(_) => None?,
                        }
                    })
                })() {
14
                    let val_len = value.as_bytes().len();
14
                    if val_len != exp_len {
                        return Err(format!("got {val_len} bytes, expected {exp_len}"));
14
                    }
                }
14
                Ok(IdentifiedDigest { alg, value })
            })()
14
            .map_err(IdentifiedDigestParseError)
14
        }
    }
}
/// Types for decoding RSA fingerprints
mod fingerprint {
    use super::*;
    use crate::{Error, NetdocErrorKind as EK, Pos, Result};
    use base64ct::{Base64Unpadded, Encoding as _};
    use tor_llcrypto::pk::rsa::RsaIdentity;
    /// A hex-encoded RSA key identity (fingerprint) with spaces in it.
    ///
    /// Netdoc parsing adapter for [`RsaIdentity`]
    #[derive(Debug, Clone, Eq, PartialEq, Hash, Deftly)]
    #[derive_deftly(Transparent)]
    #[allow(clippy::exhaustive_structs)]
    pub(crate) struct SpFingerprint(pub RsaIdentity);
    /// A hex-encoded fingerprint with no spaces.
    ///
    /// Netdoc parsing adapter for [`RsaIdentity`]
    #[derive(Debug, Clone, Eq, PartialEq, Hash, Deftly)]
    #[derive_deftly(Transparent)]
    #[allow(clippy::exhaustive_structs)]
    pub struct Fingerprint(pub RsaIdentity);
    /// A base64-encoded fingerprint (unpadded)
    ///
    /// Netdoc parsing adapter for [`RsaIdentity`]
    #[derive(Debug, Clone, Eq, PartialEq, Hash, Deftly)]
    #[derive_deftly(Transparent)]
    #[allow(clippy::exhaustive_structs)]
    pub struct Base64Fingerprint(pub RsaIdentity);
    /// A "long identity" in the format used for Family members.
    ///
    /// Netdoc parsing adapter for [`RsaIdentity`]
    #[derive(Debug, Clone, Eq, PartialEq, Hash, Deftly)]
    #[derive_deftly(Transparent)]
    #[allow(clippy::exhaustive_structs)]
    pub(crate) struct LongIdent(pub RsaIdentity);
    /// Helper: parse an identity from a hexadecimal string
813391
    fn parse_hex_ident(s: &str) -> Result<RsaIdentity> {
813444
        RsaIdentity::from_hex(s).ok_or_else(|| {
2146
            EK::BadArgument
2146
                .at_pos(Pos::at(s))
2146
                .with_msg("wrong length on fingerprint")
2146
        })
813391
    }
    impl FromStr for SpFingerprint {
        type Err = Error;
2248
        fn from_str(s: &str) -> Result<SpFingerprint> {
2250
            let ident = parse_hex_ident(&s.replace(' ', "")).map_err(|e| e.at_pos(Pos::at(s)))?;
2244
            Ok(SpFingerprint(ident))
2248
        }
    }
    impl FromStr for Base64Fingerprint {
        type Err = Error;
2546
        fn from_str(s: &str) -> Result<Base64Fingerprint> {
2546
            let b = s.parse::<super::B64>()?;
2546
            let ident = RsaIdentity::from_bytes(b.as_bytes()).ok_or_else(|| {
                EK::BadArgument
                    .at_pos(Pos::at(s))
                    .with_msg("Wrong identity length")
            })?;
2546
            Ok(Base64Fingerprint(ident))
2546
        }
    }
    impl Display for Base64Fingerprint {
2
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2
            Display::fmt(&Base64Unpadded::encode_string(self.as_bytes()), f)
2
        }
    }
    impl FromStr for Fingerprint {
        type Err = Error;
10311
        fn from_str(s: &str) -> Result<Fingerprint> {
10316
            let ident = parse_hex_ident(s).map_err(|e| e.at_pos(Pos::at(s)))?;
10301
            Ok(Fingerprint(ident))
10311
        }
    }
    impl Display for Fingerprint {
4
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4
            Display::fmt(&hex::encode_upper(self.as_bytes()), f)
4
        }
    }
    impl FromStr for LongIdent {
        type Err = Error;
800832
        fn from_str(mut s: &str) -> Result<LongIdent> {
800832
            if s.starts_with('$') {
406
                s = &s[1..];
800600
            }
800832
            if let Some(idx) = s.find(['=', '~']) {
6
                s = &s[..idx];
800826
            }
800832
            let ident = parse_hex_ident(s)?;
798700
            Ok(LongIdent(ident))
800832
        }
    }
    impl Display for LongIdent {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "${}", self.0.as_hex_upper())
        }
    }
    impl crate::NormalItemArgument for Fingerprint {}
    impl crate::NormalItemArgument for Base64Fingerprint {}
    impl crate::NormalItemArgument for LongIdent {}
}
/// A type for relay nicknames
mod nickname {
    use super::*;
    use crate::{Error, NetdocErrorKind as EK, Pos, Result};
    use tinystr::TinyAsciiStr;
    /// This is a strange limit, but it comes from Tor.
    const MAX_NICKNAME_LEN: usize = 19;
    /// The nickname for a Tor relay.
    ///
    /// These nicknames are legacy mechanism that's occasionally useful in
    /// debugging. They should *never* be used to uniquely identify relays;
    /// nothing prevents two relays from having the same nickname.
    ///
    /// Nicknames are required to be ASCII, alphanumeric, and between 1 and 19
    /// characters inclusive.
    #[derive(Clone, Debug)]
    pub struct Nickname(tinystr::TinyAsciiStr<MAX_NICKNAME_LEN>);
    impl Nickname {
        /// Return a view of this nickname as a string slice.
8
        pub(crate) fn as_str(&self) -> &str {
8
            self.0.as_str()
8
        }
    }
    impl Display for Nickname {
2
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2
            self.as_str().fmt(f)
2
        }
    }
    impl FromStr for Nickname {
        type Err = Error;
459191
        fn from_str(s: &str) -> Result<Self> {
459193
            let tiny = TinyAsciiStr::from_str(s).map_err(|_| {
4
                EK::BadArgument
4
                    .at_pos(Pos::at(s))
4
                    .with_msg("Invalid nickname")
6
            })?;
459187
            if tiny.is_ascii_alphanumeric() && !tiny.is_empty() {
459183
                Ok(Nickname(tiny))
            } else {
4
                Err(EK::BadArgument
4
                    .at_pos(Pos::at(s))
4
                    .with_msg("Invalid nickname"))
            }
459191
        }
    }
    impl crate::NormalItemArgument for Nickname {}
}
/// Contact information of the relay operator.
mod contact_info {
    use super::*;
    /// `contact` item: contact information (eg of a relay dirauth operator)
    ///
    /// <https://spec.torproject.org/dir-spec/server-descriptor-format.html#item:contact>
    ///
    /// Also used for authority entries in netstatus documents.
    #[derive(Clone, Debug, PartialEq, Eq, Deftly)] //
    #[derive(derive_more::Into, derive_more::AsRef, derive_more::Deref, derive_more::Display)]
    #[cfg_attr(feature = "encode", derive_deftly(ItemValueEncodable))]
    #[non_exhaustive]
    pub struct ContactInfo(#[cfg_attr(feature = "encode", deftly(netdoc(rest)))] String);
    /// Contact information (`contact` item value) has invalid syntax
    #[derive(Clone, Debug, thiserror::Error)]
    #[error("contact information (`contact` item value) has invalid syntax")]
    #[non_exhaustive]
    pub struct InvalidContactInfo {}
    impl FromStr for ContactInfo {
        type Err = InvalidContactInfo;
18
        fn from_str(s: &str) -> Result<Self, InvalidContactInfo> {
            // TODO torspec#396 we should probably impose more restrictions
            // For now we forbid `\n` and initial whitespace, which is enough to ensure
            // that all values will roundtrip unchanged through netdoc encoding and parsing.
18
            if s.contains('\n') || s.starts_with(char::is_whitespace) {
4
                Err(InvalidContactInfo {})
            } else {
14
                Ok(ContactInfo(s.to_owned()))
            }
18
        }
    }
    #[cfg(feature = "parse2")]
    impl ItemValueParseable for ContactInfo {
6
        fn from_unparsed(mut item: UnparsedItem<'_>) -> Result<Self, parse2::ErrorProblem> {
6
            item.check_no_object()?;
6
            item.args_mut()
6
                .into_remaining()
6
                .parse()
6
                .map_err(|_e| item.args().handle_error("info", ArgumentError::Invalid))
6
        }
    }
}
#[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)]
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
    use itertools::Itertools;
    use base64ct::Encoding;
    use super::*;
    use crate::{Pos, Result};
    /// Decode s as a multi-line base64 string, ignoring ascii whitespace.
    fn base64_decode_ignore_ws(s: &str) -> std::result::Result<Vec<u8>, base64ct::Error> {
        let mut s = s.to_string();
        s.retain(|c| !c.is_ascii_whitespace());
        base64ct::Base64::decode_vec(s.as_str())
    }
    #[test]
    fn base64() -> Result<()> {
        // Test parsing success:
        // Unpadded:
        assert_eq!("Mi43MTgyOA".parse::<B64>()?.as_bytes(), &b"2.71828"[..]);
        assert!("Mi43MTgyOA".parse::<B64>()?.check_len(7..8).is_ok());
        assert_eq!("Mg".parse::<B64>()?.as_bytes(), &b"2"[..]);
        assert!("Mg".parse::<B64>()?.check_len(1..2).is_ok());
        assert_eq!(
            "8J+NkvCfjZLwn42S8J+NkvCfjZLwn42S"
                .parse::<B64>()?
                .as_bytes(),
            "🍒🍒🍒🍒🍒🍒".as_bytes()
        );
        assert!(
            "8J+NkvCfjZLwn42S8J+NkvCfjZLwn42S"
                .parse::<B64>()?
                .check_len(24..25)
                .is_ok()
        );
        assert!(
            "ppwthHXW8kXD0f9fE7UPYsOAAu4uj5ORwSomCMxKkz8="
                .parse::<B64>()?
                .check_len(32..33)
                .is_ok()
        );
        // Padded:
        assert_eq!("Mi43MTgyOA==".parse::<B64>()?.as_bytes(), &b"2.71828"[..]);
        assert!("Mi43MTgyOA==".parse::<B64>()?.check_len(7..8).is_ok());
        assert_eq!("Mg==".parse::<B64>()?.as_bytes(), &b"2"[..]);
        assert!("Mg==".parse::<B64>()?.check_len(1..2).is_ok());
        // Test parsing failures:
        // Invalid character.
        assert!("Mi43!!!!!!".parse::<B64>().is_err());
        // Invalid last character.
        assert!("Mi".parse::<B64>().is_err());
        assert!(
            "ppwthHXW8kXD0f9fE7UPYsOAAu4uj5ORwSomCMxaaaa"
                .parse::<B64>()
                .is_err()
        );
        // Invalid length.
        assert!("Mi43MTgyOA".parse::<B64>()?.check_len(8..).is_err());
        Ok(())
    }
    #[test]
    fn base64_lengths() -> Result<()> {
        assert_eq!("".parse::<B64>()?.as_bytes(), b"");
        assert!("=".parse::<B64>().is_err());
        assert!("==".parse::<B64>().is_err());
        assert!("B".parse::<B64>().is_err());
        assert!("B=".parse::<B64>().is_err());
        assert!("B==".parse::<B64>().is_err());
        assert!("Bg=".parse::<B64>().is_err());
        assert_eq!("Bg".parse::<B64>()?.as_bytes(), b"\x06");
        assert_eq!("Bg==".parse::<B64>()?.as_bytes(), b"\x06");
        assert_eq!("BCg".parse::<B64>()?.as_bytes(), b"\x04\x28");
        assert_eq!("BCg=".parse::<B64>()?.as_bytes(), b"\x04\x28");
        assert!("BCg==".parse::<B64>().is_err());
        assert_eq!("BCDE".parse::<B64>()?.as_bytes(), b"\x04\x20\xc4");
        assert!("BCDE=".parse::<B64>().is_err());
        assert!("BCDE==".parse::<B64>().is_err());
        Ok(())
    }
    #[test]
    fn base64_rev() {
        use base64ct::{Base64, Base64Unpadded};
        // Check that strings that we accept are precisely ones which
        // can be generated by either Base64 or Base64Unpadded
        for n in 0..=5 {
            for c_vec in std::iter::repeat_n("ACEQg/=".chars(), n).multi_cartesian_product() {
                let s: String = c_vec.into_iter().collect();
                #[allow(clippy::print_stderr)]
                let b = match s.parse::<B64>() {
                    Ok(b) => {
                        eprintln!("{:10} {:?}", &s, b.as_bytes());
                        b
                    }
                    Err(_) => {
                        eprintln!("{:10} Err", &s);
                        continue;
                    }
                };
                let b = b.as_bytes();
                let ep = Base64::encode_string(b);
                let eu = Base64Unpadded::encode_string(b);
                assert!(
                    s == ep || s == eu,
                    "{:?} decoded to {:?} giving neither {:?} nor {:?}",
                    s,
                    b,
                    ep,
                    eu
                );
            }
        }
    }
    #[test]
    fn base16() -> anyhow::Result<()> {
        let chk = |s: &str, b: &[u8]| -> anyhow::Result<()> {
            let parsed = s.parse::<B16>()?;
            assert_eq!(parsed.as_bytes(), b, "{s:?}");
            assert_eq!(parsed.to_string(), s.to_ascii_lowercase());
            let parsed = s.parse::<B16U>()?;
            assert_eq!(parsed.as_bytes(), b, "{s:?}");
            assert_eq!(parsed.to_string(), s.to_ascii_uppercase());
            Ok(())
        };
        chk("332e313432", b"3.142")?;
        chk("332E313432", b"3.142")?;
        chk("332E3134", b"3.14")?;
        assert!("332E313".parse::<B16>().is_err());
        assert!("332G3134".parse::<B16>().is_err());
        Ok(())
    }
    #[test]
    fn curve25519() -> Result<()> {
        use tor_llcrypto::pk::curve25519::PublicKey;
        let k1 = "ppwthHXW8kXD0f9fE7UPYsOAAu4uj5ORwSomCMxKkz8=";
        let k2 = hex::decode("a69c2d8475d6f245c3d1ff5f13b50f62c38002ee2e8f9391c12a2608cc4a933f")
            .unwrap();
        let k2: &[u8; 32] = &k2[..].try_into().unwrap();
        let k1: PublicKey = k1.parse::<Curve25519Public>()?.into();
        assert_eq!(k1, (*k2).into());
        assert!(
            "ppwthHXW8kXD0f9fE7UPYsOAAu4uj5ORwSomCMxKkz"
                .parse::<Curve25519Public>()
                .is_err()
        );
        assert!(
            "ppwthHXW8kXD0f9fE7UPYsOAAu4uj5ORSomCMxKkz"
                .parse::<Curve25519Public>()
                .is_err()
        );
        assert!(
            "ppwthHXW8kXD0f9fE7UPYsOAAu4uj5wSomCMxKkz"
                .parse::<Curve25519Public>()
                .is_err()
        );
        assert!(
            "ppwthHXW8kXD0f9fE7UPYsOAAu4ORwSomCMxKkz"
                .parse::<Curve25519Public>()
                .is_err()
        );
        Ok(())
    }
    #[test]
    fn ed25519() -> Result<()> {
        use tor_llcrypto::pk::ed25519::Ed25519Identity;
        let k1 = "WVIPQ8oArAqLY4XzkcpIOI6U8KsUJHBQhG8SC57qru0";
        let k2 = hex::decode("59520f43ca00ac0a8b6385f391ca48388e94f0ab14247050846f120b9eeaaeed")
            .unwrap();
        let k1: Ed25519Identity = k1.parse::<Ed25519Public>()?.into();
        assert_eq!(k1, Ed25519Identity::from_bytes(&k2).unwrap());
        assert!(
            "WVIPQ8oArAqLY4Xzk0!!!!8KsUJHBQhG8SC57qru"
                .parse::<Ed25519Public>()
                .is_err()
        );
        assert!(
            "WVIPQ8oArAqLY4XzkcpIU8KsUJHBQhG8SC57qru"
                .parse::<Ed25519Public>()
                .is_err()
        );
        assert!(
            "WVIPQ8oArAqLY4XzkcpIU8KsUJHBQhG8SC57qr"
                .parse::<Ed25519Public>()
                .is_err()
        );
        // right length, bad key:
        assert!(
            "ppwthHXW8kXD0f9fE7UPYsOAAu4uj5ORwSomCMxaaaa"
                .parse::<Curve25519Public>()
                .is_err()
        );
        Ok(())
    }
    #[test]
    fn time() -> Result<()> {
        use humantime::parse_rfc3339;
        use std::time::SystemTime;
        let t = "2020-09-29 13:36:33".parse::<Iso8601TimeSp>()?;
        let t: SystemTime = t.into();
        assert_eq!(t, parse_rfc3339("2020-09-29T13:36:33Z").unwrap());
        assert!("2020-FF-29 13:36:33".parse::<Iso8601TimeSp>().is_err());
        assert!("2020-09-29Q13:99:33".parse::<Iso8601TimeSp>().is_err());
        assert!("2020-09-29".parse::<Iso8601TimeSp>().is_err());
        assert!("too bad, waluigi time".parse::<Iso8601TimeSp>().is_err());
        assert_eq!(
            "2020-09-29 13:36:33",
            "2020-09-29 13:36:33".parse::<Iso8601TimeSp>()?.to_string()
        );
        let t = "2020-09-29T13:36:33".parse::<Iso8601TimeNoSp>()?;
        let t: SystemTime = t.into();
        assert_eq!(t, parse_rfc3339("2020-09-29T13:36:33Z").unwrap());
        assert!("2020-09-29 13:36:33".parse::<Iso8601TimeNoSp>().is_err());
        assert!("2020-09-29Q13:99:33".parse::<Iso8601TimeNoSp>().is_err());
        assert!("2020-09-29".parse::<Iso8601TimeNoSp>().is_err());
        assert!("too bad, waluigi time".parse::<Iso8601TimeNoSp>().is_err());
        assert_eq!(
            "2020-09-29T13:36:33",
            "2020-09-29T13:36:33"
                .parse::<Iso8601TimeNoSp>()?
                .to_string()
        );
        Ok(())
    }
    #[test]
    fn rsa_public_key() {
        // Taken from a chutney network.
        let key_b64 = r#"
        MIIBigKCAYEAsDkzTcKS4kAF56R2ijb9qCek53tKC1EwMdpWMk58bB28fY6kHc55
        E7n1hB+LC5neZlx88GKuZ9k8P3g0MlO5ejalcfBdIIm28Nz86JXf/L23YnEpxnG/
        IpxZEcmx/EYN+vwp72W3DGuzyntaoaut6lGJk+O/aRCLLcTm4MNznvN1ackK2H6b
        Xm2ejRwtVRLoPKODJiPGl43snCfXXWsMH3IALFOgm0szPLv2fAJzBI8VWrUN81M/
        lgwJhG6+xbr1CkrXI5fKs/TNr0B0ydC9BIZplmPrnXaeNklnw1cqUJ1oxDSgBrvx
        rpDo7paObjSPV26opa68QKGa7Gu2MZQC3RzViNCbawka/108g6hSUkoM+Om2oivr
        DvtMOs10MjsfibEBVnwEhqnlb/gj3hJkYoGRsCwAyMIaMObHcmAevMJRWAjGCc8T
        GMS9dSmg1IZst+U+V2OCcIHXT6wZ1zPsBM0pYKVLCwtewaq1306k0n+ekriEo7eI
        FS3Dd/Dx/a6jAgMBAAE=
        "#;
        let key_bytes = base64_decode_ignore_ws(key_b64).unwrap();
        let rsa = RsaPublicParse1Helper::from_vec(key_bytes, Pos::None).unwrap();
        let bits = tor_llcrypto::pk::rsa::PublicKey::from(rsa.clone()).bits();
        assert_eq!(bits, 3072);
        // tests on a valid key
        assert!(rsa.clone().check_exponent(65537).is_ok());
        assert!(rsa.clone().check_exponent(1337).is_err());
        assert!(rsa.clone().check_len_eq(3072).is_ok());
        assert!(rsa.clone().check_len(1024..=4096).is_ok());
        assert!(rsa.clone().check_len(1024..=1024).is_err());
        assert!(rsa.check_len(4096..).is_err());
        // A string of bytes that is not an RSA key.
        let failure = RsaPublicParse1Helper::from_vec(vec![1, 2, 3], Pos::None);
        assert!(failure.is_err());
    }
    #[cfg(feature = "routerdesc")]
    #[test]
    fn ed_cert() {
        use tor_llcrypto::pk::ed25519::Ed25519Identity;
        // From a chutney network.
        let cert_b64 = r#"
        AQQABwRNAR6m3kq5h8i3wwac+Ti293opoOP8RKGP9MT0WD4Bbz7YAQAgBACGCdys
        G7AwsoYMIKenDN6In6ReiGF8jaYoGqmWKDVBdGGMDIZyNIq+VdhgtAB1EyNFHJU1
        jGM0ir9dackL+PIsHbzJH8s/P/8RfUsKIL6/ZHbn3nKMxLH/8kjtxp5ScAA=
        "#;
        let cert_bytes = base64_decode_ignore_ws(cert_b64).unwrap();
        // From the cert above.
        let right_subject_key: Ed25519Identity = "HqbeSrmHyLfDBpz5OLb3eimg4/xEoY/0xPRYPgFvPtg"
            .parse::<Ed25519Public>()
            .unwrap()
            .into();
        // From `ed25519()` test above.
        let wrong_subject_key: Ed25519Identity = "WVIPQ8oArAqLY4XzkcpIOI6U8KsUJHBQhG8SC57qru0"
            .parse::<Ed25519Public>()
            .unwrap()
            .into();
        // decode and check correct type and key
        let cert = UnvalidatedEdCert::from_vec(cert_bytes, Pos::None)
            .unwrap()
            .check_cert_type(tor_cert::CertType::IDENTITY_V_SIGNING)
            .unwrap()
            .check_subject_key_is(&right_subject_key)
            .unwrap();
        // check wrong type.
        assert!(
            cert.clone()
                .check_cert_type(tor_cert::CertType::RSA_ID_X509)
                .is_err()
        );
        // check wrong key.
        assert!(cert.check_subject_key_is(&wrong_subject_key).is_err());
        // Try an invalid object that isn't a certificate.
        let failure = UnvalidatedEdCert::from_vec(vec![1, 2, 3], Pos::None);
        assert!(failure.is_err());
    }
    #[test]
    fn fingerprint() -> Result<()> {
        use tor_llcrypto::pk::rsa::RsaIdentity;
        let fp1 = "7467 A97D 19CD 2B4F 2BC0 388A A99C 5E67 710F 847E";
        let fp2 = "7467A97D19CD2B4F2BC0388AA99C5E67710F847E";
        let fp3 = "$7467A97D19CD2B4F2BC0388AA99C5E67710F847E";
        let fp4 = "$7467A97D19CD2B4F2BC0388AA99C5E67710F847E=fred";
        let k = hex::decode(fp2).unwrap();
        let k = RsaIdentity::from_bytes(&k[..]).unwrap();
        assert_eq!(RsaIdentity::from(fp1.parse::<SpFingerprint>()?), k);
        assert_eq!(RsaIdentity::from(fp2.parse::<SpFingerprint>()?), k);
        assert!(fp3.parse::<SpFingerprint>().is_err());
        assert!(fp4.parse::<SpFingerprint>().is_err());
        assert!(fp1.parse::<Fingerprint>().is_err());
        assert_eq!(RsaIdentity::from(fp2.parse::<Fingerprint>()?), k);
        assert!(fp3.parse::<Fingerprint>().is_err());
        assert!(fp4.parse::<Fingerprint>().is_err());
        assert_eq!(Fingerprint(k).to_string(), fp2);
        assert!(fp1.parse::<LongIdent>().is_err());
        assert_eq!(RsaIdentity::from(fp2.parse::<LongIdent>()?), k);
        assert_eq!(RsaIdentity::from(fp3.parse::<LongIdent>()?), k);
        assert_eq!(RsaIdentity::from(fp4.parse::<LongIdent>()?), k);
        assert!("xxxx".parse::<Fingerprint>().is_err());
        assert!("ffffffffff".parse::<Fingerprint>().is_err());
        let fp_b64 = "dGepfRnNK08rwDiKqZxeZ3EPhH4";
        assert_eq!(RsaIdentity::from(fp_b64.parse::<Base64Fingerprint>()?), k);
        assert_eq!(Base64Fingerprint(k).to_string(), fp_b64);
        Ok(())
    }
    #[test]
    fn nickname() -> Result<()> {
        let n: Nickname = "Foo".parse()?;
        assert_eq!(n.as_str(), "Foo");
        assert_eq!(n.to_string(), "Foo");
        let word = "Untr1gonometr1cally";
        assert_eq!(word.len(), 19);
        let long: Nickname = word.parse()?;
        assert_eq!(long.as_str(), word);
        let too_long = "abcdefghijklmnopqrstuvwxyz";
        let not_ascii = "Eyjafjallajökull";
        let too_short = "";
        let other_invalid = "contains space";
        assert!(not_ascii.len() <= 19);
        assert!(too_long.parse::<Nickname>().is_err());
        assert!(not_ascii.parse::<Nickname>().is_err());
        assert!(too_short.parse::<Nickname>().is_err());
        assert!(other_invalid.parse::<Nickname>().is_err());
        Ok(())
    }
    #[test]
    fn contact_info() -> anyhow::Result<()> {
        const S: &str = "some relay operator";
        let n: ContactInfo = S.parse()?;
        assert_eq!(n.as_str(), S);
        assert_eq!(n.to_string(), S);
        let bad = |s: &str| {
            let _: InvalidContactInfo = s.parse::<ContactInfo>().unwrap_err();
        };
        bad(" starts with space");
        bad("contains\nnewline");
        #[cfg(all(feature = "encode", feature = "parse2"))]
        {
            use encode::NetdocEncodable;
            use parse2::{ParseInput, parse_netdoc};
            #[derive(PartialEq, Debug, Deftly)]
            #[derive_deftly(NetdocParseable, NetdocEncodable)]
            struct TestDoc {
                pub intro: (),
                pub contact: ContactInfo,
            }
            let roundtrip = |s: &str| -> anyhow::Result<()> {
                let doc = TestDoc {
                    intro: (),
                    contact: s.parse()?,
                };
                let mut enc = NetdocEncoder::new();
                doc.encode_unsigned(&mut enc)?;
                let enc = enc.finish()?;
                let reparsed = parse_netdoc::<TestDoc>(&ParseInput::new(&enc, "<test>"))?;
                assert_eq!(doc, reparsed);
                Ok(())
            };
            roundtrip("normal")?;
            roundtrip("trailing  white space  ")?;
            roundtrip("wtf is this allowed in \x03 netdocs\r")?; // TODO torspec#396
        }
        Ok(())
    }
}