librasn / rasn

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

Question about DER encoding discrepancy between versions (0.7 -> 0.8) #217

Closed orthecreedence closed 5 months ago

orthecreedence commented 5 months ago

Hi! I had some tests for my project to make sure the serialization format is preserved across project updates. I was initially only testing deserialization but recently made a change in my project that made testing serialization deterministically possible as well. The interesting thing is that I noticed the serialization format changed from when I wrote the test (this was back on v0.7).

I wrote a quick test to check this out and came up with two results:

#[derive(Debug, Clone, AsnType, Encode, Decode)]
#[rasn(choice)]
enum Tx {
    #[rasn(tag(explicit(0)))]
    Test,
    #[rasn(tag(explicit(1)))]
    Reset {
        #[rasn(tag(explicit(0)))]
        keys: Option<Vec<u32>>,
    }
}
let tx1 = Tx::Reset { keys: None };
let tx1_ser = rasn::der::encode(&tx1).unwrap();
println!("--- {:?}", tx1_ser);

In v0.7 this produces (https://lapo.it/asn1js/#oQIwAA):

[161, 2, 48, 0]

And in v0.8 this produces (https://lapo.it/asn1js/#oQQwAqAA):

[161, 4, 48, 2, 160, 0]

I'm not entirely sure which one "correct" now. It seems v0.8+ encode None as their own sequence instead of omitting the value...is this the desired behavior? From my (admittedly limited) understanding, OPTIONAL fields are omitted when they are blank.

XAMPPRocky commented 5 months ago

Thank you for your issue! The best way to find out is to compare to another tool. IF the problem is indeed in the newer version, then we need to fix it. I don't have time to make a fix myself, but I'd be happy to review a PR fixing it 🙂