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