1 1.1 elric /* $NetBSD: asn1parse.y,v 1.2 2017/01/28 21:31:45 christos Exp $ */ 2 1.1 elric 3 1.1 elric /* 4 1.1 elric * Copyright (c) 1997 - 2007 Kungliga Tekniska Hgskolan 5 1.1 elric * (Royal Institute of Technology, Stockholm, Sweden). 6 1.1 elric * All rights reserved. 7 1.1 elric * 8 1.1 elric * Portions Copyright (c) 2009 Apple Inc. All rights reserved. 9 1.1 elric * 10 1.1 elric * Redistribution and use in source and binary forms, with or without 11 1.1 elric * modification, are permitted provided that the following conditions 12 1.1 elric * are met: 13 1.1 elric * 14 1.1 elric * 1. Redistributions of source code must retain the above copyright 15 1.1 elric * notice, this list of conditions and the following disclaimer. 16 1.1 elric * 17 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright 18 1.1 elric * notice, this list of conditions and the following disclaimer in the 19 1.1 elric * documentation and/or other materials provided with the distribution. 20 1.1 elric * 21 1.1 elric * 3. Neither the name of the Institute nor the names of its contributors 22 1.1 elric * may be used to endorse or promote products derived from this software 23 1.1 elric * without specific prior written permission. 24 1.1 elric * 25 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 26 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 29 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 1.1 elric * SUCH DAMAGE. 36 1.1 elric */ 37 1.1 elric 38 1.2 christos /* Id */ 39 1.1 elric 40 1.1 elric %{ 41 1.1 elric 42 1.1 elric #include <config.h> 43 1.1 elric 44 1.1 elric #include <stdio.h> 45 1.1 elric #include <stdlib.h> 46 1.2 christos #include <stdint.h> 47 1.1 elric #include <string.h> 48 1.1 elric #include "symbol.h" 49 1.1 elric #include "lex.h" 50 1.1 elric #include "gen_locl.h" 51 1.1 elric #include "der.h" 52 1.1 elric 53 1.1 elric static Type *new_type (Typetype t); 54 1.1 elric static struct constraint_spec *new_constraint_spec(enum ctype); 55 1.1 elric static Type *new_tag(int tagclass, int tagvalue, int tagenv, Type *oldtype); 56 1.1 elric void yyerror (const char *); 57 1.1 elric static struct objid *new_objid(const char *label, int value); 58 1.1 elric static void add_oid_to_tail(struct objid *, struct objid *); 59 1.1 elric static void fix_labels(Symbol *s); 60 1.1 elric 61 1.1 elric struct string_list { 62 1.1 elric char *string; 63 1.1 elric struct string_list *next; 64 1.1 elric }; 65 1.1 elric 66 1.2 christos static int default_tag_env = TE_EXPLICIT; 67 1.2 christos 68 1.1 elric /* Declarations for Bison */ 69 1.1 elric #define YYMALLOC malloc 70 1.1 elric #define YYFREE free 71 1.1 elric 72 1.1 elric %} 73 1.1 elric 74 1.1 elric %union { 75 1.2 christos int64_t constant; 76 1.1 elric struct value *value; 77 1.1 elric struct range *range; 78 1.1 elric char *name; 79 1.1 elric Type *type; 80 1.1 elric Member *member; 81 1.1 elric struct objid *objid; 82 1.1 elric char *defval; 83 1.1 elric struct string_list *sl; 84 1.1 elric struct tagtype tag; 85 1.1 elric struct memhead *members; 86 1.1 elric struct constraint_spec *constraint_spec; 87 1.1 elric } 88 1.1 elric 89 1.1 elric %token kw_ABSENT 90 1.1 elric %token kw_ABSTRACT_SYNTAX 91 1.1 elric %token kw_ALL 92 1.1 elric %token kw_APPLICATION 93 1.1 elric %token kw_AUTOMATIC 94 1.1 elric %token kw_BEGIN 95 1.1 elric %token kw_BIT 96 1.1 elric %token kw_BMPString 97 1.1 elric %token kw_BOOLEAN 98 1.1 elric %token kw_BY 99 1.1 elric %token kw_CHARACTER 100 1.1 elric %token kw_CHOICE 101 1.1 elric %token kw_CLASS 102 1.1 elric %token kw_COMPONENT 103 1.1 elric %token kw_COMPONENTS 104 1.1 elric %token kw_CONSTRAINED 105 1.1 elric %token kw_CONTAINING 106 1.1 elric %token kw_DEFAULT 107 1.1 elric %token kw_DEFINITIONS 108 1.1 elric %token kw_EMBEDDED 109 1.1 elric %token kw_ENCODED 110 1.1 elric %token kw_END 111 1.1 elric %token kw_ENUMERATED 112 1.1 elric %token kw_EXCEPT 113 1.1 elric %token kw_EXPLICIT 114 1.1 elric %token kw_EXPORTS 115 1.1 elric %token kw_EXTENSIBILITY 116 1.1 elric %token kw_EXTERNAL 117 1.1 elric %token kw_FALSE 118 1.1 elric %token kw_FROM 119 1.1 elric %token kw_GeneralString 120 1.1 elric %token kw_GeneralizedTime 121 1.1 elric %token kw_GraphicString 122 1.1 elric %token kw_IA5String 123 1.1 elric %token kw_IDENTIFIER 124 1.1 elric %token kw_IMPLICIT 125 1.1 elric %token kw_IMPLIED 126 1.1 elric %token kw_IMPORTS 127 1.1 elric %token kw_INCLUDES 128 1.1 elric %token kw_INSTANCE 129 1.1 elric %token kw_INTEGER 130 1.1 elric %token kw_INTERSECTION 131 1.1 elric %token kw_ISO646String 132 1.1 elric %token kw_MAX 133 1.1 elric %token kw_MIN 134 1.1 elric %token kw_MINUS_INFINITY 135 1.1 elric %token kw_NULL 136 1.1 elric %token kw_NumericString 137 1.1 elric %token kw_OBJECT 138 1.1 elric %token kw_OCTET 139 1.1 elric %token kw_OF 140 1.1 elric %token kw_OPTIONAL 141 1.1 elric %token kw_ObjectDescriptor 142 1.1 elric %token kw_PATTERN 143 1.1 elric %token kw_PDV 144 1.1 elric %token kw_PLUS_INFINITY 145 1.1 elric %token kw_PRESENT 146 1.1 elric %token kw_PRIVATE 147 1.1 elric %token kw_PrintableString 148 1.1 elric %token kw_REAL 149 1.1 elric %token kw_RELATIVE_OID 150 1.1 elric %token kw_SEQUENCE 151 1.1 elric %token kw_SET 152 1.1 elric %token kw_SIZE 153 1.1 elric %token kw_STRING 154 1.1 elric %token kw_SYNTAX 155 1.1 elric %token kw_T61String 156 1.1 elric %token kw_TAGS 157 1.1 elric %token kw_TRUE 158 1.1 elric %token kw_TYPE_IDENTIFIER 159 1.1 elric %token kw_TeletexString 160 1.1 elric %token kw_UNION 161 1.1 elric %token kw_UNIQUE 162 1.1 elric %token kw_UNIVERSAL 163 1.1 elric %token kw_UTCTime 164 1.1 elric %token kw_UTF8String 165 1.1 elric %token kw_UniversalString 166 1.1 elric %token kw_VideotexString 167 1.1 elric %token kw_VisibleString 168 1.1 elric %token kw_WITH 169 1.1 elric 170 1.1 elric %token RANGE 171 1.1 elric %token EEQUAL 172 1.1 elric %token ELLIPSIS 173 1.1 elric 174 1.1 elric %token <name> IDENTIFIER referencename 175 1.1 elric %token <name> STRING 176 1.1 elric 177 1.1 elric %token <constant> NUMBER 178 1.1 elric %type <constant> SignedNumber 179 1.1 elric %type <constant> Class tagenv 180 1.1 elric 181 1.1 elric %type <value> Value 182 1.1 elric %type <value> BuiltinValue 183 1.1 elric %type <value> IntegerValue 184 1.1 elric %type <value> BooleanValue 185 1.1 elric %type <value> ObjectIdentifierValue 186 1.1 elric %type <value> CharacterStringValue 187 1.1 elric %type <value> NullValue 188 1.1 elric %type <value> DefinedValue 189 1.1 elric %type <value> ReferencedValue 190 1.1 elric %type <value> Valuereference 191 1.1 elric 192 1.1 elric %type <type> Type 193 1.1 elric %type <type> BuiltinType 194 1.1 elric %type <type> BitStringType 195 1.1 elric %type <type> BooleanType 196 1.1 elric %type <type> ChoiceType 197 1.1 elric %type <type> ConstrainedType 198 1.1 elric %type <type> EnumeratedType 199 1.1 elric %type <type> IntegerType 200 1.1 elric %type <type> NullType 201 1.1 elric %type <type> OctetStringType 202 1.1 elric %type <type> SequenceType 203 1.1 elric %type <type> SequenceOfType 204 1.1 elric %type <type> SetType 205 1.1 elric %type <type> SetOfType 206 1.1 elric %type <type> TaggedType 207 1.1 elric %type <type> ReferencedType 208 1.1 elric %type <type> DefinedType 209 1.1 elric %type <type> UsefulType 210 1.1 elric %type <type> ObjectIdentifierType 211 1.1 elric %type <type> CharacterStringType 212 1.1 elric %type <type> RestrictedCharactedStringType 213 1.1 elric 214 1.1 elric %type <tag> Tag 215 1.1 elric 216 1.1 elric %type <member> ComponentType 217 1.1 elric %type <member> NamedBit 218 1.1 elric %type <member> NamedNumber 219 1.1 elric %type <member> NamedType 220 1.1 elric %type <members> ComponentTypeList 221 1.1 elric %type <members> Enumerations 222 1.1 elric %type <members> NamedBitList 223 1.1 elric %type <members> NamedNumberList 224 1.1 elric 225 1.1 elric %type <objid> objid objid_list objid_element objid_opt 226 1.1 elric %type <range> range size 227 1.1 elric 228 1.1 elric %type <sl> referencenames 229 1.1 elric 230 1.1 elric %type <constraint_spec> Constraint 231 1.1 elric %type <constraint_spec> ConstraintSpec 232 1.1 elric %type <constraint_spec> GeneralConstraint 233 1.1 elric %type <constraint_spec> ContentsConstraint 234 1.1 elric %type <constraint_spec> UserDefinedConstraint 235 1.1 elric 236 1.1 elric 237 1.1 elric 238 1.1 elric %start ModuleDefinition 239 1.1 elric 240 1.1 elric %% 241 1.1 elric 242 1.1 elric ModuleDefinition: IDENTIFIER objid_opt kw_DEFINITIONS TagDefault ExtensionDefault 243 1.1 elric EEQUAL kw_BEGIN ModuleBody kw_END 244 1.1 elric { 245 1.1 elric checkundefined(); 246 1.1 elric } 247 1.1 elric ; 248 1.1 elric 249 1.2 christos TagDefault : kw_EXPLICIT kw_TAGS 250 1.2 christos { default_tag_env = TE_EXPLICIT; } 251 1.1 elric | kw_IMPLICIT kw_TAGS 252 1.2 christos { default_tag_env = TE_IMPLICIT; } 253 1.1 elric | kw_AUTOMATIC kw_TAGS 254 1.1 elric { lex_error_message("automatic tagging is not supported"); } 255 1.1 elric | /* empty */ 256 1.1 elric ; 257 1.1 elric 258 1.1 elric ExtensionDefault: kw_EXTENSIBILITY kw_IMPLIED 259 1.1 elric { lex_error_message("no extensibility options supported"); } 260 1.1 elric | /* empty */ 261 1.1 elric ; 262 1.1 elric 263 1.1 elric ModuleBody : Exports Imports AssignmentList 264 1.1 elric | /* empty */ 265 1.1 elric ; 266 1.1 elric 267 1.1 elric Imports : kw_IMPORTS SymbolsImported ';' 268 1.1 elric | /* empty */ 269 1.1 elric ; 270 1.1 elric 271 1.1 elric SymbolsImported : SymbolsFromModuleList 272 1.1 elric | /* empty */ 273 1.1 elric ; 274 1.1 elric 275 1.1 elric SymbolsFromModuleList: SymbolsFromModule 276 1.1 elric | SymbolsFromModuleList SymbolsFromModule 277 1.1 elric ; 278 1.1 elric 279 1.1 elric SymbolsFromModule: referencenames kw_FROM IDENTIFIER objid_opt 280 1.1 elric { 281 1.1 elric struct string_list *sl; 282 1.1 elric for(sl = $1; sl != NULL; sl = sl->next) { 283 1.1 elric Symbol *s = addsym(sl->string); 284 1.1 elric s->stype = Stype; 285 1.1 elric gen_template_import(s); 286 1.1 elric } 287 1.1 elric add_import($3); 288 1.1 elric } 289 1.1 elric ; 290 1.1 elric 291 1.1 elric Exports : kw_EXPORTS referencenames ';' 292 1.1 elric { 293 1.1 elric struct string_list *sl; 294 1.1 elric for(sl = $2; sl != NULL; sl = sl->next) 295 1.1 elric add_export(sl->string); 296 1.1 elric } 297 1.1 elric | kw_EXPORTS kw_ALL 298 1.1 elric | /* empty */ 299 1.1 elric ; 300 1.1 elric 301 1.1 elric AssignmentList : Assignment 302 1.1 elric | Assignment AssignmentList 303 1.1 elric ; 304 1.1 elric 305 1.1 elric Assignment : TypeAssignment 306 1.1 elric | ValueAssignment 307 1.1 elric ; 308 1.1 elric 309 1.1 elric referencenames : IDENTIFIER ',' referencenames 310 1.1 elric { 311 1.1 elric $$ = emalloc(sizeof(*$$)); 312 1.1 elric $$->string = $1; 313 1.1 elric $$->next = $3; 314 1.1 elric } 315 1.1 elric | IDENTIFIER 316 1.1 elric { 317 1.1 elric $$ = emalloc(sizeof(*$$)); 318 1.1 elric $$->string = $1; 319 1.1 elric $$->next = NULL; 320 1.1 elric } 321 1.1 elric ; 322 1.1 elric 323 1.1 elric TypeAssignment : IDENTIFIER EEQUAL Type 324 1.1 elric { 325 1.1 elric Symbol *s = addsym ($1); 326 1.1 elric s->stype = Stype; 327 1.1 elric s->type = $3; 328 1.1 elric fix_labels(s); 329 1.1 elric generate_type (s); 330 1.1 elric } 331 1.1 elric ; 332 1.1 elric 333 1.1 elric Type : BuiltinType 334 1.1 elric | ReferencedType 335 1.1 elric | ConstrainedType 336 1.1 elric ; 337 1.1 elric 338 1.1 elric BuiltinType : BitStringType 339 1.1 elric | BooleanType 340 1.1 elric | CharacterStringType 341 1.1 elric | ChoiceType 342 1.1 elric | EnumeratedType 343 1.1 elric | IntegerType 344 1.1 elric | NullType 345 1.1 elric | ObjectIdentifierType 346 1.1 elric | OctetStringType 347 1.1 elric | SequenceType 348 1.1 elric | SequenceOfType 349 1.1 elric | SetType 350 1.1 elric | SetOfType 351 1.1 elric | TaggedType 352 1.1 elric ; 353 1.1 elric 354 1.1 elric BooleanType : kw_BOOLEAN 355 1.1 elric { 356 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_Boolean, 357 1.1 elric TE_EXPLICIT, new_type(TBoolean)); 358 1.1 elric } 359 1.1 elric ; 360 1.1 elric 361 1.1 elric range : '(' Value RANGE Value ')' 362 1.1 elric { 363 1.1 elric if($2->type != integervalue) 364 1.1 elric lex_error_message("Non-integer used in first part of range"); 365 1.1 elric if($2->type != integervalue) 366 1.1 elric lex_error_message("Non-integer in second part of range"); 367 1.1 elric $$ = ecalloc(1, sizeof(*$$)); 368 1.1 elric $$->min = $2->u.integervalue; 369 1.1 elric $$->max = $4->u.integervalue; 370 1.1 elric } 371 1.1 elric | '(' Value RANGE kw_MAX ')' 372 1.2 christos { 373 1.1 elric if($2->type != integervalue) 374 1.1 elric lex_error_message("Non-integer in first part of range"); 375 1.1 elric $$ = ecalloc(1, sizeof(*$$)); 376 1.1 elric $$->min = $2->u.integervalue; 377 1.2 christos $$->max = INT_MAX; 378 1.1 elric } 379 1.1 elric | '(' kw_MIN RANGE Value ')' 380 1.2 christos { 381 1.1 elric if($4->type != integervalue) 382 1.1 elric lex_error_message("Non-integer in second part of range"); 383 1.1 elric $$ = ecalloc(1, sizeof(*$$)); 384 1.2 christos $$->min = INT_MIN; 385 1.1 elric $$->max = $4->u.integervalue; 386 1.1 elric } 387 1.1 elric | '(' Value ')' 388 1.1 elric { 389 1.1 elric if($2->type != integervalue) 390 1.1 elric lex_error_message("Non-integer used in limit"); 391 1.1 elric $$ = ecalloc(1, sizeof(*$$)); 392 1.1 elric $$->min = $2->u.integervalue; 393 1.1 elric $$->max = $2->u.integervalue; 394 1.1 elric } 395 1.1 elric ; 396 1.1 elric 397 1.1 elric 398 1.1 elric IntegerType : kw_INTEGER 399 1.1 elric { 400 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_Integer, 401 1.1 elric TE_EXPLICIT, new_type(TInteger)); 402 1.1 elric } 403 1.1 elric | kw_INTEGER range 404 1.1 elric { 405 1.1 elric $$ = new_type(TInteger); 406 1.1 elric $$->range = $2; 407 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_Integer, TE_EXPLICIT, $$); 408 1.1 elric } 409 1.1 elric | kw_INTEGER '{' NamedNumberList '}' 410 1.1 elric { 411 1.1 elric $$ = new_type(TInteger); 412 1.1 elric $$->members = $3; 413 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_Integer, TE_EXPLICIT, $$); 414 1.1 elric } 415 1.1 elric ; 416 1.1 elric 417 1.1 elric NamedNumberList : NamedNumber 418 1.1 elric { 419 1.1 elric $$ = emalloc(sizeof(*$$)); 420 1.1 elric ASN1_TAILQ_INIT($$); 421 1.1 elric ASN1_TAILQ_INSERT_HEAD($$, $1, members); 422 1.1 elric } 423 1.1 elric | NamedNumberList ',' NamedNumber 424 1.1 elric { 425 1.1 elric ASN1_TAILQ_INSERT_TAIL($1, $3, members); 426 1.1 elric $$ = $1; 427 1.1 elric } 428 1.1 elric | NamedNumberList ',' ELLIPSIS 429 1.1 elric { $$ = $1; } /* XXX used for Enumerations */ 430 1.1 elric ; 431 1.1 elric 432 1.1 elric NamedNumber : IDENTIFIER '(' SignedNumber ')' 433 1.1 elric { 434 1.1 elric $$ = emalloc(sizeof(*$$)); 435 1.1 elric $$->name = $1; 436 1.1 elric $$->gen_name = estrdup($1); 437 1.1 elric output_name ($$->gen_name); 438 1.1 elric $$->val = $3; 439 1.1 elric $$->optional = 0; 440 1.1 elric $$->ellipsis = 0; 441 1.1 elric $$->type = NULL; 442 1.1 elric } 443 1.1 elric ; 444 1.1 elric 445 1.1 elric EnumeratedType : kw_ENUMERATED '{' Enumerations '}' 446 1.1 elric { 447 1.1 elric $$ = new_type(TInteger); 448 1.1 elric $$->members = $3; 449 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_Enumerated, TE_EXPLICIT, $$); 450 1.1 elric } 451 1.1 elric ; 452 1.1 elric 453 1.1 elric Enumerations : NamedNumberList /* XXX */ 454 1.1 elric ; 455 1.1 elric 456 1.1 elric BitStringType : kw_BIT kw_STRING 457 1.1 elric { 458 1.1 elric $$ = new_type(TBitString); 459 1.1 elric $$->members = emalloc(sizeof(*$$->members)); 460 1.1 elric ASN1_TAILQ_INIT($$->members); 461 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_BitString, TE_EXPLICIT, $$); 462 1.1 elric } 463 1.1 elric | kw_BIT kw_STRING '{' NamedBitList '}' 464 1.1 elric { 465 1.1 elric $$ = new_type(TBitString); 466 1.1 elric $$->members = $4; 467 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_BitString, TE_EXPLICIT, $$); 468 1.1 elric } 469 1.1 elric ; 470 1.1 elric 471 1.1 elric ObjectIdentifierType: kw_OBJECT kw_IDENTIFIER 472 1.1 elric { 473 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_OID, 474 1.1 elric TE_EXPLICIT, new_type(TOID)); 475 1.1 elric } 476 1.1 elric ; 477 1.1 elric OctetStringType : kw_OCTET kw_STRING size 478 1.1 elric { 479 1.1 elric Type *t = new_type(TOctetString); 480 1.1 elric t->range = $3; 481 1.2 christos if (t->range) { 482 1.2 christos if (t->range->min < 0) 483 1.2 christos lex_error_message("can't use a negative SIZE range " 484 1.2 christos "length for OCTET STRING"); 485 1.2 christos } 486 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_OctetString, 487 1.1 elric TE_EXPLICIT, t); 488 1.1 elric } 489 1.1 elric ; 490 1.1 elric 491 1.1 elric NullType : kw_NULL 492 1.1 elric { 493 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_Null, 494 1.1 elric TE_EXPLICIT, new_type(TNull)); 495 1.1 elric } 496 1.1 elric ; 497 1.1 elric 498 1.1 elric size : 499 1.1 elric { $$ = NULL; } 500 1.1 elric | kw_SIZE range 501 1.1 elric { $$ = $2; } 502 1.1 elric ; 503 1.1 elric 504 1.1 elric 505 1.1 elric SequenceType : kw_SEQUENCE '{' /* ComponentTypeLists */ ComponentTypeList '}' 506 1.1 elric { 507 1.1 elric $$ = new_type(TSequence); 508 1.1 elric $$->members = $3; 509 1.2 christos $$ = new_tag(ASN1_C_UNIV, UT_Sequence, default_tag_env, $$); 510 1.1 elric } 511 1.1 elric | kw_SEQUENCE '{' '}' 512 1.1 elric { 513 1.1 elric $$ = new_type(TSequence); 514 1.1 elric $$->members = NULL; 515 1.2 christos $$ = new_tag(ASN1_C_UNIV, UT_Sequence, default_tag_env, $$); 516 1.1 elric } 517 1.1 elric ; 518 1.1 elric 519 1.1 elric SequenceOfType : kw_SEQUENCE size kw_OF Type 520 1.1 elric { 521 1.1 elric $$ = new_type(TSequenceOf); 522 1.1 elric $$->range = $2; 523 1.2 christos if ($$->range) { 524 1.2 christos if ($$->range->min < 0) 525 1.2 christos lex_error_message("can't use a negative SIZE range " 526 1.2 christos "length for SEQUENCE OF"); 527 1.2 christos } 528 1.2 christos 529 1.1 elric $$->subtype = $4; 530 1.2 christos $$ = new_tag(ASN1_C_UNIV, UT_Sequence, default_tag_env, $$); 531 1.1 elric } 532 1.1 elric ; 533 1.1 elric 534 1.1 elric SetType : kw_SET '{' /* ComponentTypeLists */ ComponentTypeList '}' 535 1.1 elric { 536 1.1 elric $$ = new_type(TSet); 537 1.1 elric $$->members = $3; 538 1.2 christos $$ = new_tag(ASN1_C_UNIV, UT_Set, default_tag_env, $$); 539 1.1 elric } 540 1.1 elric | kw_SET '{' '}' 541 1.1 elric { 542 1.1 elric $$ = new_type(TSet); 543 1.1 elric $$->members = NULL; 544 1.2 christos $$ = new_tag(ASN1_C_UNIV, UT_Set, default_tag_env, $$); 545 1.1 elric } 546 1.1 elric ; 547 1.1 elric 548 1.1 elric SetOfType : kw_SET kw_OF Type 549 1.1 elric { 550 1.1 elric $$ = new_type(TSetOf); 551 1.1 elric $$->subtype = $3; 552 1.2 christos $$ = new_tag(ASN1_C_UNIV, UT_Set, default_tag_env, $$); 553 1.1 elric } 554 1.1 elric ; 555 1.1 elric 556 1.1 elric ChoiceType : kw_CHOICE '{' /* AlternativeTypeLists */ ComponentTypeList '}' 557 1.1 elric { 558 1.1 elric $$ = new_type(TChoice); 559 1.1 elric $$->members = $3; 560 1.1 elric } 561 1.1 elric ; 562 1.1 elric 563 1.1 elric ReferencedType : DefinedType 564 1.1 elric | UsefulType 565 1.1 elric ; 566 1.1 elric 567 1.1 elric DefinedType : IDENTIFIER 568 1.1 elric { 569 1.1 elric Symbol *s = addsym($1); 570 1.1 elric $$ = new_type(TType); 571 1.1 elric if(s->stype != Stype && s->stype != SUndefined) 572 1.1 elric lex_error_message ("%s is not a type\n", $1); 573 1.1 elric else 574 1.1 elric $$->symbol = s; 575 1.1 elric } 576 1.1 elric ; 577 1.1 elric 578 1.1 elric UsefulType : kw_GeneralizedTime 579 1.1 elric { 580 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_GeneralizedTime, 581 1.1 elric TE_EXPLICIT, new_type(TGeneralizedTime)); 582 1.1 elric } 583 1.1 elric | kw_UTCTime 584 1.1 elric { 585 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_UTCTime, 586 1.1 elric TE_EXPLICIT, new_type(TUTCTime)); 587 1.1 elric } 588 1.1 elric ; 589 1.1 elric 590 1.1 elric ConstrainedType : Type Constraint 591 1.1 elric { 592 1.1 elric /* if (Constraint.type == contentConstrant) { 593 1.1 elric assert(Constraint.u.constraint.type == octetstring|bitstring-w/o-NamedBitList); // remember to check type reference too 594 1.1 elric if (Constraint.u.constraint.type) { 595 1.1 elric assert((Constraint.u.constraint.type.length % 8) == 0); 596 1.1 elric } 597 1.1 elric } 598 1.1 elric if (Constraint.u.constraint.encoding) { 599 1.1 elric type == der-oid|ber-oid 600 1.1 elric } 601 1.1 elric */ 602 1.1 elric } 603 1.1 elric ; 604 1.1 elric 605 1.1 elric 606 1.1 elric Constraint : '(' ConstraintSpec ')' 607 1.1 elric { 608 1.1 elric $$ = $2; 609 1.1 elric } 610 1.1 elric ; 611 1.1 elric 612 1.1 elric ConstraintSpec : GeneralConstraint 613 1.1 elric ; 614 1.1 elric 615 1.1 elric GeneralConstraint: ContentsConstraint 616 1.1 elric | UserDefinedConstraint 617 1.1 elric ; 618 1.1 elric 619 1.1 elric ContentsConstraint: kw_CONTAINING Type 620 1.1 elric { 621 1.1 elric $$ = new_constraint_spec(CT_CONTENTS); 622 1.1 elric $$->u.content.type = $2; 623 1.1 elric $$->u.content.encoding = NULL; 624 1.1 elric } 625 1.1 elric | kw_ENCODED kw_BY Value 626 1.1 elric { 627 1.1 elric if ($3->type != objectidentifiervalue) 628 1.1 elric lex_error_message("Non-OID used in ENCODED BY constraint"); 629 1.1 elric $$ = new_constraint_spec(CT_CONTENTS); 630 1.1 elric $$->u.content.type = NULL; 631 1.1 elric $$->u.content.encoding = $3; 632 1.1 elric } 633 1.1 elric | kw_CONTAINING Type kw_ENCODED kw_BY Value 634 1.1 elric { 635 1.1 elric if ($5->type != objectidentifiervalue) 636 1.1 elric lex_error_message("Non-OID used in ENCODED BY constraint"); 637 1.1 elric $$ = new_constraint_spec(CT_CONTENTS); 638 1.1 elric $$->u.content.type = $2; 639 1.1 elric $$->u.content.encoding = $5; 640 1.1 elric } 641 1.1 elric ; 642 1.1 elric 643 1.1 elric UserDefinedConstraint: kw_CONSTRAINED kw_BY '{' '}' 644 1.1 elric { 645 1.1 elric $$ = new_constraint_spec(CT_USER); 646 1.1 elric } 647 1.1 elric ; 648 1.1 elric 649 1.1 elric TaggedType : Tag tagenv Type 650 1.1 elric { 651 1.1 elric $$ = new_type(TTag); 652 1.1 elric $$->tag = $1; 653 1.1 elric $$->tag.tagenv = $2; 654 1.2 christos if (template_flag) { 655 1.2 christos $$->subtype = $3; 656 1.2 christos } else { 657 1.2 christos if($3->type == TTag && $2 == TE_IMPLICIT) { 658 1.1 elric $$->subtype = $3->subtype; 659 1.1 elric free($3); 660 1.2 christos } else { 661 1.1 elric $$->subtype = $3; 662 1.2 christos } 663 1.2 christos } 664 1.1 elric } 665 1.1 elric ; 666 1.1 elric 667 1.1 elric Tag : '[' Class NUMBER ']' 668 1.1 elric { 669 1.1 elric $$.tagclass = $2; 670 1.1 elric $$.tagvalue = $3; 671 1.2 christos $$.tagenv = default_tag_env; 672 1.1 elric } 673 1.1 elric ; 674 1.1 elric 675 1.1 elric Class : /* */ 676 1.1 elric { 677 1.1 elric $$ = ASN1_C_CONTEXT; 678 1.1 elric } 679 1.1 elric | kw_UNIVERSAL 680 1.1 elric { 681 1.1 elric $$ = ASN1_C_UNIV; 682 1.1 elric } 683 1.1 elric | kw_APPLICATION 684 1.1 elric { 685 1.1 elric $$ = ASN1_C_APPL; 686 1.1 elric } 687 1.1 elric | kw_PRIVATE 688 1.1 elric { 689 1.1 elric $$ = ASN1_C_PRIVATE; 690 1.1 elric } 691 1.1 elric ; 692 1.1 elric 693 1.1 elric tagenv : /* */ 694 1.1 elric { 695 1.2 christos $$ = default_tag_env; 696 1.1 elric } 697 1.1 elric | kw_EXPLICIT 698 1.1 elric { 699 1.2 christos $$ = default_tag_env; 700 1.1 elric } 701 1.1 elric | kw_IMPLICIT 702 1.1 elric { 703 1.1 elric $$ = TE_IMPLICIT; 704 1.1 elric } 705 1.1 elric ; 706 1.1 elric 707 1.1 elric 708 1.1 elric ValueAssignment : IDENTIFIER Type EEQUAL Value 709 1.1 elric { 710 1.1 elric Symbol *s; 711 1.1 elric s = addsym ($1); 712 1.1 elric 713 1.1 elric s->stype = SValue; 714 1.1 elric s->value = $4; 715 1.1 elric generate_constant (s); 716 1.1 elric } 717 1.1 elric ; 718 1.1 elric 719 1.1 elric CharacterStringType: RestrictedCharactedStringType 720 1.1 elric ; 721 1.1 elric 722 1.1 elric RestrictedCharactedStringType: kw_GeneralString 723 1.1 elric { 724 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_GeneralString, 725 1.1 elric TE_EXPLICIT, new_type(TGeneralString)); 726 1.1 elric } 727 1.1 elric | kw_TeletexString 728 1.1 elric { 729 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_TeletexString, 730 1.1 elric TE_EXPLICIT, new_type(TTeletexString)); 731 1.1 elric } 732 1.1 elric | kw_UTF8String 733 1.1 elric { 734 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_UTF8String, 735 1.1 elric TE_EXPLICIT, new_type(TUTF8String)); 736 1.1 elric } 737 1.1 elric | kw_PrintableString 738 1.1 elric { 739 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_PrintableString, 740 1.1 elric TE_EXPLICIT, new_type(TPrintableString)); 741 1.1 elric } 742 1.1 elric | kw_VisibleString 743 1.1 elric { 744 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_VisibleString, 745 1.1 elric TE_EXPLICIT, new_type(TVisibleString)); 746 1.1 elric } 747 1.1 elric | kw_IA5String 748 1.1 elric { 749 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_IA5String, 750 1.1 elric TE_EXPLICIT, new_type(TIA5String)); 751 1.1 elric } 752 1.1 elric | kw_BMPString 753 1.1 elric { 754 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_BMPString, 755 1.1 elric TE_EXPLICIT, new_type(TBMPString)); 756 1.1 elric } 757 1.1 elric | kw_UniversalString 758 1.1 elric { 759 1.1 elric $$ = new_tag(ASN1_C_UNIV, UT_UniversalString, 760 1.1 elric TE_EXPLICIT, new_type(TUniversalString)); 761 1.1 elric } 762 1.1 elric 763 1.1 elric ; 764 1.1 elric 765 1.1 elric ComponentTypeList: ComponentType 766 1.1 elric { 767 1.1 elric $$ = emalloc(sizeof(*$$)); 768 1.1 elric ASN1_TAILQ_INIT($$); 769 1.1 elric ASN1_TAILQ_INSERT_HEAD($$, $1, members); 770 1.1 elric } 771 1.1 elric | ComponentTypeList ',' ComponentType 772 1.1 elric { 773 1.1 elric ASN1_TAILQ_INSERT_TAIL($1, $3, members); 774 1.1 elric $$ = $1; 775 1.1 elric } 776 1.1 elric | ComponentTypeList ',' ELLIPSIS 777 1.1 elric { 778 1.1 elric struct member *m = ecalloc(1, sizeof(*m)); 779 1.1 elric m->name = estrdup("..."); 780 1.1 elric m->gen_name = estrdup("asn1_ellipsis"); 781 1.1 elric m->ellipsis = 1; 782 1.1 elric ASN1_TAILQ_INSERT_TAIL($1, m, members); 783 1.1 elric $$ = $1; 784 1.1 elric } 785 1.1 elric ; 786 1.1 elric 787 1.1 elric NamedType : IDENTIFIER Type 788 1.1 elric { 789 1.1 elric $$ = emalloc(sizeof(*$$)); 790 1.1 elric $$->name = $1; 791 1.1 elric $$->gen_name = estrdup($1); 792 1.1 elric output_name ($$->gen_name); 793 1.1 elric $$->type = $2; 794 1.1 elric $$->ellipsis = 0; 795 1.1 elric } 796 1.1 elric ; 797 1.1 elric 798 1.1 elric ComponentType : NamedType 799 1.1 elric { 800 1.1 elric $$ = $1; 801 1.1 elric $$->optional = 0; 802 1.1 elric $$->defval = NULL; 803 1.1 elric } 804 1.1 elric | NamedType kw_OPTIONAL 805 1.1 elric { 806 1.1 elric $$ = $1; 807 1.1 elric $$->optional = 1; 808 1.1 elric $$->defval = NULL; 809 1.1 elric } 810 1.1 elric | NamedType kw_DEFAULT Value 811 1.1 elric { 812 1.1 elric $$ = $1; 813 1.1 elric $$->optional = 0; 814 1.1 elric $$->defval = $3; 815 1.1 elric } 816 1.1 elric ; 817 1.1 elric 818 1.1 elric NamedBitList : NamedBit 819 1.1 elric { 820 1.1 elric $$ = emalloc(sizeof(*$$)); 821 1.1 elric ASN1_TAILQ_INIT($$); 822 1.1 elric ASN1_TAILQ_INSERT_HEAD($$, $1, members); 823 1.1 elric } 824 1.1 elric | NamedBitList ',' NamedBit 825 1.1 elric { 826 1.1 elric ASN1_TAILQ_INSERT_TAIL($1, $3, members); 827 1.1 elric $$ = $1; 828 1.1 elric } 829 1.1 elric ; 830 1.1 elric 831 1.1 elric NamedBit : IDENTIFIER '(' NUMBER ')' 832 1.1 elric { 833 1.1 elric $$ = emalloc(sizeof(*$$)); 834 1.1 elric $$->name = $1; 835 1.1 elric $$->gen_name = estrdup($1); 836 1.1 elric output_name ($$->gen_name); 837 1.1 elric $$->val = $3; 838 1.1 elric $$->optional = 0; 839 1.1 elric $$->ellipsis = 0; 840 1.1 elric $$->type = NULL; 841 1.1 elric } 842 1.1 elric ; 843 1.1 elric 844 1.1 elric objid_opt : objid 845 1.1 elric | /* empty */ { $$ = NULL; } 846 1.1 elric ; 847 1.1 elric 848 1.1 elric objid : '{' objid_list '}' 849 1.1 elric { 850 1.1 elric $$ = $2; 851 1.1 elric } 852 1.1 elric ; 853 1.1 elric 854 1.1 elric objid_list : /* empty */ 855 1.1 elric { 856 1.1 elric $$ = NULL; 857 1.1 elric } 858 1.1 elric | objid_element objid_list 859 1.1 elric { 860 1.1 elric if ($2) { 861 1.1 elric $$ = $2; 862 1.1 elric add_oid_to_tail($2, $1); 863 1.1 elric } else { 864 1.1 elric $$ = $1; 865 1.1 elric } 866 1.1 elric } 867 1.1 elric ; 868 1.1 elric 869 1.1 elric objid_element : IDENTIFIER '(' NUMBER ')' 870 1.1 elric { 871 1.1 elric $$ = new_objid($1, $3); 872 1.1 elric } 873 1.1 elric | IDENTIFIER 874 1.1 elric { 875 1.1 elric Symbol *s = addsym($1); 876 1.1 elric if(s->stype != SValue || 877 1.1 elric s->value->type != objectidentifiervalue) { 878 1.1 elric lex_error_message("%s is not an object identifier\n", 879 1.1 elric s->name); 880 1.1 elric exit(1); 881 1.1 elric } 882 1.1 elric $$ = s->value->u.objectidentifiervalue; 883 1.1 elric } 884 1.1 elric | NUMBER 885 1.1 elric { 886 1.1 elric $$ = new_objid(NULL, $1); 887 1.1 elric } 888 1.1 elric ; 889 1.1 elric 890 1.1 elric Value : BuiltinValue 891 1.1 elric | ReferencedValue 892 1.1 elric ; 893 1.1 elric 894 1.1 elric BuiltinValue : BooleanValue 895 1.1 elric | CharacterStringValue 896 1.1 elric | IntegerValue 897 1.1 elric | ObjectIdentifierValue 898 1.1 elric | NullValue 899 1.1 elric ; 900 1.1 elric 901 1.1 elric ReferencedValue : DefinedValue 902 1.1 elric ; 903 1.1 elric 904 1.1 elric DefinedValue : Valuereference 905 1.1 elric ; 906 1.1 elric 907 1.1 elric Valuereference : IDENTIFIER 908 1.1 elric { 909 1.1 elric Symbol *s = addsym($1); 910 1.1 elric if(s->stype != SValue) 911 1.1 elric lex_error_message ("%s is not a value\n", 912 1.1 elric s->name); 913 1.1 elric else 914 1.1 elric $$ = s->value; 915 1.1 elric } 916 1.1 elric ; 917 1.1 elric 918 1.1 elric CharacterStringValue: STRING 919 1.1 elric { 920 1.1 elric $$ = emalloc(sizeof(*$$)); 921 1.1 elric $$->type = stringvalue; 922 1.1 elric $$->u.stringvalue = $1; 923 1.1 elric } 924 1.1 elric ; 925 1.1 elric 926 1.1 elric BooleanValue : kw_TRUE 927 1.1 elric { 928 1.1 elric $$ = emalloc(sizeof(*$$)); 929 1.1 elric $$->type = booleanvalue; 930 1.1 elric $$->u.booleanvalue = 0; 931 1.1 elric } 932 1.1 elric | kw_FALSE 933 1.1 elric { 934 1.1 elric $$ = emalloc(sizeof(*$$)); 935 1.1 elric $$->type = booleanvalue; 936 1.1 elric $$->u.booleanvalue = 0; 937 1.1 elric } 938 1.1 elric ; 939 1.1 elric 940 1.1 elric IntegerValue : SignedNumber 941 1.1 elric { 942 1.1 elric $$ = emalloc(sizeof(*$$)); 943 1.1 elric $$->type = integervalue; 944 1.1 elric $$->u.integervalue = $1; 945 1.1 elric } 946 1.1 elric ; 947 1.1 elric 948 1.1 elric SignedNumber : NUMBER 949 1.1 elric ; 950 1.1 elric 951 1.1 elric NullValue : kw_NULL 952 1.1 elric { 953 1.1 elric } 954 1.1 elric ; 955 1.1 elric 956 1.1 elric ObjectIdentifierValue: objid 957 1.1 elric { 958 1.1 elric $$ = emalloc(sizeof(*$$)); 959 1.1 elric $$->type = objectidentifiervalue; 960 1.1 elric $$->u.objectidentifiervalue = $1; 961 1.1 elric } 962 1.1 elric ; 963 1.1 elric 964 1.1 elric %% 965 1.1 elric 966 1.1 elric void 967 1.1 elric yyerror (const char *s) 968 1.1 elric { 969 1.1 elric lex_error_message ("%s\n", s); 970 1.1 elric } 971 1.1 elric 972 1.1 elric static Type * 973 1.1 elric new_tag(int tagclass, int tagvalue, int tagenv, Type *oldtype) 974 1.1 elric { 975 1.1 elric Type *t; 976 1.1 elric if(oldtype->type == TTag && oldtype->tag.tagenv == TE_IMPLICIT) { 977 1.1 elric t = oldtype; 978 1.1 elric oldtype = oldtype->subtype; /* XXX */ 979 1.1 elric } else 980 1.1 elric t = new_type (TTag); 981 1.1 elric 982 1.1 elric t->tag.tagclass = tagclass; 983 1.1 elric t->tag.tagvalue = tagvalue; 984 1.1 elric t->tag.tagenv = tagenv; 985 1.1 elric t->subtype = oldtype; 986 1.1 elric return t; 987 1.1 elric } 988 1.1 elric 989 1.1 elric static struct objid * 990 1.1 elric new_objid(const char *label, int value) 991 1.1 elric { 992 1.1 elric struct objid *s; 993 1.1 elric s = emalloc(sizeof(*s)); 994 1.1 elric s->label = label; 995 1.1 elric s->value = value; 996 1.1 elric s->next = NULL; 997 1.1 elric return s; 998 1.1 elric } 999 1.1 elric 1000 1.1 elric static void 1001 1.1 elric add_oid_to_tail(struct objid *head, struct objid *tail) 1002 1.1 elric { 1003 1.1 elric struct objid *o; 1004 1.1 elric o = head; 1005 1.1 elric while (o->next) 1006 1.1 elric o = o->next; 1007 1.1 elric o->next = tail; 1008 1.1 elric } 1009 1.1 elric 1010 1.2 christos static unsigned long idcounter; 1011 1.2 christos 1012 1.1 elric static Type * 1013 1.1 elric new_type (Typetype tt) 1014 1.1 elric { 1015 1.1 elric Type *t = ecalloc(1, sizeof(*t)); 1016 1.1 elric t->type = tt; 1017 1.2 christos t->id = idcounter++; 1018 1.1 elric return t; 1019 1.1 elric } 1020 1.1 elric 1021 1.1 elric static struct constraint_spec * 1022 1.1 elric new_constraint_spec(enum ctype ct) 1023 1.1 elric { 1024 1.1 elric struct constraint_spec *c = ecalloc(1, sizeof(*c)); 1025 1.1 elric c->ctype = ct; 1026 1.1 elric return c; 1027 1.1 elric } 1028 1.1 elric 1029 1.1 elric static void fix_labels2(Type *t, const char *prefix); 1030 1.1 elric static void fix_labels1(struct memhead *members, const char *prefix) 1031 1.1 elric { 1032 1.1 elric Member *m; 1033 1.1 elric 1034 1.1 elric if(members == NULL) 1035 1.1 elric return; 1036 1.1 elric ASN1_TAILQ_FOREACH(m, members, members) { 1037 1.1 elric if (asprintf(&m->label, "%s_%s", prefix, m->gen_name) < 0) 1038 1.1 elric errx(1, "malloc"); 1039 1.1 elric if (m->label == NULL) 1040 1.1 elric errx(1, "malloc"); 1041 1.1 elric if(m->type != NULL) 1042 1.1 elric fix_labels2(m->type, m->label); 1043 1.1 elric } 1044 1.1 elric } 1045 1.1 elric 1046 1.1 elric static void fix_labels2(Type *t, const char *prefix) 1047 1.1 elric { 1048 1.1 elric for(; t; t = t->subtype) 1049 1.1 elric fix_labels1(t->members, prefix); 1050 1.1 elric } 1051 1.1 elric 1052 1.1 elric static void 1053 1.1 elric fix_labels(Symbol *s) 1054 1.1 elric { 1055 1.1 elric char *p = NULL; 1056 1.1 elric if (asprintf(&p, "choice_%s", s->gen_name) < 0 || p == NULL) 1057 1.1 elric errx(1, "malloc"); 1058 1.1 elric fix_labels2(s->type, p); 1059 1.1 elric free(p); 1060 1.1 elric } 1061