2

pyasn1无法单独(不定义新类型)解析下面代码片段中描述的不定长度构造数据是一个错误,还是我的示例不是有效的 BER 编码 ASN.1?

如果 pyasn1 在没有帮助的情况下无法处理这个问题,我可以求助于另一个 python 库吗?


# e7 80       : private, constructed, tag 7, indefinite length
#    02 01 44 : integer 0x44
#    02 01 55 : integer 0x55
#    00 00    : end of contents (terminating the 0xe7 object)
data = 'e7 80 02 01 44 02 01 55 00 00'

data = binascii.unhexlify( ''.join(data.split()) )

# throws pyasn1.error.PyAsn1Error: Missing end-of-octets terminator
pyasn1.codec.ber.decoder.decode(data)
4

2 回答 2

1

您的示例完全有效(BER 编码)

你甚至可以使用https://asn1.io/asn1playground/来证明

编译以下架构:

Schema DEFINITIONS EXPLICIT TAGS ::= 
BEGIN
  ASequence::= [PRIVATE 7] IMPLICIT SEQUENCE       
  {                                                     
    num1 INTEGER,
    num2 INTEGER
  }                                                     
END

并解码e7 80 02 01 44 02 01 55 00 00

结果将是:

> OSS ASN-1Step Version 9.0.1 Copyright (C) 2019 OSS Nokalva, Inc.  All
> rights reserved. This product is licensed for use by "OSS Nokalva,
> Inc."
> 
> C0043I: 0 error messages, 0 warning messages and 0 informatory
> messages issued.
> 
> 
> ASN1STEP: Decoding PDU #1 :
> 
> ASequence SEQUENCE: tag = [PRIVATE 7] constructed; length = indef  
> num1 INTEGER: tag = [UNIVERSAL 2] primitive; length = 1
>     68   
> num2 INTEGER: tag = [UNIVERSAL 2] primitive; length = 1
>     85 
> EOC 
> Successfully decoded 10 bytes. rec1value ASequence ::=  {   num1 68,   num2 85 }

请注意,您不需要模式来解码它(您只会失去语义)

您将需要 pyasn1 的见解。尝试在这里打开一个问题:https ://github.com/etingof/pyasn1/issues

于 2019-06-25T11:57:09.540 回答
-1

您的示例数据似乎格式不正确。我不知道细节,但它似乎涉及数据的第一个字节,'e7'。我理解这是消息的“类型”。似乎这种类型的数据必须比您提供的更多。

我看到使用'30'作为第一个字节的例子,代表一个“序列”。这些示例的格式与您的非常相似。因此,我尝试在您的示例数据中将 'e7' 替换为 '30',并且通过此更改,您的代码可以正常运行。

为了清楚我所说的,这段代码为我运行而没有错误:

# 30 80       : sequence, indefinite length
#    02 01 44 : integer 0x44
#    02 01 55 : integer 0x55
#    00 00    : end of contents (terminating the 0x30 object)
data = '30 80 02 01 44 02 01 55 00 00'

data = binascii.unhexlify( ''.join(data.split()) )

# throws pyasn1.error.PyAsn1Error: Missing end-of-octets terminator
pyasn1.codec.ber.decoder.decode(data)

我相信这表明您的代码是“正确的”。我希望我对这些东西有更多的了解,以便我能提供更多帮助,比如向您解释“e7”类型的全部内容。没有那个,我希望这仍然有帮助。

于 2019-06-22T16:34:40.850 回答