1
//! Parsing implementation for Tor authority certificates
2
//!
3
//! An "authority certificate" is a short signed document that binds a
4
//! directory authority's permanent "identity key" to its medium-term
5
//! "signing key".  Using separate keys here enables the authorities
6
//! to keep their identity keys securely offline, while using the
7
//! signing keys to sign votes and consensuses.
8

            
9
use crate::batching_split_before::IteratorExt as _;
10
use crate::encode::{
11
    Bug, ItemArgument, ItemEncoder, ItemObjectEncodable, NetdocEncodable, NetdocEncoder,
12
};
13
use crate::parse::keyword::Keyword;
14
use crate::parse::parser::{Section, SectionRules};
15
use crate::parse::tokenize::{ItemResult, NetDocReader};
16
use crate::parse2::{
17
    self, ArgumentError, ArgumentStream, ItemArgumentParseable, ItemObjectParseable,
18
    NetdocParseableUnverified as _, sig_hashes::Sha1WholeKeywordLine,
19
};
20
use crate::types::misc::{Fingerprint, Iso8601TimeSp, RsaPublicParse1Helper, RsaSha1Signature};
21
use crate::util::str::Extent;
22
use crate::{NetdocErrorKind as EK, NormalItemArgument, Result};
23

            
24
use tor_basic_utils::impl_debug_hex;
25
use tor_checkable::{
26
    Timebound, signed,
27
    timed::{self, TimerangeBound},
28
};
29
use tor_error::into_internal;
30
use tor_llcrypto::pk::rsa;
31
use tor_llcrypto::{d, pk, pk::rsa::RsaIdentity};
32

            
33
use std::sync::LazyLock;
34

            
35
use std::result::Result as StdResult;
36
use std::{net, time, time::SystemTime};
37

            
38
use derive_deftly::Deftly;
39
use digest::Digest;
40

            
41
#[cfg(feature = "build_docs")]
42
mod build;
43

            
44
#[cfg(feature = "build_docs")]
45
#[allow(deprecated)]
46
pub use build::AuthCertBuilder;
47

            
48
#[cfg(feature = "incomplete")]
49
mod encoded;
50
#[cfg(feature = "incomplete")]
51
pub use encoded::EncodedAuthCert;
52

            
53
decl_keyword! {
54
    pub(crate) AuthCertKwd {
55
        "dir-key-certificate-version" => DIR_KEY_CERTIFICATE_VERSION,
56
        "dir-address" => DIR_ADDRESS,
57
        "fingerprint" => FINGERPRINT,
58
        "dir-identity-key" => DIR_IDENTITY_KEY,
59
        "dir-key-published" => DIR_KEY_PUBLISHED,
60
        "dir-key-expires" => DIR_KEY_EXPIRES,
61
        "dir-signing-key" => DIR_SIGNING_KEY,
62
        "dir-key-crosscert" => DIR_KEY_CROSSCERT,
63
        "dir-key-certification" => DIR_KEY_CERTIFICATION,
64
    }
65
}
66

            
67
/// Rules about entries that must appear in an AuthCert, and how they must
68
/// be formed.
69
56
static AUTHCERT_RULES: LazyLock<SectionRules<AuthCertKwd>> = LazyLock::new(|| {
70
    use AuthCertKwd::*;
71

            
72
56
    let mut rules = SectionRules::builder();
73
56
    rules.add(DIR_KEY_CERTIFICATE_VERSION.rule().required().args(1..));
74
56
    rules.add(DIR_ADDRESS.rule().args(1..));
75
56
    rules.add(FINGERPRINT.rule().required().args(1..));
76
56
    rules.add(DIR_IDENTITY_KEY.rule().required().no_args().obj_required());
77
56
    rules.add(DIR_SIGNING_KEY.rule().required().no_args().obj_required());
78
56
    rules.add(DIR_KEY_PUBLISHED.rule().required());
79
56
    rules.add(DIR_KEY_EXPIRES.rule().required());
80
56
    rules.add(DIR_KEY_CROSSCERT.rule().required().no_args().obj_required());
81
56
    rules.add(UNRECOGNIZED.rule().may_repeat().obj_optional());
82
56
    rules.add(
83
56
        DIR_KEY_CERTIFICATION
84
56
            .rule()
85
56
            .required()
86
56
            .no_args()
87
56
            .obj_required(),
88
    );
89
56
    rules.build()
90
56
});
91

            
92
/// A single directory authority key certificate
93
///
94
/// This is the body, not including signatures.
95
///
96
/// <https://spec.torproject.org/dir-spec/creating-key-certificates.html>
97
///
98
/// To make a fresh `AuthCert`, use [`AuthCertConstructor`].
99
#[derive(Clone, Debug, Deftly)]
100
#[derive_deftly(Constructor)]
101
#[derive_deftly(NetdocParseableUnverified, NetdocEncodable)]
102
#[cfg_attr(test, derive(PartialEq, Eq))]
103
#[allow(clippy::exhaustive_structs)]
104
pub struct AuthCert {
105
    /// Intro line
106
    ///
107
    /// Currently must be version 3.
108
    ///
109
    /// <https://spec.torproject.org/dir-spec/creating-key-certificates.html#item:dir-key-certificate-version>
110
    #[deftly(constructor(default = AuthCertVersion::V3))]
111
    #[deftly(netdoc(single_arg))]
112
    pub dir_key_certificate_version: AuthCertVersion,
113

            
114
    /// An IPv4 address for this authority.
115
    #[deftly(netdoc(single_arg))]
116
    pub dir_address: Option<net::SocketAddrV4>,
117

            
118
    /// H(KP_auth_id_rsa)
119
    ///
120
    /// <https://spec.torproject.org/dir-spec/creating-key-certificates.html#item:fingerprint>
121
    #[deftly(constructor)]
122
    #[deftly(netdoc(single_arg))]
123
    pub fingerprint: Fingerprint,
124

            
125
    /// Declared time when this certificate was published
126
    ///
127
    /// <https://spec.torproject.org/dir-spec/creating-key-certificates.html#item:dir-key-published>
128
    #[deftly(constructor)]
129
    #[deftly(netdoc(single_arg))]
130
    pub dir_key_published: Iso8601TimeSp,
131

            
132
    /// Declared time when this certificate expires.
133
    ///
134
    /// <https://spec.torproject.org/dir-spec/creating-key-certificates.html#item:dir-key-expires>
135
    #[deftly(constructor)]
136
    #[deftly(netdoc(single_arg))]
137
    pub dir_key_expires: Iso8601TimeSp,
138

            
139
    /// KP_auth_id_rsa
140
    ///
141
    /// The long-term RSA identity key for this authority
142
    ///
143
    /// <https://spec.torproject.org/dir-spec/creating-key-certificates.html#item:dir-identity-key>
144
    #[deftly(constructor)]
145
    pub dir_identity_key: rsa::PublicKey,
146

            
147
    /// KP_auth_sign_rsa
148
    ///
149
    /// The medium-term RSA signing key for this authority
150
    ///
151
    /// <https://spec.torproject.org/dir-spec/creating-key-certificates.html#item:dir-signing-key>
152
    #[deftly(constructor)]
153
    pub dir_signing_key: rsa::PublicKey,
154

            
155
    /// SHA1(DER(KP_auth_id_rsa)) signed by KP_auth_sign_rsa
156
    ///
157
    /// <https://spec.torproject.org/dir-spec/creating-key-certificates.html#item:dir-key-crosscert>
158
    #[deftly(constructor)]
159
    pub dir_key_crosscert: CrossCert,
160

            
161
    #[doc(hidden)]
162
    #[deftly(netdoc(skip))]
163
    pub __non_exhaustive: (),
164
}
165

            
166
/// Represents the version of an [`AuthCert`].
167
///
168
/// Single argument.
169
///
170
/// <https://spec.torproject.org/dir-spec/creating-key-certificates.html#item:dir-key-certificate-version>
171
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, strum::EnumString, strum::Display)]
172
#[non_exhaustive]
173
pub enum AuthCertVersion {
174
    /// The current and only version understood.
175
    #[strum(serialize = "3")]
176
    V3,
177
}
178

            
179
impl NormalItemArgument for AuthCertVersion {}
180

            
181
/// A pair of key identities that identifies a certificate.
182
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
183
#[allow(clippy::exhaustive_structs)]
184
pub struct AuthCertKeyIds {
185
    /// Fingerprint of identity key
186
    pub id_fingerprint: rsa::RsaIdentity,
187
    /// Fingerprint of signing key
188
    pub sk_fingerprint: rsa::RsaIdentity,
189
}
190

            
191
/// An authority certificate whose signature and validity time we
192
/// haven't checked.
193
pub struct UncheckedAuthCert {
194
    /// Where we found this AuthCert within the string containing it.
195
    location: Option<Extent>,
196

            
197
    /// The actual unchecked certificate.
198
    c: signed::SignatureGated<timed::TimerangeBound<AuthCert>>,
199
}
200

            
201
impl UncheckedAuthCert {
202
    /// If this AuthCert was originally parsed from `haystack`, return its
203
    /// text.
204
    ///
205
    /// TODO: This is a pretty bogus interface; there should be a
206
    /// better way to remember where to look for this thing if we want
207
    /// it without keeping the input alive forever.  We should
208
    /// refactor.
209
162
    pub fn within<'a>(&self, haystack: &'a str) -> Option<&'a str> {
210
162
        self.location
211
162
            .as_ref()
212
165
            .and_then(|ext| ext.reconstruct(haystack))
213
162
    }
214
}
215

            
216
impl AuthCert {
217
    /// Make an [`AuthCertBuilder`] object that can be used to
218
    /// construct authority certificates for testing.
219
    #[cfg(feature = "build_docs")]
220
    #[deprecated = "use AuthCertConstructor instead"]
221
    #[allow(deprecated)]
222
10
    pub fn builder() -> AuthCertBuilder {
223
10
        AuthCertBuilder::new()
224
10
    }
225

            
226
    /// Parse an authority certificate from a string.
227
    ///
228
    /// This function verifies the certificate's signatures, but doesn't
229
    /// check its expiration dates.
230
68
    pub fn parse(s: &str) -> Result<UncheckedAuthCert> {
231
68
        let mut reader = NetDocReader::new(s)?;
232
68
        let body = AUTHCERT_RULES.parse(&mut reader)?;
233
68
        reader.should_be_exhausted()?;
234
73
        AuthCert::from_body(&body, s).map_err(|e| e.within(s))
235
68
    }
236

            
237
    /// Return an iterator yielding authority certificates from a string.
238
116
    pub fn parse_multiple(s: &str) -> Result<impl Iterator<Item = Result<UncheckedAuthCert>> + '_> {
239
        use AuthCertKwd::*;
240
116
        let sections = NetDocReader::new(s)?
241
324
            .batching_split_before_loose(|item| item.is_ok_with_kwd(DIR_KEY_CERTIFICATE_VERSION));
242
116
        Ok(sections
243
136
            .map(|mut section| {
244
26
                let body = AUTHCERT_RULES.parse(&mut section)?;
245
24
                AuthCert::from_body(&body, s)
246
26
            })
247
136
            .map(|r| r.map_err(|e| e.within(s))))
248
116
    }
249
    /*
250
        /// Return true if this certificate is expired at a given time, or
251
        /// not yet valid at that time.
252
        pub fn is_expired_at(&self, when: time::SystemTime) -> bool {
253
            when < self.published || when > self.expires
254
        }
255
    */
256
    /// Return the signing key certified by this certificate.
257
140
    pub fn signing_key(&self) -> &rsa::PublicKey {
258
140
        &self.dir_signing_key
259
140
    }
260

            
261
    /// Return an AuthCertKeyIds object describing the keys in this
262
    /// certificate.
263
1128
    pub fn key_ids(&self) -> AuthCertKeyIds {
264
1128
        AuthCertKeyIds {
265
1128
            id_fingerprint: self.fingerprint.0,
266
1128
            sk_fingerprint: self.dir_signing_key.to_rsa_identity(),
267
1128
        }
268
1128
    }
269

            
270
    /// Return an RsaIdentity for this certificate's identity key.
271
16
    pub fn id_fingerprint(&self) -> &rsa::RsaIdentity {
272
16
        &self.fingerprint
273
16
    }
274

            
275
    /// Return the time when this certificate says it was published.
276
56
    pub fn published(&self) -> time::SystemTime {
277
56
        *self.dir_key_published
278
56
    }
279

            
280
    /// Return the time when this certificate says it should expire.
281
56
    pub fn expires(&self) -> time::SystemTime {
282
56
        *self.dir_key_expires
283
56
    }
284

            
285
    /// Parse an authority certificate from a reader.
286
    #[allow(clippy::string_slice)] // TODO
287
196
    fn from_body(body: &Section<'_, AuthCertKwd>, s: &str) -> Result<UncheckedAuthCert> {
288
        use AuthCertKwd::*;
289

            
290
        // Make sure first and last element are correct types.  We can
291
        // safely call unwrap() on first and last, since there are required
292
        // tokens in the rules, so we know that at least one token will have
293
        // been parsed.
294
194
        let start_pos = {
295
            // Unwrap should be safe because `.parse()` would have already
296
            // returned an Error
297
            #[allow(clippy::unwrap_used)]
298
196
            let first_item = body.first_item().unwrap();
299
196
            if first_item.kwd() != DIR_KEY_CERTIFICATE_VERSION {
300
2
                return Err(EK::WrongStartingToken
301
2
                    .with_msg(first_item.kwd_str().to_string())
302
2
                    .at_pos(first_item.pos()));
303
194
            }
304
194
            first_item.pos()
305
        };
306
192
        let end_pos = {
307
            // Unwrap should be safe because `.parse()` would have already
308
            // returned an Error
309
            #[allow(clippy::unwrap_used)]
310
194
            let last_item = body.last_item().unwrap();
311
194
            if last_item.kwd() != DIR_KEY_CERTIFICATION {
312
2
                return Err(EK::WrongEndingToken
313
2
                    .with_msg(last_item.kwd_str().to_string())
314
2
                    .at_pos(last_item.pos()));
315
192
            }
316
192
            last_item.end_pos()
317
        };
318

            
319
192
        let version = body
320
192
            .required(DIR_KEY_CERTIFICATE_VERSION)?
321
192
            .parse_arg::<u32>(0)?;
322
192
        if version != 3 {
323
4
            return Err(EK::BadDocumentVersion.with_msg(format!("unexpected version {}", version)));
324
188
        }
325
188
        let dir_key_certificate_version = AuthCertVersion::V3;
326

            
327
188
        let dir_signing_key: rsa::PublicKey = body
328
188
            .required(DIR_SIGNING_KEY)?
329
188
            .parse_obj::<RsaPublicParse1Helper>("RSA PUBLIC KEY")?
330
188
            .check_len(1024..)?
331
188
            .check_exponent(65537)?
332
188
            .into();
333

            
334
188
        let dir_identity_key: rsa::PublicKey = body
335
188
            .required(DIR_IDENTITY_KEY)?
336
188
            .parse_obj::<RsaPublicParse1Helper>("RSA PUBLIC KEY")?
337
188
            .check_len(1024..)?
338
188
            .check_exponent(65537)?
339
188
            .into();
340

            
341
188
        let dir_key_published = body
342
188
            .required(DIR_KEY_PUBLISHED)?
343
188
            .args_as_str()
344
188
            .parse::<Iso8601TimeSp>()?;
345

            
346
188
        let dir_key_expires = body
347
188
            .required(DIR_KEY_EXPIRES)?
348
188
            .args_as_str()
349
188
            .parse::<Iso8601TimeSp>()?;
350

            
351
        {
352
            // Check fingerprint for consistency with key.
353
188
            let fp_tok = body.required(FINGERPRINT)?;
354
188
            let fingerprint: RsaIdentity = fp_tok.args_as_str().parse::<Fingerprint>()?.into();
355
188
            if fingerprint != dir_identity_key.to_rsa_identity() {
356
2
                return Err(EK::BadArgument
357
2
                    .at_pos(fp_tok.pos())
358
2
                    .with_msg("fingerprint does not match RSA identity"));
359
186
            }
360
        }
361

            
362
186
        let dir_address = body
363
186
            .maybe(DIR_ADDRESS)
364
186
            .parse_args_as_str::<net::SocketAddrV4>()?;
365

            
366
        // check crosscert
367
        let dir_key_crosscert;
368
184
        let v_crosscert = {
369
186
            let crosscert = body.required(DIR_KEY_CROSSCERT)?;
370
            // Unwrap should be safe because `.parse()` and `required()` would
371
            // have already returned an Error
372
            #[allow(clippy::unwrap_used)]
373
186
            let mut tag = crosscert.obj_tag().unwrap();
374
            // we are required to support both.
375
186
            if tag != "ID SIGNATURE" && tag != "SIGNATURE" {
376
2
                tag = "ID SIGNATURE";
377
184
            }
378
186
            let sig = crosscert.obj(tag)?;
379

            
380
184
            let signed = dir_identity_key.to_rsa_identity();
381
            // TODO: we need to accept prefixes here. COMPAT BLOCKER.
382

            
383
184
            let v = rsa::ValidatableRsaSignature::new(&dir_signing_key, &sig, signed.as_bytes());
384

            
385
184
            dir_key_crosscert = CrossCert {
386
184
                signature: CrossCertObject(sig),
387
184
            };
388

            
389
184
            v
390
        };
391

            
392
        // check the signature
393
184
        let v_sig = {
394
184
            let signature = body.required(DIR_KEY_CERTIFICATION)?;
395
184
            let sig = signature.obj("SIGNATURE")?;
396

            
397
184
            let mut sha1 = d::Sha1::new();
398
            // Unwrap should be safe because `.parse()` would have already
399
            // returned an Error
400
            #[allow(clippy::unwrap_used)]
401
184
            let start_offset = body.first_item().unwrap().offset_in(s).unwrap();
402
            #[allow(clippy::unwrap_used)]
403
184
            let end_offset = body.last_item().unwrap().offset_in(s).unwrap();
404
184
            let end_offset = end_offset + "dir-key-certification\n".len();
405
184
            sha1.update(&s[start_offset..end_offset]);
406
184
            let sha1 = sha1.finalize();
407
            // TODO: we need to accept prefixes here. COMPAT BLOCKER.
408

            
409
184
            rsa::ValidatableRsaSignature::new(&dir_identity_key, &sig, &sha1)
410
        };
411

            
412
184
        let id_fingerprint = dir_identity_key.to_rsa_identity();
413

            
414
184
        let location = {
415
184
            let start_idx = start_pos.offset_within(s);
416
184
            let end_idx = end_pos.offset_within(s);
417
184
            match (start_idx, end_idx) {
418
184
                (Some(a), Some(b)) => Extent::new(s, &s[a..b + 1]),
419
                _ => None,
420
            }
421
        };
422

            
423
184
        let authcert = AuthCert {
424
184
            dir_key_certificate_version,
425
184
            dir_address,
426
184
            dir_identity_key,
427
184
            dir_signing_key,
428
184
            dir_key_published,
429
184
            dir_key_expires,
430
184
            dir_key_crosscert,
431
184
            fingerprint: Fingerprint(id_fingerprint),
432
184
            __non_exhaustive: (),
433
184
        };
434

            
435
184
        let signatures: Vec<Box<dyn pk::ValidatableSignature>> =
436
184
            vec![Box::new(v_crosscert), Box::new(v_sig)];
437

            
438
184
        let timed = timed::TimerangeBound::new(authcert, *dir_key_published..*dir_key_expires);
439
184
        let signed = signed::SignatureGated::new(timed, signatures);
440
184
        let unchecked = UncheckedAuthCert {
441
184
            location,
442
184
            c: signed,
443
184
        };
444
184
        Ok(unchecked)
445
196
    }
446
}
447

            
448
/// Parsing/encoding module for `AuthCertKeyIds` as found in `directory-signature`
449
///
450
/// Use with `#[deftly(netdoc(with = ...))]` when deriving
451
/// `ItemValueParseable` and `ItemValueEncodable`.
452
///
453
/// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:directory-signature>
454
//
455
// Currently the only use site is `netstatus::Signature`.
456
// If we find this is being used in many places, and is therefore a standard thing,
457
// we should arrange for the derives to be able to derive from an argument collection,
458
// and use that.
459
pub(crate) mod keyids_directory_signature_args {
460
    use super::*;
461
    use std::result::Result;
462

            
463
    /// Parse
464
1964
    pub(crate) fn from_args<'s>(
465
1964
        args: &mut ArgumentStream<'s>,
466
1964
    ) -> Result<AuthCertKeyIds, ArgumentError> {
467
3974
        let mut fp = || Ok::<_, ArgumentError>(Fingerprint::from_args(args)?.0);
468
        Ok(AuthCertKeyIds {
469
1964
            id_fingerprint: fp()?,
470
1964
            sk_fingerprint: fp()?,
471
        })
472
1964
    }
473

            
474
    /// Encode
