structured-pkix 0.6.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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
//! The extensions a certificate carries, kept whether or not they are
//! understood.
//!
//! An extension marked critical says that a relying party which does not
//! understand it must not use the certificate. That is a decision about a
//! verdict, not about whether the bytes can be read, so an unknown critical
//! extension is reported here rather than refused: the product has to be able
//! to say what it found and why it stopped, and a parser that refused would
//! leave nothing to say it with.
//!
//! Which identifiers count as understood is not fixed by this crate alone. A
//! scheme knows extensions of its own, and says so by implementing
//! [`Recognizer`]. What it supplies is a set of identifiers, never code and
//! never a place to fetch anything from.

use crate::access::{DistributionPoints, InfoAccess};
use crate::constraints::{BasicConstraints, InhibitAnyPolicy, NameConstraints, PolicyConstraints};
use crate::error::{CallbackError, CallbackResult, Error, ErrorCode, Located, Result, gate};
use crate::general_name::{self, GeneralName};
use crate::identifier::{AuthorityKeyIdentifier, SubjectKeyIdentifier};
use crate::oid;
use crate::policy::{CertificatePolicies, PolicyMappings};
use crate::qualified::QcStatements;
use crate::usage::{ExtendedKeyUsage, KeyUsage};
use structured_asn1::memory::{Memory, MemoryError, OwnedVec, Work};
use structured_asn1::{Element, Oid, Parser, Reader, ScopedReader, Span, Tag, universal};

mod entry;
pub use entry::{
    ExtensionFields, ExtensionHeader, ExtensionValue, ScopedExtensionFields, open, read_with,
};

/// One extension: what it is, whether it must be understood, and its value.
#[derive(Debug, Clone, Copy)]
pub struct Extension<'a> {
    kind: Oid<'a>,
    critical: bool,
    value: &'a [u8],
    value_span: Span,
    span: Span,
    /// The parser the certificate was read with, kept so every later decode
    /// of this value is charged to that one budget: a signature accepting an
    /// unrelated parser would let repeated decodes reset the counters and
    /// cost many times the caller's declared limit.
    parser: &'a Parser,
    /// Depth of the reader that produced extnValue; lazy decoding must not
    /// reset the certificate's aggregate depth allowance.
    outer: u32,
}

impl<'a> Extension<'a> {
    /// Which extension this is.
    #[must_use]
    pub fn kind(self) -> Oid<'a> {
        self.kind
    }

    /// Whether a relying party that does not understand this extension must
    /// refuse the certificate.
    #[must_use]
    pub fn is_critical(self) -> bool {
        self.critical
    }

    /// The value, still encoded: the contents of the octet string the
    /// extension wraps it in, which is a structure of its own.
    #[must_use]
    pub fn value(self) -> &'a [u8] {
        self.value
    }

    /// Where the extension sits in the document.
    #[must_use]
    pub fn span(self) -> Span {
        self.span
    }

    /// A reader over the value using the current execution domain and the
    /// original parser session's cumulative ceilings, offsets and depth.
    ///
    /// How a scheme reads an extension of its own. Sharing the budget is what
    /// keeps a certificate carrying a hundred extensions from costing a
    /// hundred times what the caller allowed, and the offsets a reader made
    /// this way reports are offsets in the whole document. The wrapped value
    /// sits below extnValue and retains the certificate's depth allowance.
    pub fn reader<W: Work + 'a>(self, execution: &W) -> structured_asn1::Result<Reader<'a>> {
        // Always DER: the value lives inside the certificate, which RFC 5280
        // section 4.1 defines in DER, whatever encoding the envelope was
        // read in. A caller that read a BER envelope must not have its
        // leniency reach into the certificate's own structures.
        execution.read_nested(
            self.parser,
            self.value,
            self.value_span.start,
            structured_asn1::Encoding::Der,
            self.outer,
        )
    }

    /// Opens original payload bytes under a shorter current operation. Parsed
    /// input views keep their source lifetime, while only the cursor/scratch
    /// borrows this execution. Depth, source and query admission are unchanged.
    pub fn reader_scoped<'operation, W: Work>(
        self,
        execution: &'operation W,
    ) -> structured_asn1::Result<ScopedReader<'a, 'operation>>
    where
        'a: 'operation,
    {
        execution.read_scoped_nested(
            self.parser,
            self.value,
            self.value_span.start,
            structured_asn1::Encoding::Der,
            self.outer,
        )
    }

    /// Original DER value under both origin and current query ceilings.
    ///
    /// The query may come from another source. Original parser identity,
    /// offsets and inherited depth are preserved from creation; allocation,
    /// traversal and scratch also obey the current operation's local budget
    /// and aggregate account. Previously retained storage is not transferred.
    pub fn reader_in<'work>(self, query: &Reader<'work>) -> structured_asn1::Result<Reader<'work>>
    where
        'a: 'work,
    {
        self.parser.read_nested_query_in(
            query,
            self.value,
            self.value_span.start,
            structured_asn1::Encoding::Der,
            self.outer,
        )
    }

    /// The parser the certificate was read with, whose budget every decode
    /// of this extension shares.
    #[must_use]
    pub fn parser(self) -> &'a Parser {
        self.parser
    }
}

