mjs / imapclient

An easy-to-use, Pythonic and complete IMAP client library
https://imapclient.readthedocs.io/
Other
519 stars 85 forks source link

Self signed certificate exceptions #352

Closed dsanghan closed 6 years ago

dsanghan commented 6 years ago

With Python 3.6+ and latest imapclient, certain servers that use self-signed certificates are causing exceptions.

I've used an ssl context as outlined in your guide:

ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE

and verified that it does work with some self-signed configurations. With others, there's exceptions:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/ssl.py", line 1223, in get_server_certificate
    with context.wrap_socket(sock) as sslsock:
  File "/usr/lib/python3.6/ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "/usr/lib/python3.6/ssl.py", line 814, in __init__
    self.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 1068, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
OSError: [Errno 0] Error

For this particular ssl certificate, even a basic ssl.get_server_certificate faces the same error. This seems to be a python ssl issue, since I am able to fetch the cert using openssl s_client -showcerts -connect mail.mani.pt:993:

CONNECTED(00000003)
write:errno=0
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 176 bytes
Verification: OK
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : 0000
    Session-ID: 
    Session-ID-ctx: 
    Master-Key: 
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    Start Time: 1528460816
    Timeout   : 7200 (sec)
    Verify return code: 0 (ok)
    Extended master secret: no
---

Any suggestions would be welcome. Thanks!

dsanghan commented 6 years ago

To follow up,

Seems like a pythonic issue. I've opened a bug with them.

Output from Python3.5 with OpenSSL 1.0.2g: ssl.get_server_certificate(('mail.mani.pt', 993), ssl.PROTOCOL_TLSv1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0V\nMvwxQeeHcCz5HdU2o0ypROvkhG8Er5qGVeHAv+JCj+Q4EERMoDwocwS8eedwqqPe\nLVCWwSqS8SEizDRNZZfOoXT4AJ/L10RLrnz8wtSffoxS2pZMbhHEBr3WhA72v94L\nCDU+vO9t1YN3WpXeRZfKWLw/qEE8b65H\n-----END CERTIFICATE-----\n

Output from Python3.6 with OpenSSL 1.1.0: ssl.get_server_certificate(('mail.mani.pt', 993), ssl.PROTOCOL_TLSv1)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/ssl.py", line 1223, in get_server_certificate
    with context.wrap_socket(sock) as sslsock:
  File "/usr/lib/python3.6/ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "/usr/lib/python3.6/ssl.py", line 814, in __init__
    self.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 1068, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
OSError: [Errno 0] Error