Open pcwizz opened 3 weeks ago
Cc @Nicceboy
Seems like this bug is triggered because the rasn_smi::v2::ObjectSyntax
has nested untagged Choices. I am not sure how I should fix this, since there is no single truth for encoding and decoding in OER codec for this case. If I understand correctly, the first identified tag from the nested choices is repeated, but this is ambiguous on complex types.
While this should not happen in most cases on later standards, maybe something needs to be done. Should I just throw error or try to explore possible decode variations and choose the first suitable? On simple types it would work, but no guarantees for complex ones as far as I know.
Are you sure there's nothing? That is a case that is called out in other standards. You might want to check both the OER and X680 standards.
This was all I could find related to this:
From OER standard:
20.1 The encoding of a value of a choice type shall consist of the encoding of the outermost tag of the type of the chosen alternative as specified in 8.7, followed by the encoding of the value of the chosen alternative. NOTE 1 – Since the outermost tags of the alternatives of a choice type are required to be all different (see Rec. ITU-T X.680 | ISO/IEC 8824-1, 29.3), the outermost tag is sufficient to identify the chosen alternative. NOTE 2 – If the type of the chosen alternative has more than one tag as a result of explicit tagging, the tags following the outermost tag are not encoded. NOTE 3 – If the type of the choice alternative is an untagged choice type, the outermost tag for that alternative will appear more than once in the encoding. This is different from how BER works.
From X680
31.2.7 The tagging construction specifies explicit tagging if any of the following holds: a) the "Tag EXPLICIT Type" alternative is used; b) the "Tag Type" alternative is used and the value of "TagDefault" for the module is either EXPLICIT TAGS or is empty; c) the "Tag Type" alternative is used and the value of "TagDefault" for the module is IMPLICIT TAGS or AUTOMATIC TAGS, but the type defined by "Type" is an untagged choice type, an untagged open type, or an untagged "DummyReference" (see Rec. ITU-T X.683 | ISO/IEC 8824-4, Error! Reference source not found.).
Here is an example of ambiguous case:
OuterChoice ::= CHOICE {
a INTEGER,
b InnerChoice
}
InnerChoice ::= CHOICE {
c INTEGER,
d BOOLEAN
}
In both cases the outermost encoded value would be the same, whether a
was chosen from outermost choice or c
was chosen from the innermost.
Decoder would need to identify the correct type/value selection by peeking next bytes or otherwise trying to understand the context, but this feels a bit too complex to add and does not guarantee correctness.
BER handles this by using nested TLV pattern, and PER handles this by using indexes. I guess OER does not handle this case, because it is aiming for simplicity and efficiency in general.
Rather, maybe it would be better to encourage here that ASN.1 type specifications should be designed to be less ambiguous with OER (e.g. use tags), instead of trying to implement something for now.
We could implement encoder (potentially), but not decoder. Or we could implement decoder in simplified form, where it tries to create map if the type contains nested choices and then looks for repeating same bytes for suitable inner type until it does not, but I am not sure how difficult it is to add. Type encoding itself could include identical bytes, so it might not be completely correct.
I could implement complete decoder, if someone shows me the section(s) in standard(s) which proves that this is not ambiguous.
I've been doing a spot of testing of the new OER implementation and found some inputs that produce encoder output that the OER decoder refuses to decode. @XAMPPRocky asked that I report them here to be fixed.
Example
Input
Error
Fuzz target
Fuzzer output