diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-02-12 16:53:25 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-02-12 16:56:23 +0100 |
commit | 779fd253b285f315aff529690716e2e56047caa9 (patch) | |
tree | 3c52fb94dacea04f46b9f43337b6923856c15660 /openpgp-ffi/src | |
parent | ddcd197113b805410ea273e7e36ab73fe7b13c2c (diff) |
openpgp-ffi: Wrap openpgp::packet::Key.
Diffstat (limited to 'openpgp-ffi/src')
-rw-r--r-- | openpgp-ffi/src/crypto.rs | 8 | ||||
-rw-r--r-- | openpgp-ffi/src/packet/key.rs | 76 | ||||
-rw-r--r-- | openpgp-ffi/src/packet/signature.rs | 18 | ||||
-rw-r--r-- | openpgp-ffi/src/tpk.rs | 27 |
4 files changed, 60 insertions, 69 deletions
diff --git a/openpgp-ffi/src/crypto.rs b/openpgp-ffi/src/crypto.rs index 82bb1707..7a453b60 100644 --- a/openpgp-ffi/src/crypto.rs +++ b/openpgp-ffi/src/crypto.rs @@ -7,8 +7,10 @@ extern crate sequoia_openpgp; use self::sequoia_openpgp::{ crypto, - packet::Key, }; +use super::packet::key::Key; + +use MoveFromRaw; /// Frees a signer. #[::sequoia_ffi_macros::extern_fn] #[no_mangle] @@ -25,9 +27,9 @@ pub extern "system" fn pgp_key_pair_new -> *mut crypto::KeyPair { ffi_make_fry_from_errp!(errp); - let public = ffi_param_move!(public); + let public = public.move_from_raw(); let secret = ffi_param_move!(secret); - ffi_try_box!(crypto::KeyPair::new(*public, *secret)) + ffi_try_box!(crypto::KeyPair::new(public, *secret)) } /// Frees a key pair. diff --git a/openpgp-ffi/src/packet/key.rs b/openpgp-ffi/src/packet/key.rs index 7e17c507..c9eadd19 100644 --- a/openpgp-ffi/src/packet/key.rs +++ b/openpgp-ffi/src/packet/key.rs @@ -7,69 +7,63 @@ use libc::{c_int, time_t}; extern crate sequoia_openpgp as openpgp; -use self::openpgp::{ - packet, -}; use super::super::fingerprint::Fingerprint; use super::super::keyid::KeyID; +use MoveFromRaw; use MoveIntoRaw; +use RefRaw; -/// Clones the key. -#[::sequoia_ffi_macros::extern_fn] #[no_mangle] -pub extern "system" fn pgp_key_clone(key: *const packet::Key) - -> *mut packet::Key { - let key = ffi_param_ref!(key); - box_raw!(key.clone()) -} +/// Holds a public key, public subkey, private key or private subkey packet. +/// +/// See [Section 5.5 of RFC 4880] for details. +/// +/// [Section 5.5 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.5 +/// +/// Wraps [`sequoia-openpgp::packet::key::Key`]. +/// +/// [`sequoia-openpgp::packet::key::Key`]: ../../sequoia_openpgp/packet/key/struct.Key.html +#[::ffi_wrapper_type(prefix = "pgp_", + derive = "Clone, Debug, PartialEq, Parse")] +pub struct Key(openpgp::packet::Key); /// Computes and returns the key's fingerprint as per Section 12.2 /// of RFC 4880. -#[::sequoia_ffi_macros::extern_fn] #[no_mangle] -pub extern "system" fn pgp_key_fingerprint(key: *const packet::Key) - -> *mut Fingerprint { - let key = ffi_param_ref!(key); - key.fingerprint().move_into_raw() +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" +fn pgp_key_fingerprint(key: *const Key) -> *mut Fingerprint { + key.ref_raw().fingerprint().move_into_raw() } /// Computes and returns the key's key ID as per Section 12.2 of RFC /// 4880. -#[::sequoia_ffi_macros::extern_fn] #[no_mangle] -pub extern "system" fn pgp_key_keyid(key: *const packet::Key) - -> *mut KeyID { - let key = ffi_param_ref!(key); - key.keyid().move_into_raw() +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" +fn pgp_key_keyid(key: *const Key) -> *mut KeyID { + key.ref_raw().keyid().move_into_raw() } /// Returns the key's creation time. -#[::sequoia_ffi_macros::extern_fn] #[no_mangle] -pub extern "system" fn pgp_key_creation_time(key: *const packet::Key) - -> time_t -{ - let key = ffi_param_ref!(key); +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" +fn pgp_key_creation_time(key: *const Key) -> time_t { + let key = key.ref_raw(); let ct = key.creation_time(); ct.to_timespec().sec as time_t } /// Returns the key's public key algorithm. -#[::sequoia_ffi_macros::extern_fn] #[no_mangle] -pub extern "system" fn pgp_key_public_key_algo(key: *const packet::Key) - -> c_int -{ - let key = ffi_param_ref!(key); +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" +fn pgp_key_public_key_algo(key: *const Key) -> c_int { + let key = key.ref_raw(); let pk_algo : u8 = key.pk_algo().into(); pk_algo as c_int } /// Returns the public key's size in bits. -#[::sequoia_ffi_macros::extern_fn] #[no_mangle] -pub extern "system" fn pgp_key_public_key_bits(key: *const packet::Key) - -> c_int -{ +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" +fn pgp_key_public_key_bits(key: *const Key) -> c_int { use self::openpgp::crypto::mpis::PublicKey::*; - let key = ffi_param_ref!(key); + let key = key.ref_raw(); match key.mpis() { RSA { e: _, n } => n.bits as c_int, DSA { p: _, q: _, g: _, y } => y.bits as c_int, @@ -87,12 +81,10 @@ pub extern "system" fn pgp_key_public_key_bits(key: *const packet::Key) /// # Errors /// /// Fails if the secret key is missing, or encrypted. -#[::sequoia_ffi_macros::extern_fn] #[no_mangle] -pub extern "system" fn pgp_key_into_key_pair(errp: Option<&mut *mut ::error::Error>, - key: *mut packet::Key) - -> *mut self::openpgp::crypto::KeyPair -{ +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" +fn pgp_key_into_key_pair(errp: Option<&mut *mut ::error::Error>, + key: *mut Key) + -> *mut self::openpgp::crypto::KeyPair { ffi_make_fry_from_errp!(errp); - let key = ffi_param_move!(key); - ffi_try_box!(key.into_keypair()) + ffi_try_box!(key.move_from_raw().into_keypair()) } diff --git a/openpgp-ffi/src/packet/signature.rs b/openpgp-ffi/src/packet/signature.rs index 4425bc35..a019f750 100644 --- a/openpgp-ffi/src/packet/signature.rs +++ b/openpgp-ffi/src/packet/signature.rs @@ -10,9 +10,9 @@ use libc::time_t; extern crate sequoia_openpgp as openpgp; -use self::openpgp::packet; use super::super::fingerprint::Fingerprint; use super::super::keyid::KeyID; +use super::key::Key; use Maybe; use MoveFromRaw; @@ -151,9 +151,9 @@ fn pgp_signature_expired_at(sig: *const Signature, when: time_t) -> bool { /// A signature is alive if the creation date is in the past, and the /// signature has not expired. #[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" -fn pgp_signature_key_alive(sig: *const Signature, key: *const packet::Key) +fn pgp_signature_key_alive(sig: *const Signature, key: *const Key) -> bool { - sig.ref_raw().key_alive(ffi_param_ref!(key)) + sig.ref_raw().key_alive(key.ref_raw()) } /// Returns whether the signature is alive at the specified time. @@ -161,25 +161,25 @@ fn pgp_signature_key_alive(sig: *const Signature, key: *const packet::Key) /// A signature is alive if the creation date is in the past, and the /// signature has not expired at the specified time. #[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" -fn pgp_signature_key_alive_at(sig: *const Signature, key: *const packet::Key, +fn pgp_signature_key_alive_at(sig: *const Signature, key: *const Key, when: time_t) -> bool { sig.ref_raw() - .key_alive_at(ffi_param_ref!(key), + .key_alive_at(key.ref_raw(), time::at(time::Timespec::new(when as i64, 0))) } /// Returns whether the signature is expired. #[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" -fn pgp_signature_key_expired(sig: *const Signature, key: *const packet::Key) +fn pgp_signature_key_expired(sig: *const Signature, key: *const Key) -> bool { - sig.ref_raw().key_expired(ffi_param_ref!(key)) + sig.ref_raw().key_expired(key.ref_raw()) } /// Returns whether the signature is expired at the specified time. #[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" -fn pgp_signature_key_expired_at(sig: *const Signature, key: *const packet::Key, +fn pgp_signature_key_expired_at(sig: *const Signature, key: *const Key, when: time_t) -> bool { sig.ref_raw() - .key_expired_at(ffi_param_ref!(key), + .key_expired_at(key.ref_raw(), time::at(time::Timespec::new(when as i64, 0))) } diff --git a/openpgp-ffi/src/tpk.rs b/openpgp-ffi/src/tpk.rs index 5b035dff..1483c71c 100644 --- a/openpgp-ffi/src/tpk.rs +++ b/openpgp-ffi/src/tpk.rs @@ -16,7 +16,6 @@ use self::openpgp::{ autocrypt::Autocrypt, crypto, constants::ReasonForRevocation, - packet, parse::PacketParserResult, tpk::{ CipherSuite, @@ -29,6 +28,7 @@ use self::openpgp::{ use ::error::Status; use super::fingerprint::Fingerprint; +use super::packet::key::Key; use super::packet::signature::Signature; use super::packet_pile::PacketPile; use super::tsk::TSK; @@ -138,13 +138,10 @@ fn pgp_tpk_into_tsk(tpk: *mut TPK) /// Returns a reference to the TPK's primary key. /// -/// The tpk still owns the key. The caller should neither modify nor -/// free the key. +/// The tpk still owns the key. The caller must not modify the key. #[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system" -fn pgp_tpk_primary(tpk: *const TPK) - -> *const packet::Key { - let tpk = tpk.ref_raw(); - tpk.primary() +fn pgp_tpk_primary(tpk: *const TPK) -> *const Key { + tpk.ref_raw().primary().move_into_raw() } /// Returns the TPK's revocation status. @@ -196,9 +193,9 @@ fn int_to_reason_for_revocation(code: c_int) -> ReasonForRevocation { /// assert (revocation); /// pgp_signature_free (revocation); /* Free the generated one. */ /// -/// primary_key = pgp_key_clone (pgp_tpk_primary (tpk)); -/// assert (primary_key); -/// primary_keypair = pgp_key_into_key_pair (NULL, primary_key); +/// primary_key = pgp_tpk_primary (tpk); +/// primary_keypair = pgp_key_into_key_pair (NULL, pgp_key_clone (primary_key)); +/// pgp_key_free (primary_key); /// assert (primary_keypair); /// primary_signer = pgp_key_pair_as_signer (primary_keypair); /// revocation = pgp_tpk_revoke (NULL, tpk, primary_signer, @@ -263,9 +260,9 @@ fn pgp_tpk_revoke(errp: Option<&mut *mut ::error::Error>, /// assert (revocation); /// pgp_signature_free (revocation); /* Free the generated one. */ /// -/// primary_key = pgp_key_clone (pgp_tpk_primary (tpk)); -/// assert (primary_key); -/// primary_keypair = pgp_key_into_key_pair (NULL, primary_key); +/// primary_key = pgp_tpk_primary (tpk); +/// primary_keypair = pgp_key_into_key_pair (NULL, pgp_key_clone (primary_key)); +/// pgp_key_free (primary_key); /// assert (primary_keypair); /// primary_signer = pgp_key_pair_as_signer (primary_keypair); /// tpk = pgp_tpk_revoke_in_place (NULL, tpk, primary_signer, @@ -478,7 +475,7 @@ pub extern "system" fn pgp_tpk_key_iter_next<'a>( iter_wrapper: *mut KeyIterWrapper<'a>, sigo: Option<&mut Maybe<Signature>>, rso: Option<&mut &'a RevocationStatus<'a>>) - -> Option<&'a packet::Key> + -> Maybe<Key> { let iter_wrapper = ffi_param_ref_mut!(iter_wrapper); iter_wrapper.rso = None; @@ -493,7 +490,7 @@ pub extern "system" fn pgp_tpk_key_iter_next<'a>( *ptr = iter_wrapper.rso.as_ref().unwrap(); } - Some(key) + Some(key).move_into_raw() } else { None } |