Skip to content

Commit 54bbe43

Browse files
James Boultonclaude
andcommitted
TLS: a context with a TLS 1.2 minimum, not PROTOCOL_TLSv1_2
Python built with OpenSSL 4 (Homebrew's 3.14.10) has no ssl.PROTOCOL_TLSv1_2, so creating a DashConnection with a password login raised AttributeError, as did the data exporter. Both now use server_tls_context(): ssl.create_default_context() with minimum_version TLSv1_2, which checks the server's certificate and host name as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7fc42d8 commit 54bbe43

2 files changed

Lines changed: 13 additions & 19 deletions

File tree

‎dashio/dash_connection.py‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ def servername(self, val: str):
130130
self._cfg["hostURL"] = val
131131

132132

133+
def server_tls_context() -> ssl.SSLContext:
134+
"""TLS 1.2 or later, checking the Dash server's certificate and host name against the system's CAs.
135+
136+
A context rather than tls_set(tls_version=ssl.PROTOCOL_TLSv1_2): Python built with OpenSSL 4 has no
137+
PROTOCOL_TLSv1_2."""
138+
context = ssl.create_default_context()
139+
context.minimum_version = ssl.TLSVersion.TLSv1_2
140+
return context
141+
142+
133143
class DashConnection(threading.Thread):
134144
"""Setups and manages a connection thread to the Dash Server.
135145
@@ -484,15 +494,7 @@ def _new_client(self, client_id: str = "", tls_context: ssl.SSLContext | None =
484494
if tls_context is not None:
485495
client.tls_set_context(tls_context)
486496
elif self.use_ssl:
487-
client.tls_set(
488-
ca_certs=None,
489-
certfile=None,
490-
keyfile=None,
491-
cert_reqs=ssl.CERT_REQUIRED,
492-
tls_version=ssl.PROTOCOL_TLSv1_2,
493-
ciphers=None,
494-
)
495-
client.tls_insecure_set(False)
497+
client.tls_set_context(server_tls_context())
496498
# A device whose certificate is refused waits up to an hour between attempts.
497499
client.reconnect_delay_set(1, CERT_RECONNECT_MAX_DELAY if tls_context is not None else RECONNECT_MAX_DELAY)
498500
return client

‎dashio/utilities/dashio_data_exporter.py‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import argparse
55
import time
6-
import ssl
6+
from dashio.dash_connection import server_tls_context
77
import socket
88
import datetime
99
import json
@@ -175,15 +175,7 @@ def main():
175175
mqtt_client.on_subscribe = on_subscribe
176176

177177
# Setup connection
178-
mqtt_client.tls_set(
179-
ca_certs=None,
180-
certfile=None,
181-
keyfile=None,
182-
cert_reqs=ssl.CERT_REQUIRED,
183-
tls_version=ssl.PROTOCOL_TLSv1_2,
184-
ciphers=None,
185-
)
186-
mqtt_client.tls_insecure_set(False)
178+
mqtt_client.tls_set_context(server_tls_context())
187179
mqtt_client.username_pw_set(args.username, args.password)
188180

189181
# MQTT Topics to request and receive the data. A shared device's store is under the owner's username.

0 commit comments

Comments
 (0)