summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi/src/tsk.rs
blob: 1eb209696b3f8a58044100ca240700be36766169 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Transferable secret keys.
//!
//! Wraps [`sequoia-openpgp::TSK`].
//!
//! [`sequoia-openpgp::TSK`]: ../../sequoia_openpgp/struct.TSK.html

use failure;
use libc::c_char;

extern crate sequoia_openpgp as openpgp;
use self::openpgp::{
    packet::Signature,
    parse::Parse,
    serialize::Serialize,
};

use super::tpk::TPK;
use ::error::Status;
use RefRaw;
use MoveFromRaw;
use MoveIntoRaw;
use MoveResultIntoRaw;

/// A transferable secret key (TSK).
///
/// A TSK (see [RFC 4880, section 11.2]) can be used to create
/// signatures and decrypt data.
///
/// [RFC 4880, section 11.2]: https://tools.ietf.org/html/rfc4880#section-11.2
///
/// Wraps [`sequoia-openpgp::TSK`].
///
/// [`sequoia-openpgp::TSK`]: ../../sequoia_openpgp/enum.TSK.html
#[::ffi_wrapper_type(prefix = "pgp_", name = "tsk",
                     derive = "Clone, Debug, PartialEq, Parse, Serialize")]
pub struct TSK(openpgp::TSK);

/// Generates a new RSA 3072 bit key with UID `primary_uid`.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system"
fn pgp_tsk_new(errp: Option<&mut *mut ::error::Error>,
               primary_uid: *const c_char,
               tsk_out: *mut *mut TSK,
               revocation_out: *mut *mut Signature)
               -> Status
{
    let tsk_out = ffi_param_ref_mut!(tsk_out);
    let revocation_out = ffi_param_ref_mut!(revocation_out);
    let primary_uid = ffi_param_cstr!(primary_uid).to_string_lossy();
    match openpgp::TSK::new(primary_uid) {
        Ok((tsk, revocation)) => {
            *tsk_out = tsk.move_into_raw();
            *revocation_out = box_raw!(revocation);
            Status::Success
        },
        Err(e) => Err::<(), failure::Error>(e).move_into_raw(errp),
    }
}

/// Returns a reference to the corresponding TPK.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system"
fn pgp_tsk_tpk(tsk: *const TSK)
               -> *const TPK {
    tsk.ref_raw().tpk().move_into_raw()
}

/// Converts the TSK into a TPK.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system"
fn pgp_tsk_into_tpk(tsk: *mut TSK)
                    -> *mut TPK {
    tsk.move_from_raw().into_tpk().move_into_raw()
}