Skip to content

Map the remaining PKCS#11 return codes to exceptions - #251

Open
afchine1337 wants to merge 1 commit into
pyauth:masterfrom
afchine1337:return-codes
Open

afchine1337 wants to merge 1 commit into
pyauth:masterfrom
afchine1337:return-codes

Conversation

@afchine1337

Copy link
Copy Markdown

27 return codes defined in extern/pkcs11t.h had no exception class. A token returning one of them surfaced as a bare PKCS11Error("Unmapped error code 0x..."), which callers can only tell apart by parsing the message.

Several of these codes come from PKCS#11 v3.0 and v3.2, and current tokens return them:

  • CKR_KEY_FUNCTION_NOT_PERMITTED: the key's attributes do not allow the operation;
  • CKR_CURVE_NOT_SUPPORTED;
  • CKR_ACTION_PROHIBITED;
  • CKR_AEAD_DECRYPT_FAILED;
  • CKR_PARAMETER_SET_NOT_SUPPORTED.

Changes

  • pkcs11/exceptions.py: one PKCS11Error subclass per code, named after the code like the existing classes (CurveNotSupported, KeyFunctionNotPermitted, AEADDecryptFailed, ParameterSetNotSupported, ...).
  • pkcs11/_pkcs11.pxd: the 11 codes missing from the CK_RV enum are declared, and map_rv_to_error gets a branch for each of the 27 codes.

Code that catches PKCS11Error keeps working, since every new class derives from it. Code that matched on the "Unmapped error code" message will see the specific class instead.

Testing

ruff format and ruff check are clean. I ran the test suite against FreeHSM (all-mechanisms build, permissive mode): 121 passed, 43 skipped, and 1 failed, test_seed_random. That token refuses external seeding by design, and the test fails the same way on master. No new tests were added, because none of the existing tests triggers these codes on purpose.

Related to #249.

27 return codes defined in extern/pkcs11t.h had no exception, so they
surfaced as a bare PKCS11Error("Unmapped error code 0x..."). Several
come from PKCS#11 v3.0 and v3.2 and are returned by current tokens:
CKR_KEY_FUNCTION_NOT_PERMITTED (a key used for something its
attributes do not allow), CKR_CURVE_NOT_SUPPORTED,
CKR_ACTION_PROHIBITED, CKR_AEAD_DECRYPT_FAILED and
CKR_PARAMETER_SET_NOT_SUPPORTED.

Each code gets a PKCS11Error subclass named after it, as the existing
ones are, and a branch in map_rv_to_error. The 11 codes not yet
declared in the CK_RV enum are added there. Code that catches
PKCS11Error is unaffected.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant