I'm trying to connect to an Eracom HSM [which is from about 15 years ago!] using Python. My Google searches led me to a library named py-hsm. It seems that it has a really straightforward usage based on the documentation. But when I tried to use it, I faced the following error:
1 C:\> python
2 Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32
3
4 >>> from pyhsm.hsmclient import HsmClient
5 >>> c = HsmClient(pkcs11_lib="C:\Eracom\ProtectToolkit C SDK\bin\sw\cryptoki.dll")
6 Traceback (most recent call last):
7 File "<stdin>", line 1, in <module>
8 File "C:\Python\Python38-32\lib\site-packages\pyhsm\hsmclient.py", line 138, in __init__
9 self.__init_libhsm()
10 File "C:\Python\Python38-32\lib\site-packages\pyhsm\hsmclient.py", line 157, in __init_libhsm
11 self.__libhsm = CDLL(self.__pyLibHsmName)
12 File "C:\Python\Python38-32\lib\ctypes\__init__.py", line 373, in __init__
13 self._handle = _dlopen(self._name, mode)
14 FileNotFoundError: Could not find module 'libhsm.dll' (or one of its dependencies). Try using the full path with constructor syntax.
15 >>>
I did a wholes system search for the mentioned DLL (libhsm.dll), but I found nothing.
Question1:
- Is there any better package/library to aim this goal?
- How can I fix the issue?
You are going to need to download, compile and install the libhsm.so co-component on your host before you can use the py-hsm module. The py-hsm module is a high-level wrapper for all the heavy lifting HSM PKCS-11 C code which exists in libhsm.
https://github.com/bentonstark/libhsm
Also note that the python module is looking for libhsm.dll which tells me you are on a Window host. This solution will run on Windows but it will require some work to get the libhsm library to compile on Windows. If you can target Linux things will go much easier.
Once you do that, the last step is to tell py-hsm the location of your Eracom vendor PKCS API library. Note that I tested this library with a lot of HSMs but Eracom was not one of them. https://github.com/bentonstark/py-hsm