3

I have this little part of code:

from pyasn1.type import univ
from pyasn1.codec.ber import decoder

decoder.decode(binary_file.read(5))

my binary_file variable it's a particular binary file encoded (CDR)

if i try to decode the readed part it gives me this error:

pyasn1.error.PyAsn1Error: [128:0:0]+[128:32:79] not in asn1Spec: None

how can i fix?

1 Answers1

1

Unless you are decoding a data structure that only contains base ASN.1 types (such as INTEGER, SEQUENCE etc.), you need to pass your top-level ASN.1 data structure object to decoder. That way decoder could match custom tags (of TLV tuples in BER/DER/CER serialization) with the same tags present in data structure object. For example:

custom_int_type = Integer().subtype(implicitTag=Tag(tagClassContext, tagFormatSimple, 40))

custom_int_instance = custom_int_type.clone(12345)
serialization = encode(custom_int_instance)

# this will fail on unknown custom ASN.1 type tag
custom_int_instance, rest_of_serialization = decode(serialization)

# this will succeed as custom ASN.1 type (containing tag) is provided
custom_int_instance, rest_of_serialization = decode(serialization, asn1Spec=custom_int_type)

Here is a link to pyasn1 documentation on decoders.

To pass ASN.1 grammar to pyasn1 decoder you have to first turn your grammar into pyasn1/Python tree of objects. That is a one-time operation that sometimes can be automated with the asn1late tool.

My other concern is that you are possibly reading a fraction of your serialized data (5 octets). That could be a valid operation if your data was serialized using "indefinite length encoding mode", otherwise decoder may fail on insufficient input.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • am i able to use a custom .asn file to encode? something kinda: decoder.decode(var1, asn1Spec='mycustomASNfile') where var1 = binary_file.read(190) from a file which was previously encoded following the "ASN rules/structure" of my custom .asn file? (BER serialization) or i must translate the .asn file to a .py? if yes can you give me the proper syntax? thnx for helping ^^ – Aldin Selimbasic Jul 14 '16 at 07:27
  • You must translate .asn1 file into pyasn1/Python classes. That is a one-time operation which can possibly be handled by [asn1late](https://github.com/kimgr/asn1ate) tool. – Ilya Etingof Jul 26 '16 at 11:19