3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
59 #ifndef HEADER_ASN1T_H
60 # define HEADER_ASN1T_H
63 # include <openssl/e_os2.h>
64 # include <openssl/asn1.h>
66 # ifdef OPENSSL_BUILD_SHLIBCRYPTO
67 # undef OPENSSL_EXTERN
68 # define OPENSSL_EXTERN OPENSSL_EXPORT
71 /* ASN1 template defines, structures and functions */
77 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
79 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
80 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
82 /* Macros for start and end of ASN1_ITEM definition */
84 # define ASN1_ITEM_start(itname) \
85 OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
87 # define ASN1_ITEM_end(itname) \
92 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
93 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
95 /* Macros for start and end of ASN1_ITEM definition */
97 # define ASN1_ITEM_start(itname) \
98 const ASN1_ITEM * itname##_it(void) \
100 static const ASN1_ITEM local_it = {
102 # define ASN1_ITEM_end(itname) \
109 /* Macros to aid ASN1 template writing */
111 # define ASN1_ITEM_TEMPLATE(tname) \
112 static const ASN1_TEMPLATE tname##_item_tt
114 # define ASN1_ITEM_TEMPLATE_END(tname) \
116 ASN1_ITEM_start(tname) \
117 ASN1_ITYPE_PRIMITIVE,\
126 /* This is a ASN1 type which just embeds a template */
129 * This pair helps declare a SEQUENCE. We can do:
131 * ASN1_SEQUENCE(stname) = {
132 * ... SEQUENCE components ...
133 * } ASN1_SEQUENCE_END(stname)
135 * This will produce an ASN1_ITEM called stname_it
136 * for a structure called stname.
138 * If you want the same structure but a different
141 * ASN1_SEQUENCE(itname) = {
142 * ... SEQUENCE components ...
143 * } ASN1_SEQUENCE_END_name(stname, itname)
145 * This will create an item called itname_it using
146 * a structure called stname.
149 # define ASN1_SEQUENCE(tname) \
150 static const ASN1_TEMPLATE tname##_seq_tt[]
152 # define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
154 # define ASN1_SEQUENCE_END_name(stname, tname) \
156 ASN1_ITEM_start(tname) \
157 ASN1_ITYPE_SEQUENCE,\
160 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
166 # define ASN1_NDEF_SEQUENCE(tname) \
169 # define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
170 ASN1_SEQUENCE_cb(tname, cb)
172 # define ASN1_SEQUENCE_cb(tname, cb) \
173 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
176 # define ASN1_BROKEN_SEQUENCE(tname) \
177 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
180 # define ASN1_SEQUENCE_ref(tname, cb, lck) \
181 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
184 # define ASN1_SEQUENCE_enc(tname, enc, cb) \
185 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
188 # define ASN1_NDEF_SEQUENCE_END(tname) \
190 ASN1_ITEM_start(tname) \
191 ASN1_ITYPE_NDEF_SEQUENCE,\
194 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
200 # define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
202 # define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
204 # define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
206 # define ASN1_SEQUENCE_END_ref(stname, tname) \
208 ASN1_ITEM_start(tname) \
209 ASN1_ITYPE_SEQUENCE,\
212 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
218 # define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
220 ASN1_ITEM_start(tname) \
221 ASN1_ITYPE_NDEF_SEQUENCE,\
224 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
231 * This pair helps declare a CHOICE type. We can do:
233 * ASN1_CHOICE(chname) = {
234 * ... CHOICE options ...
235 * ASN1_CHOICE_END(chname)
237 * This will produce an ASN1_ITEM called chname_it
238 * for a structure called chname. The structure
239 * definition must look like this:
243 * ASN1_SOMETHING *opt1;
244 * ASN1_SOMEOTHER *opt2;
248 * the name of the selector must be 'type'.
249 * to use an alternative selector name use the
250 * ASN1_CHOICE_END_selector() version.
253 # define ASN1_CHOICE(tname) \
254 static const ASN1_TEMPLATE tname##_ch_tt[]
256 # define ASN1_CHOICE_cb(tname, cb) \
257 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
260 # define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
262 # define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
264 # define ASN1_CHOICE_END_selector(stname, tname, selname) \
266 ASN1_ITEM_start(tname) \
268 offsetof(stname,selname) ,\
270 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
276 # define ASN1_CHOICE_END_cb(stname, tname, selname) \
278 ASN1_ITEM_start(tname) \
280 offsetof(stname,selname) ,\
282 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
288 /* This helps with the template wrapper form of ASN1_ITEM */
290 # define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
292 #name, ASN1_ITEM_ref(type) }
294 /* These help with SEQUENCE or CHOICE components */
296 /* used to declare other types */
298 # define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
299 (flags), (tag), offsetof(stname, field),\
300 #field, ASN1_ITEM_ref(type) }
302 /* implicit and explicit helper macros */
304 # define ASN1_IMP_EX(stname, field, type, tag, ex) \
305 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
307 # define ASN1_EXP_EX(stname, field, type, tag, ex) \
308 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
310 /* Any defined by macros: the field used is in the table itself */
312 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
313 # define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
314 # define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
316 # define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
317 # define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
319 /* Plain simple type */
320 # define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
322 /* OPTIONAL simple type */
323 # define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
325 /* IMPLICIT tagged simple type */
326 # define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
328 /* IMPLICIT tagged OPTIONAL simple type */
329 # define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
331 /* Same as above but EXPLICIT */
333 # define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
334 # define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
336 /* SEQUENCE OF type */
337 # define ASN1_SEQUENCE_OF(stname, field, type) \
338 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
340 /* OPTIONAL SEQUENCE OF */
341 # define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
342 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
344 /* Same as above but for SET OF */
346 # define ASN1_SET_OF(stname, field, type) \
347 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
349 # define ASN1_SET_OF_OPT(stname, field, type) \
350 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
352 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
354 # define ASN1_IMP_SET_OF(stname, field, type, tag) \
355 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
357 # define ASN1_EXP_SET_OF(stname, field, type, tag) \
358 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
360 # define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
361 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
363 # define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
364 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
366 # define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
367 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
369 # define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
370 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
372 # define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
373 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
375 # define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
376 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
378 /* EXPLICIT using indefinite length constructed form */
379 # define ASN1_NDEF_EXP(stname, field, type, tag) \
380 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
382 /* EXPLICIT OPTIONAL using indefinite length constructed form */
383 # define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
384 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
386 /* Macros for the ASN1_ADB structure */
388 # define ASN1_ADB(name) \
389 static const ASN1_ADB_TABLE name##_adbtbl[]
391 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
393 # define ASN1_ADB_END(name, flags, field, app_table, def, none) \
395 static const ASN1_ADB name##_adb = {\
397 offsetof(name, field),\
400 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
407 # define ASN1_ADB_END(name, flags, field, app_table, def, none) \
409 static const ASN1_ITEM *name##_adb(void) \
411 static const ASN1_ADB internal_adb = \
414 offsetof(name, field),\
417 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
421 return (const ASN1_ITEM *) &internal_adb; \
423 void dummy_function(void)
427 # define ADB_ENTRY(val, template) {val, template}
429 # define ASN1_ADB_TEMPLATE(name) \
430 static const ASN1_TEMPLATE name##_tt
433 * This is the ASN1 template structure that defines a wrapper round the
434 * actual type. It determines the actual position of the field in the value
435 * structure, various flags such as OPTIONAL and the field name.
438 struct ASN1_TEMPLATE_st {
439 unsigned long flags; /* Various flags */
440 long tag; /* tag, not used if no tagging */
441 unsigned long offset; /* Offset of this field in structure */
442 # ifndef NO_ASN1_FIELD_NAMES
443 const char *field_name; /* Field name */
445 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
448 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
450 # define ASN1_TEMPLATE_item(t) (t->item_ptr)
451 # define ASN1_TEMPLATE_adb(t) (t->item_ptr)
453 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
454 typedef struct ASN1_ADB_st ASN1_ADB;
457 unsigned long flags; /* Various flags */
458 unsigned long offset; /* Offset of selector field */
459 STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
460 const ASN1_ADB_TABLE *tbl; /* Table of possible types */
461 long tblcount; /* Number of entries in tbl */
462 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
463 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
466 struct ASN1_ADB_TABLE_st {
467 long value; /* NID for an object or value for an int */
468 const ASN1_TEMPLATE tt; /* item for this value */
473 /* Field is optional */
474 # define ASN1_TFLG_OPTIONAL (0x1)
476 /* Field is a SET OF */
477 # define ASN1_TFLG_SET_OF (0x1 << 1)
479 /* Field is a SEQUENCE OF */
480 # define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
483 * Special case: this refers to a SET OF that will be sorted into DER order
484 * when encoded *and* the corresponding STACK will be modified to match the
487 # define ASN1_TFLG_SET_ORDER (0x3 << 1)
489 /* Mask for SET OF or SEQUENCE OF */
490 # define ASN1_TFLG_SK_MASK (0x3 << 1)
493 * These flags mean the tag should be taken from the tag field. If EXPLICIT
494 * then the underlying type is used for the inner tag.
497 /* IMPLICIT tagging */
498 # define ASN1_TFLG_IMPTAG (0x1 << 3)
500 /* EXPLICIT tagging, inner tag from underlying type */
501 # define ASN1_TFLG_EXPTAG (0x2 << 3)
503 # define ASN1_TFLG_TAG_MASK (0x3 << 3)
505 /* context specific IMPLICIT */
506 # define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
508 /* context specific EXPLICIT */
509 # define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
512 * If tagging is in force these determine the type of tag to use. Otherwise
513 * the tag is determined by the underlying type. These values reflect the
514 * actual octet format.
518 # define ASN1_TFLG_UNIVERSAL (0x0<<6)
519 /* Application tag */
520 # define ASN1_TFLG_APPLICATION (0x1<<6)
521 /* Context specific tag */
522 # define ASN1_TFLG_CONTEXT (0x2<<6)
524 # define ASN1_TFLG_PRIVATE (0x3<<6)
526 # define ASN1_TFLG_TAG_CLASS (0x3<<6)
529 * These are for ANY DEFINED BY type. In this case the 'item' field points to
530 * an ASN1_ADB structure which contains a table of values to decode the
534 # define ASN1_TFLG_ADB_MASK (0x3<<8)
536 # define ASN1_TFLG_ADB_OID (0x1<<8)
538 # define ASN1_TFLG_ADB_INT (0x1<<9)
541 * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes
542 * indefinite length constructed encoding to be used if required.
545 # define ASN1_TFLG_NDEF (0x1<<11)
547 /* This is the actual ASN1 item itself */
549 struct ASN1_ITEM_st {
550 char itype; /* The item type, primitive, SEQUENCE, CHOICE
552 long utype; /* underlying type */
553 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains
555 long tcount; /* Number of templates if SEQUENCE or CHOICE */
556 const void *funcs; /* functions that handle this type */
557 long size; /* Structure size (usually) */
558 # ifndef NO_ASN1_FIELD_NAMES
559 const char *sname; /* Structure name */
564 * These are values for the itype field and
565 * determine how the type is interpreted.
567 * For PRIMITIVE types the underlying type
568 * determines the behaviour if items is NULL.
570 * Otherwise templates must contain a single
571 * template and the type is treated in the
572 * same way as the type specified in the template.
574 * For SEQUENCE types the templates field points
575 * to the members, the size field is the
578 * For CHOICE types the templates field points
579 * to each possible member (typically a union)
580 * and the 'size' field is the offset of the
583 * The 'funcs' field is used for application
584 * specific functions.
586 * The EXTERN type uses a new style d2i/i2d.
587 * The new style should be used where possible
588 * because it avoids things like the d2i IMPLICIT
591 * MSTRING is a multiple string type, it is used
592 * for a CHOICE of character strings where the
593 * actual strings all occupy an ASN1_STRING
594 * structure. In this case the 'utype' field
595 * has a special meaning, it is used as a mask
596 * of acceptable types using the B_ASN1 constants.
598 * NDEF_SEQUENCE is the same as SEQUENCE except
599 * that it will use indefinite length constructed
600 * encoding if requested.
604 # define ASN1_ITYPE_PRIMITIVE 0x0
606 # define ASN1_ITYPE_SEQUENCE 0x1
608 # define ASN1_ITYPE_CHOICE 0x2
610 # define ASN1_ITYPE_EXTERN 0x4
612 # define ASN1_ITYPE_MSTRING 0x5
614 # define ASN1_ITYPE_NDEF_SEQUENCE 0x6
617 * Cache for ASN1 tag and length, so we don't keep re-reading it for things
622 char valid; /* Values below are valid */
623 int ret; /* return value */
624 long plen; /* length */
625 int ptag; /* class value */
626 int pclass; /* class value */
627 int hdrlen; /* header length */
630 /* Typedefs for ASN1 function pointers */
632 typedef ASN1_VALUE *ASN1_new_func(void);
633 typedef void ASN1_free_func(ASN1_VALUE *a);
634 typedef ASN1_VALUE *ASN1_d2i_func(ASN1_VALUE **a, const unsigned char **in,
636 typedef int ASN1_i2d_func(ASN1_VALUE *a, unsigned char **in);
638 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
639 const ASN1_ITEM *it, int tag, int aclass, char opt,
642 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
643 const ASN1_ITEM *it, int tag, int aclass);
644 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
645 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
647 typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
648 int indent, const char *fname,
649 const ASN1_PCTX *pctx);
651 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont,
652 int *putype, const ASN1_ITEM *it);
653 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
654 int len, int utype, char *free_cont,
655 const ASN1_ITEM *it);
656 typedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval,
657 const ASN1_ITEM *it, int indent,
658 const ASN1_PCTX *pctx);
660 typedef struct ASN1_EXTERN_FUNCS_st {
662 ASN1_ex_new_func *asn1_ex_new;
663 ASN1_ex_free_func *asn1_ex_free;
664 ASN1_ex_free_func *asn1_ex_clear;
665 ASN1_ex_d2i *asn1_ex_d2i;
666 ASN1_ex_i2d *asn1_ex_i2d;
667 ASN1_ex_print_func *asn1_ex_print;
670 typedef struct ASN1_PRIMITIVE_FUNCS_st {
673 ASN1_ex_new_func *prim_new;
674 ASN1_ex_free_func *prim_free;
675 ASN1_ex_free_func *prim_clear;
676 ASN1_primitive_c2i *prim_c2i;
677 ASN1_primitive_i2c *prim_i2c;
678 ASN1_primitive_print *prim_print;
679 } ASN1_PRIMITIVE_FUNCS;
682 * This is the ASN1_AUX structure: it handles various miscellaneous
683 * requirements. For example the use of reference counts and an informational
684 * callback. The "informational callback" is called at various points during
685 * the ASN1 encoding and decoding. It can be used to provide minor
686 * customisation of the structures used. This is most useful where the
687 * supplied routines *almost* do the right thing but need some extra help at
688 * a few points. If the callback returns zero then it is assumed a fatal
689 * error has occurred and the main operation should be abandoned. If major
690 * changes in the default behaviour are required then an external type is
694 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
697 typedef struct ASN1_AUX_st {
700 int ref_offset; /* Offset of reference value */
701 int ref_lock; /* Lock type to use */
702 ASN1_aux_cb *asn1_cb;
703 int enc_offset; /* Offset of ASN1_ENCODING structure */
706 /* For print related callbacks exarg points to this structure */
707 typedef struct ASN1_PRINT_ARG_st {
710 const ASN1_PCTX *pctx;
713 /* For streaming related callbacks exarg points to this structure */
714 typedef struct ASN1_STREAM_ARG_st {
715 /* BIO to stream through */
717 /* BIO with filters appended */
719 /* Streaming I/O boundary */
720 unsigned char **boundary;
723 /* Flags in ASN1_AUX */
725 /* Use a reference count */
726 # define ASN1_AFLG_REFCOUNT 1
727 /* Save the encoding of structure (useful for signatures) */
728 # define ASN1_AFLG_ENCODING 2
729 /* The Sequence length is invalid */
730 # define ASN1_AFLG_BROKEN 4
732 /* operation values for asn1_cb */
734 # define ASN1_OP_NEW_PRE 0
735 # define ASN1_OP_NEW_POST 1
736 # define ASN1_OP_FREE_PRE 2
737 # define ASN1_OP_FREE_POST 3
738 # define ASN1_OP_D2I_PRE 4
739 # define ASN1_OP_D2I_POST 5
740 # define ASN1_OP_I2D_PRE 6
741 # define ASN1_OP_I2D_POST 7
742 # define ASN1_OP_PRINT_PRE 8
743 # define ASN1_OP_PRINT_POST 9
744 # define ASN1_OP_STREAM_PRE 10
745 # define ASN1_OP_STREAM_POST 11
746 # define ASN1_OP_DETACHED_PRE 12
747 # define ASN1_OP_DETACHED_POST 13
749 /* Macro to implement a primitive type */
750 # define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
751 # define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
752 ASN1_ITEM_start(itname) \
753 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
754 ASN1_ITEM_end(itname)
756 /* Macro to implement a multi string type */
757 # define IMPLEMENT_ASN1_MSTRING(itname, mask) \
758 ASN1_ITEM_start(itname) \
759 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
760 ASN1_ITEM_end(itname)
762 # define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
763 ASN1_ITEM_start(sname) \
773 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
775 # define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
777 # define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
779 # define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
780 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
782 # define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
783 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
785 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
786 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
788 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
789 pre stname *fname##_new(void) \
791 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
793 pre void fname##_free(stname *a) \
795 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
798 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
799 stname *fname##_new(void) \
801 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
803 void fname##_free(stname *a) \
805 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
808 # define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
809 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
810 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
812 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
813 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
815 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
817 int i2d_##fname(stname *a, unsigned char **out) \
819 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
822 # define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
823 int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
825 return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
828 # define IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(stname) \
829 static stname *d2i_##stname(stname **a, \
830 const unsigned char **in, long len) \
832 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \
833 ASN1_ITEM_rptr(stname)); \
835 static int i2d_##stname(stname *a, unsigned char **out) \
837 return ASN1_item_i2d((ASN1_VALUE *)a, out, \
838 ASN1_ITEM_rptr(stname)); \
842 * This includes evil casts to remove const: they will go away when full ASN1
843 * constification is done.
845 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
846 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
848 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
850 int i2d_##fname(const stname *a, unsigned char **out) \
852 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
855 # define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
856 stname * stname##_dup(stname *x) \
858 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
861 # define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
862 IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
864 # define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
865 int fname##_print_ctx(BIO *out, stname *x, int indent, \
866 const ASN1_PCTX *pctx) \
868 return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \
869 ASN1_ITEM_rptr(itname), pctx); \
872 # define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
873 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
875 # define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
876 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
877 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
879 /* external definitions for primitive types */
881 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
882 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
883 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
884 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
885 DECLARE_ASN1_ITEM(CBIGNUM)
886 DECLARE_ASN1_ITEM(BIGNUM)
887 DECLARE_ASN1_ITEM(LONG)
888 DECLARE_ASN1_ITEM(ZLONG)
890 DECLARE_STACK_OF(ASN1_VALUE)
892 /* Functions used internally by the ASN1 code */
894 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
895 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
897 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
898 const ASN1_ITEM *it, int tag, int aclass, char opt,
901 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
902 const ASN1_ITEM *it, int tag, int aclass);