rasn/
de.rs

1//! Generic ASN.1 decoding framework.
2
3use alloc::{boxed::Box, vec::Vec};
4use num_bigint::BigInt;
5
6use crate::error::DecodeError;
7use crate::types::{self, AsnType, Constraints, Enumerated, SetOf, Tag};
8
9pub use nom::Needed;
10pub use rasn_derive::Decode;
11
12/// A **data type** that can decoded from any ASN.1 format.
13pub trait Decode: Sized + AsnType {
14    /// Decode this value from a given ASN.1 decoder.
15    ///
16    /// **Note for implementors** You typically do not need to implement this.
17    /// The default implementation will call [`Decode::decode_with_tag_and_constraints`] with
18    /// your types associated [`AsnType::TAG`]. You should only ever need to
19    /// implement this if you have a type that *cannot* be implicitly tagged,
20    /// such as a `CHOICE` type, which case you want to implement the decoding
21    /// in `decode`.
22    fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, D::Error> {
23        Self::decode_with_tag(decoder, Self::TAG)
24    }
25
26    /// Decode this value implicitly tagged with `tag` from a given ASN.1 decoder.
27    ///
28    /// **Note** For `CHOICE` and other types that cannot be implicitly tagged
29    /// this will **explicitly tag** the value, for all other types, it will
30    /// **implicitly** tag the value.
31    fn decode_with_tag<D: Decoder>(decoder: &mut D, tag: Tag) -> Result<Self, D::Error> {
32        Self::decode_with_tag_and_constraints(decoder, tag, Self::CONSTRAINTS)
33    }
34
35    /// Decode this value from a given ASN.1 decoder with a set of constraints
36    /// on what values of that type are allowed.
37    ///
38    /// **Note for implementors** You typically do not need to implement this.
39    /// The default implementation will call [`Decode::decode_with_tag_and_constraints`] with
40    /// your types associated [`AsnType::TAG`] and [`AsnType::CONSTRAINTS`].
41    fn decode_with_constraints<D: Decoder>(
42        decoder: &mut D,
43        constraints: Constraints,
44    ) -> Result<Self, D::Error> {
45        Self::decode_with_tag_and_constraints(decoder, Self::TAG, constraints)
46    }
47
48    /// Decode this value implicitly tagged with `tag` from a given ASN.1
49    /// decoder with a set of constraints on what values of that type are allowed.
50    ///
51    /// **Note** For `CHOICE` and other types that cannot be implicitly tagged
52    /// this will **explicitly tag** the value, for all other types, it will
53    /// **implicitly** tag the value.
54    fn decode_with_tag_and_constraints<D: Decoder>(
55        decoder: &mut D,
56        tag: Tag,
57        constraints: Constraints,
58    ) -> Result<Self, D::Error>;
59}
60
61/// A **data format** decode any ASN.1 data type.
62///
63/// Const `RCL` is the count of root components in the root component list of a sequence or set.
64/// Const `ECL` is the count of extension additions in the extension addition component type list in a sequence or set.
65pub trait Decoder<const RCL: usize = 0, const ECL: usize = 0>: Sized {
66    /// The associated success type returned on success.
67    type Ok;
68    /// The associated error type returned on failure.
69    type Error: Error + Into<crate::error::DecodeError> + From<crate::error::DecodeError>;
70    /// Helper type for decoding nested instances of `Decoder` with different fields.
71    type AnyDecoder<const R: usize, const E: usize>: Decoder<RCL, ECL, Ok = Self::Ok, Error = Self::Error>
72        + Decoder;
73
74    /// Returns codec variant of `Codec` that current decoder is decoding.
75    #[must_use]
76    fn codec(&self) -> crate::Codec;
77
78    /// Decode an unknown ASN.1 value identified by `tag` from the available input.
79    fn decode_any(&mut self) -> Result<types::Any, Self::Error>;
80    /// Decode a `BIT STRING` identified by `tag` from the available input.
81    fn decode_bit_string(
82        &mut self,
83        tag: Tag,
84        constraints: Constraints,
85    ) -> Result<types::BitString, Self::Error>;
86    /// Decode a `BOOL` identified by `tag` from the available input.
87    fn decode_bool(&mut self, tag: Tag) -> Result<bool, Self::Error>;
88    /// Decode an enumerated enum's discriminant identified by `tag` from the available input.
89    fn decode_enumerated<E: Enumerated>(&mut self, tag: Tag) -> Result<E, Self::Error>;
90    /// Decode a `INTEGER` identified by `tag` from the available input.
91    fn decode_integer<I: types::IntegerType>(
92        &mut self,
93        tag: Tag,
94        constraints: Constraints,
95    ) -> Result<I, Self::Error>;
96
97    /// Decode a `REAL` identified by `tag` from the available input.
98    fn decode_real<R: types::RealType>(
99        &mut self,
100        tag: Tag,
101        constraints: Constraints,
102    ) -> Result<R, Self::Error>;
103
104    /// Decode `NULL` identified by `tag` from the available input.
105    fn decode_null(&mut self, tag: Tag) -> Result<(), Self::Error>;
106    /// Decode a `OBJECT IDENTIFIER` identified by `tag` from the available input.
107    fn decode_object_identifier(
108        &mut self,
109        tag: Tag,
110    ) -> Result<types::ObjectIdentifier, Self::Error>;
111    /// Decode a `SEQUENCE` identified by `tag` from the available input. Returning
112    /// a new `Decoder` containing the sequence's contents to be decoded.
113    ///
114    /// Const `RC` is the count of root components in a sequence.
115    /// Const `EC` is the count of extension addition components in a sequence.
116    /// Generic `D` is the sequence type.
117    /// Generic `DF` is the closure that will initialize the sequence with default values, typically when no values are present.
118    /// Generic `F` is the closure that will decode the sequence by decoding the fields in the order as defined in the type.
119    /// NOTE: If you implement this manually, make sure to decode fields in the same order and pass the correct count of fields.
120    fn decode_sequence<const RC: usize, const EC: usize, D, DF, F>(
121        &mut self,
122        tag: Tag,
123        default_initializer_fn: Option<DF>,
124        decode_fn: F,
125    ) -> Result<D, Self::Error>
126    where
127        D: crate::types::Constructed<RC, EC>,
128        DF: FnOnce() -> D,
129        F: FnOnce(&mut Self::AnyDecoder<RC, EC>) -> Result<D, Self::Error>;
130    /// Decode a `SEQUENCE OF D` where `D: Decode` identified by `tag` from the available input.
131    fn decode_sequence_of<D: Decode>(
132        &mut self,
133        tag: Tag,
134        constraints: Constraints,
135    ) -> Result<Vec<D>, Self::Error>;
136    /// Decode a `SET OF D` where `D: Decode` identified by `tag` from the available input.
137    fn decode_set_of<D: Decode + Eq + core::hash::Hash>(
138        &mut self,
139        tag: Tag,
140        constraints: Constraints,
141    ) -> Result<types::SetOf<D>, Self::Error>;
142    /// Decode a `OCTET STRING` identified by `tag` from the available input.
143    fn decode_octet_string<'buf, T>(
144        &'buf mut self,
145        tag: Tag,
146        constraints: Constraints,
147    ) -> Result<T, Self::Error>
148    where
149        T: From<&'buf [u8]> + From<Vec<u8>>;
150
151    /// Decode a `UTF8 STRING` identified by `tag` from the available input.
152    fn decode_utf8_string(
153        &mut self,
154        tag: Tag,
155        constraints: Constraints,
156    ) -> Result<types::Utf8String, Self::Error>;
157
158    /// Decode a `VisibleString` identified by `tag` from the available input.
159    fn decode_visible_string(
160        &mut self,
161        tag: Tag,
162        constraints: Constraints,
163    ) -> Result<types::VisibleString, Self::Error>;
164
165    /// Decode a `GeneralString` identified by `tag` from the available input.
166    fn decode_general_string(
167        &mut self,
168        tag: Tag,
169        constraints: Constraints,
170    ) -> Result<types::GeneralString, Self::Error>;
171
172    /// Decode a `GraphicString` identified by `tag` from the available input.
173    fn decode_graphic_string(
174        &mut self,
175        tag: Tag,
176        constraints: Constraints,
177    ) -> Result<types::GraphicString, Self::Error>;
178
179    /// Decode a `Ia5String` identified by `tag` from the available input.
180    fn decode_ia5_string(
181        &mut self,
182        tag: Tag,
183        constraints: Constraints,
184    ) -> Result<types::Ia5String, Self::Error>;
185
186    /// Decode a `PrintableString` identified by `tag` from the available input.
187    fn decode_printable_string(
188        &mut self,
189        tag: Tag,
190        constraints: Constraints,
191    ) -> Result<types::PrintableString, Self::Error>;
192
193    /// Decode a `NumericString` identified by `tag` from the available input.
194    fn decode_numeric_string(
195        &mut self,
196        tag: Tag,
197        constraints: Constraints,
198    ) -> Result<types::NumericString, Self::Error>;
199
200    /// Decode a `TeletexString` identified by `tag` from the available input.
201    fn decode_teletex_string(
202        &mut self,
203        tag: Tag,
204        constraints: Constraints,
205    ) -> Result<types::TeletexString, Self::Error>;
206
207    /// Decode a `BmpString` identified by `tag` from the available input.
208    fn decode_bmp_string(
209        &mut self,
210        tag: Tag,
211        constraints: Constraints,
212    ) -> Result<types::BmpString, Self::Error>;
213
214    /// Decode an ASN.1 value that has been explicitly prefixed with `tag` from the available input.
215    fn decode_explicit_prefix<D: Decode>(&mut self, tag: Tag) -> Result<D, Self::Error>;
216    /// Decode an optional ASN.1 type that has been explicitly prefixed with `tag` from the available input.
217    fn decode_optional_with_explicit_prefix<D: Decode>(
218        &mut self,
219        tag: Tag,
220    ) -> Result<Option<D>, Self::Error>;
221    /// Decode a `UtcTime` identified by `tag` from the available input.
222    fn decode_utc_time(&mut self, tag: Tag) -> Result<types::UtcTime, Self::Error>;
223    /// Decode a `GeneralizedTime` identified by `tag` from the available input.
224    fn decode_generalized_time(&mut self, tag: Tag) -> Result<types::GeneralizedTime, Self::Error>;
225    /// Decode a 'DATE' identified by 'tag' from the available input
226    fn decode_date(&mut self, tag: Tag) -> Result<types::Date, Self::Error>;
227
228    /// Decode a `SET` identified by `tag` from the available input. Decoding
229    /// `SET`s works a little different than other methods, as you need to
230    /// provide two types `SET` and `FIELDS`, `SET` represents the complete type,
231    /// and `FIELDS` must represent a `CHOICE` with a variant for each field
232    /// from `SET`. As with `SET`s the field order is not guarenteed, so you'll
233    /// have map from `Vec<FIELDS>` to `SET` in `decode_operation`.
234    ///
235    /// Const `RC` is the count of root components in a sequence.
236    /// Const `EC` is the count of extension addition components in a sequence.
237    /// Generic `FIELDS` is the choice type, used by `F` to map the decoded field values correctly.
238    /// Generic `SET` is the set type.
239    /// Generic `D` is the closure that will decode the set by decoding the fields in the order as defined in the type.
240    /// Generic `F` is the closure that will map the `FIELDS` to the set.
241    fn decode_set<const RC: usize, const EC: usize, FIELDS, SET, D, F>(
242        &mut self,
243        tag: Tag,
244        decode_fn: D,
245        field_fn: F,
246    ) -> Result<SET, Self::Error>
247    where
248        SET: Decode + crate::types::Constructed<RC, EC>,
249        FIELDS: Decode,
250        D: Fn(&mut Self::AnyDecoder<RC, EC>, usize, Tag) -> Result<FIELDS, Self::Error>,
251        F: FnOnce(Vec<FIELDS>) -> Result<SET, Self::Error>;
252
253    /// Decode an the optional value in a `SEQUENCE` or `SET`.
254    fn decode_choice<D>(&mut self, constraints: Constraints) -> Result<D, Self::Error>
255    where
256        D: crate::types::DecodeChoice;
257
258    /// Decode an the optional value in a `SEQUENCE` or `SET`.
259    fn decode_optional<D: Decode>(&mut self) -> Result<Option<D>, Self::Error>;
260
261    /// Decode an the optional value in a `SEQUENCE` or `SET` with `tag`.
262    /// Passing the correct tag is required even when used with codecs where
263    /// the tag is not present.
264    fn decode_optional_with_tag<D: Decode>(&mut self, tag: Tag) -> Result<Option<D>, Self::Error>;
265
266    /// Decode an the optional value in a `SEQUENCE` or `SET` with `constraints`.
267    fn decode_optional_with_constraints<D: Decode>(
268        &mut self,
269        constraints: Constraints,
270    ) -> Result<Option<D>, Self::Error>;
271
272    /// Decode an the optional value in a `SEQUENCE` or `SET` with `tag`
273    /// and `constraints`.
274    fn decode_optional_with_tag_and_constraints<D: Decode>(
275        &mut self,
276        tag: Tag,
277        constraints: Constraints,
278    ) -> Result<Option<D>, Self::Error>;
279
280    /// Decode a `DEFAULT` value in a `SEQUENCE` or `SET`.
281    fn decode_default<D: Decode, F: FnOnce() -> D>(
282        &mut self,
283        default_fn: F,
284    ) -> Result<D, Self::Error> {
285        self.decode_default_with_tag(D::TAG, default_fn)
286    }
287
288    /// Decode a `DEFAULT` value in a `SEQUENCE` or `SET` with `tag` and `default_fn`.
289    fn decode_default_with_tag<D: Decode, F: FnOnce() -> D>(
290        &mut self,
291        tag: Tag,
292        default_fn: F,
293    ) -> Result<D, Self::Error> {
294        Ok(self
295            .decode_optional_with_tag::<D>(tag)?
296            .unwrap_or_else(default_fn))
297    }
298
299    /// Decode a `DEFAULT` value with constraints in a `SEQUENCE` or `SET` with a given `default_fn`.
300    fn decode_default_with_constraints<D: Decode, F: FnOnce() -> D>(
301        &mut self,
302        default_fn: F,
303        constraints: Constraints,
304    ) -> Result<D, Self::Error> {
305        Ok(self
306            .decode_optional_with_constraints::<D>(constraints)?
307            .unwrap_or_else(default_fn))
308    }
309
310    /// Decode a `DEFAULT` value in a `SEQUENCE` or `SET` with `tag`, `constraints` and `default_fn`.
311    fn decode_default_with_tag_and_constraints<D: Decode, F: FnOnce() -> D>(
312        &mut self,
313        tag: Tag,
314        default_fn: F,
315        constraints: Constraints,
316    ) -> Result<D, Self::Error> {
317        Ok(self
318            .decode_optional_with_tag_and_constraints::<D>(tag, constraints)?
319            .unwrap_or_else(default_fn))
320    }
321
322    /// Decode an extension addition value in a `SEQUENCE` or `SET`.
323    fn decode_extension_addition<D>(&mut self) -> Result<Option<D>, Self::Error>
324    where
325        D: Decode,
326    {
327        self.decode_extension_addition_with_constraints(Constraints::default())
328    }
329    /// Decode an extension addition with explicit tag in a `SEQUENCE` or `SET`.
330    fn decode_extension_addition_with_explicit_tag_and_constraints<D>(
331        &mut self,
332        tag: Tag,
333        constraints: Constraints,
334    ) -> Result<Option<D>, Self::Error>
335    where
336        D: Decode;
337
338    /// Decode an extension addition value with tag in a `SEQUENCE` or `SET`.
339    fn decode_extension_addition_with_tag<D>(&mut self, tag: Tag) -> Result<Option<D>, Self::Error>
340    where
341        D: Decode,
342    {
343        self.decode_extension_addition_with_tag_and_constraints(tag, Constraints::default())
344    }
345    /// Decode an extension addition with constraints in a `SEQUENCE` or `SET`
346    fn decode_extension_addition_with_constraints<D>(
347        &mut self,
348        constraints: Constraints,
349    ) -> Result<Option<D>, Self::Error>
350    where
351        D: Decode,
352    {
353        self.decode_extension_addition_with_tag_and_constraints(D::TAG, constraints)
354    }
355    /// Decode a extension addition value with tag and constraints in a `SEQUENCE` or `SET`.
356    fn decode_extension_addition_with_tag_and_constraints<D>(
357        &mut self,
358        tag: Tag,
359        constraints: Constraints,
360    ) -> Result<Option<D>, Self::Error>
361    where
362        D: Decode;
363
364    /// Decode a `DEFAULT` value in a `SEQUENCE`'s or `SET`'s extension
365    fn decode_extension_addition_with_default<D: Decode, F: FnOnce() -> D>(
366        &mut self,
367        default_fn: F,
368    ) -> Result<D, Self::Error> {
369        self.decode_extension_addition_with_default_and_constraints(
370            default_fn,
371            Constraints::default(),
372        )
373    }
374    /// Decode a `DEFAULT` value with tag in a `SEQUENCE`'s or `SET`'s extension
375    fn decode_extension_addition_with_default_and_tag<D: Decode, F: FnOnce() -> D>(
376        &mut self,
377        tag: Tag,
378        default_fn: F,
379    ) -> Result<D, Self::Error> {
380        self.decode_extension_addition_with_default_and_tag_and_constraints::<D, F>(
381            tag,
382            default_fn,
383            Constraints::default(),
384        )
385    }
386
387    /// Decode a `DEFAULT` value with constraints in a `SEQUENCE`'s or `SET`'s extension
388    fn decode_extension_addition_with_default_and_constraints<D: Decode, F: FnOnce() -> D>(
389        &mut self,
390        default_fn: F,
391        constraints: Constraints,
392    ) -> Result<D, Self::Error> {
393        Ok(self
394            .decode_extension_addition_with_constraints::<D>(constraints)?
395            .unwrap_or_else(default_fn))
396    }
397    /// Decode a `DEFAULT` value with tag and constraints in a `SEQUENCE`'s or `SET`'s extension
398    fn decode_extension_addition_with_default_and_tag_and_constraints<
399        D: Decode,
400        F: FnOnce() -> D,
401    >(
402        &mut self,
403        tag: Tag,
404        default_fn: F,
405        constraints: Constraints,
406    ) -> Result<D, Self::Error> {
407        Ok(self
408            .decode_extension_addition_with_tag_and_constraints::<D>(tag, constraints)?
409            .unwrap_or_else(default_fn))
410    }
411
412    /// Decode a extension addition group in a `SEQUENCE` or `SET`.
413    ///
414    /// Const `RC` is the count of root components in a sequence.
415    /// Const `EC` is the count of extension addition components in a sequence.
416    /// Generic `D` is the type of the extension addition group.
417    fn decode_extension_addition_group<
418        const RC: usize,
419        const EC: usize,
420        D: Decode + crate::types::Constructed<RC, EC>,
421    >(
422        &mut self,
423    ) -> Result<Option<D>, Self::Error>;
424}
425
426/// A generic error that can occur while decoding ASN.1.
427/// Caller needs always to pass a `crate::Codec` variant to `Error` when implementing the decoder
428pub trait Error: core::fmt::Display {
429    /// Creates a new general error using `msg` when decoding ASN.1.
430    #[must_use]
431    fn custom<D: core::fmt::Display>(msg: D, codec: crate::Codec) -> Self;
432    /// Creates a new error about needing more data to finish parsing.
433    #[must_use]
434    fn incomplete(needed: Needed, codec: crate::Codec) -> Self;
435    /// Creates a new error about exceeding the maximum allowed data for a type.
436    #[must_use]
437    fn exceeds_max_length(length: num_bigint::BigUint, codec: crate::Codec) -> Self;
438    /// Creates a new error about a missing field.
439    #[must_use]
440    fn missing_field(name: &'static str, codec: crate::Codec) -> Self;
441    /// Creates a new error about being unable to match any variant in a choice.
442    #[must_use]
443    fn no_valid_choice(name: &'static str, codec: crate::Codec) -> Self;
444    /// Creates a new error about being unable to decode a field in a compound
445    /// type, such as a set or sequence.
446    #[must_use]
447    fn field_error(name: &'static str, error: DecodeError, codec: crate::Codec) -> Self;
448    /// Creates a new error about finding a duplicate field.
449    #[must_use]
450    fn duplicate_field(name: &'static str, codec: crate::Codec) -> Self;
451    /// Create a new error about unknown field.
452    #[must_use]
453    fn unknown_field(index: usize, tag: Tag, codec: crate::Codec) -> Self;
454}
455
456impl Decode for () {
457    fn decode_with_tag_and_constraints<D: Decoder>(
458        decoder: &mut D,
459        tag: Tag,
460        _: Constraints,
461    ) -> Result<Self, D::Error> {
462        decoder.decode_null(tag)
463    }
464}
465
466impl<D: Decode> Decode for Option<D> {
467    fn decode<DE: Decoder>(decoder: &mut DE) -> Result<Self, DE::Error> {
468        decoder.decode_optional()
469    }
470
471    fn decode_with_tag<DE: Decoder>(decoder: &mut DE, tag: Tag) -> Result<Self, DE::Error> {
472        decoder.decode_optional_with_tag(tag)
473    }
474
475    fn decode_with_constraints<DE: Decoder>(
476        decoder: &mut DE,
477        constraints: Constraints,
478    ) -> Result<Self, DE::Error> {
479        decoder.decode_optional_with_constraints(constraints)
480    }
481
482    fn decode_with_tag_and_constraints<DE: Decoder>(
483        decoder: &mut DE,
484        tag: Tag,
485        constraints: Constraints,
486    ) -> Result<Self, DE::Error> {
487        decoder.decode_optional_with_tag_and_constraints(tag, constraints)
488    }
489}
490
491impl Decode for bool {
492    fn decode_with_tag_and_constraints<D: Decoder>(
493        decoder: &mut D,
494        tag: Tag,
495        _: Constraints,
496    ) -> Result<Self, D::Error> {
497        decoder.decode_bool(tag)
498    }
499}
500
501macro_rules! impl_integers {
502    ($($int:ty),+ $(,)?) => {
503        $(
504        impl Decode for $int {
505            fn decode_with_tag_and_constraints<D: Decoder>(decoder: &mut D, tag: Tag, constraints: Constraints) -> Result<Self, D::Error> {
506                decoder.decode_integer::<$int>(tag, constraints)
507            }
508        }
509        )+
510    }
511}
512
513impl_integers! {
514    i8,
515    i16,
516    i32,
517    i64,
518    i128,
519    isize,
520    u8,
521    u16,
522    u32,
523    u64,
524    // TODO cannot support u128 as it is constrained type by default and current constraints uses i128 for bounds
525    // u128,
526    usize,
527    BigInt
528}
529
530impl<const START: i128, const END: i128> Decode for types::ConstrainedInteger<START, END> {
531    fn decode_with_tag_and_constraints<D: Decoder>(
532        decoder: &mut D,
533        tag: Tag,
534        constraints: Constraints,
535    ) -> Result<Self, D::Error> {
536        decoder
537            .decode_integer::<types::Integer>(tag, constraints)
538            .map(Self)
539    }
540}
541
542impl Decode for types::Integer {
543    fn decode_with_tag_and_constraints<D: Decoder>(
544        decoder: &mut D,
545        tag: Tag,
546        constraints: Constraints,
547    ) -> Result<Self, D::Error> {
548        decoder.decode_integer::<types::Integer>(tag, constraints)
549    }
550}
551
552#[cfg(feature = "f32")]
553impl Decode for f32 {
554    fn decode_with_tag_and_constraints<D: Decoder>(
555        decoder: &mut D,
556        tag: Tag,
557        _: Constraints,
558    ) -> Result<Self, D::Error> {
559        decoder.decode_real::<f32>(tag, Constraints::default())
560    }
561}
562
563#[cfg(feature = "f64")]
564impl Decode for f64 {
565    fn decode_with_tag_and_constraints<D: Decoder>(
566        decoder: &mut D,
567        tag: Tag,
568        _: Constraints,
569    ) -> Result<Self, D::Error> {
570        decoder.decode_real::<f64>(tag, Constraints::default())
571    }
572}
573
574impl<T: Decode> Decode for Box<T> {
575    fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, D::Error> {
576        T::decode(decoder).map(Box::new)
577    }
578
579    fn decode_with_tag<D: Decoder>(decoder: &mut D, tag: Tag) -> Result<Self, D::Error> {
580        T::decode_with_tag(decoder, tag).map(Box::new)
581    }
582
583    fn decode_with_constraints<DE: Decoder>(
584        decoder: &mut DE,
585        constraints: Constraints,
586    ) -> Result<Self, DE::Error> {
587        T::decode_with_constraints(decoder, constraints).map(Box::new)
588    }
589
590    fn decode_with_tag_and_constraints<DE: Decoder>(
591        decoder: &mut DE,
592        tag: Tag,
593        constraints: Constraints,
594    ) -> Result<Self, DE::Error> {
595        T::decode_with_tag_and_constraints(decoder, tag, constraints).map(Box::new)
596    }
597}
598
599impl Decode for types::OctetString {
600    fn decode_with_tag_and_constraints<D: Decoder>(
601        decoder: &mut D,
602        tag: Tag,
603        constraints: Constraints,
604    ) -> Result<Self, D::Error> {
605        decoder
606            .decode_octet_string::<Vec<u8>>(tag, constraints)
607            .map(Into::into)
608    }
609}
610
611impl Decode for types::ObjectIdentifier {
612    fn decode_with_tag_and_constraints<D: Decoder>(
613        decoder: &mut D,
614        tag: Tag,
615        _: Constraints,
616    ) -> Result<Self, D::Error> {
617        decoder.decode_object_identifier(tag)
618    }
619}
620
621impl Decode for types::Utf8String {
622    fn decode_with_tag_and_constraints<D: Decoder>(
623        decoder: &mut D,
624        tag: Tag,
625        constraints: Constraints,
626    ) -> Result<Self, D::Error> {
627        decoder.decode_utf8_string(tag, constraints)
628    }
629}
630
631impl Decode for types::UtcTime {
632    fn decode_with_tag_and_constraints<D: Decoder>(
633        decoder: &mut D,
634        tag: Tag,
635        _: Constraints,
636    ) -> Result<Self, D::Error> {
637        decoder.decode_utc_time(tag)
638    }
639}
640
641impl Decode for types::GeneralizedTime {
642    fn decode_with_tag_and_constraints<D: Decoder>(
643        decoder: &mut D,
644        tag: Tag,
645        _: Constraints,
646    ) -> Result<Self, D::Error> {
647        decoder.decode_generalized_time(tag)
648    }
649}
650
651impl Decode for types::Any {
652    fn decode_with_tag_and_constraints<D: Decoder>(
653        decoder: &mut D,
654        _: Tag,
655        _: Constraints,
656    ) -> Result<Self, D::Error> {
657        decoder.decode_any()
658    }
659}
660
661impl<T: Decode> Decode for alloc::vec::Vec<T> {
662    fn decode_with_tag_and_constraints<D: Decoder>(
663        decoder: &mut D,
664        tag: Tag,
665        constraints: Constraints,
666    ) -> Result<Self, D::Error> {
667        decoder.decode_sequence_of(tag, constraints)
668    }
669}
670
671impl<T: Decode + Eq + core::hash::Hash> Decode for SetOf<T> {
672    fn decode_with_tag_and_constraints<D: Decoder>(
673        decoder: &mut D,
674        tag: Tag,
675        constraints: Constraints,
676    ) -> Result<Self, D::Error> {
677        decoder.decode_set_of(tag, constraints)
678    }
679}
680
681impl<T: Decode, const N: usize> Decode for [T; N] {
682    fn decode_with_tag_and_constraints<D: Decoder>(
683        decoder: &mut D,
684        tag: Tag,
685        constraints: Constraints,
686    ) -> Result<Self, D::Error> {
687        let sequence = decoder.decode_sequence_of(tag, constraints)?;
688        sequence.try_into().map_err(|seq: Vec<_>| {
689            D::Error::from(DecodeError::incorrect_item_number_in_sequence(
690                N,
691                seq.len(),
692                decoder.codec(),
693            ))
694        })
695    }
696}
697
698impl<T: AsnType, V: Decode> Decode for types::Implicit<T, V> {
699    fn decode_with_tag_and_constraints<D: Decoder>(
700        decoder: &mut D,
701        tag: Tag,
702        constraints: Constraints,
703    ) -> Result<Self, D::Error> {
704        Ok(Self::new(V::decode_with_tag_and_constraints(
705            decoder,
706            tag,
707            constraints,
708        )?))
709    }
710}
711
712impl<T: AsnType, V: Decode> Decode for types::Explicit<T, V> {
713    fn decode_with_tag_and_constraints<D: Decoder>(
714        decoder: &mut D,
715        tag: Tag,
716        _: Constraints,
717    ) -> Result<Self, D::Error> {
718        Ok(Self::new(decoder.decode_explicit_prefix(tag)?))
719    }
720}