Skip to content

Commit 37cd85e

Browse files
author
Steffen Klemer
committed
pkcs11: Use 1st slot as default, not slot '0'.
1 parent 982550b commit 37cd85e

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/xmlsec/pk11.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def parse_uri(pk11_uri):
3636

3737
logging.debug("parsed pkcs11 uri: %s" % repr(o))
3838

39-
slot = 0
39+
slot = None
4040
library = None
4141
keyname = None
4242
query = {}
@@ -134,8 +134,19 @@ def _find_key(session, keyname):
134134
_session_lock = threading.RLock()
135135

136136

137-
def _session(library, slot, pin=None):
137+
def _session(library, slot=None, pin=None, pk11_uri=None):
138138
_session_lock.acquire()
139+
140+
# XXX: adhoc fix -- should test cases where slot, pin and pk11_uri
141+
# contradict or both are 'None'
142+
if slot is None and pk11_uri is not None:
143+
library, slot, keyname, query = parse_uri(pk11_uri)
144+
pin_spec = query.get('pin', "env:PYKCS11PIN")
145+
if pin_spec.startswith("env:"):
146+
pin = os.environ.get(pin_spec[4:], None)
147+
else:
148+
pin = pin_spec
149+
139150
if not library in _modules:
140151
logging.debug("loading library %s" % library)
141152
lib = PyKCS11.PyKCS11Lib()
@@ -148,6 +159,13 @@ def _session(library, slot, pin=None):
148159
logging.debug("already loaded: %s: %s" % (library, _modules[library]))
149160

150161
lib = _modules[library]
162+
163+
# XXX: adhoc fix: if no slot given, use the first in the list
164+
# (not the one named '0')
165+
# Should be replaced by some proper pkcs11 uri search
166+
if slot is None:
167+
slot = lib.getSlotList(tokenPresent=True)[0]
168+
151169
session = lib.openSession(slot)
152170
if pin is not None:
153171
assert type(pin) == str # session.login does not like unicode

0 commit comments

Comments
 (0)