2 * {- join("\n * ", @autowarntext) -}
4 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
6 * Licensed under the Apache License 2.0 (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
13 use OpenSSL::stackhash qw(generate_stack_macros);
16 #ifndef OPENSSL_ASN1T_H
17 # define OPENSSL_ASN1T_H
20 # include <openssl/macros.h>
21 # ifndef OPENSSL_NO_DEPRECATED_3_0
22 # define HEADER_ASN1T_H
26 # include <openssl/e_os2.h>
27 # include <openssl/asn1.h>
29 # ifdef OPENSSL_BUILD_SHLIBCRYPTO
30 # undef OPENSSL_EXTERN
31 # define OPENSSL_EXTERN OPENSSL_EXPORT
34 /* ASN1 template defines, structures and functions */
40 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
41 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)((iptr)()))
43 /* Macros for start and end of ASN1_ITEM definition */
45 # define ASN1_ITEM_start(itname) \
46 const ASN1_ITEM * itname##_it(void) \
48 static const ASN1_ITEM local_it = {
50 # define static_ASN1_ITEM_start(itname) \
51 static ASN1_ITEM_start(itname)
53 # define ASN1_ITEM_end(itname) \
58 /* Macros to aid ASN1 template writing */
60 # define ASN1_ITEM_TEMPLATE(tname) \
61 static const ASN1_TEMPLATE tname##_item_tt
63 # define ASN1_ITEM_TEMPLATE_END(tname) \
65 ASN1_ITEM_start(tname) \
66 ASN1_ITYPE_PRIMITIVE,\
74 # define static_ASN1_ITEM_TEMPLATE_END(tname) \
76 static_ASN1_ITEM_start(tname) \
77 ASN1_ITYPE_PRIMITIVE,\
86 /* This is a ASN1 type which just embeds a template */
89 * This pair helps declare a SEQUENCE. We can do:
91 * ASN1_SEQUENCE(stname) = {
92 * ... SEQUENCE components ...
93 * } ASN1_SEQUENCE_END(stname)
95 * This will produce an ASN1_ITEM called stname_it
96 * for a structure called stname.
98 * If you want the same structure but a different
101 * ASN1_SEQUENCE(itname) = {
102 * ... SEQUENCE components ...
103 * } ASN1_SEQUENCE_END_name(stname, itname)
105 * This will create an item called itname_it using
106 * a structure called stname.
109 # define ASN1_SEQUENCE(tname) \
110 static const ASN1_TEMPLATE tname##_seq_tt[]
112 # define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
114 # define static_ASN1_SEQUENCE_END(stname) static_ASN1_SEQUENCE_END_name(stname, stname)
116 # define ASN1_SEQUENCE_END_name(stname, tname) \
118 ASN1_ITEM_start(tname) \
119 ASN1_ITYPE_SEQUENCE,\
122 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
128 # define static_ASN1_SEQUENCE_END_name(stname, tname) \
130 static_ASN1_ITEM_start(tname) \
131 ASN1_ITYPE_SEQUENCE,\
134 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
140 # define ASN1_NDEF_SEQUENCE(tname) \
143 # define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
144 ASN1_SEQUENCE_cb(tname, cb)
146 # define ASN1_SEQUENCE_cb(tname, cb) \
147 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0, NULL}; \
150 # define ASN1_SEQUENCE_const_cb(tname, const_cb) \
151 static const ASN1_AUX tname##_aux = \
152 {NULL, ASN1_AFLG_CONST_CB, 0, 0, NULL, 0, const_cb}; \
155 # define ASN1_SEQUENCE_cb_const_cb(tname, cb, const_cb) \
156 static const ASN1_AUX tname##_aux = \
157 {NULL, ASN1_AFLG_CONST_CB, 0, 0, cb, 0, const_cb}; \
160 # define ASN1_SEQUENCE_ref(tname, cb) \
161 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), offsetof(tname, lock), cb, 0, NULL}; \
164 # define ASN1_SEQUENCE_enc(tname, enc, cb) \
165 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc), NULL}; \
168 # define ASN1_NDEF_SEQUENCE_END(tname) \
170 ASN1_ITEM_start(tname) \
171 ASN1_ITYPE_NDEF_SEQUENCE,\
174 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
179 # define static_ASN1_NDEF_SEQUENCE_END(tname) \
181 static_ASN1_ITEM_start(tname) \
182 ASN1_ITYPE_NDEF_SEQUENCE,\
185 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
192 # define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
194 # define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
195 # define static_ASN1_SEQUENCE_END_cb(stname, tname) static_ASN1_SEQUENCE_END_ref(stname, tname)
197 # define ASN1_SEQUENCE_END_ref(stname, tname) \
199 ASN1_ITEM_start(tname) \
200 ASN1_ITYPE_SEQUENCE,\
203 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
208 # define static_ASN1_SEQUENCE_END_ref(stname, tname) \
210 static_ASN1_ITEM_start(tname) \
211 ASN1_ITYPE_SEQUENCE,\
214 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
220 # define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
222 ASN1_ITEM_start(tname) \
223 ASN1_ITYPE_NDEF_SEQUENCE,\
226 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
233 * This pair helps declare a CHOICE type. We can do:
235 * ASN1_CHOICE(chname) = {
236 * ... CHOICE options ...
237 * ASN1_CHOICE_END(chname)
239 * This will produce an ASN1_ITEM called chname_it
240 * for a structure called chname. The structure
241 * definition must look like this:
245 * ASN1_SOMETHING *opt1;
246 * ASN1_SOMEOTHER *opt2;
250 * the name of the selector must be 'type'.
251 * to use an alternative selector name use the
252 * ASN1_CHOICE_END_selector() version.
255 # define ASN1_CHOICE(tname) \
256 static const ASN1_TEMPLATE tname##_ch_tt[]
258 # define ASN1_CHOICE_cb(tname, cb) \
259 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0, NULL}; \
262 # define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
264 # define static_ASN1_CHOICE_END(stname) static_ASN1_CHOICE_END_name(stname, stname)
266 # define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
268 # define static_ASN1_CHOICE_END_name(stname, tname) static_ASN1_CHOICE_END_selector(stname, tname, type)
270 # define ASN1_CHOICE_END_selector(stname, tname, selname) \
272 ASN1_ITEM_start(tname) \
274 offsetof(stname,selname) ,\
276 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
282 # define static_ASN1_CHOICE_END_selector(stname, tname, selname) \
284 static_ASN1_ITEM_start(tname) \
286 offsetof(stname,selname) ,\
288 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
294 # define ASN1_CHOICE_END_cb(stname, tname, selname) \
296 ASN1_ITEM_start(tname) \
298 offsetof(stname,selname) ,\
300 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
306 /* This helps with the template wrapper form of ASN1_ITEM */
308 # define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
310 #name, ASN1_ITEM_ref(type) }
312 /* These help with SEQUENCE or CHOICE components */
314 /* used to declare other types */
316 # define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
317 (flags), (tag), offsetof(stname, field),\
318 #field, ASN1_ITEM_ref(type) }
320 /* implicit and explicit helper macros */
322 # define ASN1_IMP_EX(stname, field, type, tag, ex) \
323 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | (ex), tag, stname, field, type)
325 # define ASN1_EXP_EX(stname, field, type, tag, ex) \
326 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | (ex), tag, stname, field, type)
328 /* Any defined by macros: the field used is in the table itself */
330 # define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
331 # define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
333 /* Plain simple type */
334 # define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
335 /* Embedded simple type */
336 # define ASN1_EMBED(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_EMBED,0, stname, field, type)
338 /* OPTIONAL simple type */
339 # define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
340 # define ASN1_OPT_EMBED(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED, 0, stname, field, type)
342 /* IMPLICIT tagged simple type */
343 # define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
344 # define ASN1_IMP_EMBED(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_EMBED)
346 /* IMPLICIT tagged OPTIONAL simple type */
347 # define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
348 # define ASN1_IMP_OPT_EMBED(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED)
350 /* Same as above but EXPLICIT */
352 # define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
353 # define ASN1_EXP_EMBED(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_EMBED)
354 # define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
355 # define ASN1_EXP_OPT_EMBED(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED)
357 /* SEQUENCE OF type */
358 # define ASN1_SEQUENCE_OF(stname, field, type) \
359 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
361 /* OPTIONAL SEQUENCE OF */
362 # define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
363 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
365 /* Same as above but for SET OF */
367 # define ASN1_SET_OF(stname, field, type) \
368 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
370 # define ASN1_SET_OF_OPT(stname, field, type) \
371 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
373 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
375 # define ASN1_IMP_SET_OF(stname, field, type, tag) \
376 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
378 # define ASN1_EXP_SET_OF(stname, field, type, tag) \
379 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
381 # define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
382 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
384 # define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
385 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
387 # define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
388 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
390 # define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
391 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
393 # define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
394 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
396 # define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
397 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
399 /* EXPLICIT using indefinite length constructed form */
400 # define ASN1_NDEF_EXP(stname, field, type, tag) \
401 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
403 /* EXPLICIT OPTIONAL using indefinite length constructed form */
404 # define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
405 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
407 /* Macros for the ASN1_ADB structure */
409 # define ASN1_ADB(name) \
410 static const ASN1_ADB_TABLE name##_adbtbl[]
412 # define ASN1_ADB_END(name, flags, field, adb_cb, def, none) \
414 static const ASN1_ITEM *name##_adb(void) \
416 static const ASN1_ADB internal_adb = \
419 offsetof(name, field),\
422 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
426 return (const ASN1_ITEM *) &internal_adb; \
428 void dummy_function(void)
430 # define ADB_ENTRY(val, template) {val, template}
432 # define ASN1_ADB_TEMPLATE(name) \
433 static const ASN1_TEMPLATE name##_tt
436 * This is the ASN1 template structure that defines a wrapper round the
437 * actual type. It determines the actual position of the field in the value
438 * structure, various flags such as OPTIONAL and the field name.
441 struct ASN1_TEMPLATE_st {
442 unsigned long flags; /* Various flags */
443 long tag; /* tag, not used if no tagging */
444 unsigned long offset; /* Offset of this field in structure */
445 const char *field_name; /* Field name */
446 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
449 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
451 # define ASN1_TEMPLATE_item(t) (t->item_ptr)
452 # define ASN1_TEMPLATE_adb(t) (t->item_ptr)
454 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
455 typedef struct ASN1_ADB_st ASN1_ADB;
458 unsigned long flags; /* Various flags */
459 unsigned long offset; /* Offset of selector field */
460 int (*adb_cb)(long *psel); /* Application callback */
461 const ASN1_ADB_TABLE *tbl; /* Table of possible types */
462 long tblcount; /* Number of entries in tbl */
463 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
464 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
467 struct ASN1_ADB_TABLE_st {
468 long value; /* NID for an object or value for an int */
469 const ASN1_TEMPLATE tt; /* item for this value */
474 /* Field is optional */
475 # define ASN1_TFLG_OPTIONAL (0x1)
477 /* Field is a SET OF */
478 # define ASN1_TFLG_SET_OF (0x1 << 1)
480 /* Field is a SEQUENCE OF */
481 # define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
484 * Special case: this refers to a SET OF that will be sorted into DER order
485 * when encoded *and* the corresponding STACK will be modified to match the
488 # define ASN1_TFLG_SET_ORDER (0x3 << 1)
490 /* Mask for SET OF or SEQUENCE OF */
491 # define ASN1_TFLG_SK_MASK (0x3 << 1)
494 * These flags mean the tag should be taken from the tag field. If EXPLICIT
495 * then the underlying type is used for the inner tag.
498 /* IMPLICIT tagging */
499 # define ASN1_TFLG_IMPTAG (0x1 << 3)
501 /* EXPLICIT tagging, inner tag from underlying type */
502 # define ASN1_TFLG_EXPTAG (0x2 << 3)
504 # define ASN1_TFLG_TAG_MASK (0x3 << 3)
506 /* context specific IMPLICIT */
507 # define ASN1_TFLG_IMPLICIT (ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT)
509 /* context specific EXPLICIT */
510 # define ASN1_TFLG_EXPLICIT (ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT)
513 * If tagging is in force these determine the type of tag to use. Otherwise
514 * the tag is determined by the underlying type. These values reflect the
515 * actual octet format.
519 # define ASN1_TFLG_UNIVERSAL (0x0<<6)
520 /* Application tag */
521 # define ASN1_TFLG_APPLICATION (0x1<<6)
522 /* Context specific tag */
523 # define ASN1_TFLG_CONTEXT (0x2<<6)
525 # define ASN1_TFLG_PRIVATE (0x3<<6)
527 # define ASN1_TFLG_TAG_CLASS (0x3<<6)
530 * These are for ANY DEFINED BY type. In this case the 'item' field points to
531 * an ASN1_ADB structure which contains a table of values to decode the
535 # define ASN1_TFLG_ADB_MASK (0x3<<8)
537 # define ASN1_TFLG_ADB_OID (0x1<<8)
539 # define ASN1_TFLG_ADB_INT (0x1<<9)
542 * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes
543 * indefinite length constructed encoding to be used if required.
546 # define ASN1_TFLG_NDEF (0x1<<11)
548 /* Field is embedded and not a pointer */
549 # define ASN1_TFLG_EMBED (0x1 << 12)
551 /* This is the actual ASN1 item itself */
553 struct ASN1_ITEM_st {
554 char itype; /* The item type, primitive, SEQUENCE, CHOICE
556 long utype; /* underlying type */
557 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains
559 long tcount; /* Number of templates if SEQUENCE or CHOICE */
560 const void *funcs; /* functions that handle this type */
561 long size; /* Structure size (usually) */
562 const char *sname; /* Structure name */
566 * These are values for the itype field and
567 * determine how the type is interpreted.
569 * For PRIMITIVE types the underlying type
570 * determines the behaviour if items is NULL.
572 * Otherwise templates must contain a single
573 * template and the type is treated in the
574 * same way as the type specified in the template.
576 * For SEQUENCE types the templates field points
577 * to the members, the size field is the
580 * For CHOICE types the templates field points
581 * to each possible member (typically a union)
582 * and the 'size' field is the offset of the
585 * The 'funcs' field is used for application
586 * specific functions.
588 * The EXTERN type uses a new style d2i/i2d.
589 * The new style should be used where possible
590 * because it avoids things like the d2i IMPLICIT
593 * MSTRING is a multiple string type, it is used
594 * for a CHOICE of character strings where the
595 * actual strings all occupy an ASN1_STRING
596 * structure. In this case the 'utype' field
597 * has a special meaning, it is used as a mask
598 * of acceptable types using the B_ASN1 constants.
600 * NDEF_SEQUENCE is the same as SEQUENCE except
601 * that it will use indefinite length constructed
602 * encoding if requested.
606 # define ASN1_ITYPE_PRIMITIVE 0x0
608 # define ASN1_ITYPE_SEQUENCE 0x1
610 # define ASN1_ITYPE_CHOICE 0x2
612 # define ASN1_ITYPE_EXTERN 0x4
614 # define ASN1_ITYPE_MSTRING 0x5
616 # define ASN1_ITYPE_NDEF_SEQUENCE 0x6
619 * Cache for ASN1 tag and length, so we don't keep re-reading it for things
624 char valid; /* Values below are valid */
625 int ret; /* return value */
626 long plen; /* length */
627 int ptag; /* class value */
628 int pclass; /* class value */
629 int hdrlen; /* header length */
632 /* Typedefs for ASN1 function pointers */
633 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
634 const ASN1_ITEM *it, int tag, int aclass, char opt,
637 typedef int ASN1_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
638 const ASN1_ITEM *it, int tag, int aclass);
639 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
640 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
642 typedef int ASN1_ex_print_func(BIO *out, const ASN1_VALUE **pval,
643 int indent, const char *fname,
644 const ASN1_PCTX *pctx);
646 typedef int ASN1_primitive_i2c(const ASN1_VALUE **pval, unsigned char *cont,
647 int *putype, const ASN1_ITEM *it);
648 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
649 int len, int utype, char *free_cont,
650 const ASN1_ITEM *it);
651 typedef int ASN1_primitive_print(BIO *out, const ASN1_VALUE **pval,
652 const ASN1_ITEM *it, int indent,
653 const ASN1_PCTX *pctx);
655 typedef struct ASN1_EXTERN_FUNCS_st {
657 ASN1_ex_new_func *asn1_ex_new;
658 ASN1_ex_free_func *asn1_ex_free;
659 ASN1_ex_free_func *asn1_ex_clear;
660 ASN1_ex_d2i *asn1_ex_d2i;
661 ASN1_ex_i2d *asn1_ex_i2d;
662 ASN1_ex_print_func *asn1_ex_print;
665 typedef struct ASN1_PRIMITIVE_FUNCS_st {
668 ASN1_ex_new_func *prim_new;
669 ASN1_ex_free_func *prim_free;
670 ASN1_ex_free_func *prim_clear;
671 ASN1_primitive_c2i *prim_c2i;
672 ASN1_primitive_i2c *prim_i2c;
673 ASN1_primitive_print *prim_print;
674 } ASN1_PRIMITIVE_FUNCS;
677 * This is the ASN1_AUX structure: it handles various miscellaneous
678 * requirements. For example the use of reference counts and an informational
679 * callback. The "informational callback" is called at various points during
680 * the ASN1 encoding and decoding. It can be used to provide minor
681 * customisation of the structures used. This is most useful where the
682 * supplied routines *almost* do the right thing but need some extra help at
683 * a few points. If the callback returns zero then it is assumed a fatal
684 * error has occurred and the main operation should be abandoned. If major
685 * changes in the default behaviour are required then an external type is
687 * For the operations ASN1_OP_I2D_PRE, ASN1_OP_I2D_POST, ASN1_OP_PRINT_PRE, and
688 * ASN1_OP_PRINT_POST, meanwhile a variant of the callback with const parameter
689 * 'in' is provided to make clear statically that its input is not modified. If
690 * and only if this variant is in use the flag ASN1_AFLG_CONST_CB must be set.
693 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
695 typedef int ASN1_aux_const_cb(int operation, const ASN1_VALUE **in,
696 const ASN1_ITEM *it, void *exarg);
698 typedef struct ASN1_AUX_st {
701 int ref_offset; /* Offset of reference value */
702 int ref_lock; /* Lock type to use */
703 ASN1_aux_cb *asn1_cb;
704 int enc_offset; /* Offset of ASN1_ENCODING structure */
705 ASN1_aux_const_cb *asn1_const_cb; /* for ASN1_OP_I2D_ and ASN1_OP_PRINT_ */
708 /* For print related callbacks exarg points to this structure */
709 typedef struct ASN1_PRINT_ARG_st {
712 const ASN1_PCTX *pctx;
715 /* For streaming related callbacks exarg points to this structure */
716 typedef struct ASN1_STREAM_ARG_st {
717 /* BIO to stream through */
719 /* BIO with filters appended */
721 /* Streaming I/O boundary */
722 unsigned char **boundary;
725 /* Flags in ASN1_AUX */
727 /* Use a reference count */
728 # define ASN1_AFLG_REFCOUNT 1
729 /* Save the encoding of structure (useful for signatures) */
730 # define ASN1_AFLG_ENCODING 2
731 /* The Sequence length is invalid */
732 # define ASN1_AFLG_BROKEN 4
733 /* Use the new asn1_const_cb */
734 # define ASN1_AFLG_CONST_CB 8
736 /* operation values for asn1_cb */
738 # define ASN1_OP_NEW_PRE 0
739 # define ASN1_OP_NEW_POST 1
740 # define ASN1_OP_FREE_PRE 2
741 # define ASN1_OP_FREE_POST 3
742 # define ASN1_OP_D2I_PRE 4
743 # define ASN1_OP_D2I_POST 5
744 # define ASN1_OP_I2D_PRE 6
745 # define ASN1_OP_I2D_POST 7
746 # define ASN1_OP_PRINT_PRE 8
747 # define ASN1_OP_PRINT_POST 9
748 # define ASN1_OP_STREAM_PRE 10
749 # define ASN1_OP_STREAM_POST 11
750 # define ASN1_OP_DETACHED_PRE 12
751 # define ASN1_OP_DETACHED_POST 13
753 /* Macro to implement a primitive type */
754 # define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
755 # define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
756 ASN1_ITEM_start(itname) \
757 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
758 ASN1_ITEM_end(itname)
760 /* Macro to implement a multi string type */
761 # define IMPLEMENT_ASN1_MSTRING(itname, mask) \
762 ASN1_ITEM_start(itname) \
763 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
764 ASN1_ITEM_end(itname)
766 # define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
767 ASN1_ITEM_start(sname) \
777 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
779 # define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
781 # define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
783 # define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
784 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
786 # define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
787 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
789 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
790 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
792 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
793 pre stname *fname##_new(void) \
795 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
797 pre void fname##_free(stname *a) \
799 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
802 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
803 stname *fname##_new(void) \
805 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
807 void fname##_free(stname *a) \
809 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
812 # define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
813 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
814 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
816 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
817 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
819 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
821 int i2d_##fname(const stname *a, unsigned char **out) \
823 return ASN1_item_i2d((const ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
826 # define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
827 int i2d_##stname##_NDEF(const stname *a, unsigned char **out) \
829 return ASN1_item_ndef_i2d((const ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
832 # define IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(stname) \
833 static stname *d2i_##stname(stname **a, \
834 const unsigned char **in, long len) \
836 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \
837 ASN1_ITEM_rptr(stname)); \
839 static int i2d_##stname(const stname *a, unsigned char **out) \
841 return ASN1_item_i2d((const ASN1_VALUE *)a, out, \
842 ASN1_ITEM_rptr(stname)); \
845 # define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
846 stname * stname##_dup(const stname *x) \
848 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
851 # define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
852 IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
854 # define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
855 int fname##_print_ctx(BIO *out, const stname *x, int indent, \
856 const ASN1_PCTX *pctx) \
858 return ASN1_item_print(out, (const ASN1_VALUE *)x, indent, \
859 ASN1_ITEM_rptr(itname), pctx); \
862 /* external definitions for primitive types */
864 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
865 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
866 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
867 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
868 DECLARE_ASN1_ITEM(CBIGNUM)
869 DECLARE_ASN1_ITEM(BIGNUM)
870 DECLARE_ASN1_ITEM(INT32)
871 DECLARE_ASN1_ITEM(ZINT32)
872 DECLARE_ASN1_ITEM(UINT32)
873 DECLARE_ASN1_ITEM(ZUINT32)
874 DECLARE_ASN1_ITEM(INT64)
875 DECLARE_ASN1_ITEM(ZINT64)
876 DECLARE_ASN1_ITEM(UINT64)
877 DECLARE_ASN1_ITEM(ZUINT64)
879 # ifndef OPENSSL_NO_DEPRECATED_3_0
881 * LONG and ZLONG are strongly discouraged for use as stored data, as the
882 * underlying C type (long) differs in size depending on the architecture.
883 * They are designed with 32-bit longs in mind.
885 DECLARE_ASN1_ITEM(LONG)
886 DECLARE_ASN1_ITEM(ZLONG)
890 generate_stack_macros("ASN1_VALUE");
894 /* Functions used internally by the ASN1 code */
896 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
897 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
899 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
900 const ASN1_ITEM *it, int tag, int aclass, char opt,
903 int ASN1_item_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
904 const ASN1_ITEM *it, int tag, int aclass);
906 /* Legacy compatibility */
907 # define IMPLEMENT_ASN1_FUNCTIONS_const(name) IMPLEMENT_ASN1_FUNCTIONS(name)
908 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
909 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname)