gpt4 book ai didi

python - 如何使用 pyasn1 解析 subjectAltName 扩展数据?

转载 作者:太空狗 更新时间:2023-10-29 16:56:40 45 4
gpt4 key购买 nike

我有一些 pyOpenSSL 给我的数据,'0\r\x82\x0bexample.com'。这应该是 subjectAltName X509 扩展名的值。我尝试使用 pyasn1(并基于 pyasn1 示例之一)为此扩展编码 ASN1 规范的必要部分:

from pyasn1.type import univ, constraint, char, namedtype

from pyasn1.codec.der.decoder import decode

MAX = 64

class DirectoryString(univ.Choice):
componentType = namedtype.NamedTypes(
namedtype.NamedType(
'teletexString', char.TeletexString().subtype(
subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
namedtype.NamedType(
'printableString', char.PrintableString().subtype(
subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
namedtype.NamedType(
'universalString', char.UniversalString().subtype(
subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
namedtype.NamedType(
'utf8String', char.UTF8String().subtype(
subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
namedtype.NamedType(
'bmpString', char.BMPString().subtype(
subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
namedtype.NamedType(
'ia5String', char.IA5String().subtype(
subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
)


class AttributeValue(DirectoryString):
pass


class AttributeType(univ.ObjectIdentifier):
pass


class AttributeTypeAndValue(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.NamedType('type', AttributeType()),
namedtype.NamedType('value', AttributeValue()),
)


class RelativeDistinguishedName(univ.SetOf):
componentType = AttributeTypeAndValue()

class RDNSequence(univ.SequenceOf):
componentType = RelativeDistinguishedName()


class Name(univ.Choice):
componentType = namedtype.NamedTypes(
namedtype.NamedType('', RDNSequence()),
)


class Extension(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.NamedType('extnID', univ.ObjectIdentifier()),
namedtype.DefaultedNamedType('critical', univ.Boolean('False')),
namedtype.NamedType('extnValue', univ.OctetString()),
)


class Extensions(univ.SequenceOf):
componentType = Extension()
sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(1, MAX)


class GeneralName(univ.Choice):
componentType = namedtype.NamedTypes(
# namedtype.NamedType('otherName', AnotherName()),
namedtype.NamedType('rfc822Name', char.IA5String()),
namedtype.NamedType('dNSName', char.IA5String()),
# namedtype.NamedType('x400Address', ORAddress()),
namedtype.NamedType('directoryName', Name()),
# namedtype.NamedType('ediPartyName', EDIPartyName()),
namedtype.NamedType('uniformResourceIdentifier', char.IA5String()),
namedtype.NamedType('iPAddress', univ.OctetString()),
namedtype.NamedType('registeredID', univ.ObjectIdentifier()),
)


class GeneralNames(univ.SequenceOf):
componentType = GeneralName()
sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(1, MAX)


class SubjectAltName(GeneralNames):
pass

print decode('0\r\x82\x0bexample.com', asn1Spec=GeneralNames())

很明显,接近尾声时我有点无聊,没有完全指定 GeneralName 类型。但是,测试字符串应该包含 dNSName,而不是跳过的值之一,所以我希望它没有关系。

当程序运行时,它失败并出现我无法解释的错误:

Traceback (most recent call last):
File "x509.py", line 94, in <module>
print decode('0\r\x82\x0bexample.com', asn1Spec=GeneralNames())
File "/usr/lib/pymodules/python2.6/pyasn1/v1/codec/ber/decoder.py", line 493, in __call__
length, stGetValueDecoder, decodeFun
File "/usr/lib/pymodules/python2.6/pyasn1/v1/codec/ber/decoder.py", line 202, in valueDecoder
substrate, asn1Spec
File "/usr/lib/pymodules/python2.6/pyasn1/v1/codec/ber/decoder.py", line 453, in __call__
__chosenSpec.getTypeMap().has_key(tagSet):
File "/usr/lib/pymodules/python2.6/pyasn1/v1/type/univ.py", line 608, in getTypeMap
return Set.getComponentTypeMap(self)
File "/usr/lib/pymodules/python2.6/pyasn1/v1/type/univ.py", line 535, in getComponentTypeMap
def getComponentTypeMap(self): return self._componentType.getTypeMap(1)
File "/usr/lib/pymodules/python2.6/pyasn1/v1/type/namedtype.py", line 126, in getTypeMap
'Duplicate type %s in map %s'%(k,self.__typeMap)
pyasn1.error.PyAsn1Error: Duplicate type TagSet(Tag(tagClass=0, tagFormat=0, tagId=22)) in map {TagSet(Tag(tagClass=0, tagFormat=0, tagId=22)): IA5String()}

任何有关我哪里出错以及如何使用 pyasn1 成功解析此扩展类型的提示都将不胜感激。

最佳答案

我在 pyasn1-users 列表上发布了这个问题,Ilya Etingof(pyasn1 的作者)指出了我的错误。简而言之,GeneralName.componentType中的每个NamedType都需要被赋予标签信息。这是通过 subtype 方法完成的。例如,而不是:

namedtype.NamedType('rfc822Name', char.IA5String()),

定义应该是:

namedtype.NamedType('rfc822Name', char.IA5String().subtype(
implicitTag=tag.Tag(tag.tagClassContext,
tag.tagFormatSimple, 1))),

其中 1 来自 GeneralName 的 ASN.1 定义:

GeneralName ::= CHOICE {
otherName [0] OtherName,
rfc822Name [1] IA5String,
dNSName [2] IA5String,
x400Address [3] ORAddress,
directoryName [4] Name,
ediPartyName [5] EDIPartyName,
uniformResourceIdentifier [6] IA5String,
iPAddress [7] OCTET STRING,
registeredID [8] OBJECT IDENTIFIER
}

componentType的这些字段分别定义标签后,解析成功:

(GeneralNames().setComponentByPosition(
0, GeneralName().setComponentByPosition(1, IA5String('example.com'))), '')

关于python - 如何使用 pyasn1 解析 subjectAltName 扩展数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5519958/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com