summaryrefslogtreecommitdiff
path: root/openssl/src/cms.rs
blob: ee4b081676249987f4020e15a799a7af53a888e7 (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
//! CMS archive

use ffi;
use foreign_types::{ForeignType, ForeignTypeRef};
use std::ptr;
use error::ErrorStack;

use bio::{MemBio, MemBioSlice};

use x509::X509;
use pkey::PKeyRef;

use cvt;
use cvt_p;

foreign_type! {
    type CType = ffi::CMS_ContentInfo;
    fn drop = ffi::CMS_ContentInfo_free;

    pub struct CmsContentInfo;
    pub struct CmsContentInfoRef;
}

impl CmsContentInfoRef {
    pub fn decrypt(&self, pkey: &PKeyRef, cert: &X509) -> Result<Vec<u8>, ErrorStack> {
        unsafe {
            let pkey = pkey.as_ptr();
            let cert = cert.as_ptr();
            let out = MemBio::new()?;
            let flags: u32 = 0;

            cvt(ffi::CMS_decrypt(
                self.as_ptr(),
                pkey,
                cert,
                ptr::null_mut(),
                out.as_ptr(),
                flags.into(),
            ))?;

            Ok(out.get_buf().to_owned())
        }
    }

}

impl CmsContentInfo {
    pub fn smime_read_cms(smime: &[u8]) -> Result<CmsContentInfo, ErrorStack> {
        unsafe {
            let bio = MemBioSlice::new(smime)?;

            let cms = cvt_p(ffi::SMIME_read_CMS(
                bio.as_ptr(),
                ptr::null_mut(),
            ))?;

            Ok(CmsContentInfo::from_ptr(cms))
        }
    }
}