bentonstark / py-hsm

Python module for accessing PKCS#11 compliant HSMs
Other
57 stars 18 forks source link

Library cannot find libhsm.dll file #17

Open E-genin opened 1 year ago

E-genin commented 1 year ago

Hi,

I recompiled the libhsm and generated libhsm.dll in Windows (using codeblocks IDE), I added the dll's path to the PATH system variable.

When I try to run the sample code in the readme file :

from pyhsm.hsmclient import HsmClient

# note: listing slot information does not require a login
with HsmClient(pkcs11_lib="./cs_pkcs11_R2_dll/cs_pkcs11_R2.dll") as c:
  for s in c.get_slot_info():
    print("----------------------------------------")
    print(s.to_string())

I get the following error saying it could not find the libhsm.dll file :

Traceback (most recent call last):
  File "C:\Users\me\Desktop\HSM_EOL\HSM_PY.py", line 5, in <module>
    with HsmClient(pkcs11_lib) as c:
  File "C:\Users\me\AppData\Roaming\Python\Python39\site-packages\pyhsm\hsmclient.py", line 138, in __init__
    self.__init_libhsm()
  File "C:\Users\me\AppData\Roaming\Python\Python39\site-packages\pyhsm\hsmclient.py", line 157, in __init_libhsm
    self.__libhsm = CDLL(self.__pyLibHsmName)
  File "C:\Program Files\Python39\lib\ctypes\__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'libhsm.dll' (or one of its dependencies). Try using the full path with constructor syntax.

Can you please tell me what am I missing ?

bentonstark commented 1 year ago

You can't give a relative path to a DLL. You have to supply the full path name. Please read the output of the error as it tells you the problem.

FileNotFoundError: Could not find module 'libhsm.dll' (or one of its dependencies). Try using the full path with constructor syntax.

E-genin commented 1 year ago

@bentonstark thanks for the reply,

My understanding is that there should be two *.dll files, one is the libhsm.dll that was generated and the other one provided by UTIMACO cs_pkcs11_R2.dll

The lib I provided a relative path to is the cs_pkcs11_R2.dll but the script error refers to the libhsm.dll to which I provided the absolute path in the PATH system variable.

bentonstark commented 1 year ago

There are at least two DLLs. The way it works is the libhsm.dll contains the PKCS-11 API interfaces and will invoke the cs_pkcs11_R2.dll which implements those interfaces. The cs_pkcs11_R2.dll also implements the proprietary vendor calls to the actual HSM device. If any dependencies are missing for the cs_pkcs11_R2.dll, the libhsm.dll cannot find the cs_pkcs11_R2.dll, or Windows cannot find the libhsm.dll on a LoadLibrary API call you will get an error.

E-genin commented 1 year ago

I am not sure I am missing any dependencies, I worked with cs_pkcs11_R2.dll in another application and it works fine. Maybe something went wrong during the generation of the libhsm.dll Could you please check with the attached dll or attach your dll just to confirm it is not related to the generation.
libhsm.zip

E-genin commented 1 year ago

hi @bentonstark , did you have a chance to test the dll I attached.

bentonstark commented 1 year ago

I don't know how to politely put this but I have a full time job and other priorities that exceed free debug testing.

E-genin commented 1 year ago

I fully understand @bentonstark , I didn't mean to offend you in any way. I will keep digging on the issue and share the solution here if I find something.