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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
extern crate libc;
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate gpg_error;
extern crate gpgme_sys as ffi;

use std::ffi::{CStr, CString};
use std::fmt;
use std::mem;
use std::ptr;
use std::sync::{Arc, RwLock};

use self::engine::EngineInfoGuard;

pub use gpg_error as error;
pub use self::error::{Error, Result};
pub use self::context::Context;
pub use self::data::Data;
pub use self::utils::{StrError, StrResult};

#[macro_use]
mod utils;
pub mod engine;
pub mod context;
pub mod data;
pub mod keys;
pub mod trust;
pub mod notation;
pub mod edit;
pub mod ops;

/// Constants for use with `Token::get_dir_info`.
pub mod info {
    pub const HOME_DIR: &'static str = "homedir";
    pub const AGENT_SOCKET: &'static str = "agent-socket";
    pub const UISERVER_SOCKET: &'static str = "uiserver-socket";
    pub const GPGCONF_NAME: &'static str = "gpgconf-name";
    pub const GPG_NAME: &'static str = "gpg-name";
    pub const GPGSM_NAME: &'static str = "gpgsm-name";
    pub const G13_NAME: &'static str = "g13-name";
}

ffi_enum_wrapper! {
    #[doc="A cryptographic protocol that may be used with the library."]
    #[doc=""]
    #[doc="Each protocol is implemented by an engine that the library communicates with"]
    #[doc="to perform various operations."]
    pub enum Protocol: ffi::gpgme_protocol_t {
        PROTOCOL_OPENPGP = ffi::GPGME_PROTOCOL_OpenPGP,
        PROTOCOL_CMS = ffi::GPGME_PROTOCOL_CMS,
        PROTOCOL_GPGCONF = ffi::GPGME_PROTOCOL_GPGCONF,
        PROTOCOL_ASSUAN = ffi::GPGME_PROTOCOL_ASSUAN,
        PROTOCOL_G13 = ffi::GPGME_PROTOCOL_G13,
        PROTOCOL_UISERVER = ffi::GPGME_PROTOCOL_UISERVER,
        PROTOCOL_SPAWN = ffi::GPGME_PROTOCOL_SPAWN,
        PROTOCOL_DEFAULT = ffi::GPGME_PROTOCOL_DEFAULT,
        PROTOCOL_UNKNOWN = ffi::GPGME_PROTOCOL_UNKNOWN,
    }
}

impl Protocol {
    pub fn name(&self) -> StrResult<'static> {
        unsafe { utils::from_cstr(ffi::gpgme_get_protocol_name(self.0)) }
    }
}

impl fmt::Display for Protocol {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.name().unwrap_or("Unknown"))
    }
}

ffi_enum_wrapper! {
    pub enum Validity: ffi::gpgme_validity_t {
        VALIDITY_UNKNOWN = ffi::GPGME_VALIDITY_UNKNOWN,
        VALIDITY_UNDEFINED = ffi::GPGME_VALIDITY_UNDEFINED,
        VALIDITY_NEVER = ffi::GPGME_VALIDITY_NEVER,
        VALIDITY_MARGINAL = ffi::GPGME_VALIDITY_MARGINAL,
        VALIDITY_FULL = ffi::GPGME_VALIDITY_FULL,
        VALIDITY_ULTIMATE = ffi::GPGME_VALIDITY_ULTIMATE,
    }
}

impl fmt::Display for Validity {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            VALIDITY_UNDEFINED => write!(f, "q"),
            VALIDITY_NEVER => write!(f, "n"),
            VALIDITY_MARGINAL => write!(f, "m"),
            VALIDITY_FULL => write!(f, "f"),
            VALIDITY_ULTIMATE => write!(f, "u"),
            _ => write!(f, "?"),
        }
    }
}

struct TokenImp {
    version: &'static str,
    engine_info: RwLock<()>,
}

impl fmt::Debug for TokenImp {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Token")
    }
}

lazy_static! {
    static ref TOKEN: Token = {
        let version = unsafe {
            let base: ffi::_gpgme_signature = mem::zeroed();
            let offset = (&base.validity as *const _ as usize) - (&base as *const _ as usize);

            let result = ffi::gpgme_check_version_internal(ptr::null(), offset as libc::size_t);
            assert!(!result.is_null(), "gpgme library could not be initialized");
            CStr::from_ptr(result).to_str().expect("gpgme version string is not valid utf-8")
        };
        Token(Arc::new(TokenImp { version: version, engine_info: RwLock::new(()) }))
    };
}

