openssl d2i_TYPE分析

原创
2023/03/26 18:07
阅读数 166

由宏生成的代码:

X509 *X509_new(void)
{
    return (X509 *)ASN1_item_new(X509_it());
}

void X509_free(X509 *a)
{
    ASN1_item_free((ASN1_VALUE *)a, X509_it());
}

X509 *d2i_X509(X509 **a, const unsigned char **in, long len)
{
    return (X509 *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, X509_it());
}

int i2d_X509(const X509 *a, unsigned char **out)
{
    return ASN1_item_i2d((const ASN1_VALUE *)a, out, X509_it());
}

const ASN1_ITEM * X509_it(void)
{
    static const ASN1_ITEM local_it = {
        ASN1_ITYPE_SEQUENCE,
        V_ASN1_SEQUENCE,
        X509_seq_tt,
        sizeof(X509_seq_tt) / sizeof(ASN1_TEMPLATE),
        &X509_aux,
        sizeof(X509),
        "X509"
    };
    return &local_it;
}

static const ASN1_AUX X509_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(X509, references), offsetof(X509, lock), x509_cb, 0, NULL};

static const ASN1_TEMPLATE X509_seq_tt[] = {
    {ASN1_TFLG_EMBED, 0, offsetof(X509, cert_info), "cert_info", X509_CINF_it},
    {ASN1_TFLG_EMBED, 0, offsetof(X509, sig_alg), "sig_alg", X509_ALGOR_it},
    {ASN1_TFLG_EMBED, 0, offsetof(X509, signature), "signature", ASN1_BIT_STRING_it},
};

相关的结构体:

typedef struct x509_st X509;
struct x509_st {
    X509_CINF cert_info;
    X509_ALGOR sig_alg;
    ASN1_BIT_STRING signature;
    X509_SIG_INFO siginf;
    CRYPTO_REF_COUNT references;
    CRYPTO_EX_DATA ex_data;
    /* These contain copies of various extension values */
    // ...
};

typedef struct ASN1_ITEM_st ASN1_ITEM;
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) */
    const char *sname;          /* Structure name */
};

typedef struct ASN1_AUX_st {
    void *app_data;
    int flags;
    int ref_offset;             /* Offset of reference value */
    int ref_lock;               /* Offset of lock value */
    ASN1_aux_cb *asn1_cb;
    int enc_offset;             /* Offset of ASN1_ENCODING structure */
    ASN1_aux_const_cb *asn1_const_cb; /* for ASN1_OP_I2D_ and ASN1_OP_PRINT_ */ // 函数指针
} ASN1_AUX;

/*
 * 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.
 */
typedef struct ASN1_TEMPLATE_st ASN1_TEMPLATE;
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 */
};

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部