2 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 static Type
*new_type
(Typetype t
);
51 static struct constraint_spec
*new_constraint_spec
(enum ctype
);
52 static Type
*new_tag
(int tagclass
, int tagvalue
, int tagenv
, Type
*oldtype
);
53 void yyerror (const char *);
54 static struct objid
*new_objid
(const char *label
, int value
);
55 static void add_oid_to_tail
(struct objid
*, struct objid
*);
56 static void fix_labels
(Symbol
*s
);
60 struct string_list
*next
;
74 struct string_list
*sl
;
76 struct memhead
*members
;
77 struct constraint_spec
*constraint_spec
;
81 %token kw_ABSTRACT_SYNTAX
106 %token kw_EXTENSIBILITY
110 %token kw_GeneralString
111 %token kw_GeneralizedTime
112 %token kw_GraphicString
121 %token kw_INTERSECTION
122 %token kw_ISO646String
125 %token kw_MINUS_INFINITY
127 %token kw_NumericString
132 %token kw_ObjectDescriptor
135 %token kw_PLUS_INFINITY
138 %token kw_PrintableString
140 %token kw_RELATIVE_OID
149 %token kw_TYPE_IDENTIFIER
150 %token kw_TeletexString
156 %token kw_UniversalString
157 %token kw_VideotexString
158 %token kw_VisibleString
165 %token
<name
> IDENTIFIER referencename
168 %token
<constant
> NUMBER
169 %type
<constant
> SignedNumber
170 %type
<constant
> Class tagenv
173 %type
<value
> BuiltinValue
174 %type
<value
> IntegerValue
175 %type
<value
> BooleanValue
176 %type
<value
> ObjectIdentifierValue
177 %type
<value
> CharacterStringValue
178 %type
<value
> NullValue
179 %type
<value
> DefinedValue
180 %type
<value
> ReferencedValue
181 %type
<value
> Valuereference
184 %type
<type
> BuiltinType
185 %type
<type
> BitStringType
186 %type
<type
> BooleanType
187 %type
<type
> ChoiceType
188 %type
<type
> ConstrainedType
189 %type
<type
> EnumeratedType
190 %type
<type
> IntegerType
191 %type
<type
> NullType
192 %type
<type
> OctetStringType
193 %type
<type
> SequenceType
194 %type
<type
> SequenceOfType
196 %type
<type
> SetOfType
197 %type
<type
> TaggedType
198 %type
<type
> ReferencedType
199 %type
<type
> DefinedType
200 %type
<type
> UsefulType
201 %type
<type
> ObjectIdentifierType
202 %type
<type
> CharacterStringType
203 %type
<type
> RestrictedCharactedStringType
207 %type
<member
> ComponentType
208 %type
<member
> NamedBit
209 %type
<member
> NamedNumber
210 %type
<member
> NamedType
211 %type
<members
> ComponentTypeList
212 %type
<members
> Enumerations
213 %type
<members
> NamedBitList
214 %type
<members
> NamedNumberList
216 %type
<objid
> objid objid_list objid_element objid_opt
217 %type
<range
> range size
219 %type
<sl
> referencenames
221 %type
<constraint_spec
> Constraint
222 %type
<constraint_spec
> ConstraintSpec
223 %type
<constraint_spec
> GeneralConstraint
224 %type
<constraint_spec
> ContentsConstraint
225 %type
<constraint_spec
> UserDefinedConstraint
229 %start ModuleDefinition
233 ModuleDefinition: IDENTIFIER objid_opt kw_DEFINITIONS TagDefault ExtensionDefault
234 EEQUAL kw_BEGIN ModuleBody kw_END
240 TagDefault
: kw_EXPLICIT kw_TAGS
241 | kw_IMPLICIT kw_TAGS
242 { error_message
("implicit tagging is not supported"); }
243 | kw_AUTOMATIC kw_TAGS
244 { error_message
("automatic tagging is not supported"); }
248 ExtensionDefault: kw_EXTENSIBILITY kw_IMPLIED
249 { error_message
("no extensibility options supported"); }
253 ModuleBody
: /* Exports */ Imports AssignmentList
257 Imports
: kw_IMPORTS SymbolsImported
';'
261 SymbolsImported
: SymbolsFromModuleList
265 SymbolsFromModuleList: SymbolsFromModule
266 | SymbolsFromModuleList SymbolsFromModule
269 SymbolsFromModule: referencenames kw_FROM IDENTIFIER objid_opt
271 struct string_list
*sl
;
272 for
(sl
= $1; sl
!= NULL
; sl
= sl
->next
) {
273 Symbol
*s
= addsym
(sl
->string);
280 AssignmentList
: Assignment
281 | Assignment AssignmentList
284 Assignment
: TypeAssignment
288 referencenames
: IDENTIFIER
',' referencenames
290 $$
= emalloc
(sizeof
(*$$
));
296 $$
= emalloc
(sizeof
(*$$
));
302 TypeAssignment
: IDENTIFIER EEQUAL Type
304 Symbol
*s
= addsym
($1);
317 BuiltinType
: BitStringType
319 | CharacterStringType
324 | ObjectIdentifierType
333 BooleanType
: kw_BOOLEAN
335 $$
= new_tag
(ASN1_C_UNIV
, UT_Boolean
,
336 TE_EXPLICIT
, new_type
(TBoolean
));
340 range
: '(' Value RANGE Value
')'
342 if
($2->type
!= integervalue
)
343 error_message
("Non-integer used in first part of range");
344 if
($2->type
!= integervalue
)
345 error_message
("Non-integer in second part of range");
346 $$
= ecalloc
(1, sizeof
(*$$
));
347 $$
->min
= $2->u.integervalue
;
348 $$
->max
= $4->u.integervalue
;
350 |
'(' Value RANGE kw_MAX
')'
352 if
($2->type
!= integervalue
)
353 error_message
("Non-integer in first part of range");
354 $$
= ecalloc
(1, sizeof
(*$$
));
355 $$
->min
= $2->u.integervalue
;
356 $$
->max
= $2->u.integervalue
- 1;
358 |
'(' kw_MIN RANGE Value
')'
360 if
($4->type
!= integervalue
)
361 error_message
("Non-integer in second part of range");
362 $$
= ecalloc
(1, sizeof
(*$$
));
363 $$
->min
= $4->u.integervalue
+ 2;
364 $$
->max
= $4->u.integervalue
;
368 if
($2->type
!= integervalue
)
369 error_message
("Non-integer used in limit");
370 $$
= ecalloc
(1, sizeof
(*$$
));
371 $$
->min
= $2->u.integervalue
;
372 $$
->max
= $2->u.integervalue
;
377 IntegerType
: kw_INTEGER
379 $$
= new_tag
(ASN1_C_UNIV
, UT_Integer
,
380 TE_EXPLICIT
, new_type
(TInteger
));
384 $$
= new_type
(TInteger
);
386 $$
= new_tag
(ASN1_C_UNIV
, UT_Integer
, TE_EXPLICIT
, $$
);
388 | kw_INTEGER
'{' NamedNumberList
'}'
390 $$
= new_type
(TInteger
);
392 $$
= new_tag
(ASN1_C_UNIV
, UT_Integer
, TE_EXPLICIT
, $$
);
396 NamedNumberList
: NamedNumber
398 $$
= emalloc
(sizeof
(*$$
));
400 ASN1_TAILQ_INSERT_HEAD
($$
, $1, members
);
402 | NamedNumberList
',' NamedNumber
404 ASN1_TAILQ_INSERT_TAIL
($1, $3, members
);
407 | NamedNumberList
',' ELLIPSIS
408 { $$
= $1; } /* XXX used for Enumerations */
411 NamedNumber
: IDENTIFIER
'(' SignedNumber
')'
413 $$
= emalloc
(sizeof
(*$$
));
415 $$
->gen_name
= estrdup
($1);
416 output_name
($$
->gen_name
);
424 EnumeratedType
: kw_ENUMERATED
'{' Enumerations
'}'
426 $$
= new_type
(TInteger
);
428 $$
= new_tag
(ASN1_C_UNIV
, UT_Enumerated
, TE_EXPLICIT
, $$
);
432 Enumerations
: NamedNumberList
/* XXX */
435 BitStringType
: kw_BIT kw_STRING
437 $$
= new_type
(TBitString
);
438 $$
->members
= emalloc
(sizeof
(*$$
->members
));
439 ASN1_TAILQ_INIT
($$
->members
);
440 $$
= new_tag
(ASN1_C_UNIV
, UT_BitString
, TE_EXPLICIT
, $$
);
442 | kw_BIT kw_STRING
'{' NamedBitList
'}'
444 $$
= new_type
(TBitString
);
446 $$
= new_tag
(ASN1_C_UNIV
, UT_BitString
, TE_EXPLICIT
, $$
);
450 ObjectIdentifierType: kw_OBJECT kw_IDENTIFIER
452 $$
= new_tag
(ASN1_C_UNIV
, UT_OID
,
453 TE_EXPLICIT
, new_type
(TOID
));
456 OctetStringType
: kw_OCTET kw_STRING size
458 Type
*t
= new_type
(TOctetString
);
460 $$
= new_tag
(ASN1_C_UNIV
, UT_OctetString
,
467 $$
= new_tag
(ASN1_C_UNIV
, UT_Null
,
468 TE_EXPLICIT
, new_type
(TNull
));
479 SequenceType
: kw_SEQUENCE
'{' /* ComponentTypeLists */ ComponentTypeList
'}'
481 $$
= new_type
(TSequence
);
483 $$
= new_tag
(ASN1_C_UNIV
, UT_Sequence
, TE_EXPLICIT
, $$
);
485 | kw_SEQUENCE
'{' '}'
487 $$
= new_type
(TSequence
);
489 $$
= new_tag
(ASN1_C_UNIV
, UT_Sequence
, TE_EXPLICIT
, $$
);
493 SequenceOfType
: kw_SEQUENCE size kw_OF Type
495 $$
= new_type
(TSequenceOf
);
498 $$
= new_tag
(ASN1_C_UNIV
, UT_Sequence
, TE_EXPLICIT
, $$
);
502 SetType
: kw_SET
'{' /* ComponentTypeLists */ ComponentTypeList
'}'
506 $$
= new_tag
(ASN1_C_UNIV
, UT_Set
, TE_EXPLICIT
, $$
);
512 $$
= new_tag
(ASN1_C_UNIV
, UT_Set
, TE_EXPLICIT
, $$
);
516 SetOfType
: kw_SET kw_OF Type
518 $$
= new_type
(TSetOf
);
520 $$
= new_tag
(ASN1_C_UNIV
, UT_Set
, TE_EXPLICIT
, $$
);
524 ChoiceType
: kw_CHOICE
'{' /* AlternativeTypeLists */ ComponentTypeList
'}'
526 $$
= new_type
(TChoice
);
531 ReferencedType
: DefinedType
535 DefinedType
: IDENTIFIER
537 Symbol
*s
= addsym
($1);
538 $$
= new_type
(TType
);
539 if
(s
->stype
!= Stype
&& s
->stype
!= SUndefined
)
540 error_message
("%s is not a type\n", $1);
546 UsefulType
: kw_GeneralizedTime
548 $$
= new_tag
(ASN1_C_UNIV
, UT_GeneralizedTime
,
549 TE_EXPLICIT
, new_type
(TGeneralizedTime
));
553 $$
= new_tag
(ASN1_C_UNIV
, UT_UTCTime
,
554 TE_EXPLICIT
, new_type
(TUTCTime
));
558 ConstrainedType
: Type Constraint
560 /* if (Constraint.type == contentConstrant) {
561 assert(Constraint.u.constraint.type == octetstring|bitstring-w/o-NamedBitList); // remember to check type reference too
562 if (Constraint.u.constraint.type) {
563 assert((Constraint.u.constraint.type.length % 8) == 0);
566 if (Constraint.u.constraint.encoding) {
567 type == der-oid|ber-oid
574 Constraint
: '(' ConstraintSpec
')'
580 ConstraintSpec
: GeneralConstraint
583 GeneralConstraint: ContentsConstraint
584 | UserDefinedConstraint
587 ContentsConstraint: kw_CONTAINING Type
589 $$
= new_constraint_spec
(CT_CONTENTS
);
590 $$
->u.content.type
= $2;
591 $$
->u.content.encoding
= NULL
;
593 | kw_ENCODED kw_BY Value
595 if
($3->type
!= objectidentifiervalue
)
596 error_message
("Non-OID used in ENCODED BY constraint");
597 $$
= new_constraint_spec
(CT_CONTENTS
);
598 $$
->u.content.type
= NULL
;
599 $$
->u.content.encoding
= $3;
601 | kw_CONTAINING Type kw_ENCODED kw_BY Value
603 if
($5->type
!= objectidentifiervalue
)
604 error_message
("Non-OID used in ENCODED BY constraint");
605 $$
= new_constraint_spec
(CT_CONTENTS
);
606 $$
->u.content.type
= $2;
607 $$
->u.content.encoding
= $5;
611 UserDefinedConstraint: kw_CONSTRAINED kw_BY
'{' '}'
613 $$
= new_constraint_spec
(CT_USER
);
617 TaggedType
: Tag tagenv Type
622 if
($3->type
== TTag
&& $2 == TE_IMPLICIT
) {
623 $$
->subtype
= $3->subtype
;
630 Tag
: '[' Class NUMBER
']'
634 $$.tagenv
= TE_EXPLICIT
;
671 ValueAssignment
: IDENTIFIER Type EEQUAL Value
678 generate_constant
(s
);
682 CharacterStringType: RestrictedCharactedStringType
685 RestrictedCharactedStringType: kw_GeneralString
687 $$
= new_tag
(ASN1_C_UNIV
, UT_GeneralString
,
688 TE_EXPLICIT
, new_type
(TGeneralString
));
692 $$
= new_tag
(ASN1_C_UNIV
, UT_TeletexString
,
693 TE_EXPLICIT
, new_type
(TTeletexString
));
697 $$
= new_tag
(ASN1_C_UNIV
, UT_UTF8String
,
698 TE_EXPLICIT
, new_type
(TUTF8String
));
702 $$
= new_tag
(ASN1_C_UNIV
, UT_PrintableString
,
703 TE_EXPLICIT
, new_type
(TPrintableString
));
707 $$
= new_tag
(ASN1_C_UNIV
, UT_VisibleString
,
708 TE_EXPLICIT
, new_type
(TVisibleString
));
712 $$
= new_tag
(ASN1_C_UNIV
, UT_IA5String
,
713 TE_EXPLICIT
, new_type
(TIA5String
));
717 $$
= new_tag
(ASN1_C_UNIV
, UT_BMPString
,
718 TE_EXPLICIT
, new_type
(TBMPString
));
722 $$
= new_tag
(ASN1_C_UNIV
, UT_UniversalString
,
723 TE_EXPLICIT
, new_type
(TUniversalString
));
728 ComponentTypeList: ComponentType
730 $$
= emalloc
(sizeof
(*$$
));
732 ASN1_TAILQ_INSERT_HEAD
($$
, $1, members
);
734 | ComponentTypeList
',' ComponentType
736 ASN1_TAILQ_INSERT_TAIL
($1, $3, members
);
739 | ComponentTypeList
',' ELLIPSIS
741 struct member
*m
= ecalloc
(1, sizeof
(*m
));
742 m
->name
= estrdup
("...");
743 m
->gen_name
= estrdup
("asn1_ellipsis");
745 ASN1_TAILQ_INSERT_TAIL
($1, m
, members
);
750 NamedType
: IDENTIFIER Type
752 $$
= emalloc
(sizeof
(*$$
));
754 $$
->gen_name
= estrdup
($1);
755 output_name
($$
->gen_name
);
761 ComponentType
: NamedType
767 | NamedType kw_OPTIONAL
773 | NamedType kw_DEFAULT Value
781 NamedBitList
: NamedBit
783 $$
= emalloc
(sizeof
(*$$
));
785 ASN1_TAILQ_INSERT_HEAD
($$
, $1, members
);
787 | NamedBitList
',' NamedBit
789 ASN1_TAILQ_INSERT_TAIL
($1, $3, members
);
794 NamedBit
: IDENTIFIER
'(' NUMBER
')'
796 $$
= emalloc
(sizeof
(*$$
));
798 $$
->gen_name
= estrdup
($1);
799 output_name
($$
->gen_name
);
808 |
/* empty */ { $$
= NULL
; }
811 objid
: '{' objid_list
'}'
817 objid_list
: /* empty */
821 | objid_element objid_list
825 add_oid_to_tail
($2, $1);
832 objid_element
: IDENTIFIER
'(' NUMBER
')'
834 $$
= new_objid
($1, $3);
838 Symbol
*s
= addsym
($1);
839 if
(s
->stype
!= SValue ||
840 s
->value
->type
!= objectidentifiervalue
) {
841 error_message
("%s is not an object identifier\n",
845 $$
= s
->value
->u.objectidentifiervalue
;
849 $$
= new_objid
(NULL
, $1);
857 BuiltinValue
: BooleanValue
858 | CharacterStringValue
860 | ObjectIdentifierValue
864 ReferencedValue
: DefinedValue
867 DefinedValue
: Valuereference
870 Valuereference
: IDENTIFIER
872 Symbol
*s
= addsym
($1);
873 if
(s
->stype
!= SValue
)
874 error_message
("%s is not a value\n",
881 CharacterStringValue: STRING
883 $$
= emalloc
(sizeof
(*$$
));
884 $$
->type
= stringvalue
;
885 $$
->u.stringvalue
= $1;
889 BooleanValue
: kw_TRUE
891 $$
= emalloc
(sizeof
(*$$
));
892 $$
->type
= booleanvalue
;
893 $$
->u.booleanvalue
= 0;
897 $$
= emalloc
(sizeof
(*$$
));
898 $$
->type
= booleanvalue
;
899 $$
->u.booleanvalue
= 0;
903 IntegerValue
: SignedNumber
905 $$
= emalloc
(sizeof
(*$$
));
906 $$
->type
= integervalue
;
907 $$
->u.integervalue
= $1;
911 SignedNumber
: NUMBER
919 ObjectIdentifierValue: objid
921 $$
= emalloc
(sizeof
(*$$
));
922 $$
->type
= objectidentifiervalue
;
923 $$
->u.objectidentifiervalue
= $1;
930 yyerror (const char *s
)
932 error_message
("%s\n", s
);
936 new_tag
(int tagclass
, int tagvalue
, int tagenv
, Type
*oldtype
)
939 if
(oldtype
->type
== TTag
&& oldtype
->tag.tagenv
== TE_IMPLICIT
) {
941 oldtype
= oldtype
->subtype
; /* XXX */
945 t
->tag.tagclass
= tagclass
;
946 t
->tag.tagvalue
= tagvalue
;
947 t
->tag.tagenv
= tagenv
;
948 t
->subtype
= oldtype
;
952 static struct objid
*
953 new_objid
(const char *label
, int value
)
956 s
= emalloc
(sizeof
(*s
));
964 add_oid_to_tail
(struct objid
*head
, struct objid
*tail
)
974 new_type
(Typetype tt
)
976 Type
*t
= ecalloc
(1, sizeof
(*t
));
981 static struct constraint_spec
*
982 new_constraint_spec
(enum ctype ct
)
984 struct constraint_spec
*c
= ecalloc
(1, sizeof
(*c
));
989 static void fix_labels2
(Type
*t
, const char *prefix
);
990 static void fix_labels1
(struct memhead
*members
, const char *prefix
)
996 ASN1_TAILQ_FOREACH
(m
, members
, members
) {
997 asprintf
(&m
->label
, "%s_%s", prefix
, m
->gen_name
);
998 if
(m
->label
== NULL
)
1001 fix_labels2
(m
->type
, m
->label
);
1005 static void fix_labels2
(Type
*t
, const char *prefix
)
1007 for
(; t
; t
= t
->subtype
)
1008 fix_labels1
(t
->members
, prefix
);
1012 fix_labels
(Symbol
*s
)
1015 asprintf
(&p
, "choice_%s", s
->gen_name
);
1018 fix_labels2
(s
->type
, p
);