1 7 8 package java.security; 9 10 import java.util.*; 11 12 import java.security.Provider.Service; 13 import java.security.spec.KeySpec ; 14 import java.security.spec.InvalidKeySpecException ; 15 16 import sun.security.util.Debug; 17 import sun.security.jca.*; 18 import sun.security.jca.GetInstance.Instance; 19 20 65 66 public class KeyFactory { 67 68 private static final Debug debug = 69 Debug.getInstance("jca", "KeyFactory"); 70 71 private final String algorithm; 73 74 private Provider provider; 76 77 private volatile KeyFactorySpi spi; 79 80 private final Object lock = new Object (); 82 83 private Iterator<Service> serviceIterator; 86 87 95 protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider, 96 String algorithm) { 97 this.spi = keyFacSpi; 98 this.provider = provider; 99 this.algorithm = algorithm; 100 } 101 102 private KeyFactory(String algorithm) throws NoSuchAlgorithmException { 103 this.algorithm = algorithm; 104 List<Service> list = GetInstance.getServices("KeyFactory", algorithm); 105 serviceIterator = list.iterator(); 106 if (nextSpi(null) == null) { 108 throw new NoSuchAlgorithmException 109 (algorithm + " KeyFactory not available"); 110 } 111 } 112 113 133 public static KeyFactory getInstance(String algorithm) 134 throws NoSuchAlgorithmException { 135 return new KeyFactory (algorithm); 136 } 137 138 163 public static KeyFactory getInstance(String algorithm, String provider) 164 throws NoSuchAlgorithmException , NoSuchProviderException { 165 Instance instance = GetInstance.getInstance("KeyFactory", 166 KeyFactorySpi .class, algorithm, provider); 167 return new KeyFactory ((KeyFactorySpi )instance.impl, 168 instance.provider, algorithm); 169 } 170 171 196 public static KeyFactory getInstance(String algorithm, Provider provider) 197 throws NoSuchAlgorithmException { 198 Instance instance = GetInstance.getInstance("KeyFactory", 199 KeyFactorySpi .class, algorithm, provider); 200 return new KeyFactory ((KeyFactorySpi )instance.impl, 201 instance.provider, algorithm); 202 } 203 204 209 public final Provider getProvider() { 210 synchronized (lock) { 211 serviceIterator = null; 213 return provider; 214 } 215 } 216 217 224 public final String getAlgorithm() { 225 return this.algorithm; 226 } 227 228 234 private KeyFactorySpi nextSpi(KeyFactorySpi oldSpi) { 235 synchronized (lock) { 236 if ((oldSpi != null) && (oldSpi != spi)) { 239 return spi; 240 } 241 if (serviceIterator == null) { 242 return null; 243 } 244 while (serviceIterator.hasNext()) { 245 Service s = serviceIterator.next(); 246 try { 247 Object obj = s.newInstance(null); 248 if (obj instanceof KeyFactorySpi == false) { 249 continue; 250 } 251 KeyFactorySpi spi = (KeyFactorySpi )obj; 252 provider = s.getProvider(); 253 this.spi = spi; 254 return spi; 255 } catch (NoSuchAlgorithmException e) { 256 } 258 } 259 serviceIterator = null; 260 return null; 261 } 262 } 263 264 275 public final PublicKey generatePublic(KeySpec keySpec) 276 throws InvalidKeySpecException { 277 if (serviceIterator == null) { 278 return spi.engineGeneratePublic(keySpec); 279 } 280 Exception failure = null; 281 KeyFactorySpi mySpi = spi; 282 do { 283 try { 284 return mySpi.engineGeneratePublic(keySpec); 285 } catch (Exception e) { 286 if (failure == null) { 287 failure = e; 288 } 289 mySpi = nextSpi(mySpi); 290 } 291 } while (mySpi != null); 292 if (failure instanceof RuntimeException ) { 293 throw (RuntimeException )failure; 294 } 295 if (failure instanceof InvalidKeySpecException ) { 296 throw (InvalidKeySpecException )failure; 297 } 298 throw new InvalidKeySpecException 299 ("Could not generate public key", failure); 300 } 301 302 313 public final PrivateKey generatePrivate(KeySpec keySpec) 314 throws InvalidKeySpecException { 315 if (serviceIterator == null) { 316 return spi.engineGeneratePrivate(keySpec); 317 } 318 Exception failure = null; 319 KeyFactorySpi mySpi = spi; 320 do { 321 try { 322 return mySpi.engineGeneratePrivate(keySpec); 323 } catch (Exception e) { 324 if (failure == null) { 325 failure = e; 326 } 327 mySpi = nextSpi(mySpi); 328 } 329 } while (mySpi != null); 330 if (failure instanceof RuntimeException ) { 331 throw (RuntimeException )failure; 332 } 333 if (failure instanceof InvalidKeySpecException ) { 334 throw (InvalidKeySpecException )failure; 335 } 336 throw new InvalidKeySpecException 337 ("Could not generate private key", failure); 338 } 339 340 360 public final <T extends KeySpec > T getKeySpec(Key key, Class <T> keySpec) 361 throws InvalidKeySpecException { 362 if (serviceIterator == null) { 363 return spi.engineGetKeySpec(key, keySpec); 364 } 365 Exception failure = null; 366 KeyFactorySpi mySpi = spi; 367 do { 368 try { 369 return mySpi.engineGetKeySpec(key, keySpec); 370 } catch (Exception e) { 371 if (failure == null) { 372 failure = e; 373 } 374 mySpi = nextSpi(mySpi); 375 } 376 } while (mySpi != null); 377 if (failure instanceof RuntimeException ) { 378 throw (RuntimeException )failure; 379 } 380 if (failure instanceof InvalidKeySpecException ) { 381 throw (InvalidKeySpecException )failure; 382 } 383 throw new InvalidKeySpecException 384 ("Could not get key spec", failure); 385 } 386 387 398 public final Key translateKey(Key key) throws InvalidKeyException { 399 if (serviceIterator == null) { 400 return spi.engineTranslateKey(key); 401 } 402 Exception failure = null; 403 KeyFactorySpi mySpi = spi; 404 do { 405 try { 406 return mySpi.engineTranslateKey(key); 407 } catch (Exception e) { 408 if (failure == null) { 409 failure = e; 410 } 411 mySpi = nextSpi(mySpi); 412 } 413 } while (mySpi != null); 414 if (failure instanceof RuntimeException ) { 415 throw (RuntimeException )failure; 416 } 417 if (failure instanceof InvalidKeyException ) { 418 throw (InvalidKeyException )failure; 419 } 420 throw new InvalidKeyException 421 ("Could not translate key", failure); 422 } 423 424 } 425 | Popular Tags |