//! Handles TPKs.
//!
//! Wraps [`sequoia-openpgp::TPK`] and [related functionality].
//!
//! [`sequoia-openpgp::TPK`]: ../../../sequoia_openpgp/struct.TPK.html
//! [related functionality]: ../../../sequoia_openpgp/tpk/index.html
use std::ffi::{CString, CStr};
use std::ptr;
use std::slice;
use std::io::{Read, Write};
use libc::{uint8_t, c_char, c_int, size_t, time_t};
extern crate sequoia_openpgp;
use self::sequoia_openpgp::{
Fingerprint,
Packet,
PacketPile,
RevocationStatus,
TPK,
TSK,
autocrypt::Autocrypt,
constants::ReasonForRevocation,
packet::{self, Signature},
parse::PacketParserResult,
parse::Parse,
serialize::Serialize,
tpk::{
CipherSuite,
KeyIter,
TPKBuilder,
UserIDBinding,
UserIDBindingIter,
},
};
use ::core::Context;
use ::error::Status;
/// Returns the first TPK encountered in the reader.
#[no_mangle]
pub extern "system" fn sq_tpk_from_reader(ctx: *mut Context,
reader: *mut Box<Read>)
-> *mut TPK {
let ctx = ffi_param_ref_mut!(ctx);
let reader = ffi_param_ref_mut!(reader);
fry_box!(ctx, TPK::from_reader(reader))
}
/// Returns the first TPK encountered in the file.
#[no_mangle]
pub extern "system" fn sq_tpk_from_file(ctx: *mut Context,
filename: *const c_char)
-> *mut TPK {
let ctx = ffi_param_ref_mut!(ctx);
assert!(! filename.is_null());
let filename = unsafe {
CStr::from_ptr(filename).to_string_lossy().into_owned()
};
fry_box!(ctx, TPK::from_file(&filename))
}
/// Returns the first TPK found in `m`.
///
/// Consumes `m`.
#[no_mangle]
pub extern "system" fn sq_tpk_from_packet_pile(ctx: *mut Context,
m: *mut PacketPile)
-> *mut TPK {
let ctx = ffi_param_ref_mut!(ctx);
let m = ffi_param_move!(m);
fry_box!(ctx, TPK::from_packet_pile(*m))
}
/// Returns the first TPK found in `buf`.
///
/// `buf` must be an OpenPGP-encoded TPK.
#[no_mangle]
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);
assert!(!b.is_null());
let buf = unsafe {
slice::from_raw_parts(b, len as usize)
};
fry_box!(ctx, TPK::from_bytes(buf))
}
/// Returns the first TPK found in the packet parser.
///
/// Consumes the packet parser result.
#[no_mangle]
pub extern "system" fn sq_tpk_from_packet_parser(ctx: *mut Context,
ppr: *mut PacketParserResult)
-> *mut TPK
{
let ctx = ffi_param_ref_mut!(ctx);
let ppr = ffi_param_move!(ppr);
fry_box!(ctx, TPK::from_packet_parser(*ppr))
}
/// Frees the TPK.
#[no_mangle]
pub extern "system" fn sq_tpk_free(tpk: *mut TPK) {
ffi_free!(tpk)
}
/// Clones the TPK.
#[no_mangle]
pub extern "system" fn sq_tpk_clone(tpk: *const TPK)
-> *mut TPK {
let tpk = ffi_param_ref!(tpk);
box_raw!(tpk.clone())
}
/// Compares TPKs.
#[no_mangle]
pub extern "system" fn sq_tpk_equal(a: *const TPK,
b: *const TPK)
-> bool {
let a = ffi_param_ref!(a);
let b = ffi_param_ref!(b);
a == b
}
/// Serializes the TPK.
#[no_mangle]
pub extern "system" fn sq_tpk_serialize(ctx: *mut Context,
tpk: *const TPK,
writer: *mut Box<Write>)
-> Status {
let ctx = ffi_param_ref_mut!(ctx);
let tpk = ffi_param_ref!(tpk);
let writer = ffi_param_ref_mut!(writer);
fry_status!(ctx, tpk.serialize(writer))
}
/// Merges `other` into `tpk`.
/