From e8d2b7cd2aad554e52e1b0a1fcf7a5647562e327 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Wed, 16 Jan 2019 15:56:06 +0100 Subject: ffi: Rework complex error handling macros. - Introduce a macro that emits local macros that implicitly use the given context to store complex errors. - This prepares us to decouple error handling from contexts, at least for the functions that otherwise do not use the context. --- ffi/src/core.rs | 12 +++-- ffi/src/lib.rs | 110 +++++++++++++++++++++++------------------ ffi/src/net.rs | 17 ++++--- ffi/src/openpgp/armor.rs | 7 ++- ffi/src/openpgp/crypto.rs | 3 +- ffi/src/openpgp/mod.rs | 64 ++++++++++++++++-------- ffi/src/openpgp/packet_pile.rs | 12 +++-- ffi/src/openpgp/tpk.rs | 36 +++++++++----- ffi/src/openpgp/tsk.rs | 6 ++- ffi/src/store.rs | 69 +++++++++++++++++--------- 10 files changed, 212 insertions(+), 124 deletions(-) (limited to 'ffi') diff --git a/ffi/src/core.rs b/ffi/src/core.rs index 5f545702..de44a8bd 100644 --- a/ffi/src/core.rs +++ b/ffi/src/core.rs @@ -246,8 +246,9 @@ pub extern "system" fn sq_reader_from_file(ctx: *mut Context, filename: *const c_char) -> *mut Box { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned(); - fry_box!(ctx, File::open(Path::new(&filename)) + ffi_try_box!(File::open(Path::new(&filename)) .map(|r| Box::new(r)) .map_err(|e| e.into())) } @@ -285,12 +286,13 @@ pub extern "system" fn sq_reader_read(ctx: *mut Context, buf: *mut uint8_t, len: size_t) -> ssize_t { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let reader = ffi_param_ref_mut!(reader); assert!(!buf.is_null()); let buf = unsafe { slice::from_raw_parts_mut(buf, len as usize) }; - fry_or!(ctx, reader.read(buf).map_err(|e| e.into()), -1) as ssize_t + ffi_try_or!(reader.read(buf).map_err(|e| e.into()), -1) as ssize_t } @@ -303,8 +305,9 @@ pub extern "system" fn sq_writer_from_file(ctx: *mut Context, filename: *const c_char) -> *mut Box { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned(); - fry_box!(ctx, File::create(Path::new(&filename)) + ffi_try_box!(File::create(Path::new(&filename)) .map(|r| Box::new(r)) .map_err(|e| e.into())) } @@ -396,10 +399,11 @@ pub extern "system" fn sq_writer_write(ctx: *mut Context, buf: *const uint8_t, len: size_t) -> ssize_t { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let writer = ffi_param_ref_mut!(writer); assert!(!buf.is_null()); let buf = unsafe { slice::from_raw_parts(buf, len as usize) }; - fry_or!(ctx, writer.write(buf).map_err(|e| e.into()), -1) as ssize_t + ffi_try_or!(writer.write(buf).map_err(|e| e.into()), -1) as ssize_t } diff --git a/ffi/src/lib.rs b/ffi/src/lib.rs index a2b8269c..f50c5d36 100644 --- a/ffi/src/lib.rs +++ b/ffi/src/lib.rs @@ -259,60 +259,72 @@ macro_rules! ffi_return_maybe_string { }}; } -/// Like try! for ffi glue. -/// -/// Evaluates the given expression. On success, evaluate to -/// `Status.Success`. On failure, stashes the error in the context and -/// evaluates to the appropriate Status code. -macro_rules! fry_status { - ($ctx:expr, $expr:expr) => { - match $expr { - Ok(_) => Status::Success, - Err(e) => { - let status = Status::from(&e); - $ctx.e = Some(e); - status - }, +/* Error handling with implicit context. */ + +/// Emits local macros for error handling that use the given context +/// to store complex errors. +macro_rules! ffi_make_fry_from_ctx { + ($ctx:ident) => { + /// Like try! for ffi glue. + /// + /// Evaluates the given expression. On success, evaluate to + /// `Status.Success`. On failure, stashes the error in the + /// context and evaluates to the appropriate Status code. + #[allow(unused_macros)] + macro_rules! ffi_try_status { + ($expr:expr) => { + match $expr { + Ok(_) => Status::Success, + Err(e) => { + let status = Status::from(&e); + $ctx.e = Some(e); + status + }, + } + }; } - }; -} -/// Like try! for ffi glue. -/// -/// Unwraps the given expression. On failure, stashes the error in -/// the context and returns $or. -macro_rules! fry_or { - ($ctx:expr, $expr:expr, $or:expr) => { - match $expr { - Ok(v) => v, - Err(e) => { - $ctx.e = Some(e); - return $or; - }, + /// Like try! for ffi glue. + /// + /// Unwraps the given expression. On failure, stashes the + /// error in the context and returns $or. + #[allow(unused_macros)] + macro_rules! ffi_try_or { + ($expr:expr, $or:expr) => { + match $expr { + Ok(v) => v, + Err(e) => { + $ctx.e = Some(e); + return $or; + }, + } + }; } - }; -} -/// Like try! for ffi glue. -/// -/// Unwraps the given expression. On failure, stashes the error in -/// the context and returns NULL. -macro_rules! fry { - ($ctx:expr, $expr:expr) => { - fry_or!($ctx, $expr, ::std::ptr::null_mut()) - }; -} + /// Like try! for ffi glue. + /// + /// Unwraps the given expression. On failure, stashes the + /// error in the context and returns NULL. + #[allow(unused_macros)] + macro_rules! ffi_try { + ($expr:expr) => { + ffi_try_or!($expr, ::std::ptr::null_mut()) + }; + } -/// Like try! for ffi glue, then box into raw pointer. -/// -/// This is used to transfer ownership from Rust to C. -/// -/// Unwraps the given expression. On success, it boxes the value -/// and turns it into a raw pointer. On failure, stashes the -/// error in the context and returns NULL. -macro_rules! fry_box { - ($ctx:expr, $expr:expr) => { - Box::into_raw(Box::new(fry!($ctx, $expr))) + /// Like try! for ffi glue, then box into raw pointer. + /// + /// This is used to transfer ownership from Rust to C. + /// + /// Unwraps the given expression. On success, it boxes the + /// value and turns it into a raw pointer. On failure, + /// stashes the error in the context and returns NULL. + #[allow(unused_macros)] + macro_rules! ffi_try_box { + ($expr:expr) => { + Box::into_raw(Box::new(ffi_try!($expr))) + } + } } } diff --git a/ffi/src/net.rs b/ffi/src/net.rs index 7d1558f6..ab0800eb 100644 --- a/ffi/src/net.rs +++ b/ffi/src/net.rs @@ -51,9 +51,10 @@ use super::core::Context; pub extern "system" fn sq_keyserver_new(ctx: *mut Context, uri: *const c_char) -> *mut KeyServer { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let uri = ffi_param_cstr!(uri).to_string_lossy(); - fry_box!(ctx, KeyServer::new(&ctx.c, &uri)) + ffi_try_box!(KeyServer::new(&ctx.c, &uri)) } /// Returns a handle for the given URI. @@ -69,6 +70,7 @@ pub extern "system" fn sq_keyserver_with_cert(ctx: *mut Context, cert: *const uint8_t, len: size_t) -> *mut KeyServer { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let uri = ffi_param_cstr!(uri).to_string_lossy(); if cert.is_null() { @@ -79,9 +81,9 @@ pub extern "system" fn sq_keyserver_with_cert(ctx: *mut Context, slice::from_raw_parts(cert, len as usize) }; - let cert = fry!(ctx, Certificate::from_der(cert) + let cert = ffi_try!(Certificate::from_der(cert) .map_err(|e| e.into())); - fry_box!(ctx, KeyServer::with_cert(&ctx.c, &uri, cert)) + ffi_try_box!(KeyServer::with_cert(&ctx.c, &uri, cert)) } /// Returns a handle for the SKS keyserver pool. @@ -95,7 +97,8 @@ pub extern "system" fn sq_keyserver_with_cert(ctx: *mut Context, pub extern "system" fn sq_keyserver_sks_pool(ctx: *mut Context) -> *mut KeyServer { let ctx = ffi_param_ref_mut!(ctx); - fry_box!(ctx, KeyServer::sks_pool(&ctx.c)) + ffi_make_fry_from_ctx!(ctx); + ffi_try_box!(KeyServer::sks_pool(&ctx.c)) } /// Frees a keyserver object. @@ -113,10 +116,11 @@ pub extern "system" fn sq_keyserver_get(ctx: *mut Context, id: *const KeyID) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let ks = ffi_param_ref_mut!(ks); let id = ffi_param_ref!(id); - fry_box!(ctx, ks.get(&id)) + ffi_try_box!(ks.get(&id)) } /// Sends the given key to the server. @@ -128,8 +132,9 @@ pub extern "system" fn sq_keyserver_send(ctx: *mut Context, tpk: *const TPK) -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let ks = ffi_param_ref_mut!(ks); let tpk = ffi_param_ref!(tpk); - fry_status!(ctx, ks.send(tpk)) + ffi_try_status!(ks.send(tpk)) } diff --git a/ffi/src/openpgp/armor.rs b/ffi/src/openpgp/armor.rs index 3680b74e..7616a03d 100644 --- a/ffi/src/openpgp/armor.rs +++ b/ffi/src/openpgp/armor.rs @@ -145,10 +145,11 @@ pub extern "system" fn sq_armor_reader_from_file(ctx: *mut Context, kind: c_int) -> *mut Box { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned(); let kind = int_to_kind(kind); - fry_box!(ctx, armor::Reader::from_file(&filename, kind) + ffi_try_box!(armor::Reader::from_file(&filename, kind) .map(|r| Box::new(r)) .map_err(|e| e.into())) } @@ -214,6 +215,7 @@ pub extern "system" fn sq_armor_reader_headers(ctx: *mut Context, len: *mut size_t) -> *mut ArmorHeader { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let len = ffi_param_ref_mut!(len); // We need to downcast `reader`. To do that, we need to do a @@ -349,6 +351,7 @@ pub extern "system" fn sq_armor_writer_new -> *mut Box { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let inner = ffi_param_ref_mut!(inner); let kind = int_to_kind(kind).expect("KIND must not be SQ_ARMOR_KIND_ANY"); @@ -369,7 +372,7 @@ pub extern "system" fn sq_armor_writer_new let header: Vec<(&str, &str)> = header_.iter().map(|h| (h.0.as_ref(), h.1.as_ref())).collect(); - fry_box!(ctx, armor::Writer::new(inner, kind, &header) + ffi_try_box!(armor::Writer::new(inner, kind, &header) .map(|r| Box::new(r)) .map_err(|e| e.into())) } diff --git a/ffi/src/openpgp/crypto.rs b/ffi/src/openpgp/crypto.rs index b4da7553..0055d68f 100644 --- a/ffi/src/openpgp/crypto.rs +++ b/ffi/src/openpgp/crypto.rs @@ -27,9 +27,10 @@ pub extern "system" fn sq_key_pair_new -> *mut crypto::KeyPair { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let public = ffi_param_move!(public); let secret = ffi_param_move!(secret); - fry_box!(ctx, crypto::KeyPair::new(*public, *secret)) + ffi_try_box!(crypto::KeyPair::new(*public, *secret)) } /// Frees a key pair. diff --git a/ffi/src/openpgp/mod.rs b/ffi/src/openpgp/mod.rs index 3f74263b..8dba85a1 100644 --- a/ffi/src/openpgp/mod.rs +++ b/ffi/src/openpgp/mod.rs @@ -459,8 +459,9 @@ pub extern "system" fn sq_p_key_into_key_pair(ctx: *mut Context, -> *mut self::openpgp::crypto::KeyPair { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let key = ffi_param_move!(key); - fry_box!(ctx, key.into_keypair()) + ffi_try_box!(key.into_keypair()) } /// Returns the value of the User ID Packet. @@ -517,6 +518,7 @@ pub extern "system" fn sq_skesk_decrypt(ctx: *mut Context, key_len: *mut size_t) -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let skesk = ffi_param_ref!(skesk); assert!(!password.is_null()); let password = unsafe { @@ -539,7 +541,7 @@ pub extern "system" fn sq_skesk_decrypt(ctx: *mut Context, *key_len = k.len(); Status::Success }, - Err(e) => fry_status!(ctx, Err::<(), failure::Error>(e)), + Err(e) => ffi_try_status!(Err::<(), failure::Error>(e)), } } else { panic!("Not a SKESK packet"); @@ -572,6 +574,7 @@ pub extern "system" fn sq_pkesk_decrypt(ctx: *mut Context, key_len: *mut size_t) -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let pkesk = ffi_param_ref!(pkesk); let secret_key = ffi_param_ref!(secret_key); let algo = ffi_param_ref_mut!(algo); @@ -591,7 +594,7 @@ pub extern "system" fn sq_pkesk_decrypt(ctx: *mut Context, *key_len = k.len(); Status::Success }, - Err(e) => fry_status!(ctx, Err::<(), failure::Error>(e)), + Err(e) => ffi_try_status!(Err::<(), failure::Error>(e)), } } else { // XXX: Better message. @@ -611,8 +614,9 @@ pub extern "system" fn sq_packet_parser_from_reader<'a> (ctx: *mut Context, reader: *mut Box<'a + Read>) -> *mut PacketParserResult<'a> { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let reader = ffi_param_ref_mut!(reader); - fry_box!(ctx, PacketParser::from_reader(reader)) + ffi_try_box!(PacketParser::from_reader(reader)) } /// Starts parsing OpenPGP packets stored in a file named `path`. @@ -624,8 +628,9 @@ pub extern "system" fn sq_packet_parser_from_file (ctx: *mut Context, filename: *const c_char) -> *mut PacketParserResult<'static> { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned(); - fry_box!(ctx, PacketParser::from_file(&filename)) + ffi_try_box!(PacketParser::from_file(&filename)) } /// Starts parsing OpenPGP packets stored in a buffer. @@ -637,12 +642,13 @@ pub extern "system" fn sq_packet_parser_from_bytes (ctx: *mut Context, b: *const uint8_t, len: size_t) -> *mut PacketParserResult<'static> { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); assert!(!b.is_null()); let buf = unsafe { slice::from_raw_parts(b, len as usize) }; - fry_box!(ctx, PacketParser::from_bytes(buf)) + ffi_try_box!(PacketParser::from_bytes(buf)) } /// Frees the packet parser result @@ -773,6 +779,7 @@ pub extern "system" fn sq_packet_parser_next<'a> ppr: Option<&mut *mut PacketParserResult<'a>>) -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let pp = ffi_param_move!(pp); match pp.next() { @@ -785,7 +792,7 @@ pub extern "system" fn sq_packet_parser_next<'a> } Status::Success }, - Err(e) => fry_status!(ctx, Err::<(), failure::Error>(e)), + Err(e) => ffi_try_status!(Err::<(), failure::Error>(e)), } } @@ -817,6 +824,7 @@ pub extern "system" fn sq_packet_parser_recurse<'a> ppr: Option<&mut *mut PacketParserResult<'a>>) -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let pp = ffi_param_move!(pp); match pp.recurse() { @@ -829,7 +837,7 @@ pub extern "system" fn sq_packet_parser_recurse<'a> } Status::Success }, - Err(e) => fry_status!(ctx, Err::<(), failure::Error>(e)), + Err(e) => ffi_try_status!(Err::<(), failure::Error>(e)), } } @@ -846,9 +854,10 @@ pub extern "system" fn sq_packet_parser_buffer_unread_content<'a> len: *mut usize) -> *const uint8_t { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let pp = ffi_param_ref_mut!(pp); let len = ffi_param_ref_mut!(len); - let buf = fry!(ctx, pp.buffer_unread_content()); + let buf = ffi_try!(pp.buffer_unread_content()); *len = buf.len(); buf.as_ptr() } @@ -864,6 +873,7 @@ pub extern "system" fn sq_packet_parser_finish<'a> -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let pp = ffi_param_ref_mut!(pp); match pp.finish() { Ok(p) => { @@ -897,12 +907,13 @@ pub extern "system" fn sq_packet_parser_decrypt<'a> key: *const uint8_t, key_len: size_t) -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let pp = ffi_param_ref_mut!(pp); let key = unsafe { slice::from_raw_parts(key, key_len as usize) }; let key = key.to_owned().into(); - fry_status!(ctx, pp.decrypt((algo as u8).into(), &key)) + ffi_try_status!(pp.decrypt((algo as u8).into(), &key)) } @@ -1019,12 +1030,13 @@ pub extern "system" fn sq_writer_stack_write -> ssize_t { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let writer = ffi_param_ref_mut!(writer); assert!(!buf.is_null()); let buf = unsafe { slice::from_raw_parts(buf, len as usize) }; - fry_or!(ctx, writer.write(buf).map_err(|e| e.into()), -1) as ssize_t + ffi_try_or!(writer.write(buf).map_err(|e| e.into()), -1) as ssize_t } /// Writes up to `len` bytes of `buf` into `writer`. @@ -1040,12 +1052,13 @@ pub extern "system" fn sq_writer_stack_write_all -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let writer = ffi_param_ref_mut!(writer); assert!(!buf.is_null()); let buf = unsafe { slice::from_raw_parts(buf, len as usize) }; - fry_status!(ctx, writer.write_all(buf).map_err(|e| e.into())) + ffi_try_status!(writer.write_all(buf).map_err(|e| e.into())) } /// Finalizes this writer, returning the underlying writer. @@ -1056,9 +1069,10 @@ pub extern "system" fn sq_writer_stack_finalize_one -> *mut writer::Stack<'static, Cookie> { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); if !writer.is_null() { let writer = ffi_param_move!(writer); - maybe_box_raw!(fry!(ctx, writer.finalize_one())) + maybe_box_raw!(ffi_try!(writer.finalize_one())) } else { ptr::null_mut() } @@ -1072,9 +1086,10 @@ pub extern "system" fn sq_writer_stack_finalize -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); if !writer.is_null() { let writer = ffi_param_move!(writer); - fry_status!(ctx, writer.finalize()) + ffi_try_status!(writer.finalize()) } else { Status::Success } @@ -1093,8 +1108,9 @@ pub extern "system" fn sq_arbitrary_writer_new -> *mut writer::Stack<'static, Cookie> { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let inner = ffi_param_move!(inner); - fry_box!(ctx, ArbitraryWriter::new(*inner, tag.into())) + ffi_try_box!(ArbitraryWriter::new(*inner, tag.into())) } /// Signs a packet stream. @@ -1111,6 +1127,7 @@ pub extern "system" fn sq_signer_new -> *mut writer::Stack<'static, Cookie> { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let inner = ffi_param_move!(inner); let signers = ffi_param_ref!(signers); let signers = unsafe { @@ -1122,7 +1139,7 @@ pub extern "system" fn sq_signer_new ffi_param_ref_mut!(signer).as_mut() } ).collect(); - fry_box!(ctx, Signer::new(*inner, signers)) + ffi_try_box!(Signer::new(*inner, signers)) } /// Creates a signer for a detached signature. @@ -1135,6 +1152,7 @@ pub extern "system" fn sq_signer_new_detached -> *mut writer::Stack<'static, Cookie> { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let inner = ffi_param_move!(inner); let signers = ffi_param_ref!(signers); let signers = unsafe { @@ -1146,7 +1164,7 @@ pub extern "system" fn sq_signer_new_detached ffi_param_ref_mut!(signer).as_mut() } ).collect(); - fry_box!(ctx, Signer::detached(*inner, signers)) + ffi_try_box!(Signer::detached(*inner, signers)) } /// Writes a literal data packet. @@ -1160,8 +1178,9 @@ pub extern "system" fn sq_literal_writer_new -> *mut writer::Stack<'static, Cookie> { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let inner = ffi_param_move!(inner); - fry_box!(ctx, LiteralWriter::new(*inner, + ffi_try_box!(LiteralWriter::new(*inner, DataFormat::Binary, None, None)) @@ -1185,6 +1204,7 @@ pub extern "system" fn sq_encryptor_new -> *mut writer::Stack<'static, Cookie> { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let inner = ffi_param_move!(inner); let mut passwords_ = Vec::new(); if passwords_len > 0 { @@ -1210,7 +1230,7 @@ pub extern "system" fn sq_encryptor_new 1 => EncryptionMode::ForTransport, _ => panic!("Bad encryption mode: {}", encryption_mode), }; - fry_box!(ctx, Encryptor::new(*inner, + ffi_try_box!(Encryptor::new(*inner, &passwords_.iter().collect::>(), &recipients, encryption_mode)) @@ -1503,12 +1523,13 @@ pub fn sq_verify<'a>(ctx: *mut Context, -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let input = ffi_param_ref_mut!(input); let r = verify_real(input, dsig, output, get_public_keys, check_signatures, cookie); - fry_status!(ctx, r) + ffi_try_status!(r) } @@ -1626,11 +1647,12 @@ pub fn sq_decrypt<'a>(ctx: *mut Context, -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let input = ffi_param_ref_mut!(input); let output = ffi_param_ref_mut!(output); let r = decrypt_real(input, output, get_public_keys, get_secret_keys, check_signatures, cookie); - fry_status!(ctx, r) + ffi_try_status!(r) } diff --git a/ffi/src/openpgp/packet_pile.rs b/ffi/src/openpgp/packet_pile.rs index 6f798359..139a4861 100644 --- a/ffi/src/openpgp/packet_pile.rs +++ b/ffi/src/openpgp/packet_pile.rs @@ -33,8 +33,9 @@ pub extern "system" fn sq_packet_pile_from_reader(ctx: *mut Context, reader: *mut Box) -> *mut PacketPile { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let reader = ffi_param_ref_mut!(reader); - fry_box!(ctx, PacketPile::from_reader(reader)) + ffi_try_box!(PacketPile::from_reader(reader)) } /// Deserializes the OpenPGP message stored in the file named by @@ -46,8 +47,9 @@ pub extern "system" fn sq_packet_pile_from_file(ctx: *mut Context, filename: *const c_char) -> *mut PacketPile { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned(); - fry_box!(ctx, PacketPile::from_file(&filename)) + ffi_try_box!(PacketPile::from_file(&filename)) } /// Deserializes the OpenPGP message stored in the provided buffer. @@ -58,12 +60,13 @@ pub extern "system" fn sq_packet_pile_from_bytes(ctx: *mut Context, b: *const uint8_t, len: size_t) -> *mut PacketPile { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); assert!(!b.is_null()); let buf = unsafe { slice::from_raw_parts(b, len as usize) }; - fry_box!(ctx, PacketPile::from_bytes(buf)) + ffi_try_box!(PacketPile::from_bytes(buf)) } /// Frees the packet_pile. @@ -88,7 +91,8 @@ pub extern "system" fn sq_packet_pile_serialize(ctx: *mut Context, writer: *mut Box) -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let packet_pile = ffi_param_ref!(packet_pile); let writer = ffi_param_ref_mut!(writer); - fry_status!(ctx, packet_pile.serialize(writer)) + ffi_try_status!(packet_pile.serialize(writer)) } diff --git a/ffi/src/openpgp/tpk.rs b/ffi/src/openpgp/tpk.rs index 1a5e9ec6..0df7071f 100644 --- a/ffi/src/openpgp/tpk.rs +++ b/ffi/src/openpgp/tpk.rs @@ -43,8 +43,9 @@ pub extern "system" fn sq_tpk_from_reader(ctx: *mut Context, reader: *mut Box) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let reader = ffi_param_ref_mut!(reader); - fry_box!(ctx, TPK::from_reader(reader)) + ffi_try_box!(TPK::from_reader(reader)) } /// Returns the first TPK encountered in the file. @@ -53,8 +54,9 @@ pub extern "system" fn sq_tpk_from_file(ctx: *mut Context, filename: *const c_char) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned(); - fry_box!(ctx, TPK::from_file(&filename)) + ffi_try_box!(TPK::from_file(&filename)) } /// Returns the first TPK found in `m`. @@ -65,8 +67,9 @@ pub extern "system" fn sq_tpk_from_packet_pile(ctx: *mut Context, m: *mut PacketPile) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let m = ffi_param_move!(m); - fry_box!(ctx, TPK::from_packet_pile(*m)) + ffi_try_box!(TPK::from_packet_pile(*m)) } /// Returns the first TPK found in `buf`. @@ -77,12 +80,13 @@ pub extern "system" fn sq_tpk_from_bytes(ctx: *mut Context, b: *const uint8_t, len: size_t) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); assert!(!b.is_null()); let buf = unsafe { slice::from_raw_parts(b, len as usize) }; - fry_box!(ctx, TPK::from_bytes(buf)) + ffi_try_box!(TPK::from_bytes(buf)) } /// Returns the first TPK found in the packet parser. @@ -94,9 +98,10 @@ pub extern "system" fn sq_tpk_from_packet_parser(ctx: *mut Context, -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let ppr = ffi_param_move!(ppr); - fry_box!(ctx, TPK::from_packet_parser(*ppr)) + ffi_try_box!(TPK::from_packet_parser(*ppr)) } /// Frees the TPK. @@ -130,9 +135,10 @@ pub extern "system" fn sq_tpk_serialize(ctx: *mut Context, writer: *mut Box) -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let tpk = ffi_param_ref!(tpk); let writer = ffi_param_ref_mut!(writer); - fry_status!(ctx, tpk.serialize(writer)) + ffi_try_status!(tpk.serialize(writer)) } /// Merges `other` into `tpk`. @@ -147,9 +153,10 @@ pub extern "system" fn sq_tpk_merge(ctx: *mut Context, other: *mut TPK) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let tpk = ffi_param_move!(tpk); let other = ffi_param_move!(other); - fry_box!(ctx, tpk.merge(*other)) + ffi_try_box!(tpk.merge(*other)) } /// Adds packets to the TPK. @@ -166,13 +173,14 @@ pub extern "system" fn sq_tpk_merge_packets(ctx: *mut Context, packets_len: size_t) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let tpk = ffi_param_move!(tpk); let packets = unsafe { slice::from_raw_parts_mut(packets, packets_len) }; let packets = packets.iter_mut().map(|p| *unsafe { Box::from_raw(*p) } ).collect(); - fry_box!(ctx, tpk.merge_packets(packets)) + ffi_try_box!(tpk.merge_packets(packets)) } /// Dumps the TPK. @@ -296,6 +304,7 @@ pub extern "system" fn sq_tpk_revoke(ctx: *mut Context, -> *mut packet::Signature { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let tpk = ffi_param_ref!(tpk); let signer = ffi_param_ref_mut!(primary_signer); let code = int_to_reason_for_revocation(code); @@ -305,7 +314,7 @@ pub extern "system" fn sq_tpk_revoke(ctx: *mut Context, b"" }; - fry_box!(ctx, tpk.revoke(signer.as_mut(), code, reason)) + ffi_try_box!(tpk.revoke(signer.as_mut(), code, reason)) } /// Adds a revocation certificate to the tpk. @@ -363,6 +372,7 @@ pub extern "system" fn sq_tpk_revoke_in_place(ctx: *mut Context, -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let tpk = ffi_param_move!(tpk); let signer = ffi_param_ref_mut!(primary_signer); let code = int_to_reason_for_revocation(code); @@ -372,7 +382,7 @@ pub extern "system" fn sq_tpk_revoke_in_place(ctx: *mut Context, b"" }; - fry_box!(ctx, tpk.revoke_in_place(signer.as_mut(), code, reason)) + ffi_try_box!(tpk.revoke_in_place(signer.as_mut(), code, reason)) } /// Returns whether the TPK has expired. @@ -420,9 +430,10 @@ pub extern "system" fn sq_tpk_set_expiry(ctx: *mut Context, tpk: *mut TPK, expiry: u32) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let tpk = ffi_param_move!(tpk); - fry_box!(ctx, tpk.set_expiry_in_seconds(expiry)) + ffi_try_box!(tpk.set_expiry_in_seconds(expiry)) } /// Returns whether the TPK includes any secret key material. @@ -697,6 +708,7 @@ pub extern "system" fn sq_tpk_builder_generate -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let tpk_out = ffi_param_ref_mut!(tpk_out); let revocation_out = ffi_param_ref_mut!(revocation_out); let tpkb = ffi_param_move!(tpkb); @@ -706,6 +718,6 @@ pub extern "system" fn sq_tpk_builder_generate *revocation_out = box_raw!(revocation); Status::Success }, - Err(e) => fry_status!(ctx, Err::<(), failure::Error>(e)), + Err(e) => ffi_try_status!(Err::<(), failure::Error>(e)), } } diff --git a/ffi/src/openpgp/tsk.rs b/ffi/src/openpgp/tsk.rs index ccb07f28..10cc24fc 100644 --- a/ffi/src/openpgp/tsk.rs +++ b/ffi/src/openpgp/tsk.rs @@ -28,6 +28,7 @@ pub extern "system" fn sq_tsk_new(ctx: *mut Context, -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let tsk_out = ffi_param_ref_mut!(tsk_out); let revocation_out = ffi_param_ref_mut!(revocation_out); let primary_uid = ffi_param_cstr!(primary_uid).to_string_lossy(); @@ -37,7 +38,7 @@ pub extern "system" fn sq_tsk_new(ctx: *mut Context, *revocation_out = box_raw!(revocation); Status::Success }, - Err(e) => fry_status!(ctx, Err::<(), failure::Error>(e)), + Err(e) => ffi_try_status!(Err::<(), failure::Error>(e)), } } @@ -71,7 +72,8 @@ pub extern "system" fn sq_tsk_serialize(ctx: *mut Context, writer: *mut Box) -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let tsk = ffi_param_ref!(tsk); let writer = ffi_param_ref_mut!(writer); - fry_status!(ctx, tsk.serialize(writer)) + ffi_try_status!(tsk.serialize(writer)) } diff --git a/ffi/src/store.rs b/ffi/src/store.rs index c3eb5f58..c3ec7a04 100644 --- a/ffi/src/store.rs +++ b/ffi/src/store.rs @@ -47,9 +47,10 @@ pub extern "system" fn sq_store_list_stores(ctx: *mut Context, domain_prefix: *const c_char) -> *mut StoreIter { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let domain_prefix = ffi_param_cstr!(domain_prefix).to_string_lossy(); - fry_box!(ctx, Store::list(&ctx.c, &domain_prefix)) + ffi_try_box!(Store::list(&ctx.c, &domain_prefix)) } /// Returns the next store. @@ -96,8 +97,9 @@ pub extern "system" fn sq_store_iter_free(iter: Option<&mut StoreIter>) { pub extern "system" fn sq_store_list_keys(ctx: *mut Context) -> *mut KeyIter { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); - fry_box!(ctx, Store::list_keys(&ctx.c)) + ffi_try_box!(Store::list_keys(&ctx.c)) } /// Lists all log entries. @@ -105,8 +107,9 @@ pub extern "system" fn sq_store_list_keys(ctx: *mut Context) pub extern "system" fn sq_store_server_log(ctx: *mut Context) -> *mut LogIter { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); - fry_box!(ctx, Store::server_log(&ctx.c)) + ffi_try_box!(Store::server_log(&ctx.c)) } /// Returns the next key. @@ -187,9 +190,10 @@ pub extern "system" fn sq_store_open(ctx: *mut Context, name: *const c_char) -> *mut Store { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let name = ffi_param_cstr!(name).to_string_lossy(); - fry_box!(ctx, Store::open(&ctx.c, &name)) + ffi_try_box!(Store::open(&ctx.c, &name)) } /// Frees a sq_store_t. @@ -206,11 +210,12 @@ pub extern "system" fn sq_store_add(ctx: *mut Context, fingerprint: *const Fingerprint) -> *mut Binding { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let store = ffi_param_ref!(store); let label = ffi_param_cstr!(label).to_string_lossy(); let fingerprint = ffi_param_ref!(fingerprint); - fry_box!(ctx, store.add(&label, fingerprint)) + ffi_try_box!(store.add(&label, fingerprint)) } /// Imports a key into the store. @@ -221,11 +226,12 @@ pub extern "system" fn sq_store_import(ctx: *mut Context, tpk: *const TPK) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let store = ffi_param_ref!(store); let label = ffi_param_cstr!(label).to_string_lossy(); let tpk = ffi_param_ref!(tpk); - fry_box!(ctx, store.import(&label, tpk)) + ffi_try_box!(store.import(&label, tpk)) } /// Returns the binding for the given label. @@ -235,10 +241,11 @@ pub extern "system" fn sq_store_lookup(ctx: *mut Context, label: *const c_char) -> *mut Binding { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let store = ffi_param_ref!(store); let label = ffi_param_cstr!(label).to_string_lossy(); - fry_box!(ctx, store.lookup(&label)) + ffi_try_box!(store.lookup(&label)) } /// Looks up a key in the common key pool by KeyID. @@ -248,9 +255,10 @@ pub extern "system" fn sq_store_lookup_by_keyid(ctx: *mut Context, -> *mut Key { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let keyid = ffi_param_ref!(keyid); - fry_box!(ctx, Pool::lookup_by_keyid(&ctx.c, keyid)) + ffi_try_box!(Pool::lookup_by_keyid(&ctx.c, keyid)) } /// Looks up a key in the common key pool by (Sub)KeyID. @@ -260,9 +268,10 @@ pub extern "system" fn sq_store_lookup_by_subkeyid(ctx: *mut Context, -> *mut Key { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let keyid = ffi_param_ref!(keyid); - fry_box!(ctx, Pool::lookup_by_subkeyid(&ctx.c, keyid)) + ffi_try_box!(Pool::lookup_by_subkeyid(&ctx.c, keyid)) } /// Deletes this store. @@ -273,9 +282,10 @@ pub extern "system" fn sq_store_delete(ctx: *mut Context, store: *mut Store) -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let store = ffi_param_move!(store); - fry_status!(ctx, store.delete()) + ffi_try_status!(store.delete()) } /// Lists all bindings. @@ -284,9 +294,10 @@ pub extern "system" fn sq_store_iter(ctx: *mut Context, store: *const Store) -> *mut BindingIter { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let store = ffi_param_ref!(store); - fry_box!(ctx, store.iter()) + ffi_try_box!(store.iter()) } /// Returns the next binding. @@ -328,9 +339,10 @@ pub extern "system" fn sq_store_log(ctx: *mut Context, store: *const Store) -> *mut LogIter { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let store = ffi_param_ref!(store); - fry_box!(ctx, store.log()) + ffi_try_box!(store.log()) } /// Frees a sq_binding_t. @@ -374,9 +386,10 @@ pub extern "system" fn sq_binding_stats(ctx: *mut Context, binding: *const Binding) -> *mut Stats { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let binding = ffi_param_ref!(binding); - box_raw!(Stats::new(fry!(ctx, binding.stats()))) + box_raw!(Stats::new(ffi_try!(binding.stats()))) } /// Returns the `sq_key_t` of this binding. @@ -385,9 +398,10 @@ pub extern "system" fn sq_binding_key(ctx: *mut Context, binding: *const Binding) -> *mut Key { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let binding = ffi_param_ref!(binding); - fry_box!(ctx, binding.key()) + ffi_try_box!(binding.key()) } /// Returns the `sq_tpk_t` of this binding. @@ -396,9 +410,10 @@ pub extern "system" fn sq_binding_tpk(ctx: *mut Context, binding: *const Binding) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let binding = ffi_param_ref!(binding); - fry_box!(ctx, binding.tpk()) + ffi_try_box!(binding.tpk()) } /// Updates this binding with the given TPK. @@ -423,10 +438,11 @@ pub extern "system" fn sq_binding_import(ctx: *mut Context, tpk: *const TPK) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let binding = ffi_param_ref!(binding); let tpk = ffi_param_ref!(tpk); - fry_box!(ctx, binding.import(&tpk)) + ffi_try_box!(binding.import(&tpk)) } @@ -449,10 +465,11 @@ pub extern "system" fn sq_binding_rotate(ctx: *mut Context, tpk: *const TPK) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let binding = ffi_param_ref!(binding); let tpk = ffi_param_ref!(tpk); - fry_box!(ctx, binding.rotate(&tpk)) + ffi_try_box!(binding.rotate(&tpk)) } /// Deletes this binding. @@ -463,9 +480,10 @@ pub extern "system" fn sq_binding_delete(ctx: *mut Context, binding: *mut Binding) -> Status { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let binding = ffi_param_move!(binding); - fry_status!(ctx, binding.delete()) + ffi_try_status!(binding.delete()) } /// Lists all log entries related to this binding. @@ -474,9 +492,10 @@ pub extern "system" fn sq_binding_log(ctx: *mut Context, binding: *const Binding) -> *mut LogIter { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let binding = ffi_param_ref!(binding); - fry_box!(ctx, binding.log()) + ffi_try_box!(binding.log()) } /// Returns the `sq_stats_t` of this key. @@ -485,9 +504,10 @@ pub extern "system" fn sq_key_stats(ctx: *mut Context, key: *const Key) -> *mut Stats { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let key = ffi_param_ref!(key); - box_raw!(Stats::new(fry!(ctx, key.stats()))) + box_raw!(Stats::new(ffi_try!(key.stats()))) } /// Returns the `sq_tpk_t`. @@ -496,9 +516,10 @@ pub extern "system" fn sq_key_tpk(ctx: *mut Context, key: *const Key) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let key = ffi_param_ref!(key); - fry_box!(ctx, key.tpk()) + ffi_try_box!(key.tpk()) } /// Updates this stored key with the given TPK. @@ -516,10 +537,11 @@ pub extern "system" fn sq_key_import(ctx: *mut Context, tpk: *const TPK) -> *mut TPK { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let key = ffi_param_ref!(key); let tpk = ffi_param_ref!(tpk); - fry_box!(ctx, key.import(&tpk)) + ffi_try_box!(key.import(&tpk)) } /// Lists all log entries related to this key. @@ -528,9 +550,10 @@ pub extern "system" fn sq_key_log(ctx: *mut Context, key: *const Key) -> *mut LogIter { let ctx = ffi_param_ref_mut!(ctx); + ffi_make_fry_from_ctx!(ctx); let key = ffi_param_ref!(key); - fry_box!(ctx, key.log()) + ffi_try_box!(key.log()) } /// Frees a sq_stats_t. -- cgit v1.2.3