Skip to content

Commit 442e3e3

Browse files
committed
der: read_nested: check header length matches contents of IMPLICIT
1 parent 7f63ffb commit 442e3e3

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

der/src/asn1/context_specific.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,14 @@ impl<T> ContextSpecific<T> {
6363
T: DecodeValue<'a> + Tagged,
6464
{
6565
Self::decode_with::<_, _, T::Error>(reader, tag_number, |reader| {
66+
// Decode IMPLICIT header
6667
let header = Header::decode(reader)?;
67-
let value = T::decode_value(reader, header)?;
68+
69+
// read_nested checks if header matches decoded length
70+
let value = reader.read_nested(header.length, |reader| {
71+
// Decode inner IMPLICIT value
72+
T::decode_value(reader, header)
73+
})?;
6874

6975
if header.tag.is_constructed() != value.tag().is_constructed() {
7076
return Err(header.tag.non_canonical_error().into());
@@ -119,6 +125,7 @@ where
119125
type Error = T::Error;
120126

121127
fn decode<R: Reader<'a>>(reader: &mut R) -> Result<Self, Self::Error> {
128+
// Decode EXPLICIT header
122129
let header = Header::decode(reader)?;
123130

124131
match header.tag {
@@ -128,7 +135,10 @@ where
128135
} => Ok(Self {
129136
tag_number: number,
130137
tag_mode: TagMode::default(),
131-
value: reader.read_nested(header.length, |reader| T::decode(reader))?,
138+
value: reader.read_nested(header.length, |reader| {
139+
// Decode inner tag-length-value of EXPLICIT
140+
T::decode(reader)
141+
})?,
132142
}),
133143
tag => Err(tag.unexpected_error(None).into()),
134144
}

0 commit comments

Comments
 (0)