| 1 | /* |
| 2 | * Copyright (C) 2002-2014 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 | #ifndef LIBTASN1_H |
| 24 | #define LIBTASN1_H |
| 25 | |
| 26 | #ifndef ASN1_API |
| 27 | #if defined ASN1_BUILDING && defined HAVE_VISIBILITY && HAVE_VISIBILITY |
| 28 | #define ASN1_API __attribute__((__visibility__("default"))) |
| 29 | #elif defined ASN1_BUILDING && defined _MSC_VER && ! defined ASN1_STATIC |
| 30 | #define ASN1_API __declspec(dllexport) |
| 31 | #elif defined _MSC_VER && ! defined ASN1_STATIC |
| 32 | #define ASN1_API __declspec(dllimport) |
| 33 | #else |
| 34 | #define ASN1_API |
| 35 | #endif |
| 36 | #endif |
| 37 | |
| 38 | #include <sys/types.h> |
| 39 | #include <time.h> |
| 40 | #include <stdio.h> /* for FILE* */ |
| 41 | |
| 42 | #ifdef __cplusplus |
| 43 | extern "C" |
| 44 | { |
| 45 | #endif |
| 46 | |
| 47 | #define ASN1_VERSION "4.13" |
| 48 | |
| 49 | #if defined(__GNUC__) && !defined(ASN1_INTERNAL_BUILD) |
| 50 | # define _ASN1_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) |
| 51 | # if _ASN1_GCC_VERSION >= 30100 |
| 52 | # define _ASN1_GCC_ATTR_DEPRECATED __attribute__ ((__deprecated__)) |
| 53 | # endif |
| 54 | #endif |
| 55 | |
| 56 | #ifndef _ASN1_GCC_ATTR_DEPRECATED |
| 57 | #define _ASN1_GCC_ATTR_DEPRECATED |
| 58 | #endif |
| 59 | |
| 60 | /*****************************************/ |
| 61 | /* Errors returned by libtasn1 functions */ |
| 62 | /*****************************************/ |
| 63 | #define ASN1_SUCCESS 0 |
| 64 | #define ASN1_FILE_NOT_FOUND 1 |
| 65 | #define ASN1_ELEMENT_NOT_FOUND 2 |
| 66 | #define ASN1_IDENTIFIER_NOT_FOUND 3 |
| 67 | #define ASN1_DER_ERROR 4 |
| 68 | #define ASN1_VALUE_NOT_FOUND 5 |
| 69 | #define ASN1_GENERIC_ERROR 6 |
| 70 | #define ASN1_VALUE_NOT_VALID 7 |
| 71 | #define ASN1_TAG_ERROR 8 |
| 72 | #define ASN1_TAG_IMPLICIT 9 |
| 73 | #define ASN1_ERROR_TYPE_ANY 10 |
| 74 | #define ASN1_SYNTAX_ERROR 11 |
| 75 | #define ASN1_MEM_ERROR 12 |
| 76 | #define ASN1_MEM_ALLOC_ERROR 13 |
| 77 | #define ASN1_DER_OVERFLOW 14 |
| 78 | #define ASN1_NAME_TOO_LONG 15 |
| 79 | #define ASN1_ARRAY_ERROR 16 |
| 80 | #define ASN1_ELEMENT_NOT_EMPTY 17 |
| 81 | #define ASN1_TIME_ENCODING_ERROR 18 |
| 82 | |
| 83 | /*************************************/ |
| 84 | /* Constants used in asn1_visit_tree */ |
| 85 | /*************************************/ |
| 86 | #define ASN1_PRINT_NAME 1 |
| 87 | #define ASN1_PRINT_NAME_TYPE 2 |
| 88 | #define ASN1_PRINT_NAME_TYPE_VALUE 3 |
| 89 | #define ASN1_PRINT_ALL 4 |
| 90 | |
| 91 | /*****************************************/ |
| 92 | /* Constants returned by asn1_read_tag */ |
| 93 | /*****************************************/ |
| 94 | #define ASN1_CLASS_UNIVERSAL 0x00 /* old: 1 */ |
| 95 | #define ASN1_CLASS_APPLICATION 0x40 /* old: 2 */ |
| 96 | #define ASN1_CLASS_CONTEXT_SPECIFIC 0x80 /* old: 3 */ |
| 97 | #define ASN1_CLASS_PRIVATE 0xC0 /* old: 4 */ |
| 98 | #define ASN1_CLASS_STRUCTURED 0x20 |
| 99 | |
| 100 | /*****************************************/ |
| 101 | /* Constants returned by asn1_read_tag */ |
| 102 | /*****************************************/ |
| 103 | #define ASN1_TAG_BOOLEAN 0x01 |
| 104 | #define ASN1_TAG_INTEGER 0x02 |
| 105 | #define ASN1_TAG_SEQUENCE 0x10 |
| 106 | #define ASN1_TAG_SET 0x11 |
| 107 | #define ASN1_TAG_OCTET_STRING 0x04 |
| 108 | #define ASN1_TAG_BIT_STRING 0x03 |
| 109 | #define ASN1_TAG_UTCTime 0x17 |
| 110 | #define ASN1_TAG_GENERALIZEDTime 0x18 |
| 111 | #define ASN1_TAG_OBJECT_ID 0x06 |
| 112 | #define ASN1_TAG_ENUMERATED 0x0A |
| 113 | #define ASN1_TAG_NULL 0x05 |
| 114 | #define ASN1_TAG_GENERALSTRING 0x1B |
| 115 | #define ASN1_TAG_NUMERIC_STRING 0x12 |
| 116 | #define ASN1_TAG_IA5_STRING 0x16 |
| 117 | #define ASN1_TAG_TELETEX_STRING 0x14 |
| 118 | #define ASN1_TAG_PRINTABLE_STRING 0x13 |
| 119 | #define ASN1_TAG_UNIVERSAL_STRING 0x1C |
| 120 | #define ASN1_TAG_BMP_STRING 0x1E |
| 121 | #define ASN1_TAG_UTF8_STRING 0x0C |
| 122 | #define ASN1_TAG_VISIBLE_STRING 0x1A |
| 123 | |
| 124 | /******************************************************/ |
| 125 | /* Structure definition used for the node of the tree */ |
| 126 | /* that represent an ASN.1 DEFINITION. */ |
| 127 | /******************************************************/ |
| 128 | |
| 129 | typedef struct asn1_node_st asn1_node_st; |
| 130 | |
| 131 | typedef asn1_node_st *asn1_node; |
| 132 | |
| 133 | /* maximum number of characters of a name */ |
| 134 | /* inside a file with ASN1 definitons */ |
| 135 | #define ASN1_MAX_NAME_SIZE 64 |
| 136 | |
| 137 | |
| 138 | /*****************************************/ |
| 139 | /* For the on-disk format of ASN.1 trees */ |
| 140 | /*****************************************/ |
| 141 | struct asn1_static_node_st |
| 142 | { |
| 143 | const char *name; /* Node name */ |
| 144 | unsigned int type; /* Node type */ |
| 145 | const void *value; /* Node value */ |
| 146 | }; |
| 147 | typedef struct asn1_static_node_st asn1_static_node; |
| 148 | |
| 149 | /* List of constants for field type of node_asn */ |
| 150 | #define ASN1_ETYPE_INVALID 0 |
| 151 | #define ASN1_ETYPE_CONSTANT 1 |
| 152 | #define ASN1_ETYPE_IDENTIFIER 2 |
| 153 | #define ASN1_ETYPE_INTEGER 3 |
| 154 | #define ASN1_ETYPE_BOOLEAN 4 |
| 155 | #define ASN1_ETYPE_SEQUENCE 5 |
| 156 | #define ASN1_ETYPE_BIT_STRING 6 |
| 157 | #define ASN1_ETYPE_OCTET_STRING 7 |
| 158 | #define ASN1_ETYPE_TAG 8 |
| 159 | #define ASN1_ETYPE_DEFAULT 9 |
| 160 | #define ASN1_ETYPE_SIZE 10 |
| 161 | #define ASN1_ETYPE_SEQUENCE_OF 11 |
| 162 | #define ASN1_ETYPE_OBJECT_ID 12 |
| 163 | #define ASN1_ETYPE_ANY 13 |
| 164 | #define ASN1_ETYPE_SET 14 |
| 165 | #define ASN1_ETYPE_SET_OF 15 |
| 166 | #define ASN1_ETYPE_DEFINITIONS 16 |
| 167 | #define ASN1_ETYPE_CHOICE 18 |
| 168 | #define ASN1_ETYPE_IMPORTS 19 |
| 169 | #define ASN1_ETYPE_NULL 20 |
| 170 | #define ASN1_ETYPE_ENUMERATED 21 |
| 171 | #define ASN1_ETYPE_GENERALSTRING 27 |
| 172 | #define ASN1_ETYPE_NUMERIC_STRING 28 |
| 173 | #define ASN1_ETYPE_IA5_STRING 29 |
| 174 | #define ASN1_ETYPE_TELETEX_STRING 30 |
| 175 | #define ASN1_ETYPE_PRINTABLE_STRING 31 |
| 176 | #define ASN1_ETYPE_UNIVERSAL_STRING 32 |
| 177 | #define ASN1_ETYPE_BMP_STRING 33 |
| 178 | #define ASN1_ETYPE_UTF8_STRING 34 |
| 179 | #define ASN1_ETYPE_VISIBLE_STRING 35 |
| 180 | #define ASN1_ETYPE_UTC_TIME 36 |
| 181 | #define ASN1_ETYPE_GENERALIZED_TIME 37 |
| 182 | |
| 183 | /* Flags used by asn1_delete_structure2() */ |
| 184 | |
| 185 | /* makes sure the values are zeroized prior to deinitialization */ |
| 186 | #define ASN1_DELETE_FLAG_ZEROIZE 1 |
| 187 | |
| 188 | /* Flags used by asn1_der_decoding2(). */ |
| 189 | |
| 190 | /* This flag would allow arbitrary data past the DER data */ |
| 191 | #define ASN1_DECODE_FLAG_ALLOW_PADDING 1 |
| 192 | /* This flag would ensure that no BER decoding takes place */ |
| 193 | #define ASN1_DECODE_FLAG_STRICT_DER (1<<1) |
| 194 | /* This flag will tolerate Time encoding errors when in strict DER */ |
| 195 | #define ASN1_DECODE_FLAG_ALLOW_INCORRECT_TIME (1<<2) |
| 196 | |
| 197 | |
| 198 | struct asn1_data_node_st |
| 199 | { |
| 200 | const char *name; /* Node name */ |
| 201 | const void *value; /* Node value */ |
| 202 | unsigned int value_len; /* Node value size */ |
| 203 | unsigned int type; /* Node value type (ASN1_ETYPE_*) */ |
| 204 | }; |
| 205 | typedef struct asn1_data_node_st asn1_data_node_st; |
| 206 | |
| 207 | /***********************************/ |
| 208 | /* Fixed constants */ |
| 209 | /***********************************/ |
| 210 | |
| 211 | |
| 212 | /* maximum number of characters */ |
| 213 | /* of a description message */ |
| 214 | /* (null character included) */ |
| 215 | #define ASN1_MAX_ERROR_DESCRIPTION_SIZE 128 |
| 216 | |
| 217 | /***********************************/ |
| 218 | /* Functions definitions */ |
| 219 | /***********************************/ |
| 220 | |
| 221 | extern ASN1_API int |
| 222 | asn1_parser2tree (const char *file, |
| 223 | asn1_node * definitions, char *error_desc); |
| 224 | |
| 225 | extern ASN1_API int |
| 226 | asn1_parser2array (const char *inputFileName, |
| 227 | const char *outputFileName, |
| 228 | const char *vectorName, char *error_desc); |
| 229 | |
| 230 | extern ASN1_API int |
| 231 | asn1_array2tree (const asn1_static_node * array, |
| 232 | asn1_node * definitions, char *errorDescription); |
| 233 | |
| 234 | extern ASN1_API void |
| 235 | asn1_print_structure (FILE * out, asn1_node structure, |
| 236 | const char *name, int mode); |
| 237 | |
| 238 | extern ASN1_API int |
| 239 | asn1_create_element (asn1_node definitions, |
| 240 | const char *source_name, asn1_node * element); |
| 241 | |
| 242 | extern ASN1_API int asn1_delete_structure (asn1_node * structure); |
| 243 | |
| 244 | extern ASN1_API int asn1_delete_structure2 (asn1_node * structure, unsigned int flags); |
| 245 | |
| 246 | extern ASN1_API int |
| 247 | asn1_delete_element (asn1_node structure, const char *element_name); |
| 248 | |
| 249 | extern ASN1_API int |
| 250 | asn1_write_value (asn1_node node_root, const char *name, |
| 251 | const void *ivalue, int len); |
| 252 | |
| 253 | extern ASN1_API int |
| 254 | asn1_read_value (asn1_node root, const char *name, |
| 255 | void *ivalue, int *len); |
| 256 | |
| 257 | extern ASN1_API int |
| 258 | asn1_read_value_type (asn1_node root, const char *name, |
| 259 | void *ivalue, int *len, unsigned int *etype); |
| 260 | |
| 261 | extern ASN1_API int |
| 262 | asn1_read_node_value (asn1_node node, asn1_data_node_st * data); |
| 263 | |
| 264 | extern ASN1_API int |
| 265 | asn1_number_of_elements (asn1_node element, const char *name, int *num); |
| 266 | |
| 267 | extern ASN1_API int |
| 268 | asn1_der_coding (asn1_node element, const char *name, |
| 269 | void *ider, int *len, char *ErrorDescription); |
| 270 | |
| 271 | extern ASN1_API int |
| 272 | asn1_der_decoding2 (asn1_node *element, const void *ider, |
| 273 | int *max_ider_len, unsigned int flags, |
| 274 | char *errorDescription); |
| 275 | |
| 276 | extern ASN1_API int |
| 277 | asn1_der_decoding (asn1_node * element, const void *ider, |
| 278 | int len, char *errorDescription); |
| 279 | |
| 280 | /* Do not use. Use asn1_der_decoding() instead. */ |
| 281 | extern ASN1_API int |
| 282 | asn1_der_decoding_element (asn1_node * structure, |
| 283 | const char *elementName, |
| 284 | const void *ider, int len, |
| 285 | char *errorDescription) _ASN1_GCC_ATTR_DEPRECATED; |
| 286 | |
| 287 | extern ASN1_API int |
| 288 | asn1_der_decoding_startEnd (asn1_node element, |
| 289 | const void *ider, int len, |
| 290 | const char *name_element, |
| 291 | int *start, int *end); |
| 292 | |
| 293 | extern ASN1_API int |
| 294 | asn1_expand_any_defined_by (asn1_node definitions, asn1_node * element); |
| 295 | |
| 296 | extern ASN1_API int |
| 297 | asn1_expand_octet_string (asn1_node definitions, |
| 298 | asn1_node * element, |
| 299 | const char *octetName, const char *objectName); |
| 300 | |
| 301 | extern ASN1_API int |
| 302 | asn1_read_tag (asn1_node root, const char *name, |
| 303 | int *tagValue, int *classValue); |
| 304 | |
| 305 | extern ASN1_API const char *asn1_find_structure_from_oid (asn1_node |
| 306 | definitions, |
| 307 | const char |
| 308 | *oidValue); |
| 309 | |
| 310 | extern ASN1_API const char *asn1_check_version (const char *req_version); |
| 311 | |
| 312 | extern ASN1_API const char *asn1_strerror (int error); |
| 313 | |
| 314 | extern ASN1_API void asn1_perror (int error); |
| 315 | |
| 316 | #define ASN1_MAX_TAG_SIZE 4 |
| 317 | #define ASN1_MAX_LENGTH_SIZE 9 |
| 318 | #define ASN1_MAX_TL_SIZE (ASN1_MAX_TAG_SIZE+ASN1_MAX_LENGTH_SIZE) |
| 319 | extern ASN1_API long |
| 320 | asn1_get_length_der (const unsigned char *der, int der_len, int *len); |
| 321 | |
| 322 | extern ASN1_API long |
| 323 | asn1_get_length_ber (const unsigned char *ber, int ber_len, int *len); |
| 324 | |
| 325 | extern ASN1_API void |
| 326 | asn1_length_der (unsigned long int len, unsigned char *der, int *der_len); |
| 327 | |
| 328 | /* Other utility functions. */ |
| 329 | |
| 330 | extern ASN1_API |
| 331 | int asn1_decode_simple_der (unsigned int etype, const unsigned char *der, |
| 332 | unsigned int der_len, |
| 333 | const unsigned char **str, |
| 334 | unsigned int *str_len); |
| 335 | |
| 336 | extern ASN1_API |
| 337 | int asn1_decode_simple_ber (unsigned int etype, const unsigned char *der, |
| 338 | unsigned int der_len, |
| 339 | unsigned char **str, |
| 340 | unsigned int *str_len, |
| 341 | unsigned int *ber_len); |
| 342 | |
| 343 | extern ASN1_API int |
| 344 | asn1_encode_simple_der (unsigned int etype, const unsigned char *str, |
| 345 | unsigned int str_len, unsigned char *tl, |
| 346 | unsigned int *tl_len); |
| 347 | |
| 348 | extern ASN1_API asn1_node |
| 349 | asn1_find_node (asn1_node pointer, const char *name); |
| 350 | |
| 351 | extern ASN1_API int |
| 352 | asn1_copy_node (asn1_node dst, const char *dst_name, |
| 353 | asn1_node src, const char *src_name); |
| 354 | extern ASN1_API asn1_node |
| 355 | asn1_dup_node (asn1_node src, const char *src_name); |
| 356 | |
| 357 | /* Internal and low-level DER utility functions. */ |
| 358 | |
| 359 | extern ASN1_API int |
| 360 | asn1_get_tag_der (const unsigned char *der, int der_len, |
| 361 | unsigned char *cls, int *len, unsigned long *tag); |
| 362 | |
| 363 | extern ASN1_API void |
| 364 | asn1_octet_der (const unsigned char *str, int str_len, |
| 365 | unsigned char *der, int *der_len); |
| 366 | |
| 367 | extern ASN1_API int |
| 368 | asn1_get_octet_der (const unsigned char *der, int der_len, |
| 369 | int *ret_len, unsigned char *str, |
| 370 | int str_size, int *str_len); |
| 371 | |
| 372 | extern ASN1_API void asn1_bit_der (const unsigned char *str, int bit_len, |
| 373 | unsigned char *der, int *der_len); |
| 374 | |
| 375 | extern ASN1_API int |
| 376 | asn1_get_bit_der (const unsigned char *der, int der_len, |
| 377 | int *ret_len, unsigned char *str, |
| 378 | int str_size, int *bit_len); |
| 379 | |
| 380 | extern ASN1_API int |
| 381 | asn1_get_object_id_der (const unsigned char *der, |
| 382 | int der_len, int *ret_len, |
| 383 | char *str, int str_size); |
| 384 | |
| 385 | /* Compatibility types */ |
| 386 | |
| 387 | typedef int asn1_retCode; /* type returned by libtasn1 functions */ |
| 388 | |
| 389 | #define node_asn_struct asn1_node_st |
| 390 | #define node_asn asn1_node_st |
| 391 | #define ASN1_TYPE asn1_node |
| 392 | #define ASN1_TYPE_EMPTY NULL |
| 393 | |
| 394 | #define static_struct_asn asn1_static_node_st |
| 395 | #define ASN1_ARRAY_TYPE asn1_static_node |
| 396 | #define asn1_static_node_t asn1_static_node |
| 397 | |
| 398 | #define node_data_struct asn1_data_node_st |
| 399 | #define ASN1_DATA_NODE asn1_data_node_st |
| 400 | |
| 401 | #ifdef __cplusplus |
| 402 | } |
| 403 | #endif |
| 404 | |
| 405 | #endif /* LIBTASN1_H */ |
| 406 | |