Created
August 19, 2016 12:38
-
-
Save elnygren/2506bfd75be1344fe0cd1d40df923908 to your computer and use it in GitHub Desktop.
SSL certificate status with pyOpenSSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from OpenSSL import SSL | |
import sys, os, select, socket | |
def parse_name(name): | |
return { | |
'common_name': name.commonName | |
} | |
def verify_cb(conn, cert, errnum, depth, ok): | |
cert_data = { | |
'subject': parse_name(cert.get_subject()), | |
'issuer': parse_name(cert.get_issuer()), | |
'from': cert.get_notAfter(), | |
'until': cert.get_notBefore(), | |
'expired': cert.has_expired(), | |
'name_hash': cert.subject_name_hash() | |
} | |
print('===== cert data ======') | |
for key, val in cert_data.items(): | |
print('{0}: {1}'.format(key, val)) | |
return ok | |
ctx = SSL.Context(SSL.SSLv23_METHOD) | |
ctx.set_verify(SSL.VERIFY_NONE, verify_cb) # Demand a certificate | |
sock = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM)) | |
sock.connect(('yourhost.com', 443)) | |
sock.send('ping') | |
sock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment