Example usage for org.apache.http.conn.ssl X509HostnameVerifier X509HostnameVerifier

List of usage examples for org.apache.http.conn.ssl X509HostnameVerifier X509HostnameVerifier

Introduction

In this page you can find the example usage for org.apache.http.conn.ssl X509HostnameVerifier X509HostnameVerifier.

Prototype

X509HostnameVerifier

Source Link

Usage

From source file:ro.teodorbaciu.commons.client.ws.util.WebClientDevWrapper.java

/**
 * Provides a new instance of http client that wraps the 
 * instance specified as parameter.//from   ww  w . j  a  v  a  2 s  . c  om
 */
@SuppressWarnings("deprecation")
public static DefaultHttpClient wrapClient(HttpClient base) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                    throws java.security.cert.CertificateException {

            }

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

            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            @Override
            public void verify(String string, SSLSocket ssls) throws IOException {
            }

            @Override
            public void verify(String string, X509Certificate xc) throws SSLException {
            }

            @Override
            public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            }

            @Override
            public boolean verify(String string, SSLSession ssls) {
                return true;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(verifier);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.ibm.sbt.services.util.SSLUtil.java

public static DefaultHttpClient wrapHttpClient(DefaultHttpClient base) {
    try {//from w  w  w  . jav  a  2 s  .c  o m
        // Create and assign a dummy TrustManager
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] cert, String s) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] cert, String s) throws CertificateException {
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);

        // When Apache Client AllowAllHostnameVerifier is strict, this should be used
        // Stays here for reference
        X509HostnameVerifier verifier = new X509HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }

            @Override
            public void verify(String s, SSLSocket sslSession) throws IOException {
            }

            @Override
            public void verify(String s, String[] ss1, String[] ss2) throws SSLException {
            }

            @Override
            public void verify(String s, X509Certificate cerst) throws SSLException {
            }

        };
        ssf.setHostnameVerifier(verifier);

        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:cn.wanghaomiao.seimi.http.hc.HttpClientCMPBox.java

public HttpClientCMPBox() {
    SSLContextBuilder builder = new SSLContextBuilder();
    try {//from  ww w.  j a  va  2  s .c om
        builder.loadTrustMaterial(null, new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        });
        SSLContext sslContext = builder.build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                new X509HostnameVerifier() {
                    @Override
                    public void verify(String host, SSLSocket ssl) throws IOException {
                    }

                    @Override
                    public void verify(String host, X509Certificate cert) throws SSLException {
                    }

                    @Override
                    public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
                    }

                    @Override
                    public boolean verify(String s, SSLSession sslSession) {
                        return true;
                    }
                });
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", new PlainConnectionSocketFactory()).register("https", sslsf).build();
        poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
        poolingHttpClientConnectionManager.setMaxTotal(500);
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(1000);
    } catch (Exception e) {
        Logger logger = LoggerFactory.getLogger(getClass());
        logger.error("init fail,err={}", e.getMessage(), e);
    }

}

From source file:ui.shared.FreebaseHelper.java

@SuppressWarnings("deprecation")
public static HttpClient wrapClient(HttpClient base) {
    try {//from  w  w  w  . j  a v a 2 s  .  c  o m
        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;
            }
        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            public void verify(String string, SSLSocket ssls) throws IOException {
            }

            public void verify(String string, X509Certificate xc) throws SSLException {
            }

            public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            }

            public boolean verify(String string, SSLSession ssls) {
                return true;
            }

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

From source file:com.seajas.search.contender.http.ExclusiveConnectionManager.java

/**
 * Create a scheme registry which either trusts all certificates, or uses the HttpClient default.
 *
 * @param trustAllCertificates// ww  w . j av  a2s . c om
 * @return SchemeRegistry
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 * @throws KeyStoreException
 * @throws UnrecoverableKeyException
 */
private static SchemeRegistry createSchemeRegistry(final Boolean trustAllCertificates)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    if (trustAllCertificates) {
        SchemeRegistry registry = new SchemeRegistry();

        if (logger.isInfoEnabled())
            logger.info("Trusting all certificates");

        X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() {
            @Override
            public void verify(String host, SSLSocket ssl) throws IOException {
                // Do nothing
            }

            @Override
            public void verify(String host, X509Certificate cert) throws SSLException {
                //Do nothing
            }

            @Override
            public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
                //Do nothing
            }

            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        };

        registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        registry.register(new Scheme("https", 443, new SSLSocketFactory(new TrustStrategy() {
            @Override
            public boolean isTrusted(final X509Certificate[] chain, final String authType)
                    throws CertificateException {
                return true;
            }
        }, hostnameVerifier)/* {
                            @Override
                            public Socket connectSocket(final Socket socket,
                            final InetSocketAddress remoteAddress,
                            final InetSocketAddress localAddress,
                            final HttpParams param) throws IOException, UnknownHostException, ConnectTimeoutException {
                            if (socket instanceof SSLSocket) {
                            try {
                            if (logger.isDebugEnabled()) {
                            String currentHost = null;
                                    
                            logger.debug("This JVM seems to potentially not support SNI - trying to fix");
                                    
                            try {
                            currentHost = (String) FieldUtils.getFieldValue(socket, "host");
                            } catch (IllegalAccessException e) {
                            logger.debug("Unable to access field 'host' from the underlaying SSLSocket");
                            }
                                    
                            logger.debug("Overriding default socket hostname of " + (currentHost != null ? currentHost : "(null)") + " with " + remoteAddress.getHostName());
                            }
                                    
                            PropertyUtils.setProperty(socket, "host", remoteAddress.getHostName());
                            } catch (NoSuchMethodException e) {
                            if (logger.isDebugEnabled())
                            logger.debug(e);
                            } catch (IllegalAccessException e) {
                            if (logger.isDebugEnabled())
                            logger.debug(e);
                            } catch (InvocationTargetException e) {
                            if (logger.isDebugEnabled())
                            logger.debug(e);
                            }
                            } else {
                            if (logger.isDebugEnabled())
                            logger.debug("This is not the JVM we're looking for - should support SNI");
                            }
                                    
                            return super.connectSocket(socket, remoteAddress, localAddress, param);
                            }
                            } */));

        return registry;
    } else
        return SchemeRegistryFactory.createDefault();
}

