author | valeriep |
Tue, 04 Sep 2012 18:41:06 -0700 | |
changeset 13672 | 604588823b5a |
parent 12685 | 8a448b5b9006 |
child 18772 | ab43f18c524e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
11509
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.pkcs11; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.util.*; |
|
30 |
||
31 |
import java.security.*; |
|
32 |
import java.security.interfaces.*; |
|
33 |
||
34 |
import javax.crypto.interfaces.*; |
|
35 |
||
36 |
import javax.security.auth.Subject; |
|
37 |
import javax.security.auth.login.LoginException; |
|
38 |
import javax.security.auth.login.FailedLoginException; |
|
39 |
import javax.security.auth.callback.Callback; |
|
40 |
import javax.security.auth.callback.CallbackHandler; |
|
41 |
import javax.security.auth.callback.ConfirmationCallback; |
|
42 |
import javax.security.auth.callback.PasswordCallback; |
|
43 |
import javax.security.auth.callback.TextOutputCallback; |
|
44 |
||
45 |
import sun.security.util.Debug; |
|
46 |
import sun.security.util.ResourcesMgr; |
|
47 |
||
48 |
import sun.security.pkcs11.Secmod.*; |
|
49 |
||
50 |
import sun.security.pkcs11.wrapper.*; |
|
51 |
import static sun.security.pkcs11.wrapper.PKCS11Constants.*; |
|
52 |
||
53 |
/** |
|
54 |
* PKCS#11 provider main class. |
|
55 |
* |
|
56 |
* @author Andreas Sterbenz |
|
57 |
* @since 1.5 |
|
58 |
*/ |
|
59 |
public final class SunPKCS11 extends AuthProvider { |
|
60 |
||
61 |
private static final long serialVersionUID = -1354835039035306505L; |
|
62 |
||
63 |
static final Debug debug = Debug.getInstance("sunpkcs11"); |
|
64 |
||
65 |
private static int dummyConfigId; |
|
66 |
||
67 |
// the PKCS11 object through which we make the native calls |
|
68 |
final PKCS11 p11; |
|
69 |
||
70 |
// name of the configuration file |
|
71 |
private final String configName; |
|
72 |
||
73 |
// configuration information |
|
74 |
final Config config; |
|
75 |
||
76 |
// id of the PKCS#11 slot we are using |
|
77 |
final long slotID; |
|
78 |
||
79 |
private CallbackHandler pHandler; |
|
80 |
private final Object LOCK_HANDLER = new Object(); |
|
81 |
||
82 |
final boolean removable; |
|
83 |
||
84 |
final Module nssModule; |
|
85 |
||
86 |
final boolean nssUseSecmodTrust; |
|
87 |
||
88 |
private volatile Token token; |
|
89 |
||
90 |
private TokenPoller poller; |
|
91 |
||
92 |
Token getToken() { |
|
93 |
return token; |
|
94 |
} |
|
95 |
||
96 |
public SunPKCS11() { |
|
97 |
super("SunPKCS11-Dummy", 1.7d, "SunPKCS11-Dummy"); |
|
98 |
throw new ProviderException |
|
99 |
("SunPKCS11 requires configuration file argument"); |
|
100 |
} |
|
101 |
||
102 |
public SunPKCS11(String configName) { |
|
103 |
this(checkNull(configName), null); |
|
104 |
} |
|
105 |
||
106 |
public SunPKCS11(InputStream configStream) { |
|
107 |
this(getDummyConfigName(), checkNull(configStream)); |
|
108 |
} |
|
109 |
||
110 |
private static <T> T checkNull(T obj) { |
|
111 |
if (obj == null) { |
|
112 |
throw new NullPointerException(); |
|
113 |
} |
|
114 |
return obj; |
|
115 |
} |
|
116 |
||
117 |
private static synchronized String getDummyConfigName() { |
|
118 |
int id = ++dummyConfigId; |
|
119 |
return "---DummyConfig-" + id + "---"; |
|
120 |
} |
|
121 |
||
122 |
/** |
|
2596 | 123 |
* @deprecated use new SunPKCS11(String) or new SunPKCS11(InputStream) |
124 |
* instead |
|
2 | 125 |
*/ |
126 |
@Deprecated |
|
127 |
public SunPKCS11(String configName, InputStream configStream) { |
|
2596 | 128 |
super("SunPKCS11-" + |
129 |
Config.getConfig(configName, configStream).getName(), |
|
2 | 130 |
1.7d, Config.getConfig(configName, configStream).getDescription()); |
131 |
this.configName = configName; |
|
132 |
this.config = Config.removeConfig(configName); |
|
133 |
||
134 |
if (debug != null) { |
|
135 |
System.out.println("SunPKCS11 loading " + configName); |
|
136 |
} |
|
137 |
||
138 |
String library = config.getLibrary(); |
|
139 |
String functionList = config.getFunctionList(); |
|
140 |
long slotID = config.getSlotID(); |
|
141 |
int slotListIndex = config.getSlotListIndex(); |
|
142 |
||
143 |
boolean useSecmod = config.getNssUseSecmod(); |
|
144 |
boolean nssUseSecmodTrust = config.getNssUseSecmodTrust(); |
|
145 |
Module nssModule = null; |
|
146 |
||
147 |
// |
|
148 |
// Initialization via Secmod. The way this works is as follows: |
|
149 |
// SunPKCS11 is either in normal mode or in NSS Secmod mode. |
|
150 |
// Secmod is activated by specifying one or more of the following |
|
151 |
// options in the config file: |
|
152 |
// nssUseSecmod, nssSecmodDirectory, nssLibrary, nssModule |
|
153 |
// |
|
154 |
// XXX add more explanation here |
|
155 |
// |
|
156 |
// If we are in Secmod mode and configured to use either the |
|
157 |
// nssKeyStore or the nssTrustAnchors module, we automatically |
|
2596 | 158 |
// switch to using the NSS trust attributes for trusted certs |
159 |
// (KeyStore). |
|
2 | 160 |
// |
161 |
||
162 |
if (useSecmod) { |
|
163 |
// note: Config ensures library/slot/slotListIndex not specified |
|
164 |
// in secmod mode. |
|
165 |
Secmod secmod = Secmod.getInstance(); |
|
166 |
DbMode nssDbMode = config.getNssDbMode(); |
|
167 |
try { |
|
168 |
String nssLibraryDirectory = config.getNssLibraryDirectory(); |
|
169 |
String nssSecmodDirectory = config.getNssSecmodDirectory(); |
|
170 |
||
171 |
if (secmod.isInitialized()) { |
|
172 |
if (nssSecmodDirectory != null) { |
|
173 |
String s = secmod.getConfigDir(); |
|
2596 | 174 |
if ((s != null) && |
175 |
(s.equals(nssSecmodDirectory) == false)) { |
|
2 | 176 |
throw new ProviderException("Secmod directory " |
177 |
+ nssSecmodDirectory |
|
2596 | 178 |
+ " invalid, NSS already initialized with " |
179 |
+ s); |
|
2 | 180 |
} |
181 |
} |
|
182 |
if (nssLibraryDirectory != null) { |
|
183 |
String s = secmod.getLibDir(); |
|
2596 | 184 |
if ((s != null) && |
185 |
(s.equals(nssLibraryDirectory) == false)) { |
|
2 | 186 |
throw new ProviderException("NSS library directory " |
187 |
+ nssLibraryDirectory |
|
2596 | 188 |
+ " invalid, NSS already initialized with " |
189 |
+ s); |
|
2 | 190 |
} |
191 |
} |
|
192 |
} else { |
|
193 |
if (nssDbMode != DbMode.NO_DB) { |
|
194 |
if (nssSecmodDirectory == null) { |
|
2596 | 195 |
throw new ProviderException( |
196 |
"Secmod not initialized and " |
|
197 |
+ "nssSecmodDirectory not specified"); |
|
2 | 198 |
} |
199 |
} else { |
|
200 |
if (nssSecmodDirectory != null) { |
|
2596 | 201 |
throw new ProviderException( |
202 |
"nssSecmodDirectory must not be " |
|
203 |
+ "specified in noDb mode"); |
|
2 | 204 |
} |
205 |
} |
|
2596 | 206 |
secmod.initialize(nssDbMode, nssSecmodDirectory, |
207 |
nssLibraryDirectory); |
|
2 | 208 |
} |
209 |
} catch (IOException e) { |
|
210 |
// XXX which exception to throw |
|
211 |
throw new ProviderException("Could not initialize NSS", e); |
|
212 |
} |
|
213 |
List<Module> modules = secmod.getModules(); |
|
214 |
if (config.getShowInfo()) { |
|
215 |
System.out.println("NSS modules: " + modules); |
|
216 |
} |
|
217 |
||
218 |
String moduleName = config.getNssModule(); |
|
219 |
if (moduleName == null) { |
|
220 |
nssModule = secmod.getModule(ModuleType.FIPS); |
|
221 |
if (nssModule != null) { |
|
222 |
moduleName = "fips"; |
|
223 |
} else { |
|
2596 | 224 |
moduleName = (nssDbMode == DbMode.NO_DB) ? |
225 |
"crypto" : "keystore"; |
|
2 | 226 |
} |
227 |
} |
|
228 |
if (moduleName.equals("fips")) { |
|
229 |
nssModule = secmod.getModule(ModuleType.FIPS); |
|
230 |
nssUseSecmodTrust = true; |
|
231 |
functionList = "FC_GetFunctionList"; |
|
232 |
} else if (moduleName.equals("keystore")) { |
|
233 |
nssModule = secmod.getModule(ModuleType.KEYSTORE); |
|
234 |
nssUseSecmodTrust = true; |
|
235 |
} else if (moduleName.equals("crypto")) { |
|
236 |
nssModule = secmod.getModule(ModuleType.CRYPTO); |
|
237 |
} else if (moduleName.equals("trustanchors")) { |
|
238 |
// XXX should the option be called trustanchor or trustanchors?? |
|
239 |
nssModule = secmod.getModule(ModuleType.TRUSTANCHOR); |
|
240 |
nssUseSecmodTrust = true; |
|
241 |
} else if (moduleName.startsWith("external-")) { |
|
242 |
int moduleIndex; |
|
243 |
try { |
|
244 |
moduleIndex = Integer.parseInt |
|
245 |
(moduleName.substring("external-".length())); |
|
246 |
} catch (NumberFormatException e) { |
|
247 |
moduleIndex = -1; |
|
248 |
} |
|
249 |
if (moduleIndex < 1) { |
|
250 |
throw new ProviderException |
|
251 |
("Invalid external module: " + moduleName); |
|
252 |
} |
|
253 |
int k = 0; |
|
254 |
for (Module module : modules) { |
|
255 |
if (module.getType() == ModuleType.EXTERNAL) { |
|
256 |
if (++k == moduleIndex) { |
|
257 |
nssModule = module; |
|
258 |
break; |
|
259 |
} |
|
260 |
} |
|
261 |
} |
|
262 |
if (nssModule == null) { |
|
263 |
throw new ProviderException("Invalid module " + moduleName |
|
264 |
+ ": only " + k + " external NSS modules available"); |
|
265 |
} |
|
266 |
} else { |
|
2596 | 267 |
throw new ProviderException( |
268 |
"Unknown NSS module: " + moduleName); |
|
2 | 269 |
} |
270 |
if (nssModule == null) { |
|
2596 | 271 |
throw new ProviderException( |
272 |
"NSS module not available: " + moduleName); |
|
2 | 273 |
} |
274 |
if (nssModule.hasInitializedProvider()) { |
|
275 |
throw new ProviderException("Secmod module already configured"); |
|
276 |
} |
|
277 |
library = nssModule.libraryName; |
|
278 |
slotListIndex = nssModule.slot; |
|
279 |
} |
|
280 |
this.nssUseSecmodTrust = nssUseSecmodTrust; |
|
281 |
this.nssModule = nssModule; |
|
282 |
||
283 |
File libraryFile = new File(library); |
|
284 |
// if the filename is a simple filename without path |
|
285 |
// (e.g. "libpkcs11.so"), it may refer to a library somewhere on the |
|
286 |
// OS library search path. Omit the test for file existance as that |
|
287 |
// only looks in the current directory. |
|
288 |
if (libraryFile.getName().equals(library) == false) { |
|
289 |
if (new File(library).isFile() == false) { |
|
290 |
String msg = "Library " + library + " does not exist"; |
|
291 |
if (config.getHandleStartupErrors() == Config.ERR_HALT) { |
|
292 |
throw new ProviderException(msg); |
|
293 |
} else { |
|
294 |
throw new UnsupportedOperationException(msg); |
|
295 |
} |
|
296 |
} |
|
297 |
} |
|
298 |
||
299 |
try { |
|
300 |
if (debug != null) { |
|
301 |
debug.println("Initializing PKCS#11 library " + library); |
|
302 |
} |
|
303 |
CK_C_INITIALIZE_ARGS initArgs = new CK_C_INITIALIZE_ARGS(); |
|
304 |
String nssArgs = config.getNssArgs(); |
|
305 |
if (nssArgs != null) { |
|
306 |
initArgs.pReserved = nssArgs; |
|
307 |
} |
|
308 |
// request multithreaded access first |
|
309 |
initArgs.flags = CKF_OS_LOCKING_OK; |
|
310 |
PKCS11 tmpPKCS11; |
|
311 |
try { |
|
2596 | 312 |
tmpPKCS11 = PKCS11.getInstance( |
313 |
library, functionList, initArgs, |
|
314 |
config.getOmitInitialize()); |
|
2 | 315 |
} catch (PKCS11Exception e) { |
316 |
if (debug != null) { |
|
317 |
debug.println("Multi-threaded initialization failed: " + e); |
|
318 |
} |
|
319 |
if (config.getAllowSingleThreadedModules() == false) { |
|
320 |
throw e; |
|
321 |
} |
|
322 |
// fall back to single threaded access |
|
323 |
if (nssArgs == null) { |
|
324 |
// if possible, use null initArgs for better compatibility |
|
325 |
initArgs = null; |
|
326 |
} else { |
|
327 |
initArgs.flags = 0; |
|
328 |
} |
|
2596 | 329 |
tmpPKCS11 = PKCS11.getInstance(library, |
330 |
functionList, initArgs, config.getOmitInitialize()); |
|
2 | 331 |
} |
332 |
p11 = tmpPKCS11; |
|
333 |
||
334 |
CK_INFO p11Info = p11.C_GetInfo(); |
|
335 |
if (p11Info.cryptokiVersion.major < 2) { |
|
336 |
throw new ProviderException("Only PKCS#11 v2.0 and later " |
|
337 |
+ "supported, library version is v" + p11Info.cryptokiVersion); |
|
338 |
} |
|
339 |
boolean showInfo = config.getShowInfo(); |
|
340 |
if (showInfo) { |
|
341 |
System.out.println("Information for provider " + getName()); |
|
342 |
System.out.println("Library info:"); |
|
343 |
System.out.println(p11Info); |
|
344 |
} |
|
12685 | 345 |
|
2 | 346 |
if ((slotID < 0) || showInfo) { |
347 |
long[] slots = p11.C_GetSlotList(false); |
|
348 |
if (showInfo) { |
|
349 |
System.out.println("All slots: " + toString(slots)); |
|
350 |
slots = p11.C_GetSlotList(true); |
|
351 |
System.out.println("Slots with tokens: " + toString(slots)); |
|
352 |
} |
|
353 |
if (slotID < 0) { |
|
2596 | 354 |
if ((slotListIndex < 0) |
355 |
|| (slotListIndex >= slots.length)) { |
|
356 |
throw new ProviderException("slotListIndex is " |
|
357 |
+ slotListIndex |
|
2 | 358 |
+ " but token only has " + slots.length + " slots"); |
359 |
} |
|
360 |
slotID = slots[slotListIndex]; |
|
361 |
} |
|
362 |
} |
|
363 |
this.slotID = slotID; |
|
364 |
CK_SLOT_INFO slotInfo = p11.C_GetSlotInfo(slotID); |
|
365 |
removable = (slotInfo.flags & CKF_REMOVABLE_DEVICE) != 0; |
|
366 |
initToken(slotInfo); |
|
367 |
if (nssModule != null) { |
|
368 |
nssModule.setProvider(this); |
|
369 |
} |
|
370 |
} catch (Exception e) { |
|
371 |
if (config.getHandleStartupErrors() == Config.ERR_IGNORE_ALL) { |
|
372 |
throw new UnsupportedOperationException |
|
373 |
("Initialization failed", e); |
|
374 |
} else { |
|
375 |
throw new ProviderException |
|
376 |
("Initialization failed", e); |
|
377 |
} |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
private static String toString(long[] longs) { |
|
382 |
if (longs.length == 0) { |
|
383 |
return "(none)"; |
|
384 |
} |
|
385 |
StringBuilder sb = new StringBuilder(); |
|
386 |
sb.append(longs[0]); |
|
387 |
for (int i = 1; i < longs.length; i++) { |
|
388 |
sb.append(", "); |
|
389 |
sb.append(longs[i]); |
|
390 |
} |
|
391 |
return sb.toString(); |
|
392 |
} |
|
393 |
||
394 |
public boolean equals(Object obj) { |
|
395 |
return this == obj; |
|
396 |
} |
|
397 |
||
398 |
public int hashCode() { |
|
399 |
return System.identityHashCode(this); |
|
400 |
} |
|
401 |
||
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
402 |
private static String[] s(String ...aliases) { |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
403 |
return aliases; |
2 | 404 |
} |
405 |
||
406 |
private static final class Descriptor { |
|
407 |
final String type; |
|
408 |
final String algorithm; |
|
409 |
final String className; |
|
410 |
final String[] aliases; |
|
411 |
final int[] mechanisms; |
|
412 |
||
413 |
private Descriptor(String type, String algorithm, String className, |
|
414 |
String[] aliases, int[] mechanisms) { |
|
415 |
this.type = type; |
|
416 |
this.algorithm = algorithm; |
|
417 |
this.className = className; |
|
418 |
this.aliases = aliases; |
|
419 |
this.mechanisms = mechanisms; |
|
420 |
} |
|
421 |
private P11Service service(Token token, int mechanism) { |
|
422 |
return new P11Service |
|
423 |
(token, type, algorithm, className, aliases, mechanism); |
|
424 |
} |
|
425 |
public String toString() { |
|
426 |
return type + "." + algorithm; |
|
427 |
} |
|
428 |
} |
|
429 |
||
430 |
// Map from mechanism to List of Descriptors that should be |
|
431 |
// registered if the mechanism is supported |
|
432 |
private final static Map<Integer,List<Descriptor>> descriptors = |
|
433 |
new HashMap<Integer,List<Descriptor>>(); |
|
434 |
||
435 |
private static int[] m(long m1) { |
|
436 |
return new int[] {(int)m1}; |
|
437 |
} |
|
438 |
||
439 |
private static int[] m(long m1, long m2) { |
|
440 |
return new int[] {(int)m1, (int)m2}; |
|
441 |
} |
|
442 |
||
443 |
private static int[] m(long m1, long m2, long m3) { |
|
444 |
return new int[] {(int)m1, (int)m2, (int)m3}; |
|
445 |
} |
|
446 |
||
447 |
private static int[] m(long m1, long m2, long m3, long m4) { |
|
448 |
return new int[] {(int)m1, (int)m2, (int)m3, (int)m4}; |
|
449 |
} |
|
450 |
||
451 |
private static void d(String type, String algorithm, String className, |
|
452 |
int[] m) { |
|
453 |
register(new Descriptor(type, algorithm, className, null, m)); |
|
454 |
} |
|
455 |
||
456 |
private static void d(String type, String algorithm, String className, |
|
457 |
String[] aliases, int[] m) { |
|
458 |
register(new Descriptor(type, algorithm, className, aliases, m)); |
|
459 |
} |
|
460 |
||
461 |
private static void register(Descriptor d) { |
|
462 |
for (int i = 0; i < d.mechanisms.length; i++) { |
|
463 |
int m = d.mechanisms[i]; |
|
464 |
Integer key = Integer.valueOf(m); |
|
465 |
List<Descriptor> list = descriptors.get(key); |
|
466 |
if (list == null) { |
|
467 |
list = new ArrayList<Descriptor>(); |
|
468 |
descriptors.put(key, list); |
|
469 |
} |
|
470 |
list.add(d); |
|
471 |
} |
|
472 |
} |
|
473 |
||
474 |
private final static String MD = "MessageDigest"; |
|
475 |
||
476 |
private final static String SIG = "Signature"; |
|
477 |
||
478 |
private final static String KPG = "KeyPairGenerator"; |
|
479 |
||
480 |
private final static String KG = "KeyGenerator"; |
|
481 |
||
482 |
private final static String AGP = "AlgorithmParameters"; |
|
483 |
||
484 |
private final static String KF = "KeyFactory"; |
|
485 |
||
486 |
private final static String SKF = "SecretKeyFactory"; |
|
487 |
||
488 |
private final static String CIP = "Cipher"; |
|
489 |
||
490 |
private final static String MAC = "Mac"; |
|
491 |
||
492 |
private final static String KA = "KeyAgreement"; |
|
493 |
||
494 |
private final static String KS = "KeyStore"; |
|
495 |
||
496 |
private final static String SR = "SecureRandom"; |
|
497 |
||
498 |
static { |
|
499 |
// names of all the implementation classes |
|
500 |
// use local variables, only used here |
|
501 |
String P11Digest = "sun.security.pkcs11.P11Digest"; |
|
502 |
String P11MAC = "sun.security.pkcs11.P11MAC"; |
|
503 |
String P11KeyPairGenerator = "sun.security.pkcs11.P11KeyPairGenerator"; |
|
504 |
String P11KeyGenerator = "sun.security.pkcs11.P11KeyGenerator"; |
|
505 |
String P11RSAKeyFactory = "sun.security.pkcs11.P11RSAKeyFactory"; |
|
506 |
String P11DSAKeyFactory = "sun.security.pkcs11.P11DSAKeyFactory"; |
|
507 |
String P11DHKeyFactory = "sun.security.pkcs11.P11DHKeyFactory"; |
|
508 |
String P11KeyAgreement = "sun.security.pkcs11.P11KeyAgreement"; |
|
509 |
String P11SecretKeyFactory = "sun.security.pkcs11.P11SecretKeyFactory"; |
|
510 |
String P11Cipher = "sun.security.pkcs11.P11Cipher"; |
|
511 |
String P11RSACipher = "sun.security.pkcs11.P11RSACipher"; |
|
512 |
String P11Signature = "sun.security.pkcs11.P11Signature"; |
|
513 |
||
514 |
// XXX register all aliases |
|
515 |
||
516 |
d(MD, "MD2", P11Digest, |
|
517 |
m(CKM_MD2)); |
|
518 |
d(MD, "MD5", P11Digest, |
|
519 |
m(CKM_MD5)); |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
520 |
d(MD, "SHA1", P11Digest, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
521 |
s("SHA", "SHA-1", "1.3.14.3.2.26", "OID.1.3.14.3.2.26"), |
2 | 522 |
m(CKM_SHA_1)); |
12685 | 523 |
|
524 |
d(MD, "SHA-224", P11Digest, |
|
525 |
s("2.16.840.1.101.3.4.2.4", "OID.2.16.840.1.101.3.4.2.4"), |
|
526 |
m(CKM_SHA224)); |
|
2 | 527 |
d(MD, "SHA-256", P11Digest, |
12685 | 528 |
s("2.16.840.1.101.3.4.2.1", "OID.2.16.840.1.101.3.4.2.1"), |
2 | 529 |
m(CKM_SHA256)); |
530 |
d(MD, "SHA-384", P11Digest, |
|
12685 | 531 |
s("2.16.840.1.101.3.4.2.2", "OID.2.16.840.1.101.3.4.2.2"), |
2 | 532 |
m(CKM_SHA384)); |
533 |
d(MD, "SHA-512", P11Digest, |
|
12685 | 534 |
s("2.16.840.1.101.3.4.2.3", "OID.2.16.840.1.101.3.4.2.3"), |
2 | 535 |
m(CKM_SHA512)); |
536 |
||
537 |
d(MAC, "HmacMD5", P11MAC, |
|
538 |
m(CKM_MD5_HMAC)); |
|
539 |
d(MAC, "HmacSHA1", P11MAC, |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
540 |
s("1.2.840.113549.2.7", "OID.1.2.840.113549.2.7"), |
2 | 541 |
m(CKM_SHA_1_HMAC)); |
12685 | 542 |
d(MAC, "HmacSHA224", P11MAC, |
543 |
s("1.2.840.113549.2.8", "OID.1.2.840.113549.2.8"), |
|
544 |
m(CKM_SHA224_HMAC)); |
|
2 | 545 |
d(MAC, "HmacSHA256", P11MAC, |
12685 | 546 |
s("1.2.840.113549.2.9", "OID.1.2.840.113549.2.9"), |
2 | 547 |
m(CKM_SHA256_HMAC)); |
548 |
d(MAC, "HmacSHA384", P11MAC, |
|
12685 | 549 |
s("1.2.840.113549.2.10", "OID.1.2.840.113549.2.10"), |
2 | 550 |
m(CKM_SHA384_HMAC)); |
551 |
d(MAC, "HmacSHA512", P11MAC, |
|
12685 | 552 |
s("1.2.840.113549.2.11", "OID.1.2.840.113549.2.11"), |
2 | 553 |
m(CKM_SHA512_HMAC)); |
554 |
d(MAC, "SslMacMD5", P11MAC, |
|
555 |
m(CKM_SSL3_MD5_MAC)); |
|
556 |
d(MAC, "SslMacSHA1", P11MAC, |
|
557 |
m(CKM_SSL3_SHA1_MAC)); |
|
558 |
||
559 |
d(KPG, "RSA", P11KeyPairGenerator, |
|
560 |
m(CKM_RSA_PKCS_KEY_PAIR_GEN)); |
|
561 |
d(KPG, "DSA", P11KeyPairGenerator, |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
562 |
s("1.3.14.3.2.12", "1.2.840.10040.4.1", "OID.1.2.840.10040.4.1"), |
2 | 563 |
m(CKM_DSA_KEY_PAIR_GEN)); |
564 |
d(KPG, "DH", P11KeyPairGenerator, s("DiffieHellman"), |
|
565 |
m(CKM_DH_PKCS_KEY_PAIR_GEN)); |
|
566 |
d(KPG, "EC", P11KeyPairGenerator, |
|
567 |
m(CKM_EC_KEY_PAIR_GEN)); |
|
568 |
||
569 |
d(KG, "ARCFOUR", P11KeyGenerator, s("RC4"), |
|
570 |
m(CKM_RC4_KEY_GEN)); |
|
571 |
d(KG, "DES", P11KeyGenerator, |
|
572 |
m(CKM_DES_KEY_GEN)); |
|
573 |
d(KG, "DESede", P11KeyGenerator, |
|
574 |
m(CKM_DES3_KEY_GEN, CKM_DES2_KEY_GEN)); |
|
575 |
d(KG, "AES", P11KeyGenerator, |
|
576 |
m(CKM_AES_KEY_GEN)); |
|
577 |
d(KG, "Blowfish", P11KeyGenerator, |
|
578 |
m(CKM_BLOWFISH_KEY_GEN)); |
|
579 |
||
580 |
// register (Secret)KeyFactories if there are any mechanisms |
|
581 |
// for a particular algorithm that we support |
|
582 |
d(KF, "RSA", P11RSAKeyFactory, |
|
583 |
m(CKM_RSA_PKCS_KEY_PAIR_GEN, CKM_RSA_PKCS, CKM_RSA_X_509)); |
|
584 |
d(KF, "DSA", P11DSAKeyFactory, |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
585 |
s("1.3.14.3.2.12", "1.2.840.10040.4.1", "OID.1.2.840.10040.4.1"), |
2 | 586 |
m(CKM_DSA_KEY_PAIR_GEN, CKM_DSA, CKM_DSA_SHA1)); |
587 |
d(KF, "DH", P11DHKeyFactory, s("DiffieHellman"), |
|
588 |
m(CKM_DH_PKCS_KEY_PAIR_GEN, CKM_DH_PKCS_DERIVE)); |
|
589 |
d(KF, "EC", P11DHKeyFactory, |
|
2596 | 590 |
m(CKM_EC_KEY_PAIR_GEN, CKM_ECDH1_DERIVE, |
591 |
CKM_ECDSA, CKM_ECDSA_SHA1)); |
|
2 | 592 |
|
593 |
// AlgorithmParameters for EC. |
|
594 |
// Only needed until we have an EC implementation in the SUN provider. |
|
2596 | 595 |
d(AGP, "EC", "sun.security.ec.ECParameters", |
596 |
s("1.2.840.10045.2.1"), |
|
597 |
m(CKM_EC_KEY_PAIR_GEN, CKM_ECDH1_DERIVE, |
|
598 |
CKM_ECDSA, CKM_ECDSA_SHA1)); |
|
2 | 599 |
|
600 |
d(KA, "DH", P11KeyAgreement, s("DiffieHellman"), |
|
601 |
m(CKM_DH_PKCS_DERIVE)); |
|
602 |
d(KA, "ECDH", "sun.security.pkcs11.P11ECDHKeyAgreement", |
|
603 |
m(CKM_ECDH1_DERIVE)); |
|
604 |
||
605 |
d(SKF, "ARCFOUR", P11SecretKeyFactory, s("RC4"), |
|
606 |
m(CKM_RC4)); |
|
607 |
d(SKF, "DES", P11SecretKeyFactory, |
|
608 |
m(CKM_DES_CBC)); |
|
609 |
d(SKF, "DESede", P11SecretKeyFactory, |
|
610 |
m(CKM_DES3_CBC)); |
|
611 |
d(SKF, "AES", P11SecretKeyFactory, |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
612 |
s("2.16.840.1.101.3.4.1", "OID.2.16.840.1.101.3.4.1"), |
2 | 613 |
m(CKM_AES_CBC)); |
614 |
d(SKF, "Blowfish", P11SecretKeyFactory, |
|
615 |
m(CKM_BLOWFISH_CBC)); |
|
616 |
||
617 |
// XXX attributes for Ciphers (supported modes, padding) |
|
618 |
d(CIP, "ARCFOUR", P11Cipher, s("RC4"), |
|
619 |
m(CKM_RC4)); |
|
620 |
d(CIP, "DES/CBC/NoPadding", P11Cipher, |
|
621 |
m(CKM_DES_CBC)); |
|
289 | 622 |
d(CIP, "DES/CBC/PKCS5Padding", P11Cipher, |
623 |
m(CKM_DES_CBC_PAD, CKM_DES_CBC)); |
|
11509
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
624 |
d(CIP, "DES/ECB/NoPadding", P11Cipher, |
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
625 |
m(CKM_DES_ECB)); |
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
626 |
d(CIP, "DES/ECB/PKCS5Padding", P11Cipher, s("DES"), |
289 | 627 |
m(CKM_DES_ECB)); |
628 |
||
2 | 629 |
d(CIP, "DESede/CBC/NoPadding", P11Cipher, |
630 |
m(CKM_DES3_CBC)); |
|
289 | 631 |
d(CIP, "DESede/CBC/PKCS5Padding", P11Cipher, |
632 |
m(CKM_DES3_CBC_PAD, CKM_DES3_CBC)); |
|
11509
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
633 |
d(CIP, "DESede/ECB/NoPadding", P11Cipher, |
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
634 |
m(CKM_DES3_ECB)); |
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
635 |
d(CIP, "DESede/ECB/PKCS5Padding", P11Cipher, s("DESede"), |
289 | 636 |
m(CKM_DES3_ECB)); |
2 | 637 |
d(CIP, "AES/CBC/NoPadding", P11Cipher, |
638 |
m(CKM_AES_CBC)); |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
639 |
d(CIP, "AES_128/CBC/NoPadding", P11Cipher, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
640 |
s("2.16.840.1.101.3.4.1.2", "OID.2.16.840.1.101.3.4.1.2"), |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
641 |
m(CKM_AES_CBC)); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
642 |
d(CIP, "AES_192/CBC/NoPadding", P11Cipher, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
643 |
s("2.16.840.1.101.3.4.1.22", "OID.2.16.840.1.101.3.4.1.22"), |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
644 |
m(CKM_AES_CBC)); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
645 |
d(CIP, "AES_256/CBC/NoPadding", P11Cipher, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
646 |
s("2.16.840.1.101.3.4.1.42", "OID.2.16.840.1.101.3.4.1.42"), |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
647 |
m(CKM_AES_CBC)); |
289 | 648 |
d(CIP, "AES/CBC/PKCS5Padding", P11Cipher, |
649 |
m(CKM_AES_CBC_PAD, CKM_AES_CBC)); |
|
11509
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
650 |
d(CIP, "AES/ECB/NoPadding", P11Cipher, |
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
651 |
m(CKM_AES_ECB)); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
652 |
d(CIP, "AES_128/ECB/NoPadding", P11Cipher, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
653 |
s("2.16.840.1.101.3.4.1.1", "OID.2.16.840.1.101.3.4.1.1"), |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
654 |
m(CKM_AES_ECB)); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
655 |
d(CIP, "AES_192/ECB/NoPadding", P11Cipher, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
656 |
s("2.16.840.1.101.3.4.1.21", "OID.2.16.840.1.101.3.4.1.21"), |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
657 |
m(CKM_AES_ECB)); |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
658 |
d(CIP, "AES_256/ECB/NoPadding", P11Cipher, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
659 |
s("2.16.840.1.101.3.4.1.41", "OID.2.16.840.1.101.3.4.1.41"), |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
660 |
m(CKM_AES_ECB)); |
11509
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
661 |
d(CIP, "AES/ECB/PKCS5Padding", P11Cipher, s("AES"), |
289 | 662 |
m(CKM_AES_ECB)); |
8542 | 663 |
d(CIP, "AES/CTR/NoPadding", P11Cipher, |
664 |
m(CKM_AES_CTR)); |
|
11509
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
665 |
d(CIP, "Blowfish/CBC/NoPadding", P11Cipher, |
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
666 |
m(CKM_BLOWFISH_CBC)); |
02fb543bc69d
7033170: Cipher.getMaxAllowedKeyLength(String) throws NoSuchAlgorithmException
valeriep
parents:
10701
diff
changeset
|
667 |
d(CIP, "Blowfish/CBC/PKCS5Padding", P11Cipher, |
2 | 668 |
m(CKM_BLOWFISH_CBC)); |
669 |
||
670 |
// XXX RSA_X_509, RSA_OAEP not yet supported |
|
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
8542
diff
changeset
|
671 |
d(CIP, "RSA/ECB/PKCS1Padding", P11RSACipher, s("RSA"), |
2 | 672 |
m(CKM_RSA_PKCS)); |
8578
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
8542
diff
changeset
|
673 |
d(CIP, "RSA/ECB/NoPadding", P11RSACipher, |
f5d3509ad92b
6994008: PKCS11 should support "RSA" and "RSA/ECB/NoPadding" ciphers
valeriep
parents:
8542
diff
changeset
|
674 |
m(CKM_RSA_X_509)); |
2 | 675 |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
676 |
d(SIG, "RawDSA", P11Signature, s("NONEwithDSA"), |
2 | 677 |
m(CKM_DSA)); |
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
678 |
d(SIG, "DSA", P11Signature, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
679 |
s("SHA1withDSA", "1.3.14.3.2.13", "1.3.14.3.2.27", |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
680 |
"1.2.840.10040.4.3", "OID.1.2.840.10040.4.3"), |
2 | 681 |
m(CKM_DSA_SHA1, CKM_DSA)); |
682 |
d(SIG, "NONEwithECDSA", P11Signature, |
|
683 |
m(CKM_ECDSA)); |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
684 |
d(SIG, "SHA1withECDSA", P11Signature, |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
685 |
s("ECDSA", "1.2.840.10045.4.1", "OID.1.2.840.10045.4.1"), |
2 | 686 |
m(CKM_ECDSA_SHA1, CKM_ECDSA)); |
12685 | 687 |
d(SIG, "SHA224withECDSA", P11Signature, |
688 |
s("1.2.840.10045.4.3.1", "OID.1.2.840.10045.4.3.1"), |
|
689 |
m(CKM_ECDSA)); |
|
2 | 690 |
d(SIG, "SHA256withECDSA", P11Signature, |
12685 | 691 |
s("1.2.840.10045.4.3.2", "OID.1.2.840.10045.4.3.2"), |
2 | 692 |
m(CKM_ECDSA)); |
693 |
d(SIG, "SHA384withECDSA", P11Signature, |
|
12685 | 694 |
s("1.2.840.10045.4.3.3", "OID.1.2.840.10045.4.3.3"), |
2 | 695 |
m(CKM_ECDSA)); |
696 |
d(SIG, "SHA512withECDSA", P11Signature, |
|
12685 | 697 |
s("1.2.840.10045.4.3.4", "OID.1.2.840.10045.4.3.4"), |
2 | 698 |
m(CKM_ECDSA)); |
699 |
d(SIG, "MD2withRSA", P11Signature, |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
700 |
s("1.2.840.113549.1.1.2", "OID.1.2.840.113549.1.1.2"), |
2 | 701 |
m(CKM_MD2_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509)); |
702 |
d(SIG, "MD5withRSA", P11Signature, |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
703 |
s("1.2.840.113549.1.1.4", "OID.1.2.840.113549.1.1.4"), |
2 | 704 |
m(CKM_MD5_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509)); |
705 |
d(SIG, "SHA1withRSA", P11Signature, |
|
13672
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
706 |
s("1.2.840.113549.1.1.5", "OID.1.2.840.113549.1.1.5", |
604588823b5a
7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents:
12685
diff
changeset
|
707 |
"1.3.14.3.2.29"), |
2 | 708 |
m(CKM_SHA1_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509)); |
12685 | 709 |
d(SIG, "SHA224withRSA", P11Signature, |
710 |
s("1.2.840.113549.1.1.14", "OID.1.2.840.113549.1.1.14"), |
|
711 |
m(CKM_SHA224_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509)); |
|
2 | 712 |
d(SIG, "SHA256withRSA", P11Signature, |
12685 | 713 |
s("1.2.840.113549.1.1.11", "OID.1.2.840.113549.1.1.11"), |
2 | 714 |
m(CKM_SHA256_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509)); |
715 |
d(SIG, "SHA384withRSA", P11Signature, |
|
12685 | 716 |
s("1.2.840.113549.1.1.12", "OID.1.2.840.113549.1.1.12"), |
2 | 717 |
m(CKM_SHA384_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509)); |
718 |
d(SIG, "SHA512withRSA", P11Signature, |
|
12685 | 719 |
s("1.2.840.113549.1.1.13", "OID.1.2.840.113549.1.1.13"), |
2 | 720 |
m(CKM_SHA512_RSA_PKCS, CKM_RSA_PKCS, CKM_RSA_X_509)); |
721 |
||
7043 | 722 |
/* |
723 |
* TLS 1.2 uses a different hash algorithm than 1.0/1.1 for the |
|
724 |
* PRF calculations. As of 2010, there is no PKCS11-level |
|
725 |
* support for TLS 1.2 PRF calculations, and no known OS's have |
|
726 |
* an internal variant we could use. Therefore for TLS 1.2, we |
|
727 |
* are updating JSSE to request different provider algorithms |
|
728 |
* (e.g. "SunTls12Prf"), and currently only SunJCE has these |
|
729 |
* TLS 1.2 algorithms. |
|
730 |
* |
|
731 |
* If we reused the names such as "SunTlsPrf", the PKCS11 |
|
732 |
* providers would need be updated to fail correctly when |
|
733 |
* presented with the wrong version number (via |
|
734 |
* Provider.Service.supportsParameters()), and we would also |
|
735 |
* need to add the appropriate supportsParamters() checks into |
|
736 |
* KeyGenerators (not currently there). |
|
737 |
* |
|
738 |
* In the future, if PKCS11 support is added, we will restructure |
|
739 |
* this. |
|
740 |
*/ |
|
2596 | 741 |
d(KG, "SunTlsRsaPremasterSecret", |
742 |
"sun.security.pkcs11.P11TlsRsaPremasterSecretGenerator", |
|
2 | 743 |
m(CKM_SSL3_PRE_MASTER_KEY_GEN, CKM_TLS_PRE_MASTER_KEY_GEN)); |
2596 | 744 |
d(KG, "SunTlsMasterSecret", |
745 |
"sun.security.pkcs11.P11TlsMasterSecretGenerator", |
|
2 | 746 |
m(CKM_SSL3_MASTER_KEY_DERIVE, CKM_TLS_MASTER_KEY_DERIVE, |
2596 | 747 |
CKM_SSL3_MASTER_KEY_DERIVE_DH, |
748 |
CKM_TLS_MASTER_KEY_DERIVE_DH)); |
|
749 |
d(KG, "SunTlsKeyMaterial", |
|
750 |
"sun.security.pkcs11.P11TlsKeyMaterialGenerator", |
|
2 | 751 |
m(CKM_SSL3_KEY_AND_MAC_DERIVE, CKM_TLS_KEY_AND_MAC_DERIVE)); |
752 |
d(KG, "SunTlsPrf", "sun.security.pkcs11.P11TlsPrfGenerator", |
|
753 |
m(CKM_TLS_PRF, CKM_NSS_TLS_PRF_GENERAL)); |
|
754 |
} |
|
755 |
||
756 |
// background thread that periodically checks for token insertion |
|
757 |
// if no token is present. We need to do that in a separate thread because |
|
758 |
// the insertion check may block for quite a long time on some tokens. |
|
759 |
private static class TokenPoller implements Runnable { |
|
760 |
private final SunPKCS11 provider; |
|
761 |
private volatile boolean enabled; |
|
762 |
private TokenPoller(SunPKCS11 provider) { |
|
763 |
this.provider = provider; |
|
764 |
enabled = true; |
|
765 |
} |
|
766 |
public void run() { |
|
767 |
int interval = provider.config.getInsertionCheckInterval(); |
|
768 |
while (enabled) { |
|
769 |
try { |
|
770 |
Thread.sleep(interval); |
|
771 |
} catch (InterruptedException e) { |
|
772 |
break; |
|
773 |
} |
|
774 |
if (enabled == false) { |
|
775 |
break; |
|
776 |
} |
|
777 |
try { |
|
778 |
provider.initToken(null); |
|
779 |
} catch (PKCS11Exception e) { |
|
780 |
// ignore |
|
781 |
} |
|
782 |
} |
|
783 |
} |
|
784 |
void disable() { |
|
785 |
enabled = false; |
|
786 |
} |
|
787 |
} |
|
788 |
||
789 |
// create the poller thread, if not already active |
|
790 |
private void createPoller() { |
|
791 |
if (poller != null) { |
|
792 |
return; |
|
793 |
} |
|
794 |
TokenPoller poller = new TokenPoller(this); |
|
795 |
Thread t = new Thread(poller, "Poller " + getName()); |
|
796 |
t.setDaemon(true); |
|
797 |
t.setPriority(Thread.MIN_PRIORITY); |
|
798 |
t.start(); |
|
799 |
this.poller = poller; |
|
800 |
} |
|
801 |
||
802 |
// destroy the poller thread, if active |
|
803 |
private void destroyPoller() { |
|
804 |
if (poller != null) { |
|
805 |
poller.disable(); |
|
806 |
poller = null; |
|
807 |
} |
|
808 |
} |
|
809 |
||
810 |
private boolean hasValidToken() { |
|
487
df198b4170f7
6524501: inconsistency with PKCS#11 spec - 0-value flags in CK_SLOT_INFO struct returned by C_GetSlotInfo()
valeriep
parents:
289
diff
changeset
|
811 |
/* Commented out to work with Solaris softtoken impl which |
df198b4170f7
6524501: inconsistency with PKCS#11 spec - 0-value flags in CK_SLOT_INFO struct returned by C_GetSlotInfo()
valeriep
parents:
289
diff
changeset
|
812 |
returns 0-value flags, e.g. both REMOVABLE_DEVICE and |
df198b4170f7
6524501: inconsistency with PKCS#11 spec - 0-value flags in CK_SLOT_INFO struct returned by C_GetSlotInfo()
valeriep
parents:
289
diff
changeset
|
813 |
TOKEN_PRESENT are false, when it can't access the token. |
2 | 814 |
if (removable == false) { |
815 |
return true; |
|
816 |
} |
|
487
df198b4170f7
6524501: inconsistency with PKCS#11 spec - 0-value flags in CK_SLOT_INFO struct returned by C_GetSlotInfo()
valeriep
parents:
289
diff
changeset
|
817 |
*/ |
2 | 818 |
Token token = this.token; |
819 |
return (token != null) && token.isValid(); |
|
820 |
} |
|
821 |
||
822 |
// destroy the token. Called if we detect that it has been removed |
|
823 |
synchronized void uninitToken(Token token) { |
|
824 |
if (this.token != token) { |
|
825 |
// mismatch, our token must already be destroyed |
|
826 |
return; |
|
827 |
} |
|
828 |
destroyPoller(); |
|
829 |
this.token = null; |
|
830 |
// unregister all algorithms |
|
831 |
AccessController.doPrivileged(new PrivilegedAction<Object>() { |
|
832 |
public Object run() { |
|
833 |
clear(); |
|
834 |
return null; |
|
835 |
} |
|
836 |
}); |
|
837 |
createPoller(); |
|
838 |
} |
|
839 |
||
840 |
// test if a token is present and initialize this provider for it if so. |
|
841 |
// does nothing if no token is found |
|
842 |
// called from constructor and by poller |
|
843 |
private void initToken(CK_SLOT_INFO slotInfo) throws PKCS11Exception { |
|
844 |
if (slotInfo == null) { |
|
845 |
slotInfo = p11.C_GetSlotInfo(slotID); |
|
846 |
} |
|
487
df198b4170f7
6524501: inconsistency with PKCS#11 spec - 0-value flags in CK_SLOT_INFO struct returned by C_GetSlotInfo()
valeriep
parents:
289
diff
changeset
|
847 |
if (removable && (slotInfo.flags & CKF_TOKEN_PRESENT) == 0) { |
2 | 848 |
createPoller(); |
849 |
return; |
|
850 |
} |
|
851 |
destroyPoller(); |
|
852 |
boolean showInfo = config.getShowInfo(); |
|
853 |
if (showInfo) { |
|
854 |
System.out.println("Slot info for slot " + slotID + ":"); |
|
855 |
System.out.println(slotInfo); |
|
856 |
} |
|
857 |
final Token token = new Token(this); |
|
858 |
if (showInfo) { |
|
859 |
System.out.println |
|
860 |
("Token info for token in slot " + slotID + ":"); |
|
861 |
System.out.println(token.tokenInfo); |
|
862 |
} |
|
863 |
long[] supportedMechanisms = p11.C_GetMechanismList(slotID); |
|
2596 | 864 |
|
865 |
// Create a map from the various Descriptors to the "most |
|
866 |
// preferred" mechanism that was defined during the |
|
867 |
// static initialization. For example, DES/CBC/PKCS5Padding |
|
868 |
// could be mapped to CKM_DES_CBC_PAD or CKM_DES_CBC. Prefer |
|
869 |
// the earliest entry. When asked for "DES/CBC/PKCS5Padding", we |
|
870 |
// return a CKM_DES_CBC_PAD. |
|
2 | 871 |
final Map<Descriptor,Integer> supportedAlgs = |
872 |
new HashMap<Descriptor,Integer>(); |
|
873 |
for (int i = 0; i < supportedMechanisms.length; i++) { |
|
874 |
long longMech = supportedMechanisms[i]; |
|
875 |
boolean isEnabled = config.isEnabled(longMech); |
|
876 |
if (showInfo) { |
|
877 |
CK_MECHANISM_INFO mechInfo = |
|
878 |
p11.C_GetMechanismInfo(slotID, longMech); |
|
879 |
System.out.println("Mechanism " + |
|
880 |
Functions.getMechanismName(longMech) + ":"); |
|
881 |
if (isEnabled == false) { |
|
882 |
System.out.println("DISABLED in configuration"); |
|
883 |
} |
|
884 |
System.out.println(mechInfo); |
|
885 |
} |
|
886 |
if (isEnabled == false) { |
|
887 |
continue; |
|
888 |
} |
|
889 |
// we do not know of mechs with the upper 32 bits set |
|
890 |
if (longMech >>> 32 != 0) { |
|
891 |
continue; |
|
892 |
} |
|
893 |
int mech = (int)longMech; |
|
894 |
Integer integerMech = Integer.valueOf(mech); |
|
895 |
List<Descriptor> ds = descriptors.get(integerMech); |
|
896 |
if (ds == null) { |
|
897 |
continue; |
|
898 |
} |
|
899 |
for (Descriptor d : ds) { |
|
900 |
Integer oldMech = supportedAlgs.get(d); |
|
901 |
if (oldMech == null) { |
|
902 |
supportedAlgs.put(d, integerMech); |
|
903 |
continue; |
|
904 |
} |
|
2596 | 905 |
// See if there is something "more preferred" |
906 |
// than what we currently have in the supportedAlgs |
|
907 |
// map. |
|
2 | 908 |
int intOldMech = oldMech.intValue(); |
909 |
for (int j = 0; j < d.mechanisms.length; j++) { |
|
910 |
int nextMech = d.mechanisms[j]; |
|
911 |
if (mech == nextMech) { |
|
912 |
supportedAlgs.put(d, integerMech); |
|
913 |
break; |
|
914 |
} else if (intOldMech == nextMech) { |
|
915 |
break; |
|
916 |
} |
|
917 |
} |
|
918 |
} |
|
919 |
||
920 |
} |
|
921 |
||
922 |
// register algorithms in provider |
|
923 |
AccessController.doPrivileged(new PrivilegedAction<Object>() { |
|
924 |
public Object run() { |
|
925 |
for (Map.Entry<Descriptor,Integer> entry |
|
926 |
: supportedAlgs.entrySet()) { |
|
927 |
Descriptor d = entry.getKey(); |
|
928 |
int mechanism = entry.getValue().intValue(); |
|
929 |
Service s = d.service(token, mechanism); |
|
930 |
putService(s); |
|
931 |
} |
|
932 |
if (((token.tokenInfo.flags & CKF_RNG) != 0) |
|
933 |
&& config.isEnabled(PCKM_SECURERANDOM) |
|
934 |
&& !token.sessionManager.lowMaxSessions()) { |
|
935 |
// do not register SecureRandom if the token does |
|
936 |
// not support many sessions. if we did, we might |
|
937 |
// run out of sessions in the middle of a |
|
938 |
// nextBytes() call where we cannot fail over. |
|
939 |
putService(new P11Service(token, SR, "PKCS11", |
|
940 |
"sun.security.pkcs11.P11SecureRandom", null, |
|
941 |
PCKM_SECURERANDOM)); |
|
942 |
} |
|
943 |
if (config.isEnabled(PCKM_KEYSTORE)) { |
|
944 |
putService(new P11Service(token, KS, "PKCS11", |
|
945 |
"sun.security.pkcs11.P11KeyStore", |
|
946 |
s("PKCS11-" + config.getName()), |
|
947 |
PCKM_KEYSTORE)); |
|
948 |
} |
|
949 |
return null; |
|
950 |
} |
|
951 |
}); |
|
952 |
||
953 |
this.token = token; |
|
954 |
} |
|
955 |
||
956 |
private static final class P11Service extends Service { |
|
957 |
||
958 |
private final Token token; |
|
959 |
||
960 |
private final long mechanism; |
|
961 |
||
962 |
P11Service(Token token, String type, String algorithm, |
|
963 |
String className, String[] al, long mechanism) { |
|
964 |
super(token.provider, type, algorithm, className, toList(al), null); |
|
965 |
this.token = token; |
|
966 |
this.mechanism = mechanism & 0xFFFFFFFFL; |
|
967 |
} |
|
968 |
||
969 |
private static List<String> toList(String[] aliases) { |
|
970 |
return (aliases == null) ? null : Arrays.asList(aliases); |
|
971 |
} |
|
972 |
||
7043 | 973 |
public Object newInstance(Object param) |
974 |
throws NoSuchAlgorithmException { |
|
2 | 975 |
if (token.isValid() == false) { |
976 |
throw new NoSuchAlgorithmException("Token has been removed"); |
|
977 |
} |
|
978 |
try { |
|
979 |
return newInstance0(param); |
|
980 |
} catch (PKCS11Exception e) { |
|
981 |
throw new NoSuchAlgorithmException(e); |
|
982 |
} |
|
983 |
} |
|
984 |
||
985 |
public Object newInstance0(Object param) throws |
|
986 |
PKCS11Exception, NoSuchAlgorithmException { |
|
987 |
String algorithm = getAlgorithm(); |
|
988 |
String type = getType(); |
|
989 |
if (type == MD) { |
|
990 |
return new P11Digest(token, algorithm, mechanism); |
|
991 |
} else if (type == CIP) { |
|
992 |
if (algorithm.startsWith("RSA")) { |
|
993 |
return new P11RSACipher(token, algorithm, mechanism); |
|
994 |
} else { |
|
995 |
return new P11Cipher(token, algorithm, mechanism); |
|
996 |
} |
|
997 |
} else if (type == SIG) { |
|
998 |
return new P11Signature(token, algorithm, mechanism); |
|
999 |
} else if (type == MAC) { |
|
1000 |
return new P11Mac(token, algorithm, mechanism); |
|
1001 |
} else if (type == KPG) { |
|
1002 |
return new P11KeyPairGenerator(token, algorithm, mechanism); |
|
1003 |
} else if (type == KA) { |
|
1004 |
if (algorithm.equals("ECDH")) { |
|
1005 |
return new P11ECDHKeyAgreement(token, algorithm, mechanism); |
|
1006 |
} else { |
|
1007 |
return new P11KeyAgreement(token, algorithm, mechanism); |
|
1008 |
} |
|
1009 |
} else if (type == KF) { |
|
1010 |
return token.getKeyFactory(algorithm); |
|
1011 |
} else if (type == SKF) { |
|
1012 |
return new P11SecretKeyFactory(token, algorithm); |
|
1013 |
} else if (type == KG) { |
|
1014 |
// reference equality |
|
1015 |
if (algorithm == "SunTlsRsaPremasterSecret") { |
|
1016 |
return new P11TlsRsaPremasterSecretGenerator( |
|
1017 |
token, algorithm, mechanism); |
|
1018 |
} else if (algorithm == "SunTlsMasterSecret") { |
|
1019 |
return new P11TlsMasterSecretGenerator( |
|
1020 |
token, algorithm, mechanism); |
|
1021 |
} else if (algorithm == "SunTlsKeyMaterial") { |
|
1022 |
return new P11TlsKeyMaterialGenerator( |
|
1023 |
token, algorithm, mechanism); |
|
1024 |
} else if (algorithm == "SunTlsPrf") { |
|
1025 |
return new P11TlsPrfGenerator(token, algorithm, mechanism); |
|
1026 |
} else { |
|
1027 |
return new P11KeyGenerator(token, algorithm, mechanism); |
|
1028 |
} |
|
1029 |
} else if (type == SR) { |
|
1030 |
return token.getRandom(); |
|
1031 |
} else if (type == KS) { |
|
1032 |
return token.getKeyStore(); |
|
1033 |
} else if (type == AGP) { |
|
1034 |
return new sun.security.ec.ECParameters(); |
|
1035 |
} else { |
|
1036 |
throw new NoSuchAlgorithmException("Unknown type: " + type); |
|
1037 |
} |
|
1038 |
} |
|
1039 |
||
1040 |
public boolean supportsParameter(Object param) { |
|
1041 |
if ((param == null) || (token.isValid() == false)) { |
|
1042 |
return false; |
|
1043 |
} |
|
1044 |
if (param instanceof Key == false) { |
|
1045 |
throw new InvalidParameterException("Parameter must be a Key"); |
|
1046 |
} |
|
1047 |
String algorithm = getAlgorithm(); |
|
1048 |
String type = getType(); |
|
1049 |
Key key = (Key)param; |
|
1050 |
String keyAlgorithm = key.getAlgorithm(); |
|
1051 |
// RSA signatures and cipher |
|
1052 |
if (((type == CIP) && algorithm.startsWith("RSA")) |
|
1053 |
|| (type == SIG) && algorithm.endsWith("RSA")) { |
|
1054 |
if (keyAlgorithm.equals("RSA") == false) { |
|
1055 |
return false; |
|
1056 |
} |
|
1057 |
return isLocalKey(key) |
|
1058 |
|| (key instanceof RSAPrivateKey) |
|
1059 |
|| (key instanceof RSAPublicKey); |
|
1060 |
} |
|
1061 |
// EC |
|
1062 |
if (((type == KA) && algorithm.equals("ECDH")) |
|
1063 |
|| ((type == SIG) && algorithm.endsWith("ECDSA"))) { |
|
1064 |
if (keyAlgorithm.equals("EC") == false) { |
|
1065 |
return false; |
|
1066 |
} |
|
1067 |
return isLocalKey(key) |
|
1068 |
|| (key instanceof ECPrivateKey) |
|
1069 |
|| (key instanceof ECPublicKey); |
|
1070 |
} |
|
1071 |
// DSA signatures |
|
1072 |
if ((type == SIG) && algorithm.endsWith("DSA")) { |
|
1073 |
if (keyAlgorithm.equals("DSA") == false) { |
|
1074 |
return false; |
|
1075 |
} |
|
1076 |
return isLocalKey(key) |
|
1077 |
|| (key instanceof DSAPrivateKey) |
|
1078 |
|| (key instanceof DSAPublicKey); |
|
1079 |
} |
|
1080 |
// MACs and symmetric ciphers |
|
1081 |
if ((type == CIP) || (type == MAC)) { |
|
1082 |
// do not check algorithm name, mismatch is unlikely anyway |
|
1083 |
return isLocalKey(key) || "RAW".equals(key.getFormat()); |
|
1084 |
} |
|
1085 |
// DH key agreement |
|
1086 |
if (type == KA) { |
|
1087 |
if (keyAlgorithm.equals("DH") == false) { |
|
1088 |
return false; |
|
1089 |
} |
|
1090 |
return isLocalKey(key) |
|
1091 |
|| (key instanceof DHPrivateKey) |
|
1092 |
|| (key instanceof DHPublicKey); |
|
1093 |
} |
|
1094 |
// should not reach here, |
|
1095 |
// unknown engine type or algorithm |
|
1096 |
throw new AssertionError |
|
1097 |
("SunPKCS11 error: " + type + ", " + algorithm); |
|
1098 |
} |
|
1099 |
||
1100 |
private boolean isLocalKey(Key key) { |
|
1101 |
return (key instanceof P11Key) && (((P11Key)key).token == token); |
|
1102 |
} |
|
1103 |
||
1104 |
public String toString() { |
|
1105 |
return super.toString() + |
|
1106 |
" (" + Functions.getMechanismName(mechanism) + ")"; |
|
1107 |
} |
|
1108 |
||
1109 |
} |
|
1110 |
||
1111 |
/** |
|
1112 |
* Log in to this provider. |
|
1113 |
* |
|
1114 |
* <p> If the token expects a PIN to be supplied by the caller, |
|
1115 |
* the <code>handler</code> implementation must support |
|
1116 |
* a <code>PasswordCallback</code>. |
|
1117 |
* |
|
1118 |
* <p> To determine if the token supports a protected authentication path, |
|
1119 |
* the CK_TOKEN_INFO flag, CKF_PROTECTED_AUTHENTICATION_PATH, is consulted. |
|
1120 |
* |
|
1121 |
* @param subject this parameter is ignored |
|
1122 |
* @param handler the <code>CallbackHandler</code> used by |
|
1123 |
* this provider to communicate with the caller |
|
1124 |
* |
|
1125 |
* @exception LoginException if the login operation fails |
|
1126 |
* @exception SecurityException if the does not pass a security check for |
|
1127 |
* <code>SecurityPermission("authProvider.<i>name</i>")</code>, |
|
1128 |
* where <i>name</i> is the value returned by |
|
1129 |
* this provider's <code>getName</code> method |
|
1130 |
*/ |
|
1131 |
public void login(Subject subject, CallbackHandler handler) |
|
1132 |
throws LoginException { |
|
1133 |
||
1134 |
// security check |
|
1135 |
||
1136 |
SecurityManager sm = System.getSecurityManager(); |
|
1137 |
if (sm != null) { |
|
1138 |
if (debug != null) { |
|
1139 |
debug.println("checking login permission"); |
|
1140 |
} |
|
1141 |
sm.checkPermission(new SecurityPermission |
|
1142 |
("authProvider." + this.getName())); |
|
1143 |
} |
|
1144 |
||
1145 |
if (hasValidToken() == false) { |
|
1146 |
throw new LoginException("No token present"); |
|
1147 |
} |
|
1148 |
||
1149 |
// see if a login is required |
|
1150 |
||
1151 |
if ((token.tokenInfo.flags & CKF_LOGIN_REQUIRED) == 0) { |
|
1152 |
if (debug != null) { |
|
1153 |
debug.println("login operation not required for token - " + |
|
1154 |
"ignoring login request"); |
|
1155 |
} |
|
1156 |
return; |
|
1157 |
} |
|
1158 |
||
1159 |
// see if user already logged in |
|
1160 |
||
1161 |
try { |
|
1162 |
if (token.isLoggedInNow(null)) { |
|
1163 |
// user already logged in |
|
1164 |
if (debug != null) { |
|
1165 |
debug.println("user already logged in"); |
|
1166 |
} |
|
1167 |
return; |
|
1168 |
} |
|
1169 |
} catch (PKCS11Exception e) { |
|
1170 |
// ignore - fall thru and attempt login |
|
1171 |
} |
|
1172 |
||
1173 |
// get the pin if necessary |
|
1174 |
||
1175 |
char[] pin = null; |
|
1176 |
if ((token.tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == 0) { |
|
1177 |
||
1178 |
// get password |
|
1179 |
||
1180 |
CallbackHandler myHandler = getCallbackHandler(handler); |
|
1181 |
if (myHandler == null) { |
|
1182 |
// XXX PolicyTool is dependent on this message text |
|
1183 |
throw new LoginException |
|
1184 |
("no password provided, and no callback handler " + |
|
1185 |
"available for retrieving password"); |
|
1186 |
} |
|
1187 |
||
1188 |
java.text.MessageFormat form = new java.text.MessageFormat |
|
1189 |
(ResourcesMgr.getString |
|
7179
4afb81e50183
6987827: security/util/Resources.java needs improvement
weijun
parents:
7043
diff
changeset
|
1190 |
("PKCS11.Token.providerName.Password.")); |
2 | 1191 |
Object[] source = { getName() }; |
1192 |
||
1193 |
PasswordCallback pcall = new PasswordCallback(form.format(source), |
|
1194 |
false); |
|
1195 |
Callback[] callbacks = { pcall }; |
|
1196 |
try { |
|
1197 |
myHandler.handle(callbacks); |
|
1198 |
} catch (Exception e) { |
|
1199 |
LoginException le = new LoginException |
|
1200 |
("Unable to perform password callback"); |
|
1201 |
le.initCause(e); |
|
1202 |
throw le; |
|
1203 |
} |
|
1204 |
||
1205 |
pin = pcall.getPassword(); |
|
1206 |
pcall.clearPassword(); |
|
1207 |
if (pin == null) { |
|
1208 |
if (debug != null) { |
|
1209 |
debug.println("caller passed NULL pin"); |
|
1210 |
} |
|
1211 |
} |
|
1212 |
} |
|
1213 |
||
1214 |
// perform token login |
|
1215 |
||
1216 |
Session session = null; |
|
1217 |
try { |
|
1218 |
session = token.getOpSession(); |
|
1219 |
||
1220 |
// pin is NULL if using CKF_PROTECTED_AUTHENTICATION_PATH |
|
1221 |
p11.C_Login(session.id(), CKU_USER, pin); |
|
1222 |
if (debug != null) { |
|
1223 |
debug.println("login succeeded"); |
|
1224 |
} |
|
1225 |
} catch (PKCS11Exception pe) { |
|
1226 |
if (pe.getErrorCode() == CKR_USER_ALREADY_LOGGED_IN) { |
|
1227 |
// let this one go |
|
1228 |
if (debug != null) { |
|
1229 |
debug.println("user already logged in"); |
|
1230 |
} |
|
1231 |
return; |
|
1232 |
} else if (pe.getErrorCode() == CKR_PIN_INCORRECT) { |
|
1233 |
FailedLoginException fle = new FailedLoginException(); |
|
1234 |
fle.initCause(pe); |
|
1235 |
throw fle; |
|
1236 |
} else { |
|
1237 |
LoginException le = new LoginException(); |
|
1238 |
le.initCause(pe); |
|
1239 |
throw le; |
|
1240 |
} |
|
1241 |
} finally { |
|
1242 |
token.releaseSession(session); |
|
1243 |
if (pin != null) { |
|
1244 |
Arrays.fill(pin, ' '); |
|
1245 |
} |
|
1246 |
} |
|
1247 |
||
1248 |
// we do not store the PIN in the subject for now |
|
1249 |
} |
|
1250 |
||
1251 |
/** |
|
1252 |
* Log out from this provider |
|
1253 |
* |
|
1254 |
* @exception LoginException if the logout operation fails |
|
1255 |
* @exception SecurityException if the does not pass a security check for |
|
1256 |
* <code>SecurityPermission("authProvider.<i>name</i>")</code>, |
|
1257 |
* where <i>name</i> is the value returned by |
|
1258 |
* this provider's <code>getName</code> method |
|
1259 |
*/ |
|
1260 |
public void logout() throws LoginException { |
|
1261 |
||
1262 |
// security check |
|
1263 |
||
1264 |
SecurityManager sm = System.getSecurityManager(); |
|
1265 |
if (sm != null) { |
|
1266 |
sm.checkPermission |
|
1267 |
(new SecurityPermission("authProvider." + this.getName())); |
|
1268 |
} |
|
1269 |
||
1270 |
if (hasValidToken() == false) { |
|
1271 |
// app may call logout for cleanup, allow |
|
1272 |
return; |
|
1273 |
} |
|
1274 |
||
1275 |
if ((token.tokenInfo.flags & CKF_LOGIN_REQUIRED) == 0) { |
|
1276 |
if (debug != null) { |
|
1277 |
debug.println("logout operation not required for token - " + |
|
1278 |
"ignoring logout request"); |
|
1279 |
} |
|
1280 |
return; |
|
1281 |
} |
|
1282 |
||
1283 |
try { |
|
1284 |
if (token.isLoggedInNow(null) == false) { |
|
1285 |
if (debug != null) { |
|
1286 |
debug.println("user not logged in"); |
|
1287 |
} |
|
1288 |
return; |
|
1289 |
} |
|
1290 |
} catch (PKCS11Exception e) { |
|
1291 |
// ignore |
|
1292 |
} |
|
1293 |
||
1294 |
// perform token logout |
|
1295 |
||
1296 |
Session session = null; |
|
1297 |
try { |
|
1298 |
session = token.getOpSession(); |
|
1299 |
p11.C_Logout(session.id()); |
|
1300 |
if (debug != null) { |
|
1301 |
debug.println("logout succeeded"); |
|
1302 |
} |
|
1303 |
} catch (PKCS11Exception pe) { |
|
1304 |
if (pe.getErrorCode() == CKR_USER_NOT_LOGGED_IN) { |
|
1305 |
// let this one go |
|
1306 |
if (debug != null) { |
|
1307 |
debug.println("user not logged in"); |
|
1308 |
} |
|
1309 |
return; |
|
1310 |
} |
|
1311 |
LoginException le = new LoginException(); |
|
1312 |
le.initCause(pe); |
|
1313 |
throw le; |
|
1314 |
} finally { |
|
1315 |
token.releaseSession(session); |
|
1316 |
} |
|
1317 |
} |
|
1318 |
||
1319 |
/** |
|
1320 |
* Set a <code>CallbackHandler</code> |
|
1321 |
* |
|
1322 |
* <p> The provider uses this handler if one is not passed to the |
|
1323 |
* <code>login</code> method. The provider also uses this handler |
|
1324 |
* if it invokes <code>login</code> on behalf of callers. |
|
1325 |
* In either case if a handler is not set via this method, |
|
1326 |
* the provider queries the |
|
1327 |
* <i>auth.login.defaultCallbackHandler</i> security property |
|
1328 |
* for the fully qualified class name of a default handler implementation. |
|
1329 |
* If the security property is not set, |
|
1330 |
* the provider is assumed to have alternative means |
|
1331 |
* for obtaining authentication information. |
|
1332 |
* |
|
1333 |
* @param handler a <code>CallbackHandler</code> for obtaining |
|
1334 |
* authentication information, which may be <code>null</code> |
|
1335 |
* |
|
1336 |
* @exception SecurityException if the caller does not pass a |
|
1337 |
* security check for |
|
1338 |
* <code>SecurityPermission("authProvider.<i>name</i>")</code>, |
|
1339 |
* where <i>name</i> is the value returned by |
|
1340 |
* this provider's <code>getName</code> method |
|
1341 |
*/ |
|
1342 |
public void setCallbackHandler(CallbackHandler handler) { |
|
1343 |
||
1344 |
// security check |
|
1345 |
||
1346 |
SecurityManager sm = System.getSecurityManager(); |
|
1347 |
if (sm != null) { |
|
1348 |
sm.checkPermission |
|
1349 |
(new SecurityPermission("authProvider." + this.getName())); |
|
1350 |
} |
|
1351 |
||
1352 |
synchronized (LOCK_HANDLER) { |
|
1353 |
pHandler = handler; |
|
1354 |
} |
|
1355 |
} |
|
1356 |
||
1357 |
private CallbackHandler getCallbackHandler(CallbackHandler handler) { |
|
1358 |
||
1359 |
// get default handler if necessary |
|
1360 |
||
1361 |
if (handler != null) { |
|
1362 |
return handler; |
|
1363 |
} |
|
1364 |
||
1365 |
if (debug != null) { |
|
1366 |
debug.println("getting provider callback handler"); |
|
1367 |
} |
|
1368 |
||
1369 |
synchronized (LOCK_HANDLER) { |
|
1370 |
// see if handler was set via setCallbackHandler |
|
1371 |
if (pHandler != null) { |
|
1372 |
return pHandler; |
|
1373 |
} |
|
1374 |
||
1375 |
try { |
|
1376 |
if (debug != null) { |
|
1377 |
debug.println("getting default callback handler"); |
|
1378 |
} |
|
1379 |
||
1380 |
CallbackHandler myHandler = AccessController.doPrivileged |
|
1381 |
(new PrivilegedExceptionAction<CallbackHandler>() { |
|
1382 |
public CallbackHandler run() throws Exception { |
|
1383 |
||
1384 |
String defaultHandler = |
|
1385 |
java.security.Security.getProperty |
|
1386 |
("auth.login.defaultCallbackHandler"); |
|
1387 |
||
1388 |
if (defaultHandler == null || |
|
1389 |
defaultHandler.length() == 0) { |
|
1390 |
||
1391 |
// ok |
|
1392 |
if (debug != null) { |
|
1393 |
debug.println("no default handler set"); |
|
1394 |
} |
|
1395 |
return null; |
|
1396 |
} |
|
1397 |
||
10701
2c8dde454979
7090499: missing rawtypes warnings in anonymous inner class
chegar
parents:
8578
diff
changeset
|
1398 |
Class<?> c = Class.forName |
2c8dde454979
7090499: missing rawtypes warnings in anonymous inner class
chegar
parents:
8578
diff
changeset
|
1399 |
(defaultHandler, |
2c8dde454979
7090499: missing rawtypes warnings in anonymous inner class
chegar
parents:
8578
diff
changeset
|
1400 |
true, |
2c8dde454979
7090499: missing rawtypes warnings in anonymous inner class
chegar
parents:
8578
diff
changeset
|
1401 |
Thread.currentThread().getContextClassLoader()); |
2 | 1402 |
return (CallbackHandler)c.newInstance(); |
1403 |
} |
|
1404 |
}); |
|
1405 |
||
1406 |
// save it |
|
1407 |
pHandler = myHandler; |
|
1408 |
return myHandler; |
|
1409 |
||
1410 |
} catch (PrivilegedActionException pae) { |
|
1411 |
// ok |
|
1412 |
if (debug != null) { |
|
1413 |
debug.println("Unable to load default callback handler"); |
|
1414 |
pae.printStackTrace(); |
|
1415 |
} |
|
1416 |
} |
|
1417 |
} |
|
1418 |
return null; |
|
1419 |
} |
|
1420 |
||
1421 |
private Object writeReplace() throws ObjectStreamException { |
|
1422 |
return new SunPKCS11Rep(this); |
|
1423 |
} |
|
1424 |
||
1425 |
/** |
|
1426 |
* Serialized representation of the SunPKCS11 provider. |
|
1427 |
*/ |
|
1428 |
private static class SunPKCS11Rep implements Serializable { |
|
1429 |
||
1430 |
static final long serialVersionUID = -2896606995897745419L; |
|
1431 |
||
1432 |
private final String providerName; |
|
1433 |
||
1434 |
private final String configName; |
|
1435 |
||
1436 |
SunPKCS11Rep(SunPKCS11 provider) throws NotSerializableException { |
|
1437 |
providerName = provider.getName(); |
|
1438 |
configName = provider.configName; |
|
1439 |
if (Security.getProvider(providerName) != provider) { |
|
1440 |
throw new NotSerializableException("Only SunPKCS11 providers " |
|
1441 |
+ "installed in java.security.Security can be serialized"); |
|
1442 |
} |
|
1443 |
} |
|
1444 |
||
1445 |
private Object readResolve() throws ObjectStreamException { |
|
1446 |
SunPKCS11 p = (SunPKCS11)Security.getProvider(providerName); |
|
1447 |
if ((p == null) || (p.configName.equals(configName) == false)) { |
|
1448 |
throw new NotSerializableException("Could not find " |
|
1449 |
+ providerName + " in installed providers"); |
|
1450 |
} |
|
1451 |
return p; |
|
1452 |
} |
|
1453 |
} |
|
1454 |
} |