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