Search in sources :

Example 11 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project fess-crawler by codelibs.

the class HcHttpClient method buildSSLSocketFactory.

protected LayeredConnectionSocketFactory buildSSLSocketFactory(final HttpClientBuilder httpClientBuilder) {
    if (sslSocketFactory != null) {
        return sslSocketFactory;
    }
    if (getInitParameter(IGNORE_SSL_CERTIFICATE_PROPERTY, false, Boolean.class)) {
        try {
            final SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (arg0, arg1) -> true).build();
            httpClientBuilder.setSSLContext(sslContext);
            return new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
        } catch (final Exception e) {
            logger.warn("Failed to create TrustSelfSignedStrategy.", e);
        }
    }
    return SSLConnectionSocketFactory.getSocketFactory();
}
Also used : AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) SSLContext(javax.net.ssl.SSLContext) CrawlerContext(org.codelibs.fess.crawler.CrawlerContext) RequestConfig(org.apache.http.client.config.RequestConfig) MaxLengthExceededException(org.codelibs.fess.crawler.exception.MaxLengthExceededException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) Registry(org.apache.http.config.Registry) Map(java.util.Map) Lookup(org.apache.http.config.Lookup) AuthCache(org.apache.http.client.AuthCache) BasicScheme(org.apache.http.impl.auth.BasicScheme) Resource(javax.annotation.Resource) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) DefaultCookieSpecProvider(org.apache.http.impl.cookie.DefaultCookieSpecProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) PublicSuffixMatcher(org.apache.http.conn.util.PublicSuffixMatcher) RegistryBuilder(org.apache.http.config.RegistryBuilder) HttpHead(org.apache.http.client.methods.HttpHead) SimpleDateFormat(java.text.SimpleDateFormat) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CrawlingParameterUtil(org.codelibs.fess.crawler.util.CrawlingParameterUtil) ArrayList(java.util.ArrayList) SocketException(java.net.SocketException) Credentials(org.apache.http.auth.Credentials) ConnectException(java.net.ConnectException) Cookie(org.apache.http.cookie.Cookie) ContentLengthHelper(org.codelibs.fess.crawler.helper.ContentLengthHelper) RobotsTxtHelper(org.codelibs.fess.crawler.helper.RobotsTxtHelper) RFC6265CookieSpecProvider(org.apache.http.impl.cookie.RFC6265CookieSpecProvider) SystemUtils(org.apache.commons.lang3.SystemUtils) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) File(java.io.File) Constants(org.codelibs.fess.crawler.Constants) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) AuthScope(org.apache.http.auth.AuthScope) AccessTimeoutTarget(org.codelibs.fess.crawler.client.AccessTimeoutTarget) HttpResponse(org.apache.http.HttpResponse) TimeoutManager(org.codelibs.core.timer.TimeoutManager) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpHost(org.apache.http.HttpHost) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) URL(java.net.URL) Date(java.util.Date) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) LoggerFactory(org.slf4j.LoggerFactory) FormScheme(org.codelibs.fess.crawler.client.http.form.FormScheme) Header(org.apache.http.Header) AbstractCrawlerClient(org.codelibs.fess.crawler.client.AbstractCrawlerClient) EntityUtils(org.apache.http.util.EntityUtils) RobotsTxt(org.codelibs.fess.crawler.entity.RobotsTxt) PreDestroy(javax.annotation.PreDestroy) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) Locale(java.util.Locale) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) CopyUtil(org.codelibs.core.io.CopyUtil) ParseException(java.text.ParseException) IdnDnsResolver(org.codelibs.fess.crawler.client.http.conn.IdnDnsResolver) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) HttpEntity(org.apache.http.HttpEntity) IgnoreSpecProvider(org.apache.http.impl.cookie.IgnoreSpecProvider) PublicSuffixMatcherLoader(org.apache.http.conn.util.PublicSuffixMatcherLoader) CompatibilityLevel(org.apache.http.impl.cookie.DefaultCookieSpecProvider.CompatibilityLevel) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) List(java.util.List) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) Pattern(java.util.regex.Pattern) NetscapeDraftSpecProvider(org.apache.http.impl.cookie.NetscapeDraftSpecProvider) TimeoutTask(org.codelibs.core.timer.TimeoutTask) NoRouteToHostException(java.net.NoRouteToHostException) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) PropertyDesc(org.codelibs.core.beans.PropertyDesc) DateUtils(org.apache.http.client.utils.DateUtils) DnsResolver(org.apache.http.conn.DnsResolver) Pair(org.codelibs.core.misc.Pair) HashMap(java.util.HashMap) CookieSpecs(org.apache.http.client.config.CookieSpecs) CookieStore(org.apache.http.client.CookieStore) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) AuthScheme(org.apache.http.auth.AuthScheme) MimeTypeHelper(org.codelibs.fess.crawler.helper.MimeTypeHelper) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) FileInputStream(java.io.FileInputStream) CloseableUtil(org.codelibs.core.io.CloseableUtil) BeanDescFactory(org.codelibs.core.beans.factory.BeanDescFactory) TimeUnit(java.util.concurrent.TimeUnit) BeanDesc(org.codelibs.core.beans.BeanDesc) BasicHeader(org.apache.http.message.BasicHeader) InputStream(java.io.InputStream) SSLContext(javax.net.ssl.SSLContext) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) MaxLengthExceededException(org.codelibs.fess.crawler.exception.MaxLengthExceededException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) SocketException(java.net.SocketException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) ParseException(java.text.ParseException) NoRouteToHostException(java.net.NoRouteToHostException) MalformedURLException(java.net.MalformedURLException)

