- 1 year ago
How to return details of a PEM format certificate using python [duplicate]
Comment
1 / 1
To return details of a PEM format certificate using Python, you can use the OpenSSL
library. Here is an example code snippet that demonstrates how to do this:
pythonimport OpenSSL
import ssl
cert = ssl.get_server_certificate(('example.com', 443))
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
print("Subject:", x509.get_subject())
print("Issuer:", x509.get_issuer())
print("Version:", x509.get_version())
print("Serial Number:", x509.get_serial_number())
print("Not Before:", x509.get_notBefore())
print("Not After:", x509.get_notAfter())
print("Signature Algorithm:", x509.get_signature_algorithm())
In this example, we use the ssl
library to retrieve a server certificate from example.com
. We then load the PEM certificate into an OpenSSL.crypto.X509
object using the load_certificate
function. Finally, we print various details of the certificate, including the subject, issuer, version, serial number, validity period, and signature algorithm.
Note that the OpenSSL
library must be installed in order to use this code. You can install it using pip install pyopenssl
.