structured-cms 0.12.0

Borrowed CMS signatures, digital envelopes and timestamps with explicit providers and resource accounting
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
//! Original Attribute Certificate v2 structure (RFC5755 §4.1 / Appendix B).
//!
//! This reader supplies signature inputs and inspection facts, not a complete
//! RFC5755 profile or authorization verdict. In particular, issuer forms and
//! holder combinations outside §4.2's profile, negative/long serials, reversed
//! validity and unknown critical extensions remain inspectable. Holder binding,
//! algorithm agreement/acceptance, signature math, AA trust, targeting and
//! revocation require separate checks (§5). No public-key certificate is made
//! from an attribute certificate, and no opaque attribute is trusted here.
//! <https://www.rfc-editor.org/rfc/rfc5755#section-4.1>

use crate::attribute::Attribute;
use crate::signer::required;
use crate::{Error, ErrorCode, Result};
use structured_asn1::memory::{OwnedVec, SortError, Work};
use structured_asn1::{
    BitString, Element, Integer, Oid, Parser, Reader, ScopedReader, Tag, universal,
};
use structured_pkix::general_name::{self, GeneralName};
use structured_pkix::signature::SignatureValue;
use structured_pkix::{AlgorithmIdentifier, Extensions, Time};

const FIELD: &str = "attributeCertificate";
const INFO: &str = "attributeCertificate.acinfo";

pub mod clearance;
pub mod controls;
pub mod encrypted;
pub mod ietf;
pub mod proxy;
pub mod role;
pub mod service;
pub mod target;
pub mod validation;
fn sequence() -> Tag {
    Tag::constructed(universal::SEQUENCE)
}

/// Certificate identity, without looking up or authenticating a certificate.
#[derive(Debug)]
pub struct IssuerSerial<'a> {
    /// Original issuer names, nonempty but not restricted to directoryName here.
    pub issuer: OwnedVec<GeneralName<'a>>,
    /// Original serial, including a readable negative or unusually wide number.
    pub serial: Integer<'a>,
    /// Optional original unique identifier, including unused-bit information.
    pub issuer_uid: Option<BitString<'a>>,
}

/// Object selected by an explicitly written object-digest type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DigestedObject {
    /// A `SubjectPublicKeyInfo`, including effective parameters (§7.3).
    PublicKey,
    /// An entire public-key certificate (§7.3).
    PublicKeyCertificate,
    /// ASN.1's third alternative, forbidden by the RFC5755 profile.
    Other,
}

/// Written digest assertion. It neither hashes nor binds an object.
#[derive(Debug)]
pub struct ObjectDigestInfo<'a> {
    /// Object selected by the original ENUMERATED value.
    pub object_type: DigestedObject,
    /// Original optional type identifier; profile consistency is separate.
    pub other_type: Option<Oid<'a>>,
    /// Original digest algorithm with its parameter provenance.
    pub algorithm: AlgorithmIdentifier<'a>,
    /// Original digest bits; width and padding acceptance belong to the resolver.
    pub digest: BitString<'a>,
}

/// All independently written Holder alternatives, without choosing an identity.
/// RFC5755 §4.2.2 recommends only one; ASN.1 permits combinations and absence.
#[derive(Debug)]
pub struct Holder<'a> {
    /// `[0] IMPLICIT IssuerSerial`.
    pub base_certificate: Option<IssuerSerial<'a>>,
    /// `[1] IMPLICIT GeneralNames`.
    pub entity_names: Option<OwnedVec<GeneralName<'a>>>,
    /// `[2] IMPLICIT ObjectDigestInfo`.
    pub object_digest: Option<ObjectDigestInfo<'a>>,
}

/// Original issuer fields, without imposing the profile's issuerName-only restriction.
#[derive(Debug)]
pub struct Issuer<'a> {
    /// Which original ASN.1 CHOICE was written.
    pub form: IssuerForm,
    /// Original `GeneralNames`; always present for V1, optional for V2.
    pub names: Option<OwnedVec<GeneralName<'a>>>,
    /// Optional `[0] IMPLICIT IssuerSerial`, absent for V1.
    pub base_certificate: Option<IssuerSerial<'a>>,
    /// Optional `[1] IMPLICIT ObjectDigestInfo`, absent for V1.
    pub object_digest: Option<ObjectDigestInfo<'a>>,
}

