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