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
34 /* $Heimdal: parse.y 21597 2007-07-16 18:48:58Z lha $
49 __RCSID
("$Heimdal: parse.y 21597 2007-07-16 18:48:58Z lha $"
52 static Type
*new_type
(Typetype t
);
53 static struct constraint_spec
*new_constraint_spec
(enum ctype
);
54 static Type
*new_tag
(int tagclass
, int tagvalue
, int tagenv
, Type
*oldtype
);
55 void yyerror (const char *);
56 static struct objid
*new_objid
(const char *label
, int value
);
57 static void add_oid_to_tail
(struct objid
*, struct objid
*);
58 static void fix_labels
(Symbol
*s
);
62 struct string_list
*next
;
76 struct string_list
*sl
;
78 struct memhead
*members
;
79 struct constraint_spec
*constraint_spec
;
83 %token kw_ABSTRACT_SYNTAX
100 %token kw_DEFINITIONS
108 %token kw_EXTENSIBILITY
112 %token kw_GeneralString
113 %token kw_GeneralizedTime
114 %token kw_GraphicString
123 %token kw_INTERSECTION
124 %token kw_ISO646String
127 %token kw_MINUS_INFINITY
129 %token kw_NumericString
134 %token kw_ObjectDescriptor
137 %token kw_PLUS_INFINITY
140 %token kw_PrintableString
142 %token kw_RELATIVE_OID
151 %token kw_TYPE_IDENTIFIER
152 %token kw_TeletexString
158 %token kw_UniversalString
159 %token kw_VideotexString
160 %token kw_VisibleString
167 %token
<name
> IDENTIFIER referencename
170 %token
<constant
> NUMBER
171 %type
<constant
> SignedNumber
172 %type
<constant
> Class tagenv
175 %type
<value
> BuiltinValue
176 %type
<value
> IntegerValue
177 %type
<value
> BooleanValue
178 %type
<value
> ObjectIdentifierValue
179 %type
<value
> CharacterStringValue
180 %type
<value
> NullValue
181 %type
<value
> DefinedValue
182 %type
<value
> ReferencedValue
183 %type
<value
> Valuereference
186 %type
<type
> BuiltinType
187 %type
<type
> BitStringType
188 %type
<type
> BooleanType
189 %type
<type
> ChoiceType
190 %type
<type
> ConstrainedType
191 %type
<type
> EnumeratedType
192 %type
<type
> IntegerType
193 %type
<type
> NullType
194 %type
<type
> OctetStringType
195 %type
<type
> SequenceType
196 %type
<type
> SequenceOfType
198 %type
<type
> SetOfType
199 %type
<type
> TaggedType
200 %type
<type
> ReferencedType
201 %type
<type
> DefinedType
202 %type
<type
> UsefulType
203 %type
<type
> ObjectIdentifierType
204 %type
<type
> CharacterStringType
205 %type
<type
> RestrictedCharactedStringType
209 %type
<member
> ComponentType
210 %type
<member
> NamedBit
211 %type
<member
> NamedNumber
212 %type
<member
> NamedType
213 %type
<members
> ComponentTypeList
214 %type
<members
> Enumerations
215 %type
<members
> NamedBitList
216 %type
<members
> NamedNumberList
218 %type
<objid
> objid objid_list objid_element objid_opt
219 %type
<range
> range size
221 %type
<sl
> referencenames
223 %type
<constraint_spec
> Constraint
224 %type
<constraint_spec
> ConstraintSpec
225 %type
<constraint_spec
> GeneralConstraint
226 %type
<constraint_spec
> ContentsConstraint
227 %type
<constraint_spec
> UserDefinedConstraint
231 %start ModuleDefinition
235 ModuleDefinition: IDENTIFIER objid_opt kw_DEFINITIONS TagDefault ExtensionDefault
236 EEQUAL kw_BEGIN ModuleBody kw_END
242 TagDefault
: kw_EXPLICIT kw_TAGS
243 | kw_IMPLICIT kw_TAGS
244 { error_message
("implicit tagging is not supported"); }
245 | kw_AUTOMATIC kw_TAGS
246 { error_message
("automatic tagging is not supported"); }
250 ExtensionDefault: kw_EXTENSIBILITY kw_IMPLIED
251 { error_message
("no extensibility options supported"); }
255 ModuleBody
: /* Exports */ Imports AssignmentList
259 Imports
: kw_IMPORTS SymbolsImported
';'
263 SymbolsImported
: SymbolsFromModuleList
267 SymbolsFromModuleList: SymbolsFromModule
268 | SymbolsFromModuleList SymbolsFromModule
271 SymbolsFromModule: referencenames kw_FROM IDENTIFIER objid_opt
273 struct string_list
*sl
;
274 for
(sl
= $1; sl
!= NULL
; sl
= sl
->next
) {
275 Symbol
*s
= addsym
(sl
->string);
282 AssignmentList
: Assignment
283 | Assignment AssignmentList
286 Assignment
: TypeAssignment
290 referencenames
: IDENTIFIER
',' referencenames
292 $$
= emalloc
(sizeof
(*$$
));
298 $$
= emalloc
(sizeof
(*$$
));
304 TypeAssignment
: IDENTIFIER EEQUAL Type
306 Symbol
*s
= addsym
($1);
319 BuiltinType
: BitStringType
321 | CharacterStringType
326 | ObjectIdentifierType
335 BooleanType
: kw_BOOLEAN
337 $$
= new_tag
(ASN1_C_UNIV
, UT_Boolean
,
338 TE_EXPLICIT
, new_type
(TBoolean
));
342 range
: '(' Value RANGE Value
')'
344 if
($2->type
!= integervalue
)
345 error_message
("Non-integer used in first part of range");
346 if
($2->type
!= integervalue
)
347 error_message
("Non-integer in second part of range");
348 $$
= ecalloc
(1, sizeof
(*$$
));
349 $$
->min
= $2->u.integervalue
;
350 $$
->max
= $4->u.integervalue
;
352 |
'(' Value RANGE kw_MAX
')'
354 if
($2->type
!= integervalue
)
355 error_message
("Non-integer in first part of range");
356 $$
= ecalloc
(1, sizeof
(*$$
));
357 $$
->min
= $2->u.integervalue
;
358 $$
->max
= $2->u.integervalue
- 1;
360 |
'(' kw_MIN RANGE Value
')'
362 if
($4->type
!= integervalue
)
363 error_message
("Non-integer in second part of range");
364 $$
= ecalloc
(1, sizeof
(*$$
));
365 $$
->min
= $4->u.integervalue
+ 2;
366 $$
->max
= $4->u.integervalue
;
370 if
($2->type
!= integervalue
)
371 error_message
("Non-integer used in limit");
372 $$
= ecalloc
(1, sizeof
(*$$
));
373 $$
->min
= $2->u.integervalue
;
374 $$
->max
= $2->u.integervalue
;
379 IntegerType
: kw_INTEGER
381 $$
= new_tag
(ASN1_C_UNIV
, UT_Integer
,
382 TE_EXPLICIT
, new_type
(TInteger
));
386 $$
= new_type
(TInteger
);
388 $$
= new_tag
(ASN1_C_UNIV
, UT_Integer
, TE_EXPLICIT
, $$
);
390 | kw_INTEGER
'{' NamedNumberList
'}'
392 $$
= new_type
(TInteger
);
394 $$
= new_tag
(ASN1_C_UNIV
, UT_Integer
, TE_EXPLICIT
, $$
);
398 NamedNumberList
: NamedNumber
400 $$
= emalloc
(sizeof
(*$$
));
402 ASN1_TAILQ_INSERT_HEAD
($$
, $1, members
);
404 | NamedNumberList
',' NamedNumber
406 ASN1_TAILQ_INSERT_TAIL
($1, $3, members
);
409 | NamedNumberList
',' ELLIPSIS
410 { $$
= $1; } /* XXX used for Enumerations */
413 NamedNumber
: IDENTIFIER
'(' SignedNumber
')'
415 $$
= emalloc
(sizeof
(*$$
));
417 $$
->gen_name
= estrdup
($1);
418 output_name
($$
->gen_name
);
426 EnumeratedType
: kw_ENUMERATED
'{' Enumerations
'}'
428 $$
= new_type
(TInteger
);
430 $$
= new_tag
(ASN1_C_UNIV
, UT_Enumerated
, TE_EXPLICIT
, $$
);
434 Enumerations
: NamedNumberList
/* XXX */
437 BitStringType
: kw_BIT kw_STRING
439 $$
= new_type
(TBitString
);
440 $$
->members
= emalloc
(sizeof
(*$$
->members
));
441 ASN1_TAILQ_INIT
($$
->members
);
442 $$
= new_tag
(ASN1_C_UNIV
, UT_BitString
, TE_EXPLICIT
, $$
);
444 | kw_BIT kw_STRING
'{' NamedBitList
'}'
446 $$
= new_type
(TBitString
);
448 $$
= new_tag
(ASN1_C_UNIV
, UT_BitString
, TE_EXPLICIT
, $$
);
452 ObjectIdentifierType: kw_OBJECT kw_IDENTIFIER
454 $$
= new_tag
(ASN1_C_UNIV
, UT_OID
,
455 TE_EXPLICIT
, new_type
(TOID
));
458 OctetStringType
: kw_OCTET kw_STRING size
460 Type
*t
= new_type
(TOctetString
);
462 $$
= new_tag
(ASN1_C_UNIV
, UT_OctetString
,
469 $$
= new_tag
(ASN1_C_UNIV
, UT_Null
,
470 TE_EXPLICIT
, new_type
(TNull
));
481 SequenceType
: kw_SEQUENCE
'{' /* ComponentTypeLists */ ComponentTypeList
'}'
483 $$
= new_type
(TSequence
);
485 $$
= new_tag
(ASN1_C_UNIV
, UT_Sequence
, TE_EXPLICIT
, $$
);
487 | kw_SEQUENCE
'{' '}'
489 $$
= new_type
(TSequence
);
491 $$
= new_tag
(ASN1_C_UNIV
, UT_Sequence
, TE_EXPLICIT
, $$
);
495 SequenceOfType
: kw_SEQUENCE size kw_OF Type
497 $$
= new_type
(TSequenceOf
);
500 $$
= new_tag
(ASN1_C_UNIV
, UT_Sequence
, TE_EXPLICIT
, $$
);
504 SetType
: kw_SET
'{' /* ComponentTypeLists */ ComponentTypeList
'}'
508 $$
= new_tag
(ASN1_C_UNIV
, UT_Set
, TE_EXPLICIT
, $$
);
514 $$
= new_tag
(ASN1_C_UNIV
, UT_Set
, TE_EXPLICIT
, $$
);
518 SetOfType
: kw_SET kw_OF Type
520 $$
= new_type
(TSetOf
);
522 $$
= new_tag
(ASN1_C_UNIV
, UT_Set
, TE_EXPLICIT
, $$
);
526 ChoiceType
: kw_CHOICE
'{' /* AlternativeTypeLists */ ComponentTypeList
'}'
528 $$
= new_type
(TChoice
);
533 ReferencedType
: DefinedType
537 DefinedType
: IDENTIFIER
539 Symbol
*s
= addsym
($1);
540 $$
= new_type
(TType
);
541 if
(s
->stype
!= Stype
&& s
->stype
!= SUndefined
)
542 error_message
("%s is not a type\n", $1);
548 UsefulType
: kw_GeneralizedTime
550 $$
= new_tag
(ASN1_C_UNIV
, UT_GeneralizedTime
,
551 TE_EXPLICIT
, new_type
(TGeneralizedTime
));
555 $$
= new_tag
(ASN1_C_UNIV
, UT_UTCTime
,
556 TE_EXPLICIT
, new_type
(TUTCTime
));
560 ConstrainedType
: Type Constraint
562 /* if (Constraint.type == contentConstrant) {
563 assert(Constraint.u.constraint.type == octetstring|bitstring-w/o-NamedBitList); // remember to check type reference too
564 if (Constraint.u.constraint.type) {
565 assert((Constraint.u.constraint.type.length % 8) == 0);
568 if (Constraint.u.constraint.encoding) {
569 type == der-oid|ber-oid
576 Constraint
: '(' ConstraintSpec
')'
582 ConstraintSpec
: GeneralConstraint
585 GeneralConstraint: ContentsConstraint
586 | UserDefinedConstraint
589 ContentsConstraint: kw_CONTAINING Type
591 $$
= new_constraint_spec
(CT_CONTENTS
);
592 $$
->u.content.type
= $2;
593 $$
->u.content.encoding
= NULL
;
595 | kw_ENCODED kw_BY Value
597 if
($3->type
!= objectidentifiervalue
)
598 error_message
("Non-OID used in ENCODED BY constraint");
599 $$
= new_constraint_spec
(CT_CONTENTS
);
600 $$
->u.content.type
= NULL
;
601 $$
->u.content.encoding
= $3;
603 | kw_CONTAINING Type kw_ENCODED kw_BY Value
605 if
($5->type
!= objectidentifiervalue
)
606 error_message
("Non-OID used in ENCODED BY constraint");
607 $$
= new_constraint_spec
(CT_CONTENTS
);
608 $$
->u.content.type
= $2;
609 $$
->u.content.encoding
= $5;
613 UserDefinedConstraint: kw_CONSTRAINED kw_BY
'{' '}'
615 $$
= new_constraint_spec
(CT_USER
);
619 TaggedType
: Tag tagenv Type
624 if
($3->type
== TTag
&& $2 == TE_IMPLICIT
) {
625 $$
->subtype
= $3->subtype
;
632 Tag
: '[' Class NUMBER
']'
636 $$.tagenv
= TE_EXPLICIT
;
673 ValueAssignment
: IDENTIFIER Type EEQUAL Value
680 generate_constant
(s
);
684 CharacterStringType: RestrictedCharactedStringType
687 RestrictedCharactedStringType: kw_GeneralString
689 $$
= new_tag
(ASN1_C_UNIV
, UT_GeneralString
,
690 TE_EXPLICIT
, new_type
(TGeneralString
));
694 $$
= new_tag
(ASN1_C_UNIV
, UT_UTF8String
,
695 TE_EXPLICIT
, new_type
(TUTF8String
));
699 $$
= new_tag
(ASN1_C_UNIV
, UT_PrintableString
,
700 TE_EXPLICIT
, new_type
(TPrintableString
));
704 $$
= new_tag
(ASN1_C_UNIV
, UT_VisibleString
,
705 TE_EXPLICIT
, new_type
(TVisibleString
));
709 $$
= new_tag
(ASN1_C_UNIV
, UT_IA5String
,
710 TE_EXPLICIT
, new_type
(TIA5String
));
714 $$
= new_tag
(ASN1_C_UNIV
, UT_BMPString
,
715 TE_EXPLICIT
, new_type
(TBMPString
));
719 $$
= new_tag
(ASN1_C_UNIV
, UT_UniversalString
,
720 TE_EXPLICIT
, new_type
(TUniversalString
));
725 ComponentTypeList: ComponentType
727 $$
= emalloc
(sizeof
(*$$
));
729 ASN1_TAILQ_INSERT_HEAD
($$
, $1, members
);
731 | ComponentTypeList
',' ComponentType
733 ASN1_TAILQ_INSERT_TAIL
($1, $3, members
);
736 | ComponentTypeList
',' ELLIPSIS
738 struct member
*m
= ecalloc
(1, sizeof
(*m
));
739 m
->name
= estrdup
("...");
740 m
->gen_name
= estrdup
("asn1_ellipsis");
742 ASN1_TAILQ_INSERT_TAIL
($1, m
, members
);
747 NamedType
: IDENTIFIER Type
749 $$
= emalloc
(sizeof
(*$$
));
751 $$
->gen_name
= estrdup
($1);
752 output_name
($$
->gen_name
);
758 ComponentType
: NamedType
764 | NamedType kw_OPTIONAL
770 | NamedType kw_DEFAULT Value
778 NamedBitList
: NamedBit
780 $$
= emalloc
(sizeof
(*$$
));
782 ASN1_TAILQ_INSERT_HEAD
($$
, $1, members
);
784 | NamedBitList
',' NamedBit
786 ASN1_TAILQ_INSERT_TAIL
($1, $3, members
);
791 NamedBit
: IDENTIFIER
'(' NUMBER
')'
793 $$
= emalloc
(sizeof
(*$$
));
795 $$
->gen_name
= estrdup
($1);
796 output_name
($$
->gen_name
);
805 |
/* empty */ { $$
= NULL
; }
808 objid
: '{' objid_list
'}'
814 objid_list
: /* empty */
818 | objid_element objid_list
822 add_oid_to_tail
($2, $1);
829 objid_element
: IDENTIFIER
'(' NUMBER
')'
831 $$
= new_objid
($1, $3);
835 Symbol
*s
= addsym
($1);
836 if
(s
->stype
!= SValue ||
837 s
->value
->type
!= objectidentifiervalue
) {
838 error_message
("%s is not an object identifier\n",
842 $$
= s
->value
->u.objectidentifiervalue
;
846 $$
= new_objid
(NULL
, $1);
854 BuiltinValue
: BooleanValue
855 | CharacterStringValue
857 | ObjectIdentifierValue
861 ReferencedValue
: DefinedValue
864 DefinedValue
: Valuereference
867 Valuereference
: IDENTIFIER
869 Symbol
*s
= addsym
($1);
870 if
(s
->stype
!= SValue
)
871 error_message
("%s is not a value\n",
878 CharacterStringValue: STRING
880 $$
= emalloc
(sizeof
(*$$
));
881 $$
->type
= stringvalue
;
882 $$
->u.stringvalue
= $1;
886 BooleanValue
: kw_TRUE
888 $$
= emalloc
(sizeof
(*$$
));
889 $$
->type
= booleanvalue
;
890 $$
->u.booleanvalue
= 0;
894 $$
= emalloc
(sizeof
(*$$
));
895 $$
->type
= booleanvalue
;
896 $$
->u.booleanvalue
= 0;
900 IntegerValue
: SignedNumber
902 $$
= emalloc
(sizeof
(*$$
));
903 $$
->type
= integervalue
;
904 $$
->u.integervalue
= $1;
908 SignedNumber
: NUMBER
916 ObjectIdentifierValue: objid
918 $$
= emalloc
(sizeof
(*$$
));
919 $$
->type
= objectidentifiervalue
;
920 $$
->u.objectidentifiervalue
= $1;
927 yyerror (const char *s
)
929 error_message
("%s\n", s
);
933 new_tag
(int tagclass
, int tagvalue
, int tagenv
, Type
*oldtype
)
936 if
(oldtype
->type
== TTag
&& oldtype
->tag.tagenv
== TE_IMPLICIT
) {
938 oldtype
= oldtype
->subtype
; /* XXX */
942 t
->tag.tagclass
= tagclass
;
943 t
->tag.tagvalue
= tagvalue
;
944 t
->tag.tagenv
= tagenv
;
945 t
->subtype
= oldtype
;
949 static struct objid
*
950 new_objid
(const char *label
, int value
)
953 s
= emalloc
(sizeof
(*s
));
961 add_oid_to_tail
(struct objid
*head
, struct objid
*tail
)
971 new_type
(Typetype tt
)
973 Type
*t
= ecalloc
(1, sizeof
(*t
));
978 static struct constraint_spec
*
979 new_constraint_spec
(enum ctype ct
)
981 struct constraint_spec
*c
= ecalloc
(1, sizeof
(*c
));
986 static void fix_labels2
(Type
*t
, const char *prefix
);
987 static void fix_labels1
(struct memhead
*members
, const char *prefix
)
993 ASN1_TAILQ_FOREACH
(m
, members
, members
) {
994 asprintf
(&m
->label
, "%s_%s", prefix
, m
->gen_name
);
995 if
(m
->label
== NULL
)
998 fix_labels2
(m
->type
, m
->label
);
1002 static void fix_labels2
(Type
*t
, const char *prefix
)
1004 for
(; t
; t
= t
->subtype
)
1005 fix_labels1
(t
->members
, prefix
);
1009 fix_labels
(Symbol
*s
)
1012 asprintf
(&p
, "choice_%s", s
->gen_name
);
1015 fix_labels2
(s
->type
, p
);