-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
SslConfig.cipherSuiteFilter()
(#2798)
Motivation: Sometimes users can configure ciphers that are not supported by the current `SSLEngine`. As a result, they are getting a runtime exception. Modifications: - Add new API `SslConfig.cipherSuiteFilter()` that allows users to configure how Netty should consume the configured ciphers: take them all as-is or filter out unsupported ciphers from the passed list; - Test new behavior; Result: Users can configure `SslConfig` to filter out unsupported ciphers.
- Loading branch information
1 parent
f9e3f0b
commit 4da962a
Showing
8 changed files
with
246 additions
and
16 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
servicetalk-http-netty/src/test/java/io/servicetalk/http/netty/SslCipherSuiteFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright © 2024 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.http.netty; | ||
|
||
import io.servicetalk.http.api.BlockingHttpClient; | ||
import io.servicetalk.http.api.BlockingHttpConnection; | ||
import io.servicetalk.http.api.HttpResponse; | ||
import io.servicetalk.http.api.HttpServerContext; | ||
import io.servicetalk.test.resources.DefaultTestCerts; | ||
import io.servicetalk.transport.api.ClientSslConfigBuilder; | ||
import io.servicetalk.transport.api.ServerSslConfigBuilder; | ||
import io.servicetalk.transport.api.SslProvider; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
|
||
import java.io.IOException; | ||
import javax.net.ssl.SSLSession; | ||
|
||
import static io.servicetalk.http.api.HttpResponseStatus.OK; | ||
import static io.servicetalk.test.resources.DefaultTestCerts.serverPemHostname; | ||
import static io.servicetalk.transport.api.SslConfig.CipherSuiteFilter.PROVIDED; | ||
import static io.servicetalk.transport.api.SslConfig.CipherSuiteFilter.SUPPORTED; | ||
import static io.servicetalk.transport.netty.internal.AddressUtils.localAddress; | ||
import static io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.containsStringIgnoringCase; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class SslCipherSuiteFilterTest { | ||
|
||
private static final String SUPPORTED_CIPHER = "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"; | ||
private static final String UNSUPPORTED_CIPHER = "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305"; | ||
|
||
@ParameterizedTest(name = "{displayName} [{index}] provider={0}") | ||
@EnumSource(SslProvider.class) | ||
void clientWithIdentityFilterAndUnsupportedCipher(SslProvider provider) throws Exception { | ||
try (HttpServerContext serverContext = HttpServers.forAddress(localAddress(0)) | ||
.sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey) | ||
.build()) | ||
.listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok())) { | ||
|
||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { | ||
try (BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)) | ||
.sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem) | ||
.peerHost(serverPemHostname()) | ||
.provider(provider) | ||
.ciphers(SUPPORTED_CIPHER, UNSUPPORTED_CIPHER) | ||
.cipherSuiteFilter(PROVIDED) | ||
.build()) | ||
.buildBlocking()) { | ||
|
||
if (provider == SslProvider.JDK) { | ||
client.request(client.get("/")); | ||
} | ||
} | ||
}); | ||
if (provider == SslProvider.OPENSSL) { | ||
e = (IllegalArgumentException) e.getCause().getCause(); | ||
} | ||
assertThat(e.getMessage(), containsStringIgnoringCase("unsupported")); | ||
assertThat(e.getMessage(), containsString(UNSUPPORTED_CIPHER)); | ||
} | ||
} | ||
|
||
@Test | ||
void serverWithIdentityFilterAndUnsupportedCipherOpenSslProvider() throws Exception { | ||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { | ||
HttpServers.forAddress(localAddress(0)) | ||
.sslConfig(new ServerSslConfigBuilder( | ||
DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey) | ||
.provider(SslProvider.OPENSSL) | ||
.ciphers(SUPPORTED_CIPHER, UNSUPPORTED_CIPHER) | ||
.cipherSuiteFilter(PROVIDED) | ||
.build()) | ||
.listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok()); | ||
}); | ||
e = (IllegalArgumentException) e.getCause().getCause(); | ||
assertThat(e.getMessage(), containsStringIgnoringCase("unsupported")); | ||
assertThat(e.getMessage(), containsString(UNSUPPORTED_CIPHER)); | ||
} | ||
|
||
@Test | ||
void serverWithIdentityFilterAndUnsupportedCipherJdkProvider() throws Exception { | ||
try (HttpServerContext serverContext = HttpServers.forAddress(localAddress(0)) | ||
.sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey) | ||
.provider(SslProvider.JDK) | ||
.ciphers(SUPPORTED_CIPHER, UNSUPPORTED_CIPHER) | ||
.cipherSuiteFilter(PROVIDED) | ||
.build()) | ||
.listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok()); | ||
BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)) | ||
.sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem) | ||
.peerHost(serverPemHostname()) | ||
.build()) | ||
.buildBlocking()) { | ||
assertThrows(IOException.class, () -> client.request(client.get("/"))); | ||
} | ||
} | ||
|
||
@ParameterizedTest(name = "{displayName} [{index}] provider={0}") | ||
@EnumSource(SslProvider.class) | ||
void supportedFilterWithUnsupportedCipher(SslProvider provider) throws Exception { | ||
try (HttpServerContext serverContext = HttpServers.forAddress(localAddress(0)) | ||
.sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey) | ||
.provider(provider) | ||
.ciphers(SUPPORTED_CIPHER, UNSUPPORTED_CIPHER) | ||
.cipherSuiteFilter(SUPPORTED) | ||
// Enforce TLSv1.2 because OPENSSL provider ignores ciphers when TLSv1.3 is used | ||
.sslProtocols("TLSv1.2") | ||
.build()) | ||
.listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok()); | ||
BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)) | ||
.sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem) | ||
.peerHost(serverPemHostname()) | ||
.provider(provider) | ||
.ciphers(SUPPORTED_CIPHER, UNSUPPORTED_CIPHER) | ||
.cipherSuiteFilter(SUPPORTED) | ||
// Enforce TLSv1.2 because OPENSSL provider ignores ciphers when TLSv1.3 is used | ||
.sslProtocols("TLSv1.2") | ||
.build()) | ||
.buildBlocking(); | ||
BlockingHttpConnection connection = client.reserveConnection(client.get("/"))) { | ||
|
||
SSLSession sslSession = connection.connectionContext().sslSession(); | ||
assertThat(sslSession, is(notNullValue())); | ||
assertThat(sslSession.getCipherSuite(), is(SUPPORTED_CIPHER)); | ||
|
||
HttpResponse response = connection.request(client.get("/")); | ||
assertThat(response.status(), is(OK)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.