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