Enable -Wmissing-variable-declarations and
[openssl.git] / include / openssl / asn1t.h
1 /* asn1t.h */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4  * 2000.
5  */
6 /* ====================================================================
7  * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59 #ifndef HEADER_ASN1T_H
60 # define HEADER_ASN1T_H
61
62 # include <stddef.h>
63 # include <openssl/e_os2.h>
64 # include <openssl/asn1.h>
65
66 # ifdef OPENSSL_BUILD_SHLIBCRYPTO
67 #  undef OPENSSL_EXTERN
68 #  define OPENSSL_EXTERN OPENSSL_EXPORT
69 # endif
70
71 /* ASN1 template defines, structures and functions */
72
73 #ifdef  __cplusplus
74 extern "C" {
75 #endif
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 /* Macros for start and end of ASN1_ITEM definition */
83
84 #  define ASN1_ITEM_start(itname) \
85         OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
86
87 #  define static_ASN1_ITEM_start(itname) \
88         static const ASN1_ITEM itname##_it = {
89
90 #  define ASN1_ITEM_end(itname)                 \
91                 };
92
93 # else
94
95 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
96 #  define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
97
98 /* Macros for start and end of ASN1_ITEM definition */
99
100 #  define ASN1_ITEM_start(itname) \
101         const ASN1_ITEM * itname##_it(void) \
102         { \
103                 static const ASN1_ITEM local_it = {
104
105 #  define ASN1_ITEM_end(itname) \
106                 }; \
107         return &local_it; \
108         }
109
110 # endif
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 # define static_ASN1_ITEM_TEMPLATE_END(tname) \
129         ;\
130         static_ASN1_ITEM_start(tname) \
131                 ASN1_ITYPE_PRIMITIVE,\
132                 -1,\
133                 &tname##_item_tt,\
134                 0,\
135                 NULL,\
136                 0,\
137                 #tname \
138         ASN1_ITEM_end(tname)
139
140 /* This is a ASN1 type which just embeds a template */
141
142 /*-
143  * This pair helps declare a SEQUENCE. We can do:
144  *
145  *      ASN1_SEQUENCE(stname) = {
146  *              ... SEQUENCE components ...
147  *      } ASN1_SEQUENCE_END(stname)
148  *
149  *      This will produce an ASN1_ITEM called stname_it
150  *      for a structure called stname.
151  *
152  *      If you want the same structure but a different
153  *      name then use:
154  *
155  *      ASN1_SEQUENCE(itname) = {
156  *              ... SEQUENCE components ...
157  *      } ASN1_SEQUENCE_END_name(stname, itname)
158  *
159  *      This will create an item called itname_it using
160  *      a structure called stname.
161  */
162
163 # define ASN1_SEQUENCE(tname) \
164         static const ASN1_TEMPLATE tname##_seq_tt[]
165
166 # define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
167
168 # define static_ASN1_SEQUENCE_END(stname) static_ASN1_SEQUENCE_END_name(stname, stname)
169
170 # define ASN1_SEQUENCE_END_name(stname, tname) \
171         ;\
172         ASN1_ITEM_start(tname) \
173                 ASN1_ITYPE_SEQUENCE,\
174                 V_ASN1_SEQUENCE,\
175                 tname##_seq_tt,\
176                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
177                 NULL,\
178                 sizeof(stname),\
179                 #stname \
180         ASN1_ITEM_end(tname)
181
182 # define static_ASN1_SEQUENCE_END_name(stname, tname) \
183         ;\
184         static_ASN1_ITEM_start(tname) \
185                 ASN1_ITYPE_SEQUENCE,\
186                 V_ASN1_SEQUENCE,\
187                 tname##_seq_tt,\
188                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
189                 NULL,\
190                 sizeof(stname),\
191                 #stname \
192         ASN1_ITEM_end(tname)
193
194 # define ASN1_NDEF_SEQUENCE(tname) \
195         ASN1_SEQUENCE(tname)
196
197 # define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
198         ASN1_SEQUENCE_cb(tname, cb)
199
200 # define ASN1_SEQUENCE_cb(tname, cb) \
201         static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
202         ASN1_SEQUENCE(tname)
203
204 # define ASN1_BROKEN_SEQUENCE(tname) \
205         static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
206         ASN1_SEQUENCE(tname)
207
208 # define ASN1_SEQUENCE_ref(tname, cb, lck) \
209         static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
210         ASN1_SEQUENCE(tname)
211
212 # define ASN1_SEQUENCE_enc(tname, enc, cb) \
213         static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
214         ASN1_SEQUENCE(tname)
215
216 # define ASN1_NDEF_SEQUENCE_END(tname) \
217         ;\
218         ASN1_ITEM_start(tname) \
219                 ASN1_ITYPE_NDEF_SEQUENCE,\
220                 V_ASN1_SEQUENCE,\
221                 tname##_seq_tt,\
222                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
223                 NULL,\
224                 sizeof(tname),\
225                 #tname \
226         ASN1_ITEM_end(tname)
227 # define static_ASN1_NDEF_SEQUENCE_END(tname) \
228         ;\
229         static_ASN1_ITEM_start(tname)                   \
230                 ASN1_ITYPE_NDEF_SEQUENCE,\
231                 V_ASN1_SEQUENCE,\
232                 tname##_seq_tt,\
233                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
234                 NULL,\
235                 sizeof(tname),\
236                 #tname \
237         ASN1_ITEM_end(tname)
238
239 # define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
240 # define static_ASN1_BROKEN_SEQUENCE_END(stname) \
241         static_ASN1_SEQUENCE_END_ref(stname, stname)
242
243 # define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
244
245 # define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
246 # define static_ASN1_SEQUENCE_END_cb(stname, tname) static_ASN1_SEQUENCE_END_ref(stname, tname)
247
248 # define ASN1_SEQUENCE_END_ref(stname, tname) \
249         ;\
250         ASN1_ITEM_start(tname) \
251                 ASN1_ITYPE_SEQUENCE,\
252                 V_ASN1_SEQUENCE,\
253                 tname##_seq_tt,\
254                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
255                 &tname##_aux,\
256                 sizeof(stname),\
257                 #stname \
258         ASN1_ITEM_end(tname)
259 # define static_ASN1_SEQUENCE_END_ref(stname, tname) \
260         ;\
261         static_ASN1_ITEM_start(tname) \
262                 ASN1_ITYPE_SEQUENCE,\
263                 V_ASN1_SEQUENCE,\
264                 tname##_seq_tt,\
265                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
266                 &tname##_aux,\
267                 sizeof(stname),\
268                 #stname \
269         ASN1_ITEM_end(tname)
270
271 # define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
272         ;\
273         ASN1_ITEM_start(tname) \
274                 ASN1_ITYPE_NDEF_SEQUENCE,\
275                 V_ASN1_SEQUENCE,\
276                 tname##_seq_tt,\
277                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
278                 &tname##_aux,\
279                 sizeof(stname),\
280                 #stname \
281         ASN1_ITEM_end(tname)
282
283 /*-
284  * This pair helps declare a CHOICE type. We can do:
285  *
286  *      ASN1_CHOICE(chname) = {
287  *              ... CHOICE options ...
288  *      ASN1_CHOICE_END(chname)
289  *
290  *      This will produce an ASN1_ITEM called chname_it
291  *      for a structure called chname. The structure
292  *      definition must look like this:
293  *      typedef struct {
294  *              int type;
295  *              union {
296  *                      ASN1_SOMETHING *opt1;
297  *                      ASN1_SOMEOTHER *opt2;
298  *              } value;
299  *      } chname;
300  *
301  *      the name of the selector must be 'type'.
302  *      to use an alternative selector name use the
303  *      ASN1_CHOICE_END_selector() version.
304  */
305
306 # define ASN1_CHOICE(tname) \
307         static const ASN1_TEMPLATE tname##_ch_tt[]
308
309 # define ASN1_CHOICE_cb(tname, cb) \
310         static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
311         ASN1_CHOICE(tname)
312
313 # define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
314
315 # define static_ASN1_CHOICE_END(stname) static_ASN1_CHOICE_END_name(stname, stname)
316
317 # define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
318
319 # define static_ASN1_CHOICE_END_name(stname, tname) static_ASN1_CHOICE_END_selector(stname, tname, type)
320
321 # define ASN1_CHOICE_END_selector(stname, tname, selname) \
322         ;\
323         ASN1_ITEM_start(tname) \
324                 ASN1_ITYPE_CHOICE,\
325                 offsetof(stname,selname) ,\
326                 tname##_ch_tt,\
327                 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
328                 NULL,\
329                 sizeof(stname),\
330                 #stname \
331         ASN1_ITEM_end(tname)
332
333 # define static_ASN1_CHOICE_END_selector(stname, tname, selname) \
334         ;\
335         static_ASN1_ITEM_start(tname) \
336                 ASN1_ITYPE_CHOICE,\
337                 offsetof(stname,selname) ,\
338                 tname##_ch_tt,\
339                 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
340                 NULL,\
341                 sizeof(stname),\
342                 #stname \
343         ASN1_ITEM_end(tname)
344
345 # define ASN1_CHOICE_END_cb(stname, tname, selname) \
346         ;\
347         ASN1_ITEM_start(tname) \
348                 ASN1_ITYPE_CHOICE,\
349                 offsetof(stname,selname) ,\
350                 tname##_ch_tt,\
351                 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
352                 &tname##_aux,\
353                 sizeof(stname),\
354                 #stname \
355         ASN1_ITEM_end(tname)
356
357 /* This helps with the template wrapper form of ASN1_ITEM */
358
359 # define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
360         (flags), (tag), 0,\
361         #name, ASN1_ITEM_ref(type) }
362
363 /* These help with SEQUENCE or CHOICE components */
364
365 /* used to declare other types */
366
367 # define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
368         (flags), (tag), offsetof(stname, field),\
369         #field, ASN1_ITEM_ref(type) }
370
371 /* implicit and explicit helper macros */
372
373 # define ASN1_IMP_EX(stname, field, type, tag, ex) \
374                 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
375
376 # define ASN1_EXP_EX(stname, field, type, tag, ex) \
377                 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
378
379 /* Any defined by macros: the field used is in the table itself */
380
381 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
382 #  define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
383 #  define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
384 # else
385 #  define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
386 #  define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
387 # endif
388 /* Plain simple type */
389 # define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
390
391 /* OPTIONAL simple type */
392 # define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
393
394 /* IMPLICIT tagged simple type */
395 # define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
396
397 /* IMPLICIT tagged OPTIONAL simple type */
398 # define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
399
400 /* Same as above but EXPLICIT */
401
402 # define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
403 # define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
404
405 /* SEQUENCE OF type */
406 # define ASN1_SEQUENCE_OF(stname, field, type) \
407                 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
408
409 /* OPTIONAL SEQUENCE OF */
410 # define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
411                 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
412
413 /* Same as above but for SET OF */
414
415 # define ASN1_SET_OF(stname, field, type) \
416                 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
417
418 # define ASN1_SET_OF_OPT(stname, field, type) \
419                 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
420
421 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
422
423 # define ASN1_IMP_SET_OF(stname, field, type, tag) \
424                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
425
426 # define ASN1_EXP_SET_OF(stname, field, type, tag) \
427                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
428
429 # define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
430                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
431
432 # define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
433                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
434
435 # define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
436                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
437
438 # define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
439                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
440
441 # define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
442                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
443
444 # define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
445                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
446
447 /* EXPLICIT using indefinite length constructed form */
448 # define ASN1_NDEF_EXP(stname, field, type, tag) \
449                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
450
451 /* EXPLICIT OPTIONAL using indefinite length constructed form */
452 # define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
453                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
454
455 /* Macros for the ASN1_ADB structure */
456
457 # define ASN1_ADB(name) \
458         static const ASN1_ADB_TABLE name##_adbtbl[]
459
460 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
461
462 #  define ASN1_ADB_END(name, flags, field, app_table, def, none) \
463         ;\
464         static const ASN1_ADB name##_adb = {\
465                 flags,\
466                 offsetof(name, field),\
467                 app_table,\
468                 name##_adbtbl,\
469                 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
470                 def,\
471                 none\
472         }
473
474 # else
475
476 #  define ASN1_ADB_END(name, flags, field, app_table, def, none) \
477         ;\
478         static const ASN1_ITEM *name##_adb(void) \
479         { \
480         static const ASN1_ADB internal_adb = \
481                 {\
482                 flags,\
483                 offsetof(name, field),\
484                 app_table,\
485                 name##_adbtbl,\
486                 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
487                 def,\
488                 none\
489                 }; \
490                 return (const ASN1_ITEM *) &internal_adb; \
491         } \
492         void dummy_function(void)
493
494 # endif
495
496 # define ADB_ENTRY(val, template) {val, template}
497
498 # define ASN1_ADB_TEMPLATE(name) \
499         static const ASN1_TEMPLATE name##_tt
500
501 /*
502  * This is the ASN1 template structure that defines a wrapper round the
503  * actual type. It determines the actual position of the field in the value
504  * structure, various flags such as OPTIONAL and the field name.
505  */
506
507 struct ASN1_TEMPLATE_st {
508     unsigned long flags;        /* Various flags */
509     long tag;                   /* tag, not used if no tagging */
510     unsigned long offset;       /* Offset of this field in structure */
511 # ifndef NO_ASN1_FIELD_NAMES
512     const char *field_name;     /* Field name */
513 # endif
514     ASN1_ITEM_EXP *item;        /* Relevant ASN1_ITEM or ASN1_ADB */
515 };
516
517 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
518
519 # define ASN1_TEMPLATE_item(t) (t->item_ptr)
520 # define ASN1_TEMPLATE_adb(t) (t->item_ptr)
521
522 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
523 typedef struct ASN1_ADB_st ASN1_ADB;
524
525 struct ASN1_ADB_st {
526     unsigned long flags;        /* Various flags */
527     unsigned long offset;       /* Offset of selector field */
528     STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
529     const ASN1_ADB_TABLE *tbl;  /* Table of possible types */
530     long tblcount;              /* Number of entries in tbl */
531     const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
532     const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
533 };
534
535 struct ASN1_ADB_TABLE_st {
536     long value;                 /* NID for an object or value for an int */
537     const ASN1_TEMPLATE tt;     /* item for this value */
538 };
539
540 /* template flags */
541
542 /* Field is optional */
543 # define ASN1_TFLG_OPTIONAL      (0x1)
544
545 /* Field is a SET OF */
546 # define ASN1_TFLG_SET_OF        (0x1 << 1)
547
548 /* Field is a SEQUENCE OF */
549 # define ASN1_TFLG_SEQUENCE_OF   (0x2 << 1)
550
551 /*
552  * Special case: this refers to a SET OF that will be sorted into DER order
553  * when encoded *and* the corresponding STACK will be modified to match the
554  * new order.
555  */
556 # define ASN1_TFLG_SET_ORDER     (0x3 << 1)
557
558 /* Mask for SET OF or SEQUENCE OF */
559 # define ASN1_TFLG_SK_MASK       (0x3 << 1)
560
561 /*
562  * These flags mean the tag should be taken from the tag field. If EXPLICIT
563  * then the underlying type is used for the inner tag.
564  */
565
566 /* IMPLICIT tagging */
567 # define ASN1_TFLG_IMPTAG        (0x1 << 3)
568
569 /* EXPLICIT tagging, inner tag from underlying type */
570 # define ASN1_TFLG_EXPTAG        (0x2 << 3)
571
572 # define ASN1_TFLG_TAG_MASK      (0x3 << 3)
573
574 /* context specific IMPLICIT */
575 # define ASN1_TFLG_IMPLICIT      ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
576
577 /* context specific EXPLICIT */
578 # define ASN1_TFLG_EXPLICIT      ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
579
580 /*
581  * If tagging is in force these determine the type of tag to use. Otherwise
582  * the tag is determined by the underlying type. These values reflect the
583  * actual octet format.
584  */
585
586 /* Universal tag */
587 # define ASN1_TFLG_UNIVERSAL     (0x0<<6)
588 /* Application tag */
589 # define ASN1_TFLG_APPLICATION   (0x1<<6)
590 /* Context specific tag */
591 # define ASN1_TFLG_CONTEXT       (0x2<<6)
592 /* Private tag */
593 # define ASN1_TFLG_PRIVATE       (0x3<<6)
594
595 # define ASN1_TFLG_TAG_CLASS     (0x3<<6)
596
597 /*
598  * These are for ANY DEFINED BY type. In this case the 'item' field points to
599  * an ASN1_ADB structure which contains a table of values to decode the
600  * relevant type
601  */
602
603 # define ASN1_TFLG_ADB_MASK      (0x3<<8)
604
605 # define ASN1_TFLG_ADB_OID       (0x1<<8)
606
607 # define ASN1_TFLG_ADB_INT       (0x1<<9)
608
609 /*
610  * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes
611  * indefinite length constructed encoding to be used if required.
612  */
613
614 # define ASN1_TFLG_NDEF          (0x1<<11)
615
616 /* This is the actual ASN1 item itself */
617
618 struct ASN1_ITEM_st {
619     char itype;                 /* The item type, primitive, SEQUENCE, CHOICE
620                                  * or extern */
621     long utype;                 /* underlying type */
622     const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains
623                                      * the contents */
624     long tcount;                /* Number of templates if SEQUENCE or CHOICE */
625     const void *funcs;          /* functions that handle this type */
626     long size;                  /* Structure size (usually) */
627 # ifndef NO_ASN1_FIELD_NAMES
628     const char *sname;          /* Structure name */
629 # endif
630 };
631
632 /*-
633  * These are values for the itype field and
634  * determine how the type is interpreted.
635  *
636  * For PRIMITIVE types the underlying type
637  * determines the behaviour if items is NULL.
638  *
639  * Otherwise templates must contain a single
640  * template and the type is treated in the
641  * same way as the type specified in the template.
642  *
643  * For SEQUENCE types the templates field points
644  * to the members, the size field is the
645  * structure size.
646  *
647  * For CHOICE types the templates field points
648  * to each possible member (typically a union)
649  * and the 'size' field is the offset of the
650  * selector.
651  *
652  * The 'funcs' field is used for application
653  * specific functions.
654  *
655  * The EXTERN type uses a new style d2i/i2d.
656  * The new style should be used where possible
657  * because it avoids things like the d2i IMPLICIT
658  * hack.
659  *
660  * MSTRING is a multiple string type, it is used
661  * for a CHOICE of character strings where the
662  * actual strings all occupy an ASN1_STRING
663  * structure. In this case the 'utype' field
664  * has a special meaning, it is used as a mask
665  * of acceptable types using the B_ASN1 constants.
666  *
667  * NDEF_SEQUENCE is the same as SEQUENCE except
668  * that it will use indefinite length constructed
669  * encoding if requested.
670  *
671  */
672
673 # define ASN1_ITYPE_PRIMITIVE            0x0
674
675 # define ASN1_ITYPE_SEQUENCE             0x1
676
677 # define ASN1_ITYPE_CHOICE               0x2
678
679 # define ASN1_ITYPE_EXTERN               0x4
680
681 # define ASN1_ITYPE_MSTRING              0x5
682
683 # define ASN1_ITYPE_NDEF_SEQUENCE        0x6
684
685 /*
686  * Cache for ASN1 tag and length, so we don't keep re-reading it for things
687  * like CHOICE
688  */
689
690 struct ASN1_TLC_st {
691     char valid;                 /* Values below are valid */
692     int ret;                    /* return value */
693     long plen;                  /* length */
694     int ptag;                   /* class value */
695     int pclass;                 /* class value */
696     int hdrlen;                 /* header length */
697 };
698
699 /* Typedefs for ASN1 function pointers */
700
701 typedef ASN1_VALUE *ASN1_new_func(void);
702 typedef void ASN1_free_func(ASN1_VALUE *a);
703 typedef ASN1_VALUE *ASN1_d2i_func(ASN1_VALUE **a, const unsigned char **in,
704                                   long length);
705 typedef int ASN1_i2d_func(ASN1_VALUE *a, unsigned char **in);
706
707 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
708                         const ASN1_ITEM *it, int tag, int aclass, char opt,
709                         ASN1_TLC *ctx);
710
711 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
712                         const ASN1_ITEM *it, int tag, int aclass);
713 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
714 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
715
716 typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
717                                int indent, const char *fname,
718                                const ASN1_PCTX *pctx);
719
720 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont,
721                                int *putype, const ASN1_ITEM *it);
722 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
723                                int len, int utype, char *free_cont,
724                                const ASN1_ITEM *it);
725 typedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval,
726                                  const ASN1_ITEM *it, int indent,
727                                  const ASN1_PCTX *pctx);
728
729 typedef struct ASN1_EXTERN_FUNCS_st {
730     void *app_data;
731     ASN1_ex_new_func *asn1_ex_new;
732     ASN1_ex_free_func *asn1_ex_free;
733     ASN1_ex_free_func *asn1_ex_clear;
734     ASN1_ex_d2i *asn1_ex_d2i;
735     ASN1_ex_i2d *asn1_ex_i2d;
736     ASN1_ex_print_func *asn1_ex_print;
737 } ASN1_EXTERN_FUNCS;
738
739 typedef struct ASN1_PRIMITIVE_FUNCS_st {
740     void *app_data;
741     unsigned long flags;
742     ASN1_ex_new_func *prim_new;
743     ASN1_ex_free_func *prim_free;
744     ASN1_ex_free_func *prim_clear;
745     ASN1_primitive_c2i *prim_c2i;
746     ASN1_primitive_i2c *prim_i2c;
747     ASN1_primitive_print *prim_print;
748 } ASN1_PRIMITIVE_FUNCS;
749
750 /*
751  * This is the ASN1_AUX structure: it handles various miscellaneous
752  * requirements. For example the use of reference counts and an informational
753  * callback. The "informational callback" is called at various points during
754  * the ASN1 encoding and decoding. It can be used to provide minor
755  * customisation of the structures used. This is most useful where the
756  * supplied routines *almost* do the right thing but need some extra help at
757  * a few points. If the callback returns zero then it is assumed a fatal
758  * error has occurred and the main operation should be abandoned. If major
759  * changes in the default behaviour are required then an external type is
760  * more appropriate.
761  */
762
763 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
764                         void *exarg);
765
766 typedef struct ASN1_AUX_st {
767     void *app_data;
768     int flags;
769     int ref_offset;             /* Offset of reference value */
770     int ref_lock;               /* Lock type to use */
771     ASN1_aux_cb *asn1_cb;
772     int enc_offset;             /* Offset of ASN1_ENCODING structure */
773 } ASN1_AUX;
774
775 /* For print related callbacks exarg points to this structure */
776 typedef struct ASN1_PRINT_ARG_st {
777     BIO *out;
778     int indent;
779     const ASN1_PCTX *pctx;
780 } ASN1_PRINT_ARG;
781
782 /* For streaming related callbacks exarg points to this structure */
783 typedef struct ASN1_STREAM_ARG_st {
784     /* BIO to stream through */
785     BIO *out;
786     /* BIO with filters appended */
787     BIO *ndef_bio;
788     /* Streaming I/O boundary */
789     unsigned char **boundary;
790 } ASN1_STREAM_ARG;
791
792 /* Flags in ASN1_AUX */
793
794 /* Use a reference count */
795 # define ASN1_AFLG_REFCOUNT      1
796 /* Save the encoding of structure (useful for signatures) */
797 # define ASN1_AFLG_ENCODING      2
798 /* The Sequence length is invalid */
799 # define ASN1_AFLG_BROKEN        4
800
801 /* operation values for asn1_cb */
802
803 # define ASN1_OP_NEW_PRE         0
804 # define ASN1_OP_NEW_POST        1
805 # define ASN1_OP_FREE_PRE        2
806 # define ASN1_OP_FREE_POST       3
807 # define ASN1_OP_D2I_PRE         4
808 # define ASN1_OP_D2I_POST        5
809 # define ASN1_OP_I2D_PRE         6
810 # define ASN1_OP_I2D_POST        7
811 # define ASN1_OP_PRINT_PRE       8
812 # define ASN1_OP_PRINT_POST      9
813 # define ASN1_OP_STREAM_PRE      10
814 # define ASN1_OP_STREAM_POST     11
815 # define ASN1_OP_DETACHED_PRE    12
816 # define ASN1_OP_DETACHED_POST   13
817
818 /* Macro to implement a primitive type */
819 # define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
820 # define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
821                                 ASN1_ITEM_start(itname) \
822                                         ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
823                                 ASN1_ITEM_end(itname)
824
825 /* Macro to implement a multi string type */
826 # define IMPLEMENT_ASN1_MSTRING(itname, mask) \
827                                 ASN1_ITEM_start(itname) \
828                                         ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
829                                 ASN1_ITEM_end(itname)
830
831 # define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
832         ASN1_ITEM_start(sname) \
833                 ASN1_ITYPE_EXTERN, \
834                 tag, \
835                 NULL, \
836                 0, \
837                 &fptrs, \
838                 0, \
839                 #sname \
840         ASN1_ITEM_end(sname)
841
842 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
843
844 # define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
845
846 # define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
847
848 # define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
849                         IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
850
851 # define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
852                 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
853
854 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
855                 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
856
857 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
858         pre stname *fname##_new(void) \
859         { \
860                 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
861         } \
862         pre void fname##_free(stname *a) \
863         { \
864                 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
865         }
866
867 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
868         stname *fname##_new(void) \
869         { \
870                 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
871         } \
872         void fname##_free(stname *a) \
873         { \
874                 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
875         }
876
877 # define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
878         IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
879         IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
880
881 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
882         stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
883         { \
884                 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
885         } \
886         int i2d_##fname(stname *a, unsigned char **out) \
887         { \
888                 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
889         }
890
891 # define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
892         int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
893         { \
894                 return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
895         }
896
897 # define IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(stname) \
898         static stname *d2i_##stname(stname **a, \
899                                    const unsigned char **in, long len) \
900         { \
901                 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \
902                                                ASN1_ITEM_rptr(stname)); \
903         } \
904         static int i2d_##stname(stname *a, unsigned char **out) \
905         { \
906                 return ASN1_item_i2d((ASN1_VALUE *)a, out, \
907                                      ASN1_ITEM_rptr(stname)); \
908         }
909
910 /*
911  * This includes evil casts to remove const: they will go away when full ASN1
912  * constification is done.
913  */
914 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
915         stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
916         { \
917                 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
918         } \
919         int i2d_##fname(const stname *a, unsigned char **out) \
920         { \
921                 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
922         }
923
924 # define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
925         stname * stname##_dup(stname *x) \
926         { \
927         return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
928         }
929
930 # define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
931         IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
932
933 # define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
934         int fname##_print_ctx(BIO *out, stname *x, int indent, \
935                                                 const ASN1_PCTX *pctx) \
936         { \
937                 return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \
938                         ASN1_ITEM_rptr(itname), pctx); \
939         }
940
941 # define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
942                 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
943
944 # define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
945         IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
946         IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
947
948 /* external definitions for primitive types */
949
950 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
951 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
952 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
953 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
954 DECLARE_ASN1_ITEM(CBIGNUM)
955 DECLARE_ASN1_ITEM(BIGNUM)
956 DECLARE_ASN1_ITEM(LONG)
957 DECLARE_ASN1_ITEM(ZLONG)
958
959 DECLARE_STACK_OF(ASN1_VALUE)
960
961 /* Functions used internally by the ASN1 code */
962
963 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
964 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
965
966 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
967                      const ASN1_ITEM *it, int tag, int aclass, char opt,
968                      ASN1_TLC *ctx);
969
970 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
971                      const ASN1_ITEM *it, int tag, int aclass);
972
973 #ifdef  __cplusplus
974 }
975 #endif
976 #endif