Skip to content

Commit 7ab1d8a

Browse files
committed
ussl: Fix exception on missing SSL Context attributes.
Signed-off-by: iabdalkader <[email protected]>
1 parent cfb5de9 commit 7ab1d8a

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/arduino_iot_cloud/ussl.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# SSL module with m2crypto backend for HSM support.
88

99
import ssl
10+
import logging
1011

1112
pkcs11 = None
1213

@@ -29,7 +30,7 @@ def wrap_socket(sock, ssl_params={}):
2930
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
3031
if hasattr(ctx, "set_default_verify_paths"):
3132
ctx.set_default_verify_paths()
32-
if verify != ssl.CERT_REQUIRED:
33+
if hasattr(ctx, "check_hostname") and verify != ssl.CERT_REQUIRED:
3334
ctx.check_hostname = False
3435
ctx.verify_mode = verify
3536
if keyfile is not None and certfile is not None:
@@ -40,8 +41,12 @@ def wrap_socket(sock, ssl_params={}):
4041
ctx.load_verify_locations(cafile, cadata)
4142
return ctx.wrap_socket(sock, server_hostname=hostname)
4243
else:
43-
# Use M2Crypto to load key and cert from HSM.
44-
from M2Crypto import m2, SSL, Engine
44+
try:
45+
# Use M2Crypto to load key and cert from HSM.
46+
from M2Crypto import m2, SSL, Engine
47+
except (ImportError, AttributeError):
48+
logging.error("The m2crypto module is required to use HSM.")
49+
sys.exit(1)
4550

4651
global pkcs11
4752
if pkcs11 is None:

0 commit comments

Comments
 (0)