pycrate-org / pycrate

A Python library to ease the development of encoders and decoders for various protocols and file formats, especially telecom ones. Provides an ASN.1 compiler and a CSN.1 runtime.
https://github.com/pycrate-org/pycrate
GNU Lesser General Public License v2.1
39 stars 9 forks source link

Error decoding BER T.125 #13

Open user3472g opened 2 months ago

user3472g commented 2 months ago

Good Evening, I believe a bug in T.125 decoding. Consider this example. The bytes are a BER encoded T.125 connect-initial PDU taken from a pcap file in public wireshark samples. wireshark does the decode no problem

import binascii
from pycrate_asn1dir import T125
msg = T125.MCS_PROTOCOL.Connect_Initial.from_ber(binascii.unhexlify('7f658201900401010401010101ff30190201220201020201000201010201000201010202ffff020102301902010102010102010102010102010002010102020420020102301c0202ffff0202fc170202ffff0201010201000201010202ffff0201020482012f000500147c00018126000800100001c00044756361811801c0d400040008008004600301ca03aa09040000280a000049005300440032002d004b004d0038003400310037003800000000000000000004000000000000000c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001ca0100000000000f0007000100350035003200370034002d004f0045004d002d0030003000310031003900300033002d0030003000310030003700000000000000000000000000000000000000000004c00c00090000000000000002c00c000b0000000000000003c02c0003000000726470647200000000008080636c6970726472000000a0c0726470736e640000000000c0'))  
raise(ASN1ObjErr('{0}: {1} value out of constraint, {2!r}'\

pycrate_asn1rt.err.ASN1ObjErr: DomainParameters.maxMCSPDUsize: INTEGER value out of constraint, -1

user3472g commented 2 months ago

Can force error to be ignored by setting ASN1Obj._SAFE_BND = False but values are returned as -1 instead of 65535. For instance, maxMCSPDUsize should be 65535 but is presented as '-1'

mitshell commented 2 months ago

When we disable the bound constraints, as you do, we obtain the following value:

{
  callingDomainSelector '01'H,
  calledDomainSelector '01'H,
  upwardFlag TRUE,
  targetParameters {
    maxChannelIds 34,
    maxUserIds 2,
    maxTokenIds 0,
    numPriorities 1,
    minThroughput 0,
    maxHeight 1,
    maxMCSPDUsize -1,
    protocolVersion 2
  },
  minimumParameters {
    maxChannelIds 1,
    maxUserIds 1,
    maxTokenIds 1,
    numPriorities 1,
    minThroughput 0,
    maxHeight 1,
    maxMCSPDUsize 1056,
    protocolVersion 2
  },
  maximumParameters {
    maxChannelIds -1,
    maxUserIds -1001,
    maxTokenIds -1,
    numPriorities 1,
    minThroughput 0,
    maxHeight 1,
    maxMCSPDUsize -1,
    protocolVersion 2
  },
  userData '000500147C00018126000800100001C00044756361811801C0D400040008008004600301CA03AA09040000280A000049005300440032002D004B004D0038003400310037003800000000000000000004000000000000000C0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001CA0100000000000F0007000100350035003200370034002D004F0045004D002D0030003000310031003900300033002D0030003000310030003700000000000000000000000000000000000000000004C00C00090000000000000002C00C000B0000000000000003C02C0003000000726470647200000000008080636C6970726472000000A0C0726470736E640000000000C0'H
}

And yes, it's strange to have some integer fields (supposed to be positive) with negative values.

Can you indicate where did you obtained this buffer? Maybe you should double-check it's a valid one. Pcap examples could eventually be crafted and not perfectly valid.