Example 12 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project sslcontext-kickstart by Hakky54.

the class Apache4SslUtilsShould method createLayeredConnectionSocketFactoryWithTrustMaterial.

@Test
void createLayeredConnectionSocketFactoryWithTrustMaterial() {
    KeyStore trustStore = KeyStoreUtils.loadKeyStore(KEYSTORE_LOCATION + TRUSTSTORE_FILE_NAME, TRUSTSTORE_PASSWORD);
    SSLFactory sslFactory = SSLFactory.builder().withTrustMaterial(trustStore).build();
    assertThat(sslFactory.getSslContext()).isNotNull();
    assertThat(sslFactory.getKeyManager()).isNotPresent();
    assertThat(sslFactory.getTrustManager()).isNotNull();
    assertThat(sslFactory.getTrustedCertificates()).isNotEmpty();
    assertThat(sslFactory.getTrustManager()).isNotNull();
    assertThat(sslFactory.getHostnameVerifier()).isNotNull();
    LayeredConnectionSocketFactory socketFactory = Apache4SslUtils.toSocketFactory(sslFactory);
    assertThat(socketFactory).isNotNull();
}
Also used : SSLFactory(nl.altindag.ssl.SSLFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) KeyStore(java.security.KeyStore) Test(org.junit.jupiter.api.Test)

Example 13 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project OpenSearch by opensearch-project.

the class ServerUtils method execute.

/**
 * Executes the supplied request, optionally applying HTTP basic auth if the
 * username and pasword field are supplied.
 * @param request the request to execute
 * @param username the username to supply, or null
 * @param password the password to supply, or null
 * @param caCert path to the ca certificate the server side ssl cert was generated from, or no if not using ssl
 * @return the response from the server
 * @throws IOException if an error occurs
 */
private static HttpResponse execute(Request request, String username, String password, Path caCert) throws Exception {
    final Executor executor;
    if (caCert != null) {
        try (InputStream inStream = Files.newInputStream(caCert)) {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);
            KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());
            truststore.load(null, null);
            truststore.setCertificateEntry("myClusterCA", cert);
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(truststore, null);
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(truststore);
            SSLContext context = SSLContext.getInstance("TLSv1.2");
            context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
            final LayeredConnectionSocketFactory ssl = new SSLConnectionSocketFactory(context);
            final Registry<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", ssl).build();
            PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(sfr);
            connectionManager.setDefaultMaxPerRoute(100);
            connectionManager.setMaxTotal(200);
            connectionManager.setValidateAfterInactivity(1000);
            executor = Executor.newInstance(HttpClientBuilder.create().setConnectionManager(connectionManager).build());
        }
    } else {
        executor = Executor.newInstance();
    }
    if (username != null && password != null) {
        executor.auth(username, password);
        executor.authPreemptive(new HttpHost("localhost", 9200));
    }
    return executor.execute(request).returnResponse();
}
Also used : LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) InputStream(java.io.InputStream) SSLContext(javax.net.ssl.SSLContext) CertificateFactory(java.security.cert.CertificateFactory) KeyStore(java.security.KeyStore) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509Certificate(java.security.cert.X509Certificate) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) Executor(org.apache.http.client.fluent.Executor) HttpHost(org.apache.http.HttpHost) TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Example 14 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project inception by inception-project.

the class PerThreadSslCheckingHttpClientUtils method newCertCheckAwareSSLConnectionSocketFactory.

private static LayeredConnectionSocketFactory newCertCheckAwareSSLConnectionSocketFactory() {
    return new LayeredConnectionSocketFactory() {

        private SSLConnectionSocketFactory factoryWithChecks;

        private SSLConnectionSocketFactory factoryWithoutSslChecks;

        {
            final String[] supportedProtocols = split(System.getProperty("https.protocols"));
            final String[] supportedCipherSuites = split(System.getProperty("https.cipherSuites"));
            HostnameVerifier defaultHostNameVerifier = new DefaultHostnameVerifier(PublicSuffixMatcherLoader.getDefault());
            factoryWithChecks = new SSLConnectionSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(), supportedProtocols, supportedCipherSuites, defaultHostNameVerifier);
            try {
                SSLContextBuilder builder = new SSLContextBuilder();
                builder.loadTrustMaterial(null, (X509Certificate[] chain, String authType) -> true);
                HostnameVerifier hostNameVerifier = (String hostname, SSLSession session) -> true;
                factoryWithoutSslChecks = new SSLConnectionSocketFactory(builder.build(), hostNameVerifier);
            } catch (Exception e) {
                // key management exception, etc.
                throw new RuntimeException(e);
            }
        }

        @Override
        public Socket createSocket(HttpContext aContext) throws IOException {
            LOG.trace("createSocket (SSL checks: {})", SSL_VERIFICATION_ENABLED.get().peek());
            if (SSL_VERIFICATION_ENABLED.get().peek()) {
                return factoryWithChecks.createSocket(aContext);
            } else {
                return factoryWithoutSslChecks.createSocket(aContext);
            }
        }

        @Override
        public Socket connectSocket(int aConnectTimeout, Socket aSock, HttpHost aHost, InetSocketAddress aRemoteAddress, InetSocketAddress aLocalAddress, HttpContext aContext) throws IOException {
            LOG.trace("connectSocket (SSL checks: {})", SSL_VERIFICATION_ENABLED.get().peek());
            if (SSL_VERIFICATION_ENABLED.get().peek()) {
                return factoryWithChecks.connectSocket(aConnectTimeout, aSock, aHost, aRemoteAddress, aLocalAddress, aContext);
            } else {
                return factoryWithoutSslChecks.connectSocket(aConnectTimeout, aSock, aHost, aRemoteAddress, aLocalAddress, aContext);
            }
        }

        @Override
        public Socket createLayeredSocket(Socket aSocket, String aTarget, int aPort, HttpContext aContext) throws IOException, UnknownHostException {
            LOG.trace("createLayeredSocket (SSL checks: {})", SSL_VERIFICATION_ENABLED.get().peek());
            if (SSL_VERIFICATION_ENABLED.get().peek()) {
                return factoryWithChecks.createLayeredSocket(aSocket, aTarget, aPort, aContext);
            } else {
                return factoryWithoutSslChecks.createLayeredSocket(aSocket, aTarget, aPort, aContext);
            }
        }
    };
}
Also used : LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) InetSocketAddress(java.net.InetSocketAddress) SSLSession(javax.net.ssl.SSLSession) HttpContext(org.apache.http.protocol.HttpContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) HostnameVerifier(javax.net.ssl.HostnameVerifier) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) HttpHost(org.apache.http.HttpHost) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) Socket(java.net.Socket)

Example 15 with LayeredConnectionSocketFactory

use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project SEPA by arces-wot.

the class SSLManager method getSSLHttpClientTrustAllCa.

public CloseableHttpClient getSSLHttpClientTrustAllCa(String protocol) throws SEPASecurityException {
    // Trust own CA and all self-signed certificates and allow the specified
    // protocols
    LayeredConnectionSocketFactory sslsf = null;
    try {
        SSLContext ctx = SSLContext.getInstance(protocol);
        ctx.init(null, trustAllCerts, new java.security.SecureRandom());
        sslsf = new SSLConnectionSocketFactory(ctx, protocols, null, this);
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        logger.error(e.getMessage());
        if (logger.isTraceEnabled())
            e.printStackTrace();
        throw new SEPASecurityException(e.getMessage());
    }
    HttpClientBuilder clientFactory = HttpClients.custom().setSSLSocketFactory(sslsf);
    return clientFactory.build();
}
Also used : LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException)

Aggregations

LayeredConnectionSocketFactory (org.apache.http.conn.socket.LayeredConnectionSocketFactory)27 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)19 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)16 SSLContext (javax.net.ssl.SSLContext)13 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)13 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)12 KeyManagementException (java.security.KeyManagementException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 IOException (java.io.IOException)5 Socket (java.net.Socket)5 KeyStore (java.security.KeyStore)5 HostnameVerifier (javax.net.ssl.HostnameVerifier)5 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)5 HttpHost (org.apache.http.HttpHost)5 HttpClient (org.apache.http.client.HttpClient)4 File (java.io.File)3 InetSocketAddress (java.net.InetSocketAddress)3 KeyStoreException (java.security.KeyStoreException)3 RequestConfig (org.apache.http.client.config.RequestConfig)3 SocketConfig (org.apache.http.config.SocketConfig)3