commit 55b09b3dc2233f2669a79ca5c91372f53d1ebe42
parent 50729b71dbe9eed0e838cc187eb902da081376e6
Author: Eric Scouten <scouten@adobe.com>
Date: Wed, 4 Oct 2023 12:04:20 -0700
Add ability to customize HTTP headers on timestamp request to Signer and AsyncSigner traits (#315)
Diffstat:
2 files changed, 45 insertions(+), 6 deletions(-)
diff --git a/sdk/src/signer.rs b/sdk/src/signer.rs
@@ -34,6 +34,14 @@ pub trait Signer {
None
}
+ /// Additional request headers to pass to the time stamp authority.
+ ///
+ /// IMPORTANT: You should not include the "Content-type" header here.
+ /// That is provided by default.
+ fn timestamp_request_headers(&self) -> Option<Vec<(String, String)>> {
+ None
+ }
+
/// Request RFC 3161 timestamp to be included in the manifest data
/// structure.
///
@@ -42,8 +50,10 @@ pub trait Signer {
/// The default implementation will send the request to the URL
/// provided by [`Self::time_authority_url()`], if any.
fn send_timestamp_request(&self, message: &[u8]) -> Option<Result<Vec<u8>>> {
+ let headers: Option<Vec<(String, String)>> = self.timestamp_request_headers();
+
self.time_authority_url()
- .map(|url| crate::time_stamp::default_rfc3161_request(&url, message))
+ .map(|url| crate::time_stamp::default_rfc3161_request(&url, headers, message))
}
/// OCSP response for the signing cert if available
@@ -111,6 +121,14 @@ pub trait AsyncSigner: Sync {
None
}
+ /// Additional request headers to pass to the time stamp authority.
+ ///
+ /// IMPORTANT: You should not include the "Content-type" header here.
+ /// That is provided by default.
+ fn timestamp_request_headers(&self) -> Option<Vec<(String, String)>> {
+ None
+ }
+
/// Request RFC 3161 timestamp to be included in the manifest data
/// structure.
///
@@ -121,8 +139,10 @@ pub trait AsyncSigner: Sync {
async fn send_timestamp_request(&self, message: &[u8]) -> Option<Result<Vec<u8>>> {
// NOTE: This is currently synchronous, but may become
// async in the future.
+ let headers: Option<Vec<(String, String)>> = self.timestamp_request_headers();
+
self.time_authority_url()
- .map(|url| crate::time_stamp::default_rfc3161_request(&url, message))
+ .map(|url| crate::time_stamp::default_rfc3161_request(&url, headers, message))
}
/// OCSP response for the signing cert if available
diff --git a/sdk/src/time_stamp.rs b/sdk/src/time_stamp.rs
@@ -101,6 +101,7 @@ pub(crate) fn cose_sigtst_to_tstinfos(
#[cfg(feature = "openssl_sign")]
fn time_stamp_request_http(
url: &str,
+ headers: Option<Vec<(String, String)>>,
request: &crate::asn1::rfc3161::TimeStampReq,
) -> Result<Vec<u8>> {
use std::io::Read;
@@ -117,7 +118,15 @@ fn time_stamp_request_http(
let body_reader = std::io::Cursor::new(body);
- let response = ureq::post(url)
+ let mut req = ureq::post(url);
+
+ if let Some(headers) = headers {
+ for (ref name, ref value) in headers {
+ req = req.set(name.as_str(), value.as_str());
+ }
+ }
+
+ let response = req
.set("Content-Type", HTTP_CONTENT_TYPE_REQUEST)
.send(body_reader)
.map_err(|_err| Error::CoseTimeStampGeneration)?;
@@ -169,6 +178,7 @@ fn time_stamp_request_http(
#[cfg(feature = "openssl_sign")]
pub(crate) fn time_stamp_message_http(
url: &str,
+ headers: Option<Vec<(String, String)>>,
message: &[u8],
digest_algorithm: DigestAlgorithm,
) -> Result<Vec<u8>> {
@@ -195,7 +205,7 @@ pub(crate) fn time_stamp_message_http(
extensions: None,
};
- time_stamp_request_http(url, &request)
+ time_stamp_request_http(url, headers, &request)
}
pub struct TimeStampResponse(TimeStampResp);
@@ -272,10 +282,19 @@ pub fn timestamp_data(signer: &dyn Signer, data: &[u8]) -> Option<Result<Vec<u8>
}
#[allow(unused_variables)]
-pub fn default_rfc3161_request(url: &str, data: &[u8]) -> Result<Vec<u8>> {
+pub fn default_rfc3161_request(
+ url: &str,
+ headers: Option<Vec<(String, String)>>,
+ data: &[u8],
+) -> Result<Vec<u8>> {
#[cfg(feature = "openssl_sign")]
{
- let ts = time_stamp_message_http(url, data, x509_certificate::DigestAlgorithm::Sha256)?;
+ let ts = time_stamp_message_http(
+ url,
+ headers,
+ data,
+ x509_certificate::DigestAlgorithm::Sha256,
+ )?;
// sanity check
verify_timestamp(&ts, data)?;