diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-04-08 15:45:50 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-04-08 19:17:19 +0200 |
commit | 759074ef27a8e5c7832c22cc6d110d2862d39a44 (patch) | |
tree | 7f760130638c4fd6d94ca5a36c7266a66aa041ce | |
parent | 59588d78046c3d3a0b66ce104341fc0638aaeb0a (diff) |
openpgp: Move methods mapping and inspect to VerificationHelper.
-rw-r--r-- | openpgp-ffi/include/sequoia/openpgp.h | 2 | ||||
-rw-r--r-- | openpgp-ffi/src/parse/stream.rs | 45 | ||||
-rw-r--r-- | openpgp/src/parse/stream.rs | 40 | ||||
-rw-r--r-- | tool/src/commands/decrypt.rs | 18 |
4 files changed, 55 insertions, 50 deletions
diff --git a/openpgp-ffi/include/sequoia/openpgp.h b/openpgp-ffi/include/sequoia/openpgp.h index 8651127c..e0657812 100644 --- a/openpgp-ffi/include/sequoia/openpgp.h +++ b/openpgp-ffi/include/sequoia/openpgp.h @@ -1961,6 +1961,7 @@ pgp_reader_t pgp_verifier_new (pgp_error_t *errp, pgp_reader_t input, pgp_decryptor_get_certs_cb_t get_certs, pgp_decryptor_check_cb_t check, + pgp_decryptor_inspect_cb_t inspect, void *cookie, time_t time); /*/ @@ -1971,6 +1972,7 @@ pgp_detached_verifier_t pgp_detached_verifier_new (pgp_error_t *errp, pgp_reader_t signature_input, pgp_decryptor_get_certs_cb_t get_certs, pgp_decryptor_check_cb_t check, + pgp_decryptor_inspect_cb_t inspect, void *cookie, time_t time); /*/ diff --git a/openpgp-ffi/src/parse/stream.rs b/openpgp-ffi/src/parse/stream.rs index ae5bbe43..fab833c9 100644 --- a/openpgp-ffi/src/parse/stream.rs +++ b/openpgp-ffi/src/parse/stream.rs @@ -429,18 +429,21 @@ type CheckCallback = fn(*mut HelperCookie, // This fetches keys and computes the validity of the verification. struct VHelper { + inspect_cb: Option<InspectCallback>, get_certs_cb: GetPublicKeysCallback, check_signatures_cb: CheckCallback, cookie: *mut HelperCookie, } impl VHelper { - fn new(get_certs: GetPublicKeysCallback, + fn new(inspect_cb: Option<InspectCallback>, + get_certs: GetPublicKeysCallback, check_signatures: CheckCallback, cookie: *mut HelperCookie) -> Self { VHelper { + inspect_cb, get_certs_cb: get_certs, check_signatures_cb: check_signatures, cookie, @@ -449,6 +452,19 @@ impl VHelper { } impl VerificationHelper for VHelper { + fn inspect(&mut self, pp: &PacketParser) -> openpgp::Result<()> { + if let Some(cb) = self.inspect_cb { + match cb(self.cookie, pp) { + Status::Success => Ok(()), + // XXX: Convert the status to an error better. + status => Err(anyhow::anyhow!( + "Inspect Callback returned an error: {:?}", status).into()), + } + } else { + Ok(()) + } + } + fn get_certs(&mut self, ids: &[openpgp::KeyHandle]) -> Result<Vec<openpgp::Cert>, anyhow::Error> { @@ -600,7 +616,7 @@ impl VerificationHelper for VHelper { /// .key = cert, /* Move. */ /// }; /// plaintext = pgp_verifier_new (NULL, policy, source, -/// get_certs_cb, check_cb, +/// get_certs_cb, check_cb, NULL, /// &cookie, 1554542219); /// assert (source); /// @@ -621,12 +637,13 @@ fn pgp_verifier_new<'a>(errp: Option<&mut *mut crate::error::Error>, input: *mut io::Reader, get_certs: GetPublicKeysCallback, check: CheckCallback, + inspect: Option<InspectCallback>, cookie: *mut HelperCookie, time: time_t) -> Maybe<io::Reader> { let policy = policy.ref_raw().as_ref(); - let helper = VHelper::new(get_certs, check, cookie); + let helper = VHelper::new(inspect, get_certs, check, cookie); Verifier::from_reader(policy, input.ref_mut_raw(), helper, maybe_time(time)) .map(|r| io::ReaderKind::Generic(Box::new(r))) @@ -728,7 +745,7 @@ pub struct DetachedVerifier(openpgp::parse::stream::DetachedVerifier<'static, VH /// .key = cert, /* Move. */ /// }; /// verifier = pgp_detached_verifier_new (NULL, policy, signature, -/// get_certs_cb, check_cb, +/// get_certs_cb, check_cb, NULL, /// &cookie, 1554542219); /// assert (verifier); /// @@ -748,13 +765,14 @@ fn pgp_detached_verifier_new<'a>(errp: Option<&mut *mut crate::error::Error>, signature_input: *mut io::Reader, get_certs: GetPublicKeysCallback, check: CheckCallback, + inspect: Option<InspectCallback>, cookie: *mut HelperCookie, time: time_t) -> Maybe<DetachedVerifier> { let policy = policy.ref_raw().as_ref(); - let helper = VHelper::new(get_certs, check, cookie); + let helper = VHelper::new(inspect, get_certs, check, cookie); openpgp::parse::stream::DetachedVerifier::from_reader( policy, signature_input.ref_mut_raw(), helper, maybe_time(time)) @@ -778,7 +796,6 @@ fn pgp_detached_verifier_verify(errp: Option<&mut *mut crate::error::Error>, struct DHelper { vhelper: VHelper, - inspect_cb: Option<InspectCallback>, decrypt_cb: DecryptCallback, } @@ -791,8 +808,7 @@ impl DHelper { -> Self { DHelper { - vhelper: VHelper::new(get_certs, check, cookie), - inspect_cb: inspect, + vhelper: VHelper::new(inspect, get_certs, check, cookie), decrypt_cb: decrypt, } } @@ -813,19 +829,6 @@ impl VerificationHelper for DHelper { } impl DecryptionHelper for DHelper { - fn inspect(&mut self, pp: &PacketParser) -> openpgp::Result<()> { - if let Some(cb) = self.inspect_cb { - match cb(self.vhelper.cookie, pp) { - Status::Success => Ok(()), - // XXX: Convert the status to an error better. - status => Err(anyhow::anyhow!( - "Inspect Callback returned an error: {:?}", status).into()), - } - } else { - Ok(()) - } - } - fn decrypt<D>(&mut self, pkesks: &[PKESK], skesks: &[SKESK], sym_algo: Option<SymmetricAlgorithm>, mut decrypt: D) diff --git a/openpgp/src/parse/stream.rs b/openpgp/src/parse/stream.rs index 81e73094..55e2fbed 100644 --- a/openpgp/src/parse/stream.rs +++ b/openpgp/src/parse/stream.rs @@ -405,6 +405,26 @@ enum IMessageLayer { /// Helper for signature verification. 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. The default implementation returns false. + fn mapping(&self) -> bool { + false + } + + /// Inspects the message. + /// + /// Called once per packet. Can be used to dump packets in + /// encrypted messages. The default implementation does nothing. + fn inspect(&mut self, pp: &PacketParser) -> Result<()> { + // Do nothing. + let _ = pp; + Ok(()) + } + /// Retrieves the certificates containing the specified keys. /// /// When implementing this method, you should return as many @@ -939,26 +959,6 @@ pub struct Decryptor<'a, H: VerificationHelper + DecryptionHelper> { /// Helper for decrypting messages. pub trait DecryptionHelper { - /// 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. The default implementation returns false. - fn mapping(&self) -> bool { - false - } - - /// Inspects the message. - /// - /// Called once per packet. Can be used to dump packets in - /// encrypted messages. The default implementation does nothing. - fn inspect(&mut self, pp: &PacketParser) -> Result<()> { - // Do nothing. - let _ = pp; - Ok(()) - } - /// Decrypts the message. /// /// This function is called with every `PKESK` and `SKESK` found diff --git a/tool/src/commands/decrypt.rs b/tool/src/commands/decrypt.rs index 2a04a1bd..538636fb 100644 --- a/tool/src/commands/decrypt.rs +++ b/tool/src/commands/decrypt.rs @@ -114,15 +114,6 @@ impl<'a> Helper<'a> { } impl<'a> VerificationHelper for Helper<'a> { - fn get_certs(&mut self, ids: &[openpgp::KeyHandle]) -> Result<Vec<Cert>> { - self.vhelper.get_certs(ids) - } - fn check(&mut self, structure: MessageStructure) -> Result<()> { - self.vhelper.check(structure) - } -} - -impl<'a> DecryptionHelper for Helper<'a> { fn mapping(&self) -> bool { self.hex } @@ -137,6 +128,15 @@ impl<'a> DecryptionHelper for Helper<'a> { Ok(()) } + fn get_certs(&mut self, ids: &[openpgp::KeyHandle]) -> Result<Vec<Cert>> { + self.vhelper.get_certs(ids) + } + fn check(&mut self, structure: MessageStructure) -> Result<()> { + self.vhelper.check(structure) + } +} + +impl<'a> DecryptionHelper for Helper<'a> { fn decrypt<D>(&mut self, pkesks: &[PKESK], skesks: &[SKESK], sym_algo: Option<SymmetricAlgorithm>, mut decrypt: D) -> openpgp::Result<Option<Fingerprint>> |