Python:将X509证书的Issuer CN值存储为字符串 [英] Python: store Issuer CN value of X509 Certificate as a string

查看:102
本文介绍了Python:将X509证书的Issuer CN值存储为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

from cryptography import x509
from cryptography.hazmat.backends import default_backend
cert_info = x509.load_pem_x509_certificate(cert_pem, default_backend())
cert_issuer = cert_info.issuer

在PyCharm中调试时,我看到cert_issuer变量如下:

While debugging in PyCharm, I saw that the cert_issuer variable is as following:

我想将commonName值存储在变量中.(上面突出显示的值)

I want to store the commonName value in a variable. (value highlighted above)

我对Python还是很陌生,无法使用这些类型的变量查找任何内容,有人可以指导我将值存储在变量中的语法是什么.

I'm still fairly new to Python and was not able to find anything with these type of variables, can someone please guide me as to what should be the syntax to store that value in a variable.

推荐答案

发行人的通用名称(CN)可以如下确定:

The Common Name (CN) of the issuer can be determined as follows:

...
from cryptography.x509.oid import NameOID
cn = cert_info.issuer.get_attributes_for_oid(NameOID.COMMON_NAME)[0].value
...

<代码> cryptography.x509.Certificate#issuer 返回 cryptography.x509.Name 对象,其中包含属性列表.可以使用 来访问此列表的特定属性.get_attributes_for_oid(oid) ,其中必须使用来自 COMMON_NAME . get_attributes_for_oid(oid)返回的列表 cryptography.x509.NameAttributes 对象.由于只有一个 Issuer ,因此必须使用第一个 NameAttribute 对象,可以使用

cryptography.x509.Certificate#issuer returns a cryptography.x509.Name object that contains a list of attributes. A particular attribute of this list can be accessed with get_attributes_for_oid(oid), where the name of the attribute has to be specified with an OID from cryptography.x509.oid.NameOID, e.g. COMMON_NAME. get_attributes_for_oid(oid) returns a list of cryptography.x509.NameAttributes objects. Since there is only one Issuer, the first NameAttribute object has to be used, whose value can be queried with value.

这篇关于Python:将X509证书的Issuer CN值存储为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