Skip to content

Commit 287dd87

Browse files
committed
Add Implementations and tests for C_InitToken and C_InitPin
Added as Token.init_token and Session.init_pin
1 parent c148a2f commit 287dd87

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

pkcs11/_pkcs11.pyx

+46
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,31 @@ class Slot(types.Slot):
253253
class Token(types.Token):
254254
"""Extend Token with implementation."""
255255

256+
def init_token(self, token_label, so_pin):
257+
cdef CK_SLOT_ID slot_id = self.slot.slot_id
258+
cdef CK_UTF8CHAR *pin_data
259+
cdef CK_ULONG pin_length
260+
cdef CK_UTF8CHAR *label
261+
262+
if token_label is None or so_pin is None:
263+
raise ArgumentsBad("Set both `token_label` and `so_pin`")
264+
265+
pin = so_pin.encode('utf-8')
266+
tlabel = token_label.encode('utf-8')
267+
268+
if pin and tlabel:
269+
pin_data = pin
270+
pin_length = len(pin)
271+
label = tlabel
272+
273+
with nogil:
274+
assertRV(_funclist.C_InitToken(slot_id, pin_data, pin_length,
275+
label))
276+
277+
return True
278+
279+
return False
280+
256281
def open(self, rw=False, user_pin=None, so_pin=None):
257282
cdef CK_SLOT_ID slot_id = self.slot.slot_id
258283
cdef CK_SESSION_HANDLE handle
@@ -373,6 +398,27 @@ def merge_templates(default_template, *user_templates):
373398
class Session(types.Session):
374399
"""Extend Session with implementation."""
375400

401+
def init_pin(self, user_pin):
402+
cdef CK_OBJECT_HANDLE handle = self._handle
403+
cdef CK_UTF8CHAR *pin_data
404+
cdef CK_ULONG pin_length
405+
406+
if user_pin is None:
407+
raise ArgumentsBad("Set `user_pin`")
408+
409+
pin = user_pin.encode('utf-8')
410+
411+
if pin:
412+
pin_data = pin
413+
pin_length = len(pin)
414+
415+
with nogil:
416+
assertRV(_funclist.C_InitPIN(handle, pin_data, pin_length))
417+
418+
return True
419+
420+
return False
421+
376422
def close(self):
377423
cdef CK_OBJECT_HANDLE handle = self._handle
378424

tests/test_sessions.py

+13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ def test_open_session_and_login_so(self):
2626
with self.token.open(rw=True, so_pin=TOKEN_SO_PIN) as session:
2727
self.assertIsInstance(session, pkcs11.Session)
2828

29+
@Only.softhsm2 # We don't have credentials to do this for other platforms
30+
def test_init_pin(self):
31+
temp_token_pin = "bearsbeetsbattlestargalactica"
32+
33+
with self.token.open(rw=True, so_pin=TOKEN_SO_PIN) as session:
34+
self.assertTrue(session.init_pin(temp_token_pin))
35+
36+
with self.token.open(user_pin=temp_token_pin) as session:
37+
self.assertIsInstance(session, pkcs11.Session)
38+
39+
with self.token.open(rw=True, so_pin=TOKEN_SO_PIN) as session:
40+
self.assertTrue(session.init_pin(TOKEN_PIN))
41+
2942
@requires(pkcs11.Mechanism.AES_KEY_GEN)
3043
def test_generate_key(self):
3144
with self.token.open(user_pin=TOKEN_PIN) as session:

tests/test_slots_and_tokens.py

+20
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,23 @@ def test_get_token(self):
7272
self.assertEqual(token.label, TOKEN)
7373
self.assertIn(pkcs11.TokenFlag.TOKEN_INITIALIZED, token.flags)
7474
self.assertIn(pkcs11.TokenFlag.LOGIN_REQUIRED, token.flags)
75+
76+
@Only.softhsm2
77+
def test_init_token(self):
78+
lib = pkcs11.lib(LIB)
79+
tokens = lib.get_tokens()
80+
temp_token_pin = "bearsbeetsbattlestargalactica"
81+
temp_token_label = "schrute"
82+
83+
for token in tokens:
84+
if pkcs11.TokenFlag.TOKEN_INITIALIZED not in token.flags:
85+
self.assertTrue(token.init_token(temp_token_label,
86+
temp_token_pin))
87+
break
88+
else:
89+
raise AssertionError("No Uninitialized token found")
90+
91+
token, *_ = lib.get_tokens(token_label=temp_token_label)
92+
93+
self.assertIn(pkcs11.TokenFlag.TOKEN_INITIALIZED, token.flags)
94+
self.assertNotIn(pkcs11.TokenFlag.USER_PIN_INITIALIZED, token.flags)

0 commit comments

Comments
 (0)