1 7 8 package java.security; 9 10 import java.util.*; 11 12 import java.security.spec.AlgorithmParameterSpec ; 13 14 import java.security.Provider.Service; 15 16 import sun.security.jca.*; 17 import sun.security.jca.GetInstance.Instance; 18 19 96 97 public abstract class KeyPairGenerator extends KeyPairGeneratorSpi { 98 99 private final String algorithm; 100 101 Provider provider; 103 104 113 protected KeyPairGenerator(String algorithm) { 114 this.algorithm = algorithm; 115 } 116 117 126 public String getAlgorithm() { 127 return this.algorithm; 128 } 129 130 private static KeyPairGenerator getInstance(Instance instance, 131 String algorithm) { 132 KeyPairGenerator kpg; 133 if (instance.impl instanceof KeyPairGenerator ) { 134 kpg = (KeyPairGenerator )instance.impl; 135 } else { 136 KeyPairGeneratorSpi spi = (KeyPairGeneratorSpi )instance.impl; 137 kpg = new Delegate(spi, algorithm); 138 } 139 kpg.provider = instance.provider; 140 return kpg; 141 } 142 143 163 public static KeyPairGenerator getInstance(String algorithm) 164 throws NoSuchAlgorithmException { 165 List<Service> list = 166 GetInstance.getServices("KeyPairGenerator", algorithm); 167 Iterator<Service> t = list.iterator(); 168 if (t.hasNext() == false) { 169 throw new NoSuchAlgorithmException 170 (algorithm + " KeyPairGenerator not available"); 171 } 172 NoSuchAlgorithmException failure = null; 174 do { 175 Service s = t.next(); 176 try { 177 Instance instance = 178 GetInstance.getInstance(s, KeyPairGeneratorSpi .class); 179 if (instance.impl instanceof KeyPairGenerator ) { 180 return getInstance(instance, algorithm); 181 } else { 182 return new Delegate(instance, t, algorithm); 183 } 184 } catch (NoSuchAlgorithmException e) { 185 if (failure == null) { 186 failure = e; 187 } 188 } 189 } while (t.hasNext()); 190 throw failure; 191 } 192 193 219 public static KeyPairGenerator getInstance(String algorithm, 220 String provider) 221 throws NoSuchAlgorithmException , NoSuchProviderException { 222 Instance instance = GetInstance.getInstance("KeyPairGenerator", 223 KeyPairGeneratorSpi .class, algorithm, provider); 224 return getInstance(instance, algorithm); 225 } 226 227 253 public static KeyPairGenerator getInstance(String algorithm, 254 Provider provider) throws NoSuchAlgorithmException { 255 Instance instance = GetInstance.getInstance("KeyPairGenerator", 256 KeyPairGeneratorSpi .class, algorithm, provider); 257 return getInstance(instance, algorithm); 258 } 259 260 265 public final Provider getProvider() { 266 disableFailover(); 267 return this.provider; 268 } 269 270 void disableFailover() { 271 } 273 274 290 public void initialize(int keysize) { 291 initialize(keysize, JCAUtil.getSecureRandom()); 292 } 293 294 308 public void initialize(int keysize, SecureRandom random) { 309 } 320 321 349 public void initialize(AlgorithmParameterSpec params) 350 throws InvalidAlgorithmParameterException { 351 initialize(params, JCAUtil.getSecureRandom()); 352 } 353 354 377 public void initialize(AlgorithmParameterSpec params, 378 SecureRandom random) 379 throws InvalidAlgorithmParameterException 380 { 381 } 392 393 409 public final KeyPair genKeyPair() { 410 return generateKeyPair(); 411 } 412 413 427 public KeyPair generateKeyPair() { 428 return null; 442 } 443 444 445 459 460 481 private static final class Delegate extends KeyPairGenerator { 482 483 private volatile KeyPairGeneratorSpi spi; 485 486 private final Object lock = new Object (); 487 488 private Iterator<Service> serviceIterator; 489 490 private final static int I_NONE = 1; 491 private final static int I_SIZE = 2; 492 private final static int I_PARAMS = 3; 493 494 private int initType; 495 private int initKeySize; 496 private AlgorithmParameterSpec initParams; 497 private SecureRandom initRandom; 498 499 Delegate(KeyPairGeneratorSpi spi, String algorithm) { 501 super(algorithm); 502 this.spi = spi; 503 } 504 505 Delegate(Instance instance, Iterator<Service> serviceIterator, 506 String algorithm) { 507 super(algorithm); 508 spi = (KeyPairGeneratorSpi )instance.impl; 509 provider = instance.provider; 510 this.serviceIterator = serviceIterator; 511 initType = I_NONE; 512 } 513 514 520 private KeyPairGeneratorSpi nextSpi(KeyPairGeneratorSpi oldSpi, 521 boolean reinit) { 522 synchronized (lock) { 523 if ((oldSpi != null) && (oldSpi != spi)) { 526 return spi; 527 } 528 if (serviceIterator == null) { 529 return null; 530 } 531 while (serviceIterator.hasNext()) { 532 Service s = serviceIterator.next(); 533 try { 534 Object inst = s.newInstance(null); 535 if (inst instanceof KeyPairGeneratorSpi == false) { 537 continue; 538 } 539 if (inst instanceof KeyPairGenerator ) { 540 continue; 541 } 542 KeyPairGeneratorSpi spi = (KeyPairGeneratorSpi )inst; 543 if (reinit) { 544 if (initType == I_SIZE) { 545 spi.initialize(initKeySize, initRandom); 546 } else if (initType == I_PARAMS) { 547 spi.initialize(initParams, initRandom); 548 } else if (initType != I_NONE) { 549 throw new AssertionError 550 ("KeyPairGenerator initType: " + initType); 551 } 552 } 553 provider = s.getProvider(); 554 this.spi = spi; 555 return spi; 556 } catch (Exception e) { 557 } 559 } 560 disableFailover(); 561 return null; 562 } 563 } 564 565 void disableFailover() { 566 serviceIterator = null; 567 initType = 0; 568 initParams = null; 569 initRandom = null; 570 } 571 572 public void initialize(int keysize, SecureRandom random) { 574 if (serviceIterator == null) { 575 spi.initialize(keysize, random); 576 return; 577 } 578 RuntimeException failure = null; 579 KeyPairGeneratorSpi mySpi = spi; 580 do { 581 try { 582 mySpi.initialize(keysize, random); 583 initType = I_SIZE; 584 initKeySize = keysize; 585 initParams = null; 586 initRandom = random; 587 return; 588 } catch (RuntimeException e) { 589 if (failure == null) { 590 failure = e; 591 } 592 mySpi = nextSpi(mySpi, false); 593 } 594 } while (mySpi != null); 595 throw failure; 596 } 597 598 public void initialize(AlgorithmParameterSpec params, 600 SecureRandom random) throws InvalidAlgorithmParameterException { 601 if (serviceIterator == null) { 602 spi.initialize(params, random); 603 return; 604 } 605 Exception failure = null; 606 KeyPairGeneratorSpi mySpi = spi; 607 do { 608 try { 609 mySpi.initialize(params, random); 610 initType = I_PARAMS; 611 initKeySize = 0; 612 initParams = params; 613 initRandom = random; 614 return; 615 } catch (Exception e) { 616 if (failure == null) { 617 failure = e; 618 } 619 mySpi = nextSpi(mySpi, false); 620 } 621 } while (mySpi != null); 622 if (failure instanceof RuntimeException ) { 623 throw (RuntimeException )failure; 624 } 625 throw (InvalidAlgorithmParameterException )failure; 627 } 628 629 public KeyPair generateKeyPair() { 631 if (serviceIterator == null) { 632 return spi.generateKeyPair(); 633 } 634 RuntimeException failure = null; 635 KeyPairGeneratorSpi mySpi = spi; 636 do { 637 try { 638 return mySpi.generateKeyPair(); 639 } catch (RuntimeException e) { 640 if (failure == null) { 641 failure = e; 642 } 643 mySpi = nextSpi(mySpi, true); 644 } 645 } while (mySpi != null); 646 throw failure; 647 } 648 } 649 650 } 651 652 | Popular Tags |