2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 HEADER_ASN1T_H
11 # define HEADER_ASN1T_H
14 # include <openssl/e_os2.h>
15 # include <openssl/asn1.h>
17 # ifdef OPENSSL_BUILD_SHLIBCRYPTO
18 # undef OPENSSL_EXTERN
19 # define OPENSSL_EXTERN OPENSSL_EXPORT
22 /* ASN1 template defines, structures and functions */
28 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
30 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
31 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
33 /* Macros for start and end of ASN1_ITEM definition */
35 # define ASN1_ITEM_start(itname) \
36 OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
38 # define static_ASN1_ITEM_start(itname) \
39 static const ASN1_ITEM itname##_it = {
41 # define ASN1_ITEM_end(itname) \
46 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
47 # define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
49 /* Macros for start and end of ASN1_ITEM definition */
51 # define ASN1_ITEM_start(itname) \
52 const ASN1_ITEM * itname##_it(void) \
54 static const ASN1_ITEM local_it = {
56 # define static_ASN1_ITEM_start(itname) \
57 static ASN1_ITEM_start(itname)
59 # define ASN1_ITEM_end(itname) \
66 /* Macros to aid ASN1 template writing */
68 # define ASN1_ITEM_TEMPLATE(tname) \
69 static const ASN1_TEMPLATE tname##_item_tt
71 # define ASN1_ITEM_TEMPLATE_END(tname) \
73 ASN1_ITEM_start(tname) \
74 ASN1_ITYPE_PRIMITIVE,\
82 # define static_ASN1_ITEM_TEMPLATE_END(tname) \
84 static_ASN1_ITEM_start(tname) \
85 ASN1_ITYPE_PRIMITIVE,\
94 /* This is a ASN1 type which just embeds a template */
97 * This pair helps declare a SEQUENCE. We can do:
99 * ASN1_SEQUENCE(stname) = {
100 * ... SEQUENCE components ...
101 * } ASN1_SEQUENCE_END(stname)
103 * This will produce an ASN1_ITEM called stname_it
104 * for a structure called stname.
106 * If you want the same structure but a different
109 * ASN1_SEQUENCE(itname) = {
110 * ... SEQUENCE components ...
111 * } ASN1_SEQUENCE_END_name(stname, itname)
113 * This will create an item called itname_it using
114 * a structure called stname.
117 # define ASN1_SEQUENCE(tname) \
118 static const ASN1_TEMPLATE tname##_seq_tt[]
120 # define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
122 # define static_ASN1_SEQUENCE_END(stname) static_ASN1_SEQUENCE_END_name(stname, stname)
124 # define ASN1_SEQUENCE_END_name(stname, tname) \
126 ASN1_ITEM_start(tname) \
127 ASN1_ITYPE_SEQUENCE,\
130 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
136 # define static_ASN1_SEQUENCE_END_name(stname, tname) \
138 static_ASN1_ITEM_start(tname) \
139 ASN1_ITYPE_SEQUENCE,\
142 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
148 # define ASN1_NDEF_SEQUENCE(tname) \
151 # define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
152 ASN1_SEQUENCE_cb(tname, cb)
154 # define ASN1_SEQUENCE_cb(tname, cb) \
155 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
158 # define ASN1_BROKEN_SEQUENCE(tname) \
159 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
162 # define ASN1_SEQUENCE_ref(tname, cb) \
163 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), offsetof(tname, lock), cb, 0}; \
166 # define ASN1_SEQUENCE_enc(tname, enc, cb) \
167 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
170 # define ASN1_NDEF_SEQUENCE_END(tname) \
172 ASN1_ITEM_start(tname) \
173 ASN1_ITYPE_NDEF_SEQUENCE,\
176 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
181 # define static_ASN1_NDEF_SEQUENCE_END(tname) \
183 static_ASN1_ITEM_start(tname) \
184 ASN1_ITYPE_NDEF_SEQUENCE,\
187 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
193 # define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
194 # define static_ASN1_BROKEN_SEQUENCE_END(stname) \
195 static_ASN1_SEQUENCE_END_ref(stname, stname)
197 # define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
199 # define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
200 # define static_ASN1_SEQUENCE_END_cb(stname, tname) static_ASN1_SEQUENCE_END_ref(stname, tname)
202 # define ASN1_SEQUENCE_END_ref(stname, tname) \
204 ASN1_ITEM_start(tname) \
205 ASN1_ITYPE_SEQUENCE,\
208 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
213 # define static_ASN1_SEQUENCE_END_ref(stname, tname) \
215 static_ASN1_ITEM_start(tname) \
216 ASN1_ITYPE_SEQUENCE,\
219 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
225 # define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
227 ASN1_ITEM_start(tname) \
228 ASN1_ITYPE_NDEF_SEQUENCE,\
231 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
238 * This pair helps declare a CHOICE type. We can do:
240 * ASN1_CHOICE(chname) = {
241 * ... CHOICE options ...
242 * ASN1_CHOICE_END(chname)
244 * This will produce an ASN1_ITEM called chname_it
245 * for a structure called chname. The structure
246 * definition must look like this:
250 * ASN1_SOMETHING *opt1;
251 * ASN1_SOMEOTHER *opt2;
255 * the name of the selector must be 'type'.
256 * to use an alternative selector name use the
257 * ASN1_CHOICE_END_selector() version.
260 # define ASN1_CHOICE(tname) \
261 static const ASN1_TEMPLATE tname##_ch_tt[]
263 # define ASN1_CHOICE_cb(tname, cb) \
264 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
267 # define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
269 # define static_ASN1_CHOICE_END(stname) static_ASN1_CHOICE_END_name(stname, stname)
271 # define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
273 # define static_ASN1_CHOICE_END_name(stname, tname) static_ASN1_CHOICE_END_selector(stname, tname, type)
275 # define ASN1_CHOICE_END_selector(stname, tname, selname) \
277 ASN1_ITEM_start(tname) \
279 offsetof(stname,selname) ,\
281 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
287 # define static_ASN1_CHOICE_END_selector(stname, tname, selname) \
289 static_ASN1_ITEM_start(tname) \
291 offsetof(stname,selname) ,\
293 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
299 # define ASN1_CHOICE_END_cb(stname, tname, selname) \
301 ASN1_ITEM_start(tname) \
303 offsetof(stname,selname) ,\
305 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
311 /* This helps with the template wrapper form of ASN1_ITEM */
313 # define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
315 #name, ASN1_ITEM_ref(type) }
317 /* These help with SEQUENCE or CHOICE components */
319 /* used to declare other types */
321 # define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
322 (flags), (tag), offsetof(stname, field),\
323 #field, ASN1_ITEM_ref(type) }
325 /* implicit and explicit helper macros */
327 # define ASN1_IMP_EX(stname, field, type, tag, ex) \
328 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
330 # define ASN1_EXP_EX(stname, field, type, tag, ex) \
331 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
333 /* Any defined by macros: the field used is in the table itself */
335 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
336 # define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
337 # define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
339 # define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
340 # define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
342 /* Plain simple type */
343 # define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
344 /* Embedded simple type */
345 # define ASN1_EMBED(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_EMBED,0, stname, field, type)
347 /* OPTIONAL simple type */
348 # define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
350 /* IMPLICIT tagged simple type */
351 # define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
353 /* IMPLICIT tagged OPTIONAL simple type */
354 # define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
356 /* Same as above but EXPLICIT */
358 # define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
359 # define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
361 /* SEQUENCE OF type */
362 # define ASN1_SEQUENCE_OF(stname, field, type) \
363 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
365 /* OPTIONAL SEQUENCE OF */
366 # define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
367 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
369 /* Same as above but for SET OF */
371 # define ASN1_SET_OF(stname, field, type) \
372 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
374 # define ASN1_SET_OF_OPT(stname, field, type) \
375 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
377 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
379 # define ASN1_IMP_SET_OF(stname, field, type, tag) \
380 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
382 # define ASN1_EXP_SET_OF(stname, field, type, tag) \
383 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
385 # define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
386 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
388 # define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
389 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
391 # define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
392 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
394 # define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
395 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
397 # define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
398 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
400 # define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
401 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
403 /* EXPLICIT using indefinite length constructed form */
404 # define ASN1_NDEF_EXP(stname, field, type, tag) \
405 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
407 /* EXPLICIT OPTIONAL using indefinite length constructed form */
408 # define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
409 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
411 /* Macros for the ASN1_ADB structure */
413 # define ASN1_ADB(name) \
414 static const ASN1_ADB_TABLE name##_adbtbl[]
416 # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
418 # define ASN1_ADB_END(name, flags, field, adb_cb, def, none) \
420 static const ASN1_ADB name##_adb = {\
422 offsetof(name, field),\
425 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
432 # define ASN1_ADB_END(name, flags, field, adb_cb, def, none) \
434 static const ASN1_ITEM *name##_adb(void) \
436 static const ASN1_ADB internal_adb = \
439 offsetof(name, field),\
442 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
446 return (const ASN1_ITEM *) &internal_adb; \
448 void dummy_function(void)
452 # define ADB_ENTRY(val, template) {val, template}
454 # define ASN1_ADB_TEMPLATE(name) \
455 static const ASN1_TEMPLATE name##_tt
458 * This is the ASN1 template structure that defines a wrapper round the
459 * actual type. It determines the actual position of the field in the value
460 * structure, various flags such as OPTIONAL and the field name.
463 struct ASN1_TEMPLATE_st {
464 unsigned long flags; /* Various flags */
465 long tag; /* tag, not used if no tagging */
466 unsigned long offset; /* Offset of this field in structure */
467 const char *field_name; /* Field name */
468 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
471 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
473 # define ASN1_TEMPLATE_item(t) (t->item_ptr)
474 # define ASN1_TEMPLATE_adb(t) (t->item_ptr)
476 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
477 typedef struct ASN1_ADB_st ASN1_ADB;
480 unsigned long flags; /* Various flags */
481 unsigned long offset; /* Offset of selector field */
482 int (*adb_cb)(long *psel); /* Application callback */
483 const ASN1_ADB_TABLE *tbl; /* Table of possible types */
484 long tblcount; /* Number of entries in tbl */
485 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
486 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
489 struct ASN1_ADB_TABLE_st {
490 long value; /* NID for an object or value for an int */
491 const ASN1_TEMPLATE tt; /* item for this value */
496 /* Field is optional */
497 # define ASN1_TFLG_OPTIONAL (0x1)
499 /* Field is a SET OF */
500 # define ASN1_TFLG_SET_OF (0x1 << 1)
502 /* Field is a SEQUENCE OF */
503 # define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
506 * Special case: this refers to a SET OF that will be sorted into DER order
507 * when encoded *and* the corresponding STACK will be modified to match the
510 # define ASN1_TFLG_SET_ORDER (0x3 << 1)
512 /* Mask for SET OF or SEQUENCE OF */
513 # define ASN1_TFLG_SK_MASK (0x3 << 1)
516 * These flags mean the tag should be taken from the tag field. If EXPLICIT
517 * then the underlying type is used for the inner tag.
520 /* IMPLICIT tagging */
521 # define ASN1_TFLG_IMPTAG (0x1 << 3)
523 /* EXPLICIT tagging, inner tag from underlying type */
524 # define ASN1_TFLG_EXPTAG (0x2 << 3)
526 # define ASN1_TFLG_TAG_MASK (0x3 << 3)
528 /* context specific IMPLICIT */
529 # define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
531 /* context specific EXPLICIT */
532 # define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
535 * If tagging is in force these determine the type of tag to use. Otherwise
536 * the tag is determined by the underlying type. These values reflect the
537 * actual octet format.
541 # define ASN1_TFLG_UNIVERSAL (0x0<<6)
542 /* Application tag */
543 # define ASN1_TFLG_APPLICATION (0x1<<6)
544 /* Context specific tag */
545 # define ASN1_TFLG_CONTEXT (0x2<<6)
547 # define ASN1_TFLG_PRIVATE (0x3<<6)
549 # define ASN1_TFLG_TAG_CLASS (0x3<<6)
552 * These are for ANY DEFINED BY type. In this case the 'item' field points to
553 * an ASN1_ADB structure which contains a table of values to decode the
557 # define ASN1_TFLG_ADB_MASK (0x3<<8)
559 # define ASN1_TFLG_ADB_OID (0x1<<8)
561 # define ASN1_TFLG_ADB_INT (0x1<<9)
564 * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes
565 * indefinite length constructed encoding to be used if required.
568 # define ASN1_TFLG_NDEF (0x1<<11)
570 /* Field is embedded and not a pointer */
571 # define ASN1_TFLG_EMBED (0x1 << 12)
573 /* This is the actual ASN1 item itself */
575 struct ASN1_ITEM_st {
576 char itype; /* The item type, primitive, SEQUENCE, CHOICE
578 long utype; /* underlying type */
579 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains
581 long tcount; /* Number of templates if SEQUENCE or CHOICE */
582 const void *funcs; /* functions that handle this type */
583 long size; /* Structure size (usually) */
584 const char *sname; /* Structure name */
588 * These are values for the itype field and
589 * determine how the type is interpreted.
591 * For PRIMITIVE types the underlying type
592 * determines the behaviour if items is NULL.
594 * Otherwise templates must contain a single
595 * template and the type is treated in the
596 * same way as the type specified in the template.
598 * For SEQUENCE types the templates field points
599 * to the members, the size field is the
602 * For CHOICE types the templates field points
603 * to each possible member (typically a union)
604 * and the 'size' field is the offset of the
607 * The 'funcs' field is used for application
608 * specific functions.
610 * The EXTERN type uses a new style d2i/i2d.
611 * The new style should be used where possible
612 * because it avoids things like the d2i IMPLICIT
615 * MSTRING is a multiple string type, it is used
616 * for a CHOICE of character strings where the
617 * actual strings all occupy an ASN1_STRING
618 * structure. In this case the 'utype' field
619 * has a special meaning, it is used as a mask
620 * of acceptable types using the B_ASN1 constants.
622 * NDEF_SEQUENCE is the same as SEQUENCE except
623 * that it will use indefinite length constructed
624 * encoding if requested.
628 # define ASN1_ITYPE_PRIMITIVE 0x0
630 # define ASN1_ITYPE_SEQUENCE 0x1
632 # define ASN1_ITYPE_CHOICE 0x2
634 # define ASN1_ITYPE_EXTERN 0x4
636 # define ASN1_ITYPE_MSTRING 0x5
638 # define ASN1_ITYPE_NDEF_SEQUENCE 0x6
641 * Cache for ASN1 tag and length, so we don't keep re-reading it for things
646 char valid; /* Values below are valid */
647 int ret; /* return value */
648 long plen; /* length */
649 int ptag; /* class value */
650 int pclass; /* class value */
651 int hdrlen; /* header length */
654 /* Typedefs for ASN1 function pointers */
655 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
656 const ASN1_ITEM *it, int tag, int aclass, char opt,
659 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
660 const ASN1_ITEM *it, int tag, int aclass);
661 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
662 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
664 typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
665 int indent, const char *fname,
666 const ASN1_PCTX *pctx);
668 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont,
669 int *putype, const ASN1_ITEM *it);
670 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
671 int len, int utype, char *free_cont,
672 const ASN1_ITEM *it);
673 typedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval,
674 const ASN1_ITEM *it, int indent,
675 const ASN1_PCTX *pctx);
677 typedef struct ASN1_EXTERN_FUNCS_st {
679 ASN1_ex_new_func *asn1_ex_new;
680 ASN1_ex_free_func *asn1_ex_free;
681 ASN1_ex_free_func *asn1_ex_clear;
682 ASN1_ex_d2i *asn1_ex_d2i;
683 ASN1_ex_i2d *asn1_ex_i2d;
684 ASN1_ex_print_func *asn1_ex_print;
687 typedef struct ASN1_PRIMITIVE_FUNCS_st {
690 ASN1_ex_new_func *prim_new;
691 ASN1_ex_free_func *prim_free;
692 ASN1_ex_free_func *prim_clear;
693 ASN1_primitive_c2i *prim_c2i;
694 ASN1_primitive_i2c *prim_i2c;
695 ASN1_primitive_print *prim_print;
696 } ASN1_PRIMITIVE_FUNCS;
699 * This is the ASN1_AUX structure: it handles various miscellaneous
700 * requirements. For example the use of reference counts and an informational
701 * callback. The "informational callback" is called at various points during
702 * the ASN1 encoding and decoding. It can be used to provide minor
703 * customisation of the structures used. This is most useful where the
704 * supplied routines *almost* do the right thing but need some extra help at
705 * a few points. If the callback returns zero then it is assumed a fatal
706 * error has occurred and the main operation should be abandoned. If major
707 * changes in the default behaviour are required then an external type is
711 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
714 typedef struct ASN1_AUX_st {
717 int ref_offset; /* Offset of reference value */
718 int ref_lock; /* Lock type to use */
719 ASN1_aux_cb *asn1_cb;
720 int enc_offset; /* Offset of ASN1_ENCODING structure */
723 /* For print related callbacks exarg points to this structure */
724 typedef struct ASN1_PRINT_ARG_st {
727 const ASN1_PCTX *pctx;
730 /* For streaming related callbacks exarg points to this structure */
731 typedef struct ASN1_STREAM_ARG_st {
732 /* BIO to stream through */
734 /* BIO with filters appended */
736 /* Streaming I/O boundary */
737 unsigned char **boundary;
740 /* Flags in ASN1_AUX */
742 /* Use a reference count */
743 # define ASN1_AFLG_REFCOUNT 1
744 /* Save the encoding of structure (useful for signatures) */
745 # define ASN1_AFLG_ENCODING 2
746 /* The Sequence length is invalid */
747 # define ASN1_AFLG_BROKEN 4
749 /* operation values for asn1_cb */
751 # define ASN1_OP_NEW_PRE 0
752 # define ASN1_OP_NEW_POST 1
753 # define ASN1_OP_FREE_PRE 2
754 # define ASN1_OP_FREE_POST 3
755 # define ASN1_OP_D2I_PRE 4
756 # define ASN1_OP_D2I_POST 5
757 # define ASN1_OP_I2D_PRE 6
758 # define ASN1_OP_I2D_POST 7
759 # define ASN1_OP_PRINT_PRE 8
760 # define ASN1_OP_PRINT_POST 9
761 # define ASN1_OP_STREAM_PRE 10
762 # define ASN1_OP_STREAM_POST 11
763 # define ASN1_OP_DETACHED_PRE 12
764 # define ASN1_OP_DETACHED_POST 13
766 /* Macro to implement a primitive type */
767 # define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
768 # define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
769 ASN1_ITEM_start(itname) \
770 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
771 ASN1_ITEM_end(itname)
773 /* Macro to implement a multi string type */
774 # define IMPLEMENT_ASN1_MSTRING(itname, mask) \
775 ASN1_ITEM_start(itname) \
776 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
777 ASN1_ITEM_end(itname)
779 # define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
780 ASN1_ITEM_start(sname) \
790 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
792 # define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
794 # define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
796 # define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
797 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
799 # define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
800 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
802 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
803 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
805 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
806 pre stname *fname##_new(void) \
808 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
810 pre void fname##_free(stname *a) \
812 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
815 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
816 stname *fname##_new(void) \
818 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
820 void fname##_free(stname *a) \
822 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
825 # define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
826 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
827 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
829 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
830 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
832 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
834 int i2d_##fname(stname *a, unsigned char **out) \
836 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
839 # define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
840 int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
842 return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
845 # define IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(stname) \
846 static stname *d2i_##stname(stname **a, \
847 const unsigned char **in, long len) \
849 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \
850 ASN1_ITEM_rptr(stname)); \
852 static int i2d_##stname(stname *a, unsigned char **out) \
854 return ASN1_item_i2d((ASN1_VALUE *)a, out, \
855 ASN1_ITEM_rptr(stname)); \
859 * This includes evil casts to remove const: they will go away when full ASN1
860 * constification is done.
862 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
863 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
865 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
867 int i2d_##fname(const stname *a, unsigned char **out) \
869 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
872 # define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
873 stname * stname##_dup(stname *x) \
875 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
878 # define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
879 IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
881 # define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
882 int fname##_print_ctx(BIO *out, stname *x, int indent, \
883 const ASN1_PCTX *pctx) \
885 return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \
886 ASN1_ITEM_rptr(itname), pctx); \
889 # define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
890 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
892 # define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
893 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
894 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
896 /* external definitions for primitive types */
898 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
899 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
900 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
901 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
902 DECLARE_ASN1_ITEM(CBIGNUM)
903 DECLARE_ASN1_ITEM(BIGNUM)
904 DECLARE_ASN1_ITEM(LONG)
905 DECLARE_ASN1_ITEM(ZLONG)
907 DEFINE_STACK_OF(ASN1_VALUE)
909 /* Functions used internally by the ASN1 code */
911 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
912 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
914 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
915 const ASN1_ITEM *it, int tag, int aclass, char opt,
918 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
919 const ASN1_ITEM *it, int tag, int aclass);