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();
}
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();
}
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();
}
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);
}
}
};
}
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();
}
Aggregations