Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sni #331

Merged
merged 5 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void initWebSocketMqtt(
private void initSsl(final @NotNull Channel channel, final @NotNull MqttClientSslConfigImpl sslConfig)
throws SSLException {

SslUtil.initChannel(channel, sslConfig);
SslUtil.initChannel(channel, sslConfig, clientConfig.getServerHost(), clientConfig.getServerPort());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,48 +33,41 @@ public final class SslUtil {

private static final @NotNull String SSL_HANDLER_NAME = "ssl";

public static void initChannel(final @NotNull Channel channel, final @NotNull MqttClientSslConfigImpl sslConfig)
throws SSLException {
public static void initChannel(
final @NotNull Channel channel, final @NotNull MqttClientSslConfigImpl sslConfig,
final @NotNull String host, final int port) throws SSLException {

channel.pipeline().addFirst(SSL_HANDLER_NAME, createSslHandler(channel, sslConfig));
channel.pipeline().addFirst(SSL_HANDLER_NAME, createSslHandler(channel, sslConfig, host, port));
}

private static @NotNull SslHandler createSslHandler(
final @NotNull Channel channel, final @NotNull MqttClientSslConfigImpl sslConfig) throws SSLException {
final @NotNull Channel channel, final @NotNull MqttClientSslConfigImpl sslConfig,
final @NotNull String host, final int port) throws SSLException {

final SSLEngine sslEngine = createSslEngine(channel, sslConfig);
final SslHandler sslHandler = new SslHandler(sslEngine);

sslHandler.setHandshakeTimeoutMillis(sslConfig.getHandshakeTimeoutMs());
return sslHandler;
}

static @NotNull SSLEngine createSslEngine(
final @NotNull Channel channel, final @NotNull MqttClientSslConfigImpl sslConfig) throws SSLException {

final SSLEngine sslEngine = createSslContext(sslConfig).newEngine(channel.alloc());

sslEngine.setUseClientMode(true);

return sslEngine;
return createSslContext(sslConfig).newHandler(channel.alloc(), host, port);
}

private static @NotNull SslContext createSslContext(final @NotNull MqttClientSslConfigImpl sslConfig)
throws SSLException {

final SslContextBuilder sslContextBuilder = SslContextBuilder.forClient()
.sslProvider(SslProvider.JDK)
.trustManager(sslConfig.getRawTrustManagerFactory())
.keyManager(sslConfig.getRawKeyManagerFactory());
static @NotNull SslContext createSslContext(final @NotNull MqttClientSslConfigImpl sslConfig) throws SSLException {

final ImmutableList<String> protocols = sslConfig.getRawProtocols();
//noinspection ToArrayCallWithZeroLengthArrayArgument
final String[] protocolArray = (protocols == null) ? null : protocols.toArray(new String[protocols.size()]);
sslContextBuilder.protocols(protocolArray);

sslContextBuilder.ciphers(sslConfig.getRawCipherSuites(), SupportedCipherSuiteFilter.INSTANCE);

return sslContextBuilder.build();
final SslContext sslContext = SslContextBuilder.forClient()
.trustManager(sslConfig.getRawTrustManagerFactory())
.keyManager(sslConfig.getRawKeyManagerFactory())
.protocols((protocols == null) ? null : protocols.toArray(new String[0]))
.ciphers(sslConfig.getRawCipherSuites(), SupportedCipherSuiteFilter.INSTANCE)
.build();

return new DelegatingSslContext(sslContext) {
@Override
protected void initEngine(@NotNull final SSLEngine engine) {
}

@Override
protected void initHandler(@NotNull final SslHandler handler) {
handler.setHandshakeTimeoutMillis(sslConfig.getHandshakeTimeoutMs());
}
};
}

private SslUtil() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

package com.hivemq.client.internal.mqtt.handler.ssl;

import com.hivemq.client.internal.mqtt.MqttClientSslConfigImpl;
import com.hivemq.client.internal.mqtt.MqttClientSslConfigImplBuilder;
import com.hivemq.client.internal.util.collections.ImmutableList;
import io.netty.channel.Channel;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslProvider;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Test;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManagerFactory;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -50,7 +55,7 @@ public void test_createSslEngine_null_values() throws Exception {

final TrustManagerFactory tmf = null;

final SSLEngine sslEngine = SslUtil.createSslEngine(embeddedChannel,
final SSLEngine sslEngine = createSslEngine(embeddedChannel,
new MqttClientSslConfigImplBuilder.Default().trustManagerFactory(tmf).build());

assertNotNull(sslEngine);
Expand All @@ -66,7 +71,7 @@ public void test_createSslEngine_cipher_suite() throws Exception {

final ImmutableList<String> cipherSuite = getFirstSupportedCipherSuite();

final SSLEngine sslEngine = SslUtil.createSslEngine(embeddedChannel,
final SSLEngine sslEngine = createSslEngine(embeddedChannel,
new MqttClientSslConfigImplBuilder.Default().trustManagerFactory(tmf)
.cipherSuites(cipherSuite)
.build());
Expand All @@ -86,7 +91,7 @@ public void test_createSslEngine_multiple_cipher_suites() throws Exception {

final ImmutableList<String> cipherSuites = getOtherSupportedCipherSuites();

final SSLEngine sslEngine = SslUtil.createSslEngine(embeddedChannel,
final SSLEngine sslEngine = createSslEngine(embeddedChannel,
new MqttClientSslConfigImplBuilder.Default().trustManagerFactory(tmf)
.cipherSuites(cipherSuites)
.build());
Expand All @@ -109,7 +114,7 @@ public void test_createSslEngine_protocol() throws Exception {

final ImmutableList<String> protocol = ImmutableList.of("TLSv1");

final SSLEngine sslEngine = SslUtil.createSslEngine(embeddedChannel,
final SSLEngine sslEngine = createSslEngine(embeddedChannel,
new MqttClientSslConfigImplBuilder.Default().trustManagerFactory(tmf).protocols(protocol).build());

assertNotNull(sslEngine);
Expand All @@ -127,7 +132,7 @@ public void test_createSslEngine_multiple_protocols() throws Exception {

final ImmutableList<String> protocols = ImmutableList.of("TLSv1.1", "TLSv1.2");

final SSLEngine sslEngine = SslUtil.createSslEngine(embeddedChannel,
final SSLEngine sslEngine = createSslEngine(embeddedChannel,
new MqttClientSslConfigImplBuilder.Default().trustManagerFactory(tmf).protocols(protocols).build());

assertNotNull(sslEngine);
Expand Down Expand Up @@ -161,10 +166,11 @@ public void test_createSslEngine_multiple_protocols() throws Exception {
}

private List<String> getEnabledCipherSuites() throws Exception {
final SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
final SSLEngine sslEngine = context.createSSLEngine();
return Arrays.asList(sslEngine.getEnabledCipherSuites());
return Arrays.asList(SslContextBuilder.forClient()
.sslProvider(SslProvider.JDK)
.build()
.newEngine(new EmbeddedChannel().alloc())
.getEnabledCipherSuites());
}

private List<String> getEnabledProtocols() throws Exception {
Expand All @@ -173,4 +179,10 @@ private List<String> getEnabledProtocols() throws Exception {
final SSLEngine sslEngine = context.createSSLEngine();
return Arrays.asList(sslEngine.getEnabledProtocols());
}

static @NotNull SSLEngine createSslEngine(
final @NotNull Channel channel, final @NotNull MqttClientSslConfigImpl sslConfig) throws SSLException {

return SslUtil.createSslContext(sslConfig).newEngine(channel.alloc());
}
}