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 */
41 * These are the possible values for the itype field of the
42 * ASN1_ITEM structure and determine how it is interpreted.
44 * For PRIMITIVE types the underlying type
45 * determines the behaviour if items is NULL.
47 * Otherwise templates must contain a single
48 * template and the type is treated in the
49 * same way as the type specified in the template.
51 * For SEQUENCE types the templates field points
52 * to the members, the size field is the
55 * For CHOICE types the templates field points
56 * to each possible member (typically a union)
57 * and the 'size' field is the offset of the
60 * The 'funcs' field is used for application-specific
63 * The EXTERN type uses a new style d2i/i2d.
64 * The new style should be used where possible
65 * because it avoids things like the d2i IMPLICIT
68 * MSTRING is a multiple string type, it is used
69 * for a CHOICE of character strings where the
70 * actual strings all occupy an ASN1_STRING
71 * structure. In this case the 'utype' field
72 * has a special meaning, it is used as a mask
73 * of acceptable types using the B_ASN1 constants.
75 * NDEF_SEQUENCE is the same as SEQUENCE except
76 * that it will use indefinite length constructed
77 * encoding if requested.
81 # define ASN1_ITYPE_PRIMITIVE 0x0
82 # define ASN1_ITYPE_SEQUENCE 0x1
83 # define ASN1_ITYPE_CHOICE 0x2
84 /* unused value 0x3 */
85 # define ASN1_ITYPE_EXTERN 0x4
86 # define ASN1_ITYPE_MSTRING 0x5
87 # define ASN1_ITYPE_NDEF_SEQUENCE 0x6
89 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
90 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)((iptr)()))
92 /* Macros for start and end of ASN1_ITEM definition */
94 # define ASN1_ITEM_start(itname) \
95 const ASN1_ITEM * itname##_it(void) \
97 static const ASN1_ITEM local_it = {
99 # define static_ASN1_ITEM_start(itname) \
100 static ASN1_ITEM_start(itname)
102 # define ASN1_ITEM_end(itname) \
107 /* Macros to aid ASN1 template writing */
109 # define ASN1_ITEM_TEMPLATE(tname) \
110 static const ASN1_TEMPLATE tname##_item_tt
112 # define ASN1_ITEM_TEMPLATE_END(tname) \
114 ASN1_ITEM_start(tname) \
115 ASN1_ITYPE_PRIMITIVE,\
123 # define static_ASN1_ITEM_TEMPLATE_END(tname) \
125 static_ASN1_ITEM_start(tname) \
126 ASN1_ITYPE_PRIMITIVE,\
135 /* This is a ASN1 type which just embeds a template */
138 * This pair helps declare a SEQUENCE. We can do:
140 * ASN1_SEQUENCE(stname) = {
141 * ... SEQUENCE components ...
142 * } ASN1_SEQUENCE_END(stname)
144 * This will produce an ASN1_ITEM called stname_it
145 * for a structure called stname.
147 * If you want the same structure but a different
150 * ASN1_SEQUENCE(itname) = {
151 * ... SEQUENCE components ...
152 * } ASN1_SEQUENCE_END_name(stname, itname)
154 * This will create an item called itname_it using
155 * a structure called stname.
158 # define ASN1_SEQUENCE(tname) \
159 static const ASN1_TEMPLATE tname##_seq_tt[]
161 # define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
163 # define static_ASN1_SEQUENCE_END(stname) static_ASN1_SEQUENCE_END_name(stname, stname)
165 # define ASN1_SEQUENCE_END_name(stname, tname) \
167 ASN1_ITEM_start(tname) \
168 ASN1_ITYPE_SEQUENCE,\
171 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
177 # define static_ASN1_SEQUENCE_END_name(stname, tname) \
179 static_ASN1_ITEM_start(tname) \
180 ASN1_ITYPE_SEQUENCE,\
183 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
189 # define ASN1_NDEF_SEQUENCE(tname) \
192 # define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
193 ASN1_SEQUENCE_cb(tname, cb)
195 # define ASN1_SEQUENCE_cb(tname, cb) \
196 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0, NULL}; \
199 # define ASN1_SEQUENCE_const_cb(tname, const_cb) \
200 static const ASN1_AUX tname##_aux = \
201 {NULL, ASN1_AFLG_CONST_CB, 0, 0, NULL, 0, const_cb}; \
204 # define ASN1_SEQUENCE_cb_const_cb(tname, cb, const_cb) \
205 static const ASN1_AUX tname##_aux = \
206 {NULL, ASN1_AFLG_CONST_CB, 0, 0, cb, 0, const_cb}; \
209 # define ASN1_SEQUENCE_ref(tname, cb) \
210 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), offsetof(tname, lock), cb, 0, NULL}; \
213 # define ASN1_SEQUENCE_enc(tname, enc, cb) \
214 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc), NULL}; \
217 # define ASN1_NDEF_SEQUENCE_END(tname) \
219 ASN1_ITEM_start(tname) \
220 ASN1_ITYPE_NDEF_SEQUENCE,\
223 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
228 # define static_ASN1_NDEF_SEQUENCE_END(tname) \
230 static_ASN1_ITEM_start(tname) \
231 ASN1_ITYPE_NDEF_SEQUENCE,\
234 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
241 # define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
243 # define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
244 # define static_ASN1_SEQUENCE_END_cb(stname, tname) static_ASN1_SEQUENCE_END_ref(stname, tname)
246 # define ASN1_SEQUENCE_END_ref(stname, tname) \
248 ASN1_ITEM_start(tname) \
249 ASN1_ITYPE_SEQUENCE,\
252 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
257 # define static_ASN1_SEQUENCE_END_ref(stname, tname) \
259 static_ASN1_ITEM_start(tname) \
260 ASN1_ITYPE_SEQUENCE,\
263 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
269 # define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
271 ASN1_ITEM_start(tname) \
272 ASN1_ITYPE_NDEF_SEQUENCE,\
275 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
282 * This pair helps declare a CHOICE type. We can do:
284 * ASN1_CHOICE(chname) = {
285 * ... CHOICE options ...
286 * ASN1_CHOICE_END(chname)
288 * This will produce an ASN1_ITEM called chname_it
289 * for a structure called chname. The structure
290 * definition must look like this:
294 * ASN1_SOMETHING *opt1;
295 * ASN1_SOMEOTHER *opt2;
299 * the name of the selector must be 'type'.
300 * to use an alternative selector name use the
301 * ASN1_CHOICE_END_selector() version.
304 # define ASN1_CHOICE(tname) \
305 static const ASN1_TEMPLATE tname##_ch_tt[]
307 # define ASN1_CHOICE_cb(tname, cb) \
308 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0, NULL}; \
311 # define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
313 # define static_ASN1_CHOICE_END(stname) static_ASN1_CHOICE_END_name(stname, stname)
315 # define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
317 # define static_ASN1_CHOICE_END_name(stname, tname) static_ASN1_CHOICE_END_selector(stname, tname, type)
319 # define ASN1_CHOICE_END_selector(stname, tname, selname) \
321 ASN1_ITEM_start(tname) \
323 offsetof(stname,selname) ,\
325 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
331 # define static_ASN1_CHOICE_END_selector(stname, tname, selname) \
333 static_ASN1_ITEM_start(tname) \
335 offsetof(stname,selname) ,\
337 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
343 # define ASN1_CHOICE_END_cb(stname, tname, selname) \
345 ASN1_ITEM_start(tname) \
347 offsetof(stname,selname) ,\
349 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
355 /* This helps with the template wrapper form of ASN1_ITEM */
357 # define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
359 #name, ASN1_ITEM_ref(type) }
361 /* These help with SEQUENCE or CHOICE components */
363 /* used to declare other types */
365 # define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
366 (flags), (tag), offsetof(stname, field),\
367 #field, ASN1_ITEM_ref(type) }
369 /* implicit and explicit helper macros */
371 # define ASN1_IMP_EX(stname, field, type, tag, ex) \
372 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | (ex), tag, stname, field, type)
374 # define ASN1_EXP_EX(stname, field, type, tag, ex) \
375 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | (ex), tag, stname, field, type)
377 /* Any defined by macros: the field used is in the table itself */
379 # define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
380 # define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
382 /* Plain simple type */
383 # define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
384 /* Embedded simple type */
385 # define ASN1_EMBED(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_EMBED,0, stname, field, type)
387 /* OPTIONAL simple type */
388 # define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
389 # define ASN1_OPT_EMBED(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED, 0, stname, field, type)
391 /* IMPLICIT tagged simple type */
392 # define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
393 # define ASN1_IMP_EMBED(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_EMBED)
395 /* IMPLICIT tagged OPTIONAL simple type */
396 # define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
397 # define ASN1_IMP_OPT_EMBED(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED)
399 /* Same as above but EXPLICIT */
401 # define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
402 # define ASN1_EXP_EMBED(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_EMBED)
403 # define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
404 # define ASN1_EXP_OPT_EMBED(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED)
406 /* SEQUENCE OF type */
407 # define ASN1_SEQUENCE_OF(stname, field, type) \
408 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
410 /* OPTIONAL SEQUENCE OF */
411 # define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
412 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
414 /* Same as above but for SET OF */
416 # define ASN1_SET_OF(stname, field, type) \
417 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
419 # define ASN1_SET_OF_OPT(stname, field, type) \
420 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
422 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
424 # define ASN1_IMP_SET_OF(stname, field, type, tag) \
425 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
427 # define ASN1_EXP_SET_OF(stname, field, type, tag) \
428 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
430 # define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
431 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
433 # define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
434 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
436 # define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
437 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
439 # define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
440 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
442 # define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
443 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
445 # define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
446 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
448 /* EXPLICIT using indefinite length constructed form */
449 # define ASN1_NDEF_EXP(stname, field, type, tag) \
450 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
452 /* EXPLICIT OPTIONAL using indefinite length constructed form */
453 # define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
454 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
456 /* Macros for the ASN1_ADB structure */
458 # define ASN1_ADB(name) \
459 static const ASN1_ADB_TABLE name##_adbtbl[]
461 # define ASN1_ADB_END(name, flags, field, adb_cb, def, none) \
463 static const ASN1_ITEM *name##_adb(void) \
465 static const ASN1_ADB internal_adb = \
468 offsetof(name, field),\
471 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
475 return (const ASN1_ITEM *) &internal_adb; \
477 void dummy_function(void)
479 # define ADB_ENTRY(val, template) {val, template}
481 # define ASN1_ADB_TEMPLATE(name) \
482 static const ASN1_TEMPLATE name##_tt
485 * This is the ASN1 template structure that defines a wrapper round the
486 * actual type. It determines the actual position of the field in the value
487 * structure, various flags such as OPTIONAL and the field name.
490 struct ASN1_TEMPLATE_st {
491 unsigned long flags; /* Various flags */
492 long tag; /* tag, not used if no tagging */
493 unsigned long offset; /* Offset of this field in structure */
494 const char *field_name; /* Field name */
495 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
498 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
500 # define ASN1_TEMPLATE_item(t) (t->item_ptr)
501 # define ASN1_TEMPLATE_adb(t) (t->item_ptr)
503 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
504 typedef struct ASN1_ADB_st ASN1_ADB;
507 unsigned long flags; /* Various flags */
508 unsigned long offset; /* Offset of selector field */
509 int (*adb_cb)(long *psel); /* Application callback */
510 const ASN1_ADB_TABLE *tbl; /* Table of possible types */
511 long tblcount; /* Number of entries in tbl */
512 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
513 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
516 struct ASN1_ADB_TABLE_st {
517 long value; /* NID for an object or value for an int */
518 const ASN1_TEMPLATE tt; /* item for this value */
523 /* Field is optional */
524 # define ASN1_TFLG_OPTIONAL (0x1)
526 /* Field is a SET OF */
527 # define ASN1_TFLG_SET_OF (0x1 << 1)
529 /* Field is a SEQUENCE OF */
530 # define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
533 * Special case: this refers to a SET OF that will be sorted into DER order
534 * when encoded *and* the corresponding STACK will be modified to match the
537 # define ASN1_TFLG_SET_ORDER (0x3 << 1)
539 /* Mask for SET OF or SEQUENCE OF */
540 # define ASN1_TFLG_SK_MASK (0x3 << 1)
543 * These flags mean the tag should be taken from the tag field. If EXPLICIT
544 * then the underlying type is used for the inner tag.
547 /* IMPLICIT tagging */
548 # define ASN1_TFLG_IMPTAG (0x1 << 3)
550 /* EXPLICIT tagging, inner tag from underlying type */
551 # define ASN1_TFLG_EXPTAG (0x2 << 3)
553 # define ASN1_TFLG_TAG_MASK (0x3 << 3)
555 /* context specific IMPLICIT */
556 # define ASN1_TFLG_IMPLICIT (ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT)
558 /* context specific EXPLICIT */
559 # define ASN1_TFLG_EXPLICIT (ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT)
562 * If tagging is in force these determine the type of tag to use. Otherwise
563 * the tag is determined by the underlying type. These values reflect the
564 * actual octet format.
568 # define ASN1_TFLG_UNIVERSAL (0x0<<6)
569 /* Application tag */
570 # define ASN1_TFLG_APPLICATION (0x1<<6)
571 /* Context specific tag */
572 # define ASN1_TFLG_CONTEXT (0x2<<6)
574 # define ASN1_TFLG_PRIVATE (0x3<<6)
576 # define ASN1_TFLG_TAG_CLASS (0x3<<6)
579 * These are for ANY DEFINED BY type. In this case the 'item' field points to
580 * an ASN1_ADB structure which contains a table of values to decode the
584 # define ASN1_TFLG_ADB_MASK (0x3<<8)
586 # define ASN1_TFLG_ADB_OID (0x1<<8)
588 # define ASN1_TFLG_ADB_INT (0x1<<9)
591 * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes
592 * indefinite length constructed encoding to be used if required.
595 # define ASN1_TFLG_NDEF (0x1<<11)
597 /* Field is embedded and not a pointer */
598 # define ASN1_TFLG_EMBED (0x1 << 12)
600 /* This is the actual ASN1 item itself */
602 struct ASN1_ITEM_st {
603 char itype; /* The item type, primitive, SEQUENCE, CHOICE
605 long utype; /* underlying type */
606 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains
608 long tcount; /* Number of templates if SEQUENCE or CHOICE */
609 const void *funcs; /* further data and type-specific functions */
610 /* funcs can be ASN1_PRIMITIVE_FUNCS*, ASN1_EXTERN_FUNCS*, or ASN1_AUX* */
611 long size; /* Structure size (usually) */
612 const char *sname; /* Structure name */
616 * Cache for ASN1 tag and length, so we don't keep re-reading it for things
621 char valid; /* Values below are valid */
622 int ret; /* return value */
623 long plen; /* length */
624 int ptag; /* class value */
625 int pclass; /* class value */
626 int hdrlen; /* header length */
629 /* Typedefs for ASN1 function pointers */
630 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
631 const ASN1_ITEM *it, int tag, int aclass, char opt,
634 typedef int ASN1_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
635 const ASN1_ITEM *it, int tag, int aclass);
636 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
637 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
639 typedef int ASN1_ex_print_func(BIO *out, const ASN1_VALUE **pval,
640 int indent, const char *fname,
641 const ASN1_PCTX *pctx);
643 typedef int ASN1_primitive_i2c(const ASN1_VALUE **pval, unsigned char *cont,
644 int *putype, const ASN1_ITEM *it);
645 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
646 int len, int utype, char *free_cont,
647 const ASN1_ITEM *it);
648 typedef int ASN1_primitive_print(BIO *out, const ASN1_VALUE **pval,
649 const ASN1_ITEM *it, int indent,
650 const ASN1_PCTX *pctx);
652 typedef struct ASN1_EXTERN_FUNCS_st {
654 ASN1_ex_new_func *asn1_ex_new;
655 ASN1_ex_free_func *asn1_ex_free;
656 ASN1_ex_free_func *asn1_ex_clear;
657 ASN1_ex_d2i *asn1_ex_d2i;
658 ASN1_ex_i2d *asn1_ex_i2d;
659 ASN1_ex_print_func *asn1_ex_print;
662 typedef struct ASN1_PRIMITIVE_FUNCS_st {
665 ASN1_ex_new_func *prim_new;
666 ASN1_ex_free_func *prim_free;
667 ASN1_ex_free_func *prim_clear;
668 ASN1_primitive_c2i *prim_c2i;
669 ASN1_primitive_i2c *prim_i2c;
670 ASN1_primitive_print *prim_print;
671 } ASN1_PRIMITIVE_FUNCS;
674 * This is the ASN1_AUX structure: it handles various miscellaneous
675 * requirements. For example the use of reference counts and an informational
676 * callback. The "informational callback" is called at various points during
677 * the ASN1 encoding and decoding. It can be used to provide minor
678 * customisation of the structures used. This is most useful where the
679 * supplied routines *almost* do the right thing but need some extra help at
680 * a few points. If the callback returns zero then it is assumed a fatal
681 * error has occurred and the main operation should be abandoned. If major
682 * changes in the default behaviour are required then an external type is
684 * For the operations ASN1_OP_I2D_PRE, ASN1_OP_I2D_POST, ASN1_OP_PRINT_PRE, and
685 * ASN1_OP_PRINT_POST, meanwhile a variant of the callback with const parameter
686 * 'in' is provided to make clear statically that its input is not modified. If
687 * and only if this variant is in use the flag ASN1_AFLG_CONST_CB must be set.
690 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
692 typedef int ASN1_aux_const_cb(int operation, const ASN1_VALUE **in,
693 const ASN1_ITEM *it, void *exarg);
695 typedef struct ASN1_AUX_st {
698 int ref_offset; /* Offset of reference value */
699 int ref_lock; /* Lock type to use */
700 ASN1_aux_cb *asn1_cb;
701 int enc_offset; /* Offset of ASN1_ENCODING structure */
702 ASN1_aux_const_cb *asn1_const_cb; /* for ASN1_OP_I2D_ and ASN1_OP_PRINT_ */
705 /* For print related callbacks exarg points to this structure */
706 typedef struct ASN1_PRINT_ARG_st {
709 const ASN1_PCTX *pctx;
712 /* For streaming related callbacks exarg points to this structure */
713 typedef struct ASN1_STREAM_ARG_st {
714 /* BIO to stream through */
716 /* BIO with filters appended */
718 /* Streaming I/O boundary */
719 unsigned char **boundary;
722 /* Flags in ASN1_AUX */
724 /* Use a reference count */
725 # define ASN1_AFLG_REFCOUNT 1
726 /* Save the encoding of structure (useful for signatures) */
727 # define ASN1_AFLG_ENCODING 2
728 /* The Sequence length is invalid */
729 # define ASN1_AFLG_BROKEN 4
730 /* Use the new asn1_const_cb */
731 # define ASN1_AFLG_CONST_CB 8
733 /* operation values for asn1_cb */
735 # define ASN1_OP_NEW_PRE 0
736 # define ASN1_OP_NEW_POST 1
737 # define ASN1_OP_FREE_PRE 2
738 # define ASN1_OP_FREE_POST 3
739 # define ASN1_OP_D2I_PRE 4
740 # define ASN1_OP_D2I_POST 5
741 # define ASN1_OP_I2D_PRE 6
742 # define ASN1_OP_I2D_POST 7
743 # define ASN1_OP_PRINT_PRE 8
744 # define ASN1_OP_PRINT_POST 9
745 # define ASN1_OP_STREAM_PRE 10
746 # define ASN1_OP_STREAM_POST 11
747 # define ASN1_OP_DETACHED_PRE 12
748 # define ASN1_OP_DETACHED_POST 13
749 # define ASN1_OP_DUP_PRE 14
750 # define ASN1_OP_DUP_POST 15
752 /* Macro to implement a primitive type */
753 # define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
754 # define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
755 ASN1_ITEM_start(itname) \
756 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
757 ASN1_ITEM_end(itname)
759 /* Macro to implement a multi string type */
760 # define IMPLEMENT_ASN1_MSTRING(itname, mask) \
761 ASN1_ITEM_start(itname) \
762 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
763 ASN1_ITEM_end(itname)
765 # define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
766 ASN1_ITEM_start(sname) \
776 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
778 # define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
780 # define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
782 # define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
783 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
785 # define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
786 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
788 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
789 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
791 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
792 pre stname *fname##_new(void) \
794 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
796 pre void fname##_free(stname *a) \
798 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
801 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
802 stname *fname##_new(void) \
804 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
806 void fname##_free(stname *a) \
808 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
811 # define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
812 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
813 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
815 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
816 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
818 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
820 int i2d_##fname(const stname *a, unsigned char **out) \
822 return ASN1_item_i2d((const ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
825 # define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
826 int i2d_##stname##_NDEF(const stname *a, unsigned char **out) \
828 return ASN1_item_ndef_i2d((const ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
831 # define IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(stname) \
832 static stname *d2i_##stname(stname **a, \
833 const unsigned char **in, long len) \
835 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \
836 ASN1_ITEM_rptr(stname)); \
838 static int i2d_##stname(const stname *a, unsigned char **out) \
840 return ASN1_item_i2d((const ASN1_VALUE *)a, out, \
841 ASN1_ITEM_rptr(stname)); \
844 # define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
845 stname * stname##_dup(const stname *x) \
847 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
850 # define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
851 IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
853 # define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
854 int fname##_print_ctx(BIO *out, const stname *x, int indent, \
855 const ASN1_PCTX *pctx) \
857 return ASN1_item_print(out, (const ASN1_VALUE *)x, indent, \
858 ASN1_ITEM_rptr(itname), pctx); \
861 /* external definitions for primitive types */
863 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
864 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
865 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
866 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
867 DECLARE_ASN1_ITEM(CBIGNUM)
868 DECLARE_ASN1_ITEM(BIGNUM)
869 DECLARE_ASN1_ITEM(INT32)
870 DECLARE_ASN1_ITEM(ZINT32)
871 DECLARE_ASN1_ITEM(UINT32)
872 DECLARE_ASN1_ITEM(ZUINT32)
873 DECLARE_ASN1_ITEM(INT64)
874 DECLARE_ASN1_ITEM(ZINT64)
875 DECLARE_ASN1_ITEM(UINT64)
876 DECLARE_ASN1_ITEM(ZUINT64)
878 # ifndef OPENSSL_NO_DEPRECATED_3_0
880 * LONG and ZLONG are strongly discouraged for use as stored data, as the
881 * underlying C type (long) differs in size depending on the architecture.
882 * They are designed with 32-bit longs in mind.
884 DECLARE_ASN1_ITEM(LONG)
885 DECLARE_ASN1_ITEM(ZLONG)
889 generate_stack_macros("ASN1_VALUE");
893 /* Functions used internally by the ASN1 code */
895 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
896 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
898 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
899 const ASN1_ITEM *it, int tag, int aclass, char opt,
902 int ASN1_item_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
903 const ASN1_ITEM *it, int tag, int aclass);
905 /* Legacy compatibility */
906 # define IMPLEMENT_ASN1_FUNCTIONS_const(name) IMPLEMENT_ASN1_FUNCTIONS(name)
907 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
908 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname)