From 40eacf4c92c229600ccf1f328504b271fdf9d37b Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Fri, 20 Mar 2020 16:49:07 +0100 Subject: openpgp: Add a function to set the expiry of subkeys using the FFI. - Expose `ValidKeyAmalgamation::set_expiration_time` to the C FFI. --- openpgp-ffi/src/key_amalgamation.rs | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'openpgp-ffi/src/key_amalgamation.rs') diff --git a/openpgp-ffi/src/key_amalgamation.rs b/openpgp-ffi/src/key_amalgamation.rs index 68144e30..a7bbeeec 100644 --- a/openpgp-ffi/src/key_amalgamation.rs +++ b/openpgp-ffi/src/key_amalgamation.rs @@ -5,16 +5,24 @@ //! //! [`sequoia-openpgp::cert::key_amalgamation::KeyAmalgamation`]: ../../../sequoia_openpgp/cert/key_amalgamation/struct.KeyAmalgamation.html +use std::slice; +use libc::{size_t, time_t}; + extern crate sequoia_openpgp as openpgp; use self::openpgp::packet::key; use self::openpgp::cert::amalgamation::ValidAmalgamation; +use self::openpgp::crypto; use super::packet::key::Key; use super::packet::signature::Signature; +use super::packet::Packet; use super::revocation_status::RevocationStatus; +use crate::error::Status; use crate::MoveIntoRaw; +use crate::MoveResultIntoRaw; use crate::RefRaw; +use crate::maybe_time; /// A local alias to appease the proc macro transformation. type ErasedKeyAmalgamation<'a> = @@ -84,3 +92,45 @@ pub extern "C" fn pgp_valid_key_amalgamation_binding_signature<'a>(ka: *const Va .binding_signature() .move_into_raw() } + +/// Creates one or more self-signatures that when merged with the +/// certificate cause the key to expire at the specified time. +/// +/// The returned buffer must be freed using libc's allocator. +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C" +fn pgp_valid_key_amalgamation_set_expiration_time( + errp: Option<&mut *mut crate::error::Error>, + ka: *const ValidKeyAmalgamation, + primary_signer: *mut Box, + expiry: time_t, + packets: *mut *mut *mut Packet, packet_count: *mut size_t) + -> Status +{ + ffi_make_fry_from_errp!(errp); + + let ka = ka.ref_raw(); + let signer = ffi_param_ref_mut!(primary_signer); + let expiry = maybe_time(expiry); + let packets = ffi_param_ref_mut!(packets); + let packet_count = ffi_param_ref_mut!(packet_count); + + match ka.set_expiration_time(signer.as_mut(), expiry) { + Ok(sigs) => { + let buffer = unsafe { + libc::calloc(sigs.len(), std::mem::size_of::<*mut Packet>()) + as *mut *mut Packet + }; + let sl = unsafe { + slice::from_raw_parts_mut(buffer, sigs.len()) + }; + *packet_count = sigs.len(); + sl.iter_mut().zip(sigs.into_iter()) + .for_each(|(e, sig)| *e = sig.move_into_raw()); + *packets = buffer; + Status::Success + } + Err(err) => { + Err::<(), anyhow::Error>(err).move_into_raw(errp) + } + } +} -- cgit v1.2.3