Search in sources :

Example 6 with NoopHostnameVerifier

use of org.apache.http.conn.ssl.NoopHostnameVerifier in project cas by apereo.

the class SimpleHttpClientTests method verifyBypassedInvalidHttpsUrl.

@Test
public void verifyBypassedInvalidHttpsUrl() throws Exception {
    final SimpleHttpClientFactoryBean clientFactory = new SimpleHttpClientFactoryBean();
    clientFactory.setSslSocketFactory(getFriendlyToAllSSLSocketFactory());
    clientFactory.setHostnameVerifier(new NoopHostnameVerifier());
    clientFactory.setAcceptableCodes(CollectionUtils.wrapList(200, 403));
    final SimpleHttpClient client = clientFactory.getObject();
    assertTrue(client.isValidEndPoint("https://wrong.host.badssl.com/"));
}
Also used : NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) Test(org.junit.Test)

Example 7 with NoopHostnameVerifier

use of org.apache.http.conn.ssl.NoopHostnameVerifier in project openremote by openremote.

the class ExtensibleResteasyClientBuilder method initDefaultEngine43.

// The rest is copy/paste pretty much
public static ApacheHttpClient43Engine initDefaultEngine43(ExtensibleResteasyClientBuilder that) {
    HttpClient httpClient = null;
    HostnameVerifier verifier = null;
    if (that.verifier != null) {
        verifier = new ExtensibleResteasyClientBuilder.VerifierWrapper(that.verifier);
    } else {
        switch(that.policy) {
            case ANY:
                verifier = new NoopHostnameVerifier();
                break;
            case WILDCARD:
                verifier = new DefaultHostnameVerifier();
                break;
            case STRICT:
                verifier = new DefaultHostnameVerifier();
                break;
        }
    }
    try {
        SSLConnectionSocketFactory sslsf = null;
        SSLContext theContext = that.sslContext;
        if (that.disableTrustManager) {
            theContext = SSLContext.getInstance("SSL");
            theContext.init(null, new TrustManager[] { new PassthroughTrustManager() }, new SecureRandom());
            verifier = new NoopHostnameVerifier();
            sslsf = new SSLConnectionSocketFactory(theContext, verifier);
        } else if (theContext != null) {
            sslsf = new SSLConnectionSocketFactory(theContext, verifier) {

                @Override
                protected void prepareSocket(SSLSocket socket) throws IOException {
                    that.prepareSocketForSni(socket);
                }
            };
        } else if (that.clientKeyStore != null || that.truststore != null) {
            SSLContext ctx = SSLContexts.custom().useProtocol(SSLConnectionSocketFactory.TLS).setSecureRandom(null).loadKeyMaterial(that.clientKeyStore, that.clientPrivateKeyPassword != null ? that.clientPrivateKeyPassword.toCharArray() : null).loadTrustMaterial(that.truststore, TrustSelfSignedStrategy.INSTANCE).build();
            sslsf = new SSLConnectionSocketFactory(ctx, verifier) {

                @Override
                protected void prepareSocket(SSLSocket socket) throws IOException {
                    that.prepareSocketForSni(socket);
                }
            };
        } else {
            final SSLContext tlsContext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
            tlsContext.init(null, null, null);
            sslsf = new SSLConnectionSocketFactory(tlsContext, verifier);
        }
        final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf).build();
        HttpClientConnectionManager cm = null;
        if (that.connectionPoolSize > 0) {
            PoolingHttpClientConnectionManager tcm = new PoolingHttpClientConnectionManager(registry, null, null, null, that.connectionTTL, that.connectionTTLUnit);
            tcm.setMaxTotal(that.connectionPoolSize);
            if (that.maxPooledPerRoute == 0) {
                that.maxPooledPerRoute = that.connectionPoolSize;
            }
            tcm.setDefaultMaxPerRoute(that.maxPooledPerRoute);
            cm = tcm;
        } else {
            cm = new BasicHttpClientConnectionManager(registry);
        }
        RequestConfig.Builder rcBuilder = RequestConfig.custom();
        if (that.socketTimeout > -1) {
            rcBuilder.setSocketTimeout((int) that.socketTimeoutUnits.toMillis(that.socketTimeout));
        }
        if (that.establishConnectionTimeout > -1) {
            rcBuilder.setConnectTimeout((int) that.establishConnectionTimeoutUnits.toMillis(that.establishConnectionTimeout));
        }
        if (that.connectionCheckoutTimeoutMs > -1) {
            rcBuilder.setConnectionRequestTimeout(that.connectionCheckoutTimeoutMs);
        }
        // The magic configure()
        httpClient = that.configure(HttpClientBuilder.create().setConnectionManager(cm).setDefaultRequestConfig(rcBuilder.build()).setProxy(that.defaultProxy).disableContentCompression()).build();
        ApacheHttpClient43Engine engine = (ApacheHttpClient43Engine) ApacheHttpClient4EngineFactory.create(httpClient, true);
        engine.setResponseBufferSize(that.responseBufferSize);
        engine.setHostnameVerifier(verifier);
        // this may be null.  We can't really support this with Apache Client.
        engine.setSslContext(theContext);
        return engine;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) PassthroughTrustManager(org.jboss.resteasy.client.jaxrs.engines.PassthroughTrustManager) SecureRandom(java.security.SecureRandom) IOException(java.io.IOException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ApacheHttpClient43Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine) IOException(java.io.IOException) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) HttpClient(org.apache.http.client.HttpClient) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 8 with NoopHostnameVerifier

use of org.apache.http.conn.ssl.NoopHostnameVerifier in project mica2 by obiba.

the class AgateRestService method getSocketFactory.

/**
 * Do not check anything from the remote host (Agate server is trusted).
 *
 * @return
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
private SSLConnectionSocketFactory getSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
    // Accepts any SSL certificate
    TrustManager tm = new X509TrustManager() {

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

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

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { tm }, null);
    return new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
}
Also used : NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 9 with NoopHostnameVerifier

use of org.apache.http.conn.ssl.NoopHostnameVerifier in project janusgraph by JanusGraph.

the class SSLConfigurationCallbackTest method testDisableHostNameVerification.

@Test
public void testDisableHostNameVerification() throws Exception {
    final SSLConfigurationCallback cb = SSLConfigurationCallback.Builder.createCustom(sslContextBuilderMock).disableHostNameVerification().build();
    cb.customizeHttpClient(httpAsyncClientBuilderMock);
    final ArgumentCaptor<HostnameVerifier> hostnameVerifierCaptor = ArgumentCaptor.forClass(HostnameVerifier.class);
    verify(httpAsyncClientBuilderMock).setSSLHostnameVerifier(hostnameVerifierCaptor.capture());
    verify(sslContextBuilderMock).loadTrustMaterial((TrustStrategy) null);
    verify(sslContextBuilderMock).build();
    verify(httpAsyncClientBuilderMock).setSSLContext(sslContextMock);
    verifyNoMoreInteractions(sslContextMock, sslContextBuilderMock, httpAsyncClientBuilderMock);
    assertEquals(1, hostnameVerifierCaptor.getAllValues().size());
    final HostnameVerifier verifier = hostnameVerifierCaptor.getValue();
    // this assertion is implementation-specific but should be good enough
    // given the simplicity of the class under test
    assertTrue(verifier instanceof NoopHostnameVerifier);
}
Also used : NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with NoopHostnameVerifier

use of org.apache.http.conn.ssl.NoopHostnameVerifier in project dropwizard by dropwizard.

the class SslReloadAppTest method postIt.

/** Configure SSL and POST request parameters */
private void postIt(HttpsURLConnection conn) throws Exception {
    final SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(null, new TrustManager[] { TRUST_ALL }, null);
    conn.setHostnameVerifier(new NoopHostnameVerifier());
    conn.setSSLSocketFactory(sslCtx.getSocketFactory());
    // Make it a POST
    conn.setDoOutput(true);
    conn.getOutputStream().write(new byte[] {});
}
Also used : NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) SSLContext(javax.net.ssl.SSLContext)

Aggregations

NoopHostnameVerifier (org.apache.http.conn.ssl.NoopHostnameVerifier)17 SSLContext (javax.net.ssl.SSLContext)11 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)10 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)6 Test (org.junit.Test)6 IOException (java.io.IOException)5 CertificateException (java.security.cert.CertificateException)5 X509Certificate (java.security.cert.X509Certificate)5 HostnameVerifier (javax.net.ssl.HostnameVerifier)4 X509TrustManager (javax.net.ssl.X509TrustManager)4 HttpClientConnectionManager (org.apache.http.conn.HttpClientConnectionManager)4 TrustManager (javax.net.ssl.TrustManager)3 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)3 URISyntaxException (java.net.URISyntaxException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 KeyManagementException (java.security.KeyManagementException)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SecureRandom (java.security.SecureRandom)2 HttpHost (org.apache.http.HttpHost)2