From within a single python thread, as shown below, I get the error "Please insert a token in any slot" and it seems to not see my token. I change the code to not run from inside a multiprocessing Thread and it works. To take the PyKCS11 library out of the equation I also tested by using ctypes and wrapping the standard pkcs11 functions implemented in opensc, I still run into the same issue where it works except when run from a python Thread. What would cause this?
Using pkcs11 from inside a python Thread fails:
from PyKCS11 import LowLevel
import sys
from multiprocessing import Thread
class MyThread(Thread):
def run(self):
lib = "/usr/local/lib/opensc-pkcs11.so" # place here your PKCS#11 library
pin = "12345678" # place here the pin of your token
a = LowLevel.CPKCS11Lib()
info = LowLevel.CK_INFO()
slotList = LowLevel.ckintlist()
loadRes = a.Load(lib, 1)
print "Load of library '%s' : %s " % (lib, str(loadRes) )
if not loadRes:
sys.exit(1)
print "C_GetInfo: rv=" , hex(a.C_GetInfo(info))
print "Library manufacturerID: ", info.GetManufacturerID()
# listing only slots with a token inside.
rv = a.C_GetSlotList(1, slotList)
if (rv != LowLevel.CKR_OK):
sys.exit(1)
if len(slotList) == 0:
print "Please insert a token in any slot"
sys.exit(1)
mythread = MyThread()
mythread.start()
mythread.join()
Using pkcs11 outside of a Thread works:
from PyKCS11 import LowLevel
import sys
def run(self):
lib = "/usr/local/lib/opensc-pkcs11.so" # place here your PKCS#11 library
pin = "12345678" # place here the pin of your token
a = LowLevel.CPKCS11Lib()
info = LowLevel.CK_INFO()
slotList = LowLevel.ckintlist()
loadRes = a.Load(lib, 1)
print "Load of library '%s' : %s " % (lib, str(loadRes) )
if not loadRes:
sys.exit(1)
print "C_GetInfo: rv=" , hex(a.C_GetInfo(info))
print "Library manufacturerID: ", info.GetManufacturerID()
# listing only slots with a token inside.
rv = a.C_GetSlotList(1, slotList)
if (rv != LowLevel.CKR_OK):
sys.exit(1)
if len(slotList) == 0:
print "Please insert a token in any slot"
sys.exit(1)
run()
Testing Environment:
OS: OSX Yosemite
pkcs11 middleware: opensc