From baaec6fefd1b6dd8b4d27333624fb57b3d864e3d Mon Sep 17 00:00:00 2001 From: Afchine Madjlessi Date: Sat, 14 Nov 2026 16:14:51 +0100 Subject: [PATCH] Map the remaining PKCS#11 return codes to exceptions 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. --- pkcs11/_pkcs11.pxd | 66 +++++++++++++++++++++++ pkcs11/exceptions.py | 123 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) diff --git a/pkcs11/_pkcs11.pxd b/pkcs11/_pkcs11.pxd index cfa53801..25479b5d 100644 --- a/pkcs11/_pkcs11.pxd +++ b/pkcs11/_pkcs11.pxd @@ -141,6 +141,18 @@ cdef extern from '../extern/cryptoki.h': CKR_OPERATION_CANCEL_FAILED, + CKR_ACTION_PROHIBITED, + CKR_AEAD_DECRYPT_FAILED, + CKR_CURVE_NOT_SUPPORTED, + CKR_TOKEN_RESOURCE_EXCEEDED, + CKR_KEY_EXHAUSTED, + CKR_PENDING, + CKR_SESSION_ASYNC_NOT_SUPPORTED, + CKR_SEED_RANDOM_REQUIRED, + CKR_OPERATION_NOT_VALIDATED, + CKR_TOKEN_NOT_INITIALIZED, + CKR_PARAMETER_SET_NOT_SUPPORTED, + CKR_VENDOR_DEFINED, @@ -959,6 +971,60 @@ cdef inline object map_rv_to_error(CK_RV rv): # pragma: nocover exc = WrappingKeySizeRange() elif rv == CKR_WRAPPING_KEY_TYPE_INCONSISTENT: exc = WrappingKeyTypeInconsistent() + elif rv == CKR_CANCEL: + exc = Cancel() + elif rv == CKR_NEED_TO_CREATE_THREADS: + exc = NeedToCreateThreads() + elif rv == CKR_CANT_LOCK: + exc = CantLock() + elif rv == CKR_ACTION_PROHIBITED: + exc = ActionProhibited() + elif rv == CKR_AEAD_DECRYPT_FAILED: + exc = AEADDecryptFailed() + elif rv == CKR_FUNCTION_NOT_PARALLEL: + exc = FunctionNotParallel() + elif rv == CKR_KEY_CHANGED: + exc = KeyChanged() + elif rv == CKR_KEY_FUNCTION_NOT_PERMITTED: + exc = KeyFunctionNotPermitted() + elif rv == CKR_CURVE_NOT_SUPPORTED: + exc = CurveNotSupported() + elif rv == CKR_SAVED_STATE_INVALID: + exc = SavedStateInvalid() + elif rv == CKR_INFORMATION_SENSITIVE: + exc = InformationSensitive() + elif rv == CKR_STATE_UNSAVEABLE: + exc = StateUnsaveable() + elif rv == CKR_MUTEX_BAD: + exc = MutexBad() + elif rv == CKR_MUTEX_NOT_LOCKED: + exc = MutexNotLocked() + elif rv == CKR_NEW_PIN_MODE: + exc = NewPinMode() + elif rv == CKR_NEXT_OTP: + exc = NextOTP() + elif rv == CKR_FIPS_SELF_TEST_FAILED: + exc = FIPSSelfTestFailed() + elif rv == CKR_LIBRARY_LOAD_FAILED: + exc = LibraryLoadFailed() + elif rv == CKR_TOKEN_RESOURCE_EXCEEDED: + exc = TokenResourceExceeded() + elif rv == CKR_OPERATION_CANCEL_FAILED: + exc = OperationCancelFailed() + elif rv == CKR_KEY_EXHAUSTED: + exc = KeyExhausted() + elif rv == CKR_PENDING: + exc = Pending() + elif rv == CKR_SESSION_ASYNC_NOT_SUPPORTED: + exc = SessionAsyncNotSupported() + elif rv == CKR_SEED_RANDOM_REQUIRED: + exc = SeedRandomRequired() + elif rv == CKR_OPERATION_NOT_VALIDATED: + exc = OperationNotValidated() + elif rv == CKR_TOKEN_NOT_INITIALIZED: + exc = TokenNotInitialized() + elif rv == CKR_PARAMETER_SET_NOT_SUPPORTED: + exc = ParameterSetNotSupported() else: exc = PKCS11Error("Unmapped error code %s" % hex(rv)) return exc diff --git a/pkcs11/exceptions.py b/pkcs11/exceptions.py index 824d9709..60602ecf 100644 --- a/pkcs11/exceptions.py +++ b/pkcs11/exceptions.py @@ -408,3 +408,126 @@ class WrappingKeySizeRange(PKCS11Error): class WrappingKeyTypeInconsistent(PKCS11Error): pass + + +class Cancel(PKCS11Error): + pass + + +class NeedToCreateThreads(PKCS11Error): + pass + + +class CantLock(PKCS11Error): + pass + + +class ActionProhibited(PKCS11Error): + """ + The action may not be taken, for example copying an object whose + ``CKA_COPYABLE`` is false. + """ + + +class AEADDecryptFailed(PKCS11Error): + """ + Authenticated decryption failed: the tag did not verify. + """ + + +class FunctionNotParallel(PKCS11Error): + pass + + +class KeyChanged(PKCS11Error): + pass + + +class KeyFunctionNotPermitted(PKCS11Error): + """ + The key's attributes do not allow this use, for example deriving with a + key whose ``CKA_DERIVE`` is false. + """ + + +class CurveNotSupported(PKCS11Error): + """ + The token does not support the elliptic curve named in the parameters. + """ + + +class SavedStateInvalid(PKCS11Error): + pass + + +class InformationSensitive(PKCS11Error): + pass + + +class StateUnsaveable(PKCS11Error): + pass + + +class MutexBad(PKCS11Error): + pass + + +class MutexNotLocked(PKCS11Error): + pass + + +class NewPinMode(PKCS11Error): + pass + + +class NextOTP(PKCS11Error): + pass + + +class FIPSSelfTestFailed(PKCS11Error): + pass + + +class LibraryLoadFailed(PKCS11Error): + pass + + +class TokenResourceExceeded(PKCS11Error): + pass + + +class OperationCancelFailed(PKCS11Error): + pass + + +class KeyExhausted(PKCS11Error): + """ + The key has been used as many times as it may be. + """ + + +class Pending(PKCS11Error): + pass + + +class SessionAsyncNotSupported(PKCS11Error): + pass + + +class SeedRandomRequired(PKCS11Error): + pass + + +class OperationNotValidated(PKCS11Error): + pass + + +class TokenNotInitialized(PKCS11Error): + pass + + +class ParameterSetNotSupported(PKCS11Error): + """ + The token does not support the parameter set named in the template, + for example an ML-DSA, ML-KEM or SLH-DSA set. + """