You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2024/02/06 08:04:40 UTC
(httpcomponents-core) branch master updated: TLS client endpoints to make use of JSSE Endpoint Identification by default
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
The following commit(s) were added to refs/heads/master by this push:
new 49f314b1e TLS client endpoints to make use of JSSE Endpoint Identification by default
49f314b1e is described below
commit 49f314b1ed4702682114b3917081fcbeb990b142
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sun Jan 28 16:44:42 2024 +0100
TLS client endpoints to make use of JSSE Endpoint Identification by default
---
.../hc/core5/http2/ssl/H2ClientTlsStrategy.java | 10 +++-
.../apache/hc/core5/http2/ssl/H2TlsSupport.java | 20 +++++--
.../testing/classic/ClassicTLSIntegrationTest.java | 36 ++++++++++++
.../hc/core5/testing/nio/TLSIntegrationTest.java | 35 ++++++++++++
.../http/impl/bootstrap/RequesterBootstrap.java | 2 +-
.../core5/http/impl/bootstrap/ServerBootstrap.java | 2 +-
.../core5/http/io/ssl/DefaultTlsSetupHandler.java | 20 +++++++
.../core5/http/nio/ssl/BasicClientTlsStrategy.java | 18 +++++-
.../apache/hc/core5/http/nio/ssl/TlsSupport.java | 14 +++--
.../apache/hc/core5/ssl/TestSSLContextBuilder.java | 65 ++++++++++++++++++++++
10 files changed, 207 insertions(+), 15 deletions(-)
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/ssl/H2ClientTlsStrategy.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/ssl/H2ClientTlsStrategy.java
index dea438b93..550c2fca6 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/ssl/H2ClientTlsStrategy.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/ssl/H2ClientTlsStrategy.java
@@ -30,6 +30,7 @@ package org.apache.hc.core5.http2.ssl;
import java.net.SocketAddress;
import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLParameters;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.HttpHost;
@@ -110,7 +111,14 @@ public class H2ClientTlsStrategy implements TlsStrategy {
sslContext,
endpoint,
sslBufferMode,
- H2TlsSupport.enforceRequirements(attachment, initializer),
+ (e, sslEngine) -> {
+ final SSLParameters sslParameters = sslEngine.getSSLParameters();
+ sslParameters.setEndpointIdentificationAlgorithm(URIScheme.HTTPS.id);
+ sslEngine.setSSLParameters(H2TlsSupport.enforceRequirements(attachment, sslParameters));
+ if (initializer != null) {
+ initializer.initialize(e, sslEngine);
+ }
+ },
verifier,
handshakeTimeout,
callback);
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/ssl/H2TlsSupport.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/ssl/H2TlsSupport.java
index 3cf0263ad..2d7b31542 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/ssl/H2TlsSupport.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/ssl/H2TlsSupport.java
@@ -67,16 +67,24 @@ public final class H2TlsSupport {
}
}
+ /**
+ * @since 5.3
+ */
+ public static SSLParameters enforceRequirements(
+ final Object attachment,
+ final SSLParameters sslParameters) {
+ sslParameters.setProtocols(TLS.excludeWeak(sslParameters.getProtocols()));
+ sslParameters.setCipherSuites(TlsCiphers.excludeH2Blacklisted(sslParameters.getCipherSuites()));
+ setEnableRetransmissions(sslParameters, false);
+ sslParameters.setApplicationProtocols(selectApplicationProtocols(attachment));
+ return sslParameters;
+ }
+
public static SSLSessionInitializer enforceRequirements(
final Object attachment,
final SSLSessionInitializer initializer) {
return (endpoint, sslEngine) -> {
- final SSLParameters sslParameters = sslEngine.getSSLParameters();
- sslParameters.setProtocols(TLS.excludeWeak(sslParameters.getProtocols()));
- sslParameters.setCipherSuites(TlsCiphers.excludeH2Blacklisted(sslParameters.getCipherSuites()));
- setEnableRetransmissions(sslParameters, false);
- sslParameters.setApplicationProtocols(selectApplicationProtocols(attachment));
- sslEngine.setSSLParameters(sslParameters);
+ sslEngine.setSSLParameters(enforceRequirements(attachment, sslEngine.getSSLParameters()));
if (initializer != null) {
initializer.initialize(endpoint, sslEngine);
}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java
index 901fbfed3..ade8b5079 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java
@@ -30,8 +30,10 @@ package org.apache.hc.core5.testing.classic;
import static org.hamcrest.MatcherAssert.assertThat;
import java.io.IOException;
+import java.net.InetAddress;
import java.util.concurrent.atomic.AtomicReference;
+import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSession;
import org.apache.hc.core5.http.ClassicHttpRequest;
@@ -290,4 +292,38 @@ public class ClassicTLSIntegrationTest {
}
}
+ @Test
+ public void testHostNameVerification() throws Exception {
+ server = ServerBootstrap.bootstrap()
+ .setSslContext(SSLTestContexts.createServerSSLContext())
+ .create();
+ server.start();
+
+ requester = RequesterBootstrap.bootstrap()
+ .setSslContext(SSLTestContexts.createClientSSLContext())
+ .setSocketConfig(SocketConfig.custom()
+ .setSoTimeout(TIMEOUT)
+ .build())
+ .setStreamListener(LoggingHttp1StreamListener.INSTANCE)
+ .setConnPoolListener(LoggingConnPoolListener.INSTANCE)
+ .create();
+
+ final HttpContext context = new BasicHttpContext();
+ final HttpHost target1 = new HttpHost("https", InetAddress.getLocalHost(), "localhost", server.getLocalPort());
+ final ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.POST, "/stuff");
+ request1.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
+ try (final ClassicHttpResponse response1 = requester.execute(target1, request1, TIMEOUT, context)) {
+ EntityUtils.consume(response1.getEntity());
+ }
+
+ Assertions.assertThrows(SSLHandshakeException.class, () -> {
+ final HttpHost target2 = new HttpHost("https", InetAddress.getLocalHost(), "some-other-host", server.getLocalPort());
+ final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/stuff");
+ request2.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
+ try (final ClassicHttpResponse response2 = requester.execute(target2, request2, TIMEOUT, context)) {
+ EntityUtils.consume(response2.getEntity());
+ }
+ });
+ }
+
}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java
index e7770cec7..08c1f697c 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java
@@ -28,6 +28,7 @@
package org.apache.hc.core5.testing.nio;
import java.io.IOException;
+import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.ExecutionException;
@@ -357,6 +358,40 @@ public class TLSIntegrationTest {
Assertions.assertInstanceOf(IOException.class, cause);
}
+ @Test
+ public void testHostNameVerification() throws Exception {
+ server = createServer(new BasicClientTlsStrategy(SSLTestContexts.createServerSSLContext()));
+ server.start();
+
+ client = createClient(new BasicClientTlsStrategy(SSLTestContexts.createClientSSLContext()));
+ client.start();
+
+ final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS);
+ final ListenerEndpoint listener = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+ final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
+
+ final HttpHost target1 = new HttpHost(URIScheme.HTTPS.id, InetAddress.getLocalHost(), "localhost", address.getPort());
+ final Future<Message<HttpResponse, String>> resultFuture1 = client.execute(
+ target1,
+ new BasicRequestProducer(Method.POST, target1, "/stuff",
+ new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)),
+ new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
+ final Message<HttpResponse, String> message1 = resultFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+ Assertions.assertNotNull(message1);
+ Assertions.assertEquals(200, message1.getHead().getCode());
+
+ final HttpHost target2 = new HttpHost(URIScheme.HTTPS.id, InetAddress.getLocalHost(), "some-other-host", address.getPort());
+ final Future<Message<HttpResponse, String>> resultFuture2 = client.execute(
+ target2,
+ new BasicRequestProducer(Method.POST, target2, "/stuff",
+ new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)),
+ new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
+ final ExecutionException exception = Assertions.assertThrows(ExecutionException.class, () ->
+ resultFuture2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
+ final Throwable cause = exception.getCause();
+ Assertions.assertInstanceOf(SSLHandshakeException.class, cause);
+ }
+
static class SupportedTLSProtocolProvider implements ArgumentsProvider {
int javaVere = ReflectionUtils.determineJRELevel();
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java
index a239f2032..30d1654d9 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java
@@ -220,7 +220,7 @@ public class RequesterBootstrap {
connectFactory != null ? connectFactory : new DefaultBHttpClientConnectionFactory(
http1Config, CharCodingConfig.DEFAULT),
sslSocketFactory,
- sslSetupHandler != null ? sslSetupHandler : new DefaultTlsSetupHandler(),
+ sslSetupHandler != null ? sslSetupHandler : DefaultTlsSetupHandler.CLIENT,
sslSessionVerifier,
DefaultAddressResolver.INSTANCE);
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java
index f52741c4a..c8d97b7a6 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java
@@ -406,7 +406,7 @@ public class ServerBootstrap {
this.socketConfig != null ? this.socketConfig : SocketConfig.DEFAULT,
serverSocketFactoryCopy,
connectionFactoryCopy,
- sslSetupHandler != null ? sslSetupHandler : new DefaultTlsSetupHandler(),
+ sslSetupHandler != null ? sslSetupHandler : DefaultTlsSetupHandler.SERVER,
this.exceptionListener != null ? this.exceptionListener : ExceptionListener.NO_OP);
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/ssl/DefaultTlsSetupHandler.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/ssl/DefaultTlsSetupHandler.java
index 2e926bc39..17d03a1c5 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/ssl/DefaultTlsSetupHandler.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/ssl/DefaultTlsSetupHandler.java
@@ -30,6 +30,7 @@ package org.apache.hc.core5.http.io.ssl;
import javax.net.ssl.SSLParameters;
import org.apache.hc.core5.function.Callback;
+import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.ssl.TLS;
import org.apache.hc.core5.http.ssl.TlsCiphers;
@@ -40,10 +41,29 @@ import org.apache.hc.core5.http.ssl.TlsCiphers;
*/
public final class DefaultTlsSetupHandler implements Callback<SSLParameters> {
+ public final static DefaultTlsSetupHandler SERVER = new DefaultTlsSetupHandler(false);
+ public final static DefaultTlsSetupHandler CLIENT = new DefaultTlsSetupHandler(true);
+
+ private final boolean client;
+
+ public DefaultTlsSetupHandler() {
+ this.client = false;
+ }
+
+ /**
+ * @since 5.3
+ */
+ public DefaultTlsSetupHandler(final boolean client) {
+ this.client = client;
+ }
+
@Override
public void execute(final SSLParameters sslParameters) {
sslParameters.setProtocols(TLS.excludeWeak(sslParameters.getProtocols()));
sslParameters.setCipherSuites(TlsCiphers.excludeWeak(sslParameters.getCipherSuites()));
+ if (client) {
+ sslParameters.setEndpointIdentificationAlgorithm(URIScheme.HTTPS.id);
+ }
}
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java
index 1ba874fdd..e9f97ebda 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/BasicClientTlsStrategy.java
@@ -30,6 +30,7 @@ package org.apache.hc.core5.http.nio.ssl;
import java.net.SocketAddress;
import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLParameters;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.HttpHost;
@@ -105,8 +106,21 @@ public class BasicClientTlsStrategy implements TlsStrategy {
final Object attachment,
final Timeout handshakeTimeout,
final FutureCallback<TransportSecurityLayer> callback) {
- tlsSession.startTls(sslContext, endpoint, sslBufferMode,
- TlsSupport.enforceStrongSecurity(initializer), verifier, handshakeTimeout, callback);
+ tlsSession.startTls(
+ sslContext,
+ endpoint,
+ sslBufferMode,
+ (e, sslEngine) -> {
+ final SSLParameters sslParameters = sslEngine.getSSLParameters();
+ sslParameters.setEndpointIdentificationAlgorithm(URIScheme.HTTPS.id);
+ sslEngine.setSSLParameters(TlsSupport.enforceStrongSecurity(sslParameters));
+ if (initializer != null) {
+ initializer.initialize(e, sslEngine);
+ }
+ },
+ verifier,
+ handshakeTimeout,
+ callback);
}
/**
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsSupport.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsSupport.java
index 779f695be..e04c11a83 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsSupport.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/ssl/TlsSupport.java
@@ -40,12 +40,18 @@ import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
*/
public final class TlsSupport {
+ /**
+ * @since 5.3
+ */
+ public static SSLParameters enforceStrongSecurity(final SSLParameters sslParameters) {
+ sslParameters.setProtocols(TLS.excludeWeak(sslParameters.getProtocols()));
+ sslParameters.setCipherSuites(TlsCiphers.excludeWeak(sslParameters.getCipherSuites()));
+ return sslParameters;
+ }
+
public static SSLSessionInitializer enforceStrongSecurity(final SSLSessionInitializer initializer) {
return (endpoint, sslEngine) -> {
- final SSLParameters sslParameters = sslEngine.getSSLParameters();
- sslParameters.setProtocols(TLS.excludeWeak(sslParameters.getProtocols()));
- sslParameters.setCipherSuites(TlsCiphers.excludeWeak(sslParameters.getCipherSuites()));
- sslEngine.setSSLParameters(sslParameters);
+ sslEngine.setSSLParameters(enforceStrongSecurity(sslEngine.getSSLParameters()));
if (initializer != null) {
initializer.initialize(endpoint, sslEngine);
}
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java b/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java
index b6b46fe63..74c36ea2f 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/ssl/TestSSLContextBuilder.java
@@ -54,6 +54,7 @@ import java.util.concurrent.atomic.AtomicReference;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLSession;
@@ -716,4 +717,68 @@ public class TestSSLContextBuilder {
}
}
+ @Test
+ public void testJSSEEndpointIdentification() throws Exception {
+ final URL resource1 = getResource("/test-server.p12");
+ final String storePassword = "nopassword";
+ final String keyPassword = "nopassword";
+ final SSLContext serverSslContext = SSLContextBuilder.create()
+ .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray())
+ .build();
+ Assertions.assertNotNull(serverSslContext);
+ final URL resource2 = getResource("/test-client.p12");
+ final SSLContext clientSslContext = SSLContextBuilder.create()
+ .loadTrustMaterial(resource2, storePassword.toCharArray())
+ .build();
+ Assertions.assertNotNull(clientSslContext);
+ final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket();
+ serverSocket.bind(new InetSocketAddress(0));
+
+ this.executorService = Executors.newSingleThreadExecutor();
+ this.executorService.submit(() -> {
+ for (;;) {
+ try (SSLSocket socket = (SSLSocket) serverSocket.accept()) {
+ socket.getSession();
+ socket.shutdownOutput();
+ } catch (final IOException ex) {
+ return Boolean.FALSE;
+ }
+ }
+ });
+
+ final int localPort1 = serverSocket.getLocalPort();
+ try (final Socket clientSocket = new Socket()) {
+ clientSocket.connect(new InetSocketAddress("localhost", localPort1));
+ try (SSLSocket sslSocket = (SSLSocket) clientSslContext.getSocketFactory().createSocket(clientSocket, "localhost", -1, true)) {
+ final SSLParameters sslParameters = sslSocket.getSSLParameters();
+ sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
+ sslSocket.setSSLParameters(sslParameters);
+ sslSocket.startHandshake();
+ }
+ }
+ final int localPort2 = serverSocket.getLocalPort();
+ try (final Socket clientSocket = new Socket()) {
+ clientSocket.connect(new InetSocketAddress("localhost", localPort2));
+ try (SSLSocket sslSocket = (SSLSocket) clientSslContext.getSocketFactory().createSocket(clientSocket, "otherhost", -1, true)) {
+ final SSLParameters sslParameters = sslSocket.getSSLParameters();
+ sslParameters.setEndpointIdentificationAlgorithm(null);
+ sslSocket.setSSLParameters(sslParameters);
+ sslSocket.startHandshake();
+ }
+ }
+ final int localPort3 = serverSocket.getLocalPort();
+
+ Assertions.assertThrows(SSLException.class, () -> {
+ try (final Socket clientSocket = new Socket()) {
+ clientSocket.connect(new InetSocketAddress("localhost", localPort3));
+ try (SSLSocket sslSocket = (SSLSocket) clientSslContext.getSocketFactory().createSocket(clientSocket, "otherhost", -1, true)) {
+ final SSLParameters sslParameters = sslSocket.getSSLParameters();
+ sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
+ sslSocket.setSSLParameters(sslParameters);
+ sslSocket.startHandshake();
+ }
+ }
+ });
+ }
+
}