//! 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 = ctx.expect("Context is NULL");
let reader = reader.expect("Reader is NULL");
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 = ctx.expect("Context is NULL");
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 = ctx.expect("Context is NULL");
assert!(! m.is_null());
let m = unsafe { Box::from_raw(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 = ctx.expect("Context is NULL");
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 = ctx.expect("Context is NULL");
assert!(! ppr.is_null());
let ppr = unsafe { Box::from_raw(ppr) };
fry_box!(ctx, TPK::from_packet_parser(*ppr))
}
/// Frees the TPK.
#[no_mangle]
pub extern "system" fn sq_tpk_free(tpk: *mut TPK) {
if tpk.is_null() {
return
}
unsafe {
drop(Box::from_raw(tpk));
}
}
/// Clones the TPK.
#[no_mangle]
pub extern "system" fn sq_tpk_clone(tpk: Option<&TPK>)
-> *mut TPK {
let tpk = tpk.expect("TPK is NULL");
box_raw!(