FileNotFoundError: Could not find module 'libhsm.dll'

658 Views Asked by At

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:

  1. Is there any better package/library to aim this goal?
  2. How can I fix the issue?
2

There are 2 best solutions below

0
On

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

from pyhsm.hsmclient import HsmClient
c = HsmClient(pkcs11_lib="/usr/lib/vendorp11.so")
c.open_session(slot=1)
c.login(pin="partition_password")
c.logout()
c.close_session()
0
On

As far as I remember you need to install "HSM Access Provider" that provides this dll. It has two variants:

  • PCI Access Provider (for HSM physically installed in the computer)

  • Network Access Provider (for HSM accessed remotely over a network link)

The cryptoki dynamic library depends on the dynamic library libhsm.dll (shouldn't it be ethsm.dll?) provided by the access provider.

You can check your HSM access provider installation with the hsmstate command (as far as I remember it is part of the access provider installation).

You can check your overall HSM installation with the ctstat command.

If everything fails you can check your cryptoki.dll dependencies with Dependency Walker (Windows) or ldd command (Linux).

See the respective PTK-C Installation Guide.

Good luck!