2 * Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free
3 * Software Foundation, Inc.
5 * This file is part of LIBTASN1.
7 * LIBTASN1 is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
12 * LIBTASN1 is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with LIBTASN1; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 # if defined ASN1_BUILDING && defined HAVE_VISIBILITY && HAVE_VISIBILITY
29 # define ASN1_API __attribute__((__visibility__("default")))
30 # elif defined ASN1_BUILDING && defined _MSC_VER && ! defined ASN1_STATIC
31 # define ASN1_API __declspec(dllexport)
32 # elif defined _MSC_VER && ! defined ASN1_STATIC
33 # define ASN1_API __declspec(dllimport)
39 #include <stdio.h> /* for FILE* */
40 #include <sys/types.h>
48 #define ASN1_VERSION "2.7"
50 typedef int asn1_retCode; /* type returned by libtasn1 functions */
52 /*****************************************/
53 /* Errors returned by libtasn1 functions */
54 /*****************************************/
55 #define ASN1_SUCCESS 0
56 #define ASN1_FILE_NOT_FOUND 1
57 #define ASN1_ELEMENT_NOT_FOUND 2
58 #define ASN1_IDENTIFIER_NOT_FOUND 3
59 #define ASN1_DER_ERROR 4
60 #define ASN1_VALUE_NOT_FOUND 5
61 #define ASN1_GENERIC_ERROR 6
62 #define ASN1_VALUE_NOT_VALID 7
63 #define ASN1_TAG_ERROR 8
64 #define ASN1_TAG_IMPLICIT 9
65 #define ASN1_ERROR_TYPE_ANY 10
66 #define ASN1_SYNTAX_ERROR 11
67 #define ASN1_MEM_ERROR 12
68 #define ASN1_MEM_ALLOC_ERROR 13
69 #define ASN1_DER_OVERFLOW 14
70 #define ASN1_NAME_TOO_LONG 15
71 #define ASN1_ARRAY_ERROR 16
72 #define ASN1_ELEMENT_NOT_EMPTY 17
74 /*************************************/
75 /* Constants used in asn1_visit_tree */
76 /*************************************/
77 #define ASN1_PRINT_NAME 1
78 #define ASN1_PRINT_NAME_TYPE 2
79 #define ASN1_PRINT_NAME_TYPE_VALUE 3
80 #define ASN1_PRINT_ALL 4
82 /*****************************************/
83 /* Constants returned by asn1_read_tag */
84 /*****************************************/
85 #define ASN1_CLASS_UNIVERSAL 0x00 /* old: 1 */
86 #define ASN1_CLASS_APPLICATION 0x40 /* old: 2 */
87 #define ASN1_CLASS_CONTEXT_SPECIFIC 0x80 /* old: 3 */
88 #define ASN1_CLASS_PRIVATE 0xC0 /* old: 4 */
89 #define ASN1_CLASS_STRUCTURED 0x20
91 /*****************************************/
92 /* Constants returned by asn1_read_tag */
93 /*****************************************/
94 #define ASN1_TAG_BOOLEAN 0x01
95 #define ASN1_TAG_INTEGER 0x02
96 #define ASN1_TAG_SEQUENCE 0x10
97 #define ASN1_TAG_SET 0x11
98 #define ASN1_TAG_OCTET_STRING 0x04
99 #define ASN1_TAG_BIT_STRING 0x03
100 #define ASN1_TAG_UTCTime 0x17
101 #define ASN1_TAG_GENERALIZEDTime 0x18
102 #define ASN1_TAG_OBJECT_ID 0x06
103 #define ASN1_TAG_ENUMERATED 0x0A
104 #define ASN1_TAG_NULL 0x05
105 #define ASN1_TAG_GENERALSTRING 0x1B
107 /******************************************************/
108 /* Structure definition used for the node of the tree */
109 /* that represent an ASN.1 DEFINITION. */
110 /******************************************************/
112 #if !defined ASN1_BUILDING
113 /* This structure is also in internal.h, but then contains more
114 fields. You cannot make any modifications to these fields
115 without breaking ABI. */
116 struct node_asn_struct
118 char *name; /* Node name */
119 unsigned int type; /* Node type */
120 unsigned char *value; /* Node value */
122 struct node_asn_struct *down; /* Pointer to the son node */
123 struct node_asn_struct *right; /* Pointer to the brother node */
124 struct node_asn_struct *left; /* Pointer to the next list element */
128 typedef struct node_asn_struct node_asn;
130 typedef node_asn *ASN1_TYPE;
132 #define ASN1_TYPE_EMPTY NULL
134 /*****************************************/
135 /* For the on-disk format of ASN.1 trees */
136 /*****************************************/
137 struct static_struct_asn
139 const char *name; /* Node name */
140 unsigned int type; /* Node type */
141 const void *value; /* Node value */
143 typedef struct static_struct_asn ASN1_ARRAY_TYPE;
145 /***********************************/
146 /* Fixed constants */
147 /***********************************/
149 /* maximum number of characters of a name */
150 /* inside a file with ASN1 definitons */
151 #define ASN1_MAX_NAME_SIZE 128
153 /* maximum number of characters */
154 /* of a description message */
155 /* (null character included) */
156 #define ASN1_MAX_ERROR_DESCRIPTION_SIZE 128
158 /***********************************/
159 /* Functions definitions */
160 /***********************************/
162 extern ASN1_API asn1_retCode
163 asn1_parser2tree (const char *file_name,
164 ASN1_TYPE * definitions, char *errorDescription);
166 extern ASN1_API asn1_retCode
167 asn1_parser2array (const char *inputFileName,
168 const char *outputFileName,
169 const char *vectorName, char *errorDescription);
171 extern ASN1_API asn1_retCode
172 asn1_array2tree (const ASN1_ARRAY_TYPE * array,
173 ASN1_TYPE * definitions, char *errorDescription);
176 asn1_print_structure (FILE * out, ASN1_TYPE structure,
177 const char *name, int mode);
179 extern ASN1_API asn1_retCode
180 asn1_create_element (ASN1_TYPE definitions,
181 const char *source_name, ASN1_TYPE * element);
183 extern ASN1_API asn1_retCode asn1_delete_structure (ASN1_TYPE * structure);
185 extern ASN1_API asn1_retCode
186 asn1_delete_element (ASN1_TYPE structure, const char *element_name);
188 extern ASN1_API asn1_retCode
189 asn1_write_value (ASN1_TYPE node_root, const char *name,
190 const void *ivalue, int len);
192 extern ASN1_API asn1_retCode
193 asn1_read_value (ASN1_TYPE root, const char *name,
194 void *ivalue, int *len);
196 extern ASN1_API asn1_retCode
197 asn1_number_of_elements (ASN1_TYPE element, const char *name, int *num);
199 extern ASN1_API asn1_retCode
200 asn1_der_coding (ASN1_TYPE element, const char *name,
201 void *ider, int *len, char *ErrorDescription);
203 extern ASN1_API asn1_retCode
204 asn1_der_decoding (ASN1_TYPE * element, const void *ider,
205 int len, char *errorDescription);
207 extern ASN1_API asn1_retCode
208 asn1_der_decoding_element (ASN1_TYPE * structure,
209 const char *elementName,
210 const void *ider, int len,
211 char *errorDescription);
213 extern ASN1_API asn1_retCode
214 asn1_der_decoding_startEnd (ASN1_TYPE element,
215 const void *ider, int len,
216 const char *name_element,
217 int *start, int *end);
219 extern ASN1_API asn1_retCode
220 asn1_expand_any_defined_by (ASN1_TYPE definitions, ASN1_TYPE * element);
222 extern ASN1_API asn1_retCode
223 asn1_expand_octet_string (ASN1_TYPE definitions,
225 const char *octetName, const char *objectName);
227 extern ASN1_API asn1_retCode
228 asn1_read_tag (ASN1_TYPE root, const char *name,
229 int *tagValue, int *classValue);
231 extern ASN1_API const char *asn1_find_structure_from_oid (ASN1_TYPE
236 extern ASN1_API const char *asn1_check_version (const char *req_version);
238 extern ASN1_API const char *asn1_strerror (asn1_retCode error);
240 extern ASN1_API void asn1_perror (asn1_retCode error);
242 /* DER utility functions. */
245 asn1_get_tag_der (const unsigned char *der, int der_len,
246 unsigned char *cls, int *len, unsigned long *tag);
249 asn1_octet_der (const unsigned char *str, int str_len,
250 unsigned char *der, int *der_len);
252 extern ASN1_API asn1_retCode
253 asn1_get_octet_der (const unsigned char *der, int der_len,
254 int *ret_len, unsigned char *str,
255 int str_size, int *str_len);
257 extern ASN1_API void asn1_bit_der (const unsigned char *str, int bit_len,
258 unsigned char *der, int *der_len);
260 extern ASN1_API asn1_retCode
261 asn1_get_bit_der (const unsigned char *der, int der_len,
262 int *ret_len, unsigned char *str,
263 int str_size, int *bit_len);
265 extern ASN1_API signed long
266 asn1_get_length_der (const unsigned char *der, int der_len, int *len);
268 extern ASN1_API signed long
269 asn1_get_length_ber (const unsigned char *ber, int ber_len, int *len);
272 asn1_length_der (unsigned long int len, unsigned char *ans, int *ans_len);
274 /* Other utility functions. */
276 extern ASN1_API ASN1_TYPE
277 asn1_find_node (ASN1_TYPE pointer, const char *name);
279 extern ASN1_API asn1_retCode
280 asn1_copy_node (ASN1_TYPE dst, const char *dst_name,
281 ASN1_TYPE src, const char *src_name);
283 /* Deprecated stuff. */
285 #ifndef ASN1_DISABLE_DEPRECATED
287 #define LIBTASN1_VERSION ASN1_VERSION
289 #ifndef MAX_NAME_SIZE
290 # define MAX_NAME_SIZE ASN1_MAX_NAME_SIZE
293 #ifndef MAX_ERROR_DESCRIPTION_SIZE
294 # define MAX_ERROR_DESCRIPTION_SIZE ASN1_MAX_ERROR_DESCRIPTION_SIZE
297 #ifndef __attribute__
298 /* This feature is available in gcc versions 2.5 and later. */
299 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
300 # define __attribute__(Spec) /* empty */
304 /* Use asn1_strerror instead. */
305 extern ASN1_API const char *libtasn1_strerror (asn1_retCode error)
306 __attribute__ ((deprecated));
308 /* Use asn1_perror instead. */
310 libtasn1_perror (asn1_retCode error) __attribute__ ((deprecated));
318 #endif /* LIBTASN1_H */