Skip to content

Commit ca6e498

Browse files
committed
Make hostname verification compatible with Android API 24 and 25
1 parent 3e7e3fc commit ca6e498

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

src/main/java/com/hivemq/client/internal/mqtt/handler/ssl/MqttSslInitializer.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
public final class MqttSslInitializer {
4242

4343
private static final @NotNull String SSL_HANDLER_NAME = "ssl";
44+
private static final @NotNull String ENDPOINT_IDENTIFICATION_ALGORITHM = "HTTPS";
4445

4546
public static void initChannel(
4647
final @NotNull Channel channel,
@@ -76,15 +77,23 @@ Netty treats Android (all versions) as Java 6, so SSLParameters.setEndpointIdent
7677
if (hostnameVerifier == null) {
7778
final SSLParameters sslParameters = sslHandler.engine().getSSLParameters();
7879
try {
79-
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
80+
sslParameters.setEndpointIdentificationAlgorithm(ENDPOINT_IDENTIFICATION_ALGORITHM);
81+
sslHandler.engine().setSSLParameters(sslParameters);
82+
if (!ENDPOINT_IDENTIFICATION_ALGORITHM.equals(
83+
sslHandler.engine().getSSLParameters().getEndpointIdentificationAlgorithm())) {
84+
/*
85+
On Android API 24 and 25 SSLParameters.setEndpointIdentificationAlgorithm is available but the call is ignored
86+
The HttpsURLConnection.getDefaultHostnameVerifier performs HTTPS hostname verification on Android
87+
*/
88+
hostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
89+
}
8090
} catch (final NoSuchMethodError e) {
8191
/*
8292
On Android API < 24 SSLParameters.setEndpointIdentificationAlgorithm is not available
8393
The HttpsURLConnection.getDefaultHostnameVerifier performs HTTPS hostname verification on Android
8494
*/
8595
hostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
8696
}
87-
sslHandler.engine().setSSLParameters(sslParameters);
8897
}
8998

9099
final MqttSslAdapterHandler sslAdapterHandler =
@@ -102,7 +111,8 @@ Netty treats Android (all versions) as Java 6, so SSLParameters.setEndpointIdent
102111
.keyManager(sslConfig.getRawKeyManagerFactory())
103112
.protocols((protocols == null) ? null : protocols.toArray(new String[0]))
104113
.ciphers(sslConfig.getRawCipherSuites(), SupportedCipherSuiteFilter.INSTANCE)
105-
.endpointIdentificationAlgorithm((sslConfig.getRawHostnameVerifier() == null) ? "HTTPS" : null)
114+
.endpointIdentificationAlgorithm(
115+
(sslConfig.getRawHostnameVerifier() == null) ? ENDPOINT_IDENTIFICATION_ALGORITHM : null)
106116
.build();
107117
}
108118

0 commit comments

Comments
 (0)