c2pa_crypto/time_stamp/provider.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
// Copyright 2022 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.
// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.
use async_trait::async_trait;
use bcder::{encode::Values, OctetString};
use rand::{thread_rng, Rng};
use x509_certificate::DigestAlgorithm;
use crate::{asn1::rfc3161::TimeStampReq, time_stamp::TimeStampError};
/// A `TimeStampProvider` implementation can contact a [RFC 3161] time stamp
/// service and generate a corresponding time stamp for a specific piece of
/// data.
///
/// [RFC 3161]: https://datatracker.ietf.org/doc/html/rfc3161
pub trait TimeStampProvider {
/// Return the URL for time stamp service.
fn time_stamp_service_url(&self) -> Option<String> {
None
}
/// Additional request headers to pass to the time stamp service.
///
/// IMPORTANT: You should not include the "Content-type" header here.
/// That is provided by default.
fn time_stamp_request_headers(&self) -> Option<Vec<(String, String)>> {
None
}
/// Generate the request body for the HTTPS request to the time stamp
/// service.
fn time_stamp_request_body(&self, message: &[u8]) -> Result<Vec<u8>, TimeStampError> {
default_rfc3161_message(message)
}
/// Request a [RFC 3161] time stamp over an arbitrary data packet.
///
/// The default implementation will send the request to the URL
/// provided by [`Self::time_stamp_service_url()`], if any.
///
/// [RFC 3161]: https://datatracker.ietf.org/doc/html/rfc3161
///
/// todo: THIS CODE IS NOT COMPATIBLE WITH C2PA 2.x sigTst2
#[allow(unused_variables)] // `message` not used on WASM
fn send_time_stamp_request(&self, message: &[u8]) -> Option<Result<Vec<u8>, TimeStampError>> {
#[cfg(not(target_arch = "wasm32"))]
if let Some(url) = self.time_stamp_service_url() {
if let Ok(body) = self.time_stamp_request_body(message) {
let headers: Option<Vec<(String, String)>> = self.time_stamp_request_headers();
return Some(super::http_request::default_rfc3161_request(
&url, headers, &body, message,
));
}
}
None
}
}
/// An `AsyncTimeStampProvider` implementation can contact a [RFC 3161] time
/// stamp service and generate a corresponding time stamp for a specific piece
/// of data.
///
/// This is identical to [`TimeStampProvider`] except for performing its work
/// asynchronously.
///
/// [RFC 3161]: https://datatracker.ietf.org/doc/html/rfc3161
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
pub trait AsyncTimeStampProvider: Sync {
// IMPORTANT: It appears that the Sync (most platforms) vs not-Sync (WASM)
// distinction can't be achieved without duplicating the trait definition 👎🏻.
// Please verify that any changes made here are also made to the subsequent
// definition of AsyncTimeStampProvider for WASM builds.
/// Return the URL for time stamp service.
fn time_stamp_service_url(&self) -> Option<String> {
None
}
/// Additional request headers to pass to the time stamp service.
///
/// IMPORTANT: You should not include the "Content-type" header here.
/// That is provided by default.
fn time_stamp_request_headers(&self) -> Option<Vec<(String, String)>> {
None
}
/// Generate the request body for the HTTPS request to the time stamp
/// service.
fn time_stamp_request_body(&self, message: &[u8]) -> Result<Vec<u8>, TimeStampError> {
default_rfc3161_message(message)
}
/// Request a [RFC 3161] time stamp over an arbitrary data packet.
///
/// The default implementation will send the request to the URL
/// provided by [`Self::time_stamp_service_url()`], if any.
///
/// [RFC 3161]: https://datatracker.ietf.org/doc/html/rfc3161
#[allow(unused_variables)] // `message` not used on WASM
async fn send_time_stamp_request(
&self,
message: &[u8],
) -> Option<Result<Vec<u8>, TimeStampError>> {
// NOTE: This is currently synchronous, but may become
// async in the future.
#[cfg(not(target_arch = "wasm32"))]
if let Some(url) = self.time_stamp_service_url() {
if let Ok(body) = self.time_stamp_request_body(message) {
let headers: Option<Vec<(String, String)>> = self.time_stamp_request_headers();
return Some(
super::http_request::default_rfc3161_request_async(
&url, headers, &body, message,
)
.await,
);
}
}
None
}
}
/// An `AsyncTimeStampProvider` implementation can contact a [RFC 3161] time
/// stamp service and generate a corresponding time stamp for a specific piece
/// of data.
///
/// This is identical to [`TimeStampProvider`] except for performing its work
/// asynchronously.
///
/// [RFC 3161]: https://datatracker.ietf.org/doc/html/rfc3161
#[cfg(target_arch = "wasm32")]
#[async_trait(?Send)]
pub trait AsyncTimeStampProvider {
// IMPORTANT: It appears that the Sync (most platforms) vs not-Sync (WASM)
// distinction can't be achieved without duplicating the trait definition 👎🏻.
// Please verify that any changes made here are also made to the previous
// definition of AsyncTimeStampProvider for non-WASM builds.
/// Return the URL for time stamp service.
fn time_stamp_service_url(&self) -> Option<String> {
None
}
/// Additional request headers to pass to the time stamp service.
///
/// IMPORTANT: You should not include the "Content-type" header here.
/// That is provided by default.
fn time_stamp_request_headers(&self) -> Option<Vec<(String, String)>> {
None
}
/// Generate the request body for the HTTPS request to the time stamp
/// service.
fn time_stamp_request_body(&self, message: &[u8]) -> Result<Vec<u8>, TimeStampError> {
default_rfc3161_message(message)
}
/// Request a [RFC 3161] time stamp over an arbitrary data packet.
///
/// The default implementation will send the request to the URL
/// provided by [`Self::time_stamp_service_url()`], if any.
///
/// [RFC 3161]: https://datatracker.ietf.org/doc/html/rfc3161
#[allow(unused_variables)] // `message` not used on WASM
async fn send_time_stamp_request(
&self,
message: &[u8],
) -> Option<Result<Vec<u8>, TimeStampError>> {
// NOTE: This is currently synchronous, but may become
// async in the future.
#[cfg(not(target_arch = "wasm32"))]
if let Some(url) = self.time_stamp_service_url() {
if let Ok(body) = self.time_stamp_request_body(message) {
let headers: Option<Vec<(String, String)>> = self.time_stamp_request_headers();
return Some(
super::http_request::default_rfc3161_request_async(
&url, headers, &body, message,
)
.await,
);
}
}
None
}
}
/// Create an [RFC 3161] time stamp request message for a given piece of data.
///
/// [RFC 3161]: https://datatracker.ietf.org/doc/html/rfc3161
pub fn default_rfc3161_message(data: &[u8]) -> Result<Vec<u8>, TimeStampError> {
let request = time_stamp_message_http(data, DigestAlgorithm::Sha256)?;
let mut body = Vec::<u8>::new();
request
.encode_ref()
.write_encoded(bcder::Mode::Der, &mut body)?;
Ok(body)
}
fn time_stamp_message_http(
message: &[u8],
digest_algorithm: DigestAlgorithm,
) -> Result<TimeStampReq, TimeStampError> {
let mut h = digest_algorithm.digester();
h.update(message);
let digest = h.finish();
let mut random = [0u8; 8];
thread_rng().try_fill(&mut random).map_err(|_| {
TimeStampError::InternalError("Unable to generate random number".to_string())
})?;
let request = TimeStampReq {
version: bcder::Integer::from(1_u8),
message_imprint: crate::asn1::rfc3161::MessageImprint {
hash_algorithm: digest_algorithm.into(),
hashed_message: OctetString::new(bytes::Bytes::copy_from_slice(digest.as_ref())),
},
req_policy: None,
nonce: Some(bcder::Integer::from(u64::from_le_bytes(random))),
cert_req: Some(true),
extensions: None,
};
Ok(request)
}