475
18
    pub(crate) fn write_arg_onto(
476
18
        self_: &AuthCertKeyIds,
477
18
        out: &mut ItemEncoder<'_>,
478
18
    ) -> Result<(), Bug> {
479
45
        let mut fp = |id| Fingerprint(id).write_arg_onto(out);
480
18
        fp(self_.id_fingerprint)?;
481
18
        fp(self_.sk_fingerprint)?;
482
18
        Ok(())
483
18
    }
484
}
485

            
486
/// Pseudo-Signature of the long-term identity key by the medium-term key.
487
///
488
/// This type does not implement `SignatureItemParseable` because that trait
489
/// is reserved for signatures on *netdocs*, such as [`AuthCertSignature`].
490
/// As `CrossCert` does not sign a full document, it implements only
491
/// `ItemValueParseable`, instead.
492
///
493
/// Verification of this signature is done in `AuthCertUnverified::verify`,
494
/// and during parsing by the old parser.
495
/// So a `CrossCert` in [`AuthCert::dir_key_crosscert`] in a bare `AuthCert` has been validated.
496
//
497
// TODO SPEC (Diziet): it is far from clear to me that this cert serves any useful purpose.
498
// However, we are far too busy now with rewriting the universe to consider transitioning it away.
499
#[derive(Debug, Clone, PartialEq, Eq, Deftly)]
500
#[derive_deftly(ItemValueParseable, ItemValueEncodable)]
501
#[deftly(netdoc(no_extra_args))]
502
#[non_exhaustive]
503
pub struct CrossCert {
504
    /// The bytes of the signature (base64-decoded).
505
    #[deftly(netdoc(object))]
506
    pub signature: CrossCertObject,
507
}
508

            
509
/// Wrapper around [`Vec<u8>`] implementing [`ItemObjectParseable`] properly.
510
///
511
/// Unfortunately, this wrapper is necessary, because the specification
512
/// demands that these certificate objects must accept two labels:
513
/// `SIGNATURE` and `ID SIGNATURE`.  Because the deftly template for
514
/// `ItemValueParseable` only allows for a single label
515
/// (`#[deftly(netdoc(object(label = "LABEL")))]`), we must implement this
516
/// trait ourselves in order to allow multiple ones.
517
///
518
/// TODO: In the future, it might be nice to let the respective fmeta
519
/// accept a pattern, as pattern matching would allow trivially for one
520
/// to infinity different combinations.
521
/// TODO SPEC: Alternatively we could abolish the wrong labels,
522
/// or we could abolish Objects completely and just have long lines.
523
///
524
/// # Specifications
525
///
526
/// <https://spec.torproject.org/dir-spec/creating-key-certificates.html#item:dir-key-crosscert>
527
#[derive(Clone, PartialEq, Eq, derive_more::Deref)]
528
#[non_exhaustive]
529
pub struct CrossCertObject(pub Vec<u8>);
530
impl_debug_hex! { CrossCertObject . 0 }
531

            
532
impl CrossCert {
533
    /// Make a `CrossCert`
534
2
    pub fn new(
535
2
        k_auth_sign_rsa: &rsa::KeyPair,
536
2
        h_kp_auth_id_rsa: &RsaIdentity,
537
2
    ) -> StdResult<Self, Bug> {
538
2
        let signature = k_auth_sign_rsa
539
2
            .sign(h_kp_auth_id_rsa.as_bytes())
540
2
            .map_err(into_internal!("failed to sign cross-cert"))?;
541
2
        Ok(CrossCert {
542
2
            signature: CrossCertObject(signature),
543
2
        })
544
2
    }
545
}
546

            
547
/// Signatures for [`AuthCert`]
548
///
549
/// Signed by [`AuthCert::dir_identity_key`] in order to prove ownership.
550
/// Can be seen as the opposite of [`AuthCert::dir_key_crosscert`].
551
///
552
/// # Specifications
553
///
554
/// * <https://spec.torproject.org/dir-spec/creating-key-certificates.html#item:dir-key-certification>
555
/// * <https://spec.torproject.org/dir-spec/netdoc.html#signing>
556
#[derive(Debug, Clone, PartialEq, Eq, Deftly)]
557
#[derive_deftly(NetdocParseableSignatures, NetdocEncodable)]
558
#[deftly(netdoc(signatures(hashes_accu = Sha1WholeKeywordLine)))]
559
#[non_exhaustive]
560
pub struct AuthCertSignatures {
561
    /// Contains the actual signature, see [`AuthCertSignatures`].
562
    pub dir_key_certification: RsaSha1Signature,
563
}
564

            
565
/// RSA signature for data in [`AuthCert`]
566
///
567
/// <https://spec.torproject.org/dir-spec/netdoc.html#signing>
568
///
569
/// Compatibility type alias for [`RsaSha1Signature`].
570
#[deprecated = "use RsaSha1Signature"]
571
pub type AuthCertSignature = RsaSha1Signature;
572

            
573
impl ItemObjectParseable for CrossCertObject {
574
958
    fn check_label(label: &str) -> StdResult<(), parse2::EP> {
575
958
        match label {
576
958
            "SIGNATURE" | "ID SIGNATURE" => Ok(()),
577
2
            _ => Err(parse2::EP::ObjectIncorrectLabel),
578
        }
579
958
    }
580

            
581
956
    fn from_bytes(input: &[u8]) -> StdResult<Self, parse2::EP> {
582
956
        Ok(Self(input.to_vec()))
583
956
    }
584
}
585

            
586
impl ItemObjectEncodable for CrossCertObject {
587
2
    fn label(&self) -> &str {
588
2
        "ID SIGNATURE"
589
2
    }
590

            
591
2
    fn write_object_onto(&self, b: &mut Vec<u8>) -> StdResult<(), Bug> {
592
2
        b.extend(&self.0);
593
2
        Ok(())
594
2
    }
595
}
596

            
597
impl tor_checkable::SelfSigned<timed::TimerangeBound<AuthCert>> for UncheckedAuthCert {
598
    type Error = signature::Error;
599

            
600
178
    fn dangerously_assume_wellsigned(self) -> timed::TimerangeBound<AuthCert> {
601
178
        self.c.dangerously_assume_wellsigned()
602
178
    }
603
178
    fn is_well_signed(&self) -> std::result::Result<(), Self::Error> {
604
178
        self.c.is_well_signed()
605
178
    }
606
}
607

            
608
impl AuthCertUnverified {
609
    /// Verifies the signature of a [`AuthCert`]
610
    ///
611
    /// # Algorithm
612
    ///
613
    /// 1. Check whether this comes from a valid authority in `v3idents`.
614
    /// 2. Check whether the timestamps are valid (± tolerance).
615
    /// 3. Check whether the fingerprint and long-term identity key match.
616
    /// 4. Check the cross-certificate (proof-of-ownership of signing key).
617
    /// 5. Check the outer certificate (proof-of-ownership of identity key).
618
    ///
619
    /// TODO: Replace `pre_tolerance` and `post_tolerance` with
620
    /// `tor_dircommon::config::DirTolerance` which is not possible at the
621
    /// moment due to a circular dependency of `tor-dircommon` depending
622
    /// upon `tor-netdoc`.
623
    ///
624
    /// TODO: Consider whether to try to deduplicate this signature checking
625
    /// somehow, wrt to [`UncheckedAuthCert`].
626
532
    pub fn verify(
627
532
        self,
628
532
        v3idents: &[RsaIdentity],
629
532
    ) -> StdResult<TimerangeBound<AuthCert>, parse2::VerifyFailed> {
630
532
        let (body, sigs) = (self.body, self.sigs);
631

            
632
        // (1) Check whether this comes from a valid authority in `v3idents`.
633
532
        if !v3idents.contains(&body.fingerprint.0) {
634
2
            return Err(parse2::VerifyFailed::InsufficientTrustedSigners);
635
530
        }
636

            
637
        // (2) Check whether the timestamps are valid (± tolerance).
638
530
        let validity = *body.dir_key_published..=*body.dir_key_expires;
639

            
640
        // (3) Check whether the fingerprint and long-term identity key match.
641
530
        if body.dir_identity_key.to_rsa_identity() != *body.fingerprint {
642
2
            return Err(parse2::VerifyFailed::Inconsistent);
643
528
        }
644

            
645
        // (4) Check the cross-certificate (proof-of-ownership of signing key).
646
528
        body.dir_signing_key.verify(
647
528
            body.fingerprint.0.as_bytes(),
648
528
            &body.dir_key_crosscert.signature,
649
2
        )?;
650

            
651
        // (5) Check the outer certificate (proof-of-ownership of identity key).
652
526
        body.dir_identity_key.verify(
653
526
            &sigs.hashes.0.ok_or(parse2::VerifyFailed::Bug)?,
654
526
            &sigs.sigs.dir_key_certification.signature,
655
2
        )?;
656

            
657
524
        Ok(TimerangeBound::new(body, validity))
658
532
    }
659

            
660
    /// Verify the signatures (and check validity times)
661
    ///
662
    /// The pre and post tolerance (time check allowances) used are both zero.
663
    ///
664
    /// # Security considerations
665
    ///
666
    /// The caller must check that the KP_auth_id is correct/relevant.
667
18
    pub fn verify_selfcert(self, now: SystemTime) -> StdResult<AuthCert, parse2::VerifyFailed> {
668
18
        let h_kp_auth_id_rsa = self.inspect_unverified().0.fingerprint.0;
669
18
        Ok(self.verify(&[h_kp_auth_id_rsa])?.check_valid_at(&now)?)
670
18
    }
671
}
672

            
673
impl AuthCert {
674
    /// Make the base for a new `AuthCert`
675
    ///
676
    /// This contains only the mandatory fields (the ones in `AuthCertConstructor`).
677
    /// This method is an alternative to providing a `AuthCertConstructor` value display,
678
    /// and is convenient because an authcert contains much recapitulated information.
679
    ///
680
    /// # Example
681
    ///
682
    /// ```no_run
683
    /// # fn main() -> Result<(), anyhow::Error> {
684
    /// use tor_netdoc::doc::authcert::AuthCert;
685
    /// let (k_auth_id_rsa, k_auth_sign_rsa, published, expires) = todo!();
686
    /// let authcert = AuthCert {
687
    ///     dir_address: Some("192.0.2.17:7000".parse()?),
688
    ///     ..AuthCert::new_base(&k_auth_id_rsa, &k_auth_sign_rsa, published, expires)?
689
    /// };
690
    /// # Ok(())
691
    /// # }
692
    /// ```
693
2
    pub fn new_base(
694
2
        k_auth_id_rsa: &rsa::KeyPair,
695
2
        k_auth_sign_rsa: &rsa::KeyPair,
696
2
        published: SystemTime,
697
2
        expires: SystemTime,
698
2
    ) -> StdResult<Self, Bug> {
699
2
        let fingerprint = k_auth_id_rsa.to_public_key().to_rsa_identity();
700
2
        let dir_key_crosscert = CrossCert::new(k_auth_sign_rsa, &fingerprint)?;
701

            
702
2
        let base = AuthCertConstructor {
703
2
            fingerprint: fingerprint.into(),
704
2
            dir_key_published: published.into(),
705
2
            dir_key_expires: expires.into(),
706
2
            dir_identity_key: k_auth_id_rsa.to_public_key(),
707
2
            dir_signing_key: k_auth_sign_rsa.to_public_key(),
708
2
            dir_key_crosscert,
709
2
        }
710
2
        .construct();
711

            
712
2
        Ok(base)
713
2
    }
714

            
715
    /// Encode this `AuthCert` and sign it with `k_auth_id_rsa`
716
    ///
717
    /// Yields the string representation of the signed, encoded, document,
718
    /// as an [`EncodedAuthCert`].
719
    // TODO these features are quite tangled
720
    // `EncodedAuthCert` is only available with `parse2` and `plain-consensus`
721
    #[cfg(feature = "incomplete")] // Needs EncodedAuthCert
722
2
    pub fn encode_sign(&self, k_auth_id_rsa: &rsa::KeyPair) -> StdResult<EncodedAuthCert, Bug> {
723
2
        let mut encoder = NetdocEncoder::new();
724
2
        self.encode_unsigned(&mut encoder)?;
725

            
726
2
        let signature =
727
2
            RsaSha1Signature::new_sign_netdoc(k_auth_id_rsa, &encoder, "dir-key-certification")?;
728
2
        let sigs = AuthCertSignatures {
729
2
            dir_key_certification: signature,
730
2
        };
731
2
        sigs.encode_unsigned(&mut encoder)?;
732

            
733
2
        let encoded = encoder.finish()?;
734
        // This rechecks the invariants which ought to be true by construction.
735
        // That is convenient for the code here, and the perf implications are irrelevant.
736
2
        let encoded = encoded
737
2
            .try_into()
738
2
            .map_err(into_internal!("generated broken authcert"))?;
739
2
        Ok(encoded)
740
2
    }
741
}
742

            
743
#[cfg(test)]
744
mod test {
745
    // @@ begin test lint list maintained by maint/add_warning @@
746
    #![allow(clippy::bool_assert_comparison)]
747
    #![allow(clippy::clone_on_copy)]
748
    #![allow(clippy::dbg_macro)]
749
    #![allow(clippy::mixed_attributes_style)]
750
    #![allow(clippy::print_stderr)]
751
    #![allow(clippy::print_stdout)]
752
    #![allow(clippy::single_char_pattern)]
753
    #![allow(clippy::unwrap_used)]
754
    #![allow(clippy::unchecked_time_subtraction)]
755
    #![allow(clippy::useless_vec)]
756
    #![allow(clippy::needless_pass_by_value)]
757
    #![allow(clippy::string_slice)] // See arti#2571
758
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
759
    use super::*;
760
    use crate::{
761
        Pos,
762
        parse2::{ErrorProblem, ParseError, ParseInput, VerifyFailed, parse_netdoc},
763
        types,
764
    };
765
    use humantime::parse_rfc3339;
766
    use std::result::Result;
767
    use std::{
768
        fs,
769
        net::{Ipv4Addr, SocketAddrV4},
770
        str::FromStr,
771
        time::Duration,
772
    };
773
    use tor_basic_utils::test_rng;
774

            
775
    const TESTDATA: &str = include_str!("../../testdata/authcert1.txt");
776

            
777
    fn bad_data(fname: &str) -> String {
778
        use std::fs;
779
        use std::path::PathBuf;
780
        let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
781
        path.push("testdata");
782
        path.push("bad-certs");
783
        path.push(fname);
784

            
785
        fs::read_to_string(path).unwrap()
786
    }
787

            
788
    #[test]
789
    fn parse_one() -> crate::Result<()> {
790
        use tor_checkable::{SelfSigned, Timebound};
791
        let cert = AuthCert::parse(TESTDATA)?
792
            .check_signature()
793
            .unwrap()
794
            .dangerously_assume_timely();
795

            
796
        // Taken from TESTDATA
797
        assert_eq!(
798
            cert.id_fingerprint().to_string(),
799
            "$ed03bb616eb2f60bec80151114bb25cef515b226"
800
        );
801
        assert_eq!(
802
            cert.key_ids().sk_fingerprint.to_string(),
803
            "$c4f720e2c59f9ddd4867fff465ca04031e35648f"
804
        );
805

            
806
        Ok(())
807
    }
808

            
809
    #[test]
810
    fn parse_bad() {
811
        fn check(fname: &str, err: &crate::Error) {
812
            let contents = bad_data(fname);
813
            let cert = AuthCert::parse(&contents);
814
            assert!(cert.is_err());
815
            assert_eq!(&cert.err().unwrap(), err);
816
        }
817

            
818
        check(
819
            "bad-cc-tag",
820
            &EK::WrongObject.at_pos(Pos::from_line(27, 12)),
821
        );
822
        check(
823
            "bad-fingerprint",
824
            &EK::BadArgument
825
                .at_pos(Pos::from_line(2, 1))
826
                .with_msg("fingerprint does not match RSA identity"),
827
        );
828
        check(
829
            "bad-version",
830
            &EK::BadDocumentVersion.with_msg("unexpected version 4"),
831
        );
832
        check(
833
            "wrong-end",
834
            &EK::WrongEndingToken
835
                .with_msg("dir-key-crosscert")
836
                .at_pos(Pos::from_line(37, 1)),
837
        );
838
        check(
839
            "wrong-start",
840
            &EK::WrongStartingToken
841
                .with_msg("fingerprint")
842
                .at_pos(Pos::from_line(1, 1)),
843
        );
844
    }
845

            
846
    #[test]
847
    fn test_recovery_1() {
848
        let mut data = "<><><<><>\nfingerprint ABC\n".to_string();
849
        data += TESTDATA;
850

            
851
        let res: Vec<crate::Result<_>> = AuthCert::parse_multiple(&data).unwrap().collect();
852

            
853
        // We should recover from the failed case and read the next data fine.
854
        assert!(res[0].is_err());
855
        assert!(res[1].is_ok());
856
        assert_eq!(res.len(), 2);
857
    }
858

            
859
    #[test]
860
    fn test_recovery_2() {
861
        let mut data = bad_data("bad-version");
862
        data += TESTDATA;
863

            
864
        let res: Vec<crate::Result<_>> = AuthCert::parse_multiple(&data).unwrap().collect();
865

            
866
        // We should recover from the failed case and read the next data fine.
867
        assert!(res[0].is_err());
868
        assert!(res[1].is_ok());
869
        assert_eq!(res.len(), 2);
870
    }
871

            
872
    // === AUTHCERT D190BF3B00E311A9AEB6D62B51980E9B2109BAD1 ===
873
    // These values come from testdata2/keys/authority_certificate.
874
    const DIR_KEY_PUBLISHED: &str = "2000-01-01 00:00:05";
875
    const DIR_KEY_EXPIRES: &str = "2001-01-01 00:00:05";
876
    const FINGERPRINT: &str = "D190BF3B00E311A9AEB6D62B51980E9B2109BAD1";
877
    const DIR_ADDRESS: SocketAddrV4 = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 7100);
878
    const DIR_IDENTITY_KEY: &str = "
879
-----BEGIN RSA PUBLIC KEY-----
880
MIIBigKCAYEAt0rXD+1gYwKFAxrO4uNHQ9dQVUOGx5FxkioYNSct5Z3JU00dTKNJ
881
jt4OGkFYwixWwk6KLDOiB+I/q9YIdA1NlQ5R3Hz8jjvFPVl0JQQm2LYzdSzv7/CZ
882
U1qq5rYeeoYKx8qMQg4q3WgR251GEnOG+rVqzFSs0oyC+SDfYn9iMt00/pmN3HXf
883
wmasY6BescVrYoDbnpkwKATizd4lzx5K8V8aXUXtd8qnYzSyHLlhiO1eufVX07YC
884
+AVHV7W7qCTY/4I5Sm0dQ9jF/r04JBHnpH+aae48JOjWDCZj9AINi3rCKS8XClGb
885
BB/LJidoQAZraQEEtu3Ql1mjdLreeyWfXpfZFvwKuYn44FtQsOT2TVAVNqNF8N4v
886
yfwfiPN6FQWlPyMCEB81HerCn03Zi5WgQLGo7PAeO4LFrLrU16DUC5/oJENeHs0T
887
27FZQyrlf0rAxiHh7TJKcjLmzeyxCQVQlr2AXXs28gKHV0AQnEcdrVOpTrquSCQQ
888
hWBehR+ct4OJAgMBAAE=
889
-----END RSA PUBLIC KEY-----
890
    ";
891
    const DIR_SIGNING_KEY: &str = "
892
-----BEGIN RSA PUBLIC KEY-----
893
MIIBCgKCAQEAtPF94+bThLI28kn6e+MmUECMMJ5UBlnQ+Mvwn8Zd85awPQTDz5Wu
894
13sZDN3nWnhgSuP5q/WDYc5GPPtQdSWBiG1nJA2XLgEHTHf29iGZ+jAoGfIMJvBV
895
1xN8baTnsha5LGx5BQ4UqzlUmoaPzwbjehnPd00FgVkpcCvKZu1HU7fGMVwn4MMh
896
zuxJTqTgfcuFWTEu0H0ukOFX+51ih6WO3GWYqRiqgU0Q5/Ets8ccCTq7ND9d2u1P
897
d7kQzUHbVP0KmYGK4qYntGDfP4g9SmpBoUUHyP3j9en9S6PMYv8m1YFO7M7JKu6Q
898
dQZfGTxj9C/0b/jRklgn5JlKAl9eJQvCdwIDAQAB
899
-----END RSA PUBLIC KEY-----
900
";
901
    const DIR_CROSS_CERT_OBJECT: &str = "
902
-----BEGIN ID SIGNATURE-----
903
NBaPdBNCNMah6cklrALzj0RdHymF/jPGOv9NmeqaXc0uTN06S/BlVM/xTjilu+dj
904
sjPuT0BQL4/ZWyZR+R+gJJojKYILSId4IQ1elzRSxpFN+u2u/ZEmS6SR2SwpA05A
905
btOYBKAmYkY6rLsTCbXGx3lAH2kAXfcrltCNKZXV6gqW7X379fiOnSId1OWhKPe1
906
/1p3pQGZxgb8FOT1kpHxOMRBClF9Ulm3d9fQZr80Wn73gZ2Bp1RXn9c7c/71HD1c
907
mzMT023bleZ574az+117yNAr6XbIgqQfzbySzVLPXM8ZN9BrGR40KDZ2638ZJjRu
908
8HK5TzuknWlkRv3hCyRX+g==
909
-----END ID SIGNATURE-----
910
";
911
    const AUTHCERT_RAW: &str = include_str!("../../testdata2/keys/authority_certificate");
912
    /// A system time in the range of [`DIR_KEY_PUBLISHED`] and [`DIR_KEY_EXPIRES`].
913
    ///
914
    /// Constructed by ourselves to have a time point we can use for testing
915
    /// timestamp verification.
916
    const VALID_SYSTEM_TIME: &str = "2000-06-01 00:00:00";
917

            
918
    // === AUTHCERT 0B8997614EC647C1C6B6A044E2B5408F0B823FB0 ===
919
    // This values come from ../../testdata2/cached-certs--1
920
    // A different authority certificate different from the one above.
921
    const ALTERNATIVE_AUTHCERT_RAW: &str = include_str!("../../testdata2/cached-certs--1");
922

            
923
    /// Converts a string in the [`Iso8601TimeSp`] format to [`SystemTime`].
924
    ///
925
    /// This functions panics in the case the input is malformatted.
926
    fn to_system_time(s: &str) -> SystemTime {
927
        Iso8601TimeSp::from_str(s).unwrap().0
928
    }
929

            
930
    /// Converts a PEM encoded RSA Public key to an [`rsa::PublicKey`].
931
    ///
932
    /// This function panics in the case the input is malformatted.
933
    fn pem_to_rsa_pk(s: &str) -> rsa::PublicKey {
934
        rsa::PublicKey::from_der(pem::parse(s).unwrap().contents()).unwrap()
935
    }
936

            
937
    /// Converts a hex-encoded RSA identity to an [`RsaIdentity`].
938
    ///
939
    /// This function panics in the case the input is malformatted.
940
    fn to_rsa_id(s: &str) -> RsaIdentity {
941
        RsaIdentity::from_hex(s).unwrap()
942
    }
943

            
944
    /// Tests whether a [`DirKeyCrossCert`] can be parsed properly.
945
    #[test]
