Skip to content

Commit da988b1

Browse files
committed
ussl: Add and check cert_reqs arg.
1 parent bcaa0df commit da988b1

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

arduino_iot_cloud/ussl.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626

2727
from M2Crypto import Engine, m2, SSL
2828

29+
CERT_NONE = SSL.verify_none
30+
CERT_REQUIRED = SSL.verify_peer
31+
2932
_key = None
3033
_cert = None
3134

3235
# Default engine and provider.
33-
ENGINE_PATH = "/usr/lib/engines-1.1/libpkcs11.so"
34-
MODULE_PATH = "/usr/lib/softhsm/libsofthsm2.so"
36+
_ENGINE_PATH = "/usr/lib/engines-1.1/libpkcs11.so"
37+
_MODULE_PATH = "/usr/lib/softhsm/libsofthsm2.so"
3538

3639

3740
def init(pin, certfile, keyfile, engine_path, module_path):
@@ -51,9 +54,10 @@ def wrap_socket(
5154
certfile,
5255
keyfile,
5356
ca_certs=None,
57+
cert_reqs=CERT_NONE,
5458
ciphers=None,
55-
engine_path=ENGINE_PATH,
56-
module_path=MODULE_PATH,
59+
engine_path=_ENGINE_PATH,
60+
module_path=_MODULE_PATH,
5761
):
5862
if _key is None or _cert is None:
5963
init(pin, certfile, keyfile, engine_path, module_path)
@@ -66,7 +70,7 @@ def wrap_socket(
6670
if ciphers is not None:
6771
ctx.set_cipher_list(ciphers)
6872

69-
if ca_certs is not None:
73+
if ca_certs is not None and cert_reqs is not CERT_NONE:
7074
if ctx.load_verify_locations(ca_certs) != 1:
7175
raise Exception("Failed to load CA certs")
7276
ctx.set_verify(SSL.verify_peer, depth=9)

examples/example.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from arduino_iot_cloud import ColoredLight
1212
from random import randint
1313
import argparse
14+
import arduino_iot_cloud.ussl as ssl
1415

1516
KEY_PATH = "pkcs11:token=arduino"
1617
CERT_PATH = "pkcs11:token=arduino"
@@ -45,7 +46,9 @@ async def main():
4546
# client = AIOTClient(device_id, username="username", password="password")
4647
client = AIOTClient(
4748
device_id=DEVICE_ID,
48-
ssl_params={"pin": "1234", "keyfile": KEY_PATH, "certfile": CERT_PATH, "ca_certs": CA_PATH},
49+
ssl_params={"pin": "1234",
50+
"keyfile": KEY_PATH, "certfile": CERT_PATH, "ca_certs": CA_PATH, "cert_reqs":ssl.CERT_REQUIRED
51+
},
4952
)
5053

5154
# Register cloud objects. Note these objects must be created first in the dashboard.

0 commit comments

Comments
 (0)