我想pyasn1
用来解码一些数据,其中一部分是不透明的。也就是说,包含在 ASN.1 定义的结构中的部分数据可能是也可能不是 ASN.1 可解码的,我需要解析前导码以了解如何对其进行解码。
根据我对“解码未标记类型”的pyasn1 编解码器文档的理解,我应该能够使用该pyasn.univ.Any
类型来处理这种情况。
这是一些示例代码来说明我遇到的问题。
#!/usr/bin/env python
from pyasn1.type import univ, namedtype
from pyasn1.codec.der import decoder, encoder
class Example(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.NamedType('spam', univ.Integer()),
namedtype.NamedType('eggs', univ.Any())
)
example = Example()
example['spam'] = 42
example['eggs'] = univ.Any(b'\x01\x00abcde') # Some opaque data
substrate = encoder.encode(example)
"""
>>> import binascii
>>> print(binascii.hexlify(substrate).decode('ascii')))
300a02012a01006162636465
^^ ^
|| + Opaque data begins here
++ Note: the length field accounts for all remaining substrate
"""
data, tail = decoder.decode(substrate, asn1Spec=Example())
print(data)
编码示例符合我的预期。但是,该程序在解码器内部失败,并带有以下回溯。
Traceback (most recent call last):
File "./any.py", line 27, in <module>
data, tail = decoder.decode(substrate, asn1Spec=Example())
File "/Users/neirbowj/Library/Python/3.4/lib/python/site-packages /pyasn1-0.1.8-py3.4.egg/pyasn1/codec/ber/decoder.py", line 825, in __call__
File "/Users/neirbowj/Library/Python/3.4/lib/python/site-packages/pyasn1-0.1.8-py3.4.egg/pyasn1/codec/ber/decoder.py", line 342, in valueDecoder
File "/Users/neirbowj/Library/Python/3.4/lib/python/site-packages/pyasn1-0.1.8-py3.4.egg/pyasn1/codec/ber/decoder.py", line 706, in __call__
pyasn1.error.SubstrateUnderrunError: 95-octet short
我相信发生的事情是解码器正在尝试处理我试图识别为univ.Any
失败的数据部分——因为它不是有效的编码——而不是将它作为封装的一些二进制数据返回给我univ.Any
正如我所期望的那样在一个对象中。
如何使用 解析这种形式的数据pyasn1
?
顺便说一句,我尝试解码的实际数据是使用 GSSAPI 机制的 SASL 令牌,如RFC 4121: KRB5 GSSAPI 机制 v2的第 4.1 节中定义的那样,为了方便起见,我在此摘录。
GSS-API DEFINITIONS ::=
BEGIN
MechType ::= OBJECT IDENTIFIER
-- representing Kerberos V5 mechanism
GSSAPI-Token ::=
-- option indication (delegation, etc.) indicated within
-- mechanism-specific token
[APPLICATION 0] IMPLICIT SEQUENCE {
thisMech MechType,
innerToken ANY DEFINED BY thisMech
-- contents mechanism-specific
-- ASN.1 structure not required
}
END
The innerToken field starts with a two-octet token-identifier
(TOK_ID) expressed in big-endian order, followed by a Kerberos
message.
Following are the TOK_ID values used in the context establishment
tokens:
Token TOK_ID Value in Hex
-----------------------------------------
KRB_AP_REQ 01 00
KRB_AP_REP 02 00
KRB_ERROR 03 00
编辑 1:附加示例数据
我相信这是由 cyrus-sasl 和 heimdal 序列化的示例 GSSAPI-Token(经过轻微清理)。
YIIChwYJKoZIhvcSAQICAQBuggJ2MIICcqADAgEFoQMCAQ6iBwMFACAAAACjggFm
YYIBYjCCAV6gAwIBBaELGwlBU04uMVRFU1SiNjA0oAMCAQGhLTArGwtzZXJ2aWNl
bmFtZRscc2VydmljZWhvc3QudGVzdC5leGFtcGxlLmNvbaOCARAwggEMoAMCARCh
AwIBBKKB/wSB/A81akUNsyvRCCKtERWg9suf96J3prMUQkabsYGpzijfEeCNe0ja
Eq6c87deBG+LeJqFIyu65cCMF/oXtyZNB9sUxpqFBcfkAYZXTxabNLpZAUmkdt6w
dYlV8JK/G3muuG/ziM14oCbh8hIY63oi7P/Pdyrs3s8B+wkNCpjVtREHABuF6Wjx
GYem65mPqCP9ZMSyD3Bc+dLemxhm7Kap8ExoVYFRwuFqvDf/E5MLCk2HThw46UCF
DqFnU46FJBNGAK+RN2EptsqtY48gb16klqJxU7bwHeYoCsdXyB6GElIDe1qrPU15
9mGxpdmSElcVxB/3Yzei48HzlkUcfqSB8jCB76ADAgEQooHnBIHkZUyd0fJO3Bau
msqz6ndF+kBxmrGS6Y7L20dSYDI2cB8HsJdGDnEODsAAcYQ0L5c2N/mb8QHh7iU9
gtjWHpfq/FqMF4/aox/BJ0Xzuy2gS4sCafs7PTYtSDh2nyLkNYuxKdmQ1ughbIq6
APAegqa7R1iv2oCaNijrpKc2YUfznnwT/CTSsGrJpMwz4KLuBtjI4f74bQty8uNn
LVxxV4J8wU1s7lSj4Ipbi+a1WdCVsLs8lIqFmKXte+1c+qHeadoAGmSTBT3qFZae
SRdT8dpYr6i6fkjRsoyEZs9ZqQtwQAYSdMBU