/// The extensions of one certificate, in the order it holds them.
#[derive(Debug)]
pub struct Extensions<'a> {
    items: OwnedVec<Extension<'a>>,
}

impl<'a> Extensions<'a> {
    /// Account retaining this collection's storage. Deferred decoding may
    /// explicitly use a different current execution domain.
    #[must_use]
    pub fn memory(&self) -> &Memory {
        self.items.memory()
    }

    pub(crate) fn empty(execution: &Memory) -> Self {
        Self {
            items: execution.vector(),
        }
    }
    /// Every extension in order, funded by this query's current execution.
    /// The constructor allocates/clones nothing. Each metadata step is paid
    /// before advancing; a resource failure is yielded once, then traversal
    /// stops. Empty/exhausted catalogues perform no additional charged work.
    /// Recognition and payload parsing must fund their own data-dependent work.
    pub fn iter<'r, 'e, W: Work + 'e>(
        &'r self,
        execution: &'e W,
    ) -> impl Iterator<Item = Result<&'r Extension<'a>>> + 'e
    where
        'r: 'e,
    {
        let mut entries = self.items.iter();
        let mut failed = false;
        core::iter::from_fn(move || {
            if failed || entries.as_slice().is_empty() {
                return None;
            }
            if let Err(reason) = execution.charge_work(1) {
                failed = true;
                // Source metadata for the refusal; the traversal has not
                // advanced or exposed this item as a successful reading.
                let at = entries.as_slice()[0].span.start;
                return Some(Err(Error::resource(reason, "extensions", at)));
            }
            entries.next().map(Ok)
        })
    }

    /// How many there are.
    #[must_use]
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Whether there are none.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// One extension by identifier. There is at most one of each: a
    /// certificate carrying two copies is refused when it is read, because
    /// which of them applies would have no answer.
    pub fn get<W: Work>(&self, kind: Oid<'_>, execution: &W) -> Result<Option<&Extension<'a>>> {
        for item in &self.items {
            let work = item.kind.as_bytes().len().max(kind.as_bytes().len()) as u64;
            execution
                .charge_work(1 + work)
                .map_err(|reason| Error::resource(reason, "extensions", item.span.start))?;
            if item.kind == kind {
                return Ok(Some(item));
            }
        }
        Ok(None)
    }

    /// The critical extensions this run did not understand: the ones nobody
    /// knows, and the ones this crate knows by identifier but whose values do
    /// not parse.
    ///
    /// A non-empty answer is what blocks a verdict. Knowing the identifier is
    /// not understanding the extension: a critical key usage whose value
    /// never parsed was never understood, and filtering it out by identifier
    /// alone would leave a validator seeing no blocker for a demand the
    /// certificate marked as one. Extensions a `recognizer` claims are that
    /// scheme's to decode and are taken at its word.
    ///
    /// The answer is returned rather than acted on, because what to do about
    /// it belongs to the policy layer and the report has to name them. An
    /// error is a spent budget and only that: it says this run gave up before
    /// the question was answered, which must not be reported as a demand
    /// nobody understood.
    pub fn unhandled_critical<'r, E, W: Work>(
        &'r self,
        recognizer: &impl Recognizer<W, Error = E>,
        execution: &W,
    ) -> CallbackResult<OwnedVec<&'r Extension<'a>>, E> {
        let mut unhandled = execution.vector();
        for item in self.criticals(execution) {
            let item = item?;
            if !self.understands(item, recognizer, execution)? {
                unhandled
                    .try_push(item)
                    .map_err(|reason| Error::resource(reason, "extensions", item.span().start))?;
            }
        }
        Ok(unhandled.into_owned())
    }

    /// The critical extensions, in the order the certificate wrote them and
    /// without reading any of their values.
    ///
    /// Separate from [`Self::understands`] because working out whether one
    /// was understood decodes its value, and how much that value holds is a
    /// document's to decide. A caller holding a budget charges for an entry
    /// and then asks: the two joined into one step would decode before the
    /// charge could refuse it, since asking for the next entry is what would
    /// do the decoding.
    /// Each metadata entry is admitted before testing its critical flag,
    /// including skipped entries and a complete miss. A resource error is
    /// yielded once, then iteration permanently ends without further charges.
    ///
    /// # Examples
    /// ```no_run
    /// use structured_asn1::memory::Memory;
    /// use structured_pkix::{Certificate, Result};
    /// fn has_critical(certificate: &Certificate<'_>, execution: &Memory) -> Result<bool> {
    ///     match certificate.tbs().extensions().criticals(execution).next() {
    ///         Some(item) => { item?; Ok(true) }
    ///         None => Ok(false),
    ///     }
    /// }
    /// ```
    pub fn criticals<'r, 'e, W: Work + 'e>(
        &'r self,
        execution: &'e W,
    ) -> impl Iterator<Item = Result<&'r Extension<'a>>> + 'e
    where
        'r: 'e,
    {
        let mut entries = self.iter(execution);
        core::iter::from_fn(move || {
            for item in entries.by_ref() {
                let item = match item {
                    Ok(item) => item,
                    Err(error) => return Some(Err(error)),
                };
                if item.critical {
                    return Some(Ok(item));
                }
            }
            None
        })
    }

    /// Whether this run understood one extension: this crate decodes it, or
    /// a scheme claims it and it holds one complete value.
    ///
    /// Knowing the identifier is not understanding the extension: a critical
    /// key usage whose value never parsed was never understood, and taking
    /// it by identifier alone would leave a validator seeing no blocker for
    /// a demand the certificate marked as one.
    pub fn understands<E, W: Work>(
        &self,
        item: &Extension<'a>,
        recognizer: &impl Recognizer<W, Error = E>,
        execution: &W,
    ) -> CallbackResult<bool, E> {
        let work = (item.kind.as_bytes().len() as u64)
            .checked_mul(64)
            .ok_or_else(|| {
                Error::resource(MemoryError::WorkExceeded, "extensions", item.span.start)
            })?;
        execution
            .charge_work(work)
            .map_err(|reason| Error::resource(reason, "extensions", item.span.start))?;
        if is_known(item.kind) {
            return Self::decodes(item, recognizer, execution);
        }
        Ok(recognizer
            .recognizes(item.kind, execution)
            .map_err(CallbackError::Callback)?
            && Self::holds_one_value(item, execution)?)
    }

    /// Whether the value of an extension this crate does not decode is one
    /// complete DER value, all the way down.
    ///
    /// A scheme says which identifiers are its to decode. It never sees the
    /// bytes, so its claim cannot be a statement about them, and "the
    /// corresponding ASN.1 DER encoded structure is the value of the octet
    /// string extnValue" (RFC 5280 4.1.2.9) is the one thing that can be
    /// asked here: one structure, nothing after it, and every element under
    /// it written the way the encoding writes it. Without this a critical
    /// demand carrying bytes that are a value of no type would clear the
    /// gate on an identifier alone.
    fn holds_one_value<W: Work>(item: &Extension<'a>, execution: &W) -> Result<bool> {
        const FIELD: &str = "extensions";
        let read = || -> Result<()> {
            let at = |code| Error::new(code, FIELD, item.value_span.start);
            let mut reader = item.reader_scoped(execution).field(FIELD)?;
            let element = reader
                .next_element()
                .field(FIELD)?
                .ok_or_else(|| at(ErrorCode::MissingField))?;
            crate::name::walk_der(&reader, &element, FIELD, crate::name::Strings::Checked)?;
            if reader.is_empty() {
                Ok(())
            } else {
                Err(at(ErrorCode::TrailingData))
            }
        };
        Ok(gate(read())?.is_some())
    }

    /// Whether a known extension's value actually parses. An error is a spent
    /// budget, never a malformed value.
    fn decodes<E, W: Work>(
        item: &Extension<'a>,
        recognizer: &impl Recognizer<W, Error = E>,
        execution: &W,
    ) -> CallbackResult<bool, E> {
        let kind = item.kind;
        Ok(if kind == oid::BASIC_CONSTRAINTS {
            gate(BasicConstraints::read(item, execution))?.is_some()
        } else if kind == oid::KEY_USAGE {
            gate(KeyUsage::read(item, execution))?.is_some()
        } else if kind == oid::EXT_KEY_USAGE {
            gate(ExtendedKeyUsage::read(item, execution))?.is_some()
        } else if kind == oid::SUBJECT_ALT_NAME || kind == oid::ISSUER_ALT_NAME {
            // The names read for the report either way; understanding also
            // asks that every other-name type be one somebody claims.
            match gate(general_name::read_alternative(item, execution))? {
                Some(names) => all(names.iter(), item, execution, |name| {
                    other_name_known(name, recognizer, execution)
                })?,
                None => false,
            }
        } else if kind == oid::SUBJECT_KEY_IDENTIFIER {
            gate(SubjectKeyIdentifier::read(item, execution))?.is_some()
        } else if kind == oid::AUTHORITY_KEY_IDENTIFIER {
            // The issuer names inside are the identity the extension points
            // at, so they answer the same other-name question.
            match gate(AuthorityKeyIdentifier::read(item, execution))? {
                Some(identifier) => all(identifier.issuer().iter(), item, execution, |name| {
                    other_name_known(name, recognizer, execution)
                })?,
                None => false,
            }
        } else if kind == oid::NAME_CONSTRAINTS {
            match gate(NameConstraints::read(item, execution))? {
                Some(constraints) => all(
                    constraints.permitted().iter().chain(constraints.excluded()),
                    item,
                    execution,
                    |subtree| {
                        // A constraint is understood only where a
                        // certificate's names can be matched against it. The
                        // X.400 and EDI forms are kept as opaque encoded
                        // bytes, and a registered identifier has no defined
                        // constraint-matching rule either, so a critical
                        // constraint built on any of them was not understood
                        // even though its structure parsed.
                        //
                        // An other-name is the same question asked of a
                        // scheme, and it is the constraint question and not
                        // the reading one: knowing what a name of that type
                        // says is not knowing which names sit inside a
                        // subtree of it.
                        Ok(match subtree.base() {
                            GeneralName::X400(_)
                            | GeneralName::EdiParty(_)
                            | GeneralName::RegisteredId(_) => false,
                            GeneralName::Other { type_id, .. } => recognizer
                                .recognizes_name_constraint(*type_id, execution)
                                .map_err(CallbackError::Callback)?,
                            _ => true,
                        })
                    },
                )?,
                None => false,
            }
        } else if kind == oid::CERTIFICATE_POLICIES {
            // The list parsing keeps qualifiers encoded; understanding a
            // critical policy extension includes interpreting them.
            match gate(CertificatePolicies::read(item, execution))? {
                Some(policies) => policies.understood(item.parser, execution)?,
                None => false,
            }
        } else if kind == oid::POLICY_MAPPINGS {
            gate(PolicyMappings::read(item, execution))?.is_some()
        } else if kind == oid::POLICY_CONSTRAINTS {
            gate(PolicyConstraints::read(item, execution))?.is_some()
        } else if kind == oid::INHIBIT_ANY_POLICY {
            gate(InhibitAnyPolicy::read(item, execution))?.is_some()
        } else if kind == oid::AUTHORITY_INFO_ACCESS || kind == oid::SUBJECT_INFO_ACCESS {
            // A description's meaning is its method identifier, and a known
            // method does not vouch for its location: an other-name there
            // whose type nobody claims is a location this run never
            // interpreted, and a form outside the ones the method names is
            // an endpoint it cannot use. Either way the accessor still
            // reports it.
            match gate(InfoAccess::read(item, execution))? {
                Some(access) => all(
                    access.descriptions().iter(),
                    item,
                    execution,
                    |description| {
                        let method = description.method();
                        // Each extension defines its own methods: OCSP and the
                        // issuing certificates on the authority side (RFC 5280
                        // 4.2.2.1), the repository and the time-stamping service
                        // on the subject side (RFC 5280 4.2.2.2). A method
                        // carried by the other extension has no defined meaning
                        // there and was not understood.
                        let named = if kind == oid::AUTHORITY_INFO_ACCESS {
                            [oid::OCSP, oid::CA_ISSUERS].contains(&method)
                        } else {
                            [oid::TIME_STAMPING_ACCESS, oid::CA_REPOSITORY].contains(&method)
                        };
                        Ok((named
                            || recognizer
                                .recognizes_access_method(method, execution)
                                .map_err(CallbackError::Callback)?)
                            && other_name_known(description.location(), recognizer, execution)?
                            && (!named || method_location_fits(method, description.location())))
                    },
                )?,
                None => false,
            }
        } else if kind == oid::CRL_DISTRIBUTION_POINTS || kind == oid::FRESHEST_CRL {
            // A point's names say where the revocation evidence is, and the
            // issuer names who signs it: both answer the other-name question.
            match gate(DistributionPoints::read(item, execution))? {
                Some(points) => all(points.points().iter(), item, execution, |point| {
                    all(
                        point.full_name().iter().chain(point.issuer()),
                        item,
                        execution,
                        |name| other_name_known(name, recognizer, execution),
                    )
                })?,
                None => false,
            }
        } else if kind == oid::OCSP_NO_CHECK {
            gate(crate::ocsp::read_no_check(item, execution))?.is_some()
        } else if kind == oid::QC_STATEMENTS {
            // The outer list parsing is not understanding: the statement
            // payloads decode lazily, and a critical list whose statement
            // carries garbage, or whose identifier nobody here knows, is a
            // critical demand this run never understood, one level down from
            // an unknown extension.
            match gate(QcStatements::read(item, execution))? {
                Some(statements) => statements.understood(recognizer, execution)?,
                None => false,
            }
        } else {
            // is_known and this list name the same identifiers; a mismatch is
            // a bug, and claiming the value parsed would hide it.
            false
        })
    }
}