/// Initializes the gpgme library.
///
///
/// # Examples
///
/// ```no_run
/// let gpgme = gpgme::init();
/// ```
pub fn init() -> Token {
    TOKEN.clone()
}

/// Creates a new context for cryptographic operations.
///
/// # Examples
///
/// ```no_run
/// let mut ctx = gpgme::create_context().unwrap();
/// ```
pub fn create_context() -> Result<Context> {
    Context::new(init())
}

/// A type for managing global resources within the library.
#[derive(Debug, Clone)]
pub struct Token(Arc<TokenImp>);

impl Token {
    /// Checks that the linked version of the library is at least the
    /// specified version.
    ///
    /// Note: `false` is returned, if `version` is not in the format `MAJOR.MINOR.MICRO`.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let gpgme = gpgme::init();
    /// assert!(gpgme.check_version("1.4.0"));
    /// ```
    pub fn check_version<S: Into<Vec<u8>>>(&self, version: S) -> bool {
        let version = match CString::new(version) {
            Ok(v) => v,
            Err(..) => return false,
        };
        unsafe { !ffi::gpgme_check_version(version.as_ptr()).is_null() }
    }

    /// Returns the version string for the library.
    pub fn version(&self) -> &'static str {
        self.0.version
    }

    /// Returns the default value for specified configuration option.
    ///
    /// Commonly supported values for `what` are specified in [`info`](info/).
    ///
    /// This function requires a version of GPGme >= 1.5.0.
    pub fn get_dir_info<S: Into<Vec<u8>>>(&self, what: S) -> utils::StrResult<'static> {
        let what = try!(CString::new(what).or(Err(StrError::NotPresent)));
        unsafe { utils::from_cstr(ffi::gpgme_get_dirinfo(what.as_ptr())) }
    }

    /// Checks that the engine implementing the specified protocol is supported by the library.
    pub fn check_engine_version(&self, proto: Protocol) -> Result<()> {
        unsafe {
            return_err!(ffi::gpgme_engine_check_version(proto.raw()));
        }
        Ok(())
    }

    pub fn engine_info(&self) -> Result<EngineInfoGuard> {
        EngineInfoGuard::new(&TOKEN)
    }

    pub fn set_engine_filename<S>(&self, proto: Protocol, filename: S) -> Result<()>
    where S: Into<Vec<u8>> {
        let filename = try!(CString::new(filename));
        unsafe {
            let _lock = self.0.engine_info.write().expect("Engine info lock could not be acquired");
            return_err!(ffi::gpgme_set_engine_info(proto.raw(), filename.as_ptr(), ptr::null()));
        }
        Ok(())
    }

    pub fn set_engine_home_dir<S>(&self, proto: Protocol, home_dir: S) -> Result<()>
    where S: Into<Vec<u8>> {
        let home_dir = try!(CString::new(home_dir));
        unsafe {
            let _lock = self.0.engine_info.write().expect("Engine info lock could not be acquired");
            return_err!(ffi::gpgme_set_engine_info(proto.raw(), ptr::null(), home_dir.as_ptr()));
        }
        Ok(())
    }

    pub fn set_engine_info<S1, S2>(&self, proto: Protocol, filename: S1, home_dir: S2) -> Result<()>
    where S1: Into<Vec<u8>>, S2: Into<Vec<u8>> {
        let filename = try!(CString::new(filename));
        let home_dir = try!(CString::new(home_dir));
        unsafe {
            let filename = filename.as_ptr();
            let home_dir = home_dir.as_ptr();
            let _lock = self.0.engine_info.write().expect("Engine info lock could not be acquired");
            return_err!(ffi::gpgme_set_engine_info(proto.raw(), filename, home_dir));
        }
        Ok(())
    }
}

pub unsafe trait Wrapper {
    type Raw: Copy;

    unsafe fn from_raw(raw: Self::Raw) -> Self;
    fn as_raw(&self) -> Self::Raw;
    fn into_raw(self) -> Self::Raw
    where Self: Sized {
        let result = self.as_raw();
        mem::forget(self);
        result
    }
}