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 static_ASN1_ITEM_start(itname) \
88 static const ASN1_ITEM itname##_it = {
90 # define ASN1_ITEM_end(itname) \
95 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
96 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
98 /* Macros for start and end of ASN1_ITEM definition */
100 # define ASN1_ITEM_start(itname) \
101 const ASN1_ITEM * itname##_it(void) \
103 static const ASN1_ITEM local_it = {
105 # define static_ASN1_ITEM_start(itname) \
106 ASN1_ITEM_start(itname)
108 # define ASN1_ITEM_end(itname) \
115 /* Macros to aid ASN1 template writing */
117 # define ASN1_ITEM_TEMPLATE(tname) \
118 static const ASN1_TEMPLATE tname##_item_tt
120 # define ASN1_ITEM_TEMPLATE_END(tname) \
122 ASN1_ITEM_start(tname) \
123 ASN1_ITYPE_PRIMITIVE,\
131 # define static_ASN1_ITEM_TEMPLATE_END(tname) \
133 static_ASN1_ITEM_start(tname) \
134 ASN1_ITYPE_PRIMITIVE,\
143 /* This is a ASN1 type which just embeds a template */
146 * This pair helps declare a SEQUENCE. We can do:
148 * ASN1_SEQUENCE(stname) = {
149 * ... SEQUENCE components ...
150 * } ASN1_SEQUENCE_END(stname)
152 * This will produce an ASN1_ITEM called stname_it
153 * for a structure called stname.
155 * If you want the same structure but a different
158 * ASN1_SEQUENCE(itname) = {
159 * ... SEQUENCE components ...
160 * } ASN1_SEQUENCE_END_name(stname, itname)
162 * This will create an item called itname_it using
163 * a structure called stname.
166 # define ASN1_SEQUENCE(tname) \
167 static const ASN1_TEMPLATE tname##_seq_tt[]
169 # define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
171 # define static_ASN1_SEQUENCE_END(stname) static_ASN1_SEQUENCE_END_name(stname, stname)
173 # define ASN1_SEQUENCE_END_name(stname, tname) \
175 ASN1_ITEM_start(tname) \
176 ASN1_ITYPE_SEQUENCE,\
179 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
185 # define static_ASN1_SEQUENCE_END_name(stname, tname) \
187 static_ASN1_ITEM_start(tname) \
188 ASN1_ITYPE_SEQUENCE,\
191 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
197 # define ASN1_NDEF_SEQUENCE(tname) \
200 # define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
201 ASN1_SEQUENCE_cb(tname, cb)
203 # define ASN1_SEQUENCE_cb(tname, cb) \
204 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
207 # define ASN1_BROKEN_SEQUENCE(tname) \
208 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
211 # define ASN1_SEQUENCE_ref(tname, cb, lck) \
212 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
215 # define ASN1_SEQUENCE_enc(tname, enc, cb) \
216 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
219 # define ASN1_NDEF_SEQUENCE_END(tname) \
221 ASN1_ITEM_start(tname) \
222 ASN1_ITYPE_NDEF_SEQUENCE,\
225 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
230 # define static_ASN1_NDEF_SEQUENCE_END(tname) \
232 static_ASN1_ITEM_start(tname) \
233 ASN1_ITYPE_NDEF_SEQUENCE,\
236 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
242 # define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
243 # define static_ASN1_BROKEN_SEQUENCE_END(stname) \
244 static_ASN1_SEQUENCE_END_ref(stname, stname)
246 # define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
248 # define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
249 # define static_ASN1_SEQUENCE_END_cb(stname, tname) static_ASN1_SEQUENCE_END_ref(stname, tname)
251 # define ASN1_SEQUENCE_END_ref(stname, tname) \
253 ASN1_ITEM_start(tname) \
254 ASN1_ITYPE_SEQUENCE,\
257 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
262 # define static_ASN1_SEQUENCE_END_ref(stname, tname) \
264 static_ASN1_ITEM_start(tname) \
265 ASN1_ITYPE_SEQUENCE,\
268 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
274 # define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
276 ASN1_ITEM_start(tname) \
277 ASN1_ITYPE_NDEF_SEQUENCE,\
280 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
287 * This pair helps declare a CHOICE type. We can do:
289 * ASN1_CHOICE(chname) = {
290 * ... CHOICE options ...
291 * ASN1_CHOICE_END(chname)
293 * This will produce an ASN1_ITEM called chname_it
294 * for a structure called chname. The structure
295 * definition must look like this:
299 * ASN1_SOMETHING *opt1;
300 * ASN1_SOMEOTHER *opt2;
304 * the name of the selector must be 'type'.
305 * to use an alternative selector name use the
306 * ASN1_CHOICE_END_selector() version.
309 # define ASN1_CHOICE(tname) \
310 static const ASN1_TEMPLATE tname##_ch_tt[]
312 # define ASN1_CHOICE_cb(tname, cb) \
313 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
316 # define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
318 # define static_ASN1_CHOICE_END(stname) static_ASN1_CHOICE_END_name(stname, stname)
320 # define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
322 # define static_ASN1_CHOICE_END_name(stname, tname) static_ASN1_CHOICE_END_selector(stname, tname, type)
324 # define ASN1_CHOICE_END_selector(stname, tname, selname) \
326 ASN1_ITEM_start(tname) \
328 offsetof(stname,selname) ,\
330 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
336 # define static_ASN1_CHOICE_END_selector(stname, tname, selname) \
338 static_ASN1_ITEM_start(tname) \
340 offsetof(stname,selname) ,\
342 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
348 # define ASN1_CHOICE_END_cb(stname, tname, selname) \
350 ASN1_ITEM_start(tname) \
352 offsetof(stname,selname) ,\
354 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
360 /* This helps with the template wrapper form of ASN1_ITEM */
362 # define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
364 #name, ASN1_ITEM_ref(type) }
366 /* These help with SEQUENCE or CHOICE components */
368 /* used to declare other types */
370 # define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
371 (flags), (tag), offsetof(stname, field),\
372 #field, ASN1_ITEM_ref(type) }
374 /* implicit and explicit helper macros */
376 # define ASN1_IMP_EX(stname, field, type, tag, ex) \
377 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
379 # define ASN1_EXP_EX(stname, field, type, tag, ex) \
380 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
382 /* Any defined by macros: the field used is in the table itself */
384 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
385 # define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
386 # define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
388 # define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
389 # define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
391 /* Plain simple type */
392 # define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
394 /* OPTIONAL simple type */
395 # define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
397 /* IMPLICIT tagged simple type */
398 # define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
400 /* IMPLICIT tagged OPTIONAL simple type */
401 # define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
403 /* Same as above but EXPLICIT */
405 # define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
406 # define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
408 /* SEQUENCE OF type */
409 # define ASN1_SEQUENCE_OF(stname, field, type) \
410 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
412 /* OPTIONAL SEQUENCE OF */
413 # define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
414 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
416 /* Same as above but for SET OF */
418 # define ASN1_SET_OF(stname, field, type) \
419 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
421 # define ASN1_SET_OF_OPT(stname, field, type) \
422 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
424 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
426 # define ASN1_IMP_SET_OF(stname, field, type, tag) \
427 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
429 # define ASN1_EXP_SET_OF(stname, field, type, tag) \
430 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
432 # define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
433 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
435 # define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
436 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
438 # define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
439 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
441 # define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
442 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
444 # define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
445 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
447 # define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
448 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
450 /* EXPLICIT using indefinite length constructed form */
451 # define ASN1_NDEF_EXP(stname, field, type, tag) \
452 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
454 /* EXPLICIT OPTIONAL using indefinite length constructed form */
455 # define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
456 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
458 /* Macros for the ASN1_ADB structure */
460 # define ASN1_ADB(name) \
461 static const ASN1_ADB_TABLE name##_adbtbl[]
463 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
465 # define ASN1_ADB_END(name, flags, field, app_table, def, none) \
467 static const ASN1_ADB name##_adb = {\
469 offsetof(name, field),\
472 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
479 # define ASN1_ADB_END(name, flags, field, app_table, def, none) \
481 static const ASN1_ITEM *name##_adb(void) \
483 static const ASN1_ADB internal_adb = \
486 offsetof(name, field),\
489 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
493 return (const ASN1_ITEM *) &internal_adb; \
495 void dummy_function(void)
499 # define ADB_ENTRY(val, template) {val, template}
501 # define ASN1_ADB_TEMPLATE(name) \
502 static const ASN1_TEMPLATE name##_tt
505 * This is the ASN1 template structure that defines a wrapper round the
506 * actual type. It determines the actual position of the field in the value
507 * structure, various flags such as OPTIONAL and the field name.
510 struct ASN1_TEMPLATE_st {
511 unsigned long flags; /* Various flags */
512 long tag; /* tag, not used if no tagging */
513 unsigned long offset; /* Offset of this field in structure */
514 # ifndef NO_ASN1_FIELD_NAMES
515 const char *field_name; /* Field name */
517 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
520 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
522 # define ASN1_TEMPLATE_item(t) (t->item_ptr)
523 # define ASN1_TEMPLATE_adb(t) (t->item_ptr)
525 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
526 typedef struct ASN1_ADB_st ASN1_ADB;
529 unsigned long flags; /* Various flags */
530 unsigned long offset; /* Offset of selector field */
531 STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
532 const ASN1_ADB_TABLE *tbl; /* Table of possible types */
533 long tblcount; /* Number of entries in tbl */
534 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
535 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
538 struct ASN1_ADB_TABLE_st {
539 long value; /* NID for an object or value for an int */
540 const ASN1_TEMPLATE tt; /* item for this value */
545 /* Field is optional */
546 # define ASN1_TFLG_OPTIONAL (0x1)
548 /* Field is a SET OF */
549 # define ASN1_TFLG_SET_OF (0x1 << 1)
551 /* Field is a SEQUENCE OF */
552 # define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
555 * Special case: this refers to a SET OF that will be sorted into DER order
556 * when encoded *and* the corresponding STACK will be modified to match the
559 # define ASN1_TFLG_SET_ORDER (0x3 << 1)
561 /* Mask for SET OF or SEQUENCE OF */
562 # define ASN1_TFLG_SK_MASK (0x3 << 1)
565 * These flags mean the tag should be taken from the tag field. If EXPLICIT
566 * then the underlying type is used for the inner tag.
569 /* IMPLICIT tagging */
570 # define ASN1_TFLG_IMPTAG (0x1 << 3)
572 /* EXPLICIT tagging, inner tag from underlying type */
573 # define ASN1_TFLG_EXPTAG (0x2 << 3)
575 # define ASN1_TFLG_TAG_MASK (0x3 << 3)
577 /* context specific IMPLICIT */
578 # define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
580 /* context specific EXPLICIT */
581 # define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
584 * If tagging is in force these determine the type of tag to use. Otherwise
585 * the tag is determined by the underlying type. These values reflect the
586 * actual octet format.
590 # define ASN1_TFLG_UNIVERSAL (0x0<<6)
591 /* Application tag */
592 # define ASN1_TFLG_APPLICATION (0x1<<6)
593 /* Context specific tag */
594 # define ASN1_TFLG_CONTEXT (0x2<<6)
596 # define ASN1_TFLG_PRIVATE (0x3<<6)
598 # define ASN1_TFLG_TAG_CLASS (0x3<<6)
601 * These are for ANY DEFINED BY type. In this case the 'item' field points to
602 * an ASN1_ADB structure which contains a table of values to decode the
606 # define ASN1_TFLG_ADB_MASK (0x3<<8)
608 # define ASN1_TFLG_ADB_OID (0x1<<8)
610 # define ASN1_TFLG_ADB_INT (0x1<<9)
613 * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes
614 * indefinite length constructed encoding to be used if required.
617 # define ASN1_TFLG_NDEF (0x1<<11)
619 /* This is the actual ASN1 item itself */
621 struct ASN1_ITEM_st {
622 char itype; /* The item type, primitive, SEQUENCE, CHOICE
624 long utype; /* underlying type */
625 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains
627 long tcount; /* Number of templates if SEQUENCE or CHOICE */
628 const void *funcs; /* functions that handle this type */
629 long size; /* Structure size (usually) */
630 # ifndef NO_ASN1_FIELD_NAMES
631 const char *sname; /* Structure name */
636 * These are values for the itype field and
637 * determine how the type is interpreted.
639 * For PRIMITIVE types the underlying type
640 * determines the behaviour if items is NULL.
642 * Otherwise templates must contain a single
643 * template and the type is treated in the
644 * same way as the type specified in the template.
646 * For SEQUENCE types the templates field points
647 * to the members, the size field is the
650 * For CHOICE types the templates field points
651 * to each possible member (typically a union)
652 * and the 'size' field is the offset of the
655 * The 'funcs' field is used for application
656 * specific functions.
658 * The EXTERN type uses a new style d2i/i2d.
659 * The new style should be used where possible
660 * because it avoids things like the d2i IMPLICIT
663 * MSTRING is a multiple string type, it is used
664 * for a CHOICE of character strings where the
665 * actual strings all occupy an ASN1_STRING
666 * structure. In this case the 'utype' field
667 * has a special meaning, it is used as a mask
668 * of acceptable types using the B_ASN1 constants.
670 * NDEF_SEQUENCE is the same as SEQUENCE except
671 * that it will use indefinite length constructed
672 * encoding if requested.
676 # define ASN1_ITYPE_PRIMITIVE 0x0
678 # define ASN1_ITYPE_SEQUENCE 0x1
680 # define ASN1_ITYPE_CHOICE 0x2
682 # define ASN1_ITYPE_EXTERN 0x4
684 # define ASN1_ITYPE_MSTRING 0x5
686 # define ASN1_ITYPE_NDEF_SEQUENCE 0x6
689 * Cache for ASN1 tag and length, so we don't keep re-reading it for things
694 char valid; /* Values below are valid */
695 int ret; /* return value */
696 long plen; /* length */
697 int ptag; /* class value */
698 int pclass; /* class value */
699 int hdrlen; /* header length */
702 /* Typedefs for ASN1 function pointers */
704 typedef ASN1_VALUE *ASN1_new_func(void);
705 typedef void ASN1_free_func(ASN1_VALUE *a);
706 typedef ASN1_VALUE *ASN1_d2i_func(ASN1_VALUE **a, const unsigned char **in,
708 typedef int ASN1_i2d_func(ASN1_VALUE *a, unsigned char **in);
710 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
711 const ASN1_ITEM *it, int tag, int aclass, char opt,
714 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
715 const ASN1_ITEM *it, int tag, int aclass);
716 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
717 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
719 typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
720 int indent, const char *fname,
721 const ASN1_PCTX *pctx);
723 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont,
724 int *putype, const ASN1_ITEM *it);
725 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
726 int len, int utype, char *free_cont,
727 const ASN1_ITEM *it);
728 typedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval,
729 const ASN1_ITEM *it, int indent,
730 const ASN1_PCTX *pctx);
732 typedef struct ASN1_EXTERN_FUNCS_st {
734 ASN1_ex_new_func *asn1_ex_new;
735 ASN1_ex_free_func *asn1_ex_free;
736 ASN1_ex_free_func *asn1_ex_clear;
737 ASN1_ex_d2i *asn1_ex_d2i;
738 ASN1_ex_i2d *asn1_ex_i2d;
739 ASN1_ex_print_func *asn1_ex_print;
742 typedef struct ASN1_PRIMITIVE_FUNCS_st {
745 ASN1_ex_new_func *prim_new;
746 ASN1_ex_free_func *prim_free;
747 ASN1_ex_free_func *prim_clear;
748 ASN1_primitive_c2i *prim_c2i;
749 ASN1_primitive_i2c *prim_i2c;
750 ASN1_primitive_print *prim_print;
751 } ASN1_PRIMITIVE_FUNCS;
754 * This is the ASN1_AUX structure: it handles various miscellaneous
755 * requirements. For example the use of reference counts and an informational
756 * callback. The "informational callback" is called at various points during
757 * the ASN1 encoding and decoding. It can be used to provide minor
758 * customisation of the structures used. This is most useful where the
759 * supplied routines *almost* do the right thing but need some extra help at
760 * a few points. If the callback returns zero then it is assumed a fatal
761 * error has occurred and the main operation should be abandoned. If major
762 * changes in the default behaviour are required then an external type is
766 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
769 typedef struct ASN1_AUX_st {
772 int ref_offset; /* Offset of reference value */
773 int ref_lock; /* Lock type to use */
774 ASN1_aux_cb *asn1_cb;
775 int enc_offset; /* Offset of ASN1_ENCODING structure */
778 /* For print related callbacks exarg points to this structure */
779 typedef struct ASN1_PRINT_ARG_st {
782 const ASN1_PCTX *pctx;
785 /* For streaming related callbacks exarg points to this structure */
786 typedef struct ASN1_STREAM_ARG_st {
787 /* BIO to stream through */
789 /* BIO with filters appended */
791 /* Streaming I/O boundary */
792 unsigned char **boundary;
795 /* Flags in ASN1_AUX */
797 /* Use a reference count */
798 # define ASN1_AFLG_REFCOUNT 1
799 /* Save the encoding of structure (useful for signatures) */
800 # define ASN1_AFLG_ENCODING 2
801 /* The Sequence length is invalid */
802 # define ASN1_AFLG_BROKEN 4
804 /* operation values for asn1_cb */
806 # define ASN1_OP_NEW_PRE 0
807 # define ASN1_OP_NEW_POST 1
808 # define ASN1_OP_FREE_PRE 2
809 # define ASN1_OP_FREE_POST 3
810 # define ASN1_OP_D2I_PRE 4
811 # define ASN1_OP_D2I_POST 5
812 # define ASN1_OP_I2D_PRE 6
813 # define ASN1_OP_I2D_POST 7
814 # define ASN1_OP_PRINT_PRE 8
815 # define ASN1_OP_PRINT_POST 9
816 # define ASN1_OP_STREAM_PRE 10
817 # define ASN1_OP_STREAM_POST 11
818 # define ASN1_OP_DETACHED_PRE 12
819 # define ASN1_OP_DETACHED_POST 13
821 /* Macro to implement a primitive type */
822 # define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
823 # define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
824 ASN1_ITEM_start(itname) \
825 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
826 ASN1_ITEM_end(itname)
828 /* Macro to implement a multi string type */
829 # define IMPLEMENT_ASN1_MSTRING(itname, mask) \
830 ASN1_ITEM_start(itname) \
831 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
832 ASN1_ITEM_end(itname)
834 # define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
835 ASN1_ITEM_start(sname) \
845 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
847 # define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
849 # define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
851 # define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
852 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
854 # define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
855 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
857 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
858 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
860 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
861 pre stname *fname##_new(void) \
863 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
865 pre void fname##_free(stname *a) \
867 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
870 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
871 stname *fname##_new(void) \
873 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
875 void fname##_free(stname *a) \
877 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
880 # define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
881 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
882 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
884 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
885 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
887 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
889 int i2d_##fname(stname *a, unsigned char **out) \
891 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
894 # define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
895 int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
897 return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
900 # define IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(stname) \
901 static stname *d2i_##stname(stname **a, \
902 const unsigned char **in, long len) \
904 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \
905 ASN1_ITEM_rptr(stname)); \
907 static int i2d_##stname(stname *a, unsigned char **out) \
909 return ASN1_item_i2d((ASN1_VALUE *)a, out, \
910 ASN1_ITEM_rptr(stname)); \
914 * This includes evil casts to remove const: they will go away when full ASN1
915 * constification is done.
917 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
918 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
920 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
922 int i2d_##fname(const stname *a, unsigned char **out) \
924 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
927 # define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
928 stname * stname##_dup(stname *x) \
930 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
933 # define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
934 IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
936 # define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
937 int fname##_print_ctx(BIO *out, stname *x, int indent, \
938 const ASN1_PCTX *pctx) \
940 return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \
941 ASN1_ITEM_rptr(itname), pctx); \
944 # define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
945 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
947 # define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
948 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
949 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
951 /* external definitions for primitive types */
953 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
954 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
955 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
956 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
957 DECLARE_ASN1_ITEM(CBIGNUM)
958 DECLARE_ASN1_ITEM(BIGNUM)
959 DECLARE_ASN1_ITEM(LONG)
960 DECLARE_ASN1_ITEM(ZLONG)
962 DECLARE_STACK_OF(ASN1_VALUE)
964 /* Functions used internally by the ASN1 code */
966 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
967 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
969 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
970 const ASN1_ITEM *it, int tag, int aclass, char opt,
973 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
974 const ASN1_ITEM *it, int tag, int aclass);