Python: 将 X509 证书的颁发者 CN 值存储为字符串
Python: store Issuer CN value of X509 Certificate as a string
我正在使用以下代码:
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变量如下:
我想将 commonName 值存储在一个变量中。 (上面突出显示的值)
我对 Python 还是很陌生,无法找到有关这些类型变量的任何内容,有人可以指导我将该值存储在变量中的语法应该是什么。
发行人的Common Name(CN)可以确定如下:
...
from cryptography.x509.oid import NameOID
cn = cert_info.issuer.get_attributes_for_oid(NameOID.COMMON_NAME)[0].value
...
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
.
我正在使用以下代码:
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变量如下:
我想将 commonName 值存储在一个变量中。 (上面突出显示的值)
我对 Python 还是很陌生,无法找到有关这些类型变量的任何内容,有人可以指导我将该值存储在变量中的语法应该是什么。
发行人的Common Name(CN)可以确定如下:
...
from cryptography.x509.oid import NameOID
cn = cert_info.issuer.get_attributes_for_oid(NameOID.COMMON_NAME)[0].value
...
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
.