1use crate::batching_split_before::IteratorExt as _;
10use crate::encode::{
11 Bug, ItemArgument, ItemEncoder, ItemObjectEncodable, NetdocEncodable, NetdocEncoder,
12};
13use crate::parse::keyword::Keyword;
14use crate::parse::parser::{Section, SectionRules};
15use crate::parse::tokenize::{ItemResult, NetDocReader};
16use crate::parse2::{
17 self, ArgumentError, ArgumentStream, ItemArgumentParseable, ItemObjectParseable,
18 NetdocParseableUnverified as _, sig_hashes::Sha1WholeKeywordLine,
19};
20use crate::types::misc::{Fingerprint, Iso8601TimeSp, RsaPublicParse1Helper, RsaSha1Signature};
21use crate::util::str::Extent;
22use crate::{NetdocErrorKind as EK, NormalItemArgument, Result};
23
24use tor_basic_utils::impl_debug_hex;
25use tor_checkable::{
26 TimeBound, signed,
27 timed::{self, TimeRangeBound},
28};
29use tor_error::{internal, into_internal};
30use tor_llcrypto::pk::rsa;
31use tor_llcrypto::{d, pk, pk::rsa::RsaIdentity};
32
33use std::sync::LazyLock;
34
35use std::result::Result as StdResult;
36use std::{net, time, time::SystemTime};
37
38use derive_deftly::Deftly;
39use digest::Digest;
40
41#[cfg(feature = "build_docs")]
42mod build;
43
44#[cfg(feature = "build_docs")]
45#[allow(deprecated)]
46pub use build::AuthCertBuilder;
47
48mod encoded;
49pub use encoded::EncodedAuthCert;
50
51decl_keyword! {
52 pub(crate) AuthCertKwd {
53 "dir-key-certificate-version" => DIR_KEY_CERTIFICATE_VERSION,
54 "dir-address" => DIR_ADDRESS,
55 "fingerprint" => FINGERPRINT,
56 "dir-identity-key" => DIR_IDENTITY_KEY,
57 "dir-key-published" => DIR_KEY_PUBLISHED,
58 "dir-key-expires" => DIR_KEY_EXPIRES,
59 "dir-signing-key" => DIR_SIGNING_KEY,
60 "dir-key-crosscert" => DIR_KEY_CROSSCERT,
61 "dir-key-certification" => DIR_KEY_CERTIFICATION,
62 }
63}
64
65static AUTHCERT_RULES: LazyLock<SectionRules<AuthCertKwd>> = LazyLock::new(|| {
68 use AuthCertKwd::*;
69
70 let mut rules = SectionRules::builder();
71 rules.add(DIR_KEY_CERTIFICATE_VERSION.rule().required().args(1..));
72 rules.add(DIR_ADDRESS.rule().args(1..));
73 rules.add(FINGERPRINT.rule().required().args(1..));
74 rules.add(DIR_IDENTITY_KEY.rule().required().no_args().obj_required());
75 rules.add(DIR_SIGNING_KEY.rule().required().no_args().obj_required());
76 rules.add(DIR_KEY_PUBLISHED.rule().required());
77 rules.add(DIR_KEY_EXPIRES.rule().required());
78 rules.add(DIR_KEY_CROSSCERT.rule().required().no_args().obj_required());
79 rules.add(UNRECOGNIZED.rule().may_repeat().obj_optional());
80 rules.add(
81 DIR_KEY_CERTIFICATION
82 .rule()
83 .required()
84 .no_args()
85 .obj_required(),
86 );
87 rules.build()
88});
89
90#[derive(Clone, Debug, Deftly)]
98#[derive_deftly(Constructor)]
99#[derive_deftly(NetdocParseableUnverified, NetdocEncodable)]
100#[cfg_attr(test, derive(PartialEq, Eq))]
101#[allow(clippy::exhaustive_structs)]
102pub struct AuthCert {
103 #[deftly(constructor(default = AuthCertVersion::V3))]
109 #[deftly(netdoc(single_arg))]
110 pub dir_key_certificate_version: AuthCertVersion,
111
112 #[deftly(netdoc(single_arg))]
114 pub dir_address: Option<net::SocketAddrV4>,
115
116 #[deftly(constructor)]
120 #[deftly(netdoc(single_arg))]
121 pub fingerprint: Fingerprint,
122
123 #[deftly(constructor)]
127 #[deftly(netdoc(single_arg))]
128 pub dir_key_published: Iso8601TimeSp,
129
130 #[deftly(constructor)]
134 #[deftly(netdoc(single_arg))]
135 pub dir_key_expires: Iso8601TimeSp,
136
137 #[deftly(constructor)]
143 pub dir_identity_key: rsa::PublicKey,
144
145 #[deftly(constructor)]
151 pub dir_signing_key: rsa::PublicKey,
152
153 #[deftly(constructor)]
157 pub dir_key_crosscert: CrossCert,
158
159 #[doc(hidden)]
160 #[deftly(netdoc(skip))]
161 pub __non_exhaustive: (),
162}
163
164#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, strum::EnumString, strum::Display)]
170#[non_exhaustive]
171pub enum AuthCertVersion {
172 #[strum(serialize = "3")]
174 V3,
175}
176
177impl NormalItemArgument for AuthCertVersion {}
178
179#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
181#[allow(clippy::exhaustive_structs)]
182pub struct AuthCertKeyIds {
183 pub id_fingerprint: rsa::RsaIdentity,
185 pub sk_fingerprint: rsa::RsaIdentity,
187}
188
189pub struct UncheckedAuthCert {
192 location: Option<Extent>,
194
195 c: signed::SignatureGated<timed::TimeRangeBound<AuthCert>>,
197}
198
199impl UncheckedAuthCert {
200 pub fn within<'a>(&self, haystack: &'a str) -> Option<&'a str> {
208 self.location
209 .as_ref()
210 .and_then(|ext| ext.reconstruct(haystack))
211 }
212}
213
214impl AuthCert {
215 #[cfg(feature = "build_docs")]
218 #[deprecated = "use AuthCertConstructor instead"]
219 #[allow(deprecated)]
220 pub fn builder() -> AuthCertBuilder {
221 AuthCertBuilder::new()
222 }
223
224 pub fn parse(s: &str) -> Result<UncheckedAuthCert> {
229 let mut reader = NetDocReader::new(s)?;
230 let body = AUTHCERT_RULES.parse(&mut reader)?;
231 reader.should_be_exhausted()?;
232 AuthCert::from_body(&body, s).map_err(|e| e.within(s))
233 }
234
235 pub fn parse_multiple(s: &str) -> Result<impl Iterator<Item = Result<UncheckedAuthCert>> + '_> {
237 use AuthCertKwd::*;
238 let sections = NetDocReader::new(s)?
239 .batching_split_before_loose(|item| item.is_ok_with_kwd(DIR_KEY_CERTIFICATE_VERSION));
240 Ok(sections
241 .map(|mut section| {
242 let body = AUTHCERT_RULES.parse(&mut section)?;
243 AuthCert::from_body(&body, s)
244 })
245 .map(|r| r.map_err(|e| e.within(s))))
246 }
247 pub fn signing_key(&self) -> &rsa::PublicKey {
256 &self.dir_signing_key
257 }
258
259 pub fn key_ids(&self) -> AuthCertKeyIds {
262 AuthCertKeyIds {
263 id_fingerprint: self.fingerprint.0,
264 sk_fingerprint: self.dir_signing_key.to_rsa_identity(),
265 }
266 }
267
268 pub fn id_fingerprint(&self) -> &rsa::RsaIdentity {
270 &self.fingerprint
271 }
272
273 pub fn published(&self) -> time::SystemTime {
275 *self.dir_key_published
276 }
277
278 pub fn expires(&self) -> time::SystemTime {
280 *self.dir_key_expires
281 }
282
283 fn from_body(body: &Section<'_, AuthCertKwd>, s: &str) -> Result<UncheckedAuthCert> {
285 use AuthCertKwd::*;
286
287 let start_pos = {
292 #[allow(clippy::unwrap_used)]
295 let first_item = body.first_item().unwrap();
296 if first_item.kwd() != DIR_KEY_CERTIFICATE_VERSION {
297 return Err(EK::WrongStartingToken
298 .with_msg(first_item.kwd_str().to_string())
299 .at_pos(first_item.pos()));
300 }
301 first_item.pos()
302 };
303 let end_pos = {
304 #[allow(clippy::unwrap_used)]
307 let last_item = body.last_item().unwrap();
308 if last_item.kwd() != DIR_KEY_CERTIFICATION {
309 return Err(EK::WrongEndingToken
310 .with_msg(last_item.kwd_str().to_string())
311 .at_pos(last_item.pos()));
312 }
313 last_item.end_pos()
314 };
315
316 let version = body
317 .required(DIR_KEY_CERTIFICATE_VERSION)?
318 .parse_arg::<u32>(0)?;
319 if version != 3 {
320 return Err(EK::BadDocumentVersion.with_msg(format!("unexpected version {}", version)));
321 }
322 let dir_key_certificate_version = AuthCertVersion::V3;
323
324 let dir_signing_key: rsa::PublicKey = body
325 .required(DIR_SIGNING_KEY)?
326 .parse_obj::<RsaPublicParse1Helper>("RSA PUBLIC KEY")?
327 .check_len(1024..)?
328 .check_exponent(65537)?
329 .into();
330
331 let dir_identity_key: rsa::PublicKey = body
332 .required(DIR_IDENTITY_KEY)?
333 .parse_obj::<RsaPublicParse1Helper>("RSA PUBLIC KEY")?
334 .check_len(1024..)?
335 .check_exponent(65537)?
336 .into();
337
338 let dir_key_published = body
339 .required(DIR_KEY_PUBLISHED)?
340 .args_as_str()
341 .parse::<Iso8601TimeSp>()?;
342
343 let dir_key_expires = body
344 .required(DIR_KEY_EXPIRES)?
345 .args_as_str()
346 .parse::<Iso8601TimeSp>()?;
347
348 {
349 let fp_tok = body.required(FINGERPRINT)?;
351 let fingerprint: RsaIdentity = fp_tok.args_as_str().parse::<Fingerprint>()?.into();
352 if fingerprint != dir_identity_key.to_rsa_identity() {
353 return Err(EK::BadArgument
354 .at_pos(fp_tok.pos())
355 .with_msg("fingerprint does not match RSA identity"));
356 }
357 }
358
359 let dir_address = body
360 .maybe(DIR_ADDRESS)
361 .parse_args_as_str::<net::SocketAddrV4>()?;
362
363 let dir_key_crosscert;
365 let v_crosscert = {
366 let crosscert = body.required(DIR_KEY_CROSSCERT)?;
367 #[allow(clippy::unwrap_used)]
370 let mut tag = crosscert.obj_tag().unwrap();
371 if tag != "ID SIGNATURE" && tag != "SIGNATURE" {
373 tag = "ID SIGNATURE";
374 }
375 let sig = crosscert.obj(tag)?;
376
377 let signed = dir_identity_key.to_rsa_identity();
378 let v = rsa::ValidatableRsaSignature::new(&dir_signing_key, &sig, signed.as_bytes());
381
382 dir_key_crosscert = CrossCert {
383 signature: CrossCertObject(sig),
384 };
385
386 v
387 };
388
389 let v_sig = {
391 let signature = body.required(DIR_KEY_CERTIFICATION)?;
392 let sig = signature.obj("SIGNATURE")?;
393
394 let mut sha1 = d::Sha1::new();
395 #[allow(clippy::unwrap_used)]
398 let start_offset = body.first_item().unwrap().offset_in(s).unwrap();
399 #[allow(clippy::unwrap_used)]
400 let end_offset = body.last_item().unwrap().offset_in(s).unwrap();
401 let end_offset = end_offset + "dir-key-certification\n".len();
402 sha1.update(
403 s.get(start_offset..end_offset)
404 .ok_or_else(|| internal!("chopped utf8"))?,
405 );
406 let sha1 = sha1.finalize();
407 rsa::ValidatableRsaSignature::new(&dir_identity_key, &sig, &sha1)
410 };
411
412 let id_fingerprint = dir_identity_key.to_rsa_identity();
413
414 let location = {
415 let start_idx = start_pos.offset_within(s);
416 let end_idx = end_pos.offset_within(s);
417 match (start_idx, end_idx) {
418 (Some(a), Some(b)) => {
419 Extent::new(s, s.get(a..b + 1).ok_or_else(|| internal!("chopped utf8"))?)
420 }
421 _ => None,
422 }
423 };
424
425 let authcert = AuthCert {
426 dir_key_certificate_version,
427 dir_address,
428 dir_identity_key,
429 dir_signing_key,
430 dir_key_published,
431 dir_key_expires,
432 dir_key_crosscert,
433 fingerprint: Fingerprint(id_fingerprint),
434 __non_exhaustive: (),
435 };
436
437 let signatures: Vec<Box<dyn pk::ValidatableSignature>> =
438 vec![Box::new(v_crosscert), Box::new(v_sig)];
439
440 let timed = timed::TimeRangeBound::new(authcert, *dir_key_published..*dir_key_expires);
441 let signed = signed::SignatureGated::new(timed, signatures);
442 let unchecked = UncheckedAuthCert {
443 location,
444 c: signed,
445 };
446 Ok(unchecked)
447 }
448}
449
450pub(crate) mod keyids_directory_signature_args {
462 use super::*;
463 use std::result::Result;
464
465 pub(crate) fn from_args<'s>(
467 args: &mut ArgumentStream<'s>,
468 ) -> Result<AuthCertKeyIds, ArgumentError> {
469 let mut fp = || Ok::<_, ArgumentError>(Fingerprint::from_args(args)?.0);
470 Ok(AuthCertKeyIds {
471 id_fingerprint: fp()?,
472 sk_fingerprint: fp()?,
473 })
474 }
475
476 pub(crate) fn write_arg_onto(
478 self_: &AuthCertKeyIds,
479 out: &mut ItemEncoder<'_>,
480 ) -> Result<(), Bug> {
481 let mut fp = |id| Fingerprint(id).write_arg_onto(out);
482 fp(self_.id_fingerprint)?;
483 fp(self_.sk_fingerprint)?;
484 Ok(())
485 }
486}
487
488#[derive(Debug, Clone, PartialEq, Eq, Deftly)]
502#[derive_deftly(ItemValueParseable, ItemValueEncodable)]
503#[deftly(netdoc(no_extra_args))]
504#[non_exhaustive]
505pub struct CrossCert {
506 #[deftly(netdoc(object))]
508 pub signature: CrossCertObject,
509}
510
511#[derive(Clone, PartialEq, Eq, derive_more::Deref)]
530#[non_exhaustive]
531pub struct CrossCertObject(pub Vec<u8>);
532impl_debug_hex! { CrossCertObject . 0 }
533
534impl CrossCert {
535 pub fn new(
537 k_auth_sign_rsa: &rsa::KeyPair,
538 h_kp_auth_id_rsa: &RsaIdentity,
539 ) -> StdResult<Self, Bug> {
540 let signature = k_auth_sign_rsa
541 .sign(h_kp_auth_id_rsa.as_bytes())
542 .map_err(into_internal!("failed to sign cross-cert"))?;
543 Ok(CrossCert {
544 signature: CrossCertObject(signature),
545 })
546 }
547}
548
549#[derive(Debug, Clone, PartialEq, Eq, Deftly)]
559#[derive_deftly(NetdocParseableSignatures, NetdocEncodable)]
560#[deftly(netdoc(signatures(hashes_accu = Sha1WholeKeywordLine)))]
561#[non_exhaustive]
562pub struct AuthCertSignatures {
563 pub dir_key_certification: RsaSha1Signature,
565}
566
567#[deprecated = "use RsaSha1Signature"]
573pub type AuthCertSignature = RsaSha1Signature;
574
575impl ItemObjectParseable for CrossCertObject {
576 fn check_label(label: &str) -> StdResult<(), parse2::EP> {
577 match label {
578 "SIGNATURE" | "ID SIGNATURE" => Ok(()),
579 _ => Err(parse2::EP::ObjectIncorrectLabel),
580 }
581 }
582
583 fn from_bytes(input: &[u8]) -> StdResult<Self, parse2::EP> {
584 Ok(Self(input.to_vec()))
585 }
586}
587
588impl ItemObjectEncodable for CrossCertObject {
589 fn label(&self) -> &str {
590 "ID SIGNATURE"
591 }
592
593 fn write_object_onto(&self, b: &mut Vec<u8>) -> StdResult<(), Bug> {
594 b.extend(&self.0);
595 Ok(())
596 }
597}
598
599impl tor_checkable::SelfSigned<timed::TimeRangeBound<AuthCert>> for UncheckedAuthCert {
600 type Error = signature::Error;
601
602 fn dangerously_assume_wellsigned(self) -> timed::TimeRangeBound<AuthCert> {
603 self.c.dangerously_assume_wellsigned()
604 }
605 fn is_well_signed(&self) -> std::result::Result<(), Self::Error> {
606 self.c.is_well_signed()
607 }
608}
609
610impl AuthCertUnverified {
611 pub fn verify(
629 self,
630 v3idents: &[RsaIdentity],
631 ) -> StdResult<TimeRangeBound<AuthCert>, parse2::VerifyFailed> {
632 let (body, sigs) = (self.body, self.sigs);
633
634 if !v3idents.contains(&body.fingerprint.0) {
636 return Err(parse2::VerifyFailed::InsufficientTrustedSigners);
637 }
638
639 let validity = *body.dir_key_published..=*body.dir_key_expires;
641
642 if body.dir_identity_key.to_rsa_identity() != *body.fingerprint {
644 return Err(parse2::VerifyFailed::Inconsistent);
645 }
646
647 body.dir_signing_key.verify(
649 body.fingerprint.0.as_bytes(),
650 &body.dir_key_crosscert.signature,
651 )?;
652
653 body.dir_identity_key.verify(
655 &sigs.hashes.0.ok_or(parse2::VerifyFailed::Bug)?,
656 &sigs.sigs.dir_key_certification.signature,
657 )?;
658
659 Ok(TimeRangeBound::new(body, validity))
660 }
661
662 pub fn verify_selfcert(self, now: SystemTime) -> StdResult<AuthCert, parse2::VerifyFailed> {
670 let h_kp_auth_id_rsa = self.inspect_unverified().0.fingerprint.0;
671 Ok(self.verify(&[h_kp_auth_id_rsa])?.if_valid_at(&now)?)
672 }
673}
674
675impl AuthCert {
676 pub fn new_base(
696 k_auth_id_rsa: &rsa::KeyPair,
697 k_auth_sign_rsa: &rsa::KeyPair,
698 published: SystemTime,
699 expires: SystemTime,
700 ) -> StdResult<Self, Bug> {
701 let fingerprint = k_auth_id_rsa.to_public_key().to_rsa_identity();
702 let dir_key_crosscert = CrossCert::new(k_auth_sign_rsa, &fingerprint)?;
703
704 let base = AuthCertConstructor {
705 fingerprint: fingerprint.into(),
706 dir_key_published: published.into(),
707 dir_key_expires: expires.into(),
708 dir_identity_key: k_auth_id_rsa.to_public_key(),
709 dir_signing_key: k_auth_sign_rsa.to_public_key(),
710 dir_key_crosscert,
711 }
712 .construct();
713
714 Ok(base)
715 }
716
717 pub fn encode_sign(&self, k_auth_id_rsa: &rsa::KeyPair) -> StdResult<EncodedAuthCert, Bug> {
722 let mut encoder = NetdocEncoder::new();
723 self.encode_unsigned(&mut encoder)?;
724
725 let signature =
726 RsaSha1Signature::new_sign_netdoc(k_auth_id_rsa, &encoder, "dir-key-certification")?;
727 let sigs = AuthCertSignatures {
728 dir_key_certification: signature,
729 };
730 sigs.encode_unsigned(&mut encoder)?;
731
732 let encoded = encoder.finish()?;
733 let encoded = encoded
736 .try_into()
737 .map_err(into_internal!("generated broken authcert"))?;
738 Ok(encoded)
739 }
740}
741
742#[cfg(test)]
743mod test {
744 #![allow(clippy::bool_assert_comparison)]
746 #![allow(clippy::clone_on_copy)]
747 #![allow(clippy::dbg_macro)]
748 #![allow(clippy::mixed_attributes_style)]
749 #![allow(clippy::print_stderr)]
750 #![allow(clippy::print_stdout)]
751 #![allow(clippy::single_char_pattern)]
752 #![allow(clippy::unwrap_used)]
753 #![allow(clippy::unchecked_time_subtraction)]
754 #![allow(clippy::useless_vec)]
755 #![allow(clippy::needless_pass_by_value)]
756 #![allow(clippy::string_slice)] use super::*;
759 use crate::{
760 Pos,
761 encode::encode_netdoc_unsigned,
762 parse2::{ErrorProblem, ParseError, ParseInput, VerifyFailed, parse_netdoc},
763 test_support::{raw_find_item_rhs, raw_find_object},
764 types,
765 };
766 use humantime::parse_rfc3339;
767 use std::result::Result;
768 use std::{fs, net::SocketAddrV4, time::Duration};
769 use tor_basic_utils::test_rng;
770
771 const TESTDATA: &str = include_str!("../../testdata/authcert1.txt");
772
773 fn bad_data(fname: &str) -> String {
774 use std::fs;
775 use std::path::PathBuf;
776 let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
777 path.push("testdata");
778 path.push("bad-certs");
779 path.push(fname);
780
781 fs::read_to_string(path).unwrap()
782 }
783
784 #[test]
785 fn parse_one() -> crate::Result<()> {
786 use tor_checkable::{SelfSigned, TimeBound};
787 let cert = AuthCert::parse(TESTDATA)?
788 .check_signature()
789 .unwrap()
790 .dangerously_assume_timely();
791
792 assert_eq!(
794 cert.id_fingerprint().to_string(),
795 "$ed03bb616eb2f60bec80151114bb25cef515b226"
796 );
797 assert_eq!(
798 cert.key_ids().sk_fingerprint.to_string(),
799 "$c4f720e2c59f9ddd4867fff465ca04031e35648f"
800 );
801
802 Ok(())
803 }
804
805 #[test]
806 fn parse_bad() {
807 fn check(fname: &str, err: &crate::Error) {
808 let contents = bad_data(fname);
809 let cert = AuthCert::parse(&contents);
810 assert!(cert.is_err());
811 assert_eq!(&cert.err().unwrap(), err);
812 }
813
814 check(
815 "bad-cc-tag",
816 &EK::WrongObject.at_pos(Pos::from_line(27, 12)),
817 );
818 check(
819 "bad-fingerprint",
820 &EK::BadArgument
821 .at_pos(Pos::from_line(2, 1))
822 .with_msg("fingerprint does not match RSA identity"),
823 );
824 check(
825 "bad-version",
826 &EK::BadDocumentVersion.with_msg("unexpected version 4"),
827 );
828 check(
829 "wrong-end",
830 &EK::WrongEndingToken
831 .with_msg("dir-key-crosscert")
832 .at_pos(Pos::from_line(37, 1)),
833 );
834 check(
835 "wrong-start",
836 &EK::WrongStartingToken
837 .with_msg("fingerprint")
838 .at_pos(Pos::from_line(1, 1)),
839 );
840 }
841
842 #[test]
843 fn test_recovery_1() {
844 let mut data = "<><><<><>\nfingerprint ABC\n".to_string();
845 data += TESTDATA;
846
847 let res: Vec<crate::Result<_>> = AuthCert::parse_multiple(&data).unwrap().collect();
848
849 assert!(res[0].is_err());
851 assert!(res[1].is_ok());
852 assert_eq!(res.len(), 2);
853 }
854
855 #[test]
856 fn test_recovery_2() {
857 let mut data = bad_data("bad-version");
858 data += TESTDATA;
859
860 let res: Vec<crate::Result<_>> = AuthCert::parse_multiple(&data).unwrap().collect();
861
862 assert!(res[0].is_err());
864 assert!(res[1].is_ok());
865 assert_eq!(res.len(), 2);
866 }
867
868 const AUTHCERT_RAW: &str = include_str!("../../testdata2/keys/authority_certificate");
869
870 fn raw_authcert_time(kw: &str) -> SystemTime {
871 let t: Iso8601TimeSp = raw_find_item_rhs(AUTHCERT_RAW, kw).parse().expect(kw);
872 t.0
873 }
874 fn t_dir_key_published() -> SystemTime {
875 raw_authcert_time("dir-key-published")
876 }
877 fn t_dir_key_expires() -> SystemTime {
878 raw_authcert_time("dir-key-expires")
879 }
880 fn t_valid_system_time() -> SystemTime {
885 let a = t_dir_key_published();
886 let b = t_dir_key_expires();
887 a + (b.duration_since(a).unwrap() / 2)
888 }
889
890 fn t_dir_address() -> SocketAddrV4 {
891 raw_find_item_rhs(AUTHCERT_RAW, "dir-address")
892 .parse()
893 .unwrap()
894 }
895
896 fn t_dir_identity_key() -> &'static str {
897 raw_find_object(AUTHCERT_RAW, "dir-identity-key")
898 }
899 fn t_dir_signing_key() -> &'static str {
900 raw_find_object(AUTHCERT_RAW, "dir-signing-key")
901 }
902 fn t_dir_cross_cert_object() -> &'static str {
903 raw_find_object(AUTHCERT_RAW, "dir-key-crosscert")
904 }
905
906 fn t_fingerprint() -> &'static str {
907 raw_find_item_rhs(AUTHCERT_RAW, "fingerprint")
908 }
909
910 fn t_alternative_authcert_raw() -> &'static str {
914 let t1 = include_str!("../../testdata2/cached-certs--1");
921 if t1.as_bytes() != AUTHCERT_RAW.as_bytes() {
922 t1
923 } else {
924 include_str!("../../testdata2/cached-certs--2")
925 }
926 }
927
928 fn pem_to_rsa_pk(s: &str) -> rsa::PublicKey {
932 rsa::PublicKey::from_der(pem::parse(s).unwrap().contents()).unwrap()
933 }
934
935 fn to_rsa_id(s: &str) -> RsaIdentity {
939 RsaIdentity::from_hex(s).unwrap()
940 }
941
942 #[test]
944 fn dir_auth_cross_cert() {
945 #[derive(Debug, Clone, PartialEq, Eq, Deftly)]
946 #[derive_deftly(NetdocParseable)]
947 struct Dummy {
948 dir_key_crosscert: CrossCert,
949 }
950
951 let encoded = t_dir_cross_cert_object()
955 .lines()
956 .filter(|line| !line.starts_with("-----"))
957 .collect::<Vec<_>>()
958 .join("\n");
959 let decoded = pem::parse(t_dir_cross_cert_object())
960 .unwrap()
961 .contents()
962 .to_vec();
963
964 let cert = format!(
966 "dir-key-crosscert\n-----BEGIN SIGNATURE-----\n{encoded}\n-----END SIGNATURE-----"
967 );
968 let res = parse2::parse_netdoc::<Dummy>(&ParseInput::new(&cert, "")).unwrap();
969 assert_eq!(
970 res,
971 Dummy {
972 dir_key_crosscert: CrossCert {
973 signature: CrossCertObject(decoded.clone())
974 }
975 }
976 );
977
978 let cert = format!(
980 "dir-key-crosscert\n-----BEGIN ID SIGNATURE-----\n{encoded}\n-----END ID SIGNATURE-----"
981 );
982 let res = parse2::parse_netdoc::<Dummy>(&ParseInput::new(&cert, "")).unwrap();
983 assert_eq!(
984 res,
985 Dummy {
986 dir_key_crosscert: CrossCert {
987 signature: CrossCertObject(decoded.clone())
988 }
989 }
990 );
991
992 let cert =
994 format!("dir-key-crosscert\n-----BEGIN WHAT-----\n{encoded}\n-----END WHAT-----");
995 let res = parse2::parse_netdoc::<Dummy>(&ParseInput::new(&cert, ""));
996 match res {
997 Err(ParseError {
998 problem: ErrorProblem::ObjectIncorrectLabel,
999 doctype: "dir-key-crosscert",
1000 file: _,
1001 lno: 1,
1002 column: None,
1003 }) => {}
1004 other => panic!("not expected error {other:#?}"),
1005 }
1006
1007 let cert = format!(
1009 "dir-key-crosscert arg1\n-----BEGIN ID SIGNATURE-----\n{encoded}\n-----END ID SIGNATURE-----"
1010 );
1011 let res = parse2::parse_netdoc::<Dummy>(&ParseInput::new(&cert, ""));
1012 match res {
1013 Err(ParseError {
1014 problem: ErrorProblem::UnexpectedArgument { column: 19 },
1015 doctype: "dir-key-crosscert",
1016 file: _,
1017 lno: 1,
1018 column: Some(19),
1019 }) => {}
1020 other => panic!("not expected error {other:#?}"),
1021 }
1022 }
1023
1024 #[test]
1025 fn dir_auth_cert() {
1026 let res =
1027 parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(AUTHCERT_RAW, "")).unwrap();
1028 assert_eq!(
1029 *res.inspect_unverified().0,
1030 AuthCert {
1031 dir_key_certificate_version: AuthCertVersion::V3,
1032 dir_address: Some(t_dir_address()),
1033 fingerprint: types::Fingerprint(to_rsa_id(t_fingerprint())),
1034 dir_key_published: Iso8601TimeSp(t_dir_key_published()),
1035 dir_key_expires: Iso8601TimeSp(t_dir_key_expires()),
1036 dir_identity_key: pem_to_rsa_pk(t_dir_identity_key()),
1037 dir_signing_key: pem_to_rsa_pk(t_dir_signing_key()),
1038 dir_key_crosscert: CrossCert {
1039 signature: CrossCertObject(
1040 pem::parse(t_dir_cross_cert_object())
1041 .unwrap()
1042 .contents()
1043 .to_vec()
1044 )
1045 },
1046 __non_exhaustive: (),
1047 }
1048 );
1049 }
1050
1051 #[test]
1052 fn dir_auth_signature() {
1053 let res =
1054 parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(AUTHCERT_RAW, "")).unwrap();
1055
1056 let _: AuthCert = res
1058 .clone()
1059 .verify(&[to_rsa_id(t_fingerprint())])
1060 .unwrap()
1061 .if_valid_at(&t_valid_system_time())
1062 .unwrap();
1063
1064 assert_eq!(
1066 res.clone().verify(&[],).unwrap_err(),
1067 VerifyFailed::InsufficientTrustedSigners
1068 );
1069
1070 assert_eq!(
1072 res.clone()
1073 .verify(&[to_rsa_id(t_fingerprint())],)
1074 .unwrap()
1075 .if_valid_at(&SystemTime::UNIX_EPOCH,)
1076 .map_err(VerifyFailed::from)
1077 .unwrap_err(),
1078 VerifyFailed::TooNew
1079 );
1080
1081 let _: AuthCert = res
1083 .clone()
1084 .verify(&[to_rsa_id(t_fingerprint())])
1085 .unwrap()
1086 .if_valid_at(&t_dir_key_published())
1087 .unwrap();
1088
1089 assert_eq!(
1091 res.clone()
1092 .verify(&[to_rsa_id(t_fingerprint())],)
1093 .unwrap()
1094 .if_valid_at(&(t_dir_key_published() - Duration::from_secs(1)),)
1095 .map_err(VerifyFailed::from)
1096 .unwrap_err(),
1097 VerifyFailed::TooNew
1098 );
1099
1100 let _: AuthCert = res
1102 .clone()
1103 .verify(&[to_rsa_id(t_fingerprint())])
1104 .unwrap()
1105 .extend_start_bound(Duration::from_secs(1))
1106 .if_valid_at(&(t_dir_key_published() - Duration::from_secs(1)))
1107 .unwrap();
1108
1109 assert_eq!(
1111 res.clone()
1112 .verify(&[to_rsa_id(t_fingerprint())],)
1113 .unwrap()
1114 .if_valid_at(
1115 &SystemTime::UNIX_EPOCH
1116 .checked_add(Duration::from_secs(2000000000))
1117 .unwrap(),
1118 )
1119 .map_err(VerifyFailed::from)
1120 .unwrap_err(),
1121 VerifyFailed::TooOld
1122 );
1123
1124 let _: AuthCert = res
1126 .clone()
1127 .verify(&[to_rsa_id(t_fingerprint())])
1128 .unwrap()
1129 .if_valid_at(&t_dir_key_expires())
1130 .unwrap();
1131
1132 assert_eq!(
1134 res.clone()
1135 .verify(&[to_rsa_id(t_fingerprint())],)
1136 .unwrap()
1137 .if_valid_at(&(t_dir_key_expires() + Duration::from_secs(1)),)
1138 .map_err(VerifyFailed::from)
1139 .unwrap_err(),
1140 VerifyFailed::TooOld
1141 );
1142
1143 let _: AuthCert = res
1145 .clone()
1146 .verify(&[to_rsa_id(t_fingerprint())])
1147 .unwrap()
1148 .extend_end_bound(Duration::from_secs(1))
1149 .if_valid_at(&(t_dir_key_expires() + Duration::from_secs(1)))
1150 .unwrap();
1151
1152 let alternative_cert = parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(
1154 t_alternative_authcert_raw(),
1155 "",
1156 ))
1157 .unwrap();
1158
1159 let mut res =
1161 parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(AUTHCERT_RAW, "")).unwrap();
1162 res.body.dir_identity_key = alternative_cert.body.dir_identity_key.clone();
1163 assert_eq!(
1164 res.verify(&[to_rsa_id(t_fingerprint())],).unwrap_err(),
1165 VerifyFailed::Inconsistent
1166 );
1167
1168 let mut res =
1170 parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(AUTHCERT_RAW, "")).unwrap();
1171 res.body.dir_key_crosscert = alternative_cert.body.dir_key_crosscert.clone();
1172 assert_eq!(
1173 res.verify(&[to_rsa_id(t_fingerprint())],).unwrap_err(),
1174 VerifyFailed::VerifyFailed
1175 );
1176
1177 let mut res =
1179 parse2::parse_netdoc::<AuthCertUnverified>(&ParseInput::new(AUTHCERT_RAW, "")).unwrap();
1180 res.sigs = alternative_cert.sigs.clone();
1181 assert_eq!(
1182 res.verify(&[to_rsa_id(t_fingerprint())],).unwrap_err(),
1183 VerifyFailed::VerifyFailed
1184 );
1185 }
1186
1187 #[test]
1188 fn keyids_for_directory_signature() -> anyhow::Result<()> {
1189 #[derive(Deftly)]
1190 #[derive_deftly(NetdocEncodable, NetdocParseable)]
1191 struct Doc {
1192 intro: (),
1193 ids: Item,
1194 }
1195 #[derive(Deftly)]
1196 #[derive_deftly(ItemValueEncodable, ItemValueParseable)]
1197 struct Item {
1198 #[deftly(netdoc(with = keyids_directory_signature_args))]
1199 ids: AuthCertKeyIds,
1200 }
1201
1202 let text = r#"intro
1203ids 1234567812345678123456781234567812345678 ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD
1204"#;
1205 let doc = parse2::parse_netdoc::<Doc>(&ParseInput::new(text, "<text>"))?;
1206 let re_encode = encode_netdoc_unsigned([&doc])?;
1207
1208 assert_eq_or_diff!(text, re_encode);
1209 Ok(())
1210 }
1211
1212 #[test]
1213 fn roundtrip() -> Result<(), anyhow::Error> {
1214 let mut rng = test_rng::testing_rng();
1215 let k_auth_id_rsa = rsa::KeyPair::generate(&mut rng)?;
1216 let k_auth_sign_rsa = rsa::KeyPair::generate(&mut rng)?;
1217
1218 let secs = |s| Duration::from_secs(s);
1219 let now = parse_rfc3339("1993-01-01T00:00:00Z")?;
1220 let published = now - secs(1000);
1221 let expires = published + secs(86400);
1222 let tolerance = secs(10);
1223
1224 let input_value = AuthCert {
1225 dir_address: Some("192.0.2.17:7000".parse()?),
1226 ..AuthCert::new_base(&k_auth_id_rsa, &k_auth_sign_rsa, published, expires)?
1227 };
1228 dbg!(&input_value);
1229
1230 let encoded = input_value.encode_sign(&k_auth_id_rsa)?;
1231
1232 let reparsed_uv: AuthCertUnverified =
1233 parse_netdoc(&ParseInput::new(encoded.as_ref(), "<encoded>"))?;
1234 let reparsed_value = reparsed_uv
1235 .verify(&[k_auth_id_rsa.to_public_key().to_rsa_identity()])?
1236 .extend_start_bound(tolerance)
1237 .extend_end_bound(tolerance)
1238 .if_valid_at(&now)?;
1239 dbg!(&reparsed_value);
1240
1241 assert_eq!(input_value, reparsed_value);
1242 Ok(())
1243 }
1244
1245 #[test]
1246 fn parse_authcert() -> anyhow::Result<()> {
1247 let file = "testdata2/cached-certs--1";
1248 let now = parse_rfc3339("2000-06-01T00:00:05Z")?;
1249 let text = fs::read_to_string(file)?;
1250 let input = ParseInput::new(&text, file);
1251 let doc: AuthCertUnverified = parse_netdoc(&input)?;
1252 let doc = doc.verify_selfcert(now)?;
1253 println!("{doc:?}");
1254 assert_eq!(
1255 doc.fingerprint.0.to_string(),
1256 (*raw_find_item_rhs(&text, "fingerprint")
1257 .parse::<Fingerprint>()
1258 .unwrap())
1259 .to_string(),
1260 );
1261 Ok(())
1262 }
1263}