pythonpyasn1

python.pyasn1 to express below strcture


I am a new to Python and PYASN1, how to express below strcture? is there any docuement I can refer to? I search on the internect, there's a little document about the PYASN1

OtherInfo ::= SEQUENCE {
       keyInfo KeySpecificInfo,
       partyAInfo [0] OCTET STRING OPTIONAL,
       suppPubInfo [2] OCTET STRING
     }

KeySpecificInfo ::= SEQUENCE {
    algorithm OBJECT IDENTIFIER,
    counter OCTET STRING SIZE (4..4) }

Solution

  • Should be something like this, assuming your ASN.1 module declares explicit tagging by default.

    Also, the docs.

    class KeySpecificInfo(univ.Sequence):
        componentType = namedtype.NamedTypes(
            namedtype.NamedType('algorithm', ObjectIdentifier()),
            namedtype.NamedType(
                'counter', OctetString().subtype(subtypeSpec=ValueSizeConstraint(4, 4)))
        )
    
    class OtherInfo(univ.Sequence):
        componentType = namedtype.NamedTypes(
            namedtype.NamedType('keyInfo', KeySpecificInfo()),
            namedtype.OptionalNamedType('partyAInfo', OctetString().subtype(
                explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)),
            namedtype.NamedType('suppPubInfo', OctetString().subtype(
                explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2))
        )