作用:
主要就是解析ASN.1 BER编码的二进制数据,ASN.1本身只定义了表示信息的抽象句法,但是没有限定其编码的方法。各种ASN.1编码规则提供了由ASN.1描述其抽象句法的数据的值的传送语法(具体表达)。标准的ASN.1编码规则有基本编码规则(BER,Basic Encoding Rules)、规范编码规则(CER,Canonical Encoding Rules)、唯一编码规则(DER,Distinguished Encoding Rules)、压缩编码规则(PER,Packed Encoding Rules)和XML编码规则(XER,XML Encoding Rules)。
功能描述:
ASN1_get_object() parses the identifier and length octets of a BER-encoded value. On function entry, ber_in is expected to point to the first identifier octet. If the identifier and length octets turn out to be valid, the function advances ber_in to the first content octet before returning.

If the identifier octets are valid, ASN1_get_object() stores the tag number in ptag and the class of the tag in pclass. The class is either V_ASN1_UNIVERSAL or V_ASN1_APPLICATION or V_ASN1_CONTEXT_SPECIFIC or V_ASN1_PRIVATE.

If the length octets are valid, too, ASN1_get_object() stores the number encoded in the length octets in plength. If the length octet indicates the indefinite form, plength is set to 0.

ASN1_get_object() inspects at most omax bytes. If parsing of the length octets remains incomplete after inspecting that number of bytes, parsing fails with ASN1_R_HEADER_TOO_LONG.

返回值:
Bits set in the return value of ASN1_get_object() have the following meanings:

0x80
An error occurred. One of the ERRORS described below has been set.
0x20 = V_ASN1_CONSTRUCTED
The encoding is constructed rather than primitive, and the identifier and length octets are valid.
0x01
The length octet indicates the indefinite form. This bit can only occur if V_ASN1_CONSTRUCTED is also set.
Consequently, the following combinations can occur:

0x00
A valid primitive encoding.
0x20
A valid constructed encoding, definite form.
0x21
A valid constructed encoding, indefinite form.
0x80
Either a primitive encoding with a valid tag and definite length, but the content octets won't fit into omax, or parsing failed. Use ERR_GET_REASON(3) to distinguish the two cases.
0xa0
A constructed encoding with a valid tag and definite length, but the content octets won't fit into omax.
The bit combinations 0x01, 0x81, and 0xa1 cannot occur as return values.

示例用法:

ASN1_OCTET_STRING* octet_str = X509_EXTENSION_get_data(extension);
const unsigned char* octet_str_data = octet_str->data;
long xlen;
int tag, xclass;
int ret = ASN1_get_object(&octet_str_data, &xlen, &tag, &xclass, octet_str->length);
printf("value: %s\n", octet_str_data);

示例用法二:

static bool asn1_parse_integer(const unsigned char **asn1data_pos, long length, BIGNUM *bn_result) {
    debug_printf("> asn1_parse_integer(%p, %li, %p)\n",
                 (void *) asn1data_pos, length, (void *) bn_result);
    bool result = true;
    long len;
    int ret, tag, xclass;
    ret = ASN1_get_object(asn1data_pos, &len, &tag, &xclass, length);
    if (ret & 0x80) {
        fprintf(stderr, "ASN1_get_object() failed\n");
        result = false;
    }
    if (tag != V_ASN1_INTEGER) {
        fprintf(stderr, "Invalid tag for ASN1: %d (%s)\n", tag, ASN1_tag2str(tag));
        result = false;
    }
    if (result) {
        ASN1_INTEGER *temp = ASN1_INTEGER_new();
        if (!c2i_ASN1_INTEGER(&temp, asn1data_pos, len)) {
            fprintf(stderr, "d2i_ASN1_INTEGER() failed\n");
            result = false;
        }
        ASN1_INTEGER_to_BN(temp, bn_result);
        ASN1_INTEGER_free(temp);
    }

    debug_printf("< asn1_parse_integer(): %u\n", result);
    return result;
}
本文链接地址:https://const.net.cn/401.html

标签: none

添加新评论