Skip to content

Commit 04ada47

Browse files
committed
Cleanup
1 parent 8ae7610 commit 04ada47

File tree

1 file changed

+100
-79
lines changed

1 file changed

+100
-79
lines changed

openssl/src/pkcs7.rs

Lines changed: 100 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
use x509::{X509, X509Ref};
2-
use x509::store::X509Store;
3-
use ffi;
41
use bio::{MemBio, MemBioSlice};
52
use error::ErrorStack;
6-
use stack::Stack;
7-
use foreign_types::ForeignType;
8-
use symm::Cipher;
9-
use pkey::{HasPrivate, PKeyRef};
10-
use libc::c_int;
11-
use std::ptr::null_mut;
3+
use ffi;
124
use foreign_types::ForeignTypeRef;
5+
use libc::c_int;
6+
use pkey::{HasPrivate, PKeyRef};
7+
use stack::StackRef;
8+
use std::ptr;
9+
use symm::Cipher;
10+
use x509::store::X509StoreRef;
11+
use x509::{X509Ref, X509};
1312
use {cvt, cvt_p};
1413

1514
foreign_type_and_impl_send_sync! {
@@ -63,27 +62,28 @@ impl Pkcs7 {
6362
}
6463

6564
/// Parses a message in S/MIME format.
66-
///
65+
///
6766
/// Returns the loaded signature, along with the cleartext message (if
6867
/// available).
6968
///
7069
/// This corresponds to [`SMIME_read_PKCS7`].
7170
///
7271
/// [`SMIME_read_PKCS7`]: https://www.openssl.org/docs/man1.1.0/crypto/SMIME_read_PKCS7.html
73-
pub fn from_smime(input: &[u8]) -> Result<(Self, Option<Vec<u8>>), ErrorStack> {
72+
pub fn from_smime(input: &[u8]) -> Result<(Pkcs7, Option<Vec<u8>>), ErrorStack> {
7473
ffi::init();
7574

7675
let input_bio = MemBioSlice::new(input)?;
77-
let mut bcont_bio = null_mut();
76+
let mut bcont_bio = ptr::null_mut();
7877
unsafe {
79-
let pkcs7= cvt_p(ffi::SMIME_read_PKCS7(input_bio.as_ptr(), &mut bcont_bio))?;
78+
let pkcs7 =
79+
cvt_p(ffi::SMIME_read_PKCS7(input_bio.as_ptr(), &mut bcont_bio)).map(Pkcs7)?;
8080
let out = if !bcont_bio.is_null() {
8181
let bcont_bio = MemBio::from_ptr(bcont_bio);
8282
Some(bcont_bio.get_buf().to_vec())
8383
} else {
8484
None
8585
};
86-
Ok((Pkcs7::from_ptr(pkcs7), out))
86+
Ok((pkcs7, out))
8787
}
8888
}
8989

@@ -96,18 +96,21 @@ impl Pkcs7 {
9696
/// This corresponds to [`PKCS7_encrypt`].
9797
///
9898
/// [`PKCS7_encrypt`]: https://www.openssl.org/docs/man1.0.2/crypto/PKCS7_encrypt.html
99-
pub fn encrypt(certs: &Stack<X509>, input: &[u8], cipher: Cipher, flags: Pkcs7Flags) -> Result<Self, ErrorStack> {
100-
ffi::init();
101-
99+
pub fn encrypt(
100+
certs: &StackRef<X509>,
101+
input: &[u8],
102+
cipher: Cipher,
103+
flags: Pkcs7Flags,
104+
) -> Result<Pkcs7, ErrorStack> {
102105
let input_bio = MemBioSlice::new(input)?;
103106

104107
unsafe {
105108
cvt_p(ffi::PKCS7_encrypt(
106109
certs.as_ptr(),
107110
input_bio.as_ptr(),
108111
cipher.as_ptr(),
109-
flags.bits)
110-
).map(|p| Pkcs7::from_ptr(p))
112+
flags.bits,
113+
)).map(Pkcs7)
111114
}
112115
}
113116

@@ -124,24 +127,22 @@ impl Pkcs7 {
124127
pub fn sign<PT>(
125128
signcert: &X509Ref,
126129
pkey: &PKeyRef<PT>,
127-
certs: &Stack<X509>,
130+
certs: &StackRef<X509>,
128131
input: &[u8],
129-
flags: Pkcs7Flags
130-
) -> Result<Self, ErrorStack>
132+
flags: Pkcs7Flags,
133+
) -> Result<Pkcs7, ErrorStack>
131134
where
132-
PT: HasPrivate
135+
PT: HasPrivate,
133136
{
134-
ffi::init();
135-
136137
let input_bio = MemBioSlice::new(input)?;
137138
unsafe {
138139
cvt_p(ffi::PKCS7_sign(
139140
signcert.as_ptr(),
140141
pkey.as_ptr(),
141142
certs.as_ptr(),
142143
input_bio.as_ptr(),
143-
flags.bits)
144-
).map(|p| Pkcs7::from_ptr(p))
144+
flags.bits,
145+
)).map(Pkcs7)
145146
}
146147
}
147148
}
@@ -152,26 +153,16 @@ impl Pkcs7Ref {
152153
/// This corresponds to [`SMIME_write_PKCS7`].
153154
///
154155
/// [`SMIME_write_PKCS7`]: https://www.openssl.org/docs/man1.1.0/crypto/SMIME_write_PKCS7.html
155-
pub fn to_smime(
156-
&self,
157-
input: &[u8],
158-
flags: Pkcs7Flags
159-
) -> Result<Vec<u8>, ErrorStack>
160-
{
161-
ffi::init();
162-
156+
pub fn to_smime(&self, input: &[u8], flags: Pkcs7Flags) -> Result<Vec<u8>, ErrorStack> {
163157
let input_bio = MemBioSlice::new(input)?;
164158
let output = MemBio::new()?;
165159
unsafe {
166-
cvt(
167-
ffi::SMIME_write_PKCS7(
168-
output.as_ptr(),
169-
self.as_ptr(),
170-
input_bio.as_ptr(),
171-
flags.bits)
172-
).and(
173-
Ok(output.get_buf().to_owned())
174-
)
160+
cvt(ffi::SMIME_write_PKCS7(
161+
output.as_ptr(),
162+
self.as_ptr(),
163+
input_bio.as_ptr(),
164+
flags.bits,
165+
)).map(|_| output.get_buf().to_owned())
175166
}
176167
}
177168

@@ -197,17 +188,25 @@ impl Pkcs7Ref {
197188
/// This corresponds to [`PKCS7_decrypt`].
198189
///
199190
/// [`PKCS7_decrypt`]: https://www.openssl.org/docs/man1.0.2/crypto/PKCS7_decrypt.html
200-
pub fn decrypt<PT>(&self, pkey: &PKeyRef<PT>, cert: &X509Ref) -> Result<Vec<u8>, ErrorStack>
201-
where
202-
PT: HasPrivate
191+
pub fn decrypt<PT>(
192+
&self,
193+
pkey: &PKeyRef<PT>,
194+
cert: &X509Ref,
195+
flags: Pkcs7Flags,
196+
) -> Result<Vec<u8>, ErrorStack>
197+
where
198+
PT: HasPrivate,
203199
{
204-
ffi::init();
205-
206200
let output = MemBio::new()?;
207201

208202
unsafe {
209-
cvt(ffi::PKCS7_decrypt(self.as_ptr(), pkey.as_ptr(), cert.as_ptr(), output.as_ptr(), 0))
210-
.and(Ok(output.get_buf().to_owned()))
203+
cvt(ffi::PKCS7_decrypt(
204+
self.as_ptr(),
205+
pkey.as_ptr(),
206+
cert.as_ptr(),
207+
output.as_ptr(),
208+
flags.bits,
209+
)).map(|_| output.get_buf().to_owned())
211210
}
212211
}
213212

@@ -223,50 +222,48 @@ impl Pkcs7Ref {
223222
/// [`PKCS7_verify`]: https://www.openssl.org/docs/man1.0.2/crypto/PKCS7_verify.html
224223
pub fn verify(
225224
&self,
226-
certs: &Stack<X509>,
227-
store: &X509Store,
225+
certs: &StackRef<X509>,
226+
store: &X509StoreRef,
228227
indata: Option<&[u8]>,
229228
out: Option<&mut Vec<u8>>,
230-
flags: Pkcs7Flags
229+
flags: Pkcs7Flags,
231230
) -> Result<(), ErrorStack> {
232-
ffi::init();
233-
234231
let out_bio = MemBio::new()?;
235232

236233
let indata_bio = match indata {
237234
Some(data) => Some(MemBioSlice::new(data)?),
238235
None => None,
239236
};
240-
let indata_bio_ptr = indata_bio.as_ref().map_or(null_mut(), |p| p.as_ptr());
237+
let indata_bio_ptr = indata_bio.as_ref().map_or(ptr::null_mut(), |p| p.as_ptr());
241238

242-
let result = unsafe {
239+
unsafe {
243240
cvt(ffi::PKCS7_verify(
244241
self.as_ptr(),
245242
certs.as_ptr(),
246243
store.as_ptr(),
247244
indata_bio_ptr,
248245
out_bio.as_ptr(),
249-
flags.bits))
250-
.map(|_r| ())
251-
};
246+
flags.bits,
247+
)).map(|_| ())?
248+
}
252249

253250
if let Some(data) = out {
254251
data.clear();
255-
data.append(&mut out_bio.get_buf().to_vec());
252+
data.extend_from_slice(out_bio.get_buf());
256253
}
257254

258-
result
255+
Ok(())
259256
}
260257
}
261258

262259
#[cfg(test)]
263260
mod tests {
264-
use x509::X509;
265-
use x509::store::X509StoreBuilder;
266-
use symm::Cipher;
267261
use pkcs7::{Pkcs7, Pkcs7Flags};
268262
use pkey::PKey;
269263
use stack::Stack;
264+
use symm::Cipher;
265+
use x509::store::X509StoreBuilder;
266+
use x509::X509;
270267

271268
#[test]
272269
fn encrypt_decrypt_test() {
@@ -280,13 +277,18 @@ mod tests {
280277
let pkey = include_bytes!("../test/key.pem");
281278
let pkey = PKey::private_key_from_pem(pkey).unwrap();
282279

283-
let pkcs7 = Pkcs7::encrypt(&certs, message.as_bytes(), cypher, flags).expect("should succeed");
280+
let pkcs7 =
281+
Pkcs7::encrypt(&certs, message.as_bytes(), cypher, flags).expect("should succeed");
284282

285-
let encrypted = pkcs7.to_smime(message.as_bytes(), flags).expect("should succeed");
283+
let encrypted = pkcs7
284+
.to_smime(message.as_bytes(), flags)
285+
.expect("should succeed");
286286

287287
let (pkcs7_decoded, _) = Pkcs7::from_smime(encrypted.as_slice()).expect("should succeed");
288288

289-
let decoded = pkcs7_decoded.decrypt(&pkey, &cert).expect("should succeed");
289+
let decoded = pkcs7_decoded
290+
.decrypt(&pkey, &cert, Pkcs7Flags::empty())
291+
.expect("should succeed");
290292

291293
assert_eq!(decoded, message.into_bytes());
292294
}
@@ -308,18 +310,31 @@ mod tests {
308310

309311
let store = store_builder.build();
310312

311-
let pkcs7 = Pkcs7::sign(&cert, &pkey, &certs, message.as_bytes(), flags).expect("should succeed");
313+
let pkcs7 =
314+
Pkcs7::sign(&cert, &pkey, &certs, message.as_bytes(), flags).expect("should succeed");
312315

313-
let signed = pkcs7.to_smime(message.as_bytes(), flags).expect("should succeed");
316+
let signed = pkcs7
317+
.to_smime(message.as_bytes(), flags)
318+
.expect("should succeed");
314319
println!("{:?}", String::from_utf8(signed.clone()).unwrap());
315-
let (pkcs7_decoded, content) = Pkcs7::from_smime(signed.as_slice()).expect("should succeed");
320+
let (pkcs7_decoded, content) =
321+
Pkcs7::from_smime(signed.as_slice()).expect("should succeed");
316322

317323
let mut output = Vec::new();
318-
pkcs7_decoded.verify(&certs, &store, Some(message.as_bytes()), Some(&mut output), flags)
319-
.expect("should succeed");
324+
pkcs7_decoded
325+
.verify(
326+
&certs,
327+
&store,
328+
Some(message.as_bytes()),
329+
Some(&mut output),
330+
flags,
331+
).expect("should succeed");
320332

321333
assert_eq!(message.clone().into_bytes(), output);
322-
assert_eq!(message.clone().into_bytes(), content.expect("should be non-empty"));
334+
assert_eq!(
335+
message.clone().into_bytes(),
336+
content.expect("should be non-empty")
337+
);
323338
}
324339

325340
#[test]
@@ -339,14 +354,20 @@ mod tests {
339354

340355
let store = store_builder.build();
341356

342-
let pkcs7 = Pkcs7::sign(&cert, &pkey, &certs, message.as_bytes(), flags).expect("should succeed");
357+
let pkcs7 =
358+
Pkcs7::sign(&cert, &pkey, &certs, message.as_bytes(), flags).expect("should succeed");
343359

344-
let signed = pkcs7.to_smime(message.as_bytes(), flags).expect("should succeed");
360+
let signed = pkcs7
361+
.to_smime(message.as_bytes(), flags)
362+
.expect("should succeed");
345363

346-
let (pkcs7_decoded, content) = Pkcs7::from_smime(signed.as_slice()).expect("should succeed");
364+
let (pkcs7_decoded, content) =
365+
Pkcs7::from_smime(signed.as_slice()).expect("should succeed");
347366

348367
let mut output = Vec::new();
349-
pkcs7_decoded.verify(&certs, &store, None, Some(&mut output), flags).expect("should succeed");
368+
pkcs7_decoded
369+
.verify(&certs, &store, None, Some(&mut output), flags)
370+
.expect("should succeed");
350371

351372
assert_eq!(message.clone().into_bytes(), output);
352373
assert!(content.is_none());

0 commit comments

Comments
 (0)