Skip to content

Commit b0495cd

Browse files
authored
Merge pull request #83 from grant100/protected_authentication
pkcs11/_pkcs11.pyx: add support for protected authentication
2 parents 3629924 + 5edd88a commit b0495cd

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

pkcs11/_pkcs11.pyx

Lines changed: 13 additions & 1 deletion
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

@@ -235,6 +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`")
239+
elif user_pin is PROTECTED_AUTH:
240+
pin = None
241+
user_type = CKU_USER
242+
elif so_pin is PROTECTED_AUTH:
243+
pin = None
244+
user_type = CKU_SO
238245
elif user_pin is not None:
239246
pin = user_pin.encode('utf-8')
240247
user_type = CKU_USER
@@ -247,7 +254,12 @@ class Token(types.Token):
247254

248255
assertRV(_funclist.C_OpenSession(self.slot.slot_id, flags, NULL, NULL, &handle))
249256

250-
if pin is not None:
257+
if so_pin is PROTECTED_AUTH or user_pin is PROTECTED_AUTH:
258+
if self.flags & TokenFlag.PROTECTED_AUTHENTICATION_PATH:
259+
assertRV(_funclist.C_Login(handle, user_type, NULL, <CK_ULONG> 0))
260+
else:
261+
raise ArgumentsBad("Protected authentication is not supported by loaded module")
262+
elif pin is not None:
251263
assertRV(_funclist.C_Login(handle, user_type, pin, <CK_ULONG> len(pin)))
252264

253265
return Session(self, handle, rw=rw, user_type=user_type)

pkcs11/types.py

Lines changed: 4 additions & 1 deletion
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."""
@@ -203,7 +205,8 @@ def __eq__(self, other):
203205
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

0 commit comments

Comments
 (0)