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.