pythonasn.1pyasn1

pyasn1 vs indefinite-length structures


Is it a bug that pyasn1 cannot parse unaided (without defining a new type) the indefinite-length constructed data described in the code snippet below, or is my example not valid BER-encoded ASN.1?

If pyasn1 can't handle this without help, is there another python library I could turn to?


# 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)

Solution

  • Your example is perfectly valid (BER encoding)

    You can even use https://asn1.io/asn1playground/ to prove that

    Compile following schema:

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

    And decode e7 80 02 01 44 02 01 55 00 00

    Result will be:

    > 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 }
    

    Note that you don't need a schema to decode that (you would just loose the semantic)

    You will want insight from pyasn1. Try opening an issue here: https://github.com/etingof/pyasn1/issues