/// The encoded issuer CHOICE. V1 remains inspectable, not profile-conforming.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IssuerForm {
    /// Legacy `GeneralNames`, excluded by RFC5755 §4.2.3's profile.
    V1,
    /// `[0] IMPLICIT V2Form`.
    V2,
}

/// Signed v2 information, retaining original attributes and extension readers.
#[derive(Debug)]
pub struct Information<'a> {
    /// All original holder alternatives.
    pub holder: Holder<'a>,
    /// Original issuer form; not an authenticated AA.
    pub issuer: Issuer<'a>,
    /// Signed algorithm identifier, without provider resolution.
    pub signature: AlgorithmIdentifier<'a>,
    /// Original serial number, not truncated to a machine integer.
    pub serial: Integer<'a>,
    /// Inclusive lower validity label; no implicit clock comparison is made.
    pub not_before: Time,
    /// Inclusive upper validity label; a reversed interval stays inspectable.
    pub not_after: Time,
    /// Original attributes in source order, with nonempty values and unique OIDs.
    pub attributes: OwnedVec<Attribute<'a>>,
    /// Optional original issuer unique identifier.
    pub issuer_uid: Option<BitString<'a>>,
    /// Optional generic catalogue. PKC extension recognition is not AC acceptance.
    pub extensions: Option<Extensions<'a>>,
}

/// A v2 attribute certificate's structure and exact original signature inputs.
#[derive(Debug)]
pub struct AttributeCertificate<'a> {
    parser: &'a Parser,
    information: Information<'a>,
    algorithm: AlgorithmIdentifier<'a>,
    signature: SignatureValue<'a>,
    signed: Element<'a>,
    element: Element<'a>,
}

impl<'a> AttributeCertificate<'a> {
    /// Reads a standalone universal SEQUENCE, with no trailing document bytes.
    /// The producing reader selects BER or DER; signing requires the original
    /// DER `acinfo`, which this entry checks regardless of the outer encoding.
    pub fn read(parent: &mut Reader<'a>) -> Result<Self> {
        let element = required(parent, FIELD)?;
        let value = Self::read_element(parent, &element)?;
        end(parent, FIELD)?;
        Ok(value)
    }

    /// Reads one original universal SEQUENCE in its producing context.
    pub fn read_element(parent: &Reader<'a>, element: &Element<'a>) -> Result<Self> {
        tag(element, sequence(), FIELD)?;
        Self::fields(parent, element)
    }

    /// Reads CMS's `[2] IMPLICIT AttributeCertificateV2`, without retagging.
    /// Supply the original certificate-set member context, for example from
    /// [`crate::SignedData::certificate_choice_reader_in`].
    pub fn read_implicit(parent: &Reader<'a>, element: &Element<'a>) -> Result<Self> {
        tag(element, Tag::context(true, 2), FIELD)?;
        Self::fields(parent, element)
    }

    fn fields(parent: &Reader<'a>, element: &Element<'a>) -> Result<Self> {
        let mut inside = parent
            .enter(element)
            .map_err(|e| Error::encoding(e, FIELD))?;
        let signed = required(&mut inside, INFO)?;
        tag(&signed, sequence(), INFO)?;
        // X.509 signatures use the DER encoding of acinfo (RFC5755 §4.1).
        // Rereading the original element checks its header too, with the same
        // depth and execution, rather than normalizing an untrusted encoding.
        let mut der = inside
            .reread(&signed, structured_asn1::Encoding::Der)
            .map_err(|e| Error::encoding(e, INFO))?;
        let signed_der = required(&mut der, INFO)?;
        let information = information(&der, &signed_der)?;
        let encoded_algorithm = required(&mut inside, "attributeCertificate.signatureAlgorithm")?;
        let algorithm = algorithm(
            &inside,
            &encoded_algorithm,
            "attributeCertificate.signatureAlgorithm",
        )?;
        let signature = SignatureValue::read(&mut inside, "attributeCertificate.signatureValue")
            .map_err(Error::pkix)?;
        end(&inside, FIELD)?;
        Ok(Self {
            parser: parent.parser(),
            information,
            algorithm,
            signature,
            signed,
            element: *element,
        })
    }

