package cz.scholz.aliaskeymanager;

import javax.net.ssl.KeyManagerFactory;

import java.security.Provider;
import java.security.Security;

/**
 * Created by schojak on 30.5.16.
 */
public class AliasProvider extends Provider {
    private static final String DEFAULT_ALGORITHM = KeyManagerFactory.getDefaultAlgorithm();

    private final static String ALGORITHM = "aliaskm";
    private final static Double VERSION = 1.0;
    private final static String INFO = "Alias Security provider provides the Key MAnager which selects the client key for authtentication based on the alias";
    private final String KM_SERVICE = "KeyManagerFactory.aliaskm";
    private final String KM_SPI = "cz.scholz.aliaskeymanager.AliasKeyManagerFactorySpi";

    /**
     * Constructs a provider with the specified name, version number,
     * and information.
     */
    public AliasProvider() {
        super(ALGORITHM, VERSION, INFO);
        put(KM_SERVICE, KM_SPI);
    }

    public static void enable()
    {
        // Add the provider
        if (Security.getProvider(ALGORITHM) == null)
        {
            Provider aliasProvider = new AliasProvider();
            Security.addProvider(aliasProvider);
        }
    }

    public static void disable()
    {
        // If our algo is the default, return the original default
        if (ALGORITHM.equals(Security.getProperty("ssl.KeyManagerFactory.algorithm")))
        {
            Security.setProperty("ssl.KeyManagerFactory.algorithm", DEFAULT_ALGORITHM);
        }

        // Remove the provider
        if (Security.getProvider(ALGORITHM) != null)
        {
            Security.removeProvider(ALGORITHM);
        }
    }

    public static void setAsDefault()
    {
        // Make sure AliasProvidert is enabled
        enable();

        // Set AliasProvider as default and the original default as base
        System.setProperty("cz.scholz.aliaskeymanager.basealgorithm", DEFAULT_ALGORITHM);
        Security.setProperty("ssl.KeyManagerFactory.algorithm", ALGORITHM);
    }

    public static void unsetAsDefault()
    {
        // Set the original default algo to be default again
        Security.setProperty("ssl.KeyManagerFactory.algorithm", DEFAULT_ALGORITHM);
    }
}

@@ -2219,11 +2219,358 @@
 );%0A    %7D
+%0A%0A    public static void setAlias(String alias)%0A    %7B%0A        // Sets the system property with the alias%0A        System.setProperty(%22cz.scholz.aliaskeymanager.alias%22, alias);%0A    %7D%0A%0A    public static void unSetAlias()%0A    %7B%0A        // Clears the system property with the alias%0A        System.clearProperty(%22cz.scholz.aliaskeymanager.alias%22);%0A    %7D
 %0A%7D%0A

Add method for setting and unseting the alias system property