Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OER: decode error when unused preamble bits are not zero #273

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/error/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,8 @@ pub enum OerDecodeErrorKind {
/// The amount of invalid bits.
msg: alloc::string::String,
},
#[snafu(display("Invalid preamble: {msg}"))]
InvalidPreamble { msg: alloc::string::String },
}

impl OerDecodeErrorKind {
Expand All @@ -719,6 +721,10 @@ impl OerDecodeErrorKind {
pub fn invalid_bit_string(msg: alloc::string::String) -> DecodeError {
CodecDecodeError::Oer(Self::InvalidOerBitString { msg }).into()
}
#[must_use]
pub fn invalid_preamble(msg: alloc::string::String) -> DecodeError {
CodecDecodeError::Oer(Self::InvalidPreamble { msg }).into()
}
}

#[derive(Snafu, Debug)]
Expand Down
24 changes: 24 additions & 0 deletions src/oer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,28 @@ mod tests {
decode_error!(coer, Test, &[0b1000_0001, 0x01]);
decode_ok!(oer, Test, &[0b1000_0001, 0x01], Test::A);
}
#[test]
fn test_seq_preamble_unused_bits() {
use crate as rasn;
#[derive(AsnType, Decode, Encode, Clone, Debug, PartialEq, Eq)]
#[rasn(automatic_tags)]
pub struct SequenceOptionals {
pub it: Integer,
pub is: Option<OctetString>,
pub late: Option<Integer>,
}
let data = [0x10, 0x01, 0x2A];
decode_error!(coer, SequenceOptionals, &data);
let data = [0x0, 0x01, 0x2A];
round_trip!(
oer,
SequenceOptionals,
SequenceOptionals {
it: 42.into(),
is: None,
late: None
},
&data
);
}
}
16 changes: 11 additions & 5 deletions src/oer/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@ impl<'input> Decoder<'input> {
self.input = input;
Ok(byte.as_bytes()[0])
}
fn drop_bits(&mut self, num: usize) -> Result<(), DecodeError> {
let (input, _) = nom::bytes::streaming::take(num)(self.input)
fn drop_preamble_bits(&mut self, num: usize) -> Result<(), DecodeError> {
let (input, bits) = nom::bytes::streaming::take(num)(self.input)
.map_err(|e| DecodeError::map_nom_err(e, self.codec()))?;
self.input = input;
Ok(())
if bits.not_any() {
self.input = input;
Ok(())
} else {
Err(OerDecodeErrorKind::invalid_preamble(
"Preamble unused bits should be all zero.".to_string(),
))
}
}
fn parse_tag(&mut self) -> Result<Tag, DecodeError> {
// Seems like tag number
Expand Down Expand Up @@ -466,7 +472,7 @@ impl<'input> Decoder<'input> {
} else {
bitmap.len()
};
self.drop_bits((8 - preamble_length % 8) % 8)?;
self.drop_preamble_bits((8 - preamble_length % 8) % 8)?;

debug_assert_eq!(self.input.len() % 8, 0);
Ok((bitmap, extensible_present))
Expand Down
Loading