Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | /* |
michael@0 | 5 | * this file maps PKCS11 Errors into SECErrors |
michael@0 | 6 | * This is an information reducing process, since most errors are reflected |
michael@0 | 7 | * back to the user (the user doesn't care about invalid flags, or active |
michael@0 | 8 | * operations). If any of these errors need more detail in the upper layers |
michael@0 | 9 | * which call PK11 library functions, we can add more SEC_ERROR_XXX functions |
michael@0 | 10 | * and change there mappings here. |
michael@0 | 11 | * |
michael@0 | 12 | * Some PKCS11 errors are mapped to SEC_ERROR_LIBRARY_FAILURE intentionally |
michael@0 | 13 | * because they indicate that there is a bug in the library (either NSS or |
michael@0 | 14 | * the token). |
michael@0 | 15 | */ |
michael@0 | 16 | #include "pkcs11t.h" |
michael@0 | 17 | #include "pk11func.h" |
michael@0 | 18 | #include "secerr.h" |
michael@0 | 19 | #include "prerror.h" |
michael@0 | 20 | |
michael@0 | 21 | #ifdef PK11_ERROR_USE_ARRAY |
michael@0 | 22 | |
michael@0 | 23 | /* |
michael@0 | 24 | * build a static array of entries... |
michael@0 | 25 | */ |
michael@0 | 26 | static struct { |
michael@0 | 27 | CK_RV pk11_error; |
michael@0 | 28 | int sec_error; |
michael@0 | 29 | } pk11_error_map = { |
michael@0 | 30 | #define MAPERROR(x,y) {x, y}, |
michael@0 | 31 | |
michael@0 | 32 | #else |
michael@0 | 33 | |
michael@0 | 34 | /* the default is to use a big switch statement */ |
michael@0 | 35 | int |
michael@0 | 36 | PK11_MapError(CK_RV rv) { |
michael@0 | 37 | |
michael@0 | 38 | switch (rv) { |
michael@0 | 39 | #define MAPERROR(x,y) case x: return y; |
michael@0 | 40 | |
michael@0 | 41 | #endif |
michael@0 | 42 | |
michael@0 | 43 | /* the guts mapping */ |
michael@0 | 44 | MAPERROR(CKR_OK, 0) |
michael@0 | 45 | MAPERROR(CKR_CANCEL, SEC_ERROR_IO) |
michael@0 | 46 | MAPERROR(CKR_HOST_MEMORY, SEC_ERROR_NO_MEMORY) |
michael@0 | 47 | MAPERROR(CKR_SLOT_ID_INVALID, SEC_ERROR_BAD_DATA) |
michael@0 | 48 | MAPERROR(CKR_ARGUMENTS_BAD, SEC_ERROR_INVALID_ARGS) |
michael@0 | 49 | MAPERROR(CKR_ATTRIBUTE_READ_ONLY, SEC_ERROR_READ_ONLY) |
michael@0 | 50 | MAPERROR(CKR_ATTRIBUTE_SENSITIVE, SEC_ERROR_IO) /* XX SENSITIVE */ |
michael@0 | 51 | MAPERROR(CKR_ATTRIBUTE_TYPE_INVALID, SEC_ERROR_BAD_DATA) |
michael@0 | 52 | MAPERROR(CKR_ATTRIBUTE_VALUE_INVALID, SEC_ERROR_BAD_DATA) |
michael@0 | 53 | MAPERROR(CKR_BUFFER_TOO_SMALL, SEC_ERROR_OUTPUT_LEN) |
michael@0 | 54 | MAPERROR(CKR_DATA_INVALID, SEC_ERROR_BAD_DATA) |
michael@0 | 55 | MAPERROR(CKR_DATA_LEN_RANGE, SEC_ERROR_INPUT_LEN) |
michael@0 | 56 | MAPERROR(CKR_DEVICE_ERROR, SEC_ERROR_PKCS11_DEVICE_ERROR) |
michael@0 | 57 | MAPERROR(CKR_DEVICE_MEMORY, SEC_ERROR_NO_MEMORY) |
michael@0 | 58 | MAPERROR(CKR_DEVICE_REMOVED, SEC_ERROR_NO_TOKEN) |
michael@0 | 59 | MAPERROR(CKR_DOMAIN_PARAMS_INVALID, SEC_ERROR_INVALID_KEY) |
michael@0 | 60 | MAPERROR(CKR_ENCRYPTED_DATA_INVALID, SEC_ERROR_BAD_DATA) |
michael@0 | 61 | MAPERROR(CKR_ENCRYPTED_DATA_LEN_RANGE, SEC_ERROR_BAD_DATA) |
michael@0 | 62 | MAPERROR(CKR_FUNCTION_CANCELED, SEC_ERROR_LIBRARY_FAILURE) |
michael@0 | 63 | MAPERROR(CKR_FUNCTION_FAILED, SEC_ERROR_PKCS11_FUNCTION_FAILED) |
michael@0 | 64 | MAPERROR(CKR_FUNCTION_NOT_PARALLEL, SEC_ERROR_LIBRARY_FAILURE) |
michael@0 | 65 | MAPERROR(CKR_FUNCTION_NOT_SUPPORTED, PR_NOT_IMPLEMENTED_ERROR) |
michael@0 | 66 | MAPERROR(CKR_GENERAL_ERROR, SEC_ERROR_PKCS11_GENERAL_ERROR) |
michael@0 | 67 | MAPERROR(CKR_KEY_HANDLE_INVALID, SEC_ERROR_INVALID_KEY) |
michael@0 | 68 | MAPERROR(CKR_KEY_SIZE_RANGE, SEC_ERROR_INVALID_KEY) |
michael@0 | 69 | MAPERROR(CKR_KEY_TYPE_INCONSISTENT, SEC_ERROR_INVALID_KEY) |
michael@0 | 70 | MAPERROR(CKR_MECHANISM_INVALID, SEC_ERROR_INVALID_ALGORITHM) |
michael@0 | 71 | MAPERROR(CKR_MECHANISM_PARAM_INVALID, SEC_ERROR_BAD_DATA) |
michael@0 | 72 | MAPERROR(CKR_NO_EVENT, SEC_ERROR_NO_EVENT) |
michael@0 | 73 | MAPERROR(CKR_OBJECT_HANDLE_INVALID, SEC_ERROR_BAD_DATA) |
michael@0 | 74 | MAPERROR(CKR_OPERATION_ACTIVE, SEC_ERROR_LIBRARY_FAILURE) |
michael@0 | 75 | MAPERROR(CKR_OPERATION_NOT_INITIALIZED,SEC_ERROR_LIBRARY_FAILURE ) |
michael@0 | 76 | MAPERROR(CKR_PIN_INCORRECT, SEC_ERROR_BAD_PASSWORD) |
michael@0 | 77 | MAPERROR(CKR_PIN_INVALID, SEC_ERROR_INVALID_PASSWORD) |
michael@0 | 78 | MAPERROR(CKR_PIN_LEN_RANGE, SEC_ERROR_INVALID_PASSWORD) |
michael@0 | 79 | MAPERROR(CKR_PIN_EXPIRED, SEC_ERROR_EXPIRED_PASSWORD) |
michael@0 | 80 | MAPERROR(CKR_PIN_LOCKED, SEC_ERROR_LOCKED_PASSWORD) |
michael@0 | 81 | MAPERROR(CKR_SESSION_CLOSED, SEC_ERROR_LIBRARY_FAILURE) |
michael@0 | 82 | MAPERROR(CKR_SESSION_COUNT, SEC_ERROR_NO_MEMORY) /* XXXX? */ |
michael@0 | 83 | MAPERROR(CKR_SESSION_HANDLE_INVALID, SEC_ERROR_BAD_DATA) |
michael@0 | 84 | MAPERROR(CKR_SESSION_PARALLEL_NOT_SUPPORTED, SEC_ERROR_LIBRARY_FAILURE) |
michael@0 | 85 | MAPERROR(CKR_SESSION_READ_ONLY, SEC_ERROR_READ_ONLY) |
michael@0 | 86 | MAPERROR(CKR_SIGNATURE_INVALID, SEC_ERROR_BAD_SIGNATURE) |
michael@0 | 87 | MAPERROR(CKR_SIGNATURE_LEN_RANGE, SEC_ERROR_BAD_SIGNATURE) |
michael@0 | 88 | MAPERROR(CKR_TEMPLATE_INCOMPLETE, SEC_ERROR_BAD_DATA) |
michael@0 | 89 | MAPERROR(CKR_TEMPLATE_INCONSISTENT, SEC_ERROR_BAD_DATA) |
michael@0 | 90 | MAPERROR(CKR_TOKEN_NOT_PRESENT, SEC_ERROR_NO_TOKEN) |
michael@0 | 91 | MAPERROR(CKR_TOKEN_NOT_RECOGNIZED, SEC_ERROR_IO) |
michael@0 | 92 | MAPERROR(CKR_TOKEN_WRITE_PROTECTED, SEC_ERROR_READ_ONLY) |
michael@0 | 93 | MAPERROR(CKR_UNWRAPPING_KEY_HANDLE_INVALID, SEC_ERROR_INVALID_KEY) |
michael@0 | 94 | MAPERROR(CKR_UNWRAPPING_KEY_SIZE_RANGE, SEC_ERROR_INVALID_KEY) |
michael@0 | 95 | MAPERROR(CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT, SEC_ERROR_INVALID_KEY) |
michael@0 | 96 | MAPERROR(CKR_USER_ALREADY_LOGGED_IN, 0) |
michael@0 | 97 | MAPERROR(CKR_USER_NOT_LOGGED_IN, SEC_ERROR_TOKEN_NOT_LOGGED_IN) |
michael@0 | 98 | MAPERROR(CKR_USER_PIN_NOT_INITIALIZED, SEC_ERROR_NO_TOKEN) |
michael@0 | 99 | MAPERROR(CKR_USER_TYPE_INVALID, SEC_ERROR_LIBRARY_FAILURE) |
michael@0 | 100 | MAPERROR(CKR_WRAPPED_KEY_INVALID, SEC_ERROR_INVALID_KEY) |
michael@0 | 101 | MAPERROR(CKR_WRAPPED_KEY_LEN_RANGE, SEC_ERROR_INVALID_KEY) |
michael@0 | 102 | MAPERROR(CKR_WRAPPING_KEY_HANDLE_INVALID, SEC_ERROR_INVALID_KEY) |
michael@0 | 103 | MAPERROR(CKR_WRAPPING_KEY_SIZE_RANGE, SEC_ERROR_INVALID_KEY) |
michael@0 | 104 | MAPERROR(CKR_WRAPPING_KEY_TYPE_INCONSISTENT, SEC_ERROR_INVALID_KEY) |
michael@0 | 105 | MAPERROR(CKR_VENDOR_DEFINED, SEC_ERROR_LIBRARY_FAILURE) |
michael@0 | 106 | MAPERROR(CKR_NETSCAPE_CERTDB_FAILED, SEC_ERROR_BAD_DATABASE) |
michael@0 | 107 | MAPERROR(CKR_NETSCAPE_KEYDB_FAILED, SEC_ERROR_BAD_DATABASE) |
michael@0 | 108 | MAPERROR(CKR_CANT_LOCK, SEC_ERROR_INCOMPATIBLE_PKCS11) |
michael@0 | 109 | |
michael@0 | 110 | #ifdef PK11_ERROR_USE_ARRAY |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | int |
michael@0 | 114 | PK11_MapError(CK_RV rv) { |
michael@0 | 115 | int size = sizeof(pk11_error_map)/sizeof(pk11_error_map[0]); |
michael@0 | 116 | |
michael@0 | 117 | for (i=0; i < size; i++) { |
michael@0 | 118 | if (pk11_error_map[i].pk11_error == rv) { |
michael@0 | 119 | return pk11_error_map[i].sec_error; |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | return SEC_ERROR_UNKNOWN_PKCS11_ERROR; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | |
michael@0 | 126 | #else |
michael@0 | 127 | |
michael@0 | 128 | default: |
michael@0 | 129 | break; |
michael@0 | 130 | } |
michael@0 | 131 | return SEC_ERROR_UNKNOWN_PKCS11_ERROR; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | |
michael@0 | 135 | #endif |