blob: b66e23a73def86961745edba3fff19968695db0f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Portions Copyright 2003 Sun Microsystems, Inc. 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
48package sun.security.pkcs11.wrapper;
49
50import 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 */
65public 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 0x00000150,
152 0x00000160,
153 0x00000170,
154 0x00000180,
155 0x00000190,
156 0x00000191,
157 0x000001A0,
158 0x000001A1,
159 0x80000000,
160 };
161 String[] errorMessages = new String[] {
162 "CKR_OK",
163 "CKR_CANCEL",
164 "CKR_HOST_MEMORY",
165 "CKR_SLOT_ID_INVALID",
166 "CKR_GENERAL_ERROR",
167 "CKR_FUNCTION_FAILED",
168 "CKR_ARGUMENTS_BAD",
169 "CKR_NO_EVENT",
170 "CKR_NEED_TO_CREATE_THREADS",
171 "CKR_CANT_LOCK",
172 "CKR_ATTRIBUTE_READ_ONLY",
173 "CKR_ATTRIBUTE_SENSITIVE",
174 "CKR_ATTRIBUTE_TYPE_INVALID",
175 "CKR_ATTRIBUTE_VALUE_INVALID",
176 "CKR_DATA_INVALID",
177 "CKR_DATA_LEN_RANGE",
178 "CKR_DEVICE_ERROR",
179 "CKR_DEVICE_MEMORY",
180 "CKR_DEVICE_REMOVED",
181 "CKR_ENCRYPTED_DATA_INVALID",
182 "CKR_ENCRYPTED_DATA_LEN_RANGE",
183 "CKR_FUNCTION_CANCELED",
184 "CKR_FUNCTION_NOT_PARALLEL",
185 "CKR_FUNCTION_NOT_SUPPORTED",
186 "CKR_KEY_HANDLE_INVALID",
187 "CKR_KEY_SIZE_RANGE",
188 "CKR_KEY_TYPE_INCONSISTENT",
189 "CKR_KEY_NOT_NEEDED",
190 "CKR_KEY_CHANGED",
191 "CKR_KEY_NEEDED",
192 "CKR_KEY_INDIGESTIBLE",
193 "CKR_KEY_FUNCTION_NOT_PERMITTED",
194 "CKR_KEY_NOT_WRAPPABLE",
195 "CKR_KEY_UNEXTRACTABLE",
196 "CKR_MECHANISM_INVALID",
197 "CKR_MECHANISM_PARAM_INVALID",
198 "CKR_OBJECT_HANDLE_INVALID",
199 "CKR_OPERATION_ACTIVE",
200 "CKR_OPERATION_NOT_INITIALIZED",
201 "CKR_PIN_INCORRECT",
202 "CKR_PIN_INVALID",
203 "CKR_PIN_LEN_RANGE",
204 "CKR_PIN_EXPIRED",
205 "CKR_PIN_LOCKED",
206 "CKR_SESSION_CLOSED",
207 "CKR_SESSION_COUNT",
208 "CKR_SESSION_HANDLE_INVALID",
209 "CKR_SESSION_PARALLEL_NOT_SUPPORTED",
210 "CKR_SESSION_READ_ONLY",
211 "CKR_SESSION_EXISTS",
212 "CKR_SESSION_READ_ONLY_EXISTS",
213 "CKR_SESSION_READ_WRITE_SO_EXISTS",
214 "CKR_SIGNATURE_INVALID",
215 "CKR_SIGNATURE_LEN_RANGE",
216 "CKR_TEMPLATE_INCOMPLETE",
217 "CKR_TEMPLATE_INCONSISTENT",
218 "CKR_TOKEN_NOT_PRESENT",
219 "CKR_TOKEN_NOT_RECOGNIZED",
220 "CKR_TOKEN_WRITE_PROTECTED",
221 "CKR_UNWRAPPING_KEY_HANDLE_INVALID",
222 "CKR_UNWRAPPING_KEY_SIZE_RANGE",
223 "CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT",
224 "CKR_USER_ALREADY_LOGGED_IN",
225 "CKR_USER_NOT_LOGGED_IN",
226 "CKR_USER_PIN_NOT_INITIALIZED",
227 "CKR_USER_TYPE_INVALID",
228 "CKR_USER_ANOTHER_ALREADY_LOGGED_IN",
229 "CKR_USER_TOO_MANY_TYPES",
230 "CKR_WRAPPED_KEY_INVALID",
231 "CKR_WRAPPED_KEY_LEN_RANGE",
232 "CKR_WRAPPING_KEY_HANDLE_INVALID",
233 "CKR_WRAPPING_KEY_SIZE_RANGE",
234 "CKR_WRAPPING_KEY_TYPE_INCONSISTENT",
235 "CKR_RANDOM_SEED_NOT_SUPPORTED",
236 "CKR_RANDOM_NO_RNG",
237 "CKR_BUFFER_TOO_SMALL",
238 "CKR_SAVED_STATE_INVALID",
239 "CKR_INFORMATION_SENSITIVE",
240 "CKR_STATE_UNSAVEABLE",
241 "CKR_CRYPTOKI_NOT_INITIALIZED",
242 "CKR_CRYPTOKI_ALREADY_INITIALIZED",
243 "CKR_MUTEX_BAD",
244 "CKR_MUTEX_NOT_LOCKED",
245 "CKR_VENDOR_DEFINED",
246 };
247 errorMap = new HashMap<Long,String>();
248 for (int i = 0; i < errorCodes.length; i++) {
249 errorMap.put(Long.valueOf(errorCodes[i]), errorMessages[i]);
250 }
251 }
252
253
254 /**
255 * Constructor taking the error code as defined for the CKR_* constants
256 * in PKCS#11.
257 */
258 public PKCS11Exception(long errorCode) {
259 errorCode_ = errorCode;
260 }
261
262 /**
263 * This method gets the corresponding text error message from
264 * a property file. If this file is not available, it returns the error
265 * code as a hex-string.
266 *
267 * @return The message or the error code; e.g. "CKR_DEVICE_ERROR" or
268 * "0x00000030".
269 * @preconditions
270 * @postconditions (result <> null)
271 */
272 public String getMessage() {
273 String message = errorMap.get(Long.valueOf(errorCode_));
274 if (message == null) {
275 message = "0x" + Functions.toFullHexString((int)errorCode_);
276 }
277 return message;
278 }
279
280 /**
281 * Returns the PKCS#11 error code.
282 *
283 * @return The error code; e.g. 0x00000030.
284 * @preconditions
285 * @postconditions
286 */
287 public long getErrorCode() {
288 return errorCode_ ;
289 }
290
291}