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