📄 asn1t.h
字号:
struct ASN1_ADB_st { unsigned long flags; /* Various flags */ unsigned long offset; /* Offset of selector field */ STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */ const ASN1_ADB_TABLE *tbl; /* Table of possible types */ long tblcount; /* Number of entries in tbl */ const ASN1_TEMPLATE *default_tt; /* Type to use if no match */ const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */};struct ASN1_ADB_TABLE_st { long value; /* NID for an object or value for an int */ const ASN1_TEMPLATE tt; /* item for this value */};/* template flags *//* Field is optional */#define ASN1_TFLG_OPTIONAL (0x1)/* Field is a SET OF */#define ASN1_TFLG_SET_OF (0x1 << 1)/* Field is a SEQUENCE OF */#define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)/* Special case: this refers to a SET OF that * will be sorted into DER order when encoded *and* * the corresponding STACK will be modified to match * the new order. */#define ASN1_TFLG_SET_ORDER (0x3 << 1)/* Mask for SET OF or SEQUENCE OF */#define ASN1_TFLG_SK_MASK (0x3 << 1)/* These flags mean the tag should be taken from the * tag field. If EXPLICIT then the underlying type * is used for the inner tag. *//* IMPLICIT tagging */#define ASN1_TFLG_IMPTAG (0x1 << 3)/* EXPLICIT tagging, inner tag from underlying type */#define ASN1_TFLG_EXPTAG (0x2 << 3)#define ASN1_TFLG_TAG_MASK (0x3 << 3)/* context specific IMPLICIT */#define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT/* context specific EXPLICIT */#define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT/* If tagging is in force these determine the * type of tag to use. Otherwise the tag is * determined by the underlying type. These * values reflect the actual octet format. *//* Universal tag */ #define ASN1_TFLG_UNIVERSAL (0x0<<6)/* Application tag */ #define ASN1_TFLG_APPLICATION (0x1<<6)/* Context specific tag */ #define ASN1_TFLG_CONTEXT (0x2<<6)/* Private tag */ #define ASN1_TFLG_PRIVATE (0x3<<6)#define ASN1_TFLG_TAG_CLASS (0x3<<6)/* These are for ANY DEFINED BY type. In this case * the 'item' field points to an ASN1_ADB structure * which contains a table of values to decode the * relevant type */#define ASN1_TFLG_ADB_MASK (0x3<<8)#define ASN1_TFLG_ADB_OID (0x1<<8)#define ASN1_TFLG_ADB_INT (0x1<<9)/* This flag means a parent structure is passed * instead of the field: this is useful is a * SEQUENCE is being combined with a CHOICE for * example. Since this means the structure and * item name will differ we need to use the * ASN1_CHOICE_END_name() macro for example. */#define ASN1_TFLG_COMBINE (0x1<<10)/* This is the actual ASN1 item itself */struct ASN1_ITEM_st {char itype; /* The item type, primitive, SEQUENCE, CHOICE or extern */long utype; /* underlying type */const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */long tcount; /* Number of templates if SEQUENCE or CHOICE */const void *funcs; /* functions that handle this type */long size; /* Structure size (usually)*/#ifndef NO_ASN1_FIELD_NAMESconst char *sname; /* Structure name */#endif};/* These are values for the itype field and * determine how the type is interpreted. * * For PRIMITIVE types the underlying type * determines the behaviour if items is NULL. * * Otherwise templates must contain a single * template and the type is treated in the * same way as the type specified in the template. * * For SEQUENCE types the templates field points * to the members, the size field is the * structure size. * * For CHOICE types the templates field points * to each possible member (typically a union) * and the 'size' field is the offset of the * selector. * * The 'funcs' field is used for application * specific functions. * * For COMPAT types the funcs field gives a * set of functions that handle this type, this * supports the old d2i, i2d convention. * * The EXTERN type uses a new style d2i/i2d. * The new style should be used where possible * because it avoids things like the d2i IMPLICIT * hack. * * MSTRING is a multiple string type, it is used * for a CHOICE of character strings where the * actual strings all occupy an ASN1_STRING * structure. In this case the 'utype' field * has a special meaning, it is used as a mask * of acceptable types using the B_ASN1 constants. * */#define ASN1_ITYPE_PRIMITIVE 0x0#define ASN1_ITYPE_SEQUENCE 0x1#define ASN1_ITYPE_CHOICE 0x2#define ASN1_ITYPE_COMPAT 0x3#define ASN1_ITYPE_EXTERN 0x4#define ASN1_ITYPE_MSTRING 0x5/* Cache for ASN1 tag and length, so we * don't keep re-reading it for things * like CHOICE */struct ASN1_TLC_st{ char valid; /* Values below are valid */ int ret; /* return value */ long plen; /* length */ int ptag; /* class value */ int pclass; /* class value */ int hdrlen; /* header length */};/* Typedefs for ASN1 function pointers */typedef ASN1_VALUE * ASN1_new_func(void);typedef void ASN1_free_func(ASN1_VALUE *a);typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, unsigned char ** in, long length);typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);typedef int ASN1_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx);typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);typedef struct ASN1_COMPAT_FUNCS_st { ASN1_new_func *asn1_new; ASN1_free_func *asn1_free; ASN1_d2i_func *asn1_d2i; ASN1_i2d_func *asn1_i2d;} ASN1_COMPAT_FUNCS;typedef struct ASN1_EXTERN_FUNCS_st { void *app_data; ASN1_ex_new_func *asn1_ex_new; ASN1_ex_free_func *asn1_ex_free; ASN1_ex_free_func *asn1_ex_clear; ASN1_ex_d2i *asn1_ex_d2i; ASN1_ex_i2d *asn1_ex_i2d;} ASN1_EXTERN_FUNCS;typedef struct ASN1_PRIMITIVE_FUNCS_st { void *app_data; unsigned long flags; ASN1_ex_new_func *prim_new; ASN1_ex_free_func *prim_free; ASN1_ex_free_func *prim_clear; ASN1_primitive_c2i *prim_c2i; ASN1_primitive_i2c *prim_i2c;} ASN1_PRIMITIVE_FUNCS;/* This is the ASN1_AUX structure: it handles various * miscellaneous requirements. For example the use of * reference counts and an informational callback. * * The "informational callback" is called at various * points during the ASN1 encoding and decoding. It can * be used to provide minor customisation of the structures * used. This is most useful where the supplied routines * *almost* do the right thing but need some extra help * at a few points. If the callback returns zero then * it is assumed a fatal error has occurred and the * main operation should be abandoned. * * If major changes in the default behaviour are required * then an external type is more appropriate. */typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it);typedef struct ASN1_AUX_st { void *app_data; int flags; int ref_offset; /* Offset of reference value */ int ref_lock; /* Lock type to use */ ASN1_aux_cb *asn1_cb; int enc_offset; /* Offset of ASN1_ENCODING structure */} ASN1_AUX;/* Flags in ASN1_AUX *//* Use a reference count */#define ASN1_AFLG_REFCOUNT 1/* Save the encoding of structure (useful for signatures) */#define ASN1_AFLG_ENCODING 2/* The Sequence length is invalid */#define ASN1_AFLG_BROKEN 4/* operation values for asn1_cb */#define ASN1_OP_NEW_PRE 0#define ASN1_OP_NEW_POST 1#define ASN1_OP_FREE_PRE 2#define ASN1_OP_FREE_POST 3#define ASN1_OP_D2I_PRE 4#define ASN1_OP_D2I_POST 5#define ASN1_OP_I2D_PRE 6#define ASN1_OP_I2D_POST 7/* Macro to implement a primitive type */#define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)#define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \ ASN1_ITEM_start(itname) \ ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \ ASN1_ITEM_end(itname)/* Macro to implement a multi string type */#define IMPLEMENT_ASN1_MSTRING(itname, mask) \ ASN1_ITEM_start(itname) \ ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \ ASN1_ITEM_end(itname)/* Macro to implement an ASN1_ITEM in terms of old style funcs */#define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)#define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \ static const ASN1_COMPAT_FUNCS sname##_ff = { \ (ASN1_new_func *)sname##_new, \ (ASN1_free_func *)sname##_free, \ (ASN1_d2i_func *)d2i_##sname, \ (ASN1_i2d_func *)i2d_##sname, \ }; \ ASN1_ITEM_start(sname) \ ASN1_ITYPE_COMPAT, \ tag, \ NULL, \ 0, \ &sname##_ff, \ 0, \ #sname \ ASN1_ITEM_end(sname)#define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \ ASN1_ITEM_start(sname) \ ASN1_ITYPE_EXTERN, \ tag, \ NULL, \ 0, \ &fptrs, \ 0, \ #sname \ ASN1_ITEM_end(sname)/* Macro to implement standard functions in terms of ASN1_ITEM structures */#define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)#define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)#define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \ IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \ stname *fname##_new(void) \ { \ return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \ } \ void fname##_free(stname *a) \ { \ ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \ }#define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \ IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)#define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ stname *d2i_##fname(stname **a, unsigned char **in, long len) \ { \ return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\ } \ int i2d_##fname(stname *a, unsigned char **out) \ { \ return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\ } /* This includes evil casts to remove const: they will go away when full * ASN1 constification is done. */#define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ stname *d2i_##fname(stname **a, const unsigned char **in, long len) \ { \ return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, (unsigned char **)in, len, ASN1_ITEM_rptr(itname));\ } \ int i2d_##fname(const stname *a, unsigned char **out) \ { \ return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\ } #define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \ stname * stname##_dup(stname *x) \ { \ return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \ }#define IMPLEMENT_ASN1_FUNCTIONS_const(name) \ IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)#define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \ IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)/* external definitions for primitive types */DECLARE_ASN1_ITEM(ASN1_BOOLEAN)DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)DECLARE_ASN1_ITEM(ASN1_ANY)DECLARE_ASN1_ITEM(ASN1_SEQUENCE)DECLARE_ASN1_ITEM(CBIGNUM)DECLARE_ASN1_ITEM(BIGNUM)DECLARE_ASN1_ITEM(LONG)DECLARE_ASN1_ITEM(ZLONG)DECLARE_STACK_OF(ASN1_VALUE)/* Functions used internally by the ASN1 code */int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);int ASN1_template_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_TEMPLATE *tt);int ASN1_item_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx);int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt);void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);int asn1_enc_save(ASN1_VALUE **pval, unsigned char *in, int inlen, const ASN1_ITEM *it);#ifdef __cplusplus}#endif#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -