2

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)
  • There are a number of reasons that this code might be failing. The obvious one is that your data is just wrong. My guess is that this is the case. I doubt that there's a bug in a module here. With that said, it might be that your data is technically "correct", but you're making the wrong call to process it, or you haven't configured your environment correctly to accept and process the format of your data. - I don't know the details of this operation, so I can't tell you what is specifically wrong here. – CryptoFool Jun 22 '19 at 15:57
  • Replace **e7** with **a0** if you'd prefer a more normal-looking example. – PythonAteMyHamster Jun 22 '19 at 21:32
  • Now noted at https://github.com/etingof/pyasn1/issues/190 – PythonAteMyHamster Mar 28 '20 at 15:31

2 Answers2

1

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

YaFred
  • 9,698
  • 3
  • 28
  • 40
-1

Your example data appears to be malformed. I don't know the details, but it seems to involve the first byte of your data, 'e7'. I understand this to be the "type" of the message. It seem that this type must expect more data than you're giving it.

I see examples that use '30' as the first byte, representing a "sequence". These examples are formatted much like yours. So I tried replacing 'e7' with '30' in your example data, and with this change, your code runs without errors.

To be clear in what I'm saying, this code runs for me without errors:

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

I believe that this shows you that your code is "correct". I wish I knew more about this stuff so I could be more help, like to explain to you whatn the "e7" type is all about. Without that, I hope this is still helpful.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • **e7** was just mischievous ... **a0** (context-specific constructed type) is the canonical example of where things go wrong. It appears that **pyasn1** doesn't play nicely with non-universal constructed types. – PythonAteMyHamster Jun 22 '19 at 21:30