diff options
-rw-r--r-- | openpgp/src/parse/stream.rs | 77 | ||||
-rw-r--r-- | tool/src/commands/decrypt.rs | 15 |
2 files changed, 62 insertions, 30 deletions
diff --git a/openpgp/src/parse/stream.rs b/openpgp/src/parse/stream.rs index 8ed01f94..2f1747e9 100644 --- a/openpgp/src/parse/stream.rs +++ b/openpgp/src/parse/stream.rs @@ -509,21 +509,6 @@ enum IMessageLayer { /// /// [`Map`]: ../map/struct.Map.html pub trait VerificationHelper { - /// Turns mapping on or off. - /// - /// If this function returns true, the packet parser will create a - /// [`Map`] of the packets. Note that this buffers the packets - /// contents, and is not recommended unless you know that the - /// packets are small. This is called once before parsing the - /// first packet. - /// - /// [`Map`]: ../map/struct.Map.html - /// - /// The default implementation returns false. - fn mapping(&self) -> bool { - false - } - /// Inspects the message. /// /// Called once per packet. Can be used to inspect and dump @@ -782,6 +767,7 @@ pub struct Verifier<'a, H: VerificationHelper> { /// [`VerifierBuilder::with_policy`]: struct.VerifierBuilder.html#method.with_policy pub struct VerifierBuilder<'a> { message: Box<dyn BufferedReader<Cookie> + 'a>, + mapping: bool, } impl<'a> Parse<'a, VerifierBuilder<'a>> @@ -815,9 +801,25 @@ impl<'a> VerifierBuilder<'a> { { Ok(VerifierBuilder { message: Box::new(signatures), + mapping: false, }) } + /// Enables mapping. + /// + /// If mapping is enabled, the packet parser will create a [`Map`] + /// of the packets that can be inspected in + /// [`VerificationHelper::inspect`]. Note that this buffers the + /// packets contents, and is not recommended unless you know that + /// the packets are small. + /// + /// [`Map`]: ../map/struct.Map.html + /// [`VerificationHelper::inspect`]: trait.VerificationHelper.html#tymethod.inspect + pub fn mapping(mut self, enabled: bool) -> Self { + self.mapping = enabled; + self + } + /// Creates the `Verifier`. /// /// Signature verifications are done under the given `policy` and @@ -837,7 +839,7 @@ impl<'a> VerifierBuilder<'a> { policy, self.message, NoDecryptionHelper { v: helper, }, - t, Mode::Verify)?, + t, Mode::Verify, self.mapping)?, }) } } @@ -929,6 +931,7 @@ pub struct DetachedVerifier<'a, H: VerificationHelper> { /// [`DetachedVerifierBuilder::with_policy`]: struct.DetachedVerifierBuilder.html#method.with_policy pub struct DetachedVerifierBuilder<'a> { signatures: Box<dyn BufferedReader<Cookie> + 'a>, + mapping: bool, } impl<'a> Parse<'a, DetachedVerifierBuilder<'a>> @@ -962,9 +965,25 @@ impl<'a> DetachedVerifierBuilder<'a> { { Ok(DetachedVerifierBuilder { signatures: Box::new(signatures), + mapping: false, }) } + /// Enables mapping. + /// + /// If mapping is enabled, the packet parser will create a [`Map`] + /// of the packets that can be inspected in + /// [`VerificationHelper::inspect`]. Note that this buffers the + /// packets contents, and is not recommended unless you know that + /// the packets are small. + /// + /// [`Map`]: ../map/struct.Map.html + /// [`VerificationHelper::inspect`]: trait.VerificationHelper.html#tymethod.inspect + pub fn mapping(mut self, enabled: bool) -> Self { + self.mapping = enabled; + self + } + /// Creates the `DetachedVerifier`. /// /// Signature verifications are done under the given `policy` and @@ -984,7 +1003,7 @@ impl<'a> DetachedVerifierBuilder<'a> { policy, self.signatures, NoDecryptionHelper { v: helper, }, - t, Mode::VerifyDetached)?, + t, Mode::VerifyDetached, self.mapping)?, }) } } @@ -1157,6 +1176,7 @@ pub struct Decryptor<'a, H: VerificationHelper + DecryptionHelper> { /// [`DecryptorBuilder::with_policy`]: struct.DecryptorBuilder.html#method.with_policy pub struct DecryptorBuilder<'a> { message: Box<dyn BufferedReader<Cookie> + 'a>, + mapping: bool, } impl<'a> Parse<'a, DecryptorBuilder<'a>> @@ -1190,9 +1210,25 @@ impl<'a> DecryptorBuilder<'a> { { Ok(DecryptorBuilder { message: Box::new(signatures), + mapping: false, }) } + /// Enables mapping. + /// + /// If mapping is enabled, the packet parser will create a [`Map`] + /// of the packets that can be inspected in + /// [`VerificationHelper::inspect`]. Note that this buffers the + /// packets contents, and is not recommended unless you know that + /// the packets are small. + /// + /// [`Map`]: ../map/struct.Map.html + /// [`VerificationHelper::inspect`]: trait.VerificationHelper.html#tymethod.inspect + pub fn mapping(mut self, enabled: bool) -> Self { + self.mapping = enabled; + self + } + /// Creates the `Decryptor`. /// /// Signature verifications are done under the given `policy` and @@ -1213,7 +1249,7 @@ impl<'a> DecryptorBuilder<'a> { policy, self.message, helper, - t, Mode::Decrypt) + t, Mode::Decrypt, self.mapping) } } @@ -1399,7 +1435,8 @@ impl<'a, H: VerificationHelper + DecryptionHelper> Decryptor<'a, H> { policy: &'a dyn Policy, bio: Box<dyn BufferedReader<Cookie> + 'a>, helper: H, time: T, - mode: Mode) + mode: Mode, + mapping: bool) -> Result<Decryptor<'a, H>> where T: Into<Option<time::SystemTime>> { @@ -1413,7 +1450,7 @@ impl<'a, H: VerificationHelper + DecryptionHelper> Decryptor<'a, H> { let time = time.unwrap_or_else(time::SystemTime::now); let mut ppr = PacketParserBuilder::from_buffered_reader(bio)? - .map(helper.mapping()).build()?; + .map(mapping).build()?; let mut v = Decryptor { helper, diff --git a/tool/src/commands/decrypt.rs b/tool/src/commands/decrypt.rs index 1b228a7f..9e951af4 100644 --- a/tool/src/commands/decrypt.rs +++ b/tool/src/commands/decrypt.rs @@ -33,14 +33,13 @@ struct Helper<'a> { key_hints: HashMap<KeyID, String>, dump_session_key: bool, dumper: Option<PacketDumper>, - hex: bool, } impl<'a> Helper<'a> { fn new(ctx: &'a Context, policy: &'a dyn Policy, mapping: &'a mut store::Mapping, signatures: usize, certs: Vec<Cert>, secrets: Vec<Cert>, - dump_session_key: bool, dump: bool, hex: bool) + dump_session_key: bool, dump: bool) -> Self { let mut keys = HashMap::new(); @@ -74,14 +73,13 @@ impl<'a> Helper<'a> { key_identities: identities, key_hints: hints, dump_session_key: dump_session_key, - dumper: if dump || hex { + dumper: if dump { let width = terminal::size().ok().map(|(cols, _)| cols as usize) .unwrap_or(80); Some(PacketDumper::new(width, false)) } else { None }, - hex: hex, } } @@ -116,10 +114,6 @@ impl<'a> Helper<'a> { } impl<'a> VerificationHelper for Helper<'a> { - fn mapping(&self) -> bool { - self.hex - } - fn inspect(&mut self, pp: &PacketParser) -> Result<()> { if let Some(dumper) = self.dumper.as_mut() { dumper.packet(&mut io::stderr(), @@ -287,8 +281,9 @@ pub fn decrypt(ctx: &Context, policy: &dyn Policy, mapping: &mut store::Mapping, dump: bool, hex: bool) -> Result<()> { let helper = Helper::new(ctx, policy, mapping, signatures, certs, secrets, - dump_session_key, dump, hex); + dump_session_key, dump || hex); let mut decryptor = DecryptorBuilder::from_reader(input)? + .mapping(hex) .with_policy(policy, None, helper) .context("Decryption failed")?; @@ -309,7 +304,7 @@ pub fn decrypt_unwrap(ctx: &Context, policy: &dyn Policy, -> Result<()> { let mut helper = Helper::new(ctx, policy, mapping, 0, Vec::new(), secrets, - dump_session_key, false, false); + dump_session_key, false); let mut ppr = PacketParser::from_reader(input)?; |