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
281
282
283
284
285
286
287
288
//! Envelope encryption.
//!
//! # Example
//!
//! ```rust
//!
//! extern crate openssl;
//!
//! use openssl::rsa::Rsa;
//! use openssl::envelope::Seal;
//! use openssl::pkey::PKey;
//! use openssl::symm::Cipher;
//!
//! fn main() {
//!     let rsa = Rsa::generate(2048).unwrap();
//!     let key = PKey::from_rsa(rsa).unwrap();
//!
//!     let cipher = Cipher::aes_256_cbc();
//!     let mut seal = Seal::new(cipher, &[key]).unwrap();
//!
//!     let secret = b"My secret message";
//!     let mut encrypted = vec![0; secret.len() + cipher.block_size()];
//!
//!     let mut enc_len = seal.update(secret, &mut encrypted).unwrap();
//!     enc_len += seal.finalize(&mut encrypted[enc_len..]).unwrap();
//!     encrypted.truncate(enc_len);
//! }
//! ```
use error::ErrorStack;
use ffi;
use foreign_types::{ForeignType, ForeignTypeRef};
use libc::c_int;
use pkey::{HasPrivate, HasPublic, PKey, PKeyRef};
use std::cmp;
use std::ptr;
use symm::Cipher;
use {cvt, cvt_p};

/// Represents an EVP_Seal context.
pub struct Seal {
    ctx: *mut ffi::EVP_CIPHER_CTX,
    block_size: usize,
    iv: Option<Vec<u8>>,
    enc_keys: Vec<Vec<u8>>,
}

impl Seal {
    /// Creates a new `Seal`.
    pub fn new<T>(cipher: Cipher, pub_keys: &[PKey<T>]) -> Result<Seal, ErrorStack>
    where
        T: HasPublic,
    {
        unsafe {
            assert!(pub_keys.len() <= c_int::max_value() as usize);

            let ctx = cvt_p(ffi::EVP_CIPHER_CTX_new())?;
            let mut enc_key_ptrs = vec![];
            let mut pub_key_ptrs = vec![];
            let mut enc_keys = vec![];
            for key in pub_keys {
                let mut enc_key = vec![0; key.size()];
                let enc_key_ptr = enc_key.as_mut_ptr();
                enc_keys.push(enc_key);
                enc_key_ptrs.push(enc_key_ptr);
                pub_key_ptrs.push(key.as_ptr());
            }
            let mut iv = cipher.iv_len().map(|len| vec![0; len]);
            let iv_ptr = iv.as_mut().map_or(ptr::null_mut(), |v| v.as_mut_ptr());
            let mut enc_key_lens = vec![0; enc_keys.len()];

            cvt(ffi::EVP_SealInit(
                ctx,
                cipher.as_ptr(),
                enc_key_ptrs.as_mut_ptr(),
                enc_key_lens.as_mut_ptr(),
                iv_ptr,
                pub_key_ptrs.as_mut_ptr(),
                pub_key_ptrs.len() as c_int,
            ))?;

            for (buf, len) in enc_keys.iter_mut().zip(&enc_key_lens) {
                buf.truncate(*len as usize);
            }

            Ok(Seal {
                ctx,
                block_size: cipher.block_size(),
                iv,
                enc_keys,
            })
        }
    }

    /// Returns the initialization vector, if the cipher uses one.
    #[allow(clippy::option_as_ref_deref)]
    pub fn iv(&self) -> Option<&[u8]> {
        self.iv.as_ref().map(|v| &**v)
    }

    /// Returns the encrypted keys.
    pub fn encrypted_keys(&self) -> &[Vec<u8>] {
        &self.enc_keys
    }

    /// Feeds data from `input` through the cipher, writing encrypted bytes into `output`.
    ///
    /// The number of bytes written to `output` is returned. Note that this may
    /// not be equal to the length of `input`.
    ///
    /// # Panics
    ///
    /// Panics if `output.len() < input.len() + block_size` where `block_size` is
    /// the block size of the cipher (see `Cipher::block_size`), or if
    /// `output.len() > c_int::max_value()`.
    pub fn update(&mut self, input: &[u8], output: &mut [u8]) -> Result<usize, ErrorStack> {
        unsafe {
            assert!(output.len() >= input.len() + self.block_size);
            assert!(output.len() <= c_int::max_value() as usize);
            let mut outl = output.len() as c_int;
            let inl = input.len() as c_int;
            cvt(ffi::EVP_EncryptUpdate(
                self.ctx,
                output.as_mut_ptr(),
                &mut outl,
                input.as_ptr(),
                inl,
            ))?;
            Ok(outl as usize)
        }
    }

    /// Finishes the encryption process, writing any remaining data to `output`.
    ///
    /// The number of bytes written to `output` is returned.
    ///
    /// `update` should not be called after this method.
    ///
    /// # Panics
    ///
    /// Panics if `output` is less than the cipher's block size.
    pub fn finalize(&mut self, output: &mut [u8]) -> Result<usize, ErrorStack> {
        unsafe {
            assert!(output.len() >= self.block_size);
            let mut outl = cmp::min(output.len(), c_int::max_value() as usize) as c_int;

            cvt(ffi::EVP_SealFinal(self.ctx, output.as_mut_ptr(), &mut outl))?;

            Ok(outl as usize)
        }
    }
}

