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:com.pispower.video.sdk.net.SimpleSSLSocketFactory.java

/**
 * Creates a new SSL Socket Factory with the given KeyStore.
 * //www.  j a va 2 s . co m
 * @param truststore
 *            A KeyStore to create the SSL Socket Factory in context of
 * @throws NoSuchAlgorithmException
 *             NoSuchAlgorithmException
 * @throws KeyManagementException
 *             KeyManagementException
 * @throws KeyStoreException
 *             KeyStoreException
 * @throws UnrecoverableKeyException
 *             UnrecoverableKeyException
 */
public SimpleSSLSocketFactory(KeyStore truststore)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(truststore);

    X509TrustManager tm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

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

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

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

From source file:com.nextdoor.bender.ipc.http.AbstractHttpTransportFactory.java

/**
 * There isn't an easy way in java to trust non-self signed certs. Just allow all until java
 * KeyStore functionality is added to Bender.
 *
 * @return a context that trusts all SSL certs
 *///from  w  w  w.ja  v a  2 s .  c om
private SSLContext getSSLContext() {
    /*
     * Create SSLContext and TrustManager that will trust all SSL certs.
     *
     * Copy pasta from http://stackoverflow.com/a/4837230
     */
    TrustManager tm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

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

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

    SSLContext ctx;
    try {
        ctx = SSLContext.getInstance("TLS");
    } catch (NoSuchAlgorithmException e) {
        throw new TransportFactoryInitException("JVM does not have proper libraries for TSL");
    }

    try {
        ctx.init(null, new TrustManager[] { tm }, new java.security.SecureRandom());
    } catch (KeyManagementException e) {
        throw new TransportFactoryInitException("Unable to init SSLContext with TrustManager", e);
    }
    return ctx;
}

From source file:es.tsb.ltba.nomhad.httpclient.NomhadHttpClient.java

/**
 * Authentication/*from www . ja  v a  2 s  . c  o  m*/
 * 
 * @param base
 *            the client to be configured
 * @return the authentication-enabled client
 */
private static DefaultHttpClient wrapClient(HttpClient base) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.jwrapper.maven.java.JavaDownloadMojo.java

protected void setupNonVerifingSSL() throws Exception {

    // Create a trust manager that does not validate certificate chains
    final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override//w w  w .  j ava  2s . c  o  m
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(final X509Certificate[] arg0, final String arg1)
                throws CertificateException {
        }

        @Override
        public void checkServerTrusted(final X509Certificate[] arg0, final String arg1)
                throws CertificateException {
        }
    } };

    // Install the all-trusting trust manager
    final SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    // Create all-trusting host name verifier
    final HostnameVerifier allHostsValid = new HostnameVerifier() {
        @Override
        public boolean verify(final String hostname, final SSLSession session) {
            return true;
        }
    };

    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

}

From source file:com.lugia.timetable.SSLHttpClient.java

private static TrustManager createTrustManager() {
    return new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }// w w  w.  ja v  a 2 s .co  m

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

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

From source file:oracle.custom.ui.utils.ServerUtils.java

public static SSLContext getContext() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager tms[] = new TrustManager[] { new X509TrustManager() {
        @Override/* ww  w. java2  s.c  o m*/
        public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {

        }

        @Override
        public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {

        }

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

    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tms, new SecureRandom());
    return context;
}

From source file:com.aliyun.oss.common.comm.HttpClientFactory.java

private static SSLSocketFactory getSSLSocketFactory() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }//from  w ww  .j a  v  a 2 s. c om

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

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

    try {
        SSLContext sslcontext = SSLContext.getInstance("SSL");
        sslcontext.init(null, trustAllCerts, null);
        SSLSocketFactory ssf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        return ssf;

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

From source file:com.base.net.volley.toolbox.HurlStack.java

private SSLSocketFactory getDefaultSSLSocketFactory() {
    SSLSocketFactory mySSLSocketFactory = null;
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }/*from  www . j  a va2s  .  co m*/

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

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

    // Install the all-trusting trust manager

    SSLContext sc;
    try {
        sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new SecureRandom());
        mySSLSocketFactory = sc.getSocketFactory();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return mySSLSocketFactory;
}

From source file:org.apache.cloudstack.storage.datastore.util.NexentaNmsClient.java

protected DefaultHttpClient getHttpsClient() {
    try {/*ww  w. j  av a2  s.c o m*/
        SSLContext sslContext = SSLUtils.getSSLContext();
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

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

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

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

        SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext,
                SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        SchemeRegistry registry = new SchemeRegistry();

        registry.register(new Scheme("https", nmsUrl.getPort(), socketFactory));

        BasicClientConnectionManager mgr = new BasicClientConnectionManager(registry);

        return new DefaultHttpClient(mgr);
    } catch (NoSuchAlgorithmException ex) {
        throw new CloudRuntimeException(ex.getMessage());
    } catch (KeyManagementException ex) {
        throw new CloudRuntimeException(ex.getMessage());
    }
}