Skip to content

Commit 2557fa7

Browse files
committed
Refactor SMTP logic
1 parent 3e0015c commit 2557fa7

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

sslyze/connection_helpers/opportunistic_tls_helpers.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,36 @@ def prepare_socket_for_tls_handshake(self, sock: socket.socket) -> None:
7272
smtp.sock = sock
7373

7474
try:
75-
code, server_reply = smtp.getreply()
76-
message = server_reply.decode()
75+
code, server_reply_as_bytes = smtp.getreply()
7776
except SMTPException as exc:
78-
code, message = -1, str(exc)
77+
raise OpportunisticTlsError(f"Unexpected error while performing the SMTP EHLO handshake: {str(exc)}")
78+
7979
if code != 220:
80-
raise OpportunisticTlsError(f"Unable to find 220 service ready response: {message}")
80+
server_reply_as_str = server_reply_as_bytes.decode()
81+
raise OpportunisticTlsError(
82+
f"Server did not send a '220 service ready' SMTP message: {server_reply_as_str}"
83+
)
8184

8285
try:
83-
code, server_reply = smtp.ehlo()
84-
message = server_reply.decode()
86+
code, server_reply_as_bytes = smtp.ehlo()
8587
except SMTPException as exc:
86-
code, message = -1, str(exc)
88+
raise OpportunisticTlsError(f"Unexpected error while performing the SMTP EHLO handshake: {str(exc)}")
89+
8790
if code != 250:
88-
raise OpportunisticTlsError(f"SMTP EHLO was rejected: {message}")
91+
server_reply_as_str = server_reply_as_bytes.decode()
92+
raise OpportunisticTlsError(f"SMTP EHLO was rejected: {server_reply_as_str}")
8993

9094
if not smtp.has_extn("starttls"):
91-
raise OpportunisticTlsError(f"Server does not support STARTTLS: {message}")
95+
raise OpportunisticTlsError("Server does not support STARTTLS with SMTP")
9296

9397
try:
94-
code, server_reply = smtp.docmd("STARTTLS")
95-
message = server_reply.decode()
98+
code, server_reply_as_bytes = smtp.docmd("STARTTLS")
9699
except SMTPException as exc:
97-
code, message = -1, str(exc)
100+
raise OpportunisticTlsError(f"Unexpected error while performing the SMTP EHLO handshake: {str(exc)}")
101+
98102
if code != 220:
99-
raise OpportunisticTlsError(f"SMTP STARTTLS rejected: {message}")
103+
server_reply_as_str = server_reply_as_bytes.decode()
104+
raise OpportunisticTlsError(f"SMTP STARTTLS rejected: {server_reply_as_str}")
100105

101106

102107
class _XmppHelper(_OpportunisticTlsHelper):
@@ -242,15 +247,15 @@ class _PostgresHelper(_GenericOpportunisticTlsHelper):
242247

243248

244249
def get_opportunistic_tls_helper(
245-
protocol: ProtocolWithOpportunisticTlsEnum, xmpp_to_hostname: Optional[str], smtp_ehlo_hostname: str
250+
protocol: ProtocolWithOpportunisticTlsEnum, xmpp_to_hostname: Optional[str], smtp_ehlo_hostname: Optional[str]
246251
) -> _OpportunisticTlsHelper:
247252
helper_cls = _START_TLS_HELPER_CLASSES[protocol]
248253
if protocol in [ProtocolWithOpportunisticTlsEnum.XMPP, ProtocolWithOpportunisticTlsEnum.XMPP_SERVER]:
249254
if xmpp_to_hostname is None:
250255
raise ValueError("Received None for xmpp_to_hostname")
251256
opportunistic_tls_helper = helper_cls(xmpp_to=xmpp_to_hostname)
252257
elif protocol == ProtocolWithOpportunisticTlsEnum.SMTP:
253-
opportunistic_tls_helper = helper_cls(smtp_ehlo_hostname)
258+
opportunistic_tls_helper = helper_cls(smtp_ehlo_hostname=smtp_ehlo_hostname)
254259
else:
255260
opportunistic_tls_helper = helper_cls()
256261

sslyze/server_setting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def __post_init__(self) -> None:
206206
else:
207207
if self.xmpp_to_hostname:
208208
raise InvalidServerNetworkConfigurationError("Can only specify xmpp_to for the XMPP StartTLS protocol.")
209-
209+
210210
if self.tls_opportunistic_encryption in [
211211
ProtocolWithOpportunisticTlsEnum.SMTP,
212212
]:
@@ -217,7 +217,7 @@ def __post_init__(self) -> None:
217217
raise InvalidServerNetworkConfigurationError(
218218
"Can only specify smtp_ehlo_hostname for the SMTP StartTLS protocol."
219219
)
220-
220+
221221
if self.tls_opportunistic_encryption and self.http_user_agent:
222222
raise InvalidServerNetworkConfigurationError(
223223
"Cannot specify both tls_opportunistic_encryption and http_user_agent"

0 commit comments

Comments
 (0)