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

FROM constrainsts on IA5String #150

Closed jblestang closed 3 years ago

jblestang commented 3 years ago

I'm trying to add constraints on an IA5String which shall allow from my understanding all values in the range [0;127]

emitterId [6] IA5String (SIZE(0..5)) (FROM({0,0} | '0'..'9' | 'A'..'Z'))

Pycrate is giving me the following error when I'm trying to validate my ASN.1 :

pycrate_asn1c.err.ASN1ProcTextErr: _alpha_characters: invalid IA5String value, {0,0}

§43.8 of X.680 states that any value in the range [0;127] are authorized. I'm wondering wether my syntax/understanding of how to specify control characters is correct or if pycrate does not support expressing characters using the 'cell/row' notation.

p1-bmu commented 3 years ago

Thank you for your feedback. As you might have understood, ITUT X.680 is saying many things that are rarely used in practice. And this case of string characters noted by their numerical value is one of this that, I never met and did not implement in pycrate.

The code that parses alphabet constraint is here: https://github.com/P1sec/pycrate/blob/bb67b3f95454c65de9131a258589349dfaf7b3b9/pycrate_asn1c/asnobj.py#L4163 It relies on parsing string value, implemented here: https://github.com/P1sec/pycrate/blob/bb67b3f95454c65de9131a258589349dfaf7b3b9/pycrate_asn1c/asnobj.py#L5128

Parsing characters by their numerical value may be easy for basic ascii-based strings, but may get more complex for special string objects relying on custom character tables.

jblestang commented 3 years ago

Using and hexadecimal approach and double quotes instead of single quotes seems to let me validate my ASN.1 using pycrate.

emitterId IA5String (FROM(“\x00”|”0”..”9”|”A”..”Z”))
p1-bmu commented 3 years ago

This is nice, thanks for your feedback.

p1-bmu commented 3 years ago

From the existing ASN.1 directories in pycrate, it seems double-quote is the way to go for alphabet constraints. E.g.:

./ITUT_H225_2009-12/H323-MESSAGES.asn:719:TBCD-STRING ::= IA5String(FROM ("0123456789#*abc"))
./ITUT_H225_2009-12/H323-MESSAGES.asn:808:IsupDigits ::= IA5String(SIZE (1..128))(FROM ("0123456789ABCDE"))
./ITUT_H245_2011-05/MULTIMEDIA-SYSTEM-CONTROL.asn:1784:     e164Address             IA5String(SIZE(1..128)) (FROM ("0123456789#*,")),
./ITUT_H245_2011-05/MULTIMEDIA-SYSTEM-CONTROL.asn:4185:     signalType              IA5String (SIZE (1) ^ FROM ("0123456789#*ABCD!")),
./OMA_LPPe/LPPe.asn:311:OMA-LPPe-CharArray ::= VisibleString(FROM ("a".."z" | "A".."Z" | "0".."9" | ".-"))(SIZE (1..31))
./OMA_ULP/ULP-Components.asn:731:          ims-public-identity              VisibleString(FROM ("a".."z" | "A".."Z" | "0".."9" | ":./-_~#@?")) (SIZE (1..255)),