Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with DefaultedNamedType #1

Open
russhousley opened this issue Apr 6, 2021 · 0 comments
Open

Issue with DefaultedNamedType #1

russhousley opened this issue Apr 6, 2021 · 0 comments

Comments

@russhousley
Copy link

The code below to illustrates the problem. The code produces two different hex strings:

300c06082a831a8c9a6e01023000
300a06082a831a8c9a6e0102

I believe that the first one is correct! The empty SEQUENCE should not be omitted in this situation.

#!/usr/bin/env python

from pyasn1.codec.der.decoder import decode as der_decode
from pyasn1.codec.der.encoder import encode as der_encode
from pyasn1.type import univ, namedtype
from pyasn1_modules import rfc5280
import binascii

oid = univ.ObjectIdentifier((1, 2, 410, 200046, 1, 2))

class PadAlgo(univ.Choice):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('specifiedPadAlgo', univ.OctetString()),
        namedtype.NamedType('generalPadAlgo', univ.ObjectIdentifier())
    )

pad_algo_1 = PadAlgo()
pad_algo_1['specifiedPadAlgo'] = univ.OctetString(hexValue='01')

class CbcParameters(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.DefaultedNamedType('m', univ.Integer().subtype(value=1)),
        namedtype.DefaultedNamedType('padAlgo', pad_algo_1)
    )

algorithmIdentifierMapUpdate = {
    oid: CbcParameters(),
}

rfc5280.algorithmIdentifierMap.update(algorithmIdentifierMapUpdate)

param = CbcParameters()
param['m'] = 1
param['padAlgo'] = pad_algo_1

AlgoId = rfc5280.AlgorithmIdentifier()
AlgoId['algorithm'] = oid
AlgoId['parameters'] = der_encode(param)

substrate = der_encode(AlgoId)
print(binascii.hexlify(substrate))

asn1Object, rest = der_decode(substrate,
                              asn1Spec=rfc5280.AlgorithmIdentifier(),
                              decodeOpenTypes=True)

substrate = der_encode(asn1Object)
print(binascii.hexlify(substrate))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant