Skip to content

Commit a48269f

Browse files
author
Jonas Maier
committed
change interface to match original openssl, add helper functions for convenience
1 parent 4e98937 commit a48269f

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

openssl-sys/src/handwritten/x509.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ extern "C" {
210210

211211
pub fn X509_to_X509_REQ(x: *mut X509, pkey: *mut EVP_PKEY, md: *const EVP_MD) -> *mut X509_REQ;
212212

213+
pub fn X509_ALGOR_new() -> *mut X509_ALGOR;
213214
pub fn X509_ALGOR_free(x: *mut X509_ALGOR);
215+
pub fn X509_ALGOR_set_md(alg: *mut X509_ALGOR, md: *const EVP_MD);
214216

215217
pub fn X509_REVOKED_new() -> *mut X509_REVOKED;
216218
pub fn X509_REVOKED_free(x: *mut X509_REVOKED);

openssl-sys/src/ts.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use libc::*;
22

3-
#[allow(unused_imports)]
4-
use *;
3+
use crate::{ASN1_INTEGER, ASN1_OBJECT, BIO, EVP_MD, EVP_PKEY, X509, X509_ALGOR};
54

65
pub enum TS_MSG_IMPRINT {}
76
pub enum TS_REQ {}

openssl/src/ts.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
use bitflags::bitflags;
77
use foreign_types::{ForeignType, ForeignTypeRef};
88
use libc::{c_int, c_long, c_uint};
9+
use openssl_macros::corresponds;
910

1011
use std::ptr;
1112

1213
use crate::asn1::{Asn1IntegerRef, Asn1ObjectRef};
1314
use crate::bio::MemBioSlice;
1415
use crate::error::ErrorStack;
15-
use crate::hash::MessageDigest;
16+
use crate::hash::{Hasher, MessageDigest};
1617
use crate::pkey::{HasPrivate, PKeyRef};
17-
use crate::x509::{X509Algorithm, X509Ref};
18+
use crate::x509::{X509Algorithm, X509AlgorithmRef, X509Ref};
1819
use crate::{cvt, cvt_p};
1920

2021
foreign_type_and_impl_send_sync! {
@@ -33,31 +34,28 @@ impl TsMsgImprint {
3334
///
3435
/// This corresponds to `TS_MSG_IMPRINT_new`.
3536
pub fn new() -> Result<TsMsgImprint, ErrorStack> {
37+
ffi::init();
3638
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())?;
3940
Ok(TsMsgImprint::from_ptr(imprint))
4041
}
4142
}
4243

4344
/// 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> {
4747
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());
5048
cvt(ffi::TS_MSG_IMPRINT_set_algo(
5149
self.as_ptr(),
52-
algorithm.as_ptr(),
50+
algo.as_ptr(),
5351
))
5452
.map(|_| ())
5553
}
5654
}
5755

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)]
6159
pub fn set_msg(&mut self, digest: &[u8]) -> Result<(), ErrorStack> {
6260
let length = convert_digest_length_to_int(digest.len());
6361
unsafe {
@@ -69,6 +67,28 @@ impl TsMsgImprint {
6967
.map(|_| ())
7068
}
7169
}
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+
}
7292
}
7393

7494
fn convert_digest_length_to_int(len: usize) -> c_int {
@@ -372,14 +392,11 @@ mod tests {
372392
use crate::bn::BigNum;
373393
use crate::hash::MessageDigest;
374394
use crate::pkey::PKey;
375-
use crate::sha::sha512;
376395
use crate::x509::X509;
377396

378397
#[test]
379398
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();
383400

384401
let mut request = TsReq::new().unwrap();
385402
request.set_version(1).unwrap();

openssl/src/x509/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,6 +2320,16 @@ foreign_type_and_impl_send_sync! {
23202320
pub struct X509AlgorithmRef;
23212321
}
23222322

2323+
impl X509Algorithm {
2324+
pub fn new() -> Result<Self, ErrorStack> {
2325+
ffi::init();
2326+
unsafe {
2327+
let ptr = cvt_p(ffi::X509_ALGOR_new())?;
2328+
Ok(Self::from_ptr(ptr))
2329+
}
2330+
}
2331+
}
2332+
23232333
impl X509AlgorithmRef {
23242334
/// Returns the ASN.1 OID of this algorithm.
23252335
pub fn object(&self) -> &Asn1ObjectRef {
@@ -2329,6 +2339,12 @@ impl X509AlgorithmRef {
23292339
Asn1ObjectRef::from_const_ptr_opt(oid).expect("algorithm oid must not be null")
23302340
}
23312341
}
2342+
2343+
pub fn set_md(&mut self, md: MessageDigest) {
2344+
unsafe {
2345+
ffi::X509_ALGOR_set_md(self.as_ptr(), md.as_ptr());
2346+
}
2347+
}
23322348
}
23332349

23342350
foreign_type_and_impl_send_sync! {

0 commit comments

Comments
 (0)