Skip to content

Commit

Permalink
Added log info
Browse files Browse the repository at this point in the history
  • Loading branch information
msetina committed May 7, 2024
1 parent 8d79f13 commit b60611b
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 10 deletions.
22 changes: 14 additions & 8 deletions pkcs11_cryptography_keys/card_token/PKCS11_token_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ def create_key_pair(
self._session.createObject(
definition.get_template(KeyObjectTypes.private)
)
self._logger.info("Private key created")
self._session.createObject(
definition.get_template(KeyObjectTypes.public)
)
self._logger.info("Public key created")
private_objects = self._session.findObjects(
[
(PyKCS11.CKA_CLASS, PyKCS11.CKO_PRIVATE_KEY),
Expand All @@ -100,14 +102,18 @@ def create_key_pair(
definition.get_template(KeyObjectTypes.private),
mecha=definition.get_generation_mechanism(),
)
key_module = definition.get_module_name()
module = import_module(key_module)
if module != None:
ret = module.get_key(self._session, self._keyid, priv_key)
else:
raise Exception(
"Could not find module for {0}".format(key_module)
)
if priv_key is not None:
self._logger.info("Keypair generated")
key_module = definition.get_module_name()
module = import_module(key_module)
if module != None:
ret = module.get_key(
self._session, self._keyid, priv_key
)
else:
raise Exception(
"Could not find module for {0}".format(key_module)
)
else:
self._logger.info("Keypair definition missing")
else:
Expand Down
10 changes: 9 additions & 1 deletion pkcs11_cryptography_keys/sessions/PKCS11_admin_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ def _get_private_key_info(self, key_label: str | None = None) -> tuple:
[CKA_ID, CKA_LABEL],
)
keyid = bytes(attrs[0])
label = attrs[1]
label = attrs[1].strip().strip("\x00")
return keyid, label
else:
self._logger.info("Private key not found")
else:
self._logger.info("PKCS11 session not present")
return None, None

# Open session with the card
Expand Down Expand Up @@ -128,6 +132,10 @@ def open(self) -> PKCS11TokenAdmin | None:
self._key_label,
self._logger,
)
else:
self._logger.info("PKCS11 session could not be opened")
else:
self._logger.info("Slot could not be found")
return None

# context manager API
Expand Down
11 changes: 10 additions & 1 deletion pkcs11_cryptography_keys/sessions/PKCS11_key_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def _get_private_key(self, key_label: str | None = None) -> tuple:
key_type = attrs[0]
keyid = bytes(attrs[1])
return keyid, key_type, private_key
else:
self._logger.info("PKCS11 session is ot present")
return None, None, None

# Open session with the card
Expand Down Expand Up @@ -99,6 +101,10 @@ def open(
module_name = _key_modules.get(key_type, None)
if module_name is not None:
module = import_module(module_name)
else:
self._logger.info(
"Module for key type {0} is not setup".format(key_type)
)
if module is not None:
private_key = module.get_key(
self._session,
Expand All @@ -112,7 +118,10 @@ def open(
if mi.flags & mf != 0:
op = mi.flags_dict[mf].replace("CKF_", "")
private_key.fill_operations(m, op)

else:
self._logger.info("PKCS11 session could not be opened")
else:
self._logger.info("Slot could not be found")
return private_key

# context manager API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def open(self) -> PKCS11SlotAdmin | None:
else:
self._session.login(self._pin, CKU_SO)
return PKCS11SlotAdmin(self._session)
else:
self._logger.info("PKCS11 sessin could not be opened")
else:
self._logger.info("Slot could not be found")
return None

# context manager API
Expand Down
4 changes: 4 additions & 0 deletions pkcs11_cryptography_keys/sessions/PKCS11_slot_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def open(self) -> PKCS11Slot | None:
if self._login_required:
self._session.login(self._pin)
return PKCS11Slot(self._session)
else:
self._logger.info("PKCS11 sessin could not be opened")
else:
self._logger.info("Slot could not be found")

return None

Expand Down
6 changes: 6 additions & 0 deletions pkcs11_cryptography_keys/sessions/PKCS11_uri_admin_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def _get_private_key_info(self, key_label: str | None = None) -> tuple:
keyid = bytes(attrs[0])
label = attrs[1]
return keyid, label
else:
self._logger.info("Private key could not be found")
else:
self._logger.info("PKCS11 session is not present")
return None, None

# Open session with the card
Expand All @@ -88,6 +92,8 @@ def open(self) -> PKCS11TokenAdmin | None:
return PKCS11TokenAdmin(
self._session, keyid, label, self._logger
)
else:
self._logger.info("PKCS11 session is not present")
return None

# context manager API
Expand Down
6 changes: 6 additions & 0 deletions pkcs11_cryptography_keys/sessions/PKCS11_uri_key_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def open(
module_name = _key_modules.get(key_type, None)
if module_name is not None:
module = import_module(module_name)
else:
self._logger.info(
"Module for key type {0} is not setup".format(key_type)
)
if module is not None:
private_key = module.get_key(
self._session,
Expand All @@ -90,6 +94,8 @@ def open(
)
for m, op in pkcs11_uri.gen_operations():
private_key.fill_operations(m, op)
else:
self._logger.info("PKCS11 session is not present")
return private_key

# context manager API
Expand Down

0 comments on commit b60611b

Please sign in to comment.