Skip to content

Commit ea3182d

Browse files
author
Jonas Maier
committed
add tryfrom
1 parent a48269f commit ea3182d

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

openssl/src/ts.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use foreign_types::{ForeignType, ForeignTypeRef};
88
use libc::{c_int, c_long, c_uint};
99
use openssl_macros::corresponds;
1010

11+
use std::convert::TryFrom;
1112
use std::ptr;
1213

1314
use crate::asn1::{Asn1IntegerRef, Asn1ObjectRef};
@@ -44,25 +45,24 @@ impl TsMsgImprint {
4445
/// Sets the algorithm identifier of the message digest algorithm.
4546
#[corresponds(TS_MSG_IMPRINT_set_algo)]
4647
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(|_| ()) }
5449
}
5550

5651
/// Sets the message **digest** of the data to be timestamped.
5752
/// It is named this way to match the name in openssl itself
5853
#[corresponds(TS_MSG_IMPRINT_set_msg)]
5954
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+
6161
unsafe {
6262
cvt(ffi::TS_MSG_IMPRINT_set_msg(
6363
self.as_ptr(),
6464
digest.as_ptr() as *mut _,
65-
length,
65+
len,
6666
))
6767
.map(|_| ())
6868
}
@@ -77,7 +77,7 @@ impl TsMsgImprint {
7777
}
7878

7979
/// Creates a ready-to-use message imprint from the hash of a message and a specified hash algorithm.
80-
///
80+
///
8181
/// `hash` must have originated from the hash function specified by `md`.
8282
pub fn from_prehash_with_algo(hash: &[u8], md: MessageDigest) -> Result<Self, ErrorStack> {
8383
let mut algo = X509Algorithm::new()?;
@@ -91,14 +91,6 @@ impl TsMsgImprint {
9191
}
9292
}
9393

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-
10294
foreign_type_and_impl_send_sync! {
10395
type CType = ffi::TS_REQ;
10496
fn drop = ffi::TS_REQ_free;
@@ -277,6 +269,14 @@ impl TsVerifyContext {
277269
}
278270
}
279271

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+
280280
foreign_type_and_impl_send_sync! {
281281
type CType = ffi::TS_RESP_CTX;
282282
fn drop = ffi::TS_RESP_CTX_free;
@@ -396,7 +396,9 @@ mod tests {
396396

397397
#[test]
398398
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();
400402

401403
let mut request = TsReq::new().unwrap();
402404
request.set_version(1).unwrap();

0 commit comments

Comments
 (0)