集册 Java实例教程 获取Ssl套接字工厂

获取Ssl套接字工厂

欢马劈雪     最近更新时间:2020-01-02 10:19:05

524
获取Ssl套接字工厂

/**
 from
* 时代Java公众号 
**/

import java.io.IOException;

import java.io.InputStream;

import java.security.KeyManagementException;

import java.security.KeyStore;

import java.security.KeyStoreException;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.security.UnrecoverableKeyException;

import java.security.cert.CertificateException;

import java.security.cert.CertificateFactory;

import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;

import javax.net.ssl.KeyManager;

import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSession;

import javax.net.ssl.SSLSocketFactory;/**N o w J a v a . c o m - 时代Java**/

import javax.net.ssl.TrustManager;

import javax.net.ssl.TrustManagerFactory;

import javax.net.ssl.X509TrustManager;


public class Main{

    public static SSLSocketFactory getSslSocketFactory(

            InputStream[] certificates, InputStream bksFile, String password) {

        try {

            TrustManager[] trustManagers = prepareTrustManager(certificates);

            KeyManager[] keyManagers = prepareKeyManager(bksFile, password);

            SSLContext sslContext = SSLContext.getInstance("TLS");

            TrustManager trustManager = null;

            if (trustManagers != null) {

                trustManager = new MyTrustManager(

                        chooseTrustManager(trustManagers));

            } else {

                trustManager = new UnSafeTrustManager();

            }

            sslContext

                    .init(keyManagers, new TrustManager[] { trustManager },

                            new SecureRandom());

            return sslContext.getSocketFactory();

        } catch (NoSuchAlgorithmException e) {

            throw new AssertionError(e);

        } catch (KeyManagementException e) {

            throw new AssertionError(e);

        } catch (KeyStoreException e) {

            throw new AssertionError(e);

        }

    }

    private static TrustManager[] prepareTrustManager(

            InputStream... certificates) {

        if (certificates == null || certificates.length <= 0)

            return null;

        try {


            CertificateFactory certificateFactory = CertificateFactory

                    .getInstance("X.509");

            KeyStore keyStore = KeyStore.getInstance(KeyStore

                    .getDefaultType());

            keyStore.load(null);

            int index = 0;

            for (InputStream certificate : certificates) {

                String certificateAlias = Integer.toString(index++);

                keyStore.setCertificateEntry(certificateAlias,

                        certificateFactory.generateCertificate(certificate));

                try {

                    if (certificate != null)

                        certificate.close();

                } catch (IOException e)


                {

                }

            }

            TrustManagerFactory trustManagerFactory = null;


            trustManagerFactory = TrustManagerFactory

                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());

            trustManagerFactory.init(keyStore);


            TrustManager[] trustManagers = trustManagerFactory

                    .getTrustManagers();


            return trustManagers;

        } catch (NoSuchAlgorithmException e) {

            e.printStackTrace();

        } catch (CertificateException e) {

            e.printStackTrace();

        } catch (KeyStoreException e) {

            e.printStackTrace();

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;


    }

    private static KeyManager[] prepareKeyManager(InputStream bksFile,

            String password) {

        try {

            if (bksFile == null || password == null)

                return null;


            KeyStore clientKeyStore = KeyStore.getInstance("BKS");

            clientKeyStore.load(bksFile, password.toCharArray());

            KeyManagerFactory keyManagerFactory = KeyManagerFactory

                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());

            keyManagerFactory.init(clientKeyStore, password.toCharArray());

            return keyManagerFactory.getKeyManagers();


        } catch (KeyStoreException e) {

            e.printSta
展开阅读全文