1 7 8 package java.security; 9 10 import java.io.*; 11 import java.util.*; 12 13 import java.security.KeyStore .*; 14 import java.security.cert.Certificate ; 15 import java.security.cert.CertificateException ; 16 17 import javax.crypto.SecretKey; 18 19 import javax.security.auth.callback.*; 20 21 36 37 public abstract class KeyStoreSpi { 38 39 57 public abstract Key engineGetKey(String alias, char[] password) 58 throws NoSuchAlgorithmException , UnrecoverableKeyException ; 59 60 73 public abstract Certificate [] engineGetCertificateChain(String alias); 74 75 96 public abstract Certificate engineGetCertificate(String alias); 97 98 106 public abstract Date engineGetCreationDate(String alias); 107 108 130 public abstract void engineSetKeyEntry(String alias, Key key, 131 char[] password, 132 Certificate [] chain) 133 throws KeyStoreException ; 134 135 156 public abstract void engineSetKeyEntry(String alias, byte[] key, 157 Certificate [] chain) 158 throws KeyStoreException ; 159 160 177 public abstract void engineSetCertificateEntry(String alias, 178 Certificate cert) 179 throws KeyStoreException ; 180 181 188 public abstract void engineDeleteEntry(String alias) 189 throws KeyStoreException ; 190 191 196 public abstract Enumeration<String > engineAliases(); 197 198 205 public abstract boolean engineContainsAlias(String alias); 206 207 212 public abstract int engineSize(); 213 214 225 public abstract boolean engineIsKeyEntry(String alias); 226 227 238 public abstract boolean engineIsCertificateEntry(String alias); 239 240 263 public abstract String engineGetCertificateAlias(Certificate cert); 264 265 278 public abstract void engineStore(OutputStream stream, char[] password) 279 throws IOException, NoSuchAlgorithmException , CertificateException ; 280 281 300 public void engineStore(KeyStore.LoadStoreParameter param) 301 throws IOException, NoSuchAlgorithmException , 302 CertificateException { 303 throw new UnsupportedOperationException (); 304 } 305 306 329 public abstract void engineLoad(InputStream stream, char[] password) 330 throws IOException, NoSuchAlgorithmException , CertificateException ; 331 332 355 public void engineLoad(KeyStore.LoadStoreParameter param) 356 throws IOException, NoSuchAlgorithmException , 357 CertificateException { 358 359 if (param == null) { 360 engineLoad((InputStream)null, (char[])null); 361 return; 362 } 363 364 if (param instanceof KeyStore.SimpleLoadStoreParameter ) { 365 ProtectionParameter protection = param.getProtectionParameter(); 366 char[] password; 367 if (protection instanceof PasswordProtection) { 368 password = ((PasswordProtection)param).getPassword(); 369 } else if (protection instanceof CallbackHandlerProtection) { 370 CallbackHandler handler = 371 ((CallbackHandlerProtection)param).getCallbackHandler(); 372 PasswordCallback callback = 373 new PasswordCallback("Password: ", false); 374 try { 375 handler.handle(new Callback[] {callback}); 376 } catch (UnsupportedCallbackException e) { 377 throw new NoSuchAlgorithmException 378 ("Could not obtain password", e); 379 } 380 password = callback.getPassword(); 381 callback.clearPassword(); 382 if (password == null) { 383 throw new NoSuchAlgorithmException 384 ("No password provided"); 385 } 386 } else { 387 throw new NoSuchAlgorithmException ("ProtectionParameter must" 388 + " be PasswordProtection or CallbackHandlerProtection"); 389 } 390 engineLoad(null, password); 391 return; 392 } 393 394 throw new UnsupportedOperationException (); 395 } 396 397 417 public KeyStore.Entry engineGetEntry(String alias, 418 KeyStore.ProtectionParameter protParam) 419 throws KeyStoreException , NoSuchAlgorithmException , 420 UnrecoverableEntryException { 421 422 if (!engineContainsAlias(alias)) { 423 return null; 424 } 425 426 if (protParam == null) { 427 if (engineIsCertificateEntry(alias)) { 428 return new KeyStore.TrustedCertificateEntry 429 (engineGetCertificate(alias)); 430 } else { 431 throw new UnrecoverableEntryException 432 ("requested entry requires a password"); 433 } 434 } 435 436 if (protParam instanceof KeyStore.PasswordProtection ) { 437 if (engineIsCertificateEntry(alias)) { 438 throw new UnsupportedOperationException 439 ("trusted certificate entries are not password-protected"); 440 } else if (engineIsKeyEntry(alias)) { 441 KeyStore.PasswordProtection pp = 442 (KeyStore.PasswordProtection )protParam; 443 char[] password = pp.getPassword(); 444 445 try { 446 Key key = engineGetKey(alias, password); 447 if (key instanceof PrivateKey ) { 448 Certificate [] chain = engineGetCertificateChain(alias); 449 return new KeyStore.PrivateKeyEntry 450 ((PrivateKey )key, chain); 451 } else if (key instanceof SecretKey) { 452 return new KeyStore.SecretKeyEntry ((SecretKey)key); 453 } 454 } catch (UnrecoverableKeyException uke) { 455 UnrecoverableEntryException uee = 456 new UnrecoverableEntryException (); 457 uee.initCause(uke); 458 throw uee; 459 } 460 } 461 } 462 463 throw new UnsupportedOperationException (); 464 } 465 466 484 public void engineSetEntry(String alias, KeyStore.Entry entry, 485 KeyStore.ProtectionParameter protParam) 486 throws KeyStoreException { 487 488 if (protParam != null && 490 !(protParam instanceof KeyStore.PasswordProtection )) { 491 throw new KeyStoreException ("unsupported protection parameter"); 492 } 493 KeyStore.PasswordProtection pProtect = null; 494 if (protParam != null) { 495 pProtect = (KeyStore.PasswordProtection )protParam; 496 } 497 498 if (entry instanceof KeyStore.TrustedCertificateEntry ) { 500 if (protParam != null && pProtect.getPassword() != null) { 501 throw new KeyStoreException 503 ("trusted certificate entries are not password-protected"); 504 } else { 505 KeyStore.TrustedCertificateEntry tce = 506 (KeyStore.TrustedCertificateEntry )entry; 507 engineSetCertificateEntry(alias, tce.getTrustedCertificate()); 508 return; 509 } 510 } else if (entry instanceof KeyStore.PrivateKeyEntry ) { 511 if (pProtect == null || pProtect.getPassword() == null) { 512 throw new KeyStoreException 514 ("non-null password required to create PrivateKeyEntry"); 515 } else { 516 engineSetKeyEntry 517 (alias, 518 ((KeyStore.PrivateKeyEntry )entry).getPrivateKey(), 519 pProtect.getPassword(), 520 ((KeyStore.PrivateKeyEntry )entry).getCertificateChain()); 521 return; 522 } 523 } else if (entry instanceof KeyStore.SecretKeyEntry ) { 524 if (pProtect == null || pProtect.getPassword() == null) { 525 throw new KeyStoreException 527 ("non-null password required to create SecretKeyEntry"); 528 } else { 529 engineSetKeyEntry 530 (alias, 531 ((KeyStore.SecretKeyEntry )entry).getSecretKey(), 532 pProtect.getPassword(), 533 (Certificate [])null); 534 return; 535 } 536 } 537 538 throw new KeyStoreException 539 ("unsupported entry type: " + entry.getClass().getName()); 540 } 541 542 556 public boolean 557 engineEntryInstanceOf(String alias, 558 Class <? extends KeyStore.Entry > entryClass) 559 { 560 if (entryClass == KeyStore.TrustedCertificateEntry .class) { 561 return engineIsCertificateEntry(alias); 562 } 563 if (entryClass == KeyStore.PrivateKeyEntry .class) { 564 return engineIsKeyEntry(alias) && 565 engineGetCertificate(alias) != null; 566 } 567 if (entryClass == KeyStore.SecretKeyEntry .class) { 568 return engineIsKeyEntry(alias) && 569 engineGetCertificate(alias) == null; 570 } 571 return false; 572 } 573 } 574 | Popular Tags |