jdk/src/java.base/share/classes/java/security/KeyStoreSpi.java
changeset 32005 2c716d9dac38
parent 29492 a4bf9a570035
child 32231 202718e81db8
equal deleted inserted replaced
32004:5dba041fc305 32005:2c716d9dac38
   357      * reinitialized and loaded again from the given parameter.
   357      * reinitialized and loaded again from the given parameter.
   358      *
   358      *
   359      * @param param the {@code KeyStore.LoadStoreParameter}
   359      * @param param the {@code KeyStore.LoadStoreParameter}
   360      *          that specifies how to load the keystore,
   360      *          that specifies how to load the keystore,
   361      *          which may be {@code null}
   361      *          which may be {@code null}
       
   362      *
       
   363      * @implSpec
       
   364      * The default implementation examines {@code KeyStore.LoadStoreParameter}
       
   365      * to extract its password and pass it to
       
   366      * {@link KeyStoreSpi#engineLoad(InputStream, char[])} along with a
       
   367      * {@code null} {@code InputStream}.
       
   368      * <p>
       
   369      * If {@code KeyStore.LoadStoreParameter} is {@code null} then
       
   370      * the password parameter will also be {@code null}.
       
   371      * Otherwise the {@code KeyStore.ProtectionParameter} of
       
   372      * {@code KeyStore.LoadStoreParameter} must be either a
       
   373      * {@code KeyStore.PasswordProtection} or a
       
   374      * {@code KeyStore.CallbackHandlerProtection} that supports
       
   375      * {@code PasswordCallback} so that the password parameter can be
       
   376      * extracted. If the {@code KeyStore.ProtectionParameter} is neither
       
   377      * of those classes then a {@code NoSuchAlgorithmException} is thrown.
   362      *
   378      *
   363      * @exception IllegalArgumentException if the given
   379      * @exception IllegalArgumentException if the given
   364      *          {@code KeyStore.LoadStoreParameter}
   380      *          {@code KeyStore.LoadStoreParameter}
   365      *          input is not recognized
   381      *          input is not recognized
   366      * @exception IOException if there is an I/O or format problem with the
   382      * @exception IOException if there is an I/O or format problem with the
   383         if (param == null) {
   399         if (param == null) {
   384             engineLoad((InputStream)null, (char[])null);
   400             engineLoad((InputStream)null, (char[])null);
   385             return;
   401             return;
   386         }
   402         }
   387 
   403 
   388         if (param instanceof KeyStore.SimpleLoadStoreParameter) {
   404         ProtectionParameter protection = param.getProtectionParameter();
   389             ProtectionParameter protection = param.getProtectionParameter();
   405         char[] password;
   390             char[] password;
   406         if (protection instanceof PasswordProtection) {
   391             if (protection instanceof PasswordProtection) {
   407             password = ((PasswordProtection)protection).getPassword();
   392                 password = ((PasswordProtection)protection).getPassword();
   408         } else if (protection instanceof CallbackHandlerProtection) {
   393             } else if (protection instanceof CallbackHandlerProtection) {
   409             CallbackHandler handler =
   394                 CallbackHandler handler =
   410                 ((CallbackHandlerProtection)protection).getCallbackHandler();
   395                     ((CallbackHandlerProtection)protection).getCallbackHandler();
   411             PasswordCallback callback =
   396                 PasswordCallback callback =
   412                 new PasswordCallback("Password: ", false);
   397                     new PasswordCallback("Password: ", false);
   413             try {
   398                 try {
   414                 handler.handle(new Callback[] {callback});
   399                     handler.handle(new Callback[] {callback});
   415             } catch (UnsupportedCallbackException e) {
   400                 } catch (UnsupportedCallbackException e) {
   416                 throw new NoSuchAlgorithmException
   401                     throw new NoSuchAlgorithmException
   417                     ("Could not obtain password", e);
   402                         ("Could not obtain password", e);
       
   403                 }
       
   404                 password = callback.getPassword();
       
   405                 callback.clearPassword();
       
   406                 if (password == null) {
       
   407                     throw new NoSuchAlgorithmException
       
   408                         ("No password provided");
       
   409                 }
       
   410             } else {
       
   411                 throw new NoSuchAlgorithmException("ProtectionParameter must"
       
   412                     + " be PasswordProtection or CallbackHandlerProtection");
       
   413             }
   418             }
   414             engineLoad(null, password);
   419             password = callback.getPassword();
   415             return;
   420             callback.clearPassword();
   416         }
   421             if (password == null) {
   417 
   422                 throw new NoSuchAlgorithmException("No password provided");
   418         throw new UnsupportedOperationException();
   423             }
       
   424         } else {
       
   425             throw new NoSuchAlgorithmException("ProtectionParameter must"
       
   426                 + " be PasswordProtection or CallbackHandlerProtection");
       
   427         }
       
   428         engineLoad(null, password);
       
   429         return;
   419     }
   430     }
   420 
   431 
   421     /**
   432     /**
   422      * Gets a {@code KeyStore.Entry} for the specified alias
   433      * Gets a {@code KeyStore.Entry} for the specified alias
   423      * with the specified protection parameter.
   434      * with the specified protection parameter.