1 /* 2 * Copyright (C) 2002-2021 Free Software Foundation, Inc. 3 * 4 * This file is part of LIBTASN1. 5 * 6 * LIBTASN1 is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU Lesser General Public License as 8 * published by the Free Software Foundation; either version 2.1 of 9 * the License, or (at your option) any later version. 10 * 11 * LIBTASN1 is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with LIBTASN1; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 * 02110-1301, USA 20 * 21 */ 22 23 /** 24 * SECTION:libtasn1 25 * @short_description: GNU ASN.1 library 26 * 27 * The Libtasn1 library provides Abstract Syntax Notation One (ASN.1, as 28 * specified by the X.680 ITU-T recommendation) parsing and structures 29 * management, and Distinguished Encoding Rules (DER, as per X.690) 30 * encoding and decoding functions. 31 */ 32 33 34 #ifndef LIBTASN1_H 35 # define LIBTASN1_H 36 37 # ifndef ASN1_API 38 # if defined ASN1_BUILDING && defined HAVE_VISIBILITY && HAVE_VISIBILITY 39 # define ASN1_API __attribute__((__visibility__("default"))) 40 # elif defined ASN1_BUILDING && defined _MSC_VER && ! defined ASN1_STATIC 41 # define ASN1_API __declspec(dllexport) 42 # elif defined _MSC_VER && ! defined ASN1_STATIC 43 # define ASN1_API __declspec(dllimport) 44 # else 45 # define ASN1_API 46 # endif 47 # endif 48 49 # ifdef __GNUC__ 50 # define __LIBTASN1_CONST__ __attribute__((const)) 51 # define __LIBTASN1_PURE__ __attribute__((pure)) 52 # else 53 # define __LIBTASN1_CONST__ 54 # define __LIBTASN1_PURE__ 55 # endif 56 57 # include <sys/types.h> 58 # include <time.h> 59 # include <stdio.h> /* for FILE* */ 60 61 # ifdef __cplusplus 62 extern "C" 63 { 64 # endif 65 66 /** 67 * ASN1_VERSION: 68 * 69 * Version of the library as a string. 70 */ 71 # define ASN1_VERSION "4.18.0" 72 73 /** 74 * ASN1_VERSION_MAJOR: 75 * 76 * Major version number of the library. 77 */ 78 # define ASN1_VERSION_MAJOR 4 79 80 /** 81 * ASN1_VERSION_MINOR: 82 * 83 * Minor version number of the library. 84 */ 85 # define ASN1_VERSION_MINOR 18 86 87 /** 88 * ASN1_VERSION_PATCH: 89 * 90 * Patch version number of the library. 91 */ 92 # define ASN1_VERSION_PATCH 0 93 94 /** 95 * ASN1_VERSION_NUMBER: 96 * 97 * Version number of the library as a number. 98 */ 99 # define ASN1_VERSION_NUMBER 0x041200 100 101 102 # if defined __GNUC__ && !defined ASN1_INTERNAL_BUILD 103 # define _ASN1_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 104 # if _ASN1_GCC_VERSION >= 30100 105 # define _ASN1_GCC_ATTR_DEPRECATED __attribute__ ((__deprecated__)) 106 # endif 107 # endif 108 109 # ifndef _ASN1_GCC_ATTR_DEPRECATED 110 # define _ASN1_GCC_ATTR_DEPRECATED 111 # endif 112 113 /*****************************************/ 114 /* Errors returned by libtasn1 functions */ 115 /*****************************************/ 116 # define ASN1_SUCCESS 0 117 # define ASN1_FILE_NOT_FOUND 1 118 # define ASN1_ELEMENT_NOT_FOUND 2 119 # define ASN1_IDENTIFIER_NOT_FOUND 3 120 # define ASN1_DER_ERROR 4 121 # define ASN1_VALUE_NOT_FOUND 5 122 # define ASN1_GENERIC_ERROR 6 123 # define ASN1_VALUE_NOT_VALID 7 124 # define ASN1_TAG_ERROR 8 125 # define ASN1_TAG_IMPLICIT 9 126 # define ASN1_ERROR_TYPE_ANY 10 127 # define ASN1_SYNTAX_ERROR 11 128 # define ASN1_MEM_ERROR 12 129 # define ASN1_MEM_ALLOC_ERROR 13 130 # define ASN1_DER_OVERFLOW 14 131 # define ASN1_NAME_TOO_LONG 15 132 # define ASN1_ARRAY_ERROR 16 133 # define ASN1_ELEMENT_NOT_EMPTY 17 134 # define ASN1_TIME_ENCODING_ERROR 18 135 # define ASN1_RECURSION 19 136 137 /*************************************/ 138 /* Constants used in asn1_visit_tree */ 139 /*************************************/ 140 # define ASN1_PRINT_NAME 1 141 # define ASN1_PRINT_NAME_TYPE 2 142 # define ASN1_PRINT_NAME_TYPE_VALUE 3 143 # define ASN1_PRINT_ALL 4 144 145 /*****************************************/ 146 /* Constants returned by asn1_read_tag */ 147 /*****************************************/ 148 # define ASN1_CLASS_UNIVERSAL 0x00 /* old: 1 */ 149 # define ASN1_CLASS_APPLICATION 0x40 /* old: 2 */ 150 # define ASN1_CLASS_CONTEXT_SPECIFIC 0x80 /* old: 3 */ 151 # define ASN1_CLASS_PRIVATE 0xC0 /* old: 4 */ 152 # define ASN1_CLASS_STRUCTURED 0x20 153 154 /*****************************************/ 155 /* Constants returned by asn1_read_tag */ 156 /*****************************************/ 157 # define ASN1_TAG_BOOLEAN 0x01 158 # define ASN1_TAG_INTEGER 0x02 159 # define ASN1_TAG_SEQUENCE 0x10 160 # define ASN1_TAG_SET 0x11 161 # define ASN1_TAG_OCTET_STRING 0x04 162 # define ASN1_TAG_BIT_STRING 0x03 163 # define ASN1_TAG_UTCTime 0x17 164 # define ASN1_TAG_GENERALIZEDTime 0x18 165 # define ASN1_TAG_OBJECT_ID 0x06 166 # define ASN1_TAG_ENUMERATED 0x0A 167 # define ASN1_TAG_NULL 0x05 168 # define ASN1_TAG_GENERALSTRING 0x1B 169 # define ASN1_TAG_NUMERIC_STRING 0x12 170 # define ASN1_TAG_IA5_STRING 0x16 171 # define ASN1_TAG_TELETEX_STRING 0x14 172 # define ASN1_TAG_PRINTABLE_STRING 0x13 173 # define ASN1_TAG_UNIVERSAL_STRING 0x1C 174 # define ASN1_TAG_BMP_STRING 0x1E 175 # define ASN1_TAG_UTF8_STRING 0x0C 176 # define ASN1_TAG_VISIBLE_STRING 0x1A 177 178 /** 179 * asn1_node: 180 * 181 * Structure definition used for the node of the tree 182 * that represents an ASN.1 DEFINITION. 183 */ 184 typedef struct asn1_node_st asn1_node_st; 185 186 typedef asn1_node_st *asn1_node; 187 typedef const asn1_node_st *asn1_node_const; 188 189 /** 190 * ASN1_MAX_NAME_SIZE: 191 * 192 * Maximum number of characters of a name 193 * inside a file with ASN1 definitions. 194 */ 195 # define ASN1_MAX_NAME_SIZE 64 196 197 198 /** 199 * asn1_static_node: 200 * @name: Node name 201 * @type: Node typ 202 * @value: Node value 203 * 204 * For the on-disk format of ASN.1 trees, created by asn1_parser2array(). 205 */ 206 typedef struct asn1_static_node_st 207 { 208 const char *name; /* Node name */ 209 unsigned int type; /* Node type */ 210 const void *value; /* Node value */ 211 } asn1_static_node; 212 213 /* List of constants for field type of asn1_static_node */ 214 # define ASN1_ETYPE_INVALID 0 215 # define ASN1_ETYPE_CONSTANT 1 216 # define ASN1_ETYPE_IDENTIFIER 2 217 # define ASN1_ETYPE_INTEGER 3 218 # define ASN1_ETYPE_BOOLEAN 4 219 # define ASN1_ETYPE_SEQUENCE 5 220 # define ASN1_ETYPE_BIT_STRING 6 221 # define ASN1_ETYPE_OCTET_STRING 7 222 # define ASN1_ETYPE_TAG 8 223 # define ASN1_ETYPE_DEFAULT 9 224 # define ASN1_ETYPE_SIZE 10 225 # define ASN1_ETYPE_SEQUENCE_OF 11 226 # define ASN1_ETYPE_OBJECT_ID 12 227 # define ASN1_ETYPE_ANY 13 228 # define ASN1_ETYPE_SET 14 229 # define ASN1_ETYPE_SET_OF 15 230 # define ASN1_ETYPE_DEFINITIONS 16 231 # define ASN1_ETYPE_CHOICE 18 232 # define ASN1_ETYPE_IMPORTS 19 233 # define ASN1_ETYPE_NULL 20 234 # define ASN1_ETYPE_ENUMERATED 21 235 # define ASN1_ETYPE_GENERALSTRING 27 236 # define ASN1_ETYPE_NUMERIC_STRING 28 237 # define ASN1_ETYPE_IA5_STRING 29 238 # define ASN1_ETYPE_TELETEX_STRING 30 239 # define ASN1_ETYPE_PRINTABLE_STRING 31 240 # define ASN1_ETYPE_UNIVERSAL_STRING 32 241 # define ASN1_ETYPE_BMP_STRING 33 242 # define ASN1_ETYPE_UTF8_STRING 34 243 # define ASN1_ETYPE_VISIBLE_STRING 35 244 # define ASN1_ETYPE_UTC_TIME 36 245 # define ASN1_ETYPE_GENERALIZED_TIME 37 246 247 /** 248 * ASN1_DELETE_FLAG_ZEROIZE: 249 * 250 * Used by: asn1_delete_structure2() 251 * 252 * Zeroize values prior to deinitialization. 253 */ 254 # define ASN1_DELETE_FLAG_ZEROIZE 1 255 256 /** 257 * ASN1_DECODE_FLAG_ALLOW_PADDING: 258 * 259 * Used by: asn1_der_decoding2() 260 * 261 * This flag would allow arbitrary data past the DER data. 262 */ 263 # define ASN1_DECODE_FLAG_ALLOW_PADDING 1 264 /** 265 * ASN1_DECODE_FLAG_STRICT_DER: 266 * 267 * Used by: asn1_der_decoding2() 268 * 269 * This flag would ensure that no BER decoding takes place. 270 */ 271 # define ASN1_DECODE_FLAG_STRICT_DER (1<<1) 272 /** 273 * ASN1_DECODE_FLAG_ALLOW_INCORRECT_TIME: 274 * 275 * Used by: asn1_der_decoding2() 276 * 277 * This flag will tolerate Time encoding errors when in strict DER. 278 */ 279 # define ASN1_DECODE_FLAG_ALLOW_INCORRECT_TIME (1<<2) 280 281 282 /** 283 * asn1_data_node_st: 284 * @name: Node name 285 * @value: Node value 286 * @value_len: Node value size 287 * @type: Node value type (ASN1_ETYPE_*) 288 * 289 * Data node inside a #asn1_node structure. 290 */ 291 struct asn1_data_node_st 292 { 293 const char *name; /* Node name */ 294 const void *value; /* Node value */ 295 unsigned int value_len; /* Node value size */ 296 unsigned int type; /* Node value type (ASN1_ETYPE_*) */ 297 }; 298 typedef struct asn1_data_node_st asn1_data_node_st; 299 300 /***********************************/ 301 /* Fixed constants */ 302 /***********************************/ 303 304 /** 305 * ASN1_MAX_ERROR_DESCRIPTION_SIZE: 306 * 307 * Maximum number of characters 308 * of a description message 309 * (null character included). 310 */ 311 # define ASN1_MAX_ERROR_DESCRIPTION_SIZE 128 312 313 /***********************************/ 314 /* Functions definitions */ 315 /***********************************/ 316 317 extern ASN1_API int 318 asn1_parser2tree (const char *file, 319 asn1_node * definitions, char *error_desc); 320 321 extern ASN1_API int 322 asn1_parser2array (const char *inputFileName, 323 const char *outputFileName, 324 const char *vectorName, char *error_desc); 325 326 extern ASN1_API int 327 asn1_array2tree (const asn1_static_node * array, 328 asn1_node * definitions, char *errorDescription); 329 330 extern ASN1_API void 331 asn1_print_structure (FILE * out, asn1_node_const structure, 332 const char *name, int mode); 333 334 extern ASN1_API int 335 asn1_create_element (asn1_node_const definitions, 336 const char *source_name, asn1_node * element); 337 338 extern ASN1_API int asn1_delete_structure (asn1_node * structure); 339 340 extern ASN1_API int asn1_delete_structure2 (asn1_node * structure, 341 unsigned int flags); 342 343 extern ASN1_API int 344 asn1_delete_element (asn1_node structure, const char *element_name); 345 346 extern ASN1_API int 347 asn1_write_value (asn1_node node_root, const char *name, 348 const void *ivalue, int len); 349 350 extern ASN1_API int 351 asn1_read_value (asn1_node_const root, const char *name, 352 void *ivalue, int *len); 353 354 extern ASN1_API int 355 asn1_read_value_type (asn1_node_const root, const char *name, 356 void *ivalue, int *len, unsigned int *etype); 357 358 extern ASN1_API int 359 asn1_read_node_value (asn1_node_const node, asn1_data_node_st * data); 360 361 extern ASN1_API int 362 asn1_number_of_elements (asn1_node_const element, const char *name, 363 int *num); 364 365 extern ASN1_API int 366 asn1_der_coding (asn1_node_const element, const char *name, 367 void *ider, int *len, char *ErrorDescription); 368 369 extern ASN1_API int 370 asn1_der_decoding2 (asn1_node * element, const void *ider, 371 int *max_ider_len, unsigned int flags, 372 char *errorDescription); 373 374 extern ASN1_API int 375 asn1_der_decoding (asn1_node * element, const void *ider, 376 int ider_len, char *errorDescription); 377 378 /* Do not use. Use asn1_der_decoding() instead. */ 379 extern ASN1_API int 380 asn1_der_decoding_element (asn1_node * structure, 381 const char *elementName, 382 const void *ider, int len, 383 char *errorDescription) 384 _ASN1_GCC_ATTR_DEPRECATED; 385 386 extern ASN1_API int 387 asn1_der_decoding_startEnd (asn1_node element, 388 const void *ider, int ider_len, 389 const char *name_element, 390 int *start, int *end); 391 392 extern ASN1_API int 393 asn1_expand_any_defined_by (asn1_node_const definitions, 394 asn1_node * element); 395 396 extern ASN1_API int 397 asn1_expand_octet_string (asn1_node_const definitions, 398 asn1_node * element, 399 const char *octetName, const char *objectName); 400 401 extern ASN1_API int 402 asn1_read_tag (asn1_node_const root, const char *name, 403 int *tagValue, int *classValue); 404 405 extern ASN1_API const char *asn1_find_structure_from_oid (asn1_node_const 406 definitions, 407 const char 408 *oidValue); 409 410 __LIBTASN1_PURE__ 411 extern ASN1_API const char *asn1_check_version (const char *req_version); 412 413 __LIBTASN1_PURE__ extern ASN1_API const char *asn1_strerror (int error); 414 415 extern ASN1_API void asn1_perror (int error); 416 417 # define ASN1_MAX_TAG_SIZE 4 418 # define ASN1_MAX_LENGTH_SIZE 9 419 # define ASN1_MAX_TL_SIZE (ASN1_MAX_TAG_SIZE+ASN1_MAX_LENGTH_SIZE) 420 extern ASN1_API long 421 asn1_get_length_der (const unsigned char *der, int der_len, int *len); 422 423 extern ASN1_API long 424 asn1_get_length_ber (const unsigned char *ber, int ber_len, int *len); 425 426 extern ASN1_API void 427 asn1_length_der (unsigned long int len, unsigned char *der, int *der_len); 428 429 /* Other utility functions. */ 430 431 extern ASN1_API 432 int asn1_decode_simple_der (unsigned int etype, const unsigned char *der, 433 unsigned int _der_len, 434 const unsigned char **str, 435 unsigned int *str_len); 436 437 extern ASN1_API 438 int asn1_decode_simple_ber (unsigned int etype, const unsigned char *der, 439 unsigned int _der_len, 440 unsigned char **str, 441 unsigned int *str_len, unsigned int *ber_len); 442 443 extern ASN1_API int 444 asn1_encode_simple_der (unsigned int etype, const unsigned char *str, 445 unsigned int str_len, unsigned char *tl, 446 unsigned int *tl_len); 447 448 extern ASN1_API asn1_node 449 asn1_find_node (asn1_node_const pointer, const char *name); 450 451 extern ASN1_API int 452 asn1_copy_node (asn1_node dst, const char *dst_name, 453 asn1_node_const src, const char *src_name); 454 extern ASN1_API asn1_node 455 asn1_dup_node (asn1_node_const src, const char *src_name); 456 457 /* Internal and low-level DER utility functions. */ 458 459 extern ASN1_API int 460 asn1_get_tag_der (const unsigned char *der, int der_len, 461 unsigned char *cls, int *len, unsigned long *tag); 462 463 extern ASN1_API void 464 asn1_octet_der (const unsigned char *str, int str_len, 465 unsigned char *der, int *der_len); 466 467 extern ASN1_API int 468 asn1_get_octet_der (const unsigned char *der, int der_len, 469 int *ret_len, unsigned char *str, 470 int str_size, int *str_len); 471 472 extern ASN1_API void asn1_bit_der (const unsigned char *str, int bit_len, 473 unsigned char *der, int *der_len); 474 475 extern ASN1_API int 476 asn1_get_bit_der (const unsigned char *der, int der_len, 477 int *ret_len, unsigned char *str, 478 int str_size, int *bit_len); 479 480 extern ASN1_API int 481 asn1_get_object_id_der (const unsigned char *der, 482 int der_len, int *ret_len, 483 char *str, int str_size); 484 485 extern ASN1_API int 486 asn1_object_id_der (const char *str, unsigned char *der, int *der_len, 487 unsigned flags); 488 489 /* Compatibility types */ 490 491 /** 492 * asn1_retCode: 493 * 494 * Type formerly returned by libtasn1 functions. 495 * 496 * Deprecated: 3.0: Use int instead. 497 */ 498 typedef int asn1_retCode _ASN1_GCC_ATTR_DEPRECATED; 499 500 /** 501 * node_asn_struct: 502 * 503 * Compat #define. 504 * 505 * Deprecated: 3.0: Use #asn1_node instead. 506 */ 507 # ifndef ASN1_DISABLE_DEPRECATED 508 # if _ASN1_GCC_VERSION >= 30100 509 # define node_asn_struct _Pragma ("GCC warning \"'node_asn_struct' macro is deprecated, use 'asn1_node' instead.\"") asn1_node_st 510 # else 511 # define node_asn_struct asn1_node_st 512 # endif 513 # endif /* !ASN1_DISABLE_DEPRECATED */ 514 515 /** 516 * node_asn: 517 * 518 * Compat #define. 519 * 520 * Deprecated: 3.0: Use #asn1_node instead. 521 */ 522 # ifndef ASN1_DISABLE_DEPRECATED 523 # if _ASN1_GCC_VERSION >= 30100 524 # define node_asn _Pragma ("GCC warning \"'node_asn' macro is deprecated, use 'asn1_node' instead.\"") asn1_node_st 525 # else 526 # define node_asn asn1_node_st 527 # endif 528 # endif /* !ASN1_DISABLE_DEPRECATED */ 529 530 /** 531 * ASN1_TYPE: 532 * 533 * Compat #define. 534 * 535 * Deprecated: 3.0: Use #asn1_node instead. 536 */ 537 # ifndef ASN1_DISABLE_DEPRECATED 538 # if _ASN1_GCC_VERSION >= 30100 539 # define ASN1_TYPE _Pragma ("GCC warning \"'ASN1_TYPE' macro is deprecated, use 'asn1_node' instead.\"") asn1_node 540 # else 541 # define ASN1_TYPE asn1_node 542 # endif 543 # endif /* !ASN1_DISABLE_DEPRECATED */ 544 545 /** 546 * ASN1_TYPE_EMPTY: 547 * 548 * Compat #define. 549 * 550 * Deprecated: 3.0: Use NULL instead. 551 */ 552 # ifndef ASN1_DISABLE_DEPRECATED 553 # if _ASN1_GCC_VERSION >= 30100 554 # define ASN1_TYPE_EMPTY _Pragma ("GCC warning \"'ASN1_TYPE_EMPTY' macro is deprecated, use 'NULL' instead.\"") NULL 555 # else 556 # define ASN1_TYPE_EMPTY NULL 557 # endif 558 # endif /* !ASN1_DISABLE_DEPRECATED */ 559 560 /** 561 * static_struct_asn: 562 * 563 * Compat #define. 564 * 565 * Deprecated: 3.0: Use #asn1_static_node instead. 566 */ 567 # ifndef ASN1_DISABLE_DEPRECATED 568 # if _ASN1_GCC_VERSION >= 30100 569 # define static_struct_asn _Pragma ("GCC warning \"'static_struct_asn' macro is deprecated, use 'asn1_static_node_st' instead.\"") asn1_static_node_st 570 # else 571 # define static_struct_asn asn1_static_node_st 572 # endif 573 # endif /* !ASN1_DISABLE_DEPRECATED */ 574 575 /** 576 * ASN1_ARRAY_TYPE: 577 * 578 * Compat #define. 579 * 580 * Deprecated: 3.0: Use #asn1_static_node instead. 581 */ 582 # ifndef ASN1_DISABLE_DEPRECATED 583 # if _ASN1_GCC_VERSION >= 30100 584 # define ASN1_ARRAY_TYPE _Pragma ("GCC warning \"'ASN1_ARRAY_TYPE' macro is deprecated, use 'asn1_static_node' instead.\"") asn1_static_node 585 # else 586 # define ASN1_ARRAY_TYPE asn1_static_node 587 # endif 588 # endif /* !ASN1_DISABLE_DEPRECATED */ 589 590 /** 591 * asn1_static_node_t: 592 * 593 * Compat #define. 594 * 595 * Deprecated: 3.0: Use #asn1_static_node instead. 596 */ 597 # ifndef ASN1_DISABLE_DEPRECATED 598 # if _ASN1_GCC_VERSION >= 30100 599 # define asn1_static_node_t _Pragma ("GCC warning \"'asn1_static_node_t' macro is deprecated, use 'asn1_static_node' instead.\"") asn1_static_node 600 # else 601 # define asn1_static_node_t asn1_static_node 602 # endif 603 # endif /* !ASN1_DISABLE_DEPRECATED */ 604 605 /** 606 * node_data_struct: 607 * 608 * Compat #define. 609 * 610 * Deprecated: 3.0: Use #asn1_data_node_st instead. 611 */ 612 # ifndef ASN1_DISABLE_DEPRECATED 613 # if _ASN1_GCC_VERSION >= 30100 614 # define node_data_struct _Pragma ("GCC warning \"'node_data_struct' macro is deprecated, use 'asn1_data_node_st' instead.\"") asn1_data_node_st 615 # else 616 # define node_data_struct asn1_data_node_st 617 # endif 618 # endif /* !ASN1_DISABLE_DEPRECATED */ 619 620 /** 621 * ASN1_DATA_NODE: 622 * 623 * Compat #define. 624 * 625 * Deprecated: 3.0: Use #asn1_data_node_st instead. 626 */ 627 # ifndef ASN1_DISABLE_DEPRECATED 628 # if _ASN1_GCC_VERSION >= 30100 629 # define ASN1_DATA_NODE _Pragma ("GCC warning \"'asn1_static_node_t' macro is deprecated, use 'asn1_static_node' instead.\"") asn1_data_node_st 630 # else 631 # define ASN1_DATA_NODE asn1_data_node_st 632 # endif 633 # endif /* !ASN1_DISABLE_DEPRECATED */ 634 635 # ifdef __cplusplus 636 } 637 # endif 638 639 #endif /* LIBTASN1_H */ 640