Skip to content

Negative test case for recursively constructed octet string #1930

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions der/src/asn1/octet_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,25 @@ mod tests {

assert_eq!(decoded.as_bytes(), b"Hello, world");
}

#[test]
#[cfg(feature = "alloc")]
fn decode_ber_recursive_unsupported() {
use crate::{Decode, Error, ErrorKind, Length, asn1::OctetString};
use hex_literal::hex;

const EXAMPLE_BER: &[u8] = &hex!(
"2480" // Constructed indefinite length OCTET STRING
"2480" // Constructed indefinite length OCTET STRING
"040648656c6c6f2c" // Segment containing "Hello,"
"040620776f726c64" // Segment containing " world"
"0000" // End-of-contents marker
"040620776f726c64" // Segment containing " world"
"0000" // End-of-contents marker
);

let err = OctetString::from_ber(EXAMPLE_BER).err().unwrap();
let expected = Error::new(ErrorKind::IndefiniteLength, Length::new(4));
assert_eq!(expected, err);
}
}
8 changes: 7 additions & 1 deletion der/src/length/indefinite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ pub(crate) fn read_constructed_vec<'r, R: Reader<'r>>(
let h = Header::decode(reader)?;
h.tag.assert_eq(inner_tag)?;

// Indefinite length headers can't be indefinite
// This constructed string is ‘recursively constructed’
// as one of its segments is itself encoded with
// constructed, indefinite-length method.
// This is currently chosen to be unsupported.
//
// See discussion:
// - https://github.com/RustCrypto/formats/issues/779#issuecomment-3049589340
if h.length.is_indefinite() {
return Err(reader.error(ErrorKind::IndefiniteLength));
}
Expand Down