Tizen 2.0 Release
[external/libgnutls26.git] / lib / minitasn1 / libtasn1.h
1 /*
2  * Copyright (C) 2002-2012 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 <stdio.h>              /* for FILE* */
39 #include <sys/types.h>
40 #include <time.h>
41
42 #ifdef __cplusplus
43 extern "C"
44 {
45 #endif
46
47 #define ASN1_VERSION "2.12"
48
49   typedef int asn1_retCode;     /* type returned by libtasn1 functions */
50
51   /*****************************************/
52   /* Errors returned by libtasn1 functions */
53   /*****************************************/
54 #define ASN1_SUCCESS                    0
55 #define ASN1_FILE_NOT_FOUND             1
56 #define ASN1_ELEMENT_NOT_FOUND          2
57 #define ASN1_IDENTIFIER_NOT_FOUND       3
58 #define ASN1_DER_ERROR                  4
59 #define ASN1_VALUE_NOT_FOUND            5
60 #define ASN1_GENERIC_ERROR              6
61 #define ASN1_VALUE_NOT_VALID            7
62 #define ASN1_TAG_ERROR                  8
63 #define ASN1_TAG_IMPLICIT               9
64 #define ASN1_ERROR_TYPE_ANY             10
65 #define ASN1_SYNTAX_ERROR               11
66 #define ASN1_MEM_ERROR                  12
67 #define ASN1_MEM_ALLOC_ERROR            13
68 #define ASN1_DER_OVERFLOW               14
69 #define ASN1_NAME_TOO_LONG              15
70 #define ASN1_ARRAY_ERROR                16
71 #define ASN1_ELEMENT_NOT_EMPTY          17
72
73   /*************************************/
74   /* Constants used in asn1_visit_tree */
75   /*************************************/
76 #define ASN1_PRINT_NAME                 1
77 #define ASN1_PRINT_NAME_TYPE            2
78 #define ASN1_PRINT_NAME_TYPE_VALUE      3
79 #define ASN1_PRINT_ALL                  4
80
81   /*****************************************/
82   /* Constants returned by asn1_read_tag   */
83   /*****************************************/
84 #define ASN1_CLASS_UNIVERSAL            0x00    /* old: 1 */
85 #define ASN1_CLASS_APPLICATION          0x40    /* old: 2 */
86 #define ASN1_CLASS_CONTEXT_SPECIFIC     0x80    /* old: 3 */
87 #define ASN1_CLASS_PRIVATE              0xC0    /* old: 4 */
88 #define ASN1_CLASS_STRUCTURED           0x20
89
90   /*****************************************/
91   /* Constants returned by asn1_read_tag   */
92   /*****************************************/
93 #define ASN1_TAG_BOOLEAN                0x01
94 #define ASN1_TAG_INTEGER                0x02
95 #define ASN1_TAG_SEQUENCE               0x10
96 #define ASN1_TAG_SET                    0x11
97 #define ASN1_TAG_OCTET_STRING           0x04
98 #define ASN1_TAG_BIT_STRING             0x03
99 #define ASN1_TAG_UTCTime                0x17
100 #define ASN1_TAG_GENERALIZEDTime        0x18
101 #define ASN1_TAG_OBJECT_ID              0x06
102 #define ASN1_TAG_ENUMERATED             0x0A
103 #define ASN1_TAG_NULL                   0x05
104 #define ASN1_TAG_GENERALSTRING          0x1B
105
106   /******************************************************/
107   /* Structure definition used for the node of the tree */
108   /* that represent an ASN.1 DEFINITION.                */
109   /******************************************************/
110
111 #if !defined ASN1_BUILDING
112   /* This structure is also in internal.h, but then contains more
113      fields.  You cannot make any modifications to these fields
114      without breaking ABI.  */
115   struct node_asn_struct
116   {
117     char *name;                 /* Node name */
118     unsigned int type;          /* Node type */
119     unsigned char *value;       /* Node value */
120     int value_len;
121     struct node_asn_struct *down;       /* Pointer to the son node */
122     struct node_asn_struct *right;      /* Pointer to the brother node */
123     struct node_asn_struct *left;       /* Pointer to the next list element */
124   };
125 #endif
126
127   typedef struct node_asn_struct node_asn;
128
129   typedef node_asn *ASN1_TYPE;
130
131 #define ASN1_TYPE_EMPTY  NULL
132
133   /*****************************************/
134   /* For the on-disk format of ASN.1 trees */
135   /*****************************************/
136   struct static_struct_asn
137   {
138     const char *name;           /* Node name */
139     unsigned int type;          /* Node type */
140     const void *value;          /* Node value */
141   };
142   typedef struct static_struct_asn ASN1_ARRAY_TYPE;
143
144   /***********************************/
145   /*  Fixed constants                */
146   /***********************************/
147
148   /* maximum number of characters of a name */
149   /* inside a file with ASN1 definitons     */
150 #define ASN1_MAX_NAME_SIZE 128
151
152   /* maximum number of characters */
153   /* of a description message     */
154   /* (null character included)    */
155 #define ASN1_MAX_ERROR_DESCRIPTION_SIZE 128
156
157   /***********************************/
158   /*  Functions definitions          */
159   /***********************************/
160
161   extern ASN1_API asn1_retCode
162     asn1_parser2tree (const char *file_name,
163                       ASN1_TYPE * definitions, char *errorDescription);
164
165   extern ASN1_API asn1_retCode
166     asn1_parser2array (const char *inputFileName,
167                        const char *outputFileName,
168                        const char *vectorName, char *errorDescription);
169
170   extern ASN1_API asn1_retCode
171     asn1_array2tree (const ASN1_ARRAY_TYPE * array,
172                      ASN1_TYPE * definitions, char *errorDescription);
173
174   extern ASN1_API void
175     asn1_print_structure (FILE * out, ASN1_TYPE structure,
176                           const char *name, int mode);
177
178   extern ASN1_API asn1_retCode
179     asn1_create_element (ASN1_TYPE definitions,
180                          const char *source_name, ASN1_TYPE * element);
181
182   extern ASN1_API asn1_retCode asn1_delete_structure (ASN1_TYPE * structure);
183
184   extern ASN1_API asn1_retCode
185     asn1_delete_element (ASN1_TYPE structure, const char *element_name);
186
187   extern ASN1_API asn1_retCode
188     asn1_write_value (ASN1_TYPE node_root, const char *name,
189                       const void *ivalue, int len);
190
191   extern ASN1_API asn1_retCode
192     asn1_read_value (ASN1_TYPE root, const char *name,
193                      void *ivalue, int *len);
194
195   extern ASN1_API asn1_retCode
196     asn1_number_of_elements (ASN1_TYPE element, const char *name, int *num);
197
198   extern ASN1_API asn1_retCode
199     asn1_der_coding (ASN1_TYPE element, const char *name,
200                      void *ider, int *len, char *ErrorDescription);
201
202   extern ASN1_API asn1_retCode
203     asn1_der_decoding (ASN1_TYPE * element, const void *ider,
204                        int len, char *errorDescription);
205
206   extern ASN1_API asn1_retCode
207     asn1_der_decoding_element (ASN1_TYPE * structure,
208                                const char *elementName,
209                                const void *ider, int len,
210                                char *errorDescription);
211
212   extern ASN1_API asn1_retCode
213     asn1_der_decoding_startEnd (ASN1_TYPE element,
214                                 const void *ider, int len,
215                                 const char *name_element,
216                                 int *start, int *end);
217
218   extern ASN1_API asn1_retCode
219     asn1_expand_any_defined_by (ASN1_TYPE definitions, ASN1_TYPE * element);
220
221   extern ASN1_API asn1_retCode
222     asn1_expand_octet_string (ASN1_TYPE definitions,
223                               ASN1_TYPE * element,
224                               const char *octetName, const char *objectName);
225
226   extern ASN1_API asn1_retCode
227     asn1_read_tag (ASN1_TYPE root, const char *name,
228                    int *tagValue, int *classValue);
229
230   extern ASN1_API const char *asn1_find_structure_from_oid (ASN1_TYPE
231                                                             definitions,
232                                                             const char
233                                                             *oidValue);
234
235   extern ASN1_API const char *asn1_check_version (const char *req_version);
236
237   extern ASN1_API const char *asn1_strerror (asn1_retCode error);
238
239   extern ASN1_API void asn1_perror (asn1_retCode error);
240
241   /* DER utility functions. */
242
243   extern ASN1_API int
244     asn1_get_tag_der (const unsigned char *der, int der_len,
245                       unsigned char *cls, int *len, unsigned long *tag);
246
247   extern ASN1_API void
248     asn1_octet_der (const unsigned char *str, int str_len,
249                     unsigned char *der, int *der_len);
250
251   extern ASN1_API asn1_retCode
252     asn1_get_octet_der (const unsigned char *der, int der_len,
253                         int *ret_len, unsigned char *str,
254                         int str_size, int *str_len);
255
256   extern ASN1_API void asn1_bit_der (const unsigned char *str, int bit_len,
257                                      unsigned char *der, int *der_len);
258
259   extern ASN1_API asn1_retCode
260     asn1_get_bit_der (const unsigned char *der, int der_len,
261                       int *ret_len, unsigned char *str,
262                       int str_size, int *bit_len);
263
264   extern ASN1_API signed long
265     asn1_get_length_der (const unsigned char *der, int der_len, int *len);
266
267   extern ASN1_API signed long
268     asn1_get_length_ber (const unsigned char *ber, int ber_len, int *len);
269
270   extern ASN1_API void
271     asn1_length_der (unsigned long int len, unsigned char *ans, int *ans_len);
272
273   /* Other utility functions. */
274
275   extern ASN1_API ASN1_TYPE
276     asn1_find_node (ASN1_TYPE pointer, const char *name);
277
278   extern ASN1_API asn1_retCode
279     asn1_copy_node (ASN1_TYPE dst, const char *dst_name,
280                     ASN1_TYPE src, const char *src_name);
281
282   /* Deprecated stuff. */
283
284 #ifndef ASN1_DISABLE_DEPRECATED
285
286 #define LIBTASN1_VERSION ASN1_VERSION
287
288 #ifndef MAX_NAME_SIZE
289 # define MAX_NAME_SIZE ASN1_MAX_NAME_SIZE
290 #endif
291
292 #ifndef MAX_ERROR_DESCRIPTION_SIZE
293 # define MAX_ERROR_DESCRIPTION_SIZE ASN1_MAX_ERROR_DESCRIPTION_SIZE
294 #endif
295
296 #ifndef __attribute__
297   /* This feature is available in gcc versions 2.5 and later.  */
298 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
299 #  define __attribute__(Spec)   /* empty */
300 # endif
301 #endif
302
303   /* Use asn1_strerror instead. */
304   extern ASN1_API const char *libtasn1_strerror (asn1_retCode error)
305     __attribute__ ((deprecated));
306
307   /* Use asn1_perror instead. */
308   extern ASN1_API void
309     libtasn1_perror (asn1_retCode error) __attribute__ ((deprecated));
310
311 #endif
312
313 #ifdef __cplusplus
314 }
315 #endif
316
317 #endif                          /* LIBTASN1_H */