structured-pkix 0.1.2

Borrowed X.509 certificates and provider-neutral certification path validation
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
//! Static certificate-path processing shared by construction and supplied paths.
//! RFC5280 ยง6.1 and RFC9618 govern the common core. Revocation, application
//! purpose, algorithm permissions and trust-anchor selection remain external.

use super::{
    Anchor, BuildError, BuildResult, Interval, Meter, Policy, Truncation, Verified, Verify, Watch,
    WorkingKey, cost, sorting_rounds,
};
use crate::constraints::BasicConstraints;
use crate::extension::{Extension, Recognizer};
use crate::{Certificate, ErrorCode, Name, oid};
use structured_asn1::Oid;
use structured_asn1::memory::{Memory, MemoryError, OwnedVec};

mod processing;

/// The source of a path fact; certificate positions are leaf-first.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConstraintSource {
    /// The explicitly supplied trust anchor.
    Anchor,
    /// A certificate at this position in the supplied path.
    Certificate(usize),
}

/// How a presented name fails an accumulated name constraint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NameConstraintFailure {
    /// An authority explicitly excluded the name.
    Excluded,
    /// No permitted subtree of the relevant authority contains the name.
    NotPermitted,
    /// A relevant constraint cannot be enforced by this implementation/provider.
    Unenforceable,
}

/// A completed semantic rejection, distinct from inability to execute a check.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RejectionReason<'a> {
    /// A certification path must contain at least one certificate.
    EmptyPath,
    /// The same encoded certificate already occurs at the named position.
    DuplicateCertificate {
        /// The earlier leaf-first position.
        earlier: usize,
    },
    /// The trust anchor's own certificate is not part of a prospective path.
    AnchorIncluded,
    /// The certificate does not cover the requested validation interval.
    Validity,
    /// The certificate's issuer name differs from the expected issuer.
    Issuer,
    /// An intermediate does not assert certificate-authority status.
    NotAuthority,
    /// An intermediate's `KeyUsage` forbids signing certificates.
    KeyCertSign,
    /// An asserted authority has an empty subject name.
    EmptyAuthorityName,
    /// The path exceeds an authority's pathLenConstraint.
    PathLength {
        /// The authority whose retained limit was exhausted.
        source: ConstraintSource,
        /// The original pathLenConstraint, before counter decrements.
        limit: u32,
    },
    /// The provider completed the signature check with a negative answer.
    Signature,
    /// The provider cannot verify this algorithm/key/parameter combination.
    UnsupportedSignature,
    /// A critical extension is not understood.
    CriticalExtension(Oid<'a>),
    /// A relevant extension could not be decoded semantically.
    MalformedExtension {
        /// The extension's type identifier.
        extension: Oid<'a>,
        /// The original decoding code.
        code: ErrorCode,
        /// The original schema field.
        field: &'static str,
        /// The original document offset.
        offset: u64,
    },
    /// A name constraint is carried by a certificate that is not a CA.
    NameConstraintsOnNonAuthority,
    /// A name violates a constraint originating above this certificate.
    NameConstraints {
        /// The authority or anchor that supplied the decisive constraint.
        source: ConstraintSource,
        /// The completed constraint result.
        failure: NameConstraintFailure,
    },
    /// The path does not satisfy the requested/required certificate policies.
    Policies,
}

/// A semantic failure at its source; `None` denotes the whole supplied input.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Rejection<'a> {
    /// Leaf-first certificate position, anchor, or whole-input failure.
    pub at: Option<ConstraintSource>,
    /// The completed rejection reason.
    pub reason: RejectionReason<'a>,
}

/// Accepted static path facts, not a complete application trust verdict.
/// The policy container owns and accounts its allocation; its OID values and
/// the effective key still borrow the original certificate/anchor input.
#[derive(Debug)]
pub struct ValidationFacts<'a> {
    /// The target's effective key, including inherited algorithm parameters.
    pub key: WorkingKey<'a>,
    /// Intermediate CAs that are not self-issued, excluding target and anchor.
    /// This scalar lets caller anchor-policy checks reuse the completed census.
    pub non_self_issued_intermediates: u32,
    policies: OwnedVec<Oid<'a>>,
}

impl<'a> ValidationFacts<'a> {
    /// Accepted authority/user policy identifiers in deterministic encoded order.
    #[must_use]
    pub fn policies(&self) -> &[Oid<'a>] {
        &self.policies
    }

