remove -Werror from automake
[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
188   struct asn1_data_node_st
189   {
190     const char *name;           /* Node name */
191     const void *value;          /* Node value */
192     unsigned int value_len;     /* Node value size */
193     unsigned int type;          /* Node value type (ASN1_ETYPE_*) */
194   };
195   typedef struct asn1_data_node_st asn1_data_node_st;
196
197   /***********************************/
198   /*  Fixed constants                */
199   /***********************************/
200
201
202   /* maximum number of characters */
203   /* of a description message     */
204   /* (null character included)    */
205 #define ASN1_MAX_ERROR_DESCRIPTION_SIZE 128
206
207   /***********************************/
208   /*  Functions definitions          */
209   /***********************************/
210
211   extern ASN1_API int
212     asn1_parser2tree (const char *file,
213                       asn1_node * definitions, char *error_desc);
214
215   extern ASN1_API int
216     asn1_parser2array (const char *inputFileName,
217                        const char *outputFileName,
218                        const char *vectorName, char *error_desc);
219
220   extern ASN1_API int
221     asn1_array2tree (const asn1_static_node * array,
222                      asn1_node * definitions, char *errorDescription);
223
224   extern ASN1_API void
225     asn1_print_structure (FILE * out, asn1_node structure,
226                           const char *name, int mode);
227
228   extern ASN1_API int
229     asn1_create_element (asn1_node definitions,
230                          const char *source_name, asn1_node * element);
231
232   extern ASN1_API int asn1_delete_structure (asn1_node * structure);
233
234   extern ASN1_API int asn1_delete_structure2 (asn1_node * structure, unsigned int flags);
235
236   extern ASN1_API int
237     asn1_delete_element (asn1_node structure, const char *element_name);
238
239   extern ASN1_API int
240     asn1_write_value (asn1_node node_root, const char *name,
241                       const void *ivalue, int len);
242
243   extern ASN1_API int
244     asn1_read_value (asn1_node root, const char *name,
245                      void *ivalue, int *len);
246
247   extern ASN1_API int
248     asn1_read_value_type (asn1_node root, const char *name,
249                           void *ivalue, int *len, unsigned int *etype);
250
251   extern ASN1_API int
252     asn1_read_node_value (asn1_node node, asn1_data_node_st * data);
253
254   extern ASN1_API int
255     asn1_number_of_elements (asn1_node element, const char *name, int *num);
256
257   extern ASN1_API int
258     asn1_der_coding (asn1_node element, const char *name,
259                      void *ider, int *len, char *ErrorDescription);
260
261   extern ASN1_API int
262     asn1_der_decoding (asn1_node * element, const void *ider,
263                        int len, char *errorDescription);
264
265   /* Do not use. Use asn1_der_decoding() instead. */
266   extern ASN1_API int
267     asn1_der_decoding_element (asn1_node * structure,
268                                const char *elementName,
269                                const void *ider, int len,
270                                char *errorDescription) _ASN1_GCC_ATTR_DEPRECATED;
271
272   extern ASN1_API int
273     asn1_der_decoding_startEnd (asn1_node element,
274                                 const void *ider, int len,
275                                 const char *name_element,
276                                 int *start, int *end);
277
278   extern ASN1_API int
279     asn1_expand_any_defined_by (asn1_node definitions, asn1_node * element);
280
281   extern ASN1_API int
282     asn1_expand_octet_string (asn1_node definitions,
283                               asn1_node * element,
284                               const char *octetName, const char *objectName);
285
286   extern ASN1_API int
287     asn1_read_tag (asn1_node root, const char *name,
288                    int *tagValue, int *classValue);
289
290   extern ASN1_API const char *asn1_find_structure_from_oid (asn1_node
291                                                             definitions,
292                                                             const char
293                                                             *oidValue);
294
295   extern ASN1_API const char *asn1_check_version (const char *req_version);
296
297   extern ASN1_API const char *asn1_strerror (int error);
298
299   extern ASN1_API void asn1_perror (int error);
300
301 #define ASN1_MAX_TAG_SIZE 4
302 #define ASN1_MAX_LENGTH_SIZE 9
303 #define ASN1_MAX_TL_SIZE (ASN1_MAX_TAG_SIZE+ASN1_MAX_LENGTH_SIZE)
304   extern ASN1_API long
305     asn1_get_length_der (const unsigned char *der, int der_len, int *len);
306
307   extern ASN1_API long
308     asn1_get_length_ber (const unsigned char *ber, int ber_len, int *len);
309
310   extern ASN1_API void
311     asn1_length_der (unsigned long int len, unsigned char *der, int *der_len);
312
313   /* Other utility functions. */
314
315   extern ASN1_API
316     int asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
317                                 unsigned int der_len,
318                                 const unsigned char **str,
319                                 unsigned int *str_len);
320
321   extern ASN1_API int
322     asn1_encode_simple_der (unsigned int etype, const unsigned char *str,
323                             unsigned int str_len, unsigned char *tl,
324                             unsigned int *tl_len);
325
326   extern ASN1_API asn1_node
327     asn1_find_node (asn1_node pointer, const char *name);
328
329   extern ASN1_API int
330     asn1_copy_node (asn1_node dst, const char *dst_name,
331                     asn1_node src, const char *src_name);
332   extern ASN1_API asn1_node
333     asn1_dup_node (asn1_node src, const char *src_name);
334
335   /* Internal and low-level DER utility functions. */
336
337   extern ASN1_API int
338     asn1_get_tag_der (const unsigned char *der, int der_len,
339                       unsigned char *cls, int *len, unsigned long *tag);
340
341   extern ASN1_API void
342     asn1_octet_der (const unsigned char *str, int str_len,
343                     unsigned char *der, int *der_len);
344
345   extern ASN1_API int
346     asn1_get_octet_der (const unsigned char *der, int der_len,
347                         int *ret_len, unsigned char *str,
348                         int str_size, int *str_len);
349
350   extern ASN1_API void asn1_bit_der (const unsigned char *str, int bit_len,
351                                      unsigned char *der, int *der_len);
352
353   extern ASN1_API int
354     asn1_get_bit_der (const unsigned char *der, int der_len,
355                       int *ret_len, unsigned char *str,
356                       int str_size, int *bit_len);
357
358 /* Compatibility types */
359
360   typedef int asn1_retCode;     /* type returned by libtasn1 functions */
361
362 #define node_asn_struct asn1_node_st
363 #define node_asn asn1_node_st
364 #define ASN1_TYPE asn1_node
365 #define ASN1_TYPE_EMPTY NULL
366
367 #define static_struct_asn asn1_static_node_st
368 #define ASN1_ARRAY_TYPE asn1_static_node
369 #define asn1_static_node_t asn1_static_node
370
371 #define node_data_struct asn1_data_node_st
372 #define ASN1_DATA_NODE asn1_data_node_st
373
374 #ifdef __cplusplus
375 }
376 #endif
377
378 #endif                          /* LIBTASN1_H */