openssl d2i_TYPE分析

由宏生成的代碼:

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 */
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章