    /// Transfers owned policy-set storage with its retained capacity charge.
    #[must_use]
    pub fn into_policies(self) -> OwnedVec<Oid<'a>> {
        self.policies
    }
}

/// The completed outcome of checking exactly one supplied path.
#[derive(Debug)]
pub enum Validation<'a> {
    /// Static certificate-path checks completed successfully.
    Accepted(ValidationFacts<'a>),
    /// Static checks completed with a semantic rejection.
    Rejected(Rejection<'a>),
    /// A local processing limit stopped the operation; no accepted facts exist.
    Incomplete(Truncation),
}

/// Local limits for checking a supplied path, with no alternative-path search.
#[derive(Debug, Clone, Copy)]
pub struct ValidationBounds {
    /// Maximum number of supplied certificates, excluding the anchor.
    pub depth: usize,
    /// Maximum cumulative work units for this check.
    pub work: u64,
    /// Retained policy graph nodes, parent edges and expected OIDs together.
    pub policies: usize,
}

impl Default for ValidationBounds {
    fn default() -> Self {
        let bounds = super::Bounds::default();
        Self {
            depth: bounds.depth,
            work: bounds.work,
            policies: bounds.policies,
        }
    }
}

/// Explicit execution inputs for static checking of a borrowed supplied path.
/// Callers supply trusted anchors, cryptographic capability and application
/// policy separately; no clock, network or trust discovery is performed here.
pub struct ValidationContext<'a, 'p, V: Verify, W: Watch, R: Recognizer> {
    /// The current operation's shared work and storage account.
    pub execution: &'p Memory,
    /// The interval every certificate must cover.
    pub interval: Interval,
    /// Certificate-policy processing inputs.
    pub policy: Policy<'a>,
    /// Local bounds that only tighten the shared execution budget.
    pub bounds: ValidationBounds,
    /// Injected signature verifier; it accounts its own internal work/storage.
    pub verifier: &'p V,
    /// Caller-provided deadline check; `()` means no clock, with work still bounded.
    pub watch: &'p W,
    /// Injected recognition of scheme-specific critical extensions/names.
    pub recognizer: &'p R,
}

pub(super) struct Settings<'a, 'p, R: Recognizer> {
    pub(super) execution: &'p Memory,
    pub(super) interval: Interval,
    pub(super) policy: Policy<'a>,
    pub(super) policies: usize,
    pub(super) recognizer: &'p R,
}

/// Cached by search, computed directly for a supplied path without cache storage.
#[derive(Debug, Clone, Copy)]
pub(super) struct CertificateFacts<'a> {
    pub(super) self_issued: bool,
    pub(super) basic: Option<BasicConstraints>,
    pub(super) issuing: Option<RejectionReason<'a>>,
    pub(super) rejected: Option<RejectionReason<'a>>,
}

pub(super) trait Driver<'a, R: Recognizer>: Meter {
    type VerificationError;
    fn verified(
        &mut self,
        certificate: &'a Certificate<'a>,
        key: WorkingKey<'a>,
    ) -> BuildResult<Option<Verified>, Self::VerificationError, R::Error>;
    fn facts(
        &mut self,
        certificate: &'a Certificate<'a>,
        settings: &Settings<'a, '_, R>,
    ) -> BuildResult<Option<CertificateFacts<'a>>, Self::VerificationError, R::Error>;
}

/// Search constructs this witness only after all selected issuer-name bindings
/// and the terminal anchor binding were checked. The supplied variant performs
/// those same Name comparisons here, without repeating search's proven work.
#[derive(Clone, Copy)]
pub(super) enum Chain<'a, 'p> {
    Supplied(&'p [&'a Certificate<'a>]),
    Constructed(&'p [&'a Certificate<'a>]),
}

enum Decoded<T> {
    Value(Option<T>),
    Rejected(RejectionReason<'static>),
    Incomplete,
}

fn decode<T>(
    meter: &mut impl Meter,
    kind: Oid<'static>,
    action: impl FnOnce() -> crate::Result<Option<T>>,
) -> crate::Result<Decoded<T>> {
    if !meter.spend(cost::EXTENSION)? {
        return Ok(Decoded::Incomplete);
    }
    Ok(match decoded(kind, action())? {
        Ok(value) => Decoded::Value(value),
        Err(reason) => Decoded::Rejected(reason),
    })
}

fn decoded<T>(
    kind: Oid<'static>,
    result: crate::Result<T>,
) -> crate::Result<core::result::Result<T, RejectionReason<'static>>> {
    match result {
        Ok(value) => Ok(Ok(value)),
        Err(error) if error.is_resource() => Err(error),
        Err(error) => Ok(Err(RejectionReason::MalformedExtension {
            extension: kind,
            code: error.code(),
            field: error.field(),
            offset: error.offset(),
        })),
    }
}

pub(super) fn spend_names(
    meter: &mut impl Meter,
    left: &Name<'_>,
    right: &Name<'_>,
) -> crate::Result<bool> {
    let parts = left.parts().len() as u64 + right.parts().len() as u64;
    if !meter.spend(parts)? {
        return Ok(false);
    }
    let bytes = left.as_bytes().len() as u64 + right.as_bytes().len() as u64;
    let rounds = sorting_rounds(left).max(sorting_rounds(right));
    let work = rounds
        .checked_mul(bytes / cost::COMPARED)
        .and_then(|work| work.checked_add(cost::NAME))
        .ok_or_else(|| left.resource(MemoryError::WorkExceeded))?;
    meter.spend(work)
}

fn critical_policy(
    item: &Extension<'_>,
    execution: &Memory,
) -> Option<(Oid<'static>, crate::Result<bool>)> {
    // Known path extensions retain decoder failures even when critical.
    let kind = item.kind();
    if kind == oid::CERTIFICATE_POLICIES {
        Some((
            oid::CERTIFICATE_POLICIES,
            // RFC5280 ยง4.2.1.4 requires interpretation of all qualifiers
            // when critical. Reuse the ordinary qualifier semantic core on
            // this decoded value rather than treating shape as understanding.
            crate::policy::CertificatePolicies::read(item, execution)
                .and_then(|policies| policies.understood(item.parser(), execution)),
        ))
    } else if kind == oid::POLICY_CONSTRAINTS {
        Some((
            oid::POLICY_CONSTRAINTS,
            crate::constraints::PolicyConstraints::read(item, execution).map(|_| true),
        ))
    } else if kind == oid::POLICY_MAPPINGS {
        Some((
            oid::POLICY_MAPPINGS,
            crate::policy::PolicyMappings::read(item, execution).map(|_| true),
        ))
    } else if kind == oid::INHIBIT_ANY_POLICY {
        Some((
            oid::INHIBIT_ANY_POLICY,
            crate::constraints::InhibitAnyPolicy::read(item, execution).map(|_| true),
        ))
    } else {
        None
    }
}

pub(super) fn certificate_facts<'a, E, R: Recognizer, M: Meter>(
    certificate: &'a Certificate<'a>,
    settings: &Settings<'a, '_, R>,
    meter: &mut M,
) -> BuildResult<Option<CertificateFacts<'a>>, E, R::Error> {
    let execution = settings.execution;
    let extensions = certificate.tbs().extensions();
    if !meter.spend(cost::CHECK)? {
        return Ok(None);
    }
    let mut facts = CertificateFacts {
        self_issued: false,
        basic: None,
        issuing: Some(RejectionReason::NotAuthority),
        rejected: None,
    };
    if !settings.interval.inside(certificate.tbs().validity()) {
        facts.rejected = Some(RejectionReason::Validity);
        return Ok(Some(facts));
    }
    if !spend_names(
        meter,
        certificate.tbs().subject(),
        certificate.tbs().issuer(),
    )? {
        return Ok(None);
    }
    facts.self_issued = certificate
        .tbs()
        .subject()
        .matches(certificate.tbs().issuer(), execution)?;
    match decode(meter, oid::BASIC_CONSTRAINTS, || {
        extensions.basic_constraints(execution)
    })? {
        Decoded::Value(basic) => facts.basic = basic,
        Decoded::Rejected(reason) => {
            facts.rejected = Some(reason);
            return Ok(Some(facts));
        }
        Decoded::Incomplete => return Ok(None),
    }
    if facts.basic.is_some_and(BasicConstraints::is_authority) {
        facts.issuing = None;
        if certificate.tbs().subject().is_empty() {
            facts.rejected = Some(RejectionReason::EmptyAuthorityName);
            return Ok(Some(facts));
        }
        match decode(meter, oid::KEY_USAGE, || extensions.key_usage(execution))? {
            Decoded::Value(Some(usage)) if !usage.key_cert_sign() => {
                facts.issuing = Some(RejectionReason::KeyCertSign);
            }
            Decoded::Rejected(reason) => facts.issuing = Some(reason),
            Decoded::Incomplete => return Ok(None),
            Decoded::Value(_) => {}
        }
    }
    for demand in extensions.criticals(execution) {
        let demand = demand?;
        if !meter.spend(cost::EXTENSION)? {
            return Ok(None);
        }
        // The common processor decodes and retains nameConstraints before
        // verification, then enforces it on subordinate names. Decoding here
        // as well would allocate the same constraint payload twice.
        if demand.kind() == oid::NAME_CONSTRAINTS || demand.kind() == oid::BASIC_CONSTRAINTS {
            continue;
        }
        if demand.kind() == oid::KEY_USAGE
            && facts.basic.is_some_and(BasicConstraints::is_authority)
        {
            if matches!(
                facts.issuing,
                Some(RejectionReason::MalformedExtension { .. })
            ) {
                facts.rejected = facts.issuing;
                return Ok(Some(facts));
            }
            continue;
        }
        // RFC5280 ยง6.1.3/ยง6.1.4/ยง6.1.5: these are known path-processing
        // extensions. A malformed value keeps its decoder details even if
        // critical; it is not an unrecognized extension. Critical mappings
        // and inhibitAnyPolicy must decode even on the final certificate,
        // where their intermediate-only state transitions do not apply.
        if let Some((kind, result)) = critical_policy(demand, execution) {
            let reason = match decoded(kind, result)? {
                Ok(true) => continue,
                Ok(false) => RejectionReason::CriticalExtension(kind),
                Err(reason) => reason,
            };
            facts.rejected = Some(reason);
            return Ok(Some(facts));
        }
        if extensions.understands(demand, settings.recognizer, execution)? {
            continue;
        }
        facts.rejected = Some(RejectionReason::CriticalExtension(demand.kind()));
        return Ok(Some(facts));
    }
    Ok(Some(facts))
}

