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
// Copyright 2025 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 std::collections::HashMap;
use async_generic::async_generic;
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use crate::{
assertion::{Assertion, AssertionBase, AssertionCbor},
assertions::labels,
context::Context,
crypto::cose::CertificateTrustPolicy,
error::Result,
status_tracker::StatusTracker,
Error,
};
/// Helper class to create a `TimeStamp` assertion.
///
/// <https://spec.c2pa.org/specifications/specifications/2.2/specs/C2PA_Specification.html#timestamp_assertion>
#[derive(Serialize, Deserialize, Default, Debug, PartialEq, Eq)]
pub struct TimeStamp(pub HashMap<String, ByteBuf>);
impl TimeStamp {
/// Label prefix for a [`TimeStamp`] assertion.
///
/// See [Actions - C2PA Technical Specification](https://spec.c2pa.org/specifications/specifications/2.3/specs/C2PA_Specification.html#_actions).
pub const LABEL: &'static str = labels::TIMESTAMP;
/// Construct a new, empty [`TimeStamp`] assertion.
pub fn new() -> Self {
TimeStamp(HashMap::new())
}
/// Add a timestamp token for the given manifest id.
pub fn add_timestamp(&mut self, manifest_id: &str, timestamp: &[u8]) {
self.0
.insert(manifest_id.to_string(), ByteBuf::from(timestamp.to_vec()));
}
/// Get the timestamp token for a given manifest id.
pub fn get_timestamp(&self, manifest_id: &str) -> Option<&[u8]> {
self.0.get(manifest_id).map(|buf| buf.as_ref())
}
/// Refresh the timestamp token for a given manifest id.
///
/// `tsa_headers` are sent with the request in addition to the `Content-Type` header, which
/// lets a signer authenticate to its TSA (see [`Signer::timestamp_request_headers`]).
///
/// The signature is expected to be the `signature` field of the `COSE_Sign1_Tagged` structure
/// found in the C2PA claim signature box of the manifest corresponding to the `manifest_id`.
///
/// [`Signer::timestamp_request_headers`]: crate::Signer::timestamp_request_headers
//
// The `signature` is normally obtained from [`Store::get_cose_sign1_signature`].
//
// [`Store::get_cose_sign1_signature`][crate::store::Store::get_cose_sign1_structure].
#[async_generic]
pub(crate) fn refresh_timestamp(
&mut self,
tsa_url: &str,
tsa_headers: Option<Vec<(String, String)>>,
manifest_id: &str,
signature: &[u8],
context: &Context,
) -> Result<()> {
let timestamp_token = if _sync {
TimeStamp::send_timestamp_token_request(tsa_url, tsa_headers, signature, context)?
} else {
TimeStamp::send_timestamp_token_request_async(tsa_url, tsa_headers, signature, context)
.await?
};
self.0
.insert(manifest_id.to_owned(), ByteBuf::from(timestamp_token));
Ok(())
}
/// Send a timestamp token request to the `tsa_url` with the given `message`,
/// attaching `headers` to the HTTP request.
///
/// This function will verify the structure of the returned response but not the trust.
///
/// See [`TimeStamp::refresh_timestamp`] for more information.
#[async_generic]
pub(crate) fn send_timestamp_token_request(
tsa_url: &str,
headers: Option<Vec<(String, String)>>,
message: &[u8],
context: &Context,
) -> Result<Vec<u8>> {
let body = crate::crypto::time_stamp::default_rfc3161_message(message)?;
let bytes = if _sync {
crate::crypto::time_stamp::default_rfc3161_request(
tsa_url, headers, &body, message, context,
)
} else {
crate::crypto::time_stamp::default_rfc3161_request_async(
tsa_url, headers, &body, message, context,
)
.await
}
.map_err(|err| Error::OtherError(format!("timestamp token not found: {err:?}").into()))?;
// make sure it is a good response
let ctp = CertificateTrustPolicy::passthrough();
let mut tracker = StatusTracker::default();
if _sync {
crate::crypto::time_stamp::verify_time_stamp(
&bytes,
message,
&ctp,
&mut tracker,
false,
)?;
} else {
crate::crypto::time_stamp::verify_time_stamp_async(
&bytes,
message,
&ctp,
&mut tracker,
false,
)
.await?;
}
let token =
crate::crypto::cose::timestamptoken_from_timestamprsp(&bytes).map_err(|err| {
Error::OtherError(format!("timestamp token not found: {err:?}").into())
})?;
Ok(token)
}
}
impl AsRef<HashMap<String, ByteBuf>> for TimeStamp {
fn as_ref(&self) -> &HashMap<String, ByteBuf> {
&self.0
}
}
impl AssertionCbor for TimeStamp {}
impl AssertionBase for TimeStamp {
const LABEL: &'static str = Self::LABEL;
fn to_assertion(&self) -> Result<Assertion> {
Self::to_cbor_assertion(self)
}
fn from_assertion(assertion: &Assertion) -> Result<Self> {
Self::from_cbor_assertion(assertion)
}
}