diff options
author | Justus Winter <justus@pep-project.org> | 2017-12-07 14:42:41 +0100 |
---|---|---|
committer | Justus Winter <justus@pep-project.org> | 2017-12-07 18:53:18 +0100 |
commit | 9826b979eeeaf385d5bbf5f3a5b877c698273709 (patch) | |
tree | ea0676e3d8a50f4219e8c0ff07fe5bbfefd6ae7c /src | |
parent | 4eddd4d175860139fa3f477ca8c426e92ffa7b50 (diff) |
Add types for working with OpenPGP.
- Add KeyId to identify keys.
Diffstat (limited to 'src')
-rw-r--r-- | src/ffi.rs | 31 | ||||
-rw-r--r-- | src/openpgp/mod.rs | 1 | ||||
-rw-r--r-- | src/openpgp/types.rs | 21 | ||||
-rw-r--r-- | src/sequoia.h | 9 |
4 files changed, 59 insertions, 3 deletions
@@ -1,5 +1,5 @@ extern crate libc; -use self::libc::{uint8_t, c_char, size_t}; +use self::libc::{uint8_t, uint64_t, c_char, size_t}; use std::ffi::CStr; use std::ptr; @@ -8,6 +8,7 @@ use std::str; use keys::TPK; use openpgp; +use openpgp::types::KeyId; use super::Context; /// Create a context object. @@ -60,6 +61,34 @@ pub extern "system" fn sq_context_free(context: *mut Context) { } } +/* openpgp::types. */ +/// Returns a KeyID with the given `id`. +#[no_mangle] +pub extern "system" fn sq_keyid_new(id: uint64_t) -> *mut KeyId { + Box::into_raw(Box::new(KeyId::new(id))) +} + +/// Returns a KeyID with the given `id` encoded as hexadecimal string. +#[no_mangle] +pub extern "system" fn sq_keyid_from_hex(id: *const c_char) -> *mut KeyId { + if id.is_null() { return ptr::null_mut() } + let id = unsafe { CStr::from_ptr(id).to_string_lossy() }; + KeyId::from_hex(&id) + .map(|id| Box::into_raw(Box::new(id))) + .unwrap_or(ptr::null_mut()) +} + +/// Frees an `KeyId` object. +#[no_mangle] +pub extern "system" fn sq_keyid_free(keyid: *mut KeyId) { + if keyid.is_null() { return } + unsafe { + drop(Box::from_raw(keyid)); + } +} + + +/* keys. */ #[no_mangle] pub extern "system" fn sq_tpk_from_bytes(b: *const uint8_t, len: size_t) -> *mut TPK { assert!(!b.is_null()); diff --git a/src/openpgp/mod.rs b/src/openpgp/mod.rs index d0175ea5..683b8448 100644 --- a/src/openpgp/mod.rs +++ b/src/openpgp/mod.rs @@ -4,5 +4,6 @@ // wrong place). Hence, that here as well. pub mod parse; +pub mod types; include!("openpgp.rs"); diff --git a/src/openpgp/types.rs b/src/openpgp/types.rs new file mode 100644 index 00000000..5ad32631 --- /dev/null +++ b/src/openpgp/types.rs @@ -0,0 +1,21 @@ +//! Types for working with OpenPGP. + +/// Uniquely identifies OpenPGP keys. +pub struct KeyId(u64); + +impl KeyId { + /// Returns a KeyID with the given `id`. + pub fn new(id: u64) -> KeyId { + KeyId(id) + } + + /// Returns a KeyID with the given `id` encoded as hexadecimal string. + pub fn from_hex(id: &str) -> Option<KeyId> { + u64::from_str_radix(id, 16).ok().map(|id| Self::new(id)) + } + + /// Returns a hexadecimal representation of the key id. + pub fn as_hex(&self) -> String { + format!("{:x}", self.0) + } +} diff --git a/src/sequoia.h b/src/sequoia.h index 4d43d99c..51ac441b 100644 --- a/src/sequoia.h +++ b/src/sequoia.h @@ -1,14 +1,19 @@ #ifndef SEQUOIA_H #define SEQUOIA_H -struct sq_context; +#include <stdint.h> +struct sq_context; struct sq_context *sq_context_new(const char *domain, const char *home, const char *lib); void sq_context_free(struct sq_context *context); -struct sq_tpk; +struct sq_keyid; +struct sq_keyid *sq_keyid_new (uint64_t id); +struct sq_keyid *sq_keyid_from_hex (const char *id); +void sq_keyid_free (struct sq_keyid *keyid); +struct sq_tpk; struct sq_tpk *sq_tpk_from_bytes (const char *b, size_t len); void sq_tpk_dump (const struct sq_tpk *tpk); void sq_tpk_free (struct sq_tpk *tpk); |