diff options
author | Neal H. Walfield <neal@pep.foundation> | 2019-05-15 22:34:34 +0200 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2019-05-15 22:37:57 +0200 |
commit | 1687fa2e4fda73791d53134cb5541fa0a9412268 (patch) | |
tree | 2f48863983e5fdb243ba1aacc4c3d3b107f8e4e8 /openpgp-ffi/src/parse/stream.rs | |
parent | a25ef6974e0ba3989f6205c19a1f9ccfc81db584 (diff) |
openpgp-ffi: Expose the inspect callback in the C API
- DecryptionHelper has a default NULL implementation of the inspect
callback. Allow C code to override it by exposing it in the
pgp_decryptor_new API.
Diffstat (limited to 'openpgp-ffi/src/parse/stream.rs')
-rw-r--r-- | openpgp-ffi/src/parse/stream.rs | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/openpgp-ffi/src/parse/stream.rs b/openpgp-ffi/src/parse/stream.rs index 5b8ed62b..b887f5e6 100644 --- a/openpgp-ffi/src/parse/stream.rs +++ b/openpgp-ffi/src/parse/stream.rs @@ -48,6 +48,7 @@ use super::super::{ tpk::TPK, packet::signature::Signature, packet::key::Key, + parse::PacketParser, revocation_status::RevocationStatus, }; @@ -278,6 +279,12 @@ type GetPublicKeysCallback = fn(*mut HelperCookie, &mut *mut *mut TPK, *mut usize, *mut FreeCallback) -> Status; +/// Inspect packets as they are decrypted. +/// +/// This function is called on every packet that the decryptor +/// observes. +type InspectCallback = fn(*mut HelperCookie, *const PacketParser) -> Status; + /// Decrypts the message. /// /// This function is called with every `PKESK` and `SKESK` found in @@ -638,6 +645,7 @@ fn pgp_detached_verifier_new<'a>(errp: Option<&mut *mut ::error::Error>, struct DHelper { vhelper: VHelper, + inspect_cb: Option<InspectCallback>, decrypt_cb: DecryptCallback, } @@ -645,11 +653,13 @@ impl DHelper { fn new(get_public_keys: GetPublicKeysCallback, decrypt: DecryptCallback, check: CheckCallback, + inspect: Option<InspectCallback>, cookie: *mut HelperCookie) -> Self { DHelper { vhelper: VHelper::new(get_public_keys, check, cookie), + inspect_cb: inspect, decrypt_cb: decrypt, } } @@ -670,6 +680,19 @@ impl VerificationHelper for DHelper { } impl DecryptionHelper for DHelper { + fn inspect(&mut self, pp: &PacketParser) -> failure::Fallible<()> { + 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(failure::format_err!( + "Inspect Callback returned an error: {:?}", status).into()), + } + } else { + Ok(()) + } + } + fn decrypt<D>(&mut self, pkesks: &[PKESK], skesks: &[SKESK], mut decrypt: D) -> openpgp::Result<Option<openpgp::Fingerprint>> @@ -860,7 +883,7 @@ impl DecryptionHelper for DHelper { /// }; /// plaintext = pgp_decryptor_new (NULL, source, /// get_public_keys_cb, decrypt_cb, -/// check_cb, &cookie, 1554542219); +/// check_cb, NULL, &cookie, 1554542219); /// assert (plaintext); /// /// nread = pgp_reader_read (NULL, plaintext, buf, sizeof buf); @@ -880,12 +903,13 @@ fn pgp_decryptor_new<'a>(errp: Option<&mut *mut ::error::Error>, get_public_keys: GetPublicKeysCallback, decrypt: DecryptCallback, check: CheckCallback, + inspect: Option<InspectCallback>, cookie: *mut HelperCookie, time: time_t) -> Maybe<io::Reader> { let helper = DHelper::new( - get_public_keys, decrypt, check, cookie); + get_public_keys, decrypt, check, inspect, cookie); Decryptor::from_reader(input.ref_mut_raw(), helper, maybe_time(time)) .map(|r| io::ReaderKind::Generic(Box::new(r))) |