2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OPENSSL_ASN1T_H
11 # define OPENSSL_ASN1T_H
14 # include <openssl/macros.h>
15 # ifndef OPENSSL_NO_DEPRECATED_3_0
16 # define HEADER_ASN1T_H
20 # include <openssl/e_os2.h>
21 # include <openssl/asn1.h>
23 # ifdef OPENSSL_BUILD_SHLIBCRYPTO
24 # undef OPENSSL_EXTERN
25 # define OPENSSL_EXTERN OPENSSL_EXPORT
28 /* ASN1 template defines, structures and functions */
34 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
35 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)((iptr)()))
37 /* Macros for start and end of ASN1_ITEM definition */
39 # define ASN1_ITEM_start(itname) \
40 const ASN1_ITEM * itname##_it(void) \
42 static const ASN1_ITEM local_it = {
44 # define static_ASN1_ITEM_start(itname) \
45 static ASN1_ITEM_start(itname)
47 # define ASN1_ITEM_end(itname) \
52 /* Macros to aid ASN1 template writing */
54 # define ASN1_ITEM_TEMPLATE(tname) \
55 static const ASN1_TEMPLATE tname##_item_tt
57 # define ASN1_ITEM_TEMPLATE_END(tname) \
59 ASN1_ITEM_start(tname) \
60 ASN1_ITYPE_PRIMITIVE,\
68 # define static_ASN1_ITEM_TEMPLATE_END(tname) \
70 static_ASN1_ITEM_start(tname) \
71 ASN1_ITYPE_PRIMITIVE,\
80 /* This is a ASN1 type which just embeds a template */
83 * This pair helps declare a SEQUENCE. We can do:
85 * ASN1_SEQUENCE(stname) = {
86 * ... SEQUENCE components ...
87 * } ASN1_SEQUENCE_END(stname)
89 * This will produce an ASN1_ITEM called stname_it
90 * for a structure called stname.
92 * If you want the same structure but a different
95 * ASN1_SEQUENCE(itname) = {
96 * ... SEQUENCE components ...
97 * } ASN1_SEQUENCE_END_name(stname, itname)
99 * This will create an item called itname_it using
100 * a structure called stname.
103 # define ASN1_SEQUENCE(tname) \
104 static const ASN1_TEMPLATE tname##_seq_tt[]
106 # define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
108 # define static_ASN1_SEQUENCE_END(stname) static_ASN1_SEQUENCE_END_name(stname, stname)
110 # define ASN1_SEQUENCE_END_name(stname, tname) \
112 ASN1_ITEM_start(tname) \
113 ASN1_ITYPE_SEQUENCE,\
116 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
122 # define static_ASN1_SEQUENCE_END_name(stname, tname) \
124 static_ASN1_ITEM_start(tname) \
125 ASN1_ITYPE_SEQUENCE,\
128 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
134 # define ASN1_NDEF_SEQUENCE(tname) \
137 # define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
138 ASN1_SEQUENCE_cb(tname, cb)
140 # define ASN1_SEQUENCE_cb(tname, cb) \
141 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0, NULL}; \
144 # define ASN1_SEQUENCE_const_cb(tname, const_cb) \
145 static const ASN1_AUX tname##_aux = \
146 {NULL, ASN1_AFLG_CONST_CB, 0, 0, NULL, 0, const_cb}; \
149 # define ASN1_SEQUENCE_cb_const_cb(tname, cb, const_cb) \
150 static const ASN1_AUX tname##_aux = \
151 {NULL, ASN1_AFLG_CONST_CB, 0, 0, cb, 0, const_cb}; \
154 # define ASN1_SEQUENCE_ref(tname, cb) \
155 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), offsetof(tname, lock), cb, 0, NULL}; \
158 # define ASN1_SEQUENCE_enc(tname, enc, cb) \
159 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc), NULL}; \
162 # define ASN1_NDEF_SEQUENCE_END(tname) \
164 ASN1_ITEM_start(tname) \
165 ASN1_ITYPE_NDEF_SEQUENCE,\
168 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
173 # define static_ASN1_NDEF_SEQUENCE_END(tname) \
175 static_ASN1_ITEM_start(tname) \
176 ASN1_ITYPE_NDEF_SEQUENCE,\
179 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
186 # define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
188 # define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
189 # define static_ASN1_SEQUENCE_END_cb(stname, tname) static_ASN1_SEQUENCE_END_ref(stname, tname)
191 # define ASN1_SEQUENCE_END_ref(stname, tname) \
193 ASN1_ITEM_start(tname) \
194 ASN1_ITYPE_SEQUENCE,\
197 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
202 # define static_ASN1_SEQUENCE_END_ref(stname, tname) \
204 static_ASN1_ITEM_start(tname) \
205 ASN1_ITYPE_SEQUENCE,\
208 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
214 # define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
216 ASN1_ITEM_start(tname) \
217 ASN1_ITYPE_NDEF_SEQUENCE,\
220 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
227 * This pair helps declare a CHOICE type. We can do:
229 * ASN1_CHOICE(chname) = {
230 * ... CHOICE options ...
231 * ASN1_CHOICE_END(chname)
233 * This will produce an ASN1_ITEM called chname_it
234 * for a structure called chname. The structure
235 * definition must look like this:
239 * ASN1_SOMETHING *opt1;
240 * ASN1_SOMEOTHER *opt2;
244 * the name of the selector must be 'type'.
245 * to use an alternative selector name use the
246 * ASN1_CHOICE_END_selector() version.
249 # define ASN1_CHOICE(tname) \
250 static const ASN1_TEMPLATE tname##_ch_tt[]
252 # define ASN1_CHOICE_cb(tname, cb) \
253 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0, NULL}; \
256 # define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
258 # define static_ASN1_CHOICE_END(stname) static_ASN1_CHOICE_END_name(stname, stname)
260 # define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
262 # define static_ASN1_CHOICE_END_name(stname, tname) static_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 static_ASN1_CHOICE_END_selector(stname, tname, selname) \
278 static_ASN1_ITEM_start(tname) \
280 offsetof(stname,selname) ,\
282 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
288 # define ASN1_CHOICE_END_cb(stname, tname, selname) \
290 ASN1_ITEM_start(tname) \
292 offsetof(stname,selname) ,\
294 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
300 /* This helps with the template wrapper form of ASN1_ITEM */
302 # define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
304 #name, ASN1_ITEM_ref(type) }
306 /* These help with SEQUENCE or CHOICE components */
308 /* used to declare other types */
310 # define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
311 (flags), (tag), offsetof(stname, field),\
312 #field, ASN1_ITEM_ref(type) }
314 /* implicit and explicit helper macros */
316 # define ASN1_IMP_EX(stname, field, type, tag, ex) \
317 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | (ex), tag, stname, field, type)
319 # define ASN1_EXP_EX(stname, field, type, tag, ex) \
320 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | (ex), tag, stname, field, type)
322 /* Any defined by macros: the field used is in the table itself */
324 # define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
325 # define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
327 /* Plain simple type */
328 # define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
329 /* Embedded simple type */
330 # define ASN1_EMBED(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_EMBED,0, stname, field, type)
332 /* OPTIONAL simple type */
333 # define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
334 # define ASN1_OPT_EMBED(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED, 0, stname, field, type)
336 /* IMPLICIT tagged simple type */
337 # define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
338 # define ASN1_IMP_EMBED(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_EMBED)
340 /* IMPLICIT tagged OPTIONAL simple type */
341 # define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
342 # define ASN1_IMP_OPT_EMBED(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED)
344 /* Same as above but EXPLICIT */
346 # define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
347 # define ASN1_EXP_EMBED(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_EMBED)
348 # define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
349 # define ASN1_EXP_OPT_EMBED(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED)
351 /* SEQUENCE OF type */
352 # define ASN1_SEQUENCE_OF(stname, field, type) \
353 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
355 /* OPTIONAL SEQUENCE OF */
356 # define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
357 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
359 /* Same as above but for SET OF */
361 # define ASN1_SET_OF(stname, field, type) \
362 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
364 # define ASN1_SET_OF_OPT(stname, field, type) \
365 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
367 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
369 # define ASN1_IMP_SET_OF(stname, field, type, tag) \
370 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
372 # define ASN1_EXP_SET_OF(stname, field, type, tag) \
373 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
375 # define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
376 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
378 # define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
379 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
381 # define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
382 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
384 # define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
385 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
387 # define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
388 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
390 # define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
391 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
393 /* EXPLICIT using indefinite length constructed form */
394 # define ASN1_NDEF_EXP(stname, field, type, tag) \
395 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
397 /* EXPLICIT OPTIONAL using indefinite length constructed form */
398 # define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
399 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
401 /* Macros for the ASN1_ADB structure */
403 # define ASN1_ADB(name) \
404 static const ASN1_ADB_TABLE name##_adbtbl[]
406 # define ASN1_ADB_END(name, flags, field, adb_cb, def, none) \
408 static const ASN1_ITEM *name##_adb(void) \
410 static const ASN1_ADB internal_adb = \
413 offsetof(name, field),\
416 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
420 return (const ASN1_ITEM *) &internal_adb; \
422 void dummy_function(void)
424 # define ADB_ENTRY(val, template) {val, template}
426 # define ASN1_ADB_TEMPLATE(name) \
427 static const ASN1_TEMPLATE name##_tt
430 * This is the ASN1 template structure that defines a wrapper round the
431 * actual type. It determines the actual position of the field in the value
432 * structure, various flags such as OPTIONAL and the field name.
435 struct ASN1_TEMPLATE_st {
436 unsigned long flags; /* Various flags */
437 long tag; /* tag, not used if no tagging */
438 unsigned long offset; /* Offset of this field in structure */
439 const char *field_name; /* Field name */
440 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
443 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
445 # define ASN1_TEMPLATE_item(t) (t->item_ptr)
446 # define ASN1_TEMPLATE_adb(t) (t->item_ptr)
448 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
449 typedef struct ASN1_ADB_st ASN1_ADB;
452 unsigned long flags; /* Various flags */
453 unsigned long offset; /* Offset of selector field */
454 int (*adb_cb)(long *psel); /* Application callback */
455 const ASN1_ADB_TABLE *tbl; /* Table of possible types */
456 long tblcount; /* Number of entries in tbl */
457 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
458 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
461 struct ASN1_ADB_TABLE_st {
462 long value; /* NID for an object or value for an int */
463 const ASN1_TEMPLATE tt; /* item for this value */
468 /* Field is optional */
469 # define ASN1_TFLG_OPTIONAL (0x1)
471 /* Field is a SET OF */
472 # define ASN1_TFLG_SET_OF (0x1 << 1)
474 /* Field is a SEQUENCE OF */
475 # define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
478 * Special case: this refers to a SET OF that will be sorted into DER order
479 * when encoded *and* the corresponding STACK will be modified to match the
482 # define ASN1_TFLG_SET_ORDER (0x3 << 1)
484 /* Mask for SET OF or SEQUENCE OF */
485 # define ASN1_TFLG_SK_MASK (0x3 << 1)
488 * These flags mean the tag should be taken from the tag field. If EXPLICIT
489 * then the underlying type is used for the inner tag.
492 /* IMPLICIT tagging */
493 # define ASN1_TFLG_IMPTAG (0x1 << 3)
495 /* EXPLICIT tagging, inner tag from underlying type */
496 # define ASN1_TFLG_EXPTAG (0x2 << 3)
498 # define ASN1_TFLG_TAG_MASK (0x3 << 3)
500 /* context specific IMPLICIT */
501 # define ASN1_TFLG_IMPLICIT (ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT)
503 /* context specific EXPLICIT */
504 # define ASN1_TFLG_EXPLICIT (ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT)
507 * If tagging is in force these determine the type of tag to use. Otherwise
508 * the tag is determined by the underlying type. These values reflect the
509 * actual octet format.
513 # define ASN1_TFLG_UNIVERSAL (0x0<<6)
514 /* Application tag */
515 # define ASN1_TFLG_APPLICATION (0x1<<6)
516 /* Context specific tag */
517 # define ASN1_TFLG_CONTEXT (0x2<<6)
519 # define ASN1_TFLG_PRIVATE (0x3<<6)
521 # define ASN1_TFLG_TAG_CLASS (0x3<<6)
524 * These are for ANY DEFINED BY type. In this case the 'item' field points to
525 * an ASN1_ADB structure which contains a table of values to decode the
529 # define ASN1_TFLG_ADB_MASK (0x3<<8)
531 # define ASN1_TFLG_ADB_OID (0x1<<8)
533 # define ASN1_TFLG_ADB_INT (0x1<<9)
536 * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes
537 * indefinite length constructed encoding to be used if required.
540 # define ASN1_TFLG_NDEF (0x1<<11)
542 /* Field is embedded and not a pointer */
543 # define ASN1_TFLG_EMBED (0x1 << 12)
545 /* This is the actual ASN1 item itself */
547 struct ASN1_ITEM_st {
548 char itype; /* The item type, primitive, SEQUENCE, CHOICE
550 long utype; /* underlying type */
551 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains
553 long tcount; /* Number of templates if SEQUENCE or CHOICE */
554 const void *funcs; /* functions that handle this type */
555 long size; /* Structure size (usually) */
556 const char *sname; /* Structure name */
560 * These are values for the itype field and
561 * determine how the type is interpreted.
563 * For PRIMITIVE types the underlying type
564 * determines the behaviour if items is NULL.
566 * Otherwise templates must contain a single
567 * template and the type is treated in the
568 * same way as the type specified in the template.
570 * For SEQUENCE types the templates field points
571 * to the members, the size field is the
574 * For CHOICE types the templates field points
575 * to each possible member (typically a union)
576 * and the 'size' field is the offset of the
579 * The 'funcs' field is used for application
580 * specific functions.
582 * The EXTERN type uses a new style d2i/i2d.
583 * The new style should be used where possible
584 * because it avoids things like the d2i IMPLICIT
587 * MSTRING is a multiple string type, it is used
588 * for a CHOICE of character strings where the
589 * actual strings all occupy an ASN1_STRING
590 * structure. In this case the 'utype' field
591 * has a special meaning, it is used as a mask
592 * of acceptable types using the B_ASN1 constants.
594 * NDEF_SEQUENCE is the same as SEQUENCE except
595 * that it will use indefinite length constructed
596 * encoding if requested.
600 # define ASN1_ITYPE_PRIMITIVE 0x0
602 # define ASN1_ITYPE_SEQUENCE 0x1
604 # define ASN1_ITYPE_CHOICE 0x2
606 # define ASN1_ITYPE_EXTERN 0x4
608 # define ASN1_ITYPE_MSTRING 0x5
610 # define ASN1_ITYPE_NDEF_SEQUENCE 0x6
613 * Cache for ASN1 tag and length, so we don't keep re-reading it for things
618 char valid; /* Values below are valid */
619 int ret; /* return value */
620 long plen; /* length */
621 int ptag; /* class value */
622 int pclass; /* class value */
623 int hdrlen; /* header length */
626 /* Typedefs for ASN1 function pointers */
627 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
628 const ASN1_ITEM *it, int tag, int aclass, char opt,
631 typedef int ASN1_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
632 const ASN1_ITEM *it, int tag, int aclass);
633 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
634 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
636 typedef int ASN1_ex_print_func(BIO *out, const ASN1_VALUE **pval,
637 int indent, const char *fname,
638 const ASN1_PCTX *pctx);
640 typedef int ASN1_primitive_i2c(const ASN1_VALUE **pval, unsigned char *cont,
641 int *putype, const ASN1_ITEM *it);
642 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
643 int len, int utype, char *free_cont,
644 const ASN1_ITEM *it);
645 typedef int ASN1_primitive_print(BIO *out, const ASN1_VALUE **pval,
646 const ASN1_ITEM *it, int indent,
647 const ASN1_PCTX *pctx);
649 typedef struct ASN1_EXTERN_FUNCS_st {
651 ASN1_ex_new_func *asn1_ex_new;
652 ASN1_ex_free_func *asn1_ex_free;
653 ASN1_ex_free_func *asn1_ex_clear;
654 ASN1_ex_d2i *asn1_ex_d2i;
655 ASN1_ex_i2d *asn1_ex_i2d;
656 ASN1_ex_print_func *asn1_ex_print;
659 typedef struct ASN1_PRIMITIVE_FUNCS_st {
662 ASN1_ex_new_func *prim_new;
663 ASN1_ex_free_func *prim_free;
664 ASN1_ex_free_func *prim_clear;
665 ASN1_primitive_c2i *prim_c2i;
666 ASN1_primitive_i2c *prim_i2c;
667 ASN1_primitive_print *prim_print;
668 } ASN1_PRIMITIVE_FUNCS;
671 * This is the ASN1_AUX structure: it handles various miscellaneous
672 * requirements. For example the use of reference counts and an informational
673 * callback. The "informational callback" is called at various points during
674 * the ASN1 encoding and decoding. It can be used to provide minor
675 * customisation of the structures used. This is most useful where the
676 * supplied routines *almost* do the right thing but need some extra help at
677 * a few points. If the callback returns zero then it is assumed a fatal
678 * error has occurred and the main operation should be abandoned. If major
679 * changes in the default behaviour are required then an external type is
681 * For the operations ASN1_OP_I2D_PRE, ASN1_OP_I2D_POST, ASN1_OP_PRINT_PRE, and
682 * ASN1_OP_PRINT_POST, meanwhile a variant of the callback with const parameter
683 * 'in' is provided to make clear statically that its input is not modified. If
684 * and only if this variant is in use the flag ASN1_AFLG_CONST_CB must be set.
687 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
689 typedef int ASN1_aux_const_cb(int operation, const ASN1_VALUE **in,
690 const ASN1_ITEM *it, void *exarg);
692 typedef struct ASN1_AUX_st {
695 int ref_offset; /* Offset of reference value */
696 int ref_lock; /* Lock type to use */
697 ASN1_aux_cb *asn1_cb;
698 int enc_offset; /* Offset of ASN1_ENCODING structure */
699 ASN1_aux_const_cb *asn1_const_cb; /* for ASN1_OP_I2D_ and ASN1_OP_PRINT_ */
702 /* For print related callbacks exarg points to this structure */
703 typedef struct ASN1_PRINT_ARG_st {
706 const ASN1_PCTX *pctx;
709 /* For streaming related callbacks exarg points to this structure */
710 typedef struct ASN1_STREAM_ARG_st {
711 /* BIO to stream through */
713 /* BIO with filters appended */
715 /* Streaming I/O boundary */
716 unsigned char **boundary;
719 /* Flags in ASN1_AUX */
721 /* Use a reference count */
722 # define ASN1_AFLG_REFCOUNT 1
723 /* Save the encoding of structure (useful for signatures) */
724 # define ASN1_AFLG_ENCODING 2
725 /* The Sequence length is invalid */
726 # define ASN1_AFLG_BROKEN 4
727 /* Use the new asn1_const_cb */
728 # define ASN1_AFLG_CONST_CB 8
730 /* operation values for asn1_cb */
732 # define ASN1_OP_NEW_PRE 0
733 # define ASN1_OP_NEW_POST 1
734 # define ASN1_OP_FREE_PRE 2
735 # define ASN1_OP_FREE_POST 3
736 # define ASN1_OP_D2I_PRE 4
737 # define ASN1_OP_D2I_POST 5
738 # define ASN1_OP_I2D_PRE 6
739 # define ASN1_OP_I2D_POST 7
740 # define ASN1_OP_PRINT_PRE 8
741 # define ASN1_OP_PRINT_POST 9
742 # define ASN1_OP_STREAM_PRE 10
743 # define ASN1_OP_STREAM_POST 11
744 # define ASN1_OP_DETACHED_PRE 12
745 # define ASN1_OP_DETACHED_POST 13
747 /* Macro to implement a primitive type */
748 # define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
749 # define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
750 ASN1_ITEM_start(itname) \
751 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
752 ASN1_ITEM_end(itname)
754 /* Macro to implement a multi string type */
755 # define IMPLEMENT_ASN1_MSTRING(itname, mask) \
756 ASN1_ITEM_start(itname) \
757 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
758 ASN1_ITEM_end(itname)
760 # define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
761 ASN1_ITEM_start(sname) \
771 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
773 # define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
775 # define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
777 # define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
778 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
780 # define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
781 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
783 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
784 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
786 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
787 pre stname *fname##_new(void) \
789 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
791 pre void fname##_free(stname *a) \
793 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
796 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
797 stname *fname##_new(void) \
799 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
801 void fname##_free(stname *a) \
803 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
806 # define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
807 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
808 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
810 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
811 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
813 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
815 int i2d_##fname(const stname *a, unsigned char **out) \
817 return ASN1_item_i2d((const ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
820 # define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
821 int i2d_##stname##_NDEF(const stname *a, unsigned char **out) \
823 return ASN1_item_ndef_i2d((const ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
826 # define IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(stname) \
827 static stname *d2i_##stname(stname **a, \
828 const unsigned char **in, long len) \
830 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \
831 ASN1_ITEM_rptr(stname)); \
833 static int i2d_##stname(const stname *a, unsigned char **out) \
835 return ASN1_item_i2d((const ASN1_VALUE *)a, out, \
836 ASN1_ITEM_rptr(stname)); \
839 # define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
840 stname * stname##_dup(const stname *x) \
842 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
845 # define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
846 IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
848 # define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
849 int fname##_print_ctx(BIO *out, const stname *x, int indent, \
850 const ASN1_PCTX *pctx) \
852 return ASN1_item_print(out, (const ASN1_VALUE *)x, indent, \
853 ASN1_ITEM_rptr(itname), pctx); \
856 /* external definitions for primitive types */
858 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
859 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
860 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
861 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
862 DECLARE_ASN1_ITEM(CBIGNUM)
863 DECLARE_ASN1_ITEM(BIGNUM)
864 DECLARE_ASN1_ITEM(INT32)
865 DECLARE_ASN1_ITEM(ZINT32)
866 DECLARE_ASN1_ITEM(UINT32)
867 DECLARE_ASN1_ITEM(ZUINT32)
868 DECLARE_ASN1_ITEM(INT64)
869 DECLARE_ASN1_ITEM(ZINT64)
870 DECLARE_ASN1_ITEM(UINT64)
871 DECLARE_ASN1_ITEM(ZUINT64)
873 # ifndef OPENSSL_NO_DEPRECATED_3_0
875 * LONG and ZLONG are strongly discouraged for use as stored data, as the
876 * underlying C type (long) differs in size depending on the architecture.
877 * They are designed with 32-bit longs in mind.
879 DECLARE_ASN1_ITEM(LONG)
880 DECLARE_ASN1_ITEM(ZLONG)
883 DEFINE_STACK_OF(ASN1_VALUE)
885 /* Functions used internally by the ASN1 code */
887 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
888 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
890 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
891 const ASN1_ITEM *it, int tag, int aclass, char opt,
894 int ASN1_item_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
895 const ASN1_ITEM *it, int tag, int aclass);
897 /* Legacy compatibility */
898 # define IMPLEMENT_ASN1_FUNCTIONS_const(name) IMPLEMENT_ASN1_FUNCTIONS(name)
899 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
900 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname)