c2pa 0.81.0

Rust SDK for C2PA (Coalition for Content Provenance and Authenticity) implementors
Documentation
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// 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 std::{
    io::{Read, Seek},
    path::*,
};

use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;

use crate::{
    assertion::{Assertion, AssertionBase, AssertionCbor},
    assertions::labels,
    asset_io::CAIRead,
    cbor_types::UriT,
    error::{Error, Result},
    utils::hash_utils::{
        hash_stream_by_alg, hash_stream_by_alg_with_progress, vec_compare, verify_asset_by_alg,
        verify_by_alg, HashRange,
    },
};

const ASSERTION_CREATION_VERSION: usize = 1;

/// Helper class to create DataHash assertion
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct DataHash {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exclusions: Option<Vec<HashRange>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub alg: Option<String>,

    #[serde(with = "serde_bytes")]
    pub hash: Vec<u8>,
    #[serde(with = "serde_bytes")]
    pub pad: Vec<u8>,

    // must use explicit ByteBuf here because  #[serde(with = "serde_bytes")] does not working if Option<Vec<u8>>
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pad2: Option<serde_bytes::ByteBuf>,

    #[serde(skip_serializing)]
    pub url: Option<UriT>,

    #[serde(skip_deserializing, skip_serializing)]
    pub path: PathBuf,
}

impl DataHash {
    pub const LABEL: &'static str = labels::DATA_HASH;

    /// Create new DataHash instance
    pub fn new(name: &str, alg: &str) -> Self {
        DataHash {
            exclusions: None,
            name: Some(name.to_string()),
            alg: Some(alg.to_string()),
            hash: Vec::new(),
            pad: Vec::new(),
            pad2: None,
            url: None, //deprecated
            path: PathBuf::new(),
        }
    }

    pub fn add_exclusion(&mut self, exclusion: HashRange) {
        if self.exclusions.is_none() {
            self.exclusions = Some(Vec::new());
        }

        if let Some(ref mut e) = self.exclusions {
            e.push(exclusion);
        }
    }

    pub fn set_hash(&mut self, hash: Vec<u8>) {
        self.hash = hash;
    }

    pub fn add_padding(&mut self, padding: Vec<u8>) {
        self.pad = padding;
    }

    /// Checks if this is a remote hash
    pub fn is_remote_hash(&self) -> bool {
        self.url.is_some()
    }

    /// generate the hash value for the Asset using the range from the DataHash
    pub fn gen_hash(&mut self, asset_path: &Path) -> Result<()> {
        self.hash = self.hash_from_asset(asset_path)?;
        self.path = PathBuf::from(asset_path);
        Ok(())
    }

    /// generate the hash value for the Asset stream using the range from the DataHash
    pub fn gen_hash_from_stream<R>(&mut self, stream: &mut R) -> Result<()>
    where
        R: Read + Seek + ?Sized,
    {
        self.hash = self.hash_from_stream(stream)?;
        Ok(())
    }

    /// Like [`gen_hash_from_stream`] but fires `progress(step, total)` once per
    /// hash range so callers with a [`Context`] can report `ProgressPhase::Hashing`
    /// ticks and support cancellation.
    pub(crate) fn gen_hash_from_stream_with_progress<R, F>(
        &mut self,
        stream: &mut R,
        progress: &mut F,
    ) -> Result<()>
    where
        R: Read + Seek + ?Sized,
        F: FnMut(u32, u32) -> Result<()>,
    {
        if self.is_remote_hash() {
            return Err(Error::BadParam(
                "asset hash is remote, not yet supported".to_owned(),
            ));
        }

        let alg = self.alg.as_deref().unwrap_or("sha256").to_string();
        let exclusions = self.exclusions.clone();

        let hash = hash_stream_by_alg_with_progress(&alg, stream, exclusions, true, progress)?;

        if hash.is_empty() {
            Err(Error::BadParam("could not generate data hash".to_string()))
        } else {
            self.hash = hash;
            Ok(())
        }
    }

    // add padding to match size
    pub fn pad_to_size(&mut self, desired_size: usize) -> Result<()> {
        let mut curr_size = self.to_assertion()?.data().len();

        // this should not happen
        if curr_size > desired_size {
            return Err(Error::JumbfCreationError);
        }

        let mut last_pad = 0;
        loop {
            if curr_size == desired_size {
                break;
            }

            if desired_size > curr_size {
                self.pad.push(0x0);
                curr_size = self.to_assertion()?.data().len();
                last_pad += 1;
            } else {
                match &self.pad2 {
                    Some(_pad2) => return Err(Error::JumbfCreationError),
                    None => {
                        // if we reach here we need a new second padding object to hit exact size
                        self.pad.clear();
                        let pad2_size = last_pad / 2; // spit across two pads
                        self.pad2 = Some(ByteBuf::from(vec![0u8; pad2_size]));
                        return self.pad_to_size(desired_size);
                    }
                }
            }
        }

        Ok(())
    }

    /// generate the asset hash from a file asset using the constructed
    /// start and length values
    fn hash_from_asset(&mut self, asset_path: &Path) -> Result<Vec<u8>> {
        let mut file = std::fs::File::open(asset_path)?;
        self.hash_from_stream(&mut file)
    }

    /// generate the asset hash from a stream using the constructed
    /// start and length values
    pub fn hash_from_stream<R>(&mut self, stream: &mut R) -> Result<Vec<u8>>
    where
        R: Read + Seek + ?Sized,
    {
        if self.is_remote_hash() {
            return Err(Error::BadParam(
                "asset hash is remote, not yet supported".to_owned(),
            ));
        }

        let alg = match self.alg {
            Some(ref a) => a.clone(),
            None => "sha256".to_string(),
        };

        // sort the exclusions
        let hash = match self.exclusions {
            Some(ref e) => hash_stream_by_alg(&alg, stream, Some(e.clone()), true)?,
            None => hash_stream_by_alg(&alg, stream, None, true)?,
        };

        if hash.is_empty() {
            Err(Error::BadParam("could not generate data hash".to_string()))
        } else {
            Ok(hash)
        }
    }

    // verify data using currently set algorithm or default alg is none currently set
    pub fn verify_in_memory_hash(&self, data: &[u8], alg: Option<&str>) -> Result<()> {
        if self.is_remote_hash() {
            return Err(Error::BadParam("asset hash is remote".to_owned()));
        }

        let curr_alg = match &self.alg {
            Some(a) => a.clone(),
            None => match alg {
                Some(a) => a.to_owned(),
                None => "sha256".to_string(),
            },
        };

        let exclusions = self.exclusions.as_ref().cloned();

        if verify_by_alg(&curr_alg, &self.hash, data, exclusions) {
            Ok(())
        } else {
            Err(Error::HashMismatch("Hashes do not match".to_owned()))
        }
    }

    ///  Used to verify a DataHash against an asset.
    #[allow(dead_code)] // used in tests
    pub fn verify_hash(&self, asset_path: &Path, alg: Option<&str>) -> Result<()> {
        if self.is_remote_hash() {
            return Err(Error::BadParam("asset hash is remote".to_owned()));
        }

        let curr_alg = match &self.alg {
            Some(a) => a.clone(),
            None => match alg {
                Some(a) => a.to_owned(),
                None => return Err(Error::HashMismatch("no alg specified".to_owned())),
            },
        };

        let exclusions = self.exclusions.as_ref().cloned();

        if verify_asset_by_alg(&curr_alg, &self.hash, asset_path, exclusions) {
            Ok(())
        } else {
            Err(Error::HashMismatch("Hashes do not match".to_owned()))
        }
    }

    // verify data using currently set algorithm or default alg is none currently set
    pub fn verify_stream_hash(&self, reader: &mut dyn CAIRead, alg: Option<&str>) -> Result<()> {
        self.verify_stream_hash_with_progress(reader, alg, &mut |_, _| Ok(()))
    }