fn rejection(at: ConstraintSource, reason: RejectionReason<'_>) -> Validation<'_> {
    Validation::Rejected(Rejection {
        at: Some(at),
        reason,
    })
}

pub(super) fn process<'a, R: Recognizer, D: Driver<'a, R>>(
    input: Chain<'a, '_>,
    anchor: Anchor<'a>,
    settings: &Settings<'a, '_, R>,
    driver: &mut D,
) -> BuildResult<Validation<'a>, D::VerificationError, R::Error> {
    processing::process(input, anchor, settings, driver)
}

struct Supplied<'a, 'p, V: Verify, W: Watch, R: Recognizer> {
    context: &'p ValidationContext<'a, 'p, V, W, R>,
    origin: &'a Certificate<'a>,
    spent: u64,
    halted: Option<Truncation>,
}

impl<V: Verify, W: Watch, R: Recognizer> Supplied<'_, '_, V, W, R> {
    fn admit(&mut self, work: u64) -> crate::Result<Option<Truncation>> {
        if work > self.context.bounds.work - self.spent {
            return Ok(Some(Truncation::Work));
        }
        if self.context.watch.out_of_time() {
            return Ok(Some(Truncation::Time));
        }
        self.context
            .execution
            .charge_work(work)
            .map_err(|reason| self.origin.tbs().subject().resource(reason))?;
        self.spent += work;
        Ok(None)
    }
}

impl<V: Verify, W: Watch, R: Recognizer> Meter for Supplied<'_, '_, V, W, R> {
    fn spend(&mut self, work: u64) -> crate::Result<bool> {
        if let Some(halted) = self.admit(work)? {
            self.halted = Some(halted);
            return Ok(false);
        }
        Ok(true)
    }
}

impl<'a, V: Verify, W: Watch, R: Recognizer> Driver<'a, R> for Supplied<'a, '_, V, W, R> {
    type VerificationError = V::Error;
    fn verified(
        &mut self,
        certificate: &'a Certificate<'a>,
        key: WorkingKey<'a>,
    ) -> BuildResult<Option<Verified>, V::Error, R::Error> {
        if !self.spend(cost::SIGNATURE)? {
            return Ok(None);
        }
        self.context
            .verifier
            .verify(
                &certificate.signature_algorithm(),
                key,
                certificate.signed_bytes(),
                certificate.signature(),
                self.context.execution,
            )
            .map(Some)
            .map_err(BuildError::Verification)
    }
    fn facts(
        &mut self,
        certificate: &'a Certificate<'a>,
        settings: &Settings<'a, '_, R>,
    ) -> BuildResult<Option<CertificateFacts<'a>>, V::Error, R::Error> {
        certificate_facts(certificate, settings, self)
    }
}

