Example usage for javax.net.ssl X509TrustManager X509TrustManager

List of usage examples for javax.net.ssl X509TrustManager X509TrustManager

Introduction

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

Prototype

X509TrustManager

Source Link

Usage

From source file:li.klass.fhem.fhem.TrustAllSSLSocketFactory.java

public TrustAllSSLSocketFactory(KeyStore truststore)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(truststore);

    setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    TrustManager trustManager = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }//from w ww  .  ja  v  a 2s .c  o  m

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

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    sslContext.init(null, new TrustManager[] { trustManager }, null);
}

From source file:android.net.http.HttpsConnection.java

/**
 * @hide//from w w  w .  java  2  s  .  com
 *
 * @param sessionDir directory to cache SSL sessions
 */
public static void initializeEngine(File sessionDir) {
    try {
        SSLClientSessionCache cache = null;
        if (sessionDir != null) {
            Log.d("HttpsConnection", "Caching SSL sessions in " + sessionDir + ".");
            cache = FileClientSessionCache.usingDirectory(sessionDir);
        }

        SSLContextImpl sslContext = new SSLContextImpl();

        // here, trust managers is a single trust-all manager
        TrustManager[] trustManagers = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };

        sslContext.engineInit(null, trustManagers, null, cache, null);

        synchronized (HttpsConnection.class) {
            mSslSocketFactory = sslContext.engineGetSocketFactory();
        }
    } catch (KeyManagementException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.adobe.api.platform.msc.client.config.ClientConfig.java

/**
 * Used for creating http connections to 3rd party API. It's a heavy-weight object and it's useful to reuse it.
 *
 * @return A JaxRS Client instance//from  www .j  a va 2 s.  c  o  m
 */
@Bean
public Client getJaxrsClient() {
    try {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };

        // Ignore differences between given hostname and certificate hostname
        SSLContext ctx = SSLContext.getInstance("SSL");
        ctx.init(null, trustAllCerts, null);

        ResteasyClientBuilder builder = new ResteasyClientBuilder().sslContext(ctx)
                .register(JacksonConfig.class)
                .hostnameVerification(ResteasyClientBuilder.HostnameVerificationPolicy.ANY);

        //if not set, builder sets a pool with 10 threads
        if (workerThreadPooSize != null) {
            // if necessary expose the thread pool as bean (reuse the same thread pool for other scenarios).
            builder.asyncExecutor(Executors.newFixedThreadPool(workerThreadPooSize));
        }

        if (connectionPoolSize != null) {
            builder.connectionPoolSize(connectionPoolSize);
        }

        if (connectionTTL != null) {
            builder.connectionTTL(connectionTTL, TimeUnit.SECONDS);
        }

        if (socketTimeout != null) {
            builder.socketTimeout(socketTimeout, TimeUnit.SECONDS);
        }

        return builder.build();

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:android.net.http.HttpsConnection.java

/**
 * @hide//from w  ww.  j a v a 2 s.  com
 *
 * @param sessionDir directory to cache SSL sessions
 */
public static void initializeEngine(File sessionDir) {
    try {
        SSLClientSessionCache cache = null;
        if (sessionDir != null) {
            Log.d("HttpsConnection", "Caching SSL sessions in " + sessionDir + ".");
            cache = FileClientSessionCache.usingDirectory(sessionDir);
        }

        OpenSSLContextImpl sslContext = new OpenSSLContextImpl();

        // here, trust managers is a single trust-all manager
        TrustManager[] trustManagers = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };

        sslContext.engineInit(null, trustManagers, null);
        sslContext.engineGetClientSessionContext().setPersistentCache(cache);

        synchronized (HttpsConnection.class) {
            mSslSocketFactory = sslContext.engineGetSocketFactory();
        }
    } catch (KeyManagementException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.jaspersoft.jasperserver.jaxrs.client.core.RestClientConfiguration.java

public RestClientConfiguration() {
    trustManagers = new TrustManager[] { new X509TrustManager() {
        @Override//  w ww  . ja  v a 2 s .c om
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
                throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };
}

From source file:org.gege.caldavsyncadapter.caldav.EasySSLSocketFactory.java

private EasySSLSocketFactory() {
    super();/*from   w  w w .j  a  va 2  s  .c  o m*/
    TrustManager[] tm = new TrustManager[] { new X509TrustManager() {

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // do nothing
        }

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

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

    } };
    try {
        this.sslcontext = SSLContext.getInstance(SSLSocketFactory.TLS);
        this.sslcontext.init(null, tm, new SecureRandom());
        this.socketfactory = this.sslcontext.getSocketFactory();
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Faild to instantiate TrustAllSSLSocketFactory!", e);
    } catch (KeyManagementException e) {
        Log.e(TAG, "Failed to instantiate TrustAllSSLSocketFactory!", e);
    }
}

From source file:helpers.Methods.java

public static void trustAllCertificates() {
    //Certification check
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override/*from  w  w  w. j a v a2s .  co  m*/
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (GeneralSecurityException ex) {
        Variables.logger.Log(Methods.class, Variables.LogType.Error,
                "Error in trusting all certificates. Details:\r\n" + ex.getMessage());
    }
}

From source file:org.jasig.cas.util.SimpleHttpClientTests.java

private SSLConnectionSocketFactory getFriendlyToAllSSLSocketFactory() throws Exception {
    final TrustManager trm = new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }// ww  w. j a va  2s.c om

        public void checkClientTrusted(final X509Certificate[] certs, final String authType) {
        }

        public void checkServerTrusted(final X509Certificate[] certs, final String authType) {
        }
    };
    final SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, new TrustManager[] { trm }, null);
    return new SSLConnectionSocketFactory(sc);
}

From source file:org.wso2.carbon.apimgt.integration.client.util.Utils.java

private static SSLSocketFactory getSimpleTrustedSSLSocketFactory() {
    try {/*from   www.  jav a  2s .c  o  m*/
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } };
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        return sc.getSocketFactory();
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        return null;
    }

}

From source file:cz.zcu.kiv.eeg.mobile.base.ws.ssl.SSLSimpleClientHttpRequestFactory.java

@Override
protected HttpURLConnection openConnection(URL url, Proxy proxy) throws IOException {
    final HttpURLConnection httpUrlConnection = super.openConnection(url, proxy);
    if (url.getProtocol().toLowerCase().equals("https")) {
        try {/*  w  w  w .  j a  va  2  s .  c  o  m*/

            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(null, new TrustManager[] { new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) {
                }

                public void checkServerTrusted(X509Certificate[] chain, String authType) {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[] {};
                }
            } }, null);
            ((HttpsURLConnection) httpUrlConnection).setSSLSocketFactory(ctx.getSocketFactory());
            ((HttpsURLConnection) httpUrlConnection).setHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        } catch (Exception e) {
        }
    }
    return httpUrlConnection;
}