Getting SubjectName and SubjectAlternativeNames in Python
Author(s):
Muhammad Akbar
Publish date: Jun 19, 2019
Publish date: Jun 19, 2019
Below is a small sample code I wrote to illustrate how to retrieve SubjectName and SubjectAlternativeNames in an X509 certificate.
from cryptography import x509
def _getSubjectAltNames(cert):
for ext in cert.extensions:
ext = ext.value
if isinstance(ext, x509.SubjectAlternativeName):
return ext.get_values_for_type(x509.DNSName) + ext.get_values_for_type(x509.IPAddress)
return []
def _getSubjectName(cert):
for attribute in cert.subject.get_attributes_for_oid(x509.NameOID.COMMON_NAME):
return attribute.value
return None