【问题标题】:How to get attribute value from Extension object (Python cryptography)?如何从扩展对象(Python 密码学)中获取属性值?
【发布时间】:2021-06-16 10:03:51
【问题描述】:

我想从 Extension 对象中访问 name 属性。

from cryptography import x509
from cryptography.x509.oid import ExtensionOID

l_certificate = rgetattr(self.l_config_json, 'web_interface.certificate.certificate')
d_certificate = x509.load_pem_x509_certificate(str.encode(l_certificate))
ext = d_certificate.extensions.get_extension_for_oid(ExtensionOID.EXTENDED_KEY_USAGE)
extendedKeyUsage = ext.value
print(extendedKeyUsage)

结果:

<ExtendedKeyUsage([<ObjectIdentifier(oid=1.3.6.1.5.5.7.3.2, name=clientAuth)>, <ObjectIdentifier(oid=1.3.6.1.5.5.7.3.1, name=serverAuth)>])>

如何从结果中访问名称属性(clientAuth 和 serverAuth)??

【问题讨论】:

    标签: python cryptography x509certificate


    【解决方案1】:

    如果我理解目标,您只是想知道如何访问属性值?这些可以通过查看site-packages/cryptography/x509/extensions.py 中的类定义得出,并且是一项基本技能(即阅读源代码)。

    我所做的很简单:

    # common to cryptography.x509.extensions.ExtensionType
    data = {
        'critical': ext.critical,
        'name': ext.oid._name
    }
    # extension specific attributes
    if isinstance(ext.value, cryptography.x509.extensions.ExtendedKeyUsage):
        data['key_usages'] = [x._name for x in ext.value or []]
    

    生产的

     {'critical': False,
      'key_usages': ['serverAuth', 'clientAuth'],
      'name': 'extendedKeyUsage'}
    

    而不是 repr 你有,我猜无法解析?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      相关资源
      最近更新 更多