Rebuild ASN1 error codes to remove unused function and reason codes.
[openssl.git] / crypto / asn1 / asn1t.h
1 /* asn1t.h */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 #ifndef HEADER_ASN1T_H
59 #define HEADER_ASN1T_H
60
61 #include <stddef.h>
62 #include <openssl/e_os2.h>
63 #include <openssl/asn1.h>
64
65 #ifdef OPENSSL_BUILD_SHLIBCRYPTO
66 # undef OPENSSL_EXTERN
67 # define OPENSSL_EXTERN OPENSSL_EXPORT
68 #endif
69
70 /* ASN1 template defines, structures and functions */
71
72 #ifdef  __cplusplus
73 extern "C" {
74 #endif
75
76 /* Macros to aid ASN1 template writing */
77
78 #define ASN1_ITEM_TEMPLATE(tname) \
79         const static ASN1_TEMPLATE tname##_item_tt 
80
81 #define ASN1_ITEM_TEMPLATE_END(tname) \
82         ;\
83         OPENSSL_GLOBAL const ASN1_ITEM tname##_it = { \
84                 ASN1_ITYPE_PRIMITIVE,\
85                 -1,\
86                 &tname##_item_tt,\
87                 0,\
88                 NULL,\
89                 0,\
90                 #tname \
91         }
92
93
94 /* This is a ASN1 type which just embeds a template */
95  
96 /* This pair helps declare a SEQUENCE. We can do:
97  *
98  *      ASN1_SEQUENCE(stname) = {
99  *              ... SEQUENCE components ...
100  *      } ASN1_SEQUENCE_END(stname);
101  *
102  *      This will produce an ASN1_ITEM called stname_it
103  *      for a structure called stname.
104  *
105  *      If you want the same structure but a different
106  *      name then use:
107  *
108  *      ASN1_SEQUENCE(itname) = {
109  *              ... SEQUENCE components ...
110  *      } ASN1_SEQUENCE_END_name(stname, itname);
111  *
112  *      This will create an item called itname_it using
113  *      a structure called stname.
114  */
115
116 #define ASN1_SEQUENCE(tname) \
117         const static ASN1_TEMPLATE tname##_seq_tt[] 
118
119 #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
120
121 #define ASN1_SEQUENCE_END_name(stname, tname) \
122         ;\
123         OPENSSL_GLOBAL const ASN1_ITEM tname##_it = { \
124                 ASN1_ITYPE_SEQUENCE,\
125                 V_ASN1_SEQUENCE,\
126                 tname##_seq_tt,\
127                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
128                 NULL,\
129                 sizeof(stname),\
130                 #stname \
131         }
132
133 #define ASN1_SEQUENCE_cb(tname, cb) \
134         const static ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
135         ASN1_SEQUENCE(tname)
136
137 #define ASN1_BROKEN_SEQUENCE(tname) \
138         const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
139         ASN1_SEQUENCE(tname)
140
141 #define ASN1_SEQUENCE_ref(tname, cb, lck) \
142         const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
143         ASN1_SEQUENCE(tname)
144
145 #define ASN1_SEQUENCE_enc(tname, enc, cb) \
146         const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
147         ASN1_SEQUENCE(tname)
148
149 #define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
150
151 #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
152
153 #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
154
155 #define ASN1_SEQUENCE_END_ref(stname, tname) \
156         ;\
157         OPENSSL_GLOBAL const ASN1_ITEM tname##_it = { \
158                 ASN1_ITYPE_SEQUENCE,\
159                 V_ASN1_SEQUENCE,\
160                 tname##_seq_tt,\
161                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
162                 &tname##_aux,\
163                 sizeof(stname),\
164                 #stname \
165         }
166
167
168 /* This pair helps declare a CHOICE type. We can do:
169  *
170  *      ASN1_CHOICE(chname) = {
171  *              ... CHOICE options ...
172  *      ASN1_CHOICE_END(chname);
173  *
174  *      This will produce an ASN1_ITEM called chname_it
175  *      for a structure called chname. The structure
176  *      definition must look like this:
177  *      typedef struct {
178  *              int type;
179  *              union {
180  *                      ASN1_SOMETHING *opt1;
181  *                      ASN1_SOMEOTHER *opt2;
182  *              } value;
183  *      } chname;
184  *      
185  *      the name of the selector must be 'type'.
186  *      to use an alternative selector name use the
187  *      ASN1_CHOICE_END_selector() version.
188  */
189
190 #define ASN1_CHOICE(tname) \
191         const static ASN1_TEMPLATE tname##_ch_tt[] 
192
193 #define ASN1_CHOICE_cb(tname, cb) \
194         const static ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
195         ASN1_CHOICE(tname)
196
197 #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
198
199 #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
200
201 #define ASN1_CHOICE_END_selector(stname, tname, selname) \
202         ;\
203         OPENSSL_GLOBAL const ASN1_ITEM tname##_it = { \
204                 ASN1_ITYPE_CHOICE,\
205                 offsetof(stname,selname) ,\
206                 tname##_ch_tt,\
207                 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
208                 NULL,\
209                 sizeof(stname),\
210                 #stname \
211         }
212
213 #define ASN1_CHOICE_END_cb(stname, tname, selname) \
214         ;\
215         OPENSSL_GLOBAL const ASN1_ITEM tname##_it = { \
216                 ASN1_ITYPE_CHOICE,\
217                 offsetof(stname,selname) ,\
218                 tname##_ch_tt,\
219                 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
220                 &tname##_aux,\
221                 sizeof(stname),\
222                 #stname \
223         }
224
225 /* This helps with the template wrapper form of ASN1_ITEM */
226
227 #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
228         (flags), (tag), 0,\
229         #name, &(type##_it) }
230
231 /* These help with SEQUENCE or CHOICE components */
232
233 /* used to declare other types */
234
235 #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
236         (flags), (tag), offsetof(stname, field),\
237         #field, &(type##_it) }
238
239 /* used when the structure is combined with the parent */
240
241 #define ASN1_EX_COMBINE(flags, tag, type) { \
242         (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, &(type##_it) }
243
244 /* implicit and explicit helper macros */
245
246 #define ASN1_IMP_EX(stname, field, type, tag, ex) \
247                 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
248
249 #define ASN1_EXP_EX(stname, field, type, tag, ex) \
250                 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
251
252 /* Any defined by macros: the field used is in the table itself */
253
254 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, &(tblname##_adb) }
255 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, &(tblname##_adb) }
256
257 /* Plain simple type */
258 #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
259
260 /* OPTIONAL simple type */
261 #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
262
263 /* IMPLICIT tagged simple type */
264 #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
265
266 /* IMPLICIT tagged OPTIONAL simple type */
267 #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
268
269 /* Same as above but EXPLICIT */
270
271 #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
272 #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
273
274 /* SEQUENCE OF type */
275 #define ASN1_SEQUENCE_OF(stname, field, type) \
276                 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
277
278 /* OPTIONAL SEQUENCE OF */
279 #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
280                 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
281
282 /* Same as above but for SET OF */
283
284 #define ASN1_SET_OF(stname, field, type) \
285                 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
286
287 #define ASN1_SET_OF_OPT(stname, field, type) \
288                 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
289
290 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
291
292 #define ASN1_IMP_SET_OF(stname, field, type, tag) \
293                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
294
295 #define ASN1_EXP_SET_OF(stname, field, type, tag) \
296                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
297
298 #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
299                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
300
301 #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
302                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
303
304 #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
305                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
306
307 #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
308                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
309
310 #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
311                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
312
313 #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
314                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
315
316 /* Macros for the ASN1_ADB structure */
317
318 #define ASN1_ADB(name) \
319         const static ASN1_ADB_TABLE name##_adbtbl[] 
320
321 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
322         ;\
323         const static ASN1_ADB name##_adb = {\
324                 flags,\
325                 offsetof(name, field),\
326                 app_table,\
327                 name##_adbtbl,\
328                 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
329                 def,\
330                 none\
331         }
332
333 #define ADB_ENTRY(val, template) {val, template}
334
335 #define ASN1_ADB_TEMPLATE(name) \
336         const static ASN1_TEMPLATE name##_tt 
337
338 /* This is the ASN1 template structure that defines
339  * a wrapper round the actual type. It determines the
340  * actual position of the field in the value structure,
341  * various flags such as OPTIONAL and the field name.
342  */
343
344 struct ASN1_TEMPLATE_st {
345 unsigned long flags;            /* Various flags */
346 long tag;                       /* tag, not used if no tagging */
347 unsigned long offset;           /* Offset of this field in structure */
348 #ifndef NO_ASN1_FIELD_NAMES
349 char *field_name;               /* Field name */
350 #endif
351 const void *item;               /* Relevant ASN1_ITEM or ASN1_ADB */
352 };
353
354
355 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
356 typedef struct ASN1_ADB_st ASN1_ADB;
357
358 struct ASN1_ADB_st {
359         unsigned long flags;    /* Various flags */
360         unsigned long offset;   /* Offset of selector field */
361         STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
362         const ASN1_ADB_TABLE *tbl;      /* Table of possible types */
363         long tblcount;          /* Number of entries in tbl */
364         const ASN1_TEMPLATE *default_tt;  /* Type to use if no match */
365         const ASN1_TEMPLATE *null_tt;  /* Type to use if selector is NULL */
366 };
367
368 struct ASN1_ADB_TABLE_st {
369         long value;             /* NID for an object or value for an int */
370         const ASN1_TEMPLATE tt;         /* item for this value */
371 };
372
373 /* template flags */
374
375 /* Field is optional */
376 #define ASN1_TFLG_OPTIONAL      (0x1)
377
378 /* Field is a SET OF */
379 #define ASN1_TFLG_SET_OF        (0x1 << 1)
380
381 /* Field is a SEQUENCE OF */
382 #define ASN1_TFLG_SEQUENCE_OF   (0x2 << 1)
383
384 /* Special case: this refers to a SET OF that
385  * will be sorted into DER order when encoded *and*
386  * the corresponding STACK will be modified to match
387  * the new order.
388  */
389 #define ASN1_TFLG_SET_ORDER     (0x3 << 1)
390
391 /* Mask for SET OF or SEQUENCE OF */
392 #define ASN1_TFLG_SK_MASK       (0x3 << 1)
393
394 /* These flags mean the tag should be taken from the
395  * tag field. If EXPLICIT then the underlying type
396  * is used for the inner tag.
397  */
398
399 /* IMPLICIT tagging */
400 #define ASN1_TFLG_IMPTAG        (0x1 << 3)
401
402
403 /* EXPLICIT tagging, inner tag from underlying type */
404 #define ASN1_TFLG_EXPTAG        (0x2 << 3)
405
406 #define ASN1_TFLG_TAG_MASK      (0x3 << 3)
407
408 /* context specific IMPLICIT */
409 #define ASN1_TFLG_IMPLICIT      ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
410
411 /* context specific EXPLICIT */
412 #define ASN1_TFLG_EXPLICIT      ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
413
414 /* If tagging is in force these determine the
415  * type of tag to use. Otherwise the tag is
416  * determined by the underlying type. These 
417  * values reflect the actual octet format.
418  */
419
420 /* Universal tag */ 
421 #define ASN1_TFLG_UNIVERSAL     (0x0<<6)
422 /* Application tag */ 
423 #define ASN1_TFLG_APPLICATION   (0x1<<6)
424 /* Context specific tag */ 
425 #define ASN1_TFLG_CONTEXT       (0x2<<6)
426 /* Private tag */ 
427 #define ASN1_TFLG_PRIVATE       (0x3<<6)
428
429 #define ASN1_TFLG_TAG_CLASS     (0x3<<6)
430
431 /* These are for ANY DEFINED BY type. In this case
432  * the 'item' field points to an ASN1_ADB structure
433  * which contains a table of values to decode the
434  * relevant type
435  */
436
437 #define ASN1_TFLG_ADB_MASK      (0x3<<8)
438
439 #define ASN1_TFLG_ADB_OID       (0x1<<8)
440
441 #define ASN1_TFLG_ADB_INT       (0x1<<9)
442
443 /* This flag means a parent structure is passed
444  * instead of the field: this is useful is a
445  * SEQUENCE is being combined with a CHOICE for
446  * example. Since this means the structure and
447  * item name will differ we need to use the
448  * ASN1_CHOICE_END_name() macro for example.
449  */
450
451 #define ASN1_TFLG_COMBINE       (0x1<<10)
452
453 /* This is the actual ASN1 item itself */
454
455 struct ASN1_ITEM_st {
456 char itype;                     /* The item type, primitive, SEQUENCE, CHOICE or extern */
457 long utype;                     /* underlying type */
458 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */
459 long tcount;                    /* Number of templates if SEQUENCE or CHOICE */
460 const void *funcs;              /* functions that handle this type */
461 long size;                      /* Structure size (usually)*/
462 #ifndef NO_ASN1_FIELD_NAMES
463 const char *sname;              /* Structure name */
464 #endif
465 };
466
467 /* These are values for the itype field and
468  * determine how the type is interpreted.
469  *
470  * For PRIMITIVE types the underlying type
471  * determines the behaviour if items is NULL.
472  *
473  * Otherwise templates must contain a single 
474  * template and the type is treated in the
475  * same way as the type specified in the template.
476  *
477  * For SEQUENCE types the templates field points
478  * to the members, the size field is the
479  * structure size.
480  *
481  * For CHOICE types the templates field points
482  * to each possible member (typically a union)
483  * and the 'size' field is the offset of the
484  * selector.
485  *
486  * The 'funcs' field is used for application
487  * specific functions. 
488  *
489  * For COMPAT types the funcs field gives a
490  * set of functions that handle this type, this
491  * supports the old d2i, i2d convention.
492  *
493  * The EXTERN type uses a new style d2i/i2d.
494  * The new style should be used where possible
495  * because it avoids things like the d2i IMPLICIT
496  * hack.
497  *
498  * MSTRING is a multiple string type, it is used
499  * for a CHOICE of character strings where the
500  * actual strings all occupy an ASN1_STRING
501  * structure. In this case the 'utype' field
502  * has a special meaning, it is used as a mask
503  * of acceptable types using the B_ASN1 constants.
504  *
505  */
506
507 #define ASN1_ITYPE_PRIMITIVE    0x0
508
509 #define ASN1_ITYPE_SEQUENCE     0x1
510
511 #define ASN1_ITYPE_CHOICE       0x2
512
513 #define ASN1_ITYPE_COMPAT       0x3
514
515 #define ASN1_ITYPE_EXTERN       0x4
516
517 #define ASN1_ITYPE_MSTRING      0x5
518
519 /* Cache for ASN1 tag and length, so we
520  * don't keep re-reading it for things
521  * like CHOICE
522  */
523
524 struct ASN1_TLC_st{
525         char valid;     /* Values below are valid */
526         int ret;        /* return value */
527         long plen;      /* length */
528         int ptag;       /* class value */
529         int pclass;     /* class value */
530         int hdrlen;     /* header length */
531 };
532
533 /* Typedefs for ASN1 function pointers */
534
535 typedef ASN1_VALUE * ASN1_new_func(void);
536 typedef void ASN1_free_func(ASN1_VALUE *a);
537 typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, unsigned char ** in, long length);
538 typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
539
540 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it,
541                                         int tag, int aclass, char opt, ASN1_TLC *ctx);
542
543 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
544 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
545 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
546
547 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
548 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
549
550 typedef struct ASN1_COMPAT_FUNCS_st {
551         ASN1_new_func *asn1_new;
552         ASN1_free_func *asn1_free;
553         ASN1_d2i_func *asn1_d2i;
554         ASN1_i2d_func *asn1_i2d;
555 } ASN1_COMPAT_FUNCS;
556
557 typedef struct ASN1_EXTERN_FUNCS_st {
558         void *app_data;
559         ASN1_ex_new_func *asn1_ex_new;
560         ASN1_ex_free_func *asn1_ex_free;
561         ASN1_ex_free_func *asn1_ex_clear;
562         ASN1_ex_d2i *asn1_ex_d2i;
563         ASN1_ex_i2d *asn1_ex_i2d;
564 } ASN1_EXTERN_FUNCS;
565
566 typedef struct ASN1_PRIMITIVE_FUNCS_st {
567         void *app_data;
568         unsigned long flags;
569         ASN1_ex_new_func *prim_new;
570         ASN1_ex_free_func *prim_free;
571         ASN1_ex_free_func *prim_clear;
572         ASN1_primitive_c2i *prim_c2i;
573         ASN1_primitive_i2c *prim_i2c;
574 } ASN1_PRIMITIVE_FUNCS;
575
576 /* This is the ASN1_AUX structure: it handles various
577  * miscellaneous requirements. For example the use of
578  * reference counts and an informational callback.
579  *
580  * The "informational callback" is called at various
581  * points during the ASN1 encoding and decoding. It can
582  * be used to provide minor customisation of the structures
583  * used. This is most useful where the supplied routines
584  * *almost* do the right thing but need some extra help
585  * at a few points. If the callback returns zero then
586  * it is assumed a fatal error has occurred and the 
587  * main operation should be abandoned.
588  *
589  * If major changes in the default behaviour are required
590  * then an external type is more appropriate.
591  */
592
593 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it);
594
595 typedef struct ASN1_AUX_st {
596         void *app_data;
597         int flags;
598         int ref_offset;         /* Offset of reference value */
599         int ref_lock;           /* Lock type to use */
600         ASN1_aux_cb *asn1_cb;
601         int enc_offset;         /* Offset of ASN1_ENCODING structure */
602 } ASN1_AUX;
603
604 /* Flags in ASN1_AUX */
605
606 /* Use a reference count */
607 #define ASN1_AFLG_REFCOUNT      1
608 /* Save the encoding of structure (useful for signatures) */
609 #define ASN1_AFLG_ENCODING      2
610 /* The Sequence length is invalid */
611 #define ASN1_AFLG_BROKEN        4
612
613 /* operation values for asn1_cb */
614
615 #define ASN1_OP_NEW_PRE         0
616 #define ASN1_OP_NEW_POST        1
617 #define ASN1_OP_FREE_PRE        2
618 #define ASN1_OP_FREE_POST       3
619 #define ASN1_OP_D2I_PRE         4
620 #define ASN1_OP_D2I_POST        5
621 #define ASN1_OP_I2D_PRE         6
622 #define ASN1_OP_I2D_POST        7
623
624 /* Macro to implement a primitive type */
625 #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
626 #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) const ASN1_ITEM itname##_it = \
627                                 { ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname};
628
629 /* Macro to implement a multi string type */
630 #define IMPLEMENT_ASN1_MSTRING(itname, mask) const ASN1_ITEM itname##_it = \
631                                 { ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname};
632
633 /* Macro to implement an ASN1_ITEM in terms of old style funcs */
634
635 #define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
636
637 #define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
638         static const ASN1_COMPAT_FUNCS sname##_ff = { \
639                 (ASN1_new_func *)sname##_new, \
640                 (ASN1_free_func *)sname##_free, \
641                 (ASN1_d2i_func *)d2i_##sname, \
642                 (ASN1_i2d_func *)i2d_##sname, \
643         }; \
644         OPENSSL_GLOBAL ASN1_ITEM const sname##_it = { \
645                 ASN1_ITYPE_COMPAT, \
646                 tag, \
647                 NULL, \
648                 0, \
649                 &sname##_ff, \
650                 0, \
651                 #sname \
652         }
653
654 #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
655         OPENSSL_GLOBAL const ASN1_ITEM sname##_it = { \
656                 ASN1_ITYPE_EXTERN, \
657                 tag, \
658                 NULL, \
659                 0, \
660                 &fptrs, \
661                 0, \
662                 #sname \
663         };
664
665 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
666
667 #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
668
669 #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
670
671 #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
672                         IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
673
674 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
675         stname *fname##_new(void) \
676         { \
677                 return (stname *)ASN1_item_new(&itname##_it); \
678         } \
679         void fname##_free(stname *a) \
680         { \
681                 ASN1_item_free((ASN1_VALUE *)a, &itname##_it); \
682         }
683
684 #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
685         IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
686         IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
687
688 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
689         stname *d2i_##fname(stname **a, unsigned char **in, long len) \
690         { \
691                 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, &itname##_it);\
692         } \
693         int i2d_##fname(stname *a, unsigned char **out) \
694         { \
695                 return ASN1_item_i2d((ASN1_VALUE *)a, out, &itname##_it);\
696         } 
697
698 /* This includes evil casts to remove const: they will go away when full
699  * ASN1 constification is done.
700  */
701 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
702         stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
703         { \
704                 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, (unsigned char **)in, len, &itname##_it);\
705         } \
706         int i2d_##fname(const stname *a, unsigned char **out) \
707         { \
708                 return ASN1_item_i2d((ASN1_VALUE *)a, out, &itname##_it);\
709         } 
710
711 #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
712                 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
713
714 #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
715         IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
716         IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
717
718 /* external definitions for primitive types */
719
720 OPENSSL_EXTERN const ASN1_ITEM ASN1_BOOLEAN_it;
721 OPENSSL_EXTERN const ASN1_ITEM ASN1_TBOOLEAN_it;
722 OPENSSL_EXTERN const ASN1_ITEM ASN1_FBOOLEAN_it;
723 OPENSSL_EXTERN const ASN1_ITEM ASN1_ANY_it;
724 OPENSSL_EXTERN const ASN1_ITEM ASN1_SEQUENCE_it;
725 OPENSSL_EXTERN const ASN1_ITEM CBIGNUM_it;
726 OPENSSL_EXTERN const ASN1_ITEM BIGNUM_it;
727 OPENSSL_EXTERN const ASN1_ITEM LONG_it;
728 OPENSSL_EXTERN const ASN1_ITEM ZLONG_it;
729
730 DECLARE_STACK_OF(ASN1_VALUE)
731
732 /* Functions used internally by the ASN1 code */
733
734 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
735 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
736 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
737 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
738
739 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
740 int ASN1_template_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_TEMPLATE *tt);
741 int ASN1_item_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it,
742                                 int tag, int aclass, char opt, ASN1_TLC *ctx);
743
744 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
745 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt);
746 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
747
748 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
749 int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
750
751 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
752 int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
753
754 ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
755
756 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);
757
758 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
759
760 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
761 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
762 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);
763 int asn1_enc_save(ASN1_VALUE **pval, unsigned char *in, int inlen, const ASN1_ITEM *it);
764
765 #ifdef  __cplusplus
766 }
767 #endif
768 #endif