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
380 stars 130 forks source link

5G NAS UE policy protocol TS 24.501_UEPOL UPSISublist Len field is not encoded. #252

Open maddenj-ie opened 7 months ago

maddenj-ie commented 7 months ago

The 'Len' field of UPSISublist is not encoded when using 'to_bytes'

The issue may be reproduced using the following test

    def test_upsi_sublist_from_obj_to_bytes(self):

        upsi_sublist_obj = UPSISublist(val={
            'PLMN': '27201',
            'Cont': [
                1,
                2
            ]
        })

        show(upsi_sublist_obj)

        upsi_sublist_bytes = upsi_sublist_obj.to_bytes()
        print(upsi_sublist_obj.hex())

        expected_upsi_sublist_bytes = b'\x00\x07\x72\xf2\x10\x00\x01\x00\x02'

        assert upsi_sublist_bytes == expected_upsi_sublist_bytes

There is no 'init' method associated with the UPSISublist class.

https://github.com/P1sec/pycrate/blob/1824d90cd4815ccdf4070e76338009aa1a1e8097/pycrate_mobile/TS24501_UEPOL.py#L169

I've done some experimenting and the addition of the following code, resolves the issue.

    def __init__(self, *args, **kwargs):
        Envelope.__init__(self, *args, **kwargs)
        self[0].set_valauto(lambda: 3 + self[2].get_len())
        self[2].set_blauto(lambda: (self[0].get_val()-3) << 3)

I'm happy to assist with testing.

Thanks.

mitshell commented 4 months ago

Thanks for uncovering this bug, too. Same as with previous issue, a new repository has been setup, including new fixes. This repo will keep being maintained : https://github.com/pycrate-org/pycrate. It includes fixes for this specific 5G NAS module. Moreover, it will ease collaborative support !

If you checkout the last 0.7.1 version, this bug should be solved.