asn1crypto icon indicating copy to clipboard operation
asn1crypto copied to clipboard

How to parse concatenated encodings?

Open joernheissler opened this issue 6 years ago • 1 comments

My test data from #149 is the concatenated encoding of multiple ASN.1 values:

4f 10     d2760001240103030005000069410000
5f 52 0a  0031f573c00160059000
7f 66 08  0202080002020800
73 81 b7  c0 0a  7f000800080008000001
          c1 06  011000002000
          c2 06  010800002000
          c3 06  010800002000
          c4 07  01404040030000
          c5 3c  2c4d8093d27c463536a79f5d52b59e4f0805526f00000000000000000000
                 000000000000000000000000000000000000000000000000000000000000
          c6 3c  000000000000000000000000000000000000000000000000000000000000
                 000000000000000000000000000000000000000000000000000000000000
          cd 0c  5d86547c0000000000000000

Currently I use core.Concat to parse this, but the specs for the OpenPGP card don't define which values must be present and in which order. So it's a very brittle solution.

Another option is to wrap above data inside an encoded set with indefinite length and mark all fields as optional: MyClass.load(b'\x31\x80' + data + b'\x00\x00'). Works, but not a great solution either.

Is there currently any other option?

joernheissler avatar Sep 22 '19 20:09 joernheissler

To use the current API, you'd need to use asn1crypto.parser.parse(). This will be a little wasteful since you don't really want the header, contents and trailer returned as separate byte strings, but you will need them to calculate the length that was consumed.

It may be worthwhile to add a new method like asn1crypto.parser.peek() that returns the class, method, tag and length. Then you could use that to determine which to parse with.

A more high-level approach would be to create an asn1crypto.core.Choice with the appropriate class/tag values and repeatedly apply that. I'm not sure if this would work, as it depends on whether all of the messages have a unique class and tag. There would be a little bit of hacking since you'd have to determine how much of the input was consumed.

wbond avatar Sep 25 '19 17:09 wbond