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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// This file was generated by gir (https://github.com/gtk-rs/gir @ fe7a6ff+)
// from gir-files (https://github.com/gtk-rs/gir-files @ b215ee8)
// DO NOT EDIT

use DecryptFlags;
use DecryptResult;
use DigestAlgo;
use Error;
use SignatureList;
use Stream;
use VerifyFlags;
use ffi;
use glib::object::IsA;
use glib::translate::*;
use glib_ffi;
use gobject_ffi;
use std::mem;
use std::ptr;

glib_wrapper! {
    /// A crypto context for use with MIME.
    ///
    /// # Implements
    ///
    /// [`CryptoContextExt`](trait.CryptoContextExt.html)
    pub struct CryptoContext(Object<ffi::GMimeCryptoContext, ffi::GMimeCryptoContextClass>);

    match fn {
        get_type => || ffi::g_mime_crypto_context_get_type(),
    }
}

impl CryptoContext {
    /// Creates a new crypto context for the specified `protocol`.
    /// ## `protocol`
    /// the crypto protocol
    ///
    /// # Returns
    ///
    /// a newly allocated `CryptoContext`.
    pub fn new(protocol: &str) -> CryptoContext {
        unsafe {
            from_glib_full(ffi::g_mime_crypto_context_new(protocol.to_glib_none().0))
        }
    }

    //pub fn register(protocol: &str, callback: /*Unknown conversion*//*Unimplemented*/CryptoContextNewFunc) {
    //    unsafe { TODO: call ffi::g_mime_crypto_context_register() }
    //}
}

/// Trait containing all `CryptoContext` methods.
///
/// # Implementors
///
/// [`CryptoContext`](struct.CryptoContext.html), [`GpgContext`](struct.GpgContext.html), [`Pkcs7Context`](struct.Pkcs7Context.html)
pub trait CryptoContextExt {
    /// Decrypts the ciphertext input stream and writes the resulting cleartext
    /// to the output stream.
    ///
    /// When non-`None`, `session_key` should be a `None`-terminated string,
    /// such as the one returned by `DecryptResultExt::get_session_key`
    /// from a previous decryption. If the `session_key` is not valid, decryption
    /// will fail.
    ///
    /// If the encrypted input stream was also signed, the returned
    /// `DecryptResult` will have a non-`None` list of signatures, each with a
    /// `SignatureStatus` (among other details about each signature).
    ///
    /// On success, the returned `DecryptResult` will contain a list of
    /// certificates, one for each recipient, that the original encrypted stream
    /// was encrypted to.
    ///
    /// Note: It *may* be possible to maliciously design an encrypted stream such
    /// that recursively decrypting it will result in an endless loop, causing
    /// a denial of service attack on your application.
    /// ## `flags`
    /// a set of `DecryptFlags`
    /// ## `session_key`
    /// the session key to use or `None`
    /// ## `istream`
    /// input/ciphertext stream
    /// ## `ostream`
    /// output/cleartext stream
    ///
    /// # Returns
    ///
    /// a `DecryptResult` on success or `None`
    /// on error.
    fn decrypt<'a, P: Into<Option<&'a str>>, Q: IsA<Stream>, R: IsA<Stream>>(&self, flags: DecryptFlags, session_key: P, istream: &Q, ostream: &R) -> Result<DecryptResult, Error>;

    /// Gets the digest id based on the digest name.
    /// ## `name`
    /// digest name
    ///
    /// # Returns
    ///
    /// the equivalent digest id or `DigestAlgo::Default` on fail.
    fn digest_id(&self, name: &str) -> DigestAlgo;

    /// Gets the digest name based on the digest id `digest`.
    /// ## `digest`
    /// digest id
    ///
    /// # Returns
    ///
    /// the equivalent digest name or `None` on fail.
    fn digest_name(&self, digest: DigestAlgo) -> Option<String>;

    //fn encrypt<'a, P: Into<Option<&'a str>>, Q: IsA<Stream>, R: IsA<Stream>>(&self, sign: bool, userid: P, flags: EncryptFlags, recipients: /*Unknown conversion*//*Unimplemented*/PtrArray TypeId { ns_id: 0, id: 28 }, istream: &Q, ostream: &R) -> Result<i32, Error>;

    /// Exports the keys/certificates in `keys` to the stream `ostream` from
    /// the key/certificate database controlled by `self`.
    ///
    /// If `keys` is `None` or contains only a `None` element, then all keys
    /// will be exported.
    /// ## `keys`
    /// an array of key ids, terminated by a `None` element
    /// ## `ostream`
    /// output stream
    ///
    /// # Returns
    ///
    /// `0` on success or %-1 on fail.
    fn export_keys<P: IsA<Stream>>(&self, keys: &str, ostream: &P) -> Result<i32, Error>;

    /// Gets the encryption protocol for the crypto context.
    ///
    /// # Returns
    ///
    /// the encryption protocol or `None` if not supported.
    fn get_encryption_protocol(&self) -> Option<String>;

    /// Gets the key exchange protocol for the crypto context.
    ///
    /// # Returns
    ///
    /// the key exchange protocol or `None` if not supported.
    fn get_key_exchange_protocol(&self) -> Option<String>;

    /// Gets the signature protocol for the crypto context.
    ///
    /// # Returns
    ///
    /// the signature protocol or `None` if not supported.
    fn get_signature_protocol(&self) -> Option<String>;

    /// Imports a stream of keys/certificates contained within `istream`
    /// into the key/certificate database controlled by `self`.
    /// ## `istream`
    /// input stream (containing keys)
    ///
    /// # Returns
    ///
    /// the total number of keys imported on success or %-1 on fail.
    fn import_keys<P: IsA<Stream>>(&self, istream: &P) -> Result<i32, Error>;

    //fn set_request_password(&self, request_passwd: /*Unknown conversion*//*Unimplemented*/PasswordRequestFunc);

    /// Signs the input stream and writes the resulting signature to the output stream.
    /// ## `detach`
    /// `true` if `ostream` should be the detached signature; otherwise, `false`
    /// ## `userid`
    /// private key to use to sign the stream
    /// ## `istream`
    /// input stream
    /// ## `ostream`
    /// output stream
    ///
    /// # Returns
    ///
    /// the `DigestAlgo` used on success or %-1 on fail.
    fn sign<P: IsA<Stream>, Q: IsA<Stream>>(&self, detach: bool, userid: &str, istream: &P, ostream: &Q) -> Result<i32, Error>;

    /// Verifies the signature. If `istream` is a clearsigned stream, you
    /// should pass `None` as the `sigstream` parameter and may wish to
    /// provide an `ostream` argument for GMime to output the original
    /// plaintext into. Otherwise `sigstream` is assumed to be the signature
    /// stream and is used to verify the integirity of the `istream`.
    /// ## `flags`
    /// a `VerifyFlags`
    /// ## `istream`
    /// input stream
    /// ## `sigstream`
    /// detached-signature stream
    /// ## `ostream`
    /// output stream for use with encapsulated signature input streams
    ///
    /// # Returns
    ///
    /// a `SignatureList` object containing
    /// the status of each signature or `None` on error.
    fn verify<'a, 'b, P: IsA<Stream>, Q: IsA<Stream> + 'a, R: Into<Option<&'a Q>>, S: IsA<Stream> + 'b, T: Into<Option<&'b S>>>(&self, flags: VerifyFlags, istream: &P, sigstream: R, ostream: T) -> Result<Option<SignatureList>, Error>;
}

impl<O: IsA<CryptoContext>> CryptoContextExt for O {
    fn decrypt<'a, P: Into<Option<&'a str>>, Q: IsA<Stream>, R: IsA<Stream>>(&self, flags: DecryptFlags, session_key: P, istream: &Q, ostream: &R) -> Result<DecryptResult, Error> {
        let session_key = session_key.into();
        let session_key = session_key.to_glib_none();
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_mime_crypto_context_decrypt(self.to_glib_none().0, flags.to_glib(), session_key.0, istream.to_glib_none().0, ostream.to_glib_none().0, &mut error);
            if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
        }
    }

    fn digest_id(&self, name: &str) -> DigestAlgo {
        unsafe {
            from_glib(ffi::g_mime_crypto_context_digest_id(self.to_glib_none().0, name.to_glib_none().0))
        }
    }

    fn digest_name(&self, digest: DigestAlgo) -> Option<String> {
        unsafe {
            from_glib_none(ffi::g_mime_crypto_context_digest_name(self.to_glib_none().0, digest.to_glib()))
        }
    }

    //fn encrypt<'a, P: Into<Option<&'a str>>, Q: IsA<Stream>, R: IsA<Stream>>(&self, sign: bool, userid: P, flags: EncryptFlags, recipients: /*Unknown conversion*//*Unimplemented*/PtrArray TypeId { ns_id: 0, id: 28 }, istream: &Q, ostream: &R) -> Result<i32, Error> {
    //    unsafe { TODO: call ffi::g_mime_crypto_context_encrypt() }
    //}

    fn export_keys<P: IsA<Stream>>(&self, keys: &str, ostream: &P) -> Result<i32, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_mime_crypto_context_export_keys(self.to_glib_none().0, keys.to_glib_none().0, ostream.to_glib_none().0, &mut error);
            if error.is_null() { Ok(ret) } else { Err(from_glib_full(error)) }
        }
    }

    fn get_encryption_protocol(&self) -> Option<String> {
        unsafe {
            from_glib_none(ffi::g_mime_crypto_context_get_encryption_protocol(self.to_glib_none().0))
        }
    }

    fn get_key_exchange_protocol(&self) -> Option<String> {
        unsafe {
            from_glib_none(ffi::g_mime_crypto_context_get_key_exchange_protocol(self.to_glib_none().0))
        }
    }

    fn get_signature_protocol(&self) -> Option<String> {
        unsafe {
            from_glib_none(ffi::g_mime_crypto_context_get_signature_protocol(self.to_glib_none().0))
        }
    }

    fn import_keys<P: IsA<Stream>>(&self, istream: &P) -> Result<i32, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_mime_crypto_context_import_keys(self.to_glib_none().0, istream.to_glib_none().0, &mut error);
            if error.is_null() { Ok(ret) } else { Err(from_glib_full(error)) }
        }
    }

    //fn set_request_password(&self, request_passwd: /*Unknown conversion*//*Unimplemented*/PasswordRequestFunc) {
    //    unsafe { TODO: call ffi::g_mime_crypto_context_set_request_password() }
    //}

    fn sign<P: IsA<Stream>, Q: IsA<Stream>>(&self, detach: bool, userid: &str, istream: &P, ostream: &Q) -> Result<i32, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_mime_crypto_context_sign(self.to_glib_none().0, detach.to_glib(), userid.to_glib_none().0, istream.to_glib_none().0, ostream.to_glib_none().0, &mut error);
            if error.is_null() { Ok(ret) } else { Err(from_glib_full(error)) }
        }
    }

    fn verify<'a, 'b, P: IsA<Stream>, Q: IsA<Stream> + 'a, R: Into<Option<&'a Q>>, S: IsA<Stream> + 'b, T: Into<Option<&'b S>>>(&self, flags: VerifyFlags, istream: &P, sigstream: R, ostream: T) -> Result<Option<SignatureList>, Error> {
        let sigstream = sigstream.into();
        let sigstream = sigstream.to_glib_none();
        let ostream = ostream.into();
        let ostream = ostream.to_glib_none();
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_mime_crypto_context_verify(self.to_glib_none().0, flags.to_glib(), istream.to_glib_none().0, sigstream.0, ostream.0, &mut error);
            if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
        }
    }
}