/// Checks exactly the supplied leaf-first path to one explicit anchor.
///
/// The anchor is excluded from `chain`. This returns static path facts only;
/// revocation, purpose, key strength and application permissions remain caller
/// decisions. It neither searches alternative issuers nor re-encodes signed data.
///
/// A completed rejection is a value. Processing/resource and injected callback
/// errors remain `BuildError`, while local bounds yield `Validation::Incomplete`.
///
/// ```
/// use structured_asn1::memory::Memory;
/// use structured_pkix::{Certificate, path};
///
/// fn check<'a, V: path::Verify>(
///     chain: &[&'a Certificate<'a>],
///     anchor: path::Anchor<'a>,
///     execution: &Memory,
///     interval: path::Interval,
///     verifier: &V,
/// ) -> path::BuildResult<path::Validation<'a>, V::Error, core::convert::Infallible> {
///     let context = path::ValidationContext {
///         execution,
///         interval,
///         policy: path::Policy::default(),
///         bounds: path::ValidationBounds::default(),
///         verifier,
///         watch: &(),
///         recognizer: &(),
///     };
///     path::validate(chain, anchor, &context)
/// }
/// ```
pub fn validate<'a, V: Verify, W: Watch, R: Recognizer>(
    chain: &[&'a Certificate<'a>],
    anchor: Anchor<'a>,
    context: &ValidationContext<'a, '_, V, W, R>,
) -> BuildResult<Validation<'a>, V::Error, R::Error> {
    let Some(target) = chain.first() else {
        return Ok(Validation::Rejected(Rejection {
            at: None,
            reason: RejectionReason::EmptyPath,
        }));
    };
    if chain.len() > context.bounds.depth {
        return Ok(Validation::Incomplete(Truncation::Depth));
    }
    let length = u32::try_from(chain.len()).map_err(|_| {
        target
            .tbs()
            .subject()
            .resource(MemoryError::CapacityOverflow)
    })?;
    length.checked_add(1).ok_or_else(|| {
        target
            .tbs()
            .subject()
            .resource(MemoryError::CapacityOverflow)
    })?;
    let mut driver = Supplied {
        context,
        origin: target,
        spent: 0,
        halted: None,
    };
    let mut seen = context.execution.map();
    for (index, certificate) in chain.iter().enumerate() {
        if let Some(halted) =
            driver.admit(cost::ENTRY + certificate.as_bytes().len() as u64 / cost::HASHED)?
        {
            return Ok(Validation::Incomplete(halted));
        }
        if let Some(earlier) = seen
            .try_insert(certificate.as_bytes(), index)
            .map_err(|reason| certificate.tbs().subject().resource(reason))?
        {
            return Ok(rejection(
                ConstraintSource::Certificate(index),
                RejectionReason::DuplicateCertificate { earlier },
            ));
        }
        if let Some(published) = anchor.certificate() {
            let bytes = certificate.as_bytes().len().min(published.as_bytes().len()) as u64;
            if let Some(halted) = driver.admit(bytes / cost::COMPARED)? {
                return Ok(Validation::Incomplete(halted));
            }
            context
                .execution
                .charge_work(bytes)
                .map_err(|reason| certificate.tbs().subject().resource(reason))?;
            if super::same(certificate, published) {
                return Ok(rejection(
                    ConstraintSource::Certificate(index),
                    RejectionReason::AnchorIncluded,
                ));
            }
        }
    }
    drop(seen);
    let settings = Settings {
        execution: context.execution,
        interval: context.interval,
        policy: context.policy,
        policies: context.bounds.policies,
        recognizer: context.recognizer,
    };
    let outcome = process(Chain::Supplied(chain), anchor, &settings, &mut driver)?;
    if let Some(halted) = driver.halted {
        return Ok(Validation::Incomplete(halted));
    }
    Ok(outcome)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fixtures::{CertificateParts, oid as encoded_oid, printable, seq, set};
    use core::cell::Cell;
    use structured_asn1::{BitString, Parser};

    struct Answer {
        result: Verified,
        calls: Cell<usize>,
    }
    impl Verify for Answer {
        type Error = core::convert::Infallible;
        fn verify(
            &self,
            _algorithm: &crate::AlgorithmIdentifier<'_>,
            _key: WorkingKey<'_>,
            _signed: &[u8],
            _signature: BitString<'_>,
            _execution: &Memory,
        ) -> core::result::Result<Verified, Self::Error> {
            self.calls.set(self.calls.get() + 1);
            Ok(self.result)
        }
    }

    fn target() -> alloc::vec::Vec<u8> {
        CertificateParts {
            subject: seq(&[set(&[seq(&[
                encoded_oid(oid::COMMON_NAME),
                printable("target"),
            ])])]),
            ..CertificateParts::default()
        }
        .build()
    }

    fn root() -> alloc::vec::Vec<u8> {
        let parts = CertificateParts::default();
        CertificateParts {
            subject: parts.issuer.clone(),
            ..parts
        }
        .build()
    }

    #[test]
    fn supplied_path_separates_success_no_and_unsupported() {
        // The same public operation preserves all three completed provider answers.
        let root_der = root();
        let leaf_der = target();
        let parser = Parser::der(crate::fixtures::memory());
        let root = Certificate::read(&parser, &root_der, 0).unwrap();
        let leaf = Certificate::read(&parser, &leaf_der, 100).unwrap();
        let anchor = Anchor::new(root.tbs().subject(), root.tbs().public_key());
        for result in [Verified::Yes, Verified::No, Verified::Unsupported] {
            let execution = crate::fixtures::memory();
            let verifier = Answer {
                result,
                calls: Cell::new(0),
            };
            let context = ValidationContext {
                execution: &execution,
                interval: Interval::at(leaf.tbs().validity().not_before().seconds()),
                policy: Policy::default(),
                bounds: ValidationBounds::default(),
                verifier: &verifier,
                watch: &(),
                recognizer: &(),
            };
            let outcome = crate::path::validate(&[&leaf], anchor, &context).unwrap();
            match (result, outcome) {
                (Verified::Yes, Validation::Accepted(facts)) => {
                    assert_eq!(
                        facts.key.key().as_bytes(),
                        leaf.tbs().public_key().as_bytes()
                    );
                    assert_eq!(facts.policies(), []);
                }
                (Verified::No, Validation::Rejected(rejection)) => {
                    assert_eq!(rejection.at, Some(ConstraintSource::Certificate(0)));
                    assert_eq!(rejection.reason, RejectionReason::Signature);
                }
                (Verified::Unsupported, Validation::Rejected(rejection)) => {
                    assert_eq!(rejection.at, Some(ConstraintSource::Certificate(0)));
                    assert_eq!(rejection.reason, RejectionReason::UnsupportedSignature);
                }
                other => panic!("unexpected public path result: {other:?}"),
            }
            assert_eq!(verifier.calls.get(), 1);
            assert_eq!(execution.used(), 0);
        }
    }

    #[test]
    fn supplied_local_work_refusal_never_calls_the_provider() {
        // Local work exhaustion is incomplete; it is not a signature rejection.
        let root_der = root();
        let leaf_der = target();
        let parser = Parser::der(crate::fixtures::memory());
        let root = Certificate::read(&parser, &root_der, 0).unwrap();
        let leaf = Certificate::read(&parser, &leaf_der, 100).unwrap();
        let execution = crate::fixtures::memory();
        let verifier = Answer {
            result: Verified::Yes,
            calls: Cell::new(0),
        };
        let context = ValidationContext {
            execution: &execution,
            interval: Interval::at(leaf.tbs().validity().not_before().seconds()),
            policy: Policy::default(),
            bounds: ValidationBounds {
                work: 0,
                ..ValidationBounds::default()
            },
            verifier: &verifier,
            watch: &(),
            recognizer: &(),
        };
        assert!(matches!(
            crate::path::validate(
                &[&leaf],
                Anchor::new(root.tbs().subject(), root.tbs().public_key()),
                &context
            )
            .unwrap(),
            Validation::Incomplete(Truncation::Work)
        ));
        assert_eq!(verifier.calls.get(), 0);
        assert_eq!(execution.used(), 0);
        assert_eq!(execution.work_used(), 0);
    }
}