Skip to content

Commit

Permalink
Fix wrong tag in encoding of SET OF (Closes #30)
Browse files Browse the repository at this point in the history
  • Loading branch information
chifflier committed Feb 21, 2024
1 parent eeaed24 commit c8292f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/asn1_types/set/set_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ where
}

fn write_der_header(&self, writer: &mut dyn std::io::Write) -> SerializeResult<usize> {
self.items.write_der_header(writer)
// Do not call self.items.write_der_header(), this will encode the wrong tag (items is a Vec)
let mut len = 0;
for t in self.items.iter() {
len += t.to_der_len().map_err(|_| SerializeError::InvalidLength)?;
}
let header = Header::new(Class::Universal, true, Self::TAG, Length::Definite(len));
header.write_der_header(writer).map_err(Into::into)
}

fn write_der_content(&self, writer: &mut dyn std::io::Write) -> SerializeResult<usize> {
Expand Down
9 changes: 9 additions & 0 deletions tests/to_der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,15 @@ fn to_der_set() {
assert_eq!(&v, &hex!("31 09 02 01 02 02 01 03 02 01 04"));
}

#[test]
fn to_der_set_of() {
let seq = SetOf::from_iter([2, 3, 4]);
let v = seq.to_der_vec().expect("serialization failed");
assert_eq!(&v, &hex!("31 09 02 01 02 02 01 03 02 01 04"));
let (_, seq2) = SetOf::from_der(&v).expect("decoding serialized object failed");
assert_eq!(seq, seq2);
}

#[test]
fn to_der_str() {
let s = "abcdef";
Expand Down

0 comments on commit c8292f4

Please sign in to comment.