Showing the changes to java/pkg/sun/security/pkcs11/Secmod.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,760 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. | |
| 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. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle 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 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. | |
| 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.KeyStore.*; | |
| 33 | +import java.security.cert.X509Certificate; | |
| 34 | + | |
| 35 | +import sun.security.pkcs11.wrapper.*; | |
| 36 | +import static sun.security.pkcs11.wrapper.PKCS11Constants.*; | |
| 37 | + | |
| 38 | +/** | |
| 39 | + * The Secmod class defines the interface to the native NSS | |
| 40 | + * library and the configuration information it stores in its | |
| 41 | + * secmod.db file. | |
| 42 | + * | |
| 43 | + * <p>Example code: | |
| 44 | + * <pre> | |
| 45 | + * Secmod secmod = Secmod.getInstance(); | |
| 46 | + * if (secmod.isInitialized() == false) { | |
| 47 | + * secmod.initialize("/home/myself/.mozilla", "/usr/sfw/lib/mozilla"); | |
| 48 | + * } | |
| 49 | + * | |
| 50 | + * Provider p = secmod.getModule(ModuleType.KEYSTORE).getProvider(); | |
| 51 | + * KeyStore ks = KeyStore.getInstance("PKCS11", p); | |
| 52 | + * ks.load(null, password); | |
| 53 | + * </pre> | |
| 54 | + * | |
| 55 | + * @since 1.6 | |
| 56 | + * @author Andreas Sterbenz | |
| 57 | + */ | |
| 58 | +public final class Secmod { | |
| 59 | + | |
| 60 | + private final static boolean DEBUG = false; | |
| 61 | + | |
| 62 | + private final static Secmod INSTANCE; | |
| 63 | + | |
| 64 | + static { | |
| 65 | + sun.security.pkcs11.wrapper.PKCS11.loadNative(); | |
| 66 | + INSTANCE = new Secmod(); | |
| 67 | + } | |
| 68 | + | |
| 69 | + private final static String NSS_LIB_NAME = "nss3"; | |
| 70 | + | |
| 71 | + private final static String SOFTTOKEN_LIB_NAME = "softokn3"; | |
| 72 | + | |
| 73 | + private final static String TRUST_LIB_NAME = "nssckbi"; | |
| 74 | + | |
| 75 | + // handle to be passed to the native code, 0 means not initialized | |
| 76 | + private long nssHandle; | |
| 77 | + | |
| 78 | + // whether this is a supported version of NSS | |
| 79 | + private boolean supported; | |
| 80 | + | |
| 81 | + // list of the modules | |
| 82 | + private List<Module> modules; | |
| 83 | + | |
| 84 | + private String configDir; | |
| 85 | + | |
| 86 | + private String nssLibDir; | |
| 87 | + | |
| 88 | + private Secmod() { | |
| 89 | + // empty | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * Return the singleton Secmod instance. | |
| 94 | + */ | |
| 95 | + public static Secmod getInstance() { | |
| 96 | + return INSTANCE; | |
| 97 | + } | |
| 98 | + | |
| 99 | + private boolean isLoaded() { | |
| 100 | + if (nssHandle == 0) { | |
| 101 | + nssHandle = nssGetLibraryHandle(System.mapLibraryName(NSS_LIB_NAME)); | |
| 102 | + if (nssHandle != 0) { | |
| 103 | + fetchVersions(); | |
| 104 | + } | |
| 105 | + } | |
| 106 | + return (nssHandle != 0); | |
| 107 | + } | |
| 108 | + | |
| 109 | + private void fetchVersions() { | |
| 110 | + supported = nssVersionCheck(nssHandle, "3.7"); | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Test whether this Secmod has been initialized. Returns true | |
| 115 | + * if NSS has been initialized using either the initialize() method | |
| 116 | + * or by directly calling the native NSS APIs. The latter may be | |
| 117 | + * the case if the current process contains components that use | |
| 118 | + * NSS directly. | |
| 119 | + * | |
| 120 | + * @throws IOException if an incompatible version of NSS | |
| 121 | + * has been loaded | |
| 122 | + */ | |
| 123 | + public synchronized boolean isInitialized() throws IOException { | |
| 124 | + // NSS does not allow us to check if it is initialized already | |
| 125 | + // assume that if it is loaded it is also initialized | |
| 126 | + if (isLoaded() == false) { | |
| 127 | + return false; | |
| 128 | + } | |
| 129 | + if (supported == false) { | |
| 130 | + throw new IOException | |
| 131 | + ("An incompatible version of NSS is already loaded, " | |
| 132 | + + "3.7 or later required"); | |
| 133 | + } | |
| 134 | + return true; | |
| 135 | + } | |
| 136 | + | |
| 137 | + String getConfigDir() { | |
| 138 | + return configDir; | |
| 139 | + } | |
| 140 | + | |
| 141 | + String getLibDir() { | |
| 142 | + return nssLibDir; | |
| 143 | + } | |
| 144 | + | |
| 145 | + /** | |
| 146 | + * Initialize this Secmod. | |
| 147 | + * | |
| 148 | + * @param configDir the directory containing the NSS configuration | |
| 149 | + * files such as secmod.db | |
| 150 | + * @param nssLibDir the directory containing the NSS libraries | |
| 151 | + * (libnss3.so or nss3.dll) or null if the library is on | |
| 152 | + * the system default shared library path | |
| 153 | + * | |
| 154 | + * @throws IOException if NSS has already been initialized, | |
| 155 | + * the specified directories are invalid, or initialization | |
| 156 | + * fails for any other reason | |
| 157 | + */ | |
| 158 | + public void initialize(String configDir, String nssLibDir) | |
| 159 | + throws IOException { | |
| 160 | + initialize(DbMode.READ_WRITE, configDir, nssLibDir); | |
| 161 | + } | |
| 162 | + | |
| 163 | + public synchronized void initialize(DbMode dbMode, String configDir, String nssLibDir) | |
| 164 | + throws IOException { | |
| 165 | + if (isInitialized()) { | |
| 166 | + throw new IOException("NSS is already initialized"); | |
| 167 | + } | |
| 168 | + | |
| 169 | + if (dbMode == null) { | |
| 170 | + throw new NullPointerException(); | |
| 171 | + } | |
| 172 | + if ((dbMode != DbMode.NO_DB) && (configDir == null)) { | |
| 173 | + throw new NullPointerException(); | |
| 174 | + } | |
| 175 | + String platformLibName = System.mapLibraryName("nss3"); | |
| 176 | + String platformPath; | |
| 177 | + if (nssLibDir == null) { | |
| 178 | + platformPath = platformLibName; | |
| 179 | + } else { | |
| 180 | + File base = new File(nssLibDir); | |
| 181 | + if (base.isDirectory() == false) { | |
| 182 | + throw new IOException("nssLibDir must be a directory:" + nssLibDir); | |
| 183 | + } | |
| 184 | + File platformFile = new File(base, platformLibName); | |
| 185 | + if (platformFile.isFile() == false) { | |
| 186 | + throw new FileNotFoundException(platformFile.getPath()); | |
| 187 | + } | |
| 188 | + platformPath = platformFile.getPath(); | |
| 189 | + } | |
| 190 | + | |
| 191 | + if (configDir != null) { | |
| 192 | + File configBase = new File(configDir); | |
| 193 | + if (configBase.isDirectory() == false ) { | |
| 194 | + throw new IOException("configDir must be a directory: " + configDir); | |
| 195 | + } | |
| 196 | + File secmodFile = new File(configBase, "secmod.db"); | |
| 197 | + if (secmodFile.isFile() == false) { | |
| 198 | + throw new FileNotFoundException(secmodFile.getPath()); | |
| 199 | + } | |
| 200 | + } | |
| 201 | + | |
| 202 | + if (DEBUG) System.out.println("lib: " + platformPath); | |
| 203 | + nssHandle = nssLoadLibrary(platformPath); | |
| 204 | + if (DEBUG) System.out.println("handle: " + nssHandle); | |
| 205 | + fetchVersions(); | |
| 206 | + if (supported == false) { | |
| 207 | + throw new IOException | |
| 208 | + ("The specified version of NSS is incompatible, " | |
| 209 | + + "3.7 or later required"); | |
| 210 | + } | |
| 211 | + | |
| 212 | + if (DEBUG) System.out.println("dir: " + configDir); | |
| 213 | + boolean initok = nssInit(dbMode.functionName, nssHandle, configDir); | |
| 214 | + if (DEBUG) System.out.println("init: " + initok); | |
| 215 | + if (initok == false) { | |
| 216 | + throw new IOException("NSS initialization failed"); | |
| 217 | + } | |
| 218 | + | |
| 219 | + this.configDir = configDir; | |
| 220 | + this.nssLibDir = nssLibDir; | |
| 221 | + } | |
| 222 | + | |
| 223 | + /** | |
| 224 | + * Return an immutable list of all available modules. | |
| 225 | + * | |
| 226 | + * @throws IllegalStateException if this Secmod is misconfigured | |
| 227 | + * or not initialized | |
| 228 | + */ | |
| 229 | + public synchronized List<Module> getModules() { | |
| 230 | + try { | |
| 231 | + if (isInitialized() == false) { | |
| 232 | + throw new IllegalStateException("NSS not initialized"); | |
| 233 | + } | |
| 234 | + } catch (IOException e) { | |
| 235 | + // IOException if misconfigured | |
| 236 | + throw new IllegalStateException(e); | |
| 237 | + } | |
| 238 | + if (modules == null) { | |
| 239 | + List<Module> modules = (List<Module>)nssGetModuleList(nssHandle, | |
| 240 | + nssLibDir); | |
| 241 | + this.modules = Collections.unmodifiableList(modules); | |
| 242 | + } | |
| 243 | + return modules; | |
| 244 | + } | |
| 245 | + | |
| 246 | + private static byte[] getDigest(X509Certificate cert, String algorithm) { | |
| 247 | + try { | |
| 248 | + MessageDigest md = MessageDigest.getInstance(algorithm); | |
| 249 | + return md.digest(cert.getEncoded()); | |
| 250 | + } catch (GeneralSecurityException e) { | |
| 251 | + throw new ProviderException(e); | |
| 252 | + } | |
| 253 | + } | |
| 254 | + | |
| 255 | + boolean isTrusted(X509Certificate cert, TrustType trustType) { | |
| 256 | + Bytes bytes = new Bytes(getDigest(cert, "SHA-1")); | |
| 257 | + TrustAttributes attr = getModuleTrust(ModuleType.KEYSTORE, bytes); | |
| 258 | + if (attr == null) { | |
| 259 | + attr = getModuleTrust(ModuleType.FIPS, bytes); | |
| 260 | + if (attr == null) { | |
| 261 | + attr = getModuleTrust(ModuleType.TRUSTANCHOR, bytes); | |
| 262 | + } | |
| 263 | + } | |
| 264 | + return (attr == null) ? false : attr.isTrusted(trustType); | |
| 265 | + } | |
| 266 | + | |
| 267 | + private TrustAttributes getModuleTrust(ModuleType type, Bytes bytes) { | |
| 268 | + Module module = getModule(type); | |
| 269 | + TrustAttributes t = (module == null) ? null : module.getTrust(bytes); | |
| 270 | + return t; | |
| 271 | + } | |
| 272 | + | |
| 273 | + /** | |
| 274 | + * Constants describing the different types of NSS modules. | |
| 275 | + * For this API, NSS modules are classified as either one | |
| 276 | + * of the internal modules delivered as part of NSS or | |
| 277 | + * as an external module provided by a 3rd party. | |
| 278 | + */ | |
| 279 | + public static enum ModuleType { | |
| 280 | + /** | |
| 281 | + * The NSS Softtoken crypto module. This is the first | |
| 282 | + * slot of the softtoken object. | |
| 283 | + * This module provides | |
| 284 | + * implementations for cryptographic algorithms but no KeyStore. | |
| 285 | + */ | |
| 286 | + CRYPTO, | |
| 287 | + /** | |
| 288 | + * The NSS Softtoken KeyStore module. This is the second | |
| 289 | + * slot of the softtoken object. | |
| 290 | + * This module provides | |
| 291 | + * implementations for cryptographic algorithms (after login) | |
| 292 | + * and the KeyStore. | |
| 293 | + */ | |
| 294 | + KEYSTORE, | |
| 295 | + /** | |
| 296 | + * The NSS Softtoken module in FIPS mode. Note that in FIPS mode the | |
| 297 | + * softtoken presents only one slot, not separate CRYPTO and KEYSTORE | |
| 298 | + * slots as in non-FIPS mode. | |
| 299 | + */ | |
| 300 | + FIPS, | |
| 301 | + /** | |
| 302 | + * The NSS builtin trust anchor module. This is the | |
| 303 | + * NSSCKBI object. It provides no crypto functions. | |
| 304 | + */ | |
| 305 | + TRUSTANCHOR, | |
| 306 | + /** | |
| 307 | + * An external module. | |
| 308 | + */ | |
| 309 | + EXTERNAL, | |
| 310 | + } | |
| 311 | + | |
| 312 | + /** | |
| 313 | + * Returns the first module of the specified type. If no such | |
| 314 | + * module exists, this method returns null. | |
| 315 | + * | |
| 316 | + * @throws IllegalStateException if this Secmod is misconfigured | |
| 317 | + * or not initialized | |
| 318 | + */ | |
| 319 | + public Module getModule(ModuleType type) { | |
| 320 | + for (Module module : getModules()) { | |
| 321 | + if (module.getType() == type) { | |
| 322 | + return module; | |
| 323 | + } | |
| 324 | + } | |
| 325 | + return null; | |
| 326 | + } | |
| 327 | + | |
| 328 | + static final String TEMPLATE_EXTERNAL = | |
| 329 | + "library = %s\n" | |
| 330 | + + "name = \"%s\"\n" | |
| 331 | + + "slotListIndex = %d\n"; | |
| 332 | + | |
| 333 | + static final String TEMPLATE_TRUSTANCHOR = | |
| 334 | + "library = %s\n" | |
| 335 | + + "name = \"NSS Trust Anchors\"\n" | |
| 336 | + + "slotListIndex = 0\n" | |
| 337 | + + "enabledMechanisms = { KeyStore }\n" | |
| 338 | + + "nssUseSecmodTrust = true\n"; | |
| 339 | + | |
| 340 | + static final String TEMPLATE_CRYPTO = | |
| 341 | + "library = %s\n" | |
| 342 | + + "name = \"NSS SoftToken Crypto\"\n" | |
| 343 | + + "slotListIndex = 0\n" | |
| 344 | + + "disabledMechanisms = { KeyStore }\n"; | |
| 345 | + | |
| 346 | + static final String TEMPLATE_KEYSTORE = | |
| 347 | + "library = %s\n" | |
| 348 | + + "name = \"NSS SoftToken KeyStore\"\n" | |
| 349 | + + "slotListIndex = 1\n" | |
| 350 | + + "nssUseSecmodTrust = true\n"; | |
| 351 | + | |
| 352 | + static final String TEMPLATE_FIPS = | |
| 353 | + "library = %s\n" | |
| 354 | + + "name = \"NSS FIPS SoftToken\"\n" | |
| 355 | + + "slotListIndex = 0\n" | |
| 356 | + + "nssUseSecmodTrust = true\n"; | |
| 357 | + | |
| 358 | + /** | |
| 359 | + * A representation of one PKCS#11 slot in a PKCS#11 module. | |
| 360 | + */ | |
| 361 | + public static final class Module { | |
| 362 | + // path of the native library | |
| 363 | + final String libraryName; | |
| 364 | + // descriptive name used by NSS | |
| 365 | + final String commonName; | |
| 366 | + final int slot; | |
| 367 | + final ModuleType type; | |
| 368 | + | |
| 369 | + private String config; | |
| 370 | + private SunPKCS11 provider; | |
| 371 | + | |
| 372 | + // trust attributes. Used for the KEYSTORE and TRUSTANCHOR modules only | |
| 373 | + private Map<Bytes,TrustAttributes> trust; | |
| 374 | + | |
| 375 | + Module(String libraryDir, String libraryName, String commonName, | |
| 376 | + boolean fips, int slot) { | |
| 377 | + ModuleType type; | |
| 378 | + | |
| 379 | + if ((libraryName == null) || (libraryName.length() == 0)) { | |
| 380 | + // must be softtoken | |
| 381 | + libraryName = System.mapLibraryName(SOFTTOKEN_LIB_NAME); | |
| 382 | + if (fips == false) { | |
| 383 | + type = (slot == 0) ? ModuleType.CRYPTO : ModuleType.KEYSTORE; | |
| 384 | + } else { | |
| 385 | + type = ModuleType.FIPS; | |
| 386 | + if (slot != 0) { | |
| 387 | + throw new RuntimeException | |
| 388 | + ("Slot index should be 0 for FIPS slot"); | |
| 389 | + } | |
| 390 | + } | |
| 391 | + } else { | |
| 392 | + if (libraryName.endsWith(System.mapLibraryName(TRUST_LIB_NAME)) | |
| 393 | + || commonName.equals("Builtin Roots Module")) { | |
| 394 | + type = ModuleType.TRUSTANCHOR; | |
| 395 | + } else { | |
| 396 | + type = ModuleType.EXTERNAL; | |
| 397 | + } | |
| 398 | + if (fips) { | |
| 399 | + throw new RuntimeException("FIPS flag set for non-internal " | |
| 400 | + + "module: " + libraryName + ", " + commonName); | |
| 401 | + } | |
| 402 | + } | |
| 403 | + this.libraryName = (new File(libraryDir, libraryName)).getPath(); | |
| 404 | + this.commonName = commonName; | |
| 405 | + this.slot = slot; | |
| 406 | + this.type = type; | |
| 407 | + initConfiguration(); | |
| 408 | + } | |
| 409 | + | |
| 410 | + private void initConfiguration() { | |
| 411 | + switch (type) { | |
| 412 | + case EXTERNAL: | |
| 413 | + config = String.format(TEMPLATE_EXTERNAL, libraryName, | |
| 414 | + commonName + " " + slot, slot); | |
| 415 | + break; | |
| 416 | + case CRYPTO: | |
| 417 | + config = String.format(TEMPLATE_CRYPTO, libraryName); | |
| 418 | + break; | |
| 419 | + case KEYSTORE: | |
| 420 | + config = String.format(TEMPLATE_KEYSTORE, libraryName); | |
| 421 | + break; | |
| 422 | + case FIPS: | |
| 423 | + config = String.format(TEMPLATE_FIPS, libraryName); | |
| 424 | + break; | |
| 425 | + case TRUSTANCHOR: | |
| 426 | + config = String.format(TEMPLATE_TRUSTANCHOR, libraryName); | |
| 427 | + break; | |
| 428 | + default: | |
| 429 | + throw new RuntimeException("Unknown module type: " + type); | |
| 430 | + } | |
| 431 | + } | |
| 432 | + | |
| 433 | + /** | |
| 434 | + * Get the configuration for this module. This is a string | |
| 435 | + * in the SunPKCS11 configuration format. It can be | |
| 436 | + * customized with additional options and then made | |
| 437 | + * current using the setConfiguration() method. | |
| 438 | + */ | |
| 439 | + @Deprecated | |
| 440 | + public synchronized String getConfiguration() { | |
| 441 | + return config; | |
| 442 | + } | |
| 443 | + | |
| 444 | + /** | |
| 445 | + * Set the configuration for this module. | |
| 446 | + * | |
| 447 | + * @throws IllegalStateException if the associated provider | |
| 448 | + * instance has already been created. | |
| 449 | + */ | |
| 450 | + @Deprecated | |
| 451 | + public synchronized void setConfiguration(String config) { | |
| 452 | + if (provider != null) { | |
| 453 | + throw new IllegalStateException("Provider instance already created"); | |
| 454 | + } | |
| 455 | + this.config = config; | |
| 456 | + } | |
| 457 | + | |
| 458 | + /** | |
| 459 | + * Return the pathname of the native library that implements | |
| 460 | + * this module. For example, /usr/lib/libpkcs11.so. | |
| 461 | + */ | |
| 462 | + public String getLibraryName() { | |
| 463 | + return libraryName; | |
| 464 | + } | |
| 465 | + | |
| 466 | + /** | |
| 467 | + * Returns the type of this module. | |
| 468 | + */ | |
| 469 | + public ModuleType getType() { | |
| 470 | + return type; | |
| 471 | + } | |
| 472 | + | |
| 473 | + /** | |
| 474 | + * Returns the provider instance that is associated with this | |
| 475 | + * module. The first call to this method creates the provider | |
| 476 | + * instance. | |
| 477 | + */ | |
| 478 | + @Deprecated | |
| 479 | + public synchronized Provider getProvider() { | |
| 480 | + if (provider == null) { | |
| 481 | + provider = newProvider(); | |
| 482 | + } | |
| 483 | + return provider; | |
| 484 | + } | |
| 485 | + | |
| 486 | + synchronized boolean hasInitializedProvider() { | |
| 487 | + return provider != null; | |
| 488 | + } | |
| 489 | + | |
| 490 | + void setProvider(SunPKCS11 p) { | |
| 491 | + if (provider != null) { | |
| 492 | + throw new ProviderException("Secmod provider already initialized"); | |
| 493 | + } | |
| 494 | + provider = p; | |
| 495 | + } | |
| 496 | + | |
| 497 | + private SunPKCS11 newProvider() { | |
| 498 | + try { | |
| 499 | + InputStream in = new ByteArrayInputStream(config.getBytes("UTF8")); | |
| 500 | + return new SunPKCS11(in); | |
| 501 | + } catch (Exception e) { | |
| 502 | + // XXX | |
| 503 | + throw new ProviderException(e); | |
| 504 | + } | |
| 505 | + } | |
| 506 | + | |
| 507 | + synchronized void setTrust(Token token, X509Certificate cert) { | |
| 508 | + Bytes bytes = new Bytes(getDigest(cert, "SHA-1")); | |
| 509 | + TrustAttributes attr = getTrust(bytes); | |
| 510 | + if (attr == null) { | |
| 511 | + attr = new TrustAttributes(token, cert, bytes, CKT_NETSCAPE_TRUSTED_DELEGATOR); | |
| 512 | + trust.put(bytes, attr); | |
| 513 | + } else { | |
| 514 | + // does it already have the correct trust settings? | |
| 515 | + if (attr.isTrusted(TrustType.ALL) == false) { | |
| 516 | + // XXX not yet implemented | |
| 517 | + throw new ProviderException("Cannot change existing trust attributes"); | |
| 518 | + } | |
| 519 | + } | |
| 520 | + } | |
| 521 | + | |
| 522 | + TrustAttributes getTrust(Bytes hash) { | |
| 523 | + if (trust == null) { | |
| 524 | + // If provider is not set, create a temporary provider to | |
| 525 | + // retrieve the trust information. This can happen if we need | |
| 526 | + // to get the trust information for the trustanchor module | |
| 527 | + // because we need to look for user customized settings in the | |
| 528 | + // keystore module (which may not have a provider created yet). | |
| 529 | + // Creating a temporary provider and then dropping it on the | |
| 530 | + // floor immediately is flawed, but it's the best we can do | |
| 531 | + // for now. | |
| 532 | + synchronized (this) { | |
| 533 | + SunPKCS11 p = provider; | |
| 534 | + if (p == null) { | |
| 535 | + p = newProvider(); | |
| 536 | + } | |
| 537 | + try { | |
| 538 | + trust = Secmod.getTrust(p); | |
| 539 | + } catch (PKCS11Exception e) { | |
| 540 | + throw new RuntimeException(e); | |
| 541 | + } | |
| 542 | + } | |
| 543 | + } | |
| 544 | + return trust.get(hash); | |
| 545 | + } | |
| 546 | + | |
| 547 | + public String toString() { | |
| 548 | + return | |
| 549 | + commonName + " (" + type + ", " + libraryName + ", slot " + slot + ")"; | |
| 550 | + } | |
| 551 | + | |
| 552 | + } | |
| 553 | + | |
| 554 | + /** | |
| 555 | + * Constants representing NSS trust categories. | |
| 556 | + */ | |
| 557 | + public static enum TrustType { | |
| 558 | + /** Trusted for all purposes */ | |
| 559 | + ALL, | |
| 560 | + /** Trusted for SSL client authentication */ | |
| 561 | + CLIENT_AUTH, | |
| 562 | + /** Trusted for SSL server authentication */ | |
| 563 | + SERVER_AUTH, | |
| 564 | + /** Trusted for code signing */ | |
| 565 | + CODE_SIGNING, | |
| 566 | + /** Trusted for email protection */ | |
| 567 | + EMAIL_PROTECTION, | |
| 568 | + } | |
| 569 | + | |
| 570 | + public static enum DbMode { | |
| 571 | + READ_WRITE("NSS_InitReadWrite"), | |
| 572 | + READ_ONLY ("NSS_Init"), | |
| 573 | + NO_DB ("NSS_NoDB_Init"); | |
| 574 | + | |
| 575 | + final String functionName; | |
| 576 | + DbMode(String functionName) { | |
| 577 | + this.functionName = functionName; | |
| 578 | + } | |
| 579 | + } | |
| 580 | + | |
| 581 | + /** | |
| 582 | + * A LoadStoreParameter for use with the NSS Softtoken or | |
| 583 | + * NSS TrustAnchor KeyStores. | |
| 584 | + * <p> | |
| 585 | + * It allows the set of trusted certificates that are returned by | |
| 586 | + * the KeyStore to be specified. | |
| 587 | + */ | |
| 588 | + public static final class KeyStoreLoadParameter implements LoadStoreParameter { | |
| 589 | + final TrustType trustType; | |
| 590 | + final ProtectionParameter protection; | |
| 591 | + public KeyStoreLoadParameter(TrustType trustType, char[] password) { | |
| 592 | + this(trustType, new PasswordProtection(password)); | |
| 593 | + | |
| 594 | + } | |
| 595 | + public KeyStoreLoadParameter(TrustType trustType, ProtectionParameter prot) { | |
| 596 | + if (trustType == null) { | |
| 597 | + throw new NullPointerException("trustType must not be null"); | |
| 598 | + } | |
| 599 | + this.trustType = trustType; | |
| 600 | + this.protection = prot; | |
| 601 | + } | |
| 602 | + public ProtectionParameter getProtectionParameter() { | |
| 603 | + return protection; | |
| 604 | + } | |
| 605 | + public TrustType getTrustType() { | |
| 606 | + return trustType; | |
| 607 | + } | |
| 608 | + } | |
| 609 | + | |
| 610 | + static class TrustAttributes { | |
| 611 | + final long handle; | |
| 612 | + final long clientAuth, serverAuth, codeSigning, emailProtection; | |
| 613 | + final byte[] shaHash; | |
| 614 | + TrustAttributes(Token token, X509Certificate cert, Bytes bytes, long trustValue) { | |
| 615 | + Session session = null; | |
| 616 | + try { | |
| 617 | + session = token.getOpSession(); | |
| 618 | + // XXX use KeyStore TrustType settings to determine which | |
| 619 | + // attributes to set | |
| 620 | + CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] { | |
| 621 | + new CK_ATTRIBUTE(CKA_TOKEN, true), | |
| 622 | + new CK_ATTRIBUTE(CKA_CLASS, CKO_NETSCAPE_TRUST), | |
| 623 | + new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_SERVER_AUTH, trustValue), | |
| 624 | + new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CODE_SIGNING, trustValue), | |
| 625 | + new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_EMAIL_PROTECTION, trustValue), | |
| 626 | + new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CLIENT_AUTH, trustValue), | |
| 627 | + new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_SHA1_HASH, bytes.b), | |
| 628 | + new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_MD5_HASH, getDigest(cert, "MD5")), | |
| 629 | + new CK_ATTRIBUTE(CKA_ISSUER, cert.getIssuerX500Principal().getEncoded()), | |
| 630 | + new CK_ATTRIBUTE(CKA_SERIAL_NUMBER, cert.getSerialNumber().toByteArray()), | |
| 631 | + // XXX per PKCS#11 spec, the serial number should be in ASN.1 | |
| 632 | + }; | |
| 633 | + handle = token.p11.C_CreateObject(session.id(), attrs); | |
| 634 | + shaHash = bytes.b; | |
| 635 | + clientAuth = trustValue; | |
| 636 | + serverAuth = trustValue; | |
| 637 | + codeSigning = trustValue; | |
| 638 | + emailProtection = trustValue; | |
| 639 | + } catch (PKCS11Exception e) { | |
| 640 | + throw new ProviderException("Could not create trust object", e); | |
| 641 | + } finally { | |
| 642 | + token.releaseSession(session); | |
| 643 | + } | |
| 644 | + } | |
| 645 | + TrustAttributes(Token token, Session session, long handle) | |
| 646 | + throws PKCS11Exception { | |
| 647 | + this.handle = handle; | |
| 648 | + CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] { | |
| 649 | + new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_SERVER_AUTH), | |
| 650 | + new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CODE_SIGNING), | |
| 651 | + new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_EMAIL_PROTECTION), | |
| 652 | + new CK_ATTRIBUTE(CKA_NETSCAPE_CERT_SHA1_HASH), | |
| 653 | + }; | |
| 654 | + | |
| 655 | + token.p11.C_GetAttributeValue(session.id(), handle, attrs); | |
| 656 | + serverAuth = attrs[0].getLong(); | |
| 657 | + codeSigning = attrs[1].getLong(); | |
| 658 | + emailProtection = attrs[2].getLong(); | |
| 659 | + shaHash = attrs[3].getByteArray(); | |
| 660 | + | |
| 661 | + attrs = new CK_ATTRIBUTE[] { | |
| 662 | + new CK_ATTRIBUTE(CKA_NETSCAPE_TRUST_CLIENT_AUTH), | |
| 663 | + }; | |
| 664 | + long c; | |
| 665 | + try { | |
| 666 | + token.p11.C_GetAttributeValue(session.id(), handle, attrs); | |
| 667 | + c = attrs[0].getLong(); | |
| 668 | + } catch (PKCS11Exception e) { | |
| 669 | + // trust anchor module does not support this attribute | |
| 670 | + c = serverAuth; | |
| 671 | + } | |
| 672 | + clientAuth = c; | |
| 673 | + } | |
| 674 | + Bytes getHash() { | |
| 675 | + return new Bytes(shaHash); | |
| 676 | + } | |
| 677 | + boolean isTrusted(TrustType type) { | |
| 678 | + switch (type) { | |
| 679 | + case CLIENT_AUTH: | |
| 680 | + return isTrusted(clientAuth); | |
| 681 | + case SERVER_AUTH: | |
| 682 | + return isTrusted(serverAuth); | |
| 683 | + case CODE_SIGNING: | |
| 684 | + return isTrusted(codeSigning); | |
| 685 | + case EMAIL_PROTECTION: | |
| 686 | + return isTrusted(emailProtection); | |
| 687 | + case ALL: | |
| 688 | + return isTrusted(TrustType.CLIENT_AUTH) | |
| 689 | + && isTrusted(TrustType.SERVER_AUTH) | |
| 690 | + && isTrusted(TrustType.CODE_SIGNING) | |
| 691 | + && isTrusted(TrustType.EMAIL_PROTECTION); | |
| 692 | + default: | |
| 693 | + return false; | |
| 694 | + } | |
| 695 | + } | |
| 696 | + | |
| 697 | + private boolean isTrusted(long l) { | |
| 698 | + // XXX CKT_TRUSTED? | |
| 699 | + return (l == CKT_NETSCAPE_TRUSTED_DELEGATOR); | |
| 700 | + } | |
| 701 | + | |
| 702 | + } | |
| 703 | + | |
| 704 | + private static class Bytes { | |
| 705 | + final byte[] b; | |
| 706 | + Bytes(byte[] b) { | |
| 707 | + this.b = b; | |
| 708 | + } | |
| 709 | + public int hashCode() { | |
| 710 | + return Arrays.hashCode(b); | |
| 711 | + } | |
| 712 | + public boolean equals(Object o) { | |
| 713 | + if (this == o) { | |
| 714 | + return true; | |
| 715 | + } | |
| 716 | + if (o instanceof Bytes == false) { | |
| 717 | + return false; | |
| 718 | + } | |
| 719 | + Bytes other = (Bytes)o; | |
| 720 | + return Arrays.equals(this.b, other.b); | |
| 721 | + } | |
| 722 | + } | |
| 723 | + | |
| 724 | + private static Map<Bytes,TrustAttributes> getTrust(SunPKCS11 provider) | |
| 725 | + throws PKCS11Exception { | |
| 726 | + Map<Bytes,TrustAttributes> trustMap = new HashMap<Bytes,TrustAttributes>(); | |
| 727 | + Token token = provider.getToken(); | |
| 728 | + Session session = null; | |
| 729 | + try { | |
| 730 | + session = token.getOpSession(); | |
| 731 | + int MAX_NUM = 8192; | |
| 732 | + CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] { | |
| 733 | + new CK_ATTRIBUTE(CKA_CLASS, CKO_NETSCAPE_TRUST), | |
| 734 | + }; | |
| 735 | + token.p11.C_FindObjectsInit(session.id(), attrs); | |
| 736 | + long[] handles = token.p11.C_FindObjects(session.id(), MAX_NUM); | |
| 737 | + token.p11.C_FindObjectsFinal(session.id()); | |
| 738 | + if (DEBUG) System.out.println("handles: " + handles.length); | |
| 739 | + | |
| 740 | + for (long handle : handles) { | |
| 741 | + TrustAttributes trust = new TrustAttributes(token, session, handle); | |
| 742 | + trustMap.put(trust.getHash(), trust); | |
| 743 | + } | |
| 744 | + } finally { | |
| 745 | + token.releaseSession(session); | |
| 746 | + } | |
| 747 | + return trustMap; | |
| 748 | + } | |
| 749 | + | |
| 750 | + private static native long nssGetLibraryHandle(String libraryName); | |
| 751 | + | |
| 752 | + private static native long nssLoadLibrary(String name) throws IOException; | |
| 753 | + | |
| 754 | + private static native boolean nssVersionCheck(long handle, String minVersion); | |
| 755 | + | |
| 756 | + private static native boolean nssInit(String functionName, long handle, String configDir); | |
| 757 | + | |
| 758 | + private static native Object nssGetModuleList(long handle, String libDir); | |
| 759 | + | |
| 760 | +} | |