Example usage for javax.net.ssl TrustManagerFactory getInstance

List of usage examples for javax.net.ssl TrustManagerFactory getInstance

Introduction

In this page you can find the example usage for javax.net.ssl TrustManagerFactory getInstance.

Prototype

public static final TrustManagerFactory getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a TrustManagerFactory object that acts as a factory for trust managers.

Usage

From source file:io.swagger.client.ApiClient.java

/**
 * Apply SSL related settings to httpClient according to the current values of
 * verifyingSsl and sslCaCert./*from ww  w  .j a v  a2 s.  c  om*/
 */
private void applySslSettings() {
    try {
        KeyManager[] keyManagers = null;
        TrustManager[] trustManagers = null;
        HostnameVerifier hostnameVerifier = null;
        if (!verifyingSsl) {
            TrustManager trustAll = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            SSLContext sslContext = SSLContext.getInstance("TLS");
            trustManagers = new TrustManager[] { trustAll };
            hostnameVerifier = new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };
        } else if (sslCaCert != null) {
            char[] password = null; // Any password will work.
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(sslCaCert);
            if (certificates.isEmpty()) {
                throw new IllegalArgumentException("expected non-empty set of trusted certificates");
            }
            KeyStore caKeyStore = newEmptyKeyStore(password);
            int index = 0;
            for (Certificate certificate : certificates) {
                String certificateAlias = "ca" + Integer.toString(index++);
                caKeyStore.setCertificateEntry(certificateAlias, certificate);
            }
            TrustManagerFactory trustManagerFactory = TrustManagerFactory
                    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(caKeyStore);
            trustManagers = trustManagerFactory.getTrustManagers();
        }

        if (keyManagers != null || trustManagers != null) {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(keyManagers, trustManagers, new SecureRandom());
            httpClient.setSslSocketFactory(sslContext.getSocketFactory());
        } else {
            httpClient.setSslSocketFactory(null);
        }
        httpClient.setHostnameVerifier(hostnameVerifier);
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.wso2.carbon.device.mgt.core.geo.service.GeoLocationProviderServiceImpl.java

/**
 * Initializes the SSL Context/*from w  w w  .  j  a va2s.c o m*/
 */
private SSLContext initSSLConnection(String tenantAdminUser)
        throws NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException, KeyManagementException,
        IOException, CertificateException {
    String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password");
    String trustStorePassword = ServerConfiguration.getInstance()
            .getFirstProperty("Security.TrustStore.Password");
    String keyStoreLocation = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Location");
    String trustStoreLocation = ServerConfiguration.getInstance()
            .getFirstProperty("Security.TrustStore.Location");

    //Call to load the keystore.
    KeyStore keyStore = loadKeyStore(keyStoreLocation, keyStorePassword.toCharArray());
    //Call to load the TrustStore.
    KeyStore trustStore = loadTrustStore(trustStoreLocation, trustStorePassword.toCharArray());

    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KEY_MANAGER_TYPE);
    keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TRUST_MANAGER_TYPE);
    trustManagerFactory.init(trustStore);

    // Create and initialize SSLContext for HTTPS communication

    SSLContext sslContext = SSLContext.getInstance(SSLV3);
    sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
    SSLContext.setDefault(sslContext);
    return sslContext;
}

From source file:com.ebridgevas.android.ebridgeapp.messaging.mqttservice.MqttAndroidClient.java

/**
 * Get the SSLSocketFactory using SSL key store and password
 * <p>//from  w ww  . j a v a  2 s .c  o  m
 * A convenience method, which will help user to create a SSLSocketFactory
 * object
 * </p>
 * 
 * @param keyStore
 *            the SSL key store which is generated by some SSL key tool,
 *            such as keytool in Java JDK
 * @param password
 *            the password of the key store which is set when the key store
 *            is generated
 * @return SSLSocketFactory used to connect to the server with SSL
 *         authentication
 * @throws MqttSecurityException
 *             if there was any error when getting the SSLSocketFactory
 */
public SSLSocketFactory getSSLSocketFactory(InputStream keyStore, String password)
        throws MqttSecurityException {
    try {
        SSLContext ctx = null;
        SSLSocketFactory sslSockFactory = null;
        KeyStore ts;
        ts = KeyStore.getInstance("BKS");
        ts.load(keyStore, password.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
        tmf.init(ts);
        TrustManager[] tm = tmf.getTrustManagers();
        ctx = SSLContext.getInstance("TLSv1");
        ctx.init(null, tm, null);

        sslSockFactory = ctx.getSocketFactory();
        return sslSockFactory;

    } catch (KeyStoreException e) {
        throw new MqttSecurityException(e);
    } catch (CertificateException e) {
        throw new MqttSecurityException(e);
    } catch (FileNotFoundException e) {
        throw new MqttSecurityException(e);
    } catch (IOException e) {
        throw new MqttSecurityException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new MqttSecurityException(e);
    } catch (KeyManagementException e) {
        throw new MqttSecurityException(e);
    }
}