// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.jcajce.provider.keystore.util;

import java.io.IOException;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.PasswordCallback;
import java.security.KeyStore;

public class ParameterUtil
{
    public static char[] extractPassword(final KeyStore.LoadStoreParameter loadStoreParameter) throws IOException {
        final KeyStore.ProtectionParameter protectionParameter = loadStoreParameter.getProtectionParameter();
        if (protectionParameter == null) {
            return null;
        }
        if (protectionParameter instanceof KeyStore.PasswordProtection) {
            return ((KeyStore.PasswordProtection)protectionParameter).getPassword();
        }
        if (protectionParameter instanceof KeyStore.CallbackHandlerProtection) {
            final CallbackHandler callbackHandler = ((KeyStore.CallbackHandlerProtection)protectionParameter).getCallbackHandler();
            final PasswordCallback passwordCallback = new PasswordCallback("password: ", false);
            try {
                callbackHandler.handle(new Callback[] { passwordCallback });
                return passwordCallback.getPassword();
            }
            catch (final UnsupportedCallbackException cause) {
                throw new IllegalArgumentException("PasswordCallback not recognised: " + cause.getMessage(), cause);
            }
        }
        throw new IllegalArgumentException("no support for protection parameter of type " + ((KeyStore.CallbackHandlerProtection)protectionParameter).getClass().getName());
    }
}