/// Generates one typed accessor per extension: absent when the list does not
/// carry the identifier, otherwise whatever the reader makes of the value.
/// The table is the single place an accessor name, its identifier and its
/// reader meet, so the three cannot drift apart.
macro_rules! typed_accessors {
    ($($(#[$doc:meta])* $name:ident, $oid:path => $reader:path, $result:ty;)+) => {
        $(
            $(#[$doc])*
            pub fn $name<W: Work>(&self, execution: &W) -> Result<Option<$result>> {
                match self.get($oid, execution)? {
                    Some(item) => $reader(item, execution).map(Some),
                    None => Ok(None),
                }
            }
        )+
    };
}

/// The extensions this crate reads into structures of their own.
///
/// Each is read when it is asked for rather than when the certificate is,
/// because an extension that cannot be read is not a certificate that cannot
/// be read. Lookup and decoding use the caller's current execution domain;
/// the original parser's cumulative local ceilings still apply.
impl<'a> Extensions<'a> {
    /// Reads one original Extensions SEQUENCE with the certificate catalogue
    /// invariants: nonempty, no duplicate OIDs, primitive extnValue, and the
    /// producing encoding's DEFAULT rules. This does not recognize/trust values.
    pub fn read_element(
        parent: &ScopedReader<'a, '_>,
        element: &Element<'a>,
        field: &'static str,
    ) -> Result<Self> {
        read(parent, element, field)
    }

    /// Reads `[number] IMPLICIT Extensions` without changing original bytes.
    /// The number is selected by the enclosing schema (RFC3161 §§2.4.1–2.4.2).
    /// This strict catalogue is suitable for requests, not `TSTInfo` inspection
    /// recovery; use [`read_with`] there before deciding duplicate/fault policy.
    /// Original source/depth and producing local/current aggregate work survive.
    /// <https://www.rfc-editor.org/rfc/rfc3161#section-2.4.1>
    pub fn read_implicit(
        parent: &ScopedReader<'a, '_>,
        element: &Element<'a>,
        number: u32,
        field: &'static str,
    ) -> Result<Self> {
        read_tagged(parent, element, Tag::context(true, number), field)
    }

    typed_accessors! {
        /// Whether this is a certification authority, and how deep a path
        /// below it may go.
        basic_constraints, oid::BASIC_CONSTRAINTS => BasicConstraints::read, BasicConstraints;
        /// What the key may be used for.
        key_usage, oid::KEY_USAGE => KeyUsage::read, KeyUsage<'a>;
        /// The further purposes the key is named for.
        extended_key_usage, oid::EXT_KEY_USAGE => ExtendedKeyUsage::read, ExtendedKeyUsage<'a>;
        /// The further names for the subject.
        subject_alt_name, oid::SUBJECT_ALT_NAME => general_name::read_alternative, OwnedVec<GeneralName<'a>>;
        /// The further names for the issuer.
        issuer_alt_name, oid::ISSUER_ALT_NAME => general_name::read_alternative, OwnedVec<GeneralName<'a>>;
        /// The identifier for the key this certificate carries.
        subject_key_identifier, oid::SUBJECT_KEY_IDENTIFIER => SubjectKeyIdentifier::read, SubjectKeyIdentifier<'a>;
        /// The identifier for the key that signed it.
        authority_key_identifier, oid::AUTHORITY_KEY_IDENTIFIER => AuthorityKeyIdentifier::read, AuthorityKeyIdentifier<'a>;
        /// The name space this authority may certify within.
        name_constraints, oid::NAME_CONSTRAINTS => NameConstraints::read, NameConstraints<'a>;
        /// The policies the certificate was issued under.
        certificate_policies, oid::CERTIFICATE_POLICIES => CertificatePolicies::read, CertificatePolicies<'a>;
        /// The policies of one domain treated as equivalent to another's.
        policy_mappings, oid::POLICY_MAPPINGS => PolicyMappings::read, PolicyMappings<'a>;
        /// The limits on policy processing below this certificate.
        policy_constraints, oid::POLICY_CONSTRAINTS => PolicyConstraints::read, PolicyConstraints;
        /// After how many certificates the identifier standing for any policy
        /// stops being accepted.
        inhibit_any_policy, oid::INHIBIT_ANY_POLICY => InhibitAnyPolicy::read, InhibitAnyPolicy;
        /// Where the issuer's certificate and status service are published.
        authority_info_access, oid::AUTHORITY_INFO_ACCESS => InfoAccess::read, InfoAccess<'a>;
        /// Where services belonging to the subject are published.
        subject_info_access, oid::SUBJECT_INFO_ACCESS => InfoAccess::read, InfoAccess<'a>;
        /// Where revocation lists covering this certificate are published.
        crl_distribution_points, oid::CRL_DISTRIBUTION_POINTS => DistributionPoints::read, DistributionPoints<'a>;
        /// Where the delta revocation list is published.
        freshest_crl, oid::FRESHEST_CRL => DistributionPoints::read, DistributionPoints<'a>;
        /// What the issuer claims about this being a qualified certificate.
        qc_statements, oid::QC_STATEMENTS => QcStatements::read, QcStatements<'a>;
    }
}

/// Whether a name is not an other-name, or is one whose type identifier a
/// scheme claims. An other-name's value means what its type says, this crate
/// names no such types itself, and a critical name extension carrying one
/// nobody claims was not understood.
/// Whether a location takes a form its access method names (RFC 5280
/// 4.2.2.1 and 4.2.2.2): the status responder is reached by a URI; the
/// issuing certificates and the repository publish over transfer protocols,
/// a URI, or the directory, a directory name, and a mailbox names no
/// publication location for them; the timestamping service adds the mailbox,
/// host and address forms. A method a recognizer claims keeps its own forms.
fn method_location_fits(method: Oid<'_>, location: &GeneralName<'_>) -> bool {
    if method == oid::OCSP {
        matches!(location, GeneralName::Uri(_))
    } else if method == oid::CA_ISSUERS || method == oid::CA_REPOSITORY {
        matches!(location, GeneralName::Uri(_) | GeneralName::Directory(_))
    } else if method == oid::TIME_STAMPING_ACCESS {
        matches!(
            location,
            GeneralName::Uri(_)
                | GeneralName::Email(_)
                | GeneralName::Dns(_)
                | GeneralName::IpAddress(_)
        )
    } else {
        true
    }
}

pub(crate) fn other_name_known<E, W: Work>(
    name: &GeneralName<'_>,
    recognizer: &impl Recognizer<W, Error = E>,
    execution: &W,
) -> CallbackResult<bool, E> {
    match name {
        GeneralName::Other { type_id, .. } => recognizer
            .recognizes_other_name(*type_id, execution)
            .map_err(CallbackError::Callback),
        _ => Ok(true),
    }
}

fn all<T, E, W: Work>(
    values: impl Iterator<Item = T>,
    item: &Extension<'_>,
    execution: &W,
    mut understood: impl FnMut(T) -> CallbackResult<bool, E>,
) -> CallbackResult<bool, E> {
    for value in values {
        execution
            .charge_work(1)
            .map_err(|reason| Error::resource(reason, "extensions", item.span.start))?;
        if !understood(value)? {
            return Ok(false);
        }
    }
    Ok(true)
}

/// The identifiers a scheme understands, answered by the role an identifier
/// plays: claiming an extension says nothing about interpreting an
/// other-name or an access method that reuses the same identifier.
///
/// Data, not behaviour: an implementation answers whether an identifier is one
/// it knows. It cannot decode anything, and nothing here gives it a way to
/// reach the network. Each role defaults to no, the direction that blocks
/// rather than vouches.
pub trait Recognizer<W: Work = Memory> {
    /// The original policy, provider or resource error of this implementation.
    type Error;

    /// Whether this scheme understands the extension.
    fn recognizes(&self, kind: Oid<'_>, execution: &W) -> core::result::Result<bool, Self::Error>;

    /// Whether this scheme can interpret an other-name of this type.
    fn recognizes_other_name(
        &self,
        _type_id: Oid<'_>,
        _execution: &W,
    ) -> core::result::Result<bool, Self::Error> {
        Ok(false)
    }

    /// Whether this scheme enforces a name constraint written over an
    /// other-name of this type.
    ///
    /// A different question from reading one. Reading asks what a name of
    /// this type says; a constraint asks whether a certificate's names fall
    /// inside a subtree of it, which needs a containment rule the type's
    /// reader never states. A scheme that reads the type and has no such
    /// rule leaves the constraint enforced by nobody, so this role is
    /// answered separately and defaults to no.
    fn recognizes_name_constraint(
        &self,
        _type_id: Oid<'_>,
        _execution: &W,
    ) -> core::result::Result<bool, Self::Error> {
        Ok(false)
    }

    /// Whether this scheme understands an information-access method, its
    /// location forms included.
    fn recognizes_access_method(
        &self,
        _method: Oid<'_>,
        _execution: &W,
    ) -> core::result::Result<bool, Self::Error> {
        Ok(false)
    }

    /// Whether this scheme can interpret a qualified statement of this kind:
    /// a national statement is knowledge a scheme carries, not this crate.
    fn recognizes_statement(
        &self,
        _kind: Oid<'_>,
        _execution: &W,
    ) -> core::result::Result<bool, Self::Error> {
        Ok(false)
    }
}

/// Nothing beyond what this crate itself understands.
impl<W: Work> Recognizer<W> for () {
    type Error = core::convert::Infallible;
    fn recognizes(
        &self,
        _kind: Oid<'_>,
        _execution: &W,
    ) -> core::result::Result<bool, Self::Error> {
        Ok(false)
    }
}

impl<W: Work> Recognizer<W> for [Oid<'_>] {
    type Error = MemoryError;
    fn recognizes(&self, kind: Oid<'_>, execution: &W) -> core::result::Result<bool, Self::Error> {
        for candidate in self {
            execution
                .charge_work(1 + candidate.as_bytes().len().max(kind.as_bytes().len()) as u64)?;
            if *candidate == kind {
                return Ok(true);
            }
        }
        Ok(false)
    }
}

/// A scheme reached by reference answers exactly as it does directly.
///
/// Every role of the trait is forwarded, and a new role must be added here
/// too: each default answers no, so a role left behind silently drops the
/// scheme's knowledge for callers holding it behind a reference, which is
/// how a generic wrapper and a `&dyn Recognizer` both arrive.
impl<W: Work, T: Recognizer<W> + ?Sized> Recognizer<W> for &T {
    type Error = T::Error;
    fn recognizes(&self, kind: Oid<'_>, execution: &W) -> core::result::Result<bool, Self::Error> {
        (*self).recognizes(kind, execution)
    }

    fn recognizes_other_name(
        &self,
        type_id: Oid<'_>,
        execution: &W,
    ) -> core::result::Result<bool, Self::Error> {
        (*self).recognizes_other_name(type_id, execution)
    }

    fn recognizes_name_constraint(
        &self,
        type_id: Oid<'_>,
        execution: &W,
    ) -> core::result::Result<bool, Self::Error> {
        (*self).recognizes_name_constraint(type_id, execution)
    }

    fn recognizes_access_method(
        &self,
        method: Oid<'_>,
        execution: &W,
    ) -> core::result::Result<bool, Self::Error> {
        (*self).recognizes_access_method(method, execution)
    }

    fn recognizes_statement(
        &self,
        kind: Oid<'_>,
        execution: &W,
    ) -> core::result::Result<bool, Self::Error> {
        (*self).recognizes_statement(kind, execution)
    }
}

/// Whether this crate reads the extension into a structure of its own.
#[must_use]
pub fn is_known(kind: Oid<'_>) -> bool {
    [
        oid::BASIC_CONSTRAINTS,
        oid::KEY_USAGE,
        oid::EXT_KEY_USAGE,
        oid::SUBJECT_ALT_NAME,
        oid::ISSUER_ALT_NAME,
        oid::SUBJECT_KEY_IDENTIFIER,
        oid::AUTHORITY_KEY_IDENTIFIER,
        oid::NAME_CONSTRAINTS,
        oid::CERTIFICATE_POLICIES,
        oid::POLICY_MAPPINGS,
        oid::POLICY_CONSTRAINTS,
        oid::INHIBIT_ANY_POLICY,
        oid::AUTHORITY_INFO_ACCESS,
        oid::SUBJECT_INFO_ACCESS,
        oid::CRL_DISTRIBUTION_POINTS,
        oid::FRESHEST_CRL,
        oid::QC_STATEMENTS,
        oid::OCSP_NO_CHECK,
    ]
    .contains(&kind)
}

/// A reader over the members of the one sequence an extension's value holds,
/// which is the shape of most of them.
pub(crate) fn sequence<'a: 'operation, 'operation, W: Work>(
    extension: &Extension<'a>,
    field: &'static str,
    execution: &'operation W,
) -> Result<ScopedReader<'a, 'operation>> {
    sequence_reader(
        extension.reader_scoped(execution).field(field)?,
        field,
        extension.span().start,
    )
}

pub(crate) fn sequence_reader<'a: 'operation, 'operation>(
    mut reader: ScopedReader<'a, 'operation>,
    field: &'static str,
    at: u64,
) -> Result<ScopedReader<'a, 'operation>> {
    let fail = |code| Error::new(code, field, at);
    let element = reader
        .next_element()
        .field(field)?
        .ok_or_else(|| fail(ErrorCode::MissingField))?;
    if element.tag() != Tag::constructed(universal::SEQUENCE) {
        return Err(fail(ErrorCode::Encoding(
            structured_asn1::ErrorCode::UnexpectedTag,
        )));
    }
    // A value holding a second structure after the first is two answers to
    // what the extension says.
    if !reader.is_empty() {
        return Err(fail(ErrorCode::TrailingData));
    }

    reader.enter(&element).field(field)
}

/// The number and construction of a context-specific tag, and `None` for a
/// tag of any other class.
///
/// The fields these schemas tag by position are context-specific, and a match
/// on number and construction alone would read an APPLICATION or PRIVATE tag
/// with the same number as the field it is not.
pub(crate) fn context_of(tag: Tag) -> Option<(u32, bool)> {
    // A number this reader does not name is not a field any schema wrote.
    (tag.class() == structured_asn1::Class::ContextSpecific)
        .then(|| tag.number().map(|number| (number, tag.is_constructed())))
        .flatten()
}

/// Reads the extensions from the element the schema wraps them in. The
/// parent reader's parser is kept with each extension, so every later decode
/// of its value is charged to the budget the certificate was read with, and
/// no other parser can be handed in beside the reader.
pub(crate) fn read<'a>(
    parent: &ScopedReader<'a, '_>,
    element: &Element<'a>,
    field: &'static str,
) -> Result<Extensions<'a>> {
    read_tagged(
        parent,
        element,
        Tag::constructed(universal::SEQUENCE),
        field,
    )
}

fn read_tagged<'a>(
    parent: &ScopedReader<'a, '_>,
    element: &Element<'a>,
    expected: Tag,
    field: &'static str,
) -> Result<Extensions<'a>> {
    let parser = parent.parser();
    let fail = |code| Error::new(code, field, element.span().start);

    if element.tag() != expected {
        return Err(fail(ErrorCode::Encoding(
            structured_asn1::ErrorCode::UnexpectedTag,
        )));
    }

    let mut sequence = parent.enter(element).field(field)?;
    let mut items = parent.execution().vector();
    // A set rather than a scan of what came before: a certificate can carry
    // tens of thousands of extensions within the parse budget, and a per-item
    // scan of the list would be quadratic work the budget never sees.
    let mut kinds = parent.execution().map::<Oid<'a>, ()>();

    while let Some(entry) = sequence.next_element().field(field)? {
        let at = |code| Error::new(code, field, entry.span().start);

        let fields = open(&sequence, &entry, field)?;
        if fields.header().default_written() && fields.reader().encoding().is_der() {
            let at = fields
                .header()
                .critical_span()
                .expect("written flag has a span")
                .start;
            return Err(Error::new(ErrorCode::MalformedValue, field, at));
        }
        let decoded = fields
            .read_with(|_header, inside, value| {
                // RFC5280 §4.1 / X.690 §11.5: the strict certificate profile
                // keeps its primitive-value and DER-default invariants. TSP
                // inspection can instead retain the common structural metadata.
                // https://www.rfc-editor.org/rfc/rfc5280#section-4.1
                if value.tag() != Tag::primitive(universal::OCTET_STRING) {
                    return Err(Error::new(
                        ErrorCode::Encoding(structured_asn1::ErrorCode::UnexpectedTag),
                        field,
                        value.span().start,
                    ));
                }
                if !inside.is_empty() {
                    return Err(at(ErrorCode::TrailingData));
                }
                Ok((value.contents(), value.contents_span(), inside.depth()))
            })
            .map_err(|error| match error {
                CallbackError::Processing(error) | CallbackError::Callback(error) => error,
            })?;
        let header = decoded.header();
        let kind = header.kind();
        let critical = header.is_critical();
        let (value, value_span, outer) = decoded.into_value();

        // Two copies of one extension leave no answer to which applies, and a
        // reader that took the first would disagree with one that took the
        // last about what the certificate says.
        if kinds
            .try_insert(kind, ())
            .map_err(|reason| Error::resource(reason, field, entry.span().start))?
            .is_some()
        {
            return Err(at(ErrorCode::DuplicateExtension));
        }

        items
            .try_push(Extension {
                kind,
                critical,
                value,
                value_span,
                span: entry.span(),
                parser,
                outer,
            })
            .map_err(|reason| Error::resource(reason, field, entry.span().start))?;
    }

    // The schema requires at least one, and an empty sequence is a structure
    // that says nothing while claiming to.
    if items.is_empty() {
        return Err(fail(ErrorCode::EmptySequence));
    }

    Ok(Extensions {
        items: items.into_owned(),
    })
}

#[cfg(test)]
mod tests;