    /// Parsed signed fields. Their readability is not a profile/trust verdict.
    #[must_use]
    pub fn information(&self) -> &Information<'a> {
        &self.information
    }

    /// Decodes the optional original recipient-targeting extension under the
    /// current operation. `None` means no targeting restriction (RFC5755 §4.3.2);
    /// a present empty set is returned as such and matches no recipient.
    /// Readability alone does not authenticate the AC or its asserted targets.
    pub fn target_information<W: Work>(
        &self,
        work: &W,
    ) -> Result<Option<target::TargetInformation<'a>>> {
        let execution = work.execution_for(self.parser);
        execution
            .charge_work(1)
            .map_err(|e| Error::resource(e, target::FIELD, self.element.span().start))?;
        let Some(extensions) = &self.information.extensions else {
            return Ok(None);
        };
        extensions
            .get(target::IDENTIFIER, &execution)
            .map_err(Error::pkix)?
            .map(|extension| target::TargetInformation::read(extension, &execution))
            .transpose()
    }

    /// Exact whole original acinfo TLV, not a re-encoding of the parsed fields.
    #[must_use]
    pub fn signed_bytes(&self) -> &'a [u8] {
        self.signed.as_bytes()
    }

    /// Original source range of the signed information.
    #[must_use]
    pub fn signed_element(&self) -> Element<'a> {
        self.signed
    }

    /// Outer algorithm, to be checked against the signed algorithm by the provider.
    #[must_use]
    pub fn signature_algorithm(&self) -> AlgorithmIdentifier<'a> {
        self.algorithm
    }

    /// Original signature bits and deferred payload context.
    #[must_use]
    pub fn signature_value(&self) -> SignatureValue<'a> {
        self.signature
    }

    /// Entire original certificate, retaining an IMPLICIT tag when present.
    #[must_use]
    pub fn element(&self) -> Element<'a> {
        self.element
    }
}

fn information<'a>(parent: &Reader<'a>, element: &Element<'a>) -> Result<Information<'a>> {
    let mut inside = parent
        .enter(element)
        .map_err(|e| Error::encoding(e, INFO))?;
    let version = integer(&mut inside, "attributeCertificate.version")?;
    if version.as_u64() != Some(1) {
        return Err(Error::schema(
            ErrorCode::UnsupportedVersion,
            "attributeCertificate.version",
            element.contents_span().start,
        ));
    }
    let holder_element = required(&mut inside, "attributeCertificate.holder")?;
    tag(&holder_element, sequence(), "attributeCertificate.holder")?;
    let holder = holder(&inside, &holder_element)?;
    let issuer_element = required(&mut inside, "attributeCertificate.issuer")?;
    let issuer = match issuer_element.tag() {
        t if t == sequence() => Issuer {
            form: IssuerForm::V1,
            names: Some(names(
                &inside,
                &issuer_element,
                "attributeCertificate.issuer",
            )?),
            base_certificate: None,
            object_digest: None,
        },
        t if t == Tag::context(true, 0) => v2_form(&inside, &issuer_element)?,
        _ => {
            return Err(Error::schema(
                ErrorCode::UnexpectedTag,
                "attributeCertificate.issuer",
                issuer_element.span().start,
            ));
        }
    };
    let signature_element = required(&mut inside, "attributeCertificate.acinfo.signature")?;
    let signature = algorithm(
        &inside,
        &signature_element,
        "attributeCertificate.acinfo.signature",
    )?;
    let serial = integer(&mut inside, "attributeCertificate.serialNumber")?;
    let validity = required(&mut inside, "attributeCertificate.validity")?;
    tag(&validity, sequence(), "attributeCertificate.validity")?;
    let mut times = inside
        .enter(&validity)
        .map_err(|e| Error::encoding(e, "attributeCertificate.validity"))?;
    let not_before = time(&mut times, "attributeCertificate.notBeforeTime")?;
    let not_after = time(&mut times, "attributeCertificate.notAfterTime")?;
    end(&times, "attributeCertificate.validity")?;
    let attribute_element = required(&mut inside, "attributeCertificate.attributes")?;
    let attributes = attributes(&inside, &attribute_element)?;
    let mut next = inside
        .next_element()
        .map_err(|e| Error::encoding(e, INFO))?;
    let issuer_uid = optional_bits(
        &mut inside,
        &mut next,
        "attributeCertificate.issuerUniqueID",
    )?;
    let extensions = match next {
        Some(value) => {
            tag(&value, sequence(), "attributeCertificate.extensions")?;
            Some(
                Extensions::read_element(&inside, &value, "attributeCertificate.extensions")
                    .map_err(Error::pkix)?,
            )
        }
        None => None,
    };
    end(&inside, INFO)?;
    Ok(Information {
        holder,
        issuer,
        signature,
        serial,
        not_before,
        not_after,
        attributes,
        issuer_uid,
        extensions,
    })
}