From source file:com.ovea.facebook.client.DefaultFacebookClient.java

public DefaultFacebookClient(String client_id, String client_secret, String redirect_uri) {
    this.clientId = client_id;
    this.clientSecret = client_secret;
    this.redirectUri = redirect_uri;
    try {/*from  www  .  ja  va  2  s .  c  om*/
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
                    throws CertificateException {
            }

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

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } }, new SecureRandom());
        sslSocketFactory = new SSLSocketFactory(sslContext);
        //noinspection deprecation
        sslSocketFactory.setHostnameVerifier(new X509HostnameVerifier() {
            @Override
            public void verify(String host, SSLSocket ssl) throws IOException {
            }

            @Override
            public void verify(String host, X509Certificate cert) throws SSLException {
            }

            @Override
            public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
            }

            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        });
    } catch (NoSuchAlgorithmException e) {
        throw new FacebookException(e.getMessage(), e);
    } catch (KeyManagementException e) {
        throw new FacebookException(e.getMessage(), e);
    }
}

From source file:org.jboss.as.test.integration.web.security.cert.WebSecurityCERTTestCase.java

public static HttpClient wrapClient(HttpClient base, String alias) {
    try {/*from  w  ww .  j a  v  a  2s  . c o  m*/
        SSLContext ctx = SSLContext.getInstance("TLS");
        JBossJSSESecurityDomain jsseSecurityDomain = new JBossJSSESecurityDomain("client-cert");
        jsseSecurityDomain.setKeyStorePassword("changeit");
        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        URL keystore = tccl.getResource("security/client.keystore");
        jsseSecurityDomain.setKeyStoreURL(keystore.getPath());
        jsseSecurityDomain.setClientAlias(alias);
        jsseSecurityDomain.reloadKeyAndTrustStore();
        KeyManager[] keyManagers = jsseSecurityDomain.getKeyManagers();
        TrustManager[] trustManagers = jsseSecurityDomain.getTrustManagers();
        ctx.init(keyManagers, trustManagers, null);
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            @Override
            public void verify(String s, SSLSocket sslSocket) throws IOException {
            }

            @Override
            public void verify(String s, X509Certificate x509Certificate) throws SSLException {
            }

            @Override
            public void verify(String s, String[] strings, String[] strings1) throws SSLException {
            }

            @Override
            public boolean verify(String string, SSLSession ssls) {
                return true;
            }
        };
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, verifier);//SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 8380, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfRetriever.java

private static X509HostnameVerifier newNaiveVerifier() {
    return new X509HostnameVerifier() {
        @Override/*from www.j  a  va  2  s. c o m*/
        public void verify(String host, SSLSocket ssl) throws IOException {

        }

        @Override
        public void verify(String host, X509Certificate cert) throws SSLException {

        }

        @Override
        public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {

        }

        @Override
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }
    };
}

From source file:org.xdi.oxd.license.client.LicenseClientTest.java

public static HttpClient createHttpClientTrustAll()
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    //        System.setProperty("javax.net.debug", "SSL,handshake,trustmanager");

    //        SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() {
    //            @Override
    //            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    //                return true;
    //            }
    //        }, new AllowAllHostnameVerifier());

    SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() {
        @Override/* ww  w  .j  a  v  a2 s  .co m*/
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    }, new X509HostnameVerifier() {
        @Override
        public void verify(String host, SSLSocket ssl) throws IOException {
        }

        @Override
        public void verify(String host, X509Certificate cert) throws SSLException {
        }

        @Override
        public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
        }

        @Override
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }
    });

    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    registry.register(new Scheme("https", 443, sf));
    ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
    return new DefaultHttpClient(ccm);
}

From source file:org.eclipse.lyo.client.oauth.sample.OAuthClient.java

private static void disableCertificateValidatation(HttpClient client) {
    try {/* w w w  . j  a  va  2  s. com*/
        final SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

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

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } }, new java.security.SecureRandom());
        final SSLSocketFactory socketFactory = new SSLSocketFactory(sc, new X509HostnameVerifier() {
            public void verify(String string, SSLSocket ssls) throws IOException {
            }

            public void verify(String string, X509Certificate xc) throws SSLException {
            }

            public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            }

            public boolean verify(String string, SSLSession ssls) {
                return true;
            }
        });
        final Scheme https = new Scheme("https", 443, socketFactory);
        client.getConnectionManager().getSchemeRegistry().register(https);
    } catch (GeneralSecurityException e) {
    }
}