Search in sources :

Example 26 with NoopHostnameVerifier

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

the class BaseApiTest method createAcceptSelfSignedCertificateClient.

private static CloseableHttpClient createAcceptSelfSignedCertificateClient() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    SSLContext sslContext = SSLContextBuilder.create().loadTrustMaterial(new TrustSelfSignedStrategy()).build();
    HostnameVerifier allowAllHosts = new NoopHostnameVerifier();
    SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext, allowAllHosts);
    return HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build()).setSSLSocketFactory(connectionFactory).build();
}
Also used : NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 27 with NoopHostnameVerifier

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

the class HttpSenderBase method getSSLConnectionSocketFactory.

protected SSLConnectionSocketFactory getSSLConnectionSocketFactory() throws SenderException {
    SSLConnectionSocketFactory sslSocketFactory;
    HostnameVerifier hostnameVerifier = verifyHostname ? new DefaultHostnameVerifier() : new NoopHostnameVerifier();
    try {
        javax.net.ssl.SSLSocketFactory socketfactory = AuthSSLContextFactory.createSSLSocketFactory(this, this, getProtocol());
        sslSocketFactory = new SSLConnectionSocketFactory(socketfactory, hostnameVerifier);
    } catch (Exception e) {
        throw new SenderException("cannot create or initialize SocketFactory", e);
    }
    // Can still be null when no default or an invalid system sslSocketFactory has been defined
    if (sslSocketFactory != null) {
        httpClientBuilder.setSSLSocketFactory(sslSocketFactory);
    }
    return sslSocketFactory;
}
Also used : NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) SenderException(nl.nn.adapterframework.core.SenderException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) URISyntaxException(java.net.URISyntaxException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) SenderException(nl.nn.adapterframework.core.SenderException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientProtocolException(org.apache.http.client.ClientProtocolException) SocketTimeoutException(java.net.SocketTimeoutException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParameterException(nl.nn.adapterframework.core.ParameterException) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier)

Example 28 with NoopHostnameVerifier

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

the class SSLConfigurationCallback method customizeHttpClient.

@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
    final SSLContext sslcontext;
    final TrustStrategy trustStrategy = allowSelfSignedCertificates ? new TrustSelfSignedStrategy() : null;
    try {
        if (StringUtils.isNotEmpty(trustStoreFile)) {
            sslContextBuilder.loadTrustMaterial(new File(trustStoreFile), trustStorePassword.toCharArray(), trustStrategy);
        } else {
            sslContextBuilder.loadTrustMaterial(trustStrategy);
        }
    } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException e) {
        throw new RuntimeException("Invalid trust store file " + trustStoreFile, e);
    } catch (IOException e) {
        throw new RuntimeException("Unable to load trust store data from " + trustStoreFile, e);
    }
    try {
        if (StringUtils.isNotEmpty(keyStoreFile)) {
            sslContextBuilder.loadKeyMaterial(new File(keyStoreFile), keyStorePassword.toCharArray(), keyPassword.toCharArray());
        }
    } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException e) {
        throw new RuntimeException("Invalid key store file " + keyStoreFile, e);
    } catch (IOException e) {
        throw new RuntimeException("Unable to load key store data from " + keyStoreFile, e);
    }
    try {
        sslcontext = sslContextBuilder.build();
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        throw new RuntimeException("SSL context initialization failed", e);
    }
    httpClientBuilder.setSSLContext(sslcontext);
    if (disableHostNameVerification) {
        httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
    }
    return httpClientBuilder;
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) File(java.io.File) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Example 29 with NoopHostnameVerifier

use of org.apache.http.conn.ssl.NoopHostnameVerifier in project ats-framework by Axway.

the class HttpClient method setupSSL.

/**
 * Setup SSL. Pass the trusted certificates and client private key and certificate,
 * if applicable.
 *
 * @param httpClientBuilder The client builder
 * @throws HttpException
 */
private void setupSSL(HttpClientBuilder httpClientBuilder) throws HttpException {
    try {
        SSLContextBuilder sslContextBuilder = SSLContexts.custom();
        // set trust material
        if (trustedServerCertificates != null && trustedServerCertificates.length > 0) {
            sslContextBuilder.loadTrustMaterial(convertToKeyStore(trustedServerCertificates), new TrustStrategy() {

                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return checkIsTrusted(chain);
                }
            });
        } else {
            // no trust material provided, we will trust no matter the remote party
            sslContextBuilder.loadTrustMaterial(new TrustStrategy() {

                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            });
        }
        // set key material
        if (clientSSLKeyStore != null) {
            sslContextBuilder.loadKeyMaterial(clientSSLKeyStore, clientSSLKeyStorePassword.toCharArray());
        }
        SSLContext sslContext = sslContextBuilder.build();
        // Allow all supported protocols
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, supportedProtocols, supportedCipherSuites, new NoopHostnameVerifier());
        httpClientBuilder.setSSLSocketFactory(sslsf);
    } catch (Exception e) {
        throw new HttpException("Exception occurred when setting up SSL.", e);
    }
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509Certificate(java.security.cert.X509Certificate) URISyntaxException(java.net.URISyntaxException) GeneralSecurityException(java.security.GeneralSecurityException) ClientProtocolException(org.apache.http.client.ClientProtocolException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException)

Example 30 with NoopHostnameVerifier

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

the class HttpsClientSslLiveTest method givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate.

@Test
public final void givenHttpClientPost4_3_whenAcceptingAllCertificates_thenCanConsumeHttpsUriWithSelfSignedCertificate() throws IOException, GeneralSecurityException {
    final SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
    final NoopHostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
    final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
    final CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).setSSLSocketFactory(sslsf).build();
    // new
    final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);
    final HttpResponse response = httpClient.execute(getMethod);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    httpClient.close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) SSLContext(javax.net.ssl.SSLContext) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) Test(org.junit.Test)

Aggregations

NoopHostnameVerifier (org.apache.http.conn.ssl.NoopHostnameVerifier)33 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)22 SSLContext (javax.net.ssl.SSLContext)18 IOException (java.io.IOException)11 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)11 HostnameVerifier (javax.net.ssl.HostnameVerifier)10 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)10 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)7 X509Certificate (java.security.cert.X509Certificate)6 HttpGet (org.apache.http.client.methods.HttpGet)6 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)6 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)6 Test (org.junit.Test)6 CertificateException (java.security.cert.CertificateException)5 HttpResponse (org.apache.http.HttpResponse)5 Test (org.junit.jupiter.api.Test)5 MalformedURLException (java.net.MalformedURLException)4 KeyManagementException (java.security.KeyManagementException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HttpHost (org.apache.http.HttpHost)4