Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import javax.net.ssl.HostnameVerifier;
Expand All @@ -41,6 +43,8 @@
import javax.net.ssl.SSLSocket;

import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
import org.apache.hc.client5.http.ssl.ConscryptClientTlsStrategy;
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
import org.apache.hc.client5.http.ssl.HostnameVerificationPolicy;
import org.apache.hc.client5.http.ssl.HttpsSupport;
Expand Down Expand Up @@ -422,6 +426,55 @@ void testHostnameVerificationClient() throws Exception {
}
}

@Test
void testHostnameVerificationClientWithoutVerifier() throws Exception {
// @formatter:off
this.server = ServerBootstrap.bootstrap()
.setLocalAddress(InetAddress.getLoopbackAddress())
.setSslContext(SSLTestContexts.createServerSSLContext())
.setRequestRouter((r, c) -> null)
.create();
// @formatter:on
this.server.start();

final SSLContext sslContext = SSLTestContexts.createClientSSLContext();
final List<TlsSocketStrategy> tlsStrategies = Arrays.asList(
new DefaultClientTlsStrategy(sslContext, HostnameVerificationPolicy.CLIENT, null),
new ConscryptClientTlsStrategy(sslContext),
ClientTlsStrategyBuilder.create()
.setSslContext(sslContext)
.setHostVerificationPolicy(HostnameVerificationPolicy.CLIENT)
.buildClassic());

final HttpHost target1 = new HttpHost("https", InetAddress.getLoopbackAddress(), "localhost", server.getLocalPort());
final HttpHost target2 = new HttpHost("https", InetAddress.getLoopbackAddress(), "some-other-host", server.getLocalPort());

for (final TlsSocketStrategy tlsStrategy : tlsStrategies) {
try (final Socket socket = new Socket(InetAddress.getLoopbackAddress(), server.getLocalPort())) {
final HttpClientContext context = HttpClientContext.create();
final SSLSocket upgradedSocket = tlsStrategy.upgrade(
socket,
target1.getHostName(),
target1.getPort(),
null,
context);
final SSLSession session = upgradedSocket.getSession();
Assertions.assertTrue(Objects.toString(session.getPeerPrincipal()).startsWith("CN=localhost"));
}

try (final Socket socket = new Socket(InetAddress.getLoopbackAddress(), server.getLocalPort())) {
final HttpClientContext context = HttpClientContext.create();
Assertions.assertThrows(SSLPeerUnverifiedException.class, () ->
tlsStrategy.upgrade(
socket,
target2.getHostName(),
target2.getPort(),
null,
context));
}
}
}

@Test
void testHostnameVerificationBuiltIn() throws Exception {
// @formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ abstract class AbstractClientTlsStrategy implements TlsStrategy, TlsSocketStrate
this.sslBufferManagement = sslBufferManagement != null ? sslBufferManagement : SSLBufferMode.STATIC;
this.hostnameVerificationPolicy = hostnameVerificationPolicy != null ? hostnameVerificationPolicy :
(hostnameVerifier != null ? HostnameVerificationPolicy.BOTH : HostnameVerificationPolicy.BUILTIN);
this.hostnameVerifier = hostnameVerifier;
this.hostnameVerifier = hostnameVerifier != null ? hostnameVerifier :
(this.hostnameVerificationPolicy == HostnameVerificationPolicy.BUILTIN ? null : HttpsSupport.getDefaultHostnameVerifier());
}

/**
Expand Down