diff options
author | Neal H. Walfield <neal@pep.foundation> | 2018-11-22 10:38:47 +0100 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2018-11-22 10:55:43 +0100 |
commit | 73d61ae48eb932613821cf524d122164c9512540 (patch) | |
tree | 3e15d71174a979ce406d61cd896c4b6e111a9754 /ffi | |
parent | fce58a063f8c2aeeaef783f45ec089cfcc753102 (diff) |
ffi: Wrap tpk::UserIDBinding.
- Add sq_user_id_binding_user_id to return the value of the UserID
from a UserIDBinding.
- Export an interface to iterate over UserIDBindings,
sq_tpk_user_id_binding_iter, fn sq_user_id_binding_iter_free,
sq_user_id_binding_iter_next.
Diffstat (limited to 'ffi')
-rw-r--r-- | ffi/include/sequoia/openpgp.h | 37 | ||||
-rw-r--r-- | ffi/src/openpgp.rs | 62 |
2 files changed, 98 insertions, 1 deletions
diff --git a/ffi/include/sequoia/openpgp.h b/ffi/include/sequoia/openpgp.h index f898c2a3..c39581c7 100644 --- a/ffi/include/sequoia/openpgp.h +++ b/ffi/include/sequoia/openpgp.h @@ -545,6 +545,38 @@ typedef enum sq_reason_for_revocation { SQ_REASON_FOR_REVOCATION_FORCE_WIDTH = INT_MAX, } sq_reason_for_revocation_t; +/* openpgp::tpk::UserIDBinding. */ + +/*/ +/// A `UserIDBinding`. +/*/ +typedef struct sq_user_id_binding *sq_user_id_binding_t; + +/*/ +/// Returns the user id. +/// +/// This function may fail and return NULL if the user id contains an +/// interior NUL byte. We do this rather than complicate the API, as +/// there is no valid use for such user ids; they must be malicious. +/// +/// The caller must free the returned value. +/*/ +char *sq_user_id_binding_user_id (sq_user_id_binding_t binding); + +/* openpgp::tpk::UserIDBindingIter. */ + +/*/ +/// An iterator over `UserIDBinding`s. +/*/ +typedef struct sq_user_id_binding_iter *sq_user_id_binding_iter_t; + +/*/ +/// Returns the next element in the iterator. +/*/ +sq_user_id_binding_t sq_user_id_binding_iter_next (sq_user_id_binding_iter_t iter); + +/// Frees an sq_user_id_binding_iter_t. +void sq_user_id_binding_iter_free (sq_user_id_binding_iter_t iter); /* openpgp::tpk. */ @@ -708,6 +740,11 @@ sq_tpk_t sq_tpk_set_expiry(sq_context_t ctx, /*/ int sq_tpk_is_tsk(sq_tpk_t tpk); +/*/ +/// Returns an iterator over the `UserIDBinding`s. +/*/ +sq_user_id_binding_iter_t sq_tpk_user_id_binding_iter (sq_tpk_t tpk); + /* TPKBuilder */ typedef struct sq_tpk_builder *sq_tpk_builder_t; diff --git a/ffi/src/openpgp.rs b/ffi/src/openpgp.rs index b740e571..34c96806 100644 --- a/ffi/src/openpgp.rs +++ b/ffi/src/openpgp.rs @@ -25,7 +25,12 @@ use self::openpgp::{ }, crypto::Password, }; -use self::openpgp::tpk::{CipherSuite, TPKBuilder}; +use self::openpgp::tpk::{ + CipherSuite, + TPKBuilder, + UserIDBinding, + UserIDBindingIter, +}; use self::openpgp::packet; use self::openpgp::parse::{PacketParserResult, PacketParser, PacketParserEOF}; use self::openpgp::serialize::Serialize; @@ -982,6 +987,61 @@ pub extern "system" fn sq_revocation_status_free( }; } +/* UserIDBinding */ + +/// Returns the user id. +/// +/// This function may fail and return NULL if the user id contains an +/// interior NUL byte. We do this rather than complicate the API, as +/// there is no valid use for such user ids; they must be malicious. +/// +/// The caller must free the returned value. +#[no_mangle] +pub extern "system" fn sq_user_id_binding_user_id( + binding: Option<&UserIDBinding>) + -> *mut c_char +{ + let binding = binding.expect("Binding is NULL"); + + if let Ok(c_str) = CString::new(binding.userid().userid()) { + c_str.into_raw() + } else { + ptr::null_mut() + } +} + +/* UserIDBindingIter */ + +/// Returns an iterator over the TPK's user id bindings. +#[no_mangle] +pub extern "system" fn sq_tpk_user_id_binding_iter(tpk: Option<&TPK>) + -> *mut UserIDBindingIter +{ + let tpk = tpk.expect("TPK is NULL"); + box_raw!(tpk.userids()) +} + +/// Frees a sq_user_id_binding_iter_t. +#[no_mangle] +pub extern "system" fn sq_user_id_binding_iter_free( + iter: *mut UserIDBindingIter) +{ + if iter.is_null() { return }; + unsafe { + drop(Box::from_raw(iter)) + }; +} + +/// Returns the next `UserIDBinding`. +#[no_mangle] +pub extern "system" fn sq_user_id_binding_iter_next<'a>( + iter: Option<&mut UserIDBindingIter<'a>>) + -> Option<&'a UserIDBinding> +{ + let iter = iter.expect("Iterator is NULL"); + iter.next() +} + /* TPKBuilder */ /// Creates a default `sq_tpk_builder_t`. |