andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: /* andre@0: * this file maps PKCS11 Errors into SECErrors andre@0: * This is an information reducing process, since most errors are reflected andre@0: * back to the user (the user doesn't care about invalid flags, or active andre@0: * operations). If any of these errors need more detail in the upper layers andre@0: * which call PK11 library functions, we can add more SEC_ERROR_XXX functions andre@0: * and change there mappings here. andre@0: * andre@0: * Some PKCS11 errors are mapped to SEC_ERROR_LIBRARY_FAILURE intentionally andre@0: * because they indicate that there is a bug in the library (either NSS or andre@0: * the token). andre@0: */ andre@0: #include "pkcs11t.h" andre@0: #include "pk11func.h" andre@0: #include "secerr.h" andre@0: #include "prerror.h" andre@0: andre@0: #ifdef PK11_ERROR_USE_ARRAY andre@0: andre@0: /* andre@0: * build a static array of entries... andre@0: */ andre@0: static struct { andre@0: CK_RV pk11_error; andre@0: int sec_error; andre@0: } pk11_error_map = { andre@0: #define MAPERROR(x,y) {x, y}, andre@0: andre@0: #else andre@0: andre@0: /* the default is to use a big switch statement */ andre@0: int andre@0: PK11_MapError(CK_RV rv) { andre@0: andre@0: switch (rv) { andre@0: #define MAPERROR(x,y) case x: return y; andre@0: andre@0: #endif andre@0: andre@0: /* the guts mapping */ andre@0: MAPERROR(CKR_OK, 0) andre@0: MAPERROR(CKR_CANCEL, SEC_ERROR_IO) andre@0: MAPERROR(CKR_HOST_MEMORY, SEC_ERROR_NO_MEMORY) andre@0: MAPERROR(CKR_SLOT_ID_INVALID, SEC_ERROR_BAD_DATA) andre@0: MAPERROR(CKR_ARGUMENTS_BAD, SEC_ERROR_INVALID_ARGS) andre@0: MAPERROR(CKR_ATTRIBUTE_READ_ONLY, SEC_ERROR_READ_ONLY) andre@0: MAPERROR(CKR_ATTRIBUTE_SENSITIVE, SEC_ERROR_IO) /* XX SENSITIVE */ andre@0: MAPERROR(CKR_ATTRIBUTE_TYPE_INVALID, SEC_ERROR_BAD_DATA) andre@0: MAPERROR(CKR_ATTRIBUTE_VALUE_INVALID, SEC_ERROR_BAD_DATA) andre@0: MAPERROR(CKR_BUFFER_TOO_SMALL, SEC_ERROR_OUTPUT_LEN) andre@0: MAPERROR(CKR_DATA_INVALID, SEC_ERROR_BAD_DATA) andre@0: MAPERROR(CKR_DATA_LEN_RANGE, SEC_ERROR_INPUT_LEN) andre@0: MAPERROR(CKR_DEVICE_ERROR, SEC_ERROR_PKCS11_DEVICE_ERROR) andre@0: MAPERROR(CKR_DEVICE_MEMORY, SEC_ERROR_NO_MEMORY) andre@0: MAPERROR(CKR_DEVICE_REMOVED, SEC_ERROR_NO_TOKEN) andre@0: MAPERROR(CKR_DOMAIN_PARAMS_INVALID, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_ENCRYPTED_DATA_INVALID, SEC_ERROR_BAD_DATA) andre@0: MAPERROR(CKR_ENCRYPTED_DATA_LEN_RANGE, SEC_ERROR_BAD_DATA) andre@0: MAPERROR(CKR_FUNCTION_CANCELED, SEC_ERROR_LIBRARY_FAILURE) andre@0: MAPERROR(CKR_FUNCTION_FAILED, SEC_ERROR_PKCS11_FUNCTION_FAILED) andre@0: MAPERROR(CKR_FUNCTION_NOT_PARALLEL, SEC_ERROR_LIBRARY_FAILURE) andre@0: MAPERROR(CKR_FUNCTION_NOT_SUPPORTED, PR_NOT_IMPLEMENTED_ERROR) andre@0: MAPERROR(CKR_GENERAL_ERROR, SEC_ERROR_PKCS11_GENERAL_ERROR) andre@0: MAPERROR(CKR_KEY_HANDLE_INVALID, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_KEY_SIZE_RANGE, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_KEY_TYPE_INCONSISTENT, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_MECHANISM_INVALID, SEC_ERROR_INVALID_ALGORITHM) andre@0: MAPERROR(CKR_MECHANISM_PARAM_INVALID, SEC_ERROR_BAD_DATA) andre@0: MAPERROR(CKR_NO_EVENT, SEC_ERROR_NO_EVENT) andre@0: MAPERROR(CKR_OBJECT_HANDLE_INVALID, SEC_ERROR_BAD_DATA) andre@0: MAPERROR(CKR_OPERATION_ACTIVE, SEC_ERROR_LIBRARY_FAILURE) andre@0: MAPERROR(CKR_OPERATION_NOT_INITIALIZED,SEC_ERROR_LIBRARY_FAILURE ) andre@0: MAPERROR(CKR_PIN_INCORRECT, SEC_ERROR_BAD_PASSWORD) andre@0: MAPERROR(CKR_PIN_INVALID, SEC_ERROR_INVALID_PASSWORD) andre@0: MAPERROR(CKR_PIN_LEN_RANGE, SEC_ERROR_INVALID_PASSWORD) andre@0: MAPERROR(CKR_PIN_EXPIRED, SEC_ERROR_EXPIRED_PASSWORD) andre@0: MAPERROR(CKR_PIN_LOCKED, SEC_ERROR_LOCKED_PASSWORD) andre@0: MAPERROR(CKR_SESSION_CLOSED, SEC_ERROR_LIBRARY_FAILURE) andre@0: MAPERROR(CKR_SESSION_COUNT, SEC_ERROR_NO_MEMORY) /* XXXX? */ andre@0: MAPERROR(CKR_SESSION_HANDLE_INVALID, SEC_ERROR_BAD_DATA) andre@0: MAPERROR(CKR_SESSION_PARALLEL_NOT_SUPPORTED, SEC_ERROR_LIBRARY_FAILURE) andre@0: MAPERROR(CKR_SESSION_READ_ONLY, SEC_ERROR_READ_ONLY) andre@0: MAPERROR(CKR_SIGNATURE_INVALID, SEC_ERROR_BAD_SIGNATURE) andre@0: MAPERROR(CKR_SIGNATURE_LEN_RANGE, SEC_ERROR_BAD_SIGNATURE) andre@0: MAPERROR(CKR_TEMPLATE_INCOMPLETE, SEC_ERROR_BAD_DATA) andre@0: MAPERROR(CKR_TEMPLATE_INCONSISTENT, SEC_ERROR_BAD_DATA) andre@0: MAPERROR(CKR_TOKEN_NOT_PRESENT, SEC_ERROR_NO_TOKEN) andre@0: MAPERROR(CKR_TOKEN_NOT_RECOGNIZED, SEC_ERROR_IO) andre@0: MAPERROR(CKR_TOKEN_WRITE_PROTECTED, SEC_ERROR_READ_ONLY) andre@0: MAPERROR(CKR_UNWRAPPING_KEY_HANDLE_INVALID, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_UNWRAPPING_KEY_SIZE_RANGE, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_USER_ALREADY_LOGGED_IN, 0) andre@0: MAPERROR(CKR_USER_NOT_LOGGED_IN, SEC_ERROR_TOKEN_NOT_LOGGED_IN) andre@0: MAPERROR(CKR_USER_PIN_NOT_INITIALIZED, SEC_ERROR_NO_TOKEN) andre@0: MAPERROR(CKR_USER_TYPE_INVALID, SEC_ERROR_LIBRARY_FAILURE) andre@0: MAPERROR(CKR_WRAPPED_KEY_INVALID, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_WRAPPED_KEY_LEN_RANGE, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_WRAPPING_KEY_HANDLE_INVALID, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_WRAPPING_KEY_SIZE_RANGE, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_WRAPPING_KEY_TYPE_INCONSISTENT, SEC_ERROR_INVALID_KEY) andre@0: MAPERROR(CKR_VENDOR_DEFINED, SEC_ERROR_LIBRARY_FAILURE) andre@0: MAPERROR(CKR_NETSCAPE_CERTDB_FAILED, SEC_ERROR_BAD_DATABASE) andre@0: MAPERROR(CKR_NETSCAPE_KEYDB_FAILED, SEC_ERROR_BAD_DATABASE) andre@0: MAPERROR(CKR_CANT_LOCK, SEC_ERROR_INCOMPATIBLE_PKCS11) andre@0: andre@0: #ifdef PK11_ERROR_USE_ARRAY andre@0: }; andre@0: andre@0: int andre@0: PK11_MapError(CK_RV rv) { andre@0: int size = sizeof(pk11_error_map)/sizeof(pk11_error_map[0]); andre@0: andre@0: for (i=0; i < size; i++) { andre@0: if (pk11_error_map[i].pk11_error == rv) { andre@0: return pk11_error_map[i].sec_error; andre@0: } andre@0: } andre@0: return SEC_ERROR_UNKNOWN_PKCS11_ERROR; andre@0: } andre@0: andre@0: andre@0: #else andre@0: andre@0: default: andre@0: break; andre@0: } andre@0: return SEC_ERROR_UNKNOWN_PKCS11_ERROR; andre@0: } andre@0: andre@0: andre@0: #endif