简体   繁体   中英

ASN.1: Can the SIZE constraint be used to constrain SEQUENCE (not SEQUENCE OF)

Is the following type definition valid ASN.1 syntax?

MyType ::= SEQUENCE SIZE(2) {
  theID        OBJECT IDENTIFIER,
  someNumber   INTEGER OPTIONAL,
  someString   PrintableString OPTIONAL
}

I want to formally constrain the sequence such that for each instantiation exactly one of both optional attributes must be present.

PS: As SEQUENCE and SEQUENCE OF are encoded exactly the same way, I have the slight hope that this syntax is valid.

This syntax is not valid. SEQUENCE is like a struct type with fields, where each field can be of different type. SEQUENCE OF is like an array, where all nested types are of same type. Thus, in SEQUENCE OF you can set constraint to array size. Same rules apply to SET and SET OF , just unordered.

I would solve your problem this way: create a CHOICE of all your optional fields (without OPTIONAL modifier) and add this CHOICE to your main type:

MyChoice ::= CHOICE {
  someNumber   INTEGER,
  someString   PrintableString
}

MyType ::= SEQUENCE {
  theID        OBJECT IDENTIFIER,
  myChoice  <  MyChoice
}

In this case, theID field is mandatory and exactly one of MyChoice elements is required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM