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);
    }

    public static void setAlias(String alias)
    {
        // Sets the system property with the alias
        System.setProperty("cz.scholz.aliaskeymanager.alias", alias);
    }

    public static void unSetAlias()
    {
        // Clears the system property with the alias
        System.clearProperty("cz.scholz.aliaskeymanager.alias");
    }
}

@@ -144,37 +144,89 @@
 %0A * 
-Created by schojak
+Implementation of JSSE provider, which provides its
  o
+w
 n 
-30.5.16
+KeyManager implementation
 .%0A *
@@ -815,83 +815,34 @@
 cts 
-a provider with the specified name, version number,%0A     * and information.
+the JSSE Provider instance
 %0A   
@@ -948,32 +948,129 @@
 KM_SPI);%0A    %7D%0A%0A
+    /**%0A     * Enabled the JSSE Provider - registers it using Security.addProvideer(...)%0A     */%0A
     public stati
@@ -1298,32 +1298,132 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Disable the JSSE Provider - if it is registered, it will be removed from the%0A     */%0A
     public stati
@@ -1875,113 +1875,227 @@
     
-public static void setAsDefault()%0A    %7B%0A        // Make sure AliasProvid
+/**%0A     * Set the AliasKeyManager as the default one for the application. In case the AliasKeyManag
 er
-t
  is 
+not 
 enabled
-%0A        enable();%0A
+, this%0A     * method will automatically enable it.%0A     */%0A    public static void setAsDefault()%0A    %7B
 %0A   
@@ -2102,19 +2102,25 @@
      // 
-Set
+Make sure
  AliasPr
@@ -2130,51 +2130,66 @@
 der 
-as default and the original default as base
+is enabled before setting it as default%0A        enable();%0A
 %0A   
@@ -2366,201 +2366,429 @@
     
-public static void unsetAsDefault()%0A    %7B%0A        // Set the original default algo to be default again%0A        Security.setProperty(%22ssl.KeyManagerFactory.algorithm%22, DEFAULT_ALGORITHM);%0A    %7D%0A
+/**%0A     * Return to the original KeyManager implementation which was default before the AliasKeyManager%0A     */%0A    public static void unsetAsDefault()%0A    %7B%0A        Security.setProperty(%22ssl.KeyManagerFactory.algorithm%22, DEFAULT_ALGORITHM);%0A    %7D%0A%0A    /**%0A     * Set the system property containing the alias of the key which should be used for authentication%0A     *%0A     * @param alias Alias of the prefered key%0A     */
 %0A   
@@ -2840,59 +2840,8 @@
   %7B%0A
-        // Sets the system property with the alias%0A
     
@@ -2921,63 +2921,24 @@
     
-public static void unSetAlias()%0A    %7B%0A   
+/**%0A
      
-//
+*
  Clear
-s
  the
@@ -2958,22 +2958,129 @@
 rty 
-with the alias
+containing the alias of the key which should be used for authentication%0A     */%0A    public static void unSetAlias()%0A    %7B
 %0A   

Add Javadoc