impl Drop for Seal {
    fn drop(&mut self) {
        unsafe {
            ffi::EVP_CIPHER_CTX_free(self.ctx);
        }
    }
}

/// Represents an EVP_Open context.
pub struct Open {
    ctx: *mut ffi::EVP_CIPHER_CTX,
    block_size: usize,
}

impl Open {
    /// Creates a new `Open`.
    pub fn new<T>(
        cipher: Cipher,
        priv_key: &PKeyRef<T>,
        iv: Option<&[u8]>,
        encrypted_key: &[u8],
    ) -> Result<Open, ErrorStack>
    where
        T: HasPrivate,
    {
        unsafe {
            assert!(encrypted_key.len() <= c_int::max_value() as usize);
            match (cipher.iv_len(), iv) {
                (Some(len), Some(iv)) => assert_eq!(len, iv.len(), "IV length mismatch"),
                (None, None) => {}
                (Some(_), None) => panic!("an IV was required but not provided"),
                (None, Some(_)) => panic!("an IV was provided but not required"),
            }

            let ctx = cvt_p(ffi::EVP_CIPHER_CTX_new())?;
            cvt(ffi::EVP_OpenInit(
                ctx,
                cipher.as_ptr(),
                encrypted_key.as_ptr(),
                encrypted_key.len() as c_int,
                iv.map_or(ptr::null(), |v| v.as_ptr()),
                priv_key.as_ptr(),
            ))?;
            Ok(Open {
                ctx,
                block_size: cipher.block_size(),
            })
        }
    }

    /// Feeds data from `input` through the cipher, writing decrypted bytes into `output`.
    ///
    /// The number of bytes written to `output` is returned. Note that this may
    /// not be equal to the length of `input`.
    ///
    /// # Panics
    ///
    /// Panics if `output.len() < input.len() + block_size` where
    /// `block_size` is the block size of the cipher (see `Cipher::block_size`),
    /// or if `output.len() > c_int::max_value()`.
    pub fn update(&mut self, input: &[u8], output: &mut [u8]) -> Result<usize, ErrorStack> {
        unsafe {
            assert!(output.len() >= input.len() + self.block_size);
            assert!(output.len() <= c_int::max_value() as usize);
            let mut outl = output.len() as c_int;
            let inl = input.len() as c_int;
            cvt(ffi::EVP_DecryptUpdate(
                self.ctx,
                output.as_mut_ptr(),
                &mut outl,
                input.as_ptr(),
                inl,
            ))?;
            Ok(outl as usize)
        }
    }

    /// Finishes the decryption process, writing any remaining data to `output`.
    ///
    /// The number of bytes written to `output` is returned.
    ///
    /// `update` should not be called after this method.
    ///
    /// # Panics
    ///
    /// Panics if `output` is less than the cipher's block size.
    pub fn finalize(&mut self, output: &mut [u8]) -> Result<usize, ErrorStack> {
        unsafe {
            assert!(output.len() >= self.block_size);
            let mut outl = cmp::min(output.len(), c_int::max_value() as usize) as c_int;

            cvt(ffi::EVP_OpenFinal(self.ctx, output.as_mut_ptr(), &mut outl))?;

            Ok(outl as usize)
        }
    }
}

impl Drop for Open {
    fn drop(&mut self) {
        unsafe {
            ffi::EVP_CIPHER_CTX_free(self.ctx);
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use pkey::PKey;
    use symm::Cipher;

    #[test]
    fn public_encrypt_private_decrypt() {
        let private_pem = include_bytes!("../test/rsa.pem");
        let public_pem = include_bytes!("../test/rsa.pem.pub");
        let private_key = PKey::private_key_from_pem(private_pem).unwrap();
        let public_key = PKey::public_key_from_pem(public_pem).unwrap();
        let cipher = Cipher::aes_256_cbc();
        let secret = b"My secret message";

        let mut seal = Seal::new(cipher, &[public_key]).unwrap();
        let mut encrypted = vec![0; secret.len() + cipher.block_size()];
        let mut enc_len = seal.update(secret, &mut encrypted).unwrap();
        enc_len += seal.finalize(&mut encrypted[enc_len..]).unwrap();
        let iv = seal.iv();
        let encrypted_key = &seal.encrypted_keys()[0];

        let mut open = Open::new(cipher, &private_key, iv, &encrypted_key).unwrap();
        let mut decrypted = vec![0; enc_len + cipher.block_size()];
        let mut dec_len = open.update(&encrypted[..enc_len], &mut decrypted).unwrap();
        dec_len += open.finalize(&mut decrypted[dec_len..]).unwrap();

        assert_eq!(&secret[..], &decrypted[..dec_len]);
    }
}