1 use crate::ber::BerSize;
2 use crate::der::*;
3 use crate::error::*;
4 use nom::bytes::complete::take;
5 use nom::combinator::{all_consuming, complete, cut, map};
6 use nom::error::ParseError;
7 use nom::multi::many0;
8 use nom::{Err, IResult};
9 
10 /// Parse a SEQUENCE OF object
11 ///
12 /// Given a subparser for a DER type, parse a sequence of identical objects.
13 ///
14 /// ```rust
15 /// # use der_parser::der::{parse_der_integer, parse_der_sequence_of, DerObject};
16 /// # use der_parser::error::BerResult;
17 /// #
18 /// /// Read a SEQUENCE OF INTEGER
19 /// fn parser(i:&[u8]) -> BerResult<DerObject> {
20 ///     parse_der_sequence_of(parse_der_integer)(i)
21 /// }
22 ///
23 /// # let empty = &b""[..];
24 /// # let bytes = [ 0x30, 0x0a,
25 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
26 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
27 /// # ];
28 /// # let expected  = DerObject::from_seq(vec![
29 /// #     DerObject::from_int_slice(b"\x01\x00\x01"),
30 /// #     DerObject::from_int_slice(b"\x01\x00\x00"),
31 /// # ]);
32 /// # assert_eq!(parser(&bytes), Ok((empty, expected)));
33 /// let (rem, v) = parser(&bytes).expect("parsing failed");
34 /// ```
parse_der_sequence_of<'a, F>(f: F) -> impl FnMut(&'a [u8]) -> BerResult where F: Fn(&'a [u8]) -> BerResult,35 pub fn parse_der_sequence_of<'a, F>(f: F) -> impl FnMut(&'a [u8]) -> BerResult
36 where
37     F: Fn(&'a [u8]) -> BerResult,
38 {
39     map(parse_der_sequence_of_v(f), DerObject::from_seq)
40 }
41 
42 /// Parse a SEQUENCE OF object (returning a vec)
43 ///
44 /// Given a subparser for a DER type, parse a sequence of identical objects.
45 ///
46 /// This differs from `parse_der_sequence_of` in the parse function and return type.
47 ///
48 /// ```rust
49 /// # use der_parser::der::{parse_der_integer, parse_der_sequence_of_v, DerObject};
50 /// # use der_parser::error::BerResult;
51 /// #
52 /// /// Read a SEQUENCE OF INTEGER
53 /// fn parser(i:&[u8]) -> BerResult<Vec<DerObject>> {
54 ///     parse_der_sequence_of_v(parse_der_integer)(i)
55 /// }
56 ///
57 /// # let empty = &b""[..];
58 /// # let bytes = [ 0x30, 0x0a,
59 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
60 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
61 /// # ];
62 /// # let expected  = vec![
63 /// #     DerObject::from_int_slice(b"\x01\x00\x01"),
64 /// #     DerObject::from_int_slice(b"\x01\x00\x00"),
65 /// # ];
66 /// let (rem, v) = parser(&bytes).expect("parsing failed");
67 /// # assert_eq!(v, expected);
68 /// ```
parse_der_sequence_of_v<'a, T, F, E>( f: F, ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Vec<T>, E> where F: FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>, E: ParseError<&'a [u8]> + From<BerError>,69 pub fn parse_der_sequence_of_v<'a, T, F, E>(
70     f: F,
71 ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Vec<T>, E>
72 where
73     F: FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>,
74     E: ParseError<&'a [u8]> + From<BerError>,
75 {
76     let mut subparser = all_consuming(many0(complete(cut(f))));
77     parse_der_sequence_defined_g(move |data, _| subparser(data))
78 }
79 
80 /// Parse a defined sequence of DER elements (function version)
81 ///
82 /// Given a list of expected parsers, apply them to build a DER sequence and
83 /// return the remaining bytes and the built object.
84 ///
85 /// The remaining bytes point *after* the sequence: any bytes that are part of the sequence but not
86 /// parsed are ignored.
87 ///
88 /// The object header is not available to the parsing function, and the returned type is always a
89 /// `DerObject`.
90 /// For a generic version, see
91 /// [`parse_der_sequence_defined_g`](fn.parse_der_sequence_defined_g.html).
92 ///
93 /// # Examples
94 ///
95 /// Parsing a sequence of identical types (same as `parse_der_sequence_of`):
96 ///
97 /// ```rust
98 /// # use der_parser::der::{parse_der_integer, parse_der_sequence_defined, DerObject};
99 /// # use der_parser::error::BerResult;
100 /// use nom::combinator::complete;
101 /// use nom::multi::many1;
102 ///
103 /// fn localparse_seq(i:&[u8]) -> BerResult {
104 ///     parse_der_sequence_defined(
105 ///         many1(complete(parse_der_integer))
106 ///     )(i)
107 /// }
108 ///
109 /// # let empty = &b""[..];
110 /// # let bytes = [ 0x30, 0x0a,
111 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
112 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
113 /// # ];
114 /// # let expected  = DerObject::from_seq(vec![
115 /// #     DerObject::from_int_slice(b"\x01\x00\x01"),
116 /// #     DerObject::from_int_slice(b"\x01\x00\x00"),
117 /// # ]);
118 /// # assert_eq!(localparse_seq(&bytes), Ok((empty, expected)));
119 /// let (rem, v) = localparse_seq(&bytes).expect("parsing failed");
120 /// ```
121 ///
122 /// Parsing a defined sequence with different types:
123 ///
124 /// ```rust
125 /// # use der_parser::der::*;
126 /// # use der_parser::error::BerResult;
127 /// use nom::combinator::map;
128 /// use nom::sequence::tuple;
129 ///
130 /// /// Read a DER-encoded object:
131 /// /// SEQUENCE {
132 /// ///     a INTEGER,
133 /// ///     b OCTETSTRING
134 /// /// }
135 /// fn localparse_seq(i:&[u8]) -> BerResult {
136 ///     parse_der_sequence_defined(
137 ///         // the nom `tuple` combinator returns a tuple, so we have to map it
138 ///         // to a list
139 ///         map(
140 ///             tuple((parse_der_integer, parse_der_octetstring)),
141 ///             |(a, b)| vec![a, b]
142 ///         )
143 ///     )(i)
144 /// }
145 ///
146 /// # let empty = &b""[..];
147 /// # let bytes = [ 0x30, 0x0a,
148 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
149 /// #               0x04, 0x03, 0x01, 0x00, 0x00,
150 /// # ];
151 /// # let expected  = DerObject::from_seq(vec![
152 /// #     DerObject::from_int_slice(b"\x01\x00\x01"),
153 /// #     DerObject::from_obj(DerObjectContent::OctetString(b"\x01\x00\x00")),
154 /// # ]);
155 /// # assert_eq!(localparse_seq(&bytes), Ok((empty, expected)));
156 /// let (rem, v) = localparse_seq(&bytes).expect("parsing failed");
157 /// ```
parse_der_sequence_defined<'a, F>(mut f: F) -> impl FnMut(&'a [u8]) -> BerResult where F: FnMut(&'a [u8]) -> BerResult<Vec<DerObject>>,158 pub fn parse_der_sequence_defined<'a, F>(mut f: F) -> impl FnMut(&'a [u8]) -> BerResult
159 where
160     F: FnMut(&'a [u8]) -> BerResult<Vec<DerObject>>,
161 {
162     map(
163         parse_der_sequence_defined_g(move |data, _| f(data)),
164         DerObject::from_seq,
165     )
166 }
167 
168 /// Parse a defined SEQUENCE object (generic function)
169 ///
170 /// Given a parser for sequence content, apply it to build a DER sequence and
171 /// return the remaining bytes and the built object.
172 ///
173 /// The remaining bytes point *after* the sequence: any bytes that are part of the sequence but not
174 /// parsed are ignored.
175 ///
176 /// Unlike `parse_der_sequence_defined`, this function allows returning any object or error type,
177 /// and also passes the object header to the callback.
178 ///
179 /// # Examples
180 ///
181 /// Parsing a defined sequence with different types:
182 ///
183 /// ```rust
184 /// # use der_parser::der::*;
185 /// # use der_parser::error::BerResult;
186 /// #
187 /// # #[derive(Debug, PartialEq)]
188 /// pub struct MyObject<'a> {
189 ///     a: u32,
190 ///     b: &'a [u8],
191 /// }
192 ///
193 /// /// Read a DER-encoded object:
194 /// /// SEQUENCE {
195 /// ///     a INTEGER (0..4294967295),
196 /// ///     b OCTETSTRING
197 /// /// }
198 /// fn parse_myobject(i: &[u8]) -> BerResult<MyObject> {
199 ///     parse_der_sequence_defined_g(
200 ///         |i:&[u8], _| {
201 ///             let (i, a) = parse_der_u32(i)?;
202 ///             let (i, obj) = parse_der_octetstring(i)?;
203 ///             let b = obj.as_slice().unwrap();
204 ///             Ok((i, MyObject{ a, b }))
205 ///         }
206 ///     )(i)
207 /// }
208 ///
209 /// # let empty = &b""[..];
210 /// # let bytes = [ 0x30, 0x0a,
211 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
212 /// #               0x04, 0x03, 0x01, 0x00, 0x00,
213 /// # ];
214 /// # let expected  = MyObject {
215 /// #   a: 0x010001,
216 /// #   b: &[01, 00, 00]
217 /// # };
218 /// # assert_eq!(parse_myobject(&bytes), Ok((empty, expected)));
219 /// let (rem, v) = parse_myobject(&bytes).expect("parsing failed");
220 /// ```
parse_der_sequence_defined_g<'a, O, F, E>( mut f: F, ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E> where F: FnMut(&'a [u8], DerObjectHeader<'a>) -> IResult<&'a [u8], O, E>, E: ParseError<&'a [u8]> + From<BerError>,221 pub fn parse_der_sequence_defined_g<'a, O, F, E>(
222     mut f: F,
223 ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
224 where
225     F: FnMut(&'a [u8], DerObjectHeader<'a>) -> IResult<&'a [u8], O, E>,
226     E: ParseError<&'a [u8]> + From<BerError>,
227 {
228     parse_der_container(move |i, hdr| {
229         if hdr.tag != DerTag::Sequence {
230             return Err(Err::Error(BerError::InvalidTag.into()));
231         }
232         f(i, hdr)
233     })
234 }
235 
236 /// Parse a SET OF object
237 ///
238 /// Given a subparser for a DER type, parse a set of identical objects.
239 ///
240 /// ```rust
241 /// # use der_parser::der::{parse_der_integer, parse_der_set_of, DerObject};
242 /// # use der_parser::error::BerResult;
243 /// #
244 /// /// Read a SET OF INTEGER
245 /// fn parser(i:&[u8]) -> BerResult<DerObject> {
246 ///     parse_der_set_of(parse_der_integer)(i)
247 /// }
248 ///
249 /// # let empty = &b""[..];
250 /// # let bytes = [ 0x31, 0x0a,
251 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
252 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
253 /// # ];
254 /// # let expected  = DerObject::from_set(vec![
255 /// #     DerObject::from_int_slice(b"\x01\x00\x01"),
256 /// #     DerObject::from_int_slice(b"\x01\x00\x00"),
257 /// # ]);
258 /// # assert_eq!(parser(&bytes), Ok((empty, expected)));
259 /// let (rem, v) = parser(&bytes).expect("parsing failed");
260 /// ```
parse_der_set_of<'a, F>(f: F) -> impl FnMut(&'a [u8]) -> BerResult where F: Fn(&'a [u8]) -> BerResult,261 pub fn parse_der_set_of<'a, F>(f: F) -> impl FnMut(&'a [u8]) -> BerResult
262 where
263     F: Fn(&'a [u8]) -> BerResult,
264 {
265     map(parse_der_set_of_v(f), DerObject::from_set)
266 }
267 
268 /// Parse a SET OF object (returning a vec)
269 ///
270 /// Given a subparser for a DER type, parse a set of identical objects.
271 ///
272 /// This differs from `parse_der_set_of` in the parse function and return type.
273 ///
274 /// ```rust
275 /// # use der_parser::der::{parse_der_integer, parse_der_set_of_v, DerObject};
276 /// # use der_parser::error::BerResult;
277 /// #
278 /// /// Read a SET OF INTEGER
279 /// fn parser(i:&[u8]) -> BerResult<Vec<DerObject>> {
280 ///     parse_der_set_of_v(parse_der_integer)(i)
281 /// }
282 ///
283 /// # let empty = &b""[..];
284 /// # let bytes = [ 0x31, 0x0a,
285 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
286 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
287 /// # ];
288 /// # let expected  = vec![
289 /// #     DerObject::from_int_slice(b"\x01\x00\x01"),
290 /// #     DerObject::from_int_slice(b"\x01\x00\x00"),
291 /// # ];
292 /// let (rem, v) = parser(&bytes).expect("parsing failed");
293 /// # assert_eq!(v, expected);
294 /// ```
parse_der_set_of_v<'a, T, F, E>(f: F) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Vec<T>, E> where F: FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>, E: ParseError<&'a [u8]> + From<BerError>,295 pub fn parse_der_set_of_v<'a, T, F, E>(f: F) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Vec<T>, E>
296 where
297     F: FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>,
298     E: ParseError<&'a [u8]> + From<BerError>,
299 {
300     let mut subparser = all_consuming(many0(complete(cut(f))));
301     parse_der_set_defined_g(move |data, _| subparser(data))
302 }
303 
304 /// Parse a defined set of DER elements (function version)
305 ///
306 /// Given a list of expected parsers, apply them to build a DER set and
307 /// return the remaining bytes and the built object.
308 ///
309 /// The remaining bytes point *after* the set: any bytes that are part of the sequence but not
310 /// parsed are ignored.
311 /// The nom combinator `all_consuming` can be used to ensure all the content is parsed.
312 ///
313 /// The object header is not available to the parsing function, and the returned type is always a
314 /// `DerObject`.
315 /// For a generic version, see [`parse_der_set_defined_g`](fn.parse_der_set_defined_g.html).
316 ///
317 /// # Examples
318 ///
319 /// Parsing a set of identical types (same as `parse_der_set_of`):
320 ///
321 /// ```rust
322 /// # use der_parser::der::{parse_der_integer, parse_der_set_defined, DerObject};
323 /// # use der_parser::error::BerResult;
324 /// use nom::combinator::complete;
325 /// use nom::multi::many1;
326 ///
327 /// fn localparse_seq(i:&[u8]) -> BerResult {
328 ///     parse_der_set_defined(
329 ///         many1(complete(parse_der_integer))
330 ///     )(i)
331 /// }
332 ///
333 /// # let empty = &b""[..];
334 /// # let bytes = [ 0x31, 0x0a,
335 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
336 /// #               0x02, 0x03, 0x01, 0x00, 0x00,
337 /// # ];
338 /// # let expected  = DerObject::from_set(vec![
339 /// #     DerObject::from_int_slice(b"\x01\x00\x01"),
340 /// #     DerObject::from_int_slice(b"\x01\x00\x00"),
341 /// # ]);
342 /// # assert_eq!(localparse_seq(&bytes), Ok((empty, expected)));
343 /// let (rem, v) = localparse_seq(&bytes).expect("parsing failed");
344 /// ```
345 ///
346 /// Parsing a defined set with different types:
347 ///
348 /// ```rust
349 /// # use der_parser::der::*;
350 /// # use der_parser::error::BerResult;
351 /// use nom::combinator::map;
352 /// use nom::sequence::tuple;
353 ///
354 /// /// Read a DER-encoded object:
355 /// /// SET {
356 /// ///     a INTEGER,
357 /// ///     b OCTETSTRING
358 /// /// }
359 /// fn localparse_set(i:&[u8]) -> BerResult {
360 ///     parse_der_set_defined(
361 ///         // the nom `tuple` combinator returns a tuple, so we have to map it
362 ///         // to a list
363 ///         map(
364 ///             tuple((parse_der_integer, parse_der_octetstring)),
365 ///             |(a, b)| vec![a, b]
366 ///         )
367 ///     )(i)
368 /// }
369 ///
370 /// # let empty = &b""[..];
371 /// # let bytes = [ 0x31, 0x0a,
372 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
373 /// #               0x04, 0x03, 0x01, 0x00, 0x00,
374 /// # ];
375 /// # let expected  = DerObject::from_set(vec![
376 /// #     DerObject::from_int_slice(b"\x01\x00\x01"),
377 /// #     DerObject::from_obj(DerObjectContent::OctetString(b"\x01\x00\x00")),
378 /// # ]);
379 /// # assert_eq!(localparse_set(&bytes), Ok((empty, expected)));
380 /// let (rem, v) = localparse_set(&bytes).expect("parsing failed");
381 /// ```
parse_der_set_defined<'a, F>(mut f: F) -> impl FnMut(&'a [u8]) -> BerResult where F: FnMut(&'a [u8]) -> BerResult<Vec<DerObject>>,382 pub fn parse_der_set_defined<'a, F>(mut f: F) -> impl FnMut(&'a [u8]) -> BerResult
383 where
384     F: FnMut(&'a [u8]) -> BerResult<Vec<DerObject>>,
385 {
386     map(
387         parse_der_set_defined_g(move |data, _| f(data)),
388         DerObject::from_set,
389     )
390 }
391 
392 /// Parse a defined SET object (generic version)
393 ///
394 /// Given a parser for set content, apply it to build a DER set and
395 /// return the remaining bytes and the built object.
396 ///
397 /// The remaining bytes point *after* the set: any bytes that are part of the sequence but not
398 /// parsed are ignored.
399 /// The nom combinator `all_consuming` can be used to ensure all the content is parsed.
400 ///
401 /// Unlike `parse_der_set_defined`, this function allows returning any object or error type,
402 /// and also passes the object header to the callback.
403 ///
404 /// # Examples
405 ///
406 /// Parsing a defined set with different types:
407 ///
408 /// ```rust
409 /// # use der_parser::der::*;
410 /// # use der_parser::error::BerResult;
411 /// #
412 /// # #[derive(Debug, PartialEq)]
413 /// pub struct MyObject<'a> {
414 ///     a: u32,
415 ///     b: &'a [u8],
416 /// }
417 ///
418 /// /// Read a DER-encoded object:
419 /// /// SET {
420 /// ///     a INTEGER (0..4294967295),
421 /// ///     b OCTETSTRING
422 /// /// }
423 /// fn parse_myobject(i: &[u8]) -> BerResult<MyObject> {
424 ///     parse_der_set_defined_g(
425 ///         |i:&[u8], _| {
426 ///             let (i, a) = parse_der_u32(i)?;
427 ///             let (i, obj) = parse_der_octetstring(i)?;
428 ///             let b = obj.as_slice().unwrap();
429 ///             Ok((i, MyObject{ a, b }))
430 ///         }
431 ///     )(i)
432 /// }
433 ///
434 /// # let empty = &b""[..];
435 /// # let bytes = [ 0x31, 0x0a,
436 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
437 /// #               0x04, 0x03, 0x01, 0x00, 0x00,
438 /// # ];
439 /// # let expected  = MyObject {
440 /// #   a: 0x010001,
441 /// #   b: &[01, 00, 00]
442 /// # };
443 /// # assert_eq!(parse_myobject(&bytes), Ok((empty, expected)));
444 /// let (rem, v) = parse_myobject(&bytes).expect("parsing failed");
445 /// ```
parse_der_set_defined_g<'a, O, F, E>( mut f: F, ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E> where F: FnMut(&'a [u8], DerObjectHeader<'a>) -> IResult<&'a [u8], O, E>, E: ParseError<&'a [u8]> + From<BerError>,446 pub fn parse_der_set_defined_g<'a, O, F, E>(
447     mut f: F,
448 ) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
449 where
450     F: FnMut(&'a [u8], DerObjectHeader<'a>) -> IResult<&'a [u8], O, E>,
451     E: ParseError<&'a [u8]> + From<BerError>,
452 {
453     parse_der_container(move |i, hdr| {
454         if hdr.tag != DerTag::Set {
455             return Err(Err::Error(BerError::InvalidTag.into()));
456         }
457         f(i, hdr)
458     })
459 }
460 
461 /// Parse a DER object and apply provided function to content
462 ///
463 /// Given a parser for content, read DER object header and apply parser to
464 /// return the remaining bytes and the parser result.
465 ///
466 /// The remaining bytes point *after* the content: any bytes that are part of the content but not
467 /// parsed are ignored.
468 /// The nom combinator `all_consuming` can be used to ensure all the content is parsed.
469 ///
470 /// This function is mostly intended for structured objects, but can be used for any valid DER
471 /// object.
472 ///
473 /// # Examples
474 ///
475 /// Parsing a defined sequence with different types:
476 ///
477 /// ```rust
478 /// # use der_parser::der::*;
479 /// # use der_parser::error::{BerError, BerResult};
480 /// #
481 /// # #[derive(Debug, PartialEq)]
482 /// pub struct MyObject<'a> {
483 ///     a: u32,
484 ///     b: &'a [u8],
485 /// }
486 ///
487 /// /// Read a DER-encoded object:
488 /// /// SEQUENCE {
489 /// ///     a INTEGER (0..4294967295),
490 /// ///     b OCTETSTRING
491 /// /// }
492 /// fn parse_myobject(i: &[u8]) -> BerResult<MyObject> {
493 ///     parse_der_container(
494 ///         |i: &[u8], hdr: DerObjectHeader| {
495 ///             if hdr.tag != DerTag::Sequence {
496 ///                 return Err(nom::Err::Error(BerError::BerTypeError.into()));
497 ///             }
498 ///             let (i, a) = parse_der_u32(i)?;
499 ///             let (i, obj) = parse_der_octetstring(i)?;
500 ///             let b = obj.as_slice().unwrap();
501 ///             Ok((i, MyObject{ a, b }))
502 ///         }
503 ///     )(i)
504 /// }
505 ///
506 /// # let empty = &b""[..];
507 /// # let bytes = [ 0x30, 0x0a,
508 /// #               0x02, 0x03, 0x01, 0x00, 0x01,
509 /// #               0x04, 0x03, 0x01, 0x00, 0x00,
510 /// # ];
511 /// # let expected  = MyObject {
512 /// #   a: 0x010001,
513 /// #   b: &[01, 00, 00]
514 /// # };
515 /// # assert_eq!(parse_myobject(&bytes), Ok((empty, expected)));
516 /// let (rem, v) = parse_myobject(&bytes).expect("parsing failed");
517 /// ```
parse_der_container<'a, O, F, E>(mut f: F) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E> where F: FnMut(&'a [u8], DerObjectHeader<'a>) -> IResult<&'a [u8], O, E>, E: ParseError<&'a [u8]> + From<BerError>,518 pub fn parse_der_container<'a, O, F, E>(mut f: F) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
519 where
520     F: FnMut(&'a [u8], DerObjectHeader<'a>) -> IResult<&'a [u8], O, E>,
521     E: ParseError<&'a [u8]> + From<BerError>,
522 {
523     move |i: &[u8]| {
524         let (i, hdr) = der_read_element_header(i).map_err(nom::Err::convert)?;
525         // X.690 10.1: the definitive form of length encoding shall be used
526         let (i, data) = match hdr.len {
527             BerSize::Definite(len) => take(len)(i)?,
528             BerSize::Indefinite => {
529                 return Err(Err::Error(BerError::DerConstraintFailed.into()));
530             }
531         };
532         let (_rest, v) = f(data, hdr)?;
533         Ok((i, v))
534     }
535 }
536