Skip to content

Commit d358d3c

Browse files
authored
Merge pull request #123 from kewisch/implicit-tls
Support implicit TLS connections
2 parents cb690f5 + 4339726 commit d358d3c

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ it.
134134

135135
For the ``AUTHENTICATE`` command, supported mechanisms are ``DIGEST-MD5``,
136136
``PLAIN``, ``LOGIN``, ``OAUTHBEARER`` and ``XOAUTH2``.
137+
138+
Both explicit TLS via STARTTLS and implicit TLS are supported.
137139

138140
Basic usage
139141
^^^^^^^^^^^

sievelib/managesieve.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from functools import wraps
1313
import re
1414
import socket
15-
import ssl
15+
import ssl as ssllib
1616
from typing import Any, List, Optional, Tuple
1717

1818
from .digest_md5 import DigestMD5
@@ -127,7 +127,7 @@ def __read_block(self, size: int) -> bytes:
127127
return buf
128128
try:
129129
buf += self.sock.recv(size)
130-
except (socket.timeout, ssl.SSLError):
130+
except (socket.timeout, ssllib.SSLError):
131131
raise Error("Failed to read %d bytes from the server" % size)
132132
self.__dprint(buf)
133133
return buf
@@ -162,7 +162,7 @@ def __read_line(self) -> bytes:
162162
if not len(nval):
163163
break
164164
self.__read_buffer += nval
165-
except (socket.timeout, ssl.SSLError):
165+
except (socket.timeout, ssllib.SSLError):
166166
raise Error("Failed to read data from the server")
167167

168168
if len(ret):
@@ -504,13 +504,13 @@ def __starttls(self, keyfile=None, certfile=None) -> bool:
504504
code, data = self.__send_command("STARTTLS")
505505
if code != "OK":
506506
return False
507-
context = ssl.create_default_context()
507+
context = ssllib.create_default_context()
508508
if certfile is not None:
509509
context.load_cert_chain(certfile, keyfile=keyfile)
510510
try:
511-
# nsock = ssl.wrap_socket(self.sock, keyfile, certfile)
511+
# nsock = ssllib.wrap_socket(self.sock, keyfile, certfile)
512512
nsock = context.wrap_socket(self.sock, server_hostname=self.srvaddr)
513-
except ssl.SSLError as e:
513+
except ssllib.SSLError as e:
514514
raise Error("SSL error: %s" % str(e))
515515
self.sock = nsock
516516
self.__capabilities = {}
@@ -565,6 +565,7 @@ def connect(
565565
password: str,
566566
authz_id: str = "",
567567
starttls: bool = False,
568+
ssl: bool = False,
568569
authmech: Optional[str] = None,
569570
):
570571
"""Establish a connection with the server.
@@ -575,6 +576,7 @@ def connect(
575576
:param login: username
576577
:param password: clear password
577578
:param starttls: use a TLS connection or not
579+
:param ssl: use implict TLS/SSL when connecting
578580
:param authmech: prefered authenticate mechanism
579581
:rtype: boolean
580582
"""
@@ -584,9 +586,13 @@ def connect(
584586
except socket.error as msg:
585587
raise Error("Connection to server failed: %s" % str(msg))
586588

589+
if ssl:
590+
context = ssllib.create_default_context()
591+
self.sock = context.wrap_socket(self.sock, server_hostname=self.srvaddr)
592+
587593
if not self.__get_capabilities():
588594
raise Error("Failed to read capabilities from server")
589-
if starttls and not self.__starttls():
595+
if not ssl and starttls and not self.__starttls():
590596
return False
591597
if self.__authenticate(login, password, authz_id, authmech):
592598
return True

0 commit comments

Comments
 (0)