Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -21,7 +21,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.net.ssl.*;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -32,21 +34,8 @@
*/
public class MqttClientSslConfigImpl implements MqttClientSslConfig {

static final @Nullable HostnameVerifier DEFAULT_HOSTNAME_VERIFIER;

static {
HostnameVerifier hostnameVerifier = null;
try {
new SSLParameters().setEndpointIdentificationAlgorithm("HTTPS");
} catch (final NoSuchMethodError e) { // Android API < 24 compatibility
hostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
}
DEFAULT_HOSTNAME_VERIFIER = hostnameVerifier;
}

static final @NotNull MqttClientSslConfigImpl DEFAULT =
new MqttClientSslConfigImpl(null, null, null, null, (int) DEFAULT_HANDSHAKE_TIMEOUT_MS,
DEFAULT_HOSTNAME_VERIFIER);
new MqttClientSslConfigImpl(null, null, null, null, (int) DEFAULT_HANDSHAKE_TIMEOUT_MS, null);

private final @Nullable KeyManagerFactory keyManagerFactory;
private final @Nullable TrustManagerFactory trustManagerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class MqttClientSslConfigImplBuilder<B extends MqttClientSslConf
private @Nullable ImmutableList<String> cipherSuites;
private @Nullable ImmutableList<String> protocols;
private int handshakeTimeoutMs = (int) MqttClientSslConfigImpl.DEFAULT_HANDSHAKE_TIMEOUT_MS;
private @Nullable HostnameVerifier hostnameVerifier = MqttClientSslConfigImpl.DEFAULT_HOSTNAME_VERIFIER;
private @Nullable HostnameVerifier hostnameVerifier;

MqttClientSslConfigImplBuilder() {}

Expand Down Expand Up @@ -84,8 +84,7 @@ public abstract class MqttClientSslConfigImplBuilder<B extends MqttClientSslConf
}

public @NotNull B hostnameVerifier(final @Nullable HostnameVerifier hostnameVerifier) {
this.hostnameVerifier =
(hostnameVerifier == null) ? MqttClientSslConfigImpl.DEFAULT_HOSTNAME_VERIFIER : hostnameVerifier;
this.hostnameVerifier = hostnameVerifier;
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jetbrains.annotations.NotNull;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLParameters;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -65,10 +66,24 @@ public static void initChannel(

sslHandler.setHandshakeTimeoutMillis(sslConfig.getHandshakeTimeoutMs());

final HostnameVerifier hostnameVerifier = sslConfig.getRawHostnameVerifier();
/*
SSLParameters.setEndpointIdentificationAlgorithm is called by netty because we called SslContextBuilder.endpointIdentificationAlgorithm.
In Netty 4.1, this call is guarded by a Java version >= 7 check.
Netty treats Android (all versions) as Java 6, so SSLParameters.setEndpointIdentificationAlgorithm is not called on Android with netty 4.1.
So SSLParameters.setEndpointIdentificationAlgorithm still needs to be called here.
*/
HostnameVerifier hostnameVerifier = sslConfig.getRawHostnameVerifier();
if (hostnameVerifier == null) {
final SSLParameters sslParameters = sslHandler.engine().getSSLParameters();
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
try {
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
} catch (final NoSuchMethodError e) {
/*
On Android API < 24 SSLParameters.setEndpointIdentificationAlgorithm is not available
The HttpsURLConnection.getDefaultHostnameVerifier performs HTTPS hostname verification on Android
*/
hostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
}
sslHandler.engine().setSSLParameters(sslParameters);
}

Expand All @@ -87,6 +102,7 @@ public static void initChannel(
.keyManager(sslConfig.getRawKeyManagerFactory())
.protocols((protocols == null) ? null : protocols.toArray(new String[0]))
.ciphers(sslConfig.getRawCipherSuites(), SupportedCipherSuiteFilter.INSTANCE)
.endpointIdentificationAlgorithm((sslConfig.getRawHostnameVerifier() == null) ? "HTTPS" : null)
.build();
}

Expand Down