1

I'm starting to use the pyasn1 library and I have a question on how to set default values in a SEQUENCE OF object. My ASN1 structure is the following:

Asn1Def DEFINITIONS AUTOMATIC TAGS ::= 
BEGIN
  CasinoPlayer ::= SEQUENCE       
  {                                                     
     name      UTF8String (SIZE(1..16)),
     luckyNumbers SEQUENCE  (SIZE(3)) OF INTEGER DEFAULT {7,7,7}
  }                                                     
END

I understood how to create a DEFAULT field in the CasinoPlayer SEQUENCE using namedtype.DefaultedNamedType objects and using subtype to add SIZE constraint but how shall I initialize the default value {7,7,7}?

Thank you

zecky
  • 31
  • 3

1 Answers1

0

I am thinking it should look like this:

class CasinoPlayer(Sequence):
    componentType = NamedTypes(
        NamedType(
            'name',
            UTF8String(
                ConstraintsIntersection(
                    ValueSizeConstraint(1, 16)
                )
            )
        ),
        DefaultedNamedType(
            'luckyNumbers',
            SequenceOf(
                componentType=Integer(),
                sizeSpec=ConstraintsIntersection(
                    ValueSizeConstraint(3, 3)
                )
            ).setComponentByPosition(0, 7)
             .setComponentByPosition(1, 7)
             .setComponentByPosition(2, 7)
        )
    )

Plus you probably need to assign the tags to each ASN.1 type (as it's implied by the AUTOMATIC TAGS clause).

UPDATE:

Actually, this should work, but it does not! Luckily, there is the fix which should make defaulted SequenceOf propagating to the Sequence field for as long as it's a DefaultedNamedType.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • Thanks for the quick feedback. I just changed `NamedType` to `DefaultedNamedType` for luckyNumbers. – zecky Oct 18 '18 at 00:31
  • I tried to encode a simple object using the DER encoder and it is working fine except that the size constraint does not seem to apply. I can create a `SequenceOf` object with only 2 or 4 integers and call `setComponentByName('luckyNumbers', )`, the encoder will encode it without any error. Is it expected or am I missing something when creating an ASN1 value? – zecky Oct 18 '18 at 00:43
  • @zecky Please, try the fix linked above. Size constraint seems to work, though. If it does not, let's check out your code? – Ilya Etingof Oct 18 '18 at 07:09
  • @zecky If you tried the fix mentioned in the answer and it does not work for you, could you please publish the reproducer? May be GitHub issue would be a more convenient place for that. – Ilya Etingof Nov 21 '18 at 10:56
  • I published a test vector under [github](https://github.com/zeckyUs/testPyAsn1) I'm not sure that test 3 should work but I'm not an ASN.1 expert. What is your opinion on this? I would expect tests 7 and 8 to fail but it may be due to the way I'm using the API. – zecky Dec 03 '18 at 23:14