structured-ltv 0.12.0

Format-neutral long-term signature validation facts over PKIX, CMS and caller-owned sources
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
//! Original status artifacts derived from common authenticated evidence.

use super::{ArchiveError, Archived, ArtifactRef, Coverage, Index, IndexError, PathCertificates};
use crate::pki::{AuthorityClosure, CrlReading, MemberStatus, PathStatuses};
use crate::source::{Location, Query, ScopeError};
use crate::timestamp::Verified;
use structured_asn1::memory::{HashWork, MemoryError, OwnedVec, Work};
use structured_pkix::{crl, ocsp};

/// Archive-material census for every path actually reached through the selected
/// actor/status-authority inputs. This proves artifact availability only; it
/// preserves every original status/time/authority fact without accepting it.
pub struct AuthorityMaterial<'closure, 'view, 'certificate, 'document, N, D, G, P> {
    inputs: &'closure AuthorityClosure<'view, 'certificate, 'document>,
    paths: OwnedVec<PathMaterial<'view, 'certificate, 'document, N, D, G, P>>,
    all_covered: bool,
}

impl<'closure, 'view, 'certificate, 'document, N, D, G, P>
    AuthorityMaterial<'closure, 'view, 'certificate, 'document, N, D, G, P>
{
    /// Complete original root/authority input closure, not a selected subset.
    #[must_use]
    pub fn inputs(&self) -> &'closure AuthorityClosure<'view, 'certificate, 'document> {
        self.inputs
    }
    /// Material for every reached path, preserving the same closure order.
    #[must_use]
    pub fn paths(&self) -> &[PathMaterial<'view, 'certificate, 'document, N, D, G, P>] {
        &self.paths
    }
    /// Whether all actually required original artifacts precede this archive.
    /// Status acceptance, all actor roots and historical trust remain separate.
    #[must_use]
    pub fn all_artifacts_covered(&self) -> bool {
        self.all_covered
    }
}

/// Census every path/status/authority artifact actually required by this input
/// closure under the SAME original archive input. Reuses `archived_path`, its
/// source index and complete/delta/format-container rules. No provider is repeated and
/// an allocation/work/source refusal drops every partial material report.
pub fn archived_authorities<'closure, 'view, 'certificate, 'document, N, D, G, P, O, W, Q, C, I>(
    inputs: &'closure AuthorityClosure<'view, 'certificate, 'document>,
    index: &Index<'_, N, D, G, P, O, W, I>,
    coverage: &Coverage<N, D, G, C, I>,
    evidence: &Verified<N, D, G, I>,
    query: &Q,
) -> Result<AuthorityMaterial<'closure, 'view, 'certificate, 'document, N, D, G, P>, ArchiveError<N>>
where
    N: Copy + Eq + HashWork,
    D: Copy + Eq,
    G: Copy + Eq,
    P: Copy,
    W: Work,
    C: Work,
    Q: Query<Id = N, Document = D, Generation = G>,
{
    let signature = evidence.signature();
    let resource = |cause| ArchiveError::Index(IndexError::Resource { signature, cause });
    let scope = |cause| ArchiveError::Index(IndexError::Scope(cause));
    query.admit(signature, 1).map_err(scope)?;
    let mut paths = query.execution().vector();
    paths
        .try_reserve_exact(inputs.nodes().len())
        .map_err(resource)?;
    let mut all_covered = true;
    for node in inputs.nodes() {
        query.admit(signature, 2).map_err(scope)?;
        let material = archived_path(node.statuses, index, coverage, evidence, query)?;
        if !material.all_artifacts_covered() {
            all_covered = false;
        }
        paths.try_push(material).map_err(resource)?;
    }
    Ok(AuthorityMaterial {
        inputs,
        paths: paths.into_owned(),
        all_covered,
    })
}

/// Complete certificate/status artifact census for the SAME actual path and
/// original evaluation inputs. This is one actor's contribution to an archive;
/// other actors, status acceptance and historical trust remain separate.
pub struct PathMaterial<'view, 'certificate, 'document, N, D, G, P> {
    inputs: &'view PathStatuses<'view, 'certificate, 'document>,
    certificates: PathCertificates<'view, 'document, N, D, G, P>,
    statuses: OwnedVec<StatusMaterial<'view, 'certificate, 'document, N, D, G, P>>,
    all_covered: bool,
}

impl<'view, 'certificate, 'document, N, D, G, P>
    PathMaterial<'view, 'certificate, 'document, N, D, G, P>
{
    /// Original complete per-member input census; no caller-written actor count.
    #[must_use]
    pub fn inputs(&self) -> &'view PathStatuses<'view, 'certificate, 'document> {
        self.inputs
    }
    /// Same actual checked-path certificate set, including its existing anchor.
    #[must_use]
    pub fn certificates(&self) -> &PathCertificates<'view, 'document, N, D, G, P> {
        &self.certificates
    }
    /// Original contributing status sources/signers for members requiring a
    /// status lookup. The original input census separately retains a validated
    /// responder noCheck exemption; it never manufactures an OCSP Good result.
    #[must_use]
    pub fn statuses(&self) -> &[StatusMaterial<'view, 'certificate, 'document, N, D, G, P>] {
        &self.statuses
    }
    /// Whether all these exact inputs have covered original copies. This is
    /// neither a Good assertion nor completeness for counters/credentials/TSAs.
    #[must_use]
    pub fn all_artifacts_covered(&self) -> bool {
        self.all_covered
    }
}

/// Derive archive obligations from a complete original path-status census.
/// Reuses the same exact-source index and archive coverage for certificates and
/// all complete/delta/OCSP contributors. No signature/hash check is repeated,
/// and refusal drops the accumulated reports rather than returning a subset.
pub fn archived_path<'view, 'certificate, 'document, N, D, G, P, O, W, Q, C, I>(
    inputs: &'view PathStatuses<'view, 'certificate, 'document>,
    index: &Index<'_, N, D, G, P, O, W, I>,
    coverage: &Coverage<N, D, G, C, I>,
    evidence: &Verified<N, D, G, I>,
    query: &Q,
) -> Result<PathMaterial<'view, 'certificate, 'document, N, D, G, P>, ArchiveError<N>>
where
    N: Copy + Eq + HashWork,
    D: Copy + Eq,
    G: Copy + Eq,
    P: Copy,
    W: Work,
    C: Work,
    Q: Query<Id = N, Document = D, Generation = G>,
{
    let certificates = index.archived_path(inputs.path(), coverage, evidence, query)?;
    let mut all_covered = certificates.all_certificates_covered();
    let signature = evidence.signature();
    let resource = |cause| ArchiveError::Index(IndexError::Resource { signature, cause });
    let scope = |cause| ArchiveError::Index(IndexError::Scope(cause));
    query.admit(signature, 1).map_err(scope)?;
    let mut statuses = query.execution().vector();
    let mut count = 0;
    for input in inputs.entries() {
        query.admit(signature, 2).map_err(scope)?;
        if !matches!(input, MemberStatus::ResponderNoCheck(_)) {
            count += 1;
        }
    }
    statuses.try_reserve_exact(count).map_err(resource)?;
    for input in inputs.entries() {
        query.admit(signature, 2).map_err(scope)?;
        let Some(source) = member_source(input) else {
            continue;
        };
        let status = archived(source, index, coverage, evidence, query)?;
        if !status.all_artifacts_covered() {
            all_covered = false;
        }
        statuses.try_push(status).map_err(resource)?;
    }
    Ok(PathMaterial {
        inputs,
        certificates,
        statuses: statuses.into_owned(),
        all_covered,
    })
}

/// Actual common status evidence, not a caller-written Good assertion.
#[derive(Clone, Copy)]
pub enum StatusSource<'view, 'certificate, 'document> {
    /// Qualified reason union. Each contributing complete/delta source stays
    /// visible; partial reasons alone still do not establish all-reason absence.
    Crl(&'view crl::Coverage<'certificate, 'document>),
    /// Original complete/delta facts whose entry/window may be negative. This
    /// retains actual sources without constructing an absence assertion.
    CrlReading(CrlReading<'view, 'certificate, 'document>),
    /// Common issuer/request/time result. Its original status may be Good,
    /// Unknown or Revoked; incorporation does not change that status.
    Ocsp(&'view ocsp::BoundEvidence<'certificate, 'document, 'certificate, 'document>),
}

/// The status source of one member's status input; `None` for a validated
/// responder noCheck exemption, whose responder certificate stays in the path.
/// One mapping serves archive material and every format's placement checks.
#[must_use]
pub fn member_source<'view, 'certificate, 'document>(
    input: &'view MemberStatus<'view, 'certificate, 'document>,
) -> Option<StatusSource<'view, 'certificate, 'document>> {
    match input {
        MemberStatus::Crl(bound) => Some(StatusSource::Crl(bound.coverage())),
        MemberStatus::CrlReading(bound) => Some(StatusSource::CrlReading(bound.reading())),
        MemberStatus::Ocsp(bound) => Some(StatusSource::Ocsp(bound.evidence())),
        MemberStatus::ResponderNoCheck(_) => None,
    }
}

impl<'certificate, 'document> StatusSource<'_, 'certificate, 'document> {
    /// Actual original target processed by this same common status result.
    #[must_use]
    pub fn target(self) -> &'certificate structured_pkix::Certificate<'document> {
        match self {
            Self::Crl(qualified) => qualified.target(),
            Self::CrlReading(reading) => reading.target(),
            Self::Ocsp(qualified) => qualified.target(),
        }
    }
    /// Exact label used by the original common status processing.
    #[must_use]
    pub fn evaluated_at(self) -> structured_pkix::temporal::Reading {
        match self {
            Self::Crl(qualified) => qualified.evaluated_at(),
            Self::CrlReading(reading) => reading.evaluated_at(),
            Self::Ocsp(qualified) => qualified.evaluated_at(),
        }
    }
}

/// Role of one original artifact in common status evidence.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusPart {
    /// Authenticated whole complete CRL, also required for a delta source.
    CompleteCrl,
    /// Authenticated whole updating delta CRL; never a replacement for the base.
    DeltaCrl,
    /// Format-issued original OCSP container carrying the exact basic response.
    /// RFC5940 requires its whole envelope; legacy `CAdES` Annex A.1.4 specifies
    /// a bare `BasicOCSPResponse`. Neither permits an invented envelope.
    OcspResponse,
    /// Actual CRL/OCSP signer certificate from the same authentication receipt.
    SigningCertificate,
}

/// One required source artifact; ordinals address the retained evidence below.
#[derive(Debug)]
pub struct ArchivedStatus<N, D, G, P> {
    /// Contributing CRL reason-source ordinal, or zero for one OCSP response.
    pub source: usize,
    /// Exact artifact role, not inferred from how many values were carried.
    pub part: StatusPart,
    /// Mathematical availability under this particular archive input.
    pub availability: Archived<N, D, G, P>,
}

/// Complete artifact census for exactly one borrowed common status result.
/// Status usability, actor/path association and additional delegated-responder
/// checking remain explicit; this is not an attained baseline-level receipt.
pub struct StatusMaterial<'view, 'certificate, 'document, N, D, G, P> {
    source: StatusSource<'view, 'certificate, 'document>,
    archive: Location<N, D, G>,
    entries: OwnedVec<ArchivedStatus<N, D, G, P>>,
    all_covered: bool,
}

impl<'view, 'certificate, 'document, N: Copy, D: Copy, G: Copy, P>
    StatusMaterial<'view, 'certificate, 'document, N, D, G, P>
{
    /// Same original common result; another target/status cannot replace it.
    #[must_use]
    pub fn source(&self) -> StatusSource<'view, 'certificate, 'document> {
        self.source
    }
    /// Original archive token whose mathematical input was checked.
    #[must_use]
    pub fn archive(&self) -> Location<N, D, G> {
        self.archive
    }
    /// Every original status source and actual signer, including missing copies.
    #[must_use]
    pub fn entries(&self) -> &[ArchivedStatus<N, D, G, P>] {
        &self.entries
    }
    /// Whether all these exact artifacts have covered copies. This does not
    /// mean every path member has status, nor that an OCSP status is Good.
    #[must_use]
    pub fn all_artifacts_covered(&self) -> bool {
        self.all_covered
    }
}

/// Checks original status artifacts without reconstructing a CRL or OCSP value.
///
/// EN319132-1 V1.3.1 §§5.4.3/5.5.2.2/6.3(aa) require the full set used for
/// validation before each archive. For a delta this includes both whole lists;
/// RFC5940 §3 requires the whole `OCSPResponse` envelope in that format;
/// historical `CAdES` Annex A.1.4 carries the original bare basic response.
/// The format-issued index retains that boundary. Actual source/key
/// authentication, applicability and temporal facts come from the shared PKIX
/// result, never from a count or a fresh private status parser here.
/// <https://www.etsi.org/deliver/etsi_en/319100_319199/31913201/01.03.01_60/en_31913201v010301p.pdf>
/// <https://www.rfc-editor.org/rfc/rfc5940#section-3>
pub fn archived<'view, 'certificate, 'document, N, D, G, P, O, W, Q, C, I>(
    source: StatusSource<'view, 'certificate, 'document>,
    index: &Index<'_, N, D, G, P, O, W, I>,
    coverage: &Coverage<N, D, G, C, I>,
    evidence: &Verified<N, D, G, I>,
    query: &Q,
) -> Result<StatusMaterial<'view, 'certificate, 'document, N, D, G, P>, ArchiveError<N>>
where
    N: Copy + Eq + HashWork,
    D: Copy + Eq,
    G: Copy + Eq,
    P: Copy,
    W: Work,
    C: Work,
    Q: Query<Id = N, Document = D, Generation = G>,
{
    let signature = evidence.signature();
    query
        .admit(signature, 1)
        .map_err(|cause| ArchiveError::Index(IndexError::Scope(cause)))?;
    let mut entries = query.execution().vector();
    let mut all_covered = true;
    let mut record = |artifact, ordinal, part| {
        let availability = index.archived(artifact, coverage, evidence, query)?;
        if !matches!(availability, Archived::Present(_)) {
            all_covered = false;
        }
        entries
            .try_push(ArchivedStatus {
                source: ordinal,
                part,
                availability,
            })
            .map_err(|cause| ArchiveError::Index(IndexError::Resource { signature, cause }))
    };
    visit_artifacts(source, signature, query, &mut record).map_err(|cause| match cause {
        ArtifactVisitError::Scope(cause) => ArchiveError::Index(IndexError::Scope(cause)),
        ArtifactVisitError::Visitor(cause) => cause,
    })?;
    Ok(StatusMaterial {
        source,
        archive: evidence.token(),
        entries: entries.into_owned(),
        all_covered,
    })
}

fn record_crl<'certificate, 'document, E>(
    source: &crl::EvidenceSource<'certificate, 'document>,
    ordinal: usize,
    mut record: impl FnMut(ArtifactRef<'certificate, 'document>, usize, StatusPart) -> Result<(), E>,
) -> Result<(), E> {
    match source {
        crl::EvidenceSource::Complete(complete) => {
            record(
                ArtifactRef::Crl(complete.crl()),
                ordinal,
                StatusPart::CompleteCrl,
            )?;
            record(
                ArtifactRef::Certificate(complete.issuer()),
                ordinal,
                StatusPart::SigningCertificate,
            )?;
        }
        crl::EvidenceSource::Delta { base, delta } => {
            record(
                ArtifactRef::Crl(base.crl()),
                ordinal,
                StatusPart::CompleteCrl,
            )?;
            record(ArtifactRef::Crl(delta.crl()), ordinal, StatusPart::DeltaCrl)?;
            record(
                ArtifactRef::Certificate(base.issuer()),
                ordinal,
                StatusPart::SigningCertificate,
            )?;
            record(
                ArtifactRef::Certificate(delta.issuer()),
                ordinal,
                StatusPart::SigningCertificate,
            )?;
        }
    }
    Ok(())
}

/// A source refusal while enumerating status artifacts, or the visitor's own.
#[derive(Debug)]
pub enum ArtifactVisitError<V, N> {
    /// Source generation or current operation refused.
    Scope(ScopeError<N>),
    /// The visitor's unchanged cause.
    Visitor(V),
}

/// Enumerates every original complete/delta/OCSP source and actual
/// authentication signer of one status result, each step admitted under the
/// current query. Archive availability and every format's placement check
/// share this one walk; a visitor failure keeps its cause.
pub fn visit_artifacts<'certificate, 'document, Q: Query, V>(
    source: StatusSource<'_, 'certificate, 'document>,
    signature: Q::Id,
    query: &Q,
    mut record: impl FnMut(ArtifactRef<'certificate, 'document>, usize, StatusPart) -> Result<(), V>,
) -> Result<(), ArtifactVisitError<V, Q::Id>> {
    let pay = || query.admit(signature, 2).map_err(ArtifactVisitError::Scope);
    let record = |artifact, ordinal, part| {
        record(artifact, ordinal, part).map_err(ArtifactVisitError::Visitor)
    };
    walk_artifacts(source, pay, record)
}

/// The same walk when the enclosing operation has already checked the source
/// and needs only current admission.
pub fn visit_artifacts_in<'c, 'd, W: Work>(
    source: StatusSource<'_, 'c, 'd>,
    work: &W,
    record: impl FnMut(ArtifactRef<'c, 'd>, usize, StatusPart) -> Result<(), MemoryError>,
) -> Result<(), MemoryError> {
    walk_artifacts(source, || work.charge_work(1), record)
}

fn walk_artifacts<'c, 'd, E>(
    source: StatusSource<'_, 'c, 'd>,
    mut pay: impl FnMut() -> Result<(), E>,
    mut record: impl FnMut(ArtifactRef<'c, 'd>, usize, StatusPart) -> Result<(), E>,
) -> Result<(), E> {
    match source {
        StatusSource::Crl(qualified) => {
            for (ordinal, entry) in qualified.sources().iter().enumerate() {
                pay()?;
                record_crl(&entry.source(), ordinal, &mut record)?;
            }
        }
        StatusSource::CrlReading(reading) => {
            pay()?;
            record_crl(&reading.source(), 0, &mut record)?;
        }
        StatusSource::Ocsp(qualified) => {
            pay()?;
            let signed = qualified.evidence().source().signed();
            record(
                ArtifactRef::Ocsp(signed.response()),
                0,
                StatusPart::OcspResponse,
            )?;
            record(
                ArtifactRef::Certificate(signed.signer()),
                0,
                StatusPart::SigningCertificate,
            )?;
        }
    }
    Ok(())
}