Skip to content

Commit f9d2949

Browse files
committed
Negative test case for recursively constructed octet string
ASN.1 BER specification valid, but chosen as unsupported.
1 parent fa9f2f2 commit f9d2949

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

der/src/asn1/octet_string.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,25 @@ mod tests {
489489

490490
assert_eq!(decoded.as_bytes(), b"Hello, world");
491491
}
492+
493+
#[test]
494+
#[cfg(all(feature = "alloc", feature = "ber"))]
495+
fn decode_ber_recursive_unsupported() {
496+
use crate::{Decode, Error, ErrorKind, Length, asn1::OctetString};
497+
use hex_literal::hex;
498+
499+
const EXAMPLE_BER: &[u8] = &hex!(
500+
"2480" // Constructed indefinite length OCTET STRING
501+
"2480" // Constructed indefinite length OCTET STRING
502+
"040648656c6c6f2c" // Segment containing "Hello,"
503+
"040620776f726c64" // Segment containing " world"
504+
"0000" // End-of-contents marker
505+
"040620776f726c64" // Segment containing " world"
506+
"0000" // End-of-contents marker
507+
);
508+
509+
let err = OctetString::from_ber(EXAMPLE_BER).err().unwrap();
510+
let expected = Error::new(ErrorKind::IndefiniteLength, Length::new(4));
511+
assert_eq!(expected, err);
512+
}
492513
}

der/src/length/indefinite.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@ pub(crate) fn read_constructed_vec<'r, R: Reader<'r>>(
9090
let h = Header::decode(reader)?;
9191
h.tag().assert_eq(inner_tag)?;
9292

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

0 commit comments

Comments
 (0)