2 * Copyright (C) 2002-2012 Free Software Foundation, Inc.
4 * This file is part of LIBTASN1.
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.
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.
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
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)
38 #include <stdio.h> /* for FILE* */
39 #include <sys/types.h>
47 #define ASN1_VERSION "3.1"
49 /*****************************************/
50 /* Errors returned by libtasn1 functions */
51 /*****************************************/
52 #define ASN1_SUCCESS 0
53 #define ASN1_FILE_NOT_FOUND 1
54 #define ASN1_ELEMENT_NOT_FOUND 2
55 #define ASN1_IDENTIFIER_NOT_FOUND 3
56 #define ASN1_DER_ERROR 4
57 #define ASN1_VALUE_NOT_FOUND 5
58 #define ASN1_GENERIC_ERROR 6
59 #define ASN1_VALUE_NOT_VALID 7
60 #define ASN1_TAG_ERROR 8
61 #define ASN1_TAG_IMPLICIT 9
62 #define ASN1_ERROR_TYPE_ANY 10
63 #define ASN1_SYNTAX_ERROR 11
64 #define ASN1_MEM_ERROR 12
65 #define ASN1_MEM_ALLOC_ERROR 13
66 #define ASN1_DER_OVERFLOW 14
67 #define ASN1_NAME_TOO_LONG 15
68 #define ASN1_ARRAY_ERROR 16
69 #define ASN1_ELEMENT_NOT_EMPTY 17
71 /*************************************/
72 /* Constants used in asn1_visit_tree */
73 /*************************************/
74 #define ASN1_PRINT_NAME 1
75 #define ASN1_PRINT_NAME_TYPE 2
76 #define ASN1_PRINT_NAME_TYPE_VALUE 3
77 #define ASN1_PRINT_ALL 4
79 /*****************************************/
80 /* Constants returned by asn1_read_tag */
81 /*****************************************/
82 #define ASN1_CLASS_UNIVERSAL 0x00 /* old: 1 */
83 #define ASN1_CLASS_APPLICATION 0x40 /* old: 2 */
84 #define ASN1_CLASS_CONTEXT_SPECIFIC 0x80 /* old: 3 */
85 #define ASN1_CLASS_PRIVATE 0xC0 /* old: 4 */
86 #define ASN1_CLASS_STRUCTURED 0x20
88 /*****************************************/
89 /* Constants returned by asn1_read_tag */
90 /*****************************************/
91 #define ASN1_TAG_BOOLEAN 0x01
92 #define ASN1_TAG_INTEGER 0x02
93 #define ASN1_TAG_SEQUENCE 0x10
94 #define ASN1_TAG_SET 0x11
95 #define ASN1_TAG_OCTET_STRING 0x04
96 #define ASN1_TAG_BIT_STRING 0x03
97 #define ASN1_TAG_UTCTime 0x17
98 #define ASN1_TAG_GENERALIZEDTime 0x18
99 #define ASN1_TAG_OBJECT_ID 0x06
100 #define ASN1_TAG_ENUMERATED 0x0A
101 #define ASN1_TAG_NULL 0x05
102 #define ASN1_TAG_GENERALSTRING 0x1B
103 #define ASN1_TAG_NUMERIC_STRING 0x12
104 #define ASN1_TAG_IA5_STRING 0x16
105 #define ASN1_TAG_TELETEX_STRING 0x14
106 #define ASN1_TAG_PRINTABLE_STRING 0x13
107 #define ASN1_TAG_UNIVERSAL_STRING 0x1C
108 #define ASN1_TAG_BMP_STRING 0x1E
109 #define ASN1_TAG_UTF8_STRING 0x0C
110 #define ASN1_TAG_VISIBLE_STRING 0x1A
112 /******************************************************/
113 /* Structure definition used for the node of the tree */
114 /* that represent an ASN.1 DEFINITION. */
115 /******************************************************/
117 typedef struct asn1_node_st asn1_node_st
;
119 typedef asn1_node_st
*asn1_node
;
121 /* maximum number of characters of a name */
122 /* inside a file with ASN1 definitons */
123 #define ASN1_MAX_NAME_SIZE 64
126 /*****************************************/
127 /* For the on-disk format of ASN.1 trees */
128 /*****************************************/
129 struct asn1_static_node_st
131 const char *name
; /* Node name */
132 unsigned int type
; /* Node type */
133 const void *value
; /* Node value */
135 typedef struct asn1_static_node_st asn1_static_node
;
137 /* List of constants for field type of node_asn */
138 #define ASN1_ETYPE_INVALID 0
139 #define ASN1_ETYPE_CONSTANT 1
140 #define ASN1_ETYPE_IDENTIFIER 2
141 #define ASN1_ETYPE_INTEGER 3
142 #define ASN1_ETYPE_BOOLEAN 4
143 #define ASN1_ETYPE_SEQUENCE 5
144 #define ASN1_ETYPE_BIT_STRING 6
145 #define ASN1_ETYPE_OCTET_STRING 7
146 #define ASN1_ETYPE_TAG 8
147 #define ASN1_ETYPE_DEFAULT 9
148 #define ASN1_ETYPE_SIZE 10
149 #define ASN1_ETYPE_SEQUENCE_OF 11
150 #define ASN1_ETYPE_OBJECT_ID 12
151 #define ASN1_ETYPE_ANY 13
152 #define ASN1_ETYPE_SET 14
153 #define ASN1_ETYPE_SET_OF 15
154 #define ASN1_ETYPE_DEFINITIONS 16
155 #define ASN1_ETYPE_CHOICE 18
156 #define ASN1_ETYPE_IMPORTS 19
157 #define ASN1_ETYPE_NULL 20
158 #define ASN1_ETYPE_ENUMERATED 21
159 #define ASN1_ETYPE_GENERALSTRING 27
160 #define ASN1_ETYPE_NUMERIC_STRING 28
161 #define ASN1_ETYPE_IA5_STRING 29
162 #define ASN1_ETYPE_TELETEX_STRING 30
163 #define ASN1_ETYPE_PRINTABLE_STRING 31
164 #define ASN1_ETYPE_UNIVERSAL_STRING 32
165 #define ASN1_ETYPE_BMP_STRING 33
166 #define ASN1_ETYPE_UTF8_STRING 34
167 #define ASN1_ETYPE_VISIBLE_STRING 35
168 #define ASN1_ETYPE_UTC_TIME 36
169 #define ASN1_ETYPE_GENERALIZED_TIME 37
171 struct asn1_data_node_st
173 const char *name
; /* Node name */
174 const void *value
; /* Node value */
175 unsigned int value_len
; /* Node value size */
176 unsigned int type
; /* Node value type (ASN1_ETYPE_*) */
178 typedef struct asn1_data_node_st asn1_data_node_st
;
180 /***********************************/
181 /* Fixed constants */
182 /***********************************/
185 /* maximum number of characters */
186 /* of a description message */
187 /* (null character included) */
188 #define ASN1_MAX_ERROR_DESCRIPTION_SIZE 128
190 /***********************************/
191 /* Functions definitions */
192 /***********************************/
195 asn1_parser2tree (const char *file_name
,
196 asn1_node
* definitions
, char *errorDescription
);
199 asn1_parser2array (const char *inputFileName
,
200 const char *outputFileName
,
201 const char *vectorName
, char *errorDescription
);
204 asn1_array2tree (const asn1_static_node
* array
,
205 asn1_node
* definitions
, char *errorDescription
);
208 asn1_print_structure (FILE * out
, asn1_node structure
,
209 const char *name
, int mode
);
212 asn1_create_element (asn1_node definitions
,
213 const char *source_name
, asn1_node
* element
);
215 extern ASN1_API
int asn1_delete_structure (asn1_node
* structure
);
218 asn1_delete_element (asn1_node structure
, const char *element_name
);
221 asn1_write_value (asn1_node node_root
, const char *name
,
222 const void *ivalue
, int len
);
225 asn1_read_value (asn1_node root
, const char *name
,
226 void *ivalue
, int *len
);
229 asn1_read_value_type (asn1_node root
, const char *name
,
230 void *ivalue
, int *len
, unsigned int* etype
);
233 asn1_read_node_value (asn1_node node
, asn1_data_node_st
* data
);
236 asn1_number_of_elements (asn1_node element
, const char *name
, int *num
);
239 asn1_der_coding (asn1_node element
, const char *name
,
240 void *ider
, int *len
, char *ErrorDescription
);
243 asn1_der_decoding (asn1_node
* element
, const void *ider
,
244 int len
, char *errorDescription
);
247 asn1_der_decoding_element (asn1_node
* structure
,
248 const char *elementName
,
249 const void *ider
, int len
,
250 char *errorDescription
);
253 asn1_der_decoding_startEnd (asn1_node element
,
254 const void *ider
, int len
,
255 const char *name_element
,
256 int *start
, int *end
);
259 asn1_expand_any_defined_by (asn1_node definitions
, asn1_node
* element
);
262 asn1_expand_octet_string (asn1_node definitions
,
264 const char *octetName
, const char *objectName
);
267 asn1_read_tag (asn1_node root
, const char *name
,
268 int *tagValue
, int *classValue
);
270 extern ASN1_API
const char *asn1_find_structure_from_oid (asn1_node
275 extern ASN1_API
const char *asn1_check_version (const char *req_version
);
277 extern ASN1_API
const char *asn1_strerror (int error
);
279 extern ASN1_API
void asn1_perror (int error
);
281 #define ASN1_MAX_TAG_SIZE 4
282 #define ASN1_MAX_LENGTH_SIZE 9
283 #define ASN1_MAX_TL_SIZE (ASN1_MAX_TAG_SIZE+ASN1_MAX_LENGTH_SIZE)
285 asn1_get_length_der (const unsigned char *der
, int der_len
, int *len
);
288 asn1_get_length_ber (const unsigned char *ber
, int ber_len
, int *len
);
291 asn1_length_der (unsigned long int len
, unsigned char *ans
, int *ans_len
);
293 /* Other utility functions. */
296 int asn1_decode_simple_der (unsigned int etype
, const unsigned char *der
, unsigned int der_len
,
297 const unsigned char **str
, unsigned int *str_len
);
300 asn1_encode_simple_der (unsigned int etype
, const unsigned char *str
, unsigned int str_len
,
301 unsigned char *tl
, unsigned int *tl_len
);
303 extern ASN1_API asn1_node
304 asn1_find_node (asn1_node pointer
, const char *name
);
307 asn1_copy_node (asn1_node dst
, const char *dst_name
,
308 asn1_node src
, const char *src_name
);
310 /* Internal and low-level DER utility functions. */
313 asn1_get_tag_der (const unsigned char *der
, int der_len
,
314 unsigned char *cls
, int *len
, unsigned long *tag
);
317 asn1_octet_der (const unsigned char *str
, int str_len
,
318 unsigned char *der
, int *der_len
);
321 asn1_get_octet_der (const unsigned char *der
, int der_len
,
322 int *ret_len
, unsigned char *str
,
323 int str_size
, int *str_len
);
325 extern ASN1_API
void asn1_bit_der (const unsigned char *str
, int bit_len
,
326 unsigned char *der
, int *der_len
);
329 asn1_get_bit_der (const unsigned char *der
, int der_len
,
330 int *ret_len
, unsigned char *str
,
331 int str_size
, int *bit_len
);
333 /* Compatibility types */
335 typedef int asn1_retCode
; /* type returned by libtasn1 functions */
337 #define node_asn_struct asn1_node_st
338 #define node_asn asn1_node_st
339 #define ASN1_TYPE asn1_node
340 #define ASN1_TYPE_EMPTY NULL
342 #define static_struct_asn asn1_static_node_st
343 #define ASN1_ARRAY_TYPE asn1_static_node
344 #define asn1_static_node_t asn1_static_node
346 #define node_data_struct asn1_data_node_st
347 #define ASN1_DATA_NODE asn1_data_node_st
353 #endif /* LIBTASN1_H */