trazaeo 0.5.7

Open-source provenance SDK and specification for verifiable EO and climate data workflows
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
use crate::envelope::{verify_attestation, PublishEnvelope};
use crate::error::TrazaeoResult;
use crate::trust::{TrustPolicy, TrustStatus};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum VerificationMode {
    Sampled,
    Full,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum VerificationState {
    Passed,
    Failed,
    NotEvaluated,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum VerifiabilityTier {
    Core,
    CoreWithC2paInterop,
    CoreWithStorageBinding,
    CoreWithStorageBindingAndC2paInterop,
    CoreWithProofLogCommitment,
    CoreWithProofLogCommitmentAndC2paInterop,
    CoreWithStorageBindingAndProofLogCommitment,
    CoreWithStorageBindingProofLogCommitmentAndC2paInterop,
    Failed,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct VerificationReport {
    pub mode: VerificationMode,
    pub signature_state: VerificationState,
    pub trust_state: VerificationState,
    pub binding_state: VerificationState,
    pub reproducibility_state: VerificationState,
    pub lineage_state: VerificationState,
    pub c2pa_interop_state: VerificationState,
    pub storage_state: VerificationState,
    pub proof_log_state: VerificationState,
    pub reasons: Vec<String>,
}

fn verify_c2pa_interop_state(
    envelope: &PublishEnvelope,
    validation_errors: &[String],
    reasons: &mut Vec<String>,
) -> VerificationState {
    let has_manifest = envelope
        .c2pa_manifest_ref
        .as_ref()
        .is_some_and(|value| !value.trim().is_empty())
        || envelope
            .c2pa_manifest_hash
            .as_ref()
            .is_some_and(|value| !value.trim().is_empty());
    let has_projection = !envelope.c2pa_ingredients.is_empty() || !envelope.c2pa_actions.is_empty();

    if !has_manifest && !has_projection {
        return VerificationState::NotEvaluated;
    }
    if validation_errors
        .iter()
        .any(|error| error.contains("c2pa_") || error.contains("C2PA"))
    {
        return VerificationState::Failed;
    }
    if envelope.c2pa_ingredients.is_empty() {
        reasons.push("C2PA interop requires at least one ingredient".to_string());
        return VerificationState::Failed;
    }
    if envelope.c2pa_actions.is_empty() {
        reasons.push("C2PA interop requires at least one action".to_string());
        return VerificationState::Failed;
    }
    VerificationState::Passed
}
pub fn verify_publish_envelope(
    envelope: &PublishEnvelope,
    mode: VerificationMode,
    trust_policy: &TrustPolicy,
) -> VerificationReport {
    let mut reasons = Vec::new();
    let validation_errors = envelope.validate().err().unwrap_or_default();
    reasons.extend(
        validation_errors
            .iter()
            .map(|error| format!("invalid envelope: {error}")),
    );

    let payload = envelope.canonical_attestation_payload_bytes();
    let signature_state = if envelope.attestations.is_empty() {
        reasons.push("missing attestations".to_string());
        VerificationState::Failed
    } else if !envelope
        .attestations
        .iter()
        .all(|att| att.key_id == envelope.key_id)
    {
        reasons.push("attestation key_id must match envelope key_id".to_string());
        VerificationState::Failed
    } else if envelope
        .attestations
        .iter()
        .all(|att| verify_attestation(att, &payload))
    {
        VerificationState::Passed
    } else {
        reasons.push("invalid attestation signature".to_string());
        VerificationState::Failed
    };

    let trust_state = match trust_policy.trust_status(&envelope.key_id) {
        TrustStatus::Trusted => VerificationState::Passed,
        TrustStatus::Revoked => {
            reasons.push("key is revoked".to_string());
            VerificationState::Failed
        }
        TrustStatus::Untrusted => {
            reasons.push("key is untrusted".to_string());
            VerificationState::Failed
        }
    };

    let binding_state = if !validation_errors.is_empty() {
        VerificationState::Failed
    } else if envelope.input_refs.is_empty() || envelope.output_refs.is_empty() {
        reasons.push("missing input/output refs".to_string());
        VerificationState::Failed
    } else {
        VerificationState::Passed
    };

    let reproducibility_state = if mode == VerificationMode::Full {
        if envelope.verification_policy_id.trim().is_empty() {
            reasons.push("missing verification policy id".to_string());
            VerificationState::Failed
        } else {
            VerificationState::Passed
        }
    } else {
        VerificationState::NotEvaluated
    };

    let lineage_state = if envelope.lineage_refs.is_empty() {
        reasons.push("missing lineage refs".to_string());
        VerificationState::Failed
    } else if envelope.provenance_start_mode == "source_capture" {
        reasons.push(
            "source_capture provenance requires verification against source capture lineage envelopes"
                .to_string(),
        );
        VerificationState::Failed
    } else {
        VerificationState::Passed
    };
    let c2pa_interop_state = verify_c2pa_interop_state(envelope, &validation_errors, &mut reasons);

    VerificationReport {
        mode,
        signature_state,
        trust_state,
        binding_state,
        reproducibility_state,
        lineage_state,
        c2pa_interop_state,
        storage_state: VerificationState::NotEvaluated,
        proof_log_state: VerificationState::NotEvaluated,
        reasons,
    }
}
pub fn verify_storage_binding(
    report: VerificationReport,
    envelope: &PublishEnvelope,
    stored_uri: &str,
) -> VerificationReport {
    let mut updated = report;
    if envelope.output_refs.iter().any(|value| value == stored_uri) {
        updated.storage_state = VerificationState::Passed;
    } else {
        updated.storage_state = VerificationState::Failed;
        updated
            .reasons
            .push("stored object uri not present in output refs".to_string());
    }
    updated
}
pub fn apply_proof_log_result(
    report: VerificationReport,
    result: TrazaeoResult<()>,
) -> VerificationReport {
    let mut updated = report;
    match result {
        Ok(()) => updated.proof_log_state = VerificationState::Passed,
        Err(err) => {
            updated.proof_log_state = VerificationState::Failed;
            updated.reasons.push(err.to_string());
        }
    }
    updated
}
pub fn verifiability_tier(report: &VerificationReport) -> VerifiabilityTier {
    if !is_report_success(report) {
        return VerifiabilityTier::Failed;
    }
    match (
        report.storage_state,
        report.proof_log_state,
        report.c2pa_interop_state,
    ) {
        (VerificationState::Passed, VerificationState::Passed, VerificationState::Passed) => {
            VerifiabilityTier::CoreWithStorageBindingProofLogCommitmentAndC2paInterop
        }
        (VerificationState::Passed, VerificationState::Passed, _) => {
            VerifiabilityTier::CoreWithStorageBindingAndProofLogCommitment
        }
        (VerificationState::Passed, _, VerificationState::Passed) => {
            VerifiabilityTier::CoreWithStorageBindingAndC2paInterop
        }
        (_, VerificationState::Passed, VerificationState::Passed) => {
            VerifiabilityTier::CoreWithProofLogCommitmentAndC2paInterop
        }
        (VerificationState::Passed, _, _) => VerifiabilityTier::CoreWithStorageBinding,
        (_, VerificationState::Passed, _) => VerifiabilityTier::CoreWithProofLogCommitment,
        (_, _, VerificationState::Passed) => VerifiabilityTier::CoreWithC2paInterop,
        _ => VerifiabilityTier::Core,
    }
}
pub fn is_report_success(report: &VerificationReport) -> bool {
    let signature_ok = report.signature_state == VerificationState::Passed;
    let trust_ok = report.trust_state == VerificationState::Passed;
    let binding_ok = report.binding_state == VerificationState::Passed;
    let repro_ok = report.reproducibility_state == VerificationState::Passed
        || report.reproducibility_state == VerificationState::NotEvaluated;
    let lineage_ok = report.lineage_state == VerificationState::Passed;
    let c2pa_ok = report.c2pa_interop_state == VerificationState::Passed
        || report.c2pa_interop_state == VerificationState::NotEvaluated;
    let storage_ok = report.storage_state == VerificationState::Passed
        || report.storage_state == VerificationState::NotEvaluated;
    let proof_log_ok = report.proof_log_state == VerificationState::Passed
        || report.proof_log_state == VerificationState::NotEvaluated;
    signature_ok
        && trust_ok
        && binding_ok
        && repro_ok
        && lineage_ok
        && c2pa_ok
        && storage_ok
        && proof_log_ok
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::envelope::{make_attestation, Attestation};
    use crate::error::TrazaeoError;
    use crate::trust::TrustPolicy;

    const TEST_SIGNING_KEY_HEX: &str =
        "4f3edf983ac636a65a842ce7c78d9aa706d3b113bce036f9a4f5762b76f70f18";
    fn publish() -> PublishEnvelope {
        let mut p = PublishEnvelope {
            schema_version: "1.0.0".to_string(),
            envelope_type: "publish".to_string(),
            issued_at: "2026-01-01T00:00:00Z".to_string(),
            subject_id: "publish-1".to_string(),
            dataset_id: "sst".to_string(),
            dataset_version: "v1".to_string(),
            input_refs: vec!["obj://in".to_string()],
            output_refs: vec!["obj://out".to_string()],
            published_artifacts: vec![crate::checkpoint::CheckpointArtifact {
                artifact_id: "artifact-1".to_string(),
                content_root_hash: "hash".to_string(),
                content_descriptor_ref: None,
                content_descriptor_hash: None,
                media_type: "application/octet-stream".to_string(),
            }],
            primary_artifact_id: "artifact-1".to_string(),
            checkpoint_manifest_ref: "checkpoint://1".to_string(),
            checkpoint_manifest_hash: "checkpoint-hash".to_string(),
            checkpoint_id: "checkpoint-1".to_string(),
            checkpoint_log_root_hash: "checkpoint-log-root".to_string(),
            lineage_refs: vec!["capture://1".to_string()],
            verification_policy_id: "verify-default".to_string(),
            attestations: vec![],
            key_id: "key-1".to_string(),
            stac_refs: vec![],
            ogc_refs: vec![],
            c2pa_manifest_ref: None,
            c2pa_manifest_hash: None,
            c2pa_ingredients: vec![],
            c2pa_actions: vec![],
            reward_context_ref: None,
            reward_context_hash: None,
            provenance_start_mode: "transport_capture".to_string(),
            bootstrap_origin_label: None,
            reward_eligible: false,
        };
        let key_seed = make_attestation("s", TEST_SIGNING_KEY_HEX, "2026-01-01T00:00:00Z", b"")
            .expect("derive key id");
        p.key_id = key_seed.key_id.clone();
        p.attestations = vec![Attestation {
            signer_id: "s".to_string(),
            key_id: key_seed.key_id.clone(),
            signature: String::new(),
            signed_at: "2026-01-01T00:00:00Z".to_string(),
        }];
        let payload = p.canonical_attestation_payload_bytes();
        let attestation =
            make_attestation("s", TEST_SIGNING_KEY_HEX, "2026-01-01T00:00:00Z", &payload)
                .expect("build attestation");
        p.attestations = vec![attestation];
        p
    }

    fn resign_publish(p: &mut PublishEnvelope) {
        let key_seed = make_attestation("s", TEST_SIGNING_KEY_HEX, "2026-01-01T00:00:00Z", b"")
            .expect("derive key id");
        p.key_id = key_seed.key_id.clone();
        p.attestations = vec![Attestation {
            signer_id: "s".to_string(),
            key_id: key_seed.key_id,
            signature: String::new(),
            signed_at: "2026-01-01T00:00:00Z".to_string(),
        }];
        let payload = p.canonical_attestation_payload_bytes();
        p.attestations =
            vec![
                make_attestation("s", TEST_SIGNING_KEY_HEX, "2026-01-01T00:00:00Z", &payload)
                    .expect("build attestation"),
            ];
    }
    fn trust_policy_for(key_id: &str) -> TrustPolicy {
        let mut p = TrustPolicy::new();
        p.allow_key(key_id);
        p
    }
    #[test]
    fn sampled_verification_success() {
        let report = verify_publish_envelope(
            &publish(),
            VerificationMode::Sampled,
            &trust_policy_for(&publish().key_id),
        );
        assert!(is_report_success(&report));
        assert_eq!(
            report.reproducibility_state,
            VerificationState::NotEvaluated
        );
        assert_eq!(report.lineage_state, VerificationState::Passed);
    }
    #[test]
    fn full_verification_success() {
        let report = verify_publish_envelope(
            &publish(),
            VerificationMode::Full,
            &trust_policy_for(&publish().key_id),
        );
        assert!(is_report_success(&report));
        assert_eq!(report.reproducibility_state, VerificationState::Passed);
        assert_eq!(report.lineage_state, VerificationState::Passed);
        assert_eq!(report.storage_state, VerificationState::NotEvaluated);
        assert_eq!(report.proof_log_state, VerificationState::NotEvaluated);
    }
    #[test]
    fn verification_fails_source_capture_publish_without_lineage_envelopes() {
        let mut p = publish();
        p.provenance_start_mode = "source_capture".to_string();
        resign_publish(&mut p);

        let report =
            verify_publish_envelope(&p, VerificationMode::Sampled, &trust_policy_for(&p.key_id));

        assert_eq!(report.lineage_state, VerificationState::Failed);
        assert!(!is_report_success(&report));
        assert!(report.reasons.iter().any(|reason| reason.contains(
            "source_capture provenance requires verification against source capture lineage envelopes"
        )));
    }

    #[test]
    fn verification_fails_without_attestation() {
        let mut p = publish();
        p.attestations.clear();
        let report =
            verify_publish_envelope(&p, VerificationMode::Sampled, &trust_policy_for(&p.key_id));
        assert!(!is_report_success(&report));
        assert_eq!(report.signature_state, VerificationState::Failed);
    }
    #[test]
    fn verification_fails_when_key_untrusted() {
        let report =
            verify_publish_envelope(&publish(), VerificationMode::Sampled, &TrustPolicy::new());
        assert_eq!(report.trust_state, VerificationState::Failed);
    }
    #[test]
    fn verification_fails_when_signature_tampered() {
        let mut p = publish();
        p.dataset_version = "v2".to_string();
        let report =
            verify_publish_envelope(&p, VerificationMode::Sampled, &trust_policy_for(&p.key_id));
        assert_eq!(report.signature_state, VerificationState::Failed);
    }
    #[test]
    fn verification_fails_when_attestation_key_differs_from_trusted_envelope_key() {
        const TRUSTED_SIGNING_KEY_HEX: &str =
            "4f3edf983ac636a65a842ce7c78d9aa706d3b113bce036f9a4f5762b76f70f18";
        const ATTACKER_SIGNING_KEY_HEX: &str =
            "0000000000000000000000000000000000000000000000000000000000000001";
        let trusted_key = make_attestation(
            "trusted",
            TRUSTED_SIGNING_KEY_HEX,
            "2026-01-01T00:00:00Z",
            b"",
        )
        .expect("trusted key");
        let attacker_key = make_attestation(
            "attacker",
            ATTACKER_SIGNING_KEY_HEX,
            "2026-01-01T00:00:00Z",
            b"",
        )
        .expect("attacker key");
        let mut p = publish();
        p.key_id = trusted_key.key_id;
        for attestation in &mut p.attestations {
            attestation.key_id = attacker_key.key_id.clone();
            attestation.signature.clear();
        }
        let payload = p.canonical_attestation_payload_bytes();
        p.attestations = vec![make_attestation(
            "attacker",
            ATTACKER_SIGNING_KEY_HEX,
            "2026-01-01T00:00:00Z",
            &payload,
        )
        .expect("attacker attestation")];

        let report =
            verify_publish_envelope(&p, VerificationMode::Sampled, &trust_policy_for(&p.key_id));

        assert_eq!(report.signature_state, VerificationState::Failed);
        assert_eq!(report.trust_state, VerificationState::Passed);
        assert!(!is_report_success(&report));
        assert!(report
            .reasons
            .iter()
            .any(|reason| reason.contains("attestation key_id must match envelope key_id")));
    }
    #[test]
    fn verification_fails_when_publish_envelope_validation_fails() {
        let mut p = publish();
        p.envelope_type = "not-publish".to_string();

        let report =
            verify_publish_envelope(&p, VerificationMode::Sampled, &trust_policy_for(&p.key_id));

        assert_eq!(report.binding_state, VerificationState::Failed);
        assert!(!is_report_success(&report));
        assert!(report
            .reasons
            .iter()
            .any(|reason| reason.contains("envelope_type must be 'publish'")));
    }
    #[test]
    fn verification_fails_without_lineage_refs() {
        let mut p = publish();
        p.lineage_refs.clear();
        let report =
            verify_publish_envelope(&p, VerificationMode::Sampled, &trust_policy_for(&p.key_id));
        assert_eq!(report.lineage_state, VerificationState::Failed);
        assert!(!is_report_success(&report));
    }
    #[test]
    fn c2pa_interop_metadata_raises_verifiability_tier() {
        let mut p = publish();
        p.c2pa_ingredients = vec![crate::c2pa::input_ref_ingredient("obj://in")];
        p.c2pa_actions = vec![crate::c2pa::C2paAction {
            action: crate::c2pa::C2PA_ACTION_PUBLISHED.to_string(),
            when: "2026-01-01T00:00:00Z".to_string(),
            software_agent: "trazaeo-test".to_string(),
            parameters_ref: None,
            parameters_hash: None,
            description: None,
        }];
        resign_publish(&mut p);

        let report =
            verify_publish_envelope(&p, VerificationMode::Sampled, &trust_policy_for(&p.key_id));

        assert_eq!(report.c2pa_interop_state, VerificationState::Passed);
        assert_eq!(
            verifiability_tier(&report),
            VerifiabilityTier::CoreWithC2paInterop
        );
        assert!(is_report_success(&report));
    }
    #[test]
    fn partial_c2pa_interop_metadata_fails_verification() {
        let mut p = publish();
        p.c2pa_actions = vec![crate::c2pa::C2paAction {
            action: crate::c2pa::C2PA_ACTION_PUBLISHED.to_string(),
            when: "2026-01-01T00:00:00Z".to_string(),
            software_agent: "trazaeo-test".to_string(),
            parameters_ref: None,
            parameters_hash: None,
            description: None,
        }];
        resign_publish(&mut p);

        let report =
            verify_publish_envelope(&p, VerificationMode::Sampled, &trust_policy_for(&p.key_id));

        assert_eq!(report.c2pa_interop_state, VerificationState::Failed);
        assert!(!is_report_success(&report));
        assert!(report
            .reasons
            .iter()
            .any(|reason| reason.contains("C2PA interop requires at least one ingredient")));
    }
    #[test]
    fn storage_binding_can_raise_verifiability() {
        let report = verify_publish_envelope(
            &publish(),
            VerificationMode::Sampled,
            &trust_policy_for(&publish().key_id),
        );
        let mut bound_publish = publish();
        bound_publish.output_refs = vec!["s3://bucket/output".to_string()];
        let report = verify_storage_binding(report, &bound_publish, "s3://bucket/output");
        assert_eq!(report.storage_state, VerificationState::Passed);
        assert_eq!(
            verifiability_tier(&report),
            VerifiabilityTier::CoreWithStorageBinding
        );
    }
    #[test]
    fn proof_log_failure_is_recorded() {
        let report = verify_publish_envelope(
            &publish(),
            VerificationMode::Sampled,
            &trust_policy_for(&publish().key_id),
        );
        let report = apply_proof_log_result(
            report,
            Err(TrazaeoError::external(
                "apply proof log result",
                "missing proof log receipt",
            )),
        );
        assert_eq!(report.proof_log_state, VerificationState::Failed);
        assert!(!is_report_success(&report));
        assert_eq!(verifiability_tier(&report), VerifiabilityTier::Failed);
    }
}