0

I'm trying to use pkcs11 from Python and I have a problem. I had view many examples and all of them start with the same code. However, when I execute it, console raises error in the third line of the code.

Code:

import pkcs11
import os
# Initialise our PKCS#11 library
lib = pkcs11.lib(os.environ['PKCS11_MODULE'])
token = lib.get_token(token_label='DEMO')

Error in the line:

lib = pkcs11.lib(os.environ['PKCS11_MODULE'])

Error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/UserDict.py", line 40, in __getitem__
raise KeyError(key)
KeyError: 'PKCS11_MODULE'
1
  • The Python module doesn't implement PKSC#11 itself. It is just a Python wrapper for a module that does the real work. It looks like you haven't installed that. Look at github.com/OpenSC/OpenSC/wiki . Commented Feb 6, 2019 at 9:44

2 Answers 2

2

pkcs11 wraps a native library. It expects to find that library in a path given by the PKCS11_MODULE environment variable.

You should set that environment variable to where you have installed the native component.

$ export PKCS11_MODULE='/some/path'
$ python myscript.py
Sign up to request clarification or add additional context in comments.

Comments

0

to solve this you have to add the route of your driver (in windows a file .dll in linux a file .so), like this:

import pkcs11


lib = pkcs11.lib("C:/Windows/System32/eps2003csp11.dll")

for slot in lib.get_slots():
    token = slot.get_token()
    print(token)

    if token.label == '...':
        break

In my case, i work with usb token. With this the result is the username of the token connected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.