librasn / rasn

A Safe #[no_std] ASN.1 Codec Framework
Other
183 stars 43 forks source link

Incorrect DER Encoding Order of SET OF Type #218

Closed andrewmah closed 5 months ago

andrewmah commented 5 months ago

Rasn DER encodes the SET OF type based on lexicographical order of elements; however, I believe the correct order is lexicographical order of encoded elements. For example, the SET OF containing two UTF8 strings "c" and "ab" should be in the order "c" and then "ab" because the encoding "0x0c0163" comes before "0x0c026162"; however, rasn encodes "ab" then "c" because "ab" comes before "c". Reference: https://luca.ntop.org/Teaching/Appunti/asn1.html (section 5.15)

Code snippet to reproduce:

let mut set_of = rasn::types::SetOf::new();
set_of.insert(String::from("c");
set_of.insert(String::from("ab");
let encoding = rasn::der::encode(&set_of).unwrap();
println!("{:?}", encoding);
XAMPPRocky commented 5 months ago

Thank you for your issue! I don't have to contribute a fix at the moment, but I would be happy to review a PR fixing it. 🙂

The relevant code can be found below. https://github.com/librasn/rasn/blob/40539e024fc55ba13f89ed16940e74bb453331d4/src/ber/enc.rs#L550-L565