From d7fc5d811365593124717005b70586effcb6b0d6 Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:02:39 +0800 Subject: [PATCH 1/6] Pass peer addresses to TCP TLS handlers --- .../org/apache/dubbo/remoting/transport/netty4/NettyClient.java | 2 +- .../dubbo/remoting/transport/netty4/NettyConnectionClient.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java index bd4d89f5b70f..efaed1cee5f5 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java @@ -126,7 +126,7 @@ protected void initChannel(SocketChannel ch) throws Exception { int heartbeatInterval = UrlUtils.getHeartbeat(getUrl()); if (sslContext != null) { - ch.pipeline().addLast("negotiation", new SslClientTlsHandler(sslContext)); + ch.pipeline().addLast("negotiation", new SslClientTlsHandler(sslContext, getConnectAddress())); } NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this); diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConnectionClient.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConnectionClient.java index 8fcec6ddc1df..3ded5c3f2247 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConnectionClient.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConnectionClient.java @@ -92,7 +92,7 @@ protected void initChannel(SocketChannel ch) { NettySslContextOperator nettySslContextOperator = new NettySslContextOperator(); if (sslContext != null) { - pipeline.addLast("negotiation", new SslClientTlsHandler(sslContext)); + pipeline.addLast("negotiation", new SslClientTlsHandler(sslContext, getConnectAddress())); } // pipeline.addLast("logging", new LoggingHandler(LogLevel.INFO)); //for debug From bde410d88de6658657537f8b86e551a495a82470 Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:03:22 +0800 Subject: [PATCH 2/6] Create TLS engines with the remote host and port --- .../transport/netty4/ssl/SslClientTlsHandler.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/ssl/SslClientTlsHandler.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/ssl/SslClientTlsHandler.java index e1c08d3f0154..27e66c5d9620 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/ssl/SslClientTlsHandler.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/ssl/SslClientTlsHandler.java @@ -24,6 +24,8 @@ import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLSession; +import java.net.InetSocketAddress; + import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.ssl.SslContext; @@ -38,18 +40,26 @@ public class SslClientTlsHandler extends ChannelInboundHandlerAdapter { private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(SslClientTlsHandler.class); private static final AttributeKey SSL_SESSION_KEY = AttributeKey.valueOf(Constants.SSL_SESSION_KEY); private final SslContext sslContext; + private final InetSocketAddress peerAddress; public SslClientTlsHandler(URL url) { - this(SslContexts.buildClientSslContext(url)); + this(SslContexts.buildClientSslContext(url), url.toInetSocketAddress()); } public SslClientTlsHandler(SslContext sslContext) { + this(sslContext, null); + } + + public SslClientTlsHandler(SslContext sslContext, InetSocketAddress peerAddress) { this.sslContext = sslContext; + this.peerAddress = peerAddress; } @Override public void handlerAdded(ChannelHandlerContext ctx) { - SSLEngine sslEngine = sslContext.newEngine(ctx.alloc()); + SSLEngine sslEngine = peerAddress == null + ? sslContext.newEngine(ctx.alloc()) + : sslContext.newEngine(ctx.alloc(), peerAddress.getHostString(), peerAddress.getPort()); ctx.pipeline().addAfter(ctx.name(), null, new SslHandler(sslEngine, false)); } From b01a89286b1264bc48f9665146c9fd2e72b5f2a3 Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:04:09 +0800 Subject: [PATCH 3/6] Cover TCP TLS hostname verification --- .../ClientTlsHostnameVerificationTest.java | 171 ++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientTlsHostnameVerificationTest.java diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientTlsHostnameVerificationTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientTlsHostnameVerificationTest.java new file mode 100644 index 000000000000..76aead051a7a --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientTlsHostnameVerificationTest.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.remoting.transport.netty4; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.SslConfig; +import org.apache.dubbo.remoting.RemotingException; +import org.apache.dubbo.remoting.transport.AbstractClient; +import org.apache.dubbo.remoting.transport.ChannelHandlerAdapter; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.FrameworkModel; + +import javax.net.ssl.SSLException; + +import java.io.InputStream; +import java.net.InetSocketAddress; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.group.ChannelGroup; +import io.netty.channel.group.DefaultChannelGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.ssl.ClientAuth; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.SslHandshakeCompletionEvent; +import io.netty.handler.ssl.SslProvider; +import io.netty.util.concurrent.GlobalEventExecutor; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * Verifies hostname validation through both TCP client TLS pipelines. + * + * @date 2026-11-12 + */ +class ClientTlsHostnameVerificationTest { + + @TempDir + Path directory; + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void acceptsMatchingSubjectAlternativeName(boolean connectionClient) throws Exception { + assertHandshake(connectionClient, "server.pem", true); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void rejectsMismatchedSubjectAlternativeName(boolean connectionClient) throws Exception { + assertHandshake(connectionClient, "wrong-host.pem", false); + } + + private void assertHandshake(boolean connectionClient, String serverCertificate, boolean expectedSuccess) + throws Exception { + Path certificate = copyResource("client.pem"); + Path privateKey = copyResource("client.key"); + Path trust = copyResource("ca.pem"); + SslContext serverContext = SslContextBuilder.forServer( + copyResource(serverCertificate).toFile(), + copyResource("server.key").toFile()) + .trustManager(trust.toFile()) + .clientAuth(ClientAuth.REQUIRE) + .sslProvider(SslProvider.JDK) + .build(); + BlockingQueue handshakes = new LinkedBlockingQueue<>(); + EventLoopGroup group = new NioEventLoopGroup(1); + ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); + FrameworkModel frameworkModel = new FrameworkModel(); + AbstractClient client = null; + try { + Channel server = new ServerBootstrap() + .group(group) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel channel) { + channels.add(channel); + channel.pipeline().addLast(serverContext.newHandler(channel.alloc())); + channel.pipeline().addLast(new ChannelInboundHandlerAdapter() { + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object event) + throws Exception { + if (event instanceof SslHandshakeCompletionEvent) { + handshakes.add((SslHandshakeCompletionEvent) event); + } + super.userEventTriggered(ctx, event); + } + }); + } + }) + .bind("127.0.0.1", 0) + .sync() + .channel(); + channels.add(server); + ApplicationModel applicationModel = frameworkModel.newApplication(); + applicationModel.getApplicationConfigManager().setApplication(new ApplicationConfig("tls-hostname-test")); + SslConfig sslConfig = new SslConfig(); + sslConfig.setClientKeyCertChainPath(certificate.toString()); + sslConfig.setClientPrivateKeyPath(privateKey.toString()); + sslConfig.setClientTrustCertCollectionPath(trust.toString()); + applicationModel.getApplicationConfigManager().setSsl(sslConfig); + // Use mapped loopback so AbstractClient does not replace it with the machine address. + URL url = URL.valueOf("empty://[::ffff:127.0.0.1]:" + ((InetSocketAddress) server.localAddress()).getPort()) + .addParameter("codec", "telnet") + .setScopeModel(applicationModel.getDefaultModule()); + try { + client = connectionClient + ? new NettyConnectionClient(url, new ChannelHandlerAdapter()) + : new NettyClient(url, new ChannelHandlerAdapter()); + } catch (RemotingException exception) { + if (expectedSuccess) { + throw exception; + } + } + SslHandshakeCompletionEvent handshake = handshakes.poll(10, TimeUnit.SECONDS); + assertNotNull(handshake, "The TLS handshake did not complete"); + assertEquals(expectedSuccess, handshake.isSuccess(), () -> "TLS handshake: " + handshake.cause()); + if (!expectedSuccess) { + assertInstanceOf(SSLException.class, handshake.cause()); + } + } finally { + if (client != null) { + client.close(); + } + channels.close().awaitUninterruptibly(); + frameworkModel.destroy(); + group.shutdownGracefully(0, 5, TimeUnit.SECONDS).sync(); + } + } + + private Path copyResource(String name) throws Exception { + Path destination = directory.resolve(name); + try (InputStream stream = + ClientTlsHostnameVerificationTest.class.getResourceAsStream("/tls-hostname/" + name)) { + Files.copy(stream, destination); + } + return destination; + } +} From deab7bf5e54c363d697f802693fc9310fb321f6a Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:05:12 +0800 Subject: [PATCH 4/6] Add matching and mismatched TLS certificate fixtures --- .../src/test/resources/tls-hostname/ca.pem | 19 +++++++++++++ .../test/resources/tls-hostname/client.key | 28 +++++++++++++++++++ .../test/resources/tls-hostname/client.pem | 18 ++++++++++++ .../test/resources/tls-hostname/server.key | 28 +++++++++++++++++++ .../test/resources/tls-hostname/server.pem | 18 ++++++++++++ .../resources/tls-hostname/wrong-host.pem | 18 ++++++++++++ 6 files changed, 129 insertions(+) create mode 100644 dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/ca.pem create mode 100644 dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/client.key create mode 100644 dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/client.pem create mode 100644 dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/server.key create mode 100644 dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/server.pem create mode 100644 dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/wrong-host.pem diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/ca.pem b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/ca.pem new file mode 100644 index 000000000000..ca0e34143519 --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/ca.pem @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDBzCCAe+gAwIBAgIUE8rx3Vh8pCFm0a4RO/jdmtMzTJswDQYJKoZIhvcNAQEL +BQAwEjEQMA4GA1UEAwwHcXVpYy1jYTAgFw0yNDA4MjEwNjM3NTlaGA8yMDc0MDgw +OTA2Mzc1OVowEjEQMA4GA1UEAwwHcXVpYy1jYTCCASIwDQYJKoZIhvcNAQEBBQAD +ggEPADCCAQoCggEBANQY2D0FKsC4hzsPYWMP7Wb0SMxQDbsY0TGBR++Y4CwwrVet +lFNpFMk9TiNcUmj3Hpy6T5zsZx6CC3Phl8JFdfD/jm4OPQxd4o7yQpcqCU1+sHuB +8P0diUa5PHwfEnRfkq4wdmUZ/wv/Cn/S1WjCW62nwIQ8QKAeuxEs/wibeAZl+576 +fOi5q6XfcLncSfAranzOp9ETBkVM9IDl+RXX/Bc/QkiLGlRj4pCsnquAk0/r+m/E +fEkODANhp06CRY/S3khYaxi7nfNNIrwf/iU3mb2ZXJml/V6DQfoD0eQ3jeeGEGn/ +mExY0kATrTSoIo9Th+V1VVRxuSn0mKtI17Lrqj0CAwEAAaNTMFEwHQYDVR0OBBYE +FAQr8dTHZueMEw3eIEyMBYrTFjptMB8GA1UdIwQYMBaAFAQr8dTHZueMEw3eIEyM +BYrTFjptMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBALNTH7Hx +l2nUxn68AHagnzYBGgGzJDmGGZU5tNuzPSTUnCDe+6x527KQsRMJ8gLBbAYzef07 +98JK3SDKyl1AHus6oYBavtrE3D2vQXbCZSHR5BkdNsuwAxnqB+1qxfFCR3GUPry5 ++KocqHHG1kBApHeSYM43mAbZ2bfo9BAHAu/xq4JDc8Hdm6bwFkdMsvkOZJAVtEYi +m+ARw6XvWOsp9atRnhzfCBBhpayl6NtsnNth1wY/FuSmm2ZUlDVMsa3hjSnY0/E/ +aV9Mi1WvgGklc5efveGufoIYHy/twYwUtkYaz6Z1Z+TFoC2tTRjlXjESTN/ti+Un +XRMjTKn90G9c5bQ= +-----END CERTIFICATE----- diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/client.key b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/client.key new file mode 100644 index 000000000000..1297f49fd1ee --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/client.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC1jcPcCCtgysUO +YvtQkFU7OPAPUqP69M9pHvB61mGomrCfpeuzc2XcdnyDa/FBITH6Czd8p88eteXa ++LpgOcrU+W93ziOLHXwS1mtETOSxfzK+HQuwcUfhEGs/0HUc1wEfWeWldGaGri0C +OAtKI/vzqFaaspK+L4I++UvPO9aiypQ3WEwVlLgHnXckQl4fx8fi2HZ0wGKR7OoL +KbVITlKopN0QqRnuA1L8vohrOf8P51KioAWBDUsWJ54cnrla09NPEkEKu4DHz6aO +GJTRYttsXQwNIPUeq6z+4GzVFDih9sEI3rQFTQgXSLiUBLKqrjYnqF8vR8Yru3Jd +fvifyr1zAgMBAAECggEAMIC9rRhfol25lHTcKDNPGeHFvIo69c4WZUKXQ3HnqDgf +b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99npti07zUvNtgcbdszLfcpHvYSMI1Xpg+hZqC1BifIj +hAQdZIc3APnGeCghjemDg2NWa2yHcqkKNVkVB7647bgsKP1ewg0GFFLN17XiUfiv +6/NvZACAHJCj0YqQUpJ8dsEK +-----END PRIVATE KEY----- diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/client.pem b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/client.pem new file mode 100644 index 000000000000..80c8291a598f --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/client.pem @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC+jCCAeKgAwIBAgIUFrQFPMAmmmGTMmlyK4+r3aaCRw0wDQYJKoZIhvcNAQEL +BQAwEjEQMA4GA1UEAwwHcXVpYy1jYTAgFw0yNDA4MjEwNjM4MjZaGA8yMDc0MDgw +OTA2MzgyNlowFjEUMBIGA1UEAwwLcXVpYy1jbGllbnQwggEiMA0GCSqGSIb3DQEB +AQUAA4IBDwAwggEKAoIBAQC1jcPcCCtgysUOYvtQkFU7OPAPUqP69M9pHvB61mGo +mrCfpeuzc2XcdnyDa/FBITH6Czd8p88eteXa+LpgOcrU+W93ziOLHXwS1mtETOSx +fzK+HQuwcUfhEGs/0HUc1wEfWeWldGaGri0COAtKI/vzqFaaspK+L4I++UvPO9ai +ypQ3WEwVlLgHnXckQl4fx8fi2HZ0wGKR7OoLKbVITlKopN0QqRnuA1L8vohrOf8P +51KioAWBDUsWJ54cnrla09NPEkEKu4DHz6aOGJTRYttsXQwNIPUeq6z+4GzVFDih +9sEI3rQFTQgXSLiUBLKqrjYnqF8vR8Yru3Jdfvifyr1zAgMBAAGjQjBAMB0GA1Ud +DgQWBBQl6Ogm/VZ8q41hpdeUr7HgZY0mCDAfBgNVHSMEGDAWgBQEK/HUx2bnjBMN +3iBMjAWK0xY6bTANBgkqhkiG9w0BAQsFAAOCAQEAqD1x6VU7Ugm4Dk55dXu/Raf6 +yKWkKXzFnBukkdoaP/TO9ba/K4+l2jC7lNQ62ROTjZ0CQvRbO6Eg9eoil9bEa7Af +YayVG1zOJmpHzFg/XXYPSfqZXvEWiwxU6kSpx+eUtFx4pXVecqiySyAcQBge2aGh +0IOW86oqIcTVSA0l7ROY+vWoHOVZBvS2ujJ43z29k6/4SbmYJL6cLeqzgXnBwHKW +kND/LIUOd9OrHgP3CTiM0LjwFPxGg9C8OiUymmMqbX4D2gPj0z+al9Orr0VGMHgF +Sw4h3ovTCeU4nq+iwBc5gwI+U0BuwLFqE4emdz4s2CatWQQ8YcVCBG3eS+nHWQ== +-----END CERTIFICATE----- diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/server.key b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/server.key new file mode 100644 index 000000000000..7693b31ba0b0 --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/server.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC/wX9dQdJmAA1b +GCaLV09Fve9gLJc8/o9ERCpQyV2fCEJmWXYEzS1n8z0k3MAGxLF8pw98K6A8J7/6 +lWB0f4edsgMu05zUco/fu9nMIHnnVSEXVcEDxh1E9LcPCWKLyukPSJy1eW8VJtxA +R/sTyZUK6u7fWLQW/yp9KPdOvicv4ynHDv/S1BCUjH/N/bv8Lwc4a0U/QArlOSZ8 +CJfNWNV2gCtJtQLJZWK27qrLLMYO/A4ZgnJ79ssCaaO6KxXKmETW5y+Q3+aawLdT +0jrIcjwhmWAok7RgLeIYT1fK8QM2ON5y4efJIH116XK1B//Mcyx8ymunNcPpPPrx +MeT/ibmbAgMBAAECggEALacw3pAUOoqao2x9iMExs2YD/r6/BxuIGnWsKW23p2w8 ++scjzQLYY55KmWMhKLtdklLn4eU1Ef1YvJ0hyLqwHABMt7JT7VSVImxD71BwFX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QpuPBuaZvZHaFauAlpKiTDEU0Ggr2/bw0FXr2VXJ +PAlImidzzicjm0wIGBb5KA== +-----END PRIVATE KEY----- diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/server.pem b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/server.pem new file mode 100644 index 000000000000..3f04ea6a71b6 --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/server.pem @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC4zCCAcugAwIBAgIDFC4FMA0GCSqGSIb3DQEBCwUAMBIxEDAOBgNVBAMMB3F1 +aWMtY2EwIBcNMjQwODIxMDAwMDAwWhgPMjA3NDA4MDkwMDAwMDBaMBwxGjAYBgNV +BAMMEXRscy1ob3N0bmFtZS10ZXN0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB +CgKCAQEAv8F/XUHSZgANWxgmi1dPRb3vYCyXPP6PREQqUMldnwhCZll2BM0tZ/M9 +JNzABsSxfKcPfCugPCe/+pVgdH+HnbIDLtOc1HKP37vZzCB551UhF1XBA8YdRPS3 +Dwlii8rpD0ictXlvFSbcQEf7E8mVCuru31i0Fv8qfSj3Tr4nL+Mpxw7/0tQQlIx/ +zf27/C8HOGtFP0AK5TkmfAiXzVjVdoArSbUCyWVitu6qyyzGDvwOGYJye/bLAmmj +uisVyphE1ucvkN/mmsC3U9I6yHI8IZlgKJO0YC3iGE9XyvEDNjjecuHnySB9dely +tQf/zHMsfMprpzXD6Tz68THk/4m5mwIDAQABozYwNDAMBgNVHRMBAf8EAjAAMA8G +A1UdEQQIMAaHBH8AAAEwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQEL +BQADggEBAM3bcON1PGxZ9vOkEyO0Iaga8ivPCab0F1FXCkailYHPa4YMxe56zfzH +Y2pyuSQWNMcgNEQUr4CIqbOvrjITXlItvHx27EZWzb0QYzV79jrsf8YSmAhfL9tA +AuvIIzq1TxOVVYJAxVLRnfujrThdiePd2h7g01u+3i4gc9ctAz1SafNwXtdjsPkU +WePjkAc488v0H7CN33hP8SDuotYYZOQI/oQ/V0Ysp3tEYDTaubvBh4t9OmEYRu2e +X5xWg7P9PcPfsrhx803W6yjd49DBpkqdncxDegINhQyB/lBsqzMQHZEC5JrO/k9k +M/2RaaXdTBhynbvsxcfHCAiwVkHQZ7k= +-----END CERTIFICATE----- diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/wrong-host.pem b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/wrong-host.pem new file mode 100644 index 000000000000..d8504c00fbbf --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/wrong-host.pem @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC4zCCAcugAwIBAgIDFC4GMA0GCSqGSIb3DQEBCwUAMBIxEDAOBgNVBAMMB3F1 +aWMtY2EwIBcNMjQwODIxMDAwMDAwWhgPMjA3NDA4MDkwMDAwMDBaMBwxGjAYBgNV +BAMMEXRscy1ob3N0bmFtZS10ZXN0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB +CgKCAQEAv8F/XUHSZgANWxgmi1dPRb3vYCyXPP6PREQqUMldnwhCZll2BM0tZ/M9 +JNzABsSxfKcPfCugPCe/+pVgdH+HnbIDLtOc1HKP37vZzCB551UhF1XBA8YdRPS3 +Dwlii8rpD0ictXlvFSbcQEf7E8mVCuru31i0Fv8qfSj3Tr4nL+Mpxw7/0tQQlIx/ +zf27/C8HOGtFP0AK5TkmfAiXzVjVdoArSbUCyWVitu6qyyzGDvwOGYJye/bLAmmj +uisVyphE1ucvkN/mmsC3U9I6yHI8IZlgKJO0YC3iGE9XyvEDNjjecuHnySB9dely +tQf/zHMsfMprpzXD6Tz68THk/4m5mwIDAQABozYwNDAMBgNVHRMBAf8EAjAAMA8G +A1UdEQQIMAaHBH8AAAIwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQEL +BQADggEBAANMeMwwnNj4oEXdkHUKmN2pGxRnZoRcIJzLb5IjFSyiH4RHrZcBZfU9 +Qh62FbS3RHf5uZ4MsKeKG7cKgDR/vxVl90SKGEL6/hqrn8KwOSXHTaeP2BO0AQ1L +nIcIjrNBA+vfhx0Bbn05y9ZRL9eyRxgh5HYY92mko0WxgE2Ikqz2sLmTA7FknzSR +qfK8n+AXn0RpL5HXZT7BNSlQyME+TrZ+WQX0UEw5b9gjU0HyRFTt4OegIXENRuTA +UIGlyBbuV0OyPp7a/87JXt3Rn20iZJsnuYOMkT+5QbadKJg4v+lemxbNVcliPQpW +8kRqxXrKyG50oe2wzCNUlOd1Rx+Os9I= +-----END CERTIFICATE----- From 402ee0fe2f6a093e6a645166d5675fe0866c45ae Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:42:40 +0800 Subject: [PATCH 5/6] Exclude TLS hostname test certificates from license header checks --- .licenserc.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.licenserc.yaml b/.licenserc.yaml index d2f629f01a1f..123275decf5c 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -67,6 +67,8 @@ header: - 'Jenkinsfile' - '**/vendor/**' - '**/src/test/resources/certs/**' + - 'dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/*.pem' + - 'dubbo-remoting/dubbo-remoting-netty4/src/test/resources/tls-hostname/*.key' - '**/src/test/resources/definition/**' - 'dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocal.java' - 'dubbo-common/src/main/java/org/apache/dubbo/common/threadlocal/InternalThreadLocalMap.java' From a8dcd538cc2e3863ff0814cd5330842b11afdff7 Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:19:44 +0800 Subject: [PATCH 6/6] Tighten TCP TLS hostname regression coverage Assert the peer host and port and inspect the client's handshake failure for a certificate SAN mismatch. Delay the server handshake until the client handler has been captured, so a failed handshake cannot race with channel lookup. Add coverage for the URL and context-only SslClientTlsHandler constructors. Fixes apache/dubbo#16589 --- .../ClientTlsHostnameVerificationTest.java | 133 ++++++++++++++---- 1 file changed, 109 insertions(+), 24 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientTlsHostnameVerificationTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientTlsHostnameVerificationTest.java index 76aead051a7a..225ab784c14c 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientTlsHostnameVerificationTest.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ClientTlsHostnameVerificationTest.java @@ -19,9 +19,9 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.SslConfig; -import org.apache.dubbo.remoting.RemotingException; import org.apache.dubbo.remoting.transport.AbstractClient; import org.apache.dubbo.remoting.transport.ChannelHandlerAdapter; +import org.apache.dubbo.remoting.transport.netty4.ssl.SslClientTlsHandler; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.FrameworkModel; @@ -31,6 +31,7 @@ import java.net.InetSocketAddress; import java.nio.file.Files; import java.nio.file.Path; +import java.security.cert.CertificateException; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; @@ -40,7 +41,9 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; +import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.group.ChannelGroup; import io.netty.channel.group.DefaultChannelGroup; import io.netty.channel.nio.NioEventLoopGroup; @@ -49,9 +52,11 @@ import io.netty.handler.ssl.ClientAuth; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.SslHandshakeCompletionEvent; import io.netty.handler.ssl.SslProvider; import io.netty.util.concurrent.GlobalEventExecutor; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -59,6 +64,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * Verifies hostname validation through both TCP client TLS pipelines. @@ -82,10 +90,46 @@ void rejectsMismatchedSubjectAlternativeName(boolean connectionClient) throws Ex assertHandshake(connectionClient, "wrong-host.pem", false); } + @Test + void usesUrlAddressForLegacyConstructor() throws Exception { + FrameworkModel frameworkModel = new FrameworkModel(); + EmbeddedChannel channel = null; + try { + ApplicationModel applicationModel = configureClient(frameworkModel, copyResource("ca.pem")); + URL url = URL.valueOf("empty://localhost:20880").setScopeModel(applicationModel.getDefaultModule()); + channel = new EmbeddedChannel(new SslClientTlsHandler(url)); + SslHandler sslHandler = channel.pipeline().get(SslHandler.class); + assertNotNull(sslHandler); + assertEquals("localhost", sslHandler.engine().getPeerHost()); + assertEquals(20880, sslHandler.engine().getPeerPort()); + } finally { + try { + if (channel != null) { + channel.finishAndReleaseAll(); + } + } finally { + frameworkModel.destroy(); + } + } + } + + @Test + void preservesContextOnlyConstructor() throws Exception { + SslContext context = + SslContextBuilder.forClient().sslProvider(SslProvider.JDK).build(); + EmbeddedChannel channel = new EmbeddedChannel(new SslClientTlsHandler(context)); + try { + SslHandler sslHandler = channel.pipeline().get(SslHandler.class); + assertNotNull(sslHandler); + assertNull(sslHandler.engine().getPeerHost()); + assertEquals(-1, sslHandler.engine().getPeerPort()); + } finally { + channel.finishAndReleaseAll(); + } + } + private void assertHandshake(boolean connectionClient, String serverCertificate, boolean expectedSuccess) throws Exception { - Path certificate = copyResource("client.pem"); - Path privateKey = copyResource("client.key"); Path trust = copyResource("ca.pem"); SslContext serverContext = SslContextBuilder.forServer( copyResource(serverCertificate).toFile(), @@ -95,6 +139,7 @@ private void assertHandshake(boolean connectionClient, String serverCertificate, .sslProvider(SslProvider.JDK) .build(); BlockingQueue handshakes = new LinkedBlockingQueue<>(); + BlockingQueue acceptedChannels = new LinkedBlockingQueue<>(); EventLoopGroup group = new NioEventLoopGroup(1); ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); FrameworkModel frameworkModel = new FrameworkModel(); @@ -103,11 +148,12 @@ private void assertHandshake(boolean connectionClient, String serverCertificate, Channel server = new ServerBootstrap() .group(group) .channel(NioServerSocketChannel.class) + // Delay server TLS until the client's SslHandler has been captured. + .childOption(ChannelOption.AUTO_READ, false) .childHandler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel channel) { channels.add(channel); - channel.pipeline().addLast(serverContext.newHandler(channel.alloc())); channel.pipeline().addLast(new ChannelInboundHandlerAdapter() { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object event) @@ -118,38 +164,53 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object event) super.userEventTriggered(ctx, event); } }); + acceptedChannels.add(channel); } }) .bind("127.0.0.1", 0) .sync() .channel(); channels.add(server); - ApplicationModel applicationModel = frameworkModel.newApplication(); - applicationModel.getApplicationConfigManager().setApplication(new ApplicationConfig("tls-hostname-test")); - SslConfig sslConfig = new SslConfig(); - sslConfig.setClientKeyCertChainPath(certificate.toString()); - sslConfig.setClientPrivateKeyPath(privateKey.toString()); - sslConfig.setClientTrustCertCollectionPath(trust.toString()); - applicationModel.getApplicationConfigManager().setSsl(sslConfig); + ApplicationModel applicationModel = configureClient(frameworkModel, trust); // Use mapped loopback so AbstractClient does not replace it with the machine address. URL url = URL.valueOf("empty://[::ffff:127.0.0.1]:" + ((InetSocketAddress) server.localAddress()).getPort()) .addParameter("codec", "telnet") .setScopeModel(applicationModel.getDefaultModule()); - try { - client = connectionClient - ? new NettyConnectionClient(url, new ChannelHandlerAdapter()) - : new NettyClient(url, new ChannelHandlerAdapter()); - } catch (RemotingException exception) { - if (expectedSuccess) { - throw exception; - } - } - SslHandshakeCompletionEvent handshake = handshakes.poll(10, TimeUnit.SECONDS); - assertNotNull(handshake, "The TLS handshake did not complete"); - assertEquals(expectedSuccess, handshake.isSuccess(), () -> "TLS handshake: " + handshake.cause()); + client = connectionClient + ? new NettyConnectionClient(url, new ChannelHandlerAdapter()) + : new NettyClient(url, new ChannelHandlerAdapter()); + Channel nettyChannel = connectionClient + ? ((NettyConnectionClient) client).getChannel(Boolean.TRUE) + : ((NettyClient) client).getNettyChannel(); + assertNotNull(nettyChannel); + SslHandler clientTls = nettyChannel.pipeline().get(SslHandler.class); + assertNotNull(clientTls); + assertEquals( + client.getConnectAddress().getHostString(), + clientTls.engine().getPeerHost()); + assertEquals( + client.getConnectAddress().getPort(), clientTls.engine().getPeerPort()); + Channel accepted = acceptedChannels.poll(5, TimeUnit.SECONDS); + assertNotNull(accepted, "The server did not accept the TCP connection"); + accepted.eventLoop() + .submit(() -> { + accepted.pipeline().addFirst(serverContext.newHandler(accepted.alloc())); + accepted.config().setAutoRead(true); + }) + .sync(); + assertTrue( + clientTls.handshakeFuture().await(10, TimeUnit.SECONDS), + "The client TLS handshake did not complete"); + assertEquals( + expectedSuccess, + clientTls.handshakeFuture().isSuccess(), + () -> "Client TLS handshake: " + clientTls.handshakeFuture().cause()); if (!expectedSuccess) { - assertInstanceOf(SSLException.class, handshake.cause()); + assertSubjectAlternativeNameMismatch(clientTls.handshakeFuture().cause()); } + SslHandshakeCompletionEvent handshake = handshakes.poll(10, TimeUnit.SECONDS); + assertNotNull(handshake, "The server TLS handshake did not complete"); + assertEquals(expectedSuccess, handshake.isSuccess(), () -> "Server TLS handshake: " + handshake.cause()); } finally { if (client != null) { client.close(); @@ -160,6 +221,30 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object event) } } + private void assertSubjectAlternativeNameMismatch(Throwable cause) { + assertInstanceOf(SSLException.class, cause); + for (Throwable current = cause; current != null; current = current.getCause()) { + if (current instanceof CertificateException + && current.getMessage() != null + && current.getMessage().contains("subject alternative") + && current.getMessage().contains("127.0.0.1")) { + return; + } + } + fail("Expected a certificate SAN mismatch for 127.0.0.1", cause); + } + + private ApplicationModel configureClient(FrameworkModel frameworkModel, Path trust) throws Exception { + ApplicationModel applicationModel = frameworkModel.newApplication(); + applicationModel.getApplicationConfigManager().setApplication(new ApplicationConfig("tls-hostname-test")); + SslConfig sslConfig = new SslConfig(); + sslConfig.setClientKeyCertChainPath(copyResource("client.pem").toString()); + sslConfig.setClientPrivateKeyPath(copyResource("client.key").toString()); + sslConfig.setClientTrustCertCollectionPath(trust.toString()); + applicationModel.getApplicationConfigManager().setSsl(sslConfig); + return applicationModel; + } + private Path copyResource(String name) throws Exception { Path destination = directory.resolve(name); try (InputStream stream =