All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Communication.Secure.SSLManagerFactory Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.Communication.Secure;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.URL;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;


/**
 * @author Gunter Zeilinger 
 */
public abstract class SSLManagerFactory {


    public static KeyStore createKeyStore(X509Certificate... certs)
            throws KeyStoreException {
        KeyStore ks = KeyStore.getInstance("JKS");
        try {
            ks.load(null);
        } catch (IOException e) {
            throw new AssertionError(e);
        } catch (NoSuchAlgorithmException e) {
            throw new AssertionError(e);
        } catch (CertificateException e) {
            throw new AssertionError(e);
        }
        for (X509Certificate cert : certs)
            ks.setCertificateEntry(cert.getSubjectX500Principal().getName(), cert);
        return ks;
    }

    public static KeyStore loadKeyStore(String type, String url, String password)
            throws IOException, KeyStoreException, NoSuchAlgorithmException,
            CertificateException {
        return loadKeyStore(type, url, password.toCharArray());
    }

    public static KeyStore loadKeyStore(String type, String url, char[] password)
            throws IOException, KeyStoreException, NoSuchAlgorithmException,
            CertificateException {
        KeyStore ks = KeyStore.getInstance(type);
        InputStream in = openFileOrURL(url);
        try {
            ks.load(in, password);
        } finally {
            close(in);
        }
        return ks;
    }


    public static InputStream openFileOrURL(String name) throws IOException {

        if (name.indexOf(':') < 2)
            return new FileInputStream(name);
        return new URL(name).openStream();
    }

    public static KeyManager createKeyManager(String type, String url,
                                              char[] storePassword, char[] keyPassword)
            throws UnrecoverableKeyException, KeyStoreException,
            NoSuchAlgorithmException, CertificateException, IOException {
        return createKeyManager(loadKeyStore(type, url, storePassword), keyPassword);
    }

    public static KeyManager createKeyManager(String type, String url,
                                              String storePassword, String keyPassword)
            throws UnrecoverableKeyException, KeyStoreException,
            NoSuchAlgorithmException, CertificateException, IOException {
        return createKeyManager(loadKeyStore(type, url, storePassword), keyPassword);
    }

    public static KeyManager createKeyManager(KeyStore ks, String password)
            throws UnrecoverableKeyException, KeyStoreException {
        return createKeyManager(ks, password.toCharArray());
    }

    public static KeyManager createKeyManager(KeyStore ks, char[] password)
            throws UnrecoverableKeyException, KeyStoreException {
        try {
            KeyManagerFactory kmf = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(ks, password);
            KeyManager[] kms = kmf.getKeyManagers();
            return kms.length > 0 ? kms[0] : null;
        } catch (NoSuchAlgorithmException e) {
            throw new AssertionError(e);
        }
    }

    public static TrustManager createTrustManager(KeyStore ks)
            throws KeyStoreException {
        try {
            TrustManagerFactory kmf = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            kmf.init(ks);
            TrustManager[] tms = kmf.getTrustManagers();
            return tms.length > 0 ? tms[0] : null;
        } catch (NoSuchAlgorithmException e) {
            throw new AssertionError(e);
        }
    }

    public static TrustManager createTrustManager(X509Certificate... certs)
            throws KeyStoreException {
        return createTrustManager(createKeyStore(certs));
    }

    public static TrustManager createTrustManager(String type, String url, char[] password)
            throws KeyStoreException, NoSuchAlgorithmException,
            CertificateException, IOException {
        return createTrustManager(loadKeyStore(type, url, password));
    }

    public static TrustManager createTrustManager(String type, String url, String password)
            throws KeyStoreException, NoSuchAlgorithmException,
            CertificateException, IOException {
        return createTrustManager(loadKeyStore(type, url, password));
    }

    public static void close(Socket sock) {
        if (sock != null)
            try {
                sock.close();
            } catch (IOException ignore) {
            }
    }

    public static void close(Closeable io) {
        if (io != null)
            try {
                io.close();
            } catch (IOException ignore) {
            }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy