Skip to content

Commit 69d938a

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 69d938a

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-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

+21
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ 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+
<<<<<<< Updated upstream
30+
31+
=======
32+
>>>>>>> Stashed changes
33+
@Only.softhsm2 # We don't have credentials to do this for other platforms
34+
def test_init_pin(self):
35+
temp_token_pin = "bearsbeetsbattlestargalactica"
36+
37+
with self.token.open(rw=True, so_pin=TOKEN_SO_PIN) as session:
38+
self.assertTrue(session.init_pin(temp_token_pin))
39+
40+
with self.token.open(user_pin=temp_token_pin) as session:
41+
self.assertIsInstance(session, pkcs11.Session)
42+
43+
with self.token.open(rw=True, so_pin=TOKEN_SO_PIN) as session:
44+
self.assertTrue(session.init_pin(TOKEN_PIN))
45+
46+
<<<<<<< Updated upstream
47+
48+
=======
49+
>>>>>>> Stashed changes
2950
@requires(pkcs11.Mechanism.AES_KEY_GEN)
3051
def test_generate_key(self):
3152
with self.token.open(user_pin=TOKEN_PIN) as session:

tests/test_slots_and_tokens.py

+24
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,27 @@ 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+
<<<<<<< Updated upstream
86+
self.assertTrue(token.init_token(temp_token_label, temp_token_pin))
87+
=======
88+
self.assertTrue(token.init_token(temp_token_label,
89+
temp_token_pin))
90+
>>>>>>> Stashed changes
91+
break
92+
else:
93+
raise AssertionError("No Uninitialized token found")
94+
95+
token, *_ = lib.get_tokens(token_label=temp_token_label)
96+
97+
self.assertIn(pkcs11.TokenFlag.TOKEN_INITIALIZED, token.flags)
98+
self.assertNotIn(pkcs11.TokenFlag.USER_PIN_INITIALIZED, token.flags)

0 commit comments

Comments
 (0)