1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 
   5 /* Copyright  (c) 2002 Graz University of Technology. All rights reserved.
   6  *
   7  * Redistribution and use in  source and binary forms, with or without
   8  * modification, are permitted  provided that the following conditions are met:
   9  *
  10  * 1. Redistributions of  source code must retain the above copyright notice,
  11  *    this list of conditions and the following disclaimer.
  12  *
  13  * 2. Redistributions in  binary form must reproduce the above copyright notice,
  14  *    this list of conditions and the following disclaimer in the documentation
  15  *    and/or other materials provided with the distribution.
  16  *
  17  * 3. The end-user documentation included with the redistribution, if any, must
  18  *    include the following acknowledgment:
  19  *
  20  *    "This product includes software developed by IAIK of Graz University of
  21  *     Technology."
  22  *
  23  *    Alternately, this acknowledgment may appear in the software itself, if
  24  *    and wherever such third-party acknowledgments normally appear.
  25  *
  26  * 4. The names "Graz University of Technology" and "IAIK of Graz University of
  27  *    Technology" must not be used to endorse or promote products derived from
  28  *    this software without prior written permission.
  29  *
  30  * 5. Products derived from this software may not be called
  31  *    "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
  32  *    written permission of Graz University of Technology.
  33  *
  34  *  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
  35  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  36  *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  37  *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
  38  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  39  *  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  40  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  41  *  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  42  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  43  *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  45  *  POSSIBILITY  OF SUCH DAMAGE.
  46  */
  47 
  48 package sun.security.pkcs11.wrapper;
  49 
  50 import java.util.*;
  51 
  52 
  53 /**
  54  * This is the superclass of all checked exceptions used by this package. An
  55  * exception of this class indicates that a function call to the underlying
  56  * PKCS#11 module returned a value not equal to CKR_OK. The application can get
  57  * the returned value by calling getErrorCode(). A return value not equal to
  58  * CKR_OK is the only reason for such an exception to be thrown.
  59  * PKCS#11 defines the meaning of an error-code, which may depend on the
  60  * context in which the error occurs.
  61  *
  62  * @author <a href="mailto:Karl.Scheibelhofer@iaik.at"> Karl Scheibelhofer </a>
  63  * @invariants
  64  */
  65 public class PKCS11Exception extends Exception {
  66 
  67     /**
  68      * The code of the error which was the reason for this exception.
  69      */
  70     protected long errorCode_;
  71 
  72     private static final Map<Long,String> errorMap;
  73 
  74     static {
  75         int[] errorCodes = new int[] {
  76             0x00000000,
  77             0x00000001,
  78             0x00000002,
  79             0x00000003,
  80             0x00000005,
  81             0x00000006,
  82             0x00000007,
  83             0x00000008,
  84             0x00000009,
  85             0x0000000A,
  86             0x00000010,
  87             0x00000011,
  88             0x00000012,
  89             0x00000013,
  90             0x00000020,
  91             0x00000021,
  92             0x00000030,
  93             0x00000031,
  94             0x00000032,
  95             0x00000040,
  96             0x00000041,
  97             0x00000050,
  98             0x00000051,
  99             0x00000054,
 100             0x00000060,
 101             0x00000062,
 102             0x00000063,
 103             0x00000064,
 104             0x00000065,
 105             0x00000066,
 106             0x00000067,
 107             0x00000068,
 108             0x00000069,
 109             0x0000006A,
 110             0x00000070,
 111             0x00000071,
 112             0x00000082,
 113             0x00000090,
 114             0x00000091,
 115             0x000000A0,
 116             0x000000A1,
 117             0x000000A2,
 118             0x000000A3,
 119             0x000000A4,
 120             0x000000B0,
 121             0x000000B1,
 122             0x000000B3,
 123             0x000000B4,
 124             0x000000B5,
 125             0x000000B6,
 126             0x000000B7,
 127             0x000000B8,
 128             0x000000C0,
 129             0x000000C1,
 130             0x000000D0,
 131             0x000000D1,
 132             0x000000E0,
 133             0x000000E1,
 134             0x000000E2,
 135             0x000000F0,
 136             0x000000F1,
 137             0x000000F2,
 138             0x00000100,
 139             0x00000101,
 140             0x00000102,
 141             0x00000103,
 142             0x00000104,
 143             0x00000105,
 144             0x00000110,
 145             0x00000112,
 146             0x00000113,
 147             0x00000114,
 148             0x00000115,
 149             0x00000120,
 150             0x00000121,
 151             0x00000130,
 152             0x00000150,
 153             0x00000160,
 154             0x00000170,
 155             0x00000180,
 156             0x00000190,
 157             0x00000191,
 158             0x000001A0,
 159             0x000001A1,
 160             0x00000200,
 161             0x80000000,
 162         };
 163         String[] errorMessages = new String[] {
 164             "CKR_OK",
 165             "CKR_CANCEL",
 166             "CKR_HOST_MEMORY",
 167             "CKR_SLOT_ID_INVALID",
 168             "CKR_GENERAL_ERROR",
 169             "CKR_FUNCTION_FAILED",
 170             "CKR_ARGUMENTS_BAD",
 171             "CKR_NO_EVENT",
 172             "CKR_NEED_TO_CREATE_THREADS",
 173             "CKR_CANT_LOCK",
 174             "CKR_ATTRIBUTE_READ_ONLY",
 175             "CKR_ATTRIBUTE_SENSITIVE",
 176             "CKR_ATTRIBUTE_TYPE_INVALID",
 177             "CKR_ATTRIBUTE_VALUE_INVALID",
 178             "CKR_DATA_INVALID",
 179             "CKR_DATA_LEN_RANGE",
 180             "CKR_DEVICE_ERROR",
 181             "CKR_DEVICE_MEMORY",
 182             "CKR_DEVICE_REMOVED",
 183             "CKR_ENCRYPTED_DATA_INVALID",
 184             "CKR_ENCRYPTED_DATA_LEN_RANGE",
 185             "CKR_FUNCTION_CANCELED",
 186             "CKR_FUNCTION_NOT_PARALLEL",
 187             "CKR_FUNCTION_NOT_SUPPORTED",
 188             "CKR_KEY_HANDLE_INVALID",
 189             "CKR_KEY_SIZE_RANGE",
 190             "CKR_KEY_TYPE_INCONSISTENT",
 191             "CKR_KEY_NOT_NEEDED",
 192             "CKR_KEY_CHANGED",
 193             "CKR_KEY_NEEDED",
 194             "CKR_KEY_INDIGESTIBLE",
 195             "CKR_KEY_FUNCTION_NOT_PERMITTED",
 196             "CKR_KEY_NOT_WRAPPABLE",
 197             "CKR_KEY_UNEXTRACTABLE",
 198             "CKR_MECHANISM_INVALID",
 199             "CKR_MECHANISM_PARAM_INVALID",
 200             "CKR_OBJECT_HANDLE_INVALID",
 201             "CKR_OPERATION_ACTIVE",
 202             "CKR_OPERATION_NOT_INITIALIZED",
 203             "CKR_PIN_INCORRECT",
 204             "CKR_PIN_INVALID",
 205             "CKR_PIN_LEN_RANGE",
 206             "CKR_PIN_EXPIRED",
 207             "CKR_PIN_LOCKED",
 208             "CKR_SESSION_CLOSED",
 209             "CKR_SESSION_COUNT",
 210             "CKR_SESSION_HANDLE_INVALID",
 211             "CKR_SESSION_PARALLEL_NOT_SUPPORTED",
 212             "CKR_SESSION_READ_ONLY",
 213             "CKR_SESSION_EXISTS",
 214             "CKR_SESSION_READ_ONLY_EXISTS",
 215             "CKR_SESSION_READ_WRITE_SO_EXISTS",
 216             "CKR_SIGNATURE_INVALID",
 217             "CKR_SIGNATURE_LEN_RANGE",
 218             "CKR_TEMPLATE_INCOMPLETE",
 219             "CKR_TEMPLATE_INCONSISTENT",
 220             "CKR_TOKEN_NOT_PRESENT",
 221             "CKR_TOKEN_NOT_RECOGNIZED",
 222             "CKR_TOKEN_WRITE_PROTECTED",
 223             "CKR_UNWRAPPING_KEY_HANDLE_INVALID",
 224             "CKR_UNWRAPPING_KEY_SIZE_RANGE",
 225             "CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT",
 226             "CKR_USER_ALREADY_LOGGED_IN",
 227             "CKR_USER_NOT_LOGGED_IN",
 228             "CKR_USER_PIN_NOT_INITIALIZED",
 229             "CKR_USER_TYPE_INVALID",
 230             "CKR_USER_ANOTHER_ALREADY_LOGGED_IN",
 231             "CKR_USER_TOO_MANY_TYPES",
 232             "CKR_WRAPPED_KEY_INVALID",
 233             "CKR_WRAPPED_KEY_LEN_RANGE",
 234             "CKR_WRAPPING_KEY_HANDLE_INVALID",
 235             "CKR_WRAPPING_KEY_SIZE_RANGE",
 236             "CKR_WRAPPING_KEY_TYPE_INCONSISTENT",
 237             "CKR_RANDOM_SEED_NOT_SUPPORTED",
 238             "CKR_RANDOM_NO_RNG",
 239             "CKR_DOMAIN_PARAMS_INVALID",
 240             "CKR_BUFFER_TOO_SMALL",
 241             "CKR_SAVED_STATE_INVALID",
 242             "CKR_INFORMATION_SENSITIVE",
 243             "CKR_STATE_UNSAVEABLE",
 244             "CKR_CRYPTOKI_NOT_INITIALIZED",
 245             "CKR_CRYPTOKI_ALREADY_INITIALIZED",
 246             "CKR_MUTEX_BAD",
 247             "CKR_MUTEX_NOT_LOCKED",
 248             "CKR_FUNCTION_REJECTED",
 249             "CKR_VENDOR_DEFINED",
 250         };
 251         errorMap = new HashMap<Long,String>();
 252         for (int i = 0; i < errorCodes.length; i++) {
 253             errorMap.put(Long.valueOf(errorCodes[i]), errorMessages[i]);
 254         }
 255     }
 256 
 257 
 258     /**
 259      * Constructor taking the error code as defined for the CKR_* constants
 260      * in PKCS#11.
 261      */
 262     public PKCS11Exception(long errorCode) {
 263         errorCode_ = errorCode;
 264     }
 265 
 266     /**
 267      * This method gets the corresponding text error message from
 268      * a property file. If this file is not available, it returns the error
 269      * code as a hex-string.
 270      *
 271      * @return The message or the error code; e.g. "CKR_DEVICE_ERROR" or
 272      *         "0x00000030".
 273      * @preconditions
 274      * @postconditions (result <> null)
 275      */
 276     public String getMessage() {
 277         String message = errorMap.get(Long.valueOf(errorCode_));
 278         if (message == null) {
 279             message = "0x" + Functions.toFullHexString((int)errorCode_);
 280         }
 281         return message;
 282     }
 283 
 284     /**
 285      * Returns the PKCS#11 error code.
 286      *
 287      * @return The error code; e.g. 0x00000030.
 288      * @preconditions
 289      * @postconditions
 290      */
 291     public long getErrorCode() {
 292         return errorCode_ ;
 293     }
 294 
 295 }