//! 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: Option<&mut Context>,
reader: Option<&mut Box<Read>>)
-> *mut TPK {
let ctx = ffi_param_ref!(ctx);
let reader = ffi_param_ref!(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: Option<&mut Context>,
filename: *const c_char)
-> *mut TPK {
let ctx = ffi_param_ref!(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: Option<&mut Context>,
m: *mut PacketPile)
-> *mut TPK {
let ctx = ffi_param_ref!(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: Option<&mut Context>,
b: *const uint8_t, len: size_t)
-> *mut TPK {
let ctx = ffi_param_ref!(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: Option<&mut Context>,
ppr: *mut PacketParserResult)
-> *mut TPK
{
let ctx = ffi_param_ref!(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: Option<&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: Option<&TPK>,
b: Option<&TPK>)
-> bool {
let a = a.expect("TPK 'a' is NULL");
let b = b.expect("TPK 'b' is NULL");
a == b
}
/// Serializes the TPK.
#[no_mangle]
pub extern "system" fn sq_tpk_serialize(ctx: Option<&mut Context>,
tpk: Option<&TPK>,
writer: Option<&mut Box<Write>>)
-> Status {
let ctx = ffi_param_ref!(ctx);
let tpk = ffi_param_ref!(tpk);
let writer =