Back to index

lightning-sunbird  0.9+nobinonly
pk11err.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 /* 
00037  * this file maps PKCS11 Errors into SECErrors
00038  *  This is an information reducing process, since most errors are reflected
00039  *  back to the user (the user doesn't care about invalid flags, or active
00040  *  operations). If any of these errors need more detail in the upper layers
00041  *  which call PK11 library functions, we can add more SEC_ERROR_XXX functions
00042  *  and change there mappings here.
00043  */
00044 #include "pkcs11t.h"
00045 #include "pk11func.h"
00046 #include "secerr.h"
00047 
00048 #ifdef PK11_ERROR_USE_ARRAY 
00049 
00050 /*
00051  * build a static array of entries...
00052  */
00053 static struct {
00054        CK_RV pk11_error;
00055        int   sec_error;
00056 } pk11_error_map = {
00057 #define MAPERROR(x,y) {x, y},
00058 
00059 #else
00060 
00061 /* the default is to use a big switch statement */
00062 int
00063 PK11_MapError(CK_RV rv) {
00064 
00065        switch (rv) {
00066 #define MAPERROR(x,y) case x: return y;
00067 
00068 #endif
00069 
00070 /* the guts mapping */
00071        MAPERROR(CKR_OK, 0)
00072        MAPERROR(CKR_CANCEL, SEC_ERROR_IO)
00073        MAPERROR(CKR_HOST_MEMORY, SEC_ERROR_NO_MEMORY)
00074        MAPERROR(CKR_SLOT_ID_INVALID, SEC_ERROR_BAD_DATA)
00075        MAPERROR(CKR_ATTRIBUTE_READ_ONLY, SEC_ERROR_READ_ONLY)
00076        MAPERROR(CKR_ATTRIBUTE_SENSITIVE, SEC_ERROR_IO) /* XX SENSITIVE */
00077        MAPERROR(CKR_ATTRIBUTE_TYPE_INVALID, SEC_ERROR_BAD_DATA)
00078        MAPERROR(CKR_ATTRIBUTE_VALUE_INVALID, SEC_ERROR_BAD_DATA)
00079        MAPERROR(CKR_DATA_INVALID, SEC_ERROR_BAD_DATA)
00080        MAPERROR(CKR_DATA_LEN_RANGE, SEC_ERROR_BAD_DATA)
00081        MAPERROR(CKR_DEVICE_ERROR, SEC_ERROR_IO)
00082        MAPERROR(CKR_DEVICE_MEMORY, SEC_ERROR_NO_MEMORY)
00083        MAPERROR(CKR_DEVICE_REMOVED, SEC_ERROR_NO_TOKEN)
00084        MAPERROR(CKR_ENCRYPTED_DATA_INVALID, SEC_ERROR_BAD_DATA)
00085        MAPERROR(CKR_ENCRYPTED_DATA_LEN_RANGE, SEC_ERROR_BAD_DATA)
00086        MAPERROR(CKR_FUNCTION_CANCELED, SEC_ERROR_LIBRARY_FAILURE)
00087        MAPERROR(CKR_FUNCTION_NOT_PARALLEL, SEC_ERROR_LIBRARY_FAILURE)
00088        MAPERROR(CKR_KEY_HANDLE_INVALID, SEC_ERROR_INVALID_KEY)
00089        MAPERROR(CKR_KEY_SIZE_RANGE, SEC_ERROR_INVALID_KEY)
00090        MAPERROR(CKR_KEY_TYPE_INCONSISTENT, SEC_ERROR_INVALID_KEY)
00091        MAPERROR(CKR_MECHANISM_INVALID, SEC_ERROR_BAD_DATA)
00092        MAPERROR(CKR_MECHANISM_PARAM_INVALID, SEC_ERROR_BAD_DATA)
00093        MAPERROR(CKR_NO_EVENT, SEC_ERROR_NO_EVENT)
00094        MAPERROR(CKR_OBJECT_HANDLE_INVALID, SEC_ERROR_BAD_DATA)
00095        MAPERROR(CKR_OPERATION_ACTIVE, SEC_ERROR_LIBRARY_FAILURE)
00096        MAPERROR(CKR_OPERATION_NOT_INITIALIZED,SEC_ERROR_LIBRARY_FAILURE )
00097        MAPERROR(CKR_PIN_INCORRECT, SEC_ERROR_BAD_PASSWORD)
00098        MAPERROR(CKR_PIN_INVALID, SEC_ERROR_INVALID_PASSWORD)
00099        MAPERROR(CKR_PIN_LEN_RANGE, SEC_ERROR_INVALID_PASSWORD)
00100        MAPERROR(CKR_SESSION_CLOSED, SEC_ERROR_LIBRARY_FAILURE)
00101        MAPERROR(CKR_SESSION_COUNT, SEC_ERROR_NO_MEMORY) /* XXXX? */
00102        MAPERROR(CKR_SESSION_HANDLE_INVALID, SEC_ERROR_BAD_DATA)
00103        MAPERROR(CKR_SESSION_PARALLEL_NOT_SUPPORTED, SEC_ERROR_LIBRARY_FAILURE)
00104        MAPERROR(CKR_SESSION_READ_ONLY, SEC_ERROR_LIBRARY_FAILURE)
00105        MAPERROR(CKR_SIGNATURE_INVALID, SEC_ERROR_BAD_SIGNATURE)
00106        MAPERROR(CKR_SIGNATURE_LEN_RANGE, SEC_ERROR_BAD_SIGNATURE)
00107        MAPERROR(CKR_TEMPLATE_INCOMPLETE, SEC_ERROR_BAD_DATA)
00108        MAPERROR(CKR_TEMPLATE_INCONSISTENT, SEC_ERROR_BAD_DATA)
00109        MAPERROR(CKR_TOKEN_NOT_PRESENT, SEC_ERROR_NO_TOKEN)
00110        MAPERROR(CKR_TOKEN_NOT_RECOGNIZED, SEC_ERROR_IO)
00111        MAPERROR(CKR_TOKEN_WRITE_PROTECTED, SEC_ERROR_READ_ONLY)
00112        MAPERROR(CKR_UNWRAPPING_KEY_HANDLE_INVALID, SEC_ERROR_INVALID_KEY)
00113        MAPERROR(CKR_UNWRAPPING_KEY_SIZE_RANGE, SEC_ERROR_INVALID_KEY)
00114        MAPERROR(CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT, SEC_ERROR_INVALID_KEY)
00115        MAPERROR(CKR_USER_ALREADY_LOGGED_IN, 0)
00116        MAPERROR(CKR_USER_NOT_LOGGED_IN, SEC_ERROR_TOKEN_NOT_LOGGED_IN)
00117        MAPERROR(CKR_USER_PIN_NOT_INITIALIZED, SEC_ERROR_NO_TOKEN)
00118        MAPERROR(CKR_USER_TYPE_INVALID, SEC_ERROR_LIBRARY_FAILURE)
00119        MAPERROR(CKR_WRAPPED_KEY_INVALID, SEC_ERROR_INVALID_KEY)
00120        MAPERROR(CKR_WRAPPED_KEY_LEN_RANGE, SEC_ERROR_INVALID_KEY)
00121        MAPERROR(CKR_WRAPPING_KEY_HANDLE_INVALID, SEC_ERROR_INVALID_KEY)
00122        MAPERROR(CKR_WRAPPING_KEY_SIZE_RANGE, SEC_ERROR_INVALID_KEY)
00123        MAPERROR(CKR_WRAPPING_KEY_TYPE_INCONSISTENT, SEC_ERROR_INVALID_KEY)
00124        MAPERROR(CKR_VENDOR_DEFINED, SEC_ERROR_LIBRARY_FAILURE)
00125        MAPERROR(CKR_NETSCAPE_CERTDB_FAILED, SEC_ERROR_BAD_DATABASE)
00126        MAPERROR(CKR_NETSCAPE_KEYDB_FAILED, SEC_ERROR_BAD_DATABASE)
00127        MAPERROR(CKR_CANT_LOCK, SEC_ERROR_INCOMPATIBLE_PKCS11)
00128 
00129 #ifdef PK11_ERROR_USE_ARRAY 
00130 };
00131 
00132 int
00133 PK11_MapError(CK_RV rv) {
00134     int size = sizeof(pk11_error_map)/sizeof(pk11_error_map[0]);
00135 
00136     for (i=0; i < size; i++) {
00137        if (pk11_error_map[i].pk11_error == rv) {
00138            return pk11_error_map[i].sec_error;
00139        }
00140     }
00141     return SEC_ERROR_IO;
00142  }
00143 
00144 
00145 #else
00146 
00147     default:
00148        break;
00149     }
00150     return SEC_ERROR_IO;
00151 }
00152 
00153 
00154 #endif