structured-cades 0.16.1

Source-bound CAdES baseline composition over CMS and long-term validation facts
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! Bind complete RFC4998 execution to the actual CMS object and `CAdES` paths.
use super::{CmsObject, ObjectError, View};
use crate::{
    Error,
    basic::PathRestriction,
    prepare::Preparation,
    preservation::{Kind, Required},
    timestamp::{self, ProfileError, VisitError},
};
use core::{
    cell::{Cell, RefCell},
    convert::Infallible,
};
use structured_asn1::{
    Element, Reader,
    memory::{Execution, OwnedVec, Work},
};
use structured_cms::{
    evidence::{ArchiveTimeStamp, Membership, verify as ers},
    provider::SignatureProvider,
    stream::{Receiver, Source},
    timestamp::TokenExtension,
};
use structured_ltv::pki::CheckedPath;
use structured_pkix::temporal::{Instant, LeapTable};

const FIELD: &str = "cades.evidence.record";

/// The original candidate's already checked path and certificate source.
pub struct Authority<'path, 'd> {
    /// Same original effective key/path used for signed ESS restrictions.
    pub path: &'path CheckedPath<'d>,
    /// Candidate's own original producing reader, not a token/default parser.
    pub certificate_parent: &'path Reader<'d>,
}
/// Select paths for actual original ERS timestamps; no crypto runs here.
pub trait Authorities<'d> {
    /// Original caller selection failure.
    type Error;
    /// Current Work must fund any caller-side catalogue/discovery work.
    fn select<'path, W: Work>(
        &'path self,
        index: usize,
        token: &structured_cms::timestamp::Token<'d>,
        work: &W,
    ) -> Result<Authority<'path, 'd>, Self::Error>
    where
        'd: 'path;
}

/// Original whole-object/explicit detached acquisition failure.
#[derive(Debug)]
pub enum SourceError<E> {
    /// Exact adapted CMS framing/source refusal.
    Cms(ObjectError),
    /// Exact caller-supplied separate signed-content refusal.
    Detached(E),
}
enum Object<'s, 'a, 'd, S> {
    Cms(&'s mut CmsObject<'a, 'd>),
    Detached(&'s mut S),
}
impl<S: Source> Source for Object<'_, '_, '_, S> {
    type Error = SourceError<S::Error>;
    fn stream<W: Work>(&mut self, out: &mut impl Receiver, work: &W) -> Result<(), Self::Error> {
        match self {
            Self::Cms(source) => source.stream(out, work).map_err(SourceError::Cms),
            Self::Detached(source) => source.stream(out, work).map_err(SourceError::Detached),
        }
    }
}
/// Selection preserves caller failures separately from adapter processing refusal.
#[derive(Debug)]
pub enum SelectionError<E> {
    /// Actual caller discovery failure.
    Caller(E),
    /// Current work, slot admission or source mismatch.
    Processing(Error),
}
pub(super) struct Selected<'path, 'd, A> {
    pub(super) source: &'path A,
    pub(super) start: usize,
    pub(super) items: RefCell<OwnedVec<Option<Authority<'path, 'd>>>>,
}
impl<'path, 'd, A> Selected<'path, 'd, A> {
    pub(super) fn path<W: Work>(
        &self,
        index: usize,
        work: &W,
        at: u64,
    ) -> Result<&'path CheckedPath<'d>, Error> {
        work.charge_work(1)
            .map_err(|cause| Error::resource(cause, FIELD, at))?;
        let items = self.items.try_borrow().map_err(|_| Error::Foreign { at })?;
        let selected = items
            .get(index)
            .and_then(Option::as_ref)
            .ok_or(Error::Foreign { at })?;
        Ok(selected.path)
    }
}
impl<'path, 'd, A: Authorities<'d>> ers::Authorities<'d> for Selected<'path, 'd, A>
where
    'd: 'path,
{
    type Error = SelectionError<A::Error>;
    fn select<'a, W: Work>(
        &'a self,
        index: usize,
        stamp: &'a ArchiveTimeStamp<'d>,
        work: &W,
    ) -> Result<ers::Selection<'a>, Self::Error>
    where
        'd: 'a,
    {
        let at = stamp.original().element().span().start;
        work.charge_work(1)
            .map_err(|cause| SelectionError::Processing(Error::resource(cause, FIELD, at)))?;
        // Discovery is driven by the common verifier AFTER membership and
        // preceding-sequence preflight. Never call it in a preparatory loop.
        let source: &'path A = self.source;
        let structured_cms::evidence::TimeStamp::Rfc3161(token) = stamp.timestamp() else {
            return Err(SelectionError::Processing(Error::Foreign { at }));
        };
        let selected: Authority<'path, 'd> = source
            .select(self.start + index, token, work)
            .map_err(SelectionError::Caller)?;
        let target = selected
            .path
            .members()
            .last()
            .ok_or(SelectionError::Processing(Error::Foreign { at }))?;
        let result = ers::Selection {
            certificate: target.certificate(),
            certificate_parent: selected.certificate_parent,
            key: target.key(),
        };
        let mut items = self
            .items
            .try_borrow_mut()
            .map_err(|_| SelectionError::Processing(Error::Foreign { at }))?;
        let slot = items
            .get_mut(index)
            .ok_or(SelectionError::Processing(Error::Foreign { at }))?;
        if slot.is_some() {
            return Err(SelectionError::Processing(Error::Foreign { at }));
        }
        *slot = Some(selected);
        Ok(result)
    }
}

/// Complete source-bound report, including every failed/uncertain participant.
/// Mathematical TSA facts are reused for actors without a second signature
/// verification. Modern baseline promotion still requires its independent
/// complete status/material/coverage/chronology closure.
pub struct Checked<'prepared, 'm, 'd, 'path> {
    pub(super) prepared: &'prepared Preparation<'m, 'd>,
    pub(super) required: Required<'d>,
    pub(super) proof: usize,
    pub(super) covered: Option<OwnedVec<structured_asn1::Span>>,
    pub(super) timestamps: core::ops::Range<usize>,
    pub(super) checks: OwnedVec<timestamp::Checked<'m, 'd, 'path>>,
    pub(super) ordering: OwnedVec<ers::OrderFinding>,
    pub(super) complete: bool,
}
/// Private-constructor source/recipe/history receipt; public facts cannot make it.
pub struct Verified<'check, 'prepared, 'm, 'd, 'path>(&'check Checked<'prepared, 'm, 'd, 'path>);
impl<'prepared, 'm, 'd, 'path> Checked<'prepared, 'm, 'd, 'path> {
    pub(super) fn bind_coverage<W: Work>(
        &mut self,
        spans: Option<OwnedVec<structured_asn1::Span>>,
        work: &W,
    ) -> Result<(), Error> {
        self.covered = spans
            .map(|spans| {
                super::coverage::normalize_owned(spans, work, self.required.property.span().start)
            })
            .transpose()?;
        Ok(())
    }
    /// Every actual TSA result, in original record order.
    pub fn checks(
        &self,
    ) -> impl DoubleEndedIterator<Item = &timestamp::Checked<'m, 'd, 'path>> + ExactSizeIterator
    {
        self.checks.iter().rev()
    }
    /// Actual failed adjacent chronology comparisons.
    #[must_use]
    pub fn ordering(&self) -> &[ers::OrderFinding] {
        &self.ordering
    }
    /// Present only after the COMPLETE record, input, profiles and history hold.
    #[must_use]
    pub fn verified(&self) -> Option<Verified<'_, 'prepared, 'm, 'd, 'path>> {
        if self.complete {
            Some(Verified(self))
        } else {
            None
        }
    }
}
impl<'prepared, 'm, 'd, 'path> Verified<'_, 'prepared, 'm, 'd, 'path> {
    pub(super) fn report(&self) -> &Checked<'prepared, 'm, 'd, 'path> {
        self.0
    }
    /// Exact original `CAdES` attribute/value/placement, not a `genTime` selector.
    #[must_use]
    pub fn required(&self) -> Required<'d> {
        self.0.required
    }
    /// Same complete source preparation and its timestamp/material catalogue.
    #[must_use]
    pub fn prepared(&self) -> &'prepared Preparation<'m, 'd> {
        self.0.prepared
    }
    /// Original global TSA ordinals covered by this complete receipt.
    #[must_use]
    pub fn timestamps(&self) -> core::ops::Range<usize> {
        self.0.timestamps.clone()
    }
}

/// Source/profile/common failures stay distinct; no successful partial record.
#[derive(Debug)]
pub enum CheckError<SE, PE, RE, AE, HE> {
    /// Wrong placement/source/recipe or current processing refusal.
    Selection(Error),
    /// Actual previous/current source binding refusal.
    Snapshot(super::SnapshotError),
    /// Original schema/algorithm/source-mapped information preflight failure.
    Plan(ers::PlanError),
    /// Original caller path selection failure.
    Authority(AE),
    /// Unmodified whole common record failure, including `CAdES` ESS restrictions.
    Record(
        ers::VerificationError<
            SourceError<SE>,
            PE,
            RE,
            SelectionError<AE>,
            HE,
            ProfileError<PE, Error>,
        >,
    ),
}

/// Complete source-bound record result with independent original failure classes.
pub type CheckResult<'a, 'm, 'd, 'path, SE, PE, RE, AE, HE> =
    Result<Checked<'a, 'm, 'd, 'path>, CheckError<SE, PE, RE, AE, HE>>;

impl<'a, 'm, 'd> View<'a, 'm, 'd> {
    /// Verifies RFC4998 Appendix A's INTERNAL original whole-CMS recipe.
    /// Whole record/history/path and every actual contained TSA are checked.
    #[expect(
        clippy::too_many_arguments,
        reason = "Original CMS, current execution, selected provider/path and independent trust callbacks are separate agreed boundaries."
    )]
    #[expect(
        clippy::result_large_err,
        reason = "Source maps and opaque provider/history causes remain inline even on allocation refusal."
    )]
    pub fn check_internal<'path, P, PE, A, RE, HE>(
        &self,
        envelope: Element<'d>,
        provider: &P,
        operation: &Reader<'d>,
        authorities: &'path A,
        utc: &LeapTable<'d>,
        reference: Instant,
        recognizes: impl FnMut(usize, &TokenExtension<'_>, &Execution<'_>) -> Result<bool, RE>,
        historical: impl FnMut(ers::Historical<'_, '_>, &Execution<'_>) -> Result<ers::Decision, HE>,
    ) -> CheckResult<'a, 'm, 'd, 'path, Infallible, PE, RE, A::Error, HE>
    where
        P: for<'scope> SignatureProvider<'scope, Error = PE>,
        A: Authorities<'d>,
        'd: 'path,
    {
        if self.required().kind != Kind::EvidenceRecordInternal {
            return Err(CheckError::Selection(Error::Foreign {
                at: self.required().property.span().start,
            }));
        }
        let mut source = self
            .cms_object(envelope, operation)
            .map_err(CheckError::Selection)?;
        let covered = source
            .coverage(self, operation)
            .map_err(CheckError::Selection)?;
        self.check_objects(
            covered,
            provider,
            operation,
            authorities,
            utc,
            reference,
            &mut [Object::<timestamp::NoDetached>::Cms(&mut source)],
            Membership::Object,
            recognizes,
            historical,
        )
    }
    /// Same complete INTERNAL record with a genuine earlier CMS source.
    /// The snapshot must preserve all selected fields and prior attributes;
    /// lost historical BER coding is never reconstructed from a claimed date.
    #[expect(
        clippy::too_many_arguments,
        reason = "The actual previous snapshot supplements the independent source, provider/path and history inputs."
    )]
    #[expect(
        clippy::result_large_err,
        reason = "Snapshot and source-mapped verification failures preserve their causes without error-path allocation."
    )]
    pub fn check_internal_snapshot<'path, P, PE, A, RE, HE>(
        &self,
        envelope: Element<'d>,
        snapshot: &super::CmsSnapshot<'_, 'd>,
        provider: &P,
        operation: &Reader<'d>,
        authorities: &'path A,
        utc: &LeapTable<'d>,
        reference: Instant,
        recognizes: impl FnMut(usize, &TokenExtension<'_>, &Execution<'_>) -> Result<bool, RE>,
        historical: impl FnMut(ers::Historical<'_, '_>, &Execution<'_>) -> Result<ers::Decision, HE>,
    ) -> CheckResult<'a, 'm, 'd, 'path, Infallible, PE, RE, A::Error, HE>
    where
        P: for<'scope> SignatureProvider<'scope, Error = PE>,
        A: Authorities<'d>,
        'd: 'path,
    {
        if self.required().kind != Kind::EvidenceRecordInternal {
            return Err(CheckError::Selection(Error::Foreign {
                at: self.required().property.span().start,
            }));
        }
        let mut source = self
            .cms_object_snapshot(envelope, snapshot, operation)
            .map_err(CheckError::Snapshot)?;
        let covered = source
            .coverage(self, operation)
            .map_err(CheckError::Selection)?;
        self.check_objects(
            covered,
            provider,
            operation,
            authorities,
            utc,
            reference,
            &mut [Object::<timestamp::NoDetached>::Cms(&mut source)],
            Membership::Object,
            recognizes,
            historical,
        )
    }
    /// Verifies EXTERNAL whole CMS plus the ACTUAL separately supplied signed
    /// content as one exact document group. No whole-content buffering occurs.
    #[expect(
        clippy::too_many_arguments,
        reason = "The caller's detached source cannot be conflated with original CMS provenance or trust/provider callbacks."
    )]
    #[expect(
        clippy::result_large_err,
        reason = "Detached acquisition and original mapped/provider failures must survive resource refusal without boxing."
    )]
    pub fn check_external<'path, P, PE, A, S, RE, HE>(
        &self,
        envelope: Element<'d>,
        detached: &mut S,
        provider: &P,
        operation: &Reader<'d>,
        authorities: &'path A,
        utc: &LeapTable<'d>,
        reference: Instant,
        recognizes: impl FnMut(usize, &TokenExtension<'_>, &Execution<'_>) -> Result<bool, RE>,
        historical: impl FnMut(ers::Historical<'_, '_>, &Execution<'_>) -> Result<ers::Decision, HE>,
    ) -> CheckResult<'a, 'm, 'd, 'path, S::Error, PE, RE, A::Error, HE>
    where
        P: for<'scope> SignatureProvider<'scope, Error = PE>,
        A: Authorities<'d>,
        S: Source,
        'd: 'path,
    {
        if self.required().kind != Kind::EvidenceRecordExternal {
            return Err(CheckError::Selection(Error::Foreign {
                at: self.required().property.span().start,
            }));
        }
        let mut source = self
            .cms_object(envelope, operation)
            .map_err(CheckError::Selection)?;
        let covered = source
            .coverage(self, operation)
            .map_err(CheckError::Selection)?;
        self.check_objects(
            covered,
            provider,
            operation,
            authorities,
            utc,
            reference,
            &mut [Object::Cms(&mut source), Object::Detached(detached)],
            Membership::Group,
            recognizes,
            historical,
        )
    }

    /// EXTERNAL group using the genuine previous CMS encoding plus actual
    /// separate content. Snapshot binding runs before source/provider work.
    #[expect(
        clippy::too_many_arguments,
        reason = "Genuine previous CMS and detached content are distinct original sources alongside current verification inputs."
    )]
    #[expect(
        clippy::result_large_err,
        reason = "Original two-source and snapshot failures remain inline with the complete common verification cause."
    )]
    pub fn check_external_snapshot<'path, P, PE, A, S, RE, HE>(
        &self,
        envelope: Element<'d>,
        snapshot: &super::CmsSnapshot<'_, 'd>,
        detached: &mut S,
        provider: &P,
        operation: &Reader<'d>,
        authorities: &'path A,
        utc: &LeapTable<'d>,
        reference: Instant,
        recognizes: impl FnMut(usize, &TokenExtension<'_>, &Execution<'_>) -> Result<bool, RE>,
        historical: impl FnMut(ers::Historical<'_, '_>, &Execution<'_>) -> Result<ers::Decision, HE>,
    ) -> CheckResult<'a, 'm, 'd, 'path, S::Error, PE, RE, A::Error, HE>
    where
        P: for<'scope> SignatureProvider<'scope, Error = PE>,
        A: Authorities<'d>,
        S: Source,
        'd: 'path,
    {
        if self.required().kind != Kind::EvidenceRecordExternal {
            return Err(CheckError::Selection(Error::Foreign {
                at: self.required().property.span().start,
            }));
        }
        let mut source = self
            .cms_object_snapshot(envelope, snapshot, operation)
            .map_err(CheckError::Snapshot)?;
        let covered = source
            .coverage(self, operation)
            .map_err(CheckError::Selection)?;
        self.check_objects(
            covered,
            provider,
            operation,
            authorities,
            utc,
            reference,
            &mut [Object::Cms(&mut source), Object::Detached(detached)],
            Membership::Group,
            recognizes,
            historical,
        )
    }

    #[expect(
        clippy::too_many_arguments,
        reason = "This single kernel shares actual object/group recipes, provider/path checks and independent history callbacks."
    )]
    #[expect(
        clippy::result_large_err,
        reason = "Common ERS failures retain source mapping and opaque callback causes without a new allocation."
    )]
    fn check_objects<'path, P, PE, A, S, RE, HE>(
        &self,
        covered: OwnedVec<structured_asn1::Span>,
        provider: &P,
        operation: &Reader<'d>,
        authorities: &'path A,
        utc: &LeapTable<'d>,
        reference: Instant,
        objects: &mut [Object<'_, 'a, 'd, S>],
        membership: Membership,
        mut recognizes: impl FnMut(usize, &TokenExtension<'_>, &Execution<'_>) -> Result<bool, RE>,
        mut historical: impl FnMut(ers::Historical<'_, '_>, &Execution<'_>) -> Result<ers::Decision, HE>,
    ) -> CheckResult<'a, 'm, 'd, 'path, S::Error, PE, RE, A::Error, HE>
    where
        P: for<'scope> SignatureProvider<'scope, Error = PE>,
        A: Authorities<'d>,
        S: Source,
        'd: 'path,
    {
        let required = self.required();
        let record = self.record().ok_or_else(|| {
            CheckError::Selection(Error::Foreign {
                at: required.property.span().start,
            })
        })?;
        let plan = record.input_plan(operation).map_err(CheckError::Plan)?;
        let work = operation.execution();
        let resource = |cause| {
            CheckError::Selection(Error::resource(
                cause,
                FIELD,
                required.property.span().start,
            ))
        };
        let mut selected = work.vector();
        let mut restrictions = work.vector();
        selected
            .try_reserve_exact(record.timestamps().len())
            .map_err(resource)?;
        restrictions
            .try_reserve_exact(record.timestamps().len())
            .map_err(resource)?;
        for _ in record.timestamps() {
            work.charge_work(1).map_err(resource)?;
            selected.try_push(None).map_err(resource)?;
            restrictions
                .try_push(Cell::new(PathRestriction::Unproven))
                .map_err(resource)?;
        }
        let selected = Selected {
            source: authorities,
            start: self.entry.timestamps.start,
            items: RefCell::new(selected.into_owned()),
        };
        let report = plan
            .verify_with_profile(
                provider,
                operation,
                &selected,
                utc,
                reference,
                objects,
                membership,
                |index, extension, execution| {
                    recognizes(self.entry.timestamps.start + index, extension, execution)
                },
                |mut facts, execution| {
                    facts.index += self.entry.timestamps.start;
                    historical(facts, execution)
                },
                |index, input| {
                    let path = selected
                        .path(
                            index,
                            &input.parent.execution(),
                            required.property.span().start,
                        )
                        .map_err(|cause| structured_cms::timestamp::ProfileFailure {
                            source: structured_cms::timestamp::ProfileSource::Envelope,
                            cause: ProfileError::Input(cause),
                        })?;
                    let restriction = timestamp::Context::profile_rules(&input, path, provider)?;
                    restrictions[index].set(restriction);
                    Ok(())
                },
            )
            .map_err(CheckError::Record)?;
        let mut checked = self
            .finish_record(report, &selected, &restrictions, operation)
            .map_err(CheckError::Selection)?;
        checked
            .bind_coverage(Some(covered), &work)
            .map_err(CheckError::Selection)?;
        Ok(checked)
    }
    pub(super) fn finish_record<'path, A, W: Work>(
        &self,
        report: ers::Report<'_, 'd>,
        selected: &Selected<'path, 'd, A>,
        restrictions: &OwnedVec<Cell<PathRestriction>, W>,
        operation: &Reader<'d>,
    ) -> Result<Checked<'a, 'm, 'd, 'path>, Error>
    where
        'd: 'path,
    {
        let required = self.required();
        let work = operation.execution();
        let resource = |cause| Error::resource(cause, FIELD, required.property.span().start);
        let (_, results, ordering, mut complete) = report.into_parts();
        let mut checks = work.vector();
        checks.try_reserve_exact(results.len()).map_err(resource)?;
        for result in results {
            let (index, common, _, _) = result.into_parts();
            let path = selected.path(index, &work, required.property.span().start)?;
            let target = path
                .members()
                .last()
                .expect("nonempty selection admitted above");
            let global = self.entry.timestamps.start + index;
            let checked = timestamp::with_token(self.prepared, global, operation, |context| {
                Ok::<_, Infallible>(context.checked(
                    common,
                    restrictions[index].get(),
                    target.certificate(),
                    target.key(),
                ))
            })
            .map_err(|cause| match cause {
                VisitError::Processing(cause) => cause,
                VisitError::Unreadable { property } => Error::Foreign {
                    at: property.span().start,
                },
                VisitError::Callback(never) => match never {},
            })?;
            if checked.actor().is_none() {
                complete = false;
            }
            checks.try_push(checked).map_err(resource)?;
        }
        Ok(Checked {
            prepared: self.prepared,
            required,
            proof: self.ordinal,
            covered: None,
            timestamps: self.entry.timestamps.clone(),
            checks: checks.into_owned(),
            ordering,
            complete,
        })
    }
}