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