| 1 7 8 package java.security; 9 10 import java.io.*; 11 import java.security.cert.Certificate ; 12 import java.security.cert.X509Certificate ; 13 import java.security.cert.CertificateException ; 14 import java.util.*; 15 import javax.crypto.SecretKey; 16 17 import javax.security.auth.callback.*; 18 19 141 142 public class KeyStore { 143 144 152 private static final String KEYSTORE_TYPE = "keystore.type"; 153 154 private String type; 156 157 private Provider provider; 159 160 private KeyStoreSpi keyStoreSpi; 162 163 private boolean initialized = false; 165 166 175 public static interface LoadStoreParameter { 176 181 public ProtectionParameter getProtectionParameter(); 182 } 183 184 196 public static interface ProtectionParameter { } 197 198 203 public static class PasswordProtection implements 204 ProtectionParameter, javax.security.auth.Destroyable { 205 206 private final char[] password; 207 private volatile boolean destroyed = false; 208 209 217 public PasswordProtection(char[] password) { 218 this.password = (password == null) ? 219 null : (char[])password.clone(); 220 } 221 222 235 public synchronized char[] getPassword() { 236 if (destroyed) { 237 throw new IllegalStateException ("password has been cleared"); 238 } 239 return password; 240 } 241 242 248 public synchronized void destroy() 249 throws javax.security.auth.DestroyFailedException { 250 destroyed = true; 251 if (password != null) { 252 Arrays.fill(password, ' '); 253 } 254 } 255 256 261 public synchronized boolean isDestroyed() { 262 return destroyed; 263 } 264 } 265 266 271 public static class CallbackHandlerProtection 272 implements ProtectionParameter { 273 274 private final CallbackHandler handler; 275 276 283 public CallbackHandlerProtection(CallbackHandler handler) { 284 if (handler == null) { 285 throw new NullPointerException ("handler must not be null"); 286 } 287 this.handler = handler; 288 } 289 290 295 public CallbackHandler getCallbackHandler() { 296 return handler; 297 } 298 299 } 300 301 306 public static interface Entry { } 307 308 314 public static final class PrivateKeyEntry implements Entry { 315 316 private final PrivateKey privKey; 317 private final Certificate [] chain; 318 319 343 public PrivateKeyEntry(PrivateKey privateKey, Certificate [] chain) { 344 if (privateKey == null || chain == null) { 345 throw new NullPointerException ("invalid null input"); 346 } 347 if (chain.length == 0) { 348 throw new IllegalArgumentException  349 ("invalid zero-length input chain"); 350 } 351 352 Certificate [] clonedChain = (Certificate [])chain.clone(); 353 String certType = clonedChain[0].getType(); 354 for (int i = 1; i < clonedChain.length; i++) { 355 if (!certType.equals(clonedChain[i].getType())) { 356 throw new IllegalArgumentException  357 ("chain does not contain certificates " + 358 "of the same type"); 359 } 360 } 361 if (!privateKey.getAlgorithm().equals 362 (clonedChain[0].getPublicKey().getAlgorithm())) { 363 throw new IllegalArgumentException  364 ("private key algorithm does not match " + 365 "algorithm of public key in end entity " + 366 "certificate (at index 0)"); 367 } 368 this.privKey = privateKey; 369 370 if (clonedChain[0] instanceof X509Certificate && 371 !(clonedChain instanceof X509Certificate [])) { 372 373 this.chain = new X509Certificate [clonedChain.length]; 374 System.arraycopy(clonedChain, 0, 375 this.chain, 0, clonedChain.length); 376 } else { 377 this.chain = clonedChain; 378 } 379 } 380 381 386 public PrivateKey getPrivateKey() { 387 return privKey; 388 } 389 390 401 public Certificate [] getCertificateChain() { 402 return (Certificate [])chain.clone(); 403 } 404 405 415 public Certificate getCertificate() { 416 return chain[0]; 417 } 418 419 423 public String toString() { 424 StringBuilder sb = new StringBuilder (); 425 sb.append("Private key entry and certificate chain with " 426 + chain.length + " elements:\r\n"); 427 for (Certificate cert : chain) { 428 sb.append(cert); 429 sb.append("\r\n"); 430 } 431 return sb.toString(); 432 } 433 434 } 435 436 441 public static final class SecretKeyEntry implements Entry { 442 443 private final SecretKey sKey; 444 445 454 public SecretKeyEntry(SecretKey secretKey) { 455 if (secretKey == null) { 456 throw new NullPointerException ("invalid null input"); 457 } 458 this.sKey = secretKey; 459 } 460 461 466 public SecretKey getSecretKey() { 467 return sKey; 468 } 469 470 474 public String toString() { 475 return "Secret key entry with algorithm " + sKey.getAlgorithm(); 476 } 477 } 478 479 485 public static final class TrustedCertificateEntry implements Entry { 486 487 private final Certificate cert; 488 489 498 public TrustedCertificateEntry(Certificate trustedCert) { 499 if (trustedCert == null) { 500 throw new NullPointerException ("invalid null input"); 501 } 502 this.cert = trustedCert; 503 } 504 505 510 public Certificate getTrustedCertificate() { 511 return cert; 512 } 513 514 518 public String toString() { 519 return "Trusted certificate entry:\r\n" + cert.toString(); 520 } 521 } 522 523 531 protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type) 532 { 533 this.keyStoreSpi = keyStoreSpi; 534 this.provider = provider; 535 this.type = type; 536 } 537 538 558 public static KeyStore getInstance(String type) 559 throws KeyStoreException  560 { 561 try { 562 Object [] objs = Security.getImpl(type, "KeyStore", (String )null); 563 return new KeyStore ((KeyStoreSpi )objs[0], (Provider )objs[1], type); 564 } catch (NoSuchAlgorithmException nsae) { 565 throw new KeyStoreException (type + " not found"); 566 } catch (NoSuchProviderException nspe) { 567 throw new KeyStoreException (type + " not found"); 568 } 569 } 570 571 597 public static KeyStore getInstance(String type, String provider) 598 throws KeyStoreException , NoSuchProviderException  599 { 600 if (provider == null || provider.length() == 0) 601 throw new IllegalArgumentException ("missing provider"); 602 try { 603 Object [] objs = Security.getImpl(type, "KeyStore", provider); 604 return new KeyStore ((KeyStoreSpi )objs[0], (Provider )objs[1], type); 605 } catch (NoSuchAlgorithmException nsae) { 606 throw new KeyStoreException (type + " not found"); 607 } 608 } 609 610 636 public static KeyStore getInstance(String type, Provider provider) 637 throws KeyStoreException  638 { 639 if (provider == null) 640 throw new IllegalArgumentException ("missing provider"); 641 try { 642 Object [] objs = Security.getImpl(type, "KeyStore", provider); 643 return new KeyStore ((KeyStoreSpi )objs[0], (Provider )objs[1], type); 644 } catch (NoSuchAlgorithmException nsae) { 645 throw new KeyStoreException (type + " not found"); 646 } 647 } 648 649 671 public final static String getDefaultType() { 672 String kstype; 673 kstype = (String )AccessController.doPrivileged(new PrivilegedAction () { 674 public Object run() { 675 return Security.getProperty(KEYSTORE_TYPE); 676 } 677 }); 678 if (kstype == null) { 679 kstype = "jks"; 680 } 681 return kstype; 682 } 683 684 689 public final Provider getProvider() 690 { 691 return this.provider; 692 } 693 694 699 public final String getType() 700 { 701 return this.type; 702 } 703 704 724 public final Key getKey(String alias, char[] password) 725 throws KeyStoreException , NoSuchAlgorithmException , 726 UnrecoverableKeyException  727 { 728 if (!initialized) { 729 throw new KeyStoreException ("Uninitialized keystore"); 730 } 731 return keyStoreSpi.engineGetKey(alias, password); 732 } 733 734 750 public final Certificate [] getCertificateChain(String alias) 751 throws KeyStoreException  752 { 753 if (!initialized) { 754 throw new KeyStoreException ("Uninitialized keystore"); 755 } 756 return keyStoreSpi.engineGetCertificateChain(alias); 757 } 758 759 783 public final Certificate getCertificate(String alias) 784 throws KeyStoreException  785 { 786 if (!initialized) { 787 throw new KeyStoreException ("Uninitialized keystore"); 788 } 789 return keyStoreSpi.engineGetCertificate(alias); 790 } 791 792 803 public final Date getCreationDate(String alias) 804 throws KeyStoreException  805 { 806 if (!initialized) { 807 throw new KeyStoreException ("Uninitialized keystore"); 808 } 809 return keyStoreSpi.engineGetCreationDate(alias); 810 } 811 812 835 public final void setKeyEntry(String alias, Key key, char[] password, 836 Certificate [] chain) 837 throws KeyStoreException  838 { 839 if (!initialized) { 840 throw new KeyStoreException ("Uninitialized keystore"); 841 } 842 if ((key instanceof PrivateKey ) && 843 (chain == null || chain.length == 0)) { 844 throw new IllegalArgumentException ("Private key must be " 845 + "accompanied by certificate " 846 + "chain"); 847 } 848 keyStoreSpi.engineSetKeyEntry(alias, key, password, chain); 849 } 850 851 875 public final void setKeyEntry(String alias, byte[] key, 876 Certificate [] chain) 877 throws KeyStoreException  878 { 879 if (!initialized) { 880 throw new KeyStoreException ("Uninitialized keystore"); 881 } 882 keyStoreSpi.engineSetKeyEntry(alias, key, chain); 883 } 884 885 903 public final void setCertificateEntry(String alias, Certificate cert) 904 throws KeyStoreException  905 { 906 if (!initialized) { 907 throw new KeyStoreException ("Uninitialized keystore"); 908 } 909 keyStoreSpi.engineSetCertificateEntry(alias, cert); 910 } 911 912 920 public final void deleteEntry(String alias) 921 throws KeyStoreException  922 { 923 if (!initialized) { 924 throw new KeyStoreException ("Uninitialized keystore"); 925 } 926 keyStoreSpi.engineDeleteEntry(alias); 927 } 928 929 937 public final Enumeration<String > aliases() 938 throws KeyStoreException  939 { 940 if (!initialized) { 941 throw new KeyStoreException ("Uninitialized keystore"); 942 } 943 return keyStoreSpi.engineAliases(); 944 } 945 946 956 public final boolean containsAlias(String  |