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
419
420
421
// Copyright 2024 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::{
    borrow::Cow,
    collections::BTreeMap,
    fmt::{Debug, Formatter},
};

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

use crate::{
    crypto::cose::{CertificateTrustPolicy, Verifier},
    dynamic_assertion::PartialClaim,
    identity::{
        claim_aggregation::IcaSignatureVerifier,
        identity_assertion::{
            report::{
                IdentityAssertionReport, IdentityAssertionsForManifest,
                IdentityAssertionsForManifestStore, SignerPayloadReport,
            },
            signer_payload::SignerPayload,
        },
        internal::debug_byte_slice::DebugByteSlice,
        x509::X509SignatureVerifier,
        SignatureVerifier, ToCredentialSummary, ValidationError,
    },
    jumbf::labels::to_assertion_uri,
    log_current_item, log_item,
    status_tracker::StatusTracker,
    Context, Manifest, Reader,
};

/// This struct represents the raw content of the identity assertion.
///
/// Use [`AsyncIdentityAssertionBuilder`] and -- at your option,
/// [`AsyncIdentityAssertionSigner`] -- to ensure correct construction of a new
/// identity assertion.
///
/// [`AsyncIdentityAssertionBuilder`]: crate::identity::builder::AsyncIdentityAssertionBuilder
/// [`AsyncIdentityAssertionSigner`]: crate::identity::builder::AsyncIdentityAssertionSigner
#[derive(Deserialize, Serialize)]
pub struct IdentityAssertion {
    pub(crate) signer_payload: SignerPayload,

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

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

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

    // Label for the assertion. Only assigned when reading from a manifest.
    #[serde(skip)]
    pub(crate) label: Option<String>,
}

#[allow(unused)] // TEMPORARY while considering API simplification
impl IdentityAssertion {
    /// Find the `IdentityAssertion`s that may be present in a given
    /// [`Manifest`].
    ///
    /// Iterator returns a [`Result`] because each assertion may fail to parse.
    ///
    /// Aside from CBOR parsing, no further validation is performed.
    pub(crate) fn from_manifest<'a>(
        manifest: &'a Manifest,
        status_tracker: &'a mut StatusTracker,
    ) -> impl Iterator<Item = Result<Self, crate::Error>> + use<'a> {
        manifest
            .assertions()
            .iter()
            .filter(|a| a.label() == "cawg.identity" || a.label().starts_with("cawg.identity__"))
            .map(|a| {
                let mut ia: Result<Self, crate::Error> = a.to_assertion();
                if let Ok(ref mut ia) = ia {
                    if let Some(manifest_label) = manifest.label() {
                        ia.label = Some(to_assertion_uri(manifest_label, a.label()));
                    }
                }
                // TO DO: Add error readout if the proposed new setting resulted
                // in this assertion being parsed and converted to JSON. This function
                // has become incompatible with the now-default behavior to validate
                // identity assertions during parsing. This applies only if this API
                // becomes public again.
                (a.label().to_owned(), ia)
            })
            .inspect(|(label, r)| {
                let mut label = label.to_owned();
                if let Err(err) = r {
                    if let Some(manifest_label) = manifest.label() {
                        label = to_assertion_uri(manifest_label, &label);
                    }

                    log_item!(label, "invalid CBOR", "IdentityAssertion::from_manifest")
                        .validation_status("cawg.identity.cbor.invalid")
                        .failure_no_throw(
                            status_tracker,
                            crate::Error::AssertionSpecificError(err.to_string()),
                        );
                }
            })
            .map(move |(_label, r)| r)
    }

    /// Create a summary report from this `IdentityAssertion`.
    ///
    /// This will [`validate`] the assertion and then render the result as
    /// an opaque [`Serialize`]-able struct that describes the decoded content
    /// of the identity assertion.
    ///
    /// [`validate`]: Self::validate
    pub(crate) async fn to_summary<SV: SignatureVerifier>(
        &self,
        manifest: &Manifest,
        status_tracker: &mut StatusTracker,
        verifier: &SV,
    ) -> impl Serialize
    where
        <SV as SignatureVerifier>::Output: 'static,
    {
        self.to_summary_impl(manifest, status_tracker, verifier)
            .await
    }

    pub(crate) async fn to_summary_impl<SV: SignatureVerifier>(
        &self,
        manifest: &Manifest,
        status_tracker: &mut StatusTracker,
        verifier: &SV,
    ) -> IdentityAssertionReport<
        <<SV as SignatureVerifier>::Output as ToCredentialSummary>::CredentialSummary,
    >
    where
        <SV as SignatureVerifier>::Output: 'static,
    {
        match self.validate(manifest, status_tracker, verifier).await {
            Ok(named_actor) => {
                let summary = named_actor.to_summary();

                IdentityAssertionReport {
                    signer_payload: SignerPayloadReport::from_signer_payload(&self.signer_payload),
                    named_actor: Some(summary),
                }
            }

            Err(_err) => {
                todo!("Handle summary report for failure case");
            }
        }
    }

    /// Summarize all of the identity assertions found for a [`Manifest`].
    pub(crate) async fn summarize_all<SV: SignatureVerifier>(
        manifest: &Manifest,
        status_tracker: &mut StatusTracker,
        verifier: &SV,
    ) -> impl Serialize {
        Self::summarize_all_impl(manifest, status_tracker, verifier).await
    }

    pub(crate) async fn summarize_all_impl<SV: SignatureVerifier>(
        manifest: &Manifest,
        status_tracker: &mut StatusTracker,
        verifier: &SV,
    ) -> IdentityAssertionsForManifest<
        <<SV as SignatureVerifier>::Output as ToCredentialSummary>::CredentialSummary,
    > {
        // NOTE: We can't write this using .map(...).collect() because there are async
        // calls.
        let mut reports: Vec<
            IdentityAssertionReport<
                <<SV as SignatureVerifier>::Output as ToCredentialSummary>::CredentialSummary,
            >,
        > = vec![];

        let assertion_results: Vec<Result<IdentityAssertion, crate::Error>> =
            Self::from_manifest(manifest, status_tracker).collect();

        for assertion in assertion_results {
            let report = match assertion {
                Ok(assertion) => {
                    assertion
                        .to_summary_impl(manifest, status_tracker, verifier)
                        .await
                }
                Err(_) => {
                    todo!("Handle assertion failed to parse case");
                }
            };

            reports.push(report);
        }

        IdentityAssertionsForManifest::<
            <<SV as SignatureVerifier>::Output as ToCredentialSummary>::CredentialSummary,
        > {
            assertion_reports: reports,
        }
    }

    /// Summarize all of the identity assertions found for a [`Reader`].
    pub(crate) async fn summarize_from_reader<SV: SignatureVerifier>(
        reader: &Reader,
        status_tracker: &mut StatusTracker,
        verifier: &SV,
    ) -> impl Serialize {
        // NOTE: We can't write this using .map(...).collect() because there are async
        // calls.
        let mut reports: BTreeMap<
            String,
            IdentityAssertionsForManifest<
                <<SV as SignatureVerifier>::Output as ToCredentialSummary>::CredentialSummary,
            >,
        > = BTreeMap::new();

        for (id, manifest) in reader.manifests() {
            let report = Self::summarize_all_impl(manifest, status_tracker, verifier).await;
            reports.insert(id.clone(), report);
        }

        IdentityAssertionsForManifestStore::<
            <<SV as SignatureVerifier>::Output as ToCredentialSummary>::CredentialSummary,
        > {
            assertions_for_manifest: reports,
        }
    }

    /// Using the provided [`SignatureVerifier`], check the validity of this
    /// identity assertion.
    ///
    /// If successful, returns the credential-type specific information that can
    /// be derived from the signature. This is the [`SignatureVerifier::Output`]
    /// type which typically describes the named actor, but may also contain
    /// information about the time of signing or the credential's source.
    pub(crate) async fn validate<SV: SignatureVerifier>(
        &self,
        manifest: &Manifest,
        status_tracker: &mut StatusTracker,
        verifier: &SV,
    ) -> Result<SV::Output, ValidationError<SV::Error>> {
        if let Some(ref label) = self.label {
            status_tracker.push_current_uri(label);
        }

        let result = self.validate_imp(manifest, status_tracker, verifier).await;

        if self.label.is_some() {
            status_tracker.pop_current_uri();
        }

        result
    }

    async fn validate_imp<SV: SignatureVerifier>(
        &self,
        manifest: &Manifest,
        status_tracker: &mut StatusTracker,
        verifier: &SV,
    ) -> Result<SV::Output, ValidationError<SV::Error>> {
        self.check_padding(status_tracker)?;

        self.signer_payload
            .check_against_manifest(manifest, status_tracker)?;

        verifier
            .check_signature(&self.signer_payload, &self.signature, status_tracker)
            .await
    }

    /// Using the provided [`SignatureVerifier`], check the validity of this
    /// identity assertion.
    ///
    /// If successful, returns the credential-type specific information that can
    /// be derived from the signature. This is the [`SignatureVerifier::Output`]
    /// type which typically describes the named actor, but may also contain
    /// information about the time of signing or the credential's source.
    pub(crate) async fn validate_partial_claim(
        &self,
        partial_claim: &PartialClaim,
        status_tracker: &mut StatusTracker,
    ) -> Result<serde_json::Value, ValidationError<String>> {
        let settings = Context::new().settings().clone();
        self.check_padding(status_tracker)?;

        self.signer_payload
            .check_against_partial_claim(partial_claim, status_tracker)?;

        let sig_type = self.signer_payload.sig_type.as_str();

        if sig_type == "cawg.x509.cose" {
            let mut ctp = CertificateTrustPolicy::default();

            // Load the trust handler settings. Don't worry about status as these
            // are checked during setting generation.

            let cose_verifier = if settings.cawg_trust.verify_trust_list {
                if let Some(ta) = &settings.cawg_trust.trust_anchors {
                    let _ = ctp.add_trust_anchors(ta.as_bytes());
                }

                if let Some(pa) = &settings.cawg_trust.user_anchors {
                    let _ = ctp.add_user_trust_anchors(pa.as_bytes());
                }

                if let Some(tc) = &settings.cawg_trust.trust_config {
                    ctp.add_valid_ekus(tc.as_bytes());
                }

                if let Some(al) = &settings.cawg_trust.allowed_list {
                    let _ = ctp.add_end_entity_credentials(al.as_bytes());
                }

                Verifier::VerifyTrustPolicy(Cow::Owned(ctp))
            } else {
                Verifier::IgnoreProfileAndTrustPolicy
            };

            let verifier = X509SignatureVerifier { cose_verifier };

            let result = verifier
                .check_signature(&self.signer_payload, &self.signature, status_tracker)
                .await
                .map(|v| v.to_summary())
                .map_err(|e| ValidationError::UnknownSignatureType(e.to_string()))?;

            log_current_item!(
                "CAWG X.509 identity signature valid",
                "validate_partial_claim"
            )
            .validation_status("cawg.identity.well-formed")
            .success(status_tracker);
            // TO DO (CAI-7980): Should instead issue `cawg.identity.trusted` if the
            // signing cert is found on a configured trust list.

            serde_json::to_value(result)
                .map_err(|e| ValidationError::UnknownSignatureType(e.to_string()))
        } else if sig_type == "cawg.identity_claims_aggregation" {
            let verifier = IcaSignatureVerifier {};

            let result = verifier
                .check_signature(&self.signer_payload, &self.signature, status_tracker)
                .await
                .map(|v| v.to_summary())
                .map_err(|e| ValidationError::UnknownSignatureType(e.to_string()))?;
            log_current_item!(
                "CAWG identity_claims_aggregation signature valid",
                "validate_partial_claim"
            )
            .validation_status("cawg.ica.credential_valid")
            .success(status_tracker);

            serde_json::to_value(result)
                .map_err(|e| ValidationError::UnknownSignatureType(e.to_string()))
        } else {
            Err(ValidationError::UnknownSignatureType(sig_type.to_string()))
        }
    }

    fn check_padding<E: Debug>(
        &self,
        status_tracker: &mut StatusTracker,
    ) -> Result<(), ValidationError<E>> {
        if !self.pad1.iter().all(|b| *b == 0) {
            log_current_item!(
                "invalid value in pad fields",
                "SignerPayload::check_padding"
            )
            .validation_status("cawg.identity.pad.invalid")
            .failure(status_tracker, ValidationError::<E>::InvalidPadding)?;

            // We'll only get to this line if `pad1` is invalid and the status tracker is
            // configured to continue through recoverable errors. In that case, we want to
            // avoid logging a second "invalid padding" warning if `pad2` is also invalid.
            return Ok(());
        }

        if let Some(pad2) = self.pad2.as_ref() {
            if !pad2.iter().all(|b| *b == 0) {
                log_current_item!(
                    "invalid value in pad fields",
                    "SignerPayload::check_padding"
                )
                .validation_status("cawg.identity.pad.invalid")
                .failure(status_tracker, ValidationError::<E>::InvalidPadding)?;
            }
        }

        Ok(())
    }

    /// TO DO: Docs
    pub fn signer_payload(&self) -> &SignerPayload {
        &self.signer_payload
    }
}

impl Debug for IdentityAssertion {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        f.debug_struct("IdentityAssertion")
            .field("signer_payload", &self.signer_payload)
            .field("signature", &DebugByteSlice(&self.signature))
            .field("label", &self.label)
            .finish()
    }
}