View Javadoc
1   /*
2    * Copyright (c) 2003, 2011, 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      private static final long serialVersionUID = 4877072363729195L;
67  
68      /**
69       * The code of the error which was the reason for this exception.
70       */
71      protected long errorCode_;
72  
73      private static final Map<Long,String> errorMap;
74  
75      static {
76          int[] errorCodes = new int[] {
77              0x00000000,
78              0x00000001,
79              0x00000002,
80              0x00000003,
81              0x00000005,
82              0x00000006,
83              0x00000007,
84              0x00000008,
85              0x00000009,
86              0x0000000A,
87              0x00000010,
88              0x00000011,
89              0x00000012,
90              0x00000013,
91              0x00000020,
92              0x00000021,
93              0x00000030,
94              0x00000031,
95              0x00000032,
96              0x00000040,
97              0x00000041,
98              0x00000050,
99              0x00000051,
100             0x00000054,
101             0x00000060,
102             0x00000062,
103             0x00000063,
104             0x00000064,
105             0x00000065,
106             0x00000066,
107             0x00000067,
108             0x00000068,
109             0x00000069,
110             0x0000006A,
111             0x00000070,
112             0x00000071,
113             0x00000082,
114             0x00000090,
115             0x00000091,
116             0x000000A0,
117             0x000000A1,
118             0x000000A2,
119             0x000000A3,
120             0x000000A4,
121             0x000000B0,
122             0x000000B1,
123             0x000000B3,
124             0x000000B4,
125             0x000000B5,
126             0x000000B6,
127             0x000000B7,
128             0x000000B8,
129             0x000000C0,
130             0x000000C1,
131             0x000000D0,
132             0x000000D1,
133             0x000000E0,
134             0x000000E1,
135             0x000000E2,
136             0x000000F0,
137             0x000000F1,
138             0x000000F2,
139             0x00000100,
140             0x00000101,
141             0x00000102,
142             0x00000103,
143             0x00000104,
144             0x00000105,
145             0x00000110,
146             0x00000112,
147             0x00000113,
148             0x00000114,
149             0x00000115,
150             0x00000120,
151             0x00000121,
152             0x00000130,
153             0x00000150,
154             0x00000160,
155             0x00000170,
156             0x00000180,
157             0x00000190,
158             0x00000191,
159             0x000001A0,
160             0x000001A1,
161             0x00000200,
162             0x80000000,
163         };
164         String[] errorMessages = new String[] {
165             "CKR_OK",
166             "CKR_CANCEL",
167             "CKR_HOST_MEMORY",
168             "CKR_SLOT_ID_INVALID",
169             "CKR_GENERAL_ERROR",
170             "CKR_FUNCTION_FAILED",
171             "CKR_ARGUMENTS_BAD",
172             "CKR_NO_EVENT",
173             "CKR_NEED_TO_CREATE_THREADS",
174             "CKR_CANT_LOCK",
175             "CKR_ATTRIBUTE_READ_ONLY",
176             "CKR_ATTRIBUTE_SENSITIVE",
177             "CKR_ATTRIBUTE_TYPE_INVALID",
178             "CKR_ATTRIBUTE_VALUE_INVALID",
179             "CKR_DATA_INVALID",
180             "CKR_DATA_LEN_RANGE",
181             "CKR_DEVICE_ERROR",
182             "CKR_DEVICE_MEMORY",
183             "CKR_DEVICE_REMOVED",
184             "CKR_ENCRYPTED_DATA_INVALID",
185             "CKR_ENCRYPTED_DATA_LEN_RANGE",
186             "CKR_FUNCTION_CANCELED",
187             "CKR_FUNCTION_NOT_PARALLEL",
188             "CKR_FUNCTION_NOT_SUPPORTED",
189             "CKR_KEY_HANDLE_INVALID",
190             "CKR_KEY_SIZE_RANGE",
191             "CKR_KEY_TYPE_INCONSISTENT",
192             "CKR_KEY_NOT_NEEDED",
193             "CKR_KEY_CHANGED",
194             "CKR_KEY_NEEDED",
195             "CKR_KEY_INDIGESTIBLE",
196             "CKR_KEY_FUNCTION_NOT_PERMITTED",
197             "CKR_KEY_NOT_WRAPPABLE",
198             "CKR_KEY_UNEXTRACTABLE",
199             "CKR_MECHANISM_INVALID",
200             "CKR_MECHANISM_PARAM_INVALID",
201             "CKR_OBJECT_HANDLE_INVALID",
202             "CKR_OPERATION_ACTIVE",
203             "CKR_OPERATION_NOT_INITIALIZED",
204             "CKR_PIN_INCORRECT",
205             "CKR_PIN_INVALID",
206             "CKR_PIN_LEN_RANGE",
207             "CKR_PIN_EXPIRED",
208             "CKR_PIN_LOCKED",
209             "CKR_SESSION_CLOSED",
210             "CKR_SESSION_COUNT",
211             "CKR_SESSION_HANDLE_INVALID",
212             "CKR_SESSION_PARALLEL_NOT_SUPPORTED",
213             "CKR_SESSION_READ_ONLY",
214             "CKR_SESSION_EXISTS",
215             "CKR_SESSION_READ_ONLY_EXISTS",
216             "CKR_SESSION_READ_WRITE_SO_EXISTS",
217             "CKR_SIGNATURE_INVALID",
218             "CKR_SIGNATURE_LEN_RANGE",
219             "CKR_TEMPLATE_INCOMPLETE",
220             "CKR_TEMPLATE_INCONSISTENT",
221             "CKR_TOKEN_NOT_PRESENT",
222             "CKR_TOKEN_NOT_RECOGNIZED",
223             "CKR_TOKEN_WRITE_PROTECTED",
224             "CKR_UNWRAPPING_KEY_HANDLE_INVALID",
225             "CKR_UNWRAPPING_KEY_SIZE_RANGE",
226             "CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT",
227             "CKR_USER_ALREADY_LOGGED_IN",
228             "CKR_USER_NOT_LOGGED_IN",
229             "CKR_USER_PIN_NOT_INITIALIZED",
230             "CKR_USER_TYPE_INVALID",
231             "CKR_USER_ANOTHER_ALREADY_LOGGED_IN",
232             "CKR_USER_TOO_MANY_TYPES",
233             "CKR_WRAPPED_KEY_INVALID",
234             "CKR_WRAPPED_KEY_LEN_RANGE",
235             "CKR_WRAPPING_KEY_HANDLE_INVALID",
236             "CKR_WRAPPING_KEY_SIZE_RANGE",
237             "CKR_WRAPPING_KEY_TYPE_INCONSISTENT",
238             "CKR_RANDOM_SEED_NOT_SUPPORTED",
239             "CKR_RANDOM_NO_RNG",
240             "CKR_DOMAIN_PARAMS_INVALID",
241             "CKR_BUFFER_TOO_SMALL",
242             "CKR_SAVED_STATE_INVALID",
243             "CKR_INFORMATION_SENSITIVE",
244             "CKR_STATE_UNSAVEABLE",
245             "CKR_CRYPTOKI_NOT_INITIALIZED",
246             "CKR_CRYPTOKI_ALREADY_INITIALIZED",
247             "CKR_MUTEX_BAD",
248             "CKR_MUTEX_NOT_LOCKED",
249             "CKR_FUNCTION_REJECTED",
250             "CKR_VENDOR_DEFINED",
251         };
252         errorMap = new HashMap<Long,String>();
253         for (int i = 0; i < errorCodes.length; i++) {
254             errorMap.put(Long.valueOf(errorCodes[i]), errorMessages[i]);
255         }
256     }
257 
258 
259     /**
260      * Constructor taking the error code as defined for the CKR_* constants
261      * in PKCS#11.
262      */
263     public PKCS11Exception(long errorCode) {
264         errorCode_ = errorCode;
265     }
266 
267     /**
268      * This method gets the corresponding text error message from
269      * a property file. If this file is not available, it returns the error
270      * code as a hex-string.
271      *
272      * @return The message or the error code; e.g. "CKR_DEVICE_ERROR" or
273      *         "0x00000030".
274      * @preconditions
275      * @postconditions (result <> null)
276      */
277     public String getMessage() {
278         String message = errorMap.get(Long.valueOf(errorCode_));
279         if (message == null) {
280             message = "0x" + Functions.toFullHexString((int)errorCode_);
281         }
282         return message;
283     }
284 
285     /**
286      * Returns the PKCS#11 error code.
287      *
288      * @return The error code; e.g. 0x00000030.
289      * @preconditions
290      * @postconditions
291      */
292     public long getErrorCode() {
293         return errorCode_ ;
294     }
295 
296 }