Skip to content

BER encoding of implicit tag on tagged choice is incorrect #520

@t-higuchi

Description

@t-higuchi

ASN.1 definition:

TestModuleA DEFINITIONS AUTOMATIC TAGS::= BEGIN
    --tagged choice
    TC2  ::= [4] CHOICE { a INTEGER, b BOOLEAN }

    --implicit tag on tagged choice TC2
    TTC2 ::= [6] TC2
END

Tag [6] on TC2 is implicit tag since TC2 is tagged choice (not an untagged choice),
according to X.680 section 31.2.7 clause c:

31.2.7 The tagging construction specifies explicit tagging if any of the following holds:
 
   ...

  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".

The tagging construction specifies implicit tagging otherwise.
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^    

Code to reproduce:

    use rasn::prelude::*;
    #[doc = "tagged choice"]
    #[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, Eq, Hash)]
    #[rasn(choice, tag(explicit(context, 4)), automatic_tags)]
    pub enum TC2 {
        A(Integer),
        B(bool),
    }
    #[doc = "implicit tag on tagged choice TC2"]
    #[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, Eq, Hash)]
    #[rasn(delegate, tag(context, 6))]
    pub struct TTC2(pub TC2);

    let tc2 = TC2::A(0x55.into());
    let ttc2 = TTC2(tc2.clone());

    let tc2_enc = rasn::ber::encode(&tc2).unwrap();
    let ttc2_enc = rasn::ber::encode(&ttc2).unwrap();

    println!("TC2                 : {:02x?}", tc2_enc);
    println!("implicit tag on TC2 : {:02x?}", ttc2_enc);

Output:

TC2                 : [a4, 03, 80, 01, 55]
implicit tag on TC2 : [a6, 05, a4, 03, 80, 01, 55]
                       ^^^^^^ explicitly tagged instead

Expected encoding of implicit tag on TC2 is [a6, 03, 80, 01, 55].

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions