package cz.scholz.aliaskeymanager;

import javax.net.ssl.*;

import java.security.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by schojak on 30.5.16.
 */
public class AliasKeyManagerFactorySpi extends KeyManagerFactorySpi {
    protected final String algorithm = "aliaskm";
    protected String baseAlgorithm;
    protected KeyManagerFactory originalFactory;
    protected String alias;

    protected void engineInit(KeyStore keyStore, char[] chars) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
        if (KeyManagerFactory.getDefaultAlgorithm().equals(algorithm))
        {
            baseAlgorithm = System.getProperty("cz.scholz.aliaskeymanager.basealgorithm");

            if (algorithm.equals(baseAlgorithm))
            {
                throw new NoSuchAlgorithmException("Base algorithm has to be different from " + algorithm);
            }
        }
        else
        {
            baseAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
        }

        if (baseAlgorithm == null)
        {
            throw new NoSuchAlgorithmException("No base algorithm has been identified");
        }

        originalFactory = KeyManagerFactory.getInstance(baseAlgorithm);
        originalFactory.init(keyStore, chars);

        alias = determineAlias(keyStore, chars);
    }

    protected void engineInit(ManagerFactoryParameters managerFactoryParameters) throws InvalidAlgorithmParameterException {
        // Do nothing
    }

    protected KeyManager[] engineGetKeyManagers() {
        return wrapKeyManagers(originalFactory.getKeyManagers());
    }

    protected KeyManager[] wrapKeyManagers(KeyManager[] originalKeyManagers)
    {
        List<KeyManager> wrapped = new ArrayList<KeyManager>();

        for (int i = 0; i < originalKeyManagers.length; i++) {
            if (originalKeyManagers[i] instanceof X509ExtendedKeyManager) {
                KeyManager wrap = new AliasKeyManager(alias, (X509ExtendedKeyManager) originalKeyManagers[i]);
                wrapped.add(wrap);
            }
        }

        return wrapped.toArray(new KeyManager[0]);
    }

    protected String determineAlias(KeyStore keyStore, char[] chars) throws KeyStoreException {
        String alias = System.getProperty("cz.scholz.aliaskeymanager.alias");

        if (alias != null)
        {
            if (keyStore.containsAlias(alias)) {
                return alias;
            }
            else
            {
                throw new KeyStoreException("Alias " + alias + " does not exist in the keystore");
            }
        }

        else
        {
            Enumeration<String> aliases = keyStore.aliases();

            if (aliases.hasMoreElements())
            {
                return aliases.nextElement();
            }
            else
            {
                throw new KeyStoreException("No aliases found in the keystore");
            }
        }
    }
}

@@ -171,37 +171,194 @@
 %0A * 
-Created by schojak on 30.5.16
+AliasKeyManagerFactorySpi is used by Java's KeyManagerFactory to create the KeyManmager instances. This class%0A * contains the initialization of the KeyManager based on the base algorithm
 .%0A *
@@ -593,16 +593,515 @@
 alias;%0A%0A
+    /**%0A     * Initializes the KeyManagerFactory with the keystore and its password. This class creates an instance of the%0A     * KeyManagerFactory created using the base algorithm / default algorithm and determines the key alias which should%0A     * be used.%0A     *%0A     * @param keyStore Keystore containing the private key%0A     * @param chars Password to access the keyStore%0A     * @throws KeyStoreException%0A     * @throws NoSuchAlgorithmException%0A     * @throws UnrecoverableKeyException%0A     */%0A
     prot
@@ -2035,282 +2035,909 @@
     
-protected void engineInit(ManagerFactoryParameters managerFactoryParameters) throws InvalidAlgorithmParameterException %7B%0A        // Do nothing%0A    %7D%0A%0A    protected KeyManager%5B%5D engineGetKeyManagers() %7B%0A        return wrapKeyManagers(originalFactory.getKeyManagers());%0A    %7D%0A
+/**%0A     * Currently not implemented.%0A     *%0A     * @param managerFactoryParameters ManagerFactoryParameters object containing the parametrs of the KeyManagerFactory.%0A     * @throws InvalidAlgorithmParameterException%0A     */%0A    protected void engineInit(ManagerFactoryParameters managerFactoryParameters) throws InvalidAlgorithmParameterException %7B%0A        // Do nothing%0A    %7D%0A%0A    /**%0A     * Return the array with wrapped KeyManager instances.%0A     *%0A     * @return Array of KeyManager instances%0A     */%0A    protected KeyManager%5B%5D engineGetKeyManagers() %7B%0A        return wrapKeyManagers(originalFactory.getKeyManagers());%0A    %7D%0A%0A    /**%0A     * Wraps the KeyManager instances provided by the default KeyManager into the AliasKeyManager.%0A     *%0A     * @param originalKeyManagers The KeyMAnager instances as returned by the default algorithm%0A     * @return Array of wrapped KeyManager instances%0A     */
 %0A   
@@ -3445,24 +3445,411 @@
 0%5D);%0A    %7D%0A%0A
+    /**%0A     * Determines and validates the alias which should be used for the authentication. The alias is either taken from%0A     * the System property or from the KeyStore.%0A     *%0A     * @param keyStore Keystore containing the private key%0A     * @param chars Password to access the keyStore%0A     * @return Alias of the key which should be used%0A     * @throws KeyStoreException%0A     */%0A
     protecte

Add Javadoc