Skip to content

Commit 72ddee6

Browse files
committed
ZOOKEEPER-5045: Fall back to TLSv1.2 default in FIPS mode
1 parent 8184fd1 commit 72ddee6

3 files changed

Lines changed: 41 additions & 17 deletions

File tree

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,17 @@ public abstract class X509Util implements Closeable, AutoCloseable {
8787
}
8888
}
8989

90-
public static final String DEFAULT_PROTOCOL = defaultTlsProtocol();
91-
9290
/**
93-
* Return TLSv1.3 or TLSv1.2 depending on Java runtime version being used.
91+
* Return TLSv1.2 when FIPS mode is enabled.
92+
* Otherwise, returns TLSv1.3 or TLSv1.2 depending on Java runtime version being used.
9493
* TLSv1.3 was first introduced in JDK11 and back-ported to OpenJDK 8u272.
9594
*/
96-
private static String defaultTlsProtocol() {
95+
public static String defaultTlsProtocol(ZKConfig config) {
96+
if (getFipsMode(config)) {
97+
LOG.info("FIPS mode is enabled. Fall back to TLSv1.2 as the default protocol.");
98+
return TLS_1_2;
99+
}
100+
97101
String defaultProtocol = TLS_1_2;
98102
List<String> supported = new ArrayList<>();
99103
try {
@@ -410,8 +414,8 @@ public SSLContextAndOptions createSSLContextAndOptionsFromConfig(ZKConfig config
410414
+ ": "
411415
+ trustStoreTypeProp, e);
412416
}
413-
414-
String protocol = config.getProperty(sslProtocolProperty, DEFAULT_PROTOCOL);
417+
String defaultTlsProtocol = defaultTlsProtocol(config);
418+
String protocol = config.getProperty(sslProtocolProperty, defaultTlsProtocol);
415419
try {
416420
SSLContext sslContext = SSLContext.getInstance(protocol);
417421
sslContext.init(keyManagers, trustManagers, null);

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
package org.apache.zookeeper.common;
2020

21+
import static org.apache.zookeeper.common.X509Util.FIPS_MODE_PROPERTY;
22+
import static org.apache.zookeeper.common.X509Util.TLS_1_2;
23+
import static org.apache.zookeeper.common.X509Util.TLS_1_3;
2124
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2225
import static org.junit.jupiter.api.Assertions.assertEquals;
2326
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -90,6 +93,7 @@ public void cleanUp() {
9093
System.clearProperty(x509Util.getSslHandshakeDetectionTimeoutMillisProperty());
9194
System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY);
9295
System.clearProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET);
96+
System.clearProperty(FIPS_MODE_PROPERTY);
9397
x509Util.close();
9498
}
9599

@@ -100,24 +104,36 @@ public void testCreateSSLContextWithoutCustomProtocol(
100104
X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)
101105
throws Exception {
102106
init(caKeyType, certKeyType, keyPassword, paramIndex);
107+
System.setProperty(FIPS_MODE_PROPERTY, Boolean.FALSE.toString());
103108
SSLContext sslContext = x509Util.getDefaultSSLContext();
104-
assertEquals(X509Util.DEFAULT_PROTOCOL, sslContext.getProtocol());
109+
String defaultTlsProtocol = X509Util.defaultTlsProtocol(new ZKConfig());
110+
assertEquals(defaultTlsProtocol, sslContext.getProtocol());
105111

106-
// Check that TLSv1.3 is selected in JDKs that support it (OpenJDK 8u272 and later).
107112
List<String> supported = Arrays.asList(SSLContext.getDefault().getSupportedSSLParameters().getProtocols());
108-
if (supported.contains(X509Util.TLS_1_3)) {
109-
// SSLContext protocol.
110-
assertEquals(X509Util.TLS_1_3, sslContext.getProtocol());
111-
// Enabled protocols.
113+
if (supported.contains(TLS_1_3)) {
114+
assertEquals(TLS_1_3, sslContext.getProtocol());
112115
List<String> protos = Arrays.asList(sslContext.getDefaultSSLParameters().getProtocols());
113-
assertTrue(protos.contains(X509Util.TLS_1_2));
114-
assertTrue(protos.contains(X509Util.TLS_1_3));
116+
assertTrue(protos.contains(TLS_1_2));
117+
assertTrue(protos.contains(TLS_1_3));
115118
} else {
116-
assertEquals(X509Util.TLS_1_2, sslContext.getProtocol());
117-
assertArrayEquals(new String[]{X509Util.TLS_1_2}, sslContext.getDefaultSSLParameters().getProtocols());
119+
assertEquals(TLS_1_2, sslContext.getProtocol());
120+
assertArrayEquals(new String[]{TLS_1_2}, sslContext.getDefaultSSLParameters().getProtocols());
118121
}
119122
}
120123

124+
@ParameterizedTest
125+
@MethodSource("data")
126+
@Timeout(value = 5)
127+
public void testCreateSSLContextWithoutCustomProtocol_FIPSEnabled(
128+
X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex)
129+
throws Exception {
130+
init(caKeyType, certKeyType, keyPassword, paramIndex);
131+
System.setProperty(FIPS_MODE_PROPERTY, Boolean.TRUE.toString());
132+
SSLContext sslContext = x509Util.getDefaultSSLContext();
133+
assertEquals(TLS_1_2, sslContext.getProtocol());
134+
assertArrayEquals(new String[]{TLS_1_2}, sslContext.getDefaultSSLParameters().getProtocols());
135+
}
136+
121137
@ParameterizedTest
122138
@MethodSource("data")
123139
@Timeout(value = 5)
@@ -873,4 +889,5 @@ private void testCreateSSLContext_withWrongPasswordFromFile(final String keyPass
873889
x509Util.getDefaultSSLContext();
874890
});
875891
}
892+
876893
}

zookeeper-server/src/test/java/org/apache/zookeeper/server/admin/CommandAuthTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import org.apache.zookeeper.common.ClientX509Util;
4949
import org.apache.zookeeper.common.QuorumX509Util;
5050
import org.apache.zookeeper.common.X509Exception;
51+
import org.apache.zookeeper.common.X509Util;
52+
import org.apache.zookeeper.common.ZKConfig;
5153
import org.apache.zookeeper.data.ACL;
5254
import org.apache.zookeeper.data.Id;
5355
import org.apache.zookeeper.server.NettyServerCnxnFactory;
@@ -281,7 +283,8 @@ private void setupTLS() throws Exception {
281283
System.setProperty("zookeeper.admin.needClientAuth", "true");
282284

283285
// create SSLContext
284-
final SSLContext sslContext = SSLContext.getInstance(ClientX509Util.DEFAULT_PROTOCOL);
286+
String defaultTlsProtocol = X509Util.defaultTlsProtocol(new ZKConfig());
287+
final SSLContext sslContext = SSLContext.getInstance(defaultTlsProtocol);
285288
final X509AuthenticationProvider authProvider = (X509AuthenticationProvider) ProviderRegistry.getProvider("x509");
286289
if (authProvider == null) {
287290
throw new X509Exception.SSLContextException("Could not create SSLContext with x509 auth provider");

0 commit comments

Comments
 (0)