2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
\r
5 /* ====================================================================
\r
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions
\r
12 * 1. Redistributions of source code must retain the above copyright
\r
13 * notice, this list of conditions and the following disclaimer.
\r
15 * 2. Redistributions in binary form must reproduce the above copyright
\r
16 * notice, this list of conditions and the following disclaimer in
\r
17 * the documentation and/or other materials provided with the
\r
20 * 3. All advertising materials mentioning features or use of this
\r
21 * software must display the following acknowledgment:
\r
22 * "This product includes software developed by the OpenSSL Project
\r
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
\r
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
\r
26 * endorse or promote products derived from this software without
\r
27 * prior written permission. For written permission, please contact
\r
28 * licensing@OpenSSL.org.
\r
30 * 5. Products derived from this software may not be called "OpenSSL"
\r
31 * nor may "OpenSSL" appear in their names without prior written
\r
32 * permission of the OpenSSL Project.
\r
34 * 6. Redistributions of any form whatsoever must retain the following
\r
36 * "This product includes software developed by the OpenSSL Project
\r
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
\r
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
\r
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
\r
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
\r
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
\r
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
\r
51 * ====================================================================
\r
53 * This product includes cryptographic software written by Eric Young
\r
54 * (eay@cryptsoft.com). This product includes software written by Tim
\r
55 * Hudson (tjh@cryptsoft.com).
\r
58 #ifndef HEADER_ASN1T_H
\r
59 #define HEADER_ASN1T_H
\r
62 #include <openssl/e_os2.h>
\r
63 #include <openssl/asn1.h>
\r
65 #ifdef OPENSSL_BUILD_SHLIBCRYPTO
\r
66 # undef OPENSSL_EXTERN
\r
67 # define OPENSSL_EXTERN OPENSSL_EXPORT
\r
70 /* ASN1 template defines, structures and functions */
\r
77 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
\r
79 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
\r
80 #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
\r
83 /* Macros for start and end of ASN1_ITEM definition */
\r
85 #define ASN1_ITEM_start(itname) \
\r
86 OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
\r
88 #define ASN1_ITEM_end(itname) \
\r
93 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
\r
94 #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
\r
97 /* Macros for start and end of ASN1_ITEM definition */
\r
99 #define ASN1_ITEM_start(itname) \
\r
100 const ASN1_ITEM * itname##_it(void) \
\r
102 static const ASN1_ITEM local_it = { \
\r
104 #define ASN1_ITEM_end(itname) \
\r
106 return &local_it; \
\r
112 /* Macros to aid ASN1 template writing */
\r
114 #define ASN1_ITEM_TEMPLATE(tname) \
\r
115 static const ASN1_TEMPLATE tname##_item_tt
\r
117 #define ASN1_ITEM_TEMPLATE_END(tname) \
\r
119 ASN1_ITEM_start(tname) \
\r
120 ASN1_ITYPE_PRIMITIVE,\
\r
127 ASN1_ITEM_end(tname)
\r
130 /* This is a ASN1 type which just embeds a template */
\r
132 /* This pair helps declare a SEQUENCE. We can do:
\r
134 * ASN1_SEQUENCE(stname) = {
\r
135 * ... SEQUENCE components ...
\r
136 * } ASN1_SEQUENCE_END(stname)
\r
138 * This will produce an ASN1_ITEM called stname_it
\r
139 * for a structure called stname.
\r
141 * If you want the same structure but a different
\r
144 * ASN1_SEQUENCE(itname) = {
\r
145 * ... SEQUENCE components ...
\r
146 * } ASN1_SEQUENCE_END_name(stname, itname)
\r
148 * This will create an item called itname_it using
\r
149 * a structure called stname.
\r
152 #define ASN1_SEQUENCE(tname) \
\r
153 static const ASN1_TEMPLATE tname##_seq_tt[]
\r
155 #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
\r
157 #define ASN1_SEQUENCE_END_name(stname, tname) \
\r
159 ASN1_ITEM_start(tname) \
\r
160 ASN1_ITYPE_SEQUENCE,\
\r
163 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
\r
167 ASN1_ITEM_end(tname)
\r
169 #define ASN1_NDEF_SEQUENCE(tname) \
\r
170 ASN1_SEQUENCE(tname)
\r
172 #define ASN1_SEQUENCE_cb(tname, cb) \
\r
173 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
\r
174 ASN1_SEQUENCE(tname)
\r
176 #define ASN1_BROKEN_SEQUENCE(tname) \
\r
177 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
\r
178 ASN1_SEQUENCE(tname)
\r
180 #define ASN1_SEQUENCE_ref(tname, cb, lck) \
\r
181 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
\r
182 ASN1_SEQUENCE(tname)
\r
184 #define ASN1_SEQUENCE_enc(tname, enc, cb) \
\r
185 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
\r
186 ASN1_SEQUENCE(tname)
\r
188 #define ASN1_NDEF_SEQUENCE_END(tname) \
\r
190 ASN1_ITEM_start(tname) \
\r
191 ASN1_ITYPE_NDEF_SEQUENCE,\
\r
194 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
\r
198 ASN1_ITEM_end(tname)
\r
200 #define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
\r
202 #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
\r
204 #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
\r
206 #define ASN1_SEQUENCE_END_ref(stname, tname) \
\r
208 ASN1_ITEM_start(tname) \
\r
209 ASN1_ITYPE_SEQUENCE,\
\r
212 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
\r
216 ASN1_ITEM_end(tname)
\r
219 /* This pair helps declare a CHOICE type. We can do:
\r
221 * ASN1_CHOICE(chname) = {
\r
222 * ... CHOICE options ...
\r
223 * ASN1_CHOICE_END(chname)
\r
225 * This will produce an ASN1_ITEM called chname_it
\r
226 * for a structure called chname. The structure
\r
227 * definition must look like this:
\r
231 * ASN1_SOMETHING *opt1;
\r
232 * ASN1_SOMEOTHER *opt2;
\r
236 * the name of the selector must be 'type'.
\r
237 * to use an alternative selector name use the
\r
238 * ASN1_CHOICE_END_selector() version.
\r
241 #define ASN1_CHOICE(tname) \
\r
242 static const ASN1_TEMPLATE tname##_ch_tt[]
\r
244 #define ASN1_CHOICE_cb(tname, cb) \
\r
245 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
\r
248 #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
\r
250 #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
\r
252 #define ASN1_CHOICE_END_selector(stname, tname, selname) \
\r
254 ASN1_ITEM_start(tname) \
\r
255 ASN1_ITYPE_CHOICE,\
\r
256 offsetof(stname,selname) ,\
\r
258 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
\r
262 ASN1_ITEM_end(tname)
\r
264 #define ASN1_CHOICE_END_cb(stname, tname, selname) \
\r
266 ASN1_ITEM_start(tname) \
\r
267 ASN1_ITYPE_CHOICE,\
\r
268 offsetof(stname,selname) ,\
\r
270 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
\r
274 ASN1_ITEM_end(tname)
\r
276 /* This helps with the template wrapper form of ASN1_ITEM */
\r
278 #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
\r
279 (flags), (tag), 0,\
\r
280 #name, ASN1_ITEM_ref(type) }
\r
282 /* These help with SEQUENCE or CHOICE components */
\r
284 /* used to declare other types */
\r
286 #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
\r
287 (flags), (tag), offsetof(stname, field),\
\r
288 #field, ASN1_ITEM_ref(type) }
\r
290 /* used when the structure is combined with the parent */
\r
292 #define ASN1_EX_COMBINE(flags, tag, type) { \
\r
293 (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
\r
295 /* implicit and explicit helper macros */
\r
297 #define ASN1_IMP_EX(stname, field, type, tag, ex) \
\r
298 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
\r
300 #define ASN1_EXP_EX(stname, field, type, tag, ex) \
\r
301 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
\r
303 /* Any defined by macros: the field used is in the table itself */
\r
305 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
\r
306 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
\r
307 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
\r
309 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
\r
310 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
\r
312 /* Plain simple type */
\r
313 #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
\r
315 /* OPTIONAL simple type */
\r
316 #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
\r
318 /* IMPLICIT tagged simple type */
\r
319 #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
\r
321 /* IMPLICIT tagged OPTIONAL simple type */
\r
322 #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
\r
324 /* Same as above but EXPLICIT */
\r
326 #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
\r
327 #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
\r
329 /* SEQUENCE OF type */
\r
330 #define ASN1_SEQUENCE_OF(stname, field, type) \
\r
331 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
\r
333 /* OPTIONAL SEQUENCE OF */
\r
334 #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
\r
335 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
\r
337 /* Same as above but for SET OF */
\r
339 #define ASN1_SET_OF(stname, field, type) \
\r
340 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
\r
342 #define ASN1_SET_OF_OPT(stname, field, type) \
\r
343 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
\r
345 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
\r
347 #define ASN1_IMP_SET_OF(stname, field, type, tag) \
\r
348 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
\r
350 #define ASN1_EXP_SET_OF(stname, field, type, tag) \
\r
351 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
\r
353 #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
\r
354 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
\r
356 #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
\r
357 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
\r
359 #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
\r
360 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
\r
362 #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
\r
363 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
\r
365 #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
\r
366 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
\r
368 #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
\r
369 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
\r
371 /* EXPLICIT OPTIONAL using indefinite length constructed form */
\r
372 #define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
\r
373 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
\r
375 /* Macros for the ASN1_ADB structure */
\r
377 #define ASN1_ADB(name) \
\r
378 static const ASN1_ADB_TABLE name##_adbtbl[]
\r
380 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
\r
382 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
\r
384 static const ASN1_ADB name##_adb = {\
\r
386 offsetof(name, field),\
\r
389 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
\r
396 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
\r
398 static const ASN1_ITEM *name##_adb(void) \
\r
400 static const ASN1_ADB internal_adb = \
\r
403 offsetof(name, field),\
\r
406 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
\r
410 return (const ASN1_ITEM *) &internal_adb; \
\r
412 void dummy_function(void)
\r
416 #define ADB_ENTRY(val, template) {val, template}
\r
418 #define ASN1_ADB_TEMPLATE(name) \
\r
419 static const ASN1_TEMPLATE name##_tt
\r
421 /* This is the ASN1 template structure that defines
\r
422 * a wrapper round the actual type. It determines the
\r
423 * actual position of the field in the value structure,
\r
424 * various flags such as OPTIONAL and the field name.
\r
427 struct ASN1_TEMPLATE_st {
\r
428 unsigned long flags; /* Various flags */
\r
429 long tag; /* tag, not used if no tagging */
\r
430 unsigned long offset; /* Offset of this field in structure */
\r
431 #ifndef NO_ASN1_FIELD_NAMES
\r
432 const char *field_name; /* Field name */
\r
434 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
\r
437 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
\r
439 #define ASN1_TEMPLATE_item(t) (t->item_ptr)
\r
440 #define ASN1_TEMPLATE_adb(t) (t->item_ptr)
\r
442 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
\r
443 typedef struct ASN1_ADB_st ASN1_ADB;
\r
445 struct ASN1_ADB_st {
\r
446 unsigned long flags; /* Various flags */
\r
447 unsigned long offset; /* Offset of selector field */
\r
448 STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
\r
449 const ASN1_ADB_TABLE *tbl; /* Table of possible types */
\r
450 long tblcount; /* Number of entries in tbl */
\r
451 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
\r
452 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
\r
455 struct ASN1_ADB_TABLE_st {
\r
456 long value; /* NID for an object or value for an int */
\r
457 const ASN1_TEMPLATE tt; /* item for this value */
\r
460 /* template flags */
\r
462 /* Field is optional */
\r
463 #define ASN1_TFLG_OPTIONAL (0x1)
\r
465 /* Field is a SET OF */
\r
466 #define ASN1_TFLG_SET_OF (0x1 << 1)
\r
468 /* Field is a SEQUENCE OF */
\r
469 #define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
\r
471 /* Special case: this refers to a SET OF that
\r
472 * will be sorted into DER order when encoded *and*
\r
473 * the corresponding STACK will be modified to match
\r
476 #define ASN1_TFLG_SET_ORDER (0x3 << 1)
\r
478 /* Mask for SET OF or SEQUENCE OF */
\r
479 #define ASN1_TFLG_SK_MASK (0x3 << 1)
\r
481 /* These flags mean the tag should be taken from the
\r
482 * tag field. If EXPLICIT then the underlying type
\r
483 * is used for the inner tag.
\r
486 /* IMPLICIT tagging */
\r
487 #define ASN1_TFLG_IMPTAG (0x1 << 3)
\r
490 /* EXPLICIT tagging, inner tag from underlying type */
\r
491 #define ASN1_TFLG_EXPTAG (0x2 << 3)
\r
493 #define ASN1_TFLG_TAG_MASK (0x3 << 3)
\r
495 /* context specific IMPLICIT */
\r
496 #define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
\r
498 /* context specific EXPLICIT */
\r
499 #define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
\r
501 /* If tagging is in force these determine the
\r
502 * type of tag to use. Otherwise the tag is
\r
503 * determined by the underlying type. These
\r
504 * values reflect the actual octet format.
\r
507 /* Universal tag */
\r
508 #define ASN1_TFLG_UNIVERSAL (0x0<<6)
\r
509 /* Application tag */
\r
510 #define ASN1_TFLG_APPLICATION (0x1<<6)
\r
511 /* Context specific tag */
\r
512 #define ASN1_TFLG_CONTEXT (0x2<<6)
\r
514 #define ASN1_TFLG_PRIVATE (0x3<<6)
\r
516 #define ASN1_TFLG_TAG_CLASS (0x3<<6)
\r
518 /* These are for ANY DEFINED BY type. In this case
\r
519 * the 'item' field points to an ASN1_ADB structure
\r
520 * which contains a table of values to decode the
\r
524 #define ASN1_TFLG_ADB_MASK (0x3<<8)
\r
526 #define ASN1_TFLG_ADB_OID (0x1<<8)
\r
528 #define ASN1_TFLG_ADB_INT (0x1<<9)
\r
530 /* This flag means a parent structure is passed
\r
531 * instead of the field: this is useful is a
\r
532 * SEQUENCE is being combined with a CHOICE for
\r
533 * example. Since this means the structure and
\r
534 * item name will differ we need to use the
\r
535 * ASN1_CHOICE_END_name() macro for example.
\r
538 #define ASN1_TFLG_COMBINE (0x1<<10)
\r
540 /* This flag when present in a SEQUENCE OF, SET OF
\r
541 * or EXPLICIT causes indefinite length constructed
\r
542 * encoding to be used if required.
\r
545 #define ASN1_TFLG_NDEF (0x1<<11)
\r
547 /* This is the actual ASN1 item itself */
\r
549 struct ASN1_ITEM_st {
\r
550 char itype; /* The item type, primitive, SEQUENCE, CHOICE or extern */
\r
551 long utype; /* underlying type */
\r
552 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */
\r
553 long tcount; /* Number of templates if SEQUENCE or CHOICE */
\r
554 const void *funcs; /* functions that handle this type */
\r
555 long size; /* Structure size (usually)*/
\r
556 #ifndef NO_ASN1_FIELD_NAMES
\r
557 const char *sname; /* Structure name */
\r
561 /* These are values for the itype field and
\r
562 * determine how the type is interpreted.
\r
564 * For PRIMITIVE types the underlying type
\r
565 * determines the behaviour if items is NULL.
\r
567 * Otherwise templates must contain a single
\r
568 * template and the type is treated in the
\r
569 * same way as the type specified in the template.
\r
571 * For SEQUENCE types the templates field points
\r
572 * to the members, the size field is the
\r
575 * For CHOICE types the templates field points
\r
576 * to each possible member (typically a union)
\r
577 * and the 'size' field is the offset of the
\r
580 * The 'funcs' field is used for application
\r
581 * specific functions.
\r
583 * For COMPAT types the funcs field gives a
\r
584 * set of functions that handle this type, this
\r
585 * supports the old d2i, i2d convention.
\r
587 * The EXTERN type uses a new style d2i/i2d.
\r
588 * The new style should be used where possible
\r
589 * because it avoids things like the d2i IMPLICIT
\r
592 * MSTRING is a multiple string type, it is used
\r
593 * for a CHOICE of character strings where the
\r
594 * actual strings all occupy an ASN1_STRING
\r
595 * structure. In this case the 'utype' field
\r
596 * has a special meaning, it is used as a mask
\r
597 * of acceptable types using the B_ASN1 constants.
\r
599 * NDEF_SEQUENCE is the same as SEQUENCE except
\r
600 * that it will use indefinite length constructed
\r
601 * encoding if requested.
\r
605 #define ASN1_ITYPE_PRIMITIVE 0x0
\r
607 #define ASN1_ITYPE_SEQUENCE 0x1
\r
609 #define ASN1_ITYPE_CHOICE 0x2
\r
611 #define ASN1_ITYPE_COMPAT 0x3
\r
613 #define ASN1_ITYPE_EXTERN 0x4
\r
615 #define ASN1_ITYPE_MSTRING 0x5
\r
617 #define ASN1_ITYPE_NDEF_SEQUENCE 0x6
\r
619 /* Cache for ASN1 tag and length, so we
\r
620 * don't keep re-reading it for things
\r
624 struct ASN1_TLC_st{
\r
625 char valid; /* Values below are valid */
\r
626 int ret; /* return value */
\r
627 long plen; /* length */
\r
628 int ptag; /* class value */
\r
629 int pclass; /* class value */
\r
630 int hdrlen; /* header length */
\r
633 /* Typedefs for ASN1 function pointers */
\r
635 typedef ASN1_VALUE * ASN1_new_func(void);
\r
636 typedef void ASN1_free_func(ASN1_VALUE *a);
\r
637 typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, const unsigned char ** in, long length);
\r
638 typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
\r
640 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
\r
641 int tag, int aclass, char opt, ASN1_TLC *ctx);
\r
643 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
\r
644 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
645 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
647 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
\r
648 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
\r
650 typedef struct ASN1_COMPAT_FUNCS_st {
\r
651 ASN1_new_func *asn1_new;
\r
652 ASN1_free_func *asn1_free;
\r
653 ASN1_d2i_func *asn1_d2i;
\r
654 ASN1_i2d_func *asn1_i2d;
\r
655 } ASN1_COMPAT_FUNCS;
\r
657 typedef struct ASN1_EXTERN_FUNCS_st {
\r
659 ASN1_ex_new_func *asn1_ex_new;
\r
660 ASN1_ex_free_func *asn1_ex_free;
\r
661 ASN1_ex_free_func *asn1_ex_clear;
\r
662 ASN1_ex_d2i *asn1_ex_d2i;
\r
663 ASN1_ex_i2d *asn1_ex_i2d;
\r
664 } ASN1_EXTERN_FUNCS;
\r
666 typedef struct ASN1_PRIMITIVE_FUNCS_st {
\r
668 unsigned long flags;
\r
669 ASN1_ex_new_func *prim_new;
\r
670 ASN1_ex_free_func *prim_free;
\r
671 ASN1_ex_free_func *prim_clear;
\r
672 ASN1_primitive_c2i *prim_c2i;
\r
673 ASN1_primitive_i2c *prim_i2c;
\r
674 } ASN1_PRIMITIVE_FUNCS;
\r
676 /* This is the ASN1_AUX structure: it handles various
\r
677 * miscellaneous requirements. For example the use of
\r
678 * reference counts and an informational callback.
\r
680 * The "informational callback" is called at various
\r
681 * points during the ASN1 encoding and decoding. It can
\r
682 * be used to provide minor customisation of the structures
\r
683 * used. This is most useful where the supplied routines
\r
684 * *almost* do the right thing but need some extra help
\r
685 * at a few points. If the callback returns zero then
\r
686 * it is assumed a fatal error has occurred and the
\r
687 * main operation should be abandoned.
\r
689 * If major changes in the default behaviour are required
\r
690 * then an external type is more appropriate.
\r
693 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it);
\r
695 typedef struct ASN1_AUX_st {
\r
698 int ref_offset; /* Offset of reference value */
\r
699 int ref_lock; /* Lock type to use */
\r
700 ASN1_aux_cb *asn1_cb;
\r
701 int enc_offset; /* Offset of ASN1_ENCODING structure */
\r
704 /* Flags in ASN1_AUX */
\r
706 /* Use a reference count */
\r
707 #define ASN1_AFLG_REFCOUNT 1
\r
708 /* Save the encoding of structure (useful for signatures) */
\r
709 #define ASN1_AFLG_ENCODING 2
\r
710 /* The Sequence length is invalid */
\r
711 #define ASN1_AFLG_BROKEN 4
\r
713 /* operation values for asn1_cb */
\r
715 #define ASN1_OP_NEW_PRE 0
\r
716 #define ASN1_OP_NEW_POST 1
\r
717 #define ASN1_OP_FREE_PRE 2
\r
718 #define ASN1_OP_FREE_POST 3
\r
719 #define ASN1_OP_D2I_PRE 4
\r
720 #define ASN1_OP_D2I_POST 5
\r
721 #define ASN1_OP_I2D_PRE 6
\r
722 #define ASN1_OP_I2D_POST 7
\r
724 /* Macro to implement a primitive type */
\r
725 #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
\r
726 #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
\r
727 ASN1_ITEM_start(itname) \
\r
728 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
\r
729 ASN1_ITEM_end(itname)
\r
731 /* Macro to implement a multi string type */
\r
732 #define IMPLEMENT_ASN1_MSTRING(itname, mask) \
\r
733 ASN1_ITEM_start(itname) \
\r
734 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
\r
735 ASN1_ITEM_end(itname)
\r
737 /* Macro to implement an ASN1_ITEM in terms of old style funcs */
\r
739 #define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
\r
741 #define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
\r
742 static const ASN1_COMPAT_FUNCS sname##_ff = { \
\r
743 (ASN1_new_func *)sname##_new, \
\r
744 (ASN1_free_func *)sname##_free, \
\r
745 (ASN1_d2i_func *)d2i_##sname, \
\r
746 (ASN1_i2d_func *)i2d_##sname, \
\r
748 ASN1_ITEM_start(sname) \
\r
749 ASN1_ITYPE_COMPAT, \
\r
756 ASN1_ITEM_end(sname)
\r
758 #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
\r
759 ASN1_ITEM_start(sname) \
\r
760 ASN1_ITYPE_EXTERN, \
\r
767 ASN1_ITEM_end(sname)
\r
769 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
\r
771 #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
\r
773 #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
\r
775 #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
\r
776 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
\r
778 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
\r
779 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
\r
781 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
\r
782 stname *fname##_new(void) \
\r
784 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
\r
786 void fname##_free(stname *a) \
\r
788 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
\r
791 #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
\r
792 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
\r
793 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
\r
795 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
\r
796 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
\r
798 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
\r
800 int i2d_##fname(stname *a, unsigned char **out) \
\r
802 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
\r
805 #define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
\r
806 int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
\r
808 return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
\r
811 /* This includes evil casts to remove const: they will go away when full
\r
812 * ASN1 constification is done.
\r
814 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
\r
815 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
\r
817 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
\r
819 int i2d_##fname(const stname *a, unsigned char **out) \
\r
821 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
\r
824 #define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
\r
825 stname * stname##_dup(stname *x) \
\r
827 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
\r
830 #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
\r
831 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
\r
833 #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
\r
834 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
\r
835 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
\r
837 /* external definitions for primitive types */
\r
839 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
\r
840 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
\r
841 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
\r
842 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
\r
843 DECLARE_ASN1_ITEM(CBIGNUM)
\r
844 DECLARE_ASN1_ITEM(BIGNUM)
\r
845 DECLARE_ASN1_ITEM(LONG)
\r
846 DECLARE_ASN1_ITEM(ZLONG)
\r
848 DECLARE_STACK_OF(ASN1_VALUE)
\r
850 /* Functions used internally by the ASN1 code */
\r
852 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
853 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
854 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
\r
855 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
857 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
\r
858 int ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_TEMPLATE *tt);
\r
859 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
\r
860 int tag, int aclass, char opt, ASN1_TLC *ctx);
\r
862 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
\r
863 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt);
\r
864 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
866 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
\r
867 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
\r
869 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
870 int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
\r
872 ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
\r
874 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);
\r
876 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
\r
878 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
879 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
880 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);
\r
881 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, const ASN1_ITEM *it);
\r