diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-11-22 14:26:14 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-11-22 14:26:14 +0100 |
commit | ced195eb1a7d9151640f1ffacd890839a55b8680 (patch) | |
tree | 53001af1ba91361bfa2ed4b585686781983c6603 /openpgp-ffi | |
parent | 28be944bd9f6548b2eee5fc222ffeb52c9d2f8aa (diff) |
openpgp: Make conversions into Key<SecretParts, _> fallible.
- Fixes #380.
Diffstat (limited to 'openpgp-ffi')
-rw-r--r-- | openpgp-ffi/src/common.rs | 24 | ||||
-rw-r--r-- | openpgp-ffi/src/packet/key.rs | 4 | ||||
-rw-r--r-- | openpgp-ffi/src/packet/pkesk.rs | 4 |
3 files changed, 30 insertions, 2 deletions
diff --git a/openpgp-ffi/src/common.rs b/openpgp-ffi/src/common.rs index 078c035e..7c615bab 100644 --- a/openpgp-ffi/src/common.rs +++ b/openpgp-ffi/src/common.rs @@ -164,6 +164,30 @@ macro_rules! ffi_make_fry_from_errp { /// Like try! for ffi glue. /// + /// Evaluates the given expression. `Ok(v)` evaluates to `v`. + /// On failure, stashes the error in the context and returns + /// the appropriate Status code. + #[allow(unused_macros)] + macro_rules! ffi_try_or_status { + ($expr:expr) => { + match $expr { + Ok(v) => v, + Err(e) => { + use crate::MoveIntoRaw; + use failure::Error; + let status = crate::error::Status::from(&e); + if let Some(errp) = $errp { + let e : Error = e.into(); + *errp = e.move_into_raw(); + } + return status; + }, + } + }; + } + + /// Like try! for ffi glue. + /// /// Unwraps the given expression. On failure, stashes the /// error in the context and returns $or. #[allow(unused_macros)] diff --git a/openpgp-ffi/src/packet/key.rs b/openpgp-ffi/src/packet/key.rs index e0eead22..7f08d040 100644 --- a/openpgp-ffi/src/packet/key.rs +++ b/openpgp-ffi/src/packet/key.rs @@ -4,6 +4,8 @@ //! //! [Section 5.5 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.5 +use std::convert::TryInto; + use libc::{c_int, time_t}; extern crate sequoia_openpgp as openpgp; @@ -79,6 +81,6 @@ fn pgp_key_into_key_pair(errp: Option<&mut *mut crate::error::Error>, { ffi_make_fry_from_errp!(errp); let key : self::openpgp::packet::key::UnspecifiedSecret - = key.move_from_raw().into(); + = ffi_try!(key.move_from_raw().try_into()); ffi_try_box!(key.into_keypair()) } diff --git a/openpgp-ffi/src/packet/pkesk.rs b/openpgp-ffi/src/packet/pkesk.rs index 8f844d6c..82227065 100644 --- a/openpgp-ffi/src/packet/pkesk.rs +++ b/openpgp-ffi/src/packet/pkesk.rs @@ -44,7 +44,9 @@ pub extern "C" fn pgp_pkesk_decrypt(errp: Option<&mut *mut crate::error::Error>, let algo = ffi_param_ref_mut!(algo); let key_len = ffi_param_ref_mut!(key_len); - match secret_key.clone().mark_parts_secret().into_keypair() { + match ffi_try_or_status!(secret_key.clone().mark_parts_secret()) + .into_keypair() + { Ok(mut keypair) => { match pkesk.decrypt(&mut keypair) { Ok((a, k)) => { |