Add ASN1_TYPE_FIELD.
[libtasn1.git] / lib / libtasn1.h
blobee029902642f51b7645b65ee4aff1abe55dae016
1 /*
2 * Copyright (C) 2004, 2005, 2006 Free Software Foundation
3 * Copyright (C) 2002 Fabio Fiorina
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
20 * 02110-1301, USA
24 #ifndef LIBTASN1_H
25 # define LIBTASN1_H
27 #include <stdio.h> /* for FILE* */
29 #ifdef __cplusplus
30 extern "C"
32 #endif
34 #define LIBTASN1_VERSION "0.3.1"
36 #include <sys/types.h>
37 #include <time.h>
39 #define MAX_NAME_SIZE 128 /* maximum number of characters of a name */
40 /* inside a file with ASN1 definitons */
41 #define MAX_ERROR_DESCRIPTION_SIZE 128 /* maximum number of characters */
42 /* of a description message */
43 /* (null character included) */
46 typedef int asn1_retCode; /* type returned by libtasn1 functions */
48 /*****************************************/
49 /* Errors returned by libtasn1 functions */
50 /*****************************************/
51 #define ASN1_SUCCESS 0
52 #define ASN1_FILE_NOT_FOUND 1
53 #define ASN1_ELEMENT_NOT_FOUND 2
54 #define ASN1_IDENTIFIER_NOT_FOUND 3
55 #define ASN1_DER_ERROR 4
56 #define ASN1_VALUE_NOT_FOUND 5
57 #define ASN1_GENERIC_ERROR 6
58 #define ASN1_VALUE_NOT_VALID 7
59 #define ASN1_TAG_ERROR 8
60 #define ASN1_TAG_IMPLICIT 9
61 #define ASN1_ERROR_TYPE_ANY 10
62 #define ASN1_SYNTAX_ERROR 11
63 #define ASN1_MEM_ERROR 12
64 #define ASN1_MEM_ALLOC_ERROR 13
65 #define ASN1_DER_OVERFLOW 14
66 #define ASN1_NAME_TOO_LONG 15
67 #define ASN1_ARRAY_ERROR 16
68 #define ASN1_ELEMENT_NOT_EMPTY 17
70 /*************************************/
71 /* Constants used in asn1_visit_tree */
72 /*************************************/
73 #define ASN1_PRINT_NAME 1
74 #define ASN1_PRINT_NAME_TYPE 2
75 #define ASN1_PRINT_NAME_TYPE_VALUE 3
76 #define ASN1_PRINT_ALL 4
78 /*****************************************/
79 /* Constants returned by asn1_read_tag */
80 /*****************************************/
81 #define ASN1_CLASS_UNIVERSAL 0x00 /* old: 1 */
82 #define ASN1_CLASS_APPLICATION 0x40 /* old: 2 */
83 #define ASN1_CLASS_CONTEXT_SPECIFIC 0x80 /* old: 3 */
84 #define ASN1_CLASS_PRIVATE 0xC0 /* old: 4 */
85 #define ASN1_CLASS_STRUCTURED 0x20
87 /*****************************************/
88 /* Constants returned by asn1_read_tag */
89 /*****************************************/
90 #define ASN1_TAG_BOOLEAN 0x01
91 #define ASN1_TAG_INTEGER 0x02
92 #define ASN1_TAG_SEQUENCE 0x10
93 #define ASN1_TAG_SET 0x11
94 #define ASN1_TAG_OCTET_STRING 0x04
95 #define ASN1_TAG_BIT_STRING 0x03
96 #define ASN1_TAG_UTCTime 0x17
97 #define ASN1_TAG_GENERALIZEDTime 0x18
98 #define ASN1_TAG_OBJECT_ID 0x06
99 #define ASN1_TAG_ENUMERATED 0x0A
100 #define ASN1_TAG_NULL 0x05
101 #define ASN1_TAG_GENERALSTRING 0x1B
103 /******************************************************/
104 /* List of constants for field type of typedef node_asn */
105 /********************************************************/
106 #define ASN1_TYPE_CONSTANT 1
107 #define ASN1_TYPE_IDENTIFIER 2
108 #define ASN1_TYPE_INTEGER 3
109 #define ASN1_TYPE_BOOLEAN 4
110 #define ASN1_TYPE_SEQUENCE 5
111 #define ASN1_TYPE_BIT_STRING 6
112 #define ASN1_TYPE_OCTET_STRING 7
113 #define ASN1_TYPE_TAG 8
114 #define ASN1_TYPE_DEFAULT 9
115 #define ASN1_TYPE_SIZE 10
116 #define ASN1_TYPE_SEQUENCE_OF 11
117 #define ASN1_TYPE_OBJECT_ID 12
118 #define ASN1_TYPE_ANY 13
119 #define ASN1_TYPE_SET 14
120 #define ASN1_TYPE_SET_OF 15
121 #define ASN1_TYPE_DEFINITIONS 16
122 #define ASN1_TYPE_TIME 17
123 #define ASN1_TYPE_CHOICE 18
124 #define ASN1_TYPE_IMPORTS 19
125 #define ASN1_TYPE_NULL 20
126 #define ASN1_TYPE_ENUMERATED 21
127 #define ASN1_TYPE_GENERALSTRING 27
129 /***********************************************************************/
130 /* List of constants to better specify the type of typedef node_asn. */
131 /* Used with TYPE_TAG. */
132 /***********************************************************************/
133 #define ASN1_CONST_UNIVERSAL (1<<8)
134 #define ASN1_CONST_PRIVATE (1<<9)
135 #define ASN1_CONST_APPLICATION (1<<10)
136 #define ASN1_CONST_EXPLICIT (1<<11)
137 #define ASN1_CONST_IMPLICIT (1<<12)
138 #define ASN1_CONST_TAG (1<<13) /* Used in ASN.1 assignement */
139 #define ASN1_CONST_OPTION (1<<14)
140 #define ASN1_CONST_DEFAULT (1<<15)
141 #define ASN1_CONST_TRUE (1<<16)
142 #define ASN1_CONST_FALSE (1<<17)
143 #define ASN1_CONST_LIST (1<<18) /* Used with TYPE_INTEGER and TYPE_BIT_STRING */
144 #define ASN1_CONST_MIN_MAX (1<<19)
145 #define ASN1_CONST_1_PARAM (1<<20)
146 #define ASN1_CONST_SIZE (1<<21)
147 #define ASN1_CONST_DEFINED_BY (1<<22)
148 #define ASN1_CONST_GENERALIZED (1<<23)
149 #define ASN1_CONST_UTC (1<<24)
150 /* #define ASN1_CONST_IMPORTS (1<<25) */
151 #define ASN1_CONST_NOT_USED (1<<26)
152 #define ASN1_CONST_SET (1<<27)
153 #define ASN1_CONST_ASSIGN (1<<28)
154 #define ASN1_CONST_DOWN (1<<29)
155 #define ASN1_CONST_RIGHT (1<<30)
157 /****************************************/
158 /* Returns the first 8 bits. */
159 /* Used with the field type of node_asn */
160 /****************************************/
161 #define ASN1_TYPE_FIELD(x) (x&0xFF)
163 /******************************************************/
164 /* Structure definition used for the node of the tree */
165 /* that represent an ASN.1 DEFINITION. */
166 /******************************************************/
167 typedef struct node_asn_struct
169 char *name; /* Node name */
170 unsigned int type; /* Node type */
171 unsigned char *value; /* Node value */
172 int value_len;
173 struct node_asn_struct *down; /* Pointer to the son node */
174 struct node_asn_struct *right; /* Pointer to the brother node */
175 struct node_asn_struct *left; /* Pointer to the next list element */
176 } node_asn;
178 typedef node_asn *ASN1_TYPE;
180 #define ASN1_TYPE_EMPTY NULL
182 struct static_struct_asn
184 char *name; /* Node name */
185 unsigned int type; /* Node type */
186 unsigned char *value; /* Node value */
189 typedef struct static_struct_asn ASN1_ARRAY_TYPE;
193 /***********************************/
194 /* Functions definitions */
195 /***********************************/
197 asn1_retCode asn1_parser2tree (const char *file_name,
198 ASN1_TYPE * definitions,
199 char *errorDescription);
201 asn1_retCode asn1_parser2array (const char *inputFileName,
202 const char *outputFileName,
203 const char *vectorName,
204 char *errorDescription);
206 asn1_retCode asn1_array2tree (const ASN1_ARRAY_TYPE * array,
207 ASN1_TYPE * definitions,
208 char *errorDescription);
210 void asn1_print_structure (FILE *out, ASN1_TYPE structure, const char *name,
211 int mode);
213 asn1_retCode asn1_create_element (ASN1_TYPE definitions,
214 const char *source_name,
215 ASN1_TYPE * element);
217 asn1_retCode asn1_delete_structure (ASN1_TYPE * structure);
219 asn1_retCode asn1_delete_element (ASN1_TYPE structure,
220 const char *element_name);
222 asn1_retCode asn1_write_value (ASN1_TYPE node_root, const char *name,
223 const void *ivalue, int len);
225 asn1_retCode asn1_read_value (ASN1_TYPE root, const char *name,
226 void *ivalue, int *len);
228 asn1_retCode asn1_number_of_elements (ASN1_TYPE element, const char *name,
229 int *num);
231 asn1_retCode asn1_der_coding (ASN1_TYPE element, const char *name,
232 void *ider, int *len, char *ErrorDescription);
234 asn1_retCode asn1_der_decoding (ASN1_TYPE * element, const void *ider,
235 int len, char *errorDescription);
237 asn1_retCode asn1_der_decoding_element (ASN1_TYPE * structure,
238 const char *elementName,
239 const void *ider, int len,
240 char *errorDescription);
242 asn1_retCode asn1_der_decoding_startEnd (ASN1_TYPE element,
243 const void *ider, int len,
244 const char *name_element,
245 int *start, int *end);
247 asn1_retCode asn1_expand_any_defined_by (ASN1_TYPE definitions,
248 ASN1_TYPE * element);
250 asn1_retCode asn1_expand_octet_string (ASN1_TYPE definitions,
251 ASN1_TYPE * element,
252 const char *octetName,
253 const char *objectName);
255 asn1_retCode asn1_read_tag (node_asn * root, const char *name,
256 int *tagValue, int *classValue);
258 const char *asn1_find_structure_from_oid (ASN1_TYPE definitions,
259 const char *oidValue);
261 const char *asn1_check_version (const char *req_version);
263 const char *libtasn1_strerror (asn1_retCode error);
265 void libtasn1_perror (asn1_retCode error);
267 /* DER utility functions. */
269 int asn1_get_tag_der (const unsigned char *der, int der_len,
270 unsigned char *class, int *len, unsigned long *tag);
272 void asn1_octet_der (const unsigned char *str, int str_len,
273 unsigned char *der, int *der_len);
275 asn1_retCode asn1_get_octet_der (const unsigned char *der, int der_len,
276 int *ret_len, unsigned char *str,
277 int str_size, int *str_len);
279 void asn1_bit_der (const unsigned char *str, int bit_len,
280 unsigned char *der, int *der_len);
282 asn1_retCode asn1_get_bit_der (const unsigned char *der, int der_len,
283 int *ret_len, unsigned char *str,
284 int str_size, int *bit_len);
286 signed long asn1_get_length_der (const unsigned char *der, int der_len,
287 int *len);
289 void asn1_length_der (unsigned long len, unsigned char *ans, int *ans_len);
291 /* Other utility functions. */
293 ASN1_TYPE asn1_find_up (ASN1_TYPE node);
295 ASN1_TYPE asn1_find_node (ASN1_TYPE pointer, const char *name);
297 #ifdef __cplusplus
299 #endif
301 #endif /* LIBTASN1_H */