2016-03-30 14:00:48 +00:00
|
|
|
import datetime, OpenSSL, socket, ssl
|
|
|
|
|
2016-03-30 14:16:08 +00:00
|
|
|
PEM = 0
|
|
|
|
DER = 1
|
|
|
|
DERBIN = 2
|
2016-03-30 14:00:48 +00:00
|
|
|
|
2016-03-30 14:16:08 +00:00
|
|
|
def getRemoteCert(host, port, form = PEM):
|
|
|
|
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.settimeout(1)
|
|
|
|
wrappedSocket = context.wrap_socket(sock, server_hostname=host)
|
|
|
|
try:
|
|
|
|
wrappedSocket.connect((host, port))
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if form == PEM:
|
|
|
|
response = ssl.DER_cert_to_PEM_cert(wrappedSocket.getpeercert(True))
|
|
|
|
elif form == DER:
|
|
|
|
response = wrappedSocket.getpeercert(False)
|
|
|
|
elif form == DERBIN:
|
|
|
|
response = wrappedSocket.getpeercert(True)
|
|
|
|
wrappedSocket.close()
|
|
|
|
return response
|
2016-03-16 15:49:56 +00:00
|
|
|
|
|
|
|
def getRemoteExpiry(host,port):
|
2016-03-30 14:16:08 +00:00
|
|
|
try:
|
|
|
|
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")
|
|
|
|
except:
|
|
|
|
pass
|