kimgr / asn1ate

A Python library for translating ASN.1 into other forms.
Other
69 stars 41 forks source link

Default value management #24

Open kgalhabbo opened 8 years ago

kgalhabbo commented 8 years ago

Hello,

I am trying to parse a file containing the following (from http://simalliance.org/euicc/euicc-technical-releases/):

AlgoParameter ::= SEQUENCE { key OCTET STRING, opc OCTET STRING, -- For TUAK, this parameter sets TOPc rotationConstants OCTET STRING (SIZE (5)) DEFAULT '4000204060'H, xoringConstants OCTET STRING (SIZE (40)) DEFAULT '00000000000000000000000000000001000000000000000200000000000000040000000000000008'H, sqnInit SEQUENCE (SIZE (3)) OF OCTET STRING (SIZE (6)) DEFAULT {'000000000000'H, '000000000000'H, '000000000000'H } }

I get the following error:

Traceback (most recent call last): File "pyasn1gen.py", line 588, in sys.exit(main(sys.argv[1:])) File "pyasn1gen.py", line 574, in main parse_tree = parser.parse_asn1(asn1def) File "C:\Python27\lib\site-packages\asn1ate\parser.py", line 41, in parse_asn1 parse_result = grammar.parseString(asn1_definition) File "C:\Python27\lib\site-packages\pyparsing.py", line 1114, in parseString raise exc pyparsing.ParseException: Expected "END" (at char 533), (line:18, col:28)

Do you have any suggestions about how to solve this and/or how to deal with default values? Thank you for your help.

kimgr commented 8 years ago

The problem is the last component:

sqnInit SEQUENCE (SIZE (3)) OF OCTET STRING (SIZE (6)) DEFAULT {'000000000000'H, '000000000000'H, '000000000000'H }

specifically the curly-brace form on DEFAULT.

We don't yet seem to handle the SequenceOfValue production -- I'm not entirely sure where to put it.

kgalhabbo commented 8 years ago

Thank you for your answer. What would you therefore recommend?

kimgr commented 8 years ago

I think, and this is a big maybe, that you can omit the DEFAULT clause entirely.

If I recall correctly, DEFAULTs are handled encode-time, so if you're decoding, you'll always get a full value. If you're encoding, you'll have to provide the value explicitly.

That might work.

On Thu, Dec 10, 2015 at 9:11 AM, kgalhabbo notifications@github.com wrote:

Thank you for your answer. What would you therefore recommend?

— Reply to this email directly or view it on GitHub https://github.com/kimgr/asn1ate/issues/24#issuecomment-163529597.

kimgr commented 8 years ago

From #26 -- this actually runs clean through the parser, but generates nonsense pyasn1 code:

         Clearance  ::=  SEQUENCE {                                          
               policyId       [0] OBJECT IDENTIFIER,
               classList      [1] ClassList DEFAULT {unclassified},
               securityCategories [2] SET OF SecurityCategory  OPTIONAL
         }

Output:

# the subtype(value={unclassified}) part is not valid Python
namedtype.DefaultedNamedType('classList', ClassList().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)).subtype(value={unclassified})),