Skip to content

Commit 5edd88a

Browse files
author
Grant Sowards
committed
pkcs11/_pkcs11.pyx: use PROTECTED_AUTH sentinel vlaue; pkcs11/types.py: add PROTECTED_AUTH sentinel value and update doc string
1 parent 0e831d0 commit 5edd88a

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

pkcs11/_pkcs11.pyx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ from .types import (
3232
_CK_UTF8CHAR_to_str,
3333
_CK_VERSION_to_tuple,
3434
_CK_MECHANISM_TYPE_to_enum,
35+
PROTECTED_AUTH,
3536
)
3637

3738

@@ -225,7 +226,7 @@ class Slot(types.Slot):
225226
class Token(types.Token):
226227
"""Extend Token with implementation."""
227228

228-
def open(self, rw=False, user_pin=None, so_pin=None, use_protected_auth=False):
229+
def open(self, rw=False, user_pin=None, so_pin=None):
229230
cdef CK_SESSION_HANDLE handle
230231
cdef CK_FLAGS flags = CKF_SERIAL_SESSION
231232
cdef CK_USER_TYPE user_type
@@ -235,13 +236,12 @@ class Token(types.Token):
235236

236237
if user_pin is not None and so_pin is not None:
237238
raise ArgumentsBad("Set either `user_pin` or `so_pin`")
238-
elif user_pin is not None and use_protected_auth:
239-
raise ArgumentsBad("Set either `user_pin` or `use_protected_auth`")
240-
elif so_pin is not None and use_protected_auth:
241-
raise ArgumentsBad("Set either `so_pin` or `use_protected_auth`")
242-
elif use_protected_auth:
239+
elif user_pin is PROTECTED_AUTH:
243240
pin = None
244241
user_type = CKU_USER
242+
elif so_pin is PROTECTED_AUTH:
243+
pin = None
244+
user_type = CKU_SO
245245
elif user_pin is not None:
246246
pin = user_pin.encode('utf-8')
247247
user_type = CKU_USER
@@ -254,7 +254,7 @@ class Token(types.Token):
254254

255255
assertRV(_funclist.C_OpenSession(self.slot.slot_id, flags, NULL, NULL, &handle))
256256

257-
if use_protected_auth:
257+
if so_pin is PROTECTED_AUTH or user_pin is PROTECTED_AUTH:
258258
if self.flags & TokenFlag.PROTECTED_AUTHENTICATION_PATH:
259259
assertRV(_funclist.C_Login(handle, user_type, NULL, <CK_ULONG> 0))
260260
else:

pkcs11/types.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
SignatureLenRange,
2828
)
2929

30+
PROTECTED_AUTH = object()
31+
"""Indicate the pin should be supplied via an external mechanism (e.g. pin pad)"""
3032

3133
def _CK_UTF8CHAR_to_str(data):
3234
"""Convert CK_UTF8CHAR to string."""
@@ -200,10 +202,11 @@ def __init__(self, slot,
200202
def __eq__(self, other):
201203
return self.slot == other.slot
202204

203-
def open(self, rw=False, user_pin=None, so_pin=None, use_protected_auth=False):
205+
def open(self, rw=False, user_pin=None, so_pin=None):
204206
"""
205207
Open a session on the token and optionally log in as a user or
206-
security officer (pass one of `user_pin` or `so_pin`).
208+
security officer (pass one of `user_pin` or `so_pin`). Pass PROTECTED_AUTH to
209+
indicate the pin should be supplied via an external mechanism (e.g. pin pad).
207210
208211
Can be used as a context manager or close with :meth:`Session.close`.
209212
@@ -217,7 +220,6 @@ def open(self, rw=False, user_pin=None, so_pin=None, use_protected_auth=False):
217220
:param bytes user_pin: Authenticate to this session as a user.
218221
:param bytes so_pin: Authenticate to this session as a
219222
security officer.
220-
:param use_protected_auth: True to use protected authentication on a token
221223
222224
:rtype: Session
223225
"""

0 commit comments

Comments
 (0)