bamthomas / aioimaplib

Python asyncio IMAP4rev1 client library
GNU General Public License v3.0
135 stars 58 forks source link

Py 3.10: ssl.SSLError: Cannot create a client socket with a PROTOCOL_TLS_SERVER context #78

Closed frenck closed 2 years ago

frenck commented 2 years ago

Reported at the Home Assistant project:

2022-07-06 11:27:01 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1089, in create_connection
    transport, protocol = await self._create_connection_transport(
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1119, in _create_connection_transport
    await waiter
  File "/usr/local/lib/python3.10/asyncio/sslproto.py", line 637, in _on_handshake_complete
    raise handshake_exc
  File "/usr/local/lib/python3.10/asyncio/sslproto.py", line 682, in _process_write_backlog
    ssldata = self._sslpipe.do_handshake(
  File "/usr/local/lib/python3.10/asyncio/sslproto.py", line 116, in do_handshake
    self._sslobj = self._context.wrap_bio(
  File "/usr/local/lib/python3.10/ssl.py", line 527, in wrap_bio
    return self.sslobject_class._create(
  File "/usr/local/lib/python3.10/ssl.py", line 866, in _create
    sslobj = context._wrap_bio(
ssl.SSLError: Cannot create a client socket with a PROTOCOL_TLS_SERVER context (_ssl.c:801)
No error when I disable imap sensor in config and restart

This seems to be related to more strict handling in Python 3.10 (which the Home Assistant project has been switched to).

It happens when setting the default SSL context:

https://github.com/bamthomas/aioimaplib/blob/544e735852599cfe8ba8251561460494b9404418/aioimaplib/aioimaplib.py#L834

According to documentation, however, this is used to create a server side sockets:

image

ref: https://docs.python.org/3/library/ssl.html#ssl.Purpose.CLIENT_AUTH

I guess it needs to be using: CleanShot 2022-07-06 at 08 43 21

ref: https://docs.python.org/3/library/ssl.html#ssl.Purpose.SERVER_AUTH

Downstream issue: https://github.com/home-assistant/core/issues/74487

nao-pon commented 2 years ago

@frenck thank you! After fixing it as you pointed out, it works fine. 😄

J-o-h-n-M commented 2 years ago

@frenck thank you! After fixing it as you pointed out, it works fine. 😄

Where can I find the file in home assistant? I searched everywhere but cant find aioimaplib.py. I would like to fix it and then hopefully on the next update it will be overwritten with a good version by itself. I am on Home assistant OS.

nao-pon commented 2 years ago

It needs to be fixed in the homeassistant instance of docker.

@J-o-h-n-M You need to shell log in to the host as an administrator using SSH & Web Terminal add-on etc.

ssh hassio@192.168.x.x
docker exec -it homeassistant /bin/bash

vi /usr/local/lib/python3.10/site-packages/aioimaplib/aioimaplib.py
J-o-h-n-M commented 2 years ago

@nao-pon Thanks! I also made the modification that @frenck suggested and indeed it works like a charm again.

J-o-h-n-M commented 2 years ago

Any idea when this will be in a HA release? I have to update manually every time now.

frenck commented 2 years ago

Any idea when this will be in a HA release?

Depends when this issue is addressed in this library :)

DavidFW1960 commented 2 years ago

Any idea when this will be in a HA release? I have to update manually every time now.

I made a custom_component that fixes this until they merge the fix here. Referenced in the HA thread here https://github.com/home-assistant/core/issues/74487#issuecomment-1192136483