| 1 |
/* asn1-parse.y - ASN.1 grammar |
| 2 |
* Copyright (C) 2000, 2001 Fabio Fiorina |
| 3 |
* Copyright (C) 2001 Free Software Foundation, Inc. |
| 4 |
* Copyright (C) 2002, 2003, 2006, 2007, 2010, 2012 g10 Code GmbH |
| 5 |
* |
| 6 |
* This file is part of KSBA. |
| 7 |
* |
| 8 |
* KSBA is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of either |
| 10 |
* |
| 11 |
* - the GNU Lesser General Public License as published by the Free |
| 12 |
* Software Foundation; either version 3 of the License, or (at |
| 13 |
* your option) any later version. |
| 14 |
* |
| 15 |
* or |
| 16 |
* |
| 17 |
* - the GNU General Public License as published by the Free |
| 18 |
* Software Foundation; either version 2 of the License, or (at |
| 19 |
* your option) any later version. |
| 20 |
* |
| 21 |
* or both in parallel, as here. |
| 22 |
* |
| 23 |
* KSBA is distributed in the hope that it will be useful, but WITHOUT |
| 24 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 25 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 26 |
* License for more details. |
| 27 |
* |
| 28 |
* You should have received a copies of the GNU General Public License |
| 29 |
* and the GNU Lesser General Public License along with this program; |
| 30 |
* if not, see <http://www.gnu.org/licenses/>. |
| 31 |
*/ |
| 32 |
|
| 33 |
|
| 34 |
/*****************************************************/ |
| 35 |
/* File: x509_ASN.y */ |
| 36 |
/* Description: input file for 'bison' program. */ |
| 37 |
/* The output file is a parser (in C language) for */ |
| 38 |
/* ASN.1 syntax */ |
| 39 |
/*****************************************************/ |
| 40 |
|
| 41 |
|
| 42 |
%{ |
| 43 |
#ifndef BUILD_GENTOOLS |
| 44 |
# include <config.h> |
| 45 |
#endif |
| 46 |
#include <stdlib.h> |
| 47 |
#include <stdio.h> |
| 48 |
#include <string.h> |
| 49 |
#include <assert.h> |
| 50 |
#include <ctype.h> |
| 51 |
#include <errno.h> |
| 52 |
|
| 53 |
#ifdef BUILD_GENTOOLS |
| 54 |
# include "gen-help.h" |
| 55 |
#else |
| 56 |
# include "util.h" |
| 57 |
# include "ksba.h" |
| 58 |
#endif |
| 59 |
|
| 60 |
#include "asn1-func.h" |
| 61 |
|
| 62 |
/* It would be better to make yyparse static but there is no way to do |
| 63 |
this. Let's hope that this macros works. */ |
| 64 |
#define yyparse _ksba_asn1_yyparse |
| 65 |
|
| 66 |
/* #define YYDEBUG 1 */ |
| 67 |
#define MAX_STRING_LENGTH 129 |
| 68 |
|
| 69 |
|
| 70 |
/* constants used in the grammar */ |
| 71 |
enum { |
| 72 |
CONST_EXPLICIT = 1, |
| 73 |
CONST_IMPLICIT |
| 74 |
}; |
| 75 |
|
| 76 |
struct parser_control_s { |
| 77 |
FILE *fp; |
| 78 |
int lineno; |
| 79 |
int debug; |
| 80 |
int result_parse; |
| 81 |
AsnNode parse_tree; |
| 82 |
AsnNode all_nodes; |
| 83 |
}; |
| 84 |
#define PARSECTL ((struct parser_control_s *)parm) |
| 85 |
|
| 86 |
%} |
| 87 |
|
| 88 |
|
| 89 |
%param {void *parm} |
| 90 |
%define api.pure full |
| 91 |
%define parse.error verbose |
| 92 |
//%expect 1 |
| 93 |
|
| 94 |
%union { |
| 95 |
unsigned int constant; |
| 96 |
char str[MAX_STRING_LENGTH]; |
| 97 |
AsnNode node; |
| 98 |
} |
| 99 |
|
| 100 |
%{ |
| 101 |
static AsnNode new_node (struct parser_control_s *parsectl, node_type_t type); |
| 102 |
#define NEW_NODE(a) (new_node (PARSECTL, (a))) |
| 103 |
static void set_name (AsnNode node, const char *name); |
| 104 |
static void set_str_value (AsnNode node, const char *text); |
| 105 |
static void set_ulong_value (AsnNode node, const char *text); |
| 106 |
static void set_right (AsnNode node, AsnNode right); |
| 107 |
static void append_right (AsnNode node, AsnNode right); |
| 108 |
static void set_down (AsnNode node, AsnNode down); |
| 109 |
|
| 110 |
|
| 111 |
static int yylex (YYSTYPE *lvalp, void *parm); |
| 112 |
static void yyerror (void *parm, const char *s); |
| 113 |
%} |
| 114 |
|
| 115 |
%token ASSIG "::=" |
| 116 |
%token <str> NUM |
| 117 |
%token <str> IDENTIFIER |
| 118 |
%token OPTIONAL "OPTIONAL" |
| 119 |
%token INTEGER "INTEGER" |
| 120 |
%token SIZE "SIZE" |
| 121 |
%token OCTET "OCTET" |
| 122 |
%token STRING "STRING" |
| 123 |
%token SEQUENCE "SEQUENCE" |
| 124 |
%token BIT "BIT" |
| 125 |
%token UNIVERSAL "UNIVERSAL" |
| 126 |
%token PRIVATE "PRIVATE" |
| 127 |
%token DEFAULT "DEFAULT" |
| 128 |
%token CHOICE "CHOICE" |
| 129 |
%token OF "OF" |
| 130 |
%token OBJECT "OBJECT" |
| 131 |
%token STR_IDENTIFIER "IDENTIFIER" |
| 132 |
%token ksba_BOOLEAN "BOOLEAN" |
| 133 |
%token ksba_TRUE "TRUE" |
| 134 |
%token ksba_FALSE "FALSE" |
| 135 |
%token APPLICATION "APPLICATION" |
| 136 |
%token ANY "ANY" |
| 137 |
%token DEFINED "DEFINED" |
| 138 |
%token SET "SET" |
| 139 |
%token BY "BY" |
| 140 |
%token EXPLICIT "EXPLICIT" |
| 141 |
%token IMPLICIT "IMPLICIT" |
| 142 |
%token DEFINITIONS "DEFINITIONS" |
| 143 |
%token TAGS "TAGS" |
| 144 |
%token ksba_BEGIN "BEGIN" |
| 145 |
%token ksba_END "END" |
| 146 |
%token UTCTime "UTCTime" |
| 147 |
%token GeneralizedTime "GeneralizedTime" |
| 148 |
%token FROM "FROM" |
| 149 |
%token IMPORTS "IMPORTS" |
| 150 |
%token TOKEN_NULL "NULL" |
| 151 |
%token ENUMERATED "ENUMERATED" |
| 152 |
%token UTF8STRING "UTF8String" |
| 153 |
%token NUMERICSTRING "NumericString" |
| 154 |
%token PRINTABLESTRING "PrintableString" |
| 155 |
%token TELETEXSTRING "TeletexString" |
| 156 |
%token IA5STRING "IA5String" |
| 157 |
%token UNIVERSALSTRING "UniversalString" |
| 158 |
%token BMPSTRING "BMPString" |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
%type <node> octet_string_def constant constant_list type_assig_right |
| 163 |
%type <node> integer_def type_assig type_assig_list sequence_def type_def |
| 164 |
%type <node> bit_string_def default size_def choise_def object_def |
| 165 |
%type <node> boolean_def any_def size_def2 obj_constant obj_constant_list |
| 166 |
%type <node> constant_def type_constant type_constant_list definitions |
| 167 |
%type <node> definitions_id Time bit_element bit_element_list set_def |
| 168 |
%type <node> /* identifier_list */ imports_def tag_type tag type_assig_right_tag |
| 169 |
%type <node> type_assig_right_tag_default enumerated_def string_def |
| 170 |
%type <node> utf8_string_def numeric_string_def printable_string_def |
| 171 |
%type <node> teletex_string_def ia5_string_def universal_string_def |
| 172 |
%type <node> bmp_string_def |
| 173 |
%type <str> pos_num neg_num pos_neg_num pos_neg_identifier num_identifier |
| 174 |
%type <constant> class explicit_implicit |
| 175 |
|
| 176 |
|
| 177 |
%% |
| 178 |
|
| 179 |
input: /* empty */ |
| 180 |
| input definitions |
| 181 |
; |
| 182 |
|
| 183 |
pos_num : NUM { strcpy($$,$1); } |
| 184 |
| '+' NUM { strcpy($$,$2); } |
| 185 |
; |
| 186 |
|
| 187 |
neg_num : '-' NUM |
| 188 |
{ |
| 189 |
strcpy($$,"-"); |
| 190 |
strcat($$,$2); |
| 191 |
} |
| 192 |
; |
| 193 |
|
| 194 |
pos_neg_num : pos_num { strcpy($$,$1); } |
| 195 |
| neg_num { strcpy($$,$1); } |
| 196 |
; |
| 197 |
|
| 198 |
num_identifier : NUM {strcpy($$,$1);} |
| 199 |
| IDENTIFIER {strcpy($$,$1);} |
| 200 |
; |
| 201 |
|
| 202 |
pos_neg_identifier : pos_neg_num {strcpy($$,$1);} |
| 203 |
| IDENTIFIER {strcpy($$,$1);} |
| 204 |
; |
| 205 |
|
| 206 |
constant: '(' pos_neg_num ')' |
| 207 |
{ |
| 208 |
$$ = NEW_NODE (TYPE_CONSTANT); |
| 209 |
set_str_value ($$, $2); |
| 210 |
} |
| 211 |
| IDENTIFIER'('pos_neg_num')' |
| 212 |
{ |
| 213 |
$$ = NEW_NODE (TYPE_CONSTANT); |
| 214 |
set_name ($$, $1); |
| 215 |
set_str_value ($$, $3); |
| 216 |
} |
| 217 |
; |
| 218 |
|
| 219 |
constant_list: constant { $$=$1; } |
| 220 |
| constant_list ',' constant |
| 221 |
{ |
| 222 |
$$ = $1; |
| 223 |
append_right ($1, $3); |
| 224 |
} |
| 225 |
; |
| 226 |
|
| 227 |
/* |
| 228 |
identifier_list : IDENTIFIER |
| 229 |
{ |
| 230 |
$$ = NEW_NODE (TYPE_IDENTIFIER); |
| 231 |
set_name($$,$1); |
| 232 |
} |
| 233 |
| identifier_list IDENTIFIER |
| 234 |
{ |
| 235 |
AsnNode node; |
| 236 |
|
| 237 |
$$=$1; |
| 238 |
node = NEW_NODE (TYPE_IDENTIFIER); |
| 239 |
set_name (node, $2); |
| 240 |
append_right ($$, node); |
| 241 |
} |
| 242 |
; |
| 243 |
*/ |
| 244 |
|
| 245 |
obj_constant: num_identifier |
| 246 |
{ |
| 247 |
$$ = NEW_NODE (TYPE_CONSTANT); |
| 248 |
set_str_value ($$, $1); |
| 249 |
} |
| 250 |
| IDENTIFIER '(' NUM ')' |
| 251 |
{ |
| 252 |
$$ = NEW_NODE (TYPE_CONSTANT); |
| 253 |
set_name ($$, $1); |
| 254 |
set_str_value ($$, $3); |
| 255 |
} |
| 256 |
; |
| 257 |
|
| 258 |
obj_constant_list: obj_constant |
| 259 |
{ $$=$1;} |
| 260 |
| obj_constant_list obj_constant |
| 261 |
{ |
| 262 |
$$=$1; |
| 263 |
append_right ($$, $2); |
| 264 |
} |
| 265 |
; |
| 266 |
|
| 267 |
class : UNIVERSAL { $$ = CLASS_UNIVERSAL; } |
| 268 |
| PRIVATE { $$ = CLASS_PRIVATE; } |
| 269 |
| APPLICATION { $$ = CLASS_APPLICATION; } |
| 270 |
; |
| 271 |
|
| 272 |
tag_type : '[' NUM ']' |
| 273 |
{ |
| 274 |
$$ = NEW_NODE (TYPE_TAG); |
| 275 |
$$->flags.class = CLASS_CONTEXT; |
| 276 |
set_ulong_value ($$, $2); |
| 277 |
} |
| 278 |
| '[' class NUM ']' |
| 279 |
{ |
| 280 |
$$ = NEW_NODE (TYPE_TAG); |
| 281 |
$$->flags.class = $2; |
| 282 |
set_ulong_value ($$, $3); |
| 283 |
} |
| 284 |
; |
| 285 |
|
| 286 |
tag : tag_type |
| 287 |
{ $$ = $1; } |
| 288 |
| tag_type EXPLICIT |
| 289 |
{ |
| 290 |
$$ = $1; |
| 291 |
$$->flags.explicit = 1; |
| 292 |
} |
| 293 |
| tag_type IMPLICIT |
| 294 |
{ |
| 295 |
$$ = $1; |
| 296 |
$$->flags.implicit = 1; |
| 297 |
} |
| 298 |
; |
| 299 |
|
| 300 |
default : DEFAULT pos_neg_identifier |
| 301 |
{ |
| 302 |
$$ = NEW_NODE (TYPE_DEFAULT); |
| 303 |
set_str_value ($$, $2); |
| 304 |
} |
| 305 |
| DEFAULT ksba_TRUE |
| 306 |
{ |
| 307 |
$$ = NEW_NODE (TYPE_DEFAULT); |
| 308 |
$$->flags.is_true = 1; |
| 309 |
} |
| 310 |
| DEFAULT ksba_FALSE |
| 311 |
{ |
| 312 |
$$ = NEW_NODE (TYPE_DEFAULT); |
| 313 |
$$->flags.is_false = 1; |
| 314 |
} |
| 315 |
; |
| 316 |
|
| 317 |
integer_def: INTEGER |
| 318 |
{ |
| 319 |
$$ = NEW_NODE (TYPE_INTEGER); |
| 320 |
} |
| 321 |
| INTEGER '{' constant_list '}' |
| 322 |
{ |
| 323 |
$$ = NEW_NODE (TYPE_INTEGER); |
| 324 |
$$->flags.has_list = 1; |
| 325 |
set_down ($$, $3); |
| 326 |
} |
| 327 |
| integer_def '(' num_identifier '.' '.' num_identifier ')' |
| 328 |
{ |
| 329 |
$$ = NEW_NODE (TYPE_INTEGER); |
| 330 |
$$->flags.has_min_max = 1; |
| 331 |
/* the following is wrong. Better use a union for the value*/ |
| 332 |
set_down ($$, NEW_NODE (TYPE_SIZE) ); |
| 333 |
set_str_value ($$->down, $6); |
| 334 |
set_name ($$->down, $3); |
| 335 |
} |
| 336 |
; |
| 337 |
|
| 338 |
boolean_def: ksba_BOOLEAN |
| 339 |
{ |
| 340 |
$$ = NEW_NODE (TYPE_BOOLEAN); |
| 341 |
} |
| 342 |
; |
| 343 |
|
| 344 |
Time: UTCTime |
| 345 |
{ |
| 346 |
$$ = NEW_NODE (TYPE_UTC_TIME); |
| 347 |
} |
| 348 |
| GeneralizedTime |
| 349 |
{ |
| 350 |
$$ = NEW_NODE (TYPE_GENERALIZED_TIME); |
| 351 |
} |
| 352 |
; |
| 353 |
|
| 354 |
size_def2: SIZE '(' num_identifier ')' |
| 355 |
{ |
| 356 |
$$ = NEW_NODE (TYPE_SIZE); |
| 357 |
$$->flags.one_param = 1; |
| 358 |
set_str_value ($$, $3); |
| 359 |
} |
| 360 |
| SIZE '(' num_identifier '.' '.' num_identifier ')' |
| 361 |
{ |
| 362 |
$$ = NEW_NODE (TYPE_SIZE); |
| 363 |
$$->flags.has_min_max = 1; |
| 364 |
set_str_value ($$, $3); |
| 365 |
set_name ($$, $6); |
| 366 |
} |
| 367 |
; |
| 368 |
|
| 369 |
size_def: size_def2 |
| 370 |
{ |
| 371 |
$$=$1; |
| 372 |
} |
| 373 |
| '(' size_def2 ')' |
| 374 |
{ |
| 375 |
$$=$2; |
| 376 |
} |
| 377 |
; |
| 378 |
|
| 379 |
octet_string_def : OCTET STRING |
| 380 |
{ |
| 381 |
$$ = NEW_NODE (TYPE_OCTET_STRING); |
| 382 |
} |
| 383 |
| OCTET STRING size_def |
| 384 |
{ |
| 385 |
$$ = NEW_NODE (TYPE_OCTET_STRING); |
| 386 |
$$->flags.has_size = 1; |
| 387 |
set_down ($$,$3); |
| 388 |
} |
| 389 |
; |
| 390 |
|
| 391 |
utf8_string_def: UTF8STRING { $$ = NEW_NODE (TYPE_UTF8_STRING); } |
| 392 |
| UTF8STRING size_def |
| 393 |
{ |
| 394 |
$$ = NEW_NODE (TYPE_UTF8_STRING); |
| 395 |
$$->flags.has_size = 1; |
| 396 |
set_down ($$,$2); |
| 397 |
} |
| 398 |
; |
| 399 |
numeric_string_def: NUMERICSTRING { $$ = NEW_NODE (TYPE_NUMERIC_STRING); } |
| 400 |
| NUMERICSTRING size_def |
| 401 |
{ |
| 402 |
$$ = NEW_NODE (TYPE_NUMERIC_STRING); |
| 403 |
$$->flags.has_size = 1; |
| 404 |
set_down ($$,$2); |
| 405 |
} |
| 406 |
; |
| 407 |
printable_string_def: PRINTABLESTRING |
| 408 |
{ $$ = NEW_NODE (TYPE_PRINTABLE_STRING); } |
| 409 |
| PRINTABLESTRING size_def |
| 410 |
{ |
| 411 |
$$ = NEW_NODE (TYPE_PRINTABLE_STRING); |
| 412 |
$$->flags.has_size = 1; |
| 413 |
set_down ($$,$2); |
| 414 |
} |
| 415 |
; |
| 416 |
teletex_string_def: TELETEXSTRING |
| 417 |
{ $$ = NEW_NODE (TYPE_TELETEX_STRING); } |
| 418 |
| TELETEXSTRING size_def |
| 419 |
{ |
| 420 |
$$ = NEW_NODE (TYPE_TELETEX_STRING); |
| 421 |
$$->flags.has_size = 1; |
| 422 |
set_down ($$,$2); |
| 423 |
} |
| 424 |
; |
| 425 |
ia5_string_def: IA5STRING { $$ = NEW_NODE (TYPE_IA5_STRING); } |
| 426 |
| IA5STRING size_def |
| 427 |
{ |
| 428 |
$$ = NEW_NODE (TYPE_IA5_STRING); |
| 429 |
$$->flags.has_size = 1; |
| 430 |
set_down ($$,$2); |
| 431 |
} |
| 432 |
; |
| 433 |
universal_string_def: UNIVERSALSTRING |
| 434 |
{ $$ = NEW_NODE (TYPE_UNIVERSAL_STRING); } |
| 435 |
| UNIVERSALSTRING size_def |
| 436 |
{ |
| 437 |
$$ = NEW_NODE (TYPE_UNIVERSAL_STRING); |
| 438 |
$$->flags.has_size = 1; |
| 439 |
set_down ($$,$2); |
| 440 |
} |
| 441 |
; |
| 442 |
bmp_string_def: BMPSTRING { $$ = NEW_NODE (TYPE_BMP_STRING); } |
| 443 |
| BMPSTRING size_def |
| 444 |
{ |
| 445 |
$$ = NEW_NODE (TYPE_BMP_STRING); |
| 446 |
$$->flags.has_size = 1; |
| 447 |
set_down ($$,$2); |
| 448 |
} |
| 449 |
; |
| 450 |
|
| 451 |
|
| 452 |
string_def: utf8_string_def |
| 453 |
| numeric_string_def |
| 454 |
| printable_string_def |
| 455 |
| teletex_string_def |
| 456 |
| ia5_string_def |
| 457 |
| universal_string_def |
| 458 |
| bmp_string_def |
| 459 |
; |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
bit_element : IDENTIFIER'('NUM')' |
| 465 |
{ |
| 466 |
$$ = NEW_NODE (TYPE_CONSTANT); |
| 467 |
set_name ($$, $1); |
| 468 |
set_str_value ($$, $3); |
| 469 |
} |
| 470 |
; |
| 471 |
|
| 472 |
bit_element_list : bit_element |
| 473 |
{ |
| 474 |
$$=$1; |
| 475 |
} |
| 476 |
| bit_element_list ',' bit_element |
| 477 |
{ |
| 478 |
$$=$1; |
| 479 |
append_right ($$, $3); |
| 480 |
} |
| 481 |
; |
| 482 |
|
| 483 |
bit_string_def : BIT STRING |
| 484 |
{ |
| 485 |
$$ = NEW_NODE (TYPE_BIT_STRING); |
| 486 |
} |
| 487 |
| BIT STRING '{' bit_element_list '}' |
| 488 |
{ |
| 489 |
$$ = NEW_NODE (TYPE_BIT_STRING); |
| 490 |
$$->flags.has_list = 1; |
| 491 |
set_down ($$, $4); |
| 492 |
} |
| 493 |
; |
| 494 |
|
| 495 |
enumerated_def : ENUMERATED '{' bit_element_list '}' |
| 496 |
{ |
| 497 |
$$ = NEW_NODE (TYPE_ENUMERATED); |
| 498 |
$$->flags.has_list = 1; |
| 499 |
set_down ($$, $3); |
| 500 |
} |
| 501 |
; |
| 502 |
|
| 503 |
object_def : OBJECT STR_IDENTIFIER |
| 504 |
{ |
| 505 |
$$ = NEW_NODE (TYPE_OBJECT_ID); |
| 506 |
} |
| 507 |
; |
| 508 |
|
| 509 |
type_assig_right: IDENTIFIER |
| 510 |
{ |
| 511 |
$$ = NEW_NODE (TYPE_IDENTIFIER); |
| 512 |
set_str_value ($$, $1); |
| 513 |
} |
| 514 |
| IDENTIFIER size_def |
| 515 |
{ |
| 516 |
$$ = NEW_NODE (TYPE_IDENTIFIER); |
| 517 |
$$->flags.has_size = 1; |
| 518 |
set_str_value ($$, $1); |
| 519 |
set_down ($$, $2); |
| 520 |
} |
| 521 |
| integer_def {$$=$1;} |
| 522 |
| enumerated_def {$$=$1;} |
| 523 |
| boolean_def {$$=$1;} |
| 524 |
| string_def {$$=$1;} |
| 525 |
| Time |
| 526 |
| octet_string_def {$$=$1;} |
| 527 |
| bit_string_def {$$=$1;} |
| 528 |
| sequence_def {$$=$1;} |
| 529 |
| object_def {$$=$1;} |
| 530 |
| choise_def {$$=$1;} |
| 531 |
| any_def {$$=$1;} |
| 532 |
| set_def {$$=$1;} |
| 533 |
| TOKEN_NULL |
| 534 |
{ |
| 535 |
$$ = NEW_NODE(TYPE_NULL); |
| 536 |
} |
| 537 |
; |
| 538 |
|
| 539 |
type_assig_right_tag : type_assig_right |
| 540 |
{ |
| 541 |
$$ = $1; |
| 542 |
} |
| 543 |
| tag type_assig_right |
| 544 |
{ |
| 545 |
/* $2->flags.has_tag = 1; */ |
| 546 |
/* $$ = $2; */ |
| 547 |
/* set_right ($1, $$->down ); */ |
| 548 |
/* set_down ($$, $1); */ |
| 549 |
$$ = $1; |
| 550 |
set_down ($$, $2); |
| 551 |
} |
| 552 |
; |
| 553 |
|
| 554 |
type_assig_right_tag_default : type_assig_right_tag |
| 555 |
{ |
| 556 |
$$ = $1; |
| 557 |
} |
| 558 |
| type_assig_right_tag default |
| 559 |
{ |
| 560 |
$1->flags.has_default = 1; |
| 561 |
$$ = $1; |
| 562 |
set_right ($2, $$->down); |
| 563 |
set_down ($$, $2); |
| 564 |
} |
| 565 |
| type_assig_right_tag OPTIONAL |
| 566 |
{ |
| 567 |
$1->flags.is_optional = 1; |
| 568 |
$$ = $1; |
| 569 |
} |
| 570 |
; |
| 571 |
|
| 572 |
type_assig : IDENTIFIER type_assig_right_tag_default |
| 573 |
{ |
| 574 |
set_name ($2, $1); |
| 575 |
$$ = $2; |
| 576 |
} |
| 577 |
; |
| 578 |
|
| 579 |
type_assig_list : type_assig |
| 580 |
{ $$=$1; } |
| 581 |
| type_assig_list ',' type_assig |
| 582 |
{ |
| 583 |
$$=$1; |
| 584 |
append_right ($$, $3); |
| 585 |
} |
| 586 |
; |
| 587 |
|
| 588 |
sequence_def : SEQUENCE '{' type_assig_list '}' |
| 589 |
{ |
| 590 |
$$ = NEW_NODE (TYPE_SEQUENCE); |
| 591 |
set_down ($$, $3); |
| 592 |
} |
| 593 |
| SEQUENCE OF type_assig_right |
| 594 |
{ |
| 595 |
$$ = NEW_NODE (TYPE_SEQUENCE_OF); |
| 596 |
set_down ($$, $3); |
| 597 |
} |
| 598 |
| SEQUENCE size_def OF type_assig_right |
| 599 |
{ |
| 600 |
$$ = NEW_NODE (TYPE_SEQUENCE_OF); |
| 601 |
$$->flags.has_size = 1; |
| 602 |
set_right ($2,$4); |
| 603 |
set_down ($$,$2); |
| 604 |
} |
| 605 |
; |
| 606 |
|
| 607 |
set_def : SET '{' type_assig_list '}' |
| 608 |
{ |
| 609 |
$$ = NEW_NODE (TYPE_SET); |
| 610 |
set_down ($$, $3); |
| 611 |
} |
| 612 |
| SET OF type_assig_right |
| 613 |
{ |
| 614 |
$$ = NEW_NODE (TYPE_SET_OF); |
| 615 |
set_down ($$, $3); |
| 616 |
} |
| 617 |
| SET size_def OF type_assig_right |
| 618 |
{ |
| 619 |
$$ = NEW_NODE (TYPE_SET_OF); |
| 620 |
$$->flags.has_size = 1; |
| 621 |
set_right ($2, $4); |
| 622 |
set_down ($$, $2); |
| 623 |
} |
| 624 |
; |
| 625 |
|
| 626 |
choise_def : CHOICE '{' type_assig_list '}' |
| 627 |
{ |
| 628 |
$$ = NEW_NODE (TYPE_CHOICE); |
| 629 |
set_down ($$, $3); |
| 630 |
} |
| 631 |
; |
| 632 |
|
| 633 |
any_def : ANY |
| 634 |
{ |
| 635 |
$$ = NEW_NODE (TYPE_ANY); |
| 636 |
} |
| 637 |
| ANY DEFINED BY IDENTIFIER |
| 638 |
{ |
| 639 |
AsnNode node; |
| 640 |
|
| 641 |
$$ = NEW_NODE (TYPE_ANY); |
| 642 |
$$->flags.has_defined_by = 1; |
| 643 |
node = NEW_NODE (TYPE_CONSTANT); |
| 644 |
set_name (node, $4); |
| 645 |
set_down($$, node); |
| 646 |
} |
| 647 |
; |
| 648 |
|
| 649 |
type_def : IDENTIFIER "::=" type_assig_right_tag |
| 650 |
{ |
| 651 |
set_name ($3, $1); |
| 652 |
$$ = $3; |
| 653 |
} |
| 654 |
; |
| 655 |
|
| 656 |
constant_def : IDENTIFIER OBJECT STR_IDENTIFIER "::=" '{' obj_constant_list '}' |
| 657 |
{ |
| 658 |
$$ = NEW_NODE (TYPE_OBJECT_ID); |
| 659 |
$$->flags.assignment = 1; |
| 660 |
set_name ($$, $1); |
| 661 |
set_down ($$, $6); |
| 662 |
} |
| 663 |
| IDENTIFIER IDENTIFIER "::=" '{' obj_constant_list '}' |
| 664 |
{ |
| 665 |
$$ = NEW_NODE (TYPE_OBJECT_ID); |
| 666 |
$$->flags.assignment = 1; |
| 667 |
$$->flags.one_param = 1; |
| 668 |
set_name ($$, $1); |
| 669 |
set_str_value ($$, $2); |
| 670 |
set_down ($$, $5); |
| 671 |
} |
| 672 |
| IDENTIFIER INTEGER "::=" NUM |
| 673 |
{ |
| 674 |
$$ = NEW_NODE (TYPE_INTEGER); |
| 675 |
$$->flags.assignment = 1; |
| 676 |
set_name ($$, $1); |
| 677 |
set_str_value ($$, $4); |
| 678 |
} |
| 679 |
; |
| 680 |
|
| 681 |
type_constant: type_def { $$ = $1; } |
| 682 |
| constant_def { $$ = $1; } |
| 683 |
; |
| 684 |
|
| 685 |
type_constant_list : type_constant |
| 686 |
{ $$ = $1; } |
| 687 |
| type_constant_list type_constant |
| 688 |
{ |
| 689 |
$$ = $1; |
| 690 |
append_right ($$, $2); |
| 691 |
} |
| 692 |
; |
| 693 |
|
| 694 |
definitions_id : IDENTIFIER '{' obj_constant_list '}' |
| 695 |
{ |
| 696 |
$$ = NEW_NODE (TYPE_OBJECT_ID); |
| 697 |
set_down ($$, $3); |
| 698 |
set_name ($$, $1); |
| 699 |
} |
| 700 |
; |
| 701 |
|
| 702 |
imports_def : /* empty */ |
| 703 |
{ $$=NULL;} |
| 704 |
/* |
| 705 |
| IMPORTS identifier_list FROM IDENTIFIER |
| 706 |
{ |
| 707 |
AsnNode node; |
| 708 |
|
| 709 |
$$ = NEW_NODE (TYPE_IMPORTS); |
| 710 |
node = NEW_NODE (TYPE_OBJECT_ID); |
| 711 |
set_name (node, $4); |
| 712 |
set_down (node, $5); |
| 713 |
set_down ($$, node); |
| 714 |
set_right ($$, $2); |
| 715 |
} |
| 716 |
*/ |
| 717 |
; |
| 718 |
|
| 719 |
explicit_implicit : EXPLICIT { $$ = CONST_EXPLICIT; } |
| 720 |
| IMPLICIT { $$ = CONST_IMPLICIT; } |
| 721 |
; |
| 722 |
|
| 723 |
definitions: definitions_id |
| 724 |
DEFINITIONS explicit_implicit TAGS "::=" ksba_BEGIN imports_def |
| 725 |
type_constant_list ksba_END |
| 726 |
{ |
| 727 |
AsnNode node; |
| 728 |
|
| 729 |
$$ = node = NEW_NODE (TYPE_DEFINITIONS); |
| 730 |
|
| 731 |
if ($3 == CONST_EXPLICIT) |
| 732 |
node->flags.explicit = 1; |
| 733 |
else if ($3 == CONST_IMPLICIT) |
| 734 |
node->flags.implicit = 1; |
| 735 |
|
| 736 |
if ($7) |
| 737 |
node->flags.has_imports = 1; |
| 738 |
|
| 739 |
set_name ($$, $1->name); |
| 740 |
set_name ($1, ""); |
| 741 |
|
| 742 |
if (!node->flags.has_imports) |
| 743 |
set_right ($1,$8); |
| 744 |
else |
| 745 |
{ |
| 746 |
set_right ($7,$8); |
| 747 |
set_right ($1,$7); |
| 748 |
} |
| 749 |
|
| 750 |
set_down ($$, $1); |
| 751 |
|
| 752 |
_ksba_asn_set_default_tag ($$); |
| 753 |
_ksba_asn_type_set_config ($$); |
| 754 |
PARSECTL->result_parse = _ksba_asn_check_identifier($$); |
| 755 |
PARSECTL->parse_tree=$$; |
| 756 |
} |
| 757 |
; |
| 758 |
|
| 759 |
%% |
| 760 |
|
| 761 |
|
| 762 |
struct token_table_s { |
| 763 |
const char *word; |
| 764 |
int token; |
| 765 |
}; |
| 766 |
|
| 767 |
static struct token_table_s token_table[] = { |
| 768 |
{ "::=", ASSIG }, |
| 769 |
{ "ANY", ANY }, |
| 770 |
{ "APPLICATION", APPLICATION }, |
| 771 |
{ "BEGIN", ksba_BEGIN }, |
| 772 |
{ "BIT", BIT }, |
| 773 |
{ "BMPString", BMPSTRING }, |
| 774 |
{ "BOOLEAN", ksba_BOOLEAN }, |
| 775 |
{ "BY", BY }, |
| 776 |
{ "CHOICE", CHOICE }, |
| 777 |
{ "DEFAULT", DEFAULT }, |
| 778 |
{ "DEFINED", DEFINED }, |
| 779 |
{ "DEFINITIONS", DEFINITIONS }, |
| 780 |
{ "END", ksba_END }, |
| 781 |
{ "ENUMERATED", ENUMERATED }, |
| 782 |
{ "EXPLICIT", EXPLICIT }, |
| 783 |
{ "FALSE", ksba_FALSE }, |
| 784 |
{ "FROM", FROM }, |
| 785 |
{ "GeneralizedTime", GeneralizedTime }, |
| 786 |
{ "IA5String", IA5STRING }, |
| 787 |
{ "IDENTIFIER", STR_IDENTIFIER }, |
| 788 |
{ "IMPLICIT", IMPLICIT }, |
| 789 |
{ "IMPORTS", IMPORTS }, |
| 790 |
{ "INTEGER", INTEGER }, |
| 791 |
{ "NULL", TOKEN_NULL }, |
| 792 |
{ "NumericString", NUMERICSTRING }, |
| 793 |
{ "OBJECT", OBJECT }, |
| 794 |
{ "OCTET", OCTET }, |
| 795 |
{ "OF", OF }, |
| 796 |
{ "OPTIONAL", OPTIONAL }, |
| 797 |
{ "PRIVATE", PRIVATE }, |
| 798 |
{ "PrintableString", PRINTABLESTRING }, |
| 799 |
{ "SEQUENCE", SEQUENCE }, |
| 800 |
{ "SET", SET }, |
| 801 |
{ "SIZE", SIZE }, |
| 802 |
{ "STRING", STRING }, |
| 803 |
{ "TAGS", TAGS }, |
| 804 |
{ "TRUE", ksba_TRUE }, |
| 805 |
{ "TeletexString", TELETEXSTRING }, |
| 806 |
{ "UNIVERSAL", UNIVERSAL }, |
| 807 |
{ "UTCTime", UTCTime }, |
| 808 |
{ "UTF8String", UTF8STRING }, |
| 809 |
{ "UniversalString", UNIVERSALSTRING }, |
| 810 |
}; |
| 811 |
|
| 812 |
/*************************************************************/ |
| 813 |
/* Function: yylex */ |
| 814 |
/* Description: looks for tokens in file_asn1 pointer file. */ |
| 815 |
/* Return: int */ |
| 816 |
/* Token identifier or ASCII code or 0(zero: End Of File) */ |
| 817 |
/*************************************************************/ |
| 818 |
static int |
| 819 |
yylex (YYSTYPE *lvalp, void *parm) |
| 820 |
{ |
| 821 |
int c,counter=0,k; |
| 822 |
char string[MAX_STRING_LENGTH]; |
| 823 |
FILE *fp = PARSECTL->fp; |
| 824 |
|
| 825 |
if (!PARSECTL->lineno) |
| 826 |
PARSECTL->lineno++; /* start with line one */ |
| 827 |
|
| 828 |
while (1) |
| 829 |
{ |
| 830 |
while ( (c=fgetc (fp))==' ' || c=='\t') |
| 831 |
; |
| 832 |
if (c =='\n') |
| 833 |
{ |
| 834 |
PARSECTL->lineno++; |
| 835 |
continue; |
| 836 |
} |
| 837 |
if(c==EOF) |
| 838 |
return 0; |
| 839 |
|
| 840 |
if ( c=='(' || c==')' || c=='[' || c==']' |
| 841 |
|| c=='{' || c=='}' || c==',' || c=='.' || c=='+') |
| 842 |
return c; |
| 843 |
|
| 844 |
if (c=='-') |
| 845 |
{ |
| 846 |
if ( (c=fgetc(fp))!='-') |
| 847 |
{ |
| 848 |
ungetc(c,fp); |
| 849 |
return '-'; |
| 850 |
} |
| 851 |
else |
| 852 |
{ |
| 853 |
/* A comment finishes at the end of line */ |
| 854 |
counter=0; |
| 855 |
while ( (c=fgetc(fp))!=EOF && c != '\n' ) |
| 856 |
; |
| 857 |
if (c==EOF) |
| 858 |
return 0; |
| 859 |
else |
| 860 |
continue; /* repeat the search */ |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
do |
| 865 |
{ |
| 866 |
if (counter >= DIM (string)-1 ) |
| 867 |
{ |
| 868 |
fprintf (stderr,"%s:%d: token too long\n", "myfile:", |
| 869 |
PARSECTL->lineno); |
| 870 |
return 0; /* EOF */ |
| 871 |
} |
| 872 |
string[counter++]=c; |
| 873 |
} |
| 874 |
while ( !((c=fgetc(fp))==EOF |
| 875 |
|| c==' '|| c=='\t' || c=='\n' |
| 876 |
|| c=='(' || c==')' || c=='[' || c==']' |
| 877 |
|| c=='{' || c=='}' || c==',' || c=='.')); |
| 878 |
|
| 879 |
ungetc (c,fp); |
| 880 |
string[counter]=0; |
| 881 |
/*fprintf (stderr, "yylex token `%s'\n", string);*/ |
| 882 |
|
| 883 |
/* Is STRING a number? */ |
| 884 |
for (k=0; k<counter; k++) |
| 885 |
{ |
| 886 |
if(!isdigit(string[k])) |
| 887 |
break; |
| 888 |
} |
| 889 |
if (k>=counter) |
| 890 |
{ |
| 891 |
strcpy (lvalp->str,string); |
| 892 |
if (PARSECTL->debug) |
| 893 |
fprintf (stderr,"%d: yylex found number `%s'\n", |
| 894 |
PARSECTL->lineno, string); |
| 895 |
return NUM; |
| 896 |
} |
| 897 |
|
| 898 |
/* Is STRING a keyword? */ |
| 899 |
for (k = 0; k < DIM (token_table); k++) |
| 900 |
{ |
| 901 |
if (!strcmp (token_table[k].word, string)) |
| 902 |
return token_table[k].token; |
| 903 |
} |
| 904 |
|
| 905 |
/* STRING is an IDENTIFIER */ |
| 906 |
strcpy(lvalp->str,string); |
| 907 |
if (PARSECTL->debug) |
| 908 |
fprintf (stderr,"%d: yylex found identifier `%s'\n", |
| 909 |
PARSECTL->lineno, string); |
| 910 |
return IDENTIFIER; |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
static void |
| 915 |
yyerror (void *parm, const char *s) |
| 916 |
{ |
| 917 |
(void)parm; |
| 918 |
/* Sends the error description to stderr */ |
| 919 |
fprintf (stderr, "%s\n", s); |
| 920 |
/* Why doesn't bison provide a way to pass the parm to yyerror? |
| 921 |
Update: Newer bison versions allow for this. We need to see how |
| 922 |
we can make use of it. */ |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
static AsnNode |
| 928 |
new_node (struct parser_control_s *parsectl, node_type_t type) |
| 929 |
{ |
| 930 |
AsnNode node; |
| 931 |
|
| 932 |
node = xcalloc (1, sizeof *node); |
| 933 |
node->type = type; |
| 934 |
node->off = -1; |
| 935 |
node->link_next = parsectl->all_nodes; |
| 936 |
parsectl->all_nodes = node; |
| 937 |
|
| 938 |
return node; |
| 939 |
} |
| 940 |
|
| 941 |
static void |
| 942 |
release_all_nodes (AsnNode node) |
| 943 |
{ |
| 944 |
AsnNode node2; |
| 945 |
|
| 946 |
for (; node; node = node2) |
| 947 |
{ |
| 948 |
node2 = node->link_next; |
| 949 |
xfree (node->name); |
| 950 |
|
| 951 |
if (node->valuetype == VALTYPE_CSTR) |
| 952 |
xfree (node->value.v_cstr); |
| 953 |
else if (node->valuetype == VALTYPE_MEM) |
| 954 |
xfree (node->value.v_mem.buf); |
| 955 |
|
| 956 |
xfree (node); |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
static void |
| 961 |
set_name (AsnNode node, const char *name) |
| 962 |
{ |
| 963 |
_ksba_asn_set_name (node, name); |
| 964 |
} |
| 965 |
|
| 966 |
static void |
| 967 |
set_str_value (AsnNode node, const char *text) |
| 968 |
{ |
| 969 |
if (text && *text) |
| 970 |
_ksba_asn_set_value (node, VALTYPE_CSTR, text, 0); |
| 971 |
else |
| 972 |
_ksba_asn_set_value (node, VALTYPE_NULL, NULL, 0); |
| 973 |
} |
| 974 |
|
| 975 |
static void |
| 976 |
set_ulong_value (AsnNode node, const char *text) |
| 977 |
{ |
| 978 |
unsigned long val; |
| 979 |
|
| 980 |
if (text && *text) |
| 981 |
val = strtoul (text, NULL, 10); |
| 982 |
else |
| 983 |
val = 0; |
| 984 |
_ksba_asn_set_value (node, VALTYPE_ULONG, &val, sizeof(val)); |
| 985 |
} |
| 986 |
|
| 987 |
static void |
| 988 |
set_right (AsnNode node, AsnNode right) |
| 989 |
{ |
| 990 |
return_if_fail (node); |
| 991 |
|
| 992 |
node->right = right; |
| 993 |
if (right) |
| 994 |
right->left = node; |
| 995 |
} |
| 996 |
|
| 997 |
static void |
| 998 |
append_right (AsnNode node, AsnNode right) |
| 999 |
{ |
| 1000 |
return_if_fail (node); |
| 1001 |
|
| 1002 |
while (node->right) |
| 1003 |
node = node->right; |
| 1004 |
|
| 1005 |
node->right = right; |
| 1006 |
if (right) |
| 1007 |
right->left = node; |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
static void |
| 1012 |
set_down (AsnNode node, AsnNode down) |
| 1013 |
{ |
| 1014 |
return_if_fail (node); |
| 1015 |
|
| 1016 |
node->down = down; |
| 1017 |
if (down) |
| 1018 |
down->left = node; |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
/** |
| 1023 |
* ksba_asn_parse_file: |
| 1024 |
* @file_name: Filename with the ASN module |
| 1025 |
* @pointer: Returns the syntax tree |
| 1026 |
* @debug: Enable debug output |
| 1027 |
* |
| 1028 |
* Parse an ASN.1 file and return an syntax tree. |
| 1029 |
* |
| 1030 |
* Return value: 0 for okay or an ASN_xx error code |
| 1031 |
**/ |
| 1032 |
int |
| 1033 |
ksba_asn_parse_file (const char *file_name, ksba_asn_tree_t *result, int debug) |
| 1034 |
{ |
| 1035 |
struct parser_control_s parsectl; |
| 1036 |
|
| 1037 |
*result = NULL; |
| 1038 |
|
| 1039 |
parsectl.fp = file_name? fopen (file_name, "r") : NULL; |
| 1040 |
if ( !parsectl.fp ) |
| 1041 |
return gpg_error_from_syserror (); |
| 1042 |
|
| 1043 |
parsectl.lineno = 0; |
| 1044 |
parsectl.debug = debug; |
| 1045 |
parsectl.result_parse = gpg_error (GPG_ERR_SYNTAX); |
| 1046 |
parsectl.parse_tree = NULL; |
| 1047 |
parsectl.all_nodes = NULL; |
| 1048 |
/* yydebug = 1; */ |
| 1049 |
if ( yyparse ((void*)&parsectl) || parsectl.result_parse ) |
| 1050 |
{ /* error */ |
| 1051 |
fprintf (stderr, "%s:%d: parse error\n", |
| 1052 |
file_name?file_name:"-", parsectl.lineno ); |
| 1053 |
release_all_nodes (parsectl.all_nodes); |
| 1054 |
parsectl.all_nodes = NULL; |
| 1055 |
} |
| 1056 |
else |
| 1057 |
{ /* okay */ |
| 1058 |
ksba_asn_tree_t tree; |
| 1059 |
|
| 1060 |
_ksba_asn_change_integer_value (parsectl.parse_tree); |
| 1061 |
_ksba_asn_expand_object_id (parsectl.parse_tree); |
| 1062 |
tree = xmalloc ( sizeof *tree + (file_name? strlen (file_name):1) ); |
| 1063 |
tree->parse_tree = parsectl.parse_tree; |
| 1064 |
tree->node_list = parsectl.all_nodes; |
| 1065 |
strcpy (tree->filename, file_name? file_name:"-"); |
| 1066 |
*result = tree; |
| 1067 |
} |
| 1068 |
|
| 1069 |
if (file_name) |
| 1070 |
fclose (parsectl.fp); |
| 1071 |
return parsectl.result_parse; |
| 1072 |
} |
| 1073 |
|
| 1074 |
void |
| 1075 |
ksba_asn_tree_release (ksba_asn_tree_t tree) |
| 1076 |
{ |
| 1077 |
if (!tree) |
| 1078 |
return; |
| 1079 |
release_all_nodes (tree->node_list); |
| 1080 |
tree->node_list = NULL; |
| 1081 |
xfree (tree); |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
void |
| 1086 |
_ksba_asn_release_nodes (AsnNode node) |
| 1087 |
{ |
| 1088 |
/* FIXME: it does not work yet because the allocation function in |
| 1089 |
asn1-func.c does not link all nodes together */ |
| 1090 |
release_all_nodes (node); |
| 1091 |
} |