@@ -8,6 +8,7 @@ use foreign_types::{ForeignType, ForeignTypeRef};
8
8
use libc:: { c_int, c_long, c_uint} ;
9
9
use openssl_macros:: corresponds;
10
10
11
+ use std:: convert:: TryFrom ;
11
12
use std:: ptr;
12
13
13
14
use crate :: asn1:: { Asn1IntegerRef , Asn1ObjectRef } ;
@@ -44,25 +45,24 @@ impl TsMsgImprint {
44
45
/// Sets the algorithm identifier of the message digest algorithm.
45
46
#[ corresponds( TS_MSG_IMPRINT_set_algo ) ]
46
47
pub fn set_algo ( & mut self , algo : & X509AlgorithmRef ) -> Result < ( ) , ErrorStack > {
47
- unsafe {
48
- cvt ( ffi:: TS_MSG_IMPRINT_set_algo (
49
- self . as_ptr ( ) ,
50
- algo. as_ptr ( ) ,
51
- ) )
52
- . map ( |_| ( ) )
53
- }
48
+ unsafe { cvt ( ffi:: TS_MSG_IMPRINT_set_algo ( self . as_ptr ( ) , algo. as_ptr ( ) ) ) . map ( |_| ( ) ) }
54
49
}
55
50
56
51
/// Sets the message **digest** of the data to be timestamped.
57
52
/// It is named this way to match the name in openssl itself
58
53
#[ corresponds( TS_MSG_IMPRINT_set_msg ) ]
59
54
pub fn set_msg ( & mut self , digest : & [ u8 ] ) -> Result < ( ) , ErrorStack > {
60
- let length = convert_digest_length_to_int ( digest. len ( ) ) ;
55
+ let len = if digest. len ( ) > c_int:: MAX as usize {
56
+ panic ! ( "digest length is too large" ) ;
57
+ } else {
58
+ digest. len ( ) as c_int
59
+ } ;
60
+
61
61
unsafe {
62
62
cvt ( ffi:: TS_MSG_IMPRINT_set_msg (
63
63
self . as_ptr ( ) ,
64
64
digest. as_ptr ( ) as * mut _ ,
65
- length ,
65
+ len ,
66
66
) )
67
67
. map ( |_| ( ) )
68
68
}
@@ -77,7 +77,7 @@ impl TsMsgImprint {
77
77
}
78
78
79
79
/// Creates a ready-to-use message imprint from the hash of a message and a specified hash algorithm.
80
- ///
80
+ ///
81
81
/// `hash` must have originated from the hash function specified by `md`.
82
82
pub fn from_prehash_with_algo ( hash : & [ u8 ] , md : MessageDigest ) -> Result < Self , ErrorStack > {
83
83
let mut algo = X509Algorithm :: new ( ) ?;
@@ -91,14 +91,6 @@ impl TsMsgImprint {
91
91
}
92
92
}
93
93
94
- fn convert_digest_length_to_int ( len : usize ) -> c_int {
95
- if len > std:: i32:: MAX as usize {
96
- panic ! ( "Digest length is too large" ) ;
97
- } else {
98
- len as i32
99
- }
100
- }
101
-
102
94
foreign_type_and_impl_send_sync ! {
103
95
type CType = ffi:: TS_REQ ;
104
96
fn drop = ffi:: TS_REQ_free ;
@@ -277,6 +269,14 @@ impl TsVerifyContext {
277
269
}
278
270
}
279
271
272
+ impl TryFrom < & TsReqRef > for TsVerifyContext {
273
+ type Error = ErrorStack ;
274
+
275
+ fn try_from ( value : & TsReqRef ) -> Result < Self , Self :: Error > {
276
+ Self :: from_req ( value)
277
+ }
278
+ }
279
+
280
280
foreign_type_and_impl_send_sync ! {
281
281
type CType = ffi:: TS_RESP_CTX ;
282
282
fn drop = ffi:: TS_RESP_CTX_free ;
@@ -396,7 +396,9 @@ mod tests {
396
396
397
397
#[ test]
398
398
fn test_request ( ) {
399
- let imprint = TsMsgImprint :: from_message_with_algo ( b"BLAHBLAHBLAH\n " , MessageDigest :: sha512 ( ) ) . unwrap ( ) ;
399
+ let imprint =
400
+ TsMsgImprint :: from_message_with_algo ( b"BLAHBLAHBLAH\n " , MessageDigest :: sha512 ( ) )
401
+ . unwrap ( ) ;
400
402
401
403
let mut request = TsReq :: new ( ) . unwrap ( ) ;
402
404
request. set_version ( 1 ) . unwrap ( ) ;
0 commit comments