Skip to content

Commit 153090b

Browse files
committed
Add check for builtin ssl module
Created exception for absence of ssl built-in module. Added first pass at X.509 certificate support
1 parent e8a9a45 commit 153090b

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

adafruit_azureiot/device_registration.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
1313
* Author(s): Jim Bennett, Elena Horton
1414
"""
15+
SSL_MODULE = True
1516

1617
import json
17-
import ssl
1818
import time
19+
try:
20+
import ssl
21+
except:
22+
SSL_MODULE = False
1923

2024
import adafruit_logging as logging
2125
from adafruit_logging import Logger
@@ -51,6 +55,9 @@ def __init__(
5155
device_id: str,
5256
device_sas_key: str,
5357
logger: Logger = None,
58+
device_certificate = None,
59+
private_certificate_key = None,
60+
use_builtin_ssl_module = True
5461
):
5562
"""Creates an instance of the device registration service
5663
@@ -77,6 +84,10 @@ def __init__(
7784
self._socket = socket
7885
self._iface = iface
7986

87+
self._device_certificate = device_certificate
88+
self._private_certificate_key = private_certificate_key
89+
self._use_builtin_ssl_module = use_builtin_ssl_module
90+
8091
# pylint: disable=W0613
8192
# pylint: disable=C0103
8293
def _on_connect(self, client, userdata, _, rc) -> None:
@@ -196,15 +207,28 @@ def register_device(self, expiry: int) -> str:
196207

197208
MQTT.set_socket(self._socket, self._iface)
198209

199-
self._mqtt = MQTT.MQTT(
200-
broker=constants.DPS_END_POINT,
201-
username=username,
202-
password=auth_string,
203-
port=8883,
204-
keep_alive=120,
205-
client_id=self._device_id,
206-
ssl_context=ssl.create_default_context(),
207-
)
210+
if self._use_builtin_ssl_module and SSL_MODULE:
211+
self._mqtt = MQTT.MQTT(
212+
broker=constants.DPS_END_POINT,
213+
username=username,
214+
password=auth_string,
215+
port=8883,
216+
keep_alive=120,
217+
client_id=self._device_id,
218+
ssl_context=ssl.create_default_context(),
219+
)
220+
else:
221+
if self._device_certificate is not None:
222+
self.iface.set_certificate(self._device_certificate)
223+
self.iface.set_private_key(self._private_certificate_key)
224+
self._mqtt = MQTT.MQTT(
225+
broker=constants.DPS_END_POINT,
226+
username=username,
227+
password=auth_string,
228+
port=8883,
229+
keep_alive=120,
230+
client_id=self._device_id
231+
)
208232

209233
self._mqtt.enable_logger(logging, self._logger.getEffectiveLevel())
210234

adafruit_azureiot/iot_mqtt.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
* Author(s): Jim Bennett, Elena Horton
1313
"""
1414

15+
SSL_MODULE = True
16+
1517
import gc
1618
import json
17-
import ssl
1819
import time
20+
try:
21+
import ssl
22+
except:
23+
SSL_MODULE = False
1924

2025
import adafruit_minimqtt.adafruit_minimqtt as MQTT
2126
import adafruit_logging as logging
@@ -132,15 +137,28 @@ def _create_mqtt_client(self) -> None:
132137
)
133138
)
134139

135-
self._mqtts = MQTT.MQTT(
140+
if self._use_builtin_ssl_module and SSL_MODULE:
141+
self._mqtt = MQTT.MQTT(
136142
broker=self._hostname,
137143
username=self._username,
138144
password=self._passwd,
139145
port=8883,
140146
keep_alive=120,
141147
client_id=self._device_id,
142148
ssl_context=ssl.create_default_context(),
143-
)
149+
)
150+
else:
151+
if self._device_certificate is not None:
152+
self.iface.set_certificate(self._device_certificate)
153+
self.iface.set_private_key(self._private_certificate_key)
154+
self._mqtt = MQTT.MQTT(
155+
broker=self._hostname,
156+
username=self._username,
157+
password=self._passwd,
158+
port=8883,
159+
keep_alive=120,
160+
client_id=self._device_id
161+
)
144162

145163
self._mqtts.enable_logger(logging, self._logger.getEffectiveLevel())
146164

@@ -332,6 +350,9 @@ def __init__(
332350
device_sas_key: str,
333351
token_expires: int = 21600,
334352
logger: Logger = None,
353+
device_certificate = None,
354+
private_certificate_key = None,
355+
use_builtin_ssl_module = True
335356
):
336357
"""Create the Azure IoT MQTT client
337358
@@ -365,6 +386,10 @@ def __init__(
365386
self._logger.addHandler(logging.StreamHandler())
366387
self._is_subscribed_to_twins = False
367388

389+
self._device_certificate = device_certificate
390+
self._private_certificate_key = private_certificate_key
391+
self._use_builtin_ssl_module = use_builtin_ssl_module
392+
368393
def _subscribe_to_core_topics(self):
369394
device_bound_topic = "devices/{}/messages/devicebound/#".format(self._device_id)
370395
self._mqtts.add_topic_callback(

0 commit comments

Comments
 (0)