特定解码选择ASN.1

问题描述 投票:0回答:2

我正在使用erlang ASN.1编译器,并且具有以下ASN.1定义:

DecryptedCertificate ::= SEQUENCE {
    certificateProfileIdentifier INTEGER(0..255),
    certificateAuthorityReference CertificateAuthority,
    certificateHolderAuthorization CertificateHolderAuthorization,
    endOfValidity TimeReal,
    certificateHolderReference KeyIdentifier,
    rsaPublicKey RsaPublicKey
}

KeyIdentifier ::= CHOICE {
    extendedSerialNumber ExtendedSerialNumber,
    certificateRequestID CertificateRequestID,
    certificationAuthorityKID CertificationAuthorityKID
}

当我解码二进制文件时,它总是选择CertificateRequestID选项,我想为解码器指定一个特定的选择,可以吗?

PS:我正在使用PER。

erlang elixir asn.1
2个回答
0
投票

[深入研究异构系统之间的Asn.1通信之后,我发现了一个名为Selecting a CHOICE alternative的漂亮段落,该段落准确地指定了我要寻找的内容:

选择的类型可以通过左尖括号“

所以我的结构的解决方案是使用:

DecryptedCertificate ::= SEQUENCE {
    certificateProfileIdentifier INTEGER(0..255),
    certificateAuthorityReference CertificateAuthority,
    certificateHolderAuthorization CertificateHolderAuthorization,
    endOfValidity TimeReal,
    certificateHolderReference certificationAuthorityKID < KeyIdentifier,
    rsaPublicKey RsaPublicKey
}

当然,我本人也可以对类型进行硬编码,但是在规范修改和可读性方面更灵活。


0
投票

当我解码二进制文件时,它总是选择CertificateRequestID选项,我想为解码器指定一个特定的选择,可以吗?

无法指定要解码的内容。当您解码特定的二进制消息/记录/ PDU时,解码器将根据ASN.1定义和UPER / APER编码规则来选择二进制文件中包含的任何内容。

[二进制文件中的某处,有两个位确定KeyIdentifier所包含的内容,如果您能够找到并更改它们,则解码器将尝试对另一个字段进行解码,但是由于您的二进制消息实际上包含了该字段,因此很可能会失败一个不同的字段。

[您可以尝试创建KeyIdentifier,填充所需的任何值,然后对其进行编码,以了解该不同二进制文件的外观。

UPDATE

PER格式不包含选项类型的标题。

在PER编码中,CHOICE确实包含指定编码类型的索引(标头)。参见X.691 23 Encoding the choice type

23 Encoding the choice type
NOTE – (Tutorial) A choice type is encoded by encoding an index specifying the
chosen alternative. This is encoded as for a constrained integer (unless the 
extension marker is present in the choice type, in which case it is a normally
small non-negative whole number) and would therefore typically occupy a fixed
length bit-field of the minimum number of bits needed to encode the index.
Although it could in principle be arbitrarily large.) This is followed by the
encoding of the chosen alternative, with alternatives that are extension 
additions encoded as if they were the value of an open type field. Where the
choice has only one alternative, there is no encoding for the index.
© www.soinside.com 2019 - 2024. All rights reserved.