From 145e08d6dcf7c4baae60b844a886598f4bb4ee5a Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Wed, 17 Apr 2024 17:53:34 +0200 Subject: openpgp: Introduce a constructor for ComponentBundle. --- openpgp/src/cert/bundle.rs | 21 ++++++ openpgp/src/cert/parser/low_level/grammar.lalrpop | 90 +++++------------------ 2 files changed, 39 insertions(+), 72 deletions(-) diff --git a/openpgp/src/cert/bundle.rs b/openpgp/src/cert/bundle.rs index 077dfdc0..c5d7ab5b 100644 --- a/openpgp/src/cert/bundle.rs +++ b/openpgp/src/cert/bundle.rs @@ -179,6 +179,27 @@ impl Deref for ComponentBundle } impl ComponentBundle { + /// Creates a new component. + /// + /// Should only be used from the cert parser. However, we cannot + /// use `pub(in ...)` because the cert parser isn't an ancestor of + /// this module. + pub(crate) fn new(component: C, + hash_algo_security: HashAlgoSecurity, + sigs: Vec) + -> ComponentBundle + { + ComponentBundle { + component, + hash_algo_security, + self_signatures: vec![], + certifications: sigs, + attestations: vec![], + self_revocations: vec![], + other_revocations: vec![], + } + } + /// Returns a reference to the bundle's component. /// /// # Examples diff --git a/openpgp/src/cert/parser/low_level/grammar.lalrpop b/openpgp/src/cert/parser/low_level/grammar.lalrpop index f1bf420e..d566d420 100644 --- a/openpgp/src/cert/parser/low_level/grammar.lalrpop +++ b/openpgp/src/cert/parser/low_level/grammar.lalrpop @@ -52,15 +52,7 @@ pub Cert: Option = { let sec = key.hash_algo_security(); let mut cert = Cert { - primary: PrimaryKeyBundle { - component: key, - hash_algo_security: sec, - self_signatures: vec![], - certifications: sigs, - attestations: vec![], - self_revocations: vec![], - other_revocations: vec![], - }, + primary: PrimaryKeyBundle::new(key, sec, sigs), subkeys: ComponentBundles::default(), userids: ComponentBundles::default(), user_attributes: ComponentBundles::default(), @@ -184,25 +176,12 @@ Component: Option = { let sigs = sigs.unwrap(); let sec = key.hash_algo_security(); - Some(Component::SubkeyBundle(SubkeyBundle { - component: key, - hash_algo_security: sec, - self_signatures: vec![], - certifications: sigs, - attestations: vec![], - self_revocations: vec![], - other_revocations: vec![], - })) + Some(Component::SubkeyBundle( + SubkeyBundle::new(key, sec, sigs))) }, - Some(Err(u)) => Some(Component::UnknownBundle(UnknownBundle { - component: u, - hash_algo_security: SecondPreImageResistance, - self_signatures: vec![], - certifications: sigs.unwrap_or_default(), - attestations: vec![], - self_revocations: vec![], - other_revocations: vec![], - })), + Some(Err(u)) => Some(Component::UnknownBundle( + UnknownBundle::new(u, SecondPreImageResistance, + sigs.unwrap_or_default()))), // Just validating a cert... None => None, } @@ -213,25 +192,12 @@ Component: Option = { let sigs = sigs.unwrap(); let sec = u.hash_algo_security(); - Some(Component::UserIDBundle(UserIDBundle { - component: u, - hash_algo_security: sec, - self_signatures: vec![], - certifications: sigs, - attestations: vec![], - self_revocations: vec![], - other_revocations: vec![], - })) + Some(Component::UserIDBundle( + UserIDBundle::new(u, sec, sigs))) }, - Some(Err(u)) => Some(Component::UnknownBundle(UnknownBundle { - component: u, - hash_algo_security: SecondPreImageResistance, - self_signatures: vec![], - certifications: sigs.unwrap_or_default(), - attestations: vec![], - self_revocations: vec![], - other_revocations: vec![], - })), + Some(Err(u)) => Some(Component::UnknownBundle( + UnknownBundle::new(u, SecondPreImageResistance, + sigs.unwrap_or_default()))), // Just validating a cert... None => None, } @@ -242,25 +208,12 @@ Component: Option = { let sigs = sigs.unwrap(); let sec = u.hash_algo_security(); - Some(Component::UserAttributeBundle(UserAttributeBundle { - component: u, - hash_algo_security: sec, - self_signatures: vec![], - certifications: sigs, - attestations: vec![], - self_revocations: vec![], - other_revocations: vec![], - })) + Some(Component::UserAttributeBundle( + UserAttributeBundle::new(u, sec, sigs))) }, - Some(Err(u)) => Some(Component::UnknownBundle(UnknownBundle { - component: u, - hash_algo_security: SecondPreImageResistance, - self_signatures: vec![], - certifications: sigs.unwrap_or_default(), - attestations: vec![], - self_revocations: vec![], - other_revocations: vec![], - })), + Some(Err(u)) => Some(Component::UnknownBundle( + UnknownBundle::new(u, SecondPreImageResistance, + sigs.unwrap_or_default()))), // Just validating a cert... None => None, } @@ -271,15 +224,8 @@ Component: Option = { let sigs = sigs.unwrap(); let sec = u.hash_algo_security(); - Some(Component::UnknownBundle(UnknownBundle { - component: u, - hash_algo_security: sec, - self_signatures: vec![], - certifications: sigs, - attestations: vec![], - self_revocations: vec![], - other_revocations: vec![], - })) + Some(Component::UnknownBundle( + UnknownBundle::new(u, sec, sigs))) }, // Just validating a cert... None => None, -- cgit v1.2.3