From 687255e17fd2c4fe6871735f896c9abc2a6c88c2 Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Thu, 9 May 2019 10:06:54 +0200 Subject: openpgp-ffi: Add some bindings for TPKParser --- openpgp-ffi/include/sequoia/openpgp.h | 32 ++++++++++++++ openpgp-ffi/include/sequoia/openpgp/types.h | 7 +++ openpgp-ffi/src/tpk.rs | 68 ++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 2 deletions(-) (limited to 'openpgp-ffi') diff --git a/openpgp-ffi/include/sequoia/openpgp.h b/openpgp-ffi/include/sequoia/openpgp.h index f504c037..1df8909c 100644 --- a/openpgp-ffi/include/sequoia/openpgp.h +++ b/openpgp-ffi/include/sequoia/openpgp.h @@ -799,6 +799,38 @@ pgp_tpk_key_iter_t pgp_tpk_key_iter_valid (pgp_tpk_t tpk); /*/ char *pgp_tpk_primary_user_id(pgp_tpk_t tpk); +/*/ +/// Returns a TPKParser. +/// +/// A TPK parser parses a keyring, which is simply zero or more TPKs +/// concatenated together. +/*/ +pgp_tpk_parser_t pgp_tpk_parser_from_bytes(pgp_error_t *errp, + char *buf, size_t len); + +/*/ +/// Returns a TPKParser. +/// +/// A TPK parser parses a keyring, which is simply zero or more TPKs +/// concatenated together. +/*/ +pgp_tpk_parser_t pgp_tpk_parser_from_packet_parser(pgp_packet_parser_result_t ppr); + +/*/ +/// Returns the next TPK, if any. +/// +/// If there is an error parsing the TPK, it is returned in *errp. +/// +/// If this function returns NULL and does not set *errp, then the end +/// of the file was reached. +/*/ +pgp_tpk_t pgp_tpk_parser_next(pgp_error_t *errp, pgp_tpk_parser_t parser); + +/*/ +/// Frees an pgp_tpk_key_iter_t. +/*/ +void pgp_tpk_parser_free (pgp_tpk_parser_t parser); + /* TPKBuilder */ /*/ diff --git a/openpgp-ffi/include/sequoia/openpgp/types.h b/openpgp-ffi/include/sequoia/openpgp/types.h index 36860a8b..c6572346 100644 --- a/openpgp-ffi/include/sequoia/openpgp/types.h +++ b/openpgp-ffi/include/sequoia/openpgp/types.h @@ -385,6 +385,13 @@ typedef struct pgp_tpk_key_iter *pgp_tpk_key_iter_t; /*/ typedef struct pgp_tpk *pgp_tpk_t; +/*/ +/// A parser for TPKs +/// +/// A `TPKParser` parses a keyring, which is simply zero or more +/// binary TPKs concatenated together. +/*/ +typedef struct pgp_tpk_parser *pgp_tpk_parser_t; /*/ /// A transferable secret key (TSK). diff --git a/openpgp-ffi/src/tpk.rs b/openpgp-ffi/src/tpk.rs index 3238e5a7..991bb837 100644 --- a/openpgp-ffi/src/tpk.rs +++ b/openpgp-ffi/src/tpk.rs @@ -7,7 +7,7 @@ use std::ptr; use std::slice; -use libc::{c_char, c_int, size_t, time_t}; +use libc::{c_char, c_int, size_t, time_t, uint8_t}; extern crate sequoia_openpgp as openpgp; use self::openpgp::{ @@ -16,11 +16,15 @@ use self::openpgp::{ autocrypt::Autocrypt, crypto, constants::ReasonForRevocation, - parse::PacketParserResult, + parse::{ + PacketParserResult, + Parse, + }, tpk::{ CipherSuite, KeyIter, TPKBuilder, + TPKParser, UserIDBinding, UserIDBindingIter, }, @@ -676,6 +680,66 @@ pub extern "C" fn pgp_tpk_key_iter_next<'a>( } } +/// Wrappers a TPKParser for export via the FFI. +pub struct TPKParserWrapper<'a> { + parser: TPKParser<'a, std::vec::IntoIter>, +} + +/// Returns a TPKParser. +/// +/// A `TPKParser` parses a keyring, which is simply zero or more TPKs +/// concatenated together. +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C" +fn pgp_tpk_parser_from_bytes(errp: Option<&mut *mut ::error::Error>, + buf: *mut uint8_t, len: size_t) + -> *mut TPKParserWrapper<'static> +{ + ffi_make_fry_from_errp!(errp); + + let buf : &[u8] = unsafe { std::slice::from_raw_parts(buf, len) }; + box_raw!(TPKParserWrapper { parser: ffi_try!(TPKParser::from_bytes(buf)) }) +} + +/// Returns a TPKParser. +/// +/// A `TPKParser` parses a keyring, which is simply zero or more TPKs +/// concatenated together. +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C" +fn pgp_tpk_parser_from_packet_parser(ppr: *mut PacketParserResult<'static>) + -> *mut TPKParserWrapper<'static> +{ + let ppr = ffi_param_move!(ppr); + let parser = TPKParser::from_packet_parser(*ppr); + box_raw!(TPKParserWrapper { parser: parser }) +} + + +/// Returns the next TPK, if any. +/// +/// If there is an error parsing the TPK, it is returned in *errp. +/// +/// If this function returns NULL and does not set *errp, then the end +/// of the file was reached. +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C" +fn pgp_tpk_parser_next(errp: Option<&mut *mut ::error::Error>, + parser: *mut TPKParserWrapper) + -> *mut TPK +{ + ffi_make_fry_from_errp!(errp); + let wrapper : &mut TPKParserWrapper = ffi_param_ref_mut!(parser); + match wrapper.parser.next() { + Some(tpkr) => ffi_try!(tpkr).move_into_raw(), + None => ::std::ptr::null_mut(), + } +} + +/// Frees a pgp_tpk_parser_t. +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] +pub extern "C" fn pgp_tpk_parser_free(parser: Option<&mut TPKParserWrapper>) +{ + ffi_free!(parser) +} + /* TPKBuilder */ /// Creates a default `pgp_tpk_builder_t`. -- cgit v1.2.3