P1sec / pycrate

A Python library to ease the development of encoders and decoders for various protocols and file formats; contains ASN.1 and CSN.1 compilers.
GNU Lesser General Public License v2.1
381 stars 132 forks source link

Handle CONTAINING contraint on optional CLASS field #140

Closed benmaddison closed 3 years ago

benmaddison commented 3 years ago

Resolves #139

This adds logic to the decoding of BIT STRING and OCTET STRING values with a CONTAINING constraint.

An exception is raised if a matching object information instance was found on the lookup-table of the OPEN object, but the instance did not include the referenced field.

The exception causes processing of the CONTAINING constraint to abort, and fall back to decoding the value as a bare BIT/OCTET STRING.

The case where the referenced field is OPTIONAL on the CLASS definition is distinguishable by the type of exception raised. A warning is only printed where the field is non-OPTIONAL.

Updated exception handling has been implemented for BER/DER, and those that clearly follow the same logic.

Similar logic may be needed in the JER/OER/etc. cases. I am not familiar enough with those code-paths to provide this without some guidance.

p1-bmu commented 3 years ago

If your raise in _get_tab_obj(), bad things will happen in the decoding of OPEN types (https://github.com/P1sec/pycrate/blob/aef4e0ee9f5f70afd2bdacac3147983cbf05c05d/pycrate_asn1rt/asnobj_ext.py#L76). This is because an OPEN object can get its value by other ways than just doing a look up in a constraint table, during the decoding process.

As such, I am not going to pull your PR. I need to further investigate this and will try to develop another solution.

benmaddison commented 3 years ago

fixed in https://github.com/P1sec/pycrate/commit/7f3647e4bfa166c9eca3f8c5bb168bf7da0c39ad instead