946
    fn dir_auth_cross_cert() {
947
        #[derive(Debug, Clone, PartialEq, Eq, Deftly)]
948
        #[derive_deftly(NetdocParseable)]
949
        struct Dummy {
950
            dir_key_crosscert: CrossCert,
951
        }
952

            
953
        // "Encodes" a DIR_CROSS_CERT_OBJECT by simply removing the lines
954
        // indicating the BEGIN and END, as the purpose is to test multiple
955
        // labels.
956
        let encoded = DIR_CROSS_CERT_OBJECT
957
            .lines()
958
            .filter(|line| !line.starts_with("-----"))
959
            .collect::<Vec<_>>()
960
            .join("\n");
961
        let decoded = pem::parse(DIR_CROSS_CERT_OBJECT)
962
            .unwrap()
963
            .contents()
964
            .to_vec();
965

            
966
        // Try with `SIGNATURE`.
967
        let cert = format!(
968
            "dir-key-crosscert\n-----BEGIN SIGNATURE-----\n{encoded}\n-----END SIGNATURE-----"
969
        );
970
        let res = parse2::parse_netdoc::<Dummy>(&ParseInput::new(&cert, "")).unwrap();
971
        assert_eq!(
972
            res,
973
            Dummy {
974
                dir_key_crosscert: CrossCert {
975
                    signature: CrossCertObject(decoded.clone())
976
                }
977
            }
978
        );
979

            
980
        // Try with `ID SIGNATURE`.
981
        let cert = format!(
982
            "dir-key-crosscert\n-----BEGIN ID SIGNATURE-----\n{encoded}\n-----END ID SIGNATURE-----"
983
        );
984
        let res = parse2::parse_netdoc::<Dummy>(&ParseInput::new(&cert, "")).unwrap();
985
        assert_eq!(
986
            res,
987
            Dummy {
988
                dir_key_crosscert: CrossCert {
989
                    signature: CrossCertObject(decoded.clone())
990
                }
991
            }
992
        );
993

            
994
        // Try with different label and fail.
995
        let cert =
996
            format!("dir-key-crosscert\n-----BEGIN WHAT-----\n{encoded}\n-----END WHAT-----");
997
        let res = parse2::parse_netdoc::<Dummy>(&ParseInput::new(&cert, ""));
998
        match res {
999
            Err(ParseError {
                problem: ErrorProblem::ObjectIncorrectLabel,
                doctype: "dir-key-crosscert",
                file: _,
                lno: 1,
                column: None,
            }) => {}
            other => panic!("not expected error {other:#?}"),
        }
        // Try with extra args.
        let cert = format!(
            "dir-key-crosscert arg1\n-----BEGIN ID SIGNATURE-----\n{encoded}\n-----END ID SIGNATURE-----"
        );
        let res = parse2::parse_netdoc::<Dummy>(&ParseInput::new(&cert, ""));
        match res {
            Err(ParseError {
                problem: ErrorProblem::UnexpectedArgument { column: 19 },
                doctype: "dir-key-crosscert",
                file: _,
                lno: 1,
                column: Some(19),
            }) => {}
            other => panic!("not expected error {other:#?}"),
        }
    }
    #[test]
    fn dir_auth_cert() {
        let res =
            parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(AUTHCERT_RAW, "")).unwrap();
        assert_eq!(
            *res.inspect_unverified().0,
            AuthCert {
                dir_key_certificate_version: AuthCertVersion::V3,
                dir_address: Some(DIR_ADDRESS),
                fingerprint: types::Fingerprint(to_rsa_id(FINGERPRINT)),
                dir_key_published: Iso8601TimeSp(to_system_time(DIR_KEY_PUBLISHED)),
                dir_key_expires: Iso8601TimeSp(to_system_time(DIR_KEY_EXPIRES)),
                dir_identity_key: pem_to_rsa_pk(DIR_IDENTITY_KEY),
                dir_signing_key: pem_to_rsa_pk(DIR_SIGNING_KEY),
                dir_key_crosscert: CrossCert {
                    signature: CrossCertObject(
                        pem::parse(DIR_CROSS_CERT_OBJECT)
                            .unwrap()
                            .contents()
                            .to_vec()
                    )
                },
                __non_exhaustive: (),
            }
        );
    }
    #[test]
    fn dir_auth_signature() {
        let res =
            parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(AUTHCERT_RAW, "")).unwrap();
        // Test a valid signature.
        let _: AuthCert = res
            .clone()
            .verify(&[to_rsa_id(FINGERPRINT)])
            .unwrap()
            .check_valid_at(&to_system_time(VALID_SYSTEM_TIME))
            .unwrap();
        // Test with an invalid authority.
        assert_eq!(
            res.clone().verify(&[],).unwrap_err(),
            VerifyFailed::InsufficientTrustedSigners
        );
        // Test a key too far in the future.
        assert_eq!(
            res.clone()
                .verify(&[to_rsa_id(FINGERPRINT)],)
                .unwrap()
                .check_valid_at(&SystemTime::UNIX_EPOCH,)
                .map_err(VerifyFailed::from)
                .unwrap_err(),
            VerifyFailed::TooNew
        );
        // Test an almost too new.
        let _: AuthCert = res
            .clone()
            .verify(&[to_rsa_id(FINGERPRINT)])
            .unwrap()
            .check_valid_at(&to_system_time(DIR_KEY_PUBLISHED))
            .unwrap();
        // Now fail when we are 1s below ...
        assert_eq!(
            res.clone()
                .verify(&[to_rsa_id(FINGERPRINT)],)
                .unwrap()
                .check_valid_at(&(to_system_time(DIR_KEY_PUBLISHED) - Duration::from_secs(1)),)
                .map_err(VerifyFailed::from)
                .unwrap_err(),
            VerifyFailed::TooNew
        );
        // ... but succeed again with a clock skew tolerance.
        let _: AuthCert = res
            .clone()
            .verify(&[to_rsa_id(FINGERPRINT)])
            .unwrap()
            .extend_pre_tolerance(Duration::from_secs(1))
            .check_valid_at(&(to_system_time(DIR_KEY_PUBLISHED) - Duration::from_secs(1)))
            .unwrap();
        // Test a key too old.
        assert_eq!(
            res.clone()
                .verify(&[to_rsa_id(FINGERPRINT)],)
                .unwrap()
                .check_valid_at(
                    &SystemTime::UNIX_EPOCH
                        .checked_add(Duration::from_secs(2000000000))
                        .unwrap(),
                )
                .map_err(VerifyFailed::from)
                .unwrap_err(),
            VerifyFailed::TooOld
        );
        // Test an almost too old.
        let _: AuthCert = res
            .clone()
            .verify(&[to_rsa_id(FINGERPRINT)])
            .unwrap()
            .check_valid_at(&to_system_time(DIR_KEY_EXPIRES))
            .unwrap();
        // Now fail when we are 1s above ...
        assert_eq!(
            res.clone()
                .verify(&[to_rsa_id(FINGERPRINT)],)
                .unwrap()
                .check_valid_at(&(to_system_time(DIR_KEY_EXPIRES) + Duration::from_secs(1)),)
                .map_err(VerifyFailed::from)
                .unwrap_err(),
            VerifyFailed::TooOld
        );
        // ... but succeed again with a clock skew tolerance.
        let _: AuthCert = res
            .clone()
            .verify(&[to_rsa_id(FINGERPRINT)])
            .unwrap()
            .extend_tolerance(Duration::from_secs(1))
            .check_valid_at(&(to_system_time(DIR_KEY_EXPIRES) + Duration::from_secs(1)))
            .unwrap();
        // Check with non-matching fingerprint and long-term identity key.
        let mut cert =
            parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(AUTHCERT_RAW, "")).unwrap();
        let alternative_cert = parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(
            ALTERNATIVE_AUTHCERT_RAW,
            "",
        ))
        .unwrap();
        cert.body.dir_identity_key = alternative_cert.body.dir_identity_key.clone();
        assert_eq!(
            cert.verify(&[to_rsa_id(FINGERPRINT)],).unwrap_err(),
            VerifyFailed::Inconsistent
        );
        // Check invalid cross-cert.
        let mut cert =
            parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(AUTHCERT_RAW, "")).unwrap();
        cert.body.dir_key_crosscert = alternative_cert.body.dir_key_crosscert.clone();
        assert_eq!(
            cert.verify(&[to_rsa_id(FINGERPRINT)],).unwrap_err(),
            VerifyFailed::VerifyFailed
        );
        // Check outer signature.
        let mut cert =
            parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(AUTHCERT_RAW, "")).unwrap();
        cert.sigs = alternative_cert.sigs.clone();
        assert_eq!(
            cert.verify(&[to_rsa_id(FINGERPRINT)],).unwrap_err(),
            VerifyFailed::VerifyFailed
        );
    }
    #[test]
    fn keyids_for_directory_signature() -> anyhow::Result<()> {
        #[derive(Deftly)]
        #[derive_deftly(NetdocEncodable, NetdocParseable)]
        struct Doc {
            intro: (),
            ids: Item,
        }
        #[derive(Deftly)]
        #[derive_deftly(ItemValueEncodable, ItemValueParseable)]
        struct Item {
            #[deftly(netdoc(with = keyids_directory_signature_args))]
            ids: AuthCertKeyIds,
        }
        let text = r#"intro
ids 1234567812345678123456781234567812345678 ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD
"#;
        let doc = parse2::parse_netdoc::<Doc>(&ParseInput::new(text, "<text>"))?;
        let mut re_encode = NetdocEncoder::new();
        doc.encode_unsigned(&mut re_encode)?;
        let re_encode = re_encode.finish()?;
        assert_eq_or_diff!(text, re_encode);
        Ok(())
    }
    #[test]
    #[cfg(feature = "incomplete")]
    fn roundtrip() -> Result<(), anyhow::Error> {
        let mut rng = test_rng::testing_rng();
        let k_auth_id_rsa = rsa::KeyPair::generate(&mut rng)?;
        let k_auth_sign_rsa = rsa::KeyPair::generate(&mut rng)?;
        let secs = |s| Duration::from_secs(s);
        let now = parse_rfc3339("1993-01-01T00:00:00Z")?;
        let published = now - secs(1000);
        let expires = published + secs(86400);
        let tolerance = secs(10);
        let input_value = AuthCert {
            dir_address: Some("192.0.2.17:7000".parse()?),
            ..AuthCert::new_base(&k_auth_id_rsa, &k_auth_sign_rsa, published, expires)?
        };
        dbg!(&input_value);
        let encoded = input_value.encode_sign(&k_auth_id_rsa)?;
        let reparsed_uv: AuthCertUnverified =
            parse_netdoc(&ParseInput::new(encoded.as_ref(), "<encoded>"))?;
        let reparsed_value = reparsed_uv
            .verify(&[k_auth_id_rsa.to_public_key().to_rsa_identity()])?
            .extend_pre_tolerance(tolerance)
            .extend_tolerance(tolerance)
            .check_valid_at(&now)?;
        dbg!(&reparsed_value);
        assert_eq!(input_value, reparsed_value);
        Ok(())
    }
    #[cfg(feature = "incomplete")]
    #[test]
    fn parse_authcert() -> anyhow::Result<()> {
        let file = "testdata2/cached-certs--1";
        let now = parse_rfc3339("2000-06-01T00:00:05Z")?;
        let text = fs::read_to_string(file)?;
        let input = ParseInput::new(&text, file);
        let doc: AuthCertUnverified = parse_netdoc(&input)?;
        let doc = doc.verify_selfcert(now)?;
        println!("{doc:?}");
        assert_eq!(
            doc.fingerprint.0.to_string(),
            "$0b8997614ec647c1c6b6a044e2b5408f0b823fb0",
        );
        Ok(())
    }
}