Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/wrapper/PKCS11Exception.java
41161 views
1
/*
2
* Copyright (c) 2003, 2021, 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
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
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:[email protected]"> Karl Scheibelhofer </a>
63
* @invariants
64
*/
65
public class PKCS11Exception extends Exception {
66
private static final long serialVersionUID = 4077027363729192L;
67
68
/**
69
* The code of the error which was the reason for this exception.
70
*/
71
protected long errorCode;
72
73
protected String errorMsg;
74
75
public static final long CKR_GENERAL_ERROR = RV.CKR_GENERAL_ERROR.value;
76
public static final long CKR_ATTRIBUTE_TYPE_INVALID =
77
RV.CKR_ATTRIBUTE_TYPE_INVALID.value;
78
public static final long CKR_DATA_LEN_RANGE = RV.CKR_DATA_LEN_RANGE.value;
79
public static final long CKR_ENCRYPTED_DATA_INVALID =
80
RV.CKR_ENCRYPTED_DATA_INVALID.value;
81
public static final long CKR_ENCRYPTED_DATA_LEN_RANGE =
82
RV.CKR_ENCRYPTED_DATA_LEN_RANGE.value;
83
public static final long CKR_MECHANISM_INVALID =
84
RV.CKR_MECHANISM_INVALID.value;
85
public static final long CKR_MECHANISM_PARAM_INVALID =
86
RV.CKR_MECHANISM_PARAM_INVALID.value;
87
public static final long CKR_OPERATION_NOT_INITIALIZED =
88
RV.CKR_OPERATION_NOT_INITIALIZED.value;
89
public static final long CKR_PIN_INCORRECT =
90
RV.CKR_PIN_INCORRECT.value;
91
public static final long CKR_SIGNATURE_INVALID =
92
RV.CKR_SIGNATURE_INVALID.value;
93
public static final long CKR_SIGNATURE_LEN_RANGE =
94
RV.CKR_SIGNATURE_LEN_RANGE.value;
95
public static final long CKR_USER_ALREADY_LOGGED_IN =
96
RV.CKR_USER_ALREADY_LOGGED_IN.value;
97
public static final long CKR_USER_NOT_LOGGED_IN =
98
RV.CKR_USER_NOT_LOGGED_IN.value;
99
public static final long CKR_BUFFER_TOO_SMALL =
100
RV.CKR_BUFFER_TOO_SMALL.value;
101
public static final long CKR_CRYPTOKI_ALREADY_INITIALIZED =
102
RV.CKR_CRYPTOKI_ALREADY_INITIALIZED.value;
103
104
// enum for all PKCS#11 return value
105
static enum RV {
106
CKR_OK(0x00000000L),
107
CKR_CANCEL(0x00000001L),
108
CKR_HOST_MEMORY(0x00000002L),
109
CKR_SLOT_ID_INVALID(0x00000003L),
110
CKR_GENERAL_ERROR(0x00000005L),
111
CKR_FUNCTION_FAILED(0x00000006L),
112
CKR_ARGUMENTS_BAD(0x00000007L),
113
CKR_NO_EVENT(0x00000008L),
114
CKR_NEED_TO_CREATE_THREADS(0x00000009L),
115
CKR_CANT_LOCK(0x0000000AL),
116
CKR_ATTRIBUTE_READ_ONLY(0x00000010L),
117
CKR_ATTRIBUTE_SENSITIVE(0x00000011L),
118
CKR_ATTRIBUTE_TYPE_INVALID(0x00000012L),
119
CKR_ATTRIBUTE_VALUE_INVALID(0x00000013L),
120
CKR_ACTION_PROHIBITED(0x0000001BL),
121
CKR_DATA_INVALID(0x00000020L),
122
CKR_DATA_LEN_RANGE(0x00000021L),
123
CKR_DEVICE_ERROR(0x00000030L),
124
CKR_DEVICE_MEMORY(0x00000031L),
125
CKR_DEVICE_REMOVED(0x00000032L),
126
CKR_ENCRYPTED_DATA_INVALID(0x00000040L),
127
CKR_ENCRYPTED_DATA_LEN_RANGE(0x00000041L),
128
CKR_AEAD_DECRYPT_FAILED(0x00000042L),
129
CKR_FUNCTION_CANCELED(0x00000050L),
130
CKR_FUNCTION_NOT_PARALLEL(0x00000051L),
131
CKR_FUNCTION_NOT_SUPPORTED(0x00000054L),
132
CKR_KEY_HANDLE_INVALID(0x00000060L),
133
CKR_KEY_SIZE_RANGE(0x00000062L),
134
CKR_KEY_TYPE_INCONSISTENT(0x00000063L),
135
CKR_KEY_NOT_NEEDED(0x00000064L),
136
CKR_KEY_CHANGED(0x00000065L),
137
CKR_KEY_NEEDED(0x00000066L),
138
CKR_KEY_INDIGESTIBLE(0x00000067L),
139
CKR_KEY_FUNCTION_NOT_PERMITTED(0x00000068L),
140
CKR_KEY_NOT_WRAPPABLE(0x00000069L),
141
CKR_KEY_UNEXTRACTABLE(0x0000006AL),
142
CKR_MECHANISM_INVALID(0x00000070L),
143
CKR_MECHANISM_PARAM_INVALID(0x00000071L),
144
CKR_OBJECT_HANDLE_INVALID(0x00000082L),
145
CKR_OPERATION_ACTIVE(0x00000090L),
146
CKR_OPERATION_NOT_INITIALIZED(0x00000091L),
147
CKR_PIN_INCORRECT(0x000000A0L),
148
CKR_PIN_INVALID(0x000000A1L),
149
CKR_PIN_LEN_RANGE(0x000000A2L),
150
CKR_PIN_EXPIRED(0x000000A3L),
151
CKR_PIN_LOCKED(0x000000A4L),
152
CKR_SESSION_CLOSED(0x000000B0L),
153
CKR_SESSION_COUNT(0x000000B1L),
154
CKR_SESSION_HANDLE_INVALID(0x000000B3L),
155
CKR_SESSION_PARALLEL_NOT_SUPPORTED(0x000000B4L),
156
CKR_SESSION_READ_ONLY(0x000000B5L),
157
CKR_SESSION_EXISTS(0x000000B6L),
158
CKR_SESSION_READ_ONLY_EXISTS(0x000000B7L),
159
CKR_SESSION_READ_WRITE_SO_EXISTS(0x000000B8L),
160
CKR_SIGNATURE_INVALID(0x000000C0L),
161
CKR_SIGNATURE_LEN_RANGE(0x000000C1L),
162
CKR_TEMPLATE_INCOMPLETE(0x000000D0L),
163
CKR_TEMPLATE_INCONSISTENT(0x000000D1L),
164
CKR_TOKEN_NOT_PRESENT(0x000000E0L),
165
CKR_TOKEN_NOT_RECOGNIZED(0x000000E1L),
166
CKR_TOKEN_WRITE_PROTECTED(0x000000E2L),
167
CKR_UNWRAPPING_KEY_HANDLE_INVALID(0x000000F0L),
168
CKR_UNWRAPPING_KEY_SIZE_RANGE(0x000000F1L),
169
CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT(0x000000F2L),
170
CKR_USER_ALREADY_LOGGED_IN(0x00000100L),
171
CKR_USER_NOT_LOGGED_IN(0x00000101L),
172
CKR_USER_PIN_NOT_INITIALIZED(0x00000102L),
173
CKR_USER_TYPE_INVALID(0x00000103L),
174
CKR_USER_ANOTHER_ALREADY_LOGGED_IN(0x00000104L),
175
CKR_USER_TOO_MANY_TYPES(0x00000105L),
176
CKR_WRAPPED_KEY_INVALID(0x00000110L),
177
CKR_WRAPPED_KEY_LEN_RANGE(0x00000112L),
178
CKR_WRAPPING_KEY_HANDLE_INVALID(0x00000113L),
179
CKR_WRAPPING_KEY_SIZE_RANGE(0x00000114L),
180
CKR_WRAPPING_KEY_TYPE_INCONSISTENT(0x00000115L),
181
CKR_RANDOM_SEED_NOT_SUPPORTED(0x00000120L),
182
CKR_RANDOM_NO_RNG(0x00000121L),
183
CKR_DOMAIN_PARAMS_INVALID(0x00000130L),
184
CKR_CURVE_NOT_SUPPORTED(0x00000140L),
185
CKR_BUFFER_TOO_SMALL(0x00000150L),
186
CKR_SAVED_STATE_INVALID(0x00000160L),
187
CKR_INFORMATION_SENSITIVE(0x00000170L),
188
CKR_STATE_UNSAVEABLE(0x00000180L),
189
CKR_CRYPTOKI_NOT_INITIALIZED(0x00000190L),
190
CKR_CRYPTOKI_ALREADY_INITIALIZED(0x00000191L),
191
CKR_MUTEX_BAD(0x000001A0L),
192
CKR_MUTEX_NOT_LOCKED(0x000001A1L),
193
CKR_NEW_PIN_MODE(0x000001B0L),
194
CKR_NEXT_OTP(0x000001B1L),
195
CKR_EXCEEDED_MAX_ITERATIONS(0x000001B5L),
196
CKR_FIPS_SELF_TEST_FAILED(0x000001B6L),
197
CKR_LIBRARY_LOAD_FAILED(0x000001B7L),
198
CKR_PIN_TOO_WEAK(0x000001B8L),
199
CKR_PUBLIC_KEY_INVALID(0x000001B9L),
200
CKR_FUNCTION_REJECTED(0x00000200L),
201
CKR_TOKEN_RESOURCE_EXCEEDED(0x00000201L),
202
CKR_OPERATION_CANCEL_FAILED(0x00000202L),
203
CKR_VENDOR_DEFINED(0x80000000L);
204
205
private final long value;
206
207
RV(long value) {
208
this.value = value;
209
}
210
};
211
212
private static String lookup(long errorCode) {
213
for (RV r : RV.values()) {
214
if (r.value == errorCode) {
215
return r.name();
216
}
217
}
218
// for unknown PKCS11 return values, just use hex as its string
219
return "0x" + Functions.toFullHexString((int)errorCode);
220
}
221
222
/**
223
* Constructor taking the error code (the CKR_* constants in PKCS#11) and
224
* extra info for error message.
225
*/
226
public PKCS11Exception(long errorCode, String extraInfo) {
227
this.errorCode = errorCode;
228
this.errorMsg = lookup(errorCode);
229
if (extraInfo != null) {
230
this.errorMsg += extraInfo;
231
}
232
}
233
234
/**
235
* This method gets the corresponding text error message from
236
* a property file. If this file is not available, it returns the error
237
* code as a hex-string.
238
*
239
* @return The message or the error code; e.g. "CKR_DEVICE_ERROR" or
240
* "0x00000030".
241
* @preconditions
242
* @postconditions (result <> null)
243
*/
244
public String getMessage() {
245
return errorMsg;
246
}
247
248
/**
249
* Returns the PKCS#11 error code.
250
*
251
* @return The error code; e.g. 0x00000030.
252
* @preconditions
253
* @postconditions
254
*/
255
public long getErrorCode() {
256
return errorCode;
257
}
258
}
259
260