1
- use x509:: { X509 , X509Ref } ;
2
- use x509:: store:: X509Store ;
3
- use ffi;
4
1
use bio:: { MemBio , MemBioSlice } ;
5
2
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;
12
4
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 } ;
13
12
use { cvt, cvt_p} ;
14
13
15
14
foreign_type_and_impl_send_sync ! {
@@ -63,27 +62,28 @@ impl Pkcs7 {
63
62
}
64
63
65
64
/// Parses a message in S/MIME format.
66
- ///
65
+ ///
67
66
/// Returns the loaded signature, along with the cleartext message (if
68
67
/// available).
69
68
///
70
69
/// This corresponds to [`SMIME_read_PKCS7`].
71
70
///
72
71
/// [`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 > {
74
73
ffi:: init ( ) ;
75
74
76
75
let input_bio = MemBioSlice :: new ( input) ?;
77
- let mut bcont_bio = null_mut ( ) ;
76
+ let mut bcont_bio = ptr :: null_mut ( ) ;
78
77
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 ) ?;
80
80
let out = if !bcont_bio. is_null ( ) {
81
81
let bcont_bio = MemBio :: from_ptr ( bcont_bio) ;
82
82
Some ( bcont_bio. get_buf ( ) . to_vec ( ) )
83
83
} else {
84
84
None
85
85
} ;
86
- Ok ( ( Pkcs7 :: from_ptr ( pkcs7) , out) )
86
+ Ok ( ( pkcs7, out) )
87
87
}
88
88
}
89
89
@@ -96,18 +96,21 @@ impl Pkcs7 {
96
96
/// This corresponds to [`PKCS7_encrypt`].
97
97
///
98
98
/// [`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 > {
102
105
let input_bio = MemBioSlice :: new ( input) ?;
103
106
104
107
unsafe {
105
108
cvt_p ( ffi:: PKCS7_encrypt (
106
109
certs. as_ptr ( ) ,
107
110
input_bio. as_ptr ( ) ,
108
111
cipher. as_ptr ( ) ,
109
- flags. bits )
110
- ) . map ( |p| Pkcs7 :: from_ptr ( p ) )
112
+ flags. bits ,
113
+ ) ) . map ( Pkcs7 )
111
114
}
112
115
}
113
116
@@ -124,24 +127,22 @@ impl Pkcs7 {
124
127
pub fn sign < PT > (
125
128
signcert : & X509Ref ,
126
129
pkey : & PKeyRef < PT > ,
127
- certs : & Stack < X509 > ,
130
+ certs : & StackRef < X509 > ,
128
131
input : & [ u8 ] ,
129
- flags : Pkcs7Flags
130
- ) -> Result < Self , ErrorStack >
132
+ flags : Pkcs7Flags ,
133
+ ) -> Result < Pkcs7 , ErrorStack >
131
134
where
132
- PT : HasPrivate
135
+ PT : HasPrivate ,
133
136
{
134
- ffi:: init ( ) ;
135
-
136
137
let input_bio = MemBioSlice :: new ( input) ?;
137
138
unsafe {
138
139
cvt_p ( ffi:: PKCS7_sign (
139
140
signcert. as_ptr ( ) ,
140
141
pkey. as_ptr ( ) ,
141
142
certs. as_ptr ( ) ,
142
143
input_bio. as_ptr ( ) ,
143
- flags. bits )
144
- ) . map ( |p| Pkcs7 :: from_ptr ( p ) )
144
+ flags. bits ,
145
+ ) ) . map ( Pkcs7 )
145
146
}
146
147
}
147
148
}
@@ -152,26 +153,16 @@ impl Pkcs7Ref {
152
153
/// This corresponds to [`SMIME_write_PKCS7`].
153
154
///
154
155
/// [`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 > {
163
157
let input_bio = MemBioSlice :: new ( input) ?;
164
158
let output = MemBio :: new ( ) ?;
165
159
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 ( ) )
175
166
}
176
167
}
177
168
@@ -197,17 +188,25 @@ impl Pkcs7Ref {
197
188
/// This corresponds to [`PKCS7_decrypt`].
198
189
///
199
190
/// [`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 ,
203
199
{
204
- ffi:: init ( ) ;
205
-
206
200
let output = MemBio :: new ( ) ?;
207
201
208
202
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 ( ) )
211
210
}
212
211
}
213
212
@@ -223,50 +222,48 @@ impl Pkcs7Ref {
223
222
/// [`PKCS7_verify`]: https://www.openssl.org/docs/man1.0.2/crypto/PKCS7_verify.html
224
223
pub fn verify (
225
224
& self ,
226
- certs : & Stack < X509 > ,
227
- store : & X509Store ,
225
+ certs : & StackRef < X509 > ,
226
+ store : & X509StoreRef ,
228
227
indata : Option < & [ u8 ] > ,
229
228
out : Option < & mut Vec < u8 > > ,
230
- flags : Pkcs7Flags
229
+ flags : Pkcs7Flags ,
231
230
) -> Result < ( ) , ErrorStack > {
232
- ffi:: init ( ) ;
233
-
234
231
let out_bio = MemBio :: new ( ) ?;
235
232
236
233
let indata_bio = match indata {
237
234
Some ( data) => Some ( MemBioSlice :: new ( data) ?) ,
238
235
None => None ,
239
236
} ;
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 ( ) ) ;
241
238
242
- let result = unsafe {
239
+ unsafe {
243
240
cvt ( ffi:: PKCS7_verify (
244
241
self . as_ptr ( ) ,
245
242
certs. as_ptr ( ) ,
246
243
store. as_ptr ( ) ,
247
244
indata_bio_ptr,
248
245
out_bio. as_ptr ( ) ,
249
- flags. bits ) )
250
- . map ( |_r | ( ) )
251
- } ;
246
+ flags. bits ,
247
+ ) ) . map ( |_ | ( ) ) ?
248
+ }
252
249
253
250
if let Some ( data) = out {
254
251
data. clear ( ) ;
255
- data. append ( & mut out_bio. get_buf ( ) . to_vec ( ) ) ;
252
+ data. extend_from_slice ( out_bio. get_buf ( ) ) ;
256
253
}
257
254
258
- result
255
+ Ok ( ( ) )
259
256
}
260
257
}
261
258
262
259
#[ cfg( test) ]
263
260
mod tests {
264
- use x509:: X509 ;
265
- use x509:: store:: X509StoreBuilder ;
266
- use symm:: Cipher ;
267
261
use pkcs7:: { Pkcs7 , Pkcs7Flags } ;
268
262
use pkey:: PKey ;
269
263
use stack:: Stack ;
264
+ use symm:: Cipher ;
265
+ use x509:: store:: X509StoreBuilder ;
266
+ use x509:: X509 ;
270
267
271
268
#[ test]
272
269
fn encrypt_decrypt_test ( ) {
@@ -280,13 +277,18 @@ mod tests {
280
277
let pkey = include_bytes ! ( "../test/key.pem" ) ;
281
278
let pkey = PKey :: private_key_from_pem ( pkey) . unwrap ( ) ;
282
279
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" ) ;
284
282
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" ) ;
286
286
287
287
let ( pkcs7_decoded, _) = Pkcs7 :: from_smime ( encrypted. as_slice ( ) ) . expect ( "should succeed" ) ;
288
288
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" ) ;
290
292
291
293
assert_eq ! ( decoded, message. into_bytes( ) ) ;
292
294
}
@@ -308,18 +310,31 @@ mod tests {
308
310
309
311
let store = store_builder. build ( ) ;
310
312
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" ) ;
312
315
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" ) ;
314
319
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" ) ;
316
322
317
323
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" ) ;
320
332
321
333
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
+ ) ;
323
338
}
324
339
325
340
#[ test]
@@ -339,14 +354,20 @@ mod tests {
339
354
340
355
let store = store_builder. build ( ) ;
341
356
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" ) ;
343
359
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" ) ;
345
363
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" ) ;
347
366
348
367
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" ) ;
350
371
351
372
assert_eq ! ( message. clone( ) . into_bytes( ) , output) ;
352
373
assert ! ( content. is_none( ) ) ;
0 commit comments