librasn / rasn

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

Fix/constrained extension #156

Closed 6d7a closed 1 year ago

6d7a commented 1 year ago

I ran into a problem with constrained extension additions the other day. I represented the following ASN1 snippet...

TestSequence ::= SEQUENCE { 
     hello OCTET STRING (SIZE(0..8)),
     ...,
     world INTEGER(0..9) DEFAULT 8
}

...using rasn as...

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq)]
#[non_exhaustive]
struct TestSequence {
      #[rasn(size("0..=8"))]
      hello: OctetString,
      #[rasn(
            extension_addition,
            value("0..=9"),
            default = "test_sequence_world_default"
      )]
      world: u8,
}

fn test_sequence_world_default() -> u8 {
     8
}

...and discovered that an encoding-decoding round trip fails. I did some digging and it turned out that the decoder does not take constraints into account when dealing with extension additions. This PR provides the test case above and should fix the issue. The crucial fix is in the extension addition function of the per-decoder in src/per/de.rs, where I am passing along constraint information.

6d7a commented 1 year ago

I also added the encode_default_with_constraints that is invoked in macros/src/config.rs:876.

XAMPPRocky commented 1 year ago

Thank you for your PR and congrats on your first contribution! I thought I had done this but I realised I mostly focused on constraints through the type system.