Tuesday, March 30, 2021

Download SSL / TLS certificates using Python

import OpenSSL
import ssl

hostname='www.google.com'
port=443

cert = ssl.get_server_certificate((hostname, port))
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)

x509.digest('sha256')
# b'0A:E6:46:01:80:9F:C7:33:84:19:6A:DD:6C:8E:5F:95:5C:F6:F2:75:46:32:1E:C9:61:1D:88:DA:9A:A9:B4:

x509.get_subject()
# <X509Name object '/C=US/ST=California/L=Mountain View/O=Google LLC/CN=www.google.com'

x509.get_notBefore().decode()                                                                                                                          # '20210311144716Z'

No comments: