9
9
import ssl
10
10
import sys
11
11
import logging
12
+ import binascii
12
13
13
14
pkcs11 = None
14
15
15
16
# Default engine and provider.
16
17
_ENGINE_PATH = "/usr/lib/engines-3/libpkcs11.so"
17
18
_MODULE_PATH = "/usr/lib/softhsm/libsofthsm2.so"
18
19
20
+ # Reference EC key for NXP's PlugNTrust
21
+ _EC_REF_KEY = binascii .unhexlify (
22
+ b"3041020100301306072a8648ce3d020106082a8648ce3d03010704273025"
23
+ b"0201010420100000000000000000000000000000000000ffffffffa5a6b5"
24
+ b"b6a5a6b5b61000"
25
+ )
26
+
19
27
20
28
def wrap_socket (sock , ssl_params = {}):
21
29
keyfile = ssl_params .get ("keyfile" , None )
@@ -25,9 +33,19 @@ def wrap_socket(sock, ssl_params={}):
25
33
ciphers = ssl_params .get ("ciphers" , None )
26
34
verify = ssl_params .get ("verify_mode" , ssl .CERT_NONE )
27
35
hostname = ssl_params .get ("server_hostname" , None )
28
- use_hsm = ssl_params .get ("use_hsm" , False )
36
+ micropython = sys .implementation .name == "micropython"
37
+
38
+ if keyfile is not None and "token" in keyfile and micropython :
39
+ # Create a reference EC key for NXP EdgeLock device.
40
+ objid = int (keyfile .split ("=" )[1 ], 16 ).to_bytes (4 , "big" )
41
+ keyfile = _EC_REF_KEY [0 :53 ] + objid + _EC_REF_KEY [57 :]
42
+ # Load the certificate from the secure element (when supported).
43
+ # import cryptoki
44
+ # with cryptoki.open() as token:
45
+ # cert = token.read(0x65, 412)
29
46
30
- if not use_hsm :
47
+ if keyfile is None or "token" not in keyfile :
48
+ # Use MicroPython/CPython SSL to wrap socket.
31
49
ctx = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
32
50
if hasattr (ctx , "set_default_verify_paths" ):
33
51
ctx .set_default_verify_paths ()
@@ -39,7 +57,7 @@ def wrap_socket(sock, ssl_params={}):
39
57
if ciphers is not None :
40
58
ctx .set_ciphers (ciphers )
41
59
if cafile is not None or cadata is not None :
42
- ctx .load_verify_locations (cafile , cadata )
60
+ ctx .load_verify_locations (cafile = cafile , cadata = cadata )
43
61
return ctx .wrap_socket (sock , server_hostname = hostname )
44
62
else :
45
63
# Use M2Crypto to load key and cert from HSM.
0 commit comments