Skip to content

Commit 3639994

Browse files
authored
Merge pull request #92 from grant100/wait_for_slot_event_f
pkcs11/_pkcs11.pyx: add wait_for_slot_event
2 parents c0a3fe5 + 4475448 commit 3639994

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

docs/api.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ Classes
5959

6060
:rtype: Token
6161

62+
.. method:: wait_for_slot_event(blocking=True)
63+
64+
Waits for a slot event such as a token insertion or removal to occur.
65+
66+
Returns the Slot on which the event was detected. If not blocking and no slot events were detected
67+
:class:`pkcs11.exceptions.NoEvent` is raised.
68+
69+
:param blocking: If true, the thread will block.
70+
71+
:rtype: Slot
72+
6273
.. method:: reinitialize()
6374

6475
Reinitializes the loaded PKCS#11 library.

pkcs11/_errors.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ cdef ERROR_MAP = {
3838
CKR_HOST_MEMORY: HostMemory,
3939
CKR_MECHANISM_INVALID: MechanismInvalid,
4040
CKR_MECHANISM_PARAM_INVALID: MechanismParamInvalid,
41+
CKR_NO_EVENT: NoEvent,
4142
CKR_OBJECT_HANDLE_INVALID: ObjectHandleInvalid,
4243
CKR_OPERATION_ACTIVE: OperationActive,
4344
CKR_OPERATION_NOT_INITIALIZED: OperationNotInitialized,

pkcs11/_pkcs11.pyx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,27 @@ cdef class lib:
15501550
except StopIteration:
15511551
return token
15521552

1553+
def wait_for_slot_event(self, blocking=True):
1554+
cdef CK_SLOT_ID slot_id
1555+
cdef CK_FLAGS flag = 0
1556+
1557+
if not blocking:
1558+
flag |= CKF_DONT_BLOCK
1559+
1560+
with nogil:
1561+
assertRV(_funclist.C_WaitForSlotEvent(flag, &slot_id, NULL))
1562+
1563+
cdef CK_SLOT_INFO info
1564+
1565+
with nogil:
1566+
assertRV(_funclist.C_GetSlotInfo(slot_id, &info))
1567+
1568+
slotDescription = info.slotDescription[:sizeof(info.slotDescription)]
1569+
manufacturerID = info.manufacturerID[:sizeof(info.manufacturerID)]
1570+
1571+
return Slot(self, slot_id, slotDescription, manufacturerID,
1572+
info.hardwareVersion, info.firmwareVersion, info.flags)
1573+
15531574
def reinitialize(self):
15541575
if _funclist != NULL:
15551576
with nogil:

pkcs11/_pkcs11_defn.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ cdef extern from '../extern/cryptoki.h':
148148
CK_FALSE,
149149

150150
cdef enum: # CK_FLAGS
151+
CKF_DONT_BLOCK,
151152
CKF_RW_SESSION,
152153
CKF_SERIAL_SESSION,
153154

pkcs11/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ class MultipleTokensReturned(PKCS11Error):
202202
"""
203203

204204

205+
class NoEvent(PKCS11Error):
206+
"""
207+
No slot events were detected.
208+
"""
209+
210+
205211
class NoSuchKey(PKCS11Error):
206212
"""
207213
No key matching the parameters was found.

0 commit comments

Comments
 (0)