1

The best way I found to add explicitly tagged items in pyasn1 is to... explicitly tag them. But this looks overly verbose:

cert['tbsCertificate']['extensions'] = rfc2459.Extensions().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))

Is there any way to generate an empty value which will fit into a place like extensions without specifying the tag?

4

1 に答える 1

1

はるかに簡単なアプローチがあります。規則では、複雑な [py]ASN.1 タイプのコンポーネントに None を割り当てると、そのコンポーネントはインスタンス化されますが、値はありません。

>>> 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 オブジェクトを効率的に構築できます。

于 2015-08-02T07:22:07.203 に答える