etingof / pyasn1

Generic ASN.1 library for Python
http://snmplabs.com/pyasn1
BSD 2-Clause "Simplified" License
243 stars 118 forks source link

Issues with BER indefinite-length decoding #190

Open ktptemfau opened 4 years ago

ktptemfau commented 4 years ago

Hi,

I am not sure what I am doing wrong or if I am hitting a bug The data I'm trying to decode is an CMS envelopedData blob which openssl decodes as following

  0:d=0  hl=2 l=inf  cons: SEQUENCE          
  2:d=1  hl=2 l=  11 prim: OBJECT            :id-smime-ct-authEnvelopedData
 15:d=1  hl=2 l=inf  cons: cont [ 0 ]        
 17:d=2  hl=2 l=inf  cons: SEQUENCE                      
 19:d=3  hl=2 l=   1 prim: INTEGER           :00
 22:d=3  hl=4 l= 471 cons: SET                                                                                              
 26:d=4  hl=4 l= 467 cons: SEQUENCE             
 30:d=5  hl=2 l=   1 prim: INTEGER           :00
 ...
 ..,
958:d=3  hl=2 l=  16 prim: OCTET STRING      [HEX DUMP]:ABA7DBB789D8FE7B137D3370E58007B6
976:d=3  hl=2 l=   0 prim: EOC               
978:d=2  hl=2 l=   0 prim: EOC               
980:d=1  hl=2 l=   0 prim: EOC  

When I decode the data like this

with open(self.filename,'r') as file:
    data = file.read()
obj, rest  = ber_decoder(base64.b64decode(data), asn1Spec=rfc5652.ContentInfo(), decodeOpenTypes=False)

and print obj this is what I get

<class 'pyasn1_modules.rfc5652.ContentInfo'>
ContentInfo:
 contentType=1.2.840.113549.1.9.16.1.23
 content=0x3080.....
  ...
  ...
  ....a0410aba7dbb789d8fe7b137d3370e58007b6

If I decode this way

  obj, rest  = ber_decoder(base64.b64decode(data), asn1Spec=rfc5652.ContentInfo(), decodeOpenTypes=True)

I end up with

SubstrateUnderrunError: No EOO seen before substrate ends

Looking at the printout of the decoded data, I can understand the exeception as the EOC bytes of 'content' is missing.

Any hints or pointers on how I can resolve this issue?

Thanks for your time!

qsqa commented 4 years ago

See also https://stackoverflow.com/questions/56716754/pyasn1-vs-indefinite-length-structures