Initial support for ASN1_ITEM_FUNCTION option to
[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
77 #ifndef ASN1_ITEM_FUNCTIONS
78
79 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
80 #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
81
82
83 /* Macros for start and end of ASN1_ITEM definition */
84
85 #define ASN1_ITEM_start(itname) \
86         OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
87
88 #define ASN1_ITEM_end(itname) \
89                 }
90
91 #else
92
93 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
94 #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
95
96
97 /* Macros for start and end of ASN1_ITEM definition */
98
99 #define ASN1_ITEM_start(itname) \
100         const ASN1_ITEM * itname##_it(void) \
101         { \
102                 static const ASN1_ITEM local_it = { \
103
104 #define ASN1_ITEM_end(itname) \
105                 }; \
106         return &local_it; \
107         }\
108         void dummy_function(void)
109
110 #endif
111
112
113 /* Macros to aid ASN1 template writing */
114
115 #define ASN1_ITEM_TEMPLATE(tname) \
116         const static ASN1_TEMPLATE tname##_item_tt 
117
118 #define ASN1_ITEM_TEMPLATE_END(tname) \
119         ;\
120         ASN1_ITEM_start(tname) \
121                 ASN1_ITYPE_PRIMITIVE,\
122                 -1,\
123                 &tname##_item_tt,\
124                 0,\
125                 NULL,\
126                 0,\
127                 #tname \
128         ASN1_ITEM_end(tname)
129
130
131 /* This is a ASN1 type which just embeds a template */
132  
133 /* This pair helps declare a SEQUENCE. We can do:
134  *
135  *      ASN1_SEQUENCE(stname) = {
136  *              ... SEQUENCE components ...
137  *      } ASN1_SEQUENCE_END(stname);
138  *
139  *      This will produce an ASN1_ITEM called stname_it
140  *      for a structure called stname.
141  *
142  *      If you want the same structure but a different
143  *      name then use:
144  *
145  *      ASN1_SEQUENCE(itname) = {
146  *              ... SEQUENCE components ...
147  *      } ASN1_SEQUENCE_END_name(stname, itname);
148  *
149  *      This will create an item called itname_it using
150  *      a structure called stname.
151  */
152
153 #define ASN1_SEQUENCE(tname) \
154         const static ASN1_TEMPLATE tname##_seq_tt[] 
155
156 #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
157
158 #define ASN1_SEQUENCE_END_name(stname, tname) \
159         ;\
160         ASN1_ITEM_start(tname) \
161                 ASN1_ITYPE_SEQUENCE,\
162                 V_ASN1_SEQUENCE,\
163                 tname##_seq_tt,\
164                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
165                 NULL,\
166                 sizeof(stname),\
167                 #stname \
168         ASN1_ITEM_end(tname)
169
170 #define ASN1_SEQUENCE_cb(tname, cb) \
171         const static ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
172         ASN1_SEQUENCE(tname)
173
174 #define ASN1_BROKEN_SEQUENCE(tname) \
175         const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
176         ASN1_SEQUENCE(tname)
177
178 #define ASN1_SEQUENCE_ref(tname, cb, lck) \
179         const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
180         ASN1_SEQUENCE(tname)
181
182 #define ASN1_SEQUENCE_enc(tname, enc, cb) \
183         const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
184         ASN1_SEQUENCE(tname)
185
186 #define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
187
188 #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
189
190 #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
191
192 #define ASN1_SEQUENCE_END_ref(stname, tname) \
193         ;\
194         ASN1_ITEM_start(tname) \
195                 ASN1_ITYPE_SEQUENCE,\
196                 V_ASN1_SEQUENCE,\
197                 tname##_seq_tt,\
198                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
199                 &tname##_aux,\
200                 sizeof(stname),\
201                 #stname \
202         ASN1_ITEM_end(tname)
203
204
205 /* This pair helps declare a CHOICE type. We can do:
206  *
207  *      ASN1_CHOICE(chname) = {
208  *              ... CHOICE options ...
209  *      ASN1_CHOICE_END(chname);
210  *
211  *      This will produce an ASN1_ITEM called chname_it
212  *      for a structure called chname. The structure
213  *      definition must look like this:
214  *      typedef struct {
215  *              int type;
216  *              union {
217  *                      ASN1_SOMETHING *opt1;
218  *                      ASN1_SOMEOTHER *opt2;
219  *              } value;
220  *      } chname;
221  *      
222  *      the name of the selector must be 'type'.
223  *      to use an alternative selector name use the
224  *      ASN1_CHOICE_END_selector() version.
225  */
226
227 #define ASN1_CHOICE(tname) \
228         const static ASN1_TEMPLATE tname##_ch_tt[] 
229
230 #define ASN1_CHOICE_cb(tname, cb) \
231         const static ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
232         ASN1_CHOICE(tname)
233
234 #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
235
236 #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
237
238 #define ASN1_CHOICE_END_selector(stname, tname, selname) \
239         ;\
240         ASN1_ITEM_start(tname) \
241                 ASN1_ITYPE_CHOICE,\
242                 offsetof(stname,selname) ,\
243                 tname##_ch_tt,\
244                 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
245                 NULL,\
246                 sizeof(stname),\
247                 #stname \
248         ASN1_ITEM_end(tname)
249
250 #define ASN1_CHOICE_END_cb(stname, tname, selname) \
251         ;\
252         ASN1_ITEM_start(tname) \
253                 ASN1_ITYPE_CHOICE,\
254                 offsetof(stname,selname) ,\
255                 tname##_ch_tt,\
256                 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
257                 &tname##_aux,\
258                 sizeof(stname),\
259                 #stname \
260         ASN1_ITEM_end(tname)
261
262 /* This helps with the template wrapper form of ASN1_ITEM */
263
264 #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
265         (flags), (tag), 0,\
266         #name, ASN1_ITEM_ref(type) }
267
268 /* These help with SEQUENCE or CHOICE components */
269
270 /* used to declare other types */
271
272 #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
273         (flags), (tag), offsetof(stname, field),\
274         #field, ASN1_ITEM_ref(type) }
275
276 /* used when the structure is combined with the parent */
277
278 #define ASN1_EX_COMBINE(flags, tag, type) { \
279         (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
280
281 /* implicit and explicit helper macros */
282
283 #define ASN1_IMP_EX(stname, field, type, tag, ex) \
284                 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
285
286 #define ASN1_EXP_EX(stname, field, type, tag, ex) \
287                 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
288
289 /* Any defined by macros: the field used is in the table itself */
290
291 #ifndef ASN1_ITEM_FUNCTIONS
292 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
293 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
294 #else
295 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
296 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
297 #endif
298 /* Plain simple type */
299 #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
300
301 /* OPTIONAL simple type */
302 #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
303
304 /* IMPLICIT tagged simple type */
305 #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
306
307 /* IMPLICIT tagged OPTIONAL simple type */
308 #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
309
310 /* Same as above but EXPLICIT */
311
312 #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
313 #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
314
315 /* SEQUENCE OF type */
316 #define ASN1_SEQUENCE_OF(stname, field, type) \
317                 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
318
319 /* OPTIONAL SEQUENCE OF */
320 #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
321                 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
322
323 /* Same as above but for SET OF */
324
325 #define ASN1_SET_OF(stname, field, type) \
326                 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
327
328 #define ASN1_SET_OF_OPT(stname, field, type) \
329                 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
330
331 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
332
333 #define ASN1_IMP_SET_OF(stname, field, type, tag) \
334                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
335
336 #define ASN1_EXP_SET_OF(stname, field, type, tag) \
337                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
338
339 #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
340                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
341
342 #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
343                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
344
345 #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
346                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
347
348 #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
349                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
350
351 #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
352                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
353
354 #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
355                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
356
357 /* Macros for the ASN1_ADB structure */
358
359 #define ASN1_ADB(name) \
360         const static ASN1_ADB_TABLE name##_adbtbl[] 
361
362 #ifndef ASN1_ITEM_FUNCTIONS
363
364 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
365         ;\
366         const static ASN1_ADB name##_adb = {\
367                 flags,\
368                 offsetof(name, field),\
369                 app_table,\
370                 name##_adbtbl,\
371                 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
372                 def,\
373                 none\
374         }
375
376 #else
377
378 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
379         ;\
380         const static ASN1_ITEM *name##_adb(void) \
381         { \
382         const static ASN1_ADB internal_adb = \
383                 {\
384                 flags,\
385                 offsetof(name, field),\
386                 app_table,\
387                 name##_adbtbl,\
388                 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
389                 def,\
390                 none\
391                 }; \
392                 return (const ASN1_ITEM *) &internal_adb; \
393         } \
394         void dummy_function(void)
395
396 #endif
397
398 #define ADB_ENTRY(val, template) {val, template}
399
400 #define ASN1_ADB_TEMPLATE(name) \
401         const static ASN1_TEMPLATE name##_tt 
402
403 /* This is the ASN1 template structure that defines
404  * a wrapper round the actual type. It determines the
405  * actual position of the field in the value structure,
406  * various flags such as OPTIONAL and the field name.
407  */
408
409 struct ASN1_TEMPLATE_st {
410 unsigned long flags;            /* Various flags */
411 long tag;                       /* tag, not used if no tagging */
412 unsigned long offset;           /* Offset of this field in structure */
413 #ifndef NO_ASN1_FIELD_NAMES
414 char *field_name;               /* Field name */
415 #endif
416 ASN1_ITEM_EXP *item;            /* Relevant ASN1_ITEM or ASN1_ADB */
417 };
418
419 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
420
421 #define ASN1_TEMPLATE_item(t) (t->item_ptr)
422 #define ASN1_TEMPLATE_adb(t) (t->item_ptr)
423
424 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
425 typedef struct ASN1_ADB_st ASN1_ADB;
426
427 struct ASN1_ADB_st {
428         unsigned long flags;    /* Various flags */
429         unsigned long offset;   /* Offset of selector field */
430         STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
431         const ASN1_ADB_TABLE *tbl;      /* Table of possible types */
432         long tblcount;          /* Number of entries in tbl */
433         const ASN1_TEMPLATE *default_tt;  /* Type to use if no match */
434         const ASN1_TEMPLATE *null_tt;  /* Type to use if selector is NULL */
435 };
436
437 struct ASN1_ADB_TABLE_st {
438         long value;             /* NID for an object or value for an int */
439         const ASN1_TEMPLATE tt;         /* item for this value */
440 };
441
442 /* template flags */
443
444 /* Field is optional */
445 #define ASN1_TFLG_OPTIONAL      (0x1)
446
447 /* Field is a SET OF */
448 #define ASN1_TFLG_SET_OF        (0x1 << 1)
449
450 /* Field is a SEQUENCE OF */
451 #define ASN1_TFLG_SEQUENCE_OF   (0x2 << 1)
452
453 /* Special case: this refers to a SET OF that
454  * will be sorted into DER order when encoded *and*
455  * the corresponding STACK will be modified to match
456  * the new order.
457  */
458 #define ASN1_TFLG_SET_ORDER     (0x3 << 1)
459
460 /* Mask for SET OF or SEQUENCE OF */
461 #define ASN1_TFLG_SK_MASK       (0x3 << 1)
462
463 /* These flags mean the tag should be taken from the
464  * tag field. If EXPLICIT then the underlying type
465  * is used for the inner tag.
466  */
467
468 /* IMPLICIT tagging */
469 #define ASN1_TFLG_IMPTAG        (0x1 << 3)
470
471
472 /* EXPLICIT tagging, inner tag from underlying type */
473 #define ASN1_TFLG_EXPTAG        (0x2 << 3)
474
475 #define ASN1_TFLG_TAG_MASK      (0x3 << 3)
476
477 /* context specific IMPLICIT */
478 #define ASN1_TFLG_IMPLICIT      ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
479
480 /* context specific EXPLICIT */
481 #define ASN1_TFLG_EXPLICIT      ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
482
483 /* If tagging is in force these determine the
484  * type of tag to use. Otherwise the tag is
485  * determined by the underlying type. These 
486  * values reflect the actual octet format.
487  */
488
489 /* Universal tag */ 
490 #define ASN1_TFLG_UNIVERSAL     (0x0<<6)
491 /* Application tag */ 
492 #define ASN1_TFLG_APPLICATION   (0x1<<6)
493 /* Context specific tag */ 
494 #define ASN1_TFLG_CONTEXT       (0x2<<6)
495 /* Private tag */ 
496 #define ASN1_TFLG_PRIVATE       (0x3<<6)
497
498 #define ASN1_TFLG_TAG_CLASS     (0x3<<6)
499
500 /* These are for ANY DEFINED BY type. In this case
501  * the 'item' field points to an ASN1_ADB structure
502  * which contains a table of values to decode the
503  * relevant type
504  */
505
506 #define ASN1_TFLG_ADB_MASK      (0x3<<8)
507
508 #define ASN1_TFLG_ADB_OID       (0x1<<8)
509
510 #define ASN1_TFLG_ADB_INT       (0x1<<9)
511
512 /* This flag means a parent structure is passed
513  * instead of the field: this is useful is a
514  * SEQUENCE is being combined with a CHOICE for
515  * example. Since this means the structure and
516  * item name will differ we need to use the
517  * ASN1_CHOICE_END_name() macro for example.
518  */
519
520 #define ASN1_TFLG_COMBINE       (0x1<<10)
521
522 /* This is the actual ASN1 item itself */
523
524 struct ASN1_ITEM_st {
525 char itype;                     /* The item type, primitive, SEQUENCE, CHOICE or extern */
526 long utype;                     /* underlying type */
527 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */
528 long tcount;                    /* Number of templates if SEQUENCE or CHOICE */
529 const void *funcs;              /* functions that handle this type */
530 long size;                      /* Structure size (usually)*/
531 #ifndef NO_ASN1_FIELD_NAMES
532 const char *sname;              /* Structure name */
533 #endif
534 };
535
536 /* These are values for the itype field and
537  * determine how the type is interpreted.
538  *
539  * For PRIMITIVE types the underlying type
540  * determines the behaviour if items is NULL.
541  *
542  * Otherwise templates must contain a single 
543  * template and the type is treated in the
544  * same way as the type specified in the template.
545  *
546  * For SEQUENCE types the templates field points
547  * to the members, the size field is the
548  * structure size.
549  *
550  * For CHOICE types the templates field points
551  * to each possible member (typically a union)
552  * and the 'size' field is the offset of the
553  * selector.
554  *
555  * The 'funcs' field is used for application
556  * specific functions. 
557  *
558  * For COMPAT types the funcs field gives a
559  * set of functions that handle this type, this
560  * supports the old d2i, i2d convention.
561  *
562  * The EXTERN type uses a new style d2i/i2d.
563  * The new style should be used where possible
564  * because it avoids things like the d2i IMPLICIT
565  * hack.
566  *
567  * MSTRING is a multiple string type, it is used
568  * for a CHOICE of character strings where the
569  * actual strings all occupy an ASN1_STRING
570  * structure. In this case the 'utype' field
571  * has a special meaning, it is used as a mask
572  * of acceptable types using the B_ASN1 constants.
573  *
574  */
575
576 #define ASN1_ITYPE_PRIMITIVE    0x0
577
578 #define ASN1_ITYPE_SEQUENCE     0x1
579
580 #define ASN1_ITYPE_CHOICE       0x2
581
582 #define ASN1_ITYPE_COMPAT       0x3
583
584 #define ASN1_ITYPE_EXTERN       0x4
585
586 #define ASN1_ITYPE_MSTRING      0x5
587
588 /* Cache for ASN1 tag and length, so we
589  * don't keep re-reading it for things
590  * like CHOICE
591  */
592
593 struct ASN1_TLC_st{
594         char valid;     /* Values below are valid */
595         int ret;        /* return value */
596         long plen;      /* length */
597         int ptag;       /* class value */
598         int pclass;     /* class value */
599         int hdrlen;     /* header length */
600 };
601
602 /* Typedefs for ASN1 function pointers */
603
604 typedef ASN1_VALUE * ASN1_new_func(void);
605 typedef void ASN1_free_func(ASN1_VALUE *a);
606 typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, unsigned char ** in, long length);
607 typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
608
609 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it,
610                                         int tag, int aclass, char opt, ASN1_TLC *ctx);
611
612 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
613 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
614 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
615
616 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
617 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
618
619 typedef struct ASN1_COMPAT_FUNCS_st {
620         ASN1_new_func *asn1_new;
621         ASN1_free_func *asn1_free;
622         ASN1_d2i_func *asn1_d2i;
623         ASN1_i2d_func *asn1_i2d;
624 } ASN1_COMPAT_FUNCS;
625
626 typedef struct ASN1_EXTERN_FUNCS_st {
627         void *app_data;
628         ASN1_ex_new_func *asn1_ex_new;
629         ASN1_ex_free_func *asn1_ex_free;
630         ASN1_ex_free_func *asn1_ex_clear;
631         ASN1_ex_d2i *asn1_ex_d2i;
632         ASN1_ex_i2d *asn1_ex_i2d;
633 } ASN1_EXTERN_FUNCS;
634
635 typedef struct ASN1_PRIMITIVE_FUNCS_st {
636         void *app_data;
637         unsigned long flags;
638         ASN1_ex_new_func *prim_new;
639         ASN1_ex_free_func *prim_free;
640         ASN1_ex_free_func *prim_clear;
641         ASN1_primitive_c2i *prim_c2i;
642         ASN1_primitive_i2c *prim_i2c;
643 } ASN1_PRIMITIVE_FUNCS;
644
645 /* This is the ASN1_AUX structure: it handles various
646  * miscellaneous requirements. For example the use of
647  * reference counts and an informational callback.
648  *
649  * The "informational callback" is called at various
650  * points during the ASN1 encoding and decoding. It can
651  * be used to provide minor customisation of the structures
652  * used. This is most useful where the supplied routines
653  * *almost* do the right thing but need some extra help
654  * at a few points. If the callback returns zero then
655  * it is assumed a fatal error has occurred and the 
656  * main operation should be abandoned.
657  *
658  * If major changes in the default behaviour are required
659  * then an external type is more appropriate.
660  */
661
662 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it);
663
664 typedef struct ASN1_AUX_st {
665         void *app_data;
666         int flags;
667         int ref_offset;         /* Offset of reference value */
668         int ref_lock;           /* Lock type to use */
669         ASN1_aux_cb *asn1_cb;
670         int enc_offset;         /* Offset of ASN1_ENCODING structure */
671 } ASN1_AUX;
672
673 /* Flags in ASN1_AUX */
674
675 /* Use a reference count */
676 #define ASN1_AFLG_REFCOUNT      1
677 /* Save the encoding of structure (useful for signatures) */
678 #define ASN1_AFLG_ENCODING      2
679 /* The Sequence length is invalid */
680 #define ASN1_AFLG_BROKEN        4
681
682 /* operation values for asn1_cb */
683
684 #define ASN1_OP_NEW_PRE         0
685 #define ASN1_OP_NEW_POST        1
686 #define ASN1_OP_FREE_PRE        2
687 #define ASN1_OP_FREE_POST       3
688 #define ASN1_OP_D2I_PRE         4
689 #define ASN1_OP_D2I_POST        5
690 #define ASN1_OP_I2D_PRE         6
691 #define ASN1_OP_I2D_POST        7
692
693 /* Macro to implement a primitive type */
694 #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
695 #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
696                                 ASN1_ITEM_start(itname) \
697                                         ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
698                                 ASN1_ITEM_end(itname);
699
700 /* Macro to implement a multi string type */
701 #define IMPLEMENT_ASN1_MSTRING(itname, mask) \
702                                 ASN1_ITEM_start(itname) \
703                                         ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
704                                 ASN1_ITEM_end(itname);
705
706 /* Macro to implement an ASN1_ITEM in terms of old style funcs */
707
708 #define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
709
710 #define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
711         static const ASN1_COMPAT_FUNCS sname##_ff = { \
712                 (ASN1_new_func *)sname##_new, \
713                 (ASN1_free_func *)sname##_free, \
714                 (ASN1_d2i_func *)d2i_##sname, \
715                 (ASN1_i2d_func *)i2d_##sname, \
716         }; \
717         ASN1_ITEM_start(sname) \
718                 ASN1_ITYPE_COMPAT, \
719                 tag, \
720                 NULL, \
721                 0, \
722                 &sname##_ff, \
723                 0, \
724                 #sname \
725         ASN1_ITEM_end(sname);
726
727 #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
728         ASN1_ITEM_start(sname) \
729                 ASN1_ITYPE_EXTERN, \
730                 tag, \
731                 NULL, \
732                 0, \
733                 &fptrs, \
734                 0, \
735                 #sname \
736         ASN1_ITEM_end(sname);
737
738 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
739
740 #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
741
742 #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
743
744 #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
745                         IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
746
747 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
748         stname *fname##_new(void) \
749         { \
750                 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
751         } \
752         void fname##_free(stname *a) \
753         { \
754                 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
755         }
756
757 #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
758         IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
759         IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
760
761 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
762         stname *d2i_##fname(stname **a, unsigned char **in, long len) \
763         { \
764                 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
765         } \
766         int i2d_##fname(stname *a, unsigned char **out) \
767         { \
768                 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
769         } 
770
771 /* This includes evil casts to remove const: they will go away when full
772  * ASN1 constification is done.
773  */
774 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
775         stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
776         { \
777                 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, (unsigned char **)in, len, ASN1_ITEM_rptr(itname));\
778         } \
779         int i2d_##fname(const stname *a, unsigned char **out) \
780         { \
781                 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
782         } 
783
784 #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
785                 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
786
787 #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
788         IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
789         IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
790
791 /* external definitions for primitive types */
792
793 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
794 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
795 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
796 DECLARE_ASN1_ITEM(ASN1_ANY)
797 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
798 DECLARE_ASN1_ITEM(CBIGNUM)
799 DECLARE_ASN1_ITEM(BIGNUM)
800 DECLARE_ASN1_ITEM(LONG)
801 DECLARE_ASN1_ITEM(ZLONG)
802
803 DECLARE_STACK_OF(ASN1_VALUE)
804
805 /* Functions used internally by the ASN1 code */
806
807 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
808 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
809 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
810 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
811
812 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
813 int ASN1_template_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_TEMPLATE *tt);
814 int ASN1_item_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it,
815                                 int tag, int aclass, char opt, ASN1_TLC *ctx);
816
817 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
818 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt);
819 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
820
821 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
822 int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
823
824 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
825 int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
826
827 ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
828
829 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);
830
831 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
832
833 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
834 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
835 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);
836 int asn1_enc_save(ASN1_VALUE **pval, unsigned char *in, int inlen, const ASN1_ITEM *it);
837
838 #ifdef  __cplusplus
839 }
840 #endif
841 #endif