    /// Like [`verify_stream_hash`] but fires `progress(step, total)` once per hash
    /// range so callers with a [`Context`] can report `ProgressPhase::VerifyingAssetHash`
    /// ticks and support cancellation.
    pub(crate) fn verify_stream_hash_with_progress<R, F>(
        &self,
        reader: &mut R,
        alg: Option<&str>,
        progress: &mut F,
    ) -> Result<()>
    where
        R: Read + Seek + ?Sized,
        F: FnMut(u32, u32) -> Result<()>,
    {
        if self.is_remote_hash() {
            return Err(Error::BadParam("asset hash is remote".to_owned()));
        }

        let curr_alg = match &self.alg {
            Some(a) => a.clone(),
            None => match alg {
                Some(a) => a.to_owned(),
                None => return Err(Error::HashMismatch("no alg specified".to_owned())),
            },
        };

        let exclusions = self.exclusions.as_ref().cloned();

        let computed =
            hash_stream_by_alg_with_progress(&curr_alg, reader, exclusions, true, progress)?;

        if vec_compare(&self.hash, &computed) {
            Ok(())
        } else {
            Err(Error::HashMismatch("Hashes do not match".to_owned()))
        }
    }

    /// Create a new instance from Assertion
    pub fn from_assertion(assertion: &Assertion) -> Result<Self> {
        assertion.check_version_from_label(ASSERTION_CREATION_VERSION)?;
        Self::from_cbor_assertion(assertion)
    }
}

impl AssertionCbor for DataHash {}

impl AssertionBase for DataHash {
    const LABEL: &'static str = Self::LABEL;
    const VERSION: Option<usize> = Some(ASSERTION_CREATION_VERSION);

    fn to_assertion(&self) -> Result<Assertion> {
        Self::to_cbor_assertion(self)
    }

    fn from_assertion(assertion: &Assertion) -> Result<Self> {
        Self::from_cbor_assertion(assertion)
    }
}

#[cfg(test)]
pub mod tests {
    #![allow(clippy::panic)]
    #![allow(clippy::unwrap_used)]

    use super::*;
    use crate::{assertion::AssertionData, utils::test::fixture_path};

    #[test]
    fn test_build_assertion() {
        // try json based assertion
        let mut data_hash = DataHash::new("Some data", "sha256");
        data_hash.add_exclusion(HashRange::new(0, 1234));
        data_hash.hash = vec![1, 2, 3];

        let assertion = data_hash.to_assertion().unwrap();

        println!("assertion label: {}", assertion.label());

        let j = assertion.data();

        let from_j = Assertion::from_data_cbor(&assertion.label(), j);
        let ad_ref = from_j.decode_data();

        let _assertion_type = match ad_ref {
            AssertionData::Cbor(ref _ad_cbor) => "cbor",
            AssertionData::Json(ref _ad_json) => "json",
            AssertionData::Binary(ref _ad_bin) => "binary",
            AssertionData::Uuid(_, _) => "uuid",
        };

        if let AssertionData::Cbor(ref ad_cbor) = ad_ref {
            // compare results
            let orig_d = assertion.decode_data();
            if let AssertionData::Cbor(ref orig_cbor) = orig_d {
                // TO DISCUSS: Maurice, I'm not quite sure what we were testing
                // in the original test. LMK if I've lost too much in translation
                // here.
                let orig_as_value: DataHash = c2pa_cbor::from_slice(orig_cbor).unwrap();
                let ad_as_value: DataHash = c2pa_cbor::from_slice(ad_cbor).unwrap();

                assert_eq!(orig_as_value, ad_as_value);
            } else {
                panic!("Couldn't decode orig_d");
            }
        } else {
            panic!("Couldn't decode ad_ref");
        }
    }

    #[test]
    fn test_binary_round_trip() {
        let mut data_hash = DataHash::new("Some data", "sha256");
        data_hash.add_exclusion(HashRange::new(0x2000, 0x1000));
        data_hash.add_exclusion(HashRange::new(0x4000, 0x1000));

        // add some data to hash
        let ap = fixture_path("earth_apollo17.jpg");

        // generate the hash
        data_hash.gen_hash(&ap).unwrap();

        // verify
        data_hash.verify_hash(&ap, None).unwrap();

        let assertion = data_hash.to_assertion().unwrap();

        let orig_bytes = assertion.data();

        let assertion_from_binary = Assertion::from_data_cbor(&assertion.label(), orig_bytes);

        println!(
            "Label Match Test {} = {}",
            assertion.label(),
            assertion_from_binary.label()
        );

        assert_eq!(assertion.label(), assertion_from_binary.label());

        // compare the data as bytes
        assert_eq!(orig_bytes, assertion_from_binary.data());
        println!("Decoded binary matches");
    }
}