fn holder<'a>(parent: &Reader<'a>, element: &Element<'a>) -> Result<Holder<'a>> {
    let field = "attributeCertificate.holder";
    let mut inside = parent
        .enter(element)
        .map_err(|e| Error::encoding(e, field))?;
    let mut next = inside
        .next_element()
        .map_err(|e| Error::encoding(e, field))?;
    let base_certificate = optional(&mut inside, &mut next, Tag::context(true, 0), field)?
        .map(|e| issuer_serial(&inside, &e, field))
        .transpose()?;
    let entity_names = optional(&mut inside, &mut next, Tag::context(true, 1), field)?
        .map(|e| names(&inside, &e, field))
        .transpose()?;
    let object_digest = optional(&mut inside, &mut next, Tag::context(true, 2), field)?
        .map(|e| object_digest(&inside, &e, field))
        .transpose()?;
    no_pending(next, field)?;
    Ok(Holder {
        base_certificate,
        entity_names,
        object_digest,
    })
}

fn v2_form<'a>(parent: &Reader<'a>, element: &Element<'a>) -> Result<Issuer<'a>> {
    let field = "attributeCertificate.issuer";
    let mut inside = parent
        .enter(element)
        .map_err(|e| Error::encoding(e, field))?;
    let mut next = inside
        .next_element()
        .map_err(|e| Error::encoding(e, field))?;
    let names = optional(&mut inside, &mut next, sequence(), field)?
        .map(|e| names(&inside, &e, field))
        .transpose()?;
    let base_certificate = optional(&mut inside, &mut next, Tag::context(true, 0), field)?
        .map(|e| issuer_serial(&inside, &e, field))
        .transpose()?;
    let object_digest = optional(&mut inside, &mut next, Tag::context(true, 1), field)?
        .map(|e| object_digest(&inside, &e, field))
        .transpose()?;
    no_pending(next, field)?;
    Ok(Issuer {
        form: IssuerForm::V2,
        names,
        base_certificate,
        object_digest,
    })
}

fn issuer_serial<'a>(
    parent: &ScopedReader<'a, '_>,
    element: &Element<'a>,
    field: &'static str,
) -> Result<IssuerSerial<'a>> {
    let mut inside = parent
        .enter(element)
        .map_err(|e| Error::encoding(e, field))?;
    let name_element = required(&mut inside, field)?;
    tag(&name_element, sequence(), field)?;
    let issuer = names(&inside, &name_element, field)?;
    let serial = integer(&mut inside, field)?;
    let mut next = inside
        .next_element()
        .map_err(|e| Error::encoding(e, field))?;
    let issuer_uid = optional_bits(&mut inside, &mut next, field)?;
    no_pending(next, field)?;
    Ok(IssuerSerial {
        issuer,
        serial,
        issuer_uid,
    })
}

