Skip to content

BER: automatic tagging not applied to tagged choice #519

@t-higuchi

Description

@t-higuchi

With following ASN.1 definition,

TestModuleA DEFINITIONS AUTOMATIC TAGS::= BEGIN
    --untagged choice
    C1 ::= CHOICE { a INTEGER, b BOOLEAN }

    --tagged choice. This is explicit tagging. (see ITU-T X.680 section 31.2.7 clause c)
    TC1  ::= [4] C1
    --another form of tagged choice
    TC2  ::= [4] CHOICE { a INTEGER, b BOOLEAN }
END

It is expected that:

  • BER encoding of tagged choice TC1 and TC2 should be identical, and
  • Alternatives in choice (a and b) are automatically tagged

But rasn produces different BER encoding for TC1 and TC2.

Code to reproduce:

    use rasn::prelude::*;

    #[doc = "untagged choice"]
    #[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, Eq, Hash)]
    #[rasn(choice, automatic_tags)]
    pub enum C1 {
        A(Integer),
        B(bool),
    }
    #[doc = "tagged choice. This is explicit tagging. (see ITU-T X.680 section 31.2.7 clause c)"]
    #[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, Eq, Hash)]
    #[rasn(delegate, tag(explicit(context, 4)))]
    pub struct TC1(pub C1);
    #[doc = "another form of 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),
    }

    let choice = C1::A(0x55.into());
    let tc1 = TC1(choice.clone());
    let tc2 = TC2::A(0x55.into());

    let choice_enc = rasn::ber::encode(&choice).unwrap();
    let tc1_enc = rasn::ber::encode(&tc1).unwrap();
    let tc2_enc = rasn::ber::encode(&tc2).unwrap();

    println!("TaggedReferencedChoice: {:02x?}", tagged_ref_choice_ber);
    println!("TaggedChoice:           {:02x?}", tagged_choice_ber);

output is:

choice           : [80, 01, 55]
tagged choice TC1: [a4, 03, 80, 01, 55]
tagged choice TC2: [a4, 03, 02, 01, 55]
                            ^^^^^^^^^^ explicitly tagged INTEGER 0x55. 

It seems to be a bug where #[rasn(automatic_tags)] is not applied when CHOICE is tagged.

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