From a85285d30e5c3ae0268ca5175cea50be3fc65467 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Wed, 30 Mar 2016 16:00:48 +0200 Subject: [PATCH] Tried something, but it doesn't work... --- ssltools/__init__.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ssltools/__init__.py b/ssltools/__init__.py index 25294fe..f222486 100644 --- a/ssltools/__init__.py +++ b/ssltools/__init__.py @@ -1,7 +1,20 @@ -import datetime, OpenSSL, ssl +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) def getRemoteCert(host, port): - return ssl.get_server_certificate((host, port)) + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((host, 443)) + + sslSocket = ssl_wrap_socket(s, host) + + return sslSocket.getpeercert() 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")