I have a socket server written in python accepting connections like:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# set the socket as non-blocking
self.socket.setblocking(False)
# bind
self.socket.bind((self.host, int(self.port)))
# listen
self.socket.listen()
in a separate async function, i’m calling:
conn, address = await self.loop.sock_accept(self.socket)
Once the connections is made, it is passed to :
loop.connect_accepted_socket(lambda: <custom asyncio.Protocol class>, ssl=ssl_context)
When the connection fails to establish, the error is caught under:
except ssl.SSLError as e:
print(e)
Where e can be a range of ssl errors.
Some of them are:
- [SSL: WRONG_VERSION_NUMBER]
- [SSL: SSLV3_ALERT_BAD_CERTIFICATE] sslv3 alert bad certificate (_ssl.c:1091)
Under such scenarios, i’d like to be able to print out the certificate that we had received from the client.
Is there a way to do this?
I’ve tried printing conn.getpeercert() in the exception handler, however, i get the error message:
AttributeError: ‘socket’ object has no attribute ‘getpeercert’
Whereas, if the connection does succeed, i’m able to get the peer certs from the transport object by calling
transport.get_extra_info(“peercert”)
I’d like to know if there is a way to print the certificate received from the client upon failure to verify.