/** * Converts a protection parameter into a password protection parameter for the private key. */ private PasswordProtection privateKeyPassword(ProtectionParameter protection) throws KeyStoreException, UnsupportedCallbackException, IOException { if (protection instanceof PasswordProtection) { return (PasswordProtection) protection; } else if (protection instanceof CallbackHandlerProtection) { // Use the callback handler to resolve a password // TODO i18n? PasswordCallback callback = new PasswordCallback("Password for key " + keyFile.getFileName(), false); try { ((CallbackHandlerProtection) protection).getCallbackHandler().handle(new Callback[] { callback }); char[] password = callback.getPassword(); if (password != null) { return new PasswordProtection(password); } else { throw new KeyStoreException("No password provided"); } } finally { callback.clearPassword(); } } else { throw new IllegalArgumentException("Protection must be PasswordProtection or CallbackHandlerProtection"); } } }
CallbackHandler handler = ((KeyStore.CallbackHandlerProtection)protParam).getCallbackHandler();