fn object_digest<'a>(
    parent: &ScopedReader<'a, '_>,
    element: &Element<'a>,
    field: &'static str,
) -> Result<ObjectDigestInfo<'a>> {
    let mut inside = parent
        .enter(element)
        .map_err(|e| Error::encoding(e, field))?;
    let object = required(&mut inside, field)?;
    tag(&object, Tag::primitive(10), field)?; // X.690 §8.4: ENUMERATED has INTEGER contents.
    let number = inside
        .integer(&object)
        .map_err(|e| Error::encoding(e, field))?;
    let object_type = match number.as_u64() {
        Some(0) => DigestedObject::PublicKey,
        Some(1) => DigestedObject::PublicKeyCertificate,
        Some(2) => DigestedObject::Other,
        _ => {
            return Err(Error::schema(
                ErrorCode::MalformedValue,
                field,
                object.span().start,
            ));
        }
    };
    let mut next = inside
        .next_element()
        .map_err(|e| Error::encoding(e, field))?;
    let other_type = optional(
        &mut inside,
        &mut next,
        Tag::primitive(universal::OID),
        field,
    )?
    .map(|e| inside.oid(&e).map_err(|e| Error::encoding(e, field)))
    .transpose()?;
    let algorithm_element =
        next.ok_or_else(|| Error::schema(ErrorCode::MissingField, field, inside.offset()))?;
    let algorithm = algorithm(&inside, &algorithm_element, field)?;
    let digest_element = required(&mut inside, field)?;
    tag(
        &digest_element,
        Tag::primitive(universal::BIT_STRING),
        field,
    )?;
    let digest = inside
        .bit_string(&digest_element)
        .map_err(|e| Error::encoding(e, field))?;
    end(&inside, field)?;
    Ok(ObjectDigestInfo {
        object_type,
        other_type,
        algorithm,
        digest,
    })
}

fn names<'a>(
    parent: &ScopedReader<'a, '_>,
    element: &Element<'a>,
    field: &'static str,
) -> Result<OwnedVec<GeneralName<'a>>> {
    let mut inside = parent
        .enter(element)
        .map_err(|e| Error::encoding(e, field))?;
    let execution = parent.execution();
    let mut names = execution.vector();
    while let Some(value) = inside
        .next_element()
        .map_err(|e| Error::encoding(e, field))?
    {
        names
            .try_reserve(1)
            .map_err(|e| Error::resource(e, field, value.span().start))?;
        names
            .try_push(general_name::read(&inside, &value, field).map_err(Error::pkix)?)
            .map_err(|e| Error::resource(e, field, value.span().start))?;
    }
    if names.is_empty() {
        return Err(Error::schema(
            ErrorCode::MissingField,
            field,
            element.contents_span().start,
        ));
    }
    Ok(names.into_owned())
}

fn attributes<'a>(parent: &Reader<'a>, element: &Element<'a>) -> Result<OwnedVec<Attribute<'a>>> {
    const FIELD: &str = "attributeCertificate.attributes";
    tag(element, sequence(), FIELD)?;
    let mut inside = parent
        .enter(element)
        .map_err(|e| Error::encoding(e, FIELD))?;
    let execution = parent.execution();
    let mut attributes = execution.vector();
    let mut order = execution.vector();
    while let Some(value) = inside
        .next_element()
        .map_err(|e| Error::encoding(e, FIELD))?
    {
        attributes
            .try_reserve(1)
            .map_err(|e| Error::resource(e, FIELD, value.span().start))?;
        let attribute = Attribute::read(&inside, &value, FIELD)?;
        if attribute.values().is_empty() {
            return Err(Error::schema(
                ErrorCode::AttributeCardinality,
                FIELD,
                value.span().start,
            ));
        }
        order
            .try_push((attribute.kind(), attribute.span().start))
            .map_err(|e| Error::resource(e, FIELD, value.span().start))?;
        attributes
            .try_push(attribute)
            .map_err(|e| Error::resource(e, FIELD, value.span().start))?;
    }
    if attributes.is_empty() {
        return Err(Error::schema(
            ErrorCode::EmptyAttributes,
            FIELD,
            element.span().start,
        ));
    }
    // RFC5755 §4.2.7 forbids duplicate types but permits multiple values.
    // Sort only a paid index: the original attribute order/bytes never change.
    let refusal = |e| Error::resource(e, FIELD, element.span().start);
    order
        .try_sort_unstable_by(|(left, _), (right, _)| {
            execution.charge_work(left.as_bytes().len().min(right.as_bytes().len()) as u64)?;
            Ok::<_, structured_asn1::memory::MemoryError>(left.as_bytes().cmp(right.as_bytes()))
        })
        .map_err(|e| match e {
            SortError::Resource(e) | SortError::Comparison(e) => refusal(e),
        })?;
    for pair in order.windows(2) {
        execution
            .charge_work(pair[0].0.as_bytes().len().min(pair[1].0.as_bytes().len()) as u64 + 1)
            .map_err(refusal)?;
        if pair[0].0 == pair[1].0 {
            return Err(Error::schema(
                ErrorCode::DuplicateAttribute,
                FIELD,
                pair[1].1,
            ));
        }
    }
    Ok(attributes.into_owned())
}

fn integer<'a>(parent: &mut ScopedReader<'a, '_>, field: &'static str) -> Result<Integer<'a>> {
    let value = parent
        .expect(Tag::primitive(universal::INTEGER))
        .map_err(|e| Error::encoding(e, field))?;
    parent
        .integer(&value)
        .map_err(|e| Error::encoding(e, field))
}

fn time(parent: &mut Reader<'_>, field: &'static str) -> Result<Time> {
    let value = parent
        .expect(Tag::primitive(universal::GENERALIZED_TIME))
        .map_err(|e| Error::encoding(e, field))?;
    Time::read(parent, &value, field).map_err(Error::pkix)
}

fn algorithm<'a>(
    parent: &ScopedReader<'a, '_>,
    element: &Element<'a>,
    field: &'static str,
) -> Result<AlgorithmIdentifier<'a>> {
    structured_pkix::algorithm::read(parent, element, field).map_err(Error::pkix)
}

fn optional<'a>(
    parent: &mut ScopedReader<'a, '_>,
    next: &mut Option<Element<'a>>,
    expected: Tag,
    field: &'static str,
) -> Result<Option<Element<'a>>> {
    if next.as_ref().is_some_and(|e| e.tag() == expected) {
        let selected = next.take();
        *next = parent
            .next_element()
            .map_err(|e| Error::encoding(e, field))?;
        Ok(selected)
    } else {
        Ok(None)
    }
}

fn optional_bits<'a>(
    parent: &mut ScopedReader<'a, '_>,
    next: &mut Option<Element<'a>>,
    field: &'static str,
) -> Result<Option<BitString<'a>>> {
    optional(parent, next, Tag::primitive(universal::BIT_STRING), field)?
        .map(|e| parent.bit_string(&e).map_err(|e| Error::encoding(e, field)))
        .transpose()
}

fn no_pending(next: Option<Element<'_>>, field: &'static str) -> Result<()> {
    match next {
        Some(value) => Err(Error::schema(
            ErrorCode::TrailingData,
            field,
            value.span().start,
        )),
        None => Ok(()),
    }
}

fn tag(element: &Element<'_>, expected: Tag, field: &'static str) -> Result<()> {
    if element.tag() == expected {
        Ok(())
    } else {
        Err(Error::schema(
            ErrorCode::UnexpectedTag,
            field,
            element.span().start,
        ))
    }
}

fn end(parent: &ScopedReader<'_, '_>, field: &'static str) -> Result<()> {
    if parent.is_empty() {
        Ok(())
    } else {
        Err(Error::schema(
            ErrorCode::TrailingData,
            field,
            parent.offset(),
        ))
    }
}