Skip to content

Commit 2425dcd

Browse files
committed
Do not set Tomcat's key store and key pass when null
Fixes gh-24041
1 parent d3ea48b commit 2425dcd

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/SslConnectorCustomizer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,8 +69,12 @@ protected void configureSsl(AbstractHttp11JsseProtocol<?> protocol, Ssl ssl, Ssl
6969
protocol.setSSLEnabled(true);
7070
protocol.setSslProtocol(ssl.getProtocol());
7171
configureSslClientAuth(protocol, ssl);
72-
protocol.setKeystorePass(ssl.getKeyStorePassword());
73-
protocol.setKeyPass(ssl.getKeyPassword());
72+
if (ssl.getKeyStorePassword() != null) {
73+
protocol.setKeystorePass(ssl.getKeyStorePassword());
74+
}
75+
if (ssl.getKeyPassword() != null) {
76+
protocol.setKeyPass(ssl.getKeyPassword());
77+
}
7478
protocol.setKeyAlias(ssl.getKeyAlias());
7579
String ciphers = StringUtils.arrayToCommaDelimitedString(ssl.getCiphers());
7680
if (StringUtils.hasText(ciphers)) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/SslConnectorCustomizerTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.catalina.connector.Connector;
2929
import org.apache.catalina.startup.Tomcat;
3030
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
31+
import org.apache.coyote.http11.Http11NioProtocol;
3132
import org.apache.tomcat.util.net.SSLHostConfig;
3233
import org.junit.jupiter.api.AfterEach;
3334
import org.junit.jupiter.api.BeforeEach;
@@ -185,6 +186,26 @@ void customizeWhenSslIsEnabledWithNoKeyStoreThrowsWebServerException() {
185186
.withMessageContaining("Could not load key store 'null'");
186187
}
187188

189+
@Test
190+
void keyStorePasswordIsNotSetWhenNull() {
191+
Http11NioProtocol protocol = (Http11NioProtocol) this.tomcat.getConnector().getProtocolHandler();
192+
protocol.setKeystorePass("password");
193+
Ssl ssl = new Ssl();
194+
ssl.setKeyStore("src/test/resources/test.jks");
195+
new SslConnectorCustomizer(ssl, null).customize(this.tomcat.getConnector());
196+
assertThat(protocol.getKeystorePass()).isEqualTo("password");
197+
}
198+
199+
@Test
200+
void keyPasswordIsNotSetWhenNull() {
201+
Http11NioProtocol protocol = (Http11NioProtocol) this.tomcat.getConnector().getProtocolHandler();
202+
protocol.setKeyPass("password");
203+
Ssl ssl = new Ssl();
204+
ssl.setKeyStore("src/test/resources/test.jks");
205+
new SslConnectorCustomizer(ssl, null).customize(this.tomcat.getConnector());
206+
assertThat(protocol.getKeyPass()).isEqualTo("password");
207+
}
208+
188209
private KeyStore loadStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
189210
KeyStore keyStore = KeyStore.getInstance("JKS");
190211
Resource resource = new ClassPathResource("test.jks");

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ protected final void testBasicSslWithKeyStore(String keyStore, String keyPasswor
123123
Ssl ssl = new Ssl();
124124
ssl.setKeyStore(keyStore);
125125
ssl.setKeyPassword(keyPassword);
126+
ssl.setKeyStorePassword("secret");
126127
factory.setSsl(ssl);
127128
this.webServer = factory.getWebServer(new EchoHandler());
128129
this.webServer.start();
@@ -142,6 +143,7 @@ void sslWithValidAlias() {
142143
AbstractReactiveWebServerFactory factory = getFactory();
143144
Ssl ssl = new Ssl();
144145
ssl.setKeyStore(keyStore);
146+
ssl.setKeyStorePassword("secret");
145147
ssl.setKeyPassword(keyPassword);
146148
ssl.setKeyAlias("test-alias");
147149
factory.setSsl(ssl);
@@ -187,6 +189,7 @@ void sslWantsClientAuthenticationSucceedsWithClientCertificate() throws Exceptio
187189
ssl.setClientAuth(Ssl.ClientAuth.WANT);
188190
ssl.setKeyStore("classpath:test.jks");
189191
ssl.setKeyPassword("password");
192+
ssl.setKeyStorePassword("secret");
190193
ssl.setTrustStore("classpath:test.jks");
191194
testClientAuthSuccess(ssl, buildTrustAllSslWithClientKeyConnector());
192195
}
@@ -198,6 +201,7 @@ void sslWantsClientAuthenticationSucceedsWithoutClientCertificate() {
198201
ssl.setKeyStore("classpath:test.jks");
199202
ssl.setKeyPassword("password");
200203
ssl.setTrustStore("classpath:test.jks");
204+
ssl.setKeyStorePassword("secret");
201205
testClientAuthSuccess(ssl, buildTrustAllSslConnector());
202206
}
203207

@@ -232,6 +236,7 @@ void sslNeedsClientAuthenticationSucceedsWithClientCertificate() throws Exceptio
232236
Ssl ssl = new Ssl();
233237
ssl.setClientAuth(Ssl.ClientAuth.NEED);
234238
ssl.setKeyStore("classpath:test.jks");
239+
ssl.setKeyStorePassword("secret");
235240
ssl.setKeyPassword("password");
236241
ssl.setTrustStore("classpath:test.jks");
237242
testClientAuthSuccess(ssl, buildTrustAllSslWithClientKeyConnector());
@@ -242,6 +247,7 @@ void sslNeedsClientAuthenticationFailsWithoutClientCertificate() {
242247
Ssl ssl = new Ssl();
243248
ssl.setClientAuth(Ssl.ClientAuth.NEED);
244249
ssl.setKeyStore("classpath:test.jks");
250+
ssl.setKeyStorePassword("secret");
245251
ssl.setKeyPassword("password");
246252
ssl.setTrustStore("classpath:test.jks");
247253
testClientAuthFailure(ssl, buildTrustAllSslConnector());

0 commit comments

Comments
 (0)