我发现在pyasn1中添加显式标记项的最佳方法是...明确标记它们。但这看起来过于冗长:
cert['tbsCertificate']['extensions'] = rfc2459.Extensions().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))
有没有办法在不指定标签的情况下生成适合像extensions
这样的地方的空值?
答案 0 :(得分:1)
有更简单的方法。惯例是,如果将None分配给复杂[py] ASN.1类型的组件,则该组件将被实例化,但不具有任何值。
>>> cert = rfc2459.Certificate()
>>> print cert.prettyPrint()
Certificate:
>>> cert['tbsCertificate'] = None
>>> print cert.prettyPrint()
Certificate:
tbsCertificate=TBSCertificate:
>>> cert['tbsCertificate']['extensions'] = None
>>> print cert.prettyPrint()
Certificate:
tbsCertificate=TBSCertificate:
extensions=Extensions:
>>> cert['tbsCertificate']['extensions'][0] = None
>>> print cert.prettyPrint()
Certificate:
tbsCertificate=TBSCertificate:
extensions=Extensions:
Extension:
>>> cert['tbsCertificate']['extensions'][0]['extnID'] = '1.3.5.4.3.2'
>>> cert['tbsCertificate']['extensions'][0]['extnValue'] = '\x00\x00'
>>> print cert.prettyPrint()
Certificate:
tbsCertificate=TBSCertificate:
extensions=Extensions:
Extension:
extnID=1.3.5.4.3.2
extnValue=0x0000
>>>
这有效地允许您逐步从Python内置或其他pyasn1对象构建复合pyasn1对象,而无需重复其类型规范。