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

ASN.1 UTF8String encode/decode failure #159

Closed palyamate closed 2 years ago

palyamate commented 2 years ago

I used this scheme:

PERSON

DEFINITIONS AUTOMATIC TAGS ::=

BEGIN

Person ::= SEQUENCE {
    age               INTEGER(0..99),
    firstname         UTF8String,
    lastname          UTF8String
}

END

I encoded this on this:

{
    "age": 72,
    "firstname": "JSKHFDGhgha",
    "lastname": "HSjkhasjfhkaj"
}

and the UPER encoded version: 0148044A 6F686E03 446F65 I compiled this with _pycrateasn1compile.py and used the .from_uper function of the Person. I got this error:

Traceback (most recent call last):
  File "name_decode.py", line 4, in <module>
    person.PERSON.Person.from_uper(bytes.fromhex('0148044A 6F686E03 446F65'))
  File "/home/palya/work/pycrate-investigation/lib/python3.8/site-packages/pycrate_asn1rt/asnobj.py", line 1292, in from_uper
    self._from_per(char)
  File "/home/palya/work/pycrate-investigation/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_construct.py", line 949, in _from_per
    Comp._from_per(char)
  File "/home/palya/work/pycrate-investigation/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_str.py", line 2264, in _from_per
    self.__from_per_szunconst(char)
  File "/home/palya/work/pycrate-investigation/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_str.py", line 2326, in __from_per_szunconst
    self.__from_per_charstr(char, ldet)
  File "/home/palya/work/pycrate-investigation/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_str.py", line 2332, in __from_per_charstr
    val = char.get_bytes(8*ldet)
  File "/home/palya/work/pycrate-investigation/lib/python3.8/site-packages/pycrate_core/charpy.py", line 375, in get_bytes
    raise(CharpyErr('bitlen overflow: {0}, max {1}'\
pycrate_core.charpy.CharpyErr: bitlen overflow: 73744, max 65

The standard is here. The 10.9 says something about the string lenght encoding.

p1-bmu commented 2 years ago

Thanks for your feedback. Your encoding 0148044A 6F686E03 446F65 does not make really sense, as it looks shorter than the firstname / lastname strings, which are unconstrained UTF8, meaning each character is encoded on at least 1 byte. You can also see with the asn1playground that decoding this buffer does not work:

D0081S: End of input reached before message was fully decoded; check field 'firstname' of PDU #1.
value: D0081S: End of input reached before message was fully decoded; check field 'firstname' of PDU #1.
S0014E: Printing of PER details failed with the return code '4'.

The output may be truncated.
palyamate commented 2 years ago

Oh, sorry, I copied the wrong structure and bytes... This is the encoded version: 48044A6F 686E0344 6F65 And this is the correct JSON:

{
  "age": 72,
  "firstname": "John",
  "lastname": "Doe"
}
p1-bmu commented 2 years ago
Encoding to the file 'data.uper' using PER UNALIGNED encoding rule...
Person SEQUENCE [fieldcount (not encoded) = 3]
  age INTEGER [length (not encoded) = 0.7]
    72
  firstname UTF8String [length = 4.0]
    0x4a6f686e
  lastname UTF8String [length = 3.0]
    0x446f65
Total encoded length = 9.7
Encoded successfully in 10 bytes:
900894DE D0DC0688 DECA

You are failing somewhere with the value you want to encode or the buffer you have encoded. Please verify you data before submitting further information / issue.

palyamate commented 2 years ago

I got encode/decode error on a data, which I can't share, because it's confidential. I was pretty sure it was with the UTF8String length encoding when I debugged it. I tried to reproduce it with a minimal reproduction, but it seems I failed to do it with this example. I will close this issue for now, and I will reopen it or open another issue, when I can create a better example.