6
6
use bitflags:: bitflags;
7
7
use foreign_types:: { ForeignType , ForeignTypeRef } ;
8
8
use libc:: { c_int, c_long, c_uint} ;
9
+ use openssl_macros:: corresponds;
9
10
10
11
use std:: ptr;
11
12
12
13
use crate :: asn1:: { Asn1IntegerRef , Asn1ObjectRef } ;
13
14
use crate :: bio:: MemBioSlice ;
14
15
use crate :: error:: ErrorStack ;
15
- use crate :: hash:: MessageDigest ;
16
+ use crate :: hash:: { Hasher , MessageDigest } ;
16
17
use crate :: pkey:: { HasPrivate , PKeyRef } ;
17
- use crate :: x509:: { X509Algorithm , X509Ref } ;
18
+ use crate :: x509:: { X509Algorithm , X509AlgorithmRef , X509Ref } ;
18
19
use crate :: { cvt, cvt_p} ;
19
20
20
21
foreign_type_and_impl_send_sync ! {
@@ -33,31 +34,28 @@ impl TsMsgImprint {
33
34
///
34
35
/// This corresponds to `TS_MSG_IMPRINT_new`.
35
36
pub fn new ( ) -> Result < TsMsgImprint , ErrorStack > {
37
+ ffi:: init ( ) ;
36
38
unsafe {
37
- ffi:: init ( ) ;
38
- let imprint: * mut ffi:: TS_MSG_IMPRINT = cvt_p ( ffi:: TS_MSG_IMPRINT_new ( ) ) ?;
39
+ let imprint = cvt_p ( ffi:: TS_MSG_IMPRINT_new ( ) ) ?;
39
40
Ok ( TsMsgImprint :: from_ptr ( imprint) )
40
41
}
41
42
}
42
43
43
44
/// Sets the algorithm identifier of the message digest algorithm.
44
- ///
45
- /// This corresponds to `TS_MSG_IMPRINT_set_algo`.
46
- pub fn set_algo ( & mut self , digest : & MessageDigest ) -> Result < ( ) , ErrorStack > {
45
+ #[ corresponds( TS_MSG_IMPRINT_set_algo ) ]
46
+ pub fn set_algo ( & mut self , algo : & X509AlgorithmRef ) -> Result < ( ) , ErrorStack > {
47
47
unsafe {
48
- let algorithm = X509Algorithm :: from_ptr ( cvt_p ( ffi:: X509_ALGOR_new ( ) ) ?) ;
49
- ffi:: X509_ALGOR_set_md ( algorithm. as_ptr ( ) , digest. as_ptr ( ) ) ;
50
48
cvt ( ffi:: TS_MSG_IMPRINT_set_algo (
51
49
self . as_ptr ( ) ,
52
- algorithm . as_ptr ( ) ,
50
+ algo . as_ptr ( ) ,
53
51
) )
54
52
. map ( |_| ( ) )
55
53
}
56
54
}
57
55
58
- /// Sets the message digest of the data to be timestamped.
59
- ///
60
- /// This corresponds to ` TS_MSG_IMPRINT_set_msg`.
56
+ /// Sets the message ** digest** of the data to be timestamped.
57
+ /// It is named this way to match the name in openssl itself
58
+ # [ corresponds( TS_MSG_IMPRINT_set_msg ) ]
61
59
pub fn set_msg ( & mut self , digest : & [ u8 ] ) -> Result < ( ) , ErrorStack > {
62
60
let length = convert_digest_length_to_int ( digest. len ( ) ) ;
63
61
unsafe {
@@ -69,6 +67,28 @@ impl TsMsgImprint {
69
67
. map ( |_| ( ) )
70
68
}
71
69
}
70
+
71
+ /// Creates a ready-to-use message imprint from a message and a specified hash algorithm.
72
+ pub fn from_message_with_algo ( msg : & [ u8 ] , md : MessageDigest ) -> Result < Self , ErrorStack > {
73
+ let mut h = Hasher :: new ( md) ?;
74
+ h. update ( msg) ?;
75
+ let hash = h. finish ( ) ?;
76
+ Self :: from_prehash_with_algo ( & hash, md)
77
+ }
78
+
79
+ /// Creates a ready-to-use message imprint from the hash of a message and a specified hash algorithm.
80
+ ///
81
+ /// `hash` must have originated from the hash function specified by `md`.
82
+ pub fn from_prehash_with_algo ( hash : & [ u8 ] , md : MessageDigest ) -> Result < Self , ErrorStack > {
83
+ let mut algo = X509Algorithm :: new ( ) ?;
84
+ algo. set_md ( md) ;
85
+
86
+ let mut imprint = Self :: new ( ) ?;
87
+ imprint. set_algo ( & algo) ?;
88
+ imprint. set_msg ( hash) ?;
89
+
90
+ Ok ( imprint)
91
+ }
72
92
}
73
93
74
94
fn convert_digest_length_to_int ( len : usize ) -> c_int {
@@ -372,14 +392,11 @@ mod tests {
372
392
use crate :: bn:: BigNum ;
373
393
use crate :: hash:: MessageDigest ;
374
394
use crate :: pkey:: PKey ;
375
- use crate :: sha:: sha512;
376
395
use crate :: x509:: X509 ;
377
396
378
397
#[ test]
379
398
fn test_request ( ) {
380
- let mut imprint = TsMsgImprint :: new ( ) . unwrap ( ) ;
381
- imprint. set_algo ( & MessageDigest :: sha512 ( ) ) . unwrap ( ) ;
382
- imprint. set_msg ( & sha512 ( b"BLAHBLAHBLAH\n " ) ) . unwrap ( ) ;
399
+ let imprint = TsMsgImprint :: from_message_with_algo ( b"BLAHBLAHBLAH\n " , MessageDigest :: sha512 ( ) ) . unwrap ( ) ;
383
400
384
401
let mut request = TsReq :: new ( ) . unwrap ( ) ;
385
402
request. set_version ( 1 ) . unwrap ( ) ;
0 commit comments