2016-03-30 14:00:48 +00:00
|
|
|
import datetime, OpenSSL, socket, ssl
|
|
|
|
|
|
|
|
def ssl_wrap_socket(sock, server_hostname):
|
|
|
|
|
|
|
|
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
|
|
|
|
|
|
|
|
if ssl.HAS_SNI:
|
|
|
|
return context.wrap_socket(sock, server_hostname=server_hostname)
|
|
|
|
return context.wrap_socket(sock)
|
2016-03-16 15:49:56 +00:00
|
|
|
|
|
|
|
def getRemoteCert(host, port):
|
2016-03-30 14:00:48 +00:00
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.connect((host, 443))
|
|
|
|
|
|
|
|
sslSocket = ssl_wrap_socket(s, host)
|
|
|
|
|
|
|
|
return sslSocket.getpeercert()
|
2016-03-16 15:49:56 +00:00
|
|
|
|
|
|
|
def getRemoteExpiry(host,port):
|
|
|
|
return datetime.datetime.strptime(str(OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, getRemoteCert(host, port)).get_notAfter().decode("UTF-8")), "%Y%m%d%H%M%SZ")
|