Skip to content

Commit

Permalink
Remove BIT STRING null byte truncation in BER
Browse files Browse the repository at this point in the history
fixes #111
fixes #89
  • Loading branch information
XAMPPRocky committed Jul 26, 2023
1 parent bf86b23 commit 39a9f47
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
15 changes: 1 addition & 14 deletions src/ber/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,6 @@ impl<'input> crate::Decoder for Decoder<'input> {
if let Some(last) = buffer.last_mut() {
*last &= !((1 << bits) - 1);
}
if buffer.last().map_or(false, |i| *i == 0) {
buffer.pop();
}

let string = types::BitString::from_vec(buffer);

Expand All @@ -283,17 +280,7 @@ impl<'input> crate::Decoder for Decoder<'input> {
})?;

self.input = input;
if let Some((i, _)) = bs
.as_raw_slice()
.iter()
.enumerate()
.rev()
.find(|(_, v)| **v != 0)
{
Ok(types::BitString::from_vec(bs.as_raw_slice()[..=i].to_vec()))
} else {
Ok(bs)
}
Ok(bs)
}

fn decode_visible_string(
Expand Down
3 changes: 0 additions & 3 deletions src/ber/enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,6 @@ impl crate::Encoder for Encoder {
let bit_length = value.len();
let bytes = value.clone().into_vec();
let mut deque = VecDeque::from(bytes);
while deque.back().map_or(false, |i| *i == 0) {
deque.pop_back();
}

deque.push_front((deque.len() * 8).saturating_sub(bit_length) as u8);
self.encode_string(tag, Tag::BIT_STRING, &Vec::from(deque))
Expand Down
24 changes: 24 additions & 0 deletions standards/kerberos/tests/issue111.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use rasn::{ber, Decoder, Tag, types::Class, Encoder};
use rasn_kerberos::KerberosFlags;

#[test]
fn kerberos_flags_dec() {
let input = b"\x03\x05\x00\x40\x81\x00\x00";
let mut decoder = ber::de::Decoder::new(input, ber::de::DecoderOptions::ber());
let output = decoder
.decode_bit_string(Tag::new(Class::Universal, 3), <_>::default())
.unwrap();
let expected = KerberosFlags::from_vec([0x40, 0x81, 0x00, 0x00].to_vec());
assert_eq!(output, expected)
}

#[test]
fn kerberos_flags_enc() {
let bitstring = KerberosFlags::from_vec([0x40, 0x81, 0x00, 0x00].to_vec());
let mut encoder = ber::enc::Encoder::new(ber::enc::EncoderOptions::ber());
encoder
.encode_bit_string(Tag::new(Class::Universal, 3), <_>::default(), &bitstring)
.unwrap();
assert_eq!(encoder.output(), vec![0x03, 0x05, 0x00, 0x40, 0x81, 0x00, 0x00])
}

0 comments on commit 39a9f47

Please sign in to comment.