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
use crate::ber::MAX_RECURSION;
use crate::der::*;
use crate::error::*;
use nom::error::ParseError;
use nom::{Err, IResult};

/// Read a TAGGED EXPLICIT value (combinator)
///
/// The built object will use the outer header (and tag), and contains a `Tagged` object
/// with class, value and content.
///
/// For a generic version (different output and error types), see
/// [parse_der_tagged_explicit_g](fn.parse_der_tagged_explicit_g.html).
///
/// The following parses `[2] EXPLICIT INTEGER`:
///
/// ```rust
/// # use der_parser::der::*;
/// # use der_parser::error::BerResult;
/// use nom::combinator::map_res;
/// #
/// fn parse_int_explicit(i:&[u8]) -> BerResult<u32> {
///    map_res(
///        parse_der_tagged_explicit(2, parse_der_integer),
///        |x: DerObject| x.as_tagged()?.2.as_u32()
///    )(i)
/// }
///
/// # let bytes = &[0xa2, 0x05, 0x02, 0x03, 0x01, 0x00, 0x01];
/// let res = parse_int_explicit(bytes);
/// # match res {
/// #     Ok((rem,val)) => {
/// #         assert!(rem.is_empty());
/// #         assert_eq!(val, 0x10001);
/// #     },
/// #     _ => assert!(false)
/// # }
/// ```
pub fn parse_der_tagged_explicit<'a, T, F>(tag: T, f: F) -> impl FnMut(&'a [u8]) -> BerResult
where
    F: Fn(&'a [u8]) -> BerResult<DerObject>,
    T: Into<Tag>,
{
    let tag = tag.into();
    parse_der_tagged_explicit_g(tag, move |content, hdr| {
        let (rem, obj) = f(content)?;
        let class = hdr.class();
        let obj2 = DerObject::from_header_and_content(
            hdr,
            DerObjectContent::Tagged(class, tag, Box::new(obj)),
        );
        Ok((rem, obj2))
    })
}

/// Read a TAGGED EXPLICIT value (generic version)
///
/// The following parses `[2] EXPLICIT INTEGER`:
///
/// ```rust
/// # use der_parser::der::*;
/// # use der_parser::error::BerResult;
/// #
/// fn parse_int_explicit(i:&[u8]) -> BerResult<u32> {
///     parse_der_tagged_explicit_g(2, move |content, hdr| {
///         let (rem, obj) = parse_der_integer(content)?;
///         let value = obj.as_u32()?;
///         Ok((rem, value))
///    })(i)
/// }
///
/// # let bytes = &[0xa2, 0x05, 0x02, 0x03, 0x01, 0x00, 0x01];
/// let res = parse_int_explicit(bytes);
/// # match res {
/// #     Ok((rem,val)) => {
/// #         assert!(rem.is_empty());
/// #         assert_eq!(val, 0x10001);
/// #     },
/// #     _ => assert!(false)
/// # }
/// ```
pub fn parse_der_tagged_explicit_g<'a, T, Output, F, E>(
    tag: T,
    f: F,
) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Output, E>
where
    F: Fn(&'a [u8], Header<'a>) -> IResult<&'a [u8], Output, E>,
    E: ParseError<&'a [u8]> + From<BerError>,
    T: Into<Tag>,
{
    let tag = tag.into();
    parse_der_container(move |i, hdr| {
        if hdr.class() == Class::Universal {
            return Err(Err::Error(
                BerError::unexpected_class(None, hdr.class()).into(),
            ));
        }
        hdr.assert_tag(tag).map_err(|e| Err::Error(e.into()))?;
        // X.690 8.14.2: if implicit tagging was not used, the encoding shall be constructed
        if !hdr.is_constructed() {
            return Err(Err::Error(BerError::ConstructExpected.into()));
        }
        f(i, hdr)
        // trailing bytes are ignored
    })
}

/// Read a TAGGED IMPLICIT value (combinator)
///
/// Parse a TAGGED IMPLICIT value, given the expected tag, and the content parsing function.
///
/// The built object will use the original header (and tag), so the content may not match the tag
/// value.
///
/// For a generic version (different output and error types), see
/// [parse_der_tagged_implicit_g](fn.parse_der_tagged_implicit_g.html).
///
/// # Examples
///
/// The following parses `[2] IMPLICIT INTEGER` into a `DerObject`:
///
/// ```rust
/// # use der_parser::der::*;
/// # use der_parser::error::BerResult;
/// #
/// fn parse_int_implicit(i:&[u8]) -> BerResult<DerObject> {
///     parse_der_tagged_implicit(
///         2,
///         parse_der_content(Tag::Integer),
///     )(i)
/// }
///
/// # let bytes = &[0x82, 0x03, 0x01, 0x00, 0x01];
/// let res = parse_int_implicit(bytes);
/// # match res {
/// #     Ok((rem, content)) => {
/// #         assert!(rem.is_empty());
/// #         assert_eq!(content.as_u32(), Ok(0x10001));
/// #     },
/// #     _ => assert!(false)
/// # }
/// ```
///
/// The following parses `[2] IMPLICIT INTEGER` into an `u32`, raising an error if the integer is
/// too large:
///
/// ```rust
/// # use der_parser::der::*;
/// # use der_parser::error::BerResult;
/// use nom::combinator::map_res;
/// #
/// fn parse_int_implicit(i:&[u8]) -> BerResult<u32> {
///     map_res(
///         parse_der_tagged_implicit(
///             2,
///             parse_der_content(Tag::Integer),
///         ),
///         |x: DerObject| x.as_u32()
///     )(i)
/// }
///
/// # let bytes = &[0x82, 0x03, 0x01, 0x00, 0x01];
/// let res = parse_int_implicit(bytes);
/// # match res {
/// #     Ok((rem, val)) => {
/// #         assert!(rem.is_empty());
/// #         assert_eq!(val, 0x10001);
/// #     },
/// #     _ => assert!(false)
/// # }
/// ```
pub fn parse_der_tagged_implicit<'a, T, F>(tag: T, f: F) -> impl FnMut(&'a [u8]) -> BerResult
where
    F: Fn(&'a [u8], &'_ Header, usize) -> BerResult<'a, DerObjectContent<'a>>,
    T: Into<Tag>,
{
    let tag = tag.into();
    parse_der_tagged_implicit_g(tag, move |i, hdr, depth| {
        let (rem, content) = f(i, &hdr, depth)?;
        // trailing bytes are ignored
        let obj = DerObject::from_header_and_content(hdr, content);
        Ok((rem, obj))
    })
}

/// Read a TAGGED IMPLICIT value (generic version)
///
/// Parse a TAGGED IMPLICIT value, given the expected tag, and the content parsing function.
///
/// # Examples
///
/// The following parses `[1] IMPLICIT OCTETSTRING`, returning a `DerObject`:
///
/// ```rust
/// # use der_parser::der::*;
/// # use der_parser::error::BerResult;
/// #
/// fn parse_implicit_0_octetstring(i:&[u8]) -> BerResult<DerObjectContent> {
///     parse_der_tagged_implicit_g(
///         2,
///         parse_der_content2(Tag::OctetString)
///     )(i)
/// }
///
/// # let bytes = &[0x02, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f];
/// let res = parse_implicit_0_octetstring(bytes);
/// # match res {
/// #     Ok((rem, val)) => {
/// #         assert!(rem.is_empty());
/// #         let s = val.as_slice().unwrap();
/// #         assert_eq!(s, b"hello");
/// #     },
/// #     _ => assert!(false)
/// # }
/// ```
///
/// The following parses `[2] IMPLICIT INTEGER` into an `u32`, raising an error if the integer is
/// too large:
///
/// ```rust
/// # use der_parser::der::*;
/// # use der_parser::error::BerResult;
/// #
/// fn parse_int_implicit(i:&[u8]) -> BerResult<u32> {
///     parse_der_tagged_implicit_g(
///         2,
///         |content, hdr, depth| {
///             let (rem, obj_content) = parse_der_content(Tag::Integer)(content, &hdr, depth)?;
///             let value = obj_content.as_u32()?;
///             Ok((rem, value))
///         }
///     )(i)
/// }
///
/// # let bytes = &[0x82, 0x03, 0x01, 0x00, 0x01];
/// let res = parse_int_implicit(bytes);
/// # match res {
/// #     Ok((rem, val)) => {
/// #         assert!(rem.is_empty());
/// #         assert_eq!(val, 0x10001);
/// #     },
/// #     _ => assert!(false)
/// # }
/// ```
pub fn parse_der_tagged_implicit_g<'a, T, Output, F, E>(
    tag: T,
    f: F,
) -> impl FnMut(&'a [u8]) -> IResult<&[u8], Output, E>
where
    F: Fn(&'a [u8], Header<'a>, usize) -> IResult<&'a [u8], Output, E>,
    E: ParseError<&'a [u8]> + From<BerError>,
    T: Into<Tag>,
{
    let tag = tag.into();
    parse_der_container(move |i, hdr| {
        hdr.assert_tag(tag).map_err(|e| Err::Error(e.into()))?;
        // XXX MAX_RECURSION should not be used, it resets the depth counter
        f(i, hdr, MAX_RECURSION)
        // trailing bytes are ignored
    })
}