Browse Source

Keep client TLS hostname verification off under Netty 4.2

pull/16341/head
Viacheslav Klimov 2 months ago
parent
commit
2cc4f7b2e0
Failed to extract signature
  1. 2
      common/integration/http-integrations/src/main/java/org/thingsboard/integration/http/loriot/LoriotIntegration.java
  2. 2
      common/integration/http-integrations/src/main/java/org/thingsboard/integration/http/thingpark/ThingParkIntegration.java
  3. 2
      common/integration/mqtt-integrations/src/main/java/org/thingsboard/integration/mqtt/AbstractMqttIntegration.java
  4. 1
      common/integration/mqtt-integrations/src/main/java/org/thingsboard/integration/mqtt/azure/AzureIotHubSasCredentials.java
  5. 1
      common/integration/mqtt-integrations/src/main/java/org/thingsboard/integration/mqtt/credentials/CertPemClientCredentials.java
  6. 1
      common/integration/mqtt-integrations/src/main/java/org/thingsboard/integration/mqtt/ttn/TtnIntegration.java
  7. 2
      common/report/src/main/java/org/thingsboard/server/report/util/WebReportClient.java
  8. 151
      netty-mqtt/src/test/java/org/thingsboard/mqtt/MqttClientTlsTest.java
  9. 19
      netty-mqtt/src/test/resources/mismatched-host-cert.pem
  10. 28
      netty-mqtt/src/test/resources/mismatched-host-key.pem
  11. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/CertPemCredentials.java
  12. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/ClientCredentials.java
  13. 1
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/azure/AzureIotHubSasCredentials.java
  14. 2
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/mqtt/TbMqttNodeTest.java

2
common/integration/http-integrations/src/main/java/org/thingsboard/integration/http/loriot/LoriotIntegration.java

@ -74,7 +74,7 @@ public class LoriotIntegration extends BasicHttpIntegration<JsonHttpIntegrationM
if (loriotConfiguration.isSendDownlink()) {
HttpClient httpClient = HttpClient.create()
.runOn(context.getEventLoopGroup());
SslContext sslContext = SslContextBuilder.forClient().build();
SslContext sslContext = SslContextBuilder.forClient().endpointIdentificationAlgorithm(null).build();
httpClient.secure(t -> t.sslContext(sslContext));
this.webClient = WebClient.builder()

2
common/integration/http-integrations/src/main/java/org/thingsboard/integration/http/thingpark/ThingParkIntegration.java

@ -71,7 +71,7 @@ public class ThingParkIntegration extends AbstractHttpIntegration<ThingParkInteg
HttpClient httpClient = HttpClient.create()
.runOn(context.getEventLoopGroup());
SslContext sslContext = SslContextBuilder.forClient().build();
SslContext sslContext = SslContextBuilder.forClient().endpointIdentificationAlgorithm(null).build();
httpClient.secure(t -> t.sslContext(sslContext));
this.webClient = WebClient.builder()

2
common/integration/mqtt-integrations/src/main/java/org/thingsboard/integration/mqtt/AbstractMqttIntegration.java

@ -249,7 +249,7 @@ public abstract class AbstractMqttIntegration<T extends MqttIntegrationMsg> exte
protected Optional<SslContext> initSslContext(MqttClientConfiguration configuration) throws SSLException {
Optional<SslContext> result = configuration.getCredentials().initSslContext();
if (configuration.isSsl() && result.isEmpty()) {
result = Optional.of(SslContextBuilder.forClient().build());
result = Optional.of(SslContextBuilder.forClient().endpointIdentificationAlgorithm(null).build());
}
return result;
}

1
common/integration/mqtt-integrations/src/main/java/org/thingsboard/integration/mqtt/azure/AzureIotHubSasCredentials.java

@ -39,6 +39,7 @@ public class AzureIotHubSasCredentials implements MqttClientCredentials {
caCert = AzureIotHubUtil.getDefaultCaCert();
}
return Optional.of(SslContextBuilder.forClient()
.endpointIdentificationAlgorithm(null)
.trustManager(createAndInitTrustManagerFactory())
.clientAuth(ClientAuth.REQUIRE)
.build());

1
common/integration/mqtt-integrations/src/main/java/org/thingsboard/integration/mqtt/credentials/CertPemClientCredentials.java

@ -52,6 +52,7 @@ public class CertPemClientCredentials implements MqttClientCredentials {
try {
Security.addProvider(new BouncyCastleProvider());
return Optional.of(SslContextBuilder.forClient()
.endpointIdentificationAlgorithm(null)
.keyManager(createAndInitKeyManagerFactory())
.trustManager(createAndInitTrustManagerFactory())
.clientAuth(ClientAuth.REQUIRE)

1
common/integration/mqtt-integrations/src/main/java/org/thingsboard/integration/mqtt/ttn/TtnIntegration.java

@ -59,6 +59,7 @@ public class TtnIntegration extends BasicMqttIntegration {
if (configuration.isSsl()) {
Security.addProvider(new BouncyCastleProvider());
return Optional.of(SslContextBuilder.forClient()
.endpointIdentificationAlgorithm(null)
.keyManager((KeyManagerFactory) null)
.trustManager((File) null)
.clientAuth(ClientAuth.NONE)

2
common/report/src/main/java/org/thingsboard/server/report/util/WebReportClient.java

@ -67,7 +67,7 @@ public class WebReportClient {
.runOn(eventLoopGroup)
.secure(t -> {
try {
t.sslContext(SslContextBuilder.forClient().build());
t.sslContext(SslContextBuilder.forClient().endpointIdentificationAlgorithm(null).build());
} catch (SSLException e) {
throw new RuntimeException(e);
}

151
netty-mqtt/src/test/java/org/thingsboard/mqtt/MqttClientTlsTest.java

@ -0,0 +1,151 @@
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
// SPDX-License-Identifier: Apache-2.0
package org.thingsboard.mqtt;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.mqtt.MqttConnAckMessage;
import io.netty.handler.codec.mqtt.MqttConnectReturnCode;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageBuilders;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.thingsboard.common.util.AbstractListeningExecutor;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
class MqttClientTlsTest {
private EventLoopGroup serverGroup;
private Channel serverChannel;
private CompletableFuture<Future<Channel>> serverHandshake;
private AbstractListeningExecutor handlerExecutor;
private MqttClient client;
@BeforeEach
void setup() throws Exception {
SslContext serverSslContext;
try (InputStream cert = resource("mismatched-host-cert.pem"); InputStream key = resource("mismatched-host-key.pem")) {
serverSslContext = SslContextBuilder.forServer(cert, key).build();
}
serverHandshake = new CompletableFuture<>();
serverGroup = new NioEventLoopGroup(1);
serverChannel = new ServerBootstrap()
.group(serverGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
SslHandler sslHandler = serverSslContext.newHandler(ch.alloc());
serverHandshake.complete(sslHandler.handshakeFuture());
ch.pipeline().addLast(sslHandler, new MqttDecoder(), MqttEncoder.INSTANCE, new ConnAckHandler());
}
})
.bind("localhost", 0).sync().channel();
handlerExecutor = new AbstractListeningExecutor() {
@Override
protected int getThreadPollSize() {
return 1;
}
};
handlerExecutor.init();
}
@AfterEach
void cleanup() {
if (client != null) {
client.disconnect();
}
handlerExecutor.destroy();
serverChannel.close().syncUninterruptibly();
serverGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS).syncUninterruptibly();
}
@Test
void connectsOverTlsWhenCertificateNameDoesNotMatchHostAndEndpointIdentificationIsOff() throws Exception {
SslContext clientSslContext;
try (InputStream cert = resource("mismatched-host-cert.pem")) {
clientSslContext = SslContextBuilder.forClient().endpointIdentificationAlgorithm(null).trustManager(cert).build();
}
Promise<MqttConnectResult> connectFuture = connect(clientSslContext);
Awaitility.await("waiting for client to connect")
.atMost(Duration.ofSeconds(10))
.until(connectFuture::isDone);
assertThat(connectFuture.isSuccess()).isTrue();
assertThat(connectFuture.getNow().getReturnCode()).isEqualTo(MqttConnectReturnCode.CONNECTION_ACCEPTED);
assertThat(client.isConnected()).isTrue();
}
@Test
void rejectsCertificateNameMismatchWithNettyDefaultEndpointIdentification() throws Exception {
SslContext clientSslContext;
try (InputStream cert = resource("mismatched-host-cert.pem")) {
clientSslContext = SslContextBuilder.forClient().trustManager(cert).build();
}
connect(clientSslContext);
Future<Channel> handshake = serverHandshake.get(10, TimeUnit.SECONDS);
Awaitility.await("waiting for server handshake to finish")
.atMost(Duration.ofSeconds(10))
.until(handshake::isDone);
assertThat(handshake.isSuccess()).isFalse();
assertThat(client.isConnected()).isFalse();
}
private Promise<MqttConnectResult> connect(SslContext clientSslContext) {
MqttClientConfig config = new MqttClientConfig(clientSslContext);
config.setOwnerId("Test[Tls]");
config.setClientId("tls");
config.setReconnect(false);
client = MqttClient.create(config, null, handlerExecutor);
int port = ((InetSocketAddress) serverChannel.localAddress()).getPort();
return client.connect("localhost", port);
}
private static InputStream resource(String name) {
return MqttClientTlsTest.class.getClassLoader().getResourceAsStream(name);
}
private static class ConnAckHandler extends SimpleChannelInboundHandler<MqttMessage> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, MqttMessage msg) {
if (msg.fixedHeader().messageType() == MqttMessageType.CONNECT) {
MqttConnAckMessage connAck = MqttMessageBuilders.connAck()
.returnCode(MqttConnectReturnCode.CONNECTION_ACCEPTED)
.sessionPresent(false)
.build();
ctx.writeAndFlush(connAck);
}
}
}
}

19
netty-mqtt/src/test/resources/mismatched-host-cert.pem

@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDJzCCAg+gAwIBAgIUZh0cw6RBfrfnG9H3yz/2MNm6syQwDQYJKoZIhvcNAQEL
BQAwIjEgMB4GA1UEAwwXbWlzbWF0Y2hlZC1ob3N0LmludmFsaWQwIBcNMjYxMDE1
MDg0NTU3WhgPMjEyNjA5MjEwODQ1NTdaMCIxIDAeBgNVBAMMF21pc21hdGNo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aC0XM7j5DTiTkRjT7f1TT1dmtjA
cMiCN4bvI4cWUamv/FuvYQCLSutj638cgBh3t+sGAJMSnKuafbeJRacCGypEXtLy
SV8G+schjB5gbcKIojgKrk/jdQv4lptIiZ4ksSJGUcXJBpbIw278nxZVAoGAAiRQ
iEWgsAfHgkuDqJjgl2ugmOktAGlpGmKQEmZxUHfwVSyIkerOI5in0wQyqxiTkbQo
VPEpU1qJmso2qliqMME9zyqFgWHej22TXL//9/ADAadu5jHexNnXAJmLbXDTSWC/
w8eEbjNz4vyewtnYYPHcrzksol6mSvxpKz7LLVECgYB7u1UROLePGqZtvwwezO06
0BnMKBVptULLU5bqCu1nFDlqf76pJupjBoL9P1ZbluPI0EhHK3QmlaE2pyC4z/ha
976FbyZGetl3y/wrxru6zUKKaHS/PFlMwSxwFsnAp+ytplNLUqxmnCdY1ydzEIGr
heiiG7mwv7c03YOk+0C37A==
-----END PRIVATE KEY-----

2
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/CertPemCredentials.java

@ -45,7 +45,7 @@ public class CertPemCredentials implements ClientCredentials {
@Override
public SslContext initSslContext() {
try {
SslContextBuilder builder = SslContextBuilder.forClient();
SslContextBuilder builder = SslContextBuilder.forClient().endpointIdentificationAlgorithm(null);
if (StringUtils.hasLength(caCert)) {
builder.trustManager(createAndInitTrustManagerFactory());
}

2
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/ClientCredentials.java

@ -23,6 +23,6 @@ public interface ClientCredentials {
@JsonIgnore
default SslContext initSslContext() throws SSLException{
return SslContextBuilder.forClient().build();
return SslContextBuilder.forClient().endpointIdentificationAlgorithm(null).build();
}
}

1
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/azure/AzureIotHubSasCredentials.java

@ -31,6 +31,7 @@ public class AzureIotHubSasCredentials extends CertPemCredentials {
caCert = AzureIotHubUtil.getDefaultCaCert();
}
return SslContextBuilder.forClient()
.endpointIdentificationAlgorithm(null)
.trustManager(createAndInitTrustManagerFactory())
.clientAuth(ClientAuth.REQUIRE)
.build();

2
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/mqtt/TbMqttNodeTest.java

@ -172,7 +172,7 @@ public class TbMqttNodeTest extends AbstractRuleNodeUpgradeTest {
.usingRecursiveComparison()
.ignoringFields("ctx", "ctxLock", "sessionContext.context.ctx", "sessionContext.context.ctxLock",
"sslContext")
.isEqualTo(SslContextBuilder.forClient().build());
.isEqualTo(SslContextBuilder.forClient().endpointIdentificationAlgorithm(null).build());
}
@Test

Loading…
Cancel
Save