OPENSSL

Template Method in OpenSSL
asn1.h
asn1t.h

/*-
 * 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.
 *
 * 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.
 *
 * NDEF_SEQUENCE is the same as SEQUENCE except that it will use indefinite length constructed encoding if requested.
 *
 */

# define ASN1_ITYPE_PRIMITIVE            0x0

# define ASN1_ITYPE_SEQUENCE             0x1

# define ASN1_ITYPE_CHOICE               0x2

# define ASN1_ITYPE_EXTERN               0x4

# define ASN1_ITYPE_MSTRING              0x5

# define ASN1_ITYPE_NDEF_SEQUENCE        0x6

/* These help with SEQUENCE or CHOICE components */

/* used to declare other types */

# define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
        (flags), (tag), offsetof(stname, field),\
        #field, ASN1_ITEM_ref(type) }

# define ASN1_SEQUENCE(tname) \
        static const ASN1_TEMPLATE tname##_seq_tt[]

# define static_ASN1_NDEF_SEQUENCE_END(tname) \
        ;\
        static_ASN1_ITEM_start(tname) \
                ASN1_ITYPE_NDEF_SEQUENCE,\
                V_ASN1_SEQUENCE,\
                tname##_seq_tt,\
                sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
                NULL,\
                sizeof(tname),\
                #tname \
        ASN1_ITEM_end(tname)

/* ASN1_ITEM pointer exported type */
typedef const ASN1_ITEM ASN1_ITEM_EXP;

/* 
 * This is the ASN1 template structure that defines a wrapper round the actual type. It determines the actual position of the field in the value structure, various flags such as OPTIONAL and the field name.
 */
struct ASN1_TEMPLATE_st {
  unsigned long flags;        /* Various flags */
  long tag;                   /* tag, not used if no tagging */
  unsigned long offset;       /* Offset of this field in structure */
  const char *field_name;     /* Field name */
  ASN1_ITEM_EXP *item;        /* Relevant ASN1_ITEM or ASN1_ADB */
};

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 count;                         /* Number of templates if SEQUENCE or CHOICE */
  const void *funcs;                  /* functions that handle this type */
  long size;                          /* Structure size (usually) */
  const char *sname;                  /* Structure name */
};

typedef struct ASN1_ENCODING_st {
    unsigned char *enc;         /* DER encoding */
    long len;                   /* Length of encoding */
    int modified;               /* set to 1 if 'enc' is invalid */
} ASN1_ENCODING;

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;

/* 
 * 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 */
};
int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1,
                         X509_ALGOR *algor2, ASN1_BIT_STRING *signature, void *asn,
                         EVP_PKEY *peky, const EVP_MD *type)
struct evp_md_st {
  int type;
  int pkey_type;
  int md_size;
  unsigned long flags;
  int (*init) (EVP_MD_CTX *ctx);
  int (*update) (EVP_MD_CTX *ctx, const void *data, size_t count);
  int (*final) (EVP_MD_CTX *ctx, unsigned char *md);
  int (*copy) (EVP_MD_CTX *to, const EVP_MD_CTX *from);
  int (*cleanup) (EVP_MD_CTX *ctx);
  int block_size;
  int ctx_size;
  int (*md_ctrl) (EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
} /* EVP_MD */;

OPENSSL Wiki
Compilation_and_Installation
How_to_Integrate_a_Symmetric_Cipher
How_To_Write_Unit_Tests_For_OpenSSL
OpenSSL Command-Line HOWTO
Command Line Utilities
Stack API
CSDN文章
openssl源码之ASN1从找不到X509_new的定义说起
asn1编码格式的解析过程
openssl 代码分析(4) ASN1_item_ex_d2i
OPENSSL
ASN1_AUX
ASN1_ITEM
EVP_MD
Programming Tools - Opaque Pointers

posted @ 2021-12-24 18:08  anyboo  阅读(85)  评论(0编辑  收藏  举报