pyasn1 icon indicating copy to clipboard operation
pyasn1 copied to clipboard

Unable to Marshal Sequence of SequenceOF

Open peppepetra opened this issue 6 years ago • 2 comments

Hi,

I have this go struct:

type RegistrationInfo struct {
      User string
     Addrs []string
     SecretKey []byte
     ControllerName string
}

That is marshalled into the following ASN struct:

   RegistrationInfo ::= SEQUENCE {
      user        User,
      addrs      Addrs,
      secretkey SecretKey,
          controllername ControllerName}

   User ::= UTF8String
   Addrs ::= SEQUENCE SIZE (1..MAX) OF UTF8String
   SecretKey ::= SEQUENCE SIZE (1..MAX) OF BITSTRING
   ControllerName ::= UTF8String

I would like to reproduce the same using pyasn1: I tried the following:

   class RegistrationString(univ.Sequence):
       componentType = namedtype.NamedTypes(
           namedtype.NamedType('User', univ.BitString()),
           namedtype.NamedType('Addrs', univ.SequenceOf(univ.BitString())),
           namedtype.NamedType('SecretKey', univ.SequenceOf(univ.BitString())),
           namedtype.NamedType('ControllerName', univ.BitString()),
       )

But I get the following error:

   pyasn1.error.PyAsn1Error: NamedTypes can cast only scalar values

Could you please help me with that?

I am missing something for sure.

Thank you.

peppepetra avatar Dec 18 '18 11:12 peppepetra

The following is what the asn1ate tool creates when provided with the ASN.1 definition above.

# Auto-generated by asn1ate v.0.6.0 from issue_150.asn

# (last modified on 2018-12-18 10:54:20.854134)


from pyasn1.type import univ, char, namedtype, namedval, tag, constraint, useful


class Addrs(univ.SequenceOf):
    pass


Addrs.componentType = char.UTF8String()
Addrs.subtypeSpec=constraint.ValueSizeConstraint(1, MAX)


class ControllerName(char.UTF8String):
    pass


class SecretKey(univ.SequenceOf):
    pass


SecretKey.componentType = BITSTRING()
SecretKey.subtypeSpec=constraint.ValueSizeConstraint(1, MAX)


class User(char.UTF8String):
    pass


class RegistrationInfo(univ.Sequence):
    pass


RegistrationInfo.componentType = namedtype.NamedTypes(
    namedtype.NamedType('user', User()),
    namedtype.NamedType('addrs', Addrs()),
    namedtype.NamedType('secretkey', SecretKey()),
    namedtype.NamedType('controllername', ControllerName())
)

idahogray avatar Dec 18 '18 16:12 idahogray

Sorry for delay!

@idahogray might be right - you've missed the SequenceOf component definition:

class RegistrationString(univ.Sequence):
       componentType = namedtype.NamedTypes(
           namedtype.NamedType('User', univ.BitString()),
           namedtype.NamedType('Addrs', univ.SequenceOf(componentType=univ.BitString())),
           namedtype.NamedType('SecretKey', univ.SequenceOf(componentType=univ.BitString())),
           namedtype.NamedType('ControllerName', univ.BitString()),
       )

etingof avatar Dec 29 '18 19:12 etingof