Skip to content

Commit 030b7f2

Browse files
authored
ZOOKEEPER-4954: Use FIPS style hostname verification when no custom t…
ZOOKEEPER-4954: Use FIPS style hostname verification when no custom truststore is specified Reviewers: anmolnar Author: stoty Closes #2283 from stoty/ZOOKEEPER-4954
1 parent 3d6c0d1 commit 030b7f2

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

zookeeper-server/src/main/java/org/apache/zookeeper/common/ClientX509Util.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public SslContext createNettySslContextForClient(ZKConfig config)
9393

9494
SslContext sslContext1 = sslContextBuilder.build();
9595

96-
if (getFipsMode(config) && isServerHostnameVerificationEnabled(config)) {
96+
if ((getFipsMode(config) || tm == null) && isServerHostnameVerificationEnabled(config)) {
9797
return addHostnameVerification(sslContext1, "Server");
9898
} else {
9999
return sslContext1;
@@ -138,7 +138,7 @@ public SslContext createNettySslContextForServer(ZKConfig config, KeyManager key
138138

139139
SslContext sslContext1 = sslContextBuilder.build();
140140

141-
if (getFipsMode(config) && isClientHostnameVerificationEnabled(config)) {
141+
if ((getFipsMode(config) || trustManager == null) && isClientHostnameVerificationEnabled(config)) {
142142
return addHostnameVerification(sslContext1, "Client");
143143
} else {
144144
return sslContext1;

zookeeper-server/src/test/java/org/apache/zookeeper/common/X509UtilTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import static org.junit.jupiter.api.Assertions.assertFalse;
2424
import static org.junit.jupiter.api.Assertions.assertThrows;
2525
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
import io.netty.buffer.UnpooledByteBufAllocator;
27+
import io.netty.handler.ssl.SslContext;
2628
import java.io.IOException;
2729
import java.net.InetAddress;
2830
import java.net.InetSocketAddress;
@@ -45,6 +47,7 @@
4547
import javax.net.ssl.HandshakeCompletedEvent;
4648
import javax.net.ssl.HandshakeCompletedListener;
4749
import javax.net.ssl.SSLContext;
50+
import javax.net.ssl.SSLEngine;
4851
import javax.net.ssl.SSLHandshakeException;
4952
import javax.net.ssl.SSLServerSocket;
5053
import javax.net.ssl.SSLSocket;
@@ -58,6 +61,7 @@
5861
import org.junit.jupiter.params.ParameterizedTest;
5962
import org.junit.jupiter.params.provider.MethodSource;
6063

64+
6165
public class X509UtilTest extends BaseX509ParameterizedTestCase {
6266

6367
private X509Util x509Util;
@@ -754,6 +758,28 @@ public void testCreateSSLContext_ocspWithJreProvider(
754758
}
755759
}
756760

761+
@ParameterizedTest
762+
@MethodSource("data")
763+
public void testCreateSSLContext_hostnameVerificationNoCustomTrustStore(X509KeyType caKeyType,
764+
X509KeyType certKeyType, String keyPassword, Integer paramIndex) throws Exception {
765+
init(caKeyType, certKeyType, keyPassword, paramIndex);
766+
// No truststore
767+
System.clearProperty(x509Util.getSslTruststoreLocationProperty());
768+
// Verify client hostname too
769+
System.setProperty(x509Util.getSslClientHostnameVerificationEnabledProperty(), "true");
770+
ZKConfig zkConfig = new ZKConfig();
771+
try (ClientX509Util clientX509Util = new ClientX509Util();) {
772+
UnpooledByteBufAllocator byteBufAllocator = new UnpooledByteBufAllocator(false);
773+
SslContext clientContext = clientX509Util.createNettySslContextForClient(zkConfig);
774+
SSLEngine clientEngine = clientContext.newEngine(byteBufAllocator);
775+
assertEquals(clientEngine.getSSLParameters().getEndpointIdentificationAlgorithm(), "HTTPS");
776+
777+
SslContext serverContext = clientX509Util.createNettySslContextForServer(zkConfig);
778+
SSLEngine serverEngine = serverContext.newEngine(byteBufAllocator);
779+
assertEquals(serverEngine.getSSLParameters().getEndpointIdentificationAlgorithm(), "HTTPS");
780+
}
781+
}
782+
757783
private static void forceClose(Socket s) {
758784
if (s == null || s.isClosed()) {
759785
return;

0 commit